Btrfs: remove obsolete btrfs_next_leaf call from __resolve_indirect_ref
[GitHub/mt8127/android_kernel_alcatel_ttab.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:
c3193108
JS
1079 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1080 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1081 memmove_extent_buffer(eb, o_dst, o_src,
5d9e75c4
JS
1082 tm->move.nr_items * p_size);
1083 break;
1084 case MOD_LOG_ROOT_REPLACE:
1085 /*
1086 * this operation is special. for roots, this must be
1087 * handled explicitly before rewinding.
1088 * for non-roots, this operation may exist if the node
1089 * was a root: root A -> child B; then A gets empty and
1090 * B is promoted to the new root. in the mod log, we'll
1091 * have a root-replace operation for B, a tree block
1092 * that is no root. we simply ignore that operation.
1093 */
1094 break;
1095 }
1096 next = rb_next(&tm->node);
1097 if (!next)
1098 break;
1099 tm = container_of(next, struct tree_mod_elem, node);
1100 if (tm->index != first_tm->index)
1101 break;
1102 }
1103 btrfs_set_header_nritems(eb, n);
1104}
1105
1106static struct extent_buffer *
1107tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1108 u64 time_seq)
1109{
1110 struct extent_buffer *eb_rewin;
1111 struct tree_mod_elem *tm;
1112
1113 if (!time_seq)
1114 return eb;
1115
1116 if (btrfs_header_level(eb) == 0)
1117 return eb;
1118
1119 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1120 if (!tm)
1121 return eb;
1122
1123 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1124 BUG_ON(tm->slot != 0);
1125 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1126 fs_info->tree_root->nodesize);
1127 BUG_ON(!eb_rewin);
1128 btrfs_set_header_bytenr(eb_rewin, eb->start);
1129 btrfs_set_header_backref_rev(eb_rewin,
1130 btrfs_header_backref_rev(eb));
1131 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
c3193108 1132 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
5d9e75c4
JS
1133 } else {
1134 eb_rewin = btrfs_clone_extent_buffer(eb);
1135 BUG_ON(!eb_rewin);
1136 }
1137
1138 extent_buffer_get(eb_rewin);
1139 free_extent_buffer(eb);
1140
1141 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1142
1143 return eb_rewin;
1144}
1145
1146static inline struct extent_buffer *
1147get_old_root(struct btrfs_root *root, u64 time_seq)
1148{
1149 struct tree_mod_elem *tm;
1150 struct extent_buffer *eb;
1151 struct tree_mod_root *old_root;
1152 u64 old_generation;
1153
1154 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1155 if (!tm)
1156 return root->node;
1157
1158 old_root = &tm->old_root;
1159 old_generation = tm->generation;
1160
1161 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1162 /*
1163 * there was an item in the log when __tree_mod_log_oldest_root
1164 * returned. this one must not go away, because the time_seq passed to
1165 * us must be blocking its removal.
1166 */
1167 BUG_ON(!tm);
1168
1169 if (old_root->logical == root->node->start) {
1170 /* there are logged operations for the current root */
1171 eb = btrfs_clone_extent_buffer(root->node);
1172 } else {
1173 /* there's a root replace operation for the current root */
1174 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1175 root->nodesize);
1176 btrfs_set_header_bytenr(eb, eb->start);
1177 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1178 btrfs_set_header_owner(eb, root->root_key.objectid);
1179 }
1180 if (!eb)
1181 return NULL;
1182 btrfs_set_header_level(eb, old_root->level);
1183 btrfs_set_header_generation(eb, old_generation);
1184 __tree_mod_log_rewind(eb, time_seq, tm);
1185
1186 return eb;
1187}
1188
5d4f98a2
YZ
1189static inline int should_cow_block(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root,
1191 struct extent_buffer *buf)
1192{
f1ebcc74
LB
1193 /* ensure we can see the force_cow */
1194 smp_rmb();
1195
1196 /*
1197 * We do not need to cow a block if
1198 * 1) this block is not created or changed in this transaction;
1199 * 2) this block does not belong to TREE_RELOC tree;
1200 * 3) the root is not forced COW.
1201 *
1202 * What is forced COW:
1203 * when we create snapshot during commiting the transaction,
1204 * after we've finished coping src root, we must COW the shared
1205 * block to ensure the metadata consistency.
1206 */
5d4f98a2
YZ
1207 if (btrfs_header_generation(buf) == trans->transid &&
1208 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1209 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
f1ebcc74
LB
1210 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1211 !root->force_cow)
5d4f98a2
YZ
1212 return 0;
1213 return 1;
1214}
1215
d352ac68
CM
1216/*
1217 * cows a single block, see __btrfs_cow_block for the real work.
1218 * This version of it has extra checks so that a block isn't cow'd more than
1219 * once per transaction, as long as it hasn't been written yet
1220 */
d397712b 1221noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5f39d397
CM
1222 struct btrfs_root *root, struct extent_buffer *buf,
1223 struct extent_buffer *parent, int parent_slot,
9fa8cfe7 1224 struct extent_buffer **cow_ret)
6702ed49
CM
1225{
1226 u64 search_start;
f510cfec 1227 int ret;
dc17ff8f 1228
6702ed49 1229 if (trans->transaction != root->fs_info->running_transaction) {
d397712b
CM
1230 printk(KERN_CRIT "trans %llu running %llu\n",
1231 (unsigned long long)trans->transid,
1232 (unsigned long long)
6702ed49
CM
1233 root->fs_info->running_transaction->transid);
1234 WARN_ON(1);
1235 }
1236 if (trans->transid != root->fs_info->generation) {
d397712b
CM
1237 printk(KERN_CRIT "trans %llu running %llu\n",
1238 (unsigned long long)trans->transid,
1239 (unsigned long long)root->fs_info->generation);
6702ed49
CM
1240 WARN_ON(1);
1241 }
dc17ff8f 1242
5d4f98a2 1243 if (!should_cow_block(trans, root, buf)) {
6702ed49
CM
1244 *cow_ret = buf;
1245 return 0;
1246 }
c487685d 1247
0b86a832 1248 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
b4ce94de
CM
1249
1250 if (parent)
1251 btrfs_set_lock_blocking(parent);
1252 btrfs_set_lock_blocking(buf);
1253
f510cfec 1254 ret = __btrfs_cow_block(trans, root, buf, parent,
9fa8cfe7 1255 parent_slot, cow_ret, search_start, 0);
1abe9b8a 1256
1257 trace_btrfs_cow_block(root, buf, *cow_ret);
1258
f510cfec 1259 return ret;
6702ed49
CM
1260}
1261
d352ac68
CM
1262/*
1263 * helper function for defrag to decide if two blocks pointed to by a
1264 * node are actually close by
1265 */
6b80053d 1266static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
6702ed49 1267{
6b80053d 1268 if (blocknr < other && other - (blocknr + blocksize) < 32768)
6702ed49 1269 return 1;
6b80053d 1270 if (blocknr > other && blocknr - (other + blocksize) < 32768)
6702ed49
CM
1271 return 1;
1272 return 0;
1273}
1274
081e9573
CM
1275/*
1276 * compare two keys in a memcmp fashion
1277 */
1278static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1279{
1280 struct btrfs_key k1;
1281
1282 btrfs_disk_key_to_cpu(&k1, disk);
1283
20736aba 1284 return btrfs_comp_cpu_keys(&k1, k2);
081e9573
CM
1285}
1286
f3465ca4
JB
1287/*
1288 * same as comp_keys only with two btrfs_key's
1289 */
5d4f98a2 1290int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
f3465ca4
JB
1291{
1292 if (k1->objectid > k2->objectid)
1293 return 1;
1294 if (k1->objectid < k2->objectid)
1295 return -1;
1296 if (k1->type > k2->type)
1297 return 1;
1298 if (k1->type < k2->type)
1299 return -1;
1300 if (k1->offset > k2->offset)
1301 return 1;
1302 if (k1->offset < k2->offset)
1303 return -1;
1304 return 0;
1305}
081e9573 1306
d352ac68
CM
1307/*
1308 * this is used by the defrag code to go through all the
1309 * leaves pointed to by a node and reallocate them so that
1310 * disk order is close to key order
1311 */
6702ed49 1312int btrfs_realloc_node(struct btrfs_trans_handle *trans,
5f39d397 1313 struct btrfs_root *root, struct extent_buffer *parent,
a6b6e75e
CM
1314 int start_slot, int cache_only, u64 *last_ret,
1315 struct btrfs_key *progress)
6702ed49 1316{
6b80053d 1317 struct extent_buffer *cur;
6702ed49 1318 u64 blocknr;
ca7a79ad 1319 u64 gen;
e9d0b13b
CM
1320 u64 search_start = *last_ret;
1321 u64 last_block = 0;
6702ed49
CM
1322 u64 other;
1323 u32 parent_nritems;
6702ed49
CM
1324 int end_slot;
1325 int i;
1326 int err = 0;
f2183bde 1327 int parent_level;
6b80053d
CM
1328 int uptodate;
1329 u32 blocksize;
081e9573
CM
1330 int progress_passed = 0;
1331 struct btrfs_disk_key disk_key;
6702ed49 1332
5708b959
CM
1333 parent_level = btrfs_header_level(parent);
1334 if (cache_only && parent_level != 1)
1335 return 0;
1336
d397712b 1337 if (trans->transaction != root->fs_info->running_transaction)
6702ed49 1338 WARN_ON(1);
d397712b 1339 if (trans->transid != root->fs_info->generation)
6702ed49 1340 WARN_ON(1);
86479a04 1341
6b80053d 1342 parent_nritems = btrfs_header_nritems(parent);
6b80053d 1343 blocksize = btrfs_level_size(root, parent_level - 1);
6702ed49
CM
1344 end_slot = parent_nritems;
1345
1346 if (parent_nritems == 1)
1347 return 0;
1348
b4ce94de
CM
1349 btrfs_set_lock_blocking(parent);
1350
6702ed49
CM
1351 for (i = start_slot; i < end_slot; i++) {
1352 int close = 1;
a6b6e75e 1353
081e9573
CM
1354 btrfs_node_key(parent, &disk_key, i);
1355 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1356 continue;
1357
1358 progress_passed = 1;
6b80053d 1359 blocknr = btrfs_node_blockptr(parent, i);
ca7a79ad 1360 gen = btrfs_node_ptr_generation(parent, i);
e9d0b13b
CM
1361 if (last_block == 0)
1362 last_block = blocknr;
5708b959 1363
6702ed49 1364 if (i > 0) {
6b80053d
CM
1365 other = btrfs_node_blockptr(parent, i - 1);
1366 close = close_blocks(blocknr, other, blocksize);
6702ed49 1367 }
0ef3e66b 1368 if (!close && i < end_slot - 2) {
6b80053d
CM
1369 other = btrfs_node_blockptr(parent, i + 1);
1370 close = close_blocks(blocknr, other, blocksize);
6702ed49 1371 }
e9d0b13b
CM
1372 if (close) {
1373 last_block = blocknr;
6702ed49 1374 continue;
e9d0b13b 1375 }
6702ed49 1376
6b80053d
CM
1377 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1378 if (cur)
b9fab919 1379 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
6b80053d
CM
1380 else
1381 uptodate = 0;
5708b959 1382 if (!cur || !uptodate) {
6702ed49 1383 if (cache_only) {
6b80053d 1384 free_extent_buffer(cur);
6702ed49
CM
1385 continue;
1386 }
6b80053d
CM
1387 if (!cur) {
1388 cur = read_tree_block(root, blocknr,
ca7a79ad 1389 blocksize, gen);
97d9a8a4
TI
1390 if (!cur)
1391 return -EIO;
6b80053d 1392 } else if (!uptodate) {
018642a1
TI
1393 err = btrfs_read_buffer(cur, gen);
1394 if (err) {
1395 free_extent_buffer(cur);
1396 return err;
1397 }
f2183bde 1398 }
6702ed49 1399 }
e9d0b13b 1400 if (search_start == 0)
6b80053d 1401 search_start = last_block;
e9d0b13b 1402
e7a84565 1403 btrfs_tree_lock(cur);
b4ce94de 1404 btrfs_set_lock_blocking(cur);
6b80053d 1405 err = __btrfs_cow_block(trans, root, cur, parent, i,
e7a84565 1406 &cur, search_start,
6b80053d 1407 min(16 * blocksize,
9fa8cfe7 1408 (end_slot - i) * blocksize));
252c38f0 1409 if (err) {
e7a84565 1410 btrfs_tree_unlock(cur);
6b80053d 1411 free_extent_buffer(cur);
6702ed49 1412 break;
252c38f0 1413 }
e7a84565
CM
1414 search_start = cur->start;
1415 last_block = cur->start;
f2183bde 1416 *last_ret = search_start;
e7a84565
CM
1417 btrfs_tree_unlock(cur);
1418 free_extent_buffer(cur);
6702ed49
CM
1419 }
1420 return err;
1421}
1422
74123bd7
CM
1423/*
1424 * The leaf data grows from end-to-front in the node.
1425 * this returns the address of the start of the last item,
1426 * which is the stop of the leaf data stack
1427 */
123abc88 1428static inline unsigned int leaf_data_end(struct btrfs_root *root,
5f39d397 1429 struct extent_buffer *leaf)
be0e5c09 1430{
5f39d397 1431 u32 nr = btrfs_header_nritems(leaf);
be0e5c09 1432 if (nr == 0)
123abc88 1433 return BTRFS_LEAF_DATA_SIZE(root);
5f39d397 1434 return btrfs_item_offset_nr(leaf, nr - 1);
be0e5c09
CM
1435}
1436
aa5d6bed 1437
74123bd7 1438/*
5f39d397
CM
1439 * search for key in the extent_buffer. The items start at offset p,
1440 * and they are item_size apart. There are 'max' items in p.
1441 *
74123bd7
CM
1442 * the slot in the array is returned via slot, and it points to
1443 * the place where you would insert key if it is not found in
1444 * the array.
1445 *
1446 * slot may point to max if the key is bigger than all of the keys
1447 */
e02119d5
CM
1448static noinline int generic_bin_search(struct extent_buffer *eb,
1449 unsigned long p,
1450 int item_size, struct btrfs_key *key,
1451 int max, int *slot)
be0e5c09
CM
1452{
1453 int low = 0;
1454 int high = max;
1455 int mid;
1456 int ret;
479965d6 1457 struct btrfs_disk_key *tmp = NULL;
5f39d397
CM
1458 struct btrfs_disk_key unaligned;
1459 unsigned long offset;
5f39d397
CM
1460 char *kaddr = NULL;
1461 unsigned long map_start = 0;
1462 unsigned long map_len = 0;
479965d6 1463 int err;
be0e5c09 1464
d397712b 1465 while (low < high) {
be0e5c09 1466 mid = (low + high) / 2;
5f39d397
CM
1467 offset = p + mid * item_size;
1468
a6591715 1469 if (!kaddr || offset < map_start ||
5f39d397
CM
1470 (offset + sizeof(struct btrfs_disk_key)) >
1471 map_start + map_len) {
934d375b
CM
1472
1473 err = map_private_extent_buffer(eb, offset,
479965d6 1474 sizeof(struct btrfs_disk_key),
a6591715 1475 &kaddr, &map_start, &map_len);
479965d6
CM
1476
1477 if (!err) {
1478 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1479 map_start);
1480 } else {
1481 read_extent_buffer(eb, &unaligned,
1482 offset, sizeof(unaligned));
1483 tmp = &unaligned;
1484 }
5f39d397 1485
5f39d397
CM
1486 } else {
1487 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1488 map_start);
1489 }
be0e5c09
CM
1490 ret = comp_keys(tmp, key);
1491
1492 if (ret < 0)
1493 low = mid + 1;
1494 else if (ret > 0)
1495 high = mid;
1496 else {
1497 *slot = mid;
1498 return 0;
1499 }
1500 }
1501 *slot = low;
1502 return 1;
1503}
1504
97571fd0
CM
1505/*
1506 * simple bin_search frontend that does the right thing for
1507 * leaves vs nodes
1508 */
5f39d397
CM
1509static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1510 int level, int *slot)
be0e5c09 1511{
f775738f 1512 if (level == 0)
5f39d397
CM
1513 return generic_bin_search(eb,
1514 offsetof(struct btrfs_leaf, items),
0783fcfc 1515 sizeof(struct btrfs_item),
5f39d397 1516 key, btrfs_header_nritems(eb),
7518a238 1517 slot);
f775738f 1518 else
5f39d397
CM
1519 return generic_bin_search(eb,
1520 offsetof(struct btrfs_node, ptrs),
123abc88 1521 sizeof(struct btrfs_key_ptr),
5f39d397 1522 key, btrfs_header_nritems(eb),
7518a238 1523 slot);
be0e5c09
CM
1524}
1525
5d4f98a2
YZ
1526int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1527 int level, int *slot)
1528{
1529 return bin_search(eb, key, level, slot);
1530}
1531
f0486c68
YZ
1532static void root_add_used(struct btrfs_root *root, u32 size)
1533{
1534 spin_lock(&root->accounting_lock);
1535 btrfs_set_root_used(&root->root_item,
1536 btrfs_root_used(&root->root_item) + size);
1537 spin_unlock(&root->accounting_lock);
1538}
1539
1540static void root_sub_used(struct btrfs_root *root, u32 size)
1541{
1542 spin_lock(&root->accounting_lock);
1543 btrfs_set_root_used(&root->root_item,
1544 btrfs_root_used(&root->root_item) - size);
1545 spin_unlock(&root->accounting_lock);
1546}
1547
d352ac68
CM
1548/* given a node and slot number, this reads the blocks it points to. The
1549 * extent buffer is returned with a reference taken (but unlocked).
1550 * NULL is returned on error.
1551 */
e02119d5 1552static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
5f39d397 1553 struct extent_buffer *parent, int slot)
bb803951 1554{
ca7a79ad 1555 int level = btrfs_header_level(parent);
bb803951
CM
1556 if (slot < 0)
1557 return NULL;
5f39d397 1558 if (slot >= btrfs_header_nritems(parent))
bb803951 1559 return NULL;
ca7a79ad
CM
1560
1561 BUG_ON(level == 0);
1562
db94535d 1563 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
ca7a79ad
CM
1564 btrfs_level_size(root, level - 1),
1565 btrfs_node_ptr_generation(parent, slot));
bb803951
CM
1566}
1567
d352ac68
CM
1568/*
1569 * node level balancing, used to make sure nodes are in proper order for
1570 * item deletion. We balance from the top down, so we have to make sure
1571 * that a deletion won't leave an node completely empty later on.
1572 */
e02119d5 1573static noinline int balance_level(struct btrfs_trans_handle *trans,
98ed5174
CM
1574 struct btrfs_root *root,
1575 struct btrfs_path *path, int level)
bb803951 1576{
5f39d397
CM
1577 struct extent_buffer *right = NULL;
1578 struct extent_buffer *mid;
1579 struct extent_buffer *left = NULL;
1580 struct extent_buffer *parent = NULL;
bb803951
CM
1581 int ret = 0;
1582 int wret;
1583 int pslot;
bb803951 1584 int orig_slot = path->slots[level];
79f95c82 1585 u64 orig_ptr;
bb803951
CM
1586
1587 if (level == 0)
1588 return 0;
1589
5f39d397 1590 mid = path->nodes[level];
b4ce94de 1591
bd681513
CM
1592 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1593 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
7bb86316
CM
1594 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1595
1d4f8a0c 1596 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
79f95c82 1597
a05a9bb1 1598 if (level < BTRFS_MAX_LEVEL - 1) {
5f39d397 1599 parent = path->nodes[level + 1];
a05a9bb1
LZ
1600 pslot = path->slots[level + 1];
1601 }
bb803951 1602
40689478
CM
1603 /*
1604 * deal with the case where there is only one pointer in the root
1605 * by promoting the node below to a root
1606 */
5f39d397
CM
1607 if (!parent) {
1608 struct extent_buffer *child;
bb803951 1609
5f39d397 1610 if (btrfs_header_nritems(mid) != 1)
bb803951
CM
1611 return 0;
1612
1613 /* promote the child to a root */
5f39d397 1614 child = read_node_slot(root, mid, 0);
305a26af
MF
1615 if (!child) {
1616 ret = -EROFS;
1617 btrfs_std_error(root->fs_info, ret);
1618 goto enospc;
1619 }
1620
925baedd 1621 btrfs_tree_lock(child);
b4ce94de 1622 btrfs_set_lock_blocking(child);
9fa8cfe7 1623 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
f0486c68
YZ
1624 if (ret) {
1625 btrfs_tree_unlock(child);
1626 free_extent_buffer(child);
1627 goto enospc;
1628 }
2f375ab9 1629
f230475e 1630 tree_mod_log_set_root_pointer(root, child);
240f62c8 1631 rcu_assign_pointer(root->node, child);
925baedd 1632
0b86a832 1633 add_root_to_dirty_list(root);
925baedd 1634 btrfs_tree_unlock(child);
b4ce94de 1635
925baedd 1636 path->locks[level] = 0;
bb803951 1637 path->nodes[level] = NULL;
5f39d397 1638 clean_tree_block(trans, root, mid);
925baedd 1639 btrfs_tree_unlock(mid);
bb803951 1640 /* once for the path */
5f39d397 1641 free_extent_buffer(mid);
f0486c68
YZ
1642
1643 root_sub_used(root, mid->len);
5581a51a 1644 btrfs_free_tree_block(trans, root, mid, 0, 1);
bb803951 1645 /* once for the root ptr */
3083ee2e 1646 free_extent_buffer_stale(mid);
f0486c68 1647 return 0;
bb803951 1648 }
5f39d397 1649 if (btrfs_header_nritems(mid) >
123abc88 1650 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
bb803951
CM
1651 return 0;
1652
5f39d397
CM
1653 left = read_node_slot(root, parent, pslot - 1);
1654 if (left) {
925baedd 1655 btrfs_tree_lock(left);
b4ce94de 1656 btrfs_set_lock_blocking(left);
5f39d397 1657 wret = btrfs_cow_block(trans, root, left,
9fa8cfe7 1658 parent, pslot - 1, &left);
54aa1f4d
CM
1659 if (wret) {
1660 ret = wret;
1661 goto enospc;
1662 }
2cc58cf2 1663 }
5f39d397
CM
1664 right = read_node_slot(root, parent, pslot + 1);
1665 if (right) {
925baedd 1666 btrfs_tree_lock(right);
b4ce94de 1667 btrfs_set_lock_blocking(right);
5f39d397 1668 wret = btrfs_cow_block(trans, root, right,
9fa8cfe7 1669 parent, pslot + 1, &right);
2cc58cf2
CM
1670 if (wret) {
1671 ret = wret;
1672 goto enospc;
1673 }
1674 }
1675
1676 /* first, try to make some room in the middle buffer */
5f39d397
CM
1677 if (left) {
1678 orig_slot += btrfs_header_nritems(left);
bce4eae9 1679 wret = push_node_left(trans, root, left, mid, 1);
79f95c82
CM
1680 if (wret < 0)
1681 ret = wret;
bb803951 1682 }
79f95c82
CM
1683
1684 /*
1685 * then try to empty the right most buffer into the middle
1686 */
5f39d397 1687 if (right) {
971a1f66 1688 wret = push_node_left(trans, root, mid, right, 1);
54aa1f4d 1689 if (wret < 0 && wret != -ENOSPC)
79f95c82 1690 ret = wret;
5f39d397 1691 if (btrfs_header_nritems(right) == 0) {
5f39d397 1692 clean_tree_block(trans, root, right);
925baedd 1693 btrfs_tree_unlock(right);
f3ea38da 1694 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
f0486c68 1695 root_sub_used(root, right->len);
5581a51a 1696 btrfs_free_tree_block(trans, root, right, 0, 1);
3083ee2e 1697 free_extent_buffer_stale(right);
f0486c68 1698 right = NULL;
bb803951 1699 } else {
5f39d397
CM
1700 struct btrfs_disk_key right_key;
1701 btrfs_node_key(right, &right_key, 0);
f230475e
JS
1702 tree_mod_log_set_node_key(root->fs_info, parent,
1703 &right_key, pslot + 1, 0);
5f39d397
CM
1704 btrfs_set_node_key(parent, &right_key, pslot + 1);
1705 btrfs_mark_buffer_dirty(parent);
bb803951
CM
1706 }
1707 }
5f39d397 1708 if (btrfs_header_nritems(mid) == 1) {
79f95c82
CM
1709 /*
1710 * we're not allowed to leave a node with one item in the
1711 * tree during a delete. A deletion from lower in the tree
1712 * could try to delete the only pointer in this node.
1713 * So, pull some keys from the left.
1714 * There has to be a left pointer at this point because
1715 * otherwise we would have pulled some pointers from the
1716 * right
1717 */
305a26af
MF
1718 if (!left) {
1719 ret = -EROFS;
1720 btrfs_std_error(root->fs_info, ret);
1721 goto enospc;
1722 }
5f39d397 1723 wret = balance_node_right(trans, root, mid, left);
54aa1f4d 1724 if (wret < 0) {
79f95c82 1725 ret = wret;
54aa1f4d
CM
1726 goto enospc;
1727 }
bce4eae9
CM
1728 if (wret == 1) {
1729 wret = push_node_left(trans, root, left, mid, 1);
1730 if (wret < 0)
1731 ret = wret;
1732 }
79f95c82
CM
1733 BUG_ON(wret == 1);
1734 }
5f39d397 1735 if (btrfs_header_nritems(mid) == 0) {
5f39d397 1736 clean_tree_block(trans, root, mid);
925baedd 1737 btrfs_tree_unlock(mid);
f3ea38da 1738 del_ptr(trans, root, path, level + 1, pslot, 1);
f0486c68 1739 root_sub_used(root, mid->len);
5581a51a 1740 btrfs_free_tree_block(trans, root, mid, 0, 1);
3083ee2e 1741 free_extent_buffer_stale(mid);
f0486c68 1742 mid = NULL;
79f95c82
CM
1743 } else {
1744 /* update the parent key to reflect our changes */
5f39d397
CM
1745 struct btrfs_disk_key mid_key;
1746 btrfs_node_key(mid, &mid_key, 0);
f230475e
JS
1747 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1748 pslot, 0);
5f39d397
CM
1749 btrfs_set_node_key(parent, &mid_key, pslot);
1750 btrfs_mark_buffer_dirty(parent);
79f95c82 1751 }
bb803951 1752
79f95c82 1753 /* update the path */
5f39d397
CM
1754 if (left) {
1755 if (btrfs_header_nritems(left) > orig_slot) {
1756 extent_buffer_get(left);
925baedd 1757 /* left was locked after cow */
5f39d397 1758 path->nodes[level] = left;
bb803951
CM
1759 path->slots[level + 1] -= 1;
1760 path->slots[level] = orig_slot;
925baedd
CM
1761 if (mid) {
1762 btrfs_tree_unlock(mid);
5f39d397 1763 free_extent_buffer(mid);
925baedd 1764 }
bb803951 1765 } else {
5f39d397 1766 orig_slot -= btrfs_header_nritems(left);
bb803951
CM
1767 path->slots[level] = orig_slot;
1768 }
1769 }
79f95c82 1770 /* double check we haven't messed things up */
e20d96d6 1771 if (orig_ptr !=
5f39d397 1772 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
79f95c82 1773 BUG();
54aa1f4d 1774enospc:
925baedd
CM
1775 if (right) {
1776 btrfs_tree_unlock(right);
5f39d397 1777 free_extent_buffer(right);
925baedd
CM
1778 }
1779 if (left) {
1780 if (path->nodes[level] != left)
1781 btrfs_tree_unlock(left);
5f39d397 1782 free_extent_buffer(left);
925baedd 1783 }
bb803951
CM
1784 return ret;
1785}
1786
d352ac68
CM
1787/* Node balancing for insertion. Here we only split or push nodes around
1788 * when they are completely full. This is also done top down, so we
1789 * have to be pessimistic.
1790 */
d397712b 1791static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
98ed5174
CM
1792 struct btrfs_root *root,
1793 struct btrfs_path *path, int level)
e66f709b 1794{
5f39d397
CM
1795 struct extent_buffer *right = NULL;
1796 struct extent_buffer *mid;
1797 struct extent_buffer *left = NULL;
1798 struct extent_buffer *parent = NULL;
e66f709b
CM
1799 int ret = 0;
1800 int wret;
1801 int pslot;
1802 int orig_slot = path->slots[level];
e66f709b
CM
1803
1804 if (level == 0)
1805 return 1;
1806
5f39d397 1807 mid = path->nodes[level];
7bb86316 1808 WARN_ON(btrfs_header_generation(mid) != trans->transid);
e66f709b 1809
a05a9bb1 1810 if (level < BTRFS_MAX_LEVEL - 1) {
5f39d397 1811 parent = path->nodes[level + 1];
a05a9bb1
LZ
1812 pslot = path->slots[level + 1];
1813 }
e66f709b 1814
5f39d397 1815 if (!parent)
e66f709b 1816 return 1;
e66f709b 1817
5f39d397 1818 left = read_node_slot(root, parent, pslot - 1);
e66f709b
CM
1819
1820 /* first, try to make some room in the middle buffer */
5f39d397 1821 if (left) {
e66f709b 1822 u32 left_nr;
925baedd
CM
1823
1824 btrfs_tree_lock(left);
b4ce94de
CM
1825 btrfs_set_lock_blocking(left);
1826
5f39d397 1827 left_nr = btrfs_header_nritems(left);
33ade1f8
CM
1828 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1829 wret = 1;
1830 } else {
5f39d397 1831 ret = btrfs_cow_block(trans, root, left, parent,
9fa8cfe7 1832 pslot - 1, &left);
54aa1f4d
CM
1833 if (ret)
1834 wret = 1;
1835 else {
54aa1f4d 1836 wret = push_node_left(trans, root,
971a1f66 1837 left, mid, 0);
54aa1f4d 1838 }
33ade1f8 1839 }
e66f709b
CM
1840 if (wret < 0)
1841 ret = wret;
1842 if (wret == 0) {
5f39d397 1843 struct btrfs_disk_key disk_key;
e66f709b 1844 orig_slot += left_nr;
5f39d397 1845 btrfs_node_key(mid, &disk_key, 0);
f230475e
JS
1846 tree_mod_log_set_node_key(root->fs_info, parent,
1847 &disk_key, pslot, 0);
5f39d397
CM
1848 btrfs_set_node_key(parent, &disk_key, pslot);
1849 btrfs_mark_buffer_dirty(parent);
1850 if (btrfs_header_nritems(left) > orig_slot) {
1851 path->nodes[level] = left;
e66f709b
CM
1852 path->slots[level + 1] -= 1;
1853 path->slots[level] = orig_slot;
925baedd 1854 btrfs_tree_unlock(mid);
5f39d397 1855 free_extent_buffer(mid);
e66f709b
CM
1856 } else {
1857 orig_slot -=
5f39d397 1858 btrfs_header_nritems(left);
e66f709b 1859 path->slots[level] = orig_slot;
925baedd 1860 btrfs_tree_unlock(left);
5f39d397 1861 free_extent_buffer(left);
e66f709b 1862 }
e66f709b
CM
1863 return 0;
1864 }
925baedd 1865 btrfs_tree_unlock(left);
5f39d397 1866 free_extent_buffer(left);
e66f709b 1867 }
925baedd 1868 right = read_node_slot(root, parent, pslot + 1);
e66f709b
CM
1869
1870 /*
1871 * then try to empty the right most buffer into the middle
1872 */
5f39d397 1873 if (right) {
33ade1f8 1874 u32 right_nr;
b4ce94de 1875
925baedd 1876 btrfs_tree_lock(right);
b4ce94de
CM
1877 btrfs_set_lock_blocking(right);
1878
5f39d397 1879 right_nr = btrfs_header_nritems(right);
33ade1f8
CM
1880 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1881 wret = 1;
1882 } else {
5f39d397
CM
1883 ret = btrfs_cow_block(trans, root, right,
1884 parent, pslot + 1,
9fa8cfe7 1885 &right);
54aa1f4d
CM
1886 if (ret)
1887 wret = 1;
1888 else {
54aa1f4d 1889 wret = balance_node_right(trans, root,
5f39d397 1890 right, mid);
54aa1f4d 1891 }
33ade1f8 1892 }
e66f709b
CM
1893 if (wret < 0)
1894 ret = wret;
1895 if (wret == 0) {
5f39d397
CM
1896 struct btrfs_disk_key disk_key;
1897
1898 btrfs_node_key(right, &disk_key, 0);
f230475e
JS
1899 tree_mod_log_set_node_key(root->fs_info, parent,
1900 &disk_key, pslot + 1, 0);
5f39d397
CM
1901 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1902 btrfs_mark_buffer_dirty(parent);
1903
1904 if (btrfs_header_nritems(mid) <= orig_slot) {
1905 path->nodes[level] = right;
e66f709b
CM
1906 path->slots[level + 1] += 1;
1907 path->slots[level] = orig_slot -
5f39d397 1908 btrfs_header_nritems(mid);
925baedd 1909 btrfs_tree_unlock(mid);
5f39d397 1910 free_extent_buffer(mid);
e66f709b 1911 } else {
925baedd 1912 btrfs_tree_unlock(right);
5f39d397 1913 free_extent_buffer(right);
e66f709b 1914 }
e66f709b
CM
1915 return 0;
1916 }
925baedd 1917 btrfs_tree_unlock(right);
5f39d397 1918 free_extent_buffer(right);
e66f709b 1919 }
e66f709b
CM
1920 return 1;
1921}
1922
3c69faec 1923/*
d352ac68
CM
1924 * readahead one full node of leaves, finding things that are close
1925 * to the block in 'slot', and triggering ra on them.
3c69faec 1926 */
c8c42864
CM
1927static void reada_for_search(struct btrfs_root *root,
1928 struct btrfs_path *path,
1929 int level, int slot, u64 objectid)
3c69faec 1930{
5f39d397 1931 struct extent_buffer *node;
01f46658 1932 struct btrfs_disk_key disk_key;
3c69faec 1933 u32 nritems;
3c69faec 1934 u64 search;
a7175319 1935 u64 target;
6b80053d 1936 u64 nread = 0;
cb25c2ea 1937 u64 gen;
3c69faec 1938 int direction = path->reada;
5f39d397 1939 struct extent_buffer *eb;
6b80053d
CM
1940 u32 nr;
1941 u32 blocksize;
1942 u32 nscan = 0;
db94535d 1943
a6b6e75e 1944 if (level != 1)
6702ed49
CM
1945 return;
1946
1947 if (!path->nodes[level])
3c69faec
CM
1948 return;
1949
5f39d397 1950 node = path->nodes[level];
925baedd 1951
3c69faec 1952 search = btrfs_node_blockptr(node, slot);
6b80053d
CM
1953 blocksize = btrfs_level_size(root, level - 1);
1954 eb = btrfs_find_tree_block(root, search, blocksize);
5f39d397
CM
1955 if (eb) {
1956 free_extent_buffer(eb);
3c69faec
CM
1957 return;
1958 }
1959
a7175319 1960 target = search;
6b80053d 1961
5f39d397 1962 nritems = btrfs_header_nritems(node);
6b80053d 1963 nr = slot;
25b8b936 1964
d397712b 1965 while (1) {
6b80053d
CM
1966 if (direction < 0) {
1967 if (nr == 0)
1968 break;
1969 nr--;
1970 } else if (direction > 0) {
1971 nr++;
1972 if (nr >= nritems)
1973 break;
3c69faec 1974 }
01f46658
CM
1975 if (path->reada < 0 && objectid) {
1976 btrfs_node_key(node, &disk_key, nr);
1977 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1978 break;
1979 }
6b80053d 1980 search = btrfs_node_blockptr(node, nr);
a7175319
CM
1981 if ((search <= target && target - search <= 65536) ||
1982 (search > target && search - target <= 65536)) {
cb25c2ea 1983 gen = btrfs_node_ptr_generation(node, nr);
cb25c2ea 1984 readahead_tree_block(root, search, blocksize, gen);
6b80053d
CM
1985 nread += blocksize;
1986 }
1987 nscan++;
a7175319 1988 if ((nread > 65536 || nscan > 32))
6b80053d 1989 break;
3c69faec
CM
1990 }
1991}
925baedd 1992
b4ce94de
CM
1993/*
1994 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1995 * cache
1996 */
1997static noinline int reada_for_balance(struct btrfs_root *root,
1998 struct btrfs_path *path, int level)
1999{
2000 int slot;
2001 int nritems;
2002 struct extent_buffer *parent;
2003 struct extent_buffer *eb;
2004 u64 gen;
2005 u64 block1 = 0;
2006 u64 block2 = 0;
2007 int ret = 0;
2008 int blocksize;
2009
8c594ea8 2010 parent = path->nodes[level + 1];
b4ce94de
CM
2011 if (!parent)
2012 return 0;
2013
2014 nritems = btrfs_header_nritems(parent);
8c594ea8 2015 slot = path->slots[level + 1];
b4ce94de
CM
2016 blocksize = btrfs_level_size(root, level);
2017
2018 if (slot > 0) {
2019 block1 = btrfs_node_blockptr(parent, slot - 1);
2020 gen = btrfs_node_ptr_generation(parent, slot - 1);
2021 eb = btrfs_find_tree_block(root, block1, blocksize);
b9fab919
CM
2022 /*
2023 * if we get -eagain from btrfs_buffer_uptodate, we
2024 * don't want to return eagain here. That will loop
2025 * forever
2026 */
2027 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
b4ce94de
CM
2028 block1 = 0;
2029 free_extent_buffer(eb);
2030 }
8c594ea8 2031 if (slot + 1 < nritems) {
b4ce94de
CM
2032 block2 = btrfs_node_blockptr(parent, slot + 1);
2033 gen = btrfs_node_ptr_generation(parent, slot + 1);
2034 eb = btrfs_find_tree_block(root, block2, blocksize);
b9fab919 2035 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
b4ce94de
CM
2036 block2 = 0;
2037 free_extent_buffer(eb);
2038 }
2039 if (block1 || block2) {
2040 ret = -EAGAIN;
8c594ea8
CM
2041
2042 /* release the whole path */
b3b4aa74 2043 btrfs_release_path(path);
8c594ea8
CM
2044
2045 /* read the blocks */
b4ce94de
CM
2046 if (block1)
2047 readahead_tree_block(root, block1, blocksize, 0);
2048 if (block2)
2049 readahead_tree_block(root, block2, blocksize, 0);
2050
2051 if (block1) {
2052 eb = read_tree_block(root, block1, blocksize, 0);
2053 free_extent_buffer(eb);
2054 }
8c594ea8 2055 if (block2) {
b4ce94de
CM
2056 eb = read_tree_block(root, block2, blocksize, 0);
2057 free_extent_buffer(eb);
2058 }
2059 }
2060 return ret;
2061}
2062
2063
d352ac68 2064/*
d397712b
CM
2065 * when we walk down the tree, it is usually safe to unlock the higher layers
2066 * in the tree. The exceptions are when our path goes through slot 0, because
2067 * operations on the tree might require changing key pointers higher up in the
2068 * tree.
d352ac68 2069 *
d397712b
CM
2070 * callers might also have set path->keep_locks, which tells this code to keep
2071 * the lock if the path points to the last slot in the block. This is part of
2072 * walking through the tree, and selecting the next slot in the higher block.
d352ac68 2073 *
d397712b
CM
2074 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2075 * if lowest_unlock is 1, level 0 won't be unlocked
d352ac68 2076 */
e02119d5 2077static noinline void unlock_up(struct btrfs_path *path, int level,
f7c79f30
CM
2078 int lowest_unlock, int min_write_lock_level,
2079 int *write_lock_level)
925baedd
CM
2080{
2081 int i;
2082 int skip_level = level;
051e1b9f 2083 int no_skips = 0;
925baedd
CM
2084 struct extent_buffer *t;
2085
2086 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2087 if (!path->nodes[i])
2088 break;
2089 if (!path->locks[i])
2090 break;
051e1b9f 2091 if (!no_skips && path->slots[i] == 0) {
925baedd
CM
2092 skip_level = i + 1;
2093 continue;
2094 }
051e1b9f 2095 if (!no_skips && path->keep_locks) {
925baedd
CM
2096 u32 nritems;
2097 t = path->nodes[i];
2098 nritems = btrfs_header_nritems(t);
051e1b9f 2099 if (nritems < 1 || path->slots[i] >= nritems - 1) {
925baedd
CM
2100 skip_level = i + 1;
2101 continue;
2102 }
2103 }
051e1b9f
CM
2104 if (skip_level < i && i >= lowest_unlock)
2105 no_skips = 1;
2106
925baedd
CM
2107 t = path->nodes[i];
2108 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
bd681513 2109 btrfs_tree_unlock_rw(t, path->locks[i]);
925baedd 2110 path->locks[i] = 0;
f7c79f30
CM
2111 if (write_lock_level &&
2112 i > min_write_lock_level &&
2113 i <= *write_lock_level) {
2114 *write_lock_level = i - 1;
2115 }
925baedd
CM
2116 }
2117 }
2118}
2119
b4ce94de
CM
2120/*
2121 * This releases any locks held in the path starting at level and
2122 * going all the way up to the root.
2123 *
2124 * btrfs_search_slot will keep the lock held on higher nodes in a few
2125 * corner cases, such as COW of the block at slot zero in the node. This
2126 * ignores those rules, and it should only be called when there are no
2127 * more updates to be done higher up in the tree.
2128 */
2129noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2130{
2131 int i;
2132
5d4f98a2 2133 if (path->keep_locks)
b4ce94de
CM
2134 return;
2135
2136 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2137 if (!path->nodes[i])
12f4dacc 2138 continue;
b4ce94de 2139 if (!path->locks[i])
12f4dacc 2140 continue;
bd681513 2141 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
b4ce94de
CM
2142 path->locks[i] = 0;
2143 }
2144}
2145
c8c42864
CM
2146/*
2147 * helper function for btrfs_search_slot. The goal is to find a block
2148 * in cache without setting the path to blocking. If we find the block
2149 * we return zero and the path is unchanged.
2150 *
2151 * If we can't find the block, we set the path blocking and do some
2152 * reada. -EAGAIN is returned and the search must be repeated.
2153 */
2154static int
2155read_block_for_search(struct btrfs_trans_handle *trans,
2156 struct btrfs_root *root, struct btrfs_path *p,
2157 struct extent_buffer **eb_ret, int level, int slot,
5d9e75c4 2158 struct btrfs_key *key, u64 time_seq)
c8c42864
CM
2159{
2160 u64 blocknr;
2161 u64 gen;
2162 u32 blocksize;
2163 struct extent_buffer *b = *eb_ret;
2164 struct extent_buffer *tmp;
76a05b35 2165 int ret;
c8c42864
CM
2166
2167 blocknr = btrfs_node_blockptr(b, slot);
2168 gen = btrfs_node_ptr_generation(b, slot);
2169 blocksize = btrfs_level_size(root, level - 1);
2170
2171 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
cb44921a 2172 if (tmp) {
b9fab919
CM
2173 /* first we do an atomic uptodate check */
2174 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2175 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
cb44921a
CM
2176 /*
2177 * we found an up to date block without
2178 * sleeping, return
2179 * right away
2180 */
2181 *eb_ret = tmp;
2182 return 0;
2183 }
2184 /* the pages were up to date, but we failed
2185 * the generation number check. Do a full
2186 * read for the generation number that is correct.
2187 * We must do this without dropping locks so
2188 * we can trust our generation number
2189 */
2190 free_extent_buffer(tmp);
bd681513
CM
2191 btrfs_set_path_blocking(p);
2192
b9fab919 2193 /* now we're allowed to do a blocking uptodate check */
cb44921a 2194 tmp = read_tree_block(root, blocknr, blocksize, gen);
b9fab919 2195 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
cb44921a
CM
2196 *eb_ret = tmp;
2197 return 0;
2198 }
2199 free_extent_buffer(tmp);
b3b4aa74 2200 btrfs_release_path(p);
cb44921a
CM
2201 return -EIO;
2202 }
c8c42864
CM
2203 }
2204
2205 /*
2206 * reduce lock contention at high levels
2207 * of the btree by dropping locks before
76a05b35
CM
2208 * we read. Don't release the lock on the current
2209 * level because we need to walk this node to figure
2210 * out which blocks to read.
c8c42864 2211 */
8c594ea8
CM
2212 btrfs_unlock_up_safe(p, level + 1);
2213 btrfs_set_path_blocking(p);
2214
cb44921a 2215 free_extent_buffer(tmp);
c8c42864
CM
2216 if (p->reada)
2217 reada_for_search(root, p, level, slot, key->objectid);
2218
b3b4aa74 2219 btrfs_release_path(p);
76a05b35
CM
2220
2221 ret = -EAGAIN;
5bdd3536 2222 tmp = read_tree_block(root, blocknr, blocksize, 0);
76a05b35
CM
2223 if (tmp) {
2224 /*
2225 * If the read above didn't mark this buffer up to date,
2226 * it will never end up being up to date. Set ret to EIO now
2227 * and give up so that our caller doesn't loop forever
2228 * on our EAGAINs.
2229 */
b9fab919 2230 if (!btrfs_buffer_uptodate(tmp, 0, 0))
76a05b35 2231 ret = -EIO;
c8c42864 2232 free_extent_buffer(tmp);
76a05b35
CM
2233 }
2234 return ret;
c8c42864
CM
2235}
2236
2237/*
2238 * helper function for btrfs_search_slot. This does all of the checks
2239 * for node-level blocks and does any balancing required based on
2240 * the ins_len.
2241 *
2242 * If no extra work was required, zero is returned. If we had to
2243 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2244 * start over
2245 */
2246static int
2247setup_nodes_for_search(struct btrfs_trans_handle *trans,
2248 struct btrfs_root *root, struct btrfs_path *p,
bd681513
CM
2249 struct extent_buffer *b, int level, int ins_len,
2250 int *write_lock_level)
c8c42864
CM
2251{
2252 int ret;
2253 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2254 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2255 int sret;
2256
bd681513
CM
2257 if (*write_lock_level < level + 1) {
2258 *write_lock_level = level + 1;
2259 btrfs_release_path(p);
2260 goto again;
2261 }
2262
c8c42864
CM
2263 sret = reada_for_balance(root, p, level);
2264 if (sret)
2265 goto again;
2266
2267 btrfs_set_path_blocking(p);
2268 sret = split_node(trans, root, p, level);
bd681513 2269 btrfs_clear_path_blocking(p, NULL, 0);
c8c42864
CM
2270
2271 BUG_ON(sret > 0);
2272 if (sret) {
2273 ret = sret;
2274 goto done;
2275 }
2276 b = p->nodes[level];
2277 } else if (ins_len < 0 && btrfs_header_nritems(b) <
cfbb9308 2278 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
c8c42864
CM
2279 int sret;
2280
bd681513
CM
2281 if (*write_lock_level < level + 1) {
2282 *write_lock_level = level + 1;
2283 btrfs_release_path(p);
2284 goto again;
2285 }
2286
c8c42864
CM
2287 sret = reada_for_balance(root, p, level);
2288 if (sret)
2289 goto again;
2290
2291 btrfs_set_path_blocking(p);
2292 sret = balance_level(trans, root, p, level);
bd681513 2293 btrfs_clear_path_blocking(p, NULL, 0);
c8c42864
CM
2294
2295 if (sret) {
2296 ret = sret;
2297 goto done;
2298 }
2299 b = p->nodes[level];
2300 if (!b) {
b3b4aa74 2301 btrfs_release_path(p);
c8c42864
CM
2302 goto again;
2303 }
2304 BUG_ON(btrfs_header_nritems(b) == 1);
2305 }
2306 return 0;
2307
2308again:
2309 ret = -EAGAIN;
2310done:
2311 return ret;
2312}
2313
74123bd7
CM
2314/*
2315 * look for key in the tree. path is filled in with nodes along the way
2316 * if key is found, we return zero and you can find the item in the leaf
2317 * level of the path (level 0)
2318 *
2319 * If the key isn't found, the path points to the slot where it should
aa5d6bed
CM
2320 * be inserted, and 1 is returned. If there are other errors during the
2321 * search a negative error number is returned.
97571fd0
CM
2322 *
2323 * if ins_len > 0, nodes and leaves will be split as we walk down the
2324 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2325 * possible)
74123bd7 2326 */
e089f05c
CM
2327int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2328 *root, struct btrfs_key *key, struct btrfs_path *p, int
2329 ins_len, int cow)
be0e5c09 2330{
5f39d397 2331 struct extent_buffer *b;
be0e5c09
CM
2332 int slot;
2333 int ret;
33c66f43 2334 int err;
be0e5c09 2335 int level;
925baedd 2336 int lowest_unlock = 1;
bd681513
CM
2337 int root_lock;
2338 /* everything at write_lock_level or lower must be write locked */
2339 int write_lock_level = 0;
9f3a7427 2340 u8 lowest_level = 0;
f7c79f30 2341 int min_write_lock_level;
9f3a7427 2342
6702ed49 2343 lowest_level = p->lowest_level;
323ac95b 2344 WARN_ON(lowest_level && ins_len > 0);
22b0ebda 2345 WARN_ON(p->nodes[0] != NULL);
25179201 2346
bd681513 2347 if (ins_len < 0) {
925baedd 2348 lowest_unlock = 2;
65b51a00 2349
bd681513
CM
2350 /* when we are removing items, we might have to go up to level
2351 * two as we update tree pointers Make sure we keep write
2352 * for those levels as well
2353 */
2354 write_lock_level = 2;
2355 } else if (ins_len > 0) {
2356 /*
2357 * for inserting items, make sure we have a write lock on
2358 * level 1 so we can update keys
2359 */
2360 write_lock_level = 1;
2361 }
2362
2363 if (!cow)
2364 write_lock_level = -1;
2365
2366 if (cow && (p->keep_locks || p->lowest_level))
2367 write_lock_level = BTRFS_MAX_LEVEL;
2368
f7c79f30
CM
2369 min_write_lock_level = write_lock_level;
2370
bb803951 2371again:
bd681513
CM
2372 /*
2373 * we try very hard to do read locks on the root
2374 */
2375 root_lock = BTRFS_READ_LOCK;
2376 level = 0;
5d4f98a2 2377 if (p->search_commit_root) {
bd681513
CM
2378 /*
2379 * the commit roots are read only
2380 * so we always do read locks
2381 */
5d4f98a2
YZ
2382 b = root->commit_root;
2383 extent_buffer_get(b);
bd681513 2384 level = btrfs_header_level(b);
5d4f98a2 2385 if (!p->skip_locking)
bd681513 2386 btrfs_tree_read_lock(b);
5d4f98a2 2387 } else {
bd681513 2388 if (p->skip_locking) {
5d4f98a2 2389 b = btrfs_root_node(root);
bd681513
CM
2390 level = btrfs_header_level(b);
2391 } else {
2392 /* we don't know the level of the root node
2393 * until we actually have it read locked
2394 */
2395 b = btrfs_read_lock_root_node(root);
2396 level = btrfs_header_level(b);
2397 if (level <= write_lock_level) {
2398 /* whoops, must trade for write lock */
2399 btrfs_tree_read_unlock(b);
2400 free_extent_buffer(b);
2401 b = btrfs_lock_root_node(root);
2402 root_lock = BTRFS_WRITE_LOCK;
2403
2404 /* the level might have changed, check again */
2405 level = btrfs_header_level(b);
2406 }
2407 }
5d4f98a2 2408 }
bd681513
CM
2409 p->nodes[level] = b;
2410 if (!p->skip_locking)
2411 p->locks[level] = root_lock;
925baedd 2412
eb60ceac 2413 while (b) {
5f39d397 2414 level = btrfs_header_level(b);
65b51a00
CM
2415
2416 /*
2417 * setup the path here so we can release it under lock
2418 * contention with the cow code
2419 */
02217ed2 2420 if (cow) {
c8c42864
CM
2421 /*
2422 * if we don't really need to cow this block
2423 * then we don't want to set the path blocking,
2424 * so we test it here
2425 */
5d4f98a2 2426 if (!should_cow_block(trans, root, b))
65b51a00 2427 goto cow_done;
5d4f98a2 2428
b4ce94de
CM
2429 btrfs_set_path_blocking(p);
2430
bd681513
CM
2431 /*
2432 * must have write locks on this node and the
2433 * parent
2434 */
2435 if (level + 1 > write_lock_level) {
2436 write_lock_level = level + 1;
2437 btrfs_release_path(p);
2438 goto again;
2439 }
2440
33c66f43
YZ
2441 err = btrfs_cow_block(trans, root, b,
2442 p->nodes[level + 1],
2443 p->slots[level + 1], &b);
2444 if (err) {
33c66f43 2445 ret = err;
65b51a00 2446 goto done;
54aa1f4d 2447 }
02217ed2 2448 }
65b51a00 2449cow_done:
02217ed2 2450 BUG_ON(!cow && ins_len);
65b51a00 2451
eb60ceac 2452 p->nodes[level] = b;
bd681513 2453 btrfs_clear_path_blocking(p, NULL, 0);
b4ce94de
CM
2454
2455 /*
2456 * we have a lock on b and as long as we aren't changing
2457 * the tree, there is no way to for the items in b to change.
2458 * It is safe to drop the lock on our parent before we
2459 * go through the expensive btree search on b.
2460 *
2461 * If cow is true, then we might be changing slot zero,
2462 * which may require changing the parent. So, we can't
2463 * drop the lock until after we know which slot we're
2464 * operating on.
2465 */
2466 if (!cow)
2467 btrfs_unlock_up_safe(p, level + 1);
2468
5f39d397 2469 ret = bin_search(b, key, level, &slot);
b4ce94de 2470
5f39d397 2471 if (level != 0) {
33c66f43
YZ
2472 int dec = 0;
2473 if (ret && slot > 0) {
2474 dec = 1;
be0e5c09 2475 slot -= 1;
33c66f43 2476 }
be0e5c09 2477 p->slots[level] = slot;
33c66f43 2478 err = setup_nodes_for_search(trans, root, p, b, level,
bd681513 2479 ins_len, &write_lock_level);
33c66f43 2480 if (err == -EAGAIN)
c8c42864 2481 goto again;
33c66f43
YZ
2482 if (err) {
2483 ret = err;
c8c42864 2484 goto done;
33c66f43 2485 }
c8c42864
CM
2486 b = p->nodes[level];
2487 slot = p->slots[level];
b4ce94de 2488
bd681513
CM
2489 /*
2490 * slot 0 is special, if we change the key
2491 * we have to update the parent pointer
2492 * which means we must have a write lock
2493 * on the parent
2494 */
2495 if (slot == 0 && cow &&
2496 write_lock_level < level + 1) {
2497 write_lock_level = level + 1;
2498 btrfs_release_path(p);
2499 goto again;
2500 }
2501
f7c79f30
CM
2502 unlock_up(p, level, lowest_unlock,
2503 min_write_lock_level, &write_lock_level);
f9efa9c7 2504
925baedd 2505 if (level == lowest_level) {
33c66f43
YZ
2506 if (dec)
2507 p->slots[level]++;
5b21f2ed 2508 goto done;
925baedd 2509 }
ca7a79ad 2510
33c66f43 2511 err = read_block_for_search(trans, root, p,
5d9e75c4 2512 &b, level, slot, key, 0);
33c66f43 2513 if (err == -EAGAIN)
c8c42864 2514 goto again;
33c66f43
YZ
2515 if (err) {
2516 ret = err;
76a05b35 2517 goto done;
33c66f43 2518 }
76a05b35 2519
b4ce94de 2520 if (!p->skip_locking) {
bd681513
CM
2521 level = btrfs_header_level(b);
2522 if (level <= write_lock_level) {
2523 err = btrfs_try_tree_write_lock(b);
2524 if (!err) {
2525 btrfs_set_path_blocking(p);
2526 btrfs_tree_lock(b);
2527 btrfs_clear_path_blocking(p, b,
2528 BTRFS_WRITE_LOCK);
2529 }
2530 p->locks[level] = BTRFS_WRITE_LOCK;
2531 } else {
2532 err = btrfs_try_tree_read_lock(b);
2533 if (!err) {
2534 btrfs_set_path_blocking(p);
2535 btrfs_tree_read_lock(b);
2536 btrfs_clear_path_blocking(p, b,
2537 BTRFS_READ_LOCK);
2538 }
2539 p->locks[level] = BTRFS_READ_LOCK;
b4ce94de 2540 }
bd681513 2541 p->nodes[level] = b;
b4ce94de 2542 }
be0e5c09
CM
2543 } else {
2544 p->slots[level] = slot;
87b29b20
YZ
2545 if (ins_len > 0 &&
2546 btrfs_leaf_free_space(root, b) < ins_len) {
bd681513
CM
2547 if (write_lock_level < 1) {
2548 write_lock_level = 1;
2549 btrfs_release_path(p);
2550 goto again;
2551 }
2552
b4ce94de 2553 btrfs_set_path_blocking(p);
33c66f43
YZ
2554 err = split_leaf(trans, root, key,
2555 p, ins_len, ret == 0);
bd681513 2556 btrfs_clear_path_blocking(p, NULL, 0);
b4ce94de 2557
33c66f43
YZ
2558 BUG_ON(err > 0);
2559 if (err) {
2560 ret = err;
65b51a00
CM
2561 goto done;
2562 }
5c680ed6 2563 }
459931ec 2564 if (!p->search_for_split)
f7c79f30
CM
2565 unlock_up(p, level, lowest_unlock,
2566 min_write_lock_level, &write_lock_level);
65b51a00 2567 goto done;
be0e5c09
CM
2568 }
2569 }
65b51a00
CM
2570 ret = 1;
2571done:
b4ce94de
CM
2572 /*
2573 * we don't really know what they plan on doing with the path
2574 * from here on, so for now just mark it as blocking
2575 */
b9473439
CM
2576 if (!p->leave_spinning)
2577 btrfs_set_path_blocking(p);
76a05b35 2578 if (ret < 0)
b3b4aa74 2579 btrfs_release_path(p);
65b51a00 2580 return ret;
be0e5c09
CM
2581}
2582
5d9e75c4
JS
2583/*
2584 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2585 * current state of the tree together with the operations recorded in the tree
2586 * modification log to search for the key in a previous version of this tree, as
2587 * denoted by the time_seq parameter.
2588 *
2589 * Naturally, there is no support for insert, delete or cow operations.
2590 *
2591 * The resulting path and return value will be set up as if we called
2592 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2593 */
2594int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2595 struct btrfs_path *p, u64 time_seq)
2596{
2597 struct extent_buffer *b;
2598 int slot;
2599 int ret;
2600 int err;
2601 int level;
2602 int lowest_unlock = 1;
2603 u8 lowest_level = 0;
2604
2605 lowest_level = p->lowest_level;
2606 WARN_ON(p->nodes[0] != NULL);
2607
2608 if (p->search_commit_root) {
2609 BUG_ON(time_seq);
2610 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2611 }
2612
2613again:
5d9e75c4
JS
2614 b = get_old_root(root, time_seq);
2615 extent_buffer_get(b);
2616 level = btrfs_header_level(b);
2617 btrfs_tree_read_lock(b);
2618 p->locks[level] = BTRFS_READ_LOCK;
2619
2620 while (b) {
2621 level = btrfs_header_level(b);
2622 p->nodes[level] = b;
2623 btrfs_clear_path_blocking(p, NULL, 0);
2624
2625 /*
2626 * we have a lock on b and as long as we aren't changing
2627 * the tree, there is no way to for the items in b to change.
2628 * It is safe to drop the lock on our parent before we
2629 * go through the expensive btree search on b.
2630 */
2631 btrfs_unlock_up_safe(p, level + 1);
2632
2633 ret = bin_search(b, key, level, &slot);
2634
2635 if (level != 0) {
2636 int dec = 0;
2637 if (ret && slot > 0) {
2638 dec = 1;
2639 slot -= 1;
2640 }
2641 p->slots[level] = slot;
2642 unlock_up(p, level, lowest_unlock, 0, NULL);
2643
2644 if (level == lowest_level) {
2645 if (dec)
2646 p->slots[level]++;
2647 goto done;
2648 }
2649
2650 err = read_block_for_search(NULL, root, p, &b, level,
2651 slot, key, time_seq);
2652 if (err == -EAGAIN)
2653 goto again;
2654 if (err) {
2655 ret = err;
2656 goto done;
2657 }
2658
2659 level = btrfs_header_level(b);
2660 err = btrfs_try_tree_read_lock(b);
2661 if (!err) {
2662 btrfs_set_path_blocking(p);
2663 btrfs_tree_read_lock(b);
2664 btrfs_clear_path_blocking(p, b,
2665 BTRFS_READ_LOCK);
2666 }
2667 p->locks[level] = BTRFS_READ_LOCK;
2668 p->nodes[level] = b;
2669 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2670 if (b != p->nodes[level]) {
2671 btrfs_tree_unlock_rw(p->nodes[level],
2672 p->locks[level]);
2673 p->locks[level] = 0;
2674 p->nodes[level] = b;
2675 }
2676 } else {
2677 p->slots[level] = slot;
2678 unlock_up(p, level, lowest_unlock, 0, NULL);
2679 goto done;
2680 }
2681 }
2682 ret = 1;
2683done:
2684 if (!p->leave_spinning)
2685 btrfs_set_path_blocking(p);
2686 if (ret < 0)
2687 btrfs_release_path(p);
2688
2689 return ret;
2690}
2691
74123bd7
CM
2692/*
2693 * adjust the pointers going up the tree, starting at level
2694 * making sure the right key of each node is points to 'key'.
2695 * This is used after shifting pointers to the left, so it stops
2696 * fixing up pointers when a given leaf/node is not in slot 0 of the
2697 * higher levels
aa5d6bed 2698 *
74123bd7 2699 */
143bede5
JM
2700static void fixup_low_keys(struct btrfs_trans_handle *trans,
2701 struct btrfs_root *root, struct btrfs_path *path,
2702 struct btrfs_disk_key *key, int level)
be0e5c09
CM
2703{
2704 int i;
5f39d397
CM
2705 struct extent_buffer *t;
2706
234b63a0 2707 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
be0e5c09 2708 int tslot = path->slots[i];
eb60ceac 2709 if (!path->nodes[i])
be0e5c09 2710 break;
5f39d397 2711 t = path->nodes[i];
f230475e 2712 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
5f39d397 2713 btrfs_set_node_key(t, key, tslot);
d6025579 2714 btrfs_mark_buffer_dirty(path->nodes[i]);
be0e5c09
CM
2715 if (tslot != 0)
2716 break;
2717 }
2718}
2719
31840ae1
ZY
2720/*
2721 * update item key.
2722 *
2723 * This function isn't completely safe. It's the caller's responsibility
2724 * that the new key won't break the order
2725 */
143bede5
JM
2726void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2727 struct btrfs_root *root, struct btrfs_path *path,
2728 struct btrfs_key *new_key)
31840ae1
ZY
2729{
2730 struct btrfs_disk_key disk_key;
2731 struct extent_buffer *eb;
2732 int slot;
2733
2734 eb = path->nodes[0];
2735 slot = path->slots[0];
2736 if (slot > 0) {
2737 btrfs_item_key(eb, &disk_key, slot - 1);
143bede5 2738 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
31840ae1
ZY
2739 }
2740 if (slot < btrfs_header_nritems(eb) - 1) {
2741 btrfs_item_key(eb, &disk_key, slot + 1);
143bede5 2742 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
31840ae1
ZY
2743 }
2744
2745 btrfs_cpu_key_to_disk(&disk_key, new_key);
2746 btrfs_set_item_key(eb, &disk_key, slot);
2747 btrfs_mark_buffer_dirty(eb);
2748 if (slot == 0)
2749 fixup_low_keys(trans, root, path, &disk_key, 1);
31840ae1
ZY
2750}
2751
74123bd7
CM
2752/*
2753 * try to push data from one node into the next node left in the
79f95c82 2754 * tree.
aa5d6bed
CM
2755 *
2756 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2757 * error, and > 0 if there was no room in the left hand block.
74123bd7 2758 */
98ed5174
CM
2759static int push_node_left(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root, struct extent_buffer *dst,
971a1f66 2761 struct extent_buffer *src, int empty)
be0e5c09 2762{
be0e5c09 2763 int push_items = 0;
bb803951
CM
2764 int src_nritems;
2765 int dst_nritems;
aa5d6bed 2766 int ret = 0;
be0e5c09 2767
5f39d397
CM
2768 src_nritems = btrfs_header_nritems(src);
2769 dst_nritems = btrfs_header_nritems(dst);
123abc88 2770 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
7bb86316
CM
2771 WARN_ON(btrfs_header_generation(src) != trans->transid);
2772 WARN_ON(btrfs_header_generation(dst) != trans->transid);
54aa1f4d 2773
bce4eae9 2774 if (!empty && src_nritems <= 8)
971a1f66
CM
2775 return 1;
2776
d397712b 2777 if (push_items <= 0)
be0e5c09
CM
2778 return 1;
2779
bce4eae9 2780 if (empty) {
971a1f66 2781 push_items = min(src_nritems, push_items);
bce4eae9
CM
2782 if (push_items < src_nritems) {
2783 /* leave at least 8 pointers in the node if
2784 * we aren't going to empty it
2785 */
2786 if (src_nritems - push_items < 8) {
2787 if (push_items <= 8)
2788 return 1;
2789 push_items -= 8;
2790 }
2791 }
2792 } else
2793 push_items = min(src_nritems - 8, push_items);
79f95c82 2794
f230475e
JS
2795 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2796 push_items);
5f39d397
CM
2797 copy_extent_buffer(dst, src,
2798 btrfs_node_key_ptr_offset(dst_nritems),
2799 btrfs_node_key_ptr_offset(0),
d397712b 2800 push_items * sizeof(struct btrfs_key_ptr));
5f39d397 2801
bb803951 2802 if (push_items < src_nritems) {
f230475e
JS
2803 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2804 src_nritems - push_items);
5f39d397
CM
2805 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2806 btrfs_node_key_ptr_offset(push_items),
2807 (src_nritems - push_items) *
2808 sizeof(struct btrfs_key_ptr));
2809 }
2810 btrfs_set_header_nritems(src, src_nritems - push_items);
2811 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2812 btrfs_mark_buffer_dirty(src);
2813 btrfs_mark_buffer_dirty(dst);
31840ae1 2814
79f95c82
CM
2815 return ret;
2816}
2817
2818/*
2819 * try to push data from one node into the next node right in the
2820 * tree.
2821 *
2822 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2823 * error, and > 0 if there was no room in the right hand block.
2824 *
2825 * this will only push up to 1/2 the contents of the left node over
2826 */
5f39d397
CM
2827static int balance_node_right(struct btrfs_trans_handle *trans,
2828 struct btrfs_root *root,
2829 struct extent_buffer *dst,
2830 struct extent_buffer *src)
79f95c82 2831{
79f95c82
CM
2832 int push_items = 0;
2833 int max_push;
2834 int src_nritems;
2835 int dst_nritems;
2836 int ret = 0;
79f95c82 2837
7bb86316
CM
2838 WARN_ON(btrfs_header_generation(src) != trans->transid);
2839 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2840
5f39d397
CM
2841 src_nritems = btrfs_header_nritems(src);
2842 dst_nritems = btrfs_header_nritems(dst);
123abc88 2843 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
d397712b 2844 if (push_items <= 0)
79f95c82 2845 return 1;
bce4eae9 2846
d397712b 2847 if (src_nritems < 4)
bce4eae9 2848 return 1;
79f95c82
CM
2849
2850 max_push = src_nritems / 2 + 1;
2851 /* don't try to empty the node */
d397712b 2852 if (max_push >= src_nritems)
79f95c82 2853 return 1;
252c38f0 2854
79f95c82
CM
2855 if (max_push < push_items)
2856 push_items = max_push;
2857
f230475e 2858 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
5f39d397
CM
2859 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2860 btrfs_node_key_ptr_offset(0),
2861 (dst_nritems) *
2862 sizeof(struct btrfs_key_ptr));
d6025579 2863
f230475e
JS
2864 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2865 src_nritems - push_items, push_items);
5f39d397
CM
2866 copy_extent_buffer(dst, src,
2867 btrfs_node_key_ptr_offset(0),
2868 btrfs_node_key_ptr_offset(src_nritems - push_items),
d397712b 2869 push_items * sizeof(struct btrfs_key_ptr));
79f95c82 2870
5f39d397
CM
2871 btrfs_set_header_nritems(src, src_nritems - push_items);
2872 btrfs_set_header_nritems(dst, dst_nritems + push_items);
79f95c82 2873
5f39d397
CM
2874 btrfs_mark_buffer_dirty(src);
2875 btrfs_mark_buffer_dirty(dst);
31840ae1 2876
aa5d6bed 2877 return ret;
be0e5c09
CM
2878}
2879
97571fd0
CM
2880/*
2881 * helper function to insert a new root level in the tree.
2882 * A new node is allocated, and a single item is inserted to
2883 * point to the existing root
aa5d6bed
CM
2884 *
2885 * returns zero on success or < 0 on failure.
97571fd0 2886 */
d397712b 2887static noinline int insert_new_root(struct btrfs_trans_handle *trans,
5f39d397
CM
2888 struct btrfs_root *root,
2889 struct btrfs_path *path, int level)
5c680ed6 2890{
7bb86316 2891 u64 lower_gen;
5f39d397
CM
2892 struct extent_buffer *lower;
2893 struct extent_buffer *c;
925baedd 2894 struct extent_buffer *old;
5f39d397 2895 struct btrfs_disk_key lower_key;
5c680ed6
CM
2896
2897 BUG_ON(path->nodes[level]);
2898 BUG_ON(path->nodes[level-1] != root->node);
2899
7bb86316
CM
2900 lower = path->nodes[level-1];
2901 if (level == 1)
2902 btrfs_item_key(lower, &lower_key, 0);
2903 else
2904 btrfs_node_key(lower, &lower_key, 0);
2905
31840ae1 2906 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
5d4f98a2 2907 root->root_key.objectid, &lower_key,
5581a51a 2908 level, root->node->start, 0);
5f39d397
CM
2909 if (IS_ERR(c))
2910 return PTR_ERR(c);
925baedd 2911
f0486c68
YZ
2912 root_add_used(root, root->nodesize);
2913
5d4f98a2 2914 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
5f39d397
CM
2915 btrfs_set_header_nritems(c, 1);
2916 btrfs_set_header_level(c, level);
db94535d 2917 btrfs_set_header_bytenr(c, c->start);
5f39d397 2918 btrfs_set_header_generation(c, trans->transid);
5d4f98a2 2919 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
5f39d397 2920 btrfs_set_header_owner(c, root->root_key.objectid);
5f39d397
CM
2921
2922 write_extent_buffer(c, root->fs_info->fsid,
2923 (unsigned long)btrfs_header_fsid(c),
2924 BTRFS_FSID_SIZE);
e17cade2
CM
2925
2926 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2927 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2928 BTRFS_UUID_SIZE);
2929
5f39d397 2930 btrfs_set_node_key(c, &lower_key, 0);
db94535d 2931 btrfs_set_node_blockptr(c, 0, lower->start);
7bb86316 2932 lower_gen = btrfs_header_generation(lower);
31840ae1 2933 WARN_ON(lower_gen != trans->transid);
7bb86316
CM
2934
2935 btrfs_set_node_ptr_generation(c, 0, lower_gen);
d5719762 2936
5f39d397 2937 btrfs_mark_buffer_dirty(c);
d5719762 2938
925baedd 2939 old = root->node;
f230475e 2940 tree_mod_log_set_root_pointer(root, c);
240f62c8 2941 rcu_assign_pointer(root->node, c);
925baedd
CM
2942
2943 /* the super has an extra ref to root->node */
2944 free_extent_buffer(old);
2945
0b86a832 2946 add_root_to_dirty_list(root);
5f39d397
CM
2947 extent_buffer_get(c);
2948 path->nodes[level] = c;
bd681513 2949 path->locks[level] = BTRFS_WRITE_LOCK;
5c680ed6
CM
2950 path->slots[level] = 0;
2951 return 0;
2952}
2953
74123bd7
CM
2954/*
2955 * worker function to insert a single pointer in a node.
2956 * the node should have enough room for the pointer already
97571fd0 2957 *
74123bd7
CM
2958 * slot and level indicate where you want the key to go, and
2959 * blocknr is the block the key points to.
2960 */
143bede5
JM
2961static void insert_ptr(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root, struct btrfs_path *path,
2963 struct btrfs_disk_key *key, u64 bytenr,
f3ea38da 2964 int slot, int level, int tree_mod_log)
74123bd7 2965{
5f39d397 2966 struct extent_buffer *lower;
74123bd7 2967 int nritems;
f3ea38da 2968 int ret;
5c680ed6
CM
2969
2970 BUG_ON(!path->nodes[level]);
f0486c68 2971 btrfs_assert_tree_locked(path->nodes[level]);
5f39d397
CM
2972 lower = path->nodes[level];
2973 nritems = btrfs_header_nritems(lower);
c293498b 2974 BUG_ON(slot > nritems);
143bede5 2975 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
74123bd7 2976 if (slot != nritems) {
f3ea38da
JS
2977 if (tree_mod_log && level)
2978 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2979 slot, nritems - slot);
5f39d397
CM
2980 memmove_extent_buffer(lower,
2981 btrfs_node_key_ptr_offset(slot + 1),
2982 btrfs_node_key_ptr_offset(slot),
d6025579 2983 (nritems - slot) * sizeof(struct btrfs_key_ptr));
74123bd7 2984 }
f3ea38da
JS
2985 if (tree_mod_log && level) {
2986 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2987 MOD_LOG_KEY_ADD);
2988 BUG_ON(ret < 0);
2989 }
5f39d397 2990 btrfs_set_node_key(lower, key, slot);
db94535d 2991 btrfs_set_node_blockptr(lower, slot, bytenr);
74493f7a
CM
2992 WARN_ON(trans->transid == 0);
2993 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
5f39d397
CM
2994 btrfs_set_header_nritems(lower, nritems + 1);
2995 btrfs_mark_buffer_dirty(lower);
74123bd7
CM
2996}
2997
97571fd0
CM
2998/*
2999 * split the node at the specified level in path in two.
3000 * The path is corrected to point to the appropriate node after the split
3001 *
3002 * Before splitting this tries to make some room in the node by pushing
3003 * left and right, if either one works, it returns right away.
aa5d6bed
CM
3004 *
3005 * returns 0 on success and < 0 on failure
97571fd0 3006 */
e02119d5
CM
3007static noinline int split_node(struct btrfs_trans_handle *trans,
3008 struct btrfs_root *root,
3009 struct btrfs_path *path, int level)
be0e5c09 3010{
5f39d397
CM
3011 struct extent_buffer *c;
3012 struct extent_buffer *split;
3013 struct btrfs_disk_key disk_key;
be0e5c09 3014 int mid;
5c680ed6 3015 int ret;
7518a238 3016 u32 c_nritems;
eb60ceac 3017
5f39d397 3018 c = path->nodes[level];
7bb86316 3019 WARN_ON(btrfs_header_generation(c) != trans->transid);
5f39d397 3020 if (c == root->node) {
5c680ed6 3021 /* trying to split the root, lets make a new one */
e089f05c 3022 ret = insert_new_root(trans, root, path, level + 1);
5c680ed6
CM
3023 if (ret)
3024 return ret;
b3612421 3025 } else {
e66f709b 3026 ret = push_nodes_for_insert(trans, root, path, level);
5f39d397
CM
3027 c = path->nodes[level];
3028 if (!ret && btrfs_header_nritems(c) <
c448acf0 3029 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
e66f709b 3030 return 0;
54aa1f4d
CM
3031 if (ret < 0)
3032 return ret;
be0e5c09 3033 }
e66f709b 3034
5f39d397 3035 c_nritems = btrfs_header_nritems(c);
5d4f98a2
YZ
3036 mid = (c_nritems + 1) / 2;
3037 btrfs_node_key(c, &disk_key, mid);
7bb86316 3038
5d4f98a2 3039 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
31840ae1 3040 root->root_key.objectid,
5581a51a 3041 &disk_key, level, c->start, 0);
5f39d397
CM
3042 if (IS_ERR(split))
3043 return PTR_ERR(split);
3044
f0486c68
YZ
3045 root_add_used(root, root->nodesize);
3046
5d4f98a2 3047 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
5f39d397 3048 btrfs_set_header_level(split, btrfs_header_level(c));
db94535d 3049 btrfs_set_header_bytenr(split, split->start);
5f39d397 3050 btrfs_set_header_generation(split, trans->transid);
5d4f98a2 3051 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
5f39d397
CM
3052 btrfs_set_header_owner(split, root->root_key.objectid);
3053 write_extent_buffer(split, root->fs_info->fsid,
3054 (unsigned long)btrfs_header_fsid(split),
3055 BTRFS_FSID_SIZE);
e17cade2
CM
3056 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3057 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3058 BTRFS_UUID_SIZE);
54aa1f4d 3059
f230475e 3060 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
5f39d397
CM
3061 copy_extent_buffer(split, c,
3062 btrfs_node_key_ptr_offset(0),
3063 btrfs_node_key_ptr_offset(mid),
3064 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3065 btrfs_set_header_nritems(split, c_nritems - mid);
3066 btrfs_set_header_nritems(c, mid);
aa5d6bed
CM
3067 ret = 0;
3068
5f39d397
CM
3069 btrfs_mark_buffer_dirty(c);
3070 btrfs_mark_buffer_dirty(split);
3071
143bede5 3072 insert_ptr(trans, root, path, &disk_key, split->start,
f3ea38da 3073 path->slots[level + 1] + 1, level + 1, 1);
aa5d6bed 3074
5de08d7d 3075 if (path->slots[level] >= mid) {
5c680ed6 3076 path->slots[level] -= mid;
925baedd 3077 btrfs_tree_unlock(c);
5f39d397
CM
3078 free_extent_buffer(c);
3079 path->nodes[level] = split;
5c680ed6
CM
3080 path->slots[level + 1] += 1;
3081 } else {
925baedd 3082 btrfs_tree_unlock(split);
5f39d397 3083 free_extent_buffer(split);
be0e5c09 3084 }
aa5d6bed 3085 return ret;
be0e5c09
CM
3086}
3087
74123bd7
CM
3088/*
3089 * how many bytes are required to store the items in a leaf. start
3090 * and nr indicate which items in the leaf to check. This totals up the
3091 * space used both by the item structs and the item data
3092 */
5f39d397 3093static int leaf_space_used(struct extent_buffer *l, int start, int nr)
be0e5c09
CM
3094{
3095 int data_len;
5f39d397 3096 int nritems = btrfs_header_nritems(l);
d4dbff95 3097 int end = min(nritems, start + nr) - 1;
be0e5c09
CM
3098
3099 if (!nr)
3100 return 0;
5f39d397
CM
3101 data_len = btrfs_item_end_nr(l, start);
3102 data_len = data_len - btrfs_item_offset_nr(l, end);
0783fcfc 3103 data_len += sizeof(struct btrfs_item) * nr;
d4dbff95 3104 WARN_ON(data_len < 0);
be0e5c09
CM
3105 return data_len;
3106}
3107
d4dbff95
CM
3108/*
3109 * The space between the end of the leaf items and
3110 * the start of the leaf data. IOW, how much room
3111 * the leaf has left for both items and data
3112 */
d397712b 3113noinline int btrfs_leaf_free_space(struct btrfs_root *root,
e02119d5 3114 struct extent_buffer *leaf)
d4dbff95 3115{
5f39d397
CM
3116 int nritems = btrfs_header_nritems(leaf);
3117 int ret;
3118 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3119 if (ret < 0) {
d397712b
CM
3120 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3121 "used %d nritems %d\n",
ae2f5411 3122 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
5f39d397
CM
3123 leaf_space_used(leaf, 0, nritems), nritems);
3124 }
3125 return ret;
d4dbff95
CM
3126}
3127
99d8f83c
CM
3128/*
3129 * min slot controls the lowest index we're willing to push to the
3130 * right. We'll push up to and including min_slot, but no lower
3131 */
44871b1b
CM
3132static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3133 struct btrfs_root *root,
3134 struct btrfs_path *path,
3135 int data_size, int empty,
3136 struct extent_buffer *right,
99d8f83c
CM
3137 int free_space, u32 left_nritems,
3138 u32 min_slot)
00ec4c51 3139{
5f39d397 3140 struct extent_buffer *left = path->nodes[0];
44871b1b 3141 struct extent_buffer *upper = path->nodes[1];
cfed81a0 3142 struct btrfs_map_token token;
5f39d397 3143 struct btrfs_disk_key disk_key;
00ec4c51 3144 int slot;
34a38218 3145 u32 i;
00ec4c51
CM
3146 int push_space = 0;
3147 int push_items = 0;
0783fcfc 3148 struct btrfs_item *item;
34a38218 3149 u32 nr;
7518a238 3150 u32 right_nritems;
5f39d397 3151 u32 data_end;
db94535d 3152 u32 this_item_size;
00ec4c51 3153
cfed81a0
CM
3154 btrfs_init_map_token(&token);
3155
34a38218
CM
3156 if (empty)
3157 nr = 0;
3158 else
99d8f83c 3159 nr = max_t(u32, 1, min_slot);
34a38218 3160
31840ae1 3161 if (path->slots[0] >= left_nritems)
87b29b20 3162 push_space += data_size;
31840ae1 3163
44871b1b 3164 slot = path->slots[1];
34a38218
CM
3165 i = left_nritems - 1;
3166 while (i >= nr) {
5f39d397 3167 item = btrfs_item_nr(left, i);
db94535d 3168
31840ae1
ZY
3169 if (!empty && push_items > 0) {
3170 if (path->slots[0] > i)
3171 break;
3172 if (path->slots[0] == i) {
3173 int space = btrfs_leaf_free_space(root, left);
3174 if (space + push_space * 2 > free_space)
3175 break;
3176 }
3177 }
3178
00ec4c51 3179 if (path->slots[0] == i)
87b29b20 3180 push_space += data_size;
db94535d 3181
db94535d
CM
3182 this_item_size = btrfs_item_size(left, item);
3183 if (this_item_size + sizeof(*item) + push_space > free_space)
00ec4c51 3184 break;
31840ae1 3185
00ec4c51 3186 push_items++;
db94535d 3187 push_space += this_item_size + sizeof(*item);
34a38218
CM
3188 if (i == 0)
3189 break;
3190 i--;
db94535d 3191 }
5f39d397 3192
925baedd
CM
3193 if (push_items == 0)
3194 goto out_unlock;
5f39d397 3195
34a38218 3196 if (!empty && push_items == left_nritems)
a429e513 3197 WARN_ON(1);
5f39d397 3198
00ec4c51 3199 /* push left to right */
5f39d397 3200 right_nritems = btrfs_header_nritems(right);
34a38218 3201
5f39d397 3202 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
123abc88 3203 push_space -= leaf_data_end(root, left);
5f39d397 3204
00ec4c51 3205 /* make room in the right data area */
5f39d397
CM
3206 data_end = leaf_data_end(root, right);
3207 memmove_extent_buffer(right,
3208 btrfs_leaf_data(right) + data_end - push_space,
3209 btrfs_leaf_data(right) + data_end,
3210 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3211
00ec4c51 3212 /* copy from the left data area */
5f39d397 3213 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
d6025579
CM
3214 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3215 btrfs_leaf_data(left) + leaf_data_end(root, left),
3216 push_space);
5f39d397
CM
3217
3218 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3219 btrfs_item_nr_offset(0),
3220 right_nritems * sizeof(struct btrfs_item));
3221
00ec4c51 3222 /* copy the items from left to right */
5f39d397
CM
3223 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3224 btrfs_item_nr_offset(left_nritems - push_items),
3225 push_items * sizeof(struct btrfs_item));
00ec4c51
CM
3226
3227 /* update the item pointers */
7518a238 3228 right_nritems += push_items;
5f39d397 3229 btrfs_set_header_nritems(right, right_nritems);
123abc88 3230 push_space = BTRFS_LEAF_DATA_SIZE(root);
7518a238 3231 for (i = 0; i < right_nritems; i++) {
5f39d397 3232 item = btrfs_item_nr(right, i);
cfed81a0
CM
3233 push_space -= btrfs_token_item_size(right, item, &token);
3234 btrfs_set_token_item_offset(right, item, push_space, &token);
db94535d
CM
3235 }
3236
7518a238 3237 left_nritems -= push_items;
5f39d397 3238 btrfs_set_header_nritems(left, left_nritems);
00ec4c51 3239
34a38218
CM
3240 if (left_nritems)
3241 btrfs_mark_buffer_dirty(left);
f0486c68
YZ
3242 else
3243 clean_tree_block(trans, root, left);
3244
5f39d397 3245 btrfs_mark_buffer_dirty(right);
a429e513 3246
5f39d397
CM
3247 btrfs_item_key(right, &disk_key, 0);
3248 btrfs_set_node_key(upper, &disk_key, slot + 1);
d6025579 3249 btrfs_mark_buffer_dirty(upper);
02217ed2 3250
00ec4c51 3251 /* then fixup the leaf pointer in the path */
7518a238
CM
3252 if (path->slots[0] >= left_nritems) {
3253 path->slots[0] -= left_nritems;
925baedd
CM
3254 if (btrfs_header_nritems(path->nodes[0]) == 0)
3255 clean_tree_block(trans, root, path->nodes[0]);
3256 btrfs_tree_unlock(path->nodes[0]);
5f39d397
CM
3257 free_extent_buffer(path->nodes[0]);
3258 path->nodes[0] = right;
00ec4c51
CM
3259 path->slots[1] += 1;
3260 } else {
925baedd 3261 btrfs_tree_unlock(right);
5f39d397 3262 free_extent_buffer(right);
00ec4c51
CM
3263 }
3264 return 0;
925baedd
CM
3265
3266out_unlock:
3267 btrfs_tree_unlock(right);
3268 free_extent_buffer(right);
3269 return 1;
00ec4c51 3270}
925baedd 3271
44871b1b
CM
3272/*
3273 * push some data in the path leaf to the right, trying to free up at
3274 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3275 *
3276 * returns 1 if the push failed because the other node didn't have enough
3277 * room, 0 if everything worked out and < 0 if there were major errors.
99d8f83c
CM
3278 *
3279 * this will push starting from min_slot to the end of the leaf. It won't
3280 * push any slot lower than min_slot
44871b1b
CM
3281 */
3282static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
99d8f83c
CM
3283 *root, struct btrfs_path *path,
3284 int min_data_size, int data_size,
3285 int empty, u32 min_slot)
44871b1b
CM
3286{
3287 struct extent_buffer *left = path->nodes[0];
3288 struct extent_buffer *right;
3289 struct extent_buffer *upper;
3290 int slot;
3291 int free_space;
3292 u32 left_nritems;
3293 int ret;
3294
3295 if (!path->nodes[1])
3296 return 1;
3297
3298 slot = path->slots[1];
3299 upper = path->nodes[1];
3300 if (slot >= btrfs_header_nritems(upper) - 1)
3301 return 1;
3302
3303 btrfs_assert_tree_locked(path->nodes[1]);
3304
3305 right = read_node_slot(root, upper, slot + 1);
91ca338d
TI
3306 if (right == NULL)
3307 return 1;
3308
44871b1b
CM
3309 btrfs_tree_lock(right);
3310 btrfs_set_lock_blocking(right);
3311
3312 free_space = btrfs_leaf_free_space(root, right);
3313 if (free_space < data_size)
3314 goto out_unlock;
3315
3316 /* cow and double check */
3317 ret = btrfs_cow_block(trans, root, right, upper,
3318 slot + 1, &right);
3319 if (ret)
3320 goto out_unlock;
3321
3322 free_space = btrfs_leaf_free_space(root, right);
3323 if (free_space < data_size)
3324 goto out_unlock;
3325
3326 left_nritems = btrfs_header_nritems(left);
3327 if (left_nritems == 0)
3328 goto out_unlock;
3329
99d8f83c
CM
3330 return __push_leaf_right(trans, root, path, min_data_size, empty,
3331 right, free_space, left_nritems, min_slot);
44871b1b
CM
3332out_unlock:
3333 btrfs_tree_unlock(right);
3334 free_extent_buffer(right);
3335 return 1;
3336}
3337
74123bd7
CM
3338/*
3339 * push some data in the path leaf to the left, trying to free up at
3340 * least data_size bytes. returns zero if the push worked, nonzero otherwise
99d8f83c
CM
3341 *
3342 * max_slot can put a limit on how far into the leaf we'll push items. The
3343 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3344 * items
74123bd7 3345 */
44871b1b
CM
3346static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3347 struct btrfs_root *root,
3348 struct btrfs_path *path, int data_size,
3349 int empty, struct extent_buffer *left,
99d8f83c
CM
3350 int free_space, u32 right_nritems,
3351 u32 max_slot)
be0e5c09 3352{
5f39d397
CM
3353 struct btrfs_disk_key disk_key;
3354 struct extent_buffer *right = path->nodes[0];
be0e5c09 3355 int i;
be0e5c09
CM
3356 int push_space = 0;
3357 int push_items = 0;
0783fcfc 3358 struct btrfs_item *item;
7518a238 3359 u32 old_left_nritems;
34a38218 3360 u32 nr;
aa5d6bed 3361 int ret = 0;
db94535d
CM
3362 u32 this_item_size;
3363 u32 old_left_item_size;
cfed81a0
CM
3364 struct btrfs_map_token token;
3365
3366 btrfs_init_map_token(&token);
be0e5c09 3367
34a38218 3368 if (empty)
99d8f83c 3369 nr = min(right_nritems, max_slot);
34a38218 3370 else
99d8f83c 3371 nr = min(right_nritems - 1, max_slot);
34a38218
CM
3372
3373 for (i = 0; i < nr; i++) {
5f39d397 3374 item = btrfs_item_nr(right, i);
db94535d 3375
31840ae1
ZY
3376 if (!empty && push_items > 0) {
3377 if (path->slots[0] < i)
3378 break;
3379 if (path->slots[0] == i) {
3380 int space = btrfs_leaf_free_space(root, right);
3381 if (space + push_space * 2 > free_space)
3382 break;
3383 }
3384 }
3385
be0e5c09 3386 if (path->slots[0] == i)
87b29b20 3387 push_space += data_size;
db94535d
CM
3388
3389 this_item_size = btrfs_item_size(right, item);
3390 if (this_item_size + sizeof(*item) + push_space > free_space)
be0e5c09 3391 break;
db94535d 3392
be0e5c09 3393 push_items++;
db94535d
CM
3394 push_space += this_item_size + sizeof(*item);
3395 }
3396
be0e5c09 3397 if (push_items == 0) {
925baedd
CM
3398 ret = 1;
3399 goto out;
be0e5c09 3400 }
34a38218 3401 if (!empty && push_items == btrfs_header_nritems(right))
a429e513 3402 WARN_ON(1);
5f39d397 3403
be0e5c09 3404 /* push data from right to left */
5f39d397
CM
3405 copy_extent_buffer(left, right,
3406 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3407 btrfs_item_nr_offset(0),
3408 push_items * sizeof(struct btrfs_item));
3409
123abc88 3410 push_space = BTRFS_LEAF_DATA_SIZE(root) -
d397712b 3411 btrfs_item_offset_nr(right, push_items - 1);
5f39d397
CM
3412
3413 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
d6025579
CM
3414 leaf_data_end(root, left) - push_space,
3415 btrfs_leaf_data(right) +
5f39d397 3416 btrfs_item_offset_nr(right, push_items - 1),
d6025579 3417 push_space);
5f39d397 3418 old_left_nritems = btrfs_header_nritems(left);
87b29b20 3419 BUG_ON(old_left_nritems <= 0);
eb60ceac 3420
db94535d 3421 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
0783fcfc 3422 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
5f39d397 3423 u32 ioff;
db94535d 3424
5f39d397 3425 item = btrfs_item_nr(left, i);
db94535d 3426
cfed81a0
CM
3427 ioff = btrfs_token_item_offset(left, item, &token);
3428 btrfs_set_token_item_offset(left, item,
3429 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3430 &token);
be0e5c09 3431 }
5f39d397 3432 btrfs_set_header_nritems(left, old_left_nritems + push_items);
be0e5c09
CM
3433
3434 /* fixup right node */
34a38218 3435 if (push_items > right_nritems) {
d397712b
CM
3436 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3437 right_nritems);
34a38218
CM
3438 WARN_ON(1);
3439 }
3440
3441 if (push_items < right_nritems) {
3442 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3443 leaf_data_end(root, right);
3444 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3445 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3446 btrfs_leaf_data(right) +
3447 leaf_data_end(root, right), push_space);
3448
3449 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
5f39d397
CM
3450 btrfs_item_nr_offset(push_items),
3451 (btrfs_header_nritems(right) - push_items) *
3452 sizeof(struct btrfs_item));
34a38218 3453 }
eef1c494
Y
3454 right_nritems -= push_items;
3455 btrfs_set_header_nritems(right, right_nritems);
123abc88 3456 push_space = BTRFS_LEAF_DATA_SIZE(root);
5f39d397
CM
3457 for (i = 0; i < right_nritems; i++) {
3458 item = btrfs_item_nr(right, i);
db94535d 3459
cfed81a0
CM
3460 push_space = push_space - btrfs_token_item_size(right,
3461 item, &token);
3462 btrfs_set_token_item_offset(right, item, push_space, &token);
db94535d 3463 }
eb60ceac 3464
5f39d397 3465 btrfs_mark_buffer_dirty(left);
34a38218
CM
3466 if (right_nritems)
3467 btrfs_mark_buffer_dirty(right);
f0486c68
YZ
3468 else
3469 clean_tree_block(trans, root, right);
098f59c2 3470
5f39d397 3471 btrfs_item_key(right, &disk_key, 0);
143bede5 3472 fixup_low_keys(trans, root, path, &disk_key, 1);
be0e5c09
CM
3473
3474 /* then fixup the leaf pointer in the path */
3475 if (path->slots[0] < push_items) {
3476 path->slots[0] += old_left_nritems;
925baedd 3477 btrfs_tree_unlock(path->nodes[0]);
5f39d397
CM
3478 free_extent_buffer(path->nodes[0]);
3479 path->nodes[0] = left;
be0e5c09
CM
3480 path->slots[1] -= 1;
3481 } else {
925baedd 3482 btrfs_tree_unlock(left);
5f39d397 3483 free_extent_buffer(left);
be0e5c09
CM
3484 path->slots[0] -= push_items;
3485 }
eb60ceac 3486 BUG_ON(path->slots[0] < 0);
aa5d6bed 3487 return ret;
925baedd
CM
3488out:
3489 btrfs_tree_unlock(left);
3490 free_extent_buffer(left);
3491 return ret;
be0e5c09
CM
3492}
3493
44871b1b
CM
3494/*
3495 * push some data in the path leaf to the left, trying to free up at
3496 * least data_size bytes. returns zero if the push worked, nonzero otherwise
99d8f83c
CM
3497 *
3498 * max_slot can put a limit on how far into the leaf we'll push items. The
3499 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3500 * items
44871b1b
CM
3501 */
3502static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
99d8f83c
CM
3503 *root, struct btrfs_path *path, int min_data_size,
3504 int data_size, int empty, u32 max_slot)
44871b1b
CM
3505{
3506 struct extent_buffer *right = path->nodes[0];
3507 struct extent_buffer *left;
3508 int slot;
3509 int free_space;
3510 u32 right_nritems;
3511 int ret = 0;
3512
3513 slot = path->slots[1];
3514 if (slot == 0)
3515 return 1;
3516 if (!path->nodes[1])
3517 return 1;
3518
3519 right_nritems = btrfs_header_nritems(right);
3520 if (right_nritems == 0)
3521 return 1;
3522
3523 btrfs_assert_tree_locked(path->nodes[1]);
3524
3525 left = read_node_slot(root, path->nodes[1], slot - 1);
91ca338d
TI
3526 if (left == NULL)
3527 return 1;
3528
44871b1b
CM
3529 btrfs_tree_lock(left);
3530 btrfs_set_lock_blocking(left);
3531
3532 free_space = btrfs_leaf_free_space(root, left);
3533 if (free_space < data_size) {
3534 ret = 1;
3535 goto out;
3536 }
3537
3538 /* cow and double check */
3539 ret = btrfs_cow_block(trans, root, left,
3540 path->nodes[1], slot - 1, &left);
3541 if (ret) {
3542 /* we hit -ENOSPC, but it isn't fatal here */
79787eaa
JM
3543 if (ret == -ENOSPC)
3544 ret = 1;
44871b1b
CM
3545 goto out;
3546 }
3547
3548 free_space = btrfs_leaf_free_space(root, left);
3549 if (free_space < data_size) {
3550 ret = 1;
3551 goto out;
3552 }
3553
99d8f83c
CM
3554 return __push_leaf_left(trans, root, path, min_data_size,
3555 empty, left, free_space, right_nritems,
3556 max_slot);
44871b1b
CM
3557out:
3558 btrfs_tree_unlock(left);
3559 free_extent_buffer(left);
3560 return ret;
3561}
3562
3563/*
3564 * split the path's leaf in two, making sure there is at least data_size
3565 * available for the resulting leaf level of the path.
44871b1b 3566 */
143bede5
JM
3567static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root,
3569 struct btrfs_path *path,
3570 struct extent_buffer *l,
3571 struct extent_buffer *right,
3572 int slot, int mid, int nritems)
44871b1b
CM
3573{
3574 int data_copy_size;
3575 int rt_data_off;
3576 int i;
44871b1b 3577 struct btrfs_disk_key disk_key;
cfed81a0
CM
3578 struct btrfs_map_token token;
3579
3580 btrfs_init_map_token(&token);
44871b1b
CM
3581
3582 nritems = nritems - mid;
3583 btrfs_set_header_nritems(right, nritems);
3584 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3585
3586 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3587 btrfs_item_nr_offset(mid),
3588 nritems * sizeof(struct btrfs_item));
3589
3590 copy_extent_buffer(right, l,
3591 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3592 data_copy_size, btrfs_leaf_data(l) +
3593 leaf_data_end(root, l), data_copy_size);
3594
3595 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3596 btrfs_item_end_nr(l, mid);
3597
3598 for (i = 0; i < nritems; i++) {
3599 struct btrfs_item *item = btrfs_item_nr(right, i);
3600 u32 ioff;
3601
cfed81a0
CM
3602 ioff = btrfs_token_item_offset(right, item, &token);
3603 btrfs_set_token_item_offset(right, item,
3604 ioff + rt_data_off, &token);
44871b1b
CM
3605 }
3606
44871b1b 3607 btrfs_set_header_nritems(l, mid);
44871b1b 3608 btrfs_item_key(right, &disk_key, 0);
143bede5 3609 insert_ptr(trans, root, path, &disk_key, right->start,
f3ea38da 3610 path->slots[1] + 1, 1, 0);
44871b1b
CM
3611
3612 btrfs_mark_buffer_dirty(right);
3613 btrfs_mark_buffer_dirty(l);
3614 BUG_ON(path->slots[0] != slot);
3615
44871b1b
CM
3616 if (mid <= slot) {
3617 btrfs_tree_unlock(path->nodes[0]);
3618 free_extent_buffer(path->nodes[0]);
3619 path->nodes[0] = right;
3620 path->slots[0] -= mid;
3621 path->slots[1] += 1;
3622 } else {
3623 btrfs_tree_unlock(right);
3624 free_extent_buffer(right);
3625 }
3626
3627 BUG_ON(path->slots[0] < 0);
44871b1b
CM
3628}
3629
99d8f83c
CM
3630/*
3631 * double splits happen when we need to insert a big item in the middle
3632 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3633 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3634 * A B C
3635 *
3636 * We avoid this by trying to push the items on either side of our target
3637 * into the adjacent leaves. If all goes well we can avoid the double split
3638 * completely.
3639 */
3640static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3641 struct btrfs_root *root,
3642 struct btrfs_path *path,
3643 int data_size)
3644{
3645 int ret;
3646 int progress = 0;
3647 int slot;
3648 u32 nritems;
3649
3650 slot = path->slots[0];
3651
3652 /*
3653 * try to push all the items after our slot into the
3654 * right leaf
3655 */
3656 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3657 if (ret < 0)
3658 return ret;
3659
3660 if (ret == 0)
3661 progress++;
3662
3663 nritems = btrfs_header_nritems(path->nodes[0]);
3664 /*
3665 * our goal is to get our slot at the start or end of a leaf. If
3666 * we've done so we're done
3667 */
3668 if (path->slots[0] == 0 || path->slots[0] == nritems)
3669 return 0;
3670
3671 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3672 return 0;
3673
3674 /* try to push all the items before our slot into the next leaf */
3675 slot = path->slots[0];
3676 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3677 if (ret < 0)
3678 return ret;
3679
3680 if (ret == 0)
3681 progress++;
3682
3683 if (progress)
3684 return 0;
3685 return 1;
3686}
3687
74123bd7
CM
3688/*
3689 * split the path's leaf in two, making sure there is at least data_size
3690 * available for the resulting leaf level of the path.
aa5d6bed
CM
3691 *
3692 * returns 0 if all went well and < 0 on failure.
74123bd7 3693 */
e02119d5
CM
3694static noinline int split_leaf(struct btrfs_trans_handle *trans,
3695 struct btrfs_root *root,
3696 struct btrfs_key *ins_key,
3697 struct btrfs_path *path, int data_size,
3698 int extend)
be0e5c09 3699{
5d4f98a2 3700 struct btrfs_disk_key disk_key;
5f39d397 3701 struct extent_buffer *l;
7518a238 3702 u32 nritems;
eb60ceac
CM
3703 int mid;
3704 int slot;
5f39d397 3705 struct extent_buffer *right;
d4dbff95 3706 int ret = 0;
aa5d6bed 3707 int wret;
5d4f98a2 3708 int split;
cc0c5538 3709 int num_doubles = 0;
99d8f83c 3710 int tried_avoid_double = 0;
aa5d6bed 3711
a5719521
YZ
3712 l = path->nodes[0];
3713 slot = path->slots[0];
3714 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3715 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3716 return -EOVERFLOW;
3717
40689478 3718 /* first try to make some room by pushing left and right */
99d8f83c
CM
3719 if (data_size) {
3720 wret = push_leaf_right(trans, root, path, data_size,
3721 data_size, 0, 0);
d397712b 3722 if (wret < 0)
eaee50e8 3723 return wret;
3685f791 3724 if (wret) {
99d8f83c
CM
3725 wret = push_leaf_left(trans, root, path, data_size,
3726 data_size, 0, (u32)-1);
3685f791
CM
3727 if (wret < 0)
3728 return wret;
3729 }
3730 l = path->nodes[0];
aa5d6bed 3731
3685f791 3732 /* did the pushes work? */
87b29b20 3733 if (btrfs_leaf_free_space(root, l) >= data_size)
3685f791 3734 return 0;
3326d1b0 3735 }
aa5d6bed 3736
5c680ed6 3737 if (!path->nodes[1]) {
e089f05c 3738 ret = insert_new_root(trans, root, path, 1);
5c680ed6
CM
3739 if (ret)
3740 return ret;
3741 }
cc0c5538 3742again:
5d4f98a2 3743 split = 1;
cc0c5538 3744 l = path->nodes[0];
eb60ceac 3745 slot = path->slots[0];
5f39d397 3746 nritems = btrfs_header_nritems(l);
d397712b 3747 mid = (nritems + 1) / 2;
54aa1f4d 3748
5d4f98a2
YZ
3749 if (mid <= slot) {
3750 if (nritems == 1 ||
3751 leaf_space_used(l, mid, nritems - mid) + data_size >
3752 BTRFS_LEAF_DATA_SIZE(root)) {
3753 if (slot >= nritems) {
3754 split = 0;
3755 } else {
3756 mid = slot;
3757 if (mid != nritems &&
3758 leaf_space_used(l, mid, nritems - mid) +
3759 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
99d8f83c
CM
3760 if (data_size && !tried_avoid_double)
3761 goto push_for_double;
5d4f98a2
YZ
3762 split = 2;
3763 }
3764 }
3765 }
3766 } else {
3767 if (leaf_space_used(l, 0, mid) + data_size >
3768 BTRFS_LEAF_DATA_SIZE(root)) {
3769 if (!extend && data_size && slot == 0) {
3770 split = 0;
3771 } else if ((extend || !data_size) && slot == 0) {
3772 mid = 1;
3773 } else {
3774 mid = slot;
3775 if (mid != nritems &&
3776 leaf_space_used(l, mid, nritems - mid) +
3777 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
99d8f83c
CM
3778 if (data_size && !tried_avoid_double)
3779 goto push_for_double;
5d4f98a2
YZ
3780 split = 2 ;
3781 }
3782 }
3783 }
3784 }
3785
3786 if (split == 0)
3787 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3788 else
3789 btrfs_item_key(l, &disk_key, mid);
3790
3791 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
31840ae1 3792 root->root_key.objectid,
5581a51a 3793 &disk_key, 0, l->start, 0);
f0486c68 3794 if (IS_ERR(right))
5f39d397 3795 return PTR_ERR(right);
f0486c68
YZ
3796
3797 root_add_used(root, root->leafsize);
5f39d397
CM
3798
3799 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
db94535d 3800 btrfs_set_header_bytenr(right, right->start);
5f39d397 3801 btrfs_set_header_generation(right, trans->transid);
5d4f98a2 3802 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
5f39d397
CM
3803 btrfs_set_header_owner(right, root->root_key.objectid);
3804 btrfs_set_header_level(right, 0);
3805 write_extent_buffer(right, root->fs_info->fsid,
3806 (unsigned long)btrfs_header_fsid(right),
3807 BTRFS_FSID_SIZE);
e17cade2
CM
3808
3809 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3810 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3811 BTRFS_UUID_SIZE);
44871b1b 3812
5d4f98a2
YZ
3813 if (split == 0) {
3814 if (mid <= slot) {
3815 btrfs_set_header_nritems(right, 0);
143bede5 3816 insert_ptr(trans, root, path, &disk_key, right->start,
f3ea38da 3817 path->slots[1] + 1, 1, 0);
5d4f98a2
YZ
3818 btrfs_tree_unlock(path->nodes[0]);
3819 free_extent_buffer(path->nodes[0]);
3820 path->nodes[0] = right;
3821 path->slots[0] = 0;
3822 path->slots[1] += 1;
3823 } else {
3824 btrfs_set_header_nritems(right, 0);
143bede5 3825 insert_ptr(trans, root, path, &disk_key, right->start,
f3ea38da 3826 path->slots[1], 1, 0);
5d4f98a2
YZ
3827 btrfs_tree_unlock(path->nodes[0]);
3828 free_extent_buffer(path->nodes[0]);
3829 path->nodes[0] = right;
3830 path->slots[0] = 0;
143bede5
JM
3831 if (path->slots[1] == 0)
3832 fixup_low_keys(trans, root, path,
3833 &disk_key, 1);
d4dbff95 3834 }
5d4f98a2
YZ
3835 btrfs_mark_buffer_dirty(right);
3836 return ret;
d4dbff95 3837 }
74123bd7 3838
143bede5 3839 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
31840ae1 3840
5d4f98a2 3841 if (split == 2) {
cc0c5538
CM
3842 BUG_ON(num_doubles != 0);
3843 num_doubles++;
3844 goto again;
a429e513 3845 }
44871b1b 3846
143bede5 3847 return 0;
99d8f83c
CM
3848
3849push_for_double:
3850 push_for_double_split(trans, root, path, data_size);
3851 tried_avoid_double = 1;
3852 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3853 return 0;
3854 goto again;
be0e5c09
CM
3855}
3856
ad48fd75
YZ
3857static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root,
3859 struct btrfs_path *path, int ins_len)
459931ec 3860{
ad48fd75 3861 struct btrfs_key key;
459931ec 3862 struct extent_buffer *leaf;
ad48fd75
YZ
3863 struct btrfs_file_extent_item *fi;
3864 u64 extent_len = 0;
3865 u32 item_size;
3866 int ret;
459931ec
CM
3867
3868 leaf = path->nodes[0];
ad48fd75
YZ
3869 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3870
3871 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3872 key.type != BTRFS_EXTENT_CSUM_KEY);
3873
3874 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3875 return 0;
459931ec
CM
3876
3877 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
ad48fd75
YZ
3878 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3879 fi = btrfs_item_ptr(leaf, path->slots[0],
3880 struct btrfs_file_extent_item);
3881 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3882 }
b3b4aa74 3883 btrfs_release_path(path);
459931ec 3884
459931ec 3885 path->keep_locks = 1;
ad48fd75
YZ
3886 path->search_for_split = 1;
3887 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
459931ec 3888 path->search_for_split = 0;
ad48fd75
YZ
3889 if (ret < 0)
3890 goto err;
459931ec 3891
ad48fd75
YZ
3892 ret = -EAGAIN;
3893 leaf = path->nodes[0];
459931ec 3894 /* if our item isn't there or got smaller, return now */
ad48fd75
YZ
3895 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3896 goto err;
3897
109f6aef
CM
3898 /* the leaf has changed, it now has room. return now */
3899 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3900 goto err;
3901
ad48fd75
YZ
3902 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3903 fi = btrfs_item_ptr(leaf, path->slots[0],
3904 struct btrfs_file_extent_item);
3905 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3906 goto err;
459931ec
CM
3907 }
3908
b9473439 3909 btrfs_set_path_blocking(path);
ad48fd75 3910 ret = split_leaf(trans, root, &key, path, ins_len, 1);
f0486c68
YZ
3911 if (ret)
3912 goto err;
459931ec 3913
ad48fd75 3914 path->keep_locks = 0;
b9473439 3915 btrfs_unlock_up_safe(path, 1);
ad48fd75
YZ
3916 return 0;
3917err:
3918 path->keep_locks = 0;
3919 return ret;
3920}
3921
3922static noinline int split_item(struct btrfs_trans_handle *trans,
3923 struct btrfs_root *root,
3924 struct btrfs_path *path,
3925 struct btrfs_key *new_key,
3926 unsigned long split_offset)
3927{
3928 struct extent_buffer *leaf;
3929 struct btrfs_item *item;
3930 struct btrfs_item *new_item;
3931 int slot;
3932 char *buf;
3933 u32 nritems;
3934 u32 item_size;
3935 u32 orig_offset;
3936 struct btrfs_disk_key disk_key;
3937
b9473439
CM
3938 leaf = path->nodes[0];
3939 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3940
b4ce94de
CM
3941 btrfs_set_path_blocking(path);
3942
459931ec
CM
3943 item = btrfs_item_nr(leaf, path->slots[0]);
3944 orig_offset = btrfs_item_offset(leaf, item);
3945 item_size = btrfs_item_size(leaf, item);
3946
459931ec 3947 buf = kmalloc(item_size, GFP_NOFS);
ad48fd75
YZ
3948 if (!buf)
3949 return -ENOMEM;
3950
459931ec
CM
3951 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3952 path->slots[0]), item_size);
459931ec 3953
ad48fd75 3954 slot = path->slots[0] + 1;
459931ec 3955 nritems = btrfs_header_nritems(leaf);
459931ec
CM
3956 if (slot != nritems) {
3957 /* shift the items */
3958 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
ad48fd75
YZ
3959 btrfs_item_nr_offset(slot),
3960 (nritems - slot) * sizeof(struct btrfs_item));
459931ec
CM
3961 }
3962
3963 btrfs_cpu_key_to_disk(&disk_key, new_key);
3964 btrfs_set_item_key(leaf, &disk_key, slot);
3965
3966 new_item = btrfs_item_nr(leaf, slot);
3967
3968 btrfs_set_item_offset(leaf, new_item, orig_offset);
3969 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3970
3971 btrfs_set_item_offset(leaf, item,
3972 orig_offset + item_size - split_offset);
3973 btrfs_set_item_size(leaf, item, split_offset);
3974
3975 btrfs_set_header_nritems(leaf, nritems + 1);
3976
3977 /* write the data for the start of the original item */
3978 write_extent_buffer(leaf, buf,
3979 btrfs_item_ptr_offset(leaf, path->slots[0]),
3980 split_offset);
3981
3982 /* write the data for the new item */
3983 write_extent_buffer(leaf, buf + split_offset,
3984 btrfs_item_ptr_offset(leaf, slot),
3985 item_size - split_offset);
3986 btrfs_mark_buffer_dirty(leaf);
3987
ad48fd75 3988 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
459931ec 3989 kfree(buf);
ad48fd75
YZ
3990 return 0;
3991}
3992
3993/*
3994 * This function splits a single item into two items,
3995 * giving 'new_key' to the new item and splitting the
3996 * old one at split_offset (from the start of the item).
3997 *
3998 * The path may be released by this operation. After
3999 * the split, the path is pointing to the old item. The
4000 * new item is going to be in the same node as the old one.
4001 *
4002 * Note, the item being split must be smaller enough to live alone on
4003 * a tree block with room for one extra struct btrfs_item
4004 *
4005 * This allows us to split the item in place, keeping a lock on the
4006 * leaf the entire time.
4007 */
4008int btrfs_split_item(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 struct btrfs_path *path,
4011 struct btrfs_key *new_key,
4012 unsigned long split_offset)
4013{
4014 int ret;
4015 ret = setup_leaf_for_split(trans, root, path,
4016 sizeof(struct btrfs_item));
4017 if (ret)
4018 return ret;
4019
4020 ret = split_item(trans, root, path, new_key, split_offset);
459931ec
CM
4021 return ret;
4022}
4023
ad48fd75
YZ
4024/*
4025 * This function duplicate a item, giving 'new_key' to the new item.
4026 * It guarantees both items live in the same tree leaf and the new item
4027 * is contiguous with the original item.
4028 *
4029 * This allows us to split file extent in place, keeping a lock on the
4030 * leaf the entire time.
4031 */
4032int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct btrfs_path *path,
4035 struct btrfs_key *new_key)
4036{
4037 struct extent_buffer *leaf;
4038 int ret;
4039 u32 item_size;
4040
4041 leaf = path->nodes[0];
4042 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4043 ret = setup_leaf_for_split(trans, root, path,
4044 item_size + sizeof(struct btrfs_item));
4045 if (ret)
4046 return ret;
4047
4048 path->slots[0]++;
143bede5
JM
4049 setup_items_for_insert(trans, root, path, new_key, &item_size,
4050 item_size, item_size +
4051 sizeof(struct btrfs_item), 1);
ad48fd75
YZ
4052 leaf = path->nodes[0];
4053 memcpy_extent_buffer(leaf,
4054 btrfs_item_ptr_offset(leaf, path->slots[0]),
4055 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4056 item_size);
4057 return 0;
4058}
4059
d352ac68
CM
4060/*
4061 * make the item pointed to by the path smaller. new_size indicates
4062 * how small to make it, and from_end tells us if we just chop bytes
4063 * off the end of the item or if we shift the item to chop bytes off
4064 * the front.
4065 */
143bede5
JM
4066void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *root,
4068 struct btrfs_path *path,
4069 u32 new_size, int from_end)
b18c6685 4070{
b18c6685 4071 int slot;
5f39d397
CM
4072 struct extent_buffer *leaf;
4073 struct btrfs_item *item;
b18c6685
CM
4074 u32 nritems;
4075 unsigned int data_end;
4076 unsigned int old_data_start;
4077 unsigned int old_size;
4078 unsigned int size_diff;
4079 int i;
cfed81a0
CM
4080 struct btrfs_map_token token;
4081
4082 btrfs_init_map_token(&token);
b18c6685 4083
5f39d397 4084 leaf = path->nodes[0];
179e29e4
CM
4085 slot = path->slots[0];
4086
4087 old_size = btrfs_item_size_nr(leaf, slot);
4088 if (old_size == new_size)
143bede5 4089 return;
b18c6685 4090
5f39d397 4091 nritems = btrfs_header_nritems(leaf);
b18c6685
CM
4092 data_end = leaf_data_end(root, leaf);
4093
5f39d397 4094 old_data_start = btrfs_item_offset_nr(leaf, slot);
179e29e4 4095
b18c6685
CM
4096 size_diff = old_size - new_size;
4097
4098 BUG_ON(slot < 0);
4099 BUG_ON(slot >= nritems);
4100
4101 /*
4102 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4103 */
4104 /* first correct the data pointers */
4105 for (i = slot; i < nritems; i++) {
5f39d397
CM
4106 u32 ioff;
4107 item = btrfs_item_nr(leaf, i);
db94535d 4108
cfed81a0
CM
4109 ioff = btrfs_token_item_offset(leaf, item, &token);
4110 btrfs_set_token_item_offset(leaf, item,
4111 ioff + size_diff, &token);
b18c6685 4112 }
db94535d 4113
b18c6685 4114 /* shift the data */
179e29e4
CM
4115 if (from_end) {
4116 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4117 data_end + size_diff, btrfs_leaf_data(leaf) +
4118 data_end, old_data_start + new_size - data_end);
4119 } else {
4120 struct btrfs_disk_key disk_key;
4121 u64 offset;
4122
4123 btrfs_item_key(leaf, &disk_key, slot);
4124
4125 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4126 unsigned long ptr;
4127 struct btrfs_file_extent_item *fi;
4128
4129 fi = btrfs_item_ptr(leaf, slot,
4130 struct btrfs_file_extent_item);
4131 fi = (struct btrfs_file_extent_item *)(
4132 (unsigned long)fi - size_diff);
4133
4134 if (btrfs_file_extent_type(leaf, fi) ==
4135 BTRFS_FILE_EXTENT_INLINE) {
4136 ptr = btrfs_item_ptr_offset(leaf, slot);
4137 memmove_extent_buffer(leaf, ptr,
d397712b
CM
4138 (unsigned long)fi,
4139 offsetof(struct btrfs_file_extent_item,
179e29e4
CM
4140 disk_bytenr));
4141 }
4142 }
4143
4144 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4145 data_end + size_diff, btrfs_leaf_data(leaf) +
4146 data_end, old_data_start - data_end);
4147
4148 offset = btrfs_disk_key_offset(&disk_key);
4149 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4150 btrfs_set_item_key(leaf, &disk_key, slot);
4151 if (slot == 0)
4152 fixup_low_keys(trans, root, path, &disk_key, 1);
4153 }
5f39d397
CM
4154
4155 item = btrfs_item_nr(leaf, slot);
4156 btrfs_set_item_size(leaf, item, new_size);
4157 btrfs_mark_buffer_dirty(leaf);
b18c6685 4158
5f39d397
CM
4159 if (btrfs_leaf_free_space(root, leaf) < 0) {
4160 btrfs_print_leaf(root, leaf);
b18c6685 4161 BUG();
5f39d397 4162 }
b18c6685
CM
4163}
4164
d352ac68
CM
4165/*
4166 * make the item pointed to by the path bigger, data_size is the new size.
4167 */
143bede5
JM
4168void btrfs_extend_item(struct btrfs_trans_handle *trans,
4169 struct btrfs_root *root, struct btrfs_path *path,
4170 u32 data_size)
6567e837 4171{
6567e837 4172 int slot;
5f39d397
CM
4173 struct extent_buffer *leaf;
4174 struct btrfs_item *item;
6567e837
CM
4175 u32 nritems;
4176 unsigned int data_end;
4177 unsigned int old_data;
4178 unsigned int old_size;
4179 int i;
cfed81a0
CM
4180 struct btrfs_map_token token;
4181
4182 btrfs_init_map_token(&token);
6567e837 4183
5f39d397 4184 leaf = path->nodes[0];
6567e837 4185
5f39d397 4186 nritems = btrfs_header_nritems(leaf);
6567e837
CM
4187 data_end = leaf_data_end(root, leaf);
4188
5f39d397
CM
4189 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4190 btrfs_print_leaf(root, leaf);
6567e837 4191 BUG();
5f39d397 4192 }
6567e837 4193 slot = path->slots[0];
5f39d397 4194 old_data = btrfs_item_end_nr(leaf, slot);
6567e837
CM
4195
4196 BUG_ON(slot < 0);
3326d1b0
CM
4197 if (slot >= nritems) {
4198 btrfs_print_leaf(root, leaf);
d397712b
CM
4199 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4200 slot, nritems);
3326d1b0
CM
4201 BUG_ON(1);
4202 }
6567e837
CM
4203
4204 /*
4205 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4206 */
4207 /* first correct the data pointers */
4208 for (i = slot; i < nritems; i++) {
5f39d397
CM
4209 u32 ioff;
4210 item = btrfs_item_nr(leaf, i);
db94535d 4211
cfed81a0
CM
4212 ioff = btrfs_token_item_offset(leaf, item, &token);
4213 btrfs_set_token_item_offset(leaf, item,
4214 ioff - data_size, &token);
6567e837 4215 }
5f39d397 4216
6567e837 4217 /* shift the data */
5f39d397 4218 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
6567e837
CM
4219 data_end - data_size, btrfs_leaf_data(leaf) +
4220 data_end, old_data - data_end);
5f39d397 4221
6567e837 4222 data_end = old_data;
5f39d397
CM
4223 old_size = btrfs_item_size_nr(leaf, slot);
4224 item = btrfs_item_nr(leaf, slot);
4225 btrfs_set_item_size(leaf, item, old_size + data_size);
4226 btrfs_mark_buffer_dirty(leaf);
6567e837 4227
5f39d397
CM
4228 if (btrfs_leaf_free_space(root, leaf) < 0) {
4229 btrfs_print_leaf(root, leaf);
6567e837 4230 BUG();
5f39d397 4231 }
6567e837
CM
4232}
4233
f3465ca4
JB
4234/*
4235 * Given a key and some data, insert items into the tree.
4236 * This does all the path init required, making room in the tree if needed.
4237 * Returns the number of keys that were inserted.
4238 */
4239int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4240 struct btrfs_root *root,
4241 struct btrfs_path *path,
4242 struct btrfs_key *cpu_key, u32 *data_size,
4243 int nr)
4244{
4245 struct extent_buffer *leaf;
4246 struct btrfs_item *item;
4247 int ret = 0;
4248 int slot;
f3465ca4
JB
4249 int i;
4250 u32 nritems;
4251 u32 total_data = 0;
4252 u32 total_size = 0;
4253 unsigned int data_end;
4254 struct btrfs_disk_key disk_key;
4255 struct btrfs_key found_key;
cfed81a0
CM
4256 struct btrfs_map_token token;
4257
4258 btrfs_init_map_token(&token);
f3465ca4 4259
87b29b20
YZ
4260 for (i = 0; i < nr; i++) {
4261 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4262 BTRFS_LEAF_DATA_SIZE(root)) {
4263 break;
4264 nr = i;
4265 }
f3465ca4 4266 total_data += data_size[i];
87b29b20
YZ
4267 total_size += data_size[i] + sizeof(struct btrfs_item);
4268 }
4269 BUG_ON(nr == 0);
f3465ca4 4270
f3465ca4
JB
4271 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4272 if (ret == 0)
4273 return -EEXIST;
4274 if (ret < 0)
4275 goto out;
4276
f3465ca4
JB
4277 leaf = path->nodes[0];
4278
4279 nritems = btrfs_header_nritems(leaf);
4280 data_end = leaf_data_end(root, leaf);
4281
4282 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4283 for (i = nr; i >= 0; i--) {
4284 total_data -= data_size[i];
4285 total_size -= data_size[i] + sizeof(struct btrfs_item);
4286 if (total_size < btrfs_leaf_free_space(root, leaf))
4287 break;
4288 }
4289 nr = i;
4290 }
4291
4292 slot = path->slots[0];
4293 BUG_ON(slot < 0);
4294
4295 if (slot != nritems) {
4296 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4297
4298 item = btrfs_item_nr(leaf, slot);
4299 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4300
4301 /* figure out how many keys we can insert in here */
4302 total_data = data_size[0];
4303 for (i = 1; i < nr; i++) {
5d4f98a2 4304 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
f3465ca4
JB
4305 break;
4306 total_data += data_size[i];
4307 }
4308 nr = i;
4309
4310 if (old_data < data_end) {
4311 btrfs_print_leaf(root, leaf);
d397712b 4312 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
f3465ca4
JB
4313 slot, old_data, data_end);
4314 BUG_ON(1);
4315 }
4316 /*
4317 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4318 */
4319 /* first correct the data pointers */
f3465ca4
JB
4320 for (i = slot; i < nritems; i++) {
4321 u32 ioff;
4322
4323 item = btrfs_item_nr(leaf, i);
cfed81a0
CM
4324 ioff = btrfs_token_item_offset(leaf, item, &token);
4325 btrfs_set_token_item_offset(leaf, item,
4326 ioff - total_data, &token);
f3465ca4 4327 }
f3465ca4
JB
4328 /* shift the items */
4329 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4330 btrfs_item_nr_offset(slot),
4331 (nritems - slot) * sizeof(struct btrfs_item));
4332
4333 /* shift the data */
4334 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4335 data_end - total_data, btrfs_leaf_data(leaf) +
4336 data_end, old_data - data_end);
4337 data_end = old_data;
4338 } else {
4339 /*
4340 * this sucks but it has to be done, if we are inserting at
4341 * the end of the leaf only insert 1 of the items, since we
4342 * have no way of knowing whats on the next leaf and we'd have
4343 * to drop our current locks to figure it out
4344 */
4345 nr = 1;
4346 }
4347
4348 /* setup the item for the new data */
4349 for (i = 0; i < nr; i++) {
4350 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4351 btrfs_set_item_key(leaf, &disk_key, slot + i);
4352 item = btrfs_item_nr(leaf, slot + i);
cfed81a0
CM
4353 btrfs_set_token_item_offset(leaf, item,
4354 data_end - data_size[i], &token);
f3465ca4 4355 data_end -= data_size[i];
cfed81a0 4356 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
f3465ca4
JB
4357 }
4358 btrfs_set_header_nritems(leaf, nritems + nr);
4359 btrfs_mark_buffer_dirty(leaf);
4360
4361 ret = 0;
4362 if (slot == 0) {
4363 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
143bede5 4364 fixup_low_keys(trans, root, path, &disk_key, 1);
f3465ca4
JB
4365 }
4366
4367 if (btrfs_leaf_free_space(root, leaf) < 0) {
4368 btrfs_print_leaf(root, leaf);
4369 BUG();
4370 }
4371out:
4372 if (!ret)
4373 ret = nr;
4374 return ret;
4375}
4376
74123bd7 4377/*
44871b1b
CM
4378 * this is a helper for btrfs_insert_empty_items, the main goal here is
4379 * to save stack depth by doing the bulk of the work in a function
4380 * that doesn't call btrfs_search_slot
74123bd7 4381 */
143bede5
JM
4382void setup_items_for_insert(struct btrfs_trans_handle *trans,
4383 struct btrfs_root *root, struct btrfs_path *path,
4384 struct btrfs_key *cpu_key, u32 *data_size,
4385 u32 total_data, u32 total_size, int nr)
be0e5c09 4386{
5f39d397 4387 struct btrfs_item *item;
9c58309d 4388 int i;
7518a238 4389 u32 nritems;
be0e5c09 4390 unsigned int data_end;
e2fa7227 4391 struct btrfs_disk_key disk_key;
44871b1b
CM
4392 struct extent_buffer *leaf;
4393 int slot;
cfed81a0
CM
4394 struct btrfs_map_token token;
4395
4396 btrfs_init_map_token(&token);
e2fa7227 4397
5f39d397 4398 leaf = path->nodes[0];
44871b1b 4399 slot = path->slots[0];
74123bd7 4400
5f39d397 4401 nritems = btrfs_header_nritems(leaf);
123abc88 4402 data_end = leaf_data_end(root, leaf);
eb60ceac 4403
f25956cc 4404 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3326d1b0 4405 btrfs_print_leaf(root, leaf);
d397712b 4406 printk(KERN_CRIT "not enough freespace need %u have %d\n",
9c58309d 4407 total_size, btrfs_leaf_free_space(root, leaf));
be0e5c09 4408 BUG();
d4dbff95 4409 }
5f39d397 4410
be0e5c09 4411 if (slot != nritems) {
5f39d397 4412 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
be0e5c09 4413
5f39d397
CM
4414 if (old_data < data_end) {
4415 btrfs_print_leaf(root, leaf);
d397712b 4416 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
5f39d397
CM
4417 slot, old_data, data_end);
4418 BUG_ON(1);
4419 }
be0e5c09
CM
4420 /*
4421 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4422 */
4423 /* first correct the data pointers */
0783fcfc 4424 for (i = slot; i < nritems; i++) {
5f39d397 4425 u32 ioff;
db94535d 4426
5f39d397 4427 item = btrfs_item_nr(leaf, i);
cfed81a0
CM
4428 ioff = btrfs_token_item_offset(leaf, item, &token);
4429 btrfs_set_token_item_offset(leaf, item,
4430 ioff - total_data, &token);
0783fcfc 4431 }
be0e5c09 4432 /* shift the items */
9c58309d 4433 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
5f39d397 4434 btrfs_item_nr_offset(slot),
d6025579 4435 (nritems - slot) * sizeof(struct btrfs_item));
be0e5c09
CM
4436
4437 /* shift the data */
5f39d397 4438 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
9c58309d 4439 data_end - total_data, btrfs_leaf_data(leaf) +
d6025579 4440 data_end, old_data - data_end);
be0e5c09
CM
4441 data_end = old_data;
4442 }
5f39d397 4443
62e2749e 4444 /* setup the item for the new data */
9c58309d
CM
4445 for (i = 0; i < nr; i++) {
4446 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4447 btrfs_set_item_key(leaf, &disk_key, slot + i);
4448 item = btrfs_item_nr(leaf, slot + i);
cfed81a0
CM
4449 btrfs_set_token_item_offset(leaf, item,
4450 data_end - data_size[i], &token);
9c58309d 4451 data_end -= data_size[i];
cfed81a0 4452 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
9c58309d 4453 }
44871b1b 4454
9c58309d 4455 btrfs_set_header_nritems(leaf, nritems + nr);
aa5d6bed 4456
5a01a2e3
CM
4457 if (slot == 0) {
4458 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
143bede5 4459 fixup_low_keys(trans, root, path, &disk_key, 1);
5a01a2e3 4460 }
b9473439
CM
4461 btrfs_unlock_up_safe(path, 1);
4462 btrfs_mark_buffer_dirty(leaf);
aa5d6bed 4463
5f39d397
CM
4464 if (btrfs_leaf_free_space(root, leaf) < 0) {
4465 btrfs_print_leaf(root, leaf);
be0e5c09 4466 BUG();
5f39d397 4467 }
44871b1b
CM
4468}
4469
4470/*
4471 * Given a key and some data, insert items into the tree.
4472 * This does all the path init required, making room in the tree if needed.
4473 */
4474int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4475 struct btrfs_root *root,
4476 struct btrfs_path *path,
4477 struct btrfs_key *cpu_key, u32 *data_size,
4478 int nr)
4479{
44871b1b
CM
4480 int ret = 0;
4481 int slot;
4482 int i;
4483 u32 total_size = 0;
4484 u32 total_data = 0;
4485
4486 for (i = 0; i < nr; i++)
4487 total_data += data_size[i];
4488
4489 total_size = total_data + (nr * sizeof(struct btrfs_item));
4490 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4491 if (ret == 0)
4492 return -EEXIST;
4493 if (ret < 0)
143bede5 4494 return ret;
44871b1b 4495
44871b1b
CM
4496 slot = path->slots[0];
4497 BUG_ON(slot < 0);
4498
143bede5 4499 setup_items_for_insert(trans, root, path, cpu_key, data_size,
44871b1b 4500 total_data, total_size, nr);
143bede5 4501 return 0;
62e2749e
CM
4502}
4503
4504/*
4505 * Given a key and some data, insert an item into the tree.
4506 * This does all the path init required, making room in the tree if needed.
4507 */
e089f05c
CM
4508int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4509 *root, struct btrfs_key *cpu_key, void *data, u32
4510 data_size)
62e2749e
CM
4511{
4512 int ret = 0;
2c90e5d6 4513 struct btrfs_path *path;
5f39d397
CM
4514 struct extent_buffer *leaf;
4515 unsigned long ptr;
62e2749e 4516
2c90e5d6 4517 path = btrfs_alloc_path();
db5b493a
TI
4518 if (!path)
4519 return -ENOMEM;
2c90e5d6 4520 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
62e2749e 4521 if (!ret) {
5f39d397
CM
4522 leaf = path->nodes[0];
4523 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4524 write_extent_buffer(leaf, data, ptr, data_size);
4525 btrfs_mark_buffer_dirty(leaf);
62e2749e 4526 }
2c90e5d6 4527 btrfs_free_path(path);
aa5d6bed 4528 return ret;
be0e5c09
CM
4529}
4530
74123bd7 4531/*
5de08d7d 4532 * delete the pointer from a given node.
74123bd7 4533 *
d352ac68
CM
4534 * the tree should have been previously balanced so the deletion does not
4535 * empty a node.
74123bd7 4536 */
143bede5 4537static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
f3ea38da
JS
4538 struct btrfs_path *path, int level, int slot,
4539 int tree_mod_log)
be0e5c09 4540{
5f39d397 4541 struct extent_buffer *parent = path->nodes[level];
7518a238 4542 u32 nritems;
f3ea38da 4543 int ret;
be0e5c09 4544
5f39d397 4545 nritems = btrfs_header_nritems(parent);
d397712b 4546 if (slot != nritems - 1) {
f3ea38da
JS
4547 if (tree_mod_log && level)
4548 tree_mod_log_eb_move(root->fs_info, parent, slot,
4549 slot + 1, nritems - slot - 1);
5f39d397
CM
4550 memmove_extent_buffer(parent,
4551 btrfs_node_key_ptr_offset(slot),
4552 btrfs_node_key_ptr_offset(slot + 1),
d6025579
CM
4553 sizeof(struct btrfs_key_ptr) *
4554 (nritems - slot - 1));
f395694c 4555 } else if (tree_mod_log && level) {
f3ea38da
JS
4556 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4557 MOD_LOG_KEY_REMOVE);
4558 BUG_ON(ret < 0);
bb803951 4559 }
f3ea38da 4560
7518a238 4561 nritems--;
5f39d397 4562 btrfs_set_header_nritems(parent, nritems);
7518a238 4563 if (nritems == 0 && parent == root->node) {
5f39d397 4564 BUG_ON(btrfs_header_level(root->node) != 1);
bb803951 4565 /* just turn the root into a leaf and break */
5f39d397 4566 btrfs_set_header_level(root->node, 0);
bb803951 4567 } else if (slot == 0) {
5f39d397
CM
4568 struct btrfs_disk_key disk_key;
4569
4570 btrfs_node_key(parent, &disk_key, 0);
143bede5 4571 fixup_low_keys(trans, root, path, &disk_key, level + 1);
be0e5c09 4572 }
d6025579 4573 btrfs_mark_buffer_dirty(parent);
be0e5c09
CM
4574}
4575
323ac95b
CM
4576/*
4577 * a helper function to delete the leaf pointed to by path->slots[1] and
5d4f98a2 4578 * path->nodes[1].
323ac95b
CM
4579 *
4580 * This deletes the pointer in path->nodes[1] and frees the leaf
4581 * block extent. zero is returned if it all worked out, < 0 otherwise.
4582 *
4583 * The path must have already been setup for deleting the leaf, including
4584 * all the proper balancing. path->nodes[1] must be locked.
4585 */
143bede5
JM
4586static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4587 struct btrfs_root *root,
4588 struct btrfs_path *path,
4589 struct extent_buffer *leaf)
323ac95b 4590{
5d4f98a2 4591 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
f3ea38da 4592 del_ptr(trans, root, path, 1, path->slots[1], 1);
323ac95b 4593
4d081c41
CM
4594 /*
4595 * btrfs_free_extent is expensive, we want to make sure we
4596 * aren't holding any locks when we call it
4597 */
4598 btrfs_unlock_up_safe(path, 0);
4599
f0486c68
YZ
4600 root_sub_used(root, leaf->len);
4601
3083ee2e 4602 extent_buffer_get(leaf);
5581a51a 4603 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3083ee2e 4604 free_extent_buffer_stale(leaf);
323ac95b 4605}
74123bd7
CM
4606/*
4607 * delete the item at the leaf level in path. If that empties
4608 * the leaf, remove it from the tree
4609 */
85e21bac
CM
4610int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4611 struct btrfs_path *path, int slot, int nr)
be0e5c09 4612{
5f39d397
CM
4613 struct extent_buffer *leaf;
4614 struct btrfs_item *item;
85e21bac
CM
4615 int last_off;
4616 int dsize = 0;
aa5d6bed
CM
4617 int ret = 0;
4618 int wret;
85e21bac 4619 int i;
7518a238 4620 u32 nritems;
cfed81a0
CM
4621 struct btrfs_map_token token;
4622
4623 btrfs_init_map_token(&token);
be0e5c09 4624
5f39d397 4625 leaf = path->nodes[0];
85e21bac
CM
4626 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4627
4628 for (i = 0; i < nr; i++)
4629 dsize += btrfs_item_size_nr(leaf, slot + i);
4630
5f39d397 4631 nritems = btrfs_header_nritems(leaf);
be0e5c09 4632
85e21bac 4633 if (slot + nr != nritems) {
123abc88 4634 int data_end = leaf_data_end(root, leaf);
5f39d397
CM
4635
4636 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
d6025579
CM
4637 data_end + dsize,
4638 btrfs_leaf_data(leaf) + data_end,
85e21bac 4639 last_off - data_end);
5f39d397 4640
85e21bac 4641 for (i = slot + nr; i < nritems; i++) {
5f39d397 4642 u32 ioff;
db94535d 4643
5f39d397 4644 item = btrfs_item_nr(leaf, i);
cfed81a0
CM
4645 ioff = btrfs_token_item_offset(leaf, item, &token);
4646 btrfs_set_token_item_offset(leaf, item,
4647 ioff + dsize, &token);
0783fcfc 4648 }
db94535d 4649
5f39d397 4650 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
85e21bac 4651 btrfs_item_nr_offset(slot + nr),
d6025579 4652 sizeof(struct btrfs_item) *
85e21bac 4653 (nritems - slot - nr));
be0e5c09 4654 }
85e21bac
CM
4655 btrfs_set_header_nritems(leaf, nritems - nr);
4656 nritems -= nr;
5f39d397 4657
74123bd7 4658 /* delete the leaf if we've emptied it */
7518a238 4659 if (nritems == 0) {
5f39d397
CM
4660 if (leaf == root->node) {
4661 btrfs_set_header_level(leaf, 0);
9a8dd150 4662 } else {
f0486c68
YZ
4663 btrfs_set_path_blocking(path);
4664 clean_tree_block(trans, root, leaf);
143bede5 4665 btrfs_del_leaf(trans, root, path, leaf);
9a8dd150 4666 }
be0e5c09 4667 } else {
7518a238 4668 int used = leaf_space_used(leaf, 0, nritems);
aa5d6bed 4669 if (slot == 0) {
5f39d397
CM
4670 struct btrfs_disk_key disk_key;
4671
4672 btrfs_item_key(leaf, &disk_key, 0);
143bede5 4673 fixup_low_keys(trans, root, path, &disk_key, 1);
aa5d6bed 4674 }
aa5d6bed 4675
74123bd7 4676 /* delete the leaf if it is mostly empty */
d717aa1d 4677 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
be0e5c09
CM
4678 /* push_leaf_left fixes the path.
4679 * make sure the path still points to our leaf
4680 * for possible call to del_ptr below
4681 */
4920c9ac 4682 slot = path->slots[1];
5f39d397
CM
4683 extent_buffer_get(leaf);
4684
b9473439 4685 btrfs_set_path_blocking(path);
99d8f83c
CM
4686 wret = push_leaf_left(trans, root, path, 1, 1,
4687 1, (u32)-1);
54aa1f4d 4688 if (wret < 0 && wret != -ENOSPC)
aa5d6bed 4689 ret = wret;
5f39d397
CM
4690
4691 if (path->nodes[0] == leaf &&
4692 btrfs_header_nritems(leaf)) {
99d8f83c
CM
4693 wret = push_leaf_right(trans, root, path, 1,
4694 1, 1, 0);
54aa1f4d 4695 if (wret < 0 && wret != -ENOSPC)
aa5d6bed
CM
4696 ret = wret;
4697 }
5f39d397
CM
4698
4699 if (btrfs_header_nritems(leaf) == 0) {
323ac95b 4700 path->slots[1] = slot;
143bede5 4701 btrfs_del_leaf(trans, root, path, leaf);
5f39d397 4702 free_extent_buffer(leaf);
143bede5 4703 ret = 0;
5de08d7d 4704 } else {
925baedd
CM
4705 /* if we're still in the path, make sure
4706 * we're dirty. Otherwise, one of the
4707 * push_leaf functions must have already
4708 * dirtied this buffer
4709 */
4710 if (path->nodes[0] == leaf)
4711 btrfs_mark_buffer_dirty(leaf);
5f39d397 4712 free_extent_buffer(leaf);
be0e5c09 4713 }
d5719762 4714 } else {
5f39d397 4715 btrfs_mark_buffer_dirty(leaf);
be0e5c09
CM
4716 }
4717 }
aa5d6bed 4718 return ret;
be0e5c09
CM
4719}
4720
7bb86316 4721/*
925baedd 4722 * search the tree again to find a leaf with lesser keys
7bb86316
CM
4723 * returns 0 if it found something or 1 if there are no lesser leaves.
4724 * returns < 0 on io errors.
d352ac68
CM
4725 *
4726 * This may release the path, and so you may lose any locks held at the
4727 * time you call it.
7bb86316
CM
4728 */
4729int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4730{
925baedd
CM
4731 struct btrfs_key key;
4732 struct btrfs_disk_key found_key;
4733 int ret;
7bb86316 4734
925baedd 4735 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
7bb86316 4736
925baedd
CM
4737 if (key.offset > 0)
4738 key.offset--;
4739 else if (key.type > 0)
4740 key.type--;
4741 else if (key.objectid > 0)
4742 key.objectid--;
4743 else
4744 return 1;
7bb86316 4745
b3b4aa74 4746 btrfs_release_path(path);
925baedd
CM
4747 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4748 if (ret < 0)
4749 return ret;
4750 btrfs_item_key(path->nodes[0], &found_key, 0);
4751 ret = comp_keys(&found_key, &key);
4752 if (ret < 0)
4753 return 0;
4754 return 1;
7bb86316
CM
4755}
4756
3f157a2f
CM
4757/*
4758 * A helper function to walk down the tree starting at min_key, and looking
4759 * for nodes or leaves that are either in cache or have a minimum
d352ac68 4760 * transaction id. This is used by the btree defrag code, and tree logging
3f157a2f
CM
4761 *
4762 * This does not cow, but it does stuff the starting key it finds back
4763 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4764 * key and get a writable path.
4765 *
4766 * This does lock as it descends, and path->keep_locks should be set
4767 * to 1 by the caller.
4768 *
4769 * This honors path->lowest_level to prevent descent past a given level
4770 * of the tree.
4771 *
d352ac68
CM
4772 * min_trans indicates the oldest transaction that you are interested
4773 * in walking through. Any nodes or leaves older than min_trans are
4774 * skipped over (without reading them).
4775 *
3f157a2f
CM
4776 * returns zero if something useful was found, < 0 on error and 1 if there
4777 * was nothing in the tree that matched the search criteria.
4778 */
4779int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
e02119d5 4780 struct btrfs_key *max_key,
3f157a2f
CM
4781 struct btrfs_path *path, int cache_only,
4782 u64 min_trans)
4783{
4784 struct extent_buffer *cur;
4785 struct btrfs_key found_key;
4786 int slot;
9652480b 4787 int sret;
3f157a2f
CM
4788 u32 nritems;
4789 int level;
4790 int ret = 1;
4791
934d375b 4792 WARN_ON(!path->keep_locks);
3f157a2f 4793again:
bd681513 4794 cur = btrfs_read_lock_root_node(root);
3f157a2f 4795 level = btrfs_header_level(cur);
e02119d5 4796 WARN_ON(path->nodes[level]);
3f157a2f 4797 path->nodes[level] = cur;
bd681513 4798 path->locks[level] = BTRFS_READ_LOCK;
3f157a2f
CM
4799
4800 if (btrfs_header_generation(cur) < min_trans) {
4801 ret = 1;
4802 goto out;
4803 }
d397712b 4804 while (1) {
3f157a2f
CM
4805 nritems = btrfs_header_nritems(cur);
4806 level = btrfs_header_level(cur);
9652480b 4807 sret = bin_search(cur, min_key, level, &slot);
3f157a2f 4808
323ac95b
CM
4809 /* at the lowest level, we're done, setup the path and exit */
4810 if (level == path->lowest_level) {
e02119d5
CM
4811 if (slot >= nritems)
4812 goto find_next_key;
3f157a2f
CM
4813 ret = 0;
4814 path->slots[level] = slot;
4815 btrfs_item_key_to_cpu(cur, &found_key, slot);
4816 goto out;
4817 }
9652480b
Y
4818 if (sret && slot > 0)
4819 slot--;
3f157a2f
CM
4820 /*
4821 * check this node pointer against the cache_only and
4822 * min_trans parameters. If it isn't in cache or is too
4823 * old, skip to the next one.
4824 */
d397712b 4825 while (slot < nritems) {
3f157a2f
CM
4826 u64 blockptr;
4827 u64 gen;
4828 struct extent_buffer *tmp;
e02119d5
CM
4829 struct btrfs_disk_key disk_key;
4830
3f157a2f
CM
4831 blockptr = btrfs_node_blockptr(cur, slot);
4832 gen = btrfs_node_ptr_generation(cur, slot);
4833 if (gen < min_trans) {
4834 slot++;
4835 continue;
4836 }
4837 if (!cache_only)
4838 break;
4839
e02119d5
CM
4840 if (max_key) {
4841 btrfs_node_key(cur, &disk_key, slot);
4842 if (comp_keys(&disk_key, max_key) >= 0) {
4843 ret = 1;
4844 goto out;
4845 }
4846 }
4847
3f157a2f
CM
4848 tmp = btrfs_find_tree_block(root, blockptr,
4849 btrfs_level_size(root, level - 1));
4850
b9fab919 4851 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
3f157a2f
CM
4852 free_extent_buffer(tmp);
4853 break;
4854 }
4855 if (tmp)
4856 free_extent_buffer(tmp);
4857 slot++;
4858 }
e02119d5 4859find_next_key:
3f157a2f
CM
4860 /*
4861 * we didn't find a candidate key in this node, walk forward
4862 * and find another one
4863 */
4864 if (slot >= nritems) {
e02119d5 4865 path->slots[level] = slot;
b4ce94de 4866 btrfs_set_path_blocking(path);
e02119d5 4867 sret = btrfs_find_next_key(root, path, min_key, level,
3f157a2f 4868 cache_only, min_trans);
e02119d5 4869 if (sret == 0) {
b3b4aa74 4870 btrfs_release_path(path);
3f157a2f
CM
4871 goto again;
4872 } else {
4873 goto out;
4874 }
4875 }
4876 /* save our key for returning back */
4877 btrfs_node_key_to_cpu(cur, &found_key, slot);
4878 path->slots[level] = slot;
4879 if (level == path->lowest_level) {
4880 ret = 0;
f7c79f30 4881 unlock_up(path, level, 1, 0, NULL);
3f157a2f
CM
4882 goto out;
4883 }
b4ce94de 4884 btrfs_set_path_blocking(path);
3f157a2f 4885 cur = read_node_slot(root, cur, slot);
79787eaa 4886 BUG_ON(!cur); /* -ENOMEM */
3f157a2f 4887
bd681513 4888 btrfs_tree_read_lock(cur);
b4ce94de 4889
bd681513 4890 path->locks[level - 1] = BTRFS_READ_LOCK;
3f157a2f 4891 path->nodes[level - 1] = cur;
f7c79f30 4892 unlock_up(path, level, 1, 0, NULL);
bd681513 4893 btrfs_clear_path_blocking(path, NULL, 0);
3f157a2f
CM
4894 }
4895out:
4896 if (ret == 0)
4897 memcpy(min_key, &found_key, sizeof(found_key));
b4ce94de 4898 btrfs_set_path_blocking(path);
3f157a2f
CM
4899 return ret;
4900}
4901
4902/*
4903 * this is similar to btrfs_next_leaf, but does not try to preserve
4904 * and fixup the path. It looks for and returns the next key in the
4905 * tree based on the current path and the cache_only and min_trans
4906 * parameters.
4907 *
4908 * 0 is returned if another key is found, < 0 if there are any errors
4909 * and 1 is returned if there are no higher keys in the tree
4910 *
4911 * path->keep_locks should be set to 1 on the search made before
4912 * calling this function.
4913 */
e7a84565 4914int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
33c66f43 4915 struct btrfs_key *key, int level,
3f157a2f 4916 int cache_only, u64 min_trans)
e7a84565 4917{
e7a84565
CM
4918 int slot;
4919 struct extent_buffer *c;
4920
934d375b 4921 WARN_ON(!path->keep_locks);
d397712b 4922 while (level < BTRFS_MAX_LEVEL) {
e7a84565
CM
4923 if (!path->nodes[level])
4924 return 1;
4925
4926 slot = path->slots[level] + 1;
4927 c = path->nodes[level];
3f157a2f 4928next:
e7a84565 4929 if (slot >= btrfs_header_nritems(c)) {
33c66f43
YZ
4930 int ret;
4931 int orig_lowest;
4932 struct btrfs_key cur_key;
4933 if (level + 1 >= BTRFS_MAX_LEVEL ||
4934 !path->nodes[level + 1])
e7a84565 4935 return 1;
33c66f43
YZ
4936
4937 if (path->locks[level + 1]) {
4938 level++;
4939 continue;
4940 }
4941
4942 slot = btrfs_header_nritems(c) - 1;
4943 if (level == 0)
4944 btrfs_item_key_to_cpu(c, &cur_key, slot);
4945 else
4946 btrfs_node_key_to_cpu(c, &cur_key, slot);
4947
4948 orig_lowest = path->lowest_level;
b3b4aa74 4949 btrfs_release_path(path);
33c66f43
YZ
4950 path->lowest_level = level;
4951 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4952 0, 0);
4953 path->lowest_level = orig_lowest;
4954 if (ret < 0)
4955 return ret;
4956
4957 c = path->nodes[level];
4958 slot = path->slots[level];
4959 if (ret == 0)
4960 slot++;
4961 goto next;
e7a84565 4962 }
33c66f43 4963
e7a84565
CM
4964 if (level == 0)
4965 btrfs_item_key_to_cpu(c, key, slot);
3f157a2f
CM
4966 else {
4967 u64 blockptr = btrfs_node_blockptr(c, slot);
4968 u64 gen = btrfs_node_ptr_generation(c, slot);
4969
4970 if (cache_only) {
4971 struct extent_buffer *cur;
4972 cur = btrfs_find_tree_block(root, blockptr,
4973 btrfs_level_size(root, level - 1));
b9fab919
CM
4974 if (!cur ||
4975 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
3f157a2f
CM
4976 slot++;
4977 if (cur)
4978 free_extent_buffer(cur);
4979 goto next;
4980 }
4981 free_extent_buffer(cur);
4982 }
4983 if (gen < min_trans) {
4984 slot++;
4985 goto next;
4986 }
e7a84565 4987 btrfs_node_key_to_cpu(c, key, slot);
3f157a2f 4988 }
e7a84565
CM
4989 return 0;
4990 }
4991 return 1;
4992}
4993
97571fd0 4994/*
925baedd 4995 * search the tree again to find a leaf with greater keys
0f70abe2
CM
4996 * returns 0 if it found something or 1 if there are no greater leaves.
4997 * returns < 0 on io errors.
97571fd0 4998 */
234b63a0 4999int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
d97e63b6
CM
5000{
5001 int slot;
8e73f275 5002 int level;
5f39d397 5003 struct extent_buffer *c;
8e73f275 5004 struct extent_buffer *next;
925baedd
CM
5005 struct btrfs_key key;
5006 u32 nritems;
5007 int ret;
8e73f275 5008 int old_spinning = path->leave_spinning;
bd681513 5009 int next_rw_lock = 0;
925baedd
CM
5010
5011 nritems = btrfs_header_nritems(path->nodes[0]);
d397712b 5012 if (nritems == 0)
925baedd 5013 return 1;
925baedd 5014
8e73f275
CM
5015 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5016again:
5017 level = 1;
5018 next = NULL;
bd681513 5019 next_rw_lock = 0;
b3b4aa74 5020 btrfs_release_path(path);
8e73f275 5021
a2135011 5022 path->keep_locks = 1;
31533fb2 5023 path->leave_spinning = 1;
8e73f275 5024
925baedd
CM
5025 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5026 path->keep_locks = 0;
5027
5028 if (ret < 0)
5029 return ret;
5030
a2135011 5031 nritems = btrfs_header_nritems(path->nodes[0]);
168fd7d2
CM
5032 /*
5033 * by releasing the path above we dropped all our locks. A balance
5034 * could have added more items next to the key that used to be
5035 * at the very end of the block. So, check again here and
5036 * advance the path if there are now more items available.
5037 */
a2135011 5038 if (nritems > 0 && path->slots[0] < nritems - 1) {
e457afec
YZ
5039 if (ret == 0)
5040 path->slots[0]++;
8e73f275 5041 ret = 0;
925baedd
CM
5042 goto done;
5043 }
d97e63b6 5044
d397712b 5045 while (level < BTRFS_MAX_LEVEL) {
8e73f275
CM
5046 if (!path->nodes[level]) {
5047 ret = 1;
5048 goto done;
5049 }
5f39d397 5050
d97e63b6
CM
5051 slot = path->slots[level] + 1;
5052 c = path->nodes[level];
5f39d397 5053 if (slot >= btrfs_header_nritems(c)) {
d97e63b6 5054 level++;
8e73f275
CM
5055 if (level == BTRFS_MAX_LEVEL) {
5056 ret = 1;
5057 goto done;
5058 }
d97e63b6
CM
5059 continue;
5060 }
5f39d397 5061
925baedd 5062 if (next) {
bd681513 5063 btrfs_tree_unlock_rw(next, next_rw_lock);
5f39d397 5064 free_extent_buffer(next);
925baedd 5065 }
5f39d397 5066
8e73f275 5067 next = c;
bd681513 5068 next_rw_lock = path->locks[level];
8e73f275 5069 ret = read_block_for_search(NULL, root, path, &next, level,
5d9e75c4 5070 slot, &key, 0);
8e73f275
CM
5071 if (ret == -EAGAIN)
5072 goto again;
5f39d397 5073
76a05b35 5074 if (ret < 0) {
b3b4aa74 5075 btrfs_release_path(path);
76a05b35
CM
5076 goto done;
5077 }
5078
5cd57b2c 5079 if (!path->skip_locking) {
bd681513 5080 ret = btrfs_try_tree_read_lock(next);
8e73f275
CM
5081 if (!ret) {
5082 btrfs_set_path_blocking(path);
bd681513 5083 btrfs_tree_read_lock(next);
31533fb2 5084 btrfs_clear_path_blocking(path, next,
bd681513 5085 BTRFS_READ_LOCK);
8e73f275 5086 }
31533fb2 5087 next_rw_lock = BTRFS_READ_LOCK;
5cd57b2c 5088 }
d97e63b6
CM
5089 break;
5090 }
5091 path->slots[level] = slot;
d397712b 5092 while (1) {
d97e63b6
CM
5093 level--;
5094 c = path->nodes[level];
925baedd 5095 if (path->locks[level])
bd681513 5096 btrfs_tree_unlock_rw(c, path->locks[level]);
8e73f275 5097
5f39d397 5098 free_extent_buffer(c);
d97e63b6
CM
5099 path->nodes[level] = next;
5100 path->slots[level] = 0;
a74a4b97 5101 if (!path->skip_locking)
bd681513 5102 path->locks[level] = next_rw_lock;
d97e63b6
CM
5103 if (!level)
5104 break;
b4ce94de 5105
8e73f275 5106 ret = read_block_for_search(NULL, root, path, &next, level,
5d9e75c4 5107 0, &key, 0);
8e73f275
CM
5108 if (ret == -EAGAIN)
5109 goto again;
5110
76a05b35 5111 if (ret < 0) {
b3b4aa74 5112 btrfs_release_path(path);
76a05b35
CM
5113 goto done;
5114 }
5115
5cd57b2c 5116 if (!path->skip_locking) {
bd681513 5117 ret = btrfs_try_tree_read_lock(next);
8e73f275
CM
5118 if (!ret) {
5119 btrfs_set_path_blocking(path);
bd681513 5120 btrfs_tree_read_lock(next);
31533fb2 5121 btrfs_clear_path_blocking(path, next,
bd681513
CM
5122 BTRFS_READ_LOCK);
5123 }
31533fb2 5124 next_rw_lock = BTRFS_READ_LOCK;
5cd57b2c 5125 }
d97e63b6 5126 }
8e73f275 5127 ret = 0;
925baedd 5128done:
f7c79f30 5129 unlock_up(path, 0, 1, 0, NULL);
8e73f275
CM
5130 path->leave_spinning = old_spinning;
5131 if (!old_spinning)
5132 btrfs_set_path_blocking(path);
5133
5134 return ret;
d97e63b6 5135}
0b86a832 5136
3f157a2f
CM
5137/*
5138 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5139 * searching until it gets past min_objectid or finds an item of 'type'
5140 *
5141 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5142 */
0b86a832
CM
5143int btrfs_previous_item(struct btrfs_root *root,
5144 struct btrfs_path *path, u64 min_objectid,
5145 int type)
5146{
5147 struct btrfs_key found_key;
5148 struct extent_buffer *leaf;
e02119d5 5149 u32 nritems;
0b86a832
CM
5150 int ret;
5151
d397712b 5152 while (1) {
0b86a832 5153 if (path->slots[0] == 0) {
b4ce94de 5154 btrfs_set_path_blocking(path);
0b86a832
CM
5155 ret = btrfs_prev_leaf(root, path);
5156 if (ret != 0)
5157 return ret;
5158 } else {
5159 path->slots[0]--;
5160 }
5161 leaf = path->nodes[0];
e02119d5
CM
5162 nritems = btrfs_header_nritems(leaf);
5163 if (nritems == 0)
5164 return 1;
5165 if (path->slots[0] == nritems)
5166 path->slots[0]--;
5167
0b86a832 5168 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
e02119d5
CM
5169 if (found_key.objectid < min_objectid)
5170 break;
0a4eefbb
YZ
5171 if (found_key.type == type)
5172 return 0;
e02119d5
CM
5173 if (found_key.objectid == min_objectid &&
5174 found_key.type < type)
5175 break;
0b86a832
CM
5176 }
5177 return 1;
5178}