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