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