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