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