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