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