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