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