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