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