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