This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_map.c
244 lines (217 loc) · 7.46 KB
/
block_map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) 2015-2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "block_allocator.h"
#include "block_map.h"
#include "block_tree.h"
#include "debug.h"
#include "transaction.h"
static bool print_block_map;
/**
* block_map_init - Initialize in-memory block map structute
* @tr: Transaction object.
* @block_map: Block map object.
* @root: Block mac of root tree.
* @block_size: Block size of device.
*/
void block_map_init(const struct transaction *tr,
struct block_map *block_map,
const struct block_mac *root,
size_t block_size)
{
size_t block_num_size = tr->fs->block_num_size;
size_t block_mac_size = block_num_size + tr->fs->mac_size;
memset(block_map, 0, sizeof(*block_map));
block_tree_init(&block_map->tree, block_size,
block_num_size, block_mac_size, block_mac_size);
block_map->tree.copy_on_write = 1;
block_map->tree.allow_copy_on_write = 1;
block_map->tree.root = *root;
}
/**
* block_map_get - Lookup a block
* @tr: Transaction object.
* @block_map: Block map object.
* @index: Index of block to get.
* @block_mac: Pointer to return block_mac in.
*
* Return: %true if a block_mac exists at @index, %false if not. When returning
* %true, @block_mac will be filled in. Otherwise, @block_mac is not touched.
*/
bool block_map_get(struct transaction *tr,
struct block_map *block_map,
data_block_t index,
struct block_mac *block_mac)
{
struct block_tree_path path;
index++; /* 0 is not a valid block tree key */
block_tree_walk(tr, &block_map->tree, index, false, &path);
if (block_tree_path_get_key(&path) != index) {
if (print_block_map) {
printf("%s: %lld not found (next key %lld)\n",
__func__, index, block_tree_path_get_key(&path));
}
return false;
}
*block_mac = block_tree_path_get_data_block_mac(&path);
return true;
}
/**
* block_map_set - Store a block_mac
* @tr: Transaction object.
* @block_map: Block map object.
* @index: Index of block to set.
* @block_mac: block_mac to store, or %NULL to remove the block_mac at @index.
*/
void block_map_set(struct transaction *tr, struct block_map *block_map,
data_block_t index, const struct block_mac *block_mac)
{
struct block_tree_path path;
index++; /* 0 is not a valid block tree key */
if (tr->failed) {
pr_warn("transaction failed, ignore\n");
return;
}
block_tree_walk(tr, &block_map->tree, index, false, &path);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
if (block_tree_path_get_key(&path) == index) {
if (print_block_map) {
printf("%s: block_map at %lld: remove existing entry at %lld\n",
__func__, block_mac_to_block(tr, &block_map->tree.root), index);
}
block_tree_remove(tr, &block_map->tree, index, block_tree_path_get_data(&path));
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
}
if (block_mac && block_mac_valid(tr, block_mac)) {
if (print_block_map) {
printf("%s: block_map at %lld: [%lld] = %lld\n",
__func__, block_mac_to_block(tr, &block_map->tree.root),
index, block_mac_to_block(tr, block_mac));
}
block_tree_insert(tr, &block_map->tree, index, block_mac_to_block(tr, block_mac));
/* TODO: insert mac */
}
}
/**
* block_map_put_dirty - Release a block stored in a block_map, and update mac
* @tr: Transaction object.
* @block_map: Block map object.
* @index: Index of block to update.
* @data: block cache entry.
* @data_ref: reference to @data.
*/
void block_map_put_dirty(struct transaction *tr, struct block_map *block_map,
data_block_t index, void *data, obj_ref_t *data_ref)
{
struct block_tree_path path;
index++; /* 0 is not a valid block tree key */
block_tree_walk(tr, &block_map->tree, index, false, &path);
if (tr->failed) {
pr_warn("transaction failed\n");
block_put_dirty_discard(data, data_ref);
return;
}
if (print_block_map) {
printf("%s: %lld (found key %lld)\n",
__func__, index, block_tree_path_get_key(&path));
}
assert(block_tree_path_get_key(&path) == index);
block_tree_path_put_dirty(tr, &path, path.count, data, data_ref);
}
/**
* block_map_truncate - Free blocks
* @tr: Transaction object.
* @block_map: Block map object.
* @index: Index to start freeing at.
*
* Remove and free all blocks starting at @index.
*/
void block_map_truncate(struct transaction *tr,
struct block_map *block_map,
data_block_t index)
{
struct block_tree_path path;
data_block_t key;
data_block_t data;
data_block_t curr_index;
curr_index = index + 1; /* 0 is not a valid block tree key */
while (true) {
block_tree_walk(tr, &block_map->tree, curr_index, false, &path);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
key = block_tree_path_get_key(&path);
if (!key) {
break;
}
assert(key >= curr_index);
data = block_tree_path_get_data(&path);
if (!data) {
/* block_tree_walk returned an empty insert spot for curr_index */
assert(key != curr_index);
curr_index = key;
continue;
}
assert(data);
block_tree_remove(tr, &block_map->tree, key, data);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
block_discard_dirty_by_block(tr->fs->dev, data);
block_free(tr, data);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
}
/* Only a root leaf node should remain if truncating to 0 */
assert(index || path.count == 1);
}
/**
* block_map_free - Free blocks
* @tr: Transaction object.
* @block_map: Block map object.
*
* Free block_map and all blocks stored in it.
*/
void block_map_free(struct transaction *tr, struct block_map *block_map)
{
data_block_t root_block;
if (!block_mac_valid(tr, &block_map->tree.root)) {
return;
}
if (print_block_map) {
printf("%s: root %lld\n",
__func__, block_mac_to_block(tr, &block_map->tree.root));
block_tree_print(tr, &block_map->tree);
}
block_map_truncate(tr, block_map, 0);
if (tr->failed) {
pr_warn("transaction failed\n");
return;
}
root_block = block_mac_to_block(tr, &block_map->tree.root);
block_discard_dirty_by_block(tr->fs->dev, root_block);
block_free(tr, root_block);
}