Btrfs: trivial include fixups
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / ctree.c
blobeace2ee76448d8a9fad4a9340aa319dbe2fa994c
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include "ctree.h"
20 #include "disk-io.h"
21 #include "transaction.h"
23 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
24 *root, struct btrfs_path *path, int level);
25 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_key *ins_key,
27 struct btrfs_path *path, int data_size);
28 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct buffer_head *dst, struct buffer_head
30 *src);
31 static int balance_node_right(struct btrfs_trans_handle *trans, struct
32 btrfs_root *root, struct buffer_head *dst_buf,
33 struct buffer_head *src_buf);
34 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
35 struct btrfs_path *path, int level, int slot);
37 inline void btrfs_init_path(struct btrfs_path *p)
39 memset(p, 0, sizeof(*p));
42 struct btrfs_path *btrfs_alloc_path(void)
44 struct btrfs_path *path;
45 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
46 if (path)
47 btrfs_init_path(path);
48 return path;
51 void btrfs_free_path(struct btrfs_path *p)
53 btrfs_release_path(NULL, p);
54 kmem_cache_free(btrfs_path_cachep, p);
57 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
59 int i;
60 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
61 if (!p->nodes[i])
62 break;
63 btrfs_block_release(root, p->nodes[i]);
65 memset(p, 0, sizeof(*p));
68 static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
69 *root, struct buffer_head *buf, struct buffer_head
70 *parent, int parent_slot, struct buffer_head
71 **cow_ret)
73 struct buffer_head *cow;
74 struct btrfs_node *cow_node;
75 int ret;
77 WARN_ON(!buffer_uptodate(buf));
78 if (trans->transaction != root->fs_info->running_transaction) {
79 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
80 root->fs_info->running_transaction->transid);
81 WARN_ON(1);
83 if (trans->transid != root->fs_info->generation) {
84 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
85 root->fs_info->generation);
86 WARN_ON(1);
88 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
89 trans->transid) {
90 *cow_ret = buf;
91 return 0;
93 cow = btrfs_alloc_free_block(trans, root, buf->b_blocknr);
94 if (IS_ERR(cow))
95 return PTR_ERR(cow);
96 cow_node = btrfs_buffer_node(cow);
97 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
98 WARN_ON(1);
99 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
100 btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
101 btrfs_set_header_generation(&cow_node->header, trans->transid);
102 btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
103 ret = btrfs_inc_ref(trans, root, buf);
104 if (ret)
105 return ret;
106 if (buf == root->node) {
107 root->node = cow;
108 get_bh(cow);
109 if (buf != root->commit_root) {
110 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
112 btrfs_block_release(root, buf);
113 } else {
114 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
115 bh_blocknr(cow));
116 btrfs_mark_buffer_dirty(parent);
117 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
119 btrfs_block_release(root, buf);
120 btrfs_mark_buffer_dirty(cow);
121 *cow_ret = cow;
122 return 0;
126 * The leaf data grows from end-to-front in the node.
127 * this returns the address of the start of the last item,
128 * which is the stop of the leaf data stack
130 static inline unsigned int leaf_data_end(struct btrfs_root *root,
131 struct btrfs_leaf *leaf)
133 u32 nr = btrfs_header_nritems(&leaf->header);
134 if (nr == 0)
135 return BTRFS_LEAF_DATA_SIZE(root);
136 return btrfs_item_offset(leaf->items + nr - 1);
140 * compare two keys in a memcmp fashion
142 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
144 struct btrfs_key k1;
146 btrfs_disk_key_to_cpu(&k1, disk);
148 if (k1.objectid > k2->objectid)
149 return 1;
150 if (k1.objectid < k2->objectid)
151 return -1;
152 if (k1.flags > k2->flags)
153 return 1;
154 if (k1.flags < k2->flags)
155 return -1;
156 if (k1.offset > k2->offset)
157 return 1;
158 if (k1.offset < k2->offset)
159 return -1;
160 return 0;
163 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
164 int level)
166 struct btrfs_node *parent = NULL;
167 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
168 int parent_slot;
169 int slot;
170 struct btrfs_key cpukey;
171 u32 nritems = btrfs_header_nritems(&node->header);
173 if (path->nodes[level + 1])
174 parent = btrfs_buffer_node(path->nodes[level + 1]);
175 parent_slot = path->slots[level + 1];
176 slot = path->slots[level];
177 BUG_ON(nritems == 0);
178 if (parent) {
179 struct btrfs_disk_key *parent_key;
180 parent_key = &parent->ptrs[parent_slot].key;
181 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
182 sizeof(struct btrfs_disk_key)));
183 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
184 btrfs_header_blocknr(&node->header));
186 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
187 if (slot != 0) {
188 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
189 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
191 if (slot < nritems - 1) {
192 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
193 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
195 return 0;
198 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
199 int level)
201 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
202 struct btrfs_node *parent = NULL;
203 int parent_slot;
204 int slot = path->slots[0];
205 struct btrfs_key cpukey;
207 u32 nritems = btrfs_header_nritems(&leaf->header);
209 if (path->nodes[level + 1])
210 parent = btrfs_buffer_node(path->nodes[level + 1]);
211 parent_slot = path->slots[level + 1];
212 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
214 if (nritems == 0)
215 return 0;
217 if (parent) {
218 struct btrfs_disk_key *parent_key;
219 parent_key = &parent->ptrs[parent_slot].key;
220 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
221 sizeof(struct btrfs_disk_key)));
222 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
223 btrfs_header_blocknr(&leaf->header));
225 if (slot != 0) {
226 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
227 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
228 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
229 btrfs_item_end(leaf->items + slot));
231 if (slot < nritems - 1) {
232 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
233 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
234 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
235 btrfs_item_end(leaf->items + slot + 1));
237 BUG_ON(btrfs_item_offset(leaf->items) +
238 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
239 return 0;
242 static int check_block(struct btrfs_root *root, struct btrfs_path *path,
243 int level)
245 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
246 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
247 sizeof(node->header.fsid)))
248 BUG();
249 if (level == 0)
250 return check_leaf(root, path, level);
251 return check_node(root, path, level);
255 * search for key in the array p. items p are item_size apart
256 * and there are 'max' items in p
257 * the slot in the array is returned via slot, and it points to
258 * the place where you would insert key if it is not found in
259 * the array.
261 * slot may point to max if the key is bigger than all of the keys
263 static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
264 int max, int *slot)
266 int low = 0;
267 int high = max;
268 int mid;
269 int ret;
270 struct btrfs_disk_key *tmp;
272 while(low < high) {
273 mid = (low + high) / 2;
274 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
275 ret = comp_keys(tmp, key);
277 if (ret < 0)
278 low = mid + 1;
279 else if (ret > 0)
280 high = mid;
281 else {
282 *slot = mid;
283 return 0;
286 *slot = low;
287 return 1;
291 * simple bin_search frontend that does the right thing for
292 * leaves vs nodes
294 static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
296 if (btrfs_is_leaf(c)) {
297 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
298 return generic_bin_search((void *)l->items,
299 sizeof(struct btrfs_item),
300 key, btrfs_header_nritems(&c->header),
301 slot);
302 } else {
303 return generic_bin_search((void *)c->ptrs,
304 sizeof(struct btrfs_key_ptr),
305 key, btrfs_header_nritems(&c->header),
306 slot);
308 return -1;
311 static struct buffer_head *read_node_slot(struct btrfs_root *root,
312 struct buffer_head *parent_buf,
313 int slot)
315 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
316 if (slot < 0)
317 return NULL;
318 if (slot >= btrfs_header_nritems(&node->header))
319 return NULL;
320 return read_tree_block(root, btrfs_node_blockptr(node, slot));
323 static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
324 *root, struct btrfs_path *path, int level)
326 struct buffer_head *right_buf;
327 struct buffer_head *mid_buf;
328 struct buffer_head *left_buf;
329 struct buffer_head *parent_buf = NULL;
330 struct btrfs_node *right = NULL;
331 struct btrfs_node *mid;
332 struct btrfs_node *left = NULL;
333 struct btrfs_node *parent = NULL;
334 int ret = 0;
335 int wret;
336 int pslot;
337 int orig_slot = path->slots[level];
338 int err_on_enospc = 0;
339 u64 orig_ptr;
341 if (level == 0)
342 return 0;
344 mid_buf = path->nodes[level];
345 mid = btrfs_buffer_node(mid_buf);
346 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
348 if (level < BTRFS_MAX_LEVEL - 1)
349 parent_buf = path->nodes[level + 1];
350 pslot = path->slots[level + 1];
353 * deal with the case where there is only one pointer in the root
354 * by promoting the node below to a root
356 if (!parent_buf) {
357 struct buffer_head *child;
358 u64 blocknr = bh_blocknr(mid_buf);
360 if (btrfs_header_nritems(&mid->header) != 1)
361 return 0;
363 /* promote the child to a root */
364 child = read_node_slot(root, mid_buf, 0);
365 BUG_ON(!child);
366 root->node = child;
367 path->nodes[level] = NULL;
368 clean_tree_block(trans, root, mid_buf);
369 wait_on_buffer(mid_buf);
370 /* once for the path */
371 btrfs_block_release(root, mid_buf);
372 /* once for the root ptr */
373 btrfs_block_release(root, mid_buf);
374 return btrfs_free_extent(trans, root, blocknr, 1, 1);
376 parent = btrfs_buffer_node(parent_buf);
378 if (btrfs_header_nritems(&mid->header) >
379 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
380 return 0;
382 if (btrfs_header_nritems(&mid->header) < 2)
383 err_on_enospc = 1;
385 left_buf = read_node_slot(root, parent_buf, pslot - 1);
386 right_buf = read_node_slot(root, parent_buf, pslot + 1);
388 /* first, try to make some room in the middle buffer */
389 if (left_buf) {
390 wret = btrfs_cow_block(trans, root, left_buf,
391 parent_buf, pslot - 1, &left_buf);
392 if (wret) {
393 ret = wret;
394 goto enospc;
396 left = btrfs_buffer_node(left_buf);
397 orig_slot += btrfs_header_nritems(&left->header);
398 wret = push_node_left(trans, root, left_buf, mid_buf);
399 if (wret < 0)
400 ret = wret;
401 if (btrfs_header_nritems(&mid->header) < 2)
402 err_on_enospc = 1;
406 * then try to empty the right most buffer into the middle
408 if (right_buf) {
409 wret = btrfs_cow_block(trans, root, right_buf,
410 parent_buf, pslot + 1, &right_buf);
411 if (wret) {
412 ret = wret;
413 goto enospc;
416 right = btrfs_buffer_node(right_buf);
417 wret = push_node_left(trans, root, mid_buf, right_buf);
418 if (wret < 0 && wret != -ENOSPC)
419 ret = wret;
420 if (btrfs_header_nritems(&right->header) == 0) {
421 u64 blocknr = bh_blocknr(right_buf);
422 clean_tree_block(trans, root, right_buf);
423 wait_on_buffer(right_buf);
424 btrfs_block_release(root, right_buf);
425 right_buf = NULL;
426 right = NULL;
427 wret = del_ptr(trans, root, path, level + 1, pslot +
429 if (wret)
430 ret = wret;
431 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
432 if (wret)
433 ret = wret;
434 } else {
435 btrfs_memcpy(root, parent,
436 &parent->ptrs[pslot + 1].key,
437 &right->ptrs[0].key,
438 sizeof(struct btrfs_disk_key));
439 btrfs_mark_buffer_dirty(parent_buf);
442 if (btrfs_header_nritems(&mid->header) == 1) {
444 * we're not allowed to leave a node with one item in the
445 * tree during a delete. A deletion from lower in the tree
446 * could try to delete the only pointer in this node.
447 * So, pull some keys from the left.
448 * There has to be a left pointer at this point because
449 * otherwise we would have pulled some pointers from the
450 * right
452 BUG_ON(!left_buf);
453 wret = balance_node_right(trans, root, mid_buf, left_buf);
454 if (wret < 0) {
455 ret = wret;
456 goto enospc;
458 BUG_ON(wret == 1);
460 if (btrfs_header_nritems(&mid->header) == 0) {
461 /* we've managed to empty the middle node, drop it */
462 u64 blocknr = bh_blocknr(mid_buf);
463 clean_tree_block(trans, root, mid_buf);
464 wait_on_buffer(mid_buf);
465 btrfs_block_release(root, mid_buf);
466 mid_buf = NULL;
467 mid = NULL;
468 wret = del_ptr(trans, root, path, level + 1, pslot);
469 if (wret)
470 ret = wret;
471 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
472 if (wret)
473 ret = wret;
474 } else {
475 /* update the parent key to reflect our changes */
476 btrfs_memcpy(root, parent,
477 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
478 sizeof(struct btrfs_disk_key));
479 btrfs_mark_buffer_dirty(parent_buf);
482 /* update the path */
483 if (left_buf) {
484 if (btrfs_header_nritems(&left->header) > orig_slot) {
485 get_bh(left_buf);
486 path->nodes[level] = left_buf;
487 path->slots[level + 1] -= 1;
488 path->slots[level] = orig_slot;
489 if (mid_buf)
490 btrfs_block_release(root, mid_buf);
491 } else {
492 orig_slot -= btrfs_header_nritems(&left->header);
493 path->slots[level] = orig_slot;
496 /* double check we haven't messed things up */
497 check_block(root, path, level);
498 if (orig_ptr !=
499 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
500 path->slots[level]))
501 BUG();
502 enospc:
503 if (right_buf)
504 btrfs_block_release(root, right_buf);
505 if (left_buf)
506 btrfs_block_release(root, left_buf);
507 return ret;
510 /* returns zero if the push worked, non-zero otherwise */
511 static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
513 struct btrfs_path *path, int level)
515 struct buffer_head *right_buf;
516 struct buffer_head *mid_buf;
517 struct buffer_head *left_buf;
518 struct buffer_head *parent_buf = NULL;
519 struct btrfs_node *right = NULL;
520 struct btrfs_node *mid;
521 struct btrfs_node *left = NULL;
522 struct btrfs_node *parent = NULL;
523 int ret = 0;
524 int wret;
525 int pslot;
526 int orig_slot = path->slots[level];
527 u64 orig_ptr;
529 if (level == 0)
530 return 1;
532 mid_buf = path->nodes[level];
533 mid = btrfs_buffer_node(mid_buf);
534 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
536 if (level < BTRFS_MAX_LEVEL - 1)
537 parent_buf = path->nodes[level + 1];
538 pslot = path->slots[level + 1];
540 if (!parent_buf)
541 return 1;
542 parent = btrfs_buffer_node(parent_buf);
544 left_buf = read_node_slot(root, parent_buf, pslot - 1);
546 /* first, try to make some room in the middle buffer */
547 if (left_buf) {
548 u32 left_nr;
549 left = btrfs_buffer_node(left_buf);
550 left_nr = btrfs_header_nritems(&left->header);
551 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
552 wret = 1;
553 } else {
554 ret = btrfs_cow_block(trans, root, left_buf, parent_buf,
555 pslot - 1, &left_buf);
556 if (ret)
557 wret = 1;
558 else {
559 left = btrfs_buffer_node(left_buf);
560 wret = push_node_left(trans, root,
561 left_buf, mid_buf);
564 if (wret < 0)
565 ret = wret;
566 if (wret == 0) {
567 orig_slot += left_nr;
568 btrfs_memcpy(root, parent,
569 &parent->ptrs[pslot].key,
570 &mid->ptrs[0].key,
571 sizeof(struct btrfs_disk_key));
572 btrfs_mark_buffer_dirty(parent_buf);
573 if (btrfs_header_nritems(&left->header) > orig_slot) {
574 path->nodes[level] = left_buf;
575 path->slots[level + 1] -= 1;
576 path->slots[level] = orig_slot;
577 btrfs_block_release(root, mid_buf);
578 } else {
579 orig_slot -=
580 btrfs_header_nritems(&left->header);
581 path->slots[level] = orig_slot;
582 btrfs_block_release(root, left_buf);
584 check_node(root, path, level);
585 return 0;
587 btrfs_block_release(root, left_buf);
589 right_buf = read_node_slot(root, parent_buf, pslot + 1);
592 * then try to empty the right most buffer into the middle
594 if (right_buf) {
595 u32 right_nr;
596 right = btrfs_buffer_node(right_buf);
597 right_nr = btrfs_header_nritems(&right->header);
598 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
599 wret = 1;
600 } else {
601 ret = btrfs_cow_block(trans, root, right_buf,
602 parent_buf, pslot + 1,
603 &right_buf);
604 if (ret)
605 wret = 1;
606 else {
607 right = btrfs_buffer_node(right_buf);
608 wret = balance_node_right(trans, root,
609 right_buf, mid_buf);
612 if (wret < 0)
613 ret = wret;
614 if (wret == 0) {
615 btrfs_memcpy(root, parent,
616 &parent->ptrs[pslot + 1].key,
617 &right->ptrs[0].key,
618 sizeof(struct btrfs_disk_key));
619 btrfs_mark_buffer_dirty(parent_buf);
620 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
621 path->nodes[level] = right_buf;
622 path->slots[level + 1] += 1;
623 path->slots[level] = orig_slot -
624 btrfs_header_nritems(&mid->header);
625 btrfs_block_release(root, mid_buf);
626 } else {
627 btrfs_block_release(root, right_buf);
629 check_node(root, path, level);
630 return 0;
632 btrfs_block_release(root, right_buf);
634 check_node(root, path, level);
635 return 1;
639 * look for key in the tree. path is filled in with nodes along the way
640 * if key is found, we return zero and you can find the item in the leaf
641 * level of the path (level 0)
643 * If the key isn't found, the path points to the slot where it should
644 * be inserted, and 1 is returned. If there are other errors during the
645 * search a negative error number is returned.
647 * if ins_len > 0, nodes and leaves will be split as we walk down the
648 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
649 * possible)
651 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
652 *root, struct btrfs_key *key, struct btrfs_path *p, int
653 ins_len, int cow)
655 struct buffer_head *b;
656 struct buffer_head *cow_buf;
657 struct btrfs_node *c;
658 int slot;
659 int ret;
660 int level;
662 WARN_ON(p->nodes[0] != NULL);
663 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
664 again:
665 b = root->node;
666 get_bh(b);
667 while (b) {
668 c = btrfs_buffer_node(b);
669 level = btrfs_header_level(&c->header);
670 if (cow) {
671 int wret;
672 wret = btrfs_cow_block(trans, root, b,
673 p->nodes[level + 1],
674 p->slots[level + 1],
675 &cow_buf);
676 if (wret) {
677 btrfs_block_release(root, cow_buf);
678 return wret;
680 b = cow_buf;
681 c = btrfs_buffer_node(b);
683 BUG_ON(!cow && ins_len);
684 if (level != btrfs_header_level(&c->header))
685 WARN_ON(1);
686 level = btrfs_header_level(&c->header);
687 p->nodes[level] = b;
688 ret = check_block(root, p, level);
689 if (ret)
690 return -1;
691 ret = bin_search(c, key, &slot);
692 if (!btrfs_is_leaf(c)) {
693 if (ret && slot > 0)
694 slot -= 1;
695 p->slots[level] = slot;
696 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
697 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
698 int sret = split_node(trans, root, p, level);
699 BUG_ON(sret > 0);
700 if (sret)
701 return sret;
702 b = p->nodes[level];
703 c = btrfs_buffer_node(b);
704 slot = p->slots[level];
705 } else if (ins_len < 0) {
706 int sret = balance_level(trans, root, p,
707 level);
708 if (sret)
709 return sret;
710 b = p->nodes[level];
711 if (!b)
712 goto again;
713 c = btrfs_buffer_node(b);
714 slot = p->slots[level];
715 BUG_ON(btrfs_header_nritems(&c->header) == 1);
717 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
718 } else {
719 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
720 p->slots[level] = slot;
721 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
722 sizeof(struct btrfs_item) + ins_len) {
723 int sret = split_leaf(trans, root, key,
724 p, ins_len);
725 BUG_ON(sret > 0);
726 if (sret)
727 return sret;
729 return ret;
732 return 1;
736 * adjust the pointers going up the tree, starting at level
737 * making sure the right key of each node is points to 'key'.
738 * This is used after shifting pointers to the left, so it stops
739 * fixing up pointers when a given leaf/node is not in slot 0 of the
740 * higher levels
742 * If this fails to write a tree block, it returns -1, but continues
743 * fixing up the blocks in ram so the tree is consistent.
745 static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
746 *root, struct btrfs_path *path, struct btrfs_disk_key
747 *key, int level)
749 int i;
750 int ret = 0;
751 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
752 struct btrfs_node *t;
753 int tslot = path->slots[i];
754 if (!path->nodes[i])
755 break;
756 t = btrfs_buffer_node(path->nodes[i]);
757 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
758 btrfs_mark_buffer_dirty(path->nodes[i]);
759 if (tslot != 0)
760 break;
762 return ret;
766 * try to push data from one node into the next node left in the
767 * tree.
769 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
770 * error, and > 0 if there was no room in the left hand block.
772 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
773 *root, struct buffer_head *dst_buf, struct
774 buffer_head *src_buf)
776 struct btrfs_node *src = btrfs_buffer_node(src_buf);
777 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
778 int push_items = 0;
779 int src_nritems;
780 int dst_nritems;
781 int ret = 0;
783 src_nritems = btrfs_header_nritems(&src->header);
784 dst_nritems = btrfs_header_nritems(&dst->header);
785 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
787 if (push_items <= 0) {
788 return 1;
791 if (src_nritems < push_items)
792 push_items = src_nritems;
794 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
795 push_items * sizeof(struct btrfs_key_ptr));
796 if (push_items < src_nritems) {
797 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
798 (src_nritems - push_items) *
799 sizeof(struct btrfs_key_ptr));
801 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
802 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
803 btrfs_mark_buffer_dirty(src_buf);
804 btrfs_mark_buffer_dirty(dst_buf);
805 return ret;
809 * try to push data from one node into the next node right in the
810 * tree.
812 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
813 * error, and > 0 if there was no room in the right hand block.
815 * this will only push up to 1/2 the contents of the left node over
817 static int balance_node_right(struct btrfs_trans_handle *trans, struct
818 btrfs_root *root, struct buffer_head *dst_buf,
819 struct buffer_head *src_buf)
821 struct btrfs_node *src = btrfs_buffer_node(src_buf);
822 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
823 int push_items = 0;
824 int max_push;
825 int src_nritems;
826 int dst_nritems;
827 int ret = 0;
829 src_nritems = btrfs_header_nritems(&src->header);
830 dst_nritems = btrfs_header_nritems(&dst->header);
831 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
832 if (push_items <= 0) {
833 return 1;
836 max_push = src_nritems / 2 + 1;
837 /* don't try to empty the node */
838 if (max_push > src_nritems)
839 return 1;
840 if (max_push < push_items)
841 push_items = max_push;
843 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
844 dst_nritems * sizeof(struct btrfs_key_ptr));
846 btrfs_memcpy(root, dst, dst->ptrs,
847 src->ptrs + src_nritems - push_items,
848 push_items * sizeof(struct btrfs_key_ptr));
850 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
851 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
853 btrfs_mark_buffer_dirty(src_buf);
854 btrfs_mark_buffer_dirty(dst_buf);
855 return ret;
859 * helper function to insert a new root level in the tree.
860 * A new node is allocated, and a single item is inserted to
861 * point to the existing root
863 * returns zero on success or < 0 on failure.
865 static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
866 *root, struct btrfs_path *path, int level)
868 struct buffer_head *t;
869 struct btrfs_node *lower;
870 struct btrfs_node *c;
871 struct btrfs_disk_key *lower_key;
873 BUG_ON(path->nodes[level]);
874 BUG_ON(path->nodes[level-1] != root->node);
876 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr);
877 if (IS_ERR(t))
878 return PTR_ERR(t);
879 c = btrfs_buffer_node(t);
880 memset(c, 0, root->blocksize);
881 btrfs_set_header_nritems(&c->header, 1);
882 btrfs_set_header_level(&c->header, level);
883 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
884 btrfs_set_header_generation(&c->header, trans->transid);
885 btrfs_set_header_owner(&c->header, root->root_key.objectid);
886 lower = btrfs_buffer_node(path->nodes[level-1]);
887 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
888 sizeof(c->header.fsid));
889 if (btrfs_is_leaf(lower))
890 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
891 else
892 lower_key = &lower->ptrs[0].key;
893 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
894 sizeof(struct btrfs_disk_key));
895 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
897 btrfs_mark_buffer_dirty(t);
899 /* the super has an extra ref to root->node */
900 btrfs_block_release(root, root->node);
901 root->node = t;
902 get_bh(t);
903 path->nodes[level] = t;
904 path->slots[level] = 0;
905 return 0;
909 * worker function to insert a single pointer in a node.
910 * the node should have enough room for the pointer already
912 * slot and level indicate where you want the key to go, and
913 * blocknr is the block the key points to.
915 * returns zero on success and < 0 on any error
917 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
918 *root, struct btrfs_path *path, struct btrfs_disk_key
919 *key, u64 blocknr, int slot, int level)
921 struct btrfs_node *lower;
922 int nritems;
924 BUG_ON(!path->nodes[level]);
925 lower = btrfs_buffer_node(path->nodes[level]);
926 nritems = btrfs_header_nritems(&lower->header);
927 if (slot > nritems)
928 BUG();
929 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
930 BUG();
931 if (slot != nritems) {
932 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
933 lower->ptrs + slot,
934 (nritems - slot) * sizeof(struct btrfs_key_ptr));
936 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
937 key, sizeof(struct btrfs_disk_key));
938 btrfs_set_node_blockptr(lower, slot, blocknr);
939 btrfs_set_header_nritems(&lower->header, nritems + 1);
940 btrfs_mark_buffer_dirty(path->nodes[level]);
941 check_node(root, path, level);
942 return 0;
946 * split the node at the specified level in path in two.
947 * The path is corrected to point to the appropriate node after the split
949 * Before splitting this tries to make some room in the node by pushing
950 * left and right, if either one works, it returns right away.
952 * returns 0 on success and < 0 on failure
954 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
955 *root, struct btrfs_path *path, int level)
957 struct buffer_head *t;
958 struct btrfs_node *c;
959 struct buffer_head *split_buffer;
960 struct btrfs_node *split;
961 int mid;
962 int ret;
963 int wret;
964 u32 c_nritems;
966 t = path->nodes[level];
967 c = btrfs_buffer_node(t);
968 if (t == root->node) {
969 /* trying to split the root, lets make a new one */
970 ret = insert_new_root(trans, root, path, level + 1);
971 if (ret)
972 return ret;
973 } else {
974 ret = push_nodes_for_insert(trans, root, path, level);
975 t = path->nodes[level];
976 c = btrfs_buffer_node(t);
977 if (!ret &&
978 btrfs_header_nritems(&c->header) <
979 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
980 return 0;
981 if (ret < 0)
982 return ret;
985 c_nritems = btrfs_header_nritems(&c->header);
986 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr);
987 if (IS_ERR(split_buffer))
988 return PTR_ERR(split_buffer);
990 split = btrfs_buffer_node(split_buffer);
991 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
992 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
993 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
994 btrfs_set_header_generation(&split->header, trans->transid);
995 btrfs_set_header_owner(&split->header, root->root_key.objectid);
996 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
997 sizeof(split->header.fsid));
998 mid = (c_nritems + 1) / 2;
999 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
1000 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1001 btrfs_set_header_nritems(&split->header, c_nritems - mid);
1002 btrfs_set_header_nritems(&c->header, mid);
1003 ret = 0;
1005 btrfs_mark_buffer_dirty(t);
1006 btrfs_mark_buffer_dirty(split_buffer);
1007 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
1008 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
1009 level + 1);
1010 if (wret)
1011 ret = wret;
1013 if (path->slots[level] >= mid) {
1014 path->slots[level] -= mid;
1015 btrfs_block_release(root, t);
1016 path->nodes[level] = split_buffer;
1017 path->slots[level + 1] += 1;
1018 } else {
1019 btrfs_block_release(root, split_buffer);
1021 return ret;
1025 * how many bytes are required to store the items in a leaf. start
1026 * and nr indicate which items in the leaf to check. This totals up the
1027 * space used both by the item structs and the item data
1029 static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
1031 int data_len;
1032 int nritems = btrfs_header_nritems(&l->header);
1033 int end = min(nritems, start + nr) - 1;
1035 if (!nr)
1036 return 0;
1037 data_len = btrfs_item_end(l->items + start);
1038 data_len = data_len - btrfs_item_offset(l->items + end);
1039 data_len += sizeof(struct btrfs_item) * nr;
1040 WARN_ON(data_len < 0);
1041 return data_len;
1045 * The space between the end of the leaf items and
1046 * the start of the leaf data. IOW, how much room
1047 * the leaf has left for both items and data
1049 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
1051 int nritems = btrfs_header_nritems(&leaf->header);
1052 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1056 * push some data in the path leaf to the right, trying to free up at
1057 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1059 * returns 1 if the push failed because the other node didn't have enough
1060 * room, 0 if everything worked out and < 0 if there were major errors.
1062 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1063 *root, struct btrfs_path *path, int data_size)
1065 struct buffer_head *left_buf = path->nodes[0];
1066 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
1067 struct btrfs_leaf *right;
1068 struct buffer_head *right_buf;
1069 struct buffer_head *upper;
1070 struct btrfs_node *upper_node;
1071 int slot;
1072 int i;
1073 int free_space;
1074 int push_space = 0;
1075 int push_items = 0;
1076 struct btrfs_item *item;
1077 u32 left_nritems;
1078 u32 right_nritems;
1079 int ret;
1081 slot = path->slots[1];
1082 if (!path->nodes[1]) {
1083 return 1;
1085 upper = path->nodes[1];
1086 upper_node = btrfs_buffer_node(upper);
1087 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
1088 return 1;
1090 right_buf = read_tree_block(root,
1091 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1092 right = btrfs_buffer_leaf(right_buf);
1093 free_space = btrfs_leaf_free_space(root, right);
1094 if (free_space < data_size + sizeof(struct btrfs_item)) {
1095 btrfs_block_release(root, right_buf);
1096 return 1;
1098 /* cow and double check */
1099 ret = btrfs_cow_block(trans, root, right_buf, upper,
1100 slot + 1, &right_buf);
1101 if (ret) {
1102 btrfs_block_release(root, right_buf);
1103 return 1;
1105 right = btrfs_buffer_leaf(right_buf);
1106 free_space = btrfs_leaf_free_space(root, right);
1107 if (free_space < data_size + sizeof(struct btrfs_item)) {
1108 btrfs_block_release(root, right_buf);
1109 return 1;
1112 left_nritems = btrfs_header_nritems(&left->header);
1113 if (left_nritems == 0) {
1114 btrfs_block_release(root, right_buf);
1115 return 1;
1117 for (i = left_nritems - 1; i >= 1; i--) {
1118 item = left->items + i;
1119 if (path->slots[0] == i)
1120 push_space += data_size + sizeof(*item);
1121 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1122 free_space)
1123 break;
1124 push_items++;
1125 push_space += btrfs_item_size(item) + sizeof(*item);
1127 if (push_items == 0) {
1128 btrfs_block_release(root, right_buf);
1129 return 1;
1131 if (push_items == left_nritems)
1132 WARN_ON(1);
1133 right_nritems = btrfs_header_nritems(&right->header);
1134 /* push left to right */
1135 push_space = btrfs_item_end(left->items + left_nritems - push_items);
1136 push_space -= leaf_data_end(root, left);
1137 /* make room in the right data area */
1138 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1139 leaf_data_end(root, right) - push_space,
1140 btrfs_leaf_data(right) +
1141 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1142 leaf_data_end(root, right));
1143 /* copy from the left data area */
1144 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1145 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1146 btrfs_leaf_data(left) + leaf_data_end(root, left),
1147 push_space);
1148 btrfs_memmove(root, right, right->items + push_items, right->items,
1149 right_nritems * sizeof(struct btrfs_item));
1150 /* copy the items from left to right */
1151 btrfs_memcpy(root, right, right->items, left->items +
1152 left_nritems - push_items,
1153 push_items * sizeof(struct btrfs_item));
1155 /* update the item pointers */
1156 right_nritems += push_items;
1157 btrfs_set_header_nritems(&right->header, right_nritems);
1158 push_space = BTRFS_LEAF_DATA_SIZE(root);
1159 for (i = 0; i < right_nritems; i++) {
1160 btrfs_set_item_offset(right->items + i, push_space -
1161 btrfs_item_size(right->items + i));
1162 push_space = btrfs_item_offset(right->items + i);
1164 left_nritems -= push_items;
1165 btrfs_set_header_nritems(&left->header, left_nritems);
1167 btrfs_mark_buffer_dirty(left_buf);
1168 btrfs_mark_buffer_dirty(right_buf);
1170 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
1171 &right->items[0].key, sizeof(struct btrfs_disk_key));
1172 btrfs_mark_buffer_dirty(upper);
1174 /* then fixup the leaf pointer in the path */
1175 if (path->slots[0] >= left_nritems) {
1176 path->slots[0] -= left_nritems;
1177 btrfs_block_release(root, path->nodes[0]);
1178 path->nodes[0] = right_buf;
1179 path->slots[1] += 1;
1180 } else {
1181 btrfs_block_release(root, right_buf);
1183 if (path->nodes[1])
1184 check_node(root, path, 1);
1185 return 0;
1188 * push some data in the path leaf to the left, trying to free up at
1189 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1191 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1192 *root, struct btrfs_path *path, int data_size)
1194 struct buffer_head *right_buf = path->nodes[0];
1195 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1196 struct buffer_head *t;
1197 struct btrfs_leaf *left;
1198 int slot;
1199 int i;
1200 int free_space;
1201 int push_space = 0;
1202 int push_items = 0;
1203 struct btrfs_item *item;
1204 u32 old_left_nritems;
1205 int ret = 0;
1206 int wret;
1208 slot = path->slots[1];
1209 if (slot == 0) {
1210 return 1;
1212 if (!path->nodes[1]) {
1213 return 1;
1215 t = read_tree_block(root,
1216 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1217 left = btrfs_buffer_leaf(t);
1218 free_space = btrfs_leaf_free_space(root, left);
1219 if (free_space < data_size + sizeof(struct btrfs_item)) {
1220 btrfs_block_release(root, t);
1221 return 1;
1224 /* cow and double check */
1225 ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1226 if (ret) {
1227 /* we hit -ENOSPC, but it isn't fatal here */
1228 return 1;
1230 left = btrfs_buffer_leaf(t);
1231 free_space = btrfs_leaf_free_space(root, left);
1232 if (free_space < data_size + sizeof(struct btrfs_item)) {
1233 btrfs_block_release(root, t);
1234 return 1;
1237 if (btrfs_header_nritems(&right->header) == 0) {
1238 btrfs_block_release(root, t);
1239 return 1;
1242 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
1243 item = right->items + i;
1244 if (path->slots[0] == i)
1245 push_space += data_size + sizeof(*item);
1246 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1247 free_space)
1248 break;
1249 push_items++;
1250 push_space += btrfs_item_size(item) + sizeof(*item);
1252 if (push_items == 0) {
1253 btrfs_block_release(root, t);
1254 return 1;
1256 if (push_items == btrfs_header_nritems(&right->header))
1257 WARN_ON(1);
1258 /* push data from right to left */
1259 btrfs_memcpy(root, left, left->items +
1260 btrfs_header_nritems(&left->header),
1261 right->items, push_items * sizeof(struct btrfs_item));
1262 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1263 btrfs_item_offset(right->items + push_items -1);
1264 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1265 leaf_data_end(root, left) - push_space,
1266 btrfs_leaf_data(right) +
1267 btrfs_item_offset(right->items + push_items - 1),
1268 push_space);
1269 old_left_nritems = btrfs_header_nritems(&left->header);
1270 BUG_ON(old_left_nritems < 0);
1272 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1273 u32 ioff = btrfs_item_offset(left->items + i);
1274 btrfs_set_item_offset(left->items + i, ioff -
1275 (BTRFS_LEAF_DATA_SIZE(root) -
1276 btrfs_item_offset(left->items +
1277 old_left_nritems - 1)));
1279 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
1281 /* fixup right node */
1282 push_space = btrfs_item_offset(right->items + push_items - 1) -
1283 leaf_data_end(root, right);
1284 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1285 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1286 btrfs_leaf_data(right) +
1287 leaf_data_end(root, right), push_space);
1288 btrfs_memmove(root, right, right->items, right->items + push_items,
1289 (btrfs_header_nritems(&right->header) - push_items) *
1290 sizeof(struct btrfs_item));
1291 btrfs_set_header_nritems(&right->header,
1292 btrfs_header_nritems(&right->header) -
1293 push_items);
1294 push_space = BTRFS_LEAF_DATA_SIZE(root);
1296 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1297 btrfs_set_item_offset(right->items + i, push_space -
1298 btrfs_item_size(right->items + i));
1299 push_space = btrfs_item_offset(right->items + i);
1302 btrfs_mark_buffer_dirty(t);
1303 btrfs_mark_buffer_dirty(right_buf);
1305 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
1306 if (wret)
1307 ret = wret;
1309 /* then fixup the leaf pointer in the path */
1310 if (path->slots[0] < push_items) {
1311 path->slots[0] += old_left_nritems;
1312 btrfs_block_release(root, path->nodes[0]);
1313 path->nodes[0] = t;
1314 path->slots[1] -= 1;
1315 } else {
1316 btrfs_block_release(root, t);
1317 path->slots[0] -= push_items;
1319 BUG_ON(path->slots[0] < 0);
1320 if (path->nodes[1])
1321 check_node(root, path, 1);
1322 return ret;
1326 * split the path's leaf in two, making sure there is at least data_size
1327 * available for the resulting leaf level of the path.
1329 * returns 0 if all went well and < 0 on failure.
1331 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1332 *root, struct btrfs_key *ins_key,
1333 struct btrfs_path *path, int data_size)
1335 struct buffer_head *l_buf;
1336 struct btrfs_leaf *l;
1337 u32 nritems;
1338 int mid;
1339 int slot;
1340 struct btrfs_leaf *right;
1341 struct buffer_head *right_buffer;
1342 int space_needed = data_size + sizeof(struct btrfs_item);
1343 int data_copy_size;
1344 int rt_data_off;
1345 int i;
1346 int ret = 0;
1347 int wret;
1348 int double_split = 0;
1349 struct btrfs_disk_key disk_key;
1351 /* first try to make some room by pushing left and right */
1352 wret = push_leaf_left(trans, root, path, data_size);
1353 if (wret < 0)
1354 return wret;
1355 if (wret) {
1356 wret = push_leaf_right(trans, root, path, data_size);
1357 if (wret < 0)
1358 return wret;
1360 l_buf = path->nodes[0];
1361 l = btrfs_buffer_leaf(l_buf);
1363 /* did the pushes work? */
1364 if (btrfs_leaf_free_space(root, l) >=
1365 sizeof(struct btrfs_item) + data_size)
1366 return 0;
1368 if (!path->nodes[1]) {
1369 ret = insert_new_root(trans, root, path, 1);
1370 if (ret)
1371 return ret;
1373 slot = path->slots[0];
1374 nritems = btrfs_header_nritems(&l->header);
1375 mid = (nritems + 1)/ 2;
1377 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
1378 if (IS_ERR(right_buffer))
1379 return PTR_ERR(right_buffer);
1381 right = btrfs_buffer_leaf(right_buffer);
1382 memset(&right->header, 0, sizeof(right->header));
1383 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
1384 btrfs_set_header_generation(&right->header, trans->transid);
1385 btrfs_set_header_owner(&right->header, root->root_key.objectid);
1386 btrfs_set_header_level(&right->header, 0);
1387 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1388 sizeof(right->header.fsid));
1389 if (mid <= slot) {
1390 if (nritems == 1 ||
1391 leaf_space_used(l, mid, nritems - mid) + space_needed >
1392 BTRFS_LEAF_DATA_SIZE(root)) {
1393 if (slot >= nritems) {
1394 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1395 btrfs_set_header_nritems(&right->header, 0);
1396 wret = insert_ptr(trans, root, path,
1397 &disk_key,
1398 bh_blocknr(right_buffer),
1399 path->slots[1] + 1, 1);
1400 if (wret)
1401 ret = wret;
1402 btrfs_block_release(root, path->nodes[0]);
1403 path->nodes[0] = right_buffer;
1404 path->slots[0] = 0;
1405 path->slots[1] += 1;
1406 return ret;
1408 mid = slot;
1409 double_split = 1;
1411 } else {
1412 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1413 BTRFS_LEAF_DATA_SIZE(root)) {
1414 if (slot == 0) {
1415 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1416 btrfs_set_header_nritems(&right->header, 0);
1417 wret = insert_ptr(trans, root, path,
1418 &disk_key,
1419 bh_blocknr(right_buffer),
1420 path->slots[1], 1);
1421 if (wret)
1422 ret = wret;
1423 btrfs_block_release(root, path->nodes[0]);
1424 path->nodes[0] = right_buffer;
1425 path->slots[0] = 0;
1426 if (path->slots[1] == 0) {
1427 wret = fixup_low_keys(trans, root,
1428 path, &disk_key, 1);
1429 if (wret)
1430 ret = wret;
1432 return ret;
1434 mid = slot;
1435 double_split = 1;
1438 btrfs_set_header_nritems(&right->header, nritems - mid);
1439 data_copy_size = btrfs_item_end(l->items + mid) -
1440 leaf_data_end(root, l);
1441 btrfs_memcpy(root, right, right->items, l->items + mid,
1442 (nritems - mid) * sizeof(struct btrfs_item));
1443 btrfs_memcpy(root, right,
1444 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1445 data_copy_size, btrfs_leaf_data(l) +
1446 leaf_data_end(root, l), data_copy_size);
1447 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1448 btrfs_item_end(l->items + mid);
1450 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1451 u32 ioff = btrfs_item_offset(right->items + i);
1452 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1455 btrfs_set_header_nritems(&l->header, mid);
1456 ret = 0;
1457 wret = insert_ptr(trans, root, path, &right->items[0].key,
1458 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
1459 if (wret)
1460 ret = wret;
1461 btrfs_mark_buffer_dirty(right_buffer);
1462 btrfs_mark_buffer_dirty(l_buf);
1463 BUG_ON(path->slots[0] != slot);
1464 if (mid <= slot) {
1465 btrfs_block_release(root, path->nodes[0]);
1466 path->nodes[0] = right_buffer;
1467 path->slots[0] -= mid;
1468 path->slots[1] += 1;
1469 } else
1470 btrfs_block_release(root, right_buffer);
1471 BUG_ON(path->slots[0] < 0);
1472 check_node(root, path, 1);
1474 if (!double_split)
1475 return ret;
1476 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
1477 if (IS_ERR(right_buffer))
1478 return PTR_ERR(right_buffer);
1480 right = btrfs_buffer_leaf(right_buffer);
1481 memset(&right->header, 0, sizeof(right->header));
1482 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
1483 btrfs_set_header_generation(&right->header, trans->transid);
1484 btrfs_set_header_owner(&right->header, root->root_key.objectid);
1485 btrfs_set_header_level(&right->header, 0);
1486 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1487 sizeof(right->header.fsid));
1488 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1489 btrfs_set_header_nritems(&right->header, 0);
1490 wret = insert_ptr(trans, root, path,
1491 &disk_key,
1492 bh_blocknr(right_buffer),
1493 path->slots[1], 1);
1494 if (wret)
1495 ret = wret;
1496 if (path->slots[1] == 0) {
1497 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1498 if (wret)
1499 ret = wret;
1501 btrfs_block_release(root, path->nodes[0]);
1502 path->nodes[0] = right_buffer;
1503 path->slots[0] = 0;
1504 check_node(root, path, 1);
1505 check_leaf(root, path, 0);
1506 return ret;
1509 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root,
1511 struct btrfs_path *path,
1512 u32 new_size)
1514 int ret = 0;
1515 int slot;
1516 int slot_orig;
1517 struct btrfs_leaf *leaf;
1518 struct buffer_head *leaf_buf;
1519 u32 nritems;
1520 unsigned int data_end;
1521 unsigned int old_data_start;
1522 unsigned int old_size;
1523 unsigned int size_diff;
1524 int i;
1526 slot_orig = path->slots[0];
1527 leaf_buf = path->nodes[0];
1528 leaf = btrfs_buffer_leaf(leaf_buf);
1530 nritems = btrfs_header_nritems(&leaf->header);
1531 data_end = leaf_data_end(root, leaf);
1533 slot = path->slots[0];
1534 old_data_start = btrfs_item_offset(leaf->items + slot);
1535 old_size = btrfs_item_size(leaf->items + slot);
1536 BUG_ON(old_size <= new_size);
1537 size_diff = old_size - new_size;
1539 BUG_ON(slot < 0);
1540 BUG_ON(slot >= nritems);
1543 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1545 /* first correct the data pointers */
1546 for (i = slot; i < nritems; i++) {
1547 u32 ioff = btrfs_item_offset(leaf->items + i);
1548 btrfs_set_item_offset(leaf->items + i,
1549 ioff + size_diff);
1551 /* shift the data */
1552 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1553 data_end + size_diff, btrfs_leaf_data(leaf) +
1554 data_end, old_data_start + new_size - data_end);
1555 btrfs_set_item_size(leaf->items + slot, new_size);
1556 btrfs_mark_buffer_dirty(leaf_buf);
1558 ret = 0;
1559 if (btrfs_leaf_free_space(root, leaf) < 0)
1560 BUG();
1561 check_leaf(root, path, 0);
1562 return ret;
1565 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1566 *root, struct btrfs_path *path, u32 data_size)
1568 int ret = 0;
1569 int slot;
1570 int slot_orig;
1571 struct btrfs_leaf *leaf;
1572 struct buffer_head *leaf_buf;
1573 u32 nritems;
1574 unsigned int data_end;
1575 unsigned int old_data;
1576 unsigned int old_size;
1577 int i;
1579 slot_orig = path->slots[0];
1580 leaf_buf = path->nodes[0];
1581 leaf = btrfs_buffer_leaf(leaf_buf);
1583 nritems = btrfs_header_nritems(&leaf->header);
1584 data_end = leaf_data_end(root, leaf);
1586 if (btrfs_leaf_free_space(root, leaf) < data_size)
1587 BUG();
1588 slot = path->slots[0];
1589 old_data = btrfs_item_end(leaf->items + slot);
1591 BUG_ON(slot < 0);
1592 BUG_ON(slot >= nritems);
1595 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1597 /* first correct the data pointers */
1598 for (i = slot; i < nritems; i++) {
1599 u32 ioff = btrfs_item_offset(leaf->items + i);
1600 btrfs_set_item_offset(leaf->items + i,
1601 ioff - data_size);
1603 /* shift the data */
1604 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1605 data_end - data_size, btrfs_leaf_data(leaf) +
1606 data_end, old_data - data_end);
1607 data_end = old_data;
1608 old_size = btrfs_item_size(leaf->items + slot);
1609 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1610 btrfs_mark_buffer_dirty(leaf_buf);
1612 ret = 0;
1613 if (btrfs_leaf_free_space(root, leaf) < 0)
1614 BUG();
1615 check_leaf(root, path, 0);
1616 return ret;
1620 * Given a key and some data, insert an item into the tree.
1621 * This does all the path init required, making room in the tree if needed.
1623 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1624 *root, struct btrfs_path *path, struct btrfs_key
1625 *cpu_key, u32 data_size)
1627 int ret = 0;
1628 int slot;
1629 int slot_orig;
1630 struct btrfs_leaf *leaf;
1631 struct buffer_head *leaf_buf;
1632 u32 nritems;
1633 unsigned int data_end;
1634 struct btrfs_disk_key disk_key;
1636 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1638 /* create a root if there isn't one */
1639 if (!root->node)
1640 BUG();
1641 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
1642 if (ret == 0) {
1643 return -EEXIST;
1645 if (ret < 0)
1646 goto out;
1648 slot_orig = path->slots[0];
1649 leaf_buf = path->nodes[0];
1650 leaf = btrfs_buffer_leaf(leaf_buf);
1652 nritems = btrfs_header_nritems(&leaf->header);
1653 data_end = leaf_data_end(root, leaf);
1655 if (btrfs_leaf_free_space(root, leaf) <
1656 sizeof(struct btrfs_item) + data_size) {
1657 BUG();
1659 slot = path->slots[0];
1660 BUG_ON(slot < 0);
1661 if (slot != nritems) {
1662 int i;
1663 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1666 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1668 /* first correct the data pointers */
1669 for (i = slot; i < nritems; i++) {
1670 u32 ioff = btrfs_item_offset(leaf->items + i);
1671 btrfs_set_item_offset(leaf->items + i,
1672 ioff - data_size);
1675 /* shift the items */
1676 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1677 leaf->items + slot,
1678 (nritems - slot) * sizeof(struct btrfs_item));
1680 /* shift the data */
1681 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1682 data_end - data_size, btrfs_leaf_data(leaf) +
1683 data_end, old_data - data_end);
1684 data_end = old_data;
1686 /* setup the item for the new data */
1687 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1688 sizeof(struct btrfs_disk_key));
1689 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1690 btrfs_set_item_size(leaf->items + slot, data_size);
1691 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1692 btrfs_mark_buffer_dirty(leaf_buf);
1694 ret = 0;
1695 if (slot == 0)
1696 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
1698 if (btrfs_leaf_free_space(root, leaf) < 0)
1699 BUG();
1700 check_leaf(root, path, 0);
1701 out:
1702 return ret;
1706 * Given a key and some data, insert an item into the tree.
1707 * This does all the path init required, making room in the tree if needed.
1709 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1710 *root, struct btrfs_key *cpu_key, void *data, u32
1711 data_size)
1713 int ret = 0;
1714 struct btrfs_path *path;
1715 u8 *ptr;
1717 path = btrfs_alloc_path();
1718 BUG_ON(!path);
1719 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
1720 if (!ret) {
1721 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1722 path->slots[0], u8);
1723 btrfs_memcpy(root, path->nodes[0]->b_data,
1724 ptr, data, data_size);
1725 btrfs_mark_buffer_dirty(path->nodes[0]);
1727 btrfs_free_path(path);
1728 return ret;
1732 * delete the pointer from a given node.
1734 * If the delete empties a node, the node is removed from the tree,
1735 * continuing all the way the root if required. The root is converted into
1736 * a leaf if all the nodes are emptied.
1738 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1739 struct btrfs_path *path, int level, int slot)
1741 struct btrfs_node *node;
1742 struct buffer_head *parent = path->nodes[level];
1743 u32 nritems;
1744 int ret = 0;
1745 int wret;
1747 node = btrfs_buffer_node(parent);
1748 nritems = btrfs_header_nritems(&node->header);
1749 if (slot != nritems -1) {
1750 btrfs_memmove(root, node, node->ptrs + slot,
1751 node->ptrs + slot + 1,
1752 sizeof(struct btrfs_key_ptr) *
1753 (nritems - slot - 1));
1755 nritems--;
1756 btrfs_set_header_nritems(&node->header, nritems);
1757 if (nritems == 0 && parent == root->node) {
1758 struct btrfs_header *header = btrfs_buffer_header(root->node);
1759 BUG_ON(btrfs_header_level(header) != 1);
1760 /* just turn the root into a leaf and break */
1761 btrfs_set_header_level(header, 0);
1762 } else if (slot == 0) {
1763 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
1764 level + 1);
1765 if (wret)
1766 ret = wret;
1768 btrfs_mark_buffer_dirty(parent);
1769 return ret;
1773 * delete the item at the leaf level in path. If that empties
1774 * the leaf, remove it from the tree
1776 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1777 struct btrfs_path *path)
1779 int slot;
1780 struct btrfs_leaf *leaf;
1781 struct buffer_head *leaf_buf;
1782 int doff;
1783 int dsize;
1784 int ret = 0;
1785 int wret;
1786 u32 nritems;
1788 leaf_buf = path->nodes[0];
1789 leaf = btrfs_buffer_leaf(leaf_buf);
1790 slot = path->slots[0];
1791 doff = btrfs_item_offset(leaf->items + slot);
1792 dsize = btrfs_item_size(leaf->items + slot);
1793 nritems = btrfs_header_nritems(&leaf->header);
1795 if (slot != nritems - 1) {
1796 int i;
1797 int data_end = leaf_data_end(root, leaf);
1798 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1799 data_end + dsize,
1800 btrfs_leaf_data(leaf) + data_end,
1801 doff - data_end);
1802 for (i = slot + 1; i < nritems; i++) {
1803 u32 ioff = btrfs_item_offset(leaf->items + i);
1804 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1806 btrfs_memmove(root, leaf, leaf->items + slot,
1807 leaf->items + slot + 1,
1808 sizeof(struct btrfs_item) *
1809 (nritems - slot - 1));
1811 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1812 nritems--;
1813 /* delete the leaf if we've emptied it */
1814 if (nritems == 0) {
1815 if (leaf_buf == root->node) {
1816 btrfs_set_header_level(&leaf->header, 0);
1817 } else {
1818 clean_tree_block(trans, root, leaf_buf);
1819 wait_on_buffer(leaf_buf);
1820 wret = del_ptr(trans, root, path, 1, path->slots[1]);
1821 if (wret)
1822 ret = wret;
1823 wret = btrfs_free_extent(trans, root,
1824 bh_blocknr(leaf_buf), 1, 1);
1825 if (wret)
1826 ret = wret;
1828 } else {
1829 int used = leaf_space_used(leaf, 0, nritems);
1830 if (slot == 0) {
1831 wret = fixup_low_keys(trans, root, path,
1832 &leaf->items[0].key, 1);
1833 if (wret)
1834 ret = wret;
1837 /* delete the leaf if it is mostly empty */
1838 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1839 /* push_leaf_left fixes the path.
1840 * make sure the path still points to our leaf
1841 * for possible call to del_ptr below
1843 slot = path->slots[1];
1844 get_bh(leaf_buf);
1845 wret = push_leaf_left(trans, root, path, 1);
1846 if (wret < 0 && wret != -ENOSPC)
1847 ret = wret;
1848 if (path->nodes[0] == leaf_buf &&
1849 btrfs_header_nritems(&leaf->header)) {
1850 wret = push_leaf_right(trans, root, path, 1);
1851 if (wret < 0 && wret != -ENOSPC)
1852 ret = wret;
1854 if (btrfs_header_nritems(&leaf->header) == 0) {
1855 u64 blocknr = bh_blocknr(leaf_buf);
1856 clean_tree_block(trans, root, leaf_buf);
1857 wait_on_buffer(leaf_buf);
1858 wret = del_ptr(trans, root, path, 1, slot);
1859 if (wret)
1860 ret = wret;
1861 btrfs_block_release(root, leaf_buf);
1862 wret = btrfs_free_extent(trans, root, blocknr,
1863 1, 1);
1864 if (wret)
1865 ret = wret;
1866 } else {
1867 btrfs_mark_buffer_dirty(leaf_buf);
1868 btrfs_block_release(root, leaf_buf);
1870 } else {
1871 btrfs_mark_buffer_dirty(leaf_buf);
1874 return ret;
1878 * walk up the tree as far as required to find the next leaf.
1879 * returns 0 if it found something or 1 if there are no greater leaves.
1880 * returns < 0 on io errors.
1882 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1884 int slot;
1885 int level = 1;
1886 u64 blocknr;
1887 struct buffer_head *c;
1888 struct btrfs_node *c_node;
1889 struct buffer_head *next = NULL;
1891 while(level < BTRFS_MAX_LEVEL) {
1892 if (!path->nodes[level])
1893 return 1;
1894 slot = path->slots[level] + 1;
1895 c = path->nodes[level];
1896 c_node = btrfs_buffer_node(c);
1897 if (slot >= btrfs_header_nritems(&c_node->header)) {
1898 level++;
1899 continue;
1901 blocknr = btrfs_node_blockptr(c_node, slot);
1902 if (next)
1903 btrfs_block_release(root, next);
1904 next = read_tree_block(root, blocknr);
1905 break;
1907 path->slots[level] = slot;
1908 while(1) {
1909 level--;
1910 c = path->nodes[level];
1911 btrfs_block_release(root, c);
1912 path->nodes[level] = next;
1913 path->slots[level] = 0;
1914 if (!level)
1915 break;
1916 next = read_tree_block(root,
1917 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
1919 return 0;