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