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