OSDN Git Service

Btrfs: add tree modification log functions
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / btrfs / ctree.c
1 /*
2  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/rbtree.h>
22 #include "ctree.h"
23 #include "disk-io.h"
24 #include "transaction.h"
25 #include "print-tree.h"
26 #include "locking.h"
27
28 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29                       *root, struct btrfs_path *path, int level);
30 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
31                       *root, struct btrfs_key *ins_key,
32                       struct btrfs_path *path, int data_size, int extend);
33 static int push_node_left(struct btrfs_trans_handle *trans,
34                           struct btrfs_root *root, struct extent_buffer *dst,
35                           struct extent_buffer *src, int empty);
36 static int balance_node_right(struct btrfs_trans_handle *trans,
37                               struct btrfs_root *root,
38                               struct extent_buffer *dst_buf,
39                               struct extent_buffer *src_buf);
40 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
41                    struct btrfs_path *path, int level, int slot);
42
43 struct btrfs_path *btrfs_alloc_path(void)
44 {
45         struct btrfs_path *path;
46         path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
47         return path;
48 }
49
50 /*
51  * set all locked nodes in the path to blocking locks.  This should
52  * be done before scheduling
53  */
54 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55 {
56         int i;
57         for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
58                 if (!p->nodes[i] || !p->locks[i])
59                         continue;
60                 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
61                 if (p->locks[i] == BTRFS_READ_LOCK)
62                         p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
63                 else if (p->locks[i] == BTRFS_WRITE_LOCK)
64                         p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
65         }
66 }
67
68 /*
69  * reset all the locked nodes in the patch to spinning locks.
70  *
71  * held is used to keep lockdep happy, when lockdep is enabled
72  * we set held to a blocking lock before we go around and
73  * retake all the spinlocks in the path.  You can safely use NULL
74  * for held
75  */
76 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
77                                         struct extent_buffer *held, int held_rw)
78 {
79         int i;
80
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
82         /* lockdep really cares that we take all of these spinlocks
83          * in the right order.  If any of the locks in the path are not
84          * currently blocking, it is going to complain.  So, make really
85          * really sure by forcing the path to blocking before we clear
86          * the path blocking.
87          */
88         if (held) {
89                 btrfs_set_lock_blocking_rw(held, held_rw);
90                 if (held_rw == BTRFS_WRITE_LOCK)
91                         held_rw = BTRFS_WRITE_LOCK_BLOCKING;
92                 else if (held_rw == BTRFS_READ_LOCK)
93                         held_rw = BTRFS_READ_LOCK_BLOCKING;
94         }
95         btrfs_set_path_blocking(p);
96 #endif
97
98         for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
99                 if (p->nodes[i] && p->locks[i]) {
100                         btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
101                         if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
102                                 p->locks[i] = BTRFS_WRITE_LOCK;
103                         else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
104                                 p->locks[i] = BTRFS_READ_LOCK;
105                 }
106         }
107
108 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109         if (held)
110                 btrfs_clear_lock_blocking_rw(held, held_rw);
111 #endif
112 }
113
114 /* this also releases the path */
115 void btrfs_free_path(struct btrfs_path *p)
116 {
117         if (!p)
118                 return;
119         btrfs_release_path(p);
120         kmem_cache_free(btrfs_path_cachep, p);
121 }
122
123 /*
124  * path release drops references on the extent buffers in the path
125  * and it drops any locks held by this path
126  *
127  * It is safe to call this on paths that no locks or extent buffers held.
128  */
129 noinline void btrfs_release_path(struct btrfs_path *p)
130 {
131         int i;
132
133         for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
134                 p->slots[i] = 0;
135                 if (!p->nodes[i])
136                         continue;
137                 if (p->locks[i]) {
138                         btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
139                         p->locks[i] = 0;
140                 }
141                 free_extent_buffer(p->nodes[i]);
142                 p->nodes[i] = NULL;
143         }
144 }
145
146 /*
147  * safely gets a reference on the root node of a tree.  A lock
148  * is not taken, so a concurrent writer may put a different node
149  * at the root of the tree.  See btrfs_lock_root_node for the
150  * looping required.
151  *
152  * The extent buffer returned by this has a reference taken, so
153  * it won't disappear.  It may stop being the root of the tree
154  * at any time because there are no locks held.
155  */
156 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
157 {
158         struct extent_buffer *eb;
159
160         while (1) {
161                 rcu_read_lock();
162                 eb = rcu_dereference(root->node);
163
164                 /*
165                  * RCU really hurts here, we could free up the root node because
166                  * it was cow'ed but we may not get the new root node yet so do
167                  * the inc_not_zero dance and if it doesn't work then
168                  * synchronize_rcu and try again.
169                  */
170                 if (atomic_inc_not_zero(&eb->refs)) {
171                         rcu_read_unlock();
172                         break;
173                 }
174                 rcu_read_unlock();
175                 synchronize_rcu();
176         }
177         return eb;
178 }
179
180 /* loop around taking references on and locking the root node of the
181  * tree until you end up with a lock on the root.  A locked buffer
182  * is returned, with a reference held.
183  */
184 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
185 {
186         struct extent_buffer *eb;
187
188         while (1) {
189                 eb = btrfs_root_node(root);
190                 btrfs_tree_lock(eb);
191                 if (eb == root->node)
192                         break;
193                 btrfs_tree_unlock(eb);
194                 free_extent_buffer(eb);
195         }
196         return eb;
197 }
198
199 /* loop around taking references on and locking the root node of the
200  * tree until you end up with a lock on the root.  A locked buffer
201  * is returned, with a reference held.
202  */
203 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
204 {
205         struct extent_buffer *eb;
206
207         while (1) {
208                 eb = btrfs_root_node(root);
209                 btrfs_tree_read_lock(eb);
210                 if (eb == root->node)
211                         break;
212                 btrfs_tree_read_unlock(eb);
213                 free_extent_buffer(eb);
214         }
215         return eb;
216 }
217
218 /* cowonly root (everything not a reference counted cow subvolume), just get
219  * put onto a simple dirty list.  transaction.c walks this to make sure they
220  * get properly updated on disk.
221  */
222 static void add_root_to_dirty_list(struct btrfs_root *root)
223 {
224         spin_lock(&root->fs_info->trans_lock);
225         if (root->track_dirty && list_empty(&root->dirty_list)) {
226                 list_add(&root->dirty_list,
227                          &root->fs_info->dirty_cowonly_roots);
228         }
229         spin_unlock(&root->fs_info->trans_lock);
230 }
231
232 /*
233  * used by snapshot creation to make a copy of a root for a tree with
234  * a given objectid.  The buffer with the new root node is returned in
235  * cow_ret, and this func returns zero on success or a negative error code.
236  */
237 int btrfs_copy_root(struct btrfs_trans_handle *trans,
238                       struct btrfs_root *root,
239                       struct extent_buffer *buf,
240                       struct extent_buffer **cow_ret, u64 new_root_objectid)
241 {
242         struct extent_buffer *cow;
243         int ret = 0;
244         int level;
245         struct btrfs_disk_key disk_key;
246
247         WARN_ON(root->ref_cows && trans->transid !=
248                 root->fs_info->running_transaction->transid);
249         WARN_ON(root->ref_cows && trans->transid != root->last_trans);
250
251         level = btrfs_header_level(buf);
252         if (level == 0)
253                 btrfs_item_key(buf, &disk_key, 0);
254         else
255                 btrfs_node_key(buf, &disk_key, 0);
256
257         cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
258                                      new_root_objectid, &disk_key, level,
259                                      buf->start, 0);
260         if (IS_ERR(cow))
261                 return PTR_ERR(cow);
262
263         copy_extent_buffer(cow, buf, 0, 0, cow->len);
264         btrfs_set_header_bytenr(cow, cow->start);
265         btrfs_set_header_generation(cow, trans->transid);
266         btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
267         btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
268                                      BTRFS_HEADER_FLAG_RELOC);
269         if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
270                 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
271         else
272                 btrfs_set_header_owner(cow, new_root_objectid);
273
274         write_extent_buffer(cow, root->fs_info->fsid,
275                             (unsigned long)btrfs_header_fsid(cow),
276                             BTRFS_FSID_SIZE);
277
278         WARN_ON(btrfs_header_generation(buf) > trans->transid);
279         if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
280                 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
281         else
282                 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
283
284         if (ret)
285                 return ret;
286
287         btrfs_mark_buffer_dirty(cow);
288         *cow_ret = cow;
289         return 0;
290 }
291
292 enum mod_log_op {
293         MOD_LOG_KEY_REPLACE,
294         MOD_LOG_KEY_ADD,
295         MOD_LOG_KEY_REMOVE,
296         MOD_LOG_KEY_REMOVE_WHILE_FREEING,
297         MOD_LOG_KEY_REMOVE_WHILE_MOVING,
298         MOD_LOG_MOVE_KEYS,
299         MOD_LOG_ROOT_REPLACE,
300 };
301
302 struct tree_mod_move {
303         int dst_slot;
304         int nr_items;
305 };
306
307 struct tree_mod_root {
308         u64 logical;
309         u8 level;
310 };
311
312 struct tree_mod_elem {
313         struct rb_node node;
314         u64 index;              /* shifted logical */
315         struct seq_list elem;
316         enum mod_log_op op;
317
318         /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
319         int slot;
320
321         /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
322         u64 generation;
323
324         /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
325         struct btrfs_disk_key key;
326         u64 blockptr;
327
328         /* this is used for op == MOD_LOG_MOVE_KEYS */
329         struct tree_mod_move move;
330
331         /* this is used for op == MOD_LOG_ROOT_REPLACE */
332         struct tree_mod_root old_root;
333 };
334
335 static inline void
336 __get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
337 {
338         elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
339         list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
340 }
341
342 void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
343                             struct seq_list *elem)
344 {
345         elem->flags = 1;
346         spin_lock(&fs_info->tree_mod_seq_lock);
347         __get_tree_mod_seq(fs_info, elem);
348         spin_unlock(&fs_info->tree_mod_seq_lock);
349 }
350
351 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
352                             struct seq_list *elem)
353 {
354         struct rb_root *tm_root;
355         struct rb_node *node;
356         struct rb_node *next;
357         struct seq_list *cur_elem;
358         struct tree_mod_elem *tm;
359         u64 min_seq = (u64)-1;
360         u64 seq_putting = elem->seq;
361
362         if (!seq_putting)
363                 return;
364
365         BUG_ON(!(elem->flags & 1));
366         spin_lock(&fs_info->tree_mod_seq_lock);
367         list_del(&elem->list);
368
369         list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
370                 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
371                         if (seq_putting > cur_elem->seq) {
372                                 /*
373                                  * blocker with lower sequence number exists, we
374                                  * cannot remove anything from the log
375                                  */
376                                 goto out;
377                         }
378                         min_seq = cur_elem->seq;
379                 }
380         }
381
382         /*
383          * anything that's lower than the lowest existing (read: blocked)
384          * sequence number can be removed from the tree.
385          */
386         write_lock(&fs_info->tree_mod_log_lock);
387         tm_root = &fs_info->tree_mod_log;
388         for (node = rb_first(tm_root); node; node = next) {
389                 next = rb_next(node);
390                 tm = container_of(node, struct tree_mod_elem, node);
391                 if (tm->elem.seq > min_seq)
392                         continue;
393                 rb_erase(node, tm_root);
394                 list_del(&tm->elem.list);
395                 kfree(tm);
396         }
397         write_unlock(&fs_info->tree_mod_log_lock);
398 out:
399         spin_unlock(&fs_info->tree_mod_seq_lock);
400 }
401
402 /*
403  * key order of the log:
404  *       index -> sequence
405  *
406  * the index is the shifted logical of the *new* root node for root replace
407  * operations, or the shifted logical of the affected block for all other
408  * operations.
409  */
410 static noinline int
411 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
412 {
413         struct rb_root *tm_root;
414         struct rb_node **new;
415         struct rb_node *parent = NULL;
416         struct tree_mod_elem *cur;
417         int ret = 0;
418
419         BUG_ON(!tm || !tm->elem.seq);
420
421         write_lock(&fs_info->tree_mod_log_lock);
422         tm_root = &fs_info->tree_mod_log;
423         new = &tm_root->rb_node;
424         while (*new) {
425                 cur = container_of(*new, struct tree_mod_elem, node);
426                 parent = *new;
427                 if (cur->index < tm->index)
428                         new = &((*new)->rb_left);
429                 else if (cur->index > tm->index)
430                         new = &((*new)->rb_right);
431                 else if (cur->elem.seq < tm->elem.seq)
432                         new = &((*new)->rb_left);
433                 else if (cur->elem.seq > tm->elem.seq)
434                         new = &((*new)->rb_right);
435                 else {
436                         kfree(tm);
437                         ret = -EEXIST;
438                         goto unlock;
439                 }
440         }
441
442         rb_link_node(&tm->node, parent, new);
443         rb_insert_color(&tm->node, tm_root);
444 unlock:
445         write_unlock(&fs_info->tree_mod_log_lock);
446         return ret;
447 }
448
449 int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
450                    struct tree_mod_elem **tm_ret)
451 {
452         struct tree_mod_elem *tm;
453         u64 seq = 0;
454
455         smp_mb();
456         if (list_empty(&fs_info->tree_mod_seq_list))
457                 return 0;
458
459         tm = *tm_ret = kzalloc(sizeof(*tm), flags);
460         if (!tm)
461                 return -ENOMEM;
462
463         __get_tree_mod_seq(fs_info, &tm->elem);
464         seq = tm->elem.seq;
465         tm->elem.flags = 0;
466
467         return seq;
468 }
469
470 static noinline int
471 tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
472                              struct extent_buffer *eb, int slot,
473                              enum mod_log_op op, gfp_t flags)
474 {
475         struct tree_mod_elem *tm;
476         int ret;
477
478         ret = tree_mod_alloc(fs_info, flags, &tm);
479         if (ret <= 0)
480                 return ret;
481
482         tm->index = eb->start >> PAGE_CACHE_SHIFT;
483         if (op != MOD_LOG_KEY_ADD) {
484                 btrfs_node_key(eb, &tm->key, slot);
485                 tm->blockptr = btrfs_node_blockptr(eb, slot);
486         }
487         tm->op = op;
488         tm->slot = slot;
489         tm->generation = btrfs_node_ptr_generation(eb, slot);
490
491         return __tree_mod_log_insert(fs_info, tm);
492 }
493
494 static noinline int
495 tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
496                         int slot, enum mod_log_op op)
497 {
498         return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
499 }
500
501 static noinline int
502 tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
503                          struct extent_buffer *eb, int dst_slot, int src_slot,
504                          int nr_items, gfp_t flags)
505 {
506         struct tree_mod_elem *tm;
507         int ret;
508         int i;
509
510         ret = tree_mod_alloc(fs_info, flags, &tm);
511         if (ret <= 0)
512                 return ret;
513
514         for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
515                 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
516                                               MOD_LOG_KEY_REMOVE_WHILE_MOVING);
517                 BUG_ON(ret < 0);
518         }
519
520         tm->index = eb->start >> PAGE_CACHE_SHIFT;
521         tm->slot = src_slot;
522         tm->move.dst_slot = dst_slot;
523         tm->move.nr_items = nr_items;
524         tm->op = MOD_LOG_MOVE_KEYS;
525
526         return __tree_mod_log_insert(fs_info, tm);
527 }
528
529 static noinline int
530 tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
531                          struct extent_buffer *old_root,
532                          struct extent_buffer *new_root, gfp_t flags)
533 {
534         struct tree_mod_elem *tm;
535         int ret;
536
537         ret = tree_mod_alloc(fs_info, flags, &tm);
538         if (ret <= 0)
539                 return ret;
540
541         tm->index = new_root->start >> PAGE_CACHE_SHIFT;
542         tm->old_root.logical = old_root->start;
543         tm->old_root.level = btrfs_header_level(old_root);
544         tm->generation = btrfs_header_generation(old_root);
545         tm->op = MOD_LOG_ROOT_REPLACE;
546
547         return __tree_mod_log_insert(fs_info, tm);
548 }
549
550 static struct tree_mod_elem *
551 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
552                       int smallest)
553 {
554         struct rb_root *tm_root;
555         struct rb_node *node;
556         struct tree_mod_elem *cur = NULL;
557         struct tree_mod_elem *found = NULL;
558         u64 index = start >> PAGE_CACHE_SHIFT;
559
560         read_lock(&fs_info->tree_mod_log_lock);
561         tm_root = &fs_info->tree_mod_log;
562         node = tm_root->rb_node;
563         while (node) {
564                 cur = container_of(node, struct tree_mod_elem, node);
565                 if (cur->index < index) {
566                         node = node->rb_left;
567                 } else if (cur->index > index) {
568                         node = node->rb_right;
569                 } else if (cur->elem.seq < min_seq) {
570                         node = node->rb_left;
571                 } else if (!smallest) {
572                         /* we want the node with the highest seq */
573                         if (found)
574                                 BUG_ON(found->elem.seq > cur->elem.seq);
575                         found = cur;
576                         node = node->rb_left;
577                 } else if (cur->elem.seq > min_seq) {
578                         /* we want the node with the smallest seq */
579                         if (found)
580                                 BUG_ON(found->elem.seq < cur->elem.seq);
581                         found = cur;
582                         node = node->rb_right;
583                 } else {
584                         found = cur;
585                         break;
586                 }
587         }
588         read_unlock(&fs_info->tree_mod_log_lock);
589
590         return found;
591 }
592
593 /*
594  * this returns the element from the log with the smallest time sequence
595  * value that's in the log (the oldest log item). any element with a time
596  * sequence lower than min_seq will be ignored.
597  */
598 static struct tree_mod_elem *
599 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
600                            u64 min_seq)
601 {
602         return __tree_mod_log_search(fs_info, start, min_seq, 1);
603 }
604
605 /*
606  * this returns the element from the log with the largest time sequence
607  * value that's in the log (the most recent log item). any element with
608  * a time sequence lower than min_seq will be ignored.
609  */
610 static struct tree_mod_elem *
611 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
612 {
613         return __tree_mod_log_search(fs_info, start, min_seq, 0);
614 }
615
616 static inline void
617 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
618                      struct extent_buffer *src, unsigned long dst_offset,
619                      unsigned long src_offset, int nr_items)
620 {
621         int ret;
622         int i;
623
624         smp_mb();
625         if (list_empty(&fs_info->tree_mod_seq_list))
626                 return;
627
628         if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
629                 return;
630
631         /* speed this up by single seq for all operations? */
632         for (i = 0; i < nr_items; i++) {
633                 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
634                                               MOD_LOG_KEY_REMOVE);
635                 BUG_ON(ret < 0);
636                 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
637                                               MOD_LOG_KEY_ADD);
638                 BUG_ON(ret < 0);
639         }
640 }
641
642 static inline void
643 tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
644                      int dst_offset, int src_offset, int nr_items)
645 {
646         int ret;
647         ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
648                                        nr_items, GFP_NOFS);
649         BUG_ON(ret < 0);
650 }
651
652 static inline void
653 tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
654                           struct extent_buffer *eb,
655                           struct btrfs_disk_key *disk_key, int slot, int atomic)
656 {
657         int ret;
658
659         ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
660                                            MOD_LOG_KEY_REPLACE,
661                                            atomic ? GFP_ATOMIC : GFP_NOFS);
662         BUG_ON(ret < 0);
663 }
664
665 static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
666                                  struct extent_buffer *eb)
667 {
668         int i;
669         int ret;
670         u32 nritems;
671
672         smp_mb();
673         if (list_empty(&fs_info->tree_mod_seq_list))
674                 return;
675
676         if (btrfs_header_level(eb) == 0)
677                 return;
678
679         nritems = btrfs_header_nritems(eb);
680         for (i = nritems - 1; i >= 0; i--) {
681                 ret = tree_mod_log_insert_key(fs_info, eb, i,
682                                               MOD_LOG_KEY_REMOVE_WHILE_FREEING);
683                 BUG_ON(ret < 0);
684         }
685 }
686
687 static inline void
688 tree_mod_log_set_root_pointer(struct btrfs_root *root,
689                               struct extent_buffer *new_root_node)
690 {
691         int ret;
692         tree_mod_log_free_eb(root->fs_info, root->node);
693         ret = tree_mod_log_insert_root(root->fs_info, root->node,
694                                        new_root_node, GFP_NOFS);
695         BUG_ON(ret < 0);
696 }
697
698 /*
699  * check if the tree block can be shared by multiple trees
700  */
701 int btrfs_block_can_be_shared(struct btrfs_root *root,
702                               struct extent_buffer *buf)
703 {
704         /*
705          * Tree blocks not in refernece counted trees and tree roots
706          * are never shared. If a block was allocated after the last
707          * snapshot and the block was not allocated by tree relocation,
708          * we know the block is not shared.
709          */
710         if (root->ref_cows &&
711             buf != root->node && buf != root->commit_root &&
712             (btrfs_header_generation(buf) <=
713              btrfs_root_last_snapshot(&root->root_item) ||
714              btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
715                 return 1;
716 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
717         if (root->ref_cows &&
718             btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
719                 return 1;
720 #endif
721         return 0;
722 }
723
724 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
725                                        struct btrfs_root *root,
726                                        struct extent_buffer *buf,
727                                        struct extent_buffer *cow,
728                                        int *last_ref)
729 {
730         u64 refs;
731         u64 owner;
732         u64 flags;
733         u64 new_flags = 0;
734         int ret;
735
736         /*
737          * Backrefs update rules:
738          *
739          * Always use full backrefs for extent pointers in tree block
740          * allocated by tree relocation.
741          *
742          * If a shared tree block is no longer referenced by its owner
743          * tree (btrfs_header_owner(buf) == root->root_key.objectid),
744          * use full backrefs for extent pointers in tree block.
745          *
746          * If a tree block is been relocating
747          * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
748          * use full backrefs for extent pointers in tree block.
749          * The reason for this is some operations (such as drop tree)
750          * are only allowed for blocks use full backrefs.
751          */
752
753         if (btrfs_block_can_be_shared(root, buf)) {
754                 ret = btrfs_lookup_extent_info(trans, root, buf->start,
755                                                buf->len, &refs, &flags);
756                 if (ret)
757                         return ret;
758                 if (refs == 0) {
759                         ret = -EROFS;
760                         btrfs_std_error(root->fs_info, ret);
761                         return ret;
762                 }
763         } else {
764                 refs = 1;
765                 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
766                     btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
767                         flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
768                 else
769                         flags = 0;
770         }
771
772         owner = btrfs_header_owner(buf);
773         BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
774                !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
775
776         if (refs > 1) {
777                 if ((owner == root->root_key.objectid ||
778                      root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
779                     !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
780                         ret = btrfs_inc_ref(trans, root, buf, 1, 1);
781                         BUG_ON(ret); /* -ENOMEM */
782
783                         if (root->root_key.objectid ==
784                             BTRFS_TREE_RELOC_OBJECTID) {
785                                 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
786                                 BUG_ON(ret); /* -ENOMEM */
787                                 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
788                                 BUG_ON(ret); /* -ENOMEM */
789                         }
790                         new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
791                 } else {
792
793                         if (root->root_key.objectid ==
794                             BTRFS_TREE_RELOC_OBJECTID)
795                                 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
796                         else
797                                 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
798                         BUG_ON(ret); /* -ENOMEM */
799                 }
800                 if (new_flags != 0) {
801                         ret = btrfs_set_disk_extent_flags(trans, root,
802                                                           buf->start,
803                                                           buf->len,
804                                                           new_flags, 0);
805                         if (ret)
806                                 return ret;
807                 }
808         } else {
809                 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
810                         if (root->root_key.objectid ==
811                             BTRFS_TREE_RELOC_OBJECTID)
812                                 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
813                         else
814                                 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
815                         BUG_ON(ret); /* -ENOMEM */
816                         ret = btrfs_dec_ref(trans, root, buf, 1, 1);
817                         BUG_ON(ret); /* -ENOMEM */
818                 }
819                 clean_tree_block(trans, root, buf);
820                 *last_ref = 1;
821         }
822         return 0;
823 }
824
825 /*
826  * does the dirty work in cow of a single block.  The parent block (if
827  * supplied) is updated to point to the new cow copy.  The new buffer is marked
828  * dirty and returned locked.  If you modify the block it needs to be marked
829  * dirty again.
830  *
831  * search_start -- an allocation hint for the new block
832  *
833  * empty_size -- a hint that you plan on doing more cow.  This is the size in
834  * bytes the allocator should try to find free next to the block it returns.
835  * This is just a hint and may be ignored by the allocator.
836  */
837 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
838                              struct btrfs_root *root,
839                              struct extent_buffer *buf,
840                              struct extent_buffer *parent, int parent_slot,
841                              struct extent_buffer **cow_ret,
842                              u64 search_start, u64 empty_size)
843 {
844         struct btrfs_disk_key disk_key;
845         struct extent_buffer *cow;
846         int level, ret;
847         int last_ref = 0;
848         int unlock_orig = 0;
849         u64 parent_start;
850
851         if (*cow_ret == buf)
852                 unlock_orig = 1;
853
854         btrfs_assert_tree_locked(buf);
855
856         WARN_ON(root->ref_cows && trans->transid !=
857                 root->fs_info->running_transaction->transid);
858         WARN_ON(root->ref_cows && trans->transid != root->last_trans);
859
860         level = btrfs_header_level(buf);
861
862         if (level == 0)
863                 btrfs_item_key(buf, &disk_key, 0);
864         else
865                 btrfs_node_key(buf, &disk_key, 0);
866
867         if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
868                 if (parent)
869                         parent_start = parent->start;
870                 else
871                         parent_start = 0;
872         } else
873                 parent_start = 0;
874
875         cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
876                                      root->root_key.objectid, &disk_key,
877                                      level, search_start, empty_size);
878         if (IS_ERR(cow))
879                 return PTR_ERR(cow);
880
881         /* cow is set to blocking by btrfs_init_new_buffer */
882
883         copy_extent_buffer(cow, buf, 0, 0, cow->len);
884         btrfs_set_header_bytenr(cow, cow->start);
885         btrfs_set_header_generation(cow, trans->transid);
886         btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
887         btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
888                                      BTRFS_HEADER_FLAG_RELOC);
889         if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
890                 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
891         else
892                 btrfs_set_header_owner(cow, root->root_key.objectid);
893
894         write_extent_buffer(cow, root->fs_info->fsid,
895                             (unsigned long)btrfs_header_fsid(cow),
896                             BTRFS_FSID_SIZE);
897
898         ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
899         if (ret) {
900                 btrfs_abort_transaction(trans, root, ret);
901                 return ret;
902         }
903
904         if (root->ref_cows)
905                 btrfs_reloc_cow_block(trans, root, buf, cow);
906
907         if (buf == root->node) {
908                 WARN_ON(parent && parent != buf);
909                 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
910                     btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
911                         parent_start = buf->start;
912                 else
913                         parent_start = 0;
914
915                 extent_buffer_get(cow);
916                 rcu_assign_pointer(root->node, cow);
917
918                 btrfs_free_tree_block(trans, root, buf, parent_start,
919                                       last_ref);
920                 free_extent_buffer(buf);
921                 add_root_to_dirty_list(root);
922         } else {
923                 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
924                         parent_start = parent->start;
925                 else
926                         parent_start = 0;
927
928                 WARN_ON(trans->transid != btrfs_header_generation(parent));
929                 btrfs_set_node_blockptr(parent, parent_slot,
930                                         cow->start);
931                 btrfs_set_node_ptr_generation(parent, parent_slot,
932                                               trans->transid);
933                 btrfs_mark_buffer_dirty(parent);
934                 btrfs_free_tree_block(trans, root, buf, parent_start,
935                                       last_ref);
936         }
937         if (unlock_orig)
938                 btrfs_tree_unlock(buf);
939         free_extent_buffer_stale(buf);
940         btrfs_mark_buffer_dirty(cow);
941         *cow_ret = cow;
942         return 0;
943 }
944
945 static inline int should_cow_block(struct btrfs_trans_handle *trans,
946                                    struct btrfs_root *root,
947                                    struct extent_buffer *buf)
948 {
949         /* ensure we can see the force_cow */
950         smp_rmb();
951
952         /*
953          * We do not need to cow a block if
954          * 1) this block is not created or changed in this transaction;
955          * 2) this block does not belong to TREE_RELOC tree;
956          * 3) the root is not forced COW.
957          *
958          * What is forced COW:
959          *    when we create snapshot during commiting the transaction,
960          *    after we've finished coping src root, we must COW the shared
961          *    block to ensure the metadata consistency.
962          */
963         if (btrfs_header_generation(buf) == trans->transid &&
964             !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
965             !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
966               btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
967             !root->force_cow)
968                 return 0;
969         return 1;
970 }
971
972 /*
973  * cows a single block, see __btrfs_cow_block for the real work.
974  * This version of it has extra checks so that a block isn't cow'd more than
975  * once per transaction, as long as it hasn't been written yet
976  */
977 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
978                     struct btrfs_root *root, struct extent_buffer *buf,
979                     struct extent_buffer *parent, int parent_slot,
980                     struct extent_buffer **cow_ret)
981 {
982         u64 search_start;
983         int ret;
984
985         if (trans->transaction != root->fs_info->running_transaction) {
986                 printk(KERN_CRIT "trans %llu running %llu\n",
987                        (unsigned long long)trans->transid,
988                        (unsigned long long)
989                        root->fs_info->running_transaction->transid);
990                 WARN_ON(1);
991         }
992         if (trans->transid != root->fs_info->generation) {
993                 printk(KERN_CRIT "trans %llu running %llu\n",
994                        (unsigned long long)trans->transid,
995                        (unsigned long long)root->fs_info->generation);
996                 WARN_ON(1);
997         }
998
999         if (!should_cow_block(trans, root, buf)) {
1000                 *cow_ret = buf;
1001                 return 0;
1002         }
1003
1004         search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
1005
1006         if (parent)
1007                 btrfs_set_lock_blocking(parent);
1008         btrfs_set_lock_blocking(buf);
1009
1010         ret = __btrfs_cow_block(trans, root, buf, parent,
1011                                  parent_slot, cow_ret, search_start, 0);
1012
1013         trace_btrfs_cow_block(root, buf, *cow_ret);
1014
1015         return ret;
1016 }
1017
1018 /*
1019  * helper function for defrag to decide if two blocks pointed to by a
1020  * node are actually close by
1021  */
1022 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1023 {
1024         if (blocknr < other && other - (blocknr + blocksize) < 32768)
1025                 return 1;
1026         if (blocknr > other && blocknr - (other + blocksize) < 32768)
1027                 return 1;
1028         return 0;
1029 }
1030
1031 /*
1032  * compare two keys in a memcmp fashion
1033  */
1034 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1035 {
1036         struct btrfs_key k1;
1037
1038         btrfs_disk_key_to_cpu(&k1, disk);
1039
1040         return btrfs_comp_cpu_keys(&k1, k2);
1041 }
1042
1043 /*
1044  * same as comp_keys only with two btrfs_key's
1045  */
1046 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
1047 {
1048         if (k1->objectid > k2->objectid)
1049                 return 1;
1050         if (k1->objectid < k2->objectid)
1051                 return -1;
1052         if (k1->type > k2->type)
1053                 return 1;
1054         if (k1->type < k2->type)
1055                 return -1;
1056         if (k1->offset > k2->offset)
1057                 return 1;
1058         if (k1->offset < k2->offset)
1059                 return -1;
1060         return 0;
1061 }
1062
1063 /*
1064  * this is used by the defrag code to go through all the
1065  * leaves pointed to by a node and reallocate them so that
1066  * disk order is close to key order
1067  */
1068 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1069                        struct btrfs_root *root, struct extent_buffer *parent,
1070                        int start_slot, int cache_only, u64 *last_ret,
1071                        struct btrfs_key *progress)
1072 {
1073         struct extent_buffer *cur;
1074         u64 blocknr;
1075         u64 gen;
1076         u64 search_start = *last_ret;
1077         u64 last_block = 0;
1078         u64 other;
1079         u32 parent_nritems;
1080         int end_slot;
1081         int i;
1082         int err = 0;
1083         int parent_level;
1084         int uptodate;
1085         u32 blocksize;
1086         int progress_passed = 0;
1087         struct btrfs_disk_key disk_key;
1088
1089         parent_level = btrfs_header_level(parent);
1090         if (cache_only && parent_level != 1)
1091                 return 0;
1092
1093         if (trans->transaction != root->fs_info->running_transaction)
1094                 WARN_ON(1);
1095         if (trans->transid != root->fs_info->generation)
1096                 WARN_ON(1);
1097
1098         parent_nritems = btrfs_header_nritems(parent);
1099         blocksize = btrfs_level_size(root, parent_level - 1);
1100         end_slot = parent_nritems;
1101
1102         if (parent_nritems == 1)
1103                 return 0;
1104
1105         btrfs_set_lock_blocking(parent);
1106
1107         for (i = start_slot; i < end_slot; i++) {
1108                 int close = 1;
1109
1110                 btrfs_node_key(parent, &disk_key, i);
1111                 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1112                         continue;
1113
1114                 progress_passed = 1;
1115                 blocknr = btrfs_node_blockptr(parent, i);
1116                 gen = btrfs_node_ptr_generation(parent, i);
1117                 if (last_block == 0)
1118                         last_block = blocknr;
1119
1120                 if (i > 0) {
1121                         other = btrfs_node_blockptr(parent, i - 1);
1122                         close = close_blocks(blocknr, other, blocksize);
1123                 }
1124                 if (!close && i < end_slot - 2) {
1125                         other = btrfs_node_blockptr(parent, i + 1);
1126                         close = close_blocks(blocknr, other, blocksize);
1127                 }
1128                 if (close) {
1129                         last_block = blocknr;
1130                         continue;
1131                 }
1132
1133                 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1134                 if (cur)
1135                         uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1136                 else
1137                         uptodate = 0;
1138                 if (!cur || !uptodate) {
1139                         if (cache_only) {
1140                                 free_extent_buffer(cur);
1141                                 continue;
1142                         }
1143                         if (!cur) {
1144                                 cur = read_tree_block(root, blocknr,
1145                                                          blocksize, gen);
1146                                 if (!cur)
1147                                         return -EIO;
1148                         } else if (!uptodate) {
1149                                 btrfs_read_buffer(cur, gen);
1150                         }
1151                 }
1152                 if (search_start == 0)
1153                         search_start = last_block;
1154
1155                 btrfs_tree_lock(cur);
1156                 btrfs_set_lock_blocking(cur);
1157                 err = __btrfs_cow_block(trans, root, cur, parent, i,
1158                                         &cur, search_start,
1159                                         min(16 * blocksize,
1160                                             (end_slot - i) * blocksize));
1161                 if (err) {
1162                         btrfs_tree_unlock(cur);
1163                         free_extent_buffer(cur);
1164                         break;
1165                 }
1166                 search_start = cur->start;
1167                 last_block = cur->start;
1168                 *last_ret = search_start;
1169                 btrfs_tree_unlock(cur);
1170                 free_extent_buffer(cur);
1171         }
1172         return err;
1173 }
1174
1175 /*
1176  * The leaf data grows from end-to-front in the node.
1177  * this returns the address of the start of the last item,
1178  * which is the stop of the leaf data stack
1179  */
1180 static inline unsigned int leaf_data_end(struct btrfs_root *root,
1181                                          struct extent_buffer *leaf)
1182 {
1183         u32 nr = btrfs_header_nritems(leaf);
1184         if (nr == 0)
1185                 return BTRFS_LEAF_DATA_SIZE(root);
1186         return btrfs_item_offset_nr(leaf, nr - 1);
1187 }
1188
1189
1190 /*
1191  * search for key in the extent_buffer.  The items start at offset p,
1192  * and they are item_size apart.  There are 'max' items in p.
1193  *
1194  * the slot in the array is returned via slot, and it points to
1195  * the place where you would insert key if it is not found in
1196  * the array.
1197  *
1198  * slot may point to max if the key is bigger than all of the keys
1199  */
1200 static noinline int generic_bin_search(struct extent_buffer *eb,
1201                                        unsigned long p,
1202                                        int item_size, struct btrfs_key *key,
1203                                        int max, int *slot)
1204 {
1205         int low = 0;
1206         int high = max;
1207         int mid;
1208         int ret;
1209         struct btrfs_disk_key *tmp = NULL;
1210         struct btrfs_disk_key unaligned;
1211         unsigned long offset;
1212         char *kaddr = NULL;
1213         unsigned long map_start = 0;
1214         unsigned long map_len = 0;
1215         int err;
1216
1217         while (low < high) {
1218                 mid = (low + high) / 2;
1219                 offset = p + mid * item_size;
1220
1221                 if (!kaddr || offset < map_start ||
1222                     (offset + sizeof(struct btrfs_disk_key)) >
1223                     map_start + map_len) {
1224
1225                         err = map_private_extent_buffer(eb, offset,
1226                                                 sizeof(struct btrfs_disk_key),
1227                                                 &kaddr, &map_start, &map_len);
1228
1229                         if (!err) {
1230                                 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1231                                                         map_start);
1232                         } else {
1233                                 read_extent_buffer(eb, &unaligned,
1234                                                    offset, sizeof(unaligned));
1235                                 tmp = &unaligned;
1236                         }
1237
1238                 } else {
1239                         tmp = (struct btrfs_disk_key *)(kaddr + offset -
1240                                                         map_start);
1241                 }
1242                 ret = comp_keys(tmp, key);
1243
1244                 if (ret < 0)
1245                         low = mid + 1;
1246                 else if (ret > 0)
1247                         high = mid;
1248                 else {
1249                         *slot = mid;
1250                         return 0;
1251                 }
1252         }
1253         *slot = low;
1254         return 1;
1255 }
1256
1257 /*
1258  * simple bin_search frontend that does the right thing for
1259  * leaves vs nodes
1260  */
1261 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1262                       int level, int *slot)
1263 {
1264         if (level == 0) {
1265                 return generic_bin_search(eb,
1266                                           offsetof(struct btrfs_leaf, items),
1267                                           sizeof(struct btrfs_item),
1268                                           key, btrfs_header_nritems(eb),
1269                                           slot);
1270         } else {
1271                 return generic_bin_search(eb,
1272                                           offsetof(struct btrfs_node, ptrs),
1273                                           sizeof(struct btrfs_key_ptr),
1274                                           key, btrfs_header_nritems(eb),
1275                                           slot);
1276         }
1277         return -1;
1278 }
1279
1280 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1281                      int level, int *slot)
1282 {
1283         return bin_search(eb, key, level, slot);
1284 }
1285
1286 static void root_add_used(struct btrfs_root *root, u32 size)
1287 {
1288         spin_lock(&root->accounting_lock);
1289         btrfs_set_root_used(&root->root_item,
1290                             btrfs_root_used(&root->root_item) + size);
1291         spin_unlock(&root->accounting_lock);
1292 }
1293
1294 static void root_sub_used(struct btrfs_root *root, u32 size)
1295 {
1296         spin_lock(&root->accounting_lock);
1297         btrfs_set_root_used(&root->root_item,
1298                             btrfs_root_used(&root->root_item) - size);
1299         spin_unlock(&root->accounting_lock);
1300 }
1301
1302 /* given a node and slot number, this reads the blocks it points to.  The
1303  * extent buffer is returned with a reference taken (but unlocked).
1304  * NULL is returned on error.
1305  */
1306 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
1307                                    struct extent_buffer *parent, int slot)
1308 {
1309         int level = btrfs_header_level(parent);
1310         if (slot < 0)
1311                 return NULL;
1312         if (slot >= btrfs_header_nritems(parent))
1313                 return NULL;
1314
1315         BUG_ON(level == 0);
1316
1317         return read_tree_block(root, btrfs_node_blockptr(parent, slot),
1318                        btrfs_level_size(root, level - 1),
1319                        btrfs_node_ptr_generation(parent, slot));
1320 }
1321
1322 /*
1323  * node level balancing, used to make sure nodes are in proper order for
1324  * item deletion.  We balance from the top down, so we have to make sure
1325  * that a deletion won't leave an node completely empty later on.
1326  */
1327 static noinline int balance_level(struct btrfs_trans_handle *trans,
1328                          struct btrfs_root *root,
1329                          struct btrfs_path *path, int level)
1330 {
1331         struct extent_buffer *right = NULL;
1332         struct extent_buffer *mid;
1333         struct extent_buffer *left = NULL;
1334         struct extent_buffer *parent = NULL;
1335         int ret = 0;
1336         int wret;
1337         int pslot;
1338         int orig_slot = path->slots[level];
1339         u64 orig_ptr;
1340
1341         if (level == 0)
1342                 return 0;
1343
1344         mid = path->nodes[level];
1345
1346         WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1347                 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1348         WARN_ON(btrfs_header_generation(mid) != trans->transid);
1349
1350         orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1351
1352         if (level < BTRFS_MAX_LEVEL - 1) {
1353                 parent = path->nodes[level + 1];
1354                 pslot = path->slots[level + 1];
1355         }
1356
1357         /*
1358          * deal with the case where there is only one pointer in the root
1359          * by promoting the node below to a root
1360          */
1361         if (!parent) {
1362                 struct extent_buffer *child;
1363
1364                 if (btrfs_header_nritems(mid) != 1)
1365                         return 0;
1366
1367                 /* promote the child to a root */
1368                 child = read_node_slot(root, mid, 0);
1369                 if (!child) {
1370                         ret = -EROFS;
1371                         btrfs_std_error(root->fs_info, ret);
1372                         goto enospc;
1373                 }
1374
1375                 btrfs_tree_lock(child);
1376                 btrfs_set_lock_blocking(child);
1377                 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1378                 if (ret) {
1379                         btrfs_tree_unlock(child);
1380                         free_extent_buffer(child);
1381                         goto enospc;
1382                 }
1383
1384                 rcu_assign_pointer(root->node, child);
1385
1386                 add_root_to_dirty_list(root);
1387                 btrfs_tree_unlock(child);
1388
1389                 path->locks[level] = 0;
1390                 path->nodes[level] = NULL;
1391                 clean_tree_block(trans, root, mid);
1392                 btrfs_tree_unlock(mid);
1393                 /* once for the path */
1394                 free_extent_buffer(mid);
1395
1396                 root_sub_used(root, mid->len);
1397                 btrfs_free_tree_block(trans, root, mid, 0, 1);
1398                 /* once for the root ptr */
1399                 free_extent_buffer_stale(mid);
1400                 return 0;
1401         }
1402         if (btrfs_header_nritems(mid) >
1403             BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
1404                 return 0;
1405
1406         btrfs_header_nritems(mid);
1407
1408         left = read_node_slot(root, parent, pslot - 1);
1409         if (left) {
1410                 btrfs_tree_lock(left);
1411                 btrfs_set_lock_blocking(left);
1412                 wret = btrfs_cow_block(trans, root, left,
1413                                        parent, pslot - 1, &left);
1414                 if (wret) {
1415                         ret = wret;
1416                         goto enospc;
1417                 }
1418         }
1419         right = read_node_slot(root, parent, pslot + 1);
1420         if (right) {
1421                 btrfs_tree_lock(right);
1422                 btrfs_set_lock_blocking(right);
1423                 wret = btrfs_cow_block(trans, root, right,
1424                                        parent, pslot + 1, &right);
1425                 if (wret) {
1426                         ret = wret;
1427                         goto enospc;
1428                 }
1429         }
1430
1431         /* first, try to make some room in the middle buffer */
1432         if (left) {
1433                 orig_slot += btrfs_header_nritems(left);
1434                 wret = push_node_left(trans, root, left, mid, 1);
1435                 if (wret < 0)
1436                         ret = wret;
1437                 btrfs_header_nritems(mid);
1438         }
1439
1440         /*
1441          * then try to empty the right most buffer into the middle
1442          */
1443         if (right) {
1444                 wret = push_node_left(trans, root, mid, right, 1);
1445                 if (wret < 0 && wret != -ENOSPC)
1446                         ret = wret;
1447                 if (btrfs_header_nritems(right) == 0) {
1448                         clean_tree_block(trans, root, right);
1449                         btrfs_tree_unlock(right);
1450                         del_ptr(trans, root, path, level + 1, pslot + 1);
1451                         root_sub_used(root, right->len);
1452                         btrfs_free_tree_block(trans, root, right, 0, 1);
1453                         free_extent_buffer_stale(right);
1454                         right = NULL;
1455                 } else {
1456                         struct btrfs_disk_key right_key;
1457                         btrfs_node_key(right, &right_key, 0);
1458                         btrfs_set_node_key(parent, &right_key, pslot + 1);
1459                         btrfs_mark_buffer_dirty(parent);
1460                 }
1461         }
1462         if (btrfs_header_nritems(mid) == 1) {
1463                 /*
1464                  * we're not allowed to leave a node with one item in the
1465                  * tree during a delete.  A deletion from lower in the tree
1466                  * could try to delete the only pointer in this node.
1467                  * So, pull some keys from the left.
1468                  * There has to be a left pointer at this point because
1469                  * otherwise we would have pulled some pointers from the
1470                  * right
1471                  */
1472                 if (!left) {
1473                         ret = -EROFS;
1474                         btrfs_std_error(root->fs_info, ret);
1475                         goto enospc;
1476                 }
1477                 wret = balance_node_right(trans, root, mid, left);
1478                 if (wret < 0) {
1479                         ret = wret;
1480                         goto enospc;
1481                 }
1482                 if (wret == 1) {
1483                         wret = push_node_left(trans, root, left, mid, 1);
1484                         if (wret < 0)
1485                                 ret = wret;
1486                 }
1487                 BUG_ON(wret == 1);
1488         }
1489         if (btrfs_header_nritems(mid) == 0) {
1490                 clean_tree_block(trans, root, mid);
1491                 btrfs_tree_unlock(mid);
1492                 del_ptr(trans, root, path, level + 1, pslot);
1493                 root_sub_used(root, mid->len);
1494                 btrfs_free_tree_block(trans, root, mid, 0, 1);
1495                 free_extent_buffer_stale(mid);
1496                 mid = NULL;
1497         } else {
1498                 /* update the parent key to reflect our changes */
1499                 struct btrfs_disk_key mid_key;
1500                 btrfs_node_key(mid, &mid_key, 0);
1501                 btrfs_set_node_key(parent, &mid_key, pslot);
1502                 btrfs_mark_buffer_dirty(parent);
1503         }
1504
1505         /* update the path */
1506         if (left) {
1507                 if (btrfs_header_nritems(left) > orig_slot) {
1508                         extent_buffer_get(left);
1509                         /* left was locked after cow */
1510                         path->nodes[level] = left;
1511                         path->slots[level + 1] -= 1;
1512                         path->slots[level] = orig_slot;
1513                         if (mid) {
1514                                 btrfs_tree_unlock(mid);
1515                                 free_extent_buffer(mid);
1516                         }
1517                 } else {
1518                         orig_slot -= btrfs_header_nritems(left);
1519                         path->slots[level] = orig_slot;
1520                 }
1521         }
1522         /* double check we haven't messed things up */
1523         if (orig_ptr !=
1524             btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1525                 BUG();
1526 enospc:
1527         if (right) {
1528                 btrfs_tree_unlock(right);
1529                 free_extent_buffer(right);
1530         }
1531         if (left) {
1532                 if (path->nodes[level] != left)
1533                         btrfs_tree_unlock(left);
1534                 free_extent_buffer(left);
1535         }
1536         return ret;
1537 }
1538
1539 /* Node balancing for insertion.  Here we only split or push nodes around
1540  * when they are completely full.  This is also done top down, so we
1541  * have to be pessimistic.
1542  */
1543 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1544                                           struct btrfs_root *root,
1545                                           struct btrfs_path *path, int level)
1546 {
1547         struct extent_buffer *right = NULL;
1548         struct extent_buffer *mid;
1549         struct extent_buffer *left = NULL;
1550         struct extent_buffer *parent = NULL;
1551         int ret = 0;
1552         int wret;
1553         int pslot;
1554         int orig_slot = path->slots[level];
1555
1556         if (level == 0)
1557                 return 1;
1558
1559         mid = path->nodes[level];
1560         WARN_ON(btrfs_header_generation(mid) != trans->transid);
1561
1562         if (level < BTRFS_MAX_LEVEL - 1) {
1563                 parent = path->nodes[level + 1];
1564                 pslot = path->slots[level + 1];
1565         }
1566
1567         if (!parent)
1568                 return 1;
1569
1570         left = read_node_slot(root, parent, pslot - 1);
1571
1572         /* first, try to make some room in the middle buffer */
1573         if (left) {
1574                 u32 left_nr;
1575
1576                 btrfs_tree_lock(left);
1577                 btrfs_set_lock_blocking(left);
1578
1579                 left_nr = btrfs_header_nritems(left);
1580                 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1581                         wret = 1;
1582                 } else {
1583                         ret = btrfs_cow_block(trans, root, left, parent,
1584                                               pslot - 1, &left);
1585                         if (ret)
1586                                 wret = 1;
1587                         else {
1588                                 wret = push_node_left(trans, root,
1589                                                       left, mid, 0);
1590                         }
1591                 }
1592                 if (wret < 0)
1593                         ret = wret;
1594                 if (wret == 0) {
1595                         struct btrfs_disk_key disk_key;
1596                         orig_slot += left_nr;
1597                         btrfs_node_key(mid, &disk_key, 0);
1598                         btrfs_set_node_key(parent, &disk_key, pslot);
1599                         btrfs_mark_buffer_dirty(parent);
1600                         if (btrfs_header_nritems(left) > orig_slot) {
1601                                 path->nodes[level] = left;
1602                                 path->slots[level + 1] -= 1;
1603                                 path->slots[level] = orig_slot;
1604                                 btrfs_tree_unlock(mid);
1605                                 free_extent_buffer(mid);
1606                         } else {
1607                                 orig_slot -=
1608                                         btrfs_header_nritems(left);
1609                                 path->slots[level] = orig_slot;
1610                                 btrfs_tree_unlock(left);
1611                                 free_extent_buffer(left);
1612                         }
1613                         return 0;
1614                 }
1615                 btrfs_tree_unlock(left);
1616                 free_extent_buffer(left);
1617         }
1618         right = read_node_slot(root, parent, pslot + 1);
1619
1620         /*
1621          * then try to empty the right most buffer into the middle
1622          */
1623         if (right) {
1624                 u32 right_nr;
1625
1626                 btrfs_tree_lock(right);
1627                 btrfs_set_lock_blocking(right);
1628
1629                 right_nr = btrfs_header_nritems(right);
1630                 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1631                         wret = 1;
1632                 } else {
1633                         ret = btrfs_cow_block(trans, root, right,
1634                                               parent, pslot + 1,
1635                                               &right);
1636                         if (ret)
1637                                 wret = 1;
1638                         else {
1639                                 wret = balance_node_right(trans, root,
1640                                                           right, mid);
1641                         }
1642                 }
1643                 if (wret < 0)
1644                         ret = wret;
1645                 if (wret == 0) {
1646                         struct btrfs_disk_key disk_key;
1647
1648                         btrfs_node_key(right, &disk_key, 0);
1649                         btrfs_set_node_key(parent, &disk_key, pslot + 1);
1650                         btrfs_mark_buffer_dirty(parent);
1651
1652                         if (btrfs_header_nritems(mid) <= orig_slot) {
1653                                 path->nodes[level] = right;
1654                                 path->slots[level + 1] += 1;
1655                                 path->slots[level] = orig_slot -
1656                                         btrfs_header_nritems(mid);
1657                                 btrfs_tree_unlock(mid);
1658                                 free_extent_buffer(mid);
1659                         } else {
1660                                 btrfs_tree_unlock(right);
1661                                 free_extent_buffer(right);
1662                         }
1663                         return 0;
1664                 }
1665                 btrfs_tree_unlock(right);
1666                 free_extent_buffer(right);
1667         }
1668         return 1;
1669 }
1670
1671 /*
1672  * readahead one full node of leaves, finding things that are close
1673  * to the block in 'slot', and triggering ra on them.
1674  */
1675 static void reada_for_search(struct btrfs_root *root,
1676                              struct btrfs_path *path,
1677                              int level, int slot, u64 objectid)
1678 {
1679         struct extent_buffer *node;
1680         struct btrfs_disk_key disk_key;
1681         u32 nritems;
1682         u64 search;
1683         u64 target;
1684         u64 nread = 0;
1685         u64 gen;
1686         int direction = path->reada;
1687         struct extent_buffer *eb;
1688         u32 nr;
1689         u32 blocksize;
1690         u32 nscan = 0;
1691
1692         if (level != 1)
1693                 return;
1694
1695         if (!path->nodes[level])
1696                 return;
1697
1698         node = path->nodes[level];
1699
1700         search = btrfs_node_blockptr(node, slot);
1701         blocksize = btrfs_level_size(root, level - 1);
1702         eb = btrfs_find_tree_block(root, search, blocksize);
1703         if (eb) {
1704                 free_extent_buffer(eb);
1705                 return;
1706         }
1707
1708         target = search;
1709
1710         nritems = btrfs_header_nritems(node);
1711         nr = slot;
1712
1713         while (1) {
1714                 if (direction < 0) {
1715                         if (nr == 0)
1716                                 break;
1717                         nr--;
1718                 } else if (direction > 0) {
1719                         nr++;
1720                         if (nr >= nritems)
1721                                 break;
1722                 }
1723                 if (path->reada < 0 && objectid) {
1724                         btrfs_node_key(node, &disk_key, nr);
1725                         if (btrfs_disk_key_objectid(&disk_key) != objectid)
1726                                 break;
1727                 }
1728                 search = btrfs_node_blockptr(node, nr);
1729                 if ((search <= target && target - search <= 65536) ||
1730                     (search > target && search - target <= 65536)) {
1731                         gen = btrfs_node_ptr_generation(node, nr);
1732                         readahead_tree_block(root, search, blocksize, gen);
1733                         nread += blocksize;
1734                 }
1735                 nscan++;
1736                 if ((nread > 65536 || nscan > 32))
1737                         break;
1738         }
1739 }
1740
1741 /*
1742  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1743  * cache
1744  */
1745 static noinline int reada_for_balance(struct btrfs_root *root,
1746                                       struct btrfs_path *path, int level)
1747 {
1748         int slot;
1749         int nritems;
1750         struct extent_buffer *parent;
1751         struct extent_buffer *eb;
1752         u64 gen;
1753         u64 block1 = 0;
1754         u64 block2 = 0;
1755         int ret = 0;
1756         int blocksize;
1757
1758         parent = path->nodes[level + 1];
1759         if (!parent)
1760                 return 0;
1761
1762         nritems = btrfs_header_nritems(parent);
1763         slot = path->slots[level + 1];
1764         blocksize = btrfs_level_size(root, level);
1765
1766         if (slot > 0) {
1767                 block1 = btrfs_node_blockptr(parent, slot - 1);
1768                 gen = btrfs_node_ptr_generation(parent, slot - 1);
1769                 eb = btrfs_find_tree_block(root, block1, blocksize);
1770                 /*
1771                  * if we get -eagain from btrfs_buffer_uptodate, we
1772                  * don't want to return eagain here.  That will loop
1773                  * forever
1774                  */
1775                 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
1776                         block1 = 0;
1777                 free_extent_buffer(eb);
1778         }
1779         if (slot + 1 < nritems) {
1780                 block2 = btrfs_node_blockptr(parent, slot + 1);
1781                 gen = btrfs_node_ptr_generation(parent, slot + 1);
1782                 eb = btrfs_find_tree_block(root, block2, blocksize);
1783                 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
1784                         block2 = 0;
1785                 free_extent_buffer(eb);
1786         }
1787         if (block1 || block2) {
1788                 ret = -EAGAIN;
1789
1790                 /* release the whole path */
1791                 btrfs_release_path(path);
1792
1793                 /* read the blocks */
1794                 if (block1)
1795                         readahead_tree_block(root, block1, blocksize, 0);
1796                 if (block2)
1797                         readahead_tree_block(root, block2, blocksize, 0);
1798
1799                 if (block1) {
1800                         eb = read_tree_block(root, block1, blocksize, 0);
1801                         free_extent_buffer(eb);
1802                 }
1803                 if (block2) {
1804                         eb = read_tree_block(root, block2, blocksize, 0);
1805                         free_extent_buffer(eb);
1806                 }
1807         }
1808         return ret;
1809 }
1810
1811
1812 /*
1813  * when we walk down the tree, it is usually safe to unlock the higher layers
1814  * in the tree.  The exceptions are when our path goes through slot 0, because
1815  * operations on the tree might require changing key pointers higher up in the
1816  * tree.
1817  *
1818  * callers might also have set path->keep_locks, which tells this code to keep
1819  * the lock if the path points to the last slot in the block.  This is part of
1820  * walking through the tree, and selecting the next slot in the higher block.
1821  *
1822  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1823  * if lowest_unlock is 1, level 0 won't be unlocked
1824  */
1825 static noinline void unlock_up(struct btrfs_path *path, int level,
1826                                int lowest_unlock, int min_write_lock_level,
1827                                int *write_lock_level)
1828 {
1829         int i;
1830         int skip_level = level;
1831         int no_skips = 0;
1832         struct extent_buffer *t;
1833
1834         for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1835                 if (!path->nodes[i])
1836                         break;
1837                 if (!path->locks[i])
1838                         break;
1839                 if (!no_skips && path->slots[i] == 0) {
1840                         skip_level = i + 1;
1841                         continue;
1842                 }
1843                 if (!no_skips && path->keep_locks) {
1844                         u32 nritems;
1845                         t = path->nodes[i];
1846                         nritems = btrfs_header_nritems(t);
1847                         if (nritems < 1 || path->slots[i] >= nritems - 1) {
1848                                 skip_level = i + 1;
1849                                 continue;
1850                         }
1851                 }
1852                 if (skip_level < i && i >= lowest_unlock)
1853                         no_skips = 1;
1854
1855                 t = path->nodes[i];
1856                 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1857                         btrfs_tree_unlock_rw(t, path->locks[i]);
1858                         path->locks[i] = 0;
1859                         if (write_lock_level &&
1860                             i > min_write_lock_level &&
1861                             i <= *write_lock_level) {
1862                                 *write_lock_level = i - 1;
1863                         }
1864                 }
1865         }
1866 }
1867
1868 /*
1869  * This releases any locks held in the path starting at level and
1870  * going all the way up to the root.
1871  *
1872  * btrfs_search_slot will keep the lock held on higher nodes in a few
1873  * corner cases, such as COW of the block at slot zero in the node.  This
1874  * ignores those rules, and it should only be called when there are no
1875  * more updates to be done higher up in the tree.
1876  */
1877 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1878 {
1879         int i;
1880
1881         if (path->keep_locks)
1882                 return;
1883
1884         for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1885                 if (!path->nodes[i])
1886                         continue;
1887                 if (!path->locks[i])
1888                         continue;
1889                 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1890                 path->locks[i] = 0;
1891         }
1892 }
1893
1894 /*
1895  * helper function for btrfs_search_slot.  The goal is to find a block
1896  * in cache without setting the path to blocking.  If we find the block
1897  * we return zero and the path is unchanged.
1898  *
1899  * If we can't find the block, we set the path blocking and do some
1900  * reada.  -EAGAIN is returned and the search must be repeated.
1901  */
1902 static int
1903 read_block_for_search(struct btrfs_trans_handle *trans,
1904                        struct btrfs_root *root, struct btrfs_path *p,
1905                        struct extent_buffer **eb_ret, int level, int slot,
1906                        struct btrfs_key *key)
1907 {
1908         u64 blocknr;
1909         u64 gen;
1910         u32 blocksize;
1911         struct extent_buffer *b = *eb_ret;
1912         struct extent_buffer *tmp;
1913         int ret;
1914
1915         blocknr = btrfs_node_blockptr(b, slot);
1916         gen = btrfs_node_ptr_generation(b, slot);
1917         blocksize = btrfs_level_size(root, level - 1);
1918
1919         tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1920         if (tmp) {
1921                 /* first we do an atomic uptodate check */
1922                 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1923                         if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1924                                 /*
1925                                  * we found an up to date block without
1926                                  * sleeping, return
1927                                  * right away
1928                                  */
1929                                 *eb_ret = tmp;
1930                                 return 0;
1931                         }
1932                         /* the pages were up to date, but we failed
1933                          * the generation number check.  Do a full
1934                          * read for the generation number that is correct.
1935                          * We must do this without dropping locks so
1936                          * we can trust our generation number
1937                          */
1938                         free_extent_buffer(tmp);
1939                         btrfs_set_path_blocking(p);
1940
1941                         /* now we're allowed to do a blocking uptodate check */
1942                         tmp = read_tree_block(root, blocknr, blocksize, gen);
1943                         if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
1944                                 *eb_ret = tmp;
1945                                 return 0;
1946                         }
1947                         free_extent_buffer(tmp);
1948                         btrfs_release_path(p);
1949                         return -EIO;
1950                 }
1951         }
1952
1953         /*
1954          * reduce lock contention at high levels
1955          * of the btree by dropping locks before
1956          * we read.  Don't release the lock on the current
1957          * level because we need to walk this node to figure
1958          * out which blocks to read.
1959          */
1960         btrfs_unlock_up_safe(p, level + 1);
1961         btrfs_set_path_blocking(p);
1962
1963         free_extent_buffer(tmp);
1964         if (p->reada)
1965                 reada_for_search(root, p, level, slot, key->objectid);
1966
1967         btrfs_release_path(p);
1968
1969         ret = -EAGAIN;
1970         tmp = read_tree_block(root, blocknr, blocksize, 0);
1971         if (tmp) {
1972                 /*
1973                  * If the read above didn't mark this buffer up to date,
1974                  * it will never end up being up to date.  Set ret to EIO now
1975                  * and give up so that our caller doesn't loop forever
1976                  * on our EAGAINs.
1977                  */
1978                 if (!btrfs_buffer_uptodate(tmp, 0, 0))
1979                         ret = -EIO;
1980                 free_extent_buffer(tmp);
1981         }
1982         return ret;
1983 }
1984
1985 /*
1986  * helper function for btrfs_search_slot.  This does all of the checks
1987  * for node-level blocks and does any balancing required based on
1988  * the ins_len.
1989  *
1990  * If no extra work was required, zero is returned.  If we had to
1991  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1992  * start over
1993  */
1994 static int
1995 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1996                        struct btrfs_root *root, struct btrfs_path *p,
1997                        struct extent_buffer *b, int level, int ins_len,
1998                        int *write_lock_level)
1999 {
2000         int ret;
2001         if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2002             BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2003                 int sret;
2004
2005                 if (*write_lock_level < level + 1) {
2006                         *write_lock_level = level + 1;
2007                         btrfs_release_path(p);
2008                         goto again;
2009                 }
2010
2011                 sret = reada_for_balance(root, p, level);
2012                 if (sret)
2013                         goto again;
2014
2015                 btrfs_set_path_blocking(p);
2016                 sret = split_node(trans, root, p, level);
2017                 btrfs_clear_path_blocking(p, NULL, 0);
2018
2019                 BUG_ON(sret > 0);
2020                 if (sret) {
2021                         ret = sret;
2022                         goto done;
2023                 }
2024                 b = p->nodes[level];
2025         } else if (ins_len < 0 && btrfs_header_nritems(b) <
2026                    BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
2027                 int sret;
2028
2029                 if (*write_lock_level < level + 1) {
2030                         *write_lock_level = level + 1;
2031                         btrfs_release_path(p);
2032                         goto again;
2033                 }
2034
2035                 sret = reada_for_balance(root, p, level);
2036                 if (sret)
2037                         goto again;
2038
2039                 btrfs_set_path_blocking(p);
2040                 sret = balance_level(trans, root, p, level);
2041                 btrfs_clear_path_blocking(p, NULL, 0);
2042
2043                 if (sret) {
2044                         ret = sret;
2045                         goto done;
2046                 }
2047                 b = p->nodes[level];
2048                 if (!b) {
2049                         btrfs_release_path(p);
2050                         goto again;
2051                 }
2052                 BUG_ON(btrfs_header_nritems(b) == 1);
2053         }
2054         return 0;
2055
2056 again:
2057         ret = -EAGAIN;
2058 done:
2059         return ret;
2060 }
2061
2062 /*
2063  * look for key in the tree.  path is filled in with nodes along the way
2064  * if key is found, we return zero and you can find the item in the leaf
2065  * level of the path (level 0)
2066  *
2067  * If the key isn't found, the path points to the slot where it should
2068  * be inserted, and 1 is returned.  If there are other errors during the
2069  * search a negative error number is returned.
2070  *
2071  * if ins_len > 0, nodes and leaves will be split as we walk down the
2072  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
2073  * possible)
2074  */
2075 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2076                       *root, struct btrfs_key *key, struct btrfs_path *p, int
2077                       ins_len, int cow)
2078 {
2079         struct extent_buffer *b;
2080         int slot;
2081         int ret;
2082         int err;
2083         int level;
2084         int lowest_unlock = 1;
2085         int root_lock;
2086         /* everything at write_lock_level or lower must be write locked */
2087         int write_lock_level = 0;
2088         u8 lowest_level = 0;
2089         int min_write_lock_level;
2090
2091         lowest_level = p->lowest_level;
2092         WARN_ON(lowest_level && ins_len > 0);
2093         WARN_ON(p->nodes[0] != NULL);
2094
2095         if (ins_len < 0) {
2096                 lowest_unlock = 2;
2097
2098                 /* when we are removing items, we might have to go up to level
2099                  * two as we update tree pointers  Make sure we keep write
2100                  * for those levels as well
2101                  */
2102                 write_lock_level = 2;
2103         } else if (ins_len > 0) {
2104                 /*
2105                  * for inserting items, make sure we have a write lock on
2106                  * level 1 so we can update keys
2107                  */
2108                 write_lock_level = 1;
2109         }
2110
2111         if (!cow)
2112                 write_lock_level = -1;
2113
2114         if (cow && (p->keep_locks || p->lowest_level))
2115                 write_lock_level = BTRFS_MAX_LEVEL;
2116
2117         min_write_lock_level = write_lock_level;
2118
2119 again:
2120         /*
2121          * we try very hard to do read locks on the root
2122          */
2123         root_lock = BTRFS_READ_LOCK;
2124         level = 0;
2125         if (p->search_commit_root) {
2126                 /*
2127                  * the commit roots are read only
2128                  * so we always do read locks
2129                  */
2130                 b = root->commit_root;
2131                 extent_buffer_get(b);
2132                 level = btrfs_header_level(b);
2133                 if (!p->skip_locking)
2134                         btrfs_tree_read_lock(b);
2135         } else {
2136                 if (p->skip_locking) {
2137                         b = btrfs_root_node(root);
2138                         level = btrfs_header_level(b);
2139                 } else {
2140                         /* we don't know the level of the root node
2141                          * until we actually have it read locked
2142                          */
2143                         b = btrfs_read_lock_root_node(root);
2144                         level = btrfs_header_level(b);
2145                         if (level <= write_lock_level) {
2146                                 /* whoops, must trade for write lock */
2147                                 btrfs_tree_read_unlock(b);
2148                                 free_extent_buffer(b);
2149                                 b = btrfs_lock_root_node(root);
2150                                 root_lock = BTRFS_WRITE_LOCK;
2151
2152                                 /* the level might have changed, check again */
2153                                 level = btrfs_header_level(b);
2154                         }
2155                 }
2156         }
2157         p->nodes[level] = b;
2158         if (!p->skip_locking)
2159                 p->locks[level] = root_lock;
2160
2161         while (b) {
2162                 level = btrfs_header_level(b);
2163
2164                 /*
2165                  * setup the path here so we can release it under lock
2166                  * contention with the cow code
2167                  */
2168                 if (cow) {
2169                         /*
2170                          * if we don't really need to cow this block
2171                          * then we don't want to set the path blocking,
2172                          * so we test it here
2173                          */
2174                         if (!should_cow_block(trans, root, b))
2175                                 goto cow_done;
2176
2177                         btrfs_set_path_blocking(p);
2178
2179                         /*
2180                          * must have write locks on this node and the
2181                          * parent
2182                          */
2183                         if (level + 1 > write_lock_level) {
2184                                 write_lock_level = level + 1;
2185                                 btrfs_release_path(p);
2186                                 goto again;
2187                         }
2188
2189                         err = btrfs_cow_block(trans, root, b,
2190                                               p->nodes[level + 1],
2191                                               p->slots[level + 1], &b);
2192                         if (err) {
2193                                 ret = err;
2194                                 goto done;
2195                         }
2196                 }
2197 cow_done:
2198                 BUG_ON(!cow && ins_len);
2199
2200                 p->nodes[level] = b;
2201                 btrfs_clear_path_blocking(p, NULL, 0);
2202
2203                 /*
2204                  * we have a lock on b and as long as we aren't changing
2205                  * the tree, there is no way to for the items in b to change.
2206                  * It is safe to drop the lock on our parent before we
2207                  * go through the expensive btree search on b.
2208                  *
2209                  * If cow is true, then we might be changing slot zero,
2210                  * which may require changing the parent.  So, we can't
2211                  * drop the lock until after we know which slot we're
2212                  * operating on.
2213                  */
2214                 if (!cow)
2215                         btrfs_unlock_up_safe(p, level + 1);
2216
2217                 ret = bin_search(b, key, level, &slot);
2218
2219                 if (level != 0) {
2220                         int dec = 0;
2221                         if (ret && slot > 0) {
2222                                 dec = 1;
2223                                 slot -= 1;
2224                         }
2225                         p->slots[level] = slot;
2226                         err = setup_nodes_for_search(trans, root, p, b, level,
2227                                              ins_len, &write_lock_level);
2228                         if (err == -EAGAIN)
2229                                 goto again;
2230                         if (err) {
2231                                 ret = err;
2232                                 goto done;
2233                         }
2234                         b = p->nodes[level];
2235                         slot = p->slots[level];
2236
2237                         /*
2238                          * slot 0 is special, if we change the key
2239                          * we have to update the parent pointer
2240                          * which means we must have a write lock
2241                          * on the parent
2242                          */
2243                         if (slot == 0 && cow &&
2244                             write_lock_level < level + 1) {
2245                                 write_lock_level = level + 1;
2246                                 btrfs_release_path(p);
2247                                 goto again;
2248                         }
2249
2250                         unlock_up(p, level, lowest_unlock,
2251                                   min_write_lock_level, &write_lock_level);
2252
2253                         if (level == lowest_level) {
2254                                 if (dec)
2255                                         p->slots[level]++;
2256                                 goto done;
2257                         }
2258
2259                         err = read_block_for_search(trans, root, p,
2260                                                     &b, level, slot, key);
2261                         if (err == -EAGAIN)
2262                                 goto again;
2263                         if (err) {
2264                                 ret = err;
2265                                 goto done;
2266                         }
2267
2268                         if (!p->skip_locking) {
2269                                 level = btrfs_header_level(b);
2270                                 if (level <= write_lock_level) {
2271                                         err = btrfs_try_tree_write_lock(b);
2272                                         if (!err) {
2273                                                 btrfs_set_path_blocking(p);
2274                                                 btrfs_tree_lock(b);
2275                                                 btrfs_clear_path_blocking(p, b,
2276                                                                   BTRFS_WRITE_LOCK);
2277                                         }
2278                                         p->locks[level] = BTRFS_WRITE_LOCK;
2279                                 } else {
2280                                         err = btrfs_try_tree_read_lock(b);
2281                                         if (!err) {
2282                                                 btrfs_set_path_blocking(p);
2283                                                 btrfs_tree_read_lock(b);
2284                                                 btrfs_clear_path_blocking(p, b,
2285                                                                   BTRFS_READ_LOCK);
2286                                         }
2287                                         p->locks[level] = BTRFS_READ_LOCK;
2288                                 }
2289                                 p->nodes[level] = b;
2290                         }
2291                 } else {
2292                         p->slots[level] = slot;
2293                         if (ins_len > 0 &&
2294                             btrfs_leaf_free_space(root, b) < ins_len) {
2295                                 if (write_lock_level < 1) {
2296                                         write_lock_level = 1;
2297                                         btrfs_release_path(p);
2298                                         goto again;
2299                                 }
2300
2301                                 btrfs_set_path_blocking(p);
2302                                 err = split_leaf(trans, root, key,
2303                                                  p, ins_len, ret == 0);
2304                                 btrfs_clear_path_blocking(p, NULL, 0);
2305
2306                                 BUG_ON(err > 0);
2307                                 if (err) {
2308                                         ret = err;
2309                                         goto done;
2310                                 }
2311                         }
2312                         if (!p->search_for_split)
2313                                 unlock_up(p, level, lowest_unlock,
2314                                           min_write_lock_level, &write_lock_level);
2315                         goto done;
2316                 }
2317         }
2318         ret = 1;
2319 done:
2320         /*
2321          * we don't really know what they plan on doing with the path
2322          * from here on, so for now just mark it as blocking
2323          */
2324         if (!p->leave_spinning)
2325                 btrfs_set_path_blocking(p);
2326         if (ret < 0)
2327                 btrfs_release_path(p);
2328         return ret;
2329 }
2330
2331 /*
2332  * adjust the pointers going up the tree, starting at level
2333  * making sure the right key of each node is points to 'key'.
2334  * This is used after shifting pointers to the left, so it stops
2335  * fixing up pointers when a given leaf/node is not in slot 0 of the
2336  * higher levels
2337  *
2338  */
2339 static void fixup_low_keys(struct btrfs_trans_handle *trans,
2340                            struct btrfs_root *root, struct btrfs_path *path,
2341                            struct btrfs_disk_key *key, int level)
2342 {
2343         int i;
2344         struct extent_buffer *t;
2345
2346         for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2347                 int tslot = path->slots[i];
2348                 if (!path->nodes[i])
2349                         break;
2350                 t = path->nodes[i];
2351                 btrfs_set_node_key(t, key, tslot);
2352                 btrfs_mark_buffer_dirty(path->nodes[i]);
2353                 if (tslot != 0)
2354                         break;
2355         }
2356 }
2357
2358 /*
2359  * update item key.
2360  *
2361  * This function isn't completely safe. It's the caller's responsibility
2362  * that the new key won't break the order
2363  */
2364 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2365                              struct btrfs_root *root, struct btrfs_path *path,
2366                              struct btrfs_key *new_key)
2367 {
2368         struct btrfs_disk_key disk_key;
2369         struct extent_buffer *eb;
2370         int slot;
2371
2372         eb = path->nodes[0];
2373         slot = path->slots[0];
2374         if (slot > 0) {
2375                 btrfs_item_key(eb, &disk_key, slot - 1);
2376                 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
2377         }
2378         if (slot < btrfs_header_nritems(eb) - 1) {
2379                 btrfs_item_key(eb, &disk_key, slot + 1);
2380                 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
2381         }
2382
2383         btrfs_cpu_key_to_disk(&disk_key, new_key);
2384         btrfs_set_item_key(eb, &disk_key, slot);
2385         btrfs_mark_buffer_dirty(eb);
2386         if (slot == 0)
2387                 fixup_low_keys(trans, root, path, &disk_key, 1);
2388 }
2389
2390 /*
2391  * try to push data from one node into the next node left in the
2392  * tree.
2393  *
2394  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2395  * error, and > 0 if there was no room in the left hand block.
2396  */
2397 static int push_node_left(struct btrfs_trans_handle *trans,
2398                           struct btrfs_root *root, struct extent_buffer *dst,
2399                           struct extent_buffer *src, int empty)
2400 {
2401         int push_items = 0;
2402         int src_nritems;
2403         int dst_nritems;
2404         int ret = 0;
2405
2406         src_nritems = btrfs_header_nritems(src);
2407         dst_nritems = btrfs_header_nritems(dst);
2408         push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2409         WARN_ON(btrfs_header_generation(src) != trans->transid);
2410         WARN_ON(btrfs_header_generation(dst) != trans->transid);
2411
2412         if (!empty && src_nritems <= 8)
2413                 return 1;
2414
2415         if (push_items <= 0)
2416                 return 1;
2417
2418         if (empty) {
2419                 push_items = min(src_nritems, push_items);
2420                 if (push_items < src_nritems) {
2421                         /* leave at least 8 pointers in the node if
2422                          * we aren't going to empty it
2423                          */
2424                         if (src_nritems - push_items < 8) {
2425                                 if (push_items <= 8)
2426                                         return 1;
2427                                 push_items -= 8;
2428                         }
2429                 }
2430         } else
2431                 push_items = min(src_nritems - 8, push_items);
2432
2433         copy_extent_buffer(dst, src,
2434                            btrfs_node_key_ptr_offset(dst_nritems),
2435                            btrfs_node_key_ptr_offset(0),
2436                            push_items * sizeof(struct btrfs_key_ptr));
2437
2438         if (push_items < src_nritems) {
2439                 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2440                                       btrfs_node_key_ptr_offset(push_items),
2441                                       (src_nritems - push_items) *
2442                                       sizeof(struct btrfs_key_ptr));
2443         }
2444         btrfs_set_header_nritems(src, src_nritems - push_items);
2445         btrfs_set_header_nritems(dst, dst_nritems + push_items);
2446         btrfs_mark_buffer_dirty(src);
2447         btrfs_mark_buffer_dirty(dst);
2448
2449         return ret;
2450 }
2451
2452 /*
2453  * try to push data from one node into the next node right in the
2454  * tree.
2455  *
2456  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2457  * error, and > 0 if there was no room in the right hand block.
2458  *
2459  * this will  only push up to 1/2 the contents of the left node over
2460  */
2461 static int balance_node_right(struct btrfs_trans_handle *trans,
2462                               struct btrfs_root *root,
2463                               struct extent_buffer *dst,
2464                               struct extent_buffer *src)
2465 {
2466         int push_items = 0;
2467         int max_push;
2468         int src_nritems;
2469         int dst_nritems;
2470         int ret = 0;
2471
2472         WARN_ON(btrfs_header_generation(src) != trans->transid);
2473         WARN_ON(btrfs_header_generation(dst) != trans->transid);
2474
2475         src_nritems = btrfs_header_nritems(src);
2476         dst_nritems = btrfs_header_nritems(dst);
2477         push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2478         if (push_items <= 0)
2479                 return 1;
2480
2481         if (src_nritems < 4)
2482                 return 1;
2483
2484         max_push = src_nritems / 2 + 1;
2485         /* don't try to empty the node */
2486         if (max_push >= src_nritems)
2487                 return 1;
2488
2489         if (max_push < push_items)
2490                 push_items = max_push;
2491
2492         memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2493                                       btrfs_node_key_ptr_offset(0),
2494                                       (dst_nritems) *
2495                                       sizeof(struct btrfs_key_ptr));
2496
2497         copy_extent_buffer(dst, src,
2498                            btrfs_node_key_ptr_offset(0),
2499                            btrfs_node_key_ptr_offset(src_nritems - push_items),
2500                            push_items * sizeof(struct btrfs_key_ptr));
2501
2502         btrfs_set_header_nritems(src, src_nritems - push_items);
2503         btrfs_set_header_nritems(dst, dst_nritems + push_items);
2504
2505         btrfs_mark_buffer_dirty(src);
2506         btrfs_mark_buffer_dirty(dst);
2507
2508         return ret;
2509 }
2510
2511 /*
2512  * helper function to insert a new root level in the tree.
2513  * A new node is allocated, and a single item is inserted to
2514  * point to the existing root
2515  *
2516  * returns zero on success or < 0 on failure.
2517  */
2518 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2519                            struct btrfs_root *root,
2520                            struct btrfs_path *path, int level)
2521 {
2522         u64 lower_gen;
2523         struct extent_buffer *lower;
2524         struct extent_buffer *c;
2525         struct extent_buffer *old;
2526         struct btrfs_disk_key lower_key;
2527
2528         BUG_ON(path->nodes[level]);
2529         BUG_ON(path->nodes[level-1] != root->node);
2530
2531         lower = path->nodes[level-1];
2532         if (level == 1)
2533                 btrfs_item_key(lower, &lower_key, 0);
2534         else
2535                 btrfs_node_key(lower, &lower_key, 0);
2536
2537         c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2538                                    root->root_key.objectid, &lower_key,
2539                                    level, root->node->start, 0);
2540         if (IS_ERR(c))
2541                 return PTR_ERR(c);
2542
2543         root_add_used(root, root->nodesize);
2544
2545         memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
2546         btrfs_set_header_nritems(c, 1);
2547         btrfs_set_header_level(c, level);
2548         btrfs_set_header_bytenr(c, c->start);
2549         btrfs_set_header_generation(c, trans->transid);
2550         btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
2551         btrfs_set_header_owner(c, root->root_key.objectid);
2552
2553         write_extent_buffer(c, root->fs_info->fsid,
2554                             (unsigned long)btrfs_header_fsid(c),
2555                             BTRFS_FSID_SIZE);
2556
2557         write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2558                             (unsigned long)btrfs_header_chunk_tree_uuid(c),
2559                             BTRFS_UUID_SIZE);
2560
2561         btrfs_set_node_key(c, &lower_key, 0);
2562         btrfs_set_node_blockptr(c, 0, lower->start);
2563         lower_gen = btrfs_header_generation(lower);
2564         WARN_ON(lower_gen != trans->transid);
2565
2566         btrfs_set_node_ptr_generation(c, 0, lower_gen);
2567
2568         btrfs_mark_buffer_dirty(c);
2569
2570         old = root->node;
2571         rcu_assign_pointer(root->node, c);
2572
2573         /* the super has an extra ref to root->node */
2574         free_extent_buffer(old);
2575
2576         add_root_to_dirty_list(root);
2577         extent_buffer_get(c);
2578         path->nodes[level] = c;
2579         path->locks[level] = BTRFS_WRITE_LOCK;
2580         path->slots[level] = 0;
2581         return 0;
2582 }
2583
2584 /*
2585  * worker function to insert a single pointer in a node.
2586  * the node should have enough room for the pointer already
2587  *
2588  * slot and level indicate where you want the key to go, and
2589  * blocknr is the block the key points to.
2590  */
2591 static void insert_ptr(struct btrfs_trans_handle *trans,
2592                        struct btrfs_root *root, struct btrfs_path *path,
2593                        struct btrfs_disk_key *key, u64 bytenr,
2594                        int slot, int level)
2595 {
2596         struct extent_buffer *lower;
2597         int nritems;
2598
2599         BUG_ON(!path->nodes[level]);
2600         btrfs_assert_tree_locked(path->nodes[level]);
2601         lower = path->nodes[level];
2602         nritems = btrfs_header_nritems(lower);
2603         BUG_ON(slot > nritems);
2604         BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
2605         if (slot != nritems) {
2606                 memmove_extent_buffer(lower,
2607                               btrfs_node_key_ptr_offset(slot + 1),
2608                               btrfs_node_key_ptr_offset(slot),
2609                               (nritems - slot) * sizeof(struct btrfs_key_ptr));
2610         }
2611         btrfs_set_node_key(lower, key, slot);
2612         btrfs_set_node_blockptr(lower, slot, bytenr);
2613         WARN_ON(trans->transid == 0);
2614         btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2615         btrfs_set_header_nritems(lower, nritems + 1);
2616         btrfs_mark_buffer_dirty(lower);
2617 }
2618
2619 /*
2620  * split the node at the specified level in path in two.
2621  * The path is corrected to point to the appropriate node after the split
2622  *
2623  * Before splitting this tries to make some room in the node by pushing
2624  * left and right, if either one works, it returns right away.
2625  *
2626  * returns 0 on success and < 0 on failure
2627  */
2628 static noinline int split_node(struct btrfs_trans_handle *trans,
2629                                struct btrfs_root *root,
2630                                struct btrfs_path *path, int level)
2631 {
2632         struct extent_buffer *c;
2633         struct extent_buffer *split;
2634         struct btrfs_disk_key disk_key;
2635         int mid;
2636         int ret;
2637         u32 c_nritems;
2638
2639         c = path->nodes[level];
2640         WARN_ON(btrfs_header_generation(c) != trans->transid);
2641         if (c == root->node) {
2642                 /* trying to split the root, lets make a new one */
2643                 ret = insert_new_root(trans, root, path, level + 1);
2644                 if (ret)
2645                         return ret;
2646         } else {
2647                 ret = push_nodes_for_insert(trans, root, path, level);
2648                 c = path->nodes[level];
2649                 if (!ret && btrfs_header_nritems(c) <
2650                     BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2651                         return 0;
2652                 if (ret < 0)
2653                         return ret;
2654         }
2655
2656         c_nritems = btrfs_header_nritems(c);
2657         mid = (c_nritems + 1) / 2;
2658         btrfs_node_key(c, &disk_key, mid);
2659
2660         split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2661                                         root->root_key.objectid,
2662                                         &disk_key, level, c->start, 0);
2663         if (IS_ERR(split))
2664                 return PTR_ERR(split);
2665
2666         root_add_used(root, root->nodesize);
2667
2668         memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
2669         btrfs_set_header_level(split, btrfs_header_level(c));
2670         btrfs_set_header_bytenr(split, split->start);
2671         btrfs_set_header_generation(split, trans->transid);
2672         btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
2673         btrfs_set_header_owner(split, root->root_key.objectid);
2674         write_extent_buffer(split, root->fs_info->fsid,
2675                             (unsigned long)btrfs_header_fsid(split),
2676                             BTRFS_FSID_SIZE);
2677         write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2678                             (unsigned long)btrfs_header_chunk_tree_uuid(split),
2679                             BTRFS_UUID_SIZE);
2680
2681         copy_extent_buffer(split, c,
2682                            btrfs_node_key_ptr_offset(0),
2683                            btrfs_node_key_ptr_offset(mid),
2684                            (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2685         btrfs_set_header_nritems(split, c_nritems - mid);
2686         btrfs_set_header_nritems(c, mid);
2687         ret = 0;
2688
2689         btrfs_mark_buffer_dirty(c);
2690         btrfs_mark_buffer_dirty(split);
2691
2692         insert_ptr(trans, root, path, &disk_key, split->start,
2693                    path->slots[level + 1] + 1, level + 1);
2694
2695         if (path->slots[level] >= mid) {
2696                 path->slots[level] -= mid;
2697                 btrfs_tree_unlock(c);
2698                 free_extent_buffer(c);
2699                 path->nodes[level] = split;
2700                 path->slots[level + 1] += 1;
2701         } else {
2702                 btrfs_tree_unlock(split);
2703                 free_extent_buffer(split);
2704         }
2705         return ret;
2706 }
2707
2708 /*
2709  * how many bytes are required to store the items in a leaf.  start
2710  * and nr indicate which items in the leaf to check.  This totals up the
2711  * space used both by the item structs and the item data
2712  */
2713 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2714 {
2715         int data_len;
2716         int nritems = btrfs_header_nritems(l);
2717         int end = min(nritems, start + nr) - 1;
2718
2719         if (!nr)
2720                 return 0;
2721         data_len = btrfs_item_end_nr(l, start);
2722         data_len = data_len - btrfs_item_offset_nr(l, end);
2723         data_len += sizeof(struct btrfs_item) * nr;
2724         WARN_ON(data_len < 0);
2725         return data_len;
2726 }
2727
2728 /*
2729  * The space between the end of the leaf items and
2730  * the start of the leaf data.  IOW, how much room
2731  * the leaf has left for both items and data
2732  */
2733 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2734                                    struct extent_buffer *leaf)
2735 {
2736         int nritems = btrfs_header_nritems(leaf);
2737         int ret;
2738         ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2739         if (ret < 0) {
2740                 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2741                        "used %d nritems %d\n",
2742                        ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2743                        leaf_space_used(leaf, 0, nritems), nritems);
2744         }
2745         return ret;
2746 }
2747
2748 /*
2749  * min slot controls the lowest index we're willing to push to the
2750  * right.  We'll push up to and including min_slot, but no lower
2751  */
2752 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2753                                       struct btrfs_root *root,
2754                                       struct btrfs_path *path,
2755                                       int data_size, int empty,
2756                                       struct extent_buffer *right,
2757                                       int free_space, u32 left_nritems,
2758                                       u32 min_slot)
2759 {
2760         struct extent_buffer *left = path->nodes[0];
2761         struct extent_buffer *upper = path->nodes[1];
2762         struct btrfs_map_token token;
2763         struct btrfs_disk_key disk_key;
2764         int slot;
2765         u32 i;
2766         int push_space = 0;
2767         int push_items = 0;
2768         struct btrfs_item *item;
2769         u32 nr;
2770         u32 right_nritems;
2771         u32 data_end;
2772         u32 this_item_size;
2773
2774         btrfs_init_map_token(&token);
2775
2776         if (empty)
2777                 nr = 0;
2778         else
2779                 nr = max_t(u32, 1, min_slot);
2780
2781         if (path->slots[0] >= left_nritems)
2782                 push_space += data_size;
2783
2784         slot = path->slots[1];
2785         i = left_nritems - 1;
2786         while (i >= nr) {
2787                 item = btrfs_item_nr(left, i);
2788
2789                 if (!empty && push_items > 0) {
2790                         if (path->slots[0] > i)
2791                                 break;
2792                         if (path->slots[0] == i) {
2793                                 int space = btrfs_leaf_free_space(root, left);
2794                                 if (space + push_space * 2 > free_space)
2795                                         break;
2796                         }
2797                 }
2798
2799                 if (path->slots[0] == i)
2800                         push_space += data_size;
2801
2802                 this_item_size = btrfs_item_size(left, item);
2803                 if (this_item_size + sizeof(*item) + push_space > free_space)
2804                         break;
2805
2806                 push_items++;
2807                 push_space += this_item_size + sizeof(*item);
2808                 if (i == 0)
2809                         break;
2810                 i--;
2811         }
2812
2813         if (push_items == 0)
2814                 goto out_unlock;
2815
2816         if (!empty && push_items == left_nritems)
2817                 WARN_ON(1);
2818
2819         /* push left to right */
2820         right_nritems = btrfs_header_nritems(right);
2821
2822         push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2823         push_space -= leaf_data_end(root, left);
2824
2825         /* make room in the right data area */
2826         data_end = leaf_data_end(root, right);
2827         memmove_extent_buffer(right,
2828                               btrfs_leaf_data(right) + data_end - push_space,
2829                               btrfs_leaf_data(right) + data_end,
2830                               BTRFS_LEAF_DATA_SIZE(root) - data_end);
2831
2832         /* copy from the left data area */
2833         copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2834                      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2835                      btrfs_leaf_data(left) + leaf_data_end(root, left),
2836                      push_space);
2837
2838         memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2839                               btrfs_item_nr_offset(0),
2840                               right_nritems * sizeof(struct btrfs_item));
2841
2842         /* copy the items from left to right */
2843         copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2844                    btrfs_item_nr_offset(left_nritems - push_items),
2845                    push_items * sizeof(struct btrfs_item));
2846
2847         /* update the item pointers */
2848         right_nritems += push_items;
2849         btrfs_set_header_nritems(right, right_nritems);
2850         push_space = BTRFS_LEAF_DATA_SIZE(root);
2851         for (i = 0; i < right_nritems; i++) {
2852                 item = btrfs_item_nr(right, i);
2853                 push_space -= btrfs_token_item_size(right, item, &token);
2854                 btrfs_set_token_item_offset(right, item, push_space, &token);
2855         }
2856
2857         left_nritems -= push_items;
2858         btrfs_set_header_nritems(left, left_nritems);
2859
2860         if (left_nritems)
2861                 btrfs_mark_buffer_dirty(left);
2862         else
2863                 clean_tree_block(trans, root, left);
2864
2865         btrfs_mark_buffer_dirty(right);
2866
2867         btrfs_item_key(right, &disk_key, 0);
2868         btrfs_set_node_key(upper, &disk_key, slot + 1);
2869         btrfs_mark_buffer_dirty(upper);
2870
2871         /* then fixup the leaf pointer in the path */
2872         if (path->slots[0] >= left_nritems) {
2873                 path->slots[0] -= left_nritems;
2874                 if (btrfs_header_nritems(path->nodes[0]) == 0)
2875                         clean_tree_block(trans, root, path->nodes[0]);
2876                 btrfs_tree_unlock(path->nodes[0]);
2877                 free_extent_buffer(path->nodes[0]);
2878                 path->nodes[0] = right;
2879                 path->slots[1] += 1;
2880         } else {
2881                 btrfs_tree_unlock(right);
2882                 free_extent_buffer(right);
2883         }
2884         return 0;
2885
2886 out_unlock:
2887         btrfs_tree_unlock(right);
2888         free_extent_buffer(right);
2889         return 1;
2890 }
2891
2892 /*
2893  * push some data in the path leaf to the right, trying to free up at
2894  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
2895  *
2896  * returns 1 if the push failed because the other node didn't have enough
2897  * room, 0 if everything worked out and < 0 if there were major errors.
2898  *
2899  * this will push starting from min_slot to the end of the leaf.  It won't
2900  * push any slot lower than min_slot
2901  */
2902 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2903                            *root, struct btrfs_path *path,
2904                            int min_data_size, int data_size,
2905                            int empty, u32 min_slot)
2906 {
2907         struct extent_buffer *left = path->nodes[0];
2908         struct extent_buffer *right;
2909         struct extent_buffer *upper;
2910         int slot;
2911         int free_space;
2912         u32 left_nritems;
2913         int ret;
2914
2915         if (!path->nodes[1])
2916                 return 1;
2917
2918         slot = path->slots[1];
2919         upper = path->nodes[1];
2920         if (slot >= btrfs_header_nritems(upper) - 1)
2921                 return 1;
2922
2923         btrfs_assert_tree_locked(path->nodes[1]);
2924
2925         right = read_node_slot(root, upper, slot + 1);
2926         if (right == NULL)
2927                 return 1;
2928
2929         btrfs_tree_lock(right);
2930         btrfs_set_lock_blocking(right);
2931
2932         free_space = btrfs_leaf_free_space(root, right);
2933         if (free_space < data_size)
2934                 goto out_unlock;
2935
2936         /* cow and double check */
2937         ret = btrfs_cow_block(trans, root, right, upper,
2938                               slot + 1, &right);
2939         if (ret)
2940                 goto out_unlock;
2941
2942         free_space = btrfs_leaf_free_space(root, right);
2943         if (free_space < data_size)
2944                 goto out_unlock;
2945
2946         left_nritems = btrfs_header_nritems(left);
2947         if (left_nritems == 0)
2948                 goto out_unlock;
2949
2950         return __push_leaf_right(trans, root, path, min_data_size, empty,
2951                                 right, free_space, left_nritems, min_slot);
2952 out_unlock:
2953         btrfs_tree_unlock(right);
2954         free_extent_buffer(right);
2955         return 1;
2956 }
2957
2958 /*
2959  * push some data in the path leaf to the left, trying to free up at
2960  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
2961  *
2962  * max_slot can put a limit on how far into the leaf we'll push items.  The
2963  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
2964  * items
2965  */
2966 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2967                                      struct btrfs_root *root,
2968                                      struct btrfs_path *path, int data_size,
2969                                      int empty, struct extent_buffer *left,
2970                                      int free_space, u32 right_nritems,
2971                                      u32 max_slot)
2972 {
2973         struct btrfs_disk_key disk_key;
2974         struct extent_buffer *right = path->nodes[0];
2975         int i;
2976         int push_space = 0;
2977         int push_items = 0;
2978         struct btrfs_item *item;
2979         u32 old_left_nritems;
2980         u32 nr;
2981         int ret = 0;
2982         u32 this_item_size;
2983         u32 old_left_item_size;
2984         struct btrfs_map_token token;
2985
2986         btrfs_init_map_token(&token);
2987
2988         if (empty)
2989                 nr = min(right_nritems, max_slot);
2990         else
2991                 nr = min(right_nritems - 1, max_slot);
2992
2993         for (i = 0; i < nr; i++) {
2994                 item = btrfs_item_nr(right, i);
2995
2996                 if (!empty && push_items > 0) {
2997                         if (path->slots[0] < i)
2998                                 break;
2999                         if (path->slots[0] == i) {
3000                                 int space = btrfs_leaf_free_space(root, right);
3001                                 if (space + push_space * 2 > free_space)
3002                                         break;
3003                         }
3004                 }
3005
3006                 if (path->slots[0] == i)
3007                         push_space += data_size;
3008
3009                 this_item_size = btrfs_item_size(right, item);
3010                 if (this_item_size + sizeof(*item) + push_space > free_space)
3011                         break;
3012
3013                 push_items++;
3014                 push_space += this_item_size + sizeof(*item);
3015         }
3016
3017         if (push_items == 0) {
3018                 ret = 1;
3019                 goto out;
3020         }
3021         if (!empty && push_items == btrfs_header_nritems(right))
3022                 WARN_ON(1);
3023
3024         /* push data from right to left */
3025         copy_extent_buffer(left, right,
3026                            btrfs_item_nr_offset(btrfs_header_nritems(left)),
3027                            btrfs_item_nr_offset(0),
3028                            push_items * sizeof(struct btrfs_item));
3029
3030         push_space = BTRFS_LEAF_DATA_SIZE(root) -
3031                      btrfs_item_offset_nr(right, push_items - 1);
3032
3033         copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3034                      leaf_data_end(root, left) - push_space,
3035                      btrfs_leaf_data(right) +
3036                      btrfs_item_offset_nr(right, push_items - 1),
3037                      push_space);
3038         old_left_nritems = btrfs_header_nritems(left);
3039         BUG_ON(old_left_nritems <= 0);
3040
3041         old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3042         for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3043                 u32 ioff;
3044
3045                 item = btrfs_item_nr(left, i);
3046
3047                 ioff = btrfs_token_item_offset(left, item, &token);
3048                 btrfs_set_token_item_offset(left, item,
3049                       ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3050                       &token);
3051         }
3052         btrfs_set_header_nritems(left, old_left_nritems + push_items);
3053
3054         /* fixup right node */
3055         if (push_items > right_nritems) {
3056                 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3057                        right_nritems);
3058                 WARN_ON(1);
3059         }
3060
3061         if (push_items < right_nritems) {
3062                 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3063                                                   leaf_data_end(root, right);
3064                 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3065                                       BTRFS_LEAF_DATA_SIZE(root) - push_space,
3066                                       btrfs_leaf_data(right) +
3067                                       leaf_data_end(root, right), push_space);
3068
3069                 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3070                               btrfs_item_nr_offset(push_items),
3071                              (btrfs_header_nritems(right) - push_items) *
3072                              sizeof(struct btrfs_item));
3073         }
3074         right_nritems -= push_items;
3075         btrfs_set_header_nritems(right, right_nritems);
3076         push_space = BTRFS_LEAF_DATA_SIZE(root);
3077         for (i = 0; i < right_nritems; i++) {
3078                 item = btrfs_item_nr(right, i);
3079
3080                 push_space = push_space - btrfs_token_item_size(right,
3081                                                                 item, &token);
3082                 btrfs_set_token_item_offset(right, item, push_space, &token);
3083         }
3084
3085         btrfs_mark_buffer_dirty(left);
3086         if (right_nritems)
3087                 btrfs_mark_buffer_dirty(right);
3088         else
3089                 clean_tree_block(trans, root, right);
3090
3091         btrfs_item_key(right, &disk_key, 0);
3092         fixup_low_keys(trans, root, path, &disk_key, 1);
3093
3094         /* then fixup the leaf pointer in the path */
3095         if (path->slots[0] < push_items) {
3096                 path->slots[0] += old_left_nritems;
3097                 btrfs_tree_unlock(path->nodes[0]);
3098                 free_extent_buffer(path->nodes[0]);
3099                 path->nodes[0] = left;
3100                 path->slots[1] -= 1;
3101         } else {
3102                 btrfs_tree_unlock(left);
3103                 free_extent_buffer(left);
3104                 path->slots[0] -= push_items;
3105         }
3106         BUG_ON(path->slots[0] < 0);
3107         return ret;
3108 out:
3109         btrfs_tree_unlock(left);
3110         free_extent_buffer(left);
3111         return ret;
3112 }
3113
3114 /*
3115  * push some data in the path leaf to the left, trying to free up at
3116  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
3117  *
3118  * max_slot can put a limit on how far into the leaf we'll push items.  The
3119  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
3120  * items
3121  */
3122 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3123                           *root, struct btrfs_path *path, int min_data_size,
3124                           int data_size, int empty, u32 max_slot)
3125 {
3126         struct extent_buffer *right = path->nodes[0];
3127         struct extent_buffer *left;
3128         int slot;
3129         int free_space;
3130         u32 right_nritems;
3131         int ret = 0;
3132
3133         slot = path->slots[1];
3134         if (slot == 0)
3135                 return 1;
3136         if (!path->nodes[1])
3137                 return 1;
3138
3139         right_nritems = btrfs_header_nritems(right);
3140         if (right_nritems == 0)
3141                 return 1;
3142
3143         btrfs_assert_tree_locked(path->nodes[1]);
3144
3145         left = read_node_slot(root, path->nodes[1], slot - 1);
3146         if (left == NULL)
3147                 return 1;
3148
3149         btrfs_tree_lock(left);
3150         btrfs_set_lock_blocking(left);
3151
3152         free_space = btrfs_leaf_free_space(root, left);
3153         if (free_space < data_size) {
3154                 ret = 1;
3155                 goto out;
3156         }
3157
3158         /* cow and double check */
3159         ret = btrfs_cow_block(trans, root, left,
3160                               path->nodes[1], slot - 1, &left);
3161         if (ret) {
3162                 /* we hit -ENOSPC, but it isn't fatal here */
3163                 if (ret == -ENOSPC)
3164                         ret = 1;
3165                 goto out;
3166         }
3167
3168         free_space = btrfs_leaf_free_space(root, left);
3169         if (free_space < data_size) {
3170                 ret = 1;
3171                 goto out;
3172         }
3173
3174         return __push_leaf_left(trans, root, path, min_data_size,
3175                                empty, left, free_space, right_nritems,
3176                                max_slot);
3177 out:
3178         btrfs_tree_unlock(left);
3179         free_extent_buffer(left);
3180         return ret;
3181 }
3182
3183 /*
3184  * split the path's leaf in two, making sure there is at least data_size
3185  * available for the resulting leaf level of the path.
3186  */
3187 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3188                                     struct btrfs_root *root,
3189                                     struct btrfs_path *path,
3190                                     struct extent_buffer *l,
3191                                     struct extent_buffer *right,
3192                                     int slot, int mid, int nritems)
3193 {
3194         int data_copy_size;
3195         int rt_data_off;
3196         int i;
3197         struct btrfs_disk_key disk_key;
3198         struct btrfs_map_token token;
3199
3200         btrfs_init_map_token(&token);
3201
3202         nritems = nritems - mid;
3203         btrfs_set_header_nritems(right, nritems);
3204         data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3205
3206         copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3207                            btrfs_item_nr_offset(mid),
3208                            nritems * sizeof(struct btrfs_item));
3209
3210         copy_extent_buffer(right, l,
3211                      btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3212                      data_copy_size, btrfs_leaf_data(l) +
3213                      leaf_data_end(root, l), data_copy_size);
3214
3215         rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3216                       btrfs_item_end_nr(l, mid);
3217
3218         for (i = 0; i < nritems; i++) {
3219                 struct btrfs_item *item = btrfs_item_nr(right, i);
3220                 u32 ioff;
3221
3222                 ioff = btrfs_token_item_offset(right, item, &token);
3223                 btrfs_set_token_item_offset(right, item,
3224                                             ioff + rt_data_off, &token);
3225         }
3226
3227         btrfs_set_header_nritems(l, mid);
3228         btrfs_item_key(right, &disk_key, 0);
3229         insert_ptr(trans, root, path, &disk_key, right->start,
3230                    path->slots[1] + 1, 1);
3231
3232         btrfs_mark_buffer_dirty(right);
3233         btrfs_mark_buffer_dirty(l);
3234         BUG_ON(path->slots[0] != slot);
3235
3236         if (mid <= slot) {
3237                 btrfs_tree_unlock(path->nodes[0]);
3238                 free_extent_buffer(path->nodes[0]);
3239                 path->nodes[0] = right;
3240                 path->slots[0] -= mid;
3241                 path->slots[1] += 1;
3242         } else {
3243                 btrfs_tree_unlock(right);
3244                 free_extent_buffer(right);
3245         }
3246
3247         BUG_ON(path->slots[0] < 0);
3248 }
3249
3250 /*
3251  * double splits happen when we need to insert a big item in the middle
3252  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
3253  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3254  *          A                 B                 C
3255  *
3256  * We avoid this by trying to push the items on either side of our target
3257  * into the adjacent leaves.  If all goes well we can avoid the double split
3258  * completely.
3259  */
3260 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3261                                           struct btrfs_root *root,
3262                                           struct btrfs_path *path,
3263                                           int data_size)
3264 {
3265         int ret;
3266         int progress = 0;
3267         int slot;
3268         u32 nritems;
3269
3270         slot = path->slots[0];
3271
3272         /*
3273          * try to push all the items after our slot into the
3274          * right leaf
3275          */
3276         ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3277         if (ret < 0)
3278                 return ret;
3279
3280         if (ret == 0)
3281                 progress++;
3282
3283         nritems = btrfs_header_nritems(path->nodes[0]);
3284         /*
3285          * our goal is to get our slot at the start or end of a leaf.  If
3286          * we've done so we're done
3287          */
3288         if (path->slots[0] == 0 || path->slots[0] == nritems)
3289                 return 0;
3290
3291         if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3292                 return 0;
3293
3294         /* try to push all the items before our slot into the next leaf */
3295         slot = path->slots[0];
3296         ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3297         if (ret < 0)
3298                 return ret;
3299
3300         if (ret == 0)
3301                 progress++;
3302
3303         if (progress)
3304                 return 0;
3305         return 1;
3306 }
3307
3308 /*
3309  * split the path's leaf in two, making sure there is at least data_size
3310  * available for the resulting leaf level of the path.
3311  *
3312  * returns 0 if all went well and < 0 on failure.
3313  */
3314 static noinline int split_leaf(struct btrfs_trans_handle *trans,
3315                                struct btrfs_root *root,
3316                                struct btrfs_key *ins_key,
3317                                struct btrfs_path *path, int data_size,
3318                                int extend)
3319 {
3320         struct btrfs_disk_key disk_key;
3321         struct extent_buffer *l;
3322         u32 nritems;
3323         int mid;
3324         int slot;
3325         struct extent_buffer *right;
3326         int ret = 0;
3327         int wret;
3328         int split;
3329         int num_doubles = 0;
3330         int tried_avoid_double = 0;
3331
3332         l = path->nodes[0];
3333         slot = path->slots[0];
3334         if (extend && data_size + btrfs_item_size_nr(l, slot) +
3335             sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3336                 return -EOVERFLOW;
3337
3338         /* first try to make some room by pushing left and right */
3339         if (data_size) {
3340                 wret = push_leaf_right(trans, root, path, data_size,
3341                                        data_size, 0, 0);
3342                 if (wret < 0)
3343                         return wret;
3344                 if (wret) {
3345                         wret = push_leaf_left(trans, root, path, data_size,
3346                                               data_size, 0, (u32)-1);
3347                         if (wret < 0)
3348                                 return wret;
3349                 }
3350                 l = path->nodes[0];
3351
3352                 /* did the pushes work? */
3353                 if (btrfs_leaf_free_space(root, l) >= data_size)
3354                         return 0;
3355         }
3356
3357         if (!path->nodes[1]) {
3358                 ret = insert_new_root(trans, root, path, 1);
3359                 if (ret)
3360                         return ret;
3361         }
3362 again:
3363         split = 1;
3364         l = path->nodes[0];
3365         slot = path->slots[0];
3366         nritems = btrfs_header_nritems(l);
3367         mid = (nritems + 1) / 2;
3368
3369         if (mid <= slot) {
3370                 if (nritems == 1 ||
3371                     leaf_space_used(l, mid, nritems - mid) + data_size >
3372                         BTRFS_LEAF_DATA_SIZE(root)) {
3373                         if (slot >= nritems) {
3374                                 split = 0;
3375                         } else {
3376                                 mid = slot;
3377                                 if (mid != nritems &&
3378                                     leaf_space_used(l, mid, nritems - mid) +
3379                                     data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3380                                         if (data_size && !tried_avoid_double)
3381                                                 goto push_for_double;
3382                                         split = 2;
3383                                 }
3384                         }
3385                 }
3386         } else {
3387                 if (leaf_space_used(l, 0, mid) + data_size >
3388                         BTRFS_LEAF_DATA_SIZE(root)) {
3389                         if (!extend && data_size && slot == 0) {
3390                                 split = 0;
3391                         } else if ((extend || !data_size) && slot == 0) {
3392                                 mid = 1;
3393                         } else {
3394                                 mid = slot;
3395                                 if (mid != nritems &&
3396                                     leaf_space_used(l, mid, nritems - mid) +
3397                                     data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3398                                         if (data_size && !tried_avoid_double)
3399                                                 goto push_for_double;
3400                                         split = 2 ;
3401                                 }
3402                         }
3403                 }
3404         }
3405
3406         if (split == 0)
3407                 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3408         else
3409                 btrfs_item_key(l, &disk_key, mid);
3410
3411         right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
3412                                         root->root_key.objectid,
3413                                         &disk_key, 0, l->start, 0);
3414         if (IS_ERR(right))
3415                 return PTR_ERR(right);
3416
3417         root_add_used(root, root->leafsize);
3418
3419         memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3420         btrfs_set_header_bytenr(right, right->start);
3421         btrfs_set_header_generation(right, trans->transid);
3422         btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
3423         btrfs_set_header_owner(right, root->root_key.objectid);
3424         btrfs_set_header_level(right, 0);
3425         write_extent_buffer(right, root->fs_info->fsid,
3426                             (unsigned long)btrfs_header_fsid(right),
3427                             BTRFS_FSID_SIZE);
3428
3429         write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3430                             (unsigned long)btrfs_header_chunk_tree_uuid(right),
3431                             BTRFS_UUID_SIZE);
3432
3433         if (split == 0) {
3434                 if (mid <= slot) {
3435                         btrfs_set_header_nritems(right, 0);
3436                         insert_ptr(trans, root, path, &disk_key, right->start,
3437                                    path->slots[1] + 1, 1);
3438                         btrfs_tree_unlock(path->nodes[0]);
3439                         free_extent_buffer(path->nodes[0]);
3440                         path->nodes[0] = right;
3441                         path->slots[0] = 0;
3442                         path->slots[1] += 1;
3443                 } else {
3444                         btrfs_set_header_nritems(right, 0);
3445                         insert_ptr(trans, root, path, &disk_key, right->start,
3446                                           path->slots[1], 1);
3447                         btrfs_tree_unlock(path->nodes[0]);
3448                         free_extent_buffer(path->nodes[0]);
3449                         path->nodes[0] = right;
3450                         path->slots[0] = 0;
3451                         if (path->slots[1] == 0)
3452                                 fixup_low_keys(trans, root, path,
3453                                                &disk_key, 1);
3454                 }
3455                 btrfs_mark_buffer_dirty(right);
3456                 return ret;
3457         }
3458
3459         copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3460
3461         if (split == 2) {
3462                 BUG_ON(num_doubles != 0);
3463                 num_doubles++;
3464                 goto again;
3465         }
3466
3467         return 0;
3468
3469 push_for_double:
3470         push_for_double_split(trans, root, path, data_size);
3471         tried_avoid_double = 1;
3472         if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3473                 return 0;
3474         goto again;
3475 }
3476
3477 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3478                                          struct btrfs_root *root,
3479                                          struct btrfs_path *path, int ins_len)
3480 {
3481         struct btrfs_key key;
3482         struct extent_buffer *leaf;
3483         struct btrfs_file_extent_item *fi;
3484         u64 extent_len = 0;
3485         u32 item_size;
3486         int ret;
3487
3488         leaf = path->nodes[0];
3489         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3490
3491         BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3492                key.type != BTRFS_EXTENT_CSUM_KEY);
3493
3494         if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3495                 return 0;
3496
3497         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3498         if (key.type == BTRFS_EXTENT_DATA_KEY) {
3499                 fi = btrfs_item_ptr(leaf, path->slots[0],
3500                                     struct btrfs_file_extent_item);
3501                 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3502         }
3503         btrfs_release_path(path);
3504
3505         path->keep_locks = 1;
3506         path->search_for_split = 1;
3507         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3508         path->search_for_split = 0;
3509         if (ret < 0)
3510                 goto err;
3511
3512         ret = -EAGAIN;
3513         leaf = path->nodes[0];
3514         /* if our item isn't there or got smaller, return now */
3515         if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3516                 goto err;
3517
3518         /* the leaf has  changed, it now has room.  return now */
3519         if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3520                 goto err;
3521
3522         if (key.type == BTRFS_EXTENT_DATA_KEY) {
3523                 fi = btrfs_item_ptr(leaf, path->slots[0],
3524                                     struct btrfs_file_extent_item);
3525                 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3526                         goto err;
3527         }
3528
3529         btrfs_set_path_blocking(path);
3530         ret = split_leaf(trans, root, &key, path, ins_len, 1);
3531         if (ret)
3532                 goto err;
3533
3534         path->keep_locks = 0;
3535         btrfs_unlock_up_safe(path, 1);
3536         return 0;
3537 err:
3538         path->keep_locks = 0;
3539         return ret;
3540 }
3541
3542 static noinline int split_item(struct btrfs_trans_handle *trans,
3543                                struct btrfs_root *root,
3544                                struct btrfs_path *path,
3545                                struct btrfs_key *new_key,
3546                                unsigned long split_offset)
3547 {
3548         struct extent_buffer *leaf;
3549         struct btrfs_item *item;
3550         struct btrfs_item *new_item;
3551         int slot;
3552         char *buf;
3553         u32 nritems;
3554         u32 item_size;
3555         u32 orig_offset;
3556         struct btrfs_disk_key disk_key;
3557
3558         leaf = path->nodes[0];
3559         BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3560
3561         btrfs_set_path_blocking(path);
3562
3563         item = btrfs_item_nr(leaf, path->slots[0]);
3564         orig_offset = btrfs_item_offset(leaf, item);
3565         item_size = btrfs_item_size(leaf, item);
3566
3567         buf = kmalloc(item_size, GFP_NOFS);
3568         if (!buf)
3569                 return -ENOMEM;
3570
3571         read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3572                             path->slots[0]), item_size);
3573
3574         slot = path->slots[0] + 1;
3575         nritems = btrfs_header_nritems(leaf);
3576         if (slot != nritems) {
3577                 /* shift the items */
3578                 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3579                                 btrfs_item_nr_offset(slot),
3580                                 (nritems - slot) * sizeof(struct btrfs_item));
3581         }
3582
3583         btrfs_cpu_key_to_disk(&disk_key, new_key);
3584         btrfs_set_item_key(leaf, &disk_key, slot);
3585
3586         new_item = btrfs_item_nr(leaf, slot);
3587
3588         btrfs_set_item_offset(leaf, new_item, orig_offset);
3589         btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3590
3591         btrfs_set_item_offset(leaf, item,
3592                               orig_offset + item_size - split_offset);
3593         btrfs_set_item_size(leaf, item, split_offset);
3594
3595         btrfs_set_header_nritems(leaf, nritems + 1);
3596
3597         /* write the data for the start of the original item */
3598         write_extent_buffer(leaf, buf,
3599                             btrfs_item_ptr_offset(leaf, path->slots[0]),
3600                             split_offset);
3601
3602         /* write the data for the new item */
3603         write_extent_buffer(leaf, buf + split_offset,
3604                             btrfs_item_ptr_offset(leaf, slot),
3605                             item_size - split_offset);
3606         btrfs_mark_buffer_dirty(leaf);
3607
3608         BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3609         kfree(buf);
3610         return 0;
3611 }
3612
3613 /*
3614  * This function splits a single item into two items,
3615  * giving 'new_key' to the new item and splitting the
3616  * old one at split_offset (from the start of the item).
3617  *
3618  * The path may be released by this operation.  After
3619  * the split, the path is pointing to the old item.  The
3620  * new item is going to be in the same node as the old one.
3621  *
3622  * Note, the item being split must be smaller enough to live alone on
3623  * a tree block with room for one extra struct btrfs_item
3624  *
3625  * This allows us to split the item in place, keeping a lock on the
3626  * leaf the entire time.
3627  */
3628 int btrfs_split_item(struct btrfs_trans_handle *trans,
3629                      struct btrfs_root *root,
3630                      struct btrfs_path *path,
3631                      struct btrfs_key *new_key,
3632                      unsigned long split_offset)
3633 {
3634         int ret;
3635         ret = setup_leaf_for_split(trans, root, path,
3636                                    sizeof(struct btrfs_item));
3637         if (ret)
3638                 return ret;
3639
3640         ret = split_item(trans, root, path, new_key, split_offset);
3641         return ret;
3642 }
3643
3644 /*
3645  * This function duplicate a item, giving 'new_key' to the new item.
3646  * It guarantees both items live in the same tree leaf and the new item
3647  * is contiguous with the original item.
3648  *
3649  * This allows us to split file extent in place, keeping a lock on the
3650  * leaf the entire time.
3651  */
3652 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3653                          struct btrfs_root *root,
3654                          struct btrfs_path *path,
3655                          struct btrfs_key *new_key)
3656 {
3657         struct extent_buffer *leaf;
3658         int ret;
3659         u32 item_size;
3660
3661         leaf = path->nodes[0];
3662         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3663         ret = setup_leaf_for_split(trans, root, path,
3664                                    item_size + sizeof(struct btrfs_item));
3665         if (ret)
3666                 return ret;
3667
3668         path->slots[0]++;
3669         setup_items_for_insert(trans, root, path, new_key, &item_size,
3670                                item_size, item_size +
3671                                sizeof(struct btrfs_item), 1);
3672         leaf = path->nodes[0];
3673         memcpy_extent_buffer(leaf,
3674                              btrfs_item_ptr_offset(leaf, path->slots[0]),
3675                              btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3676                              item_size);
3677         return 0;
3678 }
3679
3680 /*
3681  * make the item pointed to by the path smaller.  new_size indicates
3682  * how small to make it, and from_end tells us if we just chop bytes
3683  * off the end of the item or if we shift the item to chop bytes off
3684  * the front.
3685  */
3686 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3687                          struct btrfs_root *root,
3688                          struct btrfs_path *path,
3689                          u32 new_size, int from_end)
3690 {
3691         int slot;
3692         struct extent_buffer *leaf;
3693         struct btrfs_item *item;
3694         u32 nritems;
3695         unsigned int data_end;
3696         unsigned int old_data_start;
3697         unsigned int old_size;
3698         unsigned int size_diff;
3699         int i;
3700         struct btrfs_map_token token;
3701
3702         btrfs_init_map_token(&token);
3703
3704         leaf = path->nodes[0];
3705         slot = path->slots[0];
3706
3707         old_size = btrfs_item_size_nr(leaf, slot);
3708         if (old_size == new_size)
3709                 return;
3710
3711         nritems = btrfs_header_nritems(leaf);
3712         data_end = leaf_data_end(root, leaf);
3713
3714         old_data_start = btrfs_item_offset_nr(leaf, slot);
3715
3716         size_diff = old_size - new_size;
3717
3718         BUG_ON(slot < 0);
3719         BUG_ON(slot >= nritems);
3720
3721         /*
3722          * item0..itemN ... dataN.offset..dataN.size .. data0.size
3723          */
3724         /* first correct the data pointers */
3725         for (i = slot; i < nritems; i++) {
3726                 u32 ioff;
3727                 item = btrfs_item_nr(leaf, i);
3728
3729                 ioff = btrfs_token_item_offset(leaf, item, &token);
3730                 btrfs_set_token_item_offset(leaf, item,
3731                                             ioff + size_diff, &token);
3732         }
3733
3734         /* shift the data */
3735         if (from_end) {
3736                 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3737                               data_end + size_diff, btrfs_leaf_data(leaf) +
3738                               data_end, old_data_start + new_size - data_end);
3739         } else {
3740                 struct btrfs_disk_key disk_key;
3741                 u64 offset;
3742
3743                 btrfs_item_key(leaf, &disk_key, slot);
3744
3745                 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3746                         unsigned long ptr;
3747                         struct btrfs_file_extent_item *fi;
3748
3749                         fi = btrfs_item_ptr(leaf, slot,
3750                                             struct btrfs_file_extent_item);
3751                         fi = (struct btrfs_file_extent_item *)(
3752                              (unsigned long)fi - size_diff);
3753
3754                         if (btrfs_file_extent_type(leaf, fi) ==
3755                             BTRFS_FILE_EXTENT_INLINE) {
3756                                 ptr = btrfs_item_ptr_offset(leaf, slot);
3757                                 memmove_extent_buffer(leaf, ptr,
3758                                       (unsigned long)fi,
3759                                       offsetof(struct btrfs_file_extent_item,
3760                                                  disk_bytenr));
3761                         }
3762                 }
3763
3764                 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3765                               data_end + size_diff, btrfs_leaf_data(leaf) +
3766                               data_end, old_data_start - data_end);
3767
3768                 offset = btrfs_disk_key_offset(&disk_key);
3769                 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3770                 btrfs_set_item_key(leaf, &disk_key, slot);
3771                 if (slot == 0)
3772                         fixup_low_keys(trans, root, path, &disk_key, 1);
3773         }
3774
3775         item = btrfs_item_nr(leaf, slot);
3776         btrfs_set_item_size(leaf, item, new_size);
3777         btrfs_mark_buffer_dirty(leaf);
3778
3779         if (btrfs_leaf_free_space(root, leaf) < 0) {
3780                 btrfs_print_leaf(root, leaf);
3781                 BUG();
3782         }
3783 }
3784
3785 /*
3786  * make the item pointed to by the path bigger, data_size is the new size.
3787  */
3788 void btrfs_extend_item(struct btrfs_trans_handle *trans,
3789                        struct btrfs_root *root, struct btrfs_path *path,
3790                        u32 data_size)
3791 {
3792         int slot;
3793         struct extent_buffer *leaf;
3794         struct btrfs_item *item;
3795         u32 nritems;
3796         unsigned int data_end;
3797         unsigned int old_data;
3798         unsigned int old_size;
3799         int i;
3800         struct btrfs_map_token token;
3801
3802         btrfs_init_map_token(&token);
3803
3804         leaf = path->nodes[0];
3805
3806         nritems = btrfs_header_nritems(leaf);
3807         data_end = leaf_data_end(root, leaf);
3808
3809         if (btrfs_leaf_free_space(root, leaf) < data_size) {
3810                 btrfs_print_leaf(root, leaf);
3811                 BUG();
3812         }
3813         slot = path->slots[0];
3814         old_data = btrfs_item_end_nr(leaf, slot);
3815
3816         BUG_ON(slot < 0);
3817         if (slot >= nritems) {
3818                 btrfs_print_leaf(root, leaf);
3819                 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3820                        slot, nritems);
3821                 BUG_ON(1);
3822         }
3823
3824         /*
3825          * item0..itemN ... dataN.offset..dataN.size .. data0.size
3826          */
3827         /* first correct the data pointers */
3828         for (i = slot; i < nritems; i++) {
3829                 u32 ioff;
3830                 item = btrfs_item_nr(leaf, i);
3831
3832                 ioff = btrfs_token_item_offset(leaf, item, &token);
3833                 btrfs_set_token_item_offset(leaf, item,
3834                                             ioff - data_size, &token);
3835         }
3836
3837         /* shift the data */
3838         memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3839                       data_end - data_size, btrfs_leaf_data(leaf) +
3840                       data_end, old_data - data_end);
3841
3842         data_end = old_data;
3843         old_size = btrfs_item_size_nr(leaf, slot);
3844         item = btrfs_item_nr(leaf, slot);
3845         btrfs_set_item_size(leaf, item, old_size + data_size);
3846         btrfs_mark_buffer_dirty(leaf);
3847
3848         if (btrfs_leaf_free_space(root, leaf) < 0) {
3849                 btrfs_print_leaf(root, leaf);
3850                 BUG();
3851         }
3852 }
3853
3854 /*
3855  * Given a key and some data, insert items into the tree.
3856  * This does all the path init required, making room in the tree if needed.
3857  * Returns the number of keys that were inserted.
3858  */
3859 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3860                             struct btrfs_root *root,
3861                             struct btrfs_path *path,
3862                             struct btrfs_key *cpu_key, u32 *data_size,
3863                             int nr)
3864 {
3865         struct extent_buffer *leaf;
3866         struct btrfs_item *item;
3867         int ret = 0;
3868         int slot;
3869         int i;
3870         u32 nritems;
3871         u32 total_data = 0;
3872         u32 total_size = 0;
3873         unsigned int data_end;
3874         struct btrfs_disk_key disk_key;
3875         struct btrfs_key found_key;
3876         struct btrfs_map_token token;
3877
3878         btrfs_init_map_token(&token);
3879
3880         for (i = 0; i < nr; i++) {
3881                 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3882                     BTRFS_LEAF_DATA_SIZE(root)) {
3883                         break;
3884                         nr = i;
3885                 }
3886                 total_data += data_size[i];
3887                 total_size += data_size[i] + sizeof(struct btrfs_item);
3888         }
3889         BUG_ON(nr == 0);
3890
3891         ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3892         if (ret == 0)
3893                 return -EEXIST;
3894         if (ret < 0)
3895                 goto out;
3896
3897         leaf = path->nodes[0];
3898
3899         nritems = btrfs_header_nritems(leaf);
3900         data_end = leaf_data_end(root, leaf);
3901
3902         if (btrfs_leaf_free_space(root, leaf) < total_size) {
3903                 for (i = nr; i >= 0; i--) {
3904                         total_data -= data_size[i];
3905                         total_size -= data_size[i] + sizeof(struct btrfs_item);
3906                         if (total_size < btrfs_leaf_free_space(root, leaf))
3907                                 break;
3908                 }
3909                 nr = i;
3910         }
3911
3912         slot = path->slots[0];
3913         BUG_ON(slot < 0);
3914
3915         if (slot != nritems) {
3916                 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3917
3918                 item = btrfs_item_nr(leaf, slot);
3919                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3920
3921                 /* figure out how many keys we can insert in here */
3922                 total_data = data_size[0];
3923                 for (i = 1; i < nr; i++) {
3924                         if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3925                                 break;
3926                         total_data += data_size[i];
3927                 }
3928                 nr = i;
3929
3930                 if (old_data < data_end) {
3931                         btrfs_print_leaf(root, leaf);
3932                         printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3933                                slot, old_data, data_end);
3934                         BUG_ON(1);
3935                 }
3936                 /*
3937                  * item0..itemN ... dataN.offset..dataN.size .. data0.size
3938                  */
3939                 /* first correct the data pointers */
3940                 for (i = slot; i < nritems; i++) {
3941                         u32 ioff;
3942
3943                         item = btrfs_item_nr(leaf, i);
3944                         ioff = btrfs_token_item_offset(leaf, item, &token);
3945                         btrfs_set_token_item_offset(leaf, item,
3946                                                     ioff - total_data, &token);
3947                 }
3948                 /* shift the items */
3949                 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3950                               btrfs_item_nr_offset(slot),
3951                               (nritems - slot) * sizeof(struct btrfs_item));
3952
3953                 /* shift the data */
3954                 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3955                               data_end - total_data, btrfs_leaf_data(leaf) +
3956                               data_end, old_data - data_end);
3957                 data_end = old_data;
3958         } else {
3959                 /*
3960                  * this sucks but it has to be done, if we are inserting at
3961                  * the end of the leaf only insert 1 of the items, since we
3962                  * have no way of knowing whats on the next leaf and we'd have
3963                  * to drop our current locks to figure it out
3964                  */
3965                 nr = 1;
3966         }
3967
3968         /* setup the item for the new data */
3969         for (i = 0; i < nr; i++) {
3970                 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3971                 btrfs_set_item_key(leaf, &disk_key, slot + i);
3972                 item = btrfs_item_nr(leaf, slot + i);
3973                 btrfs_set_token_item_offset(leaf, item,
3974                                             data_end - data_size[i], &token);
3975                 data_end -= data_size[i];
3976                 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
3977         }
3978         btrfs_set_header_nritems(leaf, nritems + nr);
3979         btrfs_mark_buffer_dirty(leaf);
3980
3981         ret = 0;
3982         if (slot == 0) {
3983                 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3984                 fixup_low_keys(trans, root, path, &disk_key, 1);
3985         }
3986
3987         if (btrfs_leaf_free_space(root, leaf) < 0) {
3988                 btrfs_print_leaf(root, leaf);
3989                 BUG();
3990         }
3991 out:
3992         if (!ret)
3993                 ret = nr;
3994         return ret;
3995 }
3996
3997 /*
3998  * this is a helper for btrfs_insert_empty_items, the main goal here is
3999  * to save stack depth by doing the bulk of the work in a function
4000  * that doesn't call btrfs_search_slot
4001  */
4002 void setup_items_for_insert(struct btrfs_trans_handle *trans,
4003                             struct btrfs_root *root, struct btrfs_path *path,
4004                             struct btrfs_key *cpu_key, u32 *data_size,
4005                             u32 total_data, u32 total_size, int nr)
4006 {
4007         struct btrfs_item *item;
4008         int i;
4009         u32 nritems;
4010         unsigned int data_end;
4011         struct btrfs_disk_key disk_key;
4012         struct extent_buffer *leaf;
4013         int slot;
4014         struct btrfs_map_token token;
4015
4016         btrfs_init_map_token(&token);
4017
4018         leaf = path->nodes[0];
4019         slot = path->slots[0];
4020
4021         nritems = btrfs_header_nritems(leaf);
4022         data_end = leaf_data_end(root, leaf);
4023
4024         if (btrfs_leaf_free_space(root, leaf) < total_size) {
4025                 btrfs_print_leaf(root, leaf);
4026                 printk(KERN_CRIT "not enough freespace need %u have %d\n",
4027                        total_size, btrfs_leaf_free_space(root, leaf));
4028                 BUG();
4029         }
4030
4031         if (slot != nritems) {
4032                 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4033
4034                 if (old_data < data_end) {
4035                         btrfs_print_leaf(root, leaf);
4036                         printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4037                                slot, old_data, data_end);
4038                         BUG_ON(1);
4039                 }
4040                 /*
4041                  * item0..itemN ... dataN.offset..dataN.size .. data0.size
4042                  */
4043                 /* first correct the data pointers */
4044                 for (i = slot; i < nritems; i++) {
4045                         u32 ioff;
4046
4047                         item = btrfs_item_nr(leaf, i);
4048                         ioff = btrfs_token_item_offset(leaf, item, &token);
4049                         btrfs_set_token_item_offset(leaf, item,
4050                                                     ioff - total_data, &token);
4051                 }
4052                 /* shift the items */
4053                 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4054                               btrfs_item_nr_offset(slot),
4055                               (nritems - slot) * sizeof(struct btrfs_item));
4056
4057                 /* shift the data */
4058                 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4059                               data_end - total_data, btrfs_leaf_data(leaf) +
4060                               data_end, old_data - data_end);
4061                 data_end = old_data;
4062         }
4063
4064         /* setup the item for the new data */
4065         for (i = 0; i < nr; i++) {
4066                 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4067                 btrfs_set_item_key(leaf, &disk_key, slot + i);
4068                 item = btrfs_item_nr(leaf, slot + i);
4069                 btrfs_set_token_item_offset(leaf, item,
4070                                             data_end - data_size[i], &token);
4071                 data_end -= data_size[i];
4072                 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4073         }
4074
4075         btrfs_set_header_nritems(leaf, nritems + nr);
4076
4077         if (slot == 0) {
4078                 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4079                 fixup_low_keys(trans, root, path, &disk_key, 1);
4080         }
4081         btrfs_unlock_up_safe(path, 1);
4082         btrfs_mark_buffer_dirty(leaf);
4083
4084         if (btrfs_leaf_free_space(root, leaf) < 0) {
4085                 btrfs_print_leaf(root, leaf);
4086                 BUG();
4087         }
4088 }
4089
4090 /*
4091  * Given a key and some data, insert items into the tree.
4092  * This does all the path init required, making room in the tree if needed.
4093  */
4094 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4095                             struct btrfs_root *root,
4096                             struct btrfs_path *path,
4097                             struct btrfs_key *cpu_key, u32 *data_size,
4098                             int nr)
4099 {
4100         int ret = 0;
4101         int slot;
4102         int i;
4103         u32 total_size = 0;
4104         u32 total_data = 0;
4105
4106         for (i = 0; i < nr; i++)
4107                 total_data += data_size[i];
4108
4109         total_size = total_data + (nr * sizeof(struct btrfs_item));
4110         ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4111         if (ret == 0)
4112                 return -EEXIST;
4113         if (ret < 0)
4114                 return ret;
4115
4116         slot = path->slots[0];
4117         BUG_ON(slot < 0);
4118
4119         setup_items_for_insert(trans, root, path, cpu_key, data_size,
4120                                total_data, total_size, nr);
4121         return 0;
4122 }
4123
4124 /*
4125  * Given a key and some data, insert an item into the tree.
4126  * This does all the path init required, making room in the tree if needed.
4127  */
4128 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4129                       *root, struct btrfs_key *cpu_key, void *data, u32
4130                       data_size)
4131 {
4132         int ret = 0;
4133         struct btrfs_path *path;
4134         struct extent_buffer *leaf;
4135         unsigned long ptr;
4136
4137         path = btrfs_alloc_path();
4138         if (!path)
4139                 return -ENOMEM;
4140         ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4141         if (!ret) {
4142                 leaf = path->nodes[0];
4143                 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4144                 write_extent_buffer(leaf, data, ptr, data_size);
4145                 btrfs_mark_buffer_dirty(leaf);
4146         }
4147         btrfs_free_path(path);
4148         return ret;
4149 }
4150
4151 /*
4152  * delete the pointer from a given node.
4153  *
4154  * the tree should have been previously balanced so the deletion does not
4155  * empty a node.
4156  */
4157 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4158                     struct btrfs_path *path, int level, int slot)
4159 {
4160         struct extent_buffer *parent = path->nodes[level];
4161         u32 nritems;
4162
4163         nritems = btrfs_header_nritems(parent);
4164         if (slot != nritems - 1) {
4165                 memmove_extent_buffer(parent,
4166                               btrfs_node_key_ptr_offset(slot),
4167                               btrfs_node_key_ptr_offset(slot + 1),
4168                               sizeof(struct btrfs_key_ptr) *
4169                               (nritems - slot - 1));
4170         }
4171         nritems--;
4172         btrfs_set_header_nritems(parent, nritems);
4173         if (nritems == 0 && parent == root->node) {
4174                 BUG_ON(btrfs_header_level(root->node) != 1);
4175                 /* just turn the root into a leaf and break */
4176                 btrfs_set_header_level(root->node, 0);
4177         } else if (slot == 0) {
4178                 struct btrfs_disk_key disk_key;
4179
4180                 btrfs_node_key(parent, &disk_key, 0);
4181                 fixup_low_keys(trans, root, path, &disk_key, level + 1);
4182         }
4183         btrfs_mark_buffer_dirty(parent);
4184 }
4185
4186 /*
4187  * a helper function to delete the leaf pointed to by path->slots[1] and
4188  * path->nodes[1].
4189  *
4190  * This deletes the pointer in path->nodes[1] and frees the leaf
4191  * block extent.  zero is returned if it all worked out, < 0 otherwise.
4192  *
4193  * The path must have already been setup for deleting the leaf, including
4194  * all the proper balancing.  path->nodes[1] must be locked.
4195  */
4196 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4197                                     struct btrfs_root *root,
4198                                     struct btrfs_path *path,
4199                                     struct extent_buffer *leaf)
4200 {
4201         WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4202         del_ptr(trans, root, path, 1, path->slots[1]);
4203
4204         /*
4205          * btrfs_free_extent is expensive, we want to make sure we
4206          * aren't holding any locks when we call it
4207          */
4208         btrfs_unlock_up_safe(path, 0);
4209
4210         root_sub_used(root, leaf->len);
4211
4212         extent_buffer_get(leaf);
4213         btrfs_free_tree_block(trans, root, leaf, 0, 1);
4214         free_extent_buffer_stale(leaf);
4215 }
4216 /*
4217  * delete the item at the leaf level in path.  If that empties
4218  * the leaf, remove it from the tree
4219  */
4220 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4221                     struct btrfs_path *path, int slot, int nr)
4222 {
4223         struct extent_buffer *leaf;
4224         struct btrfs_item *item;
4225         int last_off;
4226         int dsize = 0;
4227         int ret = 0;
4228         int wret;
4229         int i;
4230         u32 nritems;
4231         struct btrfs_map_token token;
4232
4233         btrfs_init_map_token(&token);
4234
4235         leaf = path->nodes[0];
4236         last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4237
4238         for (i = 0; i < nr; i++)
4239                 dsize += btrfs_item_size_nr(leaf, slot + i);
4240
4241         nritems = btrfs_header_nritems(leaf);
4242
4243         if (slot + nr != nritems) {
4244                 int data_end = leaf_data_end(root, leaf);
4245
4246                 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4247                               data_end + dsize,
4248                               btrfs_leaf_data(leaf) + data_end,
4249                               last_off - data_end);
4250
4251                 for (i = slot + nr; i < nritems; i++) {
4252                         u32 ioff;
4253
4254                         item = btrfs_item_nr(leaf, i);
4255                         ioff = btrfs_token_item_offset(leaf, item, &token);
4256                         btrfs_set_token_item_offset(leaf, item,
4257                                                     ioff + dsize, &token);
4258                 }
4259
4260                 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4261                               btrfs_item_nr_offset(slot + nr),
4262                               sizeof(struct btrfs_item) *
4263                               (nritems - slot - nr));
4264         }
4265         btrfs_set_header_nritems(leaf, nritems - nr);
4266         nritems -= nr;
4267
4268         /* delete the leaf if we've emptied it */
4269         if (nritems == 0) {
4270                 if (leaf == root->node) {
4271                         btrfs_set_header_level(leaf, 0);
4272                 } else {
4273                         btrfs_set_path_blocking(path);
4274                         clean_tree_block(trans, root, leaf);
4275                         btrfs_del_leaf(trans, root, path, leaf);
4276                 }
4277         } else {
4278                 int used = leaf_space_used(leaf, 0, nritems);
4279                 if (slot == 0) {
4280                         struct btrfs_disk_key disk_key;
4281
4282                         btrfs_item_key(leaf, &disk_key, 0);
4283                         fixup_low_keys(trans, root, path, &disk_key, 1);
4284                 }
4285
4286                 /* delete the leaf if it is mostly empty */
4287                 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
4288                         /* push_leaf_left fixes the path.
4289                          * make sure the path still points to our leaf
4290                          * for possible call to del_ptr below
4291                          */
4292                         slot = path->slots[1];
4293                         extent_buffer_get(leaf);
4294
4295                         btrfs_set_path_blocking(path);
4296                         wret = push_leaf_left(trans, root, path, 1, 1,
4297                                               1, (u32)-1);
4298                         if (wret < 0 && wret != -ENOSPC)
4299                                 ret = wret;
4300
4301                         if (path->nodes[0] == leaf &&
4302                             btrfs_header_nritems(leaf)) {
4303                                 wret = push_leaf_right(trans, root, path, 1,
4304                                                        1, 1, 0);
4305                                 if (wret < 0 && wret != -ENOSPC)
4306                                         ret = wret;
4307                         }
4308
4309                         if (btrfs_header_nritems(leaf) == 0) {
4310                                 path->slots[1] = slot;
4311                                 btrfs_del_leaf(trans, root, path, leaf);
4312                                 free_extent_buffer(leaf);
4313                                 ret = 0;
4314                         } else {
4315                                 /* if we're still in the path, make sure
4316                                  * we're dirty.  Otherwise, one of the
4317                                  * push_leaf functions must have already
4318                                  * dirtied this buffer
4319                                  */
4320                                 if (path->nodes[0] == leaf)
4321                                         btrfs_mark_buffer_dirty(leaf);
4322                                 free_extent_buffer(leaf);
4323                         }
4324                 } else {
4325                         btrfs_mark_buffer_dirty(leaf);
4326                 }
4327         }
4328         return ret;
4329 }
4330
4331 /*
4332  * search the tree again to find a leaf with lesser keys
4333  * returns 0 if it found something or 1 if there are no lesser leaves.
4334  * returns < 0 on io errors.
4335  *
4336  * This may release the path, and so you may lose any locks held at the
4337  * time you call it.
4338  */
4339 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4340 {
4341         struct btrfs_key key;
4342         struct btrfs_disk_key found_key;
4343         int ret;
4344
4345         btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4346
4347         if (key.offset > 0)
4348                 key.offset--;
4349         else if (key.type > 0)
4350                 key.type--;
4351         else if (key.objectid > 0)
4352                 key.objectid--;
4353         else
4354                 return 1;
4355
4356         btrfs_release_path(path);
4357         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4358         if (ret < 0)
4359                 return ret;
4360         btrfs_item_key(path->nodes[0], &found_key, 0);
4361         ret = comp_keys(&found_key, &key);
4362         if (ret < 0)
4363                 return 0;
4364         return 1;
4365 }
4366
4367 /*
4368  * A helper function to walk down the tree starting at min_key, and looking
4369  * for nodes or leaves that are either in cache or have a minimum
4370  * transaction id.  This is used by the btree defrag code, and tree logging
4371  *
4372  * This does not cow, but it does stuff the starting key it finds back
4373  * into min_key, so you can call btrfs_search_slot with cow=1 on the
4374  * key and get a writable path.
4375  *
4376  * This does lock as it descends, and path->keep_locks should be set
4377  * to 1 by the caller.
4378  *
4379  * This honors path->lowest_level to prevent descent past a given level
4380  * of the tree.
4381  *
4382  * min_trans indicates the oldest transaction that you are interested
4383  * in walking through.  Any nodes or leaves older than min_trans are
4384  * skipped over (without reading them).
4385  *
4386  * returns zero if something useful was found, < 0 on error and 1 if there
4387  * was nothing in the tree that matched the search criteria.
4388  */
4389 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4390                          struct btrfs_key *max_key,
4391                          struct btrfs_path *path, int cache_only,
4392                          u64 min_trans)
4393 {
4394         struct extent_buffer *cur;
4395         struct btrfs_key found_key;
4396         int slot;
4397         int sret;
4398         u32 nritems;
4399         int level;
4400         int ret = 1;
4401
4402         WARN_ON(!path->keep_locks);
4403 again:
4404         cur = btrfs_read_lock_root_node(root);
4405         level = btrfs_header_level(cur);
4406         WARN_ON(path->nodes[level]);
4407         path->nodes[level] = cur;
4408         path->locks[level] = BTRFS_READ_LOCK;
4409
4410         if (btrfs_header_generation(cur) < min_trans) {
4411                 ret = 1;
4412                 goto out;
4413         }
4414         while (1) {
4415                 nritems = btrfs_header_nritems(cur);
4416                 level = btrfs_header_level(cur);
4417                 sret = bin_search(cur, min_key, level, &slot);
4418
4419                 /* at the lowest level, we're done, setup the path and exit */
4420                 if (level == path->lowest_level) {
4421                         if (slot >= nritems)
4422                                 goto find_next_key;
4423                         ret = 0;
4424                         path->slots[level] = slot;
4425                         btrfs_item_key_to_cpu(cur, &found_key, slot);
4426                         goto out;
4427                 }
4428                 if (sret && slot > 0)
4429                         slot--;
4430                 /*
4431                  * check this node pointer against the cache_only and
4432                  * min_trans parameters.  If it isn't in cache or is too
4433                  * old, skip to the next one.
4434                  */
4435                 while (slot < nritems) {
4436                         u64 blockptr;
4437                         u64 gen;
4438                         struct extent_buffer *tmp;
4439                         struct btrfs_disk_key disk_key;
4440
4441                         blockptr = btrfs_node_blockptr(cur, slot);
4442                         gen = btrfs_node_ptr_generation(cur, slot);
4443                         if (gen < min_trans) {
4444                                 slot++;
4445                                 continue;
4446                         }
4447                         if (!cache_only)
4448                                 break;
4449
4450                         if (max_key) {
4451                                 btrfs_node_key(cur, &disk_key, slot);
4452                                 if (comp_keys(&disk_key, max_key) >= 0) {
4453                                         ret = 1;
4454                                         goto out;
4455                                 }
4456                         }
4457
4458                         tmp = btrfs_find_tree_block(root, blockptr,
4459                                             btrfs_level_size(root, level - 1));
4460
4461                         if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
4462                                 free_extent_buffer(tmp);
4463                                 break;
4464                         }
4465                         if (tmp)
4466                                 free_extent_buffer(tmp);
4467                         slot++;
4468                 }
4469 find_next_key:
4470                 /*
4471                  * we didn't find a candidate key in this node, walk forward
4472                  * and find another one
4473                  */
4474                 if (slot >= nritems) {
4475                         path->slots[level] = slot;
4476                         btrfs_set_path_blocking(path);
4477                         sret = btrfs_find_next_key(root, path, min_key, level,
4478                                                   cache_only, min_trans);
4479                         if (sret == 0) {
4480                                 btrfs_release_path(path);
4481                                 goto again;
4482                         } else {
4483                                 goto out;
4484                         }
4485                 }
4486                 /* save our key for returning back */
4487                 btrfs_node_key_to_cpu(cur, &found_key, slot);
4488                 path->slots[level] = slot;
4489                 if (level == path->lowest_level) {
4490                         ret = 0;
4491                         unlock_up(path, level, 1, 0, NULL);
4492                         goto out;
4493                 }
4494                 btrfs_set_path_blocking(path);
4495                 cur = read_node_slot(root, cur, slot);
4496                 BUG_ON(!cur); /* -ENOMEM */
4497
4498                 btrfs_tree_read_lock(cur);
4499
4500                 path->locks[level - 1] = BTRFS_READ_LOCK;
4501                 path->nodes[level - 1] = cur;
4502                 unlock_up(path, level, 1, 0, NULL);
4503                 btrfs_clear_path_blocking(path, NULL, 0);
4504         }
4505 out:
4506         if (ret == 0)
4507                 memcpy(min_key, &found_key, sizeof(found_key));
4508         btrfs_set_path_blocking(path);
4509         return ret;
4510 }
4511
4512 /*
4513  * this is similar to btrfs_next_leaf, but does not try to preserve
4514  * and fixup the path.  It looks for and returns the next key in the
4515  * tree based on the current path and the cache_only and min_trans
4516  * parameters.
4517  *
4518  * 0 is returned if another key is found, < 0 if there are any errors
4519  * and 1 is returned if there are no higher keys in the tree
4520  *
4521  * path->keep_locks should be set to 1 on the search made before
4522  * calling this function.
4523  */
4524 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4525                         struct btrfs_key *key, int level,
4526                         int cache_only, u64 min_trans)
4527 {
4528         int slot;
4529         struct extent_buffer *c;
4530
4531         WARN_ON(!path->keep_locks);
4532         while (level < BTRFS_MAX_LEVEL) {
4533                 if (!path->nodes[level])
4534                         return 1;
4535
4536                 slot = path->slots[level] + 1;
4537                 c = path->nodes[level];
4538 next:
4539                 if (slot >= btrfs_header_nritems(c)) {
4540                         int ret;
4541                         int orig_lowest;
4542                         struct btrfs_key cur_key;
4543                         if (level + 1 >= BTRFS_MAX_LEVEL ||
4544                             !path->nodes[level + 1])
4545                                 return 1;
4546
4547                         if (path->locks[level + 1]) {
4548                                 level++;
4549                                 continue;
4550                         }
4551
4552                         slot = btrfs_header_nritems(c) - 1;
4553                         if (level == 0)
4554                                 btrfs_item_key_to_cpu(c, &cur_key, slot);
4555                         else
4556                                 btrfs_node_key_to_cpu(c, &cur_key, slot);
4557
4558                         orig_lowest = path->lowest_level;
4559                         btrfs_release_path(path);
4560                         path->lowest_level = level;
4561                         ret = btrfs_search_slot(NULL, root, &cur_key, path,
4562                                                 0, 0);
4563                         path->lowest_level = orig_lowest;
4564                         if (ret < 0)
4565                                 return ret;
4566
4567                         c = path->nodes[level];
4568                         slot = path->slots[level];
4569                         if (ret == 0)
4570                                 slot++;
4571                         goto next;
4572                 }
4573
4574                 if (level == 0)
4575                         btrfs_item_key_to_cpu(c, key, slot);
4576                 else {
4577                         u64 blockptr = btrfs_node_blockptr(c, slot);
4578                         u64 gen = btrfs_node_ptr_generation(c, slot);
4579
4580                         if (cache_only) {
4581                                 struct extent_buffer *cur;
4582                                 cur = btrfs_find_tree_block(root, blockptr,
4583                                             btrfs_level_size(root, level - 1));
4584                                 if (!cur ||
4585                                     btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
4586                                         slot++;
4587                                         if (cur)
4588                                                 free_extent_buffer(cur);
4589                                         goto next;
4590                                 }
4591                                 free_extent_buffer(cur);
4592                         }
4593                         if (gen < min_trans) {
4594                                 slot++;
4595                                 goto next;
4596                         }
4597                         btrfs_node_key_to_cpu(c, key, slot);
4598                 }
4599                 return 0;
4600         }
4601         return 1;
4602 }
4603
4604 /*
4605  * search the tree again to find a leaf with greater keys
4606  * returns 0 if it found something or 1 if there are no greater leaves.
4607  * returns < 0 on io errors.
4608  */
4609 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4610 {
4611         int slot;
4612         int level;
4613         struct extent_buffer *c;
4614         struct extent_buffer *next;
4615         struct btrfs_key key;
4616         u32 nritems;
4617         int ret;
4618         int old_spinning = path->leave_spinning;
4619         int next_rw_lock = 0;
4620
4621         nritems = btrfs_header_nritems(path->nodes[0]);
4622         if (nritems == 0)
4623                 return 1;
4624
4625         btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4626 again:
4627         level = 1;
4628         next = NULL;
4629         next_rw_lock = 0;
4630         btrfs_release_path(path);
4631
4632         path->keep_locks = 1;
4633         path->leave_spinning = 1;
4634
4635         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4636         path->keep_locks = 0;
4637
4638         if (ret < 0)
4639                 return ret;
4640
4641         nritems = btrfs_header_nritems(path->nodes[0]);
4642         /*
4643          * by releasing the path above we dropped all our locks.  A balance
4644          * could have added more items next to the key that used to be
4645          * at the very end of the block.  So, check again here and
4646          * advance the path if there are now more items available.
4647          */
4648         if (nritems > 0 && path->slots[0] < nritems - 1) {
4649                 if (ret == 0)
4650                         path->slots[0]++;
4651                 ret = 0;
4652                 goto done;
4653         }
4654
4655         while (level < BTRFS_MAX_LEVEL) {
4656                 if (!path->nodes[level]) {
4657                         ret = 1;
4658                         goto done;
4659                 }
4660
4661                 slot = path->slots[level] + 1;
4662                 c = path->nodes[level];
4663                 if (slot >= btrfs_header_nritems(c)) {
4664                         level++;
4665                         if (level == BTRFS_MAX_LEVEL) {
4666                                 ret = 1;
4667                                 goto done;
4668                         }
4669                         continue;
4670                 }
4671
4672                 if (next) {
4673                         btrfs_tree_unlock_rw(next, next_rw_lock);
4674                         free_extent_buffer(next);
4675                 }
4676
4677                 next = c;
4678                 next_rw_lock = path->locks[level];
4679                 ret = read_block_for_search(NULL, root, path, &next, level,
4680                                             slot, &key);
4681                 if (ret == -EAGAIN)
4682                         goto again;
4683
4684                 if (ret < 0) {
4685                         btrfs_release_path(path);
4686                         goto done;
4687                 }
4688
4689                 if (!path->skip_locking) {
4690                         ret = btrfs_try_tree_read_lock(next);
4691                         if (!ret) {
4692                                 btrfs_set_path_blocking(path);
4693                                 btrfs_tree_read_lock(next);
4694                                 btrfs_clear_path_blocking(path, next,
4695                                                           BTRFS_READ_LOCK);
4696                         }
4697                         next_rw_lock = BTRFS_READ_LOCK;
4698                 }
4699                 break;
4700         }
4701         path->slots[level] = slot;
4702         while (1) {
4703                 level--;
4704                 c = path->nodes[level];
4705                 if (path->locks[level])
4706                         btrfs_tree_unlock_rw(c, path->locks[level]);
4707
4708                 free_extent_buffer(c);
4709                 path->nodes[level] = next;
4710                 path->slots[level] = 0;
4711                 if (!path->skip_locking)
4712                         path->locks[level] = next_rw_lock;
4713                 if (!level)
4714                         break;
4715
4716                 ret = read_block_for_search(NULL, root, path, &next, level,
4717                                             0, &key);
4718                 if (ret == -EAGAIN)
4719                         goto again;
4720
4721                 if (ret < 0) {
4722                         btrfs_release_path(path);
4723                         goto done;
4724                 }
4725
4726                 if (!path->skip_locking) {
4727                         ret = btrfs_try_tree_read_lock(next);
4728                         if (!ret) {
4729                                 btrfs_set_path_blocking(path);
4730                                 btrfs_tree_read_lock(next);
4731                                 btrfs_clear_path_blocking(path, next,
4732                                                           BTRFS_READ_LOCK);
4733                         }
4734                         next_rw_lock = BTRFS_READ_LOCK;
4735                 }
4736         }
4737         ret = 0;
4738 done:
4739         unlock_up(path, 0, 1, 0, NULL);
4740         path->leave_spinning = old_spinning;
4741         if (!old_spinning)
4742                 btrfs_set_path_blocking(path);
4743
4744         return ret;
4745 }
4746
4747 /*
4748  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4749  * searching until it gets past min_objectid or finds an item of 'type'
4750  *
4751  * returns 0 if something is found, 1 if nothing was found and < 0 on error
4752  */
4753 int btrfs_previous_item(struct btrfs_root *root,
4754                         struct btrfs_path *path, u64 min_objectid,
4755                         int type)
4756 {
4757         struct btrfs_key found_key;
4758         struct extent_buffer *leaf;
4759         u32 nritems;
4760         int ret;
4761
4762         while (1) {
4763                 if (path->slots[0] == 0) {
4764                         btrfs_set_path_blocking(path);
4765                         ret = btrfs_prev_leaf(root, path);
4766                         if (ret != 0)
4767                                 return ret;
4768                 } else {
4769                         path->slots[0]--;
4770                 }
4771                 leaf = path->nodes[0];
4772                 nritems = btrfs_header_nritems(leaf);
4773                 if (nritems == 0)
4774                         return 1;
4775                 if (path->slots[0] == nritems)
4776                         path->slots[0]--;
4777
4778                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4779                 if (found_key.objectid < min_objectid)
4780                         break;
4781                 if (found_key.type == type)
4782                         return 0;
4783                 if (found_key.objectid == min_objectid &&
4784                     found_key.type < type)
4785                         break;
4786         }
4787         return 1;
4788 }