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