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