Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 | 2 | /* |
d352ac68 | 3 | * Copyright (C) 2007,2008 Oracle. All rights reserved. |
6cbd5570 CM |
4 | */ |
5 | ||
a6b6e75e | 6 | #include <linux/sched.h> |
5a0e3ad6 | 7 | #include <linux/slab.h> |
bd989ba3 | 8 | #include <linux/rbtree.h> |
adf02123 | 9 | #include <linux/mm.h> |
eb60ceac CM |
10 | #include "ctree.h" |
11 | #include "disk-io.h" | |
7f5c1516 | 12 | #include "transaction.h" |
5f39d397 | 13 | #include "print-tree.h" |
925baedd | 14 | #include "locking.h" |
de37aa51 | 15 | #include "volumes.h" |
f616f5cd | 16 | #include "qgroup.h" |
9a8dd150 | 17 | |
e089f05c CM |
18 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
19 | *root, struct btrfs_path *path, int level); | |
310712b2 OS |
20 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
21 | const struct btrfs_key *ins_key, struct btrfs_path *path, | |
22 | int data_size, int extend); | |
5f39d397 | 23 | static int push_node_left(struct btrfs_trans_handle *trans, |
2ff7e61e | 24 | struct extent_buffer *dst, |
971a1f66 | 25 | struct extent_buffer *src, int empty); |
5f39d397 | 26 | static int balance_node_right(struct btrfs_trans_handle *trans, |
5f39d397 CM |
27 | struct extent_buffer *dst_buf, |
28 | struct extent_buffer *src_buf); | |
afe5fea7 TI |
29 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
30 | int level, int slot); | |
d97e63b6 | 31 | |
af024ed2 JT |
32 | static const struct btrfs_csums { |
33 | u16 size; | |
59a0fcdb DS |
34 | const char name[10]; |
35 | const char driver[12]; | |
af024ed2 JT |
36 | } btrfs_csums[] = { |
37 | [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" }, | |
3951e7f0 | 38 | [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" }, |
3831bf00 | 39 | [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" }, |
352ae07b DS |
40 | [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b", |
41 | .driver = "blake2b-256" }, | |
af024ed2 JT |
42 | }; |
43 | ||
44 | int btrfs_super_csum_size(const struct btrfs_super_block *s) | |
45 | { | |
46 | u16 t = btrfs_super_csum_type(s); | |
47 | /* | |
48 | * csum type is validated at mount time | |
49 | */ | |
50 | return btrfs_csums[t].size; | |
51 | } | |
52 | ||
53 | const char *btrfs_super_csum_name(u16 csum_type) | |
54 | { | |
55 | /* csum type is validated at mount time */ | |
56 | return btrfs_csums[csum_type].name; | |
57 | } | |
58 | ||
b4e967be DS |
59 | /* |
60 | * Return driver name if defined, otherwise the name that's also a valid driver | |
61 | * name | |
62 | */ | |
63 | const char *btrfs_super_csum_driver(u16 csum_type) | |
64 | { | |
65 | /* csum type is validated at mount time */ | |
59a0fcdb DS |
66 | return btrfs_csums[csum_type].driver[0] ? |
67 | btrfs_csums[csum_type].driver : | |
b4e967be DS |
68 | btrfs_csums[csum_type].name; |
69 | } | |
70 | ||
604997b4 | 71 | size_t __attribute_const__ btrfs_get_num_csums(void) |
f7cea56c DS |
72 | { |
73 | return ARRAY_SIZE(btrfs_csums); | |
74 | } | |
75 | ||
df24a2b9 | 76 | struct btrfs_path *btrfs_alloc_path(void) |
2c90e5d6 | 77 | { |
e2c89907 | 78 | return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
2c90e5d6 CM |
79 | } |
80 | ||
d352ac68 | 81 | /* this also releases the path */ |
df24a2b9 | 82 | void btrfs_free_path(struct btrfs_path *p) |
be0e5c09 | 83 | { |
ff175d57 JJ |
84 | if (!p) |
85 | return; | |
b3b4aa74 | 86 | btrfs_release_path(p); |
df24a2b9 | 87 | kmem_cache_free(btrfs_path_cachep, p); |
be0e5c09 CM |
88 | } |
89 | ||
d352ac68 CM |
90 | /* |
91 | * path release drops references on the extent buffers in the path | |
92 | * and it drops any locks held by this path | |
93 | * | |
94 | * It is safe to call this on paths that no locks or extent buffers held. | |
95 | */ | |
b3b4aa74 | 96 | noinline void btrfs_release_path(struct btrfs_path *p) |
eb60ceac CM |
97 | { |
98 | int i; | |
a2135011 | 99 | |
234b63a0 | 100 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
3f157a2f | 101 | p->slots[i] = 0; |
eb60ceac | 102 | if (!p->nodes[i]) |
925baedd CM |
103 | continue; |
104 | if (p->locks[i]) { | |
bd681513 | 105 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
925baedd CM |
106 | p->locks[i] = 0; |
107 | } | |
5f39d397 | 108 | free_extent_buffer(p->nodes[i]); |
3f157a2f | 109 | p->nodes[i] = NULL; |
eb60ceac CM |
110 | } |
111 | } | |
112 | ||
d352ac68 CM |
113 | /* |
114 | * safely gets a reference on the root node of a tree. A lock | |
115 | * is not taken, so a concurrent writer may put a different node | |
116 | * at the root of the tree. See btrfs_lock_root_node for the | |
117 | * looping required. | |
118 | * | |
119 | * The extent buffer returned by this has a reference taken, so | |
120 | * it won't disappear. It may stop being the root of the tree | |
121 | * at any time because there are no locks held. | |
122 | */ | |
925baedd CM |
123 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
124 | { | |
125 | struct extent_buffer *eb; | |
240f62c8 | 126 | |
3083ee2e JB |
127 | while (1) { |
128 | rcu_read_lock(); | |
129 | eb = rcu_dereference(root->node); | |
130 | ||
131 | /* | |
132 | * RCU really hurts here, we could free up the root node because | |
01327610 | 133 | * it was COWed but we may not get the new root node yet so do |
3083ee2e JB |
134 | * the inc_not_zero dance and if it doesn't work then |
135 | * synchronize_rcu and try again. | |
136 | */ | |
137 | if (atomic_inc_not_zero(&eb->refs)) { | |
138 | rcu_read_unlock(); | |
139 | break; | |
140 | } | |
141 | rcu_read_unlock(); | |
142 | synchronize_rcu(); | |
143 | } | |
925baedd CM |
144 | return eb; |
145 | } | |
146 | ||
92a7cc42 QW |
147 | /* |
148 | * Cowonly root (not-shareable trees, everything not subvolume or reloc roots), | |
149 | * just get put onto a simple dirty list. Transaction walks this list to make | |
150 | * sure they get properly updated on disk. | |
d352ac68 | 151 | */ |
0b86a832 CM |
152 | static void add_root_to_dirty_list(struct btrfs_root *root) |
153 | { | |
0b246afa JM |
154 | struct btrfs_fs_info *fs_info = root->fs_info; |
155 | ||
e7070be1 JB |
156 | if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || |
157 | !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) | |
158 | return; | |
159 | ||
0b246afa | 160 | spin_lock(&fs_info->trans_lock); |
e7070be1 JB |
161 | if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { |
162 | /* Want the extent tree to be the last on the list */ | |
4fd786e6 | 163 | if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID) |
e7070be1 | 164 | list_move_tail(&root->dirty_list, |
0b246afa | 165 | &fs_info->dirty_cowonly_roots); |
e7070be1 JB |
166 | else |
167 | list_move(&root->dirty_list, | |
0b246afa | 168 | &fs_info->dirty_cowonly_roots); |
0b86a832 | 169 | } |
0b246afa | 170 | spin_unlock(&fs_info->trans_lock); |
0b86a832 CM |
171 | } |
172 | ||
d352ac68 CM |
173 | /* |
174 | * used by snapshot creation to make a copy of a root for a tree with | |
175 | * a given objectid. The buffer with the new root node is returned in | |
176 | * cow_ret, and this func returns zero on success or a negative error code. | |
177 | */ | |
be20aa9d CM |
178 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
179 | struct btrfs_root *root, | |
180 | struct extent_buffer *buf, | |
181 | struct extent_buffer **cow_ret, u64 new_root_objectid) | |
182 | { | |
0b246afa | 183 | struct btrfs_fs_info *fs_info = root->fs_info; |
be20aa9d | 184 | struct extent_buffer *cow; |
be20aa9d CM |
185 | int ret = 0; |
186 | int level; | |
5d4f98a2 | 187 | struct btrfs_disk_key disk_key; |
be20aa9d | 188 | |
92a7cc42 | 189 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
0b246afa | 190 | trans->transid != fs_info->running_transaction->transid); |
92a7cc42 | 191 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
27cdeb70 | 192 | trans->transid != root->last_trans); |
be20aa9d CM |
193 | |
194 | level = btrfs_header_level(buf); | |
5d4f98a2 YZ |
195 | if (level == 0) |
196 | btrfs_item_key(buf, &disk_key, 0); | |
197 | else | |
198 | btrfs_node_key(buf, &disk_key, 0); | |
31840ae1 | 199 | |
4d75f8a9 | 200 | cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, |
9631e4cc | 201 | &disk_key, level, buf->start, 0, BTRFS_NESTING_NORMAL); |
5d4f98a2 | 202 | if (IS_ERR(cow)) |
be20aa9d CM |
203 | return PTR_ERR(cow); |
204 | ||
58e8012c | 205 | copy_extent_buffer_full(cow, buf); |
be20aa9d CM |
206 | btrfs_set_header_bytenr(cow, cow->start); |
207 | btrfs_set_header_generation(cow, trans->transid); | |
5d4f98a2 YZ |
208 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
209 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | | |
210 | BTRFS_HEADER_FLAG_RELOC); | |
211 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) | |
212 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); | |
213 | else | |
214 | btrfs_set_header_owner(cow, new_root_objectid); | |
be20aa9d | 215 | |
de37aa51 | 216 | write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); |
2b82032c | 217 | |
be20aa9d | 218 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
5d4f98a2 | 219 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
e339a6b0 | 220 | ret = btrfs_inc_ref(trans, root, cow, 1); |
5d4f98a2 | 221 | else |
e339a6b0 | 222 | ret = btrfs_inc_ref(trans, root, cow, 0); |
4aec2b52 | 223 | |
be20aa9d CM |
224 | if (ret) |
225 | return ret; | |
226 | ||
227 | btrfs_mark_buffer_dirty(cow); | |
228 | *cow_ret = cow; | |
229 | return 0; | |
230 | } | |
231 | ||
bd989ba3 JS |
232 | enum mod_log_op { |
233 | MOD_LOG_KEY_REPLACE, | |
234 | MOD_LOG_KEY_ADD, | |
235 | MOD_LOG_KEY_REMOVE, | |
236 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, | |
237 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, | |
238 | MOD_LOG_MOVE_KEYS, | |
239 | MOD_LOG_ROOT_REPLACE, | |
240 | }; | |
241 | ||
bd989ba3 JS |
242 | struct tree_mod_root { |
243 | u64 logical; | |
244 | u8 level; | |
245 | }; | |
246 | ||
247 | struct tree_mod_elem { | |
248 | struct rb_node node; | |
298cfd36 | 249 | u64 logical; |
097b8a7c | 250 | u64 seq; |
bd989ba3 JS |
251 | enum mod_log_op op; |
252 | ||
253 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ | |
254 | int slot; | |
255 | ||
256 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ | |
257 | u64 generation; | |
258 | ||
259 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ | |
260 | struct btrfs_disk_key key; | |
261 | u64 blockptr; | |
262 | ||
263 | /* this is used for op == MOD_LOG_MOVE_KEYS */ | |
b6dfa35b DS |
264 | struct { |
265 | int dst_slot; | |
266 | int nr_items; | |
267 | } move; | |
bd989ba3 JS |
268 | |
269 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ | |
270 | struct tree_mod_root old_root; | |
271 | }; | |
272 | ||
fc36ed7e | 273 | /* |
fcebe456 | 274 | * Pull a new tree mod seq number for our operation. |
fc36ed7e | 275 | */ |
fcebe456 | 276 | static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info) |
fc36ed7e JS |
277 | { |
278 | return atomic64_inc_return(&fs_info->tree_mod_seq); | |
279 | } | |
280 | ||
097b8a7c JS |
281 | /* |
282 | * This adds a new blocker to the tree mod log's blocker list if the @elem | |
283 | * passed does not already have a sequence number set. So when a caller expects | |
284 | * to record tree modifications, it should ensure to set elem->seq to zero | |
285 | * before calling btrfs_get_tree_mod_seq. | |
286 | * Returns a fresh, unused tree log modification sequence number, even if no new | |
287 | * blocker was added. | |
288 | */ | |
289 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, | |
290 | struct seq_list *elem) | |
bd989ba3 | 291 | { |
b1a09f1e | 292 | write_lock(&fs_info->tree_mod_log_lock); |
097b8a7c | 293 | if (!elem->seq) { |
fcebe456 | 294 | elem->seq = btrfs_inc_tree_mod_seq(fs_info); |
097b8a7c JS |
295 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
296 | } | |
b1a09f1e | 297 | write_unlock(&fs_info->tree_mod_log_lock); |
097b8a7c | 298 | |
fcebe456 | 299 | return elem->seq; |
bd989ba3 JS |
300 | } |
301 | ||
302 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, | |
303 | struct seq_list *elem) | |
304 | { | |
305 | struct rb_root *tm_root; | |
306 | struct rb_node *node; | |
307 | struct rb_node *next; | |
bd989ba3 JS |
308 | struct tree_mod_elem *tm; |
309 | u64 min_seq = (u64)-1; | |
310 | u64 seq_putting = elem->seq; | |
311 | ||
312 | if (!seq_putting) | |
313 | return; | |
314 | ||
7227ff4d | 315 | write_lock(&fs_info->tree_mod_log_lock); |
bd989ba3 | 316 | list_del(&elem->list); |
097b8a7c | 317 | elem->seq = 0; |
bd989ba3 | 318 | |
42836cf4 FM |
319 | if (!list_empty(&fs_info->tree_mod_seq_list)) { |
320 | struct seq_list *first; | |
321 | ||
322 | first = list_first_entry(&fs_info->tree_mod_seq_list, | |
323 | struct seq_list, list); | |
324 | if (seq_putting > first->seq) { | |
325 | /* | |
326 | * Blocker with lower sequence number exists, we | |
327 | * cannot remove anything from the log. | |
328 | */ | |
329 | write_unlock(&fs_info->tree_mod_log_lock); | |
330 | return; | |
bd989ba3 | 331 | } |
42836cf4 | 332 | min_seq = first->seq; |
bd989ba3 | 333 | } |
097b8a7c | 334 | |
bd989ba3 JS |
335 | /* |
336 | * anything that's lower than the lowest existing (read: blocked) | |
337 | * sequence number can be removed from the tree. | |
338 | */ | |
bd989ba3 JS |
339 | tm_root = &fs_info->tree_mod_log; |
340 | for (node = rb_first(tm_root); node; node = next) { | |
341 | next = rb_next(node); | |
6b4df8b6 | 342 | tm = rb_entry(node, struct tree_mod_elem, node); |
6609fee8 | 343 | if (tm->seq >= min_seq) |
bd989ba3 JS |
344 | continue; |
345 | rb_erase(node, tm_root); | |
bd989ba3 JS |
346 | kfree(tm); |
347 | } | |
b1a09f1e | 348 | write_unlock(&fs_info->tree_mod_log_lock); |
bd989ba3 JS |
349 | } |
350 | ||
351 | /* | |
352 | * key order of the log: | |
298cfd36 | 353 | * node/leaf start address -> sequence |
bd989ba3 | 354 | * |
298cfd36 CR |
355 | * The 'start address' is the logical address of the *new* root node |
356 | * for root replace operations, or the logical address of the affected | |
357 | * block for all other operations. | |
bd989ba3 JS |
358 | */ |
359 | static noinline int | |
360 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) | |
361 | { | |
362 | struct rb_root *tm_root; | |
363 | struct rb_node **new; | |
364 | struct rb_node *parent = NULL; | |
365 | struct tree_mod_elem *cur; | |
c8cc6341 | 366 | |
73e82fe4 DS |
367 | lockdep_assert_held_write(&fs_info->tree_mod_log_lock); |
368 | ||
fcebe456 | 369 | tm->seq = btrfs_inc_tree_mod_seq(fs_info); |
bd989ba3 | 370 | |
bd989ba3 JS |
371 | tm_root = &fs_info->tree_mod_log; |
372 | new = &tm_root->rb_node; | |
373 | while (*new) { | |
6b4df8b6 | 374 | cur = rb_entry(*new, struct tree_mod_elem, node); |
bd989ba3 | 375 | parent = *new; |
298cfd36 | 376 | if (cur->logical < tm->logical) |
bd989ba3 | 377 | new = &((*new)->rb_left); |
298cfd36 | 378 | else if (cur->logical > tm->logical) |
bd989ba3 | 379 | new = &((*new)->rb_right); |
097b8a7c | 380 | else if (cur->seq < tm->seq) |
bd989ba3 | 381 | new = &((*new)->rb_left); |
097b8a7c | 382 | else if (cur->seq > tm->seq) |
bd989ba3 | 383 | new = &((*new)->rb_right); |
5de865ee FDBM |
384 | else |
385 | return -EEXIST; | |
bd989ba3 JS |
386 | } |
387 | ||
388 | rb_link_node(&tm->node, parent, new); | |
389 | rb_insert_color(&tm->node, tm_root); | |
5de865ee | 390 | return 0; |
bd989ba3 JS |
391 | } |
392 | ||
097b8a7c JS |
393 | /* |
394 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it | |
395 | * returns zero with the tree_mod_log_lock acquired. The caller must hold | |
396 | * this until all tree mod log insertions are recorded in the rb tree and then | |
b1a09f1e | 397 | * write unlock fs_info::tree_mod_log_lock. |
097b8a7c | 398 | */ |
e9b7fd4d JS |
399 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
400 | struct extent_buffer *eb) { | |
401 | smp_mb(); | |
402 | if (list_empty(&(fs_info)->tree_mod_seq_list)) | |
403 | return 1; | |
097b8a7c JS |
404 | if (eb && btrfs_header_level(eb) == 0) |
405 | return 1; | |
5de865ee | 406 | |
b1a09f1e | 407 | write_lock(&fs_info->tree_mod_log_lock); |
5de865ee | 408 | if (list_empty(&(fs_info)->tree_mod_seq_list)) { |
b1a09f1e | 409 | write_unlock(&fs_info->tree_mod_log_lock); |
5de865ee FDBM |
410 | return 1; |
411 | } | |
412 | ||
e9b7fd4d JS |
413 | return 0; |
414 | } | |
415 | ||
5de865ee FDBM |
416 | /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ |
417 | static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, | |
418 | struct extent_buffer *eb) | |
419 | { | |
420 | smp_mb(); | |
421 | if (list_empty(&(fs_info)->tree_mod_seq_list)) | |
422 | return 0; | |
423 | if (eb && btrfs_header_level(eb) == 0) | |
424 | return 0; | |
425 | ||
426 | return 1; | |
427 | } | |
428 | ||
429 | static struct tree_mod_elem * | |
430 | alloc_tree_mod_elem(struct extent_buffer *eb, int slot, | |
431 | enum mod_log_op op, gfp_t flags) | |
bd989ba3 | 432 | { |
097b8a7c | 433 | struct tree_mod_elem *tm; |
bd989ba3 | 434 | |
c8cc6341 JB |
435 | tm = kzalloc(sizeof(*tm), flags); |
436 | if (!tm) | |
5de865ee | 437 | return NULL; |
bd989ba3 | 438 | |
298cfd36 | 439 | tm->logical = eb->start; |
bd989ba3 JS |
440 | if (op != MOD_LOG_KEY_ADD) { |
441 | btrfs_node_key(eb, &tm->key, slot); | |
442 | tm->blockptr = btrfs_node_blockptr(eb, slot); | |
443 | } | |
444 | tm->op = op; | |
445 | tm->slot = slot; | |
446 | tm->generation = btrfs_node_ptr_generation(eb, slot); | |
5de865ee | 447 | RB_CLEAR_NODE(&tm->node); |
bd989ba3 | 448 | |
5de865ee | 449 | return tm; |
097b8a7c JS |
450 | } |
451 | ||
e09c2efe DS |
452 | static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, |
453 | enum mod_log_op op, gfp_t flags) | |
097b8a7c | 454 | { |
5de865ee FDBM |
455 | struct tree_mod_elem *tm; |
456 | int ret; | |
457 | ||
e09c2efe | 458 | if (!tree_mod_need_log(eb->fs_info, eb)) |
5de865ee FDBM |
459 | return 0; |
460 | ||
461 | tm = alloc_tree_mod_elem(eb, slot, op, flags); | |
462 | if (!tm) | |
463 | return -ENOMEM; | |
464 | ||
e09c2efe | 465 | if (tree_mod_dont_log(eb->fs_info, eb)) { |
5de865ee | 466 | kfree(tm); |
097b8a7c | 467 | return 0; |
5de865ee FDBM |
468 | } |
469 | ||
e09c2efe | 470 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
b1a09f1e | 471 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
5de865ee FDBM |
472 | if (ret) |
473 | kfree(tm); | |
097b8a7c | 474 | |
5de865ee | 475 | return ret; |
097b8a7c JS |
476 | } |
477 | ||
6074d45f DS |
478 | static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, |
479 | int dst_slot, int src_slot, int nr_items) | |
bd989ba3 | 480 | { |
5de865ee FDBM |
481 | struct tree_mod_elem *tm = NULL; |
482 | struct tree_mod_elem **tm_list = NULL; | |
483 | int ret = 0; | |
bd989ba3 | 484 | int i; |
5de865ee | 485 | int locked = 0; |
bd989ba3 | 486 | |
6074d45f | 487 | if (!tree_mod_need_log(eb->fs_info, eb)) |
f395694c | 488 | return 0; |
bd989ba3 | 489 | |
176ef8f5 | 490 | tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS); |
5de865ee FDBM |
491 | if (!tm_list) |
492 | return -ENOMEM; | |
493 | ||
176ef8f5 | 494 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
5de865ee FDBM |
495 | if (!tm) { |
496 | ret = -ENOMEM; | |
497 | goto free_tms; | |
498 | } | |
499 | ||
298cfd36 | 500 | tm->logical = eb->start; |
5de865ee FDBM |
501 | tm->slot = src_slot; |
502 | tm->move.dst_slot = dst_slot; | |
503 | tm->move.nr_items = nr_items; | |
504 | tm->op = MOD_LOG_MOVE_KEYS; | |
505 | ||
506 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { | |
507 | tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, | |
176ef8f5 | 508 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
5de865ee FDBM |
509 | if (!tm_list[i]) { |
510 | ret = -ENOMEM; | |
511 | goto free_tms; | |
512 | } | |
513 | } | |
514 | ||
6074d45f | 515 | if (tree_mod_dont_log(eb->fs_info, eb)) |
5de865ee FDBM |
516 | goto free_tms; |
517 | locked = 1; | |
518 | ||
01763a2e JS |
519 | /* |
520 | * When we override something during the move, we log these removals. | |
521 | * This can only happen when we move towards the beginning of the | |
522 | * buffer, i.e. dst_slot < src_slot. | |
523 | */ | |
bd989ba3 | 524 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
6074d45f | 525 | ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]); |
5de865ee FDBM |
526 | if (ret) |
527 | goto free_tms; | |
bd989ba3 JS |
528 | } |
529 | ||
6074d45f | 530 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
5de865ee FDBM |
531 | if (ret) |
532 | goto free_tms; | |
b1a09f1e | 533 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
5de865ee | 534 | kfree(tm_list); |
f395694c | 535 | |
5de865ee FDBM |
536 | return 0; |
537 | free_tms: | |
538 | for (i = 0; i < nr_items; i++) { | |
539 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) | |
6074d45f | 540 | rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log); |
5de865ee FDBM |
541 | kfree(tm_list[i]); |
542 | } | |
543 | if (locked) | |
b1a09f1e | 544 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
5de865ee FDBM |
545 | kfree(tm_list); |
546 | kfree(tm); | |
bd989ba3 | 547 | |
5de865ee | 548 | return ret; |
bd989ba3 JS |
549 | } |
550 | ||
5de865ee FDBM |
551 | static inline int |
552 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, | |
553 | struct tree_mod_elem **tm_list, | |
554 | int nritems) | |
097b8a7c | 555 | { |
5de865ee | 556 | int i, j; |
097b8a7c JS |
557 | int ret; |
558 | ||
097b8a7c | 559 | for (i = nritems - 1; i >= 0; i--) { |
5de865ee FDBM |
560 | ret = __tree_mod_log_insert(fs_info, tm_list[i]); |
561 | if (ret) { | |
562 | for (j = nritems - 1; j > i; j--) | |
563 | rb_erase(&tm_list[j]->node, | |
564 | &fs_info->tree_mod_log); | |
565 | return ret; | |
566 | } | |
097b8a7c | 567 | } |
5de865ee FDBM |
568 | |
569 | return 0; | |
097b8a7c JS |
570 | } |
571 | ||
95b757c1 DS |
572 | static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, |
573 | struct extent_buffer *new_root, int log_removal) | |
bd989ba3 | 574 | { |
95b757c1 | 575 | struct btrfs_fs_info *fs_info = old_root->fs_info; |
5de865ee FDBM |
576 | struct tree_mod_elem *tm = NULL; |
577 | struct tree_mod_elem **tm_list = NULL; | |
578 | int nritems = 0; | |
579 | int ret = 0; | |
580 | int i; | |
bd989ba3 | 581 | |
5de865ee | 582 | if (!tree_mod_need_log(fs_info, NULL)) |
097b8a7c JS |
583 | return 0; |
584 | ||
5de865ee FDBM |
585 | if (log_removal && btrfs_header_level(old_root) > 0) { |
586 | nritems = btrfs_header_nritems(old_root); | |
31e818fe | 587 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), |
bcc8e07f | 588 | GFP_NOFS); |
5de865ee FDBM |
589 | if (!tm_list) { |
590 | ret = -ENOMEM; | |
591 | goto free_tms; | |
592 | } | |
593 | for (i = 0; i < nritems; i++) { | |
594 | tm_list[i] = alloc_tree_mod_elem(old_root, i, | |
bcc8e07f | 595 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
5de865ee FDBM |
596 | if (!tm_list[i]) { |
597 | ret = -ENOMEM; | |
598 | goto free_tms; | |
599 | } | |
600 | } | |
601 | } | |
d9abbf1c | 602 | |
bcc8e07f | 603 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
5de865ee FDBM |
604 | if (!tm) { |
605 | ret = -ENOMEM; | |
606 | goto free_tms; | |
607 | } | |
bd989ba3 | 608 | |
298cfd36 | 609 | tm->logical = new_root->start; |
bd989ba3 JS |
610 | tm->old_root.logical = old_root->start; |
611 | tm->old_root.level = btrfs_header_level(old_root); | |
612 | tm->generation = btrfs_header_generation(old_root); | |
613 | tm->op = MOD_LOG_ROOT_REPLACE; | |
614 | ||
5de865ee FDBM |
615 | if (tree_mod_dont_log(fs_info, NULL)) |
616 | goto free_tms; | |
617 | ||
618 | if (tm_list) | |
619 | ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); | |
620 | if (!ret) | |
621 | ret = __tree_mod_log_insert(fs_info, tm); | |
622 | ||
b1a09f1e | 623 | write_unlock(&fs_info->tree_mod_log_lock); |
5de865ee FDBM |
624 | if (ret) |
625 | goto free_tms; | |
626 | kfree(tm_list); | |
627 | ||
628 | return ret; | |
629 | ||
630 | free_tms: | |
631 | if (tm_list) { | |
632 | for (i = 0; i < nritems; i++) | |
633 | kfree(tm_list[i]); | |
634 | kfree(tm_list); | |
635 | } | |
636 | kfree(tm); | |
637 | ||
638 | return ret; | |
bd989ba3 JS |
639 | } |
640 | ||
641 | static struct tree_mod_elem * | |
642 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, | |
643 | int smallest) | |
644 | { | |
645 | struct rb_root *tm_root; | |
646 | struct rb_node *node; | |
647 | struct tree_mod_elem *cur = NULL; | |
648 | struct tree_mod_elem *found = NULL; | |
bd989ba3 | 649 | |
b1a09f1e | 650 | read_lock(&fs_info->tree_mod_log_lock); |
bd989ba3 JS |
651 | tm_root = &fs_info->tree_mod_log; |
652 | node = tm_root->rb_node; | |
653 | while (node) { | |
6b4df8b6 | 654 | cur = rb_entry(node, struct tree_mod_elem, node); |
298cfd36 | 655 | if (cur->logical < start) { |
bd989ba3 | 656 | node = node->rb_left; |
298cfd36 | 657 | } else if (cur->logical > start) { |
bd989ba3 | 658 | node = node->rb_right; |
097b8a7c | 659 | } else if (cur->seq < min_seq) { |
bd989ba3 JS |
660 | node = node->rb_left; |
661 | } else if (!smallest) { | |
662 | /* we want the node with the highest seq */ | |
663 | if (found) | |
097b8a7c | 664 | BUG_ON(found->seq > cur->seq); |
bd989ba3 JS |
665 | found = cur; |
666 | node = node->rb_left; | |
097b8a7c | 667 | } else if (cur->seq > min_seq) { |
bd989ba3 JS |
668 | /* we want the node with the smallest seq */ |
669 | if (found) | |
097b8a7c | 670 | BUG_ON(found->seq < cur->seq); |
bd989ba3 JS |
671 | found = cur; |
672 | node = node->rb_right; | |
673 | } else { | |
674 | found = cur; | |
675 | break; | |
676 | } | |
677 | } | |
b1a09f1e | 678 | read_unlock(&fs_info->tree_mod_log_lock); |
bd989ba3 JS |
679 | |
680 | return found; | |
681 | } | |
682 | ||
683 | /* | |
684 | * this returns the element from the log with the smallest time sequence | |
685 | * value that's in the log (the oldest log item). any element with a time | |
686 | * sequence lower than min_seq will be ignored. | |
687 | */ | |
688 | static struct tree_mod_elem * | |
689 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, | |
690 | u64 min_seq) | |
691 | { | |
692 | return __tree_mod_log_search(fs_info, start, min_seq, 1); | |
693 | } | |
694 | ||
695 | /* | |
696 | * this returns the element from the log with the largest time sequence | |
697 | * value that's in the log (the most recent log item). any element with | |
698 | * a time sequence lower than min_seq will be ignored. | |
699 | */ | |
700 | static struct tree_mod_elem * | |
701 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) | |
702 | { | |
703 | return __tree_mod_log_search(fs_info, start, min_seq, 0); | |
704 | } | |
705 | ||
ed874f0d | 706 | static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst, |
bd989ba3 | 707 | struct extent_buffer *src, unsigned long dst_offset, |
90f8d62e | 708 | unsigned long src_offset, int nr_items) |
bd989ba3 | 709 | { |
ed874f0d | 710 | struct btrfs_fs_info *fs_info = dst->fs_info; |
5de865ee FDBM |
711 | int ret = 0; |
712 | struct tree_mod_elem **tm_list = NULL; | |
713 | struct tree_mod_elem **tm_list_add, **tm_list_rem; | |
bd989ba3 | 714 | int i; |
5de865ee | 715 | int locked = 0; |
bd989ba3 | 716 | |
5de865ee FDBM |
717 | if (!tree_mod_need_log(fs_info, NULL)) |
718 | return 0; | |
bd989ba3 | 719 | |
c8cc6341 | 720 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) |
5de865ee FDBM |
721 | return 0; |
722 | ||
31e818fe | 723 | tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *), |
5de865ee FDBM |
724 | GFP_NOFS); |
725 | if (!tm_list) | |
726 | return -ENOMEM; | |
bd989ba3 | 727 | |
5de865ee FDBM |
728 | tm_list_add = tm_list; |
729 | tm_list_rem = tm_list + nr_items; | |
bd989ba3 | 730 | for (i = 0; i < nr_items; i++) { |
5de865ee FDBM |
731 | tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, |
732 | MOD_LOG_KEY_REMOVE, GFP_NOFS); | |
733 | if (!tm_list_rem[i]) { | |
734 | ret = -ENOMEM; | |
735 | goto free_tms; | |
736 | } | |
737 | ||
738 | tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, | |
739 | MOD_LOG_KEY_ADD, GFP_NOFS); | |
740 | if (!tm_list_add[i]) { | |
741 | ret = -ENOMEM; | |
742 | goto free_tms; | |
743 | } | |
744 | } | |
745 | ||
746 | if (tree_mod_dont_log(fs_info, NULL)) | |
747 | goto free_tms; | |
748 | locked = 1; | |
749 | ||
750 | for (i = 0; i < nr_items; i++) { | |
751 | ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); | |
752 | if (ret) | |
753 | goto free_tms; | |
754 | ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); | |
755 | if (ret) | |
756 | goto free_tms; | |
bd989ba3 | 757 | } |
5de865ee | 758 | |
b1a09f1e | 759 | write_unlock(&fs_info->tree_mod_log_lock); |
5de865ee FDBM |
760 | kfree(tm_list); |
761 | ||
762 | return 0; | |
763 | ||
764 | free_tms: | |
765 | for (i = 0; i < nr_items * 2; i++) { | |
766 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) | |
767 | rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); | |
768 | kfree(tm_list[i]); | |
769 | } | |
770 | if (locked) | |
b1a09f1e | 771 | write_unlock(&fs_info->tree_mod_log_lock); |
5de865ee FDBM |
772 | kfree(tm_list); |
773 | ||
774 | return ret; | |
bd989ba3 JS |
775 | } |
776 | ||
db7279a2 | 777 | static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) |
bd989ba3 | 778 | { |
5de865ee FDBM |
779 | struct tree_mod_elem **tm_list = NULL; |
780 | int nritems = 0; | |
781 | int i; | |
782 | int ret = 0; | |
783 | ||
784 | if (btrfs_header_level(eb) == 0) | |
785 | return 0; | |
786 | ||
db7279a2 | 787 | if (!tree_mod_need_log(eb->fs_info, NULL)) |
5de865ee FDBM |
788 | return 0; |
789 | ||
790 | nritems = btrfs_header_nritems(eb); | |
31e818fe | 791 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS); |
5de865ee FDBM |
792 | if (!tm_list) |
793 | return -ENOMEM; | |
794 | ||
795 | for (i = 0; i < nritems; i++) { | |
796 | tm_list[i] = alloc_tree_mod_elem(eb, i, | |
797 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); | |
798 | if (!tm_list[i]) { | |
799 | ret = -ENOMEM; | |
800 | goto free_tms; | |
801 | } | |
802 | } | |
803 | ||
db7279a2 | 804 | if (tree_mod_dont_log(eb->fs_info, eb)) |
5de865ee FDBM |
805 | goto free_tms; |
806 | ||
db7279a2 | 807 | ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); |
b1a09f1e | 808 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
5de865ee FDBM |
809 | if (ret) |
810 | goto free_tms; | |
811 | kfree(tm_list); | |
812 | ||
813 | return 0; | |
814 | ||
815 | free_tms: | |
816 | for (i = 0; i < nritems; i++) | |
817 | kfree(tm_list[i]); | |
818 | kfree(tm_list); | |
819 | ||
820 | return ret; | |
bd989ba3 JS |
821 | } |
822 | ||
5d4f98a2 YZ |
823 | /* |
824 | * check if the tree block can be shared by multiple trees | |
825 | */ | |
826 | int btrfs_block_can_be_shared(struct btrfs_root *root, | |
827 | struct extent_buffer *buf) | |
828 | { | |
829 | /* | |
92a7cc42 QW |
830 | * Tree blocks not in shareable trees and tree roots are never shared. |
831 | * If a block was allocated after the last snapshot and the block was | |
832 | * not allocated by tree relocation, we know the block is not shared. | |
5d4f98a2 | 833 | */ |
92a7cc42 | 834 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
5d4f98a2 YZ |
835 | buf != root->node && buf != root->commit_root && |
836 | (btrfs_header_generation(buf) <= | |
837 | btrfs_root_last_snapshot(&root->root_item) || | |
838 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) | |
839 | return 1; | |
a79865c6 | 840 | |
5d4f98a2 YZ |
841 | return 0; |
842 | } | |
843 | ||
844 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, | |
845 | struct btrfs_root *root, | |
846 | struct extent_buffer *buf, | |
f0486c68 YZ |
847 | struct extent_buffer *cow, |
848 | int *last_ref) | |
5d4f98a2 | 849 | { |
0b246afa | 850 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
851 | u64 refs; |
852 | u64 owner; | |
853 | u64 flags; | |
854 | u64 new_flags = 0; | |
855 | int ret; | |
856 | ||
857 | /* | |
858 | * Backrefs update rules: | |
859 | * | |
860 | * Always use full backrefs for extent pointers in tree block | |
861 | * allocated by tree relocation. | |
862 | * | |
863 | * If a shared tree block is no longer referenced by its owner | |
864 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), | |
865 | * use full backrefs for extent pointers in tree block. | |
866 | * | |
867 | * If a tree block is been relocating | |
868 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), | |
869 | * use full backrefs for extent pointers in tree block. | |
870 | * The reason for this is some operations (such as drop tree) | |
871 | * are only allowed for blocks use full backrefs. | |
872 | */ | |
873 | ||
874 | if (btrfs_block_can_be_shared(root, buf)) { | |
2ff7e61e | 875 | ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, |
3173a18f JB |
876 | btrfs_header_level(buf), 1, |
877 | &refs, &flags); | |
be1a5564 MF |
878 | if (ret) |
879 | return ret; | |
e5df9573 MF |
880 | if (refs == 0) { |
881 | ret = -EROFS; | |
0b246afa | 882 | btrfs_handle_fs_error(fs_info, ret, NULL); |
e5df9573 MF |
883 | return ret; |
884 | } | |
5d4f98a2 YZ |
885 | } else { |
886 | refs = 1; | |
887 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || | |
888 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) | |
889 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
890 | else | |
891 | flags = 0; | |
892 | } | |
893 | ||
894 | owner = btrfs_header_owner(buf); | |
895 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && | |
896 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | |
897 | ||
898 | if (refs > 1) { | |
899 | if ((owner == root->root_key.objectid || | |
900 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && | |
901 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { | |
e339a6b0 | 902 | ret = btrfs_inc_ref(trans, root, buf, 1); |
692826b2 JM |
903 | if (ret) |
904 | return ret; | |
5d4f98a2 YZ |
905 | |
906 | if (root->root_key.objectid == | |
907 | BTRFS_TREE_RELOC_OBJECTID) { | |
e339a6b0 | 908 | ret = btrfs_dec_ref(trans, root, buf, 0); |
692826b2 JM |
909 | if (ret) |
910 | return ret; | |
e339a6b0 | 911 | ret = btrfs_inc_ref(trans, root, cow, 1); |
692826b2 JM |
912 | if (ret) |
913 | return ret; | |
5d4f98a2 YZ |
914 | } |
915 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
916 | } else { | |
917 | ||
918 | if (root->root_key.objectid == | |
919 | BTRFS_TREE_RELOC_OBJECTID) | |
e339a6b0 | 920 | ret = btrfs_inc_ref(trans, root, cow, 1); |
5d4f98a2 | 921 | else |
e339a6b0 | 922 | ret = btrfs_inc_ref(trans, root, cow, 0); |
692826b2 JM |
923 | if (ret) |
924 | return ret; | |
5d4f98a2 YZ |
925 | } |
926 | if (new_flags != 0) { | |
b1c79e09 JB |
927 | int level = btrfs_header_level(buf); |
928 | ||
42c9d0b5 | 929 | ret = btrfs_set_disk_extent_flags(trans, buf, |
b1c79e09 | 930 | new_flags, level, 0); |
be1a5564 MF |
931 | if (ret) |
932 | return ret; | |
5d4f98a2 YZ |
933 | } |
934 | } else { | |
935 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { | |
936 | if (root->root_key.objectid == | |
937 | BTRFS_TREE_RELOC_OBJECTID) | |
e339a6b0 | 938 | ret = btrfs_inc_ref(trans, root, cow, 1); |
5d4f98a2 | 939 | else |
e339a6b0 | 940 | ret = btrfs_inc_ref(trans, root, cow, 0); |
692826b2 JM |
941 | if (ret) |
942 | return ret; | |
e339a6b0 | 943 | ret = btrfs_dec_ref(trans, root, buf, 1); |
692826b2 JM |
944 | if (ret) |
945 | return ret; | |
5d4f98a2 | 946 | } |
6a884d7d | 947 | btrfs_clean_tree_block(buf); |
f0486c68 | 948 | *last_ref = 1; |
5d4f98a2 YZ |
949 | } |
950 | return 0; | |
951 | } | |
952 | ||
a6279470 FM |
953 | static struct extent_buffer *alloc_tree_block_no_bg_flush( |
954 | struct btrfs_trans_handle *trans, | |
955 | struct btrfs_root *root, | |
956 | u64 parent_start, | |
957 | const struct btrfs_disk_key *disk_key, | |
958 | int level, | |
959 | u64 hint, | |
9631e4cc JB |
960 | u64 empty_size, |
961 | enum btrfs_lock_nesting nest) | |
a6279470 FM |
962 | { |
963 | struct btrfs_fs_info *fs_info = root->fs_info; | |
964 | struct extent_buffer *ret; | |
965 | ||
966 | /* | |
967 | * If we are COWing a node/leaf from the extent, chunk, device or free | |
968 | * space trees, make sure that we do not finish block group creation of | |
969 | * pending block groups. We do this to avoid a deadlock. | |
970 | * COWing can result in allocation of a new chunk, and flushing pending | |
971 | * block groups (btrfs_create_pending_block_groups()) can be triggered | |
972 | * when finishing allocation of a new chunk. Creation of a pending block | |
973 | * group modifies the extent, chunk, device and free space trees, | |
974 | * therefore we could deadlock with ourselves since we are holding a | |
975 | * lock on an extent buffer that btrfs_create_pending_block_groups() may | |
976 | * try to COW later. | |
977 | * For similar reasons, we also need to delay flushing pending block | |
978 | * groups when splitting a leaf or node, from one of those trees, since | |
979 | * we are holding a write lock on it and its parent or when inserting a | |
980 | * new root node for one of those trees. | |
981 | */ | |
982 | if (root == fs_info->extent_root || | |
983 | root == fs_info->chunk_root || | |
984 | root == fs_info->dev_root || | |
985 | root == fs_info->free_space_root) | |
986 | trans->can_flush_pending_bgs = false; | |
987 | ||
988 | ret = btrfs_alloc_tree_block(trans, root, parent_start, | |
989 | root->root_key.objectid, disk_key, level, | |
9631e4cc | 990 | hint, empty_size, nest); |
a6279470 FM |
991 | trans->can_flush_pending_bgs = true; |
992 | ||
993 | return ret; | |
994 | } | |
995 | ||
d352ac68 | 996 | /* |
d397712b CM |
997 | * does the dirty work in cow of a single block. The parent block (if |
998 | * supplied) is updated to point to the new cow copy. The new buffer is marked | |
999 | * dirty and returned locked. If you modify the block it needs to be marked | |
1000 | * dirty again. | |
d352ac68 CM |
1001 | * |
1002 | * search_start -- an allocation hint for the new block | |
1003 | * | |
d397712b CM |
1004 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
1005 | * bytes the allocator should try to find free next to the block it returns. | |
1006 | * This is just a hint and may be ignored by the allocator. | |
d352ac68 | 1007 | */ |
d397712b | 1008 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
5f39d397 CM |
1009 | struct btrfs_root *root, |
1010 | struct extent_buffer *buf, | |
1011 | struct extent_buffer *parent, int parent_slot, | |
1012 | struct extent_buffer **cow_ret, | |
9631e4cc JB |
1013 | u64 search_start, u64 empty_size, |
1014 | enum btrfs_lock_nesting nest) | |
02217ed2 | 1015 | { |
0b246afa | 1016 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 | 1017 | struct btrfs_disk_key disk_key; |
5f39d397 | 1018 | struct extent_buffer *cow; |
be1a5564 | 1019 | int level, ret; |
f0486c68 | 1020 | int last_ref = 0; |
925baedd | 1021 | int unlock_orig = 0; |
0f5053eb | 1022 | u64 parent_start = 0; |
7bb86316 | 1023 | |
925baedd CM |
1024 | if (*cow_ret == buf) |
1025 | unlock_orig = 1; | |
1026 | ||
b9447ef8 | 1027 | btrfs_assert_tree_locked(buf); |
925baedd | 1028 | |
92a7cc42 | 1029 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
0b246afa | 1030 | trans->transid != fs_info->running_transaction->transid); |
92a7cc42 | 1031 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
27cdeb70 | 1032 | trans->transid != root->last_trans); |
5f39d397 | 1033 | |
7bb86316 | 1034 | level = btrfs_header_level(buf); |
31840ae1 | 1035 | |
5d4f98a2 YZ |
1036 | if (level == 0) |
1037 | btrfs_item_key(buf, &disk_key, 0); | |
1038 | else | |
1039 | btrfs_node_key(buf, &disk_key, 0); | |
1040 | ||
0f5053eb GR |
1041 | if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) |
1042 | parent_start = parent->start; | |
5d4f98a2 | 1043 | |
a6279470 | 1044 | cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key, |
9631e4cc | 1045 | level, search_start, empty_size, nest); |
54aa1f4d CM |
1046 | if (IS_ERR(cow)) |
1047 | return PTR_ERR(cow); | |
6702ed49 | 1048 | |
b4ce94de CM |
1049 | /* cow is set to blocking by btrfs_init_new_buffer */ |
1050 | ||
58e8012c | 1051 | copy_extent_buffer_full(cow, buf); |
db94535d | 1052 | btrfs_set_header_bytenr(cow, cow->start); |
5f39d397 | 1053 | btrfs_set_header_generation(cow, trans->transid); |
5d4f98a2 YZ |
1054 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
1055 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | | |
1056 | BTRFS_HEADER_FLAG_RELOC); | |
1057 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) | |
1058 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); | |
1059 | else | |
1060 | btrfs_set_header_owner(cow, root->root_key.objectid); | |
6702ed49 | 1061 | |
de37aa51 | 1062 | write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); |
2b82032c | 1063 | |
be1a5564 | 1064 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
b68dc2a9 | 1065 | if (ret) { |
66642832 | 1066 | btrfs_abort_transaction(trans, ret); |
b68dc2a9 MF |
1067 | return ret; |
1068 | } | |
1a40e23b | 1069 | |
92a7cc42 | 1070 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { |
83d4cfd4 | 1071 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
93314e3b | 1072 | if (ret) { |
66642832 | 1073 | btrfs_abort_transaction(trans, ret); |
83d4cfd4 | 1074 | return ret; |
93314e3b | 1075 | } |
83d4cfd4 | 1076 | } |
3fd0a558 | 1077 | |
02217ed2 | 1078 | if (buf == root->node) { |
925baedd | 1079 | WARN_ON(parent && parent != buf); |
5d4f98a2 YZ |
1080 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
1081 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) | |
1082 | parent_start = buf->start; | |
925baedd | 1083 | |
67439dad | 1084 | atomic_inc(&cow->refs); |
d9d19a01 DS |
1085 | ret = tree_mod_log_insert_root(root->node, cow, 1); |
1086 | BUG_ON(ret < 0); | |
240f62c8 | 1087 | rcu_assign_pointer(root->node, cow); |
925baedd | 1088 | |
f0486c68 | 1089 | btrfs_free_tree_block(trans, root, buf, parent_start, |
5581a51a | 1090 | last_ref); |
5f39d397 | 1091 | free_extent_buffer(buf); |
0b86a832 | 1092 | add_root_to_dirty_list(root); |
02217ed2 | 1093 | } else { |
5d4f98a2 | 1094 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
e09c2efe | 1095 | tree_mod_log_insert_key(parent, parent_slot, |
c8cc6341 | 1096 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
5f39d397 | 1097 | btrfs_set_node_blockptr(parent, parent_slot, |
db94535d | 1098 | cow->start); |
74493f7a CM |
1099 | btrfs_set_node_ptr_generation(parent, parent_slot, |
1100 | trans->transid); | |
d6025579 | 1101 | btrfs_mark_buffer_dirty(parent); |
5de865ee | 1102 | if (last_ref) { |
db7279a2 | 1103 | ret = tree_mod_log_free_eb(buf); |
5de865ee | 1104 | if (ret) { |
66642832 | 1105 | btrfs_abort_transaction(trans, ret); |
5de865ee FDBM |
1106 | return ret; |
1107 | } | |
1108 | } | |
f0486c68 | 1109 | btrfs_free_tree_block(trans, root, buf, parent_start, |
5581a51a | 1110 | last_ref); |
02217ed2 | 1111 | } |
925baedd CM |
1112 | if (unlock_orig) |
1113 | btrfs_tree_unlock(buf); | |
3083ee2e | 1114 | free_extent_buffer_stale(buf); |
ccd467d6 | 1115 | btrfs_mark_buffer_dirty(cow); |
2c90e5d6 | 1116 | *cow_ret = cow; |
02217ed2 CM |
1117 | return 0; |
1118 | } | |
1119 | ||
5d9e75c4 JS |
1120 | /* |
1121 | * returns the logical address of the oldest predecessor of the given root. | |
1122 | * entries older than time_seq are ignored. | |
1123 | */ | |
bcd24dab DS |
1124 | static struct tree_mod_elem *__tree_mod_log_oldest_root( |
1125 | struct extent_buffer *eb_root, u64 time_seq) | |
5d9e75c4 JS |
1126 | { |
1127 | struct tree_mod_elem *tm; | |
1128 | struct tree_mod_elem *found = NULL; | |
30b0463a | 1129 | u64 root_logical = eb_root->start; |
5d9e75c4 JS |
1130 | int looped = 0; |
1131 | ||
1132 | if (!time_seq) | |
35a3621b | 1133 | return NULL; |
5d9e75c4 JS |
1134 | |
1135 | /* | |
298cfd36 CR |
1136 | * the very last operation that's logged for a root is the |
1137 | * replacement operation (if it is replaced at all). this has | |
1138 | * the logical address of the *new* root, making it the very | |
1139 | * first operation that's logged for this root. | |
5d9e75c4 JS |
1140 | */ |
1141 | while (1) { | |
bcd24dab | 1142 | tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, |
5d9e75c4 JS |
1143 | time_seq); |
1144 | if (!looped && !tm) | |
35a3621b | 1145 | return NULL; |
5d9e75c4 | 1146 | /* |
28da9fb4 JS |
1147 | * if there are no tree operation for the oldest root, we simply |
1148 | * return it. this should only happen if that (old) root is at | |
1149 | * level 0. | |
5d9e75c4 | 1150 | */ |
28da9fb4 JS |
1151 | if (!tm) |
1152 | break; | |
5d9e75c4 | 1153 | |
28da9fb4 JS |
1154 | /* |
1155 | * if there's an operation that's not a root replacement, we | |
1156 | * found the oldest version of our root. normally, we'll find a | |
1157 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. | |
1158 | */ | |
5d9e75c4 JS |
1159 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
1160 | break; | |
1161 | ||
1162 | found = tm; | |
1163 | root_logical = tm->old_root.logical; | |
5d9e75c4 JS |
1164 | looped = 1; |
1165 | } | |
1166 | ||
a95236d9 JS |
1167 | /* if there's no old root to return, return what we found instead */ |
1168 | if (!found) | |
1169 | found = tm; | |
1170 | ||
5d9e75c4 JS |
1171 | return found; |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * tm is a pointer to the first operation to rewind within eb. then, all | |
01327610 | 1176 | * previous operations will be rewound (until we reach something older than |
5d9e75c4 JS |
1177 | * time_seq). |
1178 | */ | |
1179 | static void | |
f1ca7e98 JB |
1180 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
1181 | u64 time_seq, struct tree_mod_elem *first_tm) | |
5d9e75c4 JS |
1182 | { |
1183 | u32 n; | |
1184 | struct rb_node *next; | |
1185 | struct tree_mod_elem *tm = first_tm; | |
1186 | unsigned long o_dst; | |
1187 | unsigned long o_src; | |
1188 | unsigned long p_size = sizeof(struct btrfs_key_ptr); | |
1189 | ||
1190 | n = btrfs_header_nritems(eb); | |
b1a09f1e | 1191 | read_lock(&fs_info->tree_mod_log_lock); |
097b8a7c | 1192 | while (tm && tm->seq >= time_seq) { |
5d9e75c4 JS |
1193 | /* |
1194 | * all the operations are recorded with the operator used for | |
1195 | * the modification. as we're going backwards, we do the | |
1196 | * opposite of each operation here. | |
1197 | */ | |
1198 | switch (tm->op) { | |
1199 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: | |
1200 | BUG_ON(tm->slot < n); | |
c730ae0c | 1201 | fallthrough; |
95c80bb1 | 1202 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
4c3e6969 | 1203 | case MOD_LOG_KEY_REMOVE: |
5d9e75c4 JS |
1204 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
1205 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); | |
1206 | btrfs_set_node_ptr_generation(eb, tm->slot, | |
1207 | tm->generation); | |
4c3e6969 | 1208 | n++; |
5d9e75c4 JS |
1209 | break; |
1210 | case MOD_LOG_KEY_REPLACE: | |
1211 | BUG_ON(tm->slot >= n); | |
1212 | btrfs_set_node_key(eb, &tm->key, tm->slot); | |
1213 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); | |
1214 | btrfs_set_node_ptr_generation(eb, tm->slot, | |
1215 | tm->generation); | |
1216 | break; | |
1217 | case MOD_LOG_KEY_ADD: | |
19956c7e | 1218 | /* if a move operation is needed it's in the log */ |
5d9e75c4 JS |
1219 | n--; |
1220 | break; | |
1221 | case MOD_LOG_MOVE_KEYS: | |
c3193108 JS |
1222 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
1223 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); | |
1224 | memmove_extent_buffer(eb, o_dst, o_src, | |
5d9e75c4 JS |
1225 | tm->move.nr_items * p_size); |
1226 | break; | |
1227 | case MOD_LOG_ROOT_REPLACE: | |
1228 | /* | |
1229 | * this operation is special. for roots, this must be | |
1230 | * handled explicitly before rewinding. | |
1231 | * for non-roots, this operation may exist if the node | |
1232 | * was a root: root A -> child B; then A gets empty and | |
1233 | * B is promoted to the new root. in the mod log, we'll | |
1234 | * have a root-replace operation for B, a tree block | |
1235 | * that is no root. we simply ignore that operation. | |
1236 | */ | |
1237 | break; | |
1238 | } | |
1239 | next = rb_next(&tm->node); | |
1240 | if (!next) | |
1241 | break; | |
6b4df8b6 | 1242 | tm = rb_entry(next, struct tree_mod_elem, node); |
298cfd36 | 1243 | if (tm->logical != first_tm->logical) |
5d9e75c4 JS |
1244 | break; |
1245 | } | |
b1a09f1e | 1246 | read_unlock(&fs_info->tree_mod_log_lock); |
5d9e75c4 JS |
1247 | btrfs_set_header_nritems(eb, n); |
1248 | } | |
1249 | ||
47fb091f | 1250 | /* |
01327610 | 1251 | * Called with eb read locked. If the buffer cannot be rewound, the same buffer |
47fb091f JS |
1252 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
1253 | * returned buffer is always read-locked. If the returned buffer is not the | |
1254 | * input buffer, the lock on the input buffer is released and the input buffer | |
1255 | * is freed (its refcount is decremented). | |
1256 | */ | |
5d9e75c4 | 1257 | static struct extent_buffer * |
9ec72677 JB |
1258 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
1259 | struct extent_buffer *eb, u64 time_seq) | |
5d9e75c4 JS |
1260 | { |
1261 | struct extent_buffer *eb_rewin; | |
1262 | struct tree_mod_elem *tm; | |
1263 | ||
1264 | if (!time_seq) | |
1265 | return eb; | |
1266 | ||
1267 | if (btrfs_header_level(eb) == 0) | |
1268 | return eb; | |
1269 | ||
1270 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); | |
1271 | if (!tm) | |
1272 | return eb; | |
1273 | ||
9ec72677 | 1274 | btrfs_set_path_blocking(path); |
300aa896 | 1275 | btrfs_set_lock_blocking_read(eb); |
9ec72677 | 1276 | |
5d9e75c4 JS |
1277 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
1278 | BUG_ON(tm->slot != 0); | |
da17066c | 1279 | eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); |
db7f3436 | 1280 | if (!eb_rewin) { |
9ec72677 | 1281 | btrfs_tree_read_unlock_blocking(eb); |
db7f3436 JB |
1282 | free_extent_buffer(eb); |
1283 | return NULL; | |
1284 | } | |
5d9e75c4 JS |
1285 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
1286 | btrfs_set_header_backref_rev(eb_rewin, | |
1287 | btrfs_header_backref_rev(eb)); | |
1288 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); | |
c3193108 | 1289 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
5d9e75c4 JS |
1290 | } else { |
1291 | eb_rewin = btrfs_clone_extent_buffer(eb); | |
db7f3436 | 1292 | if (!eb_rewin) { |
9ec72677 | 1293 | btrfs_tree_read_unlock_blocking(eb); |
db7f3436 JB |
1294 | free_extent_buffer(eb); |
1295 | return NULL; | |
1296 | } | |
5d9e75c4 JS |
1297 | } |
1298 | ||
9ec72677 | 1299 | btrfs_tree_read_unlock_blocking(eb); |
5d9e75c4 JS |
1300 | free_extent_buffer(eb); |
1301 | ||
d3beaa25 JB |
1302 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin), |
1303 | eb_rewin, btrfs_header_level(eb_rewin)); | |
47fb091f | 1304 | btrfs_tree_read_lock(eb_rewin); |
f1ca7e98 | 1305 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
57911b8b | 1306 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
da17066c | 1307 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
5d9e75c4 JS |
1308 | |
1309 | return eb_rewin; | |
1310 | } | |
1311 | ||
8ba97a15 JS |
1312 | /* |
1313 | * get_old_root() rewinds the state of @root's root node to the given @time_seq | |
1314 | * value. If there are no changes, the current root->root_node is returned. If | |
1315 | * anything changed in between, there's a fresh buffer allocated on which the | |
1316 | * rewind operations are done. In any case, the returned buffer is read locked. | |
1317 | * Returns NULL on error (with no locks held). | |
1318 | */ | |
5d9e75c4 JS |
1319 | static inline struct extent_buffer * |
1320 | get_old_root(struct btrfs_root *root, u64 time_seq) | |
1321 | { | |
0b246afa | 1322 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d9e75c4 | 1323 | struct tree_mod_elem *tm; |
30b0463a JS |
1324 | struct extent_buffer *eb = NULL; |
1325 | struct extent_buffer *eb_root; | |
efad8a85 | 1326 | u64 eb_root_owner = 0; |
7bfdcf7f | 1327 | struct extent_buffer *old; |
a95236d9 | 1328 | struct tree_mod_root *old_root = NULL; |
4325edd0 | 1329 | u64 old_generation = 0; |
a95236d9 | 1330 | u64 logical; |
581c1760 | 1331 | int level; |
5d9e75c4 | 1332 | |
30b0463a | 1333 | eb_root = btrfs_read_lock_root_node(root); |
bcd24dab | 1334 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
5d9e75c4 | 1335 | if (!tm) |
30b0463a | 1336 | return eb_root; |
5d9e75c4 | 1337 | |
a95236d9 JS |
1338 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
1339 | old_root = &tm->old_root; | |
1340 | old_generation = tm->generation; | |
1341 | logical = old_root->logical; | |
581c1760 | 1342 | level = old_root->level; |
a95236d9 | 1343 | } else { |
30b0463a | 1344 | logical = eb_root->start; |
581c1760 | 1345 | level = btrfs_header_level(eb_root); |
a95236d9 | 1346 | } |
5d9e75c4 | 1347 | |
0b246afa | 1348 | tm = tree_mod_log_search(fs_info, logical, time_seq); |
834328a8 | 1349 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
30b0463a JS |
1350 | btrfs_tree_read_unlock(eb_root); |
1351 | free_extent_buffer(eb_root); | |
581c1760 | 1352 | old = read_tree_block(fs_info, logical, 0, level, NULL); |
64c043de LB |
1353 | if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { |
1354 | if (!IS_ERR(old)) | |
1355 | free_extent_buffer(old); | |
0b246afa JM |
1356 | btrfs_warn(fs_info, |
1357 | "failed to read tree block %llu from get_old_root", | |
1358 | logical); | |
834328a8 | 1359 | } else { |
7bfdcf7f LB |
1360 | eb = btrfs_clone_extent_buffer(old); |
1361 | free_extent_buffer(old); | |
834328a8 JS |
1362 | } |
1363 | } else if (old_root) { | |
efad8a85 | 1364 | eb_root_owner = btrfs_header_owner(eb_root); |
30b0463a JS |
1365 | btrfs_tree_read_unlock(eb_root); |
1366 | free_extent_buffer(eb_root); | |
0b246afa | 1367 | eb = alloc_dummy_extent_buffer(fs_info, logical); |
834328a8 | 1368 | } else { |
300aa896 | 1369 | btrfs_set_lock_blocking_read(eb_root); |
30b0463a | 1370 | eb = btrfs_clone_extent_buffer(eb_root); |
9ec72677 | 1371 | btrfs_tree_read_unlock_blocking(eb_root); |
30b0463a | 1372 | free_extent_buffer(eb_root); |
834328a8 JS |
1373 | } |
1374 | ||
8ba97a15 JS |
1375 | if (!eb) |
1376 | return NULL; | |
a95236d9 | 1377 | if (old_root) { |
5d9e75c4 JS |
1378 | btrfs_set_header_bytenr(eb, eb->start); |
1379 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); | |
efad8a85 | 1380 | btrfs_set_header_owner(eb, eb_root_owner); |
a95236d9 JS |
1381 | btrfs_set_header_level(eb, old_root->level); |
1382 | btrfs_set_header_generation(eb, old_generation); | |
5d9e75c4 | 1383 | } |
d3beaa25 JB |
1384 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb, |
1385 | btrfs_header_level(eb)); | |
1386 | btrfs_tree_read_lock(eb); | |
28da9fb4 | 1387 | if (tm) |
0b246afa | 1388 | __tree_mod_log_rewind(fs_info, eb, time_seq, tm); |
28da9fb4 JS |
1389 | else |
1390 | WARN_ON(btrfs_header_level(eb) != 0); | |
0b246afa | 1391 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
5d9e75c4 JS |
1392 | |
1393 | return eb; | |
1394 | } | |
1395 | ||
5b6602e7 JS |
1396 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
1397 | { | |
1398 | struct tree_mod_elem *tm; | |
1399 | int level; | |
30b0463a | 1400 | struct extent_buffer *eb_root = btrfs_root_node(root); |
5b6602e7 | 1401 | |
bcd24dab | 1402 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
5b6602e7 JS |
1403 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
1404 | level = tm->old_root.level; | |
1405 | } else { | |
30b0463a | 1406 | level = btrfs_header_level(eb_root); |
5b6602e7 | 1407 | } |
30b0463a | 1408 | free_extent_buffer(eb_root); |
5b6602e7 JS |
1409 | |
1410 | return level; | |
1411 | } | |
1412 | ||
5d4f98a2 YZ |
1413 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
1414 | struct btrfs_root *root, | |
1415 | struct extent_buffer *buf) | |
1416 | { | |
f5ee5c9a | 1417 | if (btrfs_is_testing(root->fs_info)) |
faa2dbf0 | 1418 | return 0; |
fccb84c9 | 1419 | |
d1980131 DS |
1420 | /* Ensure we can see the FORCE_COW bit */ |
1421 | smp_mb__before_atomic(); | |
f1ebcc74 LB |
1422 | |
1423 | /* | |
1424 | * We do not need to cow a block if | |
1425 | * 1) this block is not created or changed in this transaction; | |
1426 | * 2) this block does not belong to TREE_RELOC tree; | |
1427 | * 3) the root is not forced COW. | |
1428 | * | |
1429 | * What is forced COW: | |
01327610 | 1430 | * when we create snapshot during committing the transaction, |
52042d8e | 1431 | * after we've finished copying src root, we must COW the shared |
f1ebcc74 LB |
1432 | * block to ensure the metadata consistency. |
1433 | */ | |
5d4f98a2 YZ |
1434 | if (btrfs_header_generation(buf) == trans->transid && |
1435 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && | |
1436 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && | |
f1ebcc74 | 1437 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
27cdeb70 | 1438 | !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) |
5d4f98a2 YZ |
1439 | return 0; |
1440 | return 1; | |
1441 | } | |
1442 | ||
d352ac68 CM |
1443 | /* |
1444 | * cows a single block, see __btrfs_cow_block for the real work. | |
01327610 | 1445 | * This version of it has extra checks so that a block isn't COWed more than |
d352ac68 CM |
1446 | * once per transaction, as long as it hasn't been written yet |
1447 | */ | |
d397712b | 1448 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
5f39d397 CM |
1449 | struct btrfs_root *root, struct extent_buffer *buf, |
1450 | struct extent_buffer *parent, int parent_slot, | |
9631e4cc JB |
1451 | struct extent_buffer **cow_ret, |
1452 | enum btrfs_lock_nesting nest) | |
6702ed49 | 1453 | { |
0b246afa | 1454 | struct btrfs_fs_info *fs_info = root->fs_info; |
6702ed49 | 1455 | u64 search_start; |
f510cfec | 1456 | int ret; |
dc17ff8f | 1457 | |
83354f07 JB |
1458 | if (test_bit(BTRFS_ROOT_DELETING, &root->state)) |
1459 | btrfs_err(fs_info, | |
1460 | "COW'ing blocks on a fs root that's being dropped"); | |
1461 | ||
0b246afa | 1462 | if (trans->transaction != fs_info->running_transaction) |
31b1a2bd | 1463 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
c1c9ff7c | 1464 | trans->transid, |
0b246afa | 1465 | fs_info->running_transaction->transid); |
31b1a2bd | 1466 | |
0b246afa | 1467 | if (trans->transid != fs_info->generation) |
31b1a2bd | 1468 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
0b246afa | 1469 | trans->transid, fs_info->generation); |
dc17ff8f | 1470 | |
5d4f98a2 | 1471 | if (!should_cow_block(trans, root, buf)) { |
64c12921 | 1472 | trans->dirty = true; |
6702ed49 CM |
1473 | *cow_ret = buf; |
1474 | return 0; | |
1475 | } | |
c487685d | 1476 | |
ee22184b | 1477 | search_start = buf->start & ~((u64)SZ_1G - 1); |
b4ce94de CM |
1478 | |
1479 | if (parent) | |
8bead258 DS |
1480 | btrfs_set_lock_blocking_write(parent); |
1481 | btrfs_set_lock_blocking_write(buf); | |
b4ce94de | 1482 | |
f616f5cd QW |
1483 | /* |
1484 | * Before CoWing this block for later modification, check if it's | |
1485 | * the subtree root and do the delayed subtree trace if needed. | |
1486 | * | |
1487 | * Also We don't care about the error, as it's handled internally. | |
1488 | */ | |
1489 | btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); | |
f510cfec | 1490 | ret = __btrfs_cow_block(trans, root, buf, parent, |
9631e4cc | 1491 | parent_slot, cow_ret, search_start, 0, nest); |
1abe9b8a | 1492 | |
1493 | trace_btrfs_cow_block(root, buf, *cow_ret); | |
1494 | ||
f510cfec | 1495 | return ret; |
6702ed49 CM |
1496 | } |
1497 | ||
d352ac68 CM |
1498 | /* |
1499 | * helper function for defrag to decide if two blocks pointed to by a | |
1500 | * node are actually close by | |
1501 | */ | |
6b80053d | 1502 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
6702ed49 | 1503 | { |
6b80053d | 1504 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
6702ed49 | 1505 | return 1; |
6b80053d | 1506 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
6702ed49 CM |
1507 | return 1; |
1508 | return 0; | |
1509 | } | |
1510 | ||
ce6ef5ab DS |
1511 | #ifdef __LITTLE_ENDIAN |
1512 | ||
1513 | /* | |
1514 | * Compare two keys, on little-endian the disk order is same as CPU order and | |
1515 | * we can avoid the conversion. | |
1516 | */ | |
1517 | static int comp_keys(const struct btrfs_disk_key *disk_key, | |
1518 | const struct btrfs_key *k2) | |
1519 | { | |
1520 | const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key; | |
1521 | ||
1522 | return btrfs_comp_cpu_keys(k1, k2); | |
1523 | } | |
1524 | ||
1525 | #else | |
1526 | ||
081e9573 CM |
1527 | /* |
1528 | * compare two keys in a memcmp fashion | |
1529 | */ | |
310712b2 OS |
1530 | static int comp_keys(const struct btrfs_disk_key *disk, |
1531 | const struct btrfs_key *k2) | |
081e9573 CM |
1532 | { |
1533 | struct btrfs_key k1; | |
1534 | ||
1535 | btrfs_disk_key_to_cpu(&k1, disk); | |
1536 | ||
20736aba | 1537 | return btrfs_comp_cpu_keys(&k1, k2); |
081e9573 | 1538 | } |
ce6ef5ab | 1539 | #endif |
081e9573 | 1540 | |
f3465ca4 JB |
1541 | /* |
1542 | * same as comp_keys only with two btrfs_key's | |
1543 | */ | |
e1f60a65 | 1544 | int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2) |
f3465ca4 JB |
1545 | { |
1546 | if (k1->objectid > k2->objectid) | |
1547 | return 1; | |
1548 | if (k1->objectid < k2->objectid) | |
1549 | return -1; | |
1550 | if (k1->type > k2->type) | |
1551 | return 1; | |
1552 | if (k1->type < k2->type) | |
1553 | return -1; | |
1554 | if (k1->offset > k2->offset) | |
1555 | return 1; | |
1556 | if (k1->offset < k2->offset) | |
1557 | return -1; | |
1558 | return 0; | |
1559 | } | |
081e9573 | 1560 | |
d352ac68 CM |
1561 | /* |
1562 | * this is used by the defrag code to go through all the | |
1563 | * leaves pointed to by a node and reallocate them so that | |
1564 | * disk order is close to key order | |
1565 | */ | |
6702ed49 | 1566 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
5f39d397 | 1567 | struct btrfs_root *root, struct extent_buffer *parent, |
de78b51a | 1568 | int start_slot, u64 *last_ret, |
a6b6e75e | 1569 | struct btrfs_key *progress) |
6702ed49 | 1570 | { |
0b246afa | 1571 | struct btrfs_fs_info *fs_info = root->fs_info; |
6b80053d | 1572 | struct extent_buffer *cur; |
6702ed49 | 1573 | u64 blocknr; |
ca7a79ad | 1574 | u64 gen; |
e9d0b13b CM |
1575 | u64 search_start = *last_ret; |
1576 | u64 last_block = 0; | |
6702ed49 CM |
1577 | u64 other; |
1578 | u32 parent_nritems; | |
6702ed49 CM |
1579 | int end_slot; |
1580 | int i; | |
1581 | int err = 0; | |
f2183bde | 1582 | int parent_level; |
6b80053d CM |
1583 | int uptodate; |
1584 | u32 blocksize; | |
081e9573 CM |
1585 | int progress_passed = 0; |
1586 | struct btrfs_disk_key disk_key; | |
6702ed49 | 1587 | |
5708b959 | 1588 | parent_level = btrfs_header_level(parent); |
5708b959 | 1589 | |
0b246afa JM |
1590 | WARN_ON(trans->transaction != fs_info->running_transaction); |
1591 | WARN_ON(trans->transid != fs_info->generation); | |
86479a04 | 1592 | |
6b80053d | 1593 | parent_nritems = btrfs_header_nritems(parent); |
0b246afa | 1594 | blocksize = fs_info->nodesize; |
5dfe2be7 | 1595 | end_slot = parent_nritems - 1; |
6702ed49 | 1596 | |
5dfe2be7 | 1597 | if (parent_nritems <= 1) |
6702ed49 CM |
1598 | return 0; |
1599 | ||
8bead258 | 1600 | btrfs_set_lock_blocking_write(parent); |
b4ce94de | 1601 | |
5dfe2be7 | 1602 | for (i = start_slot; i <= end_slot; i++) { |
581c1760 | 1603 | struct btrfs_key first_key; |
6702ed49 | 1604 | int close = 1; |
a6b6e75e | 1605 | |
081e9573 CM |
1606 | btrfs_node_key(parent, &disk_key, i); |
1607 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) | |
1608 | continue; | |
1609 | ||
1610 | progress_passed = 1; | |
6b80053d | 1611 | blocknr = btrfs_node_blockptr(parent, i); |
ca7a79ad | 1612 | gen = btrfs_node_ptr_generation(parent, i); |
581c1760 | 1613 | btrfs_node_key_to_cpu(parent, &first_key, i); |
e9d0b13b CM |
1614 | if (last_block == 0) |
1615 | last_block = blocknr; | |
5708b959 | 1616 | |
6702ed49 | 1617 | if (i > 0) { |
6b80053d CM |
1618 | other = btrfs_node_blockptr(parent, i - 1); |
1619 | close = close_blocks(blocknr, other, blocksize); | |
6702ed49 | 1620 | } |
5dfe2be7 | 1621 | if (!close && i < end_slot) { |
6b80053d CM |
1622 | other = btrfs_node_blockptr(parent, i + 1); |
1623 | close = close_blocks(blocknr, other, blocksize); | |
6702ed49 | 1624 | } |
e9d0b13b CM |
1625 | if (close) { |
1626 | last_block = blocknr; | |
6702ed49 | 1627 | continue; |
e9d0b13b | 1628 | } |
6702ed49 | 1629 | |
0b246afa | 1630 | cur = find_extent_buffer(fs_info, blocknr); |
6b80053d | 1631 | if (cur) |
b9fab919 | 1632 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
6b80053d CM |
1633 | else |
1634 | uptodate = 0; | |
5708b959 | 1635 | if (!cur || !uptodate) { |
6b80053d | 1636 | if (!cur) { |
581c1760 QW |
1637 | cur = read_tree_block(fs_info, blocknr, gen, |
1638 | parent_level - 1, | |
1639 | &first_key); | |
64c043de LB |
1640 | if (IS_ERR(cur)) { |
1641 | return PTR_ERR(cur); | |
1642 | } else if (!extent_buffer_uptodate(cur)) { | |
416bc658 | 1643 | free_extent_buffer(cur); |
97d9a8a4 | 1644 | return -EIO; |
416bc658 | 1645 | } |
6b80053d | 1646 | } else if (!uptodate) { |
581c1760 QW |
1647 | err = btrfs_read_buffer(cur, gen, |
1648 | parent_level - 1,&first_key); | |
018642a1 TI |
1649 | if (err) { |
1650 | free_extent_buffer(cur); | |
1651 | return err; | |
1652 | } | |
f2183bde | 1653 | } |
6702ed49 | 1654 | } |
e9d0b13b | 1655 | if (search_start == 0) |
6b80053d | 1656 | search_start = last_block; |
e9d0b13b | 1657 | |
e7a84565 | 1658 | btrfs_tree_lock(cur); |
8bead258 | 1659 | btrfs_set_lock_blocking_write(cur); |
6b80053d | 1660 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
e7a84565 | 1661 | &cur, search_start, |
6b80053d | 1662 | min(16 * blocksize, |
9631e4cc JB |
1663 | (end_slot - i) * blocksize), |
1664 | BTRFS_NESTING_COW); | |
252c38f0 | 1665 | if (err) { |
e7a84565 | 1666 | btrfs_tree_unlock(cur); |
6b80053d | 1667 | free_extent_buffer(cur); |
6702ed49 | 1668 | break; |
252c38f0 | 1669 | } |
e7a84565 CM |
1670 | search_start = cur->start; |
1671 | last_block = cur->start; | |
f2183bde | 1672 | *last_ret = search_start; |
e7a84565 CM |
1673 | btrfs_tree_unlock(cur); |
1674 | free_extent_buffer(cur); | |
6702ed49 CM |
1675 | } |
1676 | return err; | |
1677 | } | |
1678 | ||
74123bd7 | 1679 | /* |
5f39d397 CM |
1680 | * search for key in the extent_buffer. The items start at offset p, |
1681 | * and they are item_size apart. There are 'max' items in p. | |
1682 | * | |
74123bd7 CM |
1683 | * the slot in the array is returned via slot, and it points to |
1684 | * the place where you would insert key if it is not found in | |
1685 | * the array. | |
1686 | * | |
1687 | * slot may point to max if the key is bigger than all of the keys | |
1688 | */ | |
e02119d5 | 1689 | static noinline int generic_bin_search(struct extent_buffer *eb, |
310712b2 OS |
1690 | unsigned long p, int item_size, |
1691 | const struct btrfs_key *key, | |
e02119d5 | 1692 | int max, int *slot) |
be0e5c09 CM |
1693 | { |
1694 | int low = 0; | |
1695 | int high = max; | |
be0e5c09 | 1696 | int ret; |
5cd17f34 | 1697 | const int key_size = sizeof(struct btrfs_disk_key); |
be0e5c09 | 1698 | |
5e24e9af LB |
1699 | if (low > high) { |
1700 | btrfs_err(eb->fs_info, | |
1701 | "%s: low (%d) > high (%d) eb %llu owner %llu level %d", | |
1702 | __func__, low, high, eb->start, | |
1703 | btrfs_header_owner(eb), btrfs_header_level(eb)); | |
1704 | return -EINVAL; | |
1705 | } | |
1706 | ||
d397712b | 1707 | while (low < high) { |
5cd17f34 DS |
1708 | unsigned long oip; |
1709 | unsigned long offset; | |
1710 | struct btrfs_disk_key *tmp; | |
1711 | struct btrfs_disk_key unaligned; | |
1712 | int mid; | |
1713 | ||
be0e5c09 | 1714 | mid = (low + high) / 2; |
5f39d397 | 1715 | offset = p + mid * item_size; |
5cd17f34 | 1716 | oip = offset_in_page(offset); |
5f39d397 | 1717 | |
5cd17f34 DS |
1718 | if (oip + key_size <= PAGE_SIZE) { |
1719 | const unsigned long idx = offset >> PAGE_SHIFT; | |
1720 | char *kaddr = page_address(eb->pages[idx]); | |
5f39d397 | 1721 | |
5cd17f34 | 1722 | tmp = (struct btrfs_disk_key *)(kaddr + oip); |
5f39d397 | 1723 | } else { |
5cd17f34 DS |
1724 | read_extent_buffer(eb, &unaligned, offset, key_size); |
1725 | tmp = &unaligned; | |
5f39d397 | 1726 | } |
5cd17f34 | 1727 | |
be0e5c09 CM |
1728 | ret = comp_keys(tmp, key); |
1729 | ||
1730 | if (ret < 0) | |
1731 | low = mid + 1; | |
1732 | else if (ret > 0) | |
1733 | high = mid; | |
1734 | else { | |
1735 | *slot = mid; | |
1736 | return 0; | |
1737 | } | |
1738 | } | |
1739 | *slot = low; | |
1740 | return 1; | |
1741 | } | |
1742 | ||
97571fd0 CM |
1743 | /* |
1744 | * simple bin_search frontend that does the right thing for | |
1745 | * leaves vs nodes | |
1746 | */ | |
a74b35ec | 1747 | int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key, |
e3b83361 | 1748 | int *slot) |
be0e5c09 | 1749 | { |
e3b83361 | 1750 | if (btrfs_header_level(eb) == 0) |
5f39d397 CM |
1751 | return generic_bin_search(eb, |
1752 | offsetof(struct btrfs_leaf, items), | |
0783fcfc | 1753 | sizeof(struct btrfs_item), |
5f39d397 | 1754 | key, btrfs_header_nritems(eb), |
7518a238 | 1755 | slot); |
f775738f | 1756 | else |
5f39d397 CM |
1757 | return generic_bin_search(eb, |
1758 | offsetof(struct btrfs_node, ptrs), | |
123abc88 | 1759 | sizeof(struct btrfs_key_ptr), |
5f39d397 | 1760 | key, btrfs_header_nritems(eb), |
7518a238 | 1761 | slot); |
be0e5c09 CM |
1762 | } |
1763 | ||
f0486c68 YZ |
1764 | static void root_add_used(struct btrfs_root *root, u32 size) |
1765 | { | |
1766 | spin_lock(&root->accounting_lock); | |
1767 | btrfs_set_root_used(&root->root_item, | |
1768 | btrfs_root_used(&root->root_item) + size); | |
1769 | spin_unlock(&root->accounting_lock); | |
1770 | } | |
1771 | ||
1772 | static void root_sub_used(struct btrfs_root *root, u32 size) | |
1773 | { | |
1774 | spin_lock(&root->accounting_lock); | |
1775 | btrfs_set_root_used(&root->root_item, | |
1776 | btrfs_root_used(&root->root_item) - size); | |
1777 | spin_unlock(&root->accounting_lock); | |
1778 | } | |
1779 | ||
d352ac68 CM |
1780 | /* given a node and slot number, this reads the blocks it points to. The |
1781 | * extent buffer is returned with a reference taken (but unlocked). | |
d352ac68 | 1782 | */ |
4b231ae4 DS |
1783 | struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent, |
1784 | int slot) | |
bb803951 | 1785 | { |
ca7a79ad | 1786 | int level = btrfs_header_level(parent); |
416bc658 | 1787 | struct extent_buffer *eb; |
581c1760 | 1788 | struct btrfs_key first_key; |
416bc658 | 1789 | |
fb770ae4 LB |
1790 | if (slot < 0 || slot >= btrfs_header_nritems(parent)) |
1791 | return ERR_PTR(-ENOENT); | |
ca7a79ad CM |
1792 | |
1793 | BUG_ON(level == 0); | |
1794 | ||
581c1760 | 1795 | btrfs_node_key_to_cpu(parent, &first_key, slot); |
d0d20b0f | 1796 | eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), |
581c1760 QW |
1797 | btrfs_node_ptr_generation(parent, slot), |
1798 | level - 1, &first_key); | |
fb770ae4 LB |
1799 | if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { |
1800 | free_extent_buffer(eb); | |
1801 | eb = ERR_PTR(-EIO); | |
416bc658 JB |
1802 | } |
1803 | ||
1804 | return eb; | |
bb803951 CM |
1805 | } |
1806 | ||
d352ac68 CM |
1807 | /* |
1808 | * node level balancing, used to make sure nodes are in proper order for | |
1809 | * item deletion. We balance from the top down, so we have to make sure | |
1810 | * that a deletion won't leave an node completely empty later on. | |
1811 | */ | |
e02119d5 | 1812 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
98ed5174 CM |
1813 | struct btrfs_root *root, |
1814 | struct btrfs_path *path, int level) | |
bb803951 | 1815 | { |
0b246afa | 1816 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 CM |
1817 | struct extent_buffer *right = NULL; |
1818 | struct extent_buffer *mid; | |
1819 | struct extent_buffer *left = NULL; | |
1820 | struct extent_buffer *parent = NULL; | |
bb803951 CM |
1821 | int ret = 0; |
1822 | int wret; | |
1823 | int pslot; | |
bb803951 | 1824 | int orig_slot = path->slots[level]; |
79f95c82 | 1825 | u64 orig_ptr; |
bb803951 | 1826 | |
98e6b1eb | 1827 | ASSERT(level > 0); |
bb803951 | 1828 | |
5f39d397 | 1829 | mid = path->nodes[level]; |
b4ce94de | 1830 | |
bd681513 CM |
1831 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
1832 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); | |
7bb86316 CM |
1833 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
1834 | ||
1d4f8a0c | 1835 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
79f95c82 | 1836 | |
a05a9bb1 | 1837 | if (level < BTRFS_MAX_LEVEL - 1) { |
5f39d397 | 1838 | parent = path->nodes[level + 1]; |
a05a9bb1 LZ |
1839 | pslot = path->slots[level + 1]; |
1840 | } | |
bb803951 | 1841 | |
40689478 CM |
1842 | /* |
1843 | * deal with the case where there is only one pointer in the root | |
1844 | * by promoting the node below to a root | |
1845 | */ | |
5f39d397 CM |
1846 | if (!parent) { |
1847 | struct extent_buffer *child; | |
bb803951 | 1848 | |
5f39d397 | 1849 | if (btrfs_header_nritems(mid) != 1) |
bb803951 CM |
1850 | return 0; |
1851 | ||
1852 | /* promote the child to a root */ | |
4b231ae4 | 1853 | child = btrfs_read_node_slot(mid, 0); |
fb770ae4 LB |
1854 | if (IS_ERR(child)) { |
1855 | ret = PTR_ERR(child); | |
0b246afa | 1856 | btrfs_handle_fs_error(fs_info, ret, NULL); |
305a26af MF |
1857 | goto enospc; |
1858 | } | |
1859 | ||
925baedd | 1860 | btrfs_tree_lock(child); |
8bead258 | 1861 | btrfs_set_lock_blocking_write(child); |
9631e4cc JB |
1862 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child, |
1863 | BTRFS_NESTING_COW); | |
f0486c68 YZ |
1864 | if (ret) { |
1865 | btrfs_tree_unlock(child); | |
1866 | free_extent_buffer(child); | |
1867 | goto enospc; | |
1868 | } | |
2f375ab9 | 1869 | |
d9d19a01 DS |
1870 | ret = tree_mod_log_insert_root(root->node, child, 1); |
1871 | BUG_ON(ret < 0); | |
240f62c8 | 1872 | rcu_assign_pointer(root->node, child); |
925baedd | 1873 | |
0b86a832 | 1874 | add_root_to_dirty_list(root); |
925baedd | 1875 | btrfs_tree_unlock(child); |
b4ce94de | 1876 | |
925baedd | 1877 | path->locks[level] = 0; |
bb803951 | 1878 | path->nodes[level] = NULL; |
6a884d7d | 1879 | btrfs_clean_tree_block(mid); |
925baedd | 1880 | btrfs_tree_unlock(mid); |
bb803951 | 1881 | /* once for the path */ |
5f39d397 | 1882 | free_extent_buffer(mid); |
f0486c68 YZ |
1883 | |
1884 | root_sub_used(root, mid->len); | |
5581a51a | 1885 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
bb803951 | 1886 | /* once for the root ptr */ |
3083ee2e | 1887 | free_extent_buffer_stale(mid); |
f0486c68 | 1888 | return 0; |
bb803951 | 1889 | } |
5f39d397 | 1890 | if (btrfs_header_nritems(mid) > |
0b246afa | 1891 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) |
bb803951 CM |
1892 | return 0; |
1893 | ||
4b231ae4 | 1894 | left = btrfs_read_node_slot(parent, pslot - 1); |
fb770ae4 LB |
1895 | if (IS_ERR(left)) |
1896 | left = NULL; | |
1897 | ||
5f39d397 | 1898 | if (left) { |
925baedd | 1899 | btrfs_tree_lock(left); |
8bead258 | 1900 | btrfs_set_lock_blocking_write(left); |
5f39d397 | 1901 | wret = btrfs_cow_block(trans, root, left, |
9631e4cc JB |
1902 | parent, pslot - 1, &left, |
1903 | BTRFS_NESTING_COW); | |
54aa1f4d CM |
1904 | if (wret) { |
1905 | ret = wret; | |
1906 | goto enospc; | |
1907 | } | |
2cc58cf2 | 1908 | } |
fb770ae4 | 1909 | |
4b231ae4 | 1910 | right = btrfs_read_node_slot(parent, pslot + 1); |
fb770ae4 LB |
1911 | if (IS_ERR(right)) |
1912 | right = NULL; | |
1913 | ||
5f39d397 | 1914 | if (right) { |
925baedd | 1915 | btrfs_tree_lock(right); |
8bead258 | 1916 | btrfs_set_lock_blocking_write(right); |
5f39d397 | 1917 | wret = btrfs_cow_block(trans, root, right, |
9631e4cc JB |
1918 | parent, pslot + 1, &right, |
1919 | BTRFS_NESTING_COW); | |
2cc58cf2 CM |
1920 | if (wret) { |
1921 | ret = wret; | |
1922 | goto enospc; | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | /* first, try to make some room in the middle buffer */ | |
5f39d397 CM |
1927 | if (left) { |
1928 | orig_slot += btrfs_header_nritems(left); | |
d30a668f | 1929 | wret = push_node_left(trans, left, mid, 1); |
79f95c82 CM |
1930 | if (wret < 0) |
1931 | ret = wret; | |
bb803951 | 1932 | } |
79f95c82 CM |
1933 | |
1934 | /* | |
1935 | * then try to empty the right most buffer into the middle | |
1936 | */ | |
5f39d397 | 1937 | if (right) { |
d30a668f | 1938 | wret = push_node_left(trans, mid, right, 1); |
54aa1f4d | 1939 | if (wret < 0 && wret != -ENOSPC) |
79f95c82 | 1940 | ret = wret; |
5f39d397 | 1941 | if (btrfs_header_nritems(right) == 0) { |
6a884d7d | 1942 | btrfs_clean_tree_block(right); |
925baedd | 1943 | btrfs_tree_unlock(right); |
afe5fea7 | 1944 | del_ptr(root, path, level + 1, pslot + 1); |
f0486c68 | 1945 | root_sub_used(root, right->len); |
5581a51a | 1946 | btrfs_free_tree_block(trans, root, right, 0, 1); |
3083ee2e | 1947 | free_extent_buffer_stale(right); |
f0486c68 | 1948 | right = NULL; |
bb803951 | 1949 | } else { |
5f39d397 CM |
1950 | struct btrfs_disk_key right_key; |
1951 | btrfs_node_key(right, &right_key, 0); | |
0e82bcfe DS |
1952 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
1953 | MOD_LOG_KEY_REPLACE, GFP_NOFS); | |
1954 | BUG_ON(ret < 0); | |
5f39d397 CM |
1955 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
1956 | btrfs_mark_buffer_dirty(parent); | |
bb803951 CM |
1957 | } |
1958 | } | |
5f39d397 | 1959 | if (btrfs_header_nritems(mid) == 1) { |
79f95c82 CM |
1960 | /* |
1961 | * we're not allowed to leave a node with one item in the | |
1962 | * tree during a delete. A deletion from lower in the tree | |
1963 | * could try to delete the only pointer in this node. | |
1964 | * So, pull some keys from the left. | |
1965 | * There has to be a left pointer at this point because | |
1966 | * otherwise we would have pulled some pointers from the | |
1967 | * right | |
1968 | */ | |
305a26af MF |
1969 | if (!left) { |
1970 | ret = -EROFS; | |
0b246afa | 1971 | btrfs_handle_fs_error(fs_info, ret, NULL); |
305a26af MF |
1972 | goto enospc; |
1973 | } | |
55d32ed8 | 1974 | wret = balance_node_right(trans, mid, left); |
54aa1f4d | 1975 | if (wret < 0) { |
79f95c82 | 1976 | ret = wret; |
54aa1f4d CM |
1977 | goto enospc; |
1978 | } | |
bce4eae9 | 1979 | if (wret == 1) { |
d30a668f | 1980 | wret = push_node_left(trans, left, mid, 1); |
bce4eae9 CM |
1981 | if (wret < 0) |
1982 | ret = wret; | |
1983 | } | |
79f95c82 CM |
1984 | BUG_ON(wret == 1); |
1985 | } | |
5f39d397 | 1986 | if (btrfs_header_nritems(mid) == 0) { |
6a884d7d | 1987 | btrfs_clean_tree_block(mid); |
925baedd | 1988 | btrfs_tree_unlock(mid); |
afe5fea7 | 1989 | del_ptr(root, path, level + 1, pslot); |
f0486c68 | 1990 | root_sub_used(root, mid->len); |
5581a51a | 1991 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
3083ee2e | 1992 | free_extent_buffer_stale(mid); |
f0486c68 | 1993 | mid = NULL; |
79f95c82 CM |
1994 | } else { |
1995 | /* update the parent key to reflect our changes */ | |
5f39d397 CM |
1996 | struct btrfs_disk_key mid_key; |
1997 | btrfs_node_key(mid, &mid_key, 0); | |
0e82bcfe DS |
1998 | ret = tree_mod_log_insert_key(parent, pslot, |
1999 | MOD_LOG_KEY_REPLACE, GFP_NOFS); | |
2000 | BUG_ON(ret < 0); | |
5f39d397 CM |
2001 | btrfs_set_node_key(parent, &mid_key, pslot); |
2002 | btrfs_mark_buffer_dirty(parent); | |
79f95c82 | 2003 | } |
bb803951 | 2004 | |
79f95c82 | 2005 | /* update the path */ |
5f39d397 CM |
2006 | if (left) { |
2007 | if (btrfs_header_nritems(left) > orig_slot) { | |
67439dad | 2008 | atomic_inc(&left->refs); |
925baedd | 2009 | /* left was locked after cow */ |
5f39d397 | 2010 | path->nodes[level] = left; |
bb803951 CM |
2011 | path->slots[level + 1] -= 1; |
2012 | path->slots[level] = orig_slot; | |
925baedd CM |
2013 | if (mid) { |
2014 | btrfs_tree_unlock(mid); | |
5f39d397 | 2015 | free_extent_buffer(mid); |
925baedd | 2016 | } |
bb803951 | 2017 | } else { |
5f39d397 | 2018 | orig_slot -= btrfs_header_nritems(left); |
bb803951 CM |
2019 | path->slots[level] = orig_slot; |
2020 | } | |
2021 | } | |
79f95c82 | 2022 | /* double check we haven't messed things up */ |
e20d96d6 | 2023 | if (orig_ptr != |
5f39d397 | 2024 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
79f95c82 | 2025 | BUG(); |
54aa1f4d | 2026 | enospc: |
925baedd CM |
2027 | if (right) { |
2028 | btrfs_tree_unlock(right); | |
5f39d397 | 2029 | free_extent_buffer(right); |
925baedd CM |
2030 | } |
2031 | if (left) { | |
2032 | if (path->nodes[level] != left) | |
2033 | btrfs_tree_unlock(left); | |
5f39d397 | 2034 | free_extent_buffer(left); |
925baedd | 2035 | } |
bb803951 CM |
2036 | return ret; |
2037 | } | |
2038 | ||
d352ac68 CM |
2039 | /* Node balancing for insertion. Here we only split or push nodes around |
2040 | * when they are completely full. This is also done top down, so we | |
2041 | * have to be pessimistic. | |
2042 | */ | |
d397712b | 2043 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
98ed5174 CM |
2044 | struct btrfs_root *root, |
2045 | struct btrfs_path *path, int level) | |
e66f709b | 2046 | { |
0b246afa | 2047 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 CM |
2048 | struct extent_buffer *right = NULL; |
2049 | struct extent_buffer *mid; | |
2050 | struct extent_buffer *left = NULL; | |
2051 | struct extent_buffer *parent = NULL; | |
e66f709b CM |
2052 | int ret = 0; |
2053 | int wret; | |
2054 | int pslot; | |
2055 | int orig_slot = path->slots[level]; | |
e66f709b CM |
2056 | |
2057 | if (level == 0) | |
2058 | return 1; | |
2059 | ||
5f39d397 | 2060 | mid = path->nodes[level]; |
7bb86316 | 2061 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
e66f709b | 2062 | |
a05a9bb1 | 2063 | if (level < BTRFS_MAX_LEVEL - 1) { |
5f39d397 | 2064 | parent = path->nodes[level + 1]; |
a05a9bb1 LZ |
2065 | pslot = path->slots[level + 1]; |
2066 | } | |
e66f709b | 2067 | |
5f39d397 | 2068 | if (!parent) |
e66f709b | 2069 | return 1; |
e66f709b | 2070 | |
4b231ae4 | 2071 | left = btrfs_read_node_slot(parent, pslot - 1); |
fb770ae4 LB |
2072 | if (IS_ERR(left)) |
2073 | left = NULL; | |
e66f709b CM |
2074 | |
2075 | /* first, try to make some room in the middle buffer */ | |
5f39d397 | 2076 | if (left) { |
e66f709b | 2077 | u32 left_nr; |
925baedd CM |
2078 | |
2079 | btrfs_tree_lock(left); | |
8bead258 | 2080 | btrfs_set_lock_blocking_write(left); |
b4ce94de | 2081 | |
5f39d397 | 2082 | left_nr = btrfs_header_nritems(left); |
0b246afa | 2083 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
33ade1f8 CM |
2084 | wret = 1; |
2085 | } else { | |
5f39d397 | 2086 | ret = btrfs_cow_block(trans, root, left, parent, |
9631e4cc JB |
2087 | pslot - 1, &left, |
2088 | BTRFS_NESTING_COW); | |
54aa1f4d CM |
2089 | if (ret) |
2090 | wret = 1; | |
2091 | else { | |
d30a668f | 2092 | wret = push_node_left(trans, left, mid, 0); |
54aa1f4d | 2093 | } |
33ade1f8 | 2094 | } |
e66f709b CM |
2095 | if (wret < 0) |
2096 | ret = wret; | |
2097 | if (wret == 0) { | |
5f39d397 | 2098 | struct btrfs_disk_key disk_key; |
e66f709b | 2099 | orig_slot += left_nr; |
5f39d397 | 2100 | btrfs_node_key(mid, &disk_key, 0); |
0e82bcfe DS |
2101 | ret = tree_mod_log_insert_key(parent, pslot, |
2102 | MOD_LOG_KEY_REPLACE, GFP_NOFS); | |
2103 | BUG_ON(ret < 0); | |
5f39d397 CM |
2104 | btrfs_set_node_key(parent, &disk_key, pslot); |
2105 | btrfs_mark_buffer_dirty(parent); | |
2106 | if (btrfs_header_nritems(left) > orig_slot) { | |
2107 | path->nodes[level] = left; | |
e66f709b CM |
2108 | path->slots[level + 1] -= 1; |
2109 | path->slots[level] = orig_slot; | |
925baedd | 2110 | btrfs_tree_unlock(mid); |
5f39d397 | 2111 | free_extent_buffer(mid); |
e66f709b CM |
2112 | } else { |
2113 | orig_slot -= | |
5f39d397 | 2114 | btrfs_header_nritems(left); |
e66f709b | 2115 | path->slots[level] = orig_slot; |
925baedd | 2116 | btrfs_tree_unlock(left); |
5f39d397 | 2117 | free_extent_buffer(left); |
e66f709b | 2118 | } |
e66f709b CM |
2119 | return 0; |
2120 | } | |
925baedd | 2121 | btrfs_tree_unlock(left); |
5f39d397 | 2122 | free_extent_buffer(left); |
e66f709b | 2123 | } |
4b231ae4 | 2124 | right = btrfs_read_node_slot(parent, pslot + 1); |
fb770ae4 LB |
2125 | if (IS_ERR(right)) |
2126 | right = NULL; | |
e66f709b CM |
2127 | |
2128 | /* | |
2129 | * then try to empty the right most buffer into the middle | |
2130 | */ | |
5f39d397 | 2131 | if (right) { |
33ade1f8 | 2132 | u32 right_nr; |
b4ce94de | 2133 | |
925baedd | 2134 | btrfs_tree_lock(right); |
8bead258 | 2135 | btrfs_set_lock_blocking_write(right); |
b4ce94de | 2136 | |
5f39d397 | 2137 | right_nr = btrfs_header_nritems(right); |
0b246afa | 2138 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
33ade1f8 CM |
2139 | wret = 1; |
2140 | } else { | |
5f39d397 CM |
2141 | ret = btrfs_cow_block(trans, root, right, |
2142 | parent, pslot + 1, | |
9631e4cc | 2143 | &right, BTRFS_NESTING_COW); |
54aa1f4d CM |
2144 | if (ret) |
2145 | wret = 1; | |
2146 | else { | |
55d32ed8 | 2147 | wret = balance_node_right(trans, right, mid); |
54aa1f4d | 2148 | } |
33ade1f8 | 2149 | } |
e66f709b CM |
2150 | if (wret < 0) |
2151 | ret = wret; | |
2152 | if (wret == 0) { | |
5f39d397 CM |
2153 | struct btrfs_disk_key disk_key; |
2154 | ||
2155 | btrfs_node_key(right, &disk_key, 0); | |
0e82bcfe DS |
2156 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
2157 | MOD_LOG_KEY_REPLACE, GFP_NOFS); | |
2158 | BUG_ON(ret < 0); | |
5f39d397 CM |
2159 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
2160 | btrfs_mark_buffer_dirty(parent); | |
2161 | ||
2162 | if (btrfs_header_nritems(mid) <= orig_slot) { | |
2163 | path->nodes[level] = right; | |
e66f709b CM |
2164 | path->slots[level + 1] += 1; |
2165 | path->slots[level] = orig_slot - | |
5f39d397 | 2166 | btrfs_header_nritems(mid); |
925baedd | 2167 | btrfs_tree_unlock(mid); |
5f39d397 | 2168 | free_extent_buffer(mid); |
e66f709b | 2169 | } else { |
925baedd | 2170 | btrfs_tree_unlock(right); |
5f39d397 | 2171 | free_extent_buffer(right); |
e66f709b | 2172 | } |
e66f709b CM |
2173 | return 0; |
2174 | } | |
925baedd | 2175 | btrfs_tree_unlock(right); |
5f39d397 | 2176 | free_extent_buffer(right); |
e66f709b | 2177 | } |
e66f709b CM |
2178 | return 1; |
2179 | } | |
2180 | ||
3c69faec | 2181 | /* |
d352ac68 CM |
2182 | * readahead one full node of leaves, finding things that are close |
2183 | * to the block in 'slot', and triggering ra on them. | |
3c69faec | 2184 | */ |
2ff7e61e | 2185 | static void reada_for_search(struct btrfs_fs_info *fs_info, |
c8c42864 CM |
2186 | struct btrfs_path *path, |
2187 | int level, int slot, u64 objectid) | |
3c69faec | 2188 | { |
5f39d397 | 2189 | struct extent_buffer *node; |
01f46658 | 2190 | struct btrfs_disk_key disk_key; |
3c69faec | 2191 | u32 nritems; |
3c69faec | 2192 | u64 search; |
a7175319 | 2193 | u64 target; |
6b80053d | 2194 | u64 nread = 0; |
5f39d397 | 2195 | struct extent_buffer *eb; |
6b80053d CM |
2196 | u32 nr; |
2197 | u32 blocksize; | |
2198 | u32 nscan = 0; | |
db94535d | 2199 | |
a6b6e75e | 2200 | if (level != 1) |
6702ed49 CM |
2201 | return; |
2202 | ||
2203 | if (!path->nodes[level]) | |
3c69faec CM |
2204 | return; |
2205 | ||
5f39d397 | 2206 | node = path->nodes[level]; |
925baedd | 2207 | |
3c69faec | 2208 | search = btrfs_node_blockptr(node, slot); |
0b246afa JM |
2209 | blocksize = fs_info->nodesize; |
2210 | eb = find_extent_buffer(fs_info, search); | |
5f39d397 CM |
2211 | if (eb) { |
2212 | free_extent_buffer(eb); | |
3c69faec CM |
2213 | return; |
2214 | } | |
2215 | ||
a7175319 | 2216 | target = search; |
6b80053d | 2217 | |
5f39d397 | 2218 | nritems = btrfs_header_nritems(node); |
6b80053d | 2219 | nr = slot; |
25b8b936 | 2220 | |
d397712b | 2221 | while (1) { |
e4058b54 | 2222 | if (path->reada == READA_BACK) { |
6b80053d CM |
2223 | if (nr == 0) |
2224 | break; | |
2225 | nr--; | |
e4058b54 | 2226 | } else if (path->reada == READA_FORWARD) { |
6b80053d CM |
2227 | nr++; |
2228 | if (nr >= nritems) | |
2229 | break; | |
3c69faec | 2230 | } |
e4058b54 | 2231 | if (path->reada == READA_BACK && objectid) { |
01f46658 CM |
2232 | btrfs_node_key(node, &disk_key, nr); |
2233 | if (btrfs_disk_key_objectid(&disk_key) != objectid) | |
2234 | break; | |
2235 | } | |
6b80053d | 2236 | search = btrfs_node_blockptr(node, nr); |
a7175319 CM |
2237 | if ((search <= target && target - search <= 65536) || |
2238 | (search > target && search - target <= 65536)) { | |
2ff7e61e | 2239 | readahead_tree_block(fs_info, search); |
6b80053d CM |
2240 | nread += blocksize; |
2241 | } | |
2242 | nscan++; | |
a7175319 | 2243 | if ((nread > 65536 || nscan > 32)) |
6b80053d | 2244 | break; |
3c69faec CM |
2245 | } |
2246 | } | |
925baedd | 2247 | |
2ff7e61e | 2248 | static noinline void reada_for_balance(struct btrfs_fs_info *fs_info, |
0b08851f | 2249 | struct btrfs_path *path, int level) |
b4ce94de CM |
2250 | { |
2251 | int slot; | |
2252 | int nritems; | |
2253 | struct extent_buffer *parent; | |
2254 | struct extent_buffer *eb; | |
2255 | u64 gen; | |
2256 | u64 block1 = 0; | |
2257 | u64 block2 = 0; | |
b4ce94de | 2258 | |
8c594ea8 | 2259 | parent = path->nodes[level + 1]; |
b4ce94de | 2260 | if (!parent) |
0b08851f | 2261 | return; |
b4ce94de CM |
2262 | |
2263 | nritems = btrfs_header_nritems(parent); | |
8c594ea8 | 2264 | slot = path->slots[level + 1]; |
b4ce94de CM |
2265 | |
2266 | if (slot > 0) { | |
2267 | block1 = btrfs_node_blockptr(parent, slot - 1); | |
2268 | gen = btrfs_node_ptr_generation(parent, slot - 1); | |
0b246afa | 2269 | eb = find_extent_buffer(fs_info, block1); |
b9fab919 CM |
2270 | /* |
2271 | * if we get -eagain from btrfs_buffer_uptodate, we | |
2272 | * don't want to return eagain here. That will loop | |
2273 | * forever | |
2274 | */ | |
2275 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) | |
b4ce94de CM |
2276 | block1 = 0; |
2277 | free_extent_buffer(eb); | |
2278 | } | |
8c594ea8 | 2279 | if (slot + 1 < nritems) { |
b4ce94de CM |
2280 | block2 = btrfs_node_blockptr(parent, slot + 1); |
2281 | gen = btrfs_node_ptr_generation(parent, slot + 1); | |
0b246afa | 2282 | eb = find_extent_buffer(fs_info, block2); |
b9fab919 | 2283 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
b4ce94de CM |
2284 | block2 = 0; |
2285 | free_extent_buffer(eb); | |
2286 | } | |
8c594ea8 | 2287 | |
0b08851f | 2288 | if (block1) |
2ff7e61e | 2289 | readahead_tree_block(fs_info, block1); |
0b08851f | 2290 | if (block2) |
2ff7e61e | 2291 | readahead_tree_block(fs_info, block2); |
b4ce94de CM |
2292 | } |
2293 | ||
2294 | ||
d352ac68 | 2295 | /* |
d397712b CM |
2296 | * when we walk down the tree, it is usually safe to unlock the higher layers |
2297 | * in the tree. The exceptions are when our path goes through slot 0, because | |
2298 | * operations on the tree might require changing key pointers higher up in the | |
2299 | * tree. | |
d352ac68 | 2300 | * |
d397712b CM |
2301 | * callers might also have set path->keep_locks, which tells this code to keep |
2302 | * the lock if the path points to the last slot in the block. This is part of | |
2303 | * walking through the tree, and selecting the next slot in the higher block. | |
d352ac68 | 2304 | * |
d397712b CM |
2305 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
2306 | * if lowest_unlock is 1, level 0 won't be unlocked | |
d352ac68 | 2307 | */ |
e02119d5 | 2308 | static noinline void unlock_up(struct btrfs_path *path, int level, |
f7c79f30 CM |
2309 | int lowest_unlock, int min_write_lock_level, |
2310 | int *write_lock_level) | |
925baedd CM |
2311 | { |
2312 | int i; | |
2313 | int skip_level = level; | |
051e1b9f | 2314 | int no_skips = 0; |
925baedd CM |
2315 | struct extent_buffer *t; |
2316 | ||
2317 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { | |
2318 | if (!path->nodes[i]) | |
2319 | break; | |
2320 | if (!path->locks[i]) | |
2321 | break; | |
051e1b9f | 2322 | if (!no_skips && path->slots[i] == 0) { |
925baedd CM |
2323 | skip_level = i + 1; |
2324 | continue; | |
2325 | } | |
051e1b9f | 2326 | if (!no_skips && path->keep_locks) { |
925baedd CM |
2327 | u32 nritems; |
2328 | t = path->nodes[i]; | |
2329 | nritems = btrfs_header_nritems(t); | |
051e1b9f | 2330 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
925baedd CM |
2331 | skip_level = i + 1; |
2332 | continue; | |
2333 | } | |
2334 | } | |
051e1b9f CM |
2335 | if (skip_level < i && i >= lowest_unlock) |
2336 | no_skips = 1; | |
2337 | ||
925baedd | 2338 | t = path->nodes[i]; |
d80bb3f9 | 2339 | if (i >= lowest_unlock && i > skip_level) { |
bd681513 | 2340 | btrfs_tree_unlock_rw(t, path->locks[i]); |
925baedd | 2341 | path->locks[i] = 0; |
f7c79f30 CM |
2342 | if (write_lock_level && |
2343 | i > min_write_lock_level && | |
2344 | i <= *write_lock_level) { | |
2345 | *write_lock_level = i - 1; | |
2346 | } | |
925baedd CM |
2347 | } |
2348 | } | |
2349 | } | |
2350 | ||
c8c42864 CM |
2351 | /* |
2352 | * helper function for btrfs_search_slot. The goal is to find a block | |
2353 | * in cache without setting the path to blocking. If we find the block | |
2354 | * we return zero and the path is unchanged. | |
2355 | * | |
2356 | * If we can't find the block, we set the path blocking and do some | |
2357 | * reada. -EAGAIN is returned and the search must be repeated. | |
2358 | */ | |
2359 | static int | |
d07b8528 LB |
2360 | read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, |
2361 | struct extent_buffer **eb_ret, int level, int slot, | |
cda79c54 | 2362 | const struct btrfs_key *key) |
c8c42864 | 2363 | { |
0b246afa | 2364 | struct btrfs_fs_info *fs_info = root->fs_info; |
c8c42864 CM |
2365 | u64 blocknr; |
2366 | u64 gen; | |
c8c42864 | 2367 | struct extent_buffer *tmp; |
581c1760 | 2368 | struct btrfs_key first_key; |
76a05b35 | 2369 | int ret; |
581c1760 | 2370 | int parent_level; |
c8c42864 | 2371 | |
213ff4b7 NB |
2372 | blocknr = btrfs_node_blockptr(*eb_ret, slot); |
2373 | gen = btrfs_node_ptr_generation(*eb_ret, slot); | |
2374 | parent_level = btrfs_header_level(*eb_ret); | |
2375 | btrfs_node_key_to_cpu(*eb_ret, &first_key, slot); | |
c8c42864 | 2376 | |
0b246afa | 2377 | tmp = find_extent_buffer(fs_info, blocknr); |
cb44921a | 2378 | if (tmp) { |
b9fab919 | 2379 | /* first we do an atomic uptodate check */ |
bdf7c00e | 2380 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
448de471 QW |
2381 | /* |
2382 | * Do extra check for first_key, eb can be stale due to | |
2383 | * being cached, read from scrub, or have multiple | |
2384 | * parents (shared tree blocks). | |
2385 | */ | |
e064d5e9 | 2386 | if (btrfs_verify_level_key(tmp, |
448de471 QW |
2387 | parent_level - 1, &first_key, gen)) { |
2388 | free_extent_buffer(tmp); | |
2389 | return -EUCLEAN; | |
2390 | } | |
bdf7c00e JB |
2391 | *eb_ret = tmp; |
2392 | return 0; | |
2393 | } | |
2394 | ||
2395 | /* the pages were up to date, but we failed | |
2396 | * the generation number check. Do a full | |
2397 | * read for the generation number that is correct. | |
2398 | * We must do this without dropping locks so | |
2399 | * we can trust our generation number | |
2400 | */ | |
2401 | btrfs_set_path_blocking(p); | |
2402 | ||
2403 | /* now we're allowed to do a blocking uptodate check */ | |
581c1760 | 2404 | ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key); |
bdf7c00e JB |
2405 | if (!ret) { |
2406 | *eb_ret = tmp; | |
2407 | return 0; | |
cb44921a | 2408 | } |
bdf7c00e JB |
2409 | free_extent_buffer(tmp); |
2410 | btrfs_release_path(p); | |
2411 | return -EIO; | |
c8c42864 CM |
2412 | } |
2413 | ||
2414 | /* | |
2415 | * reduce lock contention at high levels | |
2416 | * of the btree by dropping locks before | |
76a05b35 CM |
2417 | * we read. Don't release the lock on the current |
2418 | * level because we need to walk this node to figure | |
2419 | * out which blocks to read. | |
c8c42864 | 2420 | */ |
8c594ea8 CM |
2421 | btrfs_unlock_up_safe(p, level + 1); |
2422 | btrfs_set_path_blocking(p); | |
2423 | ||
e4058b54 | 2424 | if (p->reada != READA_NONE) |
2ff7e61e | 2425 | reada_for_search(fs_info, p, level, slot, key->objectid); |
c8c42864 | 2426 | |
76a05b35 | 2427 | ret = -EAGAIN; |
02a3307a | 2428 | tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1, |
581c1760 | 2429 | &first_key); |
64c043de | 2430 | if (!IS_ERR(tmp)) { |
76a05b35 CM |
2431 | /* |
2432 | * If the read above didn't mark this buffer up to date, | |
2433 | * it will never end up being up to date. Set ret to EIO now | |
2434 | * and give up so that our caller doesn't loop forever | |
2435 | * on our EAGAINs. | |
2436 | */ | |
e6a1d6fd | 2437 | if (!extent_buffer_uptodate(tmp)) |
76a05b35 | 2438 | ret = -EIO; |
c8c42864 | 2439 | free_extent_buffer(tmp); |
c871b0f2 LB |
2440 | } else { |
2441 | ret = PTR_ERR(tmp); | |
76a05b35 | 2442 | } |
02a3307a LB |
2443 | |
2444 | btrfs_release_path(p); | |
76a05b35 | 2445 | return ret; |
c8c42864 CM |
2446 | } |
2447 | ||
2448 | /* | |
2449 | * helper function for btrfs_search_slot. This does all of the checks | |
2450 | * for node-level blocks and does any balancing required based on | |
2451 | * the ins_len. | |
2452 | * | |
2453 | * If no extra work was required, zero is returned. If we had to | |
2454 | * drop the path, -EAGAIN is returned and btrfs_search_slot must | |
2455 | * start over | |
2456 | */ | |
2457 | static int | |
2458 | setup_nodes_for_search(struct btrfs_trans_handle *trans, | |
2459 | struct btrfs_root *root, struct btrfs_path *p, | |
bd681513 CM |
2460 | struct extent_buffer *b, int level, int ins_len, |
2461 | int *write_lock_level) | |
c8c42864 | 2462 | { |
0b246afa | 2463 | struct btrfs_fs_info *fs_info = root->fs_info; |
c8c42864 | 2464 | int ret; |
0b246afa | 2465 | |
c8c42864 | 2466 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
0b246afa | 2467 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { |
c8c42864 CM |
2468 | int sret; |
2469 | ||
bd681513 CM |
2470 | if (*write_lock_level < level + 1) { |
2471 | *write_lock_level = level + 1; | |
2472 | btrfs_release_path(p); | |
2473 | goto again; | |
2474 | } | |
2475 | ||
c8c42864 | 2476 | btrfs_set_path_blocking(p); |
2ff7e61e | 2477 | reada_for_balance(fs_info, p, level); |
c8c42864 | 2478 | sret = split_node(trans, root, p, level); |
c8c42864 CM |
2479 | |
2480 | BUG_ON(sret > 0); | |
2481 | if (sret) { | |
2482 | ret = sret; | |
2483 | goto done; | |
2484 | } | |
2485 | b = p->nodes[level]; | |
2486 | } else if (ins_len < 0 && btrfs_header_nritems(b) < | |
0b246afa | 2487 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { |
c8c42864 CM |
2488 | int sret; |
2489 | ||
bd681513 CM |
2490 | if (*write_lock_level < level + 1) { |
2491 | *write_lock_level = level + 1; | |
2492 | btrfs_release_path(p); | |
2493 | goto again; | |
2494 | } | |
2495 | ||
c8c42864 | 2496 | btrfs_set_path_blocking(p); |
2ff7e61e | 2497 | reada_for_balance(fs_info, p, level); |
c8c42864 | 2498 | sret = balance_level(trans, root, p, level); |
c8c42864 CM |
2499 | |
2500 | if (sret) { | |
2501 | ret = sret; | |
2502 | goto done; | |
2503 | } | |
2504 | b = p->nodes[level]; | |
2505 | if (!b) { | |
b3b4aa74 | 2506 | btrfs_release_path(p); |
c8c42864 CM |
2507 | goto again; |
2508 | } | |
2509 | BUG_ON(btrfs_header_nritems(b) == 1); | |
2510 | } | |
2511 | return 0; | |
2512 | ||
2513 | again: | |
2514 | ret = -EAGAIN; | |
2515 | done: | |
2516 | return ret; | |
2517 | } | |
2518 | ||
381cf658 | 2519 | int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, |
e33d5c3d KN |
2520 | u64 iobjectid, u64 ioff, u8 key_type, |
2521 | struct btrfs_key *found_key) | |
2522 | { | |
2523 | int ret; | |
2524 | struct btrfs_key key; | |
2525 | struct extent_buffer *eb; | |
381cf658 DS |
2526 | |
2527 | ASSERT(path); | |
1d4c08e0 | 2528 | ASSERT(found_key); |
e33d5c3d KN |
2529 | |
2530 | key.type = key_type; | |
2531 | key.objectid = iobjectid; | |
2532 | key.offset = ioff; | |
2533 | ||
2534 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); | |
1d4c08e0 | 2535 | if (ret < 0) |
e33d5c3d KN |
2536 | return ret; |
2537 | ||
2538 | eb = path->nodes[0]; | |
2539 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { | |
2540 | ret = btrfs_next_leaf(fs_root, path); | |
2541 | if (ret) | |
2542 | return ret; | |
2543 | eb = path->nodes[0]; | |
2544 | } | |
2545 | ||
2546 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); | |
2547 | if (found_key->type != key.type || | |
2548 | found_key->objectid != key.objectid) | |
2549 | return 1; | |
2550 | ||
2551 | return 0; | |
2552 | } | |
2553 | ||
1fc28d8e LB |
2554 | static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, |
2555 | struct btrfs_path *p, | |
2556 | int write_lock_level) | |
2557 | { | |
2558 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2559 | struct extent_buffer *b; | |
2560 | int root_lock; | |
2561 | int level = 0; | |
2562 | ||
2563 | /* We try very hard to do read locks on the root */ | |
2564 | root_lock = BTRFS_READ_LOCK; | |
2565 | ||
2566 | if (p->search_commit_root) { | |
be6821f8 FM |
2567 | /* |
2568 | * The commit roots are read only so we always do read locks, | |
2569 | * and we always must hold the commit_root_sem when doing | |
2570 | * searches on them, the only exception is send where we don't | |
2571 | * want to block transaction commits for a long time, so | |
2572 | * we need to clone the commit root in order to avoid races | |
2573 | * with transaction commits that create a snapshot of one of | |
2574 | * the roots used by a send operation. | |
2575 | */ | |
2576 | if (p->need_commit_sem) { | |
1fc28d8e | 2577 | down_read(&fs_info->commit_root_sem); |
be6821f8 | 2578 | b = btrfs_clone_extent_buffer(root->commit_root); |
1fc28d8e | 2579 | up_read(&fs_info->commit_root_sem); |
be6821f8 FM |
2580 | if (!b) |
2581 | return ERR_PTR(-ENOMEM); | |
2582 | ||
2583 | } else { | |
2584 | b = root->commit_root; | |
67439dad | 2585 | atomic_inc(&b->refs); |
be6821f8 FM |
2586 | } |
2587 | level = btrfs_header_level(b); | |
f9ddfd05 LB |
2588 | /* |
2589 | * Ensure that all callers have set skip_locking when | |
2590 | * p->search_commit_root = 1. | |
2591 | */ | |
2592 | ASSERT(p->skip_locking == 1); | |
1fc28d8e LB |
2593 | |
2594 | goto out; | |
2595 | } | |
2596 | ||
2597 | if (p->skip_locking) { | |
2598 | b = btrfs_root_node(root); | |
2599 | level = btrfs_header_level(b); | |
2600 | goto out; | |
2601 | } | |
2602 | ||
2603 | /* | |
662c653b LB |
2604 | * If the level is set to maximum, we can skip trying to get the read |
2605 | * lock. | |
1fc28d8e | 2606 | */ |
662c653b LB |
2607 | if (write_lock_level < BTRFS_MAX_LEVEL) { |
2608 | /* | |
2609 | * We don't know the level of the root node until we actually | |
2610 | * have it read locked | |
2611 | */ | |
51899412 | 2612 | b = __btrfs_read_lock_root_node(root, p->recurse); |
662c653b LB |
2613 | level = btrfs_header_level(b); |
2614 | if (level > write_lock_level) | |
2615 | goto out; | |
2616 | ||
2617 | /* Whoops, must trade for write lock */ | |
2618 | btrfs_tree_read_unlock(b); | |
2619 | free_extent_buffer(b); | |
2620 | } | |
1fc28d8e | 2621 | |
1fc28d8e LB |
2622 | b = btrfs_lock_root_node(root); |
2623 | root_lock = BTRFS_WRITE_LOCK; | |
2624 | ||
2625 | /* The level might have changed, check again */ | |
2626 | level = btrfs_header_level(b); | |
2627 | ||
2628 | out: | |
2629 | p->nodes[level] = b; | |
2630 | if (!p->skip_locking) | |
2631 | p->locks[level] = root_lock; | |
2632 | /* | |
2633 | * Callers are responsible for dropping b's references. | |
2634 | */ | |
2635 | return b; | |
2636 | } | |
2637 | ||
2638 | ||
74123bd7 | 2639 | /* |
4271ecea NB |
2640 | * btrfs_search_slot - look for a key in a tree and perform necessary |
2641 | * modifications to preserve tree invariants. | |
74123bd7 | 2642 | * |
4271ecea NB |
2643 | * @trans: Handle of transaction, used when modifying the tree |
2644 | * @p: Holds all btree nodes along the search path | |
2645 | * @root: The root node of the tree | |
2646 | * @key: The key we are looking for | |
2647 | * @ins_len: Indicates purpose of search, for inserts it is 1, for | |
2648 | * deletions it's -1. 0 for plain searches | |
2649 | * @cow: boolean should CoW operations be performed. Must always be 1 | |
2650 | * when modifying the tree. | |
97571fd0 | 2651 | * |
4271ecea NB |
2652 | * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. |
2653 | * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) | |
2654 | * | |
2655 | * If @key is found, 0 is returned and you can find the item in the leaf level | |
2656 | * of the path (level 0) | |
2657 | * | |
2658 | * If @key isn't found, 1 is returned and the leaf level of the path (level 0) | |
2659 | * points to the slot where it should be inserted | |
2660 | * | |
2661 | * If an error is encountered while searching the tree a negative error number | |
2662 | * is returned | |
74123bd7 | 2663 | */ |
310712b2 OS |
2664 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
2665 | const struct btrfs_key *key, struct btrfs_path *p, | |
2666 | int ins_len, int cow) | |
be0e5c09 | 2667 | { |
5f39d397 | 2668 | struct extent_buffer *b; |
be0e5c09 CM |
2669 | int slot; |
2670 | int ret; | |
33c66f43 | 2671 | int err; |
be0e5c09 | 2672 | int level; |
925baedd | 2673 | int lowest_unlock = 1; |
bd681513 CM |
2674 | /* everything at write_lock_level or lower must be write locked */ |
2675 | int write_lock_level = 0; | |
9f3a7427 | 2676 | u8 lowest_level = 0; |
f7c79f30 | 2677 | int min_write_lock_level; |
d7396f07 | 2678 | int prev_cmp; |
9f3a7427 | 2679 | |
6702ed49 | 2680 | lowest_level = p->lowest_level; |
323ac95b | 2681 | WARN_ON(lowest_level && ins_len > 0); |
22b0ebda | 2682 | WARN_ON(p->nodes[0] != NULL); |
eb653de1 | 2683 | BUG_ON(!cow && ins_len); |
25179201 | 2684 | |
bd681513 | 2685 | if (ins_len < 0) { |
925baedd | 2686 | lowest_unlock = 2; |
65b51a00 | 2687 | |
bd681513 CM |
2688 | /* when we are removing items, we might have to go up to level |
2689 | * two as we update tree pointers Make sure we keep write | |
2690 | * for those levels as well | |
2691 | */ | |
2692 | write_lock_level = 2; | |
2693 | } else if (ins_len > 0) { | |
2694 | /* | |
2695 | * for inserting items, make sure we have a write lock on | |
2696 | * level 1 so we can update keys | |
2697 | */ | |
2698 | write_lock_level = 1; | |
2699 | } | |
2700 | ||
2701 | if (!cow) | |
2702 | write_lock_level = -1; | |
2703 | ||
09a2a8f9 | 2704 | if (cow && (p->keep_locks || p->lowest_level)) |
bd681513 CM |
2705 | write_lock_level = BTRFS_MAX_LEVEL; |
2706 | ||
f7c79f30 CM |
2707 | min_write_lock_level = write_lock_level; |
2708 | ||
bb803951 | 2709 | again: |
d7396f07 | 2710 | prev_cmp = -1; |
1fc28d8e | 2711 | b = btrfs_search_slot_get_root(root, p, write_lock_level); |
be6821f8 FM |
2712 | if (IS_ERR(b)) { |
2713 | ret = PTR_ERR(b); | |
2714 | goto done; | |
2715 | } | |
925baedd | 2716 | |
eb60ceac | 2717 | while (b) { |
f624d976 QW |
2718 | int dec = 0; |
2719 | ||
5f39d397 | 2720 | level = btrfs_header_level(b); |
65b51a00 | 2721 | |
02217ed2 | 2722 | if (cow) { |
9ea2c7c9 NB |
2723 | bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); |
2724 | ||
c8c42864 CM |
2725 | /* |
2726 | * if we don't really need to cow this block | |
2727 | * then we don't want to set the path blocking, | |
2728 | * so we test it here | |
2729 | */ | |
64c12921 JM |
2730 | if (!should_cow_block(trans, root, b)) { |
2731 | trans->dirty = true; | |
65b51a00 | 2732 | goto cow_done; |
64c12921 | 2733 | } |
5d4f98a2 | 2734 | |
bd681513 CM |
2735 | /* |
2736 | * must have write locks on this node and the | |
2737 | * parent | |
2738 | */ | |
5124e00e JB |
2739 | if (level > write_lock_level || |
2740 | (level + 1 > write_lock_level && | |
2741 | level + 1 < BTRFS_MAX_LEVEL && | |
2742 | p->nodes[level + 1])) { | |
bd681513 CM |
2743 | write_lock_level = level + 1; |
2744 | btrfs_release_path(p); | |
2745 | goto again; | |
2746 | } | |
2747 | ||
160f4089 | 2748 | btrfs_set_path_blocking(p); |
9ea2c7c9 NB |
2749 | if (last_level) |
2750 | err = btrfs_cow_block(trans, root, b, NULL, 0, | |
9631e4cc JB |
2751 | &b, |
2752 | BTRFS_NESTING_COW); | |
9ea2c7c9 NB |
2753 | else |
2754 | err = btrfs_cow_block(trans, root, b, | |
2755 | p->nodes[level + 1], | |
9631e4cc JB |
2756 | p->slots[level + 1], &b, |
2757 | BTRFS_NESTING_COW); | |
33c66f43 | 2758 | if (err) { |
33c66f43 | 2759 | ret = err; |
65b51a00 | 2760 | goto done; |
54aa1f4d | 2761 | } |
02217ed2 | 2762 | } |
65b51a00 | 2763 | cow_done: |
eb60ceac | 2764 | p->nodes[level] = b; |
52398340 LB |
2765 | /* |
2766 | * Leave path with blocking locks to avoid massive | |
2767 | * lock context switch, this is made on purpose. | |
2768 | */ | |
b4ce94de CM |
2769 | |
2770 | /* | |
2771 | * we have a lock on b and as long as we aren't changing | |
2772 | * the tree, there is no way to for the items in b to change. | |
2773 | * It is safe to drop the lock on our parent before we | |
2774 | * go through the expensive btree search on b. | |
2775 | * | |
eb653de1 FDBM |
2776 | * If we're inserting or deleting (ins_len != 0), then we might |
2777 | * be changing slot zero, which may require changing the parent. | |
2778 | * So, we can't drop the lock until after we know which slot | |
2779 | * we're operating on. | |
b4ce94de | 2780 | */ |
eb653de1 FDBM |
2781 | if (!ins_len && !p->keep_locks) { |
2782 | int u = level + 1; | |
2783 | ||
2784 | if (u < BTRFS_MAX_LEVEL && p->locks[u]) { | |
2785 | btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); | |
2786 | p->locks[u] = 0; | |
2787 | } | |
2788 | } | |
b4ce94de | 2789 | |
995e9a16 NB |
2790 | /* |
2791 | * If btrfs_bin_search returns an exact match (prev_cmp == 0) | |
2792 | * we can safely assume the target key will always be in slot 0 | |
2793 | * on lower levels due to the invariants BTRFS' btree provides, | |
2794 | * namely that a btrfs_key_ptr entry always points to the | |
2795 | * lowest key in the child node, thus we can skip searching | |
2796 | * lower levels | |
2797 | */ | |
2798 | if (prev_cmp == 0) { | |
2799 | slot = 0; | |
2800 | ret = 0; | |
2801 | } else { | |
2802 | ret = btrfs_bin_search(b, key, &slot); | |
2803 | prev_cmp = ret; | |
2804 | if (ret < 0) | |
2805 | goto done; | |
2806 | } | |
b4ce94de | 2807 | |
f624d976 | 2808 | if (level == 0) { |
be0e5c09 | 2809 | p->slots[level] = slot; |
87b29b20 | 2810 | if (ins_len > 0 && |
e902baac | 2811 | btrfs_leaf_free_space(b) < ins_len) { |
bd681513 CM |
2812 | if (write_lock_level < 1) { |
2813 | write_lock_level = 1; | |
2814 | btrfs_release_path(p); | |
2815 | goto again; | |
2816 | } | |
2817 | ||
b4ce94de | 2818 | btrfs_set_path_blocking(p); |
33c66f43 YZ |
2819 | err = split_leaf(trans, root, key, |
2820 | p, ins_len, ret == 0); | |
b4ce94de | 2821 | |
33c66f43 YZ |
2822 | BUG_ON(err > 0); |
2823 | if (err) { | |
2824 | ret = err; | |
65b51a00 CM |
2825 | goto done; |
2826 | } | |
5c680ed6 | 2827 | } |
459931ec | 2828 | if (!p->search_for_split) |
f7c79f30 | 2829 | unlock_up(p, level, lowest_unlock, |
4b6f8e96 | 2830 | min_write_lock_level, NULL); |
65b51a00 | 2831 | goto done; |
be0e5c09 | 2832 | } |
f624d976 QW |
2833 | if (ret && slot > 0) { |
2834 | dec = 1; | |
2835 | slot--; | |
2836 | } | |
2837 | p->slots[level] = slot; | |
2838 | err = setup_nodes_for_search(trans, root, p, b, level, ins_len, | |
2839 | &write_lock_level); | |
2840 | if (err == -EAGAIN) | |
2841 | goto again; | |
2842 | if (err) { | |
2843 | ret = err; | |
2844 | goto done; | |
2845 | } | |
2846 | b = p->nodes[level]; | |
2847 | slot = p->slots[level]; | |
2848 | ||
2849 | /* | |
2850 | * Slot 0 is special, if we change the key we have to update | |
2851 | * the parent pointer which means we must have a write lock on | |
2852 | * the parent | |
2853 | */ | |
2854 | if (slot == 0 && ins_len && write_lock_level < level + 1) { | |
2855 | write_lock_level = level + 1; | |
2856 | btrfs_release_path(p); | |
2857 | goto again; | |
2858 | } | |
2859 | ||
2860 | unlock_up(p, level, lowest_unlock, min_write_lock_level, | |
2861 | &write_lock_level); | |
2862 | ||
2863 | if (level == lowest_level) { | |
2864 | if (dec) | |
2865 | p->slots[level]++; | |
2866 | goto done; | |
2867 | } | |
2868 | ||
2869 | err = read_block_for_search(root, p, &b, level, slot, key); | |
2870 | if (err == -EAGAIN) | |
2871 | goto again; | |
2872 | if (err) { | |
2873 | ret = err; | |
2874 | goto done; | |
2875 | } | |
2876 | ||
2877 | if (!p->skip_locking) { | |
2878 | level = btrfs_header_level(b); | |
2879 | if (level <= write_lock_level) { | |
2880 | if (!btrfs_try_tree_write_lock(b)) { | |
2881 | btrfs_set_path_blocking(p); | |
2882 | btrfs_tree_lock(b); | |
2883 | } | |
2884 | p->locks[level] = BTRFS_WRITE_LOCK; | |
2885 | } else { | |
2886 | if (!btrfs_tree_read_lock_atomic(b)) { | |
2887 | btrfs_set_path_blocking(p); | |
fd7ba1c1 JB |
2888 | __btrfs_tree_read_lock(b, BTRFS_NESTING_NORMAL, |
2889 | p->recurse); | |
f624d976 QW |
2890 | } |
2891 | p->locks[level] = BTRFS_READ_LOCK; | |
2892 | } | |
2893 | p->nodes[level] = b; | |
2894 | } | |
be0e5c09 | 2895 | } |
65b51a00 CM |
2896 | ret = 1; |
2897 | done: | |
b4ce94de CM |
2898 | /* |
2899 | * we don't really know what they plan on doing with the path | |
2900 | * from here on, so for now just mark it as blocking | |
2901 | */ | |
b9473439 CM |
2902 | if (!p->leave_spinning) |
2903 | btrfs_set_path_blocking(p); | |
5f5bc6b1 | 2904 | if (ret < 0 && !p->skip_release_on_error) |
b3b4aa74 | 2905 | btrfs_release_path(p); |
65b51a00 | 2906 | return ret; |
be0e5c09 CM |
2907 | } |
2908 | ||
5d9e75c4 JS |
2909 | /* |
2910 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the | |
2911 | * current state of the tree together with the operations recorded in the tree | |
2912 | * modification log to search for the key in a previous version of this tree, as | |
2913 | * denoted by the time_seq parameter. | |
2914 | * | |
2915 | * Naturally, there is no support for insert, delete or cow operations. | |
2916 | * | |
2917 | * The resulting path and return value will be set up as if we called | |
2918 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. | |
2919 | */ | |
310712b2 | 2920 | int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, |
5d9e75c4 JS |
2921 | struct btrfs_path *p, u64 time_seq) |
2922 | { | |
0b246afa | 2923 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d9e75c4 JS |
2924 | struct extent_buffer *b; |
2925 | int slot; | |
2926 | int ret; | |
2927 | int err; | |
2928 | int level; | |
2929 | int lowest_unlock = 1; | |
2930 | u8 lowest_level = 0; | |
2931 | ||
2932 | lowest_level = p->lowest_level; | |
2933 | WARN_ON(p->nodes[0] != NULL); | |
2934 | ||
2935 | if (p->search_commit_root) { | |
2936 | BUG_ON(time_seq); | |
2937 | return btrfs_search_slot(NULL, root, key, p, 0, 0); | |
2938 | } | |
2939 | ||
2940 | again: | |
5d9e75c4 | 2941 | b = get_old_root(root, time_seq); |
315bed43 NB |
2942 | if (!b) { |
2943 | ret = -EIO; | |
2944 | goto done; | |
2945 | } | |
5d9e75c4 | 2946 | level = btrfs_header_level(b); |
5d9e75c4 JS |
2947 | p->locks[level] = BTRFS_READ_LOCK; |
2948 | ||
2949 | while (b) { | |
abe9339d QW |
2950 | int dec = 0; |
2951 | ||
5d9e75c4 JS |
2952 | level = btrfs_header_level(b); |
2953 | p->nodes[level] = b; | |
5d9e75c4 JS |
2954 | |
2955 | /* | |
2956 | * we have a lock on b and as long as we aren't changing | |
2957 | * the tree, there is no way to for the items in b to change. | |
2958 | * It is safe to drop the lock on our parent before we | |
2959 | * go through the expensive btree search on b. | |
2960 | */ | |
2961 | btrfs_unlock_up_safe(p, level + 1); | |
2962 | ||
995e9a16 | 2963 | ret = btrfs_bin_search(b, key, &slot); |
cbca7d59 FM |
2964 | if (ret < 0) |
2965 | goto done; | |
5d9e75c4 | 2966 | |
abe9339d | 2967 | if (level == 0) { |
5d9e75c4 JS |
2968 | p->slots[level] = slot; |
2969 | unlock_up(p, level, lowest_unlock, 0, NULL); | |
abe9339d QW |
2970 | goto done; |
2971 | } | |
5d9e75c4 | 2972 | |
abe9339d QW |
2973 | if (ret && slot > 0) { |
2974 | dec = 1; | |
2975 | slot--; | |
2976 | } | |
2977 | p->slots[level] = slot; | |
2978 | unlock_up(p, level, lowest_unlock, 0, NULL); | |
5d9e75c4 | 2979 | |
abe9339d QW |
2980 | if (level == lowest_level) { |
2981 | if (dec) | |
2982 | p->slots[level]++; | |
2983 | goto done; | |
2984 | } | |
5d9e75c4 | 2985 | |
abe9339d QW |
2986 | err = read_block_for_search(root, p, &b, level, slot, key); |
2987 | if (err == -EAGAIN) | |
2988 | goto again; | |
2989 | if (err) { | |
2990 | ret = err; | |
5d9e75c4 JS |
2991 | goto done; |
2992 | } | |
abe9339d QW |
2993 | |
2994 | level = btrfs_header_level(b); | |
2995 | if (!btrfs_tree_read_lock_atomic(b)) { | |
2996 | btrfs_set_path_blocking(p); | |
2997 | btrfs_tree_read_lock(b); | |
2998 | } | |
2999 | b = tree_mod_log_rewind(fs_info, p, b, time_seq); | |
3000 | if (!b) { | |
3001 | ret = -ENOMEM; | |
3002 | goto done; | |
3003 | } | |
3004 | p->locks[level] = BTRFS_READ_LOCK; | |
3005 | p->nodes[level] = b; | |
5d9e75c4 JS |
3006 | } |
3007 | ret = 1; | |
3008 | done: | |
3009 | if (!p->leave_spinning) | |
3010 | btrfs_set_path_blocking(p); | |
3011 | if (ret < 0) | |
3012 | btrfs_release_path(p); | |
3013 | ||
3014 | return ret; | |
3015 | } | |
3016 | ||
2f38b3e1 AJ |
3017 | /* |
3018 | * helper to use instead of search slot if no exact match is needed but | |
3019 | * instead the next or previous item should be returned. | |
3020 | * When find_higher is true, the next higher item is returned, the next lower | |
3021 | * otherwise. | |
3022 | * When return_any and find_higher are both true, and no higher item is found, | |
3023 | * return the next lower instead. | |
3024 | * When return_any is true and find_higher is false, and no lower item is found, | |
3025 | * return the next higher instead. | |
3026 | * It returns 0 if any item is found, 1 if none is found (tree empty), and | |
3027 | * < 0 on error | |
3028 | */ | |
3029 | int btrfs_search_slot_for_read(struct btrfs_root *root, | |
310712b2 OS |
3030 | const struct btrfs_key *key, |
3031 | struct btrfs_path *p, int find_higher, | |
3032 | int return_any) | |
2f38b3e1 AJ |
3033 | { |
3034 | int ret; | |
3035 | struct extent_buffer *leaf; | |
3036 | ||
3037 | again: | |
3038 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); | |
3039 | if (ret <= 0) | |
3040 | return ret; | |
3041 | /* | |
3042 | * a return value of 1 means the path is at the position where the | |
3043 | * item should be inserted. Normally this is the next bigger item, | |
3044 | * but in case the previous item is the last in a leaf, path points | |
3045 | * to the first free slot in the previous leaf, i.e. at an invalid | |
3046 | * item. | |
3047 | */ | |
3048 | leaf = p->nodes[0]; | |
3049 | ||
3050 | if (find_higher) { | |
3051 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { | |
3052 | ret = btrfs_next_leaf(root, p); | |
3053 | if (ret <= 0) | |
3054 | return ret; | |
3055 | if (!return_any) | |
3056 | return 1; | |
3057 | /* | |
3058 | * no higher item found, return the next | |
3059 | * lower instead | |
3060 | */ | |
3061 | return_any = 0; | |
3062 | find_higher = 0; | |
3063 | btrfs_release_path(p); | |
3064 | goto again; | |
3065 | } | |
3066 | } else { | |
e6793769 AJ |
3067 | if (p->slots[0] == 0) { |
3068 | ret = btrfs_prev_leaf(root, p); | |
3069 | if (ret < 0) | |
3070 | return ret; | |
3071 | if (!ret) { | |
23c6bf6a FDBM |
3072 | leaf = p->nodes[0]; |
3073 | if (p->slots[0] == btrfs_header_nritems(leaf)) | |
3074 | p->slots[0]--; | |
e6793769 | 3075 | return 0; |
2f38b3e1 | 3076 | } |
e6793769 AJ |
3077 | if (!return_any) |
3078 | return 1; | |
3079 | /* | |
3080 | * no lower item found, return the next | |
3081 | * higher instead | |
3082 | */ | |
3083 | return_any = 0; | |
3084 | find_higher = 1; | |
3085 | btrfs_release_path(p); | |
3086 | goto again; | |
3087 | } else { | |
2f38b3e1 AJ |
3088 | --p->slots[0]; |
3089 | } | |
3090 | } | |
3091 | return 0; | |
3092 | } | |
3093 | ||
74123bd7 CM |
3094 | /* |
3095 | * adjust the pointers going up the tree, starting at level | |
3096 | * making sure the right key of each node is points to 'key'. | |
3097 | * This is used after shifting pointers to the left, so it stops | |
3098 | * fixing up pointers when a given leaf/node is not in slot 0 of the | |
3099 | * higher levels | |
aa5d6bed | 3100 | * |
74123bd7 | 3101 | */ |
b167fa91 | 3102 | static void fixup_low_keys(struct btrfs_path *path, |
143bede5 | 3103 | struct btrfs_disk_key *key, int level) |
be0e5c09 CM |
3104 | { |
3105 | int i; | |
5f39d397 | 3106 | struct extent_buffer *t; |
0e82bcfe | 3107 | int ret; |
5f39d397 | 3108 | |
234b63a0 | 3109 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
be0e5c09 | 3110 | int tslot = path->slots[i]; |
0e82bcfe | 3111 | |
eb60ceac | 3112 | if (!path->nodes[i]) |
be0e5c09 | 3113 | break; |
5f39d397 | 3114 | t = path->nodes[i]; |
0e82bcfe DS |
3115 | ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE, |
3116 | GFP_ATOMIC); | |
3117 | BUG_ON(ret < 0); | |
5f39d397 | 3118 | btrfs_set_node_key(t, key, tslot); |
d6025579 | 3119 | btrfs_mark_buffer_dirty(path->nodes[i]); |
be0e5c09 CM |
3120 | if (tslot != 0) |
3121 | break; | |
3122 | } | |
3123 | } | |
3124 | ||
31840ae1 ZY |
3125 | /* |
3126 | * update item key. | |
3127 | * | |
3128 | * This function isn't completely safe. It's the caller's responsibility | |
3129 | * that the new key won't break the order | |
3130 | */ | |
b7a0365e DD |
3131 | void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, |
3132 | struct btrfs_path *path, | |
310712b2 | 3133 | const struct btrfs_key *new_key) |
31840ae1 ZY |
3134 | { |
3135 | struct btrfs_disk_key disk_key; | |
3136 | struct extent_buffer *eb; | |
3137 | int slot; | |
3138 | ||
3139 | eb = path->nodes[0]; | |
3140 | slot = path->slots[0]; | |
3141 | if (slot > 0) { | |
3142 | btrfs_item_key(eb, &disk_key, slot - 1); | |
7c15d410 QW |
3143 | if (unlikely(comp_keys(&disk_key, new_key) >= 0)) { |
3144 | btrfs_crit(fs_info, | |
3145 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", | |
3146 | slot, btrfs_disk_key_objectid(&disk_key), | |
3147 | btrfs_disk_key_type(&disk_key), | |
3148 | btrfs_disk_key_offset(&disk_key), | |
3149 | new_key->objectid, new_key->type, | |
3150 | new_key->offset); | |
3151 | btrfs_print_leaf(eb); | |
3152 | BUG(); | |
3153 | } | |
31840ae1 ZY |
3154 | } |
3155 | if (slot < btrfs_header_nritems(eb) - 1) { | |
3156 | btrfs_item_key(eb, &disk_key, slot + 1); | |
7c15d410 QW |
3157 | if (unlikely(comp_keys(&disk_key, new_key) <= 0)) { |
3158 | btrfs_crit(fs_info, | |
3159 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", | |
3160 | slot, btrfs_disk_key_objectid(&disk_key), | |
3161 | btrfs_disk_key_type(&disk_key), | |
3162 | btrfs_disk_key_offset(&disk_key), | |
3163 | new_key->objectid, new_key->type, | |
3164 | new_key->offset); | |
3165 | btrfs_print_leaf(eb); | |
3166 | BUG(); | |
3167 | } | |
31840ae1 ZY |
3168 | } |
3169 | ||
3170 | btrfs_cpu_key_to_disk(&disk_key, new_key); | |
3171 | btrfs_set_item_key(eb, &disk_key, slot); | |
3172 | btrfs_mark_buffer_dirty(eb); | |
3173 | if (slot == 0) | |
b167fa91 | 3174 | fixup_low_keys(path, &disk_key, 1); |
31840ae1 ZY |
3175 | } |
3176 | ||
d16c702f QW |
3177 | /* |
3178 | * Check key order of two sibling extent buffers. | |
3179 | * | |
3180 | * Return true if something is wrong. | |
3181 | * Return false if everything is fine. | |
3182 | * | |
3183 | * Tree-checker only works inside one tree block, thus the following | |
3184 | * corruption can not be detected by tree-checker: | |
3185 | * | |
3186 | * Leaf @left | Leaf @right | |
3187 | * -------------------------------------------------------------- | |
3188 | * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 | | |
3189 | * | |
3190 | * Key f6 in leaf @left itself is valid, but not valid when the next | |
3191 | * key in leaf @right is 7. | |
3192 | * This can only be checked at tree block merge time. | |
3193 | * And since tree checker has ensured all key order in each tree block | |
3194 | * is correct, we only need to bother the last key of @left and the first | |
3195 | * key of @right. | |
3196 | */ | |
3197 | static bool check_sibling_keys(struct extent_buffer *left, | |
3198 | struct extent_buffer *right) | |
3199 | { | |
3200 | struct btrfs_key left_last; | |
3201 | struct btrfs_key right_first; | |
3202 | int level = btrfs_header_level(left); | |
3203 | int nr_left = btrfs_header_nritems(left); | |
3204 | int nr_right = btrfs_header_nritems(right); | |
3205 | ||
3206 | /* No key to check in one of the tree blocks */ | |
3207 | if (!nr_left || !nr_right) | |
3208 | return false; | |
3209 | ||
3210 | if (level) { | |
3211 | btrfs_node_key_to_cpu(left, &left_last, nr_left - 1); | |
3212 | btrfs_node_key_to_cpu(right, &right_first, 0); | |
3213 | } else { | |
3214 | btrfs_item_key_to_cpu(left, &left_last, nr_left - 1); | |
3215 | btrfs_item_key_to_cpu(right, &right_first, 0); | |
3216 | } | |
3217 | ||
3218 | if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) { | |
3219 | btrfs_crit(left->fs_info, | |
3220 | "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)", | |
3221 | left_last.objectid, left_last.type, | |
3222 | left_last.offset, right_first.objectid, | |
3223 | right_first.type, right_first.offset); | |
3224 | return true; | |
3225 | } | |
3226 | return false; | |
3227 | } | |
3228 | ||
74123bd7 CM |
3229 | /* |
3230 | * try to push data from one node into the next node left in the | |
79f95c82 | 3231 | * tree. |
aa5d6bed CM |
3232 | * |
3233 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible | |
3234 | * error, and > 0 if there was no room in the left hand block. | |
74123bd7 | 3235 | */ |
98ed5174 | 3236 | static int push_node_left(struct btrfs_trans_handle *trans, |
2ff7e61e | 3237 | struct extent_buffer *dst, |
971a1f66 | 3238 | struct extent_buffer *src, int empty) |
be0e5c09 | 3239 | { |
d30a668f | 3240 | struct btrfs_fs_info *fs_info = trans->fs_info; |
be0e5c09 | 3241 | int push_items = 0; |
bb803951 CM |
3242 | int src_nritems; |
3243 | int dst_nritems; | |
aa5d6bed | 3244 | int ret = 0; |
be0e5c09 | 3245 | |
5f39d397 CM |
3246 | src_nritems = btrfs_header_nritems(src); |
3247 | dst_nritems = btrfs_header_nritems(dst); | |
0b246afa | 3248 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
7bb86316 CM |
3249 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
3250 | WARN_ON(btrfs_header_generation(dst) != trans->transid); | |
54aa1f4d | 3251 | |
bce4eae9 | 3252 | if (!empty && src_nritems <= 8) |
971a1f66 CM |
3253 | return 1; |
3254 | ||
d397712b | 3255 | if (push_items <= 0) |
be0e5c09 CM |
3256 | return 1; |
3257 | ||
bce4eae9 | 3258 | if (empty) { |
971a1f66 | 3259 | push_items = min(src_nritems, push_items); |
bce4eae9 CM |
3260 | if (push_items < src_nritems) { |
3261 | /* leave at least 8 pointers in the node if | |
3262 | * we aren't going to empty it | |
3263 | */ | |
3264 | if (src_nritems - push_items < 8) { | |
3265 | if (push_items <= 8) | |
3266 | return 1; | |
3267 | push_items -= 8; | |
3268 | } | |
3269 | } | |
3270 | } else | |
3271 | push_items = min(src_nritems - 8, push_items); | |
79f95c82 | 3272 | |
d16c702f QW |
3273 | /* dst is the left eb, src is the middle eb */ |
3274 | if (check_sibling_keys(dst, src)) { | |
3275 | ret = -EUCLEAN; | |
3276 | btrfs_abort_transaction(trans, ret); | |
3277 | return ret; | |
3278 | } | |
ed874f0d | 3279 | ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items); |
5de865ee | 3280 | if (ret) { |
66642832 | 3281 | btrfs_abort_transaction(trans, ret); |
5de865ee FDBM |
3282 | return ret; |
3283 | } | |
5f39d397 CM |
3284 | copy_extent_buffer(dst, src, |
3285 | btrfs_node_key_ptr_offset(dst_nritems), | |
3286 | btrfs_node_key_ptr_offset(0), | |
d397712b | 3287 | push_items * sizeof(struct btrfs_key_ptr)); |
5f39d397 | 3288 | |
bb803951 | 3289 | if (push_items < src_nritems) { |
57911b8b | 3290 | /* |
bf1d3425 DS |
3291 | * Don't call tree_mod_log_insert_move here, key removal was |
3292 | * already fully logged by tree_mod_log_eb_copy above. | |
57911b8b | 3293 | */ |
5f39d397 CM |
3294 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
3295 | btrfs_node_key_ptr_offset(push_items), | |
3296 | (src_nritems - push_items) * | |
3297 | sizeof(struct btrfs_key_ptr)); | |
3298 | } | |
3299 | btrfs_set_header_nritems(src, src_nritems - push_items); | |
3300 | btrfs_set_header_nritems(dst, dst_nritems + push_items); | |
3301 | btrfs_mark_buffer_dirty(src); | |
3302 | btrfs_mark_buffer_dirty(dst); | |
31840ae1 | 3303 | |
79f95c82 CM |
3304 | return ret; |
3305 | } | |
3306 | ||
3307 | /* | |
3308 | * try to push data from one node into the next node right in the | |
3309 | * tree. | |
3310 | * | |
3311 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible | |
3312 | * error, and > 0 if there was no room in the right hand block. | |
3313 | * | |
3314 | * this will only push up to 1/2 the contents of the left node over | |
3315 | */ | |
5f39d397 | 3316 | static int balance_node_right(struct btrfs_trans_handle *trans, |
5f39d397 CM |
3317 | struct extent_buffer *dst, |
3318 | struct extent_buffer *src) | |
79f95c82 | 3319 | { |
55d32ed8 | 3320 | struct btrfs_fs_info *fs_info = trans->fs_info; |
79f95c82 CM |
3321 | int push_items = 0; |
3322 | int max_push; | |
3323 | int src_nritems; | |
3324 | int dst_nritems; | |
3325 | int ret = 0; | |
79f95c82 | 3326 | |
7bb86316 CM |
3327 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
3328 | WARN_ON(btrfs_header_generation(dst) != trans->transid); | |
3329 | ||
5f39d397 CM |
3330 | src_nritems = btrfs_header_nritems(src); |
3331 | dst_nritems = btrfs_header_nritems(dst); | |
0b246afa | 3332 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
d397712b | 3333 | if (push_items <= 0) |
79f95c82 | 3334 | return 1; |
bce4eae9 | 3335 | |
d397712b | 3336 | if (src_nritems < 4) |
bce4eae9 | 3337 | return 1; |
79f95c82 CM |
3338 | |
3339 | max_push = src_nritems / 2 + 1; | |
3340 | /* don't try to empty the node */ | |
d397712b | 3341 | if (max_push >= src_nritems) |
79f95c82 | 3342 | return 1; |
252c38f0 | 3343 | |
79f95c82 CM |
3344 | if (max_push < push_items) |
3345 | push_items = max_push; | |
3346 | ||
d16c702f QW |
3347 | /* dst is the right eb, src is the middle eb */ |
3348 | if (check_sibling_keys(src, dst)) { | |
3349 | ret = -EUCLEAN; | |
3350 | btrfs_abort_transaction(trans, ret); | |
3351 | return ret; | |
3352 | } | |
bf1d3425 DS |
3353 | ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); |
3354 | BUG_ON(ret < 0); | |
5f39d397 CM |
3355 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
3356 | btrfs_node_key_ptr_offset(0), | |
3357 | (dst_nritems) * | |
3358 | sizeof(struct btrfs_key_ptr)); | |
d6025579 | 3359 | |
ed874f0d DS |
3360 | ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, |
3361 | push_items); | |
5de865ee | 3362 | if (ret) { |
66642832 | 3363 | btrfs_abort_transaction(trans, ret); |
5de865ee FDBM |
3364 | return ret; |
3365 | } | |
5f39d397 CM |
3366 | copy_extent_buffer(dst, src, |
3367 | btrfs_node_key_ptr_offset(0), | |
3368 | btrfs_node_key_ptr_offset(src_nritems - push_items), | |
d397712b | 3369 | push_items * sizeof(struct btrfs_key_ptr)); |
79f95c82 | 3370 | |
5f39d397 CM |
3371 | btrfs_set_header_nritems(src, src_nritems - push_items); |
3372 | btrfs_set_header_nritems(dst, dst_nritems + push_items); | |
79f95c82 | 3373 | |
5f39d397 CM |
3374 | btrfs_mark_buffer_dirty(src); |
3375 | btrfs_mark_buffer_dirty(dst); | |
31840ae1 | 3376 | |
aa5d6bed | 3377 | return ret; |
be0e5c09 CM |
3378 | } |
3379 | ||
97571fd0 CM |
3380 | /* |
3381 | * helper function to insert a new root level in the tree. | |
3382 | * A new node is allocated, and a single item is inserted to | |
3383 | * point to the existing root | |
aa5d6bed CM |
3384 | * |
3385 | * returns zero on success or < 0 on failure. | |
97571fd0 | 3386 | */ |
d397712b | 3387 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
5f39d397 | 3388 | struct btrfs_root *root, |
fdd99c72 | 3389 | struct btrfs_path *path, int level) |
5c680ed6 | 3390 | { |
0b246afa | 3391 | struct btrfs_fs_info *fs_info = root->fs_info; |
7bb86316 | 3392 | u64 lower_gen; |
5f39d397 CM |
3393 | struct extent_buffer *lower; |
3394 | struct extent_buffer *c; | |
925baedd | 3395 | struct extent_buffer *old; |
5f39d397 | 3396 | struct btrfs_disk_key lower_key; |
d9d19a01 | 3397 | int ret; |
5c680ed6 CM |
3398 | |
3399 | BUG_ON(path->nodes[level]); | |
3400 | BUG_ON(path->nodes[level-1] != root->node); | |
3401 | ||
7bb86316 CM |
3402 | lower = path->nodes[level-1]; |
3403 | if (level == 1) | |
3404 | btrfs_item_key(lower, &lower_key, 0); | |
3405 | else | |
3406 | btrfs_node_key(lower, &lower_key, 0); | |
3407 | ||
a6279470 | 3408 | c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level, |
9631e4cc JB |
3409 | root->node->start, 0, |
3410 | BTRFS_NESTING_NORMAL); | |
5f39d397 CM |
3411 | if (IS_ERR(c)) |
3412 | return PTR_ERR(c); | |
925baedd | 3413 | |
0b246afa | 3414 | root_add_used(root, fs_info->nodesize); |
f0486c68 | 3415 | |
5f39d397 | 3416 | btrfs_set_header_nritems(c, 1); |
5f39d397 | 3417 | btrfs_set_node_key(c, &lower_key, 0); |
db94535d | 3418 | btrfs_set_node_blockptr(c, 0, lower->start); |
7bb86316 | 3419 | lower_gen = btrfs_header_generation(lower); |
31840ae1 | 3420 | WARN_ON(lower_gen != trans->transid); |
7bb86316 CM |
3421 | |
3422 | btrfs_set_node_ptr_generation(c, 0, lower_gen); | |
d5719762 | 3423 | |
5f39d397 | 3424 | btrfs_mark_buffer_dirty(c); |
d5719762 | 3425 | |
925baedd | 3426 | old = root->node; |
d9d19a01 DS |
3427 | ret = tree_mod_log_insert_root(root->node, c, 0); |
3428 | BUG_ON(ret < 0); | |
240f62c8 | 3429 | rcu_assign_pointer(root->node, c); |
925baedd CM |
3430 | |
3431 | /* the super has an extra ref to root->node */ | |
3432 | free_extent_buffer(old); | |
3433 | ||
0b86a832 | 3434 | add_root_to_dirty_list(root); |
67439dad | 3435 | atomic_inc(&c->refs); |
5f39d397 | 3436 | path->nodes[level] = c; |
95449a16 | 3437 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
5c680ed6 CM |
3438 | path->slots[level] = 0; |
3439 | return 0; | |
3440 | } | |
3441 | ||
74123bd7 CM |
3442 | /* |
3443 | * worker function to insert a single pointer in a node. | |
3444 | * the node should have enough room for the pointer already | |
97571fd0 | 3445 | * |
74123bd7 CM |
3446 | * slot and level indicate where you want the key to go, and |
3447 | * blocknr is the block the key points to. | |
3448 | */ | |
143bede5 | 3449 | static void insert_ptr(struct btrfs_trans_handle *trans, |
6ad3cf6d | 3450 | struct btrfs_path *path, |
143bede5 | 3451 | struct btrfs_disk_key *key, u64 bytenr, |
c3e06965 | 3452 | int slot, int level) |
74123bd7 | 3453 | { |
5f39d397 | 3454 | struct extent_buffer *lower; |
74123bd7 | 3455 | int nritems; |
f3ea38da | 3456 | int ret; |
5c680ed6 CM |
3457 | |
3458 | BUG_ON(!path->nodes[level]); | |
f0486c68 | 3459 | btrfs_assert_tree_locked(path->nodes[level]); |
5f39d397 CM |
3460 | lower = path->nodes[level]; |
3461 | nritems = btrfs_header_nritems(lower); | |
c293498b | 3462 | BUG_ON(slot > nritems); |
6ad3cf6d | 3463 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info)); |
74123bd7 | 3464 | if (slot != nritems) { |
bf1d3425 DS |
3465 | if (level) { |
3466 | ret = tree_mod_log_insert_move(lower, slot + 1, slot, | |
a446a979 | 3467 | nritems - slot); |
bf1d3425 DS |
3468 | BUG_ON(ret < 0); |
3469 | } | |
5f39d397 CM |
3470 | memmove_extent_buffer(lower, |
3471 | btrfs_node_key_ptr_offset(slot + 1), | |
3472 | btrfs_node_key_ptr_offset(slot), | |
d6025579 | 3473 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
74123bd7 | 3474 | } |
c3e06965 | 3475 | if (level) { |
e09c2efe DS |
3476 | ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, |
3477 | GFP_NOFS); | |
f3ea38da JS |
3478 | BUG_ON(ret < 0); |
3479 | } | |
5f39d397 | 3480 | btrfs_set_node_key(lower, key, slot); |
db94535d | 3481 | btrfs_set_node_blockptr(lower, slot, bytenr); |
74493f7a CM |
3482 | WARN_ON(trans->transid == 0); |
3483 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); | |
5f39d397 CM |
3484 | btrfs_set_header_nritems(lower, nritems + 1); |
3485 | btrfs_mark_buffer_dirty(lower); | |
74123bd7 CM |
3486 | } |
3487 | ||
97571fd0 CM |
3488 | /* |
3489 | * split the node at the specified level in path in two. | |
3490 | * The path is corrected to point to the appropriate node after the split | |
3491 | * | |
3492 | * Before splitting this tries to make some room in the node by pushing | |
3493 | * left and right, if either one works, it returns right away. | |
aa5d6bed CM |
3494 | * |
3495 | * returns 0 on success and < 0 on failure | |
97571fd0 | 3496 | */ |
e02119d5 CM |
3497 | static noinline int split_node(struct btrfs_trans_handle *trans, |
3498 | struct btrfs_root *root, | |
3499 | struct btrfs_path *path, int level) | |
be0e5c09 | 3500 | { |
0b246afa | 3501 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 CM |
3502 | struct extent_buffer *c; |
3503 | struct extent_buffer *split; | |
3504 | struct btrfs_disk_key disk_key; | |
be0e5c09 | 3505 | int mid; |
5c680ed6 | 3506 | int ret; |
7518a238 | 3507 | u32 c_nritems; |
eb60ceac | 3508 | |
5f39d397 | 3509 | c = path->nodes[level]; |
7bb86316 | 3510 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
5f39d397 | 3511 | if (c == root->node) { |
d9abbf1c | 3512 | /* |
90f8d62e JS |
3513 | * trying to split the root, lets make a new one |
3514 | * | |
fdd99c72 | 3515 | * tree mod log: We don't log_removal old root in |
90f8d62e JS |
3516 | * insert_new_root, because that root buffer will be kept as a |
3517 | * normal node. We are going to log removal of half of the | |
3518 | * elements below with tree_mod_log_eb_copy. We're holding a | |
3519 | * tree lock on the buffer, which is why we cannot race with | |
3520 | * other tree_mod_log users. | |
d9abbf1c | 3521 | */ |
fdd99c72 | 3522 | ret = insert_new_root(trans, root, path, level + 1); |
5c680ed6 CM |
3523 | if (ret) |
3524 | return ret; | |
b3612421 | 3525 | } else { |
e66f709b | 3526 | ret = push_nodes_for_insert(trans, root, path, level); |
5f39d397 CM |
3527 | c = path->nodes[level]; |
3528 | if (!ret && btrfs_header_nritems(c) < | |
0b246afa | 3529 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) |
e66f709b | 3530 | return 0; |
54aa1f4d CM |
3531 | if (ret < 0) |
3532 | return ret; | |
be0e5c09 | 3533 | } |
e66f709b | 3534 | |
5f39d397 | 3535 | c_nritems = btrfs_header_nritems(c); |
5d4f98a2 YZ |
3536 | mid = (c_nritems + 1) / 2; |
3537 | btrfs_node_key(c, &disk_key, mid); | |
7bb86316 | 3538 | |
a6279470 | 3539 | split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level, |
9631e4cc | 3540 | c->start, 0, BTRFS_NESTING_NORMAL); |
5f39d397 CM |
3541 | if (IS_ERR(split)) |
3542 | return PTR_ERR(split); | |
3543 | ||
0b246afa | 3544 | root_add_used(root, fs_info->nodesize); |
bc877d28 | 3545 | ASSERT(btrfs_header_level(c) == level); |
54aa1f4d | 3546 | |
ed874f0d | 3547 | ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid); |
5de865ee | 3548 | if (ret) { |
66642832 | 3549 | btrfs_abort_transaction(trans, ret); |
5de865ee FDBM |
3550 | return ret; |
3551 | } | |
5f39d397 CM |
3552 | copy_extent_buffer(split, c, |
3553 | btrfs_node_key_ptr_offset(0), | |
3554 | btrfs_node_key_ptr_offset(mid), | |
3555 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); | |
3556 | btrfs_set_header_nritems(split, c_nritems - mid); | |
3557 | btrfs_set_header_nritems(c, mid); | |
aa5d6bed CM |
3558 | ret = 0; |
3559 | ||
5f39d397 CM |
3560 | btrfs_mark_buffer_dirty(c); |
3561 | btrfs_mark_buffer_dirty(split); | |
3562 | ||
6ad3cf6d | 3563 | insert_ptr(trans, path, &disk_key, split->start, |
c3e06965 | 3564 | path->slots[level + 1] + 1, level + 1); |
aa5d6bed | 3565 | |
5de08d7d | 3566 | if (path->slots[level] >= mid) { |
5c680ed6 | 3567 | path->slots[level] -= mid; |
925baedd | 3568 | btrfs_tree_unlock(c); |
5f39d397 CM |
3569 | free_extent_buffer(c); |
3570 | path->nodes[level] = split; | |
5c680ed6 CM |
3571 | path->slots[level + 1] += 1; |
3572 | } else { | |
925baedd | 3573 | btrfs_tree_unlock(split); |
5f39d397 | 3574 | free_extent_buffer(split); |
be0e5c09 | 3575 | } |
aa5d6bed | 3576 | return ret; |
be0e5c09 CM |
3577 | } |
3578 | ||
74123bd7 CM |
3579 | /* |
3580 | * how many bytes are required to store the items in a leaf. start | |
3581 | * and nr indicate which items in the leaf to check. This totals up the | |
3582 | * space used both by the item structs and the item data | |
3583 | */ | |
5f39d397 | 3584 | static int leaf_space_used(struct extent_buffer *l, int start, int nr) |
be0e5c09 | 3585 | { |
41be1f3b JB |
3586 | struct btrfs_item *start_item; |
3587 | struct btrfs_item *end_item; | |
be0e5c09 | 3588 | int data_len; |
5f39d397 | 3589 | int nritems = btrfs_header_nritems(l); |
d4dbff95 | 3590 | int end = min(nritems, start + nr) - 1; |
be0e5c09 CM |
3591 | |
3592 | if (!nr) | |
3593 | return 0; | |
dd3cc16b RK |
3594 | start_item = btrfs_item_nr(start); |
3595 | end_item = btrfs_item_nr(end); | |
a31356b9 DS |
3596 | data_len = btrfs_item_offset(l, start_item) + |
3597 | btrfs_item_size(l, start_item); | |
3598 | data_len = data_len - btrfs_item_offset(l, end_item); | |
0783fcfc | 3599 | data_len += sizeof(struct btrfs_item) * nr; |
d4dbff95 | 3600 | WARN_ON(data_len < 0); |
be0e5c09 CM |
3601 | return data_len; |
3602 | } | |
3603 | ||
d4dbff95 CM |
3604 | /* |
3605 | * The space between the end of the leaf items and | |
3606 | * the start of the leaf data. IOW, how much room | |
3607 | * the leaf has left for both items and data | |
3608 | */ | |
e902baac | 3609 | noinline int btrfs_leaf_free_space(struct extent_buffer *leaf) |
d4dbff95 | 3610 | { |
e902baac | 3611 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
5f39d397 CM |
3612 | int nritems = btrfs_header_nritems(leaf); |
3613 | int ret; | |
0b246afa JM |
3614 | |
3615 | ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems); | |
5f39d397 | 3616 | if (ret < 0) { |
0b246afa JM |
3617 | btrfs_crit(fs_info, |
3618 | "leaf free space ret %d, leaf data size %lu, used %d nritems %d", | |
3619 | ret, | |
3620 | (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), | |
3621 | leaf_space_used(leaf, 0, nritems), nritems); | |
5f39d397 CM |
3622 | } |
3623 | return ret; | |
d4dbff95 CM |
3624 | } |
3625 | ||
99d8f83c CM |
3626 | /* |
3627 | * min slot controls the lowest index we're willing to push to the | |
3628 | * right. We'll push up to and including min_slot, but no lower | |
3629 | */ | |
f72f0010 | 3630 | static noinline int __push_leaf_right(struct btrfs_path *path, |
44871b1b CM |
3631 | int data_size, int empty, |
3632 | struct extent_buffer *right, | |
99d8f83c CM |
3633 | int free_space, u32 left_nritems, |
3634 | u32 min_slot) | |
00ec4c51 | 3635 | { |
f72f0010 | 3636 | struct btrfs_fs_info *fs_info = right->fs_info; |
5f39d397 | 3637 | struct extent_buffer *left = path->nodes[0]; |
44871b1b | 3638 | struct extent_buffer *upper = path->nodes[1]; |
cfed81a0 | 3639 | struct btrfs_map_token token; |
5f39d397 | 3640 | struct btrfs_disk_key disk_key; |
00ec4c51 | 3641 | int slot; |
34a38218 | 3642 | u32 i; |
00ec4c51 CM |
3643 | int push_space = 0; |
3644 | int push_items = 0; | |
0783fcfc | 3645 | struct btrfs_item *item; |
34a38218 | 3646 | u32 nr; |
7518a238 | 3647 | u32 right_nritems; |
5f39d397 | 3648 | u32 data_end; |
db94535d | 3649 | u32 this_item_size; |
00ec4c51 | 3650 | |
34a38218 CM |
3651 | if (empty) |
3652 | nr = 0; | |
3653 | else | |
99d8f83c | 3654 | nr = max_t(u32, 1, min_slot); |
34a38218 | 3655 | |
31840ae1 | 3656 | if (path->slots[0] >= left_nritems) |
87b29b20 | 3657 | push_space += data_size; |
31840ae1 | 3658 | |
44871b1b | 3659 | slot = path->slots[1]; |
34a38218 CM |
3660 | i = left_nritems - 1; |
3661 | while (i >= nr) { | |
dd3cc16b | 3662 | item = btrfs_item_nr(i); |
db94535d | 3663 | |
31840ae1 ZY |
3664 | if (!empty && push_items > 0) { |
3665 | if (path->slots[0] > i) | |
3666 | break; | |
3667 | if (path->slots[0] == i) { | |
e902baac DS |
3668 | int space = btrfs_leaf_free_space(left); |
3669 | ||
31840ae1 ZY |
3670 | if (space + push_space * 2 > free_space) |
3671 | break; | |
3672 | } | |
3673 | } | |
3674 | ||
00ec4c51 | 3675 | if (path->slots[0] == i) |
87b29b20 | 3676 | push_space += data_size; |
db94535d | 3677 | |
db94535d CM |
3678 | this_item_size = btrfs_item_size(left, item); |
3679 | if (this_item_size + sizeof(*item) + push_space > free_space) | |
00ec4c51 | 3680 | break; |
31840ae1 | 3681 | |
00ec4c51 | 3682 | push_items++; |
db94535d | 3683 | push_space += this_item_size + sizeof(*item); |
34a38218 CM |
3684 | if (i == 0) |
3685 | break; | |
3686 | i--; | |
db94535d | 3687 | } |
5f39d397 | 3688 | |
925baedd CM |
3689 | if (push_items == 0) |
3690 | goto out_unlock; | |
5f39d397 | 3691 | |
6c1500f2 | 3692 | WARN_ON(!empty && push_items == left_nritems); |
5f39d397 | 3693 | |
00ec4c51 | 3694 | /* push left to right */ |
5f39d397 | 3695 | right_nritems = btrfs_header_nritems(right); |
34a38218 | 3696 | |
5f39d397 | 3697 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
8f881e8c | 3698 | push_space -= leaf_data_end(left); |
5f39d397 | 3699 | |
00ec4c51 | 3700 | /* make room in the right data area */ |
8f881e8c | 3701 | data_end = leaf_data_end(right); |
5f39d397 | 3702 | memmove_extent_buffer(right, |
3d9ec8c4 NB |
3703 | BTRFS_LEAF_DATA_OFFSET + data_end - push_space, |
3704 | BTRFS_LEAF_DATA_OFFSET + data_end, | |
0b246afa | 3705 | BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); |
5f39d397 | 3706 | |
00ec4c51 | 3707 | /* copy from the left data area */ |
3d9ec8c4 | 3708 | copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + |
0b246afa | 3709 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
8f881e8c | 3710 | BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left), |
d6025579 | 3711 | push_space); |
5f39d397 CM |
3712 | |
3713 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), | |
3714 | btrfs_item_nr_offset(0), | |
3715 | right_nritems * sizeof(struct btrfs_item)); | |
3716 | ||
00ec4c51 | 3717 | /* copy the items from left to right */ |
5f39d397 CM |
3718 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
3719 | btrfs_item_nr_offset(left_nritems - push_items), | |
3720 | push_items * sizeof(struct btrfs_item)); | |
00ec4c51 CM |
3721 | |
3722 | /* update the item pointers */ | |
c82f823c | 3723 | btrfs_init_map_token(&token, right); |
7518a238 | 3724 | right_nritems += push_items; |
5f39d397 | 3725 | btrfs_set_header_nritems(right, right_nritems); |
0b246afa | 3726 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
7518a238 | 3727 | for (i = 0; i < right_nritems; i++) { |
dd3cc16b | 3728 | item = btrfs_item_nr(i); |
cc4c13d5 DS |
3729 | push_space -= btrfs_token_item_size(&token, item); |
3730 | btrfs_set_token_item_offset(&token, item, push_space); | |
db94535d CM |
3731 | } |
3732 | ||
7518a238 | 3733 | left_nritems -= push_items; |
5f39d397 | 3734 | btrfs_set_header_nritems(left, left_nritems); |
00ec4c51 | 3735 | |
34a38218 CM |
3736 | if (left_nritems) |
3737 | btrfs_mark_buffer_dirty(left); | |
f0486c68 | 3738 | else |
6a884d7d | 3739 | btrfs_clean_tree_block(left); |
f0486c68 | 3740 | |
5f39d397 | 3741 | btrfs_mark_buffer_dirty(right); |
a429e513 | 3742 | |
5f39d397 CM |
3743 | btrfs_item_key(right, &disk_key, 0); |
3744 | btrfs_set_node_key(upper, &disk_key, slot + 1); | |
d6025579 | 3745 | btrfs_mark_buffer_dirty(upper); |
02217ed2 | 3746 | |
00ec4c51 | 3747 | /* then fixup the leaf pointer in the path */ |
7518a238 CM |
3748 | if (path->slots[0] >= left_nritems) { |
3749 | path->slots[0] -= left_nritems; | |
925baedd | 3750 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
6a884d7d | 3751 | btrfs_clean_tree_block(path->nodes[0]); |
925baedd | 3752 | btrfs_tree_unlock(path->nodes[0]); |
5f39d397 CM |
3753 | free_extent_buffer(path->nodes[0]); |
3754 | path->nodes[0] = right; | |
00ec4c51 CM |
3755 | path->slots[1] += 1; |
3756 | } else { | |
925baedd | 3757 | btrfs_tree_unlock(right); |
5f39d397 | 3758 | free_extent_buffer(right); |
00ec4c51 CM |
3759 | } |
3760 | return 0; | |
925baedd CM |
3761 | |
3762 | out_unlock: | |
3763 | btrfs_tree_unlock(right); | |
3764 | free_extent_buffer(right); | |
3765 | return 1; | |
00ec4c51 | 3766 | } |
925baedd | 3767 | |
44871b1b CM |
3768 | /* |
3769 | * push some data in the path leaf to the right, trying to free up at | |
3770 | * least data_size bytes. returns zero if the push worked, nonzero otherwise | |
3771 | * | |
3772 | * returns 1 if the push failed because the other node didn't have enough | |
3773 | * room, 0 if everything worked out and < 0 if there were major errors. | |
99d8f83c CM |
3774 | * |
3775 | * this will push starting from min_slot to the end of the leaf. It won't | |
3776 | * push any slot lower than min_slot | |
44871b1b CM |
3777 | */ |
3778 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root | |
99d8f83c CM |
3779 | *root, struct btrfs_path *path, |
3780 | int min_data_size, int data_size, | |
3781 | int empty, u32 min_slot) | |
44871b1b CM |
3782 | { |
3783 | struct extent_buffer *left = path->nodes[0]; | |
3784 | struct extent_buffer *right; | |
3785 | struct extent_buffer *upper; | |
3786 | int slot; | |
3787 | int free_space; | |
3788 | u32 left_nritems; | |
3789 | int ret; | |
3790 | ||
3791 | if (!path->nodes[1]) | |
3792 | return 1; | |
3793 | ||
3794 | slot = path->slots[1]; | |
3795 | upper = path->nodes[1]; | |
3796 | if (slot >= btrfs_header_nritems(upper) - 1) | |
3797 | return 1; | |
3798 | ||
3799 | btrfs_assert_tree_locked(path->nodes[1]); | |
3800 | ||
4b231ae4 | 3801 | right = btrfs_read_node_slot(upper, slot + 1); |
fb770ae4 LB |
3802 | /* |
3803 | * slot + 1 is not valid or we fail to read the right node, | |
3804 | * no big deal, just return. | |
3805 | */ | |
3806 | if (IS_ERR(right)) | |
91ca338d TI |
3807 | return 1; |
3808 | ||
44871b1b | 3809 | btrfs_tree_lock(right); |
8bead258 | 3810 | btrfs_set_lock_blocking_write(right); |
44871b1b | 3811 | |
e902baac | 3812 | free_space = btrfs_leaf_free_space(right); |
44871b1b CM |
3813 | if (free_space < data_size) |
3814 | goto out_unlock; | |
3815 | ||
3816 | /* cow and double check */ | |
3817 | ret = btrfs_cow_block(trans, root, right, upper, | |
9631e4cc | 3818 | slot + 1, &right, BTRFS_NESTING_COW); |
44871b1b CM |
3819 | if (ret) |
3820 | goto out_unlock; | |
3821 | ||
e902baac | 3822 | free_space = btrfs_leaf_free_space(right); |
44871b1b CM |
3823 | if (free_space < data_size) |
3824 | goto out_unlock; | |
3825 | ||
3826 | left_nritems = btrfs_header_nritems(left); | |
3827 | if (left_nritems == 0) | |
3828 | goto out_unlock; | |
3829 | ||
d16c702f QW |
3830 | if (check_sibling_keys(left, right)) { |
3831 | ret = -EUCLEAN; | |
3832 | btrfs_tree_unlock(right); | |
3833 | free_extent_buffer(right); | |
3834 | return ret; | |
3835 | } | |
2ef1fed2 FDBM |
3836 | if (path->slots[0] == left_nritems && !empty) { |
3837 | /* Key greater than all keys in the leaf, right neighbor has | |
3838 | * enough room for it and we're not emptying our leaf to delete | |
3839 | * it, therefore use right neighbor to insert the new item and | |
52042d8e | 3840 | * no need to touch/dirty our left leaf. */ |
2ef1fed2 FDBM |
3841 | btrfs_tree_unlock(left); |
3842 | free_extent_buffer(left); | |
3843 | path->nodes[0] = right; | |
3844 | path->slots[0] = 0; | |
3845 | path->slots[1]++; | |
3846 | return 0; | |
3847 | } | |
3848 | ||
f72f0010 | 3849 | return __push_leaf_right(path, min_data_size, empty, |
99d8f83c | 3850 | right, free_space, left_nritems, min_slot); |
44871b1b CM |
3851 | out_unlock: |
3852 | btrfs_tree_unlock(right); | |
3853 | free_extent_buffer(right); | |
3854 | return 1; | |
3855 | } | |
3856 | ||
74123bd7 CM |
3857 | /* |
3858 | * push some data in the path leaf to the left, trying to free up at | |
3859 | * least data_size bytes. returns zero if the push worked, nonzero otherwise | |
99d8f83c CM |
3860 | * |
3861 | * max_slot can put a limit on how far into the leaf we'll push items. The | |
3862 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the | |
3863 | * items | |
74123bd7 | 3864 | */ |
8087c193 | 3865 | static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, |
44871b1b | 3866 | int empty, struct extent_buffer *left, |
99d8f83c CM |
3867 | int free_space, u32 right_nritems, |
3868 | u32 max_slot) | |
be0e5c09 | 3869 | { |
8087c193 | 3870 | struct btrfs_fs_info *fs_info = left->fs_info; |
5f39d397 CM |
3871 | struct btrfs_disk_key disk_key; |
3872 | struct extent_buffer *right = path->nodes[0]; | |
be0e5c09 | 3873 | int i; |
be0e5c09 CM |
3874 | int push_space = 0; |
3875 | int push_items = 0; | |
0783fcfc | 3876 | struct btrfs_item *item; |
7518a238 | 3877 | u32 old_left_nritems; |
34a38218 | 3878 | u32 nr; |
aa5d6bed | 3879 | int ret = 0; |
db94535d CM |
3880 | u32 this_item_size; |
3881 | u32 old_left_item_size; | |
cfed81a0 CM |
3882 | struct btrfs_map_token token; |
3883 | ||
34a38218 | 3884 | if (empty) |
99d8f83c | 3885 | nr = min(right_nritems, max_slot); |
34a38218 | 3886 | else |
99d8f83c | 3887 | nr = min(right_nritems - 1, max_slot); |
34a38218 CM |
3888 | |
3889 | for (i = 0; i < nr; i++) { | |
dd3cc16b | 3890 | item = btrfs_item_nr(i); |
db94535d | 3891 | |
31840ae1 ZY |
3892 | if (!empty && push_items > 0) { |
3893 | if (path->slots[0] < i) | |
3894 | break; | |
3895 | if (path->slots[0] == i) { | |
e902baac DS |
3896 | int space = btrfs_leaf_free_space(right); |
3897 | ||
31840ae1 ZY |
3898 | if (space + push_space * 2 > free_space) |
3899 | break; | |
3900 | } | |
3901 | } | |
3902 | ||
be0e5c09 | 3903 | if (path->slots[0] == i) |
87b29b20 | 3904 | push_space += data_size; |
db94535d CM |
3905 | |
3906 | this_item_size = btrfs_item_size(right, item); | |
3907 | if (this_item_size + sizeof(*item) + push_space > free_space) | |
be0e5c09 | 3908 | break; |
db94535d | 3909 | |
be0e5c09 | 3910 | push_items++; |
db94535d CM |
3911 | push_space += this_item_size + sizeof(*item); |
3912 | } | |
3913 | ||
be0e5c09 | 3914 | if (push_items == 0) { |
925baedd CM |
3915 | ret = 1; |
3916 | goto out; | |
be0e5c09 | 3917 | } |
fae7f21c | 3918 | WARN_ON(!empty && push_items == btrfs_header_nritems(right)); |
5f39d397 | 3919 | |
be0e5c09 | 3920 | /* push data from right to left */ |
5f39d397 CM |
3921 | copy_extent_buffer(left, right, |
3922 | btrfs_item_nr_offset(btrfs_header_nritems(left)), | |
3923 | btrfs_item_nr_offset(0), | |
3924 | push_items * sizeof(struct btrfs_item)); | |
3925 | ||
0b246afa | 3926 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - |
d397712b | 3927 | btrfs_item_offset_nr(right, push_items - 1); |
5f39d397 | 3928 | |
3d9ec8c4 | 3929 | copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + |
8f881e8c | 3930 | leaf_data_end(left) - push_space, |
3d9ec8c4 | 3931 | BTRFS_LEAF_DATA_OFFSET + |
5f39d397 | 3932 | btrfs_item_offset_nr(right, push_items - 1), |
d6025579 | 3933 | push_space); |
5f39d397 | 3934 | old_left_nritems = btrfs_header_nritems(left); |
87b29b20 | 3935 | BUG_ON(old_left_nritems <= 0); |
eb60ceac | 3936 | |
c82f823c | 3937 | btrfs_init_map_token(&token, left); |
db94535d | 3938 | old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); |
0783fcfc | 3939 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
5f39d397 | 3940 | u32 ioff; |
db94535d | 3941 | |
dd3cc16b | 3942 | item = btrfs_item_nr(i); |
db94535d | 3943 | |
cc4c13d5 DS |
3944 | ioff = btrfs_token_item_offset(&token, item); |
3945 | btrfs_set_token_item_offset(&token, item, | |
3946 | ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size)); | |
be0e5c09 | 3947 | } |
5f39d397 | 3948 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
be0e5c09 CM |
3949 | |
3950 | /* fixup right node */ | |
31b1a2bd JL |
3951 | if (push_items > right_nritems) |
3952 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, | |
d397712b | 3953 | right_nritems); |
34a38218 CM |
3954 | |
3955 | if (push_items < right_nritems) { | |
3956 | push_space = btrfs_item_offset_nr(right, push_items - 1) - | |
8f881e8c | 3957 | leaf_data_end(right); |
3d9ec8c4 | 3958 | memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + |
0b246afa | 3959 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
3d9ec8c4 | 3960 | BTRFS_LEAF_DATA_OFFSET + |
8f881e8c | 3961 | leaf_data_end(right), push_space); |
34a38218 CM |
3962 | |
3963 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), | |
5f39d397 CM |
3964 | btrfs_item_nr_offset(push_items), |
3965 | (btrfs_header_nritems(right) - push_items) * | |
3966 | sizeof(struct btrfs_item)); | |
34a38218 | 3967 | } |
c82f823c DS |
3968 | |
3969 | btrfs_init_map_token(&token, right); | |
eef1c494 Y |
3970 | right_nritems -= push_items; |
3971 | btrfs_set_header_nritems(right, right_nritems); | |
0b246afa | 3972 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
5f39d397 | 3973 | for (i = 0; i < right_nritems; i++) { |
dd3cc16b | 3974 | item = btrfs_item_nr(i); |
db94535d | 3975 | |
cc4c13d5 DS |
3976 | push_space = push_space - btrfs_token_item_size(&token, item); |
3977 | btrfs_set_token_item_offset(&token, item, push_space); | |
db94535d | 3978 | } |
eb60ceac | 3979 | |
5f39d397 | 3980 | btrfs_mark_buffer_dirty(left); |
34a38218 CM |
3981 | if (right_nritems) |
3982 | btrfs_mark_buffer_dirty(right); | |
f0486c68 | 3983 | else |
6a884d7d | 3984 | btrfs_clean_tree_block(right); |
098f59c2 | 3985 | |
5f39d397 | 3986 | btrfs_item_key(right, &disk_key, 0); |
b167fa91 | 3987 | fixup_low_keys(path, &disk_key, 1); |
be0e5c09 CM |
3988 | |
3989 | /* then fixup the leaf pointer in the path */ | |
3990 | if (path->slots[0] < push_items) { | |
3991 | path->slots[0] += old_left_nritems; | |
925baedd | 3992 | btrfs_tree_unlock(path->nodes[0]); |
5f39d397 CM |
3993 | free_extent_buffer(path->nodes[0]); |
3994 | path->nodes[0] = left; | |
be0e5c09 CM |
3995 | path->slots[1] -= 1; |
3996 | } else { | |
925baedd | 3997 | btrfs_tree_unlock(left); |
5f39d397 | 3998 | free_extent_buffer(left); |
be0e5c09 CM |
3999 | path->slots[0] -= push_items; |
4000 | } | |
eb60ceac | 4001 | BUG_ON(path->slots[0] < 0); |
aa5d6bed | 4002 | return ret; |
925baedd CM |
4003 | out: |
4004 | btrfs_tree_unlock(left); | |
4005 | free_extent_buffer(left); | |
4006 | return ret; | |
be0e5c09 CM |
4007 | } |
4008 | ||
44871b1b CM |
4009 | /* |
4010 | * push some data in the path leaf to the left, trying to free up at | |
4011 | * least data_size bytes. returns zero if the push worked, nonzero otherwise | |
99d8f83c CM |
4012 | * |
4013 | * max_slot can put a limit on how far into the leaf we'll push items. The | |
4014 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the | |
4015 | * items | |
44871b1b CM |
4016 | */ |
4017 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root | |
99d8f83c CM |
4018 | *root, struct btrfs_path *path, int min_data_size, |
4019 | int data_size, int empty, u32 max_slot) | |
44871b1b CM |
4020 | { |
4021 | struct extent_buffer *right = path->nodes[0]; | |
4022 | struct extent_buffer *left; | |
4023 | int slot; | |
4024 | int free_space; | |
4025 | u32 right_nritems; | |
4026 | int ret = 0; | |
4027 | ||
4028 | slot = path->slots[1]; | |
4029 | if (slot == 0) | |
4030 | return 1; | |
4031 | if (!path->nodes[1]) | |
4032 | return 1; | |
4033 | ||
4034 | right_nritems = btrfs_header_nritems(right); | |
4035 | if (right_nritems == 0) | |
4036 | return 1; | |
4037 | ||
4038 | btrfs_assert_tree_locked(path->nodes[1]); | |
4039 | ||
4b231ae4 | 4040 | left = btrfs_read_node_slot(path->nodes[1], slot - 1); |
fb770ae4 LB |
4041 | /* |
4042 | * slot - 1 is not valid or we fail to read the left node, | |
4043 | * no big deal, just return. | |
4044 | */ | |
4045 | if (IS_ERR(left)) | |
91ca338d TI |
4046 | return 1; |
4047 | ||
44871b1b | 4048 | btrfs_tree_lock(left); |
8bead258 | 4049 | btrfs_set_lock_blocking_write(left); |
44871b1b | 4050 | |
e902baac | 4051 | free_space = btrfs_leaf_free_space(left); |
44871b1b CM |
4052 | if (free_space < data_size) { |
4053 | ret = 1; | |
4054 | goto out; | |
4055 | } | |
4056 | ||
4057 | /* cow and double check */ | |
4058 | ret = btrfs_cow_block(trans, root, left, | |
9631e4cc JB |
4059 | path->nodes[1], slot - 1, &left, |
4060 | BTRFS_NESTING_COW); | |
44871b1b CM |
4061 | if (ret) { |
4062 | /* we hit -ENOSPC, but it isn't fatal here */ | |
79787eaa JM |
4063 | if (ret == -ENOSPC) |
4064 | ret = 1; | |
44871b1b CM |
4065 | goto out; |
4066 | } | |
4067 | ||
e902baac | 4068 | free_space = btrfs_leaf_free_space(left); |
44871b1b CM |
4069 | if (free_space < data_size) { |
4070 | ret = 1; | |
4071 | goto out; | |
4072 | } | |
4073 | ||
d16c702f QW |
4074 | if (check_sibling_keys(left, right)) { |
4075 | ret = -EUCLEAN; | |
4076 | goto out; | |
4077 | } | |
8087c193 | 4078 | return __push_leaf_left(path, min_data_size, |
99d8f83c CM |
4079 | empty, left, free_space, right_nritems, |
4080 | max_slot); | |
44871b1b CM |
4081 | out: |
4082 | btrfs_tree_unlock(left); | |
4083 | free_extent_buffer(left); | |
4084 | return ret; | |
4085 | } | |
4086 | ||
4087 | /* | |
4088 | * split the path's leaf in two, making sure there is at least data_size | |
4089 | * available for the resulting leaf level of the path. | |
44871b1b | 4090 | */ |
143bede5 | 4091 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
143bede5 JM |
4092 | struct btrfs_path *path, |
4093 | struct extent_buffer *l, | |
4094 | struct extent_buffer *right, | |
4095 | int slot, int mid, int nritems) | |
44871b1b | 4096 | { |
94f94ad9 | 4097 | struct btrfs_fs_info *fs_info = trans->fs_info; |
44871b1b CM |
4098 | int data_copy_size; |
4099 | int rt_data_off; | |
4100 | int i; | |
44871b1b | 4101 | struct btrfs_disk_key disk_key; |
cfed81a0 CM |
4102 | struct btrfs_map_token token; |
4103 | ||
44871b1b CM |
4104 | nritems = nritems - mid; |
4105 | btrfs_set_header_nritems(right, nritems); | |
8f881e8c | 4106 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l); |
44871b1b CM |
4107 | |
4108 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), | |
4109 | btrfs_item_nr_offset(mid), | |
4110 | nritems * sizeof(struct btrfs_item)); | |
4111 | ||
4112 | copy_extent_buffer(right, l, | |
3d9ec8c4 NB |
4113 | BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - |
4114 | data_copy_size, BTRFS_LEAF_DATA_OFFSET + | |
8f881e8c | 4115 | leaf_data_end(l), data_copy_size); |
44871b1b | 4116 | |
0b246afa | 4117 | rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); |
44871b1b | 4118 | |
c82f823c | 4119 | btrfs_init_map_token(&token, right); |
44871b1b | 4120 | for (i = 0; i < nritems; i++) { |
dd3cc16b | 4121 | struct btrfs_item *item = btrfs_item_nr(i); |
44871b1b CM |
4122 | u32 ioff; |
4123 | ||
cc4c13d5 DS |
4124 | ioff = btrfs_token_item_offset(&token, item); |
4125 | btrfs_set_token_item_offset(&token, item, ioff + rt_data_off); | |
44871b1b CM |
4126 | } |
4127 | ||
44871b1b | 4128 | btrfs_set_header_nritems(l, mid); |
44871b1b | 4129 | btrfs_item_key(right, &disk_key, 0); |
6ad3cf6d | 4130 | insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1); |
44871b1b CM |
4131 | |
4132 | btrfs_mark_buffer_dirty(right); | |
4133 | btrfs_mark_buffer_dirty(l); | |
4134 | BUG_ON(path->slots[0] != slot); | |
4135 | ||
44871b1b CM |
4136 | if (mid <= slot) { |
4137 | btrfs_tree_unlock(path->nodes[0]); | |
4138 | free_extent_buffer(path->nodes[0]); | |
4139 | path->nodes[0] = right; | |
4140 | path->slots[0] -= mid; | |
4141 | path->slots[1] += 1; | |
4142 | } else { | |
4143 | btrfs_tree_unlock(right); | |
4144 | free_extent_buffer(right); | |
4145 | } | |
4146 | ||
4147 | BUG_ON(path->slots[0] < 0); | |
44871b1b CM |
4148 | } |
4149 | ||
99d8f83c CM |
4150 | /* |
4151 | * double splits happen when we need to insert a big item in the middle | |
4152 | * of a leaf. A double split can leave us with 3 mostly empty leaves: | |
4153 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] | |
4154 | * A B C | |
4155 | * | |
4156 | * We avoid this by trying to push the items on either side of our target | |
4157 | * into the adjacent leaves. If all goes well we can avoid the double split | |
4158 | * completely. | |
4159 | */ | |
4160 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, | |
4161 | struct btrfs_root *root, | |
4162 | struct btrfs_path *path, | |
4163 | int data_size) | |
4164 | { | |
4165 | int ret; | |
4166 | int progress = 0; | |
4167 | int slot; | |
4168 | u32 nritems; | |
5a4267ca | 4169 | int space_needed = data_size; |
99d8f83c CM |
4170 | |
4171 | slot = path->slots[0]; | |
5a4267ca | 4172 | if (slot < btrfs_header_nritems(path->nodes[0])) |
e902baac | 4173 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
99d8f83c CM |
4174 | |
4175 | /* | |
4176 | * try to push all the items after our slot into the | |
4177 | * right leaf | |
4178 | */ | |
5a4267ca | 4179 | ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); |
99d8f83c CM |
4180 | if (ret < 0) |
4181 | return ret; | |
4182 | ||
4183 | if (ret == 0) | |
4184 | progress++; | |
4185 | ||
4186 | nritems = btrfs_header_nritems(path->nodes[0]); | |
4187 | /* | |
4188 | * our goal is to get our slot at the start or end of a leaf. If | |
4189 | * we've done so we're done | |
4190 | */ | |
4191 | if (path->slots[0] == 0 || path->slots[0] == nritems) | |
4192 | return 0; | |
4193 | ||
e902baac | 4194 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
99d8f83c CM |
4195 | return 0; |
4196 | ||
4197 | /* try to push all the items before our slot into the next leaf */ | |
4198 | slot = path->slots[0]; | |
263d3995 FM |
4199 | space_needed = data_size; |
4200 | if (slot > 0) | |
e902baac | 4201 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
5a4267ca | 4202 | ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); |
99d8f83c CM |
4203 | if (ret < 0) |
4204 | return ret; | |
4205 | ||
4206 | if (ret == 0) | |
4207 | progress++; | |
4208 | ||
4209 | if (progress) | |
4210 | return 0; | |
4211 | return 1; | |
4212 | } | |
4213 | ||
74123bd7 CM |
4214 | /* |
4215 | * split the path's leaf in two, making sure there is at least data_size | |
4216 | * available for the resulting leaf level of the path. | |
aa5d6bed CM |
4217 | * |
4218 | * returns 0 if all went well and < 0 on failure. | |
74123bd7 | 4219 | */ |
e02119d5 CM |
4220 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
4221 | struct btrfs_root *root, | |
310712b2 | 4222 | const struct btrfs_key *ins_key, |
e02119d5 CM |
4223 | struct btrfs_path *path, int data_size, |
4224 | int extend) | |
be0e5c09 | 4225 | { |
5d4f98a2 | 4226 | struct btrfs_disk_key disk_key; |
5f39d397 | 4227 | struct extent_buffer *l; |
7518a238 | 4228 | u32 nritems; |
eb60ceac CM |
4229 | int mid; |
4230 | int slot; | |
5f39d397 | 4231 | struct extent_buffer *right; |
b7a0365e | 4232 | struct btrfs_fs_info *fs_info = root->fs_info; |
d4dbff95 | 4233 | int ret = 0; |
aa5d6bed | 4234 | int wret; |
5d4f98a2 | 4235 | int split; |
cc0c5538 | 4236 | int num_doubles = 0; |
99d8f83c | 4237 | int tried_avoid_double = 0; |
aa5d6bed | 4238 | |
a5719521 YZ |
4239 | l = path->nodes[0]; |
4240 | slot = path->slots[0]; | |
4241 | if (extend && data_size + btrfs_item_size_nr(l, slot) + | |
0b246afa | 4242 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) |
a5719521 YZ |
4243 | return -EOVERFLOW; |
4244 | ||
40689478 | 4245 | /* first try to make some room by pushing left and right */ |
33157e05 | 4246 | if (data_size && path->nodes[1]) { |
5a4267ca FDBM |
4247 | int space_needed = data_size; |
4248 | ||
4249 | if (slot < btrfs_header_nritems(l)) | |
e902baac | 4250 | space_needed -= btrfs_leaf_free_space(l); |
5a4267ca FDBM |
4251 | |
4252 | wret = push_leaf_right(trans, root, path, space_needed, | |
4253 | space_needed, 0, 0); | |
d397712b | 4254 | if (wret < 0) |
eaee50e8 | 4255 | return wret; |
3685f791 | 4256 | if (wret) { |
263d3995 FM |
4257 | space_needed = data_size; |
4258 | if (slot > 0) | |
e902baac | 4259 | space_needed -= btrfs_leaf_free_space(l); |
5a4267ca FDBM |
4260 | wret = push_leaf_left(trans, root, path, space_needed, |
4261 | space_needed, 0, (u32)-1); | |
3685f791 CM |
4262 | if (wret < 0) |
4263 | return wret; | |
4264 | } | |
4265 | l = path->nodes[0]; | |
aa5d6bed | 4266 | |
3685f791 | 4267 | /* did the pushes work? */ |
e902baac | 4268 | if (btrfs_leaf_free_space(l) >= data_size) |
3685f791 | 4269 | return 0; |
3326d1b0 | 4270 | } |
aa5d6bed | 4271 | |
5c680ed6 | 4272 | if (!path->nodes[1]) { |
fdd99c72 | 4273 | ret = insert_new_root(trans, root, path, 1); |
5c680ed6 CM |
4274 | if (ret) |
4275 | return ret; | |
4276 | } | |
cc0c5538 | 4277 | again: |
5d4f98a2 | 4278 | split = 1; |
cc0c5538 | 4279 | l = path->nodes[0]; |
eb60ceac | 4280 | slot = path->slots[0]; |
5f39d397 | 4281 | nritems = btrfs_header_nritems(l); |
d397712b | 4282 | mid = (nritems + 1) / 2; |
54aa1f4d | 4283 | |
5d4f98a2 YZ |
4284 | if (mid <= slot) { |
4285 | if (nritems == 1 || | |
4286 | leaf_space_used(l, mid, nritems - mid) + data_size > | |
0b246afa | 4287 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
5d4f98a2 YZ |
4288 | if (slot >= nritems) { |
4289 | split = 0; | |
4290 | } else { | |
4291 | mid = slot; | |
4292 | if (mid != nritems && | |
4293 | leaf_space_used(l, mid, nritems - mid) + | |
0b246afa | 4294 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
99d8f83c CM |
4295 | if (data_size && !tried_avoid_double) |
4296 | goto push_for_double; | |
5d4f98a2 YZ |
4297 | split = 2; |
4298 | } | |
4299 | } | |
4300 | } | |
4301 | } else { | |
4302 | if (leaf_space_used(l, 0, mid) + data_size > | |
0b246afa | 4303 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
5d4f98a2 YZ |
4304 | if (!extend && data_size && slot == 0) { |
4305 | split = 0; | |
4306 | } else if ((extend || !data_size) && slot == 0) { | |
4307 | mid = 1; | |
4308 | } else { | |
4309 | mid = slot; | |
4310 | if (mid != nritems && | |
4311 | leaf_space_used(l, mid, nritems - mid) + | |
0b246afa | 4312 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
99d8f83c CM |
4313 | if (data_size && !tried_avoid_double) |
4314 | goto push_for_double; | |
67871254 | 4315 | split = 2; |
5d4f98a2 YZ |
4316 | } |
4317 | } | |
4318 | } | |
4319 | } | |
4320 | ||
4321 | if (split == 0) | |
4322 | btrfs_cpu_key_to_disk(&disk_key, ins_key); | |
4323 | else | |
4324 | btrfs_item_key(l, &disk_key, mid); | |
4325 | ||
a6279470 | 4326 | right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0, |
9631e4cc | 4327 | l->start, 0, BTRFS_NESTING_NORMAL); |
f0486c68 | 4328 | if (IS_ERR(right)) |
5f39d397 | 4329 | return PTR_ERR(right); |
f0486c68 | 4330 | |
0b246afa | 4331 | root_add_used(root, fs_info->nodesize); |
5f39d397 | 4332 | |
5d4f98a2 YZ |
4333 | if (split == 0) { |
4334 | if (mid <= slot) { | |
4335 | btrfs_set_header_nritems(right, 0); | |
6ad3cf6d | 4336 | insert_ptr(trans, path, &disk_key, |
2ff7e61e | 4337 | right->start, path->slots[1] + 1, 1); |
5d4f98a2 YZ |
4338 | btrfs_tree_unlock(path->nodes[0]); |
4339 | free_extent_buffer(path->nodes[0]); | |
4340 | path->nodes[0] = right; | |
4341 | path->slots[0] = 0; | |
4342 | path->slots[1] += 1; | |
4343 | } else { | |
4344 | btrfs_set_header_nritems(right, 0); | |
6ad3cf6d | 4345 | insert_ptr(trans, path, &disk_key, |
2ff7e61e | 4346 | right->start, path->slots[1], 1); |
5d4f98a2 YZ |
4347 | btrfs_tree_unlock(path->nodes[0]); |
4348 | free_extent_buffer(path->nodes[0]); | |
4349 | path->nodes[0] = right; | |
4350 | path->slots[0] = 0; | |
143bede5 | 4351 | if (path->slots[1] == 0) |
b167fa91 | 4352 | fixup_low_keys(path, &disk_key, 1); |
d4dbff95 | 4353 | } |
196e0249 LB |
4354 | /* |
4355 | * We create a new leaf 'right' for the required ins_len and | |
4356 | * we'll do btrfs_mark_buffer_dirty() on this leaf after copying | |
4357 | * the content of ins_len to 'right'. | |
4358 | */ | |
5d4f98a2 | 4359 | return ret; |
d4dbff95 | 4360 | } |
74123bd7 | 4361 | |
94f94ad9 | 4362 | copy_for_split(trans, path, l, right, slot, mid, nritems); |
31840ae1 | 4363 | |
5d4f98a2 | 4364 | if (split == 2) { |
cc0c5538 CM |
4365 | BUG_ON(num_doubles != 0); |
4366 | num_doubles++; | |
4367 | goto again; | |
a429e513 | 4368 | } |
44871b1b | 4369 | |
143bede5 | 4370 | return 0; |
99d8f83c CM |
4371 | |
4372 | push_for_double: | |
4373 | push_for_double_split(trans, root, path, data_size); | |
4374 | tried_avoid_double = 1; | |
e902baac | 4375 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
99d8f83c CM |
4376 | return 0; |
4377 | goto again; | |
be0e5c09 CM |
4378 | } |
4379 | ||
ad48fd75 YZ |
4380 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
4381 | struct btrfs_root *root, | |
4382 | struct btrfs_path *path, int ins_len) | |
459931ec | 4383 | { |
ad48fd75 | 4384 | struct btrfs_key key; |
459931ec | 4385 | struct extent_buffer *leaf; |
ad48fd75 YZ |
4386 | struct btrfs_file_extent_item *fi; |
4387 | u64 extent_len = 0; | |
4388 | u32 item_size; | |
4389 | int ret; | |
459931ec CM |
4390 | |
4391 | leaf = path->nodes[0]; | |
ad48fd75 YZ |
4392 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
4393 | ||
4394 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && | |
4395 | key.type != BTRFS_EXTENT_CSUM_KEY); | |
4396 | ||
e902baac | 4397 | if (btrfs_leaf_free_space(leaf) >= ins_len) |
ad48fd75 | 4398 | return 0; |
459931ec CM |
4399 | |
4400 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
ad48fd75 YZ |
4401 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
4402 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
4403 | struct btrfs_file_extent_item); | |
4404 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
4405 | } | |
b3b4aa74 | 4406 | btrfs_release_path(path); |
459931ec | 4407 | |
459931ec | 4408 | path->keep_locks = 1; |
ad48fd75 YZ |
4409 | path->search_for_split = 1; |
4410 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
459931ec | 4411 | path->search_for_split = 0; |
a8df6fe6 FM |
4412 | if (ret > 0) |
4413 | ret = -EAGAIN; | |
ad48fd75 YZ |
4414 | if (ret < 0) |
4415 | goto err; | |
459931ec | 4416 | |
ad48fd75 YZ |
4417 | ret = -EAGAIN; |
4418 | leaf = path->nodes[0]; | |
a8df6fe6 FM |
4419 | /* if our item isn't there, return now */ |
4420 | if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) | |
ad48fd75 YZ |
4421 | goto err; |
4422 | ||
109f6aef | 4423 | /* the leaf has changed, it now has room. return now */ |
e902baac | 4424 | if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) |
109f6aef CM |
4425 | goto err; |
4426 | ||
ad48fd75 YZ |
4427 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
4428 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
4429 | struct btrfs_file_extent_item); | |
4430 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) | |
4431 | goto err; | |
459931ec CM |
4432 | } |
4433 | ||
b9473439 | 4434 | btrfs_set_path_blocking(path); |
ad48fd75 | 4435 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
f0486c68 YZ |
4436 | if (ret) |
4437 | goto err; | |
459931ec | 4438 | |
ad48fd75 | 4439 | path->keep_locks = 0; |
b9473439 | 4440 | btrfs_unlock_up_safe(path, 1); |
ad48fd75 YZ |
4441 | return 0; |
4442 | err: | |
4443 | path->keep_locks = 0; | |
4444 | return ret; | |
4445 | } | |
4446 | ||
25263cd7 | 4447 | static noinline int split_item(struct btrfs_path *path, |
310712b2 | 4448 | const struct btrfs_key *new_key, |
ad48fd75 YZ |
4449 | unsigned long split_offset) |
4450 | { | |
4451 | struct extent_buffer *leaf; | |
4452 | struct btrfs_item *item; | |
4453 | struct btrfs_item *new_item; | |
4454 | int slot; | |
4455 | char *buf; | |
4456 | u32 nritems; | |
4457 | u32 item_size; | |
4458 | u32 orig_offset; | |
4459 | struct btrfs_disk_key disk_key; | |
4460 | ||
b9473439 | 4461 | leaf = path->nodes[0]; |
e902baac | 4462 | BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); |
b9473439 | 4463 | |
b4ce94de CM |
4464 | btrfs_set_path_blocking(path); |
4465 | ||
dd3cc16b | 4466 | item = btrfs_item_nr(path->slots[0]); |
459931ec CM |
4467 | orig_offset = btrfs_item_offset(leaf, item); |
4468 | item_size = btrfs_item_size(leaf, item); | |
4469 | ||
459931ec | 4470 | buf = kmalloc(item_size, GFP_NOFS); |
ad48fd75 YZ |
4471 | if (!buf) |
4472 | return -ENOMEM; | |
4473 | ||
459931ec CM |
4474 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
4475 | path->slots[0]), item_size); | |
459931ec | 4476 | |
ad48fd75 | 4477 | slot = path->slots[0] + 1; |
459931ec | 4478 | nritems = btrfs_header_nritems(leaf); |
459931ec CM |
4479 | if (slot != nritems) { |
4480 | /* shift the items */ | |
4481 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), | |
ad48fd75 YZ |
4482 | btrfs_item_nr_offset(slot), |
4483 | (nritems - slot) * sizeof(struct btrfs_item)); | |
459931ec CM |
4484 | } |
4485 | ||
4486 | btrfs_cpu_key_to_disk(&disk_key, new_key); | |
4487 | btrfs_set_item_key(leaf, &disk_key, slot); | |
4488 | ||
dd3cc16b | 4489 | new_item = btrfs_item_nr(slot); |
459931ec CM |
4490 | |
4491 | btrfs_set_item_offset(leaf, new_item, orig_offset); | |
4492 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); | |
4493 | ||
4494 | btrfs_set_item_offset(leaf, item, | |
4495 | orig_offset + item_size - split_offset); | |
4496 | btrfs_set_item_size(leaf, item, split_offset); | |
4497 | ||
4498 | btrfs_set_header_nritems(leaf, nritems + 1); | |
4499 | ||
4500 | /* write the data for the start of the original item */ | |
4501 | write_extent_buffer(leaf, buf, | |
4502 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
4503 | split_offset); | |
4504 | ||
4505 | /* write the data for the new item */ | |
4506 | write_extent_buffer(leaf, buf + split_offset, | |
4507 | btrfs_item_ptr_offset(leaf, slot), | |
4508 | item_size - split_offset); | |
4509 | btrfs_mark_buffer_dirty(leaf); | |
4510 | ||
e902baac | 4511 | BUG_ON(btrfs_leaf_free_space(leaf) < 0); |
459931ec | 4512 | kfree(buf); |
ad48fd75 YZ |
4513 | return 0; |
4514 | } | |
4515 | ||
4516 | /* | |
4517 | * This function splits a single item into two items, | |
4518 | * giving 'new_key' to the new item and splitting the | |
4519 | * old one at split_offset (from the start of the item). | |
4520 | * | |
4521 | * The path may be released by this operation. After | |
4522 | * the split, the path is pointing to the old item. The | |
4523 | * new item is going to be in the same node as the old one. | |
4524 | * | |
4525 | * Note, the item being split must be smaller enough to live alone on | |
4526 | * a tree block with room for one extra struct btrfs_item | |
4527 | * | |
4528 | * This allows us to split the item in place, keeping a lock on the | |
4529 | * leaf the entire time. | |
4530 | */ | |
4531 | int btrfs_split_item(struct btrfs_trans_handle *trans, | |
4532 | struct btrfs_root *root, | |
4533 | struct btrfs_path *path, | |
310712b2 | 4534 | const struct btrfs_key *new_key, |
ad48fd75 YZ |
4535 | unsigned long split_offset) |
4536 | { | |
4537 | int ret; | |
4538 | ret = setup_leaf_for_split(trans, root, path, | |
4539 | sizeof(struct btrfs_item)); | |
4540 | if (ret) | |
4541 | return ret; | |
4542 | ||
25263cd7 | 4543 | ret = split_item(path, new_key, split_offset); |
459931ec CM |
4544 | return ret; |
4545 | } | |
4546 | ||
ad48fd75 YZ |
4547 | /* |
4548 | * This function duplicate a item, giving 'new_key' to the new item. | |
4549 | * It guarantees both items live in the same tree leaf and the new item | |
4550 | * is contiguous with the original item. | |
4551 | * | |
4552 | * This allows us to split file extent in place, keeping a lock on the | |
4553 | * leaf the entire time. | |
4554 | */ | |
4555 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, | |
4556 | struct btrfs_root *root, | |
4557 | struct btrfs_path *path, | |
310712b2 | 4558 | const struct btrfs_key *new_key) |
ad48fd75 YZ |
4559 | { |
4560 | struct extent_buffer *leaf; | |
4561 | int ret; | |
4562 | u32 item_size; | |
4563 | ||
4564 | leaf = path->nodes[0]; | |
4565 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
4566 | ret = setup_leaf_for_split(trans, root, path, | |
4567 | item_size + sizeof(struct btrfs_item)); | |
4568 | if (ret) | |
4569 | return ret; | |
4570 | ||
4571 | path->slots[0]++; | |
afe5fea7 | 4572 | setup_items_for_insert(root, path, new_key, &item_size, |
143bede5 JM |
4573 | item_size, item_size + |
4574 | sizeof(struct btrfs_item), 1); | |
ad48fd75 YZ |
4575 | leaf = path->nodes[0]; |
4576 | memcpy_extent_buffer(leaf, | |
4577 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
4578 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), | |
4579 | item_size); | |
4580 | return 0; | |
4581 | } | |
4582 | ||
d352ac68 CM |
4583 | /* |
4584 | * make the item pointed to by the path smaller. new_size indicates | |
4585 | * how small to make it, and from_end tells us if we just chop bytes | |
4586 | * off the end of the item or if we shift the item to chop bytes off | |
4587 | * the front. | |
4588 | */ | |
78ac4f9e | 4589 | void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) |
b18c6685 | 4590 | { |
b18c6685 | 4591 | int slot; |
5f39d397 CM |
4592 | struct extent_buffer *leaf; |
4593 | struct btrfs_item *item; | |
b18c6685 CM |
4594 | u32 nritems; |
4595 | unsigned int data_end; | |
4596 | unsigned int old_data_start; | |
4597 | unsigned int old_size; | |
4598 | unsigned int size_diff; | |
4599 | int i; | |
cfed81a0 CM |
4600 | struct btrfs_map_token token; |
4601 | ||
5f39d397 | 4602 | leaf = path->nodes[0]; |
179e29e4 CM |
4603 | slot = path->slots[0]; |
4604 | ||
4605 | old_size = btrfs_item_size_nr(leaf, slot); | |
4606 | if (old_size == new_size) | |
143bede5 | 4607 | return; |
b18c6685 | 4608 | |
5f39d397 | 4609 | nritems = btrfs_header_nritems(leaf); |
8f881e8c | 4610 | data_end = leaf_data_end(leaf); |
b18c6685 | 4611 | |
5f39d397 | 4612 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
179e29e4 | 4613 | |
b18c6685 CM |
4614 | size_diff = old_size - new_size; |
4615 | ||
4616 | BUG_ON(slot < 0); | |
4617 | BUG_ON(slot >= nritems); | |
4618 | ||
4619 | /* | |
4620 | * item0..itemN ... dataN.offset..dataN.size .. data0.size | |
4621 | */ | |
4622 | /* first correct the data pointers */ | |
c82f823c | 4623 | btrfs_init_map_token(&token, leaf); |
b18c6685 | 4624 | for (i = slot; i < nritems; i++) { |
5f39d397 | 4625 | u32 ioff; |
dd3cc16b | 4626 | item = btrfs_item_nr(i); |
db94535d | 4627 | |
cc4c13d5 DS |
4628 | ioff = btrfs_token_item_offset(&token, item); |
4629 | btrfs_set_token_item_offset(&token, item, ioff + size_diff); | |
b18c6685 | 4630 | } |
db94535d | 4631 | |
b18c6685 | 4632 | /* shift the data */ |
179e29e4 | 4633 | if (from_end) { |
3d9ec8c4 NB |
4634 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
4635 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + | |
179e29e4 CM |
4636 | data_end, old_data_start + new_size - data_end); |
4637 | } else { | |
4638 | struct btrfs_disk_key disk_key; | |
4639 | u64 offset; | |
4640 | ||
4641 | btrfs_item_key(leaf, &disk_key, slot); | |
4642 | ||
4643 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { | |
4644 | unsigned long ptr; | |
4645 | struct btrfs_file_extent_item *fi; | |
4646 | ||
4647 | fi = btrfs_item_ptr(leaf, slot, | |
4648 | struct btrfs_file_extent_item); | |
4649 | fi = (struct btrfs_file_extent_item *)( | |
4650 | (unsigned long)fi - size_diff); | |
4651 | ||
4652 | if (btrfs_file_extent_type(leaf, fi) == | |
4653 | BTRFS_FILE_EXTENT_INLINE) { | |
4654 | ptr = btrfs_item_ptr_offset(leaf, slot); | |
4655 | memmove_extent_buffer(leaf, ptr, | |
d397712b | 4656 | (unsigned long)fi, |
7ec20afb | 4657 | BTRFS_FILE_EXTENT_INLINE_DATA_START); |
179e29e4 CM |
4658 | } |
4659 | } | |
4660 | ||
3d9ec8c4 NB |
4661 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
4662 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + | |
179e29e4 CM |
4663 | data_end, old_data_start - data_end); |
4664 | ||
4665 | offset = btrfs_disk_key_offset(&disk_key); | |
4666 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); | |
4667 | btrfs_set_item_key(leaf, &disk_key, slot); | |
4668 | if (slot == 0) | |
b167fa91 | 4669 | fixup_low_keys(path, &disk_key, 1); |
179e29e4 | 4670 | } |
5f39d397 | 4671 | |
dd3cc16b | 4672 | item = btrfs_item_nr(slot); |
5f39d397 CM |
4673 | btrfs_set_item_size(leaf, item, new_size); |
4674 | btrfs_mark_buffer_dirty(leaf); | |
b18c6685 | 4675 | |
e902baac | 4676 | if (btrfs_leaf_free_space(leaf) < 0) { |
a4f78750 | 4677 | btrfs_print_leaf(leaf); |
b18c6685 | 4678 | BUG(); |
5f39d397 | 4679 | } |
b18c6685 CM |
4680 | } |
4681 | ||
d352ac68 | 4682 | /* |
8f69dbd2 | 4683 | * make the item pointed to by the path bigger, data_size is the added size. |
d352ac68 | 4684 | */ |
c71dd880 | 4685 | void btrfs_extend_item(struct btrfs_path *path, u32 data_size) |
6567e837 | 4686 | { |
6567e837 | 4687 | int slot; |
5f39d397 CM |
4688 | struct extent_buffer *leaf; |
4689 | struct btrfs_item *item; | |
6567e837 CM |
4690 | u32 nritems; |
4691 | unsigned int data_end; | |
4692 | unsigned int old_data; | |
4693 | unsigned int old_size; | |
4694 | int i; | |
cfed81a0 CM |
4695 | struct btrfs_map_token token; |
4696 | ||
5f39d397 | 4697 | leaf = path->nodes[0]; |
6567e837 | 4698 | |
5f39d397 | 4699 | nritems = btrfs_header_nritems(leaf); |
8f881e8c | 4700 | data_end = leaf_data_end(leaf); |
6567e837 | 4701 | |
e902baac | 4702 | if (btrfs_leaf_free_space(leaf) < data_size) { |
a4f78750 | 4703 | btrfs_print_leaf(leaf); |
6567e837 | 4704 | BUG(); |
5f39d397 | 4705 | } |
6567e837 | 4706 | slot = path->slots[0]; |
5f39d397 | 4707 | old_data = btrfs_item_end_nr(leaf, slot); |
6567e837 CM |
4708 | |
4709 | BUG_ON(slot < 0); | |
3326d1b0 | 4710 | if (slot >= nritems) { |
a4f78750 | 4711 | btrfs_print_leaf(leaf); |
c71dd880 | 4712 | btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d", |
0b246afa | 4713 | slot, nritems); |
290342f6 | 4714 | BUG(); |
3326d1b0 | 4715 | } |
6567e837 CM |
4716 | |
4717 | /* | |
4718 | * item0..itemN ... dataN.offset..dataN.size .. data0.size | |
4719 | */ | |
4720 | /* first correct the data pointers */ | |
c82f823c | 4721 | btrfs_init_map_token(&token, leaf); |
6567e837 | 4722 | for (i = slot; i < nritems; i++) { |
5f39d397 | 4723 | u32 ioff; |
dd3cc16b | 4724 | item = btrfs_item_nr(i); |
db94535d | 4725 | |
cc4c13d5 DS |
4726 | ioff = btrfs_token_item_offset(&token, item); |
4727 | btrfs_set_token_item_offset(&token, item, ioff - data_size); | |
6567e837 | 4728 | } |
5f39d397 | 4729 | |
6567e837 | 4730 | /* shift the data */ |
3d9ec8c4 NB |
4731 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
4732 | data_end - data_size, BTRFS_LEAF_DATA_OFFSET + | |
6567e837 | 4733 | data_end, old_data - data_end); |
5f39d397 | 4734 | |
6567e837 | 4735 | data_end = old_data; |
5f39d397 | 4736 | old_size = btrfs_item_size_nr(leaf, slot); |
dd3cc16b | 4737 | item = btrfs_item_nr(slot); |
5f39d397 CM |
4738 | btrfs_set_item_size(leaf, item, old_size + data_size); |
4739 | btrfs_mark_buffer_dirty(leaf); | |
6567e837 | 4740 | |
e902baac | 4741 | if (btrfs_leaf_free_space(leaf) < 0) { |
a4f78750 | 4742 | btrfs_print_leaf(leaf); |
6567e837 | 4743 | BUG(); |
5f39d397 | 4744 | } |
6567e837 CM |
4745 | } |
4746 | ||
74123bd7 | 4747 | /* |
44871b1b CM |
4748 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
4749 | * to save stack depth by doing the bulk of the work in a function | |
4750 | * that doesn't call btrfs_search_slot | |
74123bd7 | 4751 | */ |
afe5fea7 | 4752 | void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, |
310712b2 | 4753 | const struct btrfs_key *cpu_key, u32 *data_size, |
143bede5 | 4754 | u32 total_data, u32 total_size, int nr) |
be0e5c09 | 4755 | { |
0b246afa | 4756 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 | 4757 | struct btrfs_item *item; |
9c58309d | 4758 | int i; |
7518a238 | 4759 | u32 nritems; |
be0e5c09 | 4760 | unsigned int data_end; |
e2fa7227 | 4761 | struct btrfs_disk_key disk_key; |
44871b1b CM |
4762 | struct extent_buffer *leaf; |
4763 | int slot; | |
cfed81a0 CM |
4764 | struct btrfs_map_token token; |
4765 | ||
24cdc847 FM |
4766 | if (path->slots[0] == 0) { |
4767 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); | |
b167fa91 | 4768 | fixup_low_keys(path, &disk_key, 1); |
24cdc847 FM |
4769 | } |
4770 | btrfs_unlock_up_safe(path, 1); | |
4771 | ||
5f39d397 | 4772 | leaf = path->nodes[0]; |
44871b1b | 4773 | slot = path->slots[0]; |
74123bd7 | 4774 | |
5f39d397 | 4775 | nritems = btrfs_header_nritems(leaf); |
8f881e8c | 4776 | data_end = leaf_data_end(leaf); |
eb60ceac | 4777 | |
e902baac | 4778 | if (btrfs_leaf_free_space(leaf) < total_size) { |
a4f78750 | 4779 | btrfs_print_leaf(leaf); |
0b246afa | 4780 | btrfs_crit(fs_info, "not enough freespace need %u have %d", |
e902baac | 4781 | total_size, btrfs_leaf_free_space(leaf)); |
be0e5c09 | 4782 | BUG(); |
d4dbff95 | 4783 | } |
5f39d397 | 4784 | |
c82f823c | 4785 | btrfs_init_map_token(&token, leaf); |
be0e5c09 | 4786 | if (slot != nritems) { |
5f39d397 | 4787 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
be0e5c09 | 4788 | |
5f39d397 | 4789 | if (old_data < data_end) { |
a4f78750 | 4790 | btrfs_print_leaf(leaf); |
0b246afa | 4791 | btrfs_crit(fs_info, "slot %d old_data %d data_end %d", |
5d163e0e | 4792 | slot, old_data, data_end); |
290342f6 | 4793 | BUG(); |
5f39d397 | 4794 | } |
be0e5c09 CM |
4795 | /* |
4796 | * item0..itemN ... dataN.offset..dataN.size .. data0.size | |
4797 | */ | |
4798 | /* first correct the data pointers */ | |
0783fcfc | 4799 | for (i = slot; i < nritems; i++) { |
5f39d397 | 4800 | u32 ioff; |
db94535d | 4801 | |
62e85577 | 4802 | item = btrfs_item_nr(i); |
cc4c13d5 DS |
4803 | ioff = btrfs_token_item_offset(&token, item); |
4804 | btrfs_set_token_item_offset(&token, item, | |
4805 | ioff - total_data); | |
0783fcfc | 4806 | } |
be0e5c09 | 4807 | /* shift the items */ |
9c58309d | 4808 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
5f39d397 | 4809 | btrfs_item_nr_offset(slot), |
d6025579 | 4810 | (nritems - slot) * sizeof(struct btrfs_item)); |
be0e5c09 CM |
4811 | |
4812 | /* shift the data */ | |
3d9ec8c4 NB |
4813 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
4814 | data_end - total_data, BTRFS_LEAF_DATA_OFFSET + | |
d6025579 | 4815 | data_end, old_data - data_end); |
be0e5c09 CM |
4816 | data_end = old_data; |
4817 | } | |
5f39d397 | 4818 | |
62e2749e | 4819 | /* setup the item for the new data */ |
9c58309d CM |
4820 | for (i = 0; i < nr; i++) { |
4821 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); | |
4822 | btrfs_set_item_key(leaf, &disk_key, slot + i); | |
dd3cc16b | 4823 | item = btrfs_item_nr(slot + i); |
cc4c13d5 | 4824 | btrfs_set_token_item_offset(&token, item, data_end - data_size[i]); |
9c58309d | 4825 | data_end -= data_size[i]; |
cc4c13d5 | 4826 | btrfs_set_token_item_size(&token, item, data_size[i]); |
9c58309d | 4827 | } |
44871b1b | 4828 | |
9c58309d | 4829 | btrfs_set_header_nritems(leaf, nritems + nr); |
b9473439 | 4830 | btrfs_mark_buffer_dirty(leaf); |
aa5d6bed | 4831 | |
e902baac | 4832 | if (btrfs_leaf_free_space(leaf) < 0) { |
a4f78750 | 4833 | btrfs_print_leaf(leaf); |
be0e5c09 | 4834 | BUG(); |
5f39d397 | 4835 | } |
44871b1b CM |
4836 | } |
4837 | ||
4838 | /* | |
4839 | * Given a key and some data, insert items into the tree. | |
4840 | * This does all the path init required, making room in the tree if needed. | |
4841 | */ | |
4842 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, | |
4843 | struct btrfs_root *root, | |
4844 | struct btrfs_path *path, | |
310712b2 | 4845 | const struct btrfs_key *cpu_key, u32 *data_size, |
44871b1b CM |
4846 | int nr) |
4847 | { | |
44871b1b CM |
4848 | int ret = 0; |
4849 | int slot; | |
4850 | int i; | |
4851 | u32 total_size = 0; | |
4852 | u32 total_data = 0; | |
4853 | ||
4854 | for (i = 0; i < nr; i++) | |
4855 | total_data += data_size[i]; | |
4856 | ||
4857 | total_size = total_data + (nr * sizeof(struct btrfs_item)); | |
4858 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); | |
4859 | if (ret == 0) | |
4860 | return -EEXIST; | |
4861 | if (ret < 0) | |
143bede5 | 4862 | return ret; |
44871b1b | 4863 | |
44871b1b CM |
4864 | slot = path->slots[0]; |
4865 | BUG_ON(slot < 0); | |
4866 | ||
afe5fea7 | 4867 | setup_items_for_insert(root, path, cpu_key, data_size, |
44871b1b | 4868 | total_data, total_size, nr); |
143bede5 | 4869 | return 0; |
62e2749e CM |
4870 | } |
4871 | ||
4872 | /* | |
4873 | * Given a key and some data, insert an item into the tree. | |
4874 | * This does all the path init required, making room in the tree if needed. | |
4875 | */ | |
310712b2 OS |
4876 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
4877 | const struct btrfs_key *cpu_key, void *data, | |
4878 | u32 data_size) | |
62e2749e CM |
4879 | { |
4880 | int ret = 0; | |
2c90e5d6 | 4881 | struct btrfs_path *path; |
5f39d397 CM |
4882 | struct extent_buffer *leaf; |
4883 | unsigned long ptr; | |
62e2749e | 4884 | |
2c90e5d6 | 4885 | path = btrfs_alloc_path(); |
db5b493a TI |
4886 | if (!path) |
4887 | return -ENOMEM; | |
2c90e5d6 | 4888 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
62e2749e | 4889 | if (!ret) { |
5f39d397 CM |
4890 | leaf = path->nodes[0]; |
4891 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
4892 | write_extent_buffer(leaf, data, ptr, data_size); | |
4893 | btrfs_mark_buffer_dirty(leaf); | |
62e2749e | 4894 | } |
2c90e5d6 | 4895 | btrfs_free_path(path); |
aa5d6bed | 4896 | return ret; |
be0e5c09 CM |
4897 | } |
4898 | ||
74123bd7 | 4899 | /* |
5de08d7d | 4900 | * delete the pointer from a given node. |
74123bd7 | 4901 | * |
d352ac68 CM |
4902 | * the tree should have been previously balanced so the deletion does not |
4903 | * empty a node. | |
74123bd7 | 4904 | */ |
afe5fea7 TI |
4905 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
4906 | int level, int slot) | |
be0e5c09 | 4907 | { |
5f39d397 | 4908 | struct extent_buffer *parent = path->nodes[level]; |
7518a238 | 4909 | u32 nritems; |
f3ea38da | 4910 | int ret; |
be0e5c09 | 4911 | |
5f39d397 | 4912 | nritems = btrfs_header_nritems(parent); |
d397712b | 4913 | if (slot != nritems - 1) { |
bf1d3425 DS |
4914 | if (level) { |
4915 | ret = tree_mod_log_insert_move(parent, slot, slot + 1, | |
a446a979 | 4916 | nritems - slot - 1); |
bf1d3425 DS |
4917 | BUG_ON(ret < 0); |
4918 | } | |
5f39d397 CM |
4919 | memmove_extent_buffer(parent, |
4920 | btrfs_node_key_ptr_offset(slot), | |
4921 | btrfs_node_key_ptr_offset(slot + 1), | |
d6025579 CM |
4922 | sizeof(struct btrfs_key_ptr) * |
4923 | (nritems - slot - 1)); | |
57ba86c0 | 4924 | } else if (level) { |
e09c2efe DS |
4925 | ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, |
4926 | GFP_NOFS); | |
57ba86c0 | 4927 | BUG_ON(ret < 0); |
bb803951 | 4928 | } |
f3ea38da | 4929 | |
7518a238 | 4930 | nritems--; |
5f39d397 | 4931 | btrfs_set_header_nritems(parent, nritems); |
7518a238 | 4932 | if (nritems == 0 && parent == root->node) { |
5f39d397 | 4933 | BUG_ON(btrfs_header_level(root->node) != 1); |
bb803951 | 4934 | /* just turn the root into a leaf and break */ |
5f39d397 | 4935 | btrfs_set_header_level(root->node, 0); |
bb803951 | 4936 | } else if (slot == 0) { |
5f39d397 CM |
4937 | struct btrfs_disk_key disk_key; |
4938 | ||
4939 | btrfs_node_key(parent, &disk_key, 0); | |
b167fa91 | 4940 | fixup_low_keys(path, &disk_key, level + 1); |
be0e5c09 | 4941 | } |
d6025579 | 4942 | btrfs_mark_buffer_dirty(parent); |
be0e5c09 CM |
4943 | } |
4944 | ||
323ac95b CM |
4945 | /* |
4946 | * a helper function to delete the leaf pointed to by path->slots[1] and | |
5d4f98a2 | 4947 | * path->nodes[1]. |
323ac95b CM |
4948 | * |
4949 | * This deletes the pointer in path->nodes[1] and frees the leaf | |
4950 | * block extent. zero is returned if it all worked out, < 0 otherwise. | |
4951 | * | |
4952 | * The path must have already been setup for deleting the leaf, including | |
4953 | * all the proper balancing. path->nodes[1] must be locked. | |
4954 | */ | |
143bede5 JM |
4955 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
4956 | struct btrfs_root *root, | |
4957 | struct btrfs_path *path, | |
4958 | struct extent_buffer *leaf) | |
323ac95b | 4959 | { |
5d4f98a2 | 4960 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
afe5fea7 | 4961 | del_ptr(root, path, 1, path->slots[1]); |
323ac95b | 4962 | |
4d081c41 CM |
4963 | /* |
4964 | * btrfs_free_extent is expensive, we want to make sure we | |
4965 | * aren't holding any locks when we call it | |
4966 | */ | |
4967 | btrfs_unlock_up_safe(path, 0); | |
4968 | ||
f0486c68 YZ |
4969 | root_sub_used(root, leaf->len); |
4970 | ||
67439dad | 4971 | atomic_inc(&leaf->refs); |
5581a51a | 4972 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
3083ee2e | 4973 | free_extent_buffer_stale(leaf); |
323ac95b | 4974 | } |
74123bd7 CM |
4975 | /* |
4976 | * delete the item at the leaf level in path. If that empties | |
4977 | * the leaf, remove it from the tree | |
4978 | */ | |
85e21bac CM |
4979 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
4980 | struct btrfs_path *path, int slot, int nr) | |
be0e5c09 | 4981 | { |
0b246afa | 4982 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 CM |
4983 | struct extent_buffer *leaf; |
4984 | struct btrfs_item *item; | |
ce0eac2a AM |
4985 | u32 last_off; |
4986 | u32 dsize = 0; | |
aa5d6bed CM |
4987 | int ret = 0; |
4988 | int wret; | |
85e21bac | 4989 | int i; |
7518a238 | 4990 | u32 nritems; |
be0e5c09 | 4991 | |
5f39d397 | 4992 | leaf = path->nodes[0]; |
85e21bac CM |
4993 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
4994 | ||
4995 | for (i = 0; i < nr; i++) | |
4996 | dsize += btrfs_item_size_nr(leaf, slot + i); | |
4997 | ||
5f39d397 | 4998 | nritems = btrfs_header_nritems(leaf); |
be0e5c09 | 4999 | |
85e21bac | 5000 | if (slot + nr != nritems) { |
8f881e8c | 5001 | int data_end = leaf_data_end(leaf); |
c82f823c | 5002 | struct btrfs_map_token token; |
5f39d397 | 5003 | |
3d9ec8c4 | 5004 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
d6025579 | 5005 | data_end + dsize, |
3d9ec8c4 | 5006 | BTRFS_LEAF_DATA_OFFSET + data_end, |
85e21bac | 5007 | last_off - data_end); |
5f39d397 | 5008 | |
c82f823c | 5009 | btrfs_init_map_token(&token, leaf); |
85e21bac | 5010 | for (i = slot + nr; i < nritems; i++) { |
5f39d397 | 5011 | u32 ioff; |
db94535d | 5012 | |
dd3cc16b | 5013 | item = btrfs_item_nr(i); |
cc4c13d5 DS |
5014 | ioff = btrfs_token_item_offset(&token, item); |
5015 | btrfs_set_token_item_offset(&token, item, ioff + dsize); | |
0783fcfc | 5016 | } |
db94535d | 5017 | |
5f39d397 | 5018 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
85e21bac | 5019 | btrfs_item_nr_offset(slot + nr), |
d6025579 | 5020 | sizeof(struct btrfs_item) * |
85e21bac | 5021 | (nritems - slot - nr)); |
be0e5c09 | 5022 | } |
85e21bac CM |
5023 | btrfs_set_header_nritems(leaf, nritems - nr); |
5024 | nritems -= nr; | |
5f39d397 | 5025 | |
74123bd7 | 5026 | /* delete the leaf if we've emptied it */ |
7518a238 | 5027 | if (nritems == 0) { |
5f39d397 CM |
5028 | if (leaf == root->node) { |
5029 | btrfs_set_header_level(leaf, 0); | |
9a8dd150 | 5030 | } else { |
f0486c68 | 5031 | btrfs_set_path_blocking(path); |
6a884d7d | 5032 | btrfs_clean_tree_block(leaf); |
143bede5 | 5033 | btrfs_del_leaf(trans, root, path, leaf); |
9a8dd150 | 5034 | } |
be0e5c09 | 5035 | } else { |
7518a238 | 5036 | int used = leaf_space_used(leaf, 0, nritems); |
aa5d6bed | 5037 | if (slot == 0) { |
5f39d397 CM |
5038 | struct btrfs_disk_key disk_key; |
5039 | ||
5040 | btrfs_item_key(leaf, &disk_key, 0); | |
b167fa91 | 5041 | fixup_low_keys(path, &disk_key, 1); |
aa5d6bed | 5042 | } |
aa5d6bed | 5043 | |
74123bd7 | 5044 | /* delete the leaf if it is mostly empty */ |
0b246afa | 5045 | if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { |
be0e5c09 CM |
5046 | /* push_leaf_left fixes the path. |
5047 | * make sure the path still points to our leaf | |
5048 | * for possible call to del_ptr below | |
5049 | */ | |
4920c9ac | 5050 | slot = path->slots[1]; |
67439dad | 5051 | atomic_inc(&leaf->refs); |
5f39d397 | 5052 | |
b9473439 | 5053 | btrfs_set_path_blocking(path); |
99d8f83c CM |
5054 | wret = push_leaf_left(trans, root, path, 1, 1, |
5055 | 1, (u32)-1); | |
54aa1f4d | 5056 | if (wret < 0 && wret != -ENOSPC) |
aa5d6bed | 5057 | ret = wret; |
5f39d397 CM |
5058 | |
5059 | if (path->nodes[0] == leaf && | |
5060 | btrfs_header_nritems(leaf)) { | |
99d8f83c CM |
5061 | wret = push_leaf_right(trans, root, path, 1, |
5062 | 1, 1, 0); | |
54aa1f4d | 5063 | if (wret < 0 && wret != -ENOSPC) |
aa5d6bed CM |
5064 | ret = wret; |
5065 | } | |
5f39d397 CM |
5066 | |
5067 | if (btrfs_header_nritems(leaf) == 0) { | |
323ac95b | 5068 | path->slots[1] = slot; |
143bede5 | 5069 | btrfs_del_leaf(trans, root, path, leaf); |
5f39d397 | 5070 | free_extent_buffer(leaf); |
143bede5 | 5071 | ret = 0; |
5de08d7d | 5072 | } else { |
925baedd CM |
5073 | /* if we're still in the path, make sure |
5074 | * we're dirty. Otherwise, one of the | |
5075 | * push_leaf functions must have already | |
5076 | * dirtied this buffer | |
5077 | */ | |
5078 | if (path->nodes[0] == leaf) | |
5079 | btrfs_mark_buffer_dirty(leaf); | |
5f39d397 | 5080 | free_extent_buffer(leaf); |
be0e5c09 | 5081 | } |
d5719762 | 5082 | } else { |
5f39d397 | 5083 | btrfs_mark_buffer_dirty(leaf); |
be0e5c09 CM |
5084 | } |
5085 | } | |
aa5d6bed | 5086 | return ret; |
be0e5c09 CM |
5087 | } |
5088 | ||
7bb86316 | 5089 | /* |
925baedd | 5090 | * search the tree again to find a leaf with lesser keys |
7bb86316 CM |
5091 | * returns 0 if it found something or 1 if there are no lesser leaves. |
5092 | * returns < 0 on io errors. | |
d352ac68 CM |
5093 | * |
5094 | * This may release the path, and so you may lose any locks held at the | |
5095 | * time you call it. | |
7bb86316 | 5096 | */ |
16e7549f | 5097 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
7bb86316 | 5098 | { |
925baedd CM |
5099 | struct btrfs_key key; |
5100 | struct btrfs_disk_key found_key; | |
5101 | int ret; | |
7bb86316 | 5102 | |
925baedd | 5103 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
7bb86316 | 5104 | |
e8b0d724 | 5105 | if (key.offset > 0) { |
925baedd | 5106 | key.offset--; |
e8b0d724 | 5107 | } else if (key.type > 0) { |
925baedd | 5108 | key.type--; |
e8b0d724 FDBM |
5109 | key.offset = (u64)-1; |
5110 | } else if (key.objectid > 0) { | |
925baedd | 5111 | key.objectid--; |
e8b0d724 FDBM |
5112 | key.type = (u8)-1; |
5113 | key.offset = (u64)-1; | |
5114 | } else { | |
925baedd | 5115 | return 1; |
e8b0d724 | 5116 | } |
7bb86316 | 5117 | |
b3b4aa74 | 5118 | btrfs_release_path(path); |
925baedd CM |
5119 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5120 | if (ret < 0) | |
5121 | return ret; | |
5122 | btrfs_item_key(path->nodes[0], &found_key, 0); | |
5123 | ret = comp_keys(&found_key, &key); | |
337c6f68 FM |
5124 | /* |
5125 | * We might have had an item with the previous key in the tree right | |
5126 | * before we released our path. And after we released our path, that | |
5127 | * item might have been pushed to the first slot (0) of the leaf we | |
5128 | * were holding due to a tree balance. Alternatively, an item with the | |
5129 | * previous key can exist as the only element of a leaf (big fat item). | |
5130 | * Therefore account for these 2 cases, so that our callers (like | |
5131 | * btrfs_previous_item) don't miss an existing item with a key matching | |
5132 | * the previous key we computed above. | |
5133 | */ | |
5134 | if (ret <= 0) | |
925baedd CM |
5135 | return 0; |
5136 | return 1; | |
7bb86316 CM |
5137 | } |
5138 | ||
3f157a2f CM |
5139 | /* |
5140 | * A helper function to walk down the tree starting at min_key, and looking | |
de78b51a ES |
5141 | * for nodes or leaves that are have a minimum transaction id. |
5142 | * This is used by the btree defrag code, and tree logging | |
3f157a2f CM |
5143 | * |
5144 | * This does not cow, but it does stuff the starting key it finds back | |
5145 | * into min_key, so you can call btrfs_search_slot with cow=1 on the | |
5146 | * key and get a writable path. | |
5147 | * | |
3f157a2f CM |
5148 | * This honors path->lowest_level to prevent descent past a given level |
5149 | * of the tree. | |
5150 | * | |
d352ac68 CM |
5151 | * min_trans indicates the oldest transaction that you are interested |
5152 | * in walking through. Any nodes or leaves older than min_trans are | |
5153 | * skipped over (without reading them). | |
5154 | * | |
3f157a2f CM |
5155 | * returns zero if something useful was found, < 0 on error and 1 if there |
5156 | * was nothing in the tree that matched the search criteria. | |
5157 | */ | |
5158 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, | |
de78b51a | 5159 | struct btrfs_path *path, |
3f157a2f CM |
5160 | u64 min_trans) |
5161 | { | |
5162 | struct extent_buffer *cur; | |
5163 | struct btrfs_key found_key; | |
5164 | int slot; | |
9652480b | 5165 | int sret; |
3f157a2f CM |
5166 | u32 nritems; |
5167 | int level; | |
5168 | int ret = 1; | |
f98de9b9 | 5169 | int keep_locks = path->keep_locks; |
3f157a2f | 5170 | |
f98de9b9 | 5171 | path->keep_locks = 1; |
3f157a2f | 5172 | again: |
bd681513 | 5173 | cur = btrfs_read_lock_root_node(root); |
3f157a2f | 5174 | level = btrfs_header_level(cur); |
e02119d5 | 5175 | WARN_ON(path->nodes[level]); |
3f157a2f | 5176 | path->nodes[level] = cur; |
bd681513 | 5177 | path->locks[level] = BTRFS_READ_LOCK; |
3f157a2f CM |
5178 | |
5179 | if (btrfs_header_generation(cur) < min_trans) { | |
5180 | ret = 1; | |
5181 | goto out; | |
5182 | } | |
d397712b | 5183 | while (1) { |
3f157a2f CM |
5184 | nritems = btrfs_header_nritems(cur); |
5185 | level = btrfs_header_level(cur); | |
e3b83361 | 5186 | sret = btrfs_bin_search(cur, min_key, &slot); |
cbca7d59 FM |
5187 | if (sret < 0) { |
5188 | ret = sret; | |
5189 | goto out; | |
5190 | } | |
3f157a2f | 5191 | |
323ac95b CM |
5192 | /* at the lowest level, we're done, setup the path and exit */ |
5193 | if (level == path->lowest_level) { | |
e02119d5 CM |
5194 | if (slot >= nritems) |
5195 | goto find_next_key; | |
3f157a2f CM |
5196 | ret = 0; |
5197 | path->slots[level] = slot; | |
5198 | btrfs_item_key_to_cpu(cur, &found_key, slot); | |
5199 | goto out; | |
5200 | } | |
9652480b Y |
5201 | if (sret && slot > 0) |
5202 | slot--; | |
3f157a2f | 5203 | /* |
de78b51a | 5204 | * check this node pointer against the min_trans parameters. |
260db43c | 5205 | * If it is too old, skip to the next one. |
3f157a2f | 5206 | */ |
d397712b | 5207 | while (slot < nritems) { |
3f157a2f | 5208 | u64 gen; |
e02119d5 | 5209 | |
3f157a2f CM |
5210 | gen = btrfs_node_ptr_generation(cur, slot); |
5211 | if (gen < min_trans) { | |
5212 | slot++; | |
5213 | continue; | |
5214 | } | |
de78b51a | 5215 | break; |
3f157a2f | 5216 | } |
e02119d5 | 5217 | find_next_key: |
3f157a2f CM |
5218 | /* |
5219 | * we didn't find a candidate key in this node, walk forward | |
5220 | * and find another one | |
5221 | */ | |
5222 | if (slot >= nritems) { | |
e02119d5 | 5223 | path->slots[level] = slot; |
b4ce94de | 5224 | btrfs_set_path_blocking(path); |
e02119d5 | 5225 | sret = btrfs_find_next_key(root, path, min_key, level, |
de78b51a | 5226 | min_trans); |
e02119d5 | 5227 | if (sret == 0) { |
b3b4aa74 | 5228 | btrfs_release_path(path); |
3f157a2f CM |
5229 | goto again; |
5230 | } else { | |
5231 | goto out; | |
5232 | } | |
5233 | } | |
5234 | /* save our key for returning back */ | |
5235 | btrfs_node_key_to_cpu(cur, &found_key, slot); | |
5236 | path->slots[level] = slot; | |
5237 | if (level == path->lowest_level) { | |
5238 | ret = 0; | |
3f157a2f CM |
5239 | goto out; |
5240 | } | |
b4ce94de | 5241 | btrfs_set_path_blocking(path); |
4b231ae4 | 5242 | cur = btrfs_read_node_slot(cur, slot); |
fb770ae4 LB |
5243 | if (IS_ERR(cur)) { |
5244 | ret = PTR_ERR(cur); | |
5245 | goto out; | |
5246 | } | |
3f157a2f | 5247 | |
bd681513 | 5248 | btrfs_tree_read_lock(cur); |
b4ce94de | 5249 | |
bd681513 | 5250 | path->locks[level - 1] = BTRFS_READ_LOCK; |
3f157a2f | 5251 | path->nodes[level - 1] = cur; |
f7c79f30 | 5252 | unlock_up(path, level, 1, 0, NULL); |
3f157a2f CM |
5253 | } |
5254 | out: | |
f98de9b9 FM |
5255 | path->keep_locks = keep_locks; |
5256 | if (ret == 0) { | |
5257 | btrfs_unlock_up_safe(path, path->lowest_level + 1); | |
5258 | btrfs_set_path_blocking(path); | |
3f157a2f | 5259 | memcpy(min_key, &found_key, sizeof(found_key)); |
f98de9b9 | 5260 | } |
3f157a2f CM |
5261 | return ret; |
5262 | } | |
5263 | ||
5264 | /* | |
5265 | * this is similar to btrfs_next_leaf, but does not try to preserve | |
5266 | * and fixup the path. It looks for and returns the next key in the | |
de78b51a | 5267 | * tree based on the current path and the min_trans parameters. |
3f157a2f CM |
5268 | * |
5269 | * 0 is returned if another key is found, < 0 if there are any errors | |
5270 | * and 1 is returned if there are no higher keys in the tree | |
5271 | * | |
5272 | * path->keep_locks should be set to 1 on the search made before | |
5273 | * calling this function. | |
5274 | */ | |
e7a84565 | 5275 | int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, |
de78b51a | 5276 | struct btrfs_key *key, int level, u64 min_trans) |
e7a84565 | 5277 | { |
e7a84565 CM |
5278 | int slot; |
5279 | struct extent_buffer *c; | |
5280 | ||
6a9fb468 | 5281 | WARN_ON(!path->keep_locks && !path->skip_locking); |
d397712b | 5282 | while (level < BTRFS_MAX_LEVEL) { |
e7a84565 CM |
5283 | if (!path->nodes[level]) |
5284 | return 1; | |
5285 | ||
5286 | slot = path->slots[level] + 1; | |
5287 | c = path->nodes[level]; | |
3f157a2f | 5288 | next: |
e7a84565 | 5289 | if (slot >= btrfs_header_nritems(c)) { |
33c66f43 YZ |
5290 | int ret; |
5291 | int orig_lowest; | |
5292 | struct btrfs_key cur_key; | |
5293 | if (level + 1 >= BTRFS_MAX_LEVEL || | |
5294 | !path->nodes[level + 1]) | |
e7a84565 | 5295 | return 1; |
33c66f43 | 5296 | |
6a9fb468 | 5297 | if (path->locks[level + 1] || path->skip_locking) { |
33c66f43 YZ |
5298 | level++; |
5299 | continue; | |
5300 | } | |
5301 | ||
5302 | slot = btrfs_header_nritems(c) - 1; | |
5303 | if (level == 0) | |
5304 | btrfs_item_key_to_cpu(c, &cur_key, slot); | |
5305 | else | |
5306 | btrfs_node_key_to_cpu(c, &cur_key, slot); | |
5307 | ||
5308 | orig_lowest = path->lowest_level; | |
b3b4aa74 | 5309 | btrfs_release_path(path); |
33c66f43 YZ |
5310 | path->lowest_level = level; |
5311 | ret = btrfs_search_slot(NULL, root, &cur_key, path, | |
5312 | 0, 0); | |
5313 | path->lowest_level = orig_lowest; | |
5314 | if (ret < 0) | |
5315 | return ret; | |
5316 | ||
5317 | c = path->nodes[level]; | |
5318 | slot = path->slots[level]; | |
5319 | if (ret == 0) | |
5320 | slot++; | |
5321 | goto next; | |
e7a84565 | 5322 | } |
33c66f43 | 5323 | |
e7a84565 CM |
5324 | if (level == 0) |
5325 | btrfs_item_key_to_cpu(c, key, slot); | |
3f157a2f | 5326 | else { |
3f157a2f CM |
5327 | u64 gen = btrfs_node_ptr_generation(c, slot); |
5328 | ||
3f157a2f CM |
5329 | if (gen < min_trans) { |
5330 | slot++; | |
5331 | goto next; | |
5332 | } | |
e7a84565 | 5333 | btrfs_node_key_to_cpu(c, key, slot); |
3f157a2f | 5334 | } |
e7a84565 CM |
5335 | return 0; |
5336 | } | |
5337 | return 1; | |
5338 | } | |
5339 | ||
97571fd0 | 5340 | /* |
925baedd | 5341 | * search the tree again to find a leaf with greater keys |
0f70abe2 CM |
5342 | * returns 0 if it found something or 1 if there are no greater leaves. |
5343 | * returns < 0 on io errors. | |
97571fd0 | 5344 | */ |
234b63a0 | 5345 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
3d7806ec JS |
5346 | { |
5347 | return btrfs_next_old_leaf(root, path, 0); | |
5348 | } | |
5349 | ||
5350 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, | |
5351 | u64 time_seq) | |
d97e63b6 CM |
5352 | { |
5353 | int slot; | |
8e73f275 | 5354 | int level; |
5f39d397 | 5355 | struct extent_buffer *c; |
8e73f275 | 5356 | struct extent_buffer *next; |
925baedd CM |
5357 | struct btrfs_key key; |
5358 | u32 nritems; | |
5359 | int ret; | |
8e73f275 | 5360 | int old_spinning = path->leave_spinning; |
bd681513 | 5361 | int next_rw_lock = 0; |
925baedd CM |
5362 | |
5363 | nritems = btrfs_header_nritems(path->nodes[0]); | |
d397712b | 5364 | if (nritems == 0) |
925baedd | 5365 | return 1; |
925baedd | 5366 | |
8e73f275 CM |
5367 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
5368 | again: | |
5369 | level = 1; | |
5370 | next = NULL; | |
bd681513 | 5371 | next_rw_lock = 0; |
b3b4aa74 | 5372 | btrfs_release_path(path); |
8e73f275 | 5373 | |
a2135011 | 5374 | path->keep_locks = 1; |
31533fb2 | 5375 | path->leave_spinning = 1; |
8e73f275 | 5376 | |
3d7806ec JS |
5377 | if (time_seq) |
5378 | ret = btrfs_search_old_slot(root, &key, path, time_seq); | |
5379 | else | |
5380 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
925baedd CM |
5381 | path->keep_locks = 0; |
5382 | ||
5383 | if (ret < 0) | |
5384 | return ret; | |
5385 | ||
a2135011 | 5386 | nritems = btrfs_header_nritems(path->nodes[0]); |
168fd7d2 CM |
5387 | /* |
5388 | * by releasing the path above we dropped all our locks. A balance | |
5389 | * could have added more items next to the key that used to be | |
5390 | * at the very end of the block. So, check again here and | |
5391 | * advance the path if there are now more items available. | |
5392 | */ | |
a2135011 | 5393 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
e457afec YZ |
5394 | if (ret == 0) |
5395 | path->slots[0]++; | |
8e73f275 | 5396 | ret = 0; |
925baedd CM |
5397 | goto done; |
5398 | } | |
0b43e04f LB |
5399 | /* |
5400 | * So the above check misses one case: | |
5401 | * - after releasing the path above, someone has removed the item that | |
5402 | * used to be at the very end of the block, and balance between leafs | |
5403 | * gets another one with bigger key.offset to replace it. | |
5404 | * | |
5405 | * This one should be returned as well, or we can get leaf corruption | |
5406 | * later(esp. in __btrfs_drop_extents()). | |
5407 | * | |
5408 | * And a bit more explanation about this check, | |
5409 | * with ret > 0, the key isn't found, the path points to the slot | |
5410 | * where it should be inserted, so the path->slots[0] item must be the | |
5411 | * bigger one. | |
5412 | */ | |
5413 | if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { | |
5414 | ret = 0; | |
5415 | goto done; | |
5416 | } | |
d97e63b6 | 5417 | |
d397712b | 5418 | while (level < BTRFS_MAX_LEVEL) { |
8e73f275 CM |
5419 | if (!path->nodes[level]) { |
5420 | ret = 1; | |
5421 | goto done; | |
5422 | } | |
5f39d397 | 5423 | |
d97e63b6 CM |
5424 | slot = path->slots[level] + 1; |
5425 | c = path->nodes[level]; | |
5f39d397 | 5426 | if (slot >= btrfs_header_nritems(c)) { |
d97e63b6 | 5427 | level++; |
8e73f275 CM |
5428 | if (level == BTRFS_MAX_LEVEL) { |
5429 | ret = 1; | |
5430 | goto done; | |
5431 | } | |
d97e63b6 CM |
5432 | continue; |
5433 | } | |
5f39d397 | 5434 | |
925baedd | 5435 | if (next) { |
bd681513 | 5436 | btrfs_tree_unlock_rw(next, next_rw_lock); |
5f39d397 | 5437 | free_extent_buffer(next); |
925baedd | 5438 | } |
5f39d397 | 5439 | |
8e73f275 | 5440 | next = c; |
bd681513 | 5441 | next_rw_lock = path->locks[level]; |
d07b8528 | 5442 | ret = read_block_for_search(root, path, &next, level, |
cda79c54 | 5443 | slot, &key); |
8e73f275 CM |
5444 | if (ret == -EAGAIN) |
5445 | goto again; | |
5f39d397 | 5446 | |
76a05b35 | 5447 | if (ret < 0) { |
b3b4aa74 | 5448 | btrfs_release_path(path); |
76a05b35 CM |
5449 | goto done; |
5450 | } | |
5451 | ||
5cd57b2c | 5452 | if (!path->skip_locking) { |
bd681513 | 5453 | ret = btrfs_try_tree_read_lock(next); |
d42244a0 JS |
5454 | if (!ret && time_seq) { |
5455 | /* | |
5456 | * If we don't get the lock, we may be racing | |
5457 | * with push_leaf_left, holding that lock while | |
5458 | * itself waiting for the leaf we've currently | |
5459 | * locked. To solve this situation, we give up | |
5460 | * on our lock and cycle. | |
5461 | */ | |
cf538830 | 5462 | free_extent_buffer(next); |
d42244a0 JS |
5463 | btrfs_release_path(path); |
5464 | cond_resched(); | |
5465 | goto again; | |
5466 | } | |
8e73f275 CM |
5467 | if (!ret) { |
5468 | btrfs_set_path_blocking(path); | |
fd7ba1c1 JB |
5469 | __btrfs_tree_read_lock(next, |
5470 | BTRFS_NESTING_NORMAL, | |
5471 | path->recurse); | |
8e73f275 | 5472 | } |
31533fb2 | 5473 | next_rw_lock = BTRFS_READ_LOCK; |
5cd57b2c | 5474 | } |
d97e63b6 CM |
5475 | break; |
5476 | } | |
5477 | path->slots[level] = slot; | |
d397712b | 5478 | while (1) { |
d97e63b6 CM |
5479 | level--; |
5480 | c = path->nodes[level]; | |
925baedd | 5481 | if (path->locks[level]) |
bd681513 | 5482 | btrfs_tree_unlock_rw(c, path->locks[level]); |
8e73f275 | 5483 | |
5f39d397 | 5484 | free_extent_buffer(c); |
d97e63b6 CM |
5485 | path->nodes[level] = next; |
5486 | path->slots[level] = 0; | |
a74a4b97 | 5487 | if (!path->skip_locking) |
bd681513 | 5488 | path->locks[level] = next_rw_lock; |
d97e63b6 CM |
5489 | if (!level) |
5490 | break; | |
b4ce94de | 5491 | |
d07b8528 | 5492 | ret = read_block_for_search(root, path, &next, level, |
cda79c54 | 5493 | 0, &key); |
8e73f275 CM |
5494 | if (ret == -EAGAIN) |
5495 | goto again; | |
5496 | ||
76a05b35 | 5497 | if (ret < 0) { |
b3b4aa74 | 5498 | btrfs_release_path(path); |
76a05b35 CM |
5499 | goto done; |
5500 | } | |
5501 | ||
5cd57b2c | 5502 | if (!path->skip_locking) { |
bd681513 | 5503 | ret = btrfs_try_tree_read_lock(next); |
8e73f275 CM |
5504 | if (!ret) { |
5505 | btrfs_set_path_blocking(path); | |
fd7ba1c1 JB |
5506 | __btrfs_tree_read_lock(next, |
5507 | BTRFS_NESTING_NORMAL, | |
5508 | path->recurse); | |
bd681513 | 5509 | } |
31533fb2 | 5510 | next_rw_lock = BTRFS_READ_LOCK; |
5cd57b2c | 5511 | } |
d97e63b6 | 5512 | } |
8e73f275 | 5513 | ret = 0; |
925baedd | 5514 | done: |
f7c79f30 | 5515 | unlock_up(path, 0, 1, 0, NULL); |
8e73f275 CM |
5516 | path->leave_spinning = old_spinning; |
5517 | if (!old_spinning) | |
5518 | btrfs_set_path_blocking(path); | |
5519 | ||
5520 | return ret; | |
d97e63b6 | 5521 | } |
0b86a832 | 5522 | |
3f157a2f CM |
5523 | /* |
5524 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps | |
5525 | * searching until it gets past min_objectid or finds an item of 'type' | |
5526 | * | |
5527 | * returns 0 if something is found, 1 if nothing was found and < 0 on error | |
5528 | */ | |
0b86a832 CM |
5529 | int btrfs_previous_item(struct btrfs_root *root, |
5530 | struct btrfs_path *path, u64 min_objectid, | |
5531 | int type) | |
5532 | { | |
5533 | struct btrfs_key found_key; | |
5534 | struct extent_buffer *leaf; | |
e02119d5 | 5535 | u32 nritems; |
0b86a832 CM |
5536 | int ret; |
5537 | ||
d397712b | 5538 | while (1) { |
0b86a832 | 5539 | if (path->slots[0] == 0) { |
b4ce94de | 5540 | btrfs_set_path_blocking(path); |
0b86a832 CM |
5541 | ret = btrfs_prev_leaf(root, path); |
5542 | if (ret != 0) | |
5543 | return ret; | |
5544 | } else { | |
5545 | path->slots[0]--; | |
5546 | } | |
5547 | leaf = path->nodes[0]; | |
e02119d5 CM |
5548 | nritems = btrfs_header_nritems(leaf); |
5549 | if (nritems == 0) | |
5550 | return 1; | |
5551 | if (path->slots[0] == nritems) | |
5552 | path->slots[0]--; | |
5553 | ||
0b86a832 | 5554 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
e02119d5 CM |
5555 | if (found_key.objectid < min_objectid) |
5556 | break; | |
0a4eefbb YZ |
5557 | if (found_key.type == type) |
5558 | return 0; | |
e02119d5 CM |
5559 | if (found_key.objectid == min_objectid && |
5560 | found_key.type < type) | |
5561 | break; | |
0b86a832 CM |
5562 | } |
5563 | return 1; | |
5564 | } | |
ade2e0b3 WS |
5565 | |
5566 | /* | |
5567 | * search in extent tree to find a previous Metadata/Data extent item with | |
5568 | * min objecitd. | |
5569 | * | |
5570 | * returns 0 if something is found, 1 if nothing was found and < 0 on error | |
5571 | */ | |
5572 | int btrfs_previous_extent_item(struct btrfs_root *root, | |
5573 | struct btrfs_path *path, u64 min_objectid) | |
5574 | { | |
5575 | struct btrfs_key found_key; | |
5576 | struct extent_buffer *leaf; | |
5577 | u32 nritems; | |
5578 | int ret; | |
5579 | ||
5580 | while (1) { | |
5581 | if (path->slots[0] == 0) { | |
5582 | btrfs_set_path_blocking(path); | |
5583 | ret = btrfs_prev_leaf(root, path); | |
5584 | if (ret != 0) | |
5585 | return ret; | |
5586 | } else { | |
5587 | path->slots[0]--; | |
5588 | } | |
5589 | leaf = path->nodes[0]; | |
5590 | nritems = btrfs_header_nritems(leaf); | |
5591 | if (nritems == 0) | |
5592 | return 1; | |
5593 | if (path->slots[0] == nritems) | |
5594 | path->slots[0]--; | |
5595 | ||
5596 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5597 | if (found_key.objectid < min_objectid) | |
5598 | break; | |
5599 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY || | |
5600 | found_key.type == BTRFS_METADATA_ITEM_KEY) | |
5601 | return 0; | |
5602 | if (found_key.objectid == min_objectid && | |
5603 | found_key.type < BTRFS_EXTENT_ITEM_KEY) | |
5604 | break; | |
5605 | } | |
5606 | return 1; | |
5607 | } |