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