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