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