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