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