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