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