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