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