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