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