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