forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extent-cache.c
341 lines (286 loc) · 8 KB
/
extent-cache.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (C) 2007 Oracle. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "extent-cache.h"
#include "rbtree-utils.h"
struct cache_extent_search_range {
u64 objectid;
u64 start;
u64 size;
};
static int cache_tree_comp_range(struct rb_node *node, void *data)
{
struct cache_extent *entry;
struct cache_extent_search_range *range;
range = (struct cache_extent_search_range *)data;
entry = rb_entry(node, struct cache_extent, rb_node);
if (entry->start + entry->size <= range->start)
return 1;
else if (range->start + range->size <= entry->start)
return -1;
else
return 0;
}
static int cache_tree_comp_nodes(struct rb_node *node1, struct rb_node *node2)
{
struct cache_extent *entry;
struct cache_extent_search_range range;
entry = rb_entry(node2, struct cache_extent, rb_node);
range.start = entry->start;
range.size = entry->size;
return cache_tree_comp_range(node1, (void *)&range);
}
static int cache_tree_comp_range2(struct rb_node *node, void *data)
{
struct cache_extent *entry;
struct cache_extent_search_range *range;
range = (struct cache_extent_search_range *)data;
entry = rb_entry(node, struct cache_extent, rb_node);
if (entry->objectid < range->objectid)
return 1;
else if (entry->objectid > range->objectid)
return -1;
else if (entry->start + entry->size <= range->start)
return 1;
else if (range->start + range->size <= entry->start)
return -1;
else
return 0;
}
static int cache_tree_comp_nodes2(struct rb_node *node1, struct rb_node *node2)
{
struct cache_extent *entry;
struct cache_extent_search_range range;
entry = rb_entry(node2, struct cache_extent, rb_node);
range.objectid = entry->objectid;
range.start = entry->start;
range.size = entry->size;
return cache_tree_comp_range2(node1, (void *)&range);
}
void cache_tree_init(struct cache_tree *tree)
{
tree->root = RB_ROOT;
}
static struct cache_extent *
alloc_cache_extent(u64 objectid, u64 start, u64 size)
{
struct cache_extent *pe = malloc(sizeof(*pe));
if (!pe)
return pe;
pe->objectid = objectid;
pe->start = start;
pe->size = size;
return pe;
}
static int __add_cache_extent(struct cache_tree *tree,
u64 objectid, u64 start, u64 size)
{
struct cache_extent *pe = alloc_cache_extent(objectid, start, size);
int ret;
if (!pe) {
fprintf(stderr, "memory allocation failed\n");
exit(1);
}
ret = insert_cache_extent(tree, pe);
if (ret)
free(pe);
return ret;
}
int add_cache_extent(struct cache_tree *tree, u64 start, u64 size)
{
return __add_cache_extent(tree, 0, start, size);
}
int add_cache_extent2(struct cache_tree *tree,
u64 objectid, u64 start, u64 size)
{
return __add_cache_extent(tree, objectid, start, size);
}
int insert_cache_extent(struct cache_tree *tree, struct cache_extent *pe)
{
return rb_insert(&tree->root, &pe->rb_node, cache_tree_comp_nodes);
}
int insert_cache_extent2(struct cache_tree *tree, struct cache_extent *pe)
{
return rb_insert(&tree->root, &pe->rb_node, cache_tree_comp_nodes2);
}
struct cache_extent *lookup_cache_extent(struct cache_tree *tree,
u64 start, u64 size)
{
struct rb_node *node;
struct cache_extent *entry;
struct cache_extent_search_range range;
range.start = start;
range.size = size;
node = rb_search(&tree->root, &range, cache_tree_comp_range, NULL);
if (!node)
return NULL;
entry = rb_entry(node, struct cache_extent, rb_node);
return entry;
}
struct cache_extent *lookup_cache_extent2(struct cache_tree *tree,
u64 objectid, u64 start, u64 size)
{
struct rb_node *node;
struct cache_extent *entry;
struct cache_extent_search_range range;
range.objectid = objectid;
range.start = start;
range.size = size;
node = rb_search(&tree->root, &range, cache_tree_comp_range2, NULL);
if (!node)
return NULL;
entry = rb_entry(node, struct cache_extent, rb_node);
return entry;
}
struct cache_extent *search_cache_extent(struct cache_tree *tree, u64 start)
{
struct rb_node *next;
struct rb_node *node;
struct cache_extent *entry;
struct cache_extent_search_range range;
range.start = start;
range.size = 1;
node = rb_search(&tree->root, &range, cache_tree_comp_range, &next);
if (!node)
node = next;
if (!node)
return NULL;
entry = rb_entry(node, struct cache_extent, rb_node);
return entry;
}
struct cache_extent *search_cache_extent2(struct cache_tree *tree,
u64 objectid, u64 start)
{
struct rb_node *next;
struct rb_node *node;
struct cache_extent *entry;
struct cache_extent_search_range range;
range.objectid = objectid;
range.start = start;
range.size = 1;
node = rb_search(&tree->root, &range, cache_tree_comp_range2, &next);
if (!node)
node = next;
if (!node)
return NULL;
entry = rb_entry(node, struct cache_extent, rb_node);
return entry;
}
struct cache_extent *first_cache_extent(struct cache_tree *tree)
{
struct rb_node *node = rb_first(&tree->root);
if (!node)
return NULL;
return rb_entry(node, struct cache_extent, rb_node);
}
struct cache_extent *last_cache_extent(struct cache_tree *tree)
{
struct rb_node *node = rb_last(&tree->root);
if (!node)
return NULL;
return rb_entry(node, struct cache_extent, rb_node);
}
struct cache_extent *prev_cache_extent(struct cache_extent *pe)
{
struct rb_node *node = rb_prev(&pe->rb_node);
if (!node)
return NULL;
return rb_entry(node, struct cache_extent, rb_node);
}
struct cache_extent *next_cache_extent(struct cache_extent *pe)
{
struct rb_node *node = rb_next(&pe->rb_node);
if (!node)
return NULL;
return rb_entry(node, struct cache_extent, rb_node);
}
void remove_cache_extent(struct cache_tree *tree, struct cache_extent *pe)
{
rb_erase(&pe->rb_node, &tree->root);
}
void cache_tree_free_extents(struct cache_tree *tree,
free_cache_extent free_func)
{
struct cache_extent *ce;
while ((ce = first_cache_extent(tree))) {
remove_cache_extent(tree, ce);
free_func(ce);
}
}
static void free_extent_cache(struct cache_extent *pe)
{
free(pe);
}
void free_extent_cache_tree(struct cache_tree *tree)
{
cache_tree_free_extents(tree, free_extent_cache);
}
int add_merge_cache_extent(struct cache_tree *tree, u64 start, u64 size)
{
struct cache_extent *cache;
struct cache_extent *next = NULL;
struct cache_extent *prev = NULL;
int next_merged = 0;
int prev_merged = 0;
int ret = 0;
if (cache_tree_empty(tree))
goto insert;
cache = search_cache_extent(tree, start);
if (!cache) {
/*
* Either the tree is completely empty, or the no range after
* start.
* Either way, the last cache_extent should be prev.
*/
prev = last_cache_extent(tree);
} else if (start <= cache->start) {
next = cache;
prev = prev_cache_extent(cache);
} else {
prev = cache;
next = next_cache_extent(cache);
}
/*
* Ensure the range to be inserted won't cover with existings
* Or we will need extra loop to do merge
*/
BUG_ON(next && start + size > next->start);
BUG_ON(prev && prev->start + prev->size > start);
if (next && start + size == next->start) {
next_merged = 1;
next->size = next->start + next->size - start;
next->start = start;
}
if (prev && prev->start + prev->size == start) {
prev_merged = 1;
if (next_merged) {
next->size = next->start + next->size - prev->start;
next->start = prev->start;
remove_cache_extent(tree, prev);
free(prev);
} else {
prev->size = start + size - prev->start;
}
}
insert:
if (!prev_merged && !next_merged)
ret = add_cache_extent(tree, start, size);
return ret;
}