1 /*
2  * Copyright (C) 2015-2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <lk/reflist.h>
20 #include <sys/types.h>
21 
22 #include "block_device.h"
23 
24 struct block_mac;
25 struct fs;
26 struct iv;
27 struct transaction;
28 
29 enum block_read_error {
30     BLOCK_READ_SUCCESS = 0,
31     BLOCK_READ_IO_ERROR,
32     BLOCK_READ_NO_DATA,
33 };
34 
35 void block_cache_complete_read(struct block_device* dev,
36                                data_block_t block,
37                                const void* data,
38                                size_t data_size,
39                                enum block_read_error res);
40 
41 enum block_write_error {
42     BLOCK_WRITE_SUCCESS = 0,
43     BLOCK_WRITE_FAILED,
44     BLOCK_WRITE_FAILED_UNKNOWN_STATE,
45     BLOCK_WRITE_SYNC_FAILED,
46 };
47 
48 void block_cache_complete_write(struct block_device* dev,
49                                 data_block_t block,
50                                 enum block_write_error res);
51 
52 void block_cache_init(void);
53 
54 void block_cache_dev_destroy(struct block_device* dev);
55 
56 void block_cache_clean_transaction(struct transaction* tr);
57 
58 void block_cache_discard_transaction(struct transaction* tr, bool discard_all);
59 
60 const void* block_get_no_read(struct transaction* tr,
61                               data_block_t block,
62                               struct obj_ref* ref);
63 
64 const void* block_get_super(struct fs* fs,
65                             data_block_t block,
66                             struct obj_ref* ref);
67 
68 const void* block_get_no_tr_fail(struct transaction* tr,
69                                  const struct block_mac* block_mac,
70                                  const struct iv* iv,
71                                  struct obj_ref* ref);
72 
73 const void* block_get(struct transaction* tr,
74                       const struct block_mac* block_mac,
75                       const struct iv* iv,
76                       struct obj_ref* ref);
77 
78 void* block_dirty(struct transaction* tr, const void* data, bool is_tmp);
79 
80 bool block_is_clean(struct block_device* dev, data_block_t block);
81 
82 void block_discard_dirty(const void* data);
83 
84 void block_discard_dirty_by_block(struct block_device* dev, data_block_t block);
85 
86 void block_put_dirty(struct transaction* tr,
87                      void* data,
88                      struct obj_ref* data_ref,
89                      struct block_mac* block_mac,
90                      void* block_mac_ref);
91 
92 void block_put_dirty_no_mac(void* data,
93                             struct obj_ref* data_ref,
94                             bool allow_tampering);
95 
96 void block_put_dirty_discard(void* data, struct obj_ref* data_ref);
97 
98 void* block_get_write_no_read(struct transaction* tr,
99                               data_block_t block,
100                               bool is_tmp,
101                               struct obj_ref* ref);
102 
103 void* block_get_write(struct transaction* tr,
104                       const struct block_mac* block_mac,
105                       const struct iv* iv,
106                       bool is_tmp,
107                       struct obj_ref* ref);
108 
109 void* block_get_cleared(struct transaction* tr,
110                         data_block_t block,
111                         bool is_tmp,
112                         struct obj_ref* ref);
113 
114 void* block_get_cleared_super(struct transaction* tr,
115                               data_block_t block,
116                               struct obj_ref* ref,
117                               bool pinned);
118 
119 void* block_move(struct transaction* tr,
120                  const void* data,
121                  data_block_t block,
122                  bool is_tmp);
123 
124 void* block_get_copy(struct transaction* tr,
125                      const void* data,
126                      data_block_t block,
127                      bool is_tmp,
128                      struct obj_ref* new_ref);
129 
130 void block_put(const void* data, struct obj_ref* ref);
131 
132 bool block_probe(struct fs* fs,
133                  const struct block_mac* block_mac,
134                  bool allow_invalid);
135 
136 data_block_t data_to_block_num(const void* data); /* test api, remove ? */
137 
138 unsigned int block_cache_debug_get_ref_block_count(void);
139