Add a readonly flag open_ctree to force RO opens
[btrfs-progs-unstable.git] / ctree.c
blob5608bb47a8e769ac3b88f9d87391a1397687f450
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.
18 #include "ctree.h"
19 #include "disk-io.h"
20 #include "transaction.h"
21 #include "print-tree.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, int extend);
28 static int push_node_left(struct btrfs_trans_handle *trans,
29 struct btrfs_root *root, struct extent_buffer *dst,
30 struct extent_buffer *src, int empty);
31 static int balance_node_right(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root,
33 struct extent_buffer *dst_buf,
34 struct extent_buffer *src_buf);
35 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
36 struct btrfs_path *path, int level, int slot);
38 inline void btrfs_init_path(struct btrfs_path *p)
40 memset(p, 0, sizeof(*p));
43 struct btrfs_path *btrfs_alloc_path(void)
45 struct btrfs_path *path;
46 path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS);
47 if (path) {
48 btrfs_init_path(path);
49 path->reada = 0;
51 return path;
54 void btrfs_free_path(struct btrfs_path *p)
56 btrfs_release_path(NULL, p);
57 kfree(p);
60 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
62 int i;
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (!p->nodes[i])
65 break;
66 free_extent_buffer(p->nodes[i]);
68 memset(p, 0, sizeof(*p));
71 static void add_root_to_dirty_list(struct btrfs_root *root)
73 if (root->track_dirty && list_empty(&root->dirty_list)) {
74 list_add(&root->dirty_list,
75 &root->fs_info->dirty_cowonly_roots);
79 int btrfs_copy_root(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 struct extent_buffer *buf,
82 struct extent_buffer **cow_ret, u64 new_root_objectid)
84 struct extent_buffer *cow;
85 u32 nritems;
86 int ret = 0;
87 int level;
88 struct btrfs_key first_key;
89 struct btrfs_root *new_root;
91 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
92 if (!new_root)
93 return -ENOMEM;
95 memcpy(new_root, root, sizeof(*new_root));
96 new_root->root_key.objectid = new_root_objectid;
98 WARN_ON(root->ref_cows && trans->transid !=
99 root->fs_info->running_transaction->transid);
100 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
102 level = btrfs_header_level(buf);
103 nritems = btrfs_header_nritems(buf);
104 if (nritems) {
105 if (level == 0)
106 btrfs_item_key_to_cpu(buf, &first_key, 0);
107 else
108 btrfs_node_key_to_cpu(buf, &first_key, 0);
109 } else {
110 first_key.objectid = 0;
112 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
113 new_root_objectid,
114 trans->transid, first_key.objectid,
115 level, buf->start, 0);
116 if (IS_ERR(cow)) {
117 kfree(new_root);
118 return PTR_ERR(cow);
121 copy_extent_buffer(cow, buf, 0, 0, cow->len);
122 btrfs_set_header_bytenr(cow, cow->start);
123 btrfs_set_header_generation(cow, trans->transid);
124 btrfs_set_header_owner(cow, new_root_objectid);
125 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
127 WARN_ON(btrfs_header_generation(buf) > trans->transid);
128 ret = btrfs_inc_ref(trans, new_root, buf);
129 kfree(new_root);
131 if (ret)
132 return ret;
134 btrfs_mark_buffer_dirty(cow);
135 *cow_ret = cow;
136 return 0;
139 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root,
141 struct extent_buffer *buf,
142 struct extent_buffer *parent, int parent_slot,
143 struct extent_buffer **cow_ret,
144 u64 search_start, u64 empty_size)
146 u64 root_gen;
147 struct extent_buffer *cow;
148 u32 nritems;
149 int ret = 0;
150 int different_trans = 0;
151 int level;
152 struct btrfs_key first_key;
154 if (root->ref_cows) {
155 root_gen = trans->transid;
156 } else {
157 root_gen = 0;
160 WARN_ON(root->ref_cows && trans->transid !=
161 root->fs_info->running_transaction->transid);
162 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
164 level = btrfs_header_level(buf);
165 nritems = btrfs_header_nritems(buf);
166 if (nritems) {
167 if (level == 0)
168 btrfs_item_key_to_cpu(buf, &first_key, 0);
169 else
170 btrfs_node_key_to_cpu(buf, &first_key, 0);
171 } else {
172 first_key.objectid = 0;
174 cow = __btrfs_alloc_free_block(trans, root, buf->len,
175 root->root_key.objectid,
176 root_gen, first_key.objectid, level,
177 search_start, empty_size);
178 if (IS_ERR(cow))
179 return PTR_ERR(cow);
181 copy_extent_buffer(cow, buf, 0, 0, cow->len);
182 btrfs_set_header_bytenr(cow, cow->start);
183 btrfs_set_header_generation(cow, trans->transid);
184 btrfs_set_header_owner(cow, root->root_key.objectid);
185 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
187 WARN_ON(btrfs_header_generation(buf) > trans->transid);
188 if (btrfs_header_generation(buf) != trans->transid) {
189 different_trans = 1;
190 ret = btrfs_inc_ref(trans, root, buf);
191 if (ret)
192 return ret;
193 } else {
194 clean_tree_block(trans, root, buf);
197 if (buf == root->node) {
198 root_gen = btrfs_header_generation(buf);
199 root->node = cow;
200 extent_buffer_get(cow);
201 if (buf != root->commit_root) {
202 btrfs_free_extent(trans, root, buf->start,
203 buf->len, root->root_key.objectid,
204 root_gen, 0, 0, 1);
206 free_extent_buffer(buf);
207 add_root_to_dirty_list(root);
208 } else {
209 root_gen = btrfs_header_generation(parent);
210 btrfs_set_node_blockptr(parent, parent_slot,
211 cow->start);
212 WARN_ON(trans->transid == 0);
213 btrfs_set_node_ptr_generation(parent, parent_slot,
214 trans->transid);
215 btrfs_mark_buffer_dirty(parent);
216 WARN_ON(btrfs_header_generation(parent) != trans->transid);
217 btrfs_free_extent(trans, root, buf->start, buf->len,
218 btrfs_header_owner(parent), root_gen,
219 0, 0, 1);
221 free_extent_buffer(buf);
222 btrfs_mark_buffer_dirty(cow);
223 *cow_ret = cow;
224 return 0;
227 int btrfs_cow_block(struct btrfs_trans_handle *trans,
228 struct btrfs_root *root, struct extent_buffer *buf,
229 struct extent_buffer *parent, int parent_slot,
230 struct extent_buffer **cow_ret)
232 u64 search_start;
233 int ret;
235 if (trans->transaction != root->fs_info->running_transaction) {
236 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
237 root->fs_info->running_transaction->transid);
238 WARN_ON(1);
241 if (trans->transid != root->fs_info->generation) {
242 printk(KERN_CRIT "trans %llu running %llu\n",
243 (unsigned long long)trans->transid,
244 (unsigned long long)root->fs_info->generation);
245 WARN_ON(1);
247 if (btrfs_header_generation(buf) == trans->transid &&
248 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
249 *cow_ret = buf;
250 return 0;
253 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
254 ret = __btrfs_cow_block(trans, root, buf, parent,
255 parent_slot, cow_ret, search_start, 0);
256 return ret;
260 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
262 if (blocknr < other && other - (blocknr + blocksize) < 32768)
263 return 1;
264 if (blocknr > other && blocknr - (other + blocksize) < 32768)
265 return 1;
266 return 0;
271 * compare two keys in a memcmp fashion
273 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
275 struct btrfs_key k1;
277 btrfs_disk_key_to_cpu(&k1, disk);
279 if (k1.objectid > k2->objectid)
280 return 1;
281 if (k1.objectid < k2->objectid)
282 return -1;
283 if (k1.type > k2->type)
284 return 1;
285 if (k1.type < k2->type)
286 return -1;
287 if (k1.offset > k2->offset)
288 return 1;
289 if (k1.offset < k2->offset)
290 return -1;
291 return 0;
295 #if 0
296 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
297 struct btrfs_root *root, struct extent_buffer *parent,
298 int start_slot, int cache_only, u64 *last_ret,
299 struct btrfs_key *progress)
301 struct extent_buffer *cur;
302 struct extent_buffer *tmp;
303 u64 blocknr;
304 u64 search_start = *last_ret;
305 u64 last_block = 0;
306 u64 other;
307 u32 parent_nritems;
308 int end_slot;
309 int i;
310 int err = 0;
311 int parent_level;
312 int uptodate;
313 u32 blocksize;
314 int progress_passed = 0;
315 struct btrfs_disk_key disk_key;
317 parent_level = btrfs_header_level(parent);
318 if (cache_only && parent_level != 1)
319 return 0;
321 if (trans->transaction != root->fs_info->running_transaction) {
322 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
323 root->fs_info->running_transaction->transid);
324 WARN_ON(1);
326 if (trans->transid != root->fs_info->generation) {
327 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
328 root->fs_info->generation);
329 WARN_ON(1);
332 parent_nritems = btrfs_header_nritems(parent);
333 blocksize = btrfs_level_size(root, parent_level - 1);
334 end_slot = parent_nritems;
336 if (parent_nritems == 1)
337 return 0;
339 for (i = start_slot; i < end_slot; i++) {
340 int close = 1;
342 if (!parent->map_token) {
343 map_extent_buffer(parent,
344 btrfs_node_key_ptr_offset(i),
345 sizeof(struct btrfs_key_ptr),
346 &parent->map_token, &parent->kaddr,
347 &parent->map_start, &parent->map_len,
348 KM_USER1);
350 btrfs_node_key(parent, &disk_key, i);
351 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
352 continue;
354 progress_passed = 1;
355 blocknr = btrfs_node_blockptr(parent, i);
356 if (last_block == 0)
357 last_block = blocknr;
359 if (i > 0) {
360 other = btrfs_node_blockptr(parent, i - 1);
361 close = close_blocks(blocknr, other, blocksize);
363 if (close && i < end_slot - 2) {
364 other = btrfs_node_blockptr(parent, i + 1);
365 close = close_blocks(blocknr, other, blocksize);
367 if (close) {
368 last_block = blocknr;
369 continue;
371 if (parent->map_token) {
372 unmap_extent_buffer(parent, parent->map_token,
373 KM_USER1);
374 parent->map_token = NULL;
377 cur = btrfs_find_tree_block(root, blocknr, blocksize);
378 if (cur)
379 uptodate = btrfs_buffer_uptodate(cur);
380 else
381 uptodate = 0;
382 if (!cur || !uptodate) {
383 if (cache_only) {
384 free_extent_buffer(cur);
385 continue;
387 if (!cur) {
388 cur = read_tree_block(root, blocknr,
389 blocksize);
390 } else if (!uptodate) {
391 btrfs_read_buffer(cur);
394 if (search_start == 0)
395 search_start = last_block;
397 err = __btrfs_cow_block(trans, root, cur, parent, i,
398 &tmp, search_start,
399 min(16 * blocksize,
400 (end_slot - i) * blocksize));
401 if (err) {
402 free_extent_buffer(cur);
403 break;
405 search_start = tmp->start;
406 last_block = tmp->start;
407 *last_ret = search_start;
408 if (parent_level == 1)
409 btrfs_clear_buffer_defrag(tmp);
410 free_extent_buffer(tmp);
412 if (parent->map_token) {
413 unmap_extent_buffer(parent, parent->map_token,
414 KM_USER1);
415 parent->map_token = NULL;
417 return err;
419 #endif
422 * The leaf data grows from end-to-front in the node.
423 * this returns the address of the start of the last item,
424 * which is the stop of the leaf data stack
426 static inline unsigned int leaf_data_end(struct btrfs_root *root,
427 struct extent_buffer *leaf)
429 u32 nr = btrfs_header_nritems(leaf);
430 if (nr == 0)
431 return BTRFS_LEAF_DATA_SIZE(root);
432 return btrfs_item_offset_nr(leaf, nr - 1);
435 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
436 int level)
438 struct extent_buffer *parent = NULL;
439 struct extent_buffer *node = path->nodes[level];
440 struct btrfs_disk_key parent_key;
441 struct btrfs_disk_key node_key;
442 int parent_slot;
443 int slot;
444 struct btrfs_key cpukey;
445 u32 nritems = btrfs_header_nritems(node);
447 if (path->nodes[level + 1])
448 parent = path->nodes[level + 1];
450 slot = path->slots[level];
451 BUG_ON(nritems == 0);
452 if (parent) {
453 parent_slot = path->slots[level + 1];
454 btrfs_node_key(parent, &parent_key, parent_slot);
455 btrfs_node_key(node, &node_key, 0);
456 BUG_ON(memcmp(&parent_key, &node_key,
457 sizeof(struct btrfs_disk_key)));
458 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
459 btrfs_header_bytenr(node));
461 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
462 if (slot != 0) {
463 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
464 btrfs_node_key(node, &node_key, slot);
465 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0);
467 if (slot < nritems - 1) {
468 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
469 btrfs_node_key(node, &node_key, slot);
470 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0);
472 return 0;
475 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
476 int level)
478 struct extent_buffer *leaf = path->nodes[level];
479 struct extent_buffer *parent = NULL;
480 int parent_slot;
481 struct btrfs_key cpukey;
482 struct btrfs_disk_key parent_key;
483 struct btrfs_disk_key leaf_key;
484 int slot = path->slots[0];
486 u32 nritems = btrfs_header_nritems(leaf);
488 if (path->nodes[level + 1])
489 parent = path->nodes[level + 1];
491 if (nritems == 0)
492 return 0;
494 if (parent) {
495 parent_slot = path->slots[level + 1];
496 btrfs_node_key(parent, &parent_key, parent_slot);
497 btrfs_item_key(leaf, &leaf_key, 0);
499 BUG_ON(memcmp(&parent_key, &leaf_key,
500 sizeof(struct btrfs_disk_key)));
501 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
502 btrfs_header_bytenr(leaf));
504 #if 0
505 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
506 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
507 btrfs_item_key(leaf, &leaf_key, i);
508 if (comp_keys(&leaf_key, &cpukey) >= 0) {
509 btrfs_print_leaf(root, leaf);
510 printk("slot %d offset bad key\n", i);
511 BUG_ON(1);
513 if (btrfs_item_offset_nr(leaf, i) !=
514 btrfs_item_end_nr(leaf, i + 1)) {
515 btrfs_print_leaf(root, leaf);
516 printk("slot %d offset bad\n", i);
517 BUG_ON(1);
519 if (i == 0) {
520 if (btrfs_item_offset_nr(leaf, i) +
521 btrfs_item_size_nr(leaf, i) !=
522 BTRFS_LEAF_DATA_SIZE(root)) {
523 btrfs_print_leaf(root, leaf);
524 printk("slot %d first offset bad\n", i);
525 BUG_ON(1);
529 if (nritems > 0) {
530 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
531 btrfs_print_leaf(root, leaf);
532 printk("slot %d bad size \n", nritems - 1);
533 BUG_ON(1);
536 #endif
537 if (slot != 0 && slot < nritems - 1) {
538 btrfs_item_key(leaf, &leaf_key, slot);
539 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
540 if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) {
541 btrfs_print_leaf(root, leaf);
542 printk("slot %d offset bad key\n", slot);
543 BUG_ON(1);
545 if (btrfs_item_offset_nr(leaf, slot - 1) !=
546 btrfs_item_end_nr(leaf, slot)) {
547 btrfs_print_leaf(root, leaf);
548 printk("slot %d offset bad\n", slot);
549 BUG_ON(1);
552 if (slot < nritems - 1) {
553 btrfs_item_key(leaf, &leaf_key, slot);
554 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
555 BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0);
556 if (btrfs_item_offset_nr(leaf, slot) !=
557 btrfs_item_end_nr(leaf, slot + 1)) {
558 btrfs_print_leaf(root, leaf);
559 printk("slot %d offset bad\n", slot);
560 BUG_ON(1);
563 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
564 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
565 return 0;
568 static int noinline check_block(struct btrfs_root *root,
569 struct btrfs_path *path, int level)
571 return 0;
572 #if 0
573 struct extent_buffer *buf = path->nodes[level];
575 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
576 (unsigned long)btrfs_header_fsid(buf),
577 BTRFS_FSID_SIZE)) {
578 printk("warning bad block %Lu\n", buf->start);
579 return 1;
581 #endif
582 if (level == 0)
583 return check_leaf(root, path, level);
584 return check_node(root, path, level);
588 * search for key in the extent_buffer. The items start at offset p,
589 * and they are item_size apart. There are 'max' items in p.
591 * the slot in the array is returned via slot, and it points to
592 * the place where you would insert key if it is not found in
593 * the array.
595 * slot may point to max if the key is bigger than all of the keys
597 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
598 int item_size, struct btrfs_key *key,
599 int max, int *slot)
601 int low = 0;
602 int high = max;
603 int mid;
604 int ret;
605 unsigned long offset;
606 struct btrfs_disk_key *tmp;
608 while(low < high) {
609 mid = (low + high) / 2;
610 offset = p + mid * item_size;
612 tmp = (struct btrfs_disk_key *)(eb->data + offset);
613 ret = btrfs_comp_keys(tmp, key);
615 if (ret < 0)
616 low = mid + 1;
617 else if (ret > 0)
618 high = mid;
619 else {
620 *slot = mid;
621 return 0;
624 *slot = low;
625 return 1;
629 * simple bin_search frontend that does the right thing for
630 * leaves vs nodes
632 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
633 int level, int *slot)
635 if (level == 0) {
636 return generic_bin_search(eb,
637 offsetof(struct btrfs_leaf, items),
638 sizeof(struct btrfs_item),
639 key, btrfs_header_nritems(eb),
640 slot);
641 } else {
642 return generic_bin_search(eb,
643 offsetof(struct btrfs_node, ptrs),
644 sizeof(struct btrfs_key_ptr),
645 key, btrfs_header_nritems(eb),
646 slot);
648 return -1;
651 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
652 struct extent_buffer *parent, int slot)
654 if (slot < 0)
655 return NULL;
656 if (slot >= btrfs_header_nritems(parent))
657 return NULL;
658 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
659 btrfs_level_size(root, btrfs_header_level(parent) - 1));
662 static int balance_level(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root,
664 struct btrfs_path *path, int level)
666 struct extent_buffer *right = NULL;
667 struct extent_buffer *mid;
668 struct extent_buffer *left = NULL;
669 struct extent_buffer *parent = NULL;
670 int ret = 0;
671 int wret;
672 int pslot;
673 int orig_slot = path->slots[level];
674 int err_on_enospc = 0;
675 u64 orig_ptr;
677 if (level == 0)
678 return 0;
680 mid = path->nodes[level];
681 WARN_ON(btrfs_header_generation(mid) != trans->transid);
683 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
685 if (level < BTRFS_MAX_LEVEL - 1)
686 parent = path->nodes[level + 1];
687 pslot = path->slots[level + 1];
690 * deal with the case where there is only one pointer in the root
691 * by promoting the node below to a root
693 if (!parent) {
694 struct extent_buffer *child;
696 if (btrfs_header_nritems(mid) != 1)
697 return 0;
699 /* promote the child to a root */
700 child = read_node_slot(root, mid, 0);
701 BUG_ON(!child);
702 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
703 BUG_ON(ret);
705 root->node = child;
706 add_root_to_dirty_list(root);
707 path->nodes[level] = NULL;
708 clean_tree_block(trans, root, mid);
709 wait_on_tree_block_writeback(root, mid);
710 /* once for the path */
711 free_extent_buffer(mid);
712 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
713 root->root_key.objectid,
714 btrfs_header_generation(mid), 0, 0, 1);
715 /* once for the root ptr */
716 free_extent_buffer(mid);
717 return ret;
719 if (btrfs_header_nritems(mid) >
720 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
721 return 0;
723 if (btrfs_header_nritems(mid) < 2)
724 err_on_enospc = 1;
726 left = read_node_slot(root, parent, pslot - 1);
727 if (left) {
728 wret = btrfs_cow_block(trans, root, left,
729 parent, pslot - 1, &left);
730 if (wret) {
731 ret = wret;
732 goto enospc;
735 right = read_node_slot(root, parent, pslot + 1);
736 if (right) {
737 wret = btrfs_cow_block(trans, root, right,
738 parent, pslot + 1, &right);
739 if (wret) {
740 ret = wret;
741 goto enospc;
745 /* first, try to make some room in the middle buffer */
746 if (left) {
747 orig_slot += btrfs_header_nritems(left);
748 wret = push_node_left(trans, root, left, mid, 1);
749 if (wret < 0)
750 ret = wret;
751 if (btrfs_header_nritems(mid) < 2)
752 err_on_enospc = 1;
756 * then try to empty the right most buffer into the middle
758 if (right) {
759 wret = push_node_left(trans, root, mid, right, 1);
760 if (wret < 0 && wret != -ENOSPC)
761 ret = wret;
762 if (btrfs_header_nritems(right) == 0) {
763 u64 bytenr = right->start;
764 u64 generation = btrfs_header_generation(parent);
765 u32 blocksize = right->len;
767 clean_tree_block(trans, root, right);
768 wait_on_tree_block_writeback(root, right);
769 free_extent_buffer(right);
770 right = NULL;
771 wret = del_ptr(trans, root, path, level + 1, pslot +
773 if (wret)
774 ret = wret;
775 wret = btrfs_free_extent(trans, root, bytenr,
776 blocksize,
777 btrfs_header_owner(parent),
778 generation, 0, 0, 1);
779 if (wret)
780 ret = wret;
781 } else {
782 struct btrfs_disk_key right_key;
783 btrfs_node_key(right, &right_key, 0);
784 btrfs_set_node_key(parent, &right_key, pslot + 1);
785 btrfs_mark_buffer_dirty(parent);
788 if (btrfs_header_nritems(mid) == 1) {
790 * we're not allowed to leave a node with one item in the
791 * tree during a delete. A deletion from lower in the tree
792 * could try to delete the only pointer in this node.
793 * So, pull some keys from the left.
794 * There has to be a left pointer at this point because
795 * otherwise we would have pulled some pointers from the
796 * right
798 BUG_ON(!left);
799 wret = balance_node_right(trans, root, mid, left);
800 if (wret < 0) {
801 ret = wret;
802 goto enospc;
804 if (wret == 1) {
805 wret = push_node_left(trans, root, left, mid, 1);
806 if (wret < 0)
807 ret = wret;
809 BUG_ON(wret == 1);
811 if (btrfs_header_nritems(mid) == 0) {
812 /* we've managed to empty the middle node, drop it */
813 u64 root_gen = btrfs_header_generation(parent);
814 u64 bytenr = mid->start;
815 u32 blocksize = mid->len;
816 clean_tree_block(trans, root, mid);
817 wait_on_tree_block_writeback(root, mid);
818 free_extent_buffer(mid);
819 mid = NULL;
820 wret = del_ptr(trans, root, path, level + 1, pslot);
821 if (wret)
822 ret = wret;
823 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
824 btrfs_header_owner(parent),
825 root_gen, 0, 0, 1);
826 if (wret)
827 ret = wret;
828 } else {
829 /* update the parent key to reflect our changes */
830 struct btrfs_disk_key mid_key;
831 btrfs_node_key(mid, &mid_key, 0);
832 btrfs_set_node_key(parent, &mid_key, pslot);
833 btrfs_mark_buffer_dirty(parent);
836 /* update the path */
837 if (left) {
838 if (btrfs_header_nritems(left) > orig_slot) {
839 extent_buffer_get(left);
840 path->nodes[level] = left;
841 path->slots[level + 1] -= 1;
842 path->slots[level] = orig_slot;
843 if (mid)
844 free_extent_buffer(mid);
845 } else {
846 orig_slot -= btrfs_header_nritems(left);
847 path->slots[level] = orig_slot;
850 /* double check we haven't messed things up */
851 check_block(root, path, level);
852 if (orig_ptr !=
853 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
854 BUG();
855 enospc:
856 if (right)
857 free_extent_buffer(right);
858 if (left)
859 free_extent_buffer(left);
860 return ret;
863 /* returns zero if the push worked, non-zero otherwise */
864 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
865 struct btrfs_root *root,
866 struct btrfs_path *path, int level)
868 struct extent_buffer *right = NULL;
869 struct extent_buffer *mid;
870 struct extent_buffer *left = NULL;
871 struct extent_buffer *parent = NULL;
872 int ret = 0;
873 int wret;
874 int pslot;
875 int orig_slot = path->slots[level];
876 u64 orig_ptr;
878 if (level == 0)
879 return 1;
881 mid = path->nodes[level];
882 WARN_ON(btrfs_header_generation(mid) != trans->transid);
883 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
885 if (level < BTRFS_MAX_LEVEL - 1)
886 parent = path->nodes[level + 1];
887 pslot = path->slots[level + 1];
889 if (!parent)
890 return 1;
892 left = read_node_slot(root, parent, pslot - 1);
894 /* first, try to make some room in the middle buffer */
895 if (left) {
896 u32 left_nr;
897 left_nr = btrfs_header_nritems(left);
898 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
899 wret = 1;
900 } else {
901 ret = btrfs_cow_block(trans, root, left, parent,
902 pslot - 1, &left);
903 if (ret)
904 wret = 1;
905 else {
906 wret = push_node_left(trans, root,
907 left, mid, 0);
910 if (wret < 0)
911 ret = wret;
912 if (wret == 0) {
913 struct btrfs_disk_key disk_key;
914 orig_slot += left_nr;
915 btrfs_node_key(mid, &disk_key, 0);
916 btrfs_set_node_key(parent, &disk_key, pslot);
917 btrfs_mark_buffer_dirty(parent);
918 if (btrfs_header_nritems(left) > orig_slot) {
919 path->nodes[level] = left;
920 path->slots[level + 1] -= 1;
921 path->slots[level] = orig_slot;
922 free_extent_buffer(mid);
923 } else {
924 orig_slot -=
925 btrfs_header_nritems(left);
926 path->slots[level] = orig_slot;
927 free_extent_buffer(left);
929 return 0;
931 free_extent_buffer(left);
933 right= read_node_slot(root, parent, pslot + 1);
936 * then try to empty the right most buffer into the middle
938 if (right) {
939 u32 right_nr;
940 right_nr = btrfs_header_nritems(right);
941 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
942 wret = 1;
943 } else {
944 ret = btrfs_cow_block(trans, root, right,
945 parent, pslot + 1,
946 &right);
947 if (ret)
948 wret = 1;
949 else {
950 wret = balance_node_right(trans, root,
951 right, mid);
954 if (wret < 0)
955 ret = wret;
956 if (wret == 0) {
957 struct btrfs_disk_key disk_key;
959 btrfs_node_key(right, &disk_key, 0);
960 btrfs_set_node_key(parent, &disk_key, pslot + 1);
961 btrfs_mark_buffer_dirty(parent);
963 if (btrfs_header_nritems(mid) <= orig_slot) {
964 path->nodes[level] = right;
965 path->slots[level + 1] += 1;
966 path->slots[level] = orig_slot -
967 btrfs_header_nritems(mid);
968 free_extent_buffer(mid);
969 } else {
970 free_extent_buffer(right);
972 return 0;
974 free_extent_buffer(right);
976 return 1;
980 * readahead one full node of leaves
982 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
983 int level, int slot, u64 objectid)
985 struct extent_buffer *node;
986 struct btrfs_disk_key disk_key;
987 u32 nritems;
988 u64 search;
989 u64 lowest_read;
990 u64 highest_read;
991 u64 nread = 0;
992 int direction = path->reada;
993 struct extent_buffer *eb;
994 u32 nr;
995 u32 blocksize;
996 u32 nscan = 0;
998 if (level != 1)
999 return;
1001 if (!path->nodes[level])
1002 return;
1004 node = path->nodes[level];
1005 search = btrfs_node_blockptr(node, slot);
1006 blocksize = btrfs_level_size(root, level - 1);
1007 eb = btrfs_find_tree_block(root, search, blocksize);
1008 if (eb) {
1009 free_extent_buffer(eb);
1010 return;
1013 highest_read = search;
1014 lowest_read = search;
1016 nritems = btrfs_header_nritems(node);
1017 nr = slot;
1018 while(1) {
1019 if (direction < 0) {
1020 if (nr == 0)
1021 break;
1022 nr--;
1023 } else if (direction > 0) {
1024 nr++;
1025 if (nr >= nritems)
1026 break;
1028 if (path->reada < 0 && objectid) {
1029 btrfs_node_key(node, &disk_key, nr);
1030 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1031 break;
1033 search = btrfs_node_blockptr(node, nr);
1034 if ((search >= lowest_read && search <= highest_read) ||
1035 (search < lowest_read && lowest_read - search <= 32768) ||
1036 (search > highest_read && search - highest_read <= 32768)) {
1037 readahead_tree_block(root, search, blocksize);
1038 nread += blocksize;
1040 nscan++;
1041 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1042 break;
1043 if(nread > (1024 * 1024) || nscan > 128)
1044 break;
1046 if (search < lowest_read)
1047 lowest_read = search;
1048 if (search > highest_read)
1049 highest_read = search;
1054 * look for key in the tree. path is filled in with nodes along the way
1055 * if key is found, we return zero and you can find the item in the leaf
1056 * level of the path (level 0)
1058 * If the key isn't found, the path points to the slot where it should
1059 * be inserted, and 1 is returned. If there are other errors during the
1060 * search a negative error number is returned.
1062 * if ins_len > 0, nodes and leaves will be split as we walk down the
1063 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1064 * possible)
1066 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1067 *root, struct btrfs_key *key, struct btrfs_path *p, int
1068 ins_len, int cow)
1070 struct extent_buffer *b;
1071 u64 bytenr;
1072 u64 ptr_gen;
1073 int slot;
1074 int ret;
1075 int level;
1076 int should_reada = p->reada;
1077 u8 lowest_level = 0;
1079 lowest_level = p->lowest_level;
1080 WARN_ON(lowest_level && ins_len);
1081 WARN_ON(p->nodes[0] != NULL);
1083 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1085 again:
1086 b = root->node;
1087 extent_buffer_get(b);
1088 while (b) {
1089 level = btrfs_header_level(b);
1090 if (cow) {
1091 int wret;
1092 wret = btrfs_cow_block(trans, root, b,
1093 p->nodes[level + 1],
1094 p->slots[level + 1],
1095 &b);
1096 if (wret) {
1097 free_extent_buffer(b);
1098 return wret;
1101 BUG_ON(!cow && ins_len);
1102 if (level != btrfs_header_level(b))
1103 WARN_ON(1);
1104 level = btrfs_header_level(b);
1105 p->nodes[level] = b;
1106 ret = check_block(root, p, level);
1107 if (ret)
1108 return -1;
1109 ret = bin_search(b, key, level, &slot);
1110 if (level != 0) {
1111 if (ret && slot > 0)
1112 slot -= 1;
1113 p->slots[level] = slot;
1114 if (ins_len > 0 && btrfs_header_nritems(b) >=
1115 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1116 int sret = split_node(trans, root, p, level);
1117 BUG_ON(sret > 0);
1118 if (sret)
1119 return sret;
1120 b = p->nodes[level];
1121 slot = p->slots[level];
1122 } else if (ins_len < 0) {
1123 int sret = balance_level(trans, root, p,
1124 level);
1125 if (sret)
1126 return sret;
1127 b = p->nodes[level];
1128 if (!b) {
1129 btrfs_release_path(NULL, p);
1130 goto again;
1132 slot = p->slots[level];
1133 BUG_ON(btrfs_header_nritems(b) == 1);
1135 /* this is only true while dropping a snapshot */
1136 if (level == lowest_level)
1137 break;
1138 bytenr = btrfs_node_blockptr(b, slot);
1139 ptr_gen = btrfs_node_ptr_generation(b, slot);
1140 if (should_reada)
1141 reada_for_search(root, p, level, slot,
1142 key->objectid);
1143 b = read_tree_block(root, bytenr,
1144 btrfs_level_size(root, level - 1));
1145 if (ptr_gen != btrfs_header_generation(b)) {
1146 printk("block %llu bad gen wanted %llu "
1147 "found %llu\n",
1148 (unsigned long long)b->start,
1149 (unsigned long long)ptr_gen,
1150 (unsigned long long)btrfs_header_generation(b));
1152 } else {
1153 p->slots[level] = slot;
1154 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1155 sizeof(struct btrfs_item) + ins_len) {
1156 int sret = split_leaf(trans, root, key,
1157 p, ins_len, ret == 0);
1158 BUG_ON(sret > 0);
1159 if (sret)
1160 return sret;
1162 return ret;
1165 return 1;
1169 * adjust the pointers going up the tree, starting at level
1170 * making sure the right key of each node is points to 'key'.
1171 * This is used after shifting pointers to the left, so it stops
1172 * fixing up pointers when a given leaf/node is not in slot 0 of the
1173 * higher levels
1175 * If this fails to write a tree block, it returns -1, but continues
1176 * fixing up the blocks in ram so the tree is consistent.
1178 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root, struct btrfs_path *path,
1180 struct btrfs_disk_key *key, int level)
1182 int i;
1183 int ret = 0;
1184 struct extent_buffer *t;
1186 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1187 int tslot = path->slots[i];
1188 if (!path->nodes[i])
1189 break;
1190 t = path->nodes[i];
1191 btrfs_set_node_key(t, key, tslot);
1192 btrfs_mark_buffer_dirty(path->nodes[i]);
1193 if (tslot != 0)
1194 break;
1196 return ret;
1200 * try to push data from one node into the next node left in the
1201 * tree.
1203 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1204 * error, and > 0 if there was no room in the left hand block.
1206 static int push_node_left(struct btrfs_trans_handle *trans,
1207 struct btrfs_root *root, struct extent_buffer *dst,
1208 struct extent_buffer *src, int empty)
1210 int push_items = 0;
1211 int src_nritems;
1212 int dst_nritems;
1213 int ret = 0;
1215 src_nritems = btrfs_header_nritems(src);
1216 dst_nritems = btrfs_header_nritems(dst);
1217 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1218 WARN_ON(btrfs_header_generation(src) != trans->transid);
1219 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1221 if (!empty && src_nritems <= 8)
1222 return 1;
1224 if (push_items <= 0) {
1225 return 1;
1228 if (empty) {
1229 push_items = min(src_nritems, push_items);
1230 if (push_items < src_nritems) {
1231 /* leave at least 8 pointers in the node if
1232 * we aren't going to empty it
1234 if (src_nritems - push_items < 8) {
1235 if (push_items <= 8)
1236 return 1;
1237 push_items -= 8;
1240 } else
1241 push_items = min(src_nritems - 8, push_items);
1243 copy_extent_buffer(dst, src,
1244 btrfs_node_key_ptr_offset(dst_nritems),
1245 btrfs_node_key_ptr_offset(0),
1246 push_items * sizeof(struct btrfs_key_ptr));
1248 if (push_items < src_nritems) {
1249 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1250 btrfs_node_key_ptr_offset(push_items),
1251 (src_nritems - push_items) *
1252 sizeof(struct btrfs_key_ptr));
1254 btrfs_set_header_nritems(src, src_nritems - push_items);
1255 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1256 btrfs_mark_buffer_dirty(src);
1257 btrfs_mark_buffer_dirty(dst);
1258 return ret;
1262 * try to push data from one node into the next node right in the
1263 * tree.
1265 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1266 * error, and > 0 if there was no room in the right hand block.
1268 * this will only push up to 1/2 the contents of the left node over
1270 static int balance_node_right(struct btrfs_trans_handle *trans,
1271 struct btrfs_root *root,
1272 struct extent_buffer *dst,
1273 struct extent_buffer *src)
1275 int push_items = 0;
1276 int max_push;
1277 int src_nritems;
1278 int dst_nritems;
1279 int ret = 0;
1281 WARN_ON(btrfs_header_generation(src) != trans->transid);
1282 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1284 src_nritems = btrfs_header_nritems(src);
1285 dst_nritems = btrfs_header_nritems(dst);
1286 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1287 if (push_items <= 0) {
1288 return 1;
1291 if (src_nritems < 4) {
1292 return 1;
1295 max_push = src_nritems / 2 + 1;
1296 /* don't try to empty the node */
1297 if (max_push >= src_nritems) {
1298 return 1;
1301 if (max_push < push_items)
1302 push_items = max_push;
1304 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1305 btrfs_node_key_ptr_offset(0),
1306 (dst_nritems) *
1307 sizeof(struct btrfs_key_ptr));
1309 copy_extent_buffer(dst, src,
1310 btrfs_node_key_ptr_offset(0),
1311 btrfs_node_key_ptr_offset(src_nritems - push_items),
1312 push_items * sizeof(struct btrfs_key_ptr));
1314 btrfs_set_header_nritems(src, src_nritems - push_items);
1315 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1317 btrfs_mark_buffer_dirty(src);
1318 btrfs_mark_buffer_dirty(dst);
1319 return ret;
1323 * helper function to insert a new root level in the tree.
1324 * A new node is allocated, and a single item is inserted to
1325 * point to the existing root
1327 * returns zero on success or < 0 on failure.
1329 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1330 struct btrfs_root *root,
1331 struct btrfs_path *path, int level)
1333 u64 root_gen;
1334 u64 lower_gen;
1335 struct extent_buffer *lower;
1336 struct extent_buffer *c;
1337 struct btrfs_disk_key lower_key;
1339 BUG_ON(path->nodes[level]);
1340 BUG_ON(path->nodes[level-1] != root->node);
1342 if (root->ref_cows)
1343 root_gen = trans->transid;
1344 else
1345 root_gen = 0;
1347 lower = path->nodes[level-1];
1348 if (level == 1)
1349 btrfs_item_key(lower, &lower_key, 0);
1350 else
1351 btrfs_node_key(lower, &lower_key, 0);
1353 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1354 root->root_key.objectid,
1355 root_gen, lower_key.objectid, level,
1356 root->node->start, 0);
1357 if (IS_ERR(c))
1358 return PTR_ERR(c);
1359 memset_extent_buffer(c, 0, 0, root->nodesize);
1360 btrfs_set_header_nritems(c, 1);
1361 btrfs_set_header_level(c, level);
1362 btrfs_set_header_bytenr(c, c->start);
1363 btrfs_set_header_generation(c, trans->transid);
1364 btrfs_set_header_owner(c, root->root_key.objectid);
1366 write_extent_buffer(c, root->fs_info->fsid,
1367 (unsigned long)btrfs_header_fsid(c),
1368 BTRFS_FSID_SIZE);
1370 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1371 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1372 BTRFS_UUID_SIZE);
1374 btrfs_set_node_key(c, &lower_key, 0);
1375 btrfs_set_node_blockptr(c, 0, lower->start);
1376 lower_gen = btrfs_header_generation(lower);
1377 WARN_ON(lower_gen == 0);
1379 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1381 btrfs_mark_buffer_dirty(c);
1383 /* the super has an extra ref to root->node */
1384 free_extent_buffer(root->node);
1385 root->node = c;
1386 add_root_to_dirty_list(root);
1387 extent_buffer_get(c);
1388 path->nodes[level] = c;
1389 path->slots[level] = 0;
1391 if (root->ref_cows && lower_gen != trans->transid) {
1392 struct btrfs_path *back_path = btrfs_alloc_path();
1393 int ret;
1394 ret = btrfs_insert_extent_backref(trans,
1395 root->fs_info->extent_root,
1396 path, lower->start,
1397 root->root_key.objectid,
1398 trans->transid, 0, 0);
1399 BUG_ON(ret);
1400 btrfs_free_path(back_path);
1402 return 0;
1406 * worker function to insert a single pointer in a node.
1407 * the node should have enough room for the pointer already
1409 * slot and level indicate where you want the key to go, and
1410 * blocknr is the block the key points to.
1412 * returns zero on success and < 0 on any error
1414 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1415 *root, struct btrfs_path *path, struct btrfs_disk_key
1416 *key, u64 bytenr, int slot, int level)
1418 struct extent_buffer *lower;
1419 int nritems;
1421 BUG_ON(!path->nodes[level]);
1422 lower = path->nodes[level];
1423 nritems = btrfs_header_nritems(lower);
1424 if (slot > nritems)
1425 BUG();
1426 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1427 BUG();
1428 if (slot != nritems) {
1429 memmove_extent_buffer(lower,
1430 btrfs_node_key_ptr_offset(slot + 1),
1431 btrfs_node_key_ptr_offset(slot),
1432 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1434 btrfs_set_node_key(lower, key, slot);
1435 btrfs_set_node_blockptr(lower, slot, bytenr);
1436 WARN_ON(trans->transid == 0);
1437 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1438 btrfs_set_header_nritems(lower, nritems + 1);
1439 btrfs_mark_buffer_dirty(lower);
1440 return 0;
1444 * split the node at the specified level in path in two.
1445 * The path is corrected to point to the appropriate node after the split
1447 * Before splitting this tries to make some room in the node by pushing
1448 * left and right, if either one works, it returns right away.
1450 * returns 0 on success and < 0 on failure
1452 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1453 *root, struct btrfs_path *path, int level)
1455 u64 root_gen;
1456 struct extent_buffer *c;
1457 struct extent_buffer *split;
1458 struct btrfs_disk_key disk_key;
1459 int mid;
1460 int ret;
1461 int wret;
1462 u32 c_nritems;
1464 c = path->nodes[level];
1465 WARN_ON(btrfs_header_generation(c) != trans->transid);
1466 if (c == root->node) {
1467 /* trying to split the root, lets make a new one */
1468 ret = insert_new_root(trans, root, path, level + 1);
1469 if (ret)
1470 return ret;
1471 } else {
1472 ret = push_nodes_for_insert(trans, root, path, level);
1473 c = path->nodes[level];
1474 if (!ret && btrfs_header_nritems(c) <
1475 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1476 return 0;
1477 if (ret < 0)
1478 return ret;
1481 c_nritems = btrfs_header_nritems(c);
1482 if (root->ref_cows)
1483 root_gen = trans->transid;
1484 else
1485 root_gen = 0;
1487 btrfs_node_key(c, &disk_key, 0);
1488 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1489 root->root_key.objectid,
1490 root_gen,
1491 btrfs_disk_key_objectid(&disk_key),
1492 level, c->start, 0);
1493 if (IS_ERR(split))
1494 return PTR_ERR(split);
1496 btrfs_set_header_flags(split, btrfs_header_flags(c));
1497 btrfs_set_header_level(split, btrfs_header_level(c));
1498 btrfs_set_header_bytenr(split, split->start);
1499 btrfs_set_header_generation(split, trans->transid);
1500 btrfs_set_header_owner(split, root->root_key.objectid);
1501 btrfs_set_header_flags(split, 0);
1502 write_extent_buffer(split, root->fs_info->fsid,
1503 (unsigned long)btrfs_header_fsid(split),
1504 BTRFS_FSID_SIZE);
1505 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1506 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1507 BTRFS_UUID_SIZE);
1509 mid = (c_nritems + 1) / 2;
1511 copy_extent_buffer(split, c,
1512 btrfs_node_key_ptr_offset(0),
1513 btrfs_node_key_ptr_offset(mid),
1514 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1515 btrfs_set_header_nritems(split, c_nritems - mid);
1516 btrfs_set_header_nritems(c, mid);
1517 ret = 0;
1519 btrfs_mark_buffer_dirty(c);
1520 btrfs_mark_buffer_dirty(split);
1522 btrfs_node_key(split, &disk_key, 0);
1523 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1524 path->slots[level + 1] + 1,
1525 level + 1);
1526 if (wret)
1527 ret = wret;
1529 if (path->slots[level] >= mid) {
1530 path->slots[level] -= mid;
1531 free_extent_buffer(c);
1532 path->nodes[level] = split;
1533 path->slots[level + 1] += 1;
1534 } else {
1535 free_extent_buffer(split);
1537 return ret;
1541 * how many bytes are required to store the items in a leaf. start
1542 * and nr indicate which items in the leaf to check. This totals up the
1543 * space used both by the item structs and the item data
1545 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1547 int data_len;
1548 int nritems = btrfs_header_nritems(l);
1549 int end = min(nritems, start + nr) - 1;
1551 if (!nr)
1552 return 0;
1553 data_len = btrfs_item_end_nr(l, start);
1554 data_len = data_len - btrfs_item_offset_nr(l, end);
1555 data_len += sizeof(struct btrfs_item) * nr;
1556 WARN_ON(data_len < 0);
1557 return data_len;
1561 * The space between the end of the leaf items and
1562 * the start of the leaf data. IOW, how much room
1563 * the leaf has left for both items and data
1565 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1567 int nritems = btrfs_header_nritems(leaf);
1568 int ret;
1569 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1570 if (ret < 0) {
1571 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1572 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1573 leaf_space_used(leaf, 0, nritems), nritems);
1575 return ret;
1579 * push some data in the path leaf to the right, trying to free up at
1580 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1582 * returns 1 if the push failed because the other node didn't have enough
1583 * room, 0 if everything worked out and < 0 if there were major errors.
1585 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1586 *root, struct btrfs_path *path, int data_size,
1587 int empty)
1589 struct extent_buffer *left = path->nodes[0];
1590 struct extent_buffer *right;
1591 struct extent_buffer *upper;
1592 struct btrfs_disk_key disk_key;
1593 int slot;
1594 u32 i;
1595 int free_space;
1596 int push_space = 0;
1597 int push_items = 0;
1598 struct btrfs_item *item;
1599 u32 left_nritems;
1600 u32 nr;
1601 u32 right_nritems;
1602 u32 data_end;
1603 u32 this_item_size;
1604 int ret;
1606 slot = path->slots[1];
1607 if (!path->nodes[1]) {
1608 return 1;
1610 upper = path->nodes[1];
1611 if (slot >= btrfs_header_nritems(upper) - 1)
1612 return 1;
1614 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1615 root->leafsize);
1616 free_space = btrfs_leaf_free_space(root, right);
1617 if (free_space < data_size + sizeof(struct btrfs_item)) {
1618 free_extent_buffer(right);
1619 return 1;
1622 /* cow and double check */
1623 ret = btrfs_cow_block(trans, root, right, upper,
1624 slot + 1, &right);
1625 if (ret) {
1626 free_extent_buffer(right);
1627 return 1;
1629 free_space = btrfs_leaf_free_space(root, right);
1630 if (free_space < data_size + sizeof(struct btrfs_item)) {
1631 free_extent_buffer(right);
1632 return 1;
1635 left_nritems = btrfs_header_nritems(left);
1636 if (left_nritems == 0) {
1637 free_extent_buffer(right);
1638 return 1;
1641 if (empty)
1642 nr = 0;
1643 else
1644 nr = 1;
1646 i = left_nritems - 1;
1647 while (i >= nr) {
1648 item = btrfs_item_nr(left, i);
1650 if (path->slots[0] == i)
1651 push_space += data_size + sizeof(*item);
1653 this_item_size = btrfs_item_size(left, item);
1654 if (this_item_size + sizeof(*item) + push_space > free_space)
1655 break;
1656 push_items++;
1657 push_space += this_item_size + sizeof(*item);
1658 if (i == 0)
1659 break;
1660 i--;
1663 if (push_items == 0) {
1664 free_extent_buffer(right);
1665 return 1;
1668 if (!empty && push_items == left_nritems)
1669 WARN_ON(1);
1671 /* push left to right */
1672 right_nritems = btrfs_header_nritems(right);
1674 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1675 push_space -= leaf_data_end(root, left);
1677 /* make room in the right data area */
1678 data_end = leaf_data_end(root, right);
1679 memmove_extent_buffer(right,
1680 btrfs_leaf_data(right) + data_end - push_space,
1681 btrfs_leaf_data(right) + data_end,
1682 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1684 /* copy from the left data area */
1685 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1686 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1687 btrfs_leaf_data(left) + leaf_data_end(root, left),
1688 push_space);
1690 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1691 btrfs_item_nr_offset(0),
1692 right_nritems * sizeof(struct btrfs_item));
1694 /* copy the items from left to right */
1695 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1696 btrfs_item_nr_offset(left_nritems - push_items),
1697 push_items * sizeof(struct btrfs_item));
1699 /* update the item pointers */
1700 right_nritems += push_items;
1701 btrfs_set_header_nritems(right, right_nritems);
1702 push_space = BTRFS_LEAF_DATA_SIZE(root);
1703 for (i = 0; i < right_nritems; i++) {
1704 item = btrfs_item_nr(right, i);
1705 push_space -= btrfs_item_size(right, item);
1706 btrfs_set_item_offset(right, item, push_space);
1709 left_nritems -= push_items;
1710 btrfs_set_header_nritems(left, left_nritems);
1712 if (left_nritems)
1713 btrfs_mark_buffer_dirty(left);
1714 btrfs_mark_buffer_dirty(right);
1716 btrfs_item_key(right, &disk_key, 0);
1717 btrfs_set_node_key(upper, &disk_key, slot + 1);
1718 btrfs_mark_buffer_dirty(upper);
1720 /* then fixup the leaf pointer in the path */
1721 if (path->slots[0] >= left_nritems) {
1722 path->slots[0] -= left_nritems;
1723 free_extent_buffer(path->nodes[0]);
1724 path->nodes[0] = right;
1725 path->slots[1] += 1;
1726 } else {
1727 free_extent_buffer(right);
1729 return 0;
1732 * push some data in the path leaf to the left, trying to free up at
1733 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1735 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1736 *root, struct btrfs_path *path, int data_size,
1737 int empty)
1739 struct btrfs_disk_key disk_key;
1740 struct extent_buffer *right = path->nodes[0];
1741 struct extent_buffer *left;
1742 int slot;
1743 int i;
1744 int free_space;
1745 int push_space = 0;
1746 int push_items = 0;
1747 struct btrfs_item *item;
1748 u32 old_left_nritems;
1749 u32 right_nritems;
1750 u32 nr;
1751 int ret = 0;
1752 int wret;
1753 u32 this_item_size;
1754 u32 old_left_item_size;
1756 slot = path->slots[1];
1757 if (slot == 0)
1758 return 1;
1759 if (!path->nodes[1])
1760 return 1;
1762 right_nritems = btrfs_header_nritems(right);
1763 if (right_nritems == 0) {
1764 return 1;
1767 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
1768 slot - 1), root->leafsize);
1769 free_space = btrfs_leaf_free_space(root, left);
1770 if (free_space < data_size + sizeof(struct btrfs_item)) {
1771 free_extent_buffer(left);
1772 return 1;
1775 /* cow and double check */
1776 ret = btrfs_cow_block(trans, root, left,
1777 path->nodes[1], slot - 1, &left);
1778 if (ret) {
1779 /* we hit -ENOSPC, but it isn't fatal here */
1780 free_extent_buffer(left);
1781 return 1;
1784 free_space = btrfs_leaf_free_space(root, left);
1785 if (free_space < data_size + sizeof(struct btrfs_item)) {
1786 free_extent_buffer(left);
1787 return 1;
1790 if (empty)
1791 nr = right_nritems;
1792 else
1793 nr = right_nritems - 1;
1795 for (i = 0; i < nr; i++) {
1796 item = btrfs_item_nr(right, i);
1798 if (path->slots[0] == i)
1799 push_space += data_size + sizeof(*item);
1801 this_item_size = btrfs_item_size(right, item);
1802 if (this_item_size + sizeof(*item) + push_space > free_space)
1803 break;
1805 push_items++;
1806 push_space += this_item_size + sizeof(*item);
1809 if (push_items == 0) {
1810 free_extent_buffer(left);
1811 return 1;
1813 if (!empty && push_items == btrfs_header_nritems(right))
1814 WARN_ON(1);
1816 /* push data from right to left */
1817 copy_extent_buffer(left, right,
1818 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1819 btrfs_item_nr_offset(0),
1820 push_items * sizeof(struct btrfs_item));
1822 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1823 btrfs_item_offset_nr(right, push_items -1);
1825 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1826 leaf_data_end(root, left) - push_space,
1827 btrfs_leaf_data(right) +
1828 btrfs_item_offset_nr(right, push_items - 1),
1829 push_space);
1830 old_left_nritems = btrfs_header_nritems(left);
1831 BUG_ON(old_left_nritems < 0);
1833 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1834 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1835 u32 ioff;
1837 item = btrfs_item_nr(left, i);
1838 ioff = btrfs_item_offset(left, item);
1839 btrfs_set_item_offset(left, item,
1840 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1842 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1844 /* fixup right node */
1845 if (push_items > right_nritems) {
1846 printk("push items %d nr %u\n", push_items, right_nritems);
1847 WARN_ON(1);
1850 if (push_items < right_nritems) {
1851 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1852 leaf_data_end(root, right);
1853 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1854 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1855 btrfs_leaf_data(right) +
1856 leaf_data_end(root, right), push_space);
1858 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1859 btrfs_item_nr_offset(push_items),
1860 (btrfs_header_nritems(right) - push_items) *
1861 sizeof(struct btrfs_item));
1863 right_nritems -= push_items;
1864 btrfs_set_header_nritems(right, right_nritems);
1865 push_space = BTRFS_LEAF_DATA_SIZE(root);
1866 for (i = 0; i < right_nritems; i++) {
1867 item = btrfs_item_nr(right, i);
1868 push_space = push_space - btrfs_item_size(right, item);
1869 btrfs_set_item_offset(right, item, push_space);
1872 btrfs_mark_buffer_dirty(left);
1873 if (right_nritems)
1874 btrfs_mark_buffer_dirty(right);
1876 btrfs_item_key(right, &disk_key, 0);
1877 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1878 if (wret)
1879 ret = wret;
1881 /* then fixup the leaf pointer in the path */
1882 if (path->slots[0] < push_items) {
1883 path->slots[0] += old_left_nritems;
1884 free_extent_buffer(path->nodes[0]);
1885 path->nodes[0] = left;
1886 path->slots[1] -= 1;
1887 } else {
1888 free_extent_buffer(left);
1889 path->slots[0] -= push_items;
1891 BUG_ON(path->slots[0] < 0);
1892 return ret;
1896 * split the path's leaf in two, making sure there is at least data_size
1897 * available for the resulting leaf level of the path.
1899 * returns 0 if all went well and < 0 on failure.
1901 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1902 *root, struct btrfs_key *ins_key,
1903 struct btrfs_path *path, int data_size, int extend)
1905 u64 root_gen;
1906 struct extent_buffer *l;
1907 u32 nritems;
1908 int mid;
1909 int slot;
1910 struct extent_buffer *right;
1911 int space_needed = data_size + sizeof(struct btrfs_item);
1912 int data_copy_size;
1913 int rt_data_off;
1914 int i;
1915 int ret = 0;
1916 int wret;
1917 int double_split;
1918 int num_doubles = 0;
1919 struct btrfs_disk_key disk_key;
1921 if (extend)
1922 space_needed = data_size;
1924 if (root->ref_cows)
1925 root_gen = trans->transid;
1926 else
1927 root_gen = 0;
1929 /* first try to make some room by pushing left and right */
1930 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
1931 wret = push_leaf_right(trans, root, path, data_size, 0);
1932 if (wret < 0) {
1933 return wret;
1935 if (wret) {
1936 wret = push_leaf_left(trans, root, path, data_size, 0);
1937 if (wret < 0)
1938 return wret;
1940 l = path->nodes[0];
1942 /* did the pushes work? */
1943 if (btrfs_leaf_free_space(root, l) >= space_needed)
1944 return 0;
1947 if (!path->nodes[1]) {
1948 ret = insert_new_root(trans, root, path, 1);
1949 if (ret)
1950 return ret;
1952 again:
1953 double_split = 0;
1954 l = path->nodes[0];
1955 slot = path->slots[0];
1956 nritems = btrfs_header_nritems(l);
1957 mid = (nritems + 1)/ 2;
1959 btrfs_item_key(l, &disk_key, 0);
1961 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
1962 root->root_key.objectid,
1963 root_gen, disk_key.objectid, 0,
1964 l->start, 0);
1965 if (IS_ERR(right)) {
1966 BUG_ON(1);
1967 return PTR_ERR(right);
1970 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1971 btrfs_set_header_bytenr(right, right->start);
1972 btrfs_set_header_generation(right, trans->transid);
1973 btrfs_set_header_owner(right, root->root_key.objectid);
1974 btrfs_set_header_level(right, 0);
1975 write_extent_buffer(right, root->fs_info->fsid,
1976 (unsigned long)btrfs_header_fsid(right),
1977 BTRFS_FSID_SIZE);
1979 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
1980 (unsigned long)btrfs_header_chunk_tree_uuid(right),
1981 BTRFS_UUID_SIZE);
1982 if (mid <= slot) {
1983 if (nritems == 1 ||
1984 leaf_space_used(l, mid, nritems - mid) + space_needed >
1985 BTRFS_LEAF_DATA_SIZE(root)) {
1986 if (slot >= nritems) {
1987 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1988 btrfs_set_header_nritems(right, 0);
1989 wret = insert_ptr(trans, root, path,
1990 &disk_key, right->start,
1991 path->slots[1] + 1, 1);
1992 if (wret)
1993 ret = wret;
1994 free_extent_buffer(path->nodes[0]);
1995 path->nodes[0] = right;
1996 path->slots[0] = 0;
1997 path->slots[1] += 1;
1998 return ret;
2000 mid = slot;
2001 if (mid != nritems &&
2002 leaf_space_used(l, mid, nritems - mid) +
2003 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2004 double_split = 1;
2007 } else {
2008 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2009 BTRFS_LEAF_DATA_SIZE(root)) {
2010 if (!extend && slot == 0) {
2011 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2012 btrfs_set_header_nritems(right, 0);
2013 wret = insert_ptr(trans, root, path,
2014 &disk_key,
2015 right->start,
2016 path->slots[1], 1);
2017 if (wret)
2018 ret = wret;
2019 free_extent_buffer(path->nodes[0]);
2020 path->nodes[0] = right;
2021 path->slots[0] = 0;
2022 if (path->slots[1] == 0) {
2023 wret = fixup_low_keys(trans, root,
2024 path, &disk_key, 1);
2025 if (wret)
2026 ret = wret;
2028 return ret;
2029 } else if (extend && slot == 0) {
2030 mid = 1;
2031 } else {
2032 mid = slot;
2033 if (mid != nritems &&
2034 leaf_space_used(l, mid, nritems - mid) +
2035 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2036 double_split = 1;
2041 nritems = nritems - mid;
2042 btrfs_set_header_nritems(right, nritems);
2043 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2045 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2046 btrfs_item_nr_offset(mid),
2047 nritems * sizeof(struct btrfs_item));
2049 copy_extent_buffer(right, l,
2050 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2051 data_copy_size, btrfs_leaf_data(l) +
2052 leaf_data_end(root, l), data_copy_size);
2054 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2055 btrfs_item_end_nr(l, mid);
2057 for (i = 0; i < nritems; i++) {
2058 struct btrfs_item *item = btrfs_item_nr(right, i);
2059 u32 ioff = btrfs_item_offset(right, item);
2060 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2063 btrfs_set_header_nritems(l, mid);
2064 ret = 0;
2065 btrfs_item_key(right, &disk_key, 0);
2066 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2067 path->slots[1] + 1, 1);
2068 if (wret)
2069 ret = wret;
2071 btrfs_mark_buffer_dirty(right);
2072 btrfs_mark_buffer_dirty(l);
2073 BUG_ON(path->slots[0] != slot);
2075 if (mid <= slot) {
2076 free_extent_buffer(path->nodes[0]);
2077 path->nodes[0] = right;
2078 path->slots[0] -= mid;
2079 path->slots[1] += 1;
2080 } else
2081 free_extent_buffer(right);
2083 BUG_ON(path->slots[0] < 0);
2085 if (double_split) {
2086 BUG_ON(num_doubles != 0);
2087 num_doubles++;
2088 goto again;
2090 return ret;
2093 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2094 struct btrfs_root *root,
2095 struct btrfs_path *path,
2096 u32 new_size, int from_end)
2098 int ret = 0;
2099 int slot;
2100 int slot_orig;
2101 struct extent_buffer *leaf;
2102 struct btrfs_item *item;
2103 u32 nritems;
2104 unsigned int data_end;
2105 unsigned int old_data_start;
2106 unsigned int old_size;
2107 unsigned int size_diff;
2108 int i;
2110 slot_orig = path->slots[0];
2111 leaf = path->nodes[0];
2112 slot = path->slots[0];
2114 old_size = btrfs_item_size_nr(leaf, slot);
2115 if (old_size == new_size)
2116 return 0;
2118 nritems = btrfs_header_nritems(leaf);
2119 data_end = leaf_data_end(root, leaf);
2121 old_data_start = btrfs_item_offset_nr(leaf, slot);
2123 size_diff = old_size - new_size;
2125 BUG_ON(slot < 0);
2126 BUG_ON(slot >= nritems);
2129 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2131 /* first correct the data pointers */
2132 for (i = slot; i < nritems; i++) {
2133 u32 ioff;
2134 item = btrfs_item_nr(leaf, i);
2135 ioff = btrfs_item_offset(leaf, item);
2136 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2139 /* shift the data */
2140 if (from_end) {
2141 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2142 data_end + size_diff, btrfs_leaf_data(leaf) +
2143 data_end, old_data_start + new_size - data_end);
2144 } else {
2145 struct btrfs_disk_key disk_key;
2146 u64 offset;
2148 btrfs_item_key(leaf, &disk_key, slot);
2150 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2151 unsigned long ptr;
2152 struct btrfs_file_extent_item *fi;
2154 fi = btrfs_item_ptr(leaf, slot,
2155 struct btrfs_file_extent_item);
2156 fi = (struct btrfs_file_extent_item *)(
2157 (unsigned long)fi - size_diff);
2159 if (btrfs_file_extent_type(leaf, fi) ==
2160 BTRFS_FILE_EXTENT_INLINE) {
2161 ptr = btrfs_item_ptr_offset(leaf, slot);
2162 memmove_extent_buffer(leaf, ptr,
2163 (unsigned long)fi,
2164 offsetof(struct btrfs_file_extent_item,
2165 disk_bytenr));
2169 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2170 data_end + size_diff, btrfs_leaf_data(leaf) +
2171 data_end, old_data_start - data_end);
2173 offset = btrfs_disk_key_offset(&disk_key);
2174 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2175 btrfs_set_item_key(leaf, &disk_key, slot);
2176 if (slot == 0)
2177 fixup_low_keys(trans, root, path, &disk_key, 1);
2180 item = btrfs_item_nr(leaf, slot);
2181 btrfs_set_item_size(leaf, item, new_size);
2182 btrfs_mark_buffer_dirty(leaf);
2184 ret = 0;
2185 if (btrfs_leaf_free_space(root, leaf) < 0) {
2186 btrfs_print_leaf(root, leaf);
2187 BUG();
2189 return ret;
2192 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root, struct btrfs_path *path,
2194 u32 data_size)
2196 int ret = 0;
2197 int slot;
2198 int slot_orig;
2199 struct extent_buffer *leaf;
2200 struct btrfs_item *item;
2201 u32 nritems;
2202 unsigned int data_end;
2203 unsigned int old_data;
2204 unsigned int old_size;
2205 int i;
2207 slot_orig = path->slots[0];
2208 leaf = path->nodes[0];
2210 nritems = btrfs_header_nritems(leaf);
2211 data_end = leaf_data_end(root, leaf);
2213 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2214 btrfs_print_leaf(root, leaf);
2215 BUG();
2217 slot = path->slots[0];
2218 old_data = btrfs_item_end_nr(leaf, slot);
2220 BUG_ON(slot < 0);
2221 if (slot >= nritems) {
2222 btrfs_print_leaf(root, leaf);
2223 printk("slot %d too large, nritems %d\n", slot, nritems);
2224 BUG_ON(1);
2228 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2230 /* first correct the data pointers */
2231 for (i = slot; i < nritems; i++) {
2232 u32 ioff;
2233 item = btrfs_item_nr(leaf, i);
2234 ioff = btrfs_item_offset(leaf, item);
2235 btrfs_set_item_offset(leaf, item, ioff - data_size);
2238 /* shift the data */
2239 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2240 data_end - data_size, btrfs_leaf_data(leaf) +
2241 data_end, old_data - data_end);
2243 data_end = old_data;
2244 old_size = btrfs_item_size_nr(leaf, slot);
2245 item = btrfs_item_nr(leaf, slot);
2246 btrfs_set_item_size(leaf, item, old_size + data_size);
2247 btrfs_mark_buffer_dirty(leaf);
2249 ret = 0;
2250 if (btrfs_leaf_free_space(root, leaf) < 0) {
2251 btrfs_print_leaf(root, leaf);
2252 BUG();
2254 return ret;
2258 * Given a key and some data, insert an item into the tree.
2259 * This does all the path init required, making room in the tree if needed.
2261 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2262 struct btrfs_root *root,
2263 struct btrfs_path *path,
2264 struct btrfs_key *cpu_key, u32 *data_size,
2265 int nr)
2267 struct extent_buffer *leaf;
2268 struct btrfs_item *item;
2269 int ret = 0;
2270 int slot;
2271 int slot_orig;
2272 int i;
2273 u32 nritems;
2274 u32 total_size = 0;
2275 u32 total_data = 0;
2276 unsigned int data_end;
2277 struct btrfs_disk_key disk_key;
2279 for (i = 0; i < nr; i++) {
2280 total_data += data_size[i];
2283 /* create a root if there isn't one */
2284 if (!root->node)
2285 BUG();
2287 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2288 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2289 if (ret == 0) {
2290 return -EEXIST;
2292 if (ret < 0)
2293 goto out;
2295 slot_orig = path->slots[0];
2296 leaf = path->nodes[0];
2298 nritems = btrfs_header_nritems(leaf);
2299 data_end = leaf_data_end(root, leaf);
2301 if (btrfs_leaf_free_space(root, leaf) <
2302 sizeof(struct btrfs_item) + total_size) {
2303 btrfs_print_leaf(root, leaf);
2304 printk("not enough freespace need %u have %d\n",
2305 total_size, btrfs_leaf_free_space(root, leaf));
2306 BUG();
2309 slot = path->slots[0];
2310 BUG_ON(slot < 0);
2312 if (slot != nritems) {
2313 int i;
2314 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2316 if (old_data < data_end) {
2317 btrfs_print_leaf(root, leaf);
2318 printk("slot %d old_data %d data_end %d\n",
2319 slot, old_data, data_end);
2320 BUG_ON(1);
2323 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2325 /* first correct the data pointers */
2326 for (i = slot; i < nritems; i++) {
2327 u32 ioff;
2329 item = btrfs_item_nr(leaf, i);
2330 ioff = btrfs_item_offset(leaf, item);
2331 btrfs_set_item_offset(leaf, item, ioff - total_data);
2334 /* shift the items */
2335 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2336 btrfs_item_nr_offset(slot),
2337 (nritems - slot) * sizeof(struct btrfs_item));
2339 /* shift the data */
2340 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2341 data_end - total_data, btrfs_leaf_data(leaf) +
2342 data_end, old_data - data_end);
2343 data_end = old_data;
2346 /* setup the item for the new data */
2347 for (i = 0; i < nr; i++) {
2348 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2349 btrfs_set_item_key(leaf, &disk_key, slot + i);
2350 item = btrfs_item_nr(leaf, slot + i);
2351 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2352 data_end -= data_size[i];
2353 btrfs_set_item_size(leaf, item, data_size[i]);
2355 btrfs_set_header_nritems(leaf, nritems + nr);
2356 btrfs_mark_buffer_dirty(leaf);
2358 ret = 0;
2359 if (slot == 0) {
2360 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2361 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2364 if (btrfs_leaf_free_space(root, leaf) < 0) {
2365 btrfs_print_leaf(root, leaf);
2366 BUG();
2369 out:
2370 return ret;
2374 * Given a key and some data, insert an item into the tree.
2375 * This does all the path init required, making room in the tree if needed.
2377 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2378 *root, struct btrfs_key *cpu_key, void *data, u32
2379 data_size)
2381 int ret = 0;
2382 struct btrfs_path *path;
2383 struct extent_buffer *leaf;
2384 unsigned long ptr;
2386 path = btrfs_alloc_path();
2387 BUG_ON(!path);
2388 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2389 if (!ret) {
2390 leaf = path->nodes[0];
2391 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2392 write_extent_buffer(leaf, data, ptr, data_size);
2393 btrfs_mark_buffer_dirty(leaf);
2395 btrfs_free_path(path);
2396 return ret;
2400 * delete the pointer from a given node.
2402 * If the delete empties a node, the node is removed from the tree,
2403 * continuing all the way the root if required. The root is converted into
2404 * a leaf if all the nodes are emptied.
2406 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2407 struct btrfs_path *path, int level, int slot)
2409 struct extent_buffer *parent = path->nodes[level];
2410 u32 nritems;
2411 int ret = 0;
2412 int wret;
2414 nritems = btrfs_header_nritems(parent);
2415 if (slot != nritems -1) {
2416 memmove_extent_buffer(parent,
2417 btrfs_node_key_ptr_offset(slot),
2418 btrfs_node_key_ptr_offset(slot + 1),
2419 sizeof(struct btrfs_key_ptr) *
2420 (nritems - slot - 1));
2422 nritems--;
2423 btrfs_set_header_nritems(parent, nritems);
2424 if (nritems == 0 && parent == root->node) {
2425 BUG_ON(btrfs_header_level(root->node) != 1);
2426 /* just turn the root into a leaf and break */
2427 btrfs_set_header_level(root->node, 0);
2428 } else if (slot == 0) {
2429 struct btrfs_disk_key disk_key;
2431 btrfs_node_key(parent, &disk_key, 0);
2432 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2433 if (wret)
2434 ret = wret;
2436 btrfs_mark_buffer_dirty(parent);
2437 return ret;
2441 * delete the item at the leaf level in path. If that empties
2442 * the leaf, remove it from the tree
2444 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2445 struct btrfs_path *path, int slot, int nr)
2447 struct extent_buffer *leaf;
2448 struct btrfs_item *item;
2449 int last_off;
2450 int dsize = 0;
2451 int ret = 0;
2452 int wret;
2453 int i;
2454 u32 nritems;
2456 leaf = path->nodes[0];
2457 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2459 for (i = 0; i < nr; i++)
2460 dsize += btrfs_item_size_nr(leaf, slot + i);
2462 nritems = btrfs_header_nritems(leaf);
2464 if (slot + nr != nritems) {
2465 int i;
2466 int data_end = leaf_data_end(root, leaf);
2468 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2469 data_end + dsize,
2470 btrfs_leaf_data(leaf) + data_end,
2471 last_off - data_end);
2473 for (i = slot + nr; i < nritems; i++) {
2474 u32 ioff;
2476 item = btrfs_item_nr(leaf, i);
2477 ioff = btrfs_item_offset(leaf, item);
2478 btrfs_set_item_offset(leaf, item, ioff + dsize);
2481 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2482 btrfs_item_nr_offset(slot + nr),
2483 sizeof(struct btrfs_item) *
2484 (nritems - slot - nr));
2486 btrfs_set_header_nritems(leaf, nritems - nr);
2487 nritems -= nr;
2489 /* delete the leaf if we've emptied it */
2490 if (nritems == 0) {
2491 if (leaf == root->node) {
2492 btrfs_set_header_level(leaf, 0);
2493 } else {
2494 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2495 clean_tree_block(trans, root, leaf);
2496 wait_on_tree_block_writeback(root, leaf);
2497 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2498 if (wret)
2499 ret = wret;
2500 wret = btrfs_free_extent(trans, root,
2501 leaf->start, leaf->len,
2502 btrfs_header_owner(path->nodes[1]),
2503 root_gen, 0, 0, 1);
2504 if (wret)
2505 ret = wret;
2507 } else {
2508 int used = leaf_space_used(leaf, 0, nritems);
2509 if (slot == 0) {
2510 struct btrfs_disk_key disk_key;
2512 btrfs_item_key(leaf, &disk_key, 0);
2513 wret = fixup_low_keys(trans, root, path,
2514 &disk_key, 1);
2515 if (wret)
2516 ret = wret;
2519 /* delete the leaf if it is mostly empty */
2520 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2521 /* push_leaf_left fixes the path.
2522 * make sure the path still points to our leaf
2523 * for possible call to del_ptr below
2525 slot = path->slots[1];
2526 extent_buffer_get(leaf);
2528 wret = push_leaf_left(trans, root, path, 1, 1);
2529 if (wret < 0 && wret != -ENOSPC)
2530 ret = wret;
2532 if (path->nodes[0] == leaf &&
2533 btrfs_header_nritems(leaf)) {
2534 wret = push_leaf_right(trans, root, path, 1, 1);
2535 if (wret < 0 && wret != -ENOSPC)
2536 ret = wret;
2539 if (btrfs_header_nritems(leaf) == 0) {
2540 u64 root_gen;
2541 u64 bytenr = leaf->start;
2542 u32 blocksize = leaf->len;
2544 root_gen = btrfs_header_generation(
2545 path->nodes[1]);
2547 clean_tree_block(trans, root, leaf);
2548 wait_on_tree_block_writeback(root, leaf);
2550 wret = del_ptr(trans, root, path, 1, slot);
2551 if (wret)
2552 ret = wret;
2554 free_extent_buffer(leaf);
2555 wret = btrfs_free_extent(trans, root, bytenr,
2556 blocksize,
2557 btrfs_header_owner(path->nodes[1]),
2558 root_gen, 0, 0, 1);
2559 if (wret)
2560 ret = wret;
2561 } else {
2562 btrfs_mark_buffer_dirty(leaf);
2563 free_extent_buffer(leaf);
2565 } else {
2566 btrfs_mark_buffer_dirty(leaf);
2569 return ret;
2573 * walk up the tree as far as required to find the previous leaf.
2574 * returns 0 if it found something or 1 if there are no lesser leaves.
2575 * returns < 0 on io errors.
2577 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2579 u64 bytenr;
2580 int slot;
2581 int level = 1;
2582 struct extent_buffer *c;
2583 struct extent_buffer *next = NULL;
2585 while(level < BTRFS_MAX_LEVEL) {
2586 if (!path->nodes[level])
2587 return 1;
2589 slot = path->slots[level];
2590 c = path->nodes[level];
2591 if (slot == 0) {
2592 level++;
2593 if (level == BTRFS_MAX_LEVEL)
2594 return 1;
2595 continue;
2597 slot--;
2599 bytenr = btrfs_node_blockptr(c, slot);
2600 if (next)
2601 free_extent_buffer(next);
2603 next = read_tree_block(root, bytenr,
2604 btrfs_level_size(root, level - 1));
2605 break;
2607 path->slots[level] = slot;
2608 while(1) {
2609 level--;
2610 c = path->nodes[level];
2611 free_extent_buffer(c);
2612 slot = btrfs_header_nritems(next);
2613 if (slot != 0)
2614 slot--;
2615 path->nodes[level] = next;
2616 path->slots[level] = slot;
2617 if (!level)
2618 break;
2619 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
2620 btrfs_level_size(root, level - 1));
2622 return 0;
2626 * walk up the tree as far as required to find the next leaf.
2627 * returns 0 if it found something or 1 if there are no greater leaves.
2628 * returns < 0 on io errors.
2630 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2632 int slot;
2633 int level = 1;
2634 u64 bytenr;
2635 struct extent_buffer *c;
2636 struct extent_buffer *next = NULL;
2638 while(level < BTRFS_MAX_LEVEL) {
2639 if (!path->nodes[level])
2640 return 1;
2642 slot = path->slots[level] + 1;
2643 c = path->nodes[level];
2644 if (slot >= btrfs_header_nritems(c)) {
2645 level++;
2646 if (level == BTRFS_MAX_LEVEL)
2647 return 1;
2648 continue;
2651 bytenr = btrfs_node_blockptr(c, slot);
2652 if (next)
2653 free_extent_buffer(next);
2655 if (path->reada)
2656 reada_for_search(root, path, level, slot, 0);
2658 next = read_tree_block(root, bytenr,
2659 btrfs_level_size(root, level -1));
2660 break;
2662 path->slots[level] = slot;
2663 while(1) {
2664 level--;
2665 c = path->nodes[level];
2666 free_extent_buffer(c);
2667 path->nodes[level] = next;
2668 path->slots[level] = 0;
2669 if (!level)
2670 break;
2671 if (path->reada)
2672 reada_for_search(root, path, level, 0, 0);
2673 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2674 btrfs_level_size(root, level - 1));
2676 return 0;
2679 int btrfs_previous_item(struct btrfs_root *root,
2680 struct btrfs_path *path, u64 min_objectid,
2681 int type)
2683 struct btrfs_key found_key;
2684 struct extent_buffer *leaf;
2685 int ret;
2687 while(1) {
2688 if (path->slots[0] == 0) {
2689 ret = btrfs_prev_leaf(root, path);
2690 if (ret != 0)
2691 return ret;
2692 } else {
2693 path->slots[0]--;
2695 leaf = path->nodes[0];
2696 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2697 if (found_key.type == type)
2698 return 0;
2700 return 1;