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