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