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