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