]>
Commit | Line | Data |
---|---|---|
be0e5c09 CM |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include "kerncompat.h" | |
eb60ceac CM |
4 | #include "radix-tree.h" |
5 | #include "ctree.h" | |
6 | #include "disk-io.h" | |
5de08d7d | 7 | #include "print-tree.h" |
9a8dd150 | 8 | |
aa5d6bed CM |
9 | static int split_node(struct ctree_root *root, struct ctree_path *path, |
10 | int level); | |
11 | static int split_leaf(struct ctree_root *root, struct ctree_path *path, | |
12 | int data_size); | |
bb803951 CM |
13 | static int push_node_left(struct ctree_root *root, struct tree_buffer *dst, |
14 | struct tree_buffer *src); | |
79f95c82 CM |
15 | static int balance_node_right(struct ctree_root *root, |
16 | struct tree_buffer *dst_buf, | |
17 | struct tree_buffer *src_buf); | |
bb803951 CM |
18 | static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level, |
19 | int slot); | |
d97e63b6 | 20 | |
5de08d7d | 21 | inline void init_path(struct ctree_path *p) |
be0e5c09 CM |
22 | { |
23 | memset(p, 0, sizeof(*p)); | |
24 | } | |
25 | ||
5de08d7d | 26 | void release_path(struct ctree_root *root, struct ctree_path *p) |
eb60ceac CM |
27 | { |
28 | int i; | |
29 | for (i = 0; i < MAX_LEVEL; i++) { | |
30 | if (!p->nodes[i]) | |
31 | break; | |
32 | tree_block_release(root, p->nodes[i]); | |
33 | } | |
aa5d6bed | 34 | memset(p, 0, sizeof(*p)); |
eb60ceac CM |
35 | } |
36 | ||
02217ed2 CM |
37 | int btrfs_cow_block(struct ctree_root *root, |
38 | struct tree_buffer *buf, | |
39 | struct tree_buffer *parent, | |
40 | int parent_slot, | |
41 | struct tree_buffer **cow_ret) | |
42 | { | |
43 | struct tree_buffer *cow; | |
44 | ||
45 | if (!list_empty(&buf->dirty)) { | |
46 | *cow_ret = buf; | |
47 | return 0; | |
48 | } | |
49 | cow = alloc_free_block(root); | |
50 | memcpy(&cow->node, &buf->node, sizeof(buf->node)); | |
7518a238 | 51 | btrfs_set_header_blocknr(&cow->node.header, cow->blocknr); |
02217ed2 | 52 | *cow_ret = cow; |
a28ec197 | 53 | btrfs_inc_ref(root, buf); |
02217ed2 CM |
54 | if (buf == root->node) { |
55 | root->node = cow; | |
56 | cow->count++; | |
a28ec197 CM |
57 | if (buf != root->commit_root) |
58 | free_extent(root, buf->blocknr, 1); | |
02217ed2 CM |
59 | tree_block_release(root, buf); |
60 | } else { | |
61 | parent->node.blockptrs[parent_slot] = cow->blocknr; | |
62 | BUG_ON(list_empty(&parent->dirty)); | |
a28ec197 | 63 | free_extent(root, buf->blocknr, 1); |
02217ed2 CM |
64 | } |
65 | tree_block_release(root, buf); | |
66 | return 0; | |
67 | } | |
68 | ||
74123bd7 CM |
69 | /* |
70 | * The leaf data grows from end-to-front in the node. | |
71 | * this returns the address of the start of the last item, | |
72 | * which is the stop of the leaf data stack | |
73 | */ | |
be0e5c09 CM |
74 | static inline unsigned int leaf_data_end(struct leaf *leaf) |
75 | { | |
7518a238 | 76 | u32 nr = btrfs_header_nritems(&leaf->header); |
be0e5c09 | 77 | if (nr == 0) |
d97e63b6 | 78 | return sizeof(leaf->data); |
be0e5c09 CM |
79 | return leaf->items[nr-1].offset; |
80 | } | |
81 | ||
74123bd7 CM |
82 | /* |
83 | * The space between the end of the leaf items and | |
84 | * the start of the leaf data. IOW, how much room | |
85 | * the leaf has left for both items and data | |
86 | */ | |
5de08d7d | 87 | int leaf_free_space(struct leaf *leaf) |
be0e5c09 CM |
88 | { |
89 | int data_end = leaf_data_end(leaf); | |
7518a238 | 90 | int nritems = btrfs_header_nritems(&leaf->header); |
be0e5c09 CM |
91 | char *items_end = (char *)(leaf->items + nritems + 1); |
92 | return (char *)(leaf->data + data_end) - (char *)items_end; | |
93 | } | |
94 | ||
74123bd7 CM |
95 | /* |
96 | * compare two keys in a memcmp fashion | |
97 | */ | |
e2fa7227 | 98 | int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
be0e5c09 | 99 | { |
e2fa7227 CM |
100 | struct btrfs_key k1; |
101 | ||
102 | btrfs_disk_key_to_cpu(&k1, disk); | |
103 | ||
104 | if (k1.objectid > k2->objectid) | |
be0e5c09 | 105 | return 1; |
e2fa7227 | 106 | if (k1.objectid < k2->objectid) |
be0e5c09 | 107 | return -1; |
e2fa7227 | 108 | if (k1.flags > k2->flags) |
be0e5c09 | 109 | return 1; |
e2fa7227 | 110 | if (k1.flags < k2->flags) |
be0e5c09 | 111 | return -1; |
e2fa7227 | 112 | if (k1.offset > k2->offset) |
be0e5c09 | 113 | return 1; |
e2fa7227 | 114 | if (k1.offset < k2->offset) |
be0e5c09 CM |
115 | return -1; |
116 | return 0; | |
117 | } | |
74123bd7 | 118 | |
aa5d6bed CM |
119 | int check_node(struct ctree_path *path, int level) |
120 | { | |
121 | int i; | |
122 | struct node *parent = NULL; | |
123 | struct node *node = &path->nodes[level]->node; | |
124 | int parent_slot; | |
7518a238 | 125 | u32 nritems = btrfs_header_nritems(&node->header); |
aa5d6bed CM |
126 | |
127 | if (path->nodes[level + 1]) | |
128 | parent = &path->nodes[level + 1]->node; | |
129 | parent_slot = path->slots[level + 1]; | |
7518a238 CM |
130 | BUG_ON(nritems == 0); |
131 | if (parent) { | |
e2fa7227 | 132 | struct btrfs_disk_key *parent_key; |
aa5d6bed | 133 | parent_key = &parent->keys[parent_slot]; |
e2fa7227 CM |
134 | BUG_ON(memcmp(parent_key, node->keys, |
135 | sizeof(struct btrfs_disk_key))); | |
7518a238 CM |
136 | BUG_ON(parent->blockptrs[parent_slot] != |
137 | btrfs_header_blocknr(&node->header)); | |
aa5d6bed | 138 | } |
7518a238 CM |
139 | BUG_ON(nritems > NODEPTRS_PER_BLOCK); |
140 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { | |
e2fa7227 CM |
141 | struct btrfs_key cpukey; |
142 | btrfs_disk_key_to_cpu(&cpukey, &node->keys[i + 1]); | |
143 | BUG_ON(comp_keys(&node->keys[i], &cpukey) >= 0); | |
aa5d6bed CM |
144 | } |
145 | return 0; | |
146 | } | |
147 | ||
148 | int check_leaf(struct ctree_path *path, int level) | |
149 | { | |
150 | int i; | |
151 | struct leaf *leaf = &path->nodes[level]->leaf; | |
152 | struct node *parent = NULL; | |
153 | int parent_slot; | |
7518a238 | 154 | u32 nritems = btrfs_header_nritems(&leaf->header); |
aa5d6bed CM |
155 | |
156 | if (path->nodes[level + 1]) | |
157 | parent = &path->nodes[level + 1]->node; | |
158 | parent_slot = path->slots[level + 1]; | |
7518a238 CM |
159 | BUG_ON(leaf_free_space(leaf) < 0); |
160 | ||
161 | if (nritems == 0) | |
162 | return 0; | |
163 | ||
164 | if (parent) { | |
e2fa7227 | 165 | struct btrfs_disk_key *parent_key; |
aa5d6bed CM |
166 | parent_key = &parent->keys[parent_slot]; |
167 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, | |
e2fa7227 | 168 | sizeof(struct btrfs_disk_key))); |
7518a238 CM |
169 | BUG_ON(parent->blockptrs[parent_slot] != |
170 | btrfs_header_blocknr(&leaf->header)); | |
aa5d6bed | 171 | } |
7518a238 | 172 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
e2fa7227 CM |
173 | struct btrfs_key cpukey; |
174 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); | |
aa5d6bed | 175 | BUG_ON(comp_keys(&leaf->items[i].key, |
e2fa7227 | 176 | &cpukey) >= 0); |
aa5d6bed CM |
177 | BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset + |
178 | leaf->items[i + 1].size); | |
179 | if (i == 0) { | |
180 | BUG_ON(leaf->items[i].offset + leaf->items[i].size != | |
181 | LEAF_DATA_SIZE); | |
182 | } | |
183 | } | |
aa5d6bed CM |
184 | return 0; |
185 | } | |
186 | ||
187 | int check_block(struct ctree_path *path, int level) | |
188 | { | |
189 | if (level == 0) | |
190 | return check_leaf(path, level); | |
191 | return check_node(path, level); | |
192 | } | |
193 | ||
74123bd7 CM |
194 | /* |
195 | * search for key in the array p. items p are item_size apart | |
196 | * and there are 'max' items in p | |
197 | * the slot in the array is returned via slot, and it points to | |
198 | * the place where you would insert key if it is not found in | |
199 | * the array. | |
200 | * | |
201 | * slot may point to max if the key is bigger than all of the keys | |
202 | */ | |
e2fa7227 | 203 | int generic_bin_search(char *p, int item_size, struct btrfs_key *key, |
be0e5c09 CM |
204 | int max, int *slot) |
205 | { | |
206 | int low = 0; | |
207 | int high = max; | |
208 | int mid; | |
209 | int ret; | |
e2fa7227 | 210 | struct btrfs_disk_key *tmp; |
be0e5c09 CM |
211 | |
212 | while(low < high) { | |
213 | mid = (low + high) / 2; | |
e2fa7227 | 214 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
be0e5c09 CM |
215 | ret = comp_keys(tmp, key); |
216 | ||
217 | if (ret < 0) | |
218 | low = mid + 1; | |
219 | else if (ret > 0) | |
220 | high = mid; | |
221 | else { | |
222 | *slot = mid; | |
223 | return 0; | |
224 | } | |
225 | } | |
226 | *slot = low; | |
227 | return 1; | |
228 | } | |
229 | ||
97571fd0 CM |
230 | /* |
231 | * simple bin_search frontend that does the right thing for | |
232 | * leaves vs nodes | |
233 | */ | |
e2fa7227 | 234 | int bin_search(struct node *c, struct btrfs_key *key, int *slot) |
be0e5c09 | 235 | { |
7518a238 | 236 | if (btrfs_is_leaf(c)) { |
be0e5c09 CM |
237 | struct leaf *l = (struct leaf *)c; |
238 | return generic_bin_search((void *)l->items, sizeof(struct item), | |
7518a238 CM |
239 | key, btrfs_header_nritems(&c->header), |
240 | slot); | |
be0e5c09 | 241 | } else { |
e2fa7227 CM |
242 | return generic_bin_search((void *)c->keys, |
243 | sizeof(struct btrfs_disk_key), | |
7518a238 CM |
244 | key, btrfs_header_nritems(&c->header), |
245 | slot); | |
be0e5c09 CM |
246 | } |
247 | return -1; | |
248 | } | |
249 | ||
bb803951 CM |
250 | struct tree_buffer *read_node_slot(struct ctree_root *root, |
251 | struct tree_buffer *parent_buf, | |
252 | int slot) | |
253 | { | |
254 | struct node *node = &parent_buf->node; | |
255 | if (slot < 0) | |
256 | return NULL; | |
7518a238 | 257 | if (slot >= btrfs_header_nritems(&node->header)) |
bb803951 CM |
258 | return NULL; |
259 | return read_tree_block(root, node->blockptrs[slot]); | |
260 | } | |
261 | ||
262 | static int balance_level(struct ctree_root *root, struct ctree_path *path, | |
263 | int level) | |
264 | { | |
265 | struct tree_buffer *right_buf; | |
266 | struct tree_buffer *mid_buf; | |
267 | struct tree_buffer *left_buf; | |
268 | struct tree_buffer *parent_buf = NULL; | |
269 | struct node *right = NULL; | |
270 | struct node *mid; | |
271 | struct node *left = NULL; | |
272 | struct node *parent = NULL; | |
273 | int ret = 0; | |
274 | int wret; | |
275 | int pslot; | |
bb803951 | 276 | int orig_slot = path->slots[level]; |
79f95c82 | 277 | u64 orig_ptr; |
bb803951 CM |
278 | |
279 | if (level == 0) | |
280 | return 0; | |
281 | ||
282 | mid_buf = path->nodes[level]; | |
283 | mid = &mid_buf->node; | |
79f95c82 CM |
284 | orig_ptr = mid->blockptrs[orig_slot]; |
285 | ||
bb803951 CM |
286 | if (level < MAX_LEVEL - 1) |
287 | parent_buf = path->nodes[level + 1]; | |
288 | pslot = path->slots[level + 1]; | |
289 | ||
290 | if (!parent_buf) { | |
291 | struct tree_buffer *child; | |
292 | u64 blocknr = mid_buf->blocknr; | |
293 | ||
7518a238 | 294 | if (btrfs_header_nritems(&mid->header) != 1) |
bb803951 CM |
295 | return 0; |
296 | ||
297 | /* promote the child to a root */ | |
298 | child = read_node_slot(root, mid_buf, 0); | |
299 | BUG_ON(!child); | |
300 | root->node = child; | |
301 | path->nodes[level] = NULL; | |
302 | /* once for the path */ | |
303 | tree_block_release(root, mid_buf); | |
304 | /* once for the root ptr */ | |
305 | tree_block_release(root, mid_buf); | |
ed2ff2cb | 306 | clean_tree_block(root, mid_buf); |
bb803951 CM |
307 | return free_extent(root, blocknr, 1); |
308 | } | |
309 | parent = &parent_buf->node; | |
310 | ||
7518a238 | 311 | if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4) |
bb803951 CM |
312 | return 0; |
313 | ||
bb803951 CM |
314 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
315 | right_buf = read_node_slot(root, parent_buf, pslot + 1); | |
79f95c82 CM |
316 | |
317 | /* first, try to make some room in the middle buffer */ | |
bb803951 | 318 | if (left_buf) { |
02217ed2 CM |
319 | btrfs_cow_block(root, left_buf, parent_buf, |
320 | pslot - 1, &left_buf); | |
bb803951 | 321 | left = &left_buf->node; |
7518a238 | 322 | orig_slot += btrfs_header_nritems(&left->header); |
79f95c82 CM |
323 | wret = push_node_left(root, left_buf, mid_buf); |
324 | if (wret < 0) | |
325 | ret = wret; | |
bb803951 | 326 | } |
79f95c82 CM |
327 | |
328 | /* | |
329 | * then try to empty the right most buffer into the middle | |
330 | */ | |
bb803951 | 331 | if (right_buf) { |
02217ed2 CM |
332 | btrfs_cow_block(root, right_buf, parent_buf, |
333 | pslot + 1, &right_buf); | |
79f95c82 CM |
334 | right = &right_buf->node; |
335 | wret = push_node_left(root, mid_buf, right_buf); | |
336 | if (wret < 0) | |
337 | ret = wret; | |
7518a238 | 338 | if (btrfs_header_nritems(&right->header) == 0) { |
bb803951 CM |
339 | u64 blocknr = right_buf->blocknr; |
340 | tree_block_release(root, right_buf); | |
ed2ff2cb | 341 | clean_tree_block(root, right_buf); |
bb803951 CM |
342 | right_buf = NULL; |
343 | right = NULL; | |
344 | wret = del_ptr(root, path, level + 1, pslot + 1); | |
345 | if (wret) | |
346 | ret = wret; | |
347 | wret = free_extent(root, blocknr, 1); | |
348 | if (wret) | |
349 | ret = wret; | |
350 | } else { | |
351 | memcpy(parent->keys + pslot + 1, right->keys, | |
e2fa7227 | 352 | sizeof(struct btrfs_disk_key)); |
02217ed2 | 353 | BUG_ON(list_empty(&parent_buf->dirty)); |
bb803951 CM |
354 | } |
355 | } | |
7518a238 | 356 | if (btrfs_header_nritems(&mid->header) == 1) { |
79f95c82 CM |
357 | /* |
358 | * we're not allowed to leave a node with one item in the | |
359 | * tree during a delete. A deletion from lower in the tree | |
360 | * could try to delete the only pointer in this node. | |
361 | * So, pull some keys from the left. | |
362 | * There has to be a left pointer at this point because | |
363 | * otherwise we would have pulled some pointers from the | |
364 | * right | |
365 | */ | |
366 | BUG_ON(!left_buf); | |
367 | wret = balance_node_right(root, mid_buf, left_buf); | |
368 | if (wret < 0) | |
369 | ret = wret; | |
370 | BUG_ON(wret == 1); | |
371 | } | |
7518a238 | 372 | if (btrfs_header_nritems(&mid->header) == 0) { |
79f95c82 | 373 | /* we've managed to empty the middle node, drop it */ |
bb803951 CM |
374 | u64 blocknr = mid_buf->blocknr; |
375 | tree_block_release(root, mid_buf); | |
ed2ff2cb | 376 | clean_tree_block(root, mid_buf); |
bb803951 CM |
377 | mid_buf = NULL; |
378 | mid = NULL; | |
379 | wret = del_ptr(root, path, level + 1, pslot); | |
380 | if (wret) | |
381 | ret = wret; | |
382 | wret = free_extent(root, blocknr, 1); | |
383 | if (wret) | |
384 | ret = wret; | |
79f95c82 CM |
385 | } else { |
386 | /* update the parent key to reflect our changes */ | |
e2fa7227 CM |
387 | memcpy(parent->keys + pslot, mid->keys, |
388 | sizeof(struct btrfs_disk_key)); | |
02217ed2 | 389 | BUG_ON(list_empty(&parent_buf->dirty)); |
79f95c82 | 390 | } |
bb803951 | 391 | |
79f95c82 | 392 | /* update the path */ |
bb803951 | 393 | if (left_buf) { |
7518a238 | 394 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
bb803951 CM |
395 | left_buf->count++; // released below |
396 | path->nodes[level] = left_buf; | |
397 | path->slots[level + 1] -= 1; | |
398 | path->slots[level] = orig_slot; | |
399 | if (mid_buf) | |
400 | tree_block_release(root, mid_buf); | |
401 | } else { | |
7518a238 | 402 | orig_slot -= btrfs_header_nritems(&left->header); |
bb803951 CM |
403 | path->slots[level] = orig_slot; |
404 | } | |
405 | } | |
79f95c82 CM |
406 | /* double check we haven't messed things up */ |
407 | check_block(path, level); | |
408 | if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]]) | |
409 | BUG(); | |
bb803951 CM |
410 | |
411 | if (right_buf) | |
412 | tree_block_release(root, right_buf); | |
413 | if (left_buf) | |
414 | tree_block_release(root, left_buf); | |
bb803951 CM |
415 | return ret; |
416 | } | |
417 | ||
74123bd7 CM |
418 | /* |
419 | * look for key in the tree. path is filled in with nodes along the way | |
420 | * if key is found, we return zero and you can find the item in the leaf | |
421 | * level of the path (level 0) | |
422 | * | |
423 | * If the key isn't found, the path points to the slot where it should | |
aa5d6bed CM |
424 | * be inserted, and 1 is returned. If there are other errors during the |
425 | * search a negative error number is returned. | |
97571fd0 CM |
426 | * |
427 | * if ins_len > 0, nodes and leaves will be split as we walk down the | |
428 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if | |
429 | * possible) | |
74123bd7 | 430 | */ |
e2fa7227 | 431 | int search_slot(struct ctree_root *root, struct btrfs_key *key, |
02217ed2 | 432 | struct ctree_path *p, int ins_len, int cow) |
be0e5c09 | 433 | { |
bb803951 | 434 | struct tree_buffer *b; |
02217ed2 | 435 | struct tree_buffer *cow_buf; |
eb60ceac | 436 | struct node *c; |
be0e5c09 CM |
437 | int slot; |
438 | int ret; | |
439 | int level; | |
5c680ed6 | 440 | |
bb803951 CM |
441 | again: |
442 | b = root->node; | |
eb60ceac CM |
443 | b->count++; |
444 | while (b) { | |
7518a238 | 445 | level = btrfs_header_level(&b->node.header); |
02217ed2 CM |
446 | if (cow) { |
447 | int wret; | |
448 | wret = btrfs_cow_block(root, b, p->nodes[level + 1], | |
449 | p->slots[level + 1], &cow_buf); | |
450 | b = cow_buf; | |
451 | } | |
452 | BUG_ON(!cow && ins_len); | |
eb60ceac | 453 | c = &b->node; |
eb60ceac | 454 | p->nodes[level] = b; |
aa5d6bed CM |
455 | ret = check_block(p, level); |
456 | if (ret) | |
457 | return -1; | |
be0e5c09 | 458 | ret = bin_search(c, key, &slot); |
7518a238 | 459 | if (!btrfs_is_leaf(c)) { |
be0e5c09 CM |
460 | if (ret && slot > 0) |
461 | slot -= 1; | |
462 | p->slots[level] = slot; | |
7518a238 CM |
463 | if (ins_len > 0 && btrfs_header_nritems(&c->header) == |
464 | NODEPTRS_PER_BLOCK) { | |
5c680ed6 CM |
465 | int sret = split_node(root, p, level); |
466 | BUG_ON(sret > 0); | |
467 | if (sret) | |
468 | return sret; | |
469 | b = p->nodes[level]; | |
470 | c = &b->node; | |
471 | slot = p->slots[level]; | |
bb803951 CM |
472 | } else if (ins_len < 0) { |
473 | int sret = balance_level(root, p, level); | |
474 | if (sret) | |
475 | return sret; | |
476 | b = p->nodes[level]; | |
477 | if (!b) | |
478 | goto again; | |
479 | c = &b->node; | |
480 | slot = p->slots[level]; | |
7518a238 | 481 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
5c680ed6 | 482 | } |
eb60ceac | 483 | b = read_tree_block(root, c->blockptrs[slot]); |
be0e5c09 | 484 | } else { |
5c680ed6 | 485 | struct leaf *l = (struct leaf *)c; |
be0e5c09 | 486 | p->slots[level] = slot; |
5de08d7d CM |
487 | if (ins_len > 0 && leaf_free_space(l) < |
488 | sizeof(struct item) + ins_len) { | |
5c680ed6 CM |
489 | int sret = split_leaf(root, p, ins_len); |
490 | BUG_ON(sret > 0); | |
491 | if (sret) | |
492 | return sret; | |
493 | } | |
bb803951 | 494 | BUG_ON(root->node->count == 1); |
be0e5c09 CM |
495 | return ret; |
496 | } | |
497 | } | |
bb803951 | 498 | BUG_ON(root->node->count == 1); |
aa5d6bed | 499 | return 1; |
be0e5c09 CM |
500 | } |
501 | ||
74123bd7 CM |
502 | /* |
503 | * adjust the pointers going up the tree, starting at level | |
504 | * making sure the right key of each node is points to 'key'. | |
505 | * This is used after shifting pointers to the left, so it stops | |
506 | * fixing up pointers when a given leaf/node is not in slot 0 of the | |
507 | * higher levels | |
aa5d6bed CM |
508 | * |
509 | * If this fails to write a tree block, it returns -1, but continues | |
510 | * fixing up the blocks in ram so the tree is consistent. | |
74123bd7 | 511 | */ |
aa5d6bed | 512 | static int fixup_low_keys(struct ctree_root *root, |
e2fa7227 | 513 | struct ctree_path *path, struct btrfs_disk_key *key, |
eb60ceac | 514 | int level) |
be0e5c09 CM |
515 | { |
516 | int i; | |
aa5d6bed | 517 | int ret = 0; |
be0e5c09 | 518 | for (i = level; i < MAX_LEVEL; i++) { |
eb60ceac | 519 | struct node *t; |
be0e5c09 | 520 | int tslot = path->slots[i]; |
eb60ceac | 521 | if (!path->nodes[i]) |
be0e5c09 | 522 | break; |
eb60ceac | 523 | t = &path->nodes[i]->node; |
be0e5c09 | 524 | memcpy(t->keys + tslot, key, sizeof(*key)); |
02217ed2 | 525 | BUG_ON(list_empty(&path->nodes[i]->dirty)); |
be0e5c09 CM |
526 | if (tslot != 0) |
527 | break; | |
528 | } | |
aa5d6bed | 529 | return ret; |
be0e5c09 CM |
530 | } |
531 | ||
74123bd7 CM |
532 | /* |
533 | * try to push data from one node into the next node left in the | |
79f95c82 | 534 | * tree. |
aa5d6bed CM |
535 | * |
536 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible | |
537 | * error, and > 0 if there was no room in the left hand block. | |
74123bd7 | 538 | */ |
bb803951 CM |
539 | static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf, |
540 | struct tree_buffer *src_buf) | |
be0e5c09 | 541 | { |
bb803951 CM |
542 | struct node *src = &src_buf->node; |
543 | struct node *dst = &dst_buf->node; | |
be0e5c09 | 544 | int push_items = 0; |
bb803951 CM |
545 | int src_nritems; |
546 | int dst_nritems; | |
aa5d6bed | 547 | int ret = 0; |
be0e5c09 | 548 | |
7518a238 CM |
549 | src_nritems = btrfs_header_nritems(&src->header); |
550 | dst_nritems = btrfs_header_nritems(&dst->header); | |
bb803951 | 551 | push_items = NODEPTRS_PER_BLOCK - dst_nritems; |
eb60ceac | 552 | if (push_items <= 0) { |
be0e5c09 | 553 | return 1; |
eb60ceac | 554 | } |
be0e5c09 | 555 | |
bb803951 | 556 | if (src_nritems < push_items) |
79f95c82 CM |
557 | push_items = src_nritems; |
558 | ||
bb803951 | 559 | memcpy(dst->keys + dst_nritems, src->keys, |
e2fa7227 | 560 | push_items * sizeof(struct btrfs_disk_key)); |
bb803951 | 561 | memcpy(dst->blockptrs + dst_nritems, src->blockptrs, |
be0e5c09 | 562 | push_items * sizeof(u64)); |
bb803951 CM |
563 | if (push_items < src_nritems) { |
564 | memmove(src->keys, src->keys + push_items, | |
e2fa7227 CM |
565 | (src_nritems - push_items) * |
566 | sizeof(struct btrfs_disk_key)); | |
bb803951 CM |
567 | memmove(src->blockptrs, src->blockptrs + push_items, |
568 | (src_nritems - push_items) * sizeof(u64)); | |
569 | } | |
7518a238 CM |
570 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
571 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); | |
02217ed2 CM |
572 | BUG_ON(list_empty(&src_buf->dirty)); |
573 | BUG_ON(list_empty(&dst_buf->dirty)); | |
79f95c82 CM |
574 | return ret; |
575 | } | |
576 | ||
577 | /* | |
578 | * try to push data from one node into the next node right in the | |
579 | * tree. | |
580 | * | |
581 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible | |
582 | * error, and > 0 if there was no room in the right hand block. | |
583 | * | |
584 | * this will only push up to 1/2 the contents of the left node over | |
585 | */ | |
586 | static int balance_node_right(struct ctree_root *root, | |
587 | struct tree_buffer *dst_buf, | |
588 | struct tree_buffer *src_buf) | |
589 | { | |
590 | struct node *src = &src_buf->node; | |
591 | struct node *dst = &dst_buf->node; | |
592 | int push_items = 0; | |
593 | int max_push; | |
594 | int src_nritems; | |
595 | int dst_nritems; | |
596 | int ret = 0; | |
79f95c82 | 597 | |
7518a238 CM |
598 | src_nritems = btrfs_header_nritems(&src->header); |
599 | dst_nritems = btrfs_header_nritems(&dst->header); | |
79f95c82 CM |
600 | push_items = NODEPTRS_PER_BLOCK - dst_nritems; |
601 | if (push_items <= 0) { | |
602 | return 1; | |
603 | } | |
604 | ||
605 | max_push = src_nritems / 2 + 1; | |
606 | /* don't try to empty the node */ | |
607 | if (max_push > src_nritems) | |
608 | return 1; | |
609 | if (max_push < push_items) | |
610 | push_items = max_push; | |
611 | ||
612 | memmove(dst->keys + push_items, dst->keys, | |
e2fa7227 | 613 | dst_nritems * sizeof(struct btrfs_disk_key)); |
79f95c82 CM |
614 | memmove(dst->blockptrs + push_items, dst->blockptrs, |
615 | dst_nritems * sizeof(u64)); | |
616 | memcpy(dst->keys, src->keys + src_nritems - push_items, | |
e2fa7227 | 617 | push_items * sizeof(struct btrfs_disk_key)); |
79f95c82 CM |
618 | memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items, |
619 | push_items * sizeof(u64)); | |
620 | ||
7518a238 CM |
621 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
622 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); | |
79f95c82 | 623 | |
02217ed2 CM |
624 | BUG_ON(list_empty(&src_buf->dirty)); |
625 | BUG_ON(list_empty(&dst_buf->dirty)); | |
aa5d6bed | 626 | return ret; |
be0e5c09 CM |
627 | } |
628 | ||
97571fd0 CM |
629 | /* |
630 | * helper function to insert a new root level in the tree. | |
631 | * A new node is allocated, and a single item is inserted to | |
632 | * point to the existing root | |
aa5d6bed CM |
633 | * |
634 | * returns zero on success or < 0 on failure. | |
97571fd0 | 635 | */ |
5de08d7d CM |
636 | static int insert_new_root(struct ctree_root *root, |
637 | struct ctree_path *path, int level) | |
5c680ed6 CM |
638 | { |
639 | struct tree_buffer *t; | |
640 | struct node *lower; | |
641 | struct node *c; | |
e2fa7227 | 642 | struct btrfs_disk_key *lower_key; |
5c680ed6 CM |
643 | |
644 | BUG_ON(path->nodes[level]); | |
645 | BUG_ON(path->nodes[level-1] != root->node); | |
646 | ||
647 | t = alloc_free_block(root); | |
648 | c = &t->node; | |
649 | memset(c, 0, sizeof(c)); | |
7518a238 CM |
650 | btrfs_set_header_nritems(&c->header, 1); |
651 | btrfs_set_header_level(&c->header, level); | |
652 | btrfs_set_header_blocknr(&c->header, t->blocknr); | |
653 | btrfs_set_header_parentid(&c->header, | |
654 | btrfs_header_parentid(&root->node->node.header)); | |
5c680ed6 | 655 | lower = &path->nodes[level-1]->node; |
7518a238 | 656 | if (btrfs_is_leaf(lower)) |
5c680ed6 CM |
657 | lower_key = &((struct leaf *)lower)->items[0].key; |
658 | else | |
659 | lower_key = lower->keys; | |
e2fa7227 | 660 | memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key)); |
5c680ed6 CM |
661 | c->blockptrs[0] = path->nodes[level-1]->blocknr; |
662 | /* the super has an extra ref to root->node */ | |
663 | tree_block_release(root, root->node); | |
664 | root->node = t; | |
665 | t->count++; | |
5c680ed6 CM |
666 | path->nodes[level] = t; |
667 | path->slots[level] = 0; | |
668 | return 0; | |
669 | } | |
670 | ||
74123bd7 CM |
671 | /* |
672 | * worker function to insert a single pointer in a node. | |
673 | * the node should have enough room for the pointer already | |
97571fd0 | 674 | * |
74123bd7 CM |
675 | * slot and level indicate where you want the key to go, and |
676 | * blocknr is the block the key points to. | |
aa5d6bed CM |
677 | * |
678 | * returns zero on success and < 0 on any error | |
74123bd7 | 679 | */ |
aa5d6bed | 680 | static int insert_ptr(struct ctree_root *root, |
e2fa7227 | 681 | struct ctree_path *path, struct btrfs_disk_key *key, |
74123bd7 CM |
682 | u64 blocknr, int slot, int level) |
683 | { | |
74123bd7 | 684 | struct node *lower; |
74123bd7 | 685 | int nritems; |
5c680ed6 CM |
686 | |
687 | BUG_ON(!path->nodes[level]); | |
74123bd7 | 688 | lower = &path->nodes[level]->node; |
7518a238 | 689 | nritems = btrfs_header_nritems(&lower->header); |
74123bd7 CM |
690 | if (slot > nritems) |
691 | BUG(); | |
692 | if (nritems == NODEPTRS_PER_BLOCK) | |
693 | BUG(); | |
694 | if (slot != nritems) { | |
695 | memmove(lower->keys + slot + 1, lower->keys + slot, | |
e2fa7227 | 696 | (nritems - slot) * sizeof(struct btrfs_disk_key)); |
74123bd7 CM |
697 | memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot, |
698 | (nritems - slot) * sizeof(u64)); | |
699 | } | |
e2fa7227 | 700 | memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key)); |
74123bd7 | 701 | lower->blockptrs[slot] = blocknr; |
7518a238 | 702 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
74123bd7 CM |
703 | if (lower->keys[1].objectid == 0) |
704 | BUG(); | |
02217ed2 | 705 | BUG_ON(list_empty(&path->nodes[level]->dirty)); |
74123bd7 CM |
706 | return 0; |
707 | } | |
708 | ||
97571fd0 CM |
709 | /* |
710 | * split the node at the specified level in path in two. | |
711 | * The path is corrected to point to the appropriate node after the split | |
712 | * | |
713 | * Before splitting this tries to make some room in the node by pushing | |
714 | * left and right, if either one works, it returns right away. | |
aa5d6bed CM |
715 | * |
716 | * returns 0 on success and < 0 on failure | |
97571fd0 | 717 | */ |
aa5d6bed CM |
718 | static int split_node(struct ctree_root *root, struct ctree_path *path, |
719 | int level) | |
be0e5c09 | 720 | { |
5c680ed6 CM |
721 | struct tree_buffer *t; |
722 | struct node *c; | |
723 | struct tree_buffer *split_buffer; | |
724 | struct node *split; | |
be0e5c09 | 725 | int mid; |
5c680ed6 | 726 | int ret; |
aa5d6bed | 727 | int wret; |
7518a238 | 728 | u32 c_nritems; |
eb60ceac | 729 | |
5c680ed6 CM |
730 | t = path->nodes[level]; |
731 | c = &t->node; | |
732 | if (t == root->node) { | |
733 | /* trying to split the root, lets make a new one */ | |
734 | ret = insert_new_root(root, path, level + 1); | |
735 | if (ret) | |
736 | return ret; | |
be0e5c09 | 737 | } |
7518a238 | 738 | c_nritems = btrfs_header_nritems(&c->header); |
5c680ed6 CM |
739 | split_buffer = alloc_free_block(root); |
740 | split = &split_buffer->node; | |
7518a238 CM |
741 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
742 | btrfs_set_header_blocknr(&split->header, split_buffer->blocknr); | |
743 | btrfs_set_header_parentid(&split->header, | |
744 | btrfs_header_parentid(&root->node->node.header)); | |
745 | mid = (c_nritems + 1) / 2; | |
5c680ed6 | 746 | memcpy(split->keys, c->keys + mid, |
e2fa7227 | 747 | (c_nritems - mid) * sizeof(struct btrfs_disk_key)); |
5c680ed6 | 748 | memcpy(split->blockptrs, c->blockptrs + mid, |
7518a238 CM |
749 | (c_nritems - mid) * sizeof(u64)); |
750 | btrfs_set_header_nritems(&split->header, c_nritems - mid); | |
751 | btrfs_set_header_nritems(&c->header, mid); | |
aa5d6bed CM |
752 | ret = 0; |
753 | ||
02217ed2 | 754 | BUG_ON(list_empty(&t->dirty)); |
aa5d6bed CM |
755 | wret = insert_ptr(root, path, split->keys, split_buffer->blocknr, |
756 | path->slots[level + 1] + 1, level + 1); | |
757 | if (wret) | |
758 | ret = wret; | |
759 | ||
5de08d7d | 760 | if (path->slots[level] >= mid) { |
5c680ed6 CM |
761 | path->slots[level] -= mid; |
762 | tree_block_release(root, t); | |
763 | path->nodes[level] = split_buffer; | |
764 | path->slots[level + 1] += 1; | |
765 | } else { | |
766 | tree_block_release(root, split_buffer); | |
be0e5c09 | 767 | } |
aa5d6bed | 768 | return ret; |
be0e5c09 CM |
769 | } |
770 | ||
74123bd7 CM |
771 | /* |
772 | * how many bytes are required to store the items in a leaf. start | |
773 | * and nr indicate which items in the leaf to check. This totals up the | |
774 | * space used both by the item structs and the item data | |
775 | */ | |
aa5d6bed | 776 | static int leaf_space_used(struct leaf *l, int start, int nr) |
be0e5c09 CM |
777 | { |
778 | int data_len; | |
779 | int end = start + nr - 1; | |
780 | ||
781 | if (!nr) | |
782 | return 0; | |
783 | data_len = l->items[start].offset + l->items[start].size; | |
784 | data_len = data_len - l->items[end].offset; | |
785 | data_len += sizeof(struct item) * nr; | |
786 | return data_len; | |
787 | } | |
788 | ||
00ec4c51 CM |
789 | /* |
790 | * push some data in the path leaf to the right, trying to free up at | |
791 | * least data_size bytes. returns zero if the push worked, nonzero otherwise | |
aa5d6bed CM |
792 | * |
793 | * returns 1 if the push failed because the other node didn't have enough | |
794 | * room, 0 if everything worked out and < 0 if there were major errors. | |
00ec4c51 | 795 | */ |
aa5d6bed CM |
796 | static int push_leaf_right(struct ctree_root *root, struct ctree_path *path, |
797 | int data_size) | |
00ec4c51 CM |
798 | { |
799 | struct tree_buffer *left_buf = path->nodes[0]; | |
800 | struct leaf *left = &left_buf->leaf; | |
801 | struct leaf *right; | |
802 | struct tree_buffer *right_buf; | |
803 | struct tree_buffer *upper; | |
804 | int slot; | |
805 | int i; | |
806 | int free_space; | |
807 | int push_space = 0; | |
808 | int push_items = 0; | |
809 | struct item *item; | |
7518a238 CM |
810 | u32 left_nritems; |
811 | u32 right_nritems; | |
00ec4c51 CM |
812 | |
813 | slot = path->slots[1]; | |
814 | if (!path->nodes[1]) { | |
815 | return 1; | |
816 | } | |
817 | upper = path->nodes[1]; | |
7518a238 | 818 | if (slot >= btrfs_header_nritems(&upper->node.header) - 1) { |
00ec4c51 CM |
819 | return 1; |
820 | } | |
821 | right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]); | |
822 | right = &right_buf->leaf; | |
823 | free_space = leaf_free_space(right); | |
824 | if (free_space < data_size + sizeof(struct item)) { | |
825 | tree_block_release(root, right_buf); | |
826 | return 1; | |
827 | } | |
02217ed2 CM |
828 | /* cow and double check */ |
829 | btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf); | |
830 | right = &right_buf->leaf; | |
831 | free_space = leaf_free_space(right); | |
832 | if (free_space < data_size + sizeof(struct item)) { | |
833 | tree_block_release(root, right_buf); | |
834 | return 1; | |
835 | } | |
836 | ||
7518a238 CM |
837 | left_nritems = btrfs_header_nritems(&left->header); |
838 | for (i = left_nritems - 1; i >= 0; i--) { | |
00ec4c51 CM |
839 | item = left->items + i; |
840 | if (path->slots[0] == i) | |
841 | push_space += data_size + sizeof(*item); | |
842 | if (item->size + sizeof(*item) + push_space > free_space) | |
843 | break; | |
844 | push_items++; | |
845 | push_space += item->size + sizeof(*item); | |
846 | } | |
847 | if (push_items == 0) { | |
848 | tree_block_release(root, right_buf); | |
849 | return 1; | |
850 | } | |
7518a238 | 851 | right_nritems = btrfs_header_nritems(&right->header); |
00ec4c51 | 852 | /* push left to right */ |
7518a238 CM |
853 | push_space = left->items[left_nritems - push_items].offset + |
854 | left->items[left_nritems - push_items].size; | |
00ec4c51 CM |
855 | push_space -= leaf_data_end(left); |
856 | /* make room in the right data area */ | |
857 | memmove(right->data + leaf_data_end(right) - push_space, | |
858 | right->data + leaf_data_end(right), | |
859 | LEAF_DATA_SIZE - leaf_data_end(right)); | |
860 | /* copy from the left data area */ | |
861 | memcpy(right->data + LEAF_DATA_SIZE - push_space, | |
862 | left->data + leaf_data_end(left), | |
863 | push_space); | |
864 | memmove(right->items + push_items, right->items, | |
7518a238 | 865 | right_nritems * sizeof(struct item)); |
00ec4c51 | 866 | /* copy the items from left to right */ |
7518a238 | 867 | memcpy(right->items, left->items + left_nritems - push_items, |
00ec4c51 CM |
868 | push_items * sizeof(struct item)); |
869 | ||
870 | /* update the item pointers */ | |
7518a238 CM |
871 | right_nritems += push_items; |
872 | btrfs_set_header_nritems(&right->header, right_nritems); | |
00ec4c51 | 873 | push_space = LEAF_DATA_SIZE; |
7518a238 | 874 | for (i = 0; i < right_nritems; i++) { |
00ec4c51 CM |
875 | right->items[i].offset = push_space - right->items[i].size; |
876 | push_space = right->items[i].offset; | |
877 | } | |
7518a238 CM |
878 | left_nritems -= push_items; |
879 | btrfs_set_header_nritems(&left->header, left_nritems); | |
00ec4c51 | 880 | |
02217ed2 CM |
881 | BUG_ON(list_empty(&left_buf->dirty)); |
882 | BUG_ON(list_empty(&right_buf->dirty)); | |
00ec4c51 | 883 | memcpy(upper->node.keys + slot + 1, |
e2fa7227 | 884 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
02217ed2 CM |
885 | BUG_ON(list_empty(&upper->dirty)); |
886 | ||
00ec4c51 | 887 | /* then fixup the leaf pointer in the path */ |
7518a238 CM |
888 | if (path->slots[0] >= left_nritems) { |
889 | path->slots[0] -= left_nritems; | |
00ec4c51 CM |
890 | tree_block_release(root, path->nodes[0]); |
891 | path->nodes[0] = right_buf; | |
892 | path->slots[1] += 1; | |
893 | } else { | |
894 | tree_block_release(root, right_buf); | |
895 | } | |
896 | return 0; | |
897 | } | |
74123bd7 CM |
898 | /* |
899 | * push some data in the path leaf to the left, trying to free up at | |
900 | * least data_size bytes. returns zero if the push worked, nonzero otherwise | |
901 | */ | |
aa5d6bed CM |
902 | static int push_leaf_left(struct ctree_root *root, struct ctree_path *path, |
903 | int data_size) | |
be0e5c09 | 904 | { |
eb60ceac CM |
905 | struct tree_buffer *right_buf = path->nodes[0]; |
906 | struct leaf *right = &right_buf->leaf; | |
907 | struct tree_buffer *t; | |
be0e5c09 CM |
908 | struct leaf *left; |
909 | int slot; | |
910 | int i; | |
911 | int free_space; | |
912 | int push_space = 0; | |
913 | int push_items = 0; | |
914 | struct item *item; | |
7518a238 | 915 | u32 old_left_nritems; |
aa5d6bed CM |
916 | int ret = 0; |
917 | int wret; | |
be0e5c09 CM |
918 | |
919 | slot = path->slots[1]; | |
920 | if (slot == 0) { | |
921 | return 1; | |
922 | } | |
923 | if (!path->nodes[1]) { | |
924 | return 1; | |
925 | } | |
eb60ceac CM |
926 | t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]); |
927 | left = &t->leaf; | |
be0e5c09 CM |
928 | free_space = leaf_free_space(left); |
929 | if (free_space < data_size + sizeof(struct item)) { | |
eb60ceac | 930 | tree_block_release(root, t); |
be0e5c09 CM |
931 | return 1; |
932 | } | |
02217ed2 CM |
933 | |
934 | /* cow and double check */ | |
935 | btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t); | |
936 | left = &t->leaf; | |
937 | free_space = leaf_free_space(left); | |
938 | if (free_space < data_size + sizeof(struct item)) { | |
939 | tree_block_release(root, t); | |
940 | return 1; | |
941 | } | |
942 | ||
7518a238 | 943 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
be0e5c09 CM |
944 | item = right->items + i; |
945 | if (path->slots[0] == i) | |
946 | push_space += data_size + sizeof(*item); | |
947 | if (item->size + sizeof(*item) + push_space > free_space) | |
948 | break; | |
949 | push_items++; | |
950 | push_space += item->size + sizeof(*item); | |
951 | } | |
952 | if (push_items == 0) { | |
eb60ceac | 953 | tree_block_release(root, t); |
be0e5c09 CM |
954 | return 1; |
955 | } | |
956 | /* push data from right to left */ | |
7518a238 | 957 | memcpy(left->items + btrfs_header_nritems(&left->header), |
be0e5c09 CM |
958 | right->items, push_items * sizeof(struct item)); |
959 | push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset; | |
960 | memcpy(left->data + leaf_data_end(left) - push_space, | |
961 | right->data + right->items[push_items - 1].offset, | |
962 | push_space); | |
7518a238 | 963 | old_left_nritems = btrfs_header_nritems(&left->header); |
eb60ceac CM |
964 | BUG_ON(old_left_nritems < 0); |
965 | ||
be0e5c09 CM |
966 | for(i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
967 | left->items[i].offset -= LEAF_DATA_SIZE - | |
968 | left->items[old_left_nritems -1].offset; | |
969 | } | |
7518a238 | 970 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
be0e5c09 CM |
971 | |
972 | /* fixup right node */ | |
973 | push_space = right->items[push_items-1].offset - leaf_data_end(right); | |
974 | memmove(right->data + LEAF_DATA_SIZE - push_space, right->data + | |
975 | leaf_data_end(right), push_space); | |
976 | memmove(right->items, right->items + push_items, | |
7518a238 CM |
977 | (btrfs_header_nritems(&right->header) - push_items) * |
978 | sizeof(struct item)); | |
979 | btrfs_set_header_nritems(&right->header, | |
980 | btrfs_header_nritems(&right->header) - | |
981 | push_items); | |
be0e5c09 | 982 | push_space = LEAF_DATA_SIZE; |
eb60ceac | 983 | |
7518a238 | 984 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
be0e5c09 CM |
985 | right->items[i].offset = push_space - right->items[i].size; |
986 | push_space = right->items[i].offset; | |
987 | } | |
eb60ceac | 988 | |
02217ed2 CM |
989 | BUG_ON(list_empty(&t->dirty)); |
990 | BUG_ON(list_empty(&right_buf->dirty)); | |
eb60ceac | 991 | |
aa5d6bed CM |
992 | wret = fixup_low_keys(root, path, &right->items[0].key, 1); |
993 | if (wret) | |
994 | ret = wret; | |
be0e5c09 CM |
995 | |
996 | /* then fixup the leaf pointer in the path */ | |
997 | if (path->slots[0] < push_items) { | |
998 | path->slots[0] += old_left_nritems; | |
eb60ceac CM |
999 | tree_block_release(root, path->nodes[0]); |
1000 | path->nodes[0] = t; | |
be0e5c09 CM |
1001 | path->slots[1] -= 1; |
1002 | } else { | |
eb60ceac | 1003 | tree_block_release(root, t); |
be0e5c09 CM |
1004 | path->slots[0] -= push_items; |
1005 | } | |
eb60ceac | 1006 | BUG_ON(path->slots[0] < 0); |
aa5d6bed | 1007 | return ret; |
be0e5c09 CM |
1008 | } |
1009 | ||
74123bd7 CM |
1010 | /* |
1011 | * split the path's leaf in two, making sure there is at least data_size | |
1012 | * available for the resulting leaf level of the path. | |
aa5d6bed CM |
1013 | * |
1014 | * returns 0 if all went well and < 0 on failure. | |
74123bd7 | 1015 | */ |
aa5d6bed CM |
1016 | static int split_leaf(struct ctree_root *root, struct ctree_path *path, |
1017 | int data_size) | |
be0e5c09 | 1018 | { |
aa5d6bed CM |
1019 | struct tree_buffer *l_buf; |
1020 | struct leaf *l; | |
7518a238 | 1021 | u32 nritems; |
eb60ceac CM |
1022 | int mid; |
1023 | int slot; | |
be0e5c09 | 1024 | struct leaf *right; |
eb60ceac | 1025 | struct tree_buffer *right_buffer; |
be0e5c09 CM |
1026 | int space_needed = data_size + sizeof(struct item); |
1027 | int data_copy_size; | |
1028 | int rt_data_off; | |
1029 | int i; | |
1030 | int ret; | |
aa5d6bed CM |
1031 | int wret; |
1032 | ||
aa5d6bed CM |
1033 | l_buf = path->nodes[0]; |
1034 | l = &l_buf->leaf; | |
1035 | ||
1036 | /* did the pushes work? */ | |
1037 | if (leaf_free_space(l) >= sizeof(struct item) + data_size) | |
1038 | return 0; | |
1039 | ||
5c680ed6 CM |
1040 | if (!path->nodes[1]) { |
1041 | ret = insert_new_root(root, path, 1); | |
1042 | if (ret) | |
1043 | return ret; | |
1044 | } | |
eb60ceac | 1045 | slot = path->slots[0]; |
7518a238 | 1046 | nritems = btrfs_header_nritems(&l->header); |
eb60ceac | 1047 | mid = (nritems + 1)/ 2; |
eb60ceac CM |
1048 | right_buffer = alloc_free_block(root); |
1049 | BUG_ON(!right_buffer); | |
1050 | BUG_ON(mid == nritems); | |
1051 | right = &right_buffer->leaf; | |
be0e5c09 CM |
1052 | memset(right, 0, sizeof(*right)); |
1053 | if (mid <= slot) { | |
97571fd0 | 1054 | /* FIXME, just alloc a new leaf here */ |
be0e5c09 CM |
1055 | if (leaf_space_used(l, mid, nritems - mid) + space_needed > |
1056 | LEAF_DATA_SIZE) | |
1057 | BUG(); | |
1058 | } else { | |
97571fd0 | 1059 | /* FIXME, just alloc a new leaf here */ |
be0e5c09 CM |
1060 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
1061 | LEAF_DATA_SIZE) | |
1062 | BUG(); | |
1063 | } | |
7518a238 CM |
1064 | btrfs_set_header_nritems(&right->header, nritems - mid); |
1065 | btrfs_set_header_blocknr(&right->header, right_buffer->blocknr); | |
1066 | btrfs_set_header_level(&right->header, 0); | |
1067 | btrfs_set_header_parentid(&right->header, | |
1068 | btrfs_header_parentid(&root->node->node.header)); | |
be0e5c09 CM |
1069 | data_copy_size = l->items[mid].offset + l->items[mid].size - |
1070 | leaf_data_end(l); | |
1071 | memcpy(right->items, l->items + mid, | |
1072 | (nritems - mid) * sizeof(struct item)); | |
1073 | memcpy(right->data + LEAF_DATA_SIZE - data_copy_size, | |
1074 | l->data + leaf_data_end(l), data_copy_size); | |
1075 | rt_data_off = LEAF_DATA_SIZE - | |
1076 | (l->items[mid].offset + l->items[mid].size); | |
74123bd7 | 1077 | |
7518a238 | 1078 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) |
be0e5c09 | 1079 | right->items[i].offset += rt_data_off; |
74123bd7 | 1080 | |
7518a238 | 1081 | btrfs_set_header_nritems(&l->header, mid); |
aa5d6bed CM |
1082 | ret = 0; |
1083 | wret = insert_ptr(root, path, &right->items[0].key, | |
5c680ed6 | 1084 | right_buffer->blocknr, path->slots[1] + 1, 1); |
aa5d6bed CM |
1085 | if (wret) |
1086 | ret = wret; | |
02217ed2 CM |
1087 | BUG_ON(list_empty(&right_buffer->dirty)); |
1088 | BUG_ON(list_empty(&l_buf->dirty)); | |
eb60ceac | 1089 | BUG_ON(path->slots[0] != slot); |
be0e5c09 | 1090 | if (mid <= slot) { |
eb60ceac CM |
1091 | tree_block_release(root, path->nodes[0]); |
1092 | path->nodes[0] = right_buffer; | |
be0e5c09 CM |
1093 | path->slots[0] -= mid; |
1094 | path->slots[1] += 1; | |
eb60ceac CM |
1095 | } else |
1096 | tree_block_release(root, right_buffer); | |
1097 | BUG_ON(path->slots[0] < 0); | |
be0e5c09 CM |
1098 | return ret; |
1099 | } | |
1100 | ||
74123bd7 CM |
1101 | /* |
1102 | * Given a key and some data, insert an item into the tree. | |
1103 | * This does all the path init required, making room in the tree if needed. | |
1104 | */ | |
e2fa7227 | 1105 | int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key, |
be0e5c09 CM |
1106 | void *data, int data_size) |
1107 | { | |
aa5d6bed | 1108 | int ret = 0; |
be0e5c09 | 1109 | int slot; |
eb60ceac | 1110 | int slot_orig; |
be0e5c09 | 1111 | struct leaf *leaf; |
eb60ceac | 1112 | struct tree_buffer *leaf_buf; |
7518a238 | 1113 | u32 nritems; |
be0e5c09 CM |
1114 | unsigned int data_end; |
1115 | struct ctree_path path; | |
e2fa7227 CM |
1116 | struct btrfs_disk_key disk_key; |
1117 | ||
1118 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); | |
be0e5c09 | 1119 | |
74123bd7 | 1120 | /* create a root if there isn't one */ |
5c680ed6 | 1121 | if (!root->node) |
cfaa7295 | 1122 | BUG(); |
be0e5c09 | 1123 | init_path(&path); |
e2fa7227 | 1124 | ret = search_slot(root, cpu_key, &path, data_size, 1); |
eb60ceac CM |
1125 | if (ret == 0) { |
1126 | release_path(root, &path); | |
f0930a37 | 1127 | return -EEXIST; |
aa5d6bed | 1128 | } |
ed2ff2cb CM |
1129 | if (ret < 0) |
1130 | goto out; | |
be0e5c09 | 1131 | |
eb60ceac CM |
1132 | slot_orig = path.slots[0]; |
1133 | leaf_buf = path.nodes[0]; | |
1134 | leaf = &leaf_buf->leaf; | |
74123bd7 | 1135 | |
7518a238 | 1136 | nritems = btrfs_header_nritems(&leaf->header); |
be0e5c09 | 1137 | data_end = leaf_data_end(leaf); |
eb60ceac | 1138 | |
be0e5c09 CM |
1139 | if (leaf_free_space(leaf) < sizeof(struct item) + data_size) |
1140 | BUG(); | |
1141 | ||
1142 | slot = path.slots[0]; | |
eb60ceac | 1143 | BUG_ON(slot < 0); |
be0e5c09 CM |
1144 | if (slot != nritems) { |
1145 | int i; | |
1146 | unsigned int old_data = leaf->items[slot].offset + | |
1147 | leaf->items[slot].size; | |
1148 | ||
1149 | /* | |
1150 | * item0..itemN ... dataN.offset..dataN.size .. data0.size | |
1151 | */ | |
1152 | /* first correct the data pointers */ | |
1153 | for (i = slot; i < nritems; i++) | |
1154 | leaf->items[i].offset -= data_size; | |
1155 | ||
1156 | /* shift the items */ | |
1157 | memmove(leaf->items + slot + 1, leaf->items + slot, | |
1158 | (nritems - slot) * sizeof(struct item)); | |
1159 | ||
1160 | /* shift the data */ | |
1161 | memmove(leaf->data + data_end - data_size, leaf->data + | |
1162 | data_end, old_data - data_end); | |
1163 | data_end = old_data; | |
1164 | } | |
74123bd7 | 1165 | /* copy the new data in */ |
e2fa7227 CM |
1166 | memcpy(&leaf->items[slot].key, &disk_key, |
1167 | sizeof(struct btrfs_disk_key)); | |
be0e5c09 CM |
1168 | leaf->items[slot].offset = data_end - data_size; |
1169 | leaf->items[slot].size = data_size; | |
1170 | memcpy(leaf->data + data_end - data_size, data, data_size); | |
7518a238 | 1171 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
aa5d6bed CM |
1172 | |
1173 | ret = 0; | |
8e19f2cd | 1174 | if (slot == 0) |
e2fa7227 | 1175 | ret = fixup_low_keys(root, &path, &disk_key, 1); |
aa5d6bed | 1176 | |
02217ed2 | 1177 | BUG_ON(list_empty(&leaf_buf->dirty)); |
be0e5c09 CM |
1178 | if (leaf_free_space(leaf) < 0) |
1179 | BUG(); | |
bb803951 | 1180 | check_leaf(&path, 0); |
ed2ff2cb | 1181 | out: |
eb60ceac | 1182 | release_path(root, &path); |
aa5d6bed | 1183 | return ret; |
be0e5c09 CM |
1184 | } |
1185 | ||
74123bd7 | 1186 | /* |
5de08d7d | 1187 | * delete the pointer from a given node. |
74123bd7 CM |
1188 | * |
1189 | * If the delete empties a node, the node is removed from the tree, | |
1190 | * continuing all the way the root if required. The root is converted into | |
1191 | * a leaf if all the nodes are emptied. | |
1192 | */ | |
bb803951 CM |
1193 | static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level, |
1194 | int slot) | |
be0e5c09 | 1195 | { |
be0e5c09 | 1196 | struct node *node; |
bb803951 | 1197 | struct tree_buffer *parent = path->nodes[level]; |
7518a238 | 1198 | u32 nritems; |
aa5d6bed | 1199 | int ret = 0; |
bb803951 | 1200 | int wret; |
be0e5c09 | 1201 | |
bb803951 | 1202 | node = &parent->node; |
7518a238 | 1203 | nritems = btrfs_header_nritems(&node->header); |
bb803951 CM |
1204 | if (slot != nritems -1) { |
1205 | memmove(node->keys + slot, node->keys + slot + 1, | |
e2fa7227 | 1206 | sizeof(struct btrfs_disk_key) * (nritems - slot - 1)); |
bb803951 CM |
1207 | memmove(node->blockptrs + slot, |
1208 | node->blockptrs + slot + 1, | |
1209 | sizeof(u64) * (nritems - slot - 1)); | |
1210 | } | |
7518a238 CM |
1211 | nritems--; |
1212 | btrfs_set_header_nritems(&node->header, nritems); | |
1213 | if (nritems == 0 && parent == root->node) { | |
1214 | BUG_ON(btrfs_header_level(&root->node->node.header) != 1); | |
bb803951 | 1215 | /* just turn the root into a leaf and break */ |
7518a238 | 1216 | btrfs_set_header_level(&root->node->node.header, 0); |
bb803951 CM |
1217 | } else if (slot == 0) { |
1218 | wret = fixup_low_keys(root, path, node->keys, level + 1); | |
0f70abe2 CM |
1219 | if (wret) |
1220 | ret = wret; | |
be0e5c09 | 1221 | } |
02217ed2 | 1222 | BUG_ON(list_empty(&parent->dirty)); |
aa5d6bed | 1223 | return ret; |
be0e5c09 CM |
1224 | } |
1225 | ||
74123bd7 CM |
1226 | /* |
1227 | * delete the item at the leaf level in path. If that empties | |
1228 | * the leaf, remove it from the tree | |
1229 | */ | |
4920c9ac | 1230 | int del_item(struct ctree_root *root, struct ctree_path *path) |
be0e5c09 | 1231 | { |
be0e5c09 CM |
1232 | int slot; |
1233 | struct leaf *leaf; | |
eb60ceac | 1234 | struct tree_buffer *leaf_buf; |
be0e5c09 CM |
1235 | int doff; |
1236 | int dsize; | |
aa5d6bed CM |
1237 | int ret = 0; |
1238 | int wret; | |
7518a238 | 1239 | u32 nritems; |
be0e5c09 | 1240 | |
eb60ceac CM |
1241 | leaf_buf = path->nodes[0]; |
1242 | leaf = &leaf_buf->leaf; | |
4920c9ac | 1243 | slot = path->slots[0]; |
be0e5c09 CM |
1244 | doff = leaf->items[slot].offset; |
1245 | dsize = leaf->items[slot].size; | |
7518a238 | 1246 | nritems = btrfs_header_nritems(&leaf->header); |
be0e5c09 | 1247 | |
7518a238 | 1248 | if (slot != nritems - 1) { |
be0e5c09 CM |
1249 | int i; |
1250 | int data_end = leaf_data_end(leaf); | |
1251 | memmove(leaf->data + data_end + dsize, | |
1252 | leaf->data + data_end, | |
1253 | doff - data_end); | |
7518a238 | 1254 | for (i = slot + 1; i < nritems; i++) |
be0e5c09 CM |
1255 | leaf->items[i].offset += dsize; |
1256 | memmove(leaf->items + slot, leaf->items + slot + 1, | |
1257 | sizeof(struct item) * | |
7518a238 | 1258 | (nritems - slot - 1)); |
be0e5c09 | 1259 | } |
7518a238 CM |
1260 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
1261 | nritems--; | |
74123bd7 | 1262 | /* delete the leaf if we've emptied it */ |
7518a238 | 1263 | if (nritems == 0) { |
eb60ceac | 1264 | if (leaf_buf == root->node) { |
7518a238 | 1265 | btrfs_set_header_level(&leaf->header, 0); |
02217ed2 | 1266 | BUG_ON(list_empty(&leaf_buf->dirty)); |
9a8dd150 | 1267 | } else { |
ed2ff2cb | 1268 | clean_tree_block(root, leaf_buf); |
bb803951 | 1269 | wret = del_ptr(root, path, 1, path->slots[1]); |
aa5d6bed CM |
1270 | if (wret) |
1271 | ret = wret; | |
0f70abe2 CM |
1272 | wret = free_extent(root, leaf_buf->blocknr, 1); |
1273 | if (wret) | |
1274 | ret = wret; | |
9a8dd150 | 1275 | } |
be0e5c09 | 1276 | } else { |
7518a238 | 1277 | int used = leaf_space_used(leaf, 0, nritems); |
aa5d6bed CM |
1278 | if (slot == 0) { |
1279 | wret = fixup_low_keys(root, path, | |
1280 | &leaf->items[0].key, 1); | |
1281 | if (wret) | |
1282 | ret = wret; | |
1283 | } | |
02217ed2 | 1284 | BUG_ON(list_empty(&leaf_buf->dirty)); |
aa5d6bed | 1285 | |
74123bd7 | 1286 | /* delete the leaf if it is mostly empty */ |
5de08d7d | 1287 | if (used < LEAF_DATA_SIZE / 3) { |
be0e5c09 CM |
1288 | /* push_leaf_left fixes the path. |
1289 | * make sure the path still points to our leaf | |
1290 | * for possible call to del_ptr below | |
1291 | */ | |
4920c9ac | 1292 | slot = path->slots[1]; |
eb60ceac | 1293 | leaf_buf->count++; |
aa5d6bed CM |
1294 | wret = push_leaf_left(root, path, 1); |
1295 | if (wret < 0) | |
1296 | ret = wret; | |
f0930a37 | 1297 | if (path->nodes[0] == leaf_buf && |
7518a238 | 1298 | btrfs_header_nritems(&leaf->header)) { |
aa5d6bed CM |
1299 | wret = push_leaf_right(root, path, 1); |
1300 | if (wret < 0) | |
1301 | ret = wret; | |
1302 | } | |
7518a238 | 1303 | if (btrfs_header_nritems(&leaf->header) == 0) { |
5de08d7d | 1304 | u64 blocknr = leaf_buf->blocknr; |
ed2ff2cb | 1305 | clean_tree_block(root, leaf_buf); |
bb803951 | 1306 | wret = del_ptr(root, path, 1, slot); |
aa5d6bed CM |
1307 | if (wret) |
1308 | ret = wret; | |
5de08d7d | 1309 | tree_block_release(root, leaf_buf); |
0f70abe2 CM |
1310 | wret = free_extent(root, blocknr, 1); |
1311 | if (wret) | |
1312 | ret = wret; | |
5de08d7d CM |
1313 | } else { |
1314 | tree_block_release(root, leaf_buf); | |
be0e5c09 CM |
1315 | } |
1316 | } | |
1317 | } | |
aa5d6bed | 1318 | return ret; |
be0e5c09 CM |
1319 | } |
1320 | ||
97571fd0 CM |
1321 | /* |
1322 | * walk up the tree as far as required to find the next leaf. | |
0f70abe2 CM |
1323 | * returns 0 if it found something or 1 if there are no greater leaves. |
1324 | * returns < 0 on io errors. | |
97571fd0 | 1325 | */ |
d97e63b6 CM |
1326 | int next_leaf(struct ctree_root *root, struct ctree_path *path) |
1327 | { | |
1328 | int slot; | |
1329 | int level = 1; | |
1330 | u64 blocknr; | |
1331 | struct tree_buffer *c; | |
cfaa7295 | 1332 | struct tree_buffer *next = NULL; |
d97e63b6 CM |
1333 | |
1334 | while(level < MAX_LEVEL) { | |
1335 | if (!path->nodes[level]) | |
0f70abe2 | 1336 | return 1; |
d97e63b6 CM |
1337 | slot = path->slots[level] + 1; |
1338 | c = path->nodes[level]; | |
7518a238 | 1339 | if (slot >= btrfs_header_nritems(&c->node.header)) { |
d97e63b6 CM |
1340 | level++; |
1341 | continue; | |
1342 | } | |
1343 | blocknr = c->node.blockptrs[slot]; | |
cfaa7295 CM |
1344 | if (next) |
1345 | tree_block_release(root, next); | |
d97e63b6 CM |
1346 | next = read_tree_block(root, blocknr); |
1347 | break; | |
1348 | } | |
1349 | path->slots[level] = slot; | |
1350 | while(1) { | |
1351 | level--; | |
1352 | c = path->nodes[level]; | |
1353 | tree_block_release(root, c); | |
1354 | path->nodes[level] = next; | |
1355 | path->slots[level] = 0; | |
1356 | if (!level) | |
1357 | break; | |
1358 | next = read_tree_block(root, next->node.blockptrs[0]); | |
1359 | } | |
1360 | return 0; | |
1361 | } | |
1362 | ||
02217ed2 | 1363 |