Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 | 4 | */ |
c1d7c514 | 5 | |
ec6b910f | 6 | #include <linux/sched.h> |
f361bf4a | 7 | #include <linux/sched/signal.h> |
edbd8d4e | 8 | #include <linux/pagemap.h> |
ec44a35c | 9 | #include <linux/writeback.h> |
21af804c | 10 | #include <linux/blkdev.h> |
b7a9f29f | 11 | #include <linux/sort.h> |
4184ea7f | 12 | #include <linux/rcupdate.h> |
817d52f8 | 13 | #include <linux/kthread.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
dff51cd1 | 15 | #include <linux/ratelimit.h> |
b150a4f1 | 16 | #include <linux/percpu_counter.h> |
69fe2d75 | 17 | #include <linux/lockdep.h> |
9678c543 | 18 | #include <linux/crc32c.h> |
602cbe91 | 19 | #include "misc.h" |
995946dd | 20 | #include "tree-log.h" |
fec577fb CM |
21 | #include "disk-io.h" |
22 | #include "print-tree.h" | |
0b86a832 | 23 | #include "volumes.h" |
53b381b3 | 24 | #include "raid56.h" |
925baedd | 25 | #include "locking.h" |
fa9c0d79 | 26 | #include "free-space-cache.h" |
1e144fb8 | 27 | #include "free-space-tree.h" |
6ab0a202 | 28 | #include "sysfs.h" |
fcebe456 | 29 | #include "qgroup.h" |
fd708b81 | 30 | #include "ref-verify.h" |
8719aaae | 31 | #include "space-info.h" |
d12ffdd1 | 32 | #include "block-rsv.h" |
86736342 | 33 | #include "delalloc-space.h" |
aac0023c | 34 | #include "block-group.h" |
b0643e59 | 35 | #include "discard.h" |
c57dd1f2 | 36 | #include "rcu-string.h" |
169e0da9 | 37 | #include "zoned.h" |
6143c23c | 38 | #include "dev-replace.h" |
fec577fb | 39 | |
709c0486 AJ |
40 | #undef SCRAMBLE_DELAYED_REFS |
41 | ||
9f9b8e8d | 42 | |
5d4f98a2 | 43 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
e72cb923 NB |
44 | struct btrfs_delayed_ref_node *node, u64 parent, |
45 | u64 root_objectid, u64 owner_objectid, | |
46 | u64 owner_offset, int refs_to_drop, | |
47 | struct btrfs_delayed_extent_op *extra_op); | |
5d4f98a2 YZ |
48 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
49 | struct extent_buffer *leaf, | |
50 | struct btrfs_extent_item *ei); | |
51 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
5d4f98a2 YZ |
52 | u64 parent, u64 root_objectid, |
53 | u64 flags, u64 owner, u64 offset, | |
54 | struct btrfs_key *ins, int ref_mod); | |
55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, | |
4e6bd4e0 | 56 | struct btrfs_delayed_ref_node *node, |
21ebfbe7 | 57 | struct btrfs_delayed_extent_op *extent_op); |
11833d66 YZ |
58 | static int find_next_key(struct btrfs_path *path, int level, |
59 | struct btrfs_key *key); | |
6a63209f | 60 | |
32da5386 | 61 | static int block_group_bits(struct btrfs_block_group *cache, u64 bits) |
0f9dd46c JB |
62 | { |
63 | return (cache->flags & bits) == bits; | |
64 | } | |
65 | ||
6f410d1b JB |
66 | int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info, |
67 | u64 start, u64 num_bytes) | |
817d52f8 | 68 | { |
11833d66 | 69 | u64 end = start + num_bytes - 1; |
fe119a6e NB |
70 | set_extent_bits(&fs_info->excluded_extents, start, end, |
71 | EXTENT_UPTODATE); | |
11833d66 YZ |
72 | return 0; |
73 | } | |
817d52f8 | 74 | |
32da5386 | 75 | void btrfs_free_excluded_extents(struct btrfs_block_group *cache) |
11833d66 | 76 | { |
9e715da8 | 77 | struct btrfs_fs_info *fs_info = cache->fs_info; |
11833d66 | 78 | u64 start, end; |
817d52f8 | 79 | |
b3470b5d DS |
80 | start = cache->start; |
81 | end = start + cache->length - 1; | |
11833d66 | 82 | |
fe119a6e NB |
83 | clear_extent_bits(&fs_info->excluded_extents, start, end, |
84 | EXTENT_UPTODATE); | |
817d52f8 JB |
85 | } |
86 | ||
1a4ed8fd | 87 | /* simple helper to search for an existing data extent at a given offset */ |
2ff7e61e | 88 | int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len) |
e02119d5 CM |
89 | { |
90 | int ret; | |
91 | struct btrfs_key key; | |
31840ae1 | 92 | struct btrfs_path *path; |
e02119d5 | 93 | |
31840ae1 | 94 | path = btrfs_alloc_path(); |
d8926bb3 MF |
95 | if (!path) |
96 | return -ENOMEM; | |
97 | ||
e02119d5 CM |
98 | key.objectid = start; |
99 | key.offset = len; | |
3173a18f | 100 | key.type = BTRFS_EXTENT_ITEM_KEY; |
0b246afa | 101 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
31840ae1 | 102 | btrfs_free_path(path); |
7bb86316 CM |
103 | return ret; |
104 | } | |
105 | ||
a22285a6 | 106 | /* |
3173a18f | 107 | * helper function to lookup reference count and flags of a tree block. |
a22285a6 YZ |
108 | * |
109 | * the head node for delayed ref is used to store the sum of all the | |
110 | * reference count modifications queued up in the rbtree. the head | |
111 | * node may also store the extent flags to set. This way you can check | |
112 | * to see what the reference count and extent flags would be if all of | |
113 | * the delayed refs are not processed. | |
114 | */ | |
115 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, | |
2ff7e61e | 116 | struct btrfs_fs_info *fs_info, u64 bytenr, |
3173a18f | 117 | u64 offset, int metadata, u64 *refs, u64 *flags) |
a22285a6 YZ |
118 | { |
119 | struct btrfs_delayed_ref_head *head; | |
120 | struct btrfs_delayed_ref_root *delayed_refs; | |
121 | struct btrfs_path *path; | |
122 | struct btrfs_extent_item *ei; | |
123 | struct extent_buffer *leaf; | |
124 | struct btrfs_key key; | |
125 | u32 item_size; | |
126 | u64 num_refs; | |
127 | u64 extent_flags; | |
128 | int ret; | |
129 | ||
3173a18f JB |
130 | /* |
131 | * If we don't have skinny metadata, don't bother doing anything | |
132 | * different | |
133 | */ | |
0b246afa JM |
134 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
135 | offset = fs_info->nodesize; | |
3173a18f JB |
136 | metadata = 0; |
137 | } | |
138 | ||
a22285a6 YZ |
139 | path = btrfs_alloc_path(); |
140 | if (!path) | |
141 | return -ENOMEM; | |
142 | ||
a22285a6 YZ |
143 | if (!trans) { |
144 | path->skip_locking = 1; | |
145 | path->search_commit_root = 1; | |
146 | } | |
639eefc8 FDBM |
147 | |
148 | search_again: | |
149 | key.objectid = bytenr; | |
150 | key.offset = offset; | |
151 | if (metadata) | |
152 | key.type = BTRFS_METADATA_ITEM_KEY; | |
153 | else | |
154 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
155 | ||
6534c0c9 | 156 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
a22285a6 YZ |
157 | if (ret < 0) |
158 | goto out_free; | |
159 | ||
3173a18f | 160 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
74be9510 FDBM |
161 | if (path->slots[0]) { |
162 | path->slots[0]--; | |
163 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
164 | path->slots[0]); | |
165 | if (key.objectid == bytenr && | |
166 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
0b246afa | 167 | key.offset == fs_info->nodesize) |
74be9510 FDBM |
168 | ret = 0; |
169 | } | |
3173a18f JB |
170 | } |
171 | ||
a22285a6 YZ |
172 | if (ret == 0) { |
173 | leaf = path->nodes[0]; | |
174 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
175 | if (item_size >= sizeof(*ei)) { | |
176 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
177 | struct btrfs_extent_item); | |
178 | num_refs = btrfs_extent_refs(leaf, ei); | |
179 | extent_flags = btrfs_extent_flags(leaf, ei); | |
180 | } else { | |
ba3c2b19 NB |
181 | ret = -EINVAL; |
182 | btrfs_print_v0_err(fs_info); | |
183 | if (trans) | |
184 | btrfs_abort_transaction(trans, ret); | |
185 | else | |
186 | btrfs_handle_fs_error(fs_info, ret, NULL); | |
187 | ||
188 | goto out_free; | |
a22285a6 | 189 | } |
ba3c2b19 | 190 | |
a22285a6 YZ |
191 | BUG_ON(num_refs == 0); |
192 | } else { | |
193 | num_refs = 0; | |
194 | extent_flags = 0; | |
195 | ret = 0; | |
196 | } | |
197 | ||
198 | if (!trans) | |
199 | goto out; | |
200 | ||
201 | delayed_refs = &trans->transaction->delayed_refs; | |
202 | spin_lock(&delayed_refs->lock); | |
f72ad18e | 203 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
a22285a6 YZ |
204 | if (head) { |
205 | if (!mutex_trylock(&head->mutex)) { | |
d278850e | 206 | refcount_inc(&head->refs); |
a22285a6 YZ |
207 | spin_unlock(&delayed_refs->lock); |
208 | ||
b3b4aa74 | 209 | btrfs_release_path(path); |
a22285a6 | 210 | |
8cc33e5c DS |
211 | /* |
212 | * Mutex was contended, block until it's released and try | |
213 | * again | |
214 | */ | |
a22285a6 YZ |
215 | mutex_lock(&head->mutex); |
216 | mutex_unlock(&head->mutex); | |
d278850e | 217 | btrfs_put_delayed_ref_head(head); |
639eefc8 | 218 | goto search_again; |
a22285a6 | 219 | } |
d7df2c79 | 220 | spin_lock(&head->lock); |
a22285a6 YZ |
221 | if (head->extent_op && head->extent_op->update_flags) |
222 | extent_flags |= head->extent_op->flags_to_set; | |
223 | else | |
224 | BUG_ON(num_refs == 0); | |
225 | ||
d278850e | 226 | num_refs += head->ref_mod; |
d7df2c79 | 227 | spin_unlock(&head->lock); |
a22285a6 YZ |
228 | mutex_unlock(&head->mutex); |
229 | } | |
230 | spin_unlock(&delayed_refs->lock); | |
231 | out: | |
232 | WARN_ON(num_refs == 0); | |
233 | if (refs) | |
234 | *refs = num_refs; | |
235 | if (flags) | |
236 | *flags = extent_flags; | |
237 | out_free: | |
238 | btrfs_free_path(path); | |
239 | return ret; | |
240 | } | |
241 | ||
d8d5f3e1 CM |
242 | /* |
243 | * Back reference rules. Back refs have three main goals: | |
244 | * | |
245 | * 1) differentiate between all holders of references to an extent so that | |
246 | * when a reference is dropped we can make sure it was a valid reference | |
247 | * before freeing the extent. | |
248 | * | |
249 | * 2) Provide enough information to quickly find the holders of an extent | |
250 | * if we notice a given block is corrupted or bad. | |
251 | * | |
252 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
253 | * maintenance. This is actually the same as #2, but with a slightly | |
254 | * different use case. | |
255 | * | |
5d4f98a2 YZ |
256 | * There are two kinds of back refs. The implicit back refs is optimized |
257 | * for pointers in non-shared tree blocks. For a given pointer in a block, | |
258 | * back refs of this kind provide information about the block's owner tree | |
259 | * and the pointer's key. These information allow us to find the block by | |
260 | * b-tree searching. The full back refs is for pointers in tree blocks not | |
261 | * referenced by their owner trees. The location of tree block is recorded | |
262 | * in the back refs. Actually the full back refs is generic, and can be | |
263 | * used in all cases the implicit back refs is used. The major shortcoming | |
264 | * of the full back refs is its overhead. Every time a tree block gets | |
265 | * COWed, we have to update back refs entry for all pointers in it. | |
266 | * | |
267 | * For a newly allocated tree block, we use implicit back refs for | |
268 | * pointers in it. This means most tree related operations only involve | |
269 | * implicit back refs. For a tree block created in old transaction, the | |
270 | * only way to drop a reference to it is COW it. So we can detect the | |
271 | * event that tree block loses its owner tree's reference and do the | |
272 | * back refs conversion. | |
273 | * | |
01327610 | 274 | * When a tree block is COWed through a tree, there are four cases: |
5d4f98a2 YZ |
275 | * |
276 | * The reference count of the block is one and the tree is the block's | |
277 | * owner tree. Nothing to do in this case. | |
278 | * | |
279 | * The reference count of the block is one and the tree is not the | |
280 | * block's owner tree. In this case, full back refs is used for pointers | |
281 | * in the block. Remove these full back refs, add implicit back refs for | |
282 | * every pointers in the new block. | |
283 | * | |
284 | * The reference count of the block is greater than one and the tree is | |
285 | * the block's owner tree. In this case, implicit back refs is used for | |
286 | * pointers in the block. Add full back refs for every pointers in the | |
287 | * block, increase lower level extents' reference counts. The original | |
288 | * implicit back refs are entailed to the new block. | |
289 | * | |
290 | * The reference count of the block is greater than one and the tree is | |
291 | * not the block's owner tree. Add implicit back refs for every pointer in | |
292 | * the new block, increase lower level extents' reference count. | |
293 | * | |
294 | * Back Reference Key composing: | |
295 | * | |
296 | * The key objectid corresponds to the first byte in the extent, | |
297 | * The key type is used to differentiate between types of back refs. | |
298 | * There are different meanings of the key offset for different types | |
299 | * of back refs. | |
300 | * | |
d8d5f3e1 CM |
301 | * File extents can be referenced by: |
302 | * | |
303 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
31840ae1 | 304 | * - different files inside a single subvolume |
d8d5f3e1 CM |
305 | * - different offsets inside a file (bookend extents in file.c) |
306 | * | |
5d4f98a2 | 307 | * The extent ref structure for the implicit back refs has fields for: |
d8d5f3e1 CM |
308 | * |
309 | * - Objectid of the subvolume root | |
d8d5f3e1 | 310 | * - objectid of the file holding the reference |
5d4f98a2 YZ |
311 | * - original offset in the file |
312 | * - how many bookend extents | |
d8d5f3e1 | 313 | * |
5d4f98a2 YZ |
314 | * The key offset for the implicit back refs is hash of the first |
315 | * three fields. | |
d8d5f3e1 | 316 | * |
5d4f98a2 | 317 | * The extent ref structure for the full back refs has field for: |
d8d5f3e1 | 318 | * |
5d4f98a2 | 319 | * - number of pointers in the tree leaf |
d8d5f3e1 | 320 | * |
5d4f98a2 YZ |
321 | * The key offset for the implicit back refs is the first byte of |
322 | * the tree leaf | |
d8d5f3e1 | 323 | * |
5d4f98a2 YZ |
324 | * When a file extent is allocated, The implicit back refs is used. |
325 | * the fields are filled in: | |
d8d5f3e1 | 326 | * |
5d4f98a2 | 327 | * (root_key.objectid, inode objectid, offset in file, 1) |
d8d5f3e1 | 328 | * |
5d4f98a2 YZ |
329 | * When a file extent is removed file truncation, we find the |
330 | * corresponding implicit back refs and check the following fields: | |
d8d5f3e1 | 331 | * |
5d4f98a2 | 332 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
d8d5f3e1 | 333 | * |
5d4f98a2 | 334 | * Btree extents can be referenced by: |
d8d5f3e1 | 335 | * |
5d4f98a2 | 336 | * - Different subvolumes |
d8d5f3e1 | 337 | * |
5d4f98a2 YZ |
338 | * Both the implicit back refs and the full back refs for tree blocks |
339 | * only consist of key. The key offset for the implicit back refs is | |
340 | * objectid of block's owner tree. The key offset for the full back refs | |
341 | * is the first byte of parent block. | |
d8d5f3e1 | 342 | * |
5d4f98a2 YZ |
343 | * When implicit back refs is used, information about the lowest key and |
344 | * level of the tree block are required. These information are stored in | |
345 | * tree block info structure. | |
d8d5f3e1 | 346 | */ |
31840ae1 | 347 | |
167ce953 LB |
348 | /* |
349 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, | |
52042d8e | 350 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
167ce953 LB |
351 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
352 | */ | |
353 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, | |
354 | struct btrfs_extent_inline_ref *iref, | |
355 | enum btrfs_inline_ref_type is_data) | |
356 | { | |
357 | int type = btrfs_extent_inline_ref_type(eb, iref); | |
64ecdb64 | 358 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
167ce953 LB |
359 | |
360 | if (type == BTRFS_TREE_BLOCK_REF_KEY || | |
361 | type == BTRFS_SHARED_BLOCK_REF_KEY || | |
362 | type == BTRFS_SHARED_DATA_REF_KEY || | |
363 | type == BTRFS_EXTENT_DATA_REF_KEY) { | |
364 | if (is_data == BTRFS_REF_TYPE_BLOCK) { | |
64ecdb64 | 365 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
167ce953 | 366 | return type; |
64ecdb64 LB |
367 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
368 | ASSERT(eb->fs_info); | |
369 | /* | |
ea57788e QW |
370 | * Every shared one has parent tree block, |
371 | * which must be aligned to sector size. | |
64ecdb64 LB |
372 | */ |
373 | if (offset && | |
ea57788e | 374 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
64ecdb64 LB |
375 | return type; |
376 | } | |
167ce953 | 377 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
64ecdb64 | 378 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
167ce953 | 379 | return type; |
64ecdb64 LB |
380 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
381 | ASSERT(eb->fs_info); | |
382 | /* | |
ea57788e QW |
383 | * Every shared one has parent tree block, |
384 | * which must be aligned to sector size. | |
64ecdb64 LB |
385 | */ |
386 | if (offset && | |
ea57788e | 387 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
64ecdb64 LB |
388 | return type; |
389 | } | |
167ce953 LB |
390 | } else { |
391 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); | |
392 | return type; | |
393 | } | |
394 | } | |
395 | ||
396 | btrfs_print_leaf((struct extent_buffer *)eb); | |
ea57788e QW |
397 | btrfs_err(eb->fs_info, |
398 | "eb %llu iref 0x%lx invalid extent inline ref type %d", | |
399 | eb->start, (unsigned long)iref, type); | |
167ce953 LB |
400 | WARN_ON(1); |
401 | ||
402 | return BTRFS_REF_TYPE_INVALID; | |
403 | } | |
404 | ||
0785a9aa | 405 | u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
5d4f98a2 YZ |
406 | { |
407 | u32 high_crc = ~(u32)0; | |
408 | u32 low_crc = ~(u32)0; | |
409 | __le64 lenum; | |
410 | ||
411 | lenum = cpu_to_le64(root_objectid); | |
65019df8 | 412 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 413 | lenum = cpu_to_le64(owner); |
65019df8 | 414 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 415 | lenum = cpu_to_le64(offset); |
65019df8 | 416 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 YZ |
417 | |
418 | return ((u64)high_crc << 31) ^ (u64)low_crc; | |
419 | } | |
420 | ||
421 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, | |
422 | struct btrfs_extent_data_ref *ref) | |
423 | { | |
424 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), | |
425 | btrfs_extent_data_ref_objectid(leaf, ref), | |
426 | btrfs_extent_data_ref_offset(leaf, ref)); | |
427 | } | |
428 | ||
429 | static int match_extent_data_ref(struct extent_buffer *leaf, | |
430 | struct btrfs_extent_data_ref *ref, | |
431 | u64 root_objectid, u64 owner, u64 offset) | |
432 | { | |
433 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || | |
434 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || | |
435 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
436 | return 0; | |
437 | return 1; | |
438 | } | |
439 | ||
440 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, | |
5d4f98a2 YZ |
441 | struct btrfs_path *path, |
442 | u64 bytenr, u64 parent, | |
443 | u64 root_objectid, | |
444 | u64 owner, u64 offset) | |
445 | { | |
bd1d53ef | 446 | struct btrfs_root *root = trans->fs_info->extent_root; |
5d4f98a2 YZ |
447 | struct btrfs_key key; |
448 | struct btrfs_extent_data_ref *ref; | |
31840ae1 | 449 | struct extent_buffer *leaf; |
5d4f98a2 | 450 | u32 nritems; |
74493f7a | 451 | int ret; |
5d4f98a2 YZ |
452 | int recow; |
453 | int err = -ENOENT; | |
74493f7a | 454 | |
31840ae1 | 455 | key.objectid = bytenr; |
5d4f98a2 YZ |
456 | if (parent) { |
457 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
458 | key.offset = parent; | |
459 | } else { | |
460 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
461 | key.offset = hash_extent_data_ref(root_objectid, | |
462 | owner, offset); | |
463 | } | |
464 | again: | |
465 | recow = 0; | |
466 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
467 | if (ret < 0) { | |
468 | err = ret; | |
469 | goto fail; | |
470 | } | |
31840ae1 | 471 | |
5d4f98a2 YZ |
472 | if (parent) { |
473 | if (!ret) | |
474 | return 0; | |
5d4f98a2 | 475 | goto fail; |
31840ae1 ZY |
476 | } |
477 | ||
478 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
479 | nritems = btrfs_header_nritems(leaf); |
480 | while (1) { | |
481 | if (path->slots[0] >= nritems) { | |
482 | ret = btrfs_next_leaf(root, path); | |
483 | if (ret < 0) | |
484 | err = ret; | |
485 | if (ret) | |
486 | goto fail; | |
487 | ||
488 | leaf = path->nodes[0]; | |
489 | nritems = btrfs_header_nritems(leaf); | |
490 | recow = 1; | |
491 | } | |
492 | ||
493 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
494 | if (key.objectid != bytenr || | |
495 | key.type != BTRFS_EXTENT_DATA_REF_KEY) | |
496 | goto fail; | |
497 | ||
498 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
499 | struct btrfs_extent_data_ref); | |
500 | ||
501 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
502 | owner, offset)) { | |
503 | if (recow) { | |
b3b4aa74 | 504 | btrfs_release_path(path); |
5d4f98a2 YZ |
505 | goto again; |
506 | } | |
507 | err = 0; | |
508 | break; | |
509 | } | |
510 | path->slots[0]++; | |
31840ae1 | 511 | } |
5d4f98a2 YZ |
512 | fail: |
513 | return err; | |
31840ae1 ZY |
514 | } |
515 | ||
5d4f98a2 | 516 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
517 | struct btrfs_path *path, |
518 | u64 bytenr, u64 parent, | |
519 | u64 root_objectid, u64 owner, | |
520 | u64 offset, int refs_to_add) | |
31840ae1 | 521 | { |
62b895af | 522 | struct btrfs_root *root = trans->fs_info->extent_root; |
31840ae1 ZY |
523 | struct btrfs_key key; |
524 | struct extent_buffer *leaf; | |
5d4f98a2 | 525 | u32 size; |
31840ae1 ZY |
526 | u32 num_refs; |
527 | int ret; | |
74493f7a | 528 | |
74493f7a | 529 | key.objectid = bytenr; |
5d4f98a2 YZ |
530 | if (parent) { |
531 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
532 | key.offset = parent; | |
533 | size = sizeof(struct btrfs_shared_data_ref); | |
534 | } else { | |
535 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
536 | key.offset = hash_extent_data_ref(root_objectid, | |
537 | owner, offset); | |
538 | size = sizeof(struct btrfs_extent_data_ref); | |
539 | } | |
74493f7a | 540 | |
5d4f98a2 YZ |
541 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
542 | if (ret && ret != -EEXIST) | |
543 | goto fail; | |
544 | ||
545 | leaf = path->nodes[0]; | |
546 | if (parent) { | |
547 | struct btrfs_shared_data_ref *ref; | |
31840ae1 | 548 | ref = btrfs_item_ptr(leaf, path->slots[0], |
5d4f98a2 YZ |
549 | struct btrfs_shared_data_ref); |
550 | if (ret == 0) { | |
551 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); | |
552 | } else { | |
553 | num_refs = btrfs_shared_data_ref_count(leaf, ref); | |
554 | num_refs += refs_to_add; | |
555 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 556 | } |
5d4f98a2 YZ |
557 | } else { |
558 | struct btrfs_extent_data_ref *ref; | |
559 | while (ret == -EEXIST) { | |
560 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
561 | struct btrfs_extent_data_ref); | |
562 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
563 | owner, offset)) | |
564 | break; | |
b3b4aa74 | 565 | btrfs_release_path(path); |
5d4f98a2 YZ |
566 | key.offset++; |
567 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
568 | size); | |
569 | if (ret && ret != -EEXIST) | |
570 | goto fail; | |
31840ae1 | 571 | |
5d4f98a2 YZ |
572 | leaf = path->nodes[0]; |
573 | } | |
574 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
575 | struct btrfs_extent_data_ref); | |
576 | if (ret == 0) { | |
577 | btrfs_set_extent_data_ref_root(leaf, ref, | |
578 | root_objectid); | |
579 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
580 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
581 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); | |
582 | } else { | |
583 | num_refs = btrfs_extent_data_ref_count(leaf, ref); | |
584 | num_refs += refs_to_add; | |
585 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 586 | } |
31840ae1 | 587 | } |
5d4f98a2 YZ |
588 | btrfs_mark_buffer_dirty(leaf); |
589 | ret = 0; | |
590 | fail: | |
b3b4aa74 | 591 | btrfs_release_path(path); |
7bb86316 | 592 | return ret; |
74493f7a CM |
593 | } |
594 | ||
5d4f98a2 | 595 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 | 596 | struct btrfs_path *path, |
fcebe456 | 597 | int refs_to_drop, int *last_ref) |
31840ae1 | 598 | { |
5d4f98a2 YZ |
599 | struct btrfs_key key; |
600 | struct btrfs_extent_data_ref *ref1 = NULL; | |
601 | struct btrfs_shared_data_ref *ref2 = NULL; | |
31840ae1 | 602 | struct extent_buffer *leaf; |
5d4f98a2 | 603 | u32 num_refs = 0; |
31840ae1 ZY |
604 | int ret = 0; |
605 | ||
606 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
607 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
608 | ||
609 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
610 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
611 | struct btrfs_extent_data_ref); | |
612 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
613 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
614 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
615 | struct btrfs_shared_data_ref); | |
616 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
6d8ff4e4 | 617 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
ba3c2b19 NB |
618 | btrfs_print_v0_err(trans->fs_info); |
619 | btrfs_abort_transaction(trans, -EINVAL); | |
620 | return -EINVAL; | |
5d4f98a2 YZ |
621 | } else { |
622 | BUG(); | |
623 | } | |
624 | ||
56bec294 CM |
625 | BUG_ON(num_refs < refs_to_drop); |
626 | num_refs -= refs_to_drop; | |
5d4f98a2 | 627 | |
31840ae1 | 628 | if (num_refs == 0) { |
e9f6290d | 629 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
fcebe456 | 630 | *last_ref = 1; |
31840ae1 | 631 | } else { |
5d4f98a2 YZ |
632 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
633 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); | |
634 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) | |
635 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); | |
31840ae1 ZY |
636 | btrfs_mark_buffer_dirty(leaf); |
637 | } | |
31840ae1 ZY |
638 | return ret; |
639 | } | |
640 | ||
9ed0dea0 | 641 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
5d4f98a2 | 642 | struct btrfs_extent_inline_ref *iref) |
15916de8 | 643 | { |
5d4f98a2 YZ |
644 | struct btrfs_key key; |
645 | struct extent_buffer *leaf; | |
646 | struct btrfs_extent_data_ref *ref1; | |
647 | struct btrfs_shared_data_ref *ref2; | |
648 | u32 num_refs = 0; | |
3de28d57 | 649 | int type; |
5d4f98a2 YZ |
650 | |
651 | leaf = path->nodes[0]; | |
652 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
ba3c2b19 NB |
653 | |
654 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); | |
5d4f98a2 | 655 | if (iref) { |
3de28d57 LB |
656 | /* |
657 | * If type is invalid, we should have bailed out earlier than | |
658 | * this call. | |
659 | */ | |
660 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); | |
661 | ASSERT(type != BTRFS_REF_TYPE_INVALID); | |
662 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
5d4f98a2 YZ |
663 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
664 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
665 | } else { | |
666 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); | |
667 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
668 | } | |
669 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
670 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
671 | struct btrfs_extent_data_ref); | |
672 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
673 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
674 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
675 | struct btrfs_shared_data_ref); | |
676 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
5d4f98a2 YZ |
677 | } else { |
678 | WARN_ON(1); | |
679 | } | |
680 | return num_refs; | |
681 | } | |
15916de8 | 682 | |
5d4f98a2 | 683 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
684 | struct btrfs_path *path, |
685 | u64 bytenr, u64 parent, | |
686 | u64 root_objectid) | |
1f3c79a2 | 687 | { |
b8582eea | 688 | struct btrfs_root *root = trans->fs_info->extent_root; |
5d4f98a2 | 689 | struct btrfs_key key; |
1f3c79a2 | 690 | int ret; |
1f3c79a2 | 691 | |
5d4f98a2 YZ |
692 | key.objectid = bytenr; |
693 | if (parent) { | |
694 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
695 | key.offset = parent; | |
696 | } else { | |
697 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
698 | key.offset = root_objectid; | |
1f3c79a2 LH |
699 | } |
700 | ||
5d4f98a2 YZ |
701 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
702 | if (ret > 0) | |
703 | ret = -ENOENT; | |
5d4f98a2 | 704 | return ret; |
1f3c79a2 LH |
705 | } |
706 | ||
5d4f98a2 | 707 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
708 | struct btrfs_path *path, |
709 | u64 bytenr, u64 parent, | |
710 | u64 root_objectid) | |
31840ae1 | 711 | { |
5d4f98a2 | 712 | struct btrfs_key key; |
31840ae1 | 713 | int ret; |
31840ae1 | 714 | |
5d4f98a2 YZ |
715 | key.objectid = bytenr; |
716 | if (parent) { | |
717 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
718 | key.offset = parent; | |
719 | } else { | |
720 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
721 | key.offset = root_objectid; | |
722 | } | |
723 | ||
10728404 | 724 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
87bde3cd | 725 | path, &key, 0); |
b3b4aa74 | 726 | btrfs_release_path(path); |
31840ae1 ZY |
727 | return ret; |
728 | } | |
729 | ||
5d4f98a2 | 730 | static inline int extent_ref_type(u64 parent, u64 owner) |
31840ae1 | 731 | { |
5d4f98a2 YZ |
732 | int type; |
733 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
734 | if (parent > 0) | |
735 | type = BTRFS_SHARED_BLOCK_REF_KEY; | |
736 | else | |
737 | type = BTRFS_TREE_BLOCK_REF_KEY; | |
738 | } else { | |
739 | if (parent > 0) | |
740 | type = BTRFS_SHARED_DATA_REF_KEY; | |
741 | else | |
742 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
743 | } | |
744 | return type; | |
31840ae1 | 745 | } |
56bec294 | 746 | |
2c47e605 YZ |
747 | static int find_next_key(struct btrfs_path *path, int level, |
748 | struct btrfs_key *key) | |
56bec294 | 749 | |
02217ed2 | 750 | { |
2c47e605 | 751 | for (; level < BTRFS_MAX_LEVEL; level++) { |
5d4f98a2 YZ |
752 | if (!path->nodes[level]) |
753 | break; | |
5d4f98a2 YZ |
754 | if (path->slots[level] + 1 >= |
755 | btrfs_header_nritems(path->nodes[level])) | |
756 | continue; | |
757 | if (level == 0) | |
758 | btrfs_item_key_to_cpu(path->nodes[level], key, | |
759 | path->slots[level] + 1); | |
760 | else | |
761 | btrfs_node_key_to_cpu(path->nodes[level], key, | |
762 | path->slots[level] + 1); | |
763 | return 0; | |
764 | } | |
765 | return 1; | |
766 | } | |
037e6390 | 767 | |
5d4f98a2 YZ |
768 | /* |
769 | * look for inline back ref. if back ref is found, *ref_ret is set | |
770 | * to the address of inline back ref, and 0 is returned. | |
771 | * | |
772 | * if back ref isn't found, *ref_ret is set to the address where it | |
773 | * should be inserted, and -ENOENT is returned. | |
774 | * | |
775 | * if insert is true and there are too many inline back refs, the path | |
776 | * points to the extent item, and -EAGAIN is returned. | |
777 | * | |
778 | * NOTE: inline back refs are ordered in the same way that back ref | |
779 | * items in the tree are ordered. | |
780 | */ | |
781 | static noinline_for_stack | |
782 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, | |
5d4f98a2 YZ |
783 | struct btrfs_path *path, |
784 | struct btrfs_extent_inline_ref **ref_ret, | |
785 | u64 bytenr, u64 num_bytes, | |
786 | u64 parent, u64 root_objectid, | |
787 | u64 owner, u64 offset, int insert) | |
788 | { | |
867cc1fb | 789 | struct btrfs_fs_info *fs_info = trans->fs_info; |
87bde3cd | 790 | struct btrfs_root *root = fs_info->extent_root; |
5d4f98a2 YZ |
791 | struct btrfs_key key; |
792 | struct extent_buffer *leaf; | |
793 | struct btrfs_extent_item *ei; | |
794 | struct btrfs_extent_inline_ref *iref; | |
795 | u64 flags; | |
796 | u64 item_size; | |
797 | unsigned long ptr; | |
798 | unsigned long end; | |
799 | int extra_size; | |
800 | int type; | |
801 | int want; | |
802 | int ret; | |
803 | int err = 0; | |
0b246afa | 804 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
3de28d57 | 805 | int needed; |
26b8003f | 806 | |
db94535d | 807 | key.objectid = bytenr; |
31840ae1 | 808 | key.type = BTRFS_EXTENT_ITEM_KEY; |
56bec294 | 809 | key.offset = num_bytes; |
31840ae1 | 810 | |
5d4f98a2 YZ |
811 | want = extent_ref_type(parent, owner); |
812 | if (insert) { | |
813 | extra_size = btrfs_extent_inline_ref_size(want); | |
9a664971 | 814 | path->search_for_extension = 1; |
85d4198e | 815 | path->keep_locks = 1; |
5d4f98a2 YZ |
816 | } else |
817 | extra_size = -1; | |
3173a18f JB |
818 | |
819 | /* | |
16d1c062 NB |
820 | * Owner is our level, so we can just add one to get the level for the |
821 | * block we are interested in. | |
3173a18f JB |
822 | */ |
823 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { | |
824 | key.type = BTRFS_METADATA_ITEM_KEY; | |
825 | key.offset = owner; | |
826 | } | |
827 | ||
828 | again: | |
5d4f98a2 | 829 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
b9473439 | 830 | if (ret < 0) { |
5d4f98a2 YZ |
831 | err = ret; |
832 | goto out; | |
833 | } | |
3173a18f JB |
834 | |
835 | /* | |
836 | * We may be a newly converted file system which still has the old fat | |
837 | * extent entries for metadata, so try and see if we have one of those. | |
838 | */ | |
839 | if (ret > 0 && skinny_metadata) { | |
840 | skinny_metadata = false; | |
841 | if (path->slots[0]) { | |
842 | path->slots[0]--; | |
843 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
844 | path->slots[0]); | |
845 | if (key.objectid == bytenr && | |
846 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
847 | key.offset == num_bytes) | |
848 | ret = 0; | |
849 | } | |
850 | if (ret) { | |
9ce49a0b | 851 | key.objectid = bytenr; |
3173a18f JB |
852 | key.type = BTRFS_EXTENT_ITEM_KEY; |
853 | key.offset = num_bytes; | |
854 | btrfs_release_path(path); | |
855 | goto again; | |
856 | } | |
857 | } | |
858 | ||
79787eaa JM |
859 | if (ret && !insert) { |
860 | err = -ENOENT; | |
861 | goto out; | |
fae7f21c | 862 | } else if (WARN_ON(ret)) { |
492104c8 | 863 | err = -EIO; |
492104c8 | 864 | goto out; |
79787eaa | 865 | } |
5d4f98a2 YZ |
866 | |
867 | leaf = path->nodes[0]; | |
868 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
6d8ff4e4 | 869 | if (unlikely(item_size < sizeof(*ei))) { |
ba3c2b19 NB |
870 | err = -EINVAL; |
871 | btrfs_print_v0_err(fs_info); | |
872 | btrfs_abort_transaction(trans, err); | |
873 | goto out; | |
874 | } | |
5d4f98a2 | 875 | |
5d4f98a2 YZ |
876 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
877 | flags = btrfs_extent_flags(leaf, ei); | |
878 | ||
879 | ptr = (unsigned long)(ei + 1); | |
880 | end = (unsigned long)ei + item_size; | |
881 | ||
3173a18f | 882 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
5d4f98a2 YZ |
883 | ptr += sizeof(struct btrfs_tree_block_info); |
884 | BUG_ON(ptr > end); | |
5d4f98a2 YZ |
885 | } |
886 | ||
3de28d57 LB |
887 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
888 | needed = BTRFS_REF_TYPE_DATA; | |
889 | else | |
890 | needed = BTRFS_REF_TYPE_BLOCK; | |
891 | ||
5d4f98a2 YZ |
892 | err = -ENOENT; |
893 | while (1) { | |
894 | if (ptr >= end) { | |
895 | WARN_ON(ptr > end); | |
896 | break; | |
897 | } | |
898 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
3de28d57 LB |
899 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
900 | if (type == BTRFS_REF_TYPE_INVALID) { | |
af431dcb | 901 | err = -EUCLEAN; |
3de28d57 LB |
902 | goto out; |
903 | } | |
904 | ||
5d4f98a2 YZ |
905 | if (want < type) |
906 | break; | |
907 | if (want > type) { | |
908 | ptr += btrfs_extent_inline_ref_size(type); | |
909 | continue; | |
910 | } | |
911 | ||
912 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
913 | struct btrfs_extent_data_ref *dref; | |
914 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
915 | if (match_extent_data_ref(leaf, dref, root_objectid, | |
916 | owner, offset)) { | |
917 | err = 0; | |
918 | break; | |
919 | } | |
920 | if (hash_extent_data_ref_item(leaf, dref) < | |
921 | hash_extent_data_ref(root_objectid, owner, offset)) | |
922 | break; | |
923 | } else { | |
924 | u64 ref_offset; | |
925 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); | |
926 | if (parent > 0) { | |
927 | if (parent == ref_offset) { | |
928 | err = 0; | |
929 | break; | |
930 | } | |
931 | if (ref_offset < parent) | |
932 | break; | |
933 | } else { | |
934 | if (root_objectid == ref_offset) { | |
935 | err = 0; | |
936 | break; | |
937 | } | |
938 | if (ref_offset < root_objectid) | |
939 | break; | |
940 | } | |
941 | } | |
942 | ptr += btrfs_extent_inline_ref_size(type); | |
943 | } | |
944 | if (err == -ENOENT && insert) { | |
945 | if (item_size + extra_size >= | |
946 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { | |
947 | err = -EAGAIN; | |
948 | goto out; | |
949 | } | |
950 | /* | |
951 | * To add new inline back ref, we have to make sure | |
952 | * there is no corresponding back ref item. | |
953 | * For simplicity, we just do not add new inline back | |
954 | * ref if there is any kind of item for this block | |
955 | */ | |
2c47e605 YZ |
956 | if (find_next_key(path, 0, &key) == 0 && |
957 | key.objectid == bytenr && | |
85d4198e | 958 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
5d4f98a2 YZ |
959 | err = -EAGAIN; |
960 | goto out; | |
961 | } | |
962 | } | |
963 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; | |
964 | out: | |
85d4198e | 965 | if (insert) { |
5d4f98a2 | 966 | path->keep_locks = 0; |
9a664971 | 967 | path->search_for_extension = 0; |
5d4f98a2 YZ |
968 | btrfs_unlock_up_safe(path, 1); |
969 | } | |
970 | return err; | |
971 | } | |
972 | ||
973 | /* | |
974 | * helper to add new inline back ref | |
975 | */ | |
976 | static noinline_for_stack | |
87bde3cd | 977 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
143bede5 JM |
978 | struct btrfs_path *path, |
979 | struct btrfs_extent_inline_ref *iref, | |
980 | u64 parent, u64 root_objectid, | |
981 | u64 owner, u64 offset, int refs_to_add, | |
982 | struct btrfs_delayed_extent_op *extent_op) | |
5d4f98a2 YZ |
983 | { |
984 | struct extent_buffer *leaf; | |
985 | struct btrfs_extent_item *ei; | |
986 | unsigned long ptr; | |
987 | unsigned long end; | |
988 | unsigned long item_offset; | |
989 | u64 refs; | |
990 | int size; | |
991 | int type; | |
5d4f98a2 YZ |
992 | |
993 | leaf = path->nodes[0]; | |
994 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
995 | item_offset = (unsigned long)iref - (unsigned long)ei; | |
996 | ||
997 | type = extent_ref_type(parent, owner); | |
998 | size = btrfs_extent_inline_ref_size(type); | |
999 | ||
c71dd880 | 1000 | btrfs_extend_item(path, size); |
5d4f98a2 YZ |
1001 | |
1002 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1003 | refs = btrfs_extent_refs(leaf, ei); | |
1004 | refs += refs_to_add; | |
1005 | btrfs_set_extent_refs(leaf, ei, refs); | |
1006 | if (extent_op) | |
1007 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1008 | ||
1009 | ptr = (unsigned long)ei + item_offset; | |
1010 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); | |
1011 | if (ptr < end - size) | |
1012 | memmove_extent_buffer(leaf, ptr + size, ptr, | |
1013 | end - size - ptr); | |
1014 | ||
1015 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
1016 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
1017 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1018 | struct btrfs_extent_data_ref *dref; | |
1019 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1020 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); | |
1021 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); | |
1022 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); | |
1023 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); | |
1024 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1025 | struct btrfs_shared_data_ref *sref; | |
1026 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1027 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); | |
1028 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1029 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { | |
1030 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1031 | } else { | |
1032 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | |
1033 | } | |
1034 | btrfs_mark_buffer_dirty(leaf); | |
5d4f98a2 YZ |
1035 | } |
1036 | ||
1037 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, | |
5d4f98a2 YZ |
1038 | struct btrfs_path *path, |
1039 | struct btrfs_extent_inline_ref **ref_ret, | |
1040 | u64 bytenr, u64 num_bytes, u64 parent, | |
1041 | u64 root_objectid, u64 owner, u64 offset) | |
1042 | { | |
1043 | int ret; | |
1044 | ||
867cc1fb NB |
1045 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
1046 | num_bytes, parent, root_objectid, | |
1047 | owner, offset, 0); | |
5d4f98a2 | 1048 | if (ret != -ENOENT) |
54aa1f4d | 1049 | return ret; |
5d4f98a2 | 1050 | |
b3b4aa74 | 1051 | btrfs_release_path(path); |
5d4f98a2 YZ |
1052 | *ref_ret = NULL; |
1053 | ||
1054 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
b8582eea NB |
1055 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
1056 | root_objectid); | |
5d4f98a2 | 1057 | } else { |
bd1d53ef NB |
1058 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
1059 | root_objectid, owner, offset); | |
b9473439 | 1060 | } |
5d4f98a2 YZ |
1061 | return ret; |
1062 | } | |
31840ae1 | 1063 | |
5d4f98a2 YZ |
1064 | /* |
1065 | * helper to update/remove inline back ref | |
1066 | */ | |
1067 | static noinline_for_stack | |
61a18f1c | 1068 | void update_inline_extent_backref(struct btrfs_path *path, |
143bede5 JM |
1069 | struct btrfs_extent_inline_ref *iref, |
1070 | int refs_to_mod, | |
fcebe456 JB |
1071 | struct btrfs_delayed_extent_op *extent_op, |
1072 | int *last_ref) | |
5d4f98a2 | 1073 | { |
61a18f1c | 1074 | struct extent_buffer *leaf = path->nodes[0]; |
5d4f98a2 YZ |
1075 | struct btrfs_extent_item *ei; |
1076 | struct btrfs_extent_data_ref *dref = NULL; | |
1077 | struct btrfs_shared_data_ref *sref = NULL; | |
1078 | unsigned long ptr; | |
1079 | unsigned long end; | |
1080 | u32 item_size; | |
1081 | int size; | |
1082 | int type; | |
5d4f98a2 YZ |
1083 | u64 refs; |
1084 | ||
5d4f98a2 YZ |
1085 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1086 | refs = btrfs_extent_refs(leaf, ei); | |
1087 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); | |
1088 | refs += refs_to_mod; | |
1089 | btrfs_set_extent_refs(leaf, ei, refs); | |
1090 | if (extent_op) | |
1091 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1092 | ||
3de28d57 LB |
1093 | /* |
1094 | * If type is invalid, we should have bailed out after | |
1095 | * lookup_inline_extent_backref(). | |
1096 | */ | |
1097 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); | |
1098 | ASSERT(type != BTRFS_REF_TYPE_INVALID); | |
5d4f98a2 YZ |
1099 | |
1100 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1101 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1102 | refs = btrfs_extent_data_ref_count(leaf, dref); | |
1103 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1104 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1105 | refs = btrfs_shared_data_ref_count(leaf, sref); | |
1106 | } else { | |
1107 | refs = 1; | |
1108 | BUG_ON(refs_to_mod != -1); | |
56bec294 | 1109 | } |
31840ae1 | 1110 | |
5d4f98a2 YZ |
1111 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
1112 | refs += refs_to_mod; | |
1113 | ||
1114 | if (refs > 0) { | |
1115 | if (type == BTRFS_EXTENT_DATA_REF_KEY) | |
1116 | btrfs_set_extent_data_ref_count(leaf, dref, refs); | |
1117 | else | |
1118 | btrfs_set_shared_data_ref_count(leaf, sref, refs); | |
1119 | } else { | |
fcebe456 | 1120 | *last_ref = 1; |
5d4f98a2 YZ |
1121 | size = btrfs_extent_inline_ref_size(type); |
1122 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1123 | ptr = (unsigned long)iref; | |
1124 | end = (unsigned long)ei + item_size; | |
1125 | if (ptr + size < end) | |
1126 | memmove_extent_buffer(leaf, ptr, ptr + size, | |
1127 | end - ptr - size); | |
1128 | item_size -= size; | |
78ac4f9e | 1129 | btrfs_truncate_item(path, item_size, 1); |
5d4f98a2 YZ |
1130 | } |
1131 | btrfs_mark_buffer_dirty(leaf); | |
5d4f98a2 YZ |
1132 | } |
1133 | ||
1134 | static noinline_for_stack | |
1135 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, | |
5d4f98a2 YZ |
1136 | struct btrfs_path *path, |
1137 | u64 bytenr, u64 num_bytes, u64 parent, | |
1138 | u64 root_objectid, u64 owner, | |
1139 | u64 offset, int refs_to_add, | |
1140 | struct btrfs_delayed_extent_op *extent_op) | |
1141 | { | |
1142 | struct btrfs_extent_inline_ref *iref; | |
1143 | int ret; | |
1144 | ||
867cc1fb NB |
1145 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
1146 | num_bytes, parent, root_objectid, | |
1147 | owner, offset, 1); | |
5d4f98a2 | 1148 | if (ret == 0) { |
07cce5cf QW |
1149 | /* |
1150 | * We're adding refs to a tree block we already own, this | |
1151 | * should not happen at all. | |
1152 | */ | |
1153 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1154 | btrfs_crit(trans->fs_info, | |
1155 | "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu", | |
1156 | bytenr, num_bytes, root_objectid); | |
1157 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { | |
1158 | WARN_ON(1); | |
1159 | btrfs_crit(trans->fs_info, | |
1160 | "path->slots[0]=%d path->nodes[0]:", path->slots[0]); | |
1161 | btrfs_print_leaf(path->nodes[0]); | |
1162 | } | |
1163 | return -EUCLEAN; | |
1164 | } | |
61a18f1c NB |
1165 | update_inline_extent_backref(path, iref, refs_to_add, |
1166 | extent_op, NULL); | |
5d4f98a2 | 1167 | } else if (ret == -ENOENT) { |
a639cdeb | 1168 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
143bede5 JM |
1169 | root_objectid, owner, offset, |
1170 | refs_to_add, extent_op); | |
1171 | ret = 0; | |
771ed689 | 1172 | } |
5d4f98a2 YZ |
1173 | return ret; |
1174 | } | |
31840ae1 | 1175 | |
5d4f98a2 | 1176 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
1177 | struct btrfs_path *path, |
1178 | struct btrfs_extent_inline_ref *iref, | |
fcebe456 | 1179 | int refs_to_drop, int is_data, int *last_ref) |
5d4f98a2 | 1180 | { |
143bede5 | 1181 | int ret = 0; |
b9473439 | 1182 | |
5d4f98a2 YZ |
1183 | BUG_ON(!is_data && refs_to_drop != 1); |
1184 | if (iref) { | |
61a18f1c NB |
1185 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
1186 | last_ref); | |
5d4f98a2 | 1187 | } else if (is_data) { |
e9f6290d | 1188 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
fcebe456 | 1189 | last_ref); |
5d4f98a2 | 1190 | } else { |
fcebe456 | 1191 | *last_ref = 1; |
87cc7a8a | 1192 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
5d4f98a2 YZ |
1193 | } |
1194 | return ret; | |
1195 | } | |
1196 | ||
d04c6b88 JM |
1197 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
1198 | u64 *discarded_bytes) | |
5d4f98a2 | 1199 | { |
86557861 JM |
1200 | int j, ret = 0; |
1201 | u64 bytes_left, end; | |
4d89d377 | 1202 | u64 aligned_start = ALIGN(start, 1 << 9); |
d04c6b88 | 1203 | |
4d89d377 JM |
1204 | if (WARN_ON(start != aligned_start)) { |
1205 | len -= aligned_start - start; | |
1206 | len = round_down(len, 1 << 9); | |
1207 | start = aligned_start; | |
1208 | } | |
d04c6b88 | 1209 | |
4d89d377 | 1210 | *discarded_bytes = 0; |
86557861 JM |
1211 | |
1212 | if (!len) | |
1213 | return 0; | |
1214 | ||
1215 | end = start + len; | |
1216 | bytes_left = len; | |
1217 | ||
1218 | /* Skip any superblocks on this device. */ | |
1219 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { | |
1220 | u64 sb_start = btrfs_sb_offset(j); | |
1221 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; | |
1222 | u64 size = sb_start - start; | |
1223 | ||
1224 | if (!in_range(sb_start, start, bytes_left) && | |
1225 | !in_range(sb_end, start, bytes_left) && | |
1226 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) | |
1227 | continue; | |
1228 | ||
1229 | /* | |
1230 | * Superblock spans beginning of range. Adjust start and | |
1231 | * try again. | |
1232 | */ | |
1233 | if (sb_start <= start) { | |
1234 | start += sb_end - start; | |
1235 | if (start > end) { | |
1236 | bytes_left = 0; | |
1237 | break; | |
1238 | } | |
1239 | bytes_left = end - start; | |
1240 | continue; | |
1241 | } | |
1242 | ||
1243 | if (size) { | |
1244 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, | |
1245 | GFP_NOFS, 0); | |
1246 | if (!ret) | |
1247 | *discarded_bytes += size; | |
1248 | else if (ret != -EOPNOTSUPP) | |
1249 | return ret; | |
1250 | } | |
1251 | ||
1252 | start = sb_end; | |
1253 | if (start > end) { | |
1254 | bytes_left = 0; | |
1255 | break; | |
1256 | } | |
1257 | bytes_left = end - start; | |
1258 | } | |
1259 | ||
1260 | if (bytes_left) { | |
1261 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, | |
4d89d377 JM |
1262 | GFP_NOFS, 0); |
1263 | if (!ret) | |
86557861 | 1264 | *discarded_bytes += bytes_left; |
4d89d377 | 1265 | } |
d04c6b88 | 1266 | return ret; |
5d4f98a2 | 1267 | } |
5d4f98a2 | 1268 | |
6143c23c NA |
1269 | static int do_discard_extent(struct btrfs_bio_stripe *stripe, u64 *bytes) |
1270 | { | |
1271 | struct btrfs_device *dev = stripe->dev; | |
1272 | struct btrfs_fs_info *fs_info = dev->fs_info; | |
1273 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | |
1274 | u64 phys = stripe->physical; | |
1275 | u64 len = stripe->length; | |
1276 | u64 discarded = 0; | |
1277 | int ret = 0; | |
1278 | ||
1279 | /* Zone reset on a zoned filesystem */ | |
1280 | if (btrfs_can_zone_reset(dev, phys, len)) { | |
1281 | u64 src_disc; | |
1282 | ||
1283 | ret = btrfs_reset_device_zone(dev, phys, len, &discarded); | |
1284 | if (ret) | |
1285 | goto out; | |
1286 | ||
1287 | if (!btrfs_dev_replace_is_ongoing(dev_replace) || | |
1288 | dev != dev_replace->srcdev) | |
1289 | goto out; | |
1290 | ||
1291 | src_disc = discarded; | |
1292 | ||
1293 | /* Send to replace target as well */ | |
1294 | ret = btrfs_reset_device_zone(dev_replace->tgtdev, phys, len, | |
1295 | &discarded); | |
1296 | discarded += src_disc; | |
1297 | } else if (blk_queue_discard(bdev_get_queue(stripe->dev->bdev))) { | |
1298 | ret = btrfs_issue_discard(dev->bdev, phys, len, &discarded); | |
1299 | } else { | |
1300 | ret = 0; | |
1301 | *bytes = 0; | |
1302 | } | |
1303 | ||
1304 | out: | |
1305 | *bytes = discarded; | |
1306 | return ret; | |
1307 | } | |
1308 | ||
2ff7e61e | 1309 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
1edb647b | 1310 | u64 num_bytes, u64 *actual_bytes) |
5d4f98a2 | 1311 | { |
6b7faadd | 1312 | int ret = 0; |
5378e607 | 1313 | u64 discarded_bytes = 0; |
6b7faadd QW |
1314 | u64 end = bytenr + num_bytes; |
1315 | u64 cur = bytenr; | |
a1d3c478 | 1316 | struct btrfs_bio *bbio = NULL; |
5d4f98a2 | 1317 | |
e244a0ae | 1318 | |
2999241d FM |
1319 | /* |
1320 | * Avoid races with device replace and make sure our bbio has devices | |
1321 | * associated to its stripes that don't go away while we are discarding. | |
1322 | */ | |
0b246afa | 1323 | btrfs_bio_counter_inc_blocked(fs_info); |
6b7faadd QW |
1324 | while (cur < end) { |
1325 | struct btrfs_bio_stripe *stripe; | |
5d4f98a2 YZ |
1326 | int i; |
1327 | ||
6b7faadd QW |
1328 | num_bytes = end - cur; |
1329 | /* Tell the block device(s) that the sectors can be discarded */ | |
1330 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur, | |
1331 | &num_bytes, &bbio, 0); | |
1332 | /* | |
1333 | * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or | |
1334 | * -EOPNOTSUPP. For any such error, @num_bytes is not updated, | |
1335 | * thus we can't continue anyway. | |
1336 | */ | |
1337 | if (ret < 0) | |
1338 | goto out; | |
5d4f98a2 | 1339 | |
6b7faadd | 1340 | stripe = bbio->stripes; |
a1d3c478 | 1341 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
d04c6b88 | 1342 | u64 bytes; |
5e753a81 | 1343 | struct btrfs_device *device = stripe->dev; |
38b5f68e | 1344 | |
5e753a81 | 1345 | if (!device->bdev) { |
627e0873 FM |
1346 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
1347 | continue; | |
1348 | } | |
dcba6e48 | 1349 | |
5e753a81 AJ |
1350 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
1351 | continue; | |
1352 | ||
6143c23c | 1353 | ret = do_discard_extent(stripe, &bytes); |
6b7faadd | 1354 | if (!ret) { |
d04c6b88 | 1355 | discarded_bytes += bytes; |
6b7faadd QW |
1356 | } else if (ret != -EOPNOTSUPP) { |
1357 | /* | |
1358 | * Logic errors or -ENOMEM, or -EIO, but | |
1359 | * unlikely to happen. | |
1360 | * | |
1361 | * And since there are two loops, explicitly | |
1362 | * go to out to avoid confusion. | |
1363 | */ | |
1364 | btrfs_put_bbio(bbio); | |
1365 | goto out; | |
1366 | } | |
d5e2003c JB |
1367 | |
1368 | /* | |
1369 | * Just in case we get back EOPNOTSUPP for some reason, | |
1370 | * just ignore the return value so we don't screw up | |
1371 | * people calling discard_extent. | |
1372 | */ | |
1373 | ret = 0; | |
5d4f98a2 | 1374 | } |
6e9606d2 | 1375 | btrfs_put_bbio(bbio); |
6b7faadd | 1376 | cur += num_bytes; |
5d4f98a2 | 1377 | } |
6b7faadd | 1378 | out: |
0b246afa | 1379 | btrfs_bio_counter_dec(fs_info); |
5378e607 LD |
1380 | |
1381 | if (actual_bytes) | |
1382 | *actual_bytes = discarded_bytes; | |
1383 | ||
5d4f98a2 | 1384 | |
53b381b3 DW |
1385 | if (ret == -EOPNOTSUPP) |
1386 | ret = 0; | |
5d4f98a2 | 1387 | return ret; |
5d4f98a2 YZ |
1388 | } |
1389 | ||
79787eaa | 1390 | /* Can return -ENOMEM */ |
5d4f98a2 | 1391 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
82fa113f | 1392 | struct btrfs_ref *generic_ref) |
5d4f98a2 | 1393 | { |
82fa113f | 1394 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5d4f98a2 | 1395 | int ret; |
66d7e7f0 | 1396 | |
82fa113f QW |
1397 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
1398 | generic_ref->action); | |
1399 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && | |
1400 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); | |
5d4f98a2 | 1401 | |
82fa113f | 1402 | if (generic_ref->type == BTRFS_REF_METADATA) |
2187374f | 1403 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL); |
82fa113f | 1404 | else |
2187374f | 1405 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0); |
d7eae340 | 1406 | |
82fa113f | 1407 | btrfs_ref_tree_mod(fs_info, generic_ref); |
8a5040f7 | 1408 | |
5d4f98a2 YZ |
1409 | return ret; |
1410 | } | |
1411 | ||
bd3c685e NB |
1412 | /* |
1413 | * __btrfs_inc_extent_ref - insert backreference for a given extent | |
1414 | * | |
07cce5cf QW |
1415 | * The counterpart is in __btrfs_free_extent(), with examples and more details |
1416 | * how it works. | |
1417 | * | |
bd3c685e NB |
1418 | * @trans: Handle of transaction |
1419 | * | |
1420 | * @node: The delayed ref node used to get the bytenr/length for | |
1421 | * extent whose references are incremented. | |
1422 | * | |
1423 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ | |
1424 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical | |
1425 | * bytenr of the parent block. Since new extents are always | |
1426 | * created with indirect references, this will only be the case | |
1427 | * when relocating a shared extent. In that case, root_objectid | |
1a9fd417 | 1428 | * will be BTRFS_TREE_RELOC_OBJECTID. Otherwise, parent must |
bd3c685e NB |
1429 | * be 0 |
1430 | * | |
1431 | * @root_objectid: The id of the root where this modification has originated, | |
1432 | * this can be either one of the well-known metadata trees or | |
1433 | * the subvolume id which references this extent. | |
1434 | * | |
1435 | * @owner: For data extents it is the inode number of the owning file. | |
1436 | * For metadata extents this parameter holds the level in the | |
1437 | * tree of the extent. | |
1438 | * | |
1439 | * @offset: For metadata extents the offset is ignored and is currently | |
1440 | * always passed as 0. For data extents it is the fileoffset | |
1441 | * this extent belongs to. | |
1442 | * | |
1443 | * @refs_to_add Number of references to add | |
1444 | * | |
1445 | * @extent_op Pointer to a structure, holding information necessary when | |
1446 | * updating a tree block's flags | |
1447 | * | |
1448 | */ | |
5d4f98a2 | 1449 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
c682f9b3 | 1450 | struct btrfs_delayed_ref_node *node, |
5d4f98a2 YZ |
1451 | u64 parent, u64 root_objectid, |
1452 | u64 owner, u64 offset, int refs_to_add, | |
1453 | struct btrfs_delayed_extent_op *extent_op) | |
1454 | { | |
1455 | struct btrfs_path *path; | |
1456 | struct extent_buffer *leaf; | |
1457 | struct btrfs_extent_item *item; | |
fcebe456 | 1458 | struct btrfs_key key; |
c682f9b3 QW |
1459 | u64 bytenr = node->bytenr; |
1460 | u64 num_bytes = node->num_bytes; | |
5d4f98a2 YZ |
1461 | u64 refs; |
1462 | int ret; | |
5d4f98a2 YZ |
1463 | |
1464 | path = btrfs_alloc_path(); | |
1465 | if (!path) | |
1466 | return -ENOMEM; | |
1467 | ||
5d4f98a2 | 1468 | /* this will setup the path even if it fails to insert the back ref */ |
a639cdeb NB |
1469 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
1470 | parent, root_objectid, owner, | |
1471 | offset, refs_to_add, extent_op); | |
0ed4792a | 1472 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
5d4f98a2 | 1473 | goto out; |
fcebe456 JB |
1474 | |
1475 | /* | |
1476 | * Ok we had -EAGAIN which means we didn't have space to insert and | |
1477 | * inline extent ref, so just update the reference count and add a | |
1478 | * normal backref. | |
1479 | */ | |
5d4f98a2 | 1480 | leaf = path->nodes[0]; |
fcebe456 | 1481 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
5d4f98a2 YZ |
1482 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1483 | refs = btrfs_extent_refs(leaf, item); | |
1484 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); | |
1485 | if (extent_op) | |
1486 | __run_delayed_extent_op(extent_op, leaf, item); | |
56bec294 | 1487 | |
5d4f98a2 | 1488 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 1489 | btrfs_release_path(path); |
56bec294 | 1490 | |
56bec294 | 1491 | /* now insert the actual backref */ |
65cd6d9e NB |
1492 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
1493 | BUG_ON(refs_to_add != 1); | |
1494 | ret = insert_tree_block_ref(trans, path, bytenr, parent, | |
1495 | root_objectid); | |
1496 | } else { | |
1497 | ret = insert_extent_data_ref(trans, path, bytenr, parent, | |
1498 | root_objectid, owner, offset, | |
1499 | refs_to_add); | |
1500 | } | |
79787eaa | 1501 | if (ret) |
66642832 | 1502 | btrfs_abort_transaction(trans, ret); |
5d4f98a2 | 1503 | out: |
56bec294 | 1504 | btrfs_free_path(path); |
30d133fc | 1505 | return ret; |
56bec294 CM |
1506 | } |
1507 | ||
5d4f98a2 | 1508 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
1509 | struct btrfs_delayed_ref_node *node, |
1510 | struct btrfs_delayed_extent_op *extent_op, | |
1511 | int insert_reserved) | |
56bec294 | 1512 | { |
5d4f98a2 YZ |
1513 | int ret = 0; |
1514 | struct btrfs_delayed_data_ref *ref; | |
1515 | struct btrfs_key ins; | |
1516 | u64 parent = 0; | |
1517 | u64 ref_root = 0; | |
1518 | u64 flags = 0; | |
1519 | ||
1520 | ins.objectid = node->bytenr; | |
1521 | ins.offset = node->num_bytes; | |
1522 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
1523 | ||
1524 | ref = btrfs_delayed_node_to_data_ref(node); | |
2bf98ef3 | 1525 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
599c75ec | 1526 | |
5d4f98a2 YZ |
1527 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
1528 | parent = ref->parent; | |
fcebe456 | 1529 | ref_root = ref->root; |
5d4f98a2 YZ |
1530 | |
1531 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | |
3173a18f | 1532 | if (extent_op) |
5d4f98a2 | 1533 | flags |= extent_op->flags_to_set; |
ef89b824 NB |
1534 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
1535 | flags, ref->objectid, | |
1536 | ref->offset, &ins, | |
1537 | node->ref_mod); | |
5d4f98a2 | 1538 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
2590d0f1 NB |
1539 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
1540 | ref->objectid, ref->offset, | |
1541 | node->ref_mod, extent_op); | |
5d4f98a2 | 1542 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
e72cb923 | 1543 | ret = __btrfs_free_extent(trans, node, parent, |
5d4f98a2 YZ |
1544 | ref_root, ref->objectid, |
1545 | ref->offset, node->ref_mod, | |
c682f9b3 | 1546 | extent_op); |
5d4f98a2 YZ |
1547 | } else { |
1548 | BUG(); | |
1549 | } | |
1550 | return ret; | |
1551 | } | |
1552 | ||
1553 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, | |
1554 | struct extent_buffer *leaf, | |
1555 | struct btrfs_extent_item *ei) | |
1556 | { | |
1557 | u64 flags = btrfs_extent_flags(leaf, ei); | |
1558 | if (extent_op->update_flags) { | |
1559 | flags |= extent_op->flags_to_set; | |
1560 | btrfs_set_extent_flags(leaf, ei, flags); | |
1561 | } | |
1562 | ||
1563 | if (extent_op->update_key) { | |
1564 | struct btrfs_tree_block_info *bi; | |
1565 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); | |
1566 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
1567 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, | |
d278850e | 1572 | struct btrfs_delayed_ref_head *head, |
5d4f98a2 YZ |
1573 | struct btrfs_delayed_extent_op *extent_op) |
1574 | { | |
20b9a2d6 | 1575 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5d4f98a2 YZ |
1576 | struct btrfs_key key; |
1577 | struct btrfs_path *path; | |
1578 | struct btrfs_extent_item *ei; | |
1579 | struct extent_buffer *leaf; | |
1580 | u32 item_size; | |
56bec294 | 1581 | int ret; |
5d4f98a2 | 1582 | int err = 0; |
b1c79e09 | 1583 | int metadata = !extent_op->is_data; |
5d4f98a2 | 1584 | |
bf31f87f | 1585 | if (TRANS_ABORTED(trans)) |
79787eaa JM |
1586 | return 0; |
1587 | ||
0b246afa | 1588 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
3173a18f JB |
1589 | metadata = 0; |
1590 | ||
5d4f98a2 YZ |
1591 | path = btrfs_alloc_path(); |
1592 | if (!path) | |
1593 | return -ENOMEM; | |
1594 | ||
d278850e | 1595 | key.objectid = head->bytenr; |
5d4f98a2 | 1596 | |
3173a18f | 1597 | if (metadata) { |
3173a18f | 1598 | key.type = BTRFS_METADATA_ITEM_KEY; |
b1c79e09 | 1599 | key.offset = extent_op->level; |
3173a18f JB |
1600 | } else { |
1601 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
d278850e | 1602 | key.offset = head->num_bytes; |
3173a18f JB |
1603 | } |
1604 | ||
1605 | again: | |
0b246afa | 1606 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1); |
5d4f98a2 YZ |
1607 | if (ret < 0) { |
1608 | err = ret; | |
1609 | goto out; | |
1610 | } | |
1611 | if (ret > 0) { | |
3173a18f | 1612 | if (metadata) { |
55994887 FDBM |
1613 | if (path->slots[0] > 0) { |
1614 | path->slots[0]--; | |
1615 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
1616 | path->slots[0]); | |
d278850e | 1617 | if (key.objectid == head->bytenr && |
55994887 | 1618 | key.type == BTRFS_EXTENT_ITEM_KEY && |
d278850e | 1619 | key.offset == head->num_bytes) |
55994887 FDBM |
1620 | ret = 0; |
1621 | } | |
1622 | if (ret > 0) { | |
1623 | btrfs_release_path(path); | |
1624 | metadata = 0; | |
3173a18f | 1625 | |
d278850e JB |
1626 | key.objectid = head->bytenr; |
1627 | key.offset = head->num_bytes; | |
55994887 FDBM |
1628 | key.type = BTRFS_EXTENT_ITEM_KEY; |
1629 | goto again; | |
1630 | } | |
1631 | } else { | |
1632 | err = -EIO; | |
1633 | goto out; | |
3173a18f | 1634 | } |
5d4f98a2 YZ |
1635 | } |
1636 | ||
1637 | leaf = path->nodes[0]; | |
1638 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
ba3c2b19 | 1639 | |
6d8ff4e4 | 1640 | if (unlikely(item_size < sizeof(*ei))) { |
ba3c2b19 NB |
1641 | err = -EINVAL; |
1642 | btrfs_print_v0_err(fs_info); | |
1643 | btrfs_abort_transaction(trans, err); | |
1644 | goto out; | |
1645 | } | |
1646 | ||
5d4f98a2 YZ |
1647 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1648 | __run_delayed_extent_op(extent_op, leaf, ei); | |
56bec294 | 1649 | |
5d4f98a2 YZ |
1650 | btrfs_mark_buffer_dirty(leaf); |
1651 | out: | |
1652 | btrfs_free_path(path); | |
1653 | return err; | |
56bec294 CM |
1654 | } |
1655 | ||
5d4f98a2 | 1656 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
1657 | struct btrfs_delayed_ref_node *node, |
1658 | struct btrfs_delayed_extent_op *extent_op, | |
1659 | int insert_reserved) | |
56bec294 CM |
1660 | { |
1661 | int ret = 0; | |
5d4f98a2 | 1662 | struct btrfs_delayed_tree_ref *ref; |
5d4f98a2 YZ |
1663 | u64 parent = 0; |
1664 | u64 ref_root = 0; | |
56bec294 | 1665 | |
5d4f98a2 | 1666 | ref = btrfs_delayed_node_to_tree_ref(node); |
f97806f2 | 1667 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
599c75ec | 1668 | |
5d4f98a2 YZ |
1669 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
1670 | parent = ref->parent; | |
fcebe456 | 1671 | ref_root = ref->root; |
5d4f98a2 | 1672 | |
02794222 | 1673 | if (node->ref_mod != 1) { |
f97806f2 | 1674 | btrfs_err(trans->fs_info, |
02794222 LB |
1675 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
1676 | node->bytenr, node->ref_mod, node->action, ref_root, | |
1677 | parent); | |
1678 | return -EIO; | |
1679 | } | |
5d4f98a2 | 1680 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
3173a18f | 1681 | BUG_ON(!extent_op || !extent_op->update_flags); |
21ebfbe7 | 1682 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
5d4f98a2 | 1683 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
2590d0f1 NB |
1684 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
1685 | ref->level, 0, 1, extent_op); | |
5d4f98a2 | 1686 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
e72cb923 | 1687 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
c682f9b3 | 1688 | ref->level, 0, 1, extent_op); |
5d4f98a2 YZ |
1689 | } else { |
1690 | BUG(); | |
1691 | } | |
56bec294 CM |
1692 | return ret; |
1693 | } | |
1694 | ||
1695 | /* helper function to actually process a single delayed ref entry */ | |
5d4f98a2 | 1696 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
1697 | struct btrfs_delayed_ref_node *node, |
1698 | struct btrfs_delayed_extent_op *extent_op, | |
1699 | int insert_reserved) | |
56bec294 | 1700 | { |
79787eaa JM |
1701 | int ret = 0; |
1702 | ||
bf31f87f | 1703 | if (TRANS_ABORTED(trans)) { |
857cc2fc | 1704 | if (insert_reserved) |
b25c36f8 | 1705 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
79787eaa | 1706 | return 0; |
857cc2fc | 1707 | } |
79787eaa | 1708 | |
5d4f98a2 YZ |
1709 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
1710 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) | |
f97806f2 | 1711 | ret = run_delayed_tree_ref(trans, node, extent_op, |
5d4f98a2 YZ |
1712 | insert_reserved); |
1713 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || | |
1714 | node->type == BTRFS_SHARED_DATA_REF_KEY) | |
2bf98ef3 | 1715 | ret = run_delayed_data_ref(trans, node, extent_op, |
5d4f98a2 YZ |
1716 | insert_reserved); |
1717 | else | |
1718 | BUG(); | |
80ee54bf | 1719 | if (ret && insert_reserved) |
b25c36f8 | 1720 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
5d4f98a2 | 1721 | return ret; |
56bec294 CM |
1722 | } |
1723 | ||
c6fc2454 | 1724 | static inline struct btrfs_delayed_ref_node * |
56bec294 CM |
1725 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
1726 | { | |
cffc3374 FM |
1727 | struct btrfs_delayed_ref_node *ref; |
1728 | ||
e3d03965 | 1729 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
c6fc2454 | 1730 | return NULL; |
d7df2c79 | 1731 | |
cffc3374 FM |
1732 | /* |
1733 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. | |
1734 | * This is to prevent a ref count from going down to zero, which deletes | |
1735 | * the extent item from the extent tree, when there still are references | |
1736 | * to add, which would fail because they would not find the extent item. | |
1737 | */ | |
1d57ee94 WX |
1738 | if (!list_empty(&head->ref_add_list)) |
1739 | return list_first_entry(&head->ref_add_list, | |
1740 | struct btrfs_delayed_ref_node, add_list); | |
1741 | ||
e3d03965 | 1742 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
0e0adbcf | 1743 | struct btrfs_delayed_ref_node, ref_node); |
1d57ee94 WX |
1744 | ASSERT(list_empty(&ref->add_list)); |
1745 | return ref; | |
56bec294 CM |
1746 | } |
1747 | ||
2eadaa22 JB |
1748 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
1749 | struct btrfs_delayed_ref_head *head) | |
1750 | { | |
1751 | spin_lock(&delayed_refs->lock); | |
1752 | head->processing = 0; | |
1753 | delayed_refs->num_heads_ready++; | |
1754 | spin_unlock(&delayed_refs->lock); | |
1755 | btrfs_delayed_ref_unlock(head); | |
1756 | } | |
1757 | ||
bedc6617 JB |
1758 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
1759 | struct btrfs_delayed_ref_head *head) | |
b00e6250 JB |
1760 | { |
1761 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; | |
b00e6250 JB |
1762 | |
1763 | if (!extent_op) | |
bedc6617 JB |
1764 | return NULL; |
1765 | ||
b00e6250 | 1766 | if (head->must_insert_reserved) { |
bedc6617 | 1767 | head->extent_op = NULL; |
b00e6250 | 1768 | btrfs_free_delayed_extent_op(extent_op); |
bedc6617 | 1769 | return NULL; |
b00e6250 | 1770 | } |
bedc6617 JB |
1771 | return extent_op; |
1772 | } | |
1773 | ||
1774 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, | |
1775 | struct btrfs_delayed_ref_head *head) | |
1776 | { | |
1777 | struct btrfs_delayed_extent_op *extent_op; | |
1778 | int ret; | |
1779 | ||
1780 | extent_op = cleanup_extent_op(head); | |
1781 | if (!extent_op) | |
1782 | return 0; | |
1783 | head->extent_op = NULL; | |
b00e6250 | 1784 | spin_unlock(&head->lock); |
20b9a2d6 | 1785 | ret = run_delayed_extent_op(trans, head, extent_op); |
b00e6250 JB |
1786 | btrfs_free_delayed_extent_op(extent_op); |
1787 | return ret ? ret : 1; | |
1788 | } | |
1789 | ||
31890da0 JB |
1790 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
1791 | struct btrfs_delayed_ref_root *delayed_refs, | |
1792 | struct btrfs_delayed_ref_head *head) | |
07c47775 | 1793 | { |
ba2c4d4e | 1794 | int nr_items = 1; /* Dropping this ref head update. */ |
07c47775 | 1795 | |
81e75ac7 JB |
1796 | /* |
1797 | * We had csum deletions accounted for in our delayed refs rsv, we need | |
1798 | * to drop the csum leaves for this update from our delayed_refs_rsv. | |
1799 | */ | |
1800 | if (head->total_ref_mod < 0 && head->is_data) { | |
1801 | spin_lock(&delayed_refs->lock); | |
1802 | delayed_refs->pending_csums -= head->num_bytes; | |
1803 | spin_unlock(&delayed_refs->lock); | |
1804 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes); | |
1805 | } | |
1806 | ||
ba2c4d4e | 1807 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
07c47775 JB |
1808 | } |
1809 | ||
194ab0bc | 1810 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
194ab0bc JB |
1811 | struct btrfs_delayed_ref_head *head) |
1812 | { | |
f9871edd NB |
1813 | |
1814 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
194ab0bc JB |
1815 | struct btrfs_delayed_ref_root *delayed_refs; |
1816 | int ret; | |
1817 | ||
1818 | delayed_refs = &trans->transaction->delayed_refs; | |
1819 | ||
bedc6617 | 1820 | ret = run_and_cleanup_extent_op(trans, head); |
194ab0bc JB |
1821 | if (ret < 0) { |
1822 | unselect_delayed_ref_head(delayed_refs, head); | |
1823 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); | |
1824 | return ret; | |
1825 | } else if (ret) { | |
1826 | return ret; | |
1827 | } | |
1828 | ||
1829 | /* | |
1830 | * Need to drop our head ref lock and re-acquire the delayed ref lock | |
1831 | * and then re-check to make sure nobody got added. | |
1832 | */ | |
1833 | spin_unlock(&head->lock); | |
1834 | spin_lock(&delayed_refs->lock); | |
1835 | spin_lock(&head->lock); | |
e3d03965 | 1836 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
194ab0bc JB |
1837 | spin_unlock(&head->lock); |
1838 | spin_unlock(&delayed_refs->lock); | |
1839 | return 1; | |
1840 | } | |
d7baffda | 1841 | btrfs_delete_ref_head(delayed_refs, head); |
c1103f7a | 1842 | spin_unlock(&head->lock); |
1e7a1421 | 1843 | spin_unlock(&delayed_refs->lock); |
c1103f7a | 1844 | |
c1103f7a | 1845 | if (head->must_insert_reserved) { |
b25c36f8 | 1846 | btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1); |
c1103f7a | 1847 | if (head->is_data) { |
40e046ac FM |
1848 | ret = btrfs_del_csums(trans, fs_info->csum_root, |
1849 | head->bytenr, head->num_bytes); | |
c1103f7a JB |
1850 | } |
1851 | } | |
1852 | ||
31890da0 | 1853 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
07c47775 JB |
1854 | |
1855 | trace_run_delayed_ref_head(fs_info, head, 0); | |
c1103f7a | 1856 | btrfs_delayed_ref_unlock(head); |
d278850e | 1857 | btrfs_put_delayed_ref_head(head); |
856bd270 | 1858 | return ret; |
194ab0bc JB |
1859 | } |
1860 | ||
b1cdbcb5 NB |
1861 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
1862 | struct btrfs_trans_handle *trans) | |
1863 | { | |
1864 | struct btrfs_delayed_ref_root *delayed_refs = | |
1865 | &trans->transaction->delayed_refs; | |
1866 | struct btrfs_delayed_ref_head *head = NULL; | |
1867 | int ret; | |
1868 | ||
1869 | spin_lock(&delayed_refs->lock); | |
5637c74b | 1870 | head = btrfs_select_ref_head(delayed_refs); |
b1cdbcb5 NB |
1871 | if (!head) { |
1872 | spin_unlock(&delayed_refs->lock); | |
1873 | return head; | |
1874 | } | |
1875 | ||
1876 | /* | |
1877 | * Grab the lock that says we are going to process all the refs for | |
1878 | * this head | |
1879 | */ | |
9e920a6f | 1880 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
b1cdbcb5 NB |
1881 | spin_unlock(&delayed_refs->lock); |
1882 | ||
1883 | /* | |
1884 | * We may have dropped the spin lock to get the head mutex lock, and | |
1885 | * that might have given someone else time to free the head. If that's | |
1886 | * true, it has been removed from our list and we can move on. | |
1887 | */ | |
1888 | if (ret == -EAGAIN) | |
1889 | head = ERR_PTR(-EAGAIN); | |
1890 | ||
1891 | return head; | |
1892 | } | |
1893 | ||
e7261386 NB |
1894 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
1895 | struct btrfs_delayed_ref_head *locked_ref, | |
1896 | unsigned long *run_refs) | |
1897 | { | |
1898 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
1899 | struct btrfs_delayed_ref_root *delayed_refs; | |
1900 | struct btrfs_delayed_extent_op *extent_op; | |
1901 | struct btrfs_delayed_ref_node *ref; | |
1902 | int must_insert_reserved = 0; | |
1903 | int ret; | |
1904 | ||
1905 | delayed_refs = &trans->transaction->delayed_refs; | |
1906 | ||
0110a4c4 NB |
1907 | lockdep_assert_held(&locked_ref->mutex); |
1908 | lockdep_assert_held(&locked_ref->lock); | |
1909 | ||
e7261386 NB |
1910 | while ((ref = select_delayed_ref(locked_ref))) { |
1911 | if (ref->seq && | |
1912 | btrfs_check_delayed_seq(fs_info, ref->seq)) { | |
1913 | spin_unlock(&locked_ref->lock); | |
1914 | unselect_delayed_ref_head(delayed_refs, locked_ref); | |
1915 | return -EAGAIN; | |
1916 | } | |
1917 | ||
1918 | (*run_refs)++; | |
1919 | ref->in_tree = 0; | |
1920 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); | |
1921 | RB_CLEAR_NODE(&ref->ref_node); | |
1922 | if (!list_empty(&ref->add_list)) | |
1923 | list_del(&ref->add_list); | |
1924 | /* | |
1925 | * When we play the delayed ref, also correct the ref_mod on | |
1926 | * head | |
1927 | */ | |
1928 | switch (ref->action) { | |
1929 | case BTRFS_ADD_DELAYED_REF: | |
1930 | case BTRFS_ADD_DELAYED_EXTENT: | |
1931 | locked_ref->ref_mod -= ref->ref_mod; | |
1932 | break; | |
1933 | case BTRFS_DROP_DELAYED_REF: | |
1934 | locked_ref->ref_mod += ref->ref_mod; | |
1935 | break; | |
1936 | default: | |
1937 | WARN_ON(1); | |
1938 | } | |
1939 | atomic_dec(&delayed_refs->num_entries); | |
1940 | ||
1941 | /* | |
1942 | * Record the must_insert_reserved flag before we drop the | |
1943 | * spin lock. | |
1944 | */ | |
1945 | must_insert_reserved = locked_ref->must_insert_reserved; | |
1946 | locked_ref->must_insert_reserved = 0; | |
1947 | ||
1948 | extent_op = locked_ref->extent_op; | |
1949 | locked_ref->extent_op = NULL; | |
1950 | spin_unlock(&locked_ref->lock); | |
1951 | ||
1952 | ret = run_one_delayed_ref(trans, ref, extent_op, | |
1953 | must_insert_reserved); | |
1954 | ||
1955 | btrfs_free_delayed_extent_op(extent_op); | |
1956 | if (ret) { | |
1957 | unselect_delayed_ref_head(delayed_refs, locked_ref); | |
1958 | btrfs_put_delayed_ref(ref); | |
1959 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", | |
1960 | ret); | |
1961 | return ret; | |
1962 | } | |
1963 | ||
1964 | btrfs_put_delayed_ref(ref); | |
1965 | cond_resched(); | |
1966 | ||
1967 | spin_lock(&locked_ref->lock); | |
1968 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); | |
1969 | } | |
1970 | ||
1971 | return 0; | |
1972 | } | |
1973 | ||
79787eaa JM |
1974 | /* |
1975 | * Returns 0 on success or if called with an already aborted transaction. | |
1976 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. | |
1977 | */ | |
d7df2c79 | 1978 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
d7df2c79 | 1979 | unsigned long nr) |
56bec294 | 1980 | { |
0a1e458a | 1981 | struct btrfs_fs_info *fs_info = trans->fs_info; |
56bec294 | 1982 | struct btrfs_delayed_ref_root *delayed_refs; |
56bec294 | 1983 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
0a2b2a84 | 1984 | ktime_t start = ktime_get(); |
56bec294 | 1985 | int ret; |
d7df2c79 | 1986 | unsigned long count = 0; |
0a2b2a84 | 1987 | unsigned long actual_count = 0; |
56bec294 CM |
1988 | |
1989 | delayed_refs = &trans->transaction->delayed_refs; | |
0110a4c4 | 1990 | do { |
56bec294 | 1991 | if (!locked_ref) { |
b1cdbcb5 | 1992 | locked_ref = btrfs_obtain_ref_head(trans); |
0110a4c4 NB |
1993 | if (IS_ERR_OR_NULL(locked_ref)) { |
1994 | if (PTR_ERR(locked_ref) == -EAGAIN) { | |
1995 | continue; | |
1996 | } else { | |
1997 | break; | |
1998 | } | |
56bec294 | 1999 | } |
0110a4c4 | 2000 | count++; |
56bec294 | 2001 | } |
2c3cf7d5 FM |
2002 | /* |
2003 | * We need to try and merge add/drops of the same ref since we | |
2004 | * can run into issues with relocate dropping the implicit ref | |
2005 | * and then it being added back again before the drop can | |
2006 | * finish. If we merged anything we need to re-loop so we can | |
2007 | * get a good ref. | |
2008 | * Or we can get node references of the same type that weren't | |
2009 | * merged when created due to bumps in the tree mod seq, and | |
2010 | * we need to merge them to prevent adding an inline extent | |
2011 | * backref before dropping it (triggering a BUG_ON at | |
2012 | * insert_inline_extent_backref()). | |
2013 | */ | |
d7df2c79 | 2014 | spin_lock(&locked_ref->lock); |
be97f133 | 2015 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
ae1e206b | 2016 | |
0110a4c4 NB |
2017 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
2018 | &actual_count); | |
2019 | if (ret < 0 && ret != -EAGAIN) { | |
2020 | /* | |
2021 | * Error, btrfs_run_delayed_refs_for_head already | |
2022 | * unlocked everything so just bail out | |
2023 | */ | |
2024 | return ret; | |
2025 | } else if (!ret) { | |
2026 | /* | |
2027 | * Success, perform the usual cleanup of a processed | |
2028 | * head | |
2029 | */ | |
f9871edd | 2030 | ret = cleanup_ref_head(trans, locked_ref); |
194ab0bc | 2031 | if (ret > 0 ) { |
b00e6250 JB |
2032 | /* We dropped our lock, we need to loop. */ |
2033 | ret = 0; | |
d7df2c79 | 2034 | continue; |
194ab0bc JB |
2035 | } else if (ret) { |
2036 | return ret; | |
5d4f98a2 | 2037 | } |
22cd2e7d | 2038 | } |
1ce7a5ec | 2039 | |
b00e6250 | 2040 | /* |
0110a4c4 NB |
2041 | * Either success case or btrfs_run_delayed_refs_for_head |
2042 | * returned -EAGAIN, meaning we need to select another head | |
b00e6250 | 2043 | */ |
b00e6250 | 2044 | |
0110a4c4 | 2045 | locked_ref = NULL; |
c3e69d58 | 2046 | cond_resched(); |
0110a4c4 | 2047 | } while ((nr != -1 && count < nr) || locked_ref); |
0a2b2a84 JB |
2048 | |
2049 | /* | |
2050 | * We don't want to include ref heads since we can have empty ref heads | |
2051 | * and those will drastically skew our runtime down since we just do | |
2052 | * accounting, no actual extent tree updates. | |
2053 | */ | |
2054 | if (actual_count > 0) { | |
2055 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); | |
2056 | u64 avg; | |
2057 | ||
2058 | /* | |
2059 | * We weigh the current average higher than our current runtime | |
2060 | * to avoid large swings in the average. | |
2061 | */ | |
2062 | spin_lock(&delayed_refs->lock); | |
2063 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; | |
f8c269d7 | 2064 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
0a2b2a84 JB |
2065 | spin_unlock(&delayed_refs->lock); |
2066 | } | |
d7df2c79 | 2067 | return 0; |
c3e69d58 CM |
2068 | } |
2069 | ||
709c0486 AJ |
2070 | #ifdef SCRAMBLE_DELAYED_REFS |
2071 | /* | |
2072 | * Normally delayed refs get processed in ascending bytenr order. This | |
2073 | * correlates in most cases to the order added. To expose dependencies on this | |
2074 | * order, we start to process the tree in the middle instead of the beginning | |
2075 | */ | |
2076 | static u64 find_middle(struct rb_root *root) | |
2077 | { | |
2078 | struct rb_node *n = root->rb_node; | |
2079 | struct btrfs_delayed_ref_node *entry; | |
2080 | int alt = 1; | |
2081 | u64 middle; | |
2082 | u64 first = 0, last = 0; | |
2083 | ||
2084 | n = rb_first(root); | |
2085 | if (n) { | |
2086 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2087 | first = entry->bytenr; | |
2088 | } | |
2089 | n = rb_last(root); | |
2090 | if (n) { | |
2091 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2092 | last = entry->bytenr; | |
2093 | } | |
2094 | n = root->rb_node; | |
2095 | ||
2096 | while (n) { | |
2097 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2098 | WARN_ON(!entry->in_tree); | |
2099 | ||
2100 | middle = entry->bytenr; | |
2101 | ||
2102 | if (alt) | |
2103 | n = n->rb_left; | |
2104 | else | |
2105 | n = n->rb_right; | |
2106 | ||
2107 | alt = 1 - alt; | |
2108 | } | |
2109 | return middle; | |
2110 | } | |
2111 | #endif | |
2112 | ||
c3e69d58 CM |
2113 | /* |
2114 | * this starts processing the delayed reference count updates and | |
2115 | * extent insertions we have queued up so far. count can be | |
2116 | * 0, which means to process everything in the tree at the start | |
2117 | * of the run (but not newly added entries), or it can be some target | |
2118 | * number you'd like to process. | |
79787eaa JM |
2119 | * |
2120 | * Returns 0 on success or if called with an aborted transaction | |
2121 | * Returns <0 on error and aborts the transaction | |
c3e69d58 CM |
2122 | */ |
2123 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | |
c79a70b1 | 2124 | unsigned long count) |
c3e69d58 | 2125 | { |
c79a70b1 | 2126 | struct btrfs_fs_info *fs_info = trans->fs_info; |
c3e69d58 CM |
2127 | struct rb_node *node; |
2128 | struct btrfs_delayed_ref_root *delayed_refs; | |
c46effa6 | 2129 | struct btrfs_delayed_ref_head *head; |
c3e69d58 CM |
2130 | int ret; |
2131 | int run_all = count == (unsigned long)-1; | |
c3e69d58 | 2132 | |
79787eaa | 2133 | /* We'll clean this up in btrfs_cleanup_transaction */ |
bf31f87f | 2134 | if (TRANS_ABORTED(trans)) |
79787eaa JM |
2135 | return 0; |
2136 | ||
0b246afa | 2137 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
511711af CM |
2138 | return 0; |
2139 | ||
c3e69d58 | 2140 | delayed_refs = &trans->transaction->delayed_refs; |
26455d33 | 2141 | if (count == 0) |
61a56a99 | 2142 | count = delayed_refs->num_heads_ready; |
bb721703 | 2143 | |
c3e69d58 | 2144 | again: |
709c0486 AJ |
2145 | #ifdef SCRAMBLE_DELAYED_REFS |
2146 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); | |
2147 | #endif | |
0a1e458a | 2148 | ret = __btrfs_run_delayed_refs(trans, count); |
d7df2c79 | 2149 | if (ret < 0) { |
66642832 | 2150 | btrfs_abort_transaction(trans, ret); |
d7df2c79 | 2151 | return ret; |
eb099670 | 2152 | } |
c3e69d58 | 2153 | |
56bec294 | 2154 | if (run_all) { |
119e80df | 2155 | btrfs_create_pending_block_groups(trans); |
ea658bad | 2156 | |
d7df2c79 | 2157 | spin_lock(&delayed_refs->lock); |
5c9d028b | 2158 | node = rb_first_cached(&delayed_refs->href_root); |
d7df2c79 JB |
2159 | if (!node) { |
2160 | spin_unlock(&delayed_refs->lock); | |
56bec294 | 2161 | goto out; |
d7df2c79 | 2162 | } |
d278850e JB |
2163 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
2164 | href_node); | |
2165 | refcount_inc(&head->refs); | |
2166 | spin_unlock(&delayed_refs->lock); | |
e9d0b13b | 2167 | |
d278850e JB |
2168 | /* Mutex was contended, block until it's released and retry. */ |
2169 | mutex_lock(&head->mutex); | |
2170 | mutex_unlock(&head->mutex); | |
56bec294 | 2171 | |
d278850e | 2172 | btrfs_put_delayed_ref_head(head); |
d7df2c79 | 2173 | cond_resched(); |
56bec294 | 2174 | goto again; |
5f39d397 | 2175 | } |
54aa1f4d | 2176 | out: |
a28ec197 CM |
2177 | return 0; |
2178 | } | |
2179 | ||
5d4f98a2 | 2180 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
42c9d0b5 | 2181 | struct extent_buffer *eb, u64 flags, |
b1c79e09 | 2182 | int level, int is_data) |
5d4f98a2 YZ |
2183 | { |
2184 | struct btrfs_delayed_extent_op *extent_op; | |
2185 | int ret; | |
2186 | ||
78a6184a | 2187 | extent_op = btrfs_alloc_delayed_extent_op(); |
5d4f98a2 YZ |
2188 | if (!extent_op) |
2189 | return -ENOMEM; | |
2190 | ||
2191 | extent_op->flags_to_set = flags; | |
35b3ad50 DS |
2192 | extent_op->update_flags = true; |
2193 | extent_op->update_key = false; | |
2194 | extent_op->is_data = is_data ? true : false; | |
b1c79e09 | 2195 | extent_op->level = level; |
5d4f98a2 | 2196 | |
42c9d0b5 | 2197 | ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op); |
5d4f98a2 | 2198 | if (ret) |
78a6184a | 2199 | btrfs_free_delayed_extent_op(extent_op); |
5d4f98a2 YZ |
2200 | return ret; |
2201 | } | |
2202 | ||
e4c3b2dc | 2203 | static noinline int check_delayed_ref(struct btrfs_root *root, |
5d4f98a2 YZ |
2204 | struct btrfs_path *path, |
2205 | u64 objectid, u64 offset, u64 bytenr) | |
2206 | { | |
2207 | struct btrfs_delayed_ref_head *head; | |
2208 | struct btrfs_delayed_ref_node *ref; | |
2209 | struct btrfs_delayed_data_ref *data_ref; | |
2210 | struct btrfs_delayed_ref_root *delayed_refs; | |
e4c3b2dc | 2211 | struct btrfs_transaction *cur_trans; |
0e0adbcf | 2212 | struct rb_node *node; |
5d4f98a2 YZ |
2213 | int ret = 0; |
2214 | ||
998ac6d2 | 2215 | spin_lock(&root->fs_info->trans_lock); |
e4c3b2dc | 2216 | cur_trans = root->fs_info->running_transaction; |
998ac6d2 | 2217 | if (cur_trans) |
2218 | refcount_inc(&cur_trans->use_count); | |
2219 | spin_unlock(&root->fs_info->trans_lock); | |
e4c3b2dc LB |
2220 | if (!cur_trans) |
2221 | return 0; | |
2222 | ||
2223 | delayed_refs = &cur_trans->delayed_refs; | |
5d4f98a2 | 2224 | spin_lock(&delayed_refs->lock); |
f72ad18e | 2225 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
d7df2c79 JB |
2226 | if (!head) { |
2227 | spin_unlock(&delayed_refs->lock); | |
998ac6d2 | 2228 | btrfs_put_transaction(cur_trans); |
d7df2c79 JB |
2229 | return 0; |
2230 | } | |
5d4f98a2 YZ |
2231 | |
2232 | if (!mutex_trylock(&head->mutex)) { | |
d278850e | 2233 | refcount_inc(&head->refs); |
5d4f98a2 YZ |
2234 | spin_unlock(&delayed_refs->lock); |
2235 | ||
b3b4aa74 | 2236 | btrfs_release_path(path); |
5d4f98a2 | 2237 | |
8cc33e5c DS |
2238 | /* |
2239 | * Mutex was contended, block until it's released and let | |
2240 | * caller try again | |
2241 | */ | |
5d4f98a2 YZ |
2242 | mutex_lock(&head->mutex); |
2243 | mutex_unlock(&head->mutex); | |
d278850e | 2244 | btrfs_put_delayed_ref_head(head); |
998ac6d2 | 2245 | btrfs_put_transaction(cur_trans); |
5d4f98a2 YZ |
2246 | return -EAGAIN; |
2247 | } | |
d7df2c79 | 2248 | spin_unlock(&delayed_refs->lock); |
5d4f98a2 | 2249 | |
d7df2c79 | 2250 | spin_lock(&head->lock); |
0e0adbcf JB |
2251 | /* |
2252 | * XXX: We should replace this with a proper search function in the | |
2253 | * future. | |
2254 | */ | |
e3d03965 LB |
2255 | for (node = rb_first_cached(&head->ref_tree); node; |
2256 | node = rb_next(node)) { | |
0e0adbcf | 2257 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
d7df2c79 JB |
2258 | /* If it's a shared ref we know a cross reference exists */ |
2259 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { | |
2260 | ret = 1; | |
2261 | break; | |
2262 | } | |
5d4f98a2 | 2263 | |
d7df2c79 | 2264 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
5d4f98a2 | 2265 | |
d7df2c79 JB |
2266 | /* |
2267 | * If our ref doesn't match the one we're currently looking at | |
2268 | * then we have a cross reference. | |
2269 | */ | |
2270 | if (data_ref->root != root->root_key.objectid || | |
2271 | data_ref->objectid != objectid || | |
2272 | data_ref->offset != offset) { | |
2273 | ret = 1; | |
2274 | break; | |
2275 | } | |
5d4f98a2 | 2276 | } |
d7df2c79 | 2277 | spin_unlock(&head->lock); |
5d4f98a2 | 2278 | mutex_unlock(&head->mutex); |
998ac6d2 | 2279 | btrfs_put_transaction(cur_trans); |
5d4f98a2 YZ |
2280 | return ret; |
2281 | } | |
2282 | ||
e4c3b2dc | 2283 | static noinline int check_committed_ref(struct btrfs_root *root, |
5d4f98a2 | 2284 | struct btrfs_path *path, |
a84d5d42 BB |
2285 | u64 objectid, u64 offset, u64 bytenr, |
2286 | bool strict) | |
be20aa9d | 2287 | { |
0b246afa JM |
2288 | struct btrfs_fs_info *fs_info = root->fs_info; |
2289 | struct btrfs_root *extent_root = fs_info->extent_root; | |
f321e491 | 2290 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
2291 | struct btrfs_extent_data_ref *ref; |
2292 | struct btrfs_extent_inline_ref *iref; | |
2293 | struct btrfs_extent_item *ei; | |
f321e491 | 2294 | struct btrfs_key key; |
5d4f98a2 | 2295 | u32 item_size; |
3de28d57 | 2296 | int type; |
be20aa9d | 2297 | int ret; |
925baedd | 2298 | |
be20aa9d | 2299 | key.objectid = bytenr; |
31840ae1 | 2300 | key.offset = (u64)-1; |
f321e491 | 2301 | key.type = BTRFS_EXTENT_ITEM_KEY; |
be20aa9d | 2302 | |
be20aa9d CM |
2303 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
2304 | if (ret < 0) | |
2305 | goto out; | |
79787eaa | 2306 | BUG_ON(ret == 0); /* Corruption */ |
80ff3856 YZ |
2307 | |
2308 | ret = -ENOENT; | |
2309 | if (path->slots[0] == 0) | |
31840ae1 | 2310 | goto out; |
be20aa9d | 2311 | |
31840ae1 | 2312 | path->slots[0]--; |
f321e491 | 2313 | leaf = path->nodes[0]; |
5d4f98a2 | 2314 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
be20aa9d | 2315 | |
5d4f98a2 | 2316 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
be20aa9d | 2317 | goto out; |
f321e491 | 2318 | |
5d4f98a2 YZ |
2319 | ret = 1; |
2320 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
5d4f98a2 | 2321 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
bd09835d | 2322 | |
a6bd9cd1 | 2323 | /* If extent item has more than 1 inline ref then it's shared */ |
5d4f98a2 YZ |
2324 | if (item_size != sizeof(*ei) + |
2325 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) | |
2326 | goto out; | |
be20aa9d | 2327 | |
a84d5d42 BB |
2328 | /* |
2329 | * If extent created before last snapshot => it's shared unless the | |
2330 | * snapshot has been deleted. Use the heuristic if strict is false. | |
2331 | */ | |
2332 | if (!strict && | |
2333 | (btrfs_extent_generation(leaf, ei) <= | |
2334 | btrfs_root_last_snapshot(&root->root_item))) | |
5d4f98a2 YZ |
2335 | goto out; |
2336 | ||
2337 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); | |
3de28d57 | 2338 | |
a6bd9cd1 | 2339 | /* If this extent has SHARED_DATA_REF then it's shared */ |
3de28d57 LB |
2340 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
2341 | if (type != BTRFS_EXTENT_DATA_REF_KEY) | |
5d4f98a2 YZ |
2342 | goto out; |
2343 | ||
2344 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
2345 | if (btrfs_extent_refs(leaf, ei) != | |
2346 | btrfs_extent_data_ref_count(leaf, ref) || | |
2347 | btrfs_extent_data_ref_root(leaf, ref) != | |
2348 | root->root_key.objectid || | |
2349 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || | |
2350 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
2351 | goto out; | |
2352 | ||
2353 | ret = 0; | |
2354 | out: | |
2355 | return ret; | |
2356 | } | |
2357 | ||
e4c3b2dc | 2358 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
a84d5d42 | 2359 | u64 bytenr, bool strict) |
5d4f98a2 YZ |
2360 | { |
2361 | struct btrfs_path *path; | |
2362 | int ret; | |
5d4f98a2 YZ |
2363 | |
2364 | path = btrfs_alloc_path(); | |
2365 | if (!path) | |
9132c4ff | 2366 | return -ENOMEM; |
5d4f98a2 YZ |
2367 | |
2368 | do { | |
e4c3b2dc | 2369 | ret = check_committed_ref(root, path, objectid, |
a84d5d42 | 2370 | offset, bytenr, strict); |
5d4f98a2 | 2371 | if (ret && ret != -ENOENT) |
f321e491 | 2372 | goto out; |
80ff3856 | 2373 | |
380fd066 MT |
2374 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
2375 | } while (ret == -EAGAIN); | |
5d4f98a2 | 2376 | |
be20aa9d | 2377 | out: |
80ff3856 | 2378 | btrfs_free_path(path); |
37f00a6d | 2379 | if (btrfs_is_data_reloc_root(root)) |
f0486c68 | 2380 | WARN_ON(ret > 0); |
f321e491 | 2381 | return ret; |
be20aa9d | 2382 | } |
c5739bba | 2383 | |
5d4f98a2 | 2384 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
b7a9f29f | 2385 | struct btrfs_root *root, |
5d4f98a2 | 2386 | struct extent_buffer *buf, |
e339a6b0 | 2387 | int full_backref, int inc) |
31840ae1 | 2388 | { |
0b246afa | 2389 | struct btrfs_fs_info *fs_info = root->fs_info; |
31840ae1 | 2390 | u64 bytenr; |
5d4f98a2 YZ |
2391 | u64 num_bytes; |
2392 | u64 parent; | |
31840ae1 | 2393 | u64 ref_root; |
31840ae1 | 2394 | u32 nritems; |
31840ae1 ZY |
2395 | struct btrfs_key key; |
2396 | struct btrfs_file_extent_item *fi; | |
82fa113f QW |
2397 | struct btrfs_ref generic_ref = { 0 }; |
2398 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); | |
31840ae1 | 2399 | int i; |
82fa113f | 2400 | int action; |
31840ae1 ZY |
2401 | int level; |
2402 | int ret = 0; | |
fccb84c9 | 2403 | |
0b246afa | 2404 | if (btrfs_is_testing(fs_info)) |
faa2dbf0 | 2405 | return 0; |
fccb84c9 | 2406 | |
31840ae1 | 2407 | ref_root = btrfs_header_owner(buf); |
31840ae1 ZY |
2408 | nritems = btrfs_header_nritems(buf); |
2409 | level = btrfs_header_level(buf); | |
2410 | ||
92a7cc42 | 2411 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0) |
5d4f98a2 | 2412 | return 0; |
31840ae1 | 2413 | |
5d4f98a2 YZ |
2414 | if (full_backref) |
2415 | parent = buf->start; | |
2416 | else | |
2417 | parent = 0; | |
82fa113f QW |
2418 | if (inc) |
2419 | action = BTRFS_ADD_DELAYED_REF; | |
2420 | else | |
2421 | action = BTRFS_DROP_DELAYED_REF; | |
5d4f98a2 YZ |
2422 | |
2423 | for (i = 0; i < nritems; i++) { | |
31840ae1 | 2424 | if (level == 0) { |
5d4f98a2 | 2425 | btrfs_item_key_to_cpu(buf, &key, i); |
962a298f | 2426 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
31840ae1 | 2427 | continue; |
5d4f98a2 | 2428 | fi = btrfs_item_ptr(buf, i, |
31840ae1 ZY |
2429 | struct btrfs_file_extent_item); |
2430 | if (btrfs_file_extent_type(buf, fi) == | |
2431 | BTRFS_FILE_EXTENT_INLINE) | |
2432 | continue; | |
2433 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
2434 | if (bytenr == 0) | |
2435 | continue; | |
5d4f98a2 YZ |
2436 | |
2437 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); | |
2438 | key.offset -= btrfs_file_extent_offset(buf, fi); | |
82fa113f QW |
2439 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
2440 | num_bytes, parent); | |
2441 | generic_ref.real_root = root->root_key.objectid; | |
2442 | btrfs_init_data_ref(&generic_ref, ref_root, key.objectid, | |
2443 | key.offset); | |
2444 | generic_ref.skip_qgroup = for_reloc; | |
dd28b6a5 | 2445 | if (inc) |
82fa113f | 2446 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
dd28b6a5 | 2447 | else |
ffd4bb2a | 2448 | ret = btrfs_free_extent(trans, &generic_ref); |
31840ae1 ZY |
2449 | if (ret) |
2450 | goto fail; | |
2451 | } else { | |
5d4f98a2 | 2452 | bytenr = btrfs_node_blockptr(buf, i); |
0b246afa | 2453 | num_bytes = fs_info->nodesize; |
82fa113f QW |
2454 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
2455 | num_bytes, parent); | |
2456 | generic_ref.real_root = root->root_key.objectid; | |
2457 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); | |
2458 | generic_ref.skip_qgroup = for_reloc; | |
dd28b6a5 | 2459 | if (inc) |
82fa113f | 2460 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
dd28b6a5 | 2461 | else |
ffd4bb2a | 2462 | ret = btrfs_free_extent(trans, &generic_ref); |
31840ae1 ZY |
2463 | if (ret) |
2464 | goto fail; | |
2465 | } | |
2466 | } | |
2467 | return 0; | |
2468 | fail: | |
5d4f98a2 YZ |
2469 | return ret; |
2470 | } | |
2471 | ||
2472 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
e339a6b0 | 2473 | struct extent_buffer *buf, int full_backref) |
5d4f98a2 | 2474 | { |
e339a6b0 | 2475 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
5d4f98a2 YZ |
2476 | } |
2477 | ||
2478 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
e339a6b0 | 2479 | struct extent_buffer *buf, int full_backref) |
5d4f98a2 | 2480 | { |
e339a6b0 | 2481 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
31840ae1 ZY |
2482 | } |
2483 | ||
1b86826d | 2484 | static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data) |
9ed74f2d | 2485 | { |
0b246afa | 2486 | struct btrfs_fs_info *fs_info = root->fs_info; |
b742bb82 | 2487 | u64 flags; |
53b381b3 | 2488 | u64 ret; |
9ed74f2d | 2489 | |
b742bb82 YZ |
2490 | if (data) |
2491 | flags = BTRFS_BLOCK_GROUP_DATA; | |
0b246afa | 2492 | else if (root == fs_info->chunk_root) |
b742bb82 | 2493 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
9ed74f2d | 2494 | else |
b742bb82 | 2495 | flags = BTRFS_BLOCK_GROUP_METADATA; |
9ed74f2d | 2496 | |
878d7b67 | 2497 | ret = btrfs_get_alloc_profile(fs_info, flags); |
53b381b3 | 2498 | return ret; |
6a63209f | 2499 | } |
9ed74f2d | 2500 | |
2ff7e61e | 2501 | static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start) |
a061fc8d | 2502 | { |
32da5386 | 2503 | struct btrfs_block_group *cache; |
d2fb3437 | 2504 | u64 bytenr; |
0f9dd46c | 2505 | |
0b246afa JM |
2506 | spin_lock(&fs_info->block_group_cache_lock); |
2507 | bytenr = fs_info->first_logical_byte; | |
2508 | spin_unlock(&fs_info->block_group_cache_lock); | |
a1897fdd LB |
2509 | |
2510 | if (bytenr < (u64)-1) | |
2511 | return bytenr; | |
2512 | ||
0b246afa | 2513 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
0f9dd46c | 2514 | if (!cache) |
a061fc8d | 2515 | return 0; |
0f9dd46c | 2516 | |
b3470b5d | 2517 | bytenr = cache->start; |
fa9c0d79 | 2518 | btrfs_put_block_group(cache); |
d2fb3437 YZ |
2519 | |
2520 | return bytenr; | |
a061fc8d CM |
2521 | } |
2522 | ||
6690d071 NB |
2523 | static int pin_down_extent(struct btrfs_trans_handle *trans, |
2524 | struct btrfs_block_group *cache, | |
f0486c68 | 2525 | u64 bytenr, u64 num_bytes, int reserved) |
324ae4df | 2526 | { |
fdf08605 DS |
2527 | struct btrfs_fs_info *fs_info = cache->fs_info; |
2528 | ||
11833d66 YZ |
2529 | spin_lock(&cache->space_info->lock); |
2530 | spin_lock(&cache->lock); | |
2531 | cache->pinned += num_bytes; | |
bb96c4e5 JB |
2532 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
2533 | num_bytes); | |
11833d66 YZ |
2534 | if (reserved) { |
2535 | cache->reserved -= num_bytes; | |
2536 | cache->space_info->bytes_reserved -= num_bytes; | |
2537 | } | |
2538 | spin_unlock(&cache->lock); | |
2539 | spin_unlock(&cache->space_info->lock); | |
68b38550 | 2540 | |
fe119a6e | 2541 | set_extent_dirty(&trans->transaction->pinned_extents, bytenr, |
f0486c68 YZ |
2542 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
2543 | return 0; | |
2544 | } | |
68b38550 | 2545 | |
b25c36f8 | 2546 | int btrfs_pin_extent(struct btrfs_trans_handle *trans, |
f0486c68 YZ |
2547 | u64 bytenr, u64 num_bytes, int reserved) |
2548 | { | |
32da5386 | 2549 | struct btrfs_block_group *cache; |
68b38550 | 2550 | |
b25c36f8 | 2551 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
79787eaa | 2552 | BUG_ON(!cache); /* Logic error */ |
f0486c68 | 2553 | |
6690d071 | 2554 | pin_down_extent(trans, cache, bytenr, num_bytes, reserved); |
f0486c68 YZ |
2555 | |
2556 | btrfs_put_block_group(cache); | |
11833d66 YZ |
2557 | return 0; |
2558 | } | |
2559 | ||
f0486c68 | 2560 | /* |
e688b725 CM |
2561 | * this function must be called within transaction |
2562 | */ | |
9fce5704 | 2563 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, |
e688b725 CM |
2564 | u64 bytenr, u64 num_bytes) |
2565 | { | |
32da5386 | 2566 | struct btrfs_block_group *cache; |
b50c6e25 | 2567 | int ret; |
e688b725 | 2568 | |
9fce5704 | 2569 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
b50c6e25 JB |
2570 | if (!cache) |
2571 | return -EINVAL; | |
e688b725 CM |
2572 | |
2573 | /* | |
2574 | * pull in the free space cache (if any) so that our pin | |
2575 | * removes the free space from the cache. We have load_only set | |
2576 | * to one because the slow code to read in the free extents does check | |
2577 | * the pinned extents. | |
2578 | */ | |
676f1f75 | 2579 | btrfs_cache_block_group(cache, 1); |
9ad6d91f FM |
2580 | /* |
2581 | * Make sure we wait until the cache is completely built in case it is | |
2582 | * missing or is invalid and therefore needs to be rebuilt. | |
2583 | */ | |
2584 | ret = btrfs_wait_block_group_cache_done(cache); | |
2585 | if (ret) | |
2586 | goto out; | |
e688b725 | 2587 | |
6690d071 | 2588 | pin_down_extent(trans, cache, bytenr, num_bytes, 0); |
e688b725 CM |
2589 | |
2590 | /* remove us from the free space cache (if we're there at all) */ | |
b50c6e25 | 2591 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
9ad6d91f | 2592 | out: |
e688b725 | 2593 | btrfs_put_block_group(cache); |
b50c6e25 | 2594 | return ret; |
e688b725 CM |
2595 | } |
2596 | ||
2ff7e61e JM |
2597 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
2598 | u64 start, u64 num_bytes) | |
8c2a1a30 JB |
2599 | { |
2600 | int ret; | |
32da5386 | 2601 | struct btrfs_block_group *block_group; |
8c2a1a30 | 2602 | |
0b246afa | 2603 | block_group = btrfs_lookup_block_group(fs_info, start); |
8c2a1a30 JB |
2604 | if (!block_group) |
2605 | return -EINVAL; | |
2606 | ||
9ad6d91f FM |
2607 | btrfs_cache_block_group(block_group, 1); |
2608 | /* | |
2609 | * Make sure we wait until the cache is completely built in case it is | |
2610 | * missing or is invalid and therefore needs to be rebuilt. | |
2611 | */ | |
2612 | ret = btrfs_wait_block_group_cache_done(block_group); | |
2613 | if (ret) | |
2614 | goto out; | |
8c2a1a30 | 2615 | |
9ad6d91f FM |
2616 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
2617 | out: | |
8c2a1a30 JB |
2618 | btrfs_put_block_group(block_group); |
2619 | return ret; | |
2620 | } | |
2621 | ||
bcdc428c | 2622 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
8c2a1a30 | 2623 | { |
bcdc428c | 2624 | struct btrfs_fs_info *fs_info = eb->fs_info; |
8c2a1a30 JB |
2625 | struct btrfs_file_extent_item *item; |
2626 | struct btrfs_key key; | |
2627 | int found_type; | |
2628 | int i; | |
b89311ef | 2629 | int ret = 0; |
8c2a1a30 | 2630 | |
2ff7e61e | 2631 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
8c2a1a30 JB |
2632 | return 0; |
2633 | ||
2634 | for (i = 0; i < btrfs_header_nritems(eb); i++) { | |
2635 | btrfs_item_key_to_cpu(eb, &key, i); | |
2636 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2637 | continue; | |
2638 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); | |
2639 | found_type = btrfs_file_extent_type(eb, item); | |
2640 | if (found_type == BTRFS_FILE_EXTENT_INLINE) | |
2641 | continue; | |
2642 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | |
2643 | continue; | |
2644 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
2645 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
b89311ef GJ |
2646 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
2647 | if (ret) | |
2648 | break; | |
8c2a1a30 JB |
2649 | } |
2650 | ||
b89311ef | 2651 | return ret; |
8c2a1a30 JB |
2652 | } |
2653 | ||
9cfa3e34 | 2654 | static void |
32da5386 | 2655 | btrfs_inc_block_group_reservations(struct btrfs_block_group *bg) |
9cfa3e34 FM |
2656 | { |
2657 | atomic_inc(&bg->reservations); | |
2658 | } | |
2659 | ||
c759c4e1 JB |
2660 | /* |
2661 | * Returns the free cluster for the given space info and sets empty_cluster to | |
2662 | * what it should be based on the mount options. | |
2663 | */ | |
2664 | static struct btrfs_free_cluster * | |
2ff7e61e JM |
2665 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
2666 | struct btrfs_space_info *space_info, u64 *empty_cluster) | |
c759c4e1 JB |
2667 | { |
2668 | struct btrfs_free_cluster *ret = NULL; | |
c759c4e1 JB |
2669 | |
2670 | *empty_cluster = 0; | |
2671 | if (btrfs_mixed_space_info(space_info)) | |
2672 | return ret; | |
2673 | ||
c759c4e1 | 2674 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
0b246afa | 2675 | ret = &fs_info->meta_alloc_cluster; |
583b7231 HK |
2676 | if (btrfs_test_opt(fs_info, SSD)) |
2677 | *empty_cluster = SZ_2M; | |
2678 | else | |
ee22184b | 2679 | *empty_cluster = SZ_64K; |
583b7231 HK |
2680 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
2681 | btrfs_test_opt(fs_info, SSD_SPREAD)) { | |
2682 | *empty_cluster = SZ_2M; | |
0b246afa | 2683 | ret = &fs_info->data_alloc_cluster; |
c759c4e1 JB |
2684 | } |
2685 | ||
2686 | return ret; | |
2687 | } | |
2688 | ||
2ff7e61e JM |
2689 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
2690 | u64 start, u64 end, | |
678886bd | 2691 | const bool return_free_space) |
ccd467d6 | 2692 | { |
32da5386 | 2693 | struct btrfs_block_group *cache = NULL; |
7b398f8e JB |
2694 | struct btrfs_space_info *space_info; |
2695 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
c759c4e1 | 2696 | struct btrfs_free_cluster *cluster = NULL; |
11833d66 | 2697 | u64 len; |
c759c4e1 JB |
2698 | u64 total_unpinned = 0; |
2699 | u64 empty_cluster = 0; | |
7b398f8e | 2700 | bool readonly; |
ccd467d6 | 2701 | |
11833d66 | 2702 | while (start <= end) { |
7b398f8e | 2703 | readonly = false; |
11833d66 | 2704 | if (!cache || |
b3470b5d | 2705 | start >= cache->start + cache->length) { |
11833d66 YZ |
2706 | if (cache) |
2707 | btrfs_put_block_group(cache); | |
c759c4e1 | 2708 | total_unpinned = 0; |
11833d66 | 2709 | cache = btrfs_lookup_block_group(fs_info, start); |
79787eaa | 2710 | BUG_ON(!cache); /* Logic error */ |
c759c4e1 | 2711 | |
2ff7e61e | 2712 | cluster = fetch_cluster_info(fs_info, |
c759c4e1 JB |
2713 | cache->space_info, |
2714 | &empty_cluster); | |
2715 | empty_cluster <<= 1; | |
11833d66 YZ |
2716 | } |
2717 | ||
b3470b5d | 2718 | len = cache->start + cache->length - start; |
11833d66 YZ |
2719 | len = min(len, end + 1 - start); |
2720 | ||
2ca08c56 | 2721 | down_read(&fs_info->commit_root_sem); |
9076dbd5 JB |
2722 | if (start < cache->last_byte_to_unpin && return_free_space) { |
2723 | u64 add_len = min(len, cache->last_byte_to_unpin - start); | |
2724 | ||
2725 | btrfs_add_free_space(cache, start, add_len); | |
11833d66 | 2726 | } |
2ca08c56 | 2727 | up_read(&fs_info->commit_root_sem); |
11833d66 | 2728 | |
f0486c68 | 2729 | start += len; |
c759c4e1 | 2730 | total_unpinned += len; |
7b398f8e | 2731 | space_info = cache->space_info; |
f0486c68 | 2732 | |
c759c4e1 JB |
2733 | /* |
2734 | * If this space cluster has been marked as fragmented and we've | |
2735 | * unpinned enough in this block group to potentially allow a | |
2736 | * cluster to be created inside of it go ahead and clear the | |
2737 | * fragmented check. | |
2738 | */ | |
2739 | if (cluster && cluster->fragmented && | |
2740 | total_unpinned > empty_cluster) { | |
2741 | spin_lock(&cluster->lock); | |
2742 | cluster->fragmented = 0; | |
2743 | spin_unlock(&cluster->lock); | |
2744 | } | |
2745 | ||
7b398f8e | 2746 | spin_lock(&space_info->lock); |
11833d66 YZ |
2747 | spin_lock(&cache->lock); |
2748 | cache->pinned -= len; | |
bb96c4e5 | 2749 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
4f4db217 | 2750 | space_info->max_extent_size = 0; |
7b398f8e JB |
2751 | if (cache->ro) { |
2752 | space_info->bytes_readonly += len; | |
2753 | readonly = true; | |
169e0da9 NA |
2754 | } else if (btrfs_is_zoned(fs_info)) { |
2755 | /* Need reset before reusing in a zoned block group */ | |
2756 | space_info->bytes_zone_unusable += len; | |
2757 | readonly = true; | |
7b398f8e | 2758 | } |
11833d66 | 2759 | spin_unlock(&cache->lock); |
957780eb JB |
2760 | if (!readonly && return_free_space && |
2761 | global_rsv->space_info == space_info) { | |
2762 | u64 to_add = len; | |
92ac58ec | 2763 | |
7b398f8e JB |
2764 | spin_lock(&global_rsv->lock); |
2765 | if (!global_rsv->full) { | |
957780eb JB |
2766 | to_add = min(len, global_rsv->size - |
2767 | global_rsv->reserved); | |
2768 | global_rsv->reserved += to_add; | |
bb96c4e5 JB |
2769 | btrfs_space_info_update_bytes_may_use(fs_info, |
2770 | space_info, to_add); | |
7b398f8e JB |
2771 | if (global_rsv->reserved >= global_rsv->size) |
2772 | global_rsv->full = 1; | |
957780eb | 2773 | len -= to_add; |
7b398f8e JB |
2774 | } |
2775 | spin_unlock(&global_rsv->lock); | |
2776 | } | |
2732798c JB |
2777 | /* Add to any tickets we may have */ |
2778 | if (!readonly && return_free_space && len) | |
2779 | btrfs_try_granting_tickets(fs_info, space_info); | |
7b398f8e | 2780 | spin_unlock(&space_info->lock); |
ccd467d6 | 2781 | } |
11833d66 YZ |
2782 | |
2783 | if (cache) | |
2784 | btrfs_put_block_group(cache); | |
ccd467d6 CM |
2785 | return 0; |
2786 | } | |
2787 | ||
5ead2dd0 | 2788 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
a28ec197 | 2789 | { |
5ead2dd0 | 2790 | struct btrfs_fs_info *fs_info = trans->fs_info; |
32da5386 | 2791 | struct btrfs_block_group *block_group, *tmp; |
e33e17ee | 2792 | struct list_head *deleted_bgs; |
11833d66 | 2793 | struct extent_io_tree *unpin; |
1a5bc167 CM |
2794 | u64 start; |
2795 | u64 end; | |
a28ec197 | 2796 | int ret; |
a28ec197 | 2797 | |
fe119a6e | 2798 | unpin = &trans->transaction->pinned_extents; |
11833d66 | 2799 | |
bf31f87f | 2800 | while (!TRANS_ABORTED(trans)) { |
0e6ec385 FM |
2801 | struct extent_state *cached_state = NULL; |
2802 | ||
d4b450cd | 2803 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
1a5bc167 | 2804 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
0e6ec385 | 2805 | EXTENT_DIRTY, &cached_state); |
d4b450cd FM |
2806 | if (ret) { |
2807 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
a28ec197 | 2808 | break; |
d4b450cd | 2809 | } |
1f3c79a2 | 2810 | |
46b27f50 | 2811 | if (btrfs_test_opt(fs_info, DISCARD_SYNC)) |
2ff7e61e | 2812 | ret = btrfs_discard_extent(fs_info, start, |
5378e607 | 2813 | end + 1 - start, NULL); |
1f3c79a2 | 2814 | |
0e6ec385 | 2815 | clear_extent_dirty(unpin, start, end, &cached_state); |
2ff7e61e | 2816 | unpin_extent_range(fs_info, start, end, true); |
d4b450cd | 2817 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
0e6ec385 | 2818 | free_extent_state(cached_state); |
b9473439 | 2819 | cond_resched(); |
a28ec197 | 2820 | } |
817d52f8 | 2821 | |
a2309300 DZ |
2822 | if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) { |
2823 | btrfs_discard_calc_delay(&fs_info->discard_ctl); | |
b0643e59 | 2824 | btrfs_discard_schedule_work(&fs_info->discard_ctl, true); |
a2309300 | 2825 | } |
b0643e59 | 2826 | |
e33e17ee JM |
2827 | /* |
2828 | * Transaction is finished. We don't need the lock anymore. We | |
2829 | * do need to clean up the block groups in case of a transaction | |
2830 | * abort. | |
2831 | */ | |
2832 | deleted_bgs = &trans->transaction->deleted_bgs; | |
2833 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { | |
2834 | u64 trimmed = 0; | |
2835 | ||
2836 | ret = -EROFS; | |
bf31f87f | 2837 | if (!TRANS_ABORTED(trans)) |
2ff7e61e | 2838 | ret = btrfs_discard_extent(fs_info, |
b3470b5d DS |
2839 | block_group->start, |
2840 | block_group->length, | |
e33e17ee JM |
2841 | &trimmed); |
2842 | ||
2843 | list_del_init(&block_group->bg_list); | |
6b7304af | 2844 | btrfs_unfreeze_block_group(block_group); |
e33e17ee JM |
2845 | btrfs_put_block_group(block_group); |
2846 | ||
2847 | if (ret) { | |
2848 | const char *errstr = btrfs_decode_error(ret); | |
2849 | btrfs_warn(fs_info, | |
913e1535 | 2850 | "discard failed while removing blockgroup: errno=%d %s", |
e33e17ee JM |
2851 | ret, errstr); |
2852 | } | |
2853 | } | |
2854 | ||
e20d96d6 CM |
2855 | return 0; |
2856 | } | |
2857 | ||
1c2a07f5 QW |
2858 | /* |
2859 | * Drop one or more refs of @node. | |
2860 | * | |
2861 | * 1. Locate the extent refs. | |
2862 | * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item. | |
2863 | * Locate it, then reduce the refs number or remove the ref line completely. | |
2864 | * | |
2865 | * 2. Update the refs count in EXTENT/METADATA_ITEM | |
2866 | * | |
2867 | * Inline backref case: | |
2868 | * | |
2869 | * in extent tree we have: | |
2870 | * | |
2871 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 | |
2872 | * refs 2 gen 6 flags DATA | |
2873 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 | |
2874 | * extent data backref root FS_TREE objectid 257 offset 0 count 1 | |
2875 | * | |
2876 | * This function gets called with: | |
2877 | * | |
2878 | * node->bytenr = 13631488 | |
2879 | * node->num_bytes = 1048576 | |
2880 | * root_objectid = FS_TREE | |
2881 | * owner_objectid = 257 | |
2882 | * owner_offset = 0 | |
2883 | * refs_to_drop = 1 | |
2884 | * | |
2885 | * Then we should get some like: | |
2886 | * | |
2887 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 | |
2888 | * refs 1 gen 6 flags DATA | |
2889 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 | |
2890 | * | |
2891 | * Keyed backref case: | |
2892 | * | |
2893 | * in extent tree we have: | |
2894 | * | |
2895 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 | |
2896 | * refs 754 gen 6 flags DATA | |
2897 | * [...] | |
2898 | * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28 | |
2899 | * extent data backref root FS_TREE objectid 866 offset 0 count 1 | |
2900 | * | |
2901 | * This function get called with: | |
2902 | * | |
2903 | * node->bytenr = 13631488 | |
2904 | * node->num_bytes = 1048576 | |
2905 | * root_objectid = FS_TREE | |
2906 | * owner_objectid = 866 | |
2907 | * owner_offset = 0 | |
2908 | * refs_to_drop = 1 | |
2909 | * | |
2910 | * Then we should get some like: | |
2911 | * | |
2912 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 | |
2913 | * refs 753 gen 6 flags DATA | |
2914 | * | |
2915 | * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed. | |
2916 | */ | |
5d4f98a2 | 2917 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
e72cb923 NB |
2918 | struct btrfs_delayed_ref_node *node, u64 parent, |
2919 | u64 root_objectid, u64 owner_objectid, | |
2920 | u64 owner_offset, int refs_to_drop, | |
2921 | struct btrfs_delayed_extent_op *extent_op) | |
a28ec197 | 2922 | { |
e72cb923 | 2923 | struct btrfs_fs_info *info = trans->fs_info; |
e2fa7227 | 2924 | struct btrfs_key key; |
5d4f98a2 | 2925 | struct btrfs_path *path; |
1261ec42 | 2926 | struct btrfs_root *extent_root = info->extent_root; |
5f39d397 | 2927 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
2928 | struct btrfs_extent_item *ei; |
2929 | struct btrfs_extent_inline_ref *iref; | |
a28ec197 | 2930 | int ret; |
5d4f98a2 | 2931 | int is_data; |
952fccac CM |
2932 | int extent_slot = 0; |
2933 | int found_extent = 0; | |
2934 | int num_to_del = 1; | |
5d4f98a2 YZ |
2935 | u32 item_size; |
2936 | u64 refs; | |
c682f9b3 QW |
2937 | u64 bytenr = node->bytenr; |
2938 | u64 num_bytes = node->num_bytes; | |
fcebe456 | 2939 | int last_ref = 0; |
0b246afa | 2940 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
037e6390 | 2941 | |
5caf2a00 | 2942 | path = btrfs_alloc_path(); |
54aa1f4d CM |
2943 | if (!path) |
2944 | return -ENOMEM; | |
5f26f772 | 2945 | |
5d4f98a2 | 2946 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
1c2a07f5 QW |
2947 | |
2948 | if (!is_data && refs_to_drop != 1) { | |
2949 | btrfs_crit(info, | |
2950 | "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u", | |
2951 | node->bytenr, refs_to_drop); | |
2952 | ret = -EINVAL; | |
2953 | btrfs_abort_transaction(trans, ret); | |
2954 | goto out; | |
2955 | } | |
5d4f98a2 | 2956 | |
3173a18f | 2957 | if (is_data) |
897ca819 | 2958 | skinny_metadata = false; |
3173a18f | 2959 | |
fbe4801b NB |
2960 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
2961 | parent, root_objectid, owner_objectid, | |
5d4f98a2 | 2962 | owner_offset); |
7bb86316 | 2963 | if (ret == 0) { |
1c2a07f5 QW |
2964 | /* |
2965 | * Either the inline backref or the SHARED_DATA_REF/ | |
2966 | * SHARED_BLOCK_REF is found | |
2967 | * | |
2968 | * Here is a quick path to locate EXTENT/METADATA_ITEM. | |
2969 | * It's possible the EXTENT/METADATA_ITEM is near current slot. | |
2970 | */ | |
952fccac | 2971 | extent_slot = path->slots[0]; |
5d4f98a2 YZ |
2972 | while (extent_slot >= 0) { |
2973 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
952fccac | 2974 | extent_slot); |
5d4f98a2 | 2975 | if (key.objectid != bytenr) |
952fccac | 2976 | break; |
5d4f98a2 YZ |
2977 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
2978 | key.offset == num_bytes) { | |
952fccac CM |
2979 | found_extent = 1; |
2980 | break; | |
2981 | } | |
3173a18f JB |
2982 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
2983 | key.offset == owner_objectid) { | |
2984 | found_extent = 1; | |
2985 | break; | |
2986 | } | |
1c2a07f5 QW |
2987 | |
2988 | /* Quick path didn't find the EXTEMT/METADATA_ITEM */ | |
952fccac CM |
2989 | if (path->slots[0] - extent_slot > 5) |
2990 | break; | |
5d4f98a2 | 2991 | extent_slot--; |
952fccac | 2992 | } |
a79865c6 | 2993 | |
31840ae1 | 2994 | if (!found_extent) { |
1c2a07f5 QW |
2995 | if (iref) { |
2996 | btrfs_crit(info, | |
2997 | "invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref"); | |
2998 | btrfs_abort_transaction(trans, -EUCLEAN); | |
2999 | goto err_dump; | |
3000 | } | |
3001 | /* Must be SHARED_* item, remove the backref first */ | |
87cc7a8a | 3002 | ret = remove_extent_backref(trans, path, NULL, |
87bde3cd | 3003 | refs_to_drop, |
fcebe456 | 3004 | is_data, &last_ref); |
005d6427 | 3005 | if (ret) { |
66642832 | 3006 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3007 | goto out; |
3008 | } | |
b3b4aa74 | 3009 | btrfs_release_path(path); |
5d4f98a2 | 3010 | |
1c2a07f5 | 3011 | /* Slow path to locate EXTENT/METADATA_ITEM */ |
5d4f98a2 YZ |
3012 | key.objectid = bytenr; |
3013 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
3014 | key.offset = num_bytes; | |
3015 | ||
3173a18f JB |
3016 | if (!is_data && skinny_metadata) { |
3017 | key.type = BTRFS_METADATA_ITEM_KEY; | |
3018 | key.offset = owner_objectid; | |
3019 | } | |
3020 | ||
31840ae1 ZY |
3021 | ret = btrfs_search_slot(trans, extent_root, |
3022 | &key, path, -1, 1); | |
3173a18f JB |
3023 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
3024 | /* | |
3025 | * Couldn't find our skinny metadata item, | |
3026 | * see if we have ye olde extent item. | |
3027 | */ | |
3028 | path->slots[0]--; | |
3029 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3030 | path->slots[0]); | |
3031 | if (key.objectid == bytenr && | |
3032 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
3033 | key.offset == num_bytes) | |
3034 | ret = 0; | |
3035 | } | |
3036 | ||
3037 | if (ret > 0 && skinny_metadata) { | |
3038 | skinny_metadata = false; | |
9ce49a0b | 3039 | key.objectid = bytenr; |
3173a18f JB |
3040 | key.type = BTRFS_EXTENT_ITEM_KEY; |
3041 | key.offset = num_bytes; | |
3042 | btrfs_release_path(path); | |
3043 | ret = btrfs_search_slot(trans, extent_root, | |
3044 | &key, path, -1, 1); | |
3045 | } | |
3046 | ||
f3465ca4 | 3047 | if (ret) { |
5d163e0e JM |
3048 | btrfs_err(info, |
3049 | "umm, got %d back from search, was looking for %llu", | |
3050 | ret, bytenr); | |
b783e62d | 3051 | if (ret > 0) |
a4f78750 | 3052 | btrfs_print_leaf(path->nodes[0]); |
f3465ca4 | 3053 | } |
005d6427 | 3054 | if (ret < 0) { |
66642832 | 3055 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3056 | goto out; |
3057 | } | |
31840ae1 ZY |
3058 | extent_slot = path->slots[0]; |
3059 | } | |
fae7f21c | 3060 | } else if (WARN_ON(ret == -ENOENT)) { |
a4f78750 | 3061 | btrfs_print_leaf(path->nodes[0]); |
c2cf52eb SK |
3062 | btrfs_err(info, |
3063 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", | |
c1c9ff7c GU |
3064 | bytenr, parent, root_objectid, owner_objectid, |
3065 | owner_offset); | |
66642832 | 3066 | btrfs_abort_transaction(trans, ret); |
c4a050bb | 3067 | goto out; |
79787eaa | 3068 | } else { |
66642832 | 3069 | btrfs_abort_transaction(trans, ret); |
005d6427 | 3070 | goto out; |
7bb86316 | 3071 | } |
5f39d397 CM |
3072 | |
3073 | leaf = path->nodes[0]; | |
5d4f98a2 | 3074 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
6d8ff4e4 | 3075 | if (unlikely(item_size < sizeof(*ei))) { |
ba3c2b19 NB |
3076 | ret = -EINVAL; |
3077 | btrfs_print_v0_err(info); | |
3078 | btrfs_abort_transaction(trans, ret); | |
3079 | goto out; | |
3080 | } | |
952fccac | 3081 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 3082 | struct btrfs_extent_item); |
3173a18f JB |
3083 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
3084 | key.type == BTRFS_EXTENT_ITEM_KEY) { | |
5d4f98a2 | 3085 | struct btrfs_tree_block_info *bi; |
1c2a07f5 QW |
3086 | if (item_size < sizeof(*ei) + sizeof(*bi)) { |
3087 | btrfs_crit(info, | |
cad69d13 | 3088 | "invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >= %zu", |
1c2a07f5 QW |
3089 | key.objectid, key.type, key.offset, |
3090 | owner_objectid, item_size, | |
3091 | sizeof(*ei) + sizeof(*bi)); | |
3092 | btrfs_abort_transaction(trans, -EUCLEAN); | |
3093 | goto err_dump; | |
3094 | } | |
5d4f98a2 YZ |
3095 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
3096 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); | |
3097 | } | |
56bec294 | 3098 | |
5d4f98a2 | 3099 | refs = btrfs_extent_refs(leaf, ei); |
32b02538 | 3100 | if (refs < refs_to_drop) { |
1c2a07f5 QW |
3101 | btrfs_crit(info, |
3102 | "trying to drop %d refs but we only have %llu for bytenr %llu", | |
5d163e0e | 3103 | refs_to_drop, refs, bytenr); |
1c2a07f5 QW |
3104 | btrfs_abort_transaction(trans, -EUCLEAN); |
3105 | goto err_dump; | |
32b02538 | 3106 | } |
56bec294 | 3107 | refs -= refs_to_drop; |
5f39d397 | 3108 | |
5d4f98a2 YZ |
3109 | if (refs > 0) { |
3110 | if (extent_op) | |
3111 | __run_delayed_extent_op(extent_op, leaf, ei); | |
3112 | /* | |
3113 | * In the case of inline back ref, reference count will | |
3114 | * be updated by remove_extent_backref | |
952fccac | 3115 | */ |
5d4f98a2 | 3116 | if (iref) { |
1c2a07f5 QW |
3117 | if (!found_extent) { |
3118 | btrfs_crit(info, | |
3119 | "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found"); | |
3120 | btrfs_abort_transaction(trans, -EUCLEAN); | |
3121 | goto err_dump; | |
3122 | } | |
5d4f98a2 YZ |
3123 | } else { |
3124 | btrfs_set_extent_refs(leaf, ei, refs); | |
3125 | btrfs_mark_buffer_dirty(leaf); | |
3126 | } | |
3127 | if (found_extent) { | |
87cc7a8a NB |
3128 | ret = remove_extent_backref(trans, path, iref, |
3129 | refs_to_drop, is_data, | |
3130 | &last_ref); | |
005d6427 | 3131 | if (ret) { |
66642832 | 3132 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3133 | goto out; |
3134 | } | |
952fccac | 3135 | } |
5d4f98a2 | 3136 | } else { |
1c2a07f5 | 3137 | /* In this branch refs == 1 */ |
5d4f98a2 | 3138 | if (found_extent) { |
1c2a07f5 QW |
3139 | if (is_data && refs_to_drop != |
3140 | extent_data_ref_count(path, iref)) { | |
3141 | btrfs_crit(info, | |
3142 | "invalid refs_to_drop, current refs %u refs_to_drop %u", | |
3143 | extent_data_ref_count(path, iref), | |
3144 | refs_to_drop); | |
3145 | btrfs_abort_transaction(trans, -EUCLEAN); | |
3146 | goto err_dump; | |
3147 | } | |
5d4f98a2 | 3148 | if (iref) { |
1c2a07f5 QW |
3149 | if (path->slots[0] != extent_slot) { |
3150 | btrfs_crit(info, | |
3151 | "invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref", | |
3152 | key.objectid, key.type, | |
3153 | key.offset); | |
3154 | btrfs_abort_transaction(trans, -EUCLEAN); | |
3155 | goto err_dump; | |
3156 | } | |
5d4f98a2 | 3157 | } else { |
1c2a07f5 QW |
3158 | /* |
3159 | * No inline ref, we must be at SHARED_* item, | |
3160 | * And it's single ref, it must be: | |
3161 | * | extent_slot ||extent_slot + 1| | |
3162 | * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ] | |
3163 | */ | |
3164 | if (path->slots[0] != extent_slot + 1) { | |
3165 | btrfs_crit(info, | |
3166 | "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM"); | |
3167 | btrfs_abort_transaction(trans, -EUCLEAN); | |
3168 | goto err_dump; | |
3169 | } | |
5d4f98a2 YZ |
3170 | path->slots[0] = extent_slot; |
3171 | num_to_del = 2; | |
3172 | } | |
78fae27e | 3173 | } |
b9473439 | 3174 | |
fcebe456 | 3175 | last_ref = 1; |
952fccac CM |
3176 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
3177 | num_to_del); | |
005d6427 | 3178 | if (ret) { |
66642832 | 3179 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3180 | goto out; |
3181 | } | |
b3b4aa74 | 3182 | btrfs_release_path(path); |
21af804c | 3183 | |
5d4f98a2 | 3184 | if (is_data) { |
40e046ac FM |
3185 | ret = btrfs_del_csums(trans, info->csum_root, bytenr, |
3186 | num_bytes); | |
005d6427 | 3187 | if (ret) { |
66642832 | 3188 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3189 | goto out; |
3190 | } | |
459931ec CM |
3191 | } |
3192 | ||
e7355e50 | 3193 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
1e144fb8 | 3194 | if (ret) { |
66642832 | 3195 | btrfs_abort_transaction(trans, ret); |
1e144fb8 OS |
3196 | goto out; |
3197 | } | |
3198 | ||
ade4b516 | 3199 | ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0); |
005d6427 | 3200 | if (ret) { |
66642832 | 3201 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
3202 | goto out; |
3203 | } | |
a28ec197 | 3204 | } |
fcebe456 JB |
3205 | btrfs_release_path(path); |
3206 | ||
79787eaa | 3207 | out: |
5caf2a00 | 3208 | btrfs_free_path(path); |
a28ec197 | 3209 | return ret; |
1c2a07f5 QW |
3210 | err_dump: |
3211 | /* | |
3212 | * Leaf dump can take up a lot of log buffer, so we only do full leaf | |
3213 | * dump for debug build. | |
3214 | */ | |
3215 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { | |
3216 | btrfs_crit(info, "path->slots[0]=%d extent_slot=%d", | |
3217 | path->slots[0], extent_slot); | |
3218 | btrfs_print_leaf(path->nodes[0]); | |
3219 | } | |
3220 | ||
3221 | btrfs_free_path(path); | |
3222 | return -EUCLEAN; | |
a28ec197 CM |
3223 | } |
3224 | ||
1887be66 | 3225 | /* |
f0486c68 | 3226 | * when we free an block, it is possible (and likely) that we free the last |
1887be66 CM |
3227 | * delayed ref for that extent as well. This searches the delayed ref tree for |
3228 | * a given extent, and if there are no other delayed refs to be processed, it | |
3229 | * removes it from the tree. | |
3230 | */ | |
3231 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, | |
2ff7e61e | 3232 | u64 bytenr) |
1887be66 CM |
3233 | { |
3234 | struct btrfs_delayed_ref_head *head; | |
3235 | struct btrfs_delayed_ref_root *delayed_refs; | |
f0486c68 | 3236 | int ret = 0; |
1887be66 CM |
3237 | |
3238 | delayed_refs = &trans->transaction->delayed_refs; | |
3239 | spin_lock(&delayed_refs->lock); | |
f72ad18e | 3240 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
1887be66 | 3241 | if (!head) |
cf93da7b | 3242 | goto out_delayed_unlock; |
1887be66 | 3243 | |
d7df2c79 | 3244 | spin_lock(&head->lock); |
e3d03965 | 3245 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
1887be66 CM |
3246 | goto out; |
3247 | ||
bedc6617 JB |
3248 | if (cleanup_extent_op(head) != NULL) |
3249 | goto out; | |
5d4f98a2 | 3250 | |
1887be66 CM |
3251 | /* |
3252 | * waiting for the lock here would deadlock. If someone else has it | |
3253 | * locked they are already in the process of dropping it anyway | |
3254 | */ | |
3255 | if (!mutex_trylock(&head->mutex)) | |
3256 | goto out; | |
3257 | ||
d7baffda | 3258 | btrfs_delete_ref_head(delayed_refs, head); |
d7df2c79 | 3259 | head->processing = 0; |
d7baffda | 3260 | |
d7df2c79 | 3261 | spin_unlock(&head->lock); |
1887be66 CM |
3262 | spin_unlock(&delayed_refs->lock); |
3263 | ||
f0486c68 YZ |
3264 | BUG_ON(head->extent_op); |
3265 | if (head->must_insert_reserved) | |
3266 | ret = 1; | |
3267 | ||
31890da0 | 3268 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
f0486c68 | 3269 | mutex_unlock(&head->mutex); |
d278850e | 3270 | btrfs_put_delayed_ref_head(head); |
f0486c68 | 3271 | return ret; |
1887be66 | 3272 | out: |
d7df2c79 | 3273 | spin_unlock(&head->lock); |
cf93da7b CM |
3274 | |
3275 | out_delayed_unlock: | |
1887be66 CM |
3276 | spin_unlock(&delayed_refs->lock); |
3277 | return 0; | |
3278 | } | |
3279 | ||
f0486c68 YZ |
3280 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
3281 | struct btrfs_root *root, | |
3282 | struct extent_buffer *buf, | |
5581a51a | 3283 | u64 parent, int last_ref) |
f0486c68 | 3284 | { |
0b246afa | 3285 | struct btrfs_fs_info *fs_info = root->fs_info; |
ed4f255b | 3286 | struct btrfs_ref generic_ref = { 0 }; |
f0486c68 YZ |
3287 | int ret; |
3288 | ||
ed4f255b QW |
3289 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
3290 | buf->start, buf->len, parent); | |
3291 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), | |
3292 | root->root_key.objectid); | |
3293 | ||
f0486c68 | 3294 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
8a5040f7 | 3295 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
2187374f | 3296 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL); |
79787eaa | 3297 | BUG_ON(ret); /* -ENOMEM */ |
f0486c68 YZ |
3298 | } |
3299 | ||
0a16c7d7 | 3300 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
32da5386 | 3301 | struct btrfs_block_group *cache; |
485df755 | 3302 | bool must_pin = false; |
6219872d | 3303 | |
f0486c68 | 3304 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
2ff7e61e | 3305 | ret = check_ref_cleanup(trans, buf->start); |
d3575156 NA |
3306 | if (!ret) { |
3307 | btrfs_redirty_list_add(trans->transaction, buf); | |
37be25bc | 3308 | goto out; |
d3575156 | 3309 | } |
f0486c68 YZ |
3310 | } |
3311 | ||
0b246afa | 3312 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
6219872d | 3313 | |
f0486c68 | 3314 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
6690d071 | 3315 | pin_down_extent(trans, cache, buf->start, buf->len, 1); |
6219872d | 3316 | btrfs_put_block_group(cache); |
37be25bc | 3317 | goto out; |
f0486c68 YZ |
3318 | } |
3319 | ||
485df755 FM |
3320 | /* |
3321 | * If this is a leaf and there are tree mod log users, we may | |
3322 | * have recorded mod log operations that point to this leaf. | |
3323 | * So we must make sure no one reuses this leaf's extent before | |
3324 | * mod log operations are applied to a node, otherwise after | |
3325 | * rewinding a node using the mod log operations we get an | |
3326 | * inconsistent btree, as the leaf's extent may now be used as | |
3327 | * a node or leaf for another different btree. | |
3328 | * We are safe from races here because at this point no other | |
3329 | * node or root points to this extent buffer, so if after this | |
3330 | * check a new tree mod log user joins, it will not be able to | |
3331 | * find a node pointing to this leaf and record operations that | |
3332 | * point to this leaf. | |
3333 | */ | |
888dd183 FM |
3334 | if (btrfs_header_level(buf) == 0 && |
3335 | test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags)) | |
3336 | must_pin = true; | |
485df755 FM |
3337 | |
3338 | if (must_pin || btrfs_is_zoned(fs_info)) { | |
d3575156 NA |
3339 | btrfs_redirty_list_add(trans->transaction, buf); |
3340 | pin_down_extent(trans, cache, buf->start, buf->len, 1); | |
3341 | btrfs_put_block_group(cache); | |
3342 | goto out; | |
3343 | } | |
3344 | ||
f0486c68 YZ |
3345 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
3346 | ||
3347 | btrfs_add_free_space(cache, buf->start, buf->len); | |
4824f1f4 | 3348 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
6219872d | 3349 | btrfs_put_block_group(cache); |
71ff6437 | 3350 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
f0486c68 YZ |
3351 | } |
3352 | out: | |
0a16c7d7 OS |
3353 | if (last_ref) { |
3354 | /* | |
3355 | * Deleting the buffer, clear the corrupt flag since it doesn't | |
3356 | * matter anymore. | |
3357 | */ | |
3358 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); | |
3359 | } | |
f0486c68 YZ |
3360 | } |
3361 | ||
79787eaa | 3362 | /* Can return -ENOMEM */ |
ffd4bb2a | 3363 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) |
925baedd | 3364 | { |
ffd4bb2a | 3365 | struct btrfs_fs_info *fs_info = trans->fs_info; |
925baedd CM |
3366 | int ret; |
3367 | ||
f5ee5c9a | 3368 | if (btrfs_is_testing(fs_info)) |
faa2dbf0 | 3369 | return 0; |
fccb84c9 | 3370 | |
56bec294 CM |
3371 | /* |
3372 | * tree log blocks never actually go into the extent allocation | |
3373 | * tree, just update pinning info and exit early. | |
56bec294 | 3374 | */ |
ffd4bb2a QW |
3375 | if ((ref->type == BTRFS_REF_METADATA && |
3376 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || | |
3377 | (ref->type == BTRFS_REF_DATA && | |
3378 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { | |
b9473439 | 3379 | /* unlocks the pinned mutex */ |
b25c36f8 | 3380 | btrfs_pin_extent(trans, ref->bytenr, ref->len, 1); |
56bec294 | 3381 | ret = 0; |
ffd4bb2a | 3382 | } else if (ref->type == BTRFS_REF_METADATA) { |
2187374f | 3383 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL); |
5d4f98a2 | 3384 | } else { |
2187374f | 3385 | ret = btrfs_add_delayed_data_ref(trans, ref, 0); |
56bec294 | 3386 | } |
d7eae340 | 3387 | |
ffd4bb2a QW |
3388 | if (!((ref->type == BTRFS_REF_METADATA && |
3389 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || | |
3390 | (ref->type == BTRFS_REF_DATA && | |
3391 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) | |
3392 | btrfs_ref_tree_mod(fs_info, ref); | |
8a5040f7 | 3393 | |
925baedd CM |
3394 | return ret; |
3395 | } | |
3396 | ||
817d52f8 | 3397 | enum btrfs_loop_type { |
f262fa8d DS |
3398 | LOOP_CACHING_NOWAIT, |
3399 | LOOP_CACHING_WAIT, | |
3400 | LOOP_ALLOC_CHUNK, | |
3401 | LOOP_NO_EMPTY_SIZE, | |
817d52f8 JB |
3402 | }; |
3403 | ||
e570fd27 | 3404 | static inline void |
32da5386 | 3405 | btrfs_lock_block_group(struct btrfs_block_group *cache, |
e570fd27 MX |
3406 | int delalloc) |
3407 | { | |
3408 | if (delalloc) | |
3409 | down_read(&cache->data_rwsem); | |
3410 | } | |
3411 | ||
32da5386 | 3412 | static inline void btrfs_grab_block_group(struct btrfs_block_group *cache, |
e570fd27 MX |
3413 | int delalloc) |
3414 | { | |
3415 | btrfs_get_block_group(cache); | |
3416 | if (delalloc) | |
3417 | down_read(&cache->data_rwsem); | |
3418 | } | |
3419 | ||
32da5386 DS |
3420 | static struct btrfs_block_group *btrfs_lock_cluster( |
3421 | struct btrfs_block_group *block_group, | |
e570fd27 MX |
3422 | struct btrfs_free_cluster *cluster, |
3423 | int delalloc) | |
c142c6a4 | 3424 | __acquires(&cluster->refill_lock) |
e570fd27 | 3425 | { |
32da5386 | 3426 | struct btrfs_block_group *used_bg = NULL; |
6719afdc | 3427 | |
e570fd27 | 3428 | spin_lock(&cluster->refill_lock); |
6719afdc GU |
3429 | while (1) { |
3430 | used_bg = cluster->block_group; | |
3431 | if (!used_bg) | |
3432 | return NULL; | |
3433 | ||
3434 | if (used_bg == block_group) | |
e570fd27 MX |
3435 | return used_bg; |
3436 | ||
6719afdc | 3437 | btrfs_get_block_group(used_bg); |
e570fd27 | 3438 | |
6719afdc GU |
3439 | if (!delalloc) |
3440 | return used_bg; | |
e570fd27 | 3441 | |
6719afdc GU |
3442 | if (down_read_trylock(&used_bg->data_rwsem)) |
3443 | return used_bg; | |
e570fd27 | 3444 | |
6719afdc | 3445 | spin_unlock(&cluster->refill_lock); |
e570fd27 | 3446 | |
e321f8a8 LB |
3447 | /* We should only have one-level nested. */ |
3448 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); | |
e570fd27 | 3449 | |
6719afdc GU |
3450 | spin_lock(&cluster->refill_lock); |
3451 | if (used_bg == cluster->block_group) | |
3452 | return used_bg; | |
e570fd27 | 3453 | |
6719afdc GU |
3454 | up_read(&used_bg->data_rwsem); |
3455 | btrfs_put_block_group(used_bg); | |
3456 | } | |
e570fd27 MX |
3457 | } |
3458 | ||
3459 | static inline void | |
32da5386 | 3460 | btrfs_release_block_group(struct btrfs_block_group *cache, |
e570fd27 MX |
3461 | int delalloc) |
3462 | { | |
3463 | if (delalloc) | |
3464 | up_read(&cache->data_rwsem); | |
3465 | btrfs_put_block_group(cache); | |
3466 | } | |
3467 | ||
cb2f96f8 NA |
3468 | enum btrfs_extent_allocation_policy { |
3469 | BTRFS_EXTENT_ALLOC_CLUSTERED, | |
2eda5708 | 3470 | BTRFS_EXTENT_ALLOC_ZONED, |
cb2f96f8 NA |
3471 | }; |
3472 | ||
b4bd745d QW |
3473 | /* |
3474 | * Structure used internally for find_free_extent() function. Wraps needed | |
3475 | * parameters. | |
3476 | */ | |
3477 | struct find_free_extent_ctl { | |
3478 | /* Basic allocation info */ | |
a12b0dc0 | 3479 | u64 ram_bytes; |
b4bd745d | 3480 | u64 num_bytes; |
a85f05e5 | 3481 | u64 min_alloc_size; |
b4bd745d QW |
3482 | u64 empty_size; |
3483 | u64 flags; | |
3484 | int delalloc; | |
3485 | ||
3486 | /* Where to start the search inside the bg */ | |
3487 | u64 search_start; | |
3488 | ||
3489 | /* For clustered allocation */ | |
3490 | u64 empty_cluster; | |
c10859be NA |
3491 | struct btrfs_free_cluster *last_ptr; |
3492 | bool use_cluster; | |
b4bd745d QW |
3493 | |
3494 | bool have_caching_bg; | |
3495 | bool orig_have_caching_bg; | |
3496 | ||
40ab3be1 NA |
3497 | /* Allocation is called for tree-log */ |
3498 | bool for_treelog; | |
3499 | ||
c2707a25 JT |
3500 | /* Allocation is called for data relocation */ |
3501 | bool for_data_reloc; | |
3502 | ||
b4bd745d QW |
3503 | /* RAID index, converted from flags */ |
3504 | int index; | |
3505 | ||
e72d79d6 QW |
3506 | /* |
3507 | * Current loop number, check find_free_extent_update_loop() for details | |
3508 | */ | |
b4bd745d QW |
3509 | int loop; |
3510 | ||
3511 | /* | |
3512 | * Whether we're refilling a cluster, if true we need to re-search | |
3513 | * current block group but don't try to refill the cluster again. | |
3514 | */ | |
3515 | bool retry_clustered; | |
3516 | ||
3517 | /* | |
3518 | * Whether we're updating free space cache, if true we need to re-search | |
3519 | * current block group but don't try updating free space cache again. | |
3520 | */ | |
3521 | bool retry_unclustered; | |
3522 | ||
3523 | /* If current block group is cached */ | |
3524 | int cached; | |
3525 | ||
3526 | /* Max contiguous hole found */ | |
3527 | u64 max_extent_size; | |
3528 | ||
3529 | /* Total free space from free space cache, not always contiguous */ | |
3530 | u64 total_free_space; | |
3531 | ||
3532 | /* Found result */ | |
3533 | u64 found_offset; | |
cb2f96f8 | 3534 | |
ea544149 NA |
3535 | /* Hint where to start looking for an empty space */ |
3536 | u64 hint_byte; | |
3537 | ||
cb2f96f8 NA |
3538 | /* Allocation policy */ |
3539 | enum btrfs_extent_allocation_policy policy; | |
b4bd745d QW |
3540 | }; |
3541 | ||
d06e3bb6 QW |
3542 | |
3543 | /* | |
3544 | * Helper function for find_free_extent(). | |
3545 | * | |
3546 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. | |
3547 | * Return -EAGAIN to inform caller that we need to re-search this block group | |
3548 | * Return >0 to inform caller that we find nothing | |
3549 | * Return 0 means we have found a location and set ffe_ctl->found_offset. | |
3550 | */ | |
32da5386 | 3551 | static int find_free_extent_clustered(struct btrfs_block_group *bg, |
897cae79 NA |
3552 | struct find_free_extent_ctl *ffe_ctl, |
3553 | struct btrfs_block_group **cluster_bg_ret) | |
d06e3bb6 | 3554 | { |
32da5386 | 3555 | struct btrfs_block_group *cluster_bg; |
897cae79 | 3556 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
d06e3bb6 QW |
3557 | u64 aligned_cluster; |
3558 | u64 offset; | |
3559 | int ret; | |
3560 | ||
3561 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); | |
3562 | if (!cluster_bg) | |
3563 | goto refill_cluster; | |
3564 | if (cluster_bg != bg && (cluster_bg->ro || | |
3565 | !block_group_bits(cluster_bg, ffe_ctl->flags))) | |
3566 | goto release_cluster; | |
3567 | ||
3568 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, | |
b3470b5d | 3569 | ffe_ctl->num_bytes, cluster_bg->start, |
d06e3bb6 QW |
3570 | &ffe_ctl->max_extent_size); |
3571 | if (offset) { | |
3572 | /* We have a block, we're done */ | |
3573 | spin_unlock(&last_ptr->refill_lock); | |
3574 | trace_btrfs_reserve_extent_cluster(cluster_bg, | |
3575 | ffe_ctl->search_start, ffe_ctl->num_bytes); | |
3576 | *cluster_bg_ret = cluster_bg; | |
3577 | ffe_ctl->found_offset = offset; | |
3578 | return 0; | |
3579 | } | |
3580 | WARN_ON(last_ptr->block_group != cluster_bg); | |
3581 | ||
3582 | release_cluster: | |
3583 | /* | |
3584 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so | |
3585 | * lets just skip it and let the allocator find whatever block it can | |
3586 | * find. If we reach this point, we will have tried the cluster | |
3587 | * allocator plenty of times and not have found anything, so we are | |
3588 | * likely way too fragmented for the clustering stuff to find anything. | |
3589 | * | |
3590 | * However, if the cluster is taken from the current block group, | |
3591 | * release the cluster first, so that we stand a better chance of | |
3592 | * succeeding in the unclustered allocation. | |
3593 | */ | |
3594 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { | |
3595 | spin_unlock(&last_ptr->refill_lock); | |
3596 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); | |
3597 | return -ENOENT; | |
3598 | } | |
3599 | ||
3600 | /* This cluster didn't work out, free it and start over */ | |
3601 | btrfs_return_cluster_to_free_space(NULL, last_ptr); | |
3602 | ||
3603 | if (cluster_bg != bg) | |
3604 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); | |
3605 | ||
3606 | refill_cluster: | |
3607 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { | |
3608 | spin_unlock(&last_ptr->refill_lock); | |
3609 | return -ENOENT; | |
3610 | } | |
3611 | ||
3612 | aligned_cluster = max_t(u64, | |
3613 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, | |
3614 | bg->full_stripe_len); | |
2ceeae2e DS |
3615 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
3616 | ffe_ctl->num_bytes, aligned_cluster); | |
d06e3bb6 QW |
3617 | if (ret == 0) { |
3618 | /* Now pull our allocation out of this cluster */ | |
3619 | offset = btrfs_alloc_from_cluster(bg, last_ptr, | |
3620 | ffe_ctl->num_bytes, ffe_ctl->search_start, | |
3621 | &ffe_ctl->max_extent_size); | |
3622 | if (offset) { | |
3623 | /* We found one, proceed */ | |
3624 | spin_unlock(&last_ptr->refill_lock); | |
3625 | trace_btrfs_reserve_extent_cluster(bg, | |
3626 | ffe_ctl->search_start, | |
3627 | ffe_ctl->num_bytes); | |
3628 | ffe_ctl->found_offset = offset; | |
3629 | return 0; | |
3630 | } | |
3631 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && | |
3632 | !ffe_ctl->retry_clustered) { | |
3633 | spin_unlock(&last_ptr->refill_lock); | |
3634 | ||
3635 | ffe_ctl->retry_clustered = true; | |
676f1f75 | 3636 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
d06e3bb6 QW |
3637 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
3638 | return -EAGAIN; | |
3639 | } | |
3640 | /* | |
3641 | * At this point we either didn't find a cluster or we weren't able to | |
3642 | * allocate a block from our cluster. Free the cluster we've been | |
3643 | * trying to use, and go to the next block group. | |
3644 | */ | |
3645 | btrfs_return_cluster_to_free_space(NULL, last_ptr); | |
3646 | spin_unlock(&last_ptr->refill_lock); | |
3647 | return 1; | |
3648 | } | |
3649 | ||
e1a41848 QW |
3650 | /* |
3651 | * Return >0 to inform caller that we find nothing | |
3652 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset | |
3653 | * Return -EAGAIN to inform caller that we need to re-search this block group | |
3654 | */ | |
32da5386 | 3655 | static int find_free_extent_unclustered(struct btrfs_block_group *bg, |
897cae79 | 3656 | struct find_free_extent_ctl *ffe_ctl) |
e1a41848 | 3657 | { |
897cae79 | 3658 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
e1a41848 QW |
3659 | u64 offset; |
3660 | ||
3661 | /* | |
3662 | * We are doing an unclustered allocation, set the fragmented flag so | |
3663 | * we don't bother trying to setup a cluster again until we get more | |
3664 | * space. | |
3665 | */ | |
3666 | if (unlikely(last_ptr)) { | |
3667 | spin_lock(&last_ptr->lock); | |
3668 | last_ptr->fragmented = 1; | |
3669 | spin_unlock(&last_ptr->lock); | |
3670 | } | |
3671 | if (ffe_ctl->cached) { | |
3672 | struct btrfs_free_space_ctl *free_space_ctl; | |
3673 | ||
3674 | free_space_ctl = bg->free_space_ctl; | |
3675 | spin_lock(&free_space_ctl->tree_lock); | |
3676 | if (free_space_ctl->free_space < | |
3677 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + | |
3678 | ffe_ctl->empty_size) { | |
3679 | ffe_ctl->total_free_space = max_t(u64, | |
3680 | ffe_ctl->total_free_space, | |
3681 | free_space_ctl->free_space); | |
3682 | spin_unlock(&free_space_ctl->tree_lock); | |
3683 | return 1; | |
3684 | } | |
3685 | spin_unlock(&free_space_ctl->tree_lock); | |
3686 | } | |
3687 | ||
3688 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, | |
3689 | ffe_ctl->num_bytes, ffe_ctl->empty_size, | |
3690 | &ffe_ctl->max_extent_size); | |
3691 | ||
3692 | /* | |
3693 | * If we didn't find a chunk, and we haven't failed on this block group | |
3694 | * before, and this block group is in the middle of caching and we are | |
3695 | * ok with waiting, then go ahead and wait for progress to be made, and | |
3696 | * set @retry_unclustered to true. | |
3697 | * | |
3698 | * If @retry_unclustered is true then we've already waited on this | |
3699 | * block group once and should move on to the next block group. | |
3700 | */ | |
3701 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && | |
3702 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { | |
676f1f75 JB |
3703 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
3704 | ffe_ctl->empty_size); | |
e1a41848 QW |
3705 | ffe_ctl->retry_unclustered = true; |
3706 | return -EAGAIN; | |
3707 | } else if (!offset) { | |
3708 | return 1; | |
3709 | } | |
3710 | ffe_ctl->found_offset = offset; | |
3711 | return 0; | |
3712 | } | |
3713 | ||
c668690d NA |
3714 | static int do_allocation_clustered(struct btrfs_block_group *block_group, |
3715 | struct find_free_extent_ctl *ffe_ctl, | |
3716 | struct btrfs_block_group **bg_ret) | |
3717 | { | |
3718 | int ret; | |
3719 | ||
3720 | /* We want to try and use the cluster allocator, so lets look there */ | |
3721 | if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) { | |
897cae79 | 3722 | ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret); |
c668690d NA |
3723 | if (ret >= 0 || ret == -EAGAIN) |
3724 | return ret; | |
3725 | /* ret == -ENOENT case falls through */ | |
3726 | } | |
3727 | ||
897cae79 | 3728 | return find_free_extent_unclustered(block_group, ffe_ctl); |
c668690d NA |
3729 | } |
3730 | ||
40ab3be1 NA |
3731 | /* |
3732 | * Tree-log block group locking | |
3733 | * ============================ | |
3734 | * | |
3735 | * fs_info::treelog_bg_lock protects the fs_info::treelog_bg which | |
3736 | * indicates the starting address of a block group, which is reserved only | |
3737 | * for tree-log metadata. | |
3738 | * | |
3739 | * Lock nesting | |
3740 | * ============ | |
3741 | * | |
3742 | * space_info::lock | |
3743 | * block_group::lock | |
3744 | * fs_info::treelog_bg_lock | |
3745 | */ | |
3746 | ||
2eda5708 NA |
3747 | /* |
3748 | * Simple allocator for sequential-only block group. It only allows sequential | |
3749 | * allocation. No need to play with trees. This function also reserves the | |
3750 | * bytes as in btrfs_add_reserved_bytes. | |
3751 | */ | |
3752 | static int do_allocation_zoned(struct btrfs_block_group *block_group, | |
3753 | struct find_free_extent_ctl *ffe_ctl, | |
3754 | struct btrfs_block_group **bg_ret) | |
3755 | { | |
40ab3be1 | 3756 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
2eda5708 NA |
3757 | struct btrfs_space_info *space_info = block_group->space_info; |
3758 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; | |
3759 | u64 start = block_group->start; | |
3760 | u64 num_bytes = ffe_ctl->num_bytes; | |
3761 | u64 avail; | |
40ab3be1 NA |
3762 | u64 bytenr = block_group->start; |
3763 | u64 log_bytenr; | |
c2707a25 | 3764 | u64 data_reloc_bytenr; |
2eda5708 | 3765 | int ret = 0; |
2d81eb1c | 3766 | bool skip = false; |
2eda5708 NA |
3767 | |
3768 | ASSERT(btrfs_is_zoned(block_group->fs_info)); | |
3769 | ||
40ab3be1 NA |
3770 | /* |
3771 | * Do not allow non-tree-log blocks in the dedicated tree-log block | |
3772 | * group, and vice versa. | |
3773 | */ | |
3774 | spin_lock(&fs_info->treelog_bg_lock); | |
3775 | log_bytenr = fs_info->treelog_bg; | |
2d81eb1c JT |
3776 | if (log_bytenr && ((ffe_ctl->for_treelog && bytenr != log_bytenr) || |
3777 | (!ffe_ctl->for_treelog && bytenr == log_bytenr))) | |
3778 | skip = true; | |
40ab3be1 NA |
3779 | spin_unlock(&fs_info->treelog_bg_lock); |
3780 | if (skip) | |
3781 | return 1; | |
3782 | ||
c2707a25 JT |
3783 | /* |
3784 | * Do not allow non-relocation blocks in the dedicated relocation block | |
3785 | * group, and vice versa. | |
3786 | */ | |
3787 | spin_lock(&fs_info->relocation_bg_lock); | |
3788 | data_reloc_bytenr = fs_info->data_reloc_bg; | |
3789 | if (data_reloc_bytenr && | |
3790 | ((ffe_ctl->for_data_reloc && bytenr != data_reloc_bytenr) || | |
3791 | (!ffe_ctl->for_data_reloc && bytenr == data_reloc_bytenr))) | |
3792 | skip = true; | |
3793 | spin_unlock(&fs_info->relocation_bg_lock); | |
3794 | if (skip) | |
3795 | return 1; | |
2e654e4b NA |
3796 | /* Check RO and no space case before trying to activate it */ |
3797 | spin_lock(&block_group->lock); | |
3798 | if (block_group->ro || | |
3799 | block_group->alloc_offset == block_group->zone_capacity) { | |
3800 | spin_unlock(&block_group->lock); | |
3801 | return 1; | |
3802 | } | |
3803 | spin_unlock(&block_group->lock); | |
3804 | ||
3805 | if (!btrfs_zone_activate(block_group)) | |
3806 | return 1; | |
3807 | ||
2eda5708 NA |
3808 | spin_lock(&space_info->lock); |
3809 | spin_lock(&block_group->lock); | |
40ab3be1 | 3810 | spin_lock(&fs_info->treelog_bg_lock); |
c2707a25 | 3811 | spin_lock(&fs_info->relocation_bg_lock); |
40ab3be1 NA |
3812 | |
3813 | ASSERT(!ffe_ctl->for_treelog || | |
3814 | block_group->start == fs_info->treelog_bg || | |
3815 | fs_info->treelog_bg == 0); | |
c2707a25 JT |
3816 | ASSERT(!ffe_ctl->for_data_reloc || |
3817 | block_group->start == fs_info->data_reloc_bg || | |
3818 | fs_info->data_reloc_bg == 0); | |
2eda5708 NA |
3819 | |
3820 | if (block_group->ro) { | |
3821 | ret = 1; | |
3822 | goto out; | |
3823 | } | |
3824 | ||
40ab3be1 NA |
3825 | /* |
3826 | * Do not allow currently using block group to be tree-log dedicated | |
3827 | * block group. | |
3828 | */ | |
3829 | if (ffe_ctl->for_treelog && !fs_info->treelog_bg && | |
3830 | (block_group->used || block_group->reserved)) { | |
3831 | ret = 1; | |
3832 | goto out; | |
3833 | } | |
3834 | ||
c2707a25 JT |
3835 | /* |
3836 | * Do not allow currently used block group to be the data relocation | |
3837 | * dedicated block group. | |
3838 | */ | |
3839 | if (ffe_ctl->for_data_reloc && !fs_info->data_reloc_bg && | |
3840 | (block_group->used || block_group->reserved)) { | |
3841 | ret = 1; | |
3842 | goto out; | |
3843 | } | |
3844 | ||
98173255 NA |
3845 | WARN_ON_ONCE(block_group->alloc_offset > block_group->zone_capacity); |
3846 | avail = block_group->zone_capacity - block_group->alloc_offset; | |
2eda5708 NA |
3847 | if (avail < num_bytes) { |
3848 | if (ffe_ctl->max_extent_size < avail) { | |
3849 | /* | |
3850 | * With sequential allocator, free space is always | |
3851 | * contiguous | |
3852 | */ | |
3853 | ffe_ctl->max_extent_size = avail; | |
3854 | ffe_ctl->total_free_space = avail; | |
3855 | } | |
3856 | ret = 1; | |
3857 | goto out; | |
3858 | } | |
3859 | ||
40ab3be1 NA |
3860 | if (ffe_ctl->for_treelog && !fs_info->treelog_bg) |
3861 | fs_info->treelog_bg = block_group->start; | |
3862 | ||
c2707a25 JT |
3863 | if (ffe_ctl->for_data_reloc && !fs_info->data_reloc_bg) |
3864 | fs_info->data_reloc_bg = block_group->start; | |
3865 | ||
2eda5708 NA |
3866 | ffe_ctl->found_offset = start + block_group->alloc_offset; |
3867 | block_group->alloc_offset += num_bytes; | |
3868 | spin_lock(&ctl->tree_lock); | |
3869 | ctl->free_space -= num_bytes; | |
3870 | spin_unlock(&ctl->tree_lock); | |
3871 | ||
3872 | /* | |
3873 | * We do not check if found_offset is aligned to stripesize. The | |
3874 | * address is anyway rewritten when using zone append writing. | |
3875 | */ | |
3876 | ||
3877 | ffe_ctl->search_start = ffe_ctl->found_offset; | |
3878 | ||
3879 | out: | |
40ab3be1 NA |
3880 | if (ret && ffe_ctl->for_treelog) |
3881 | fs_info->treelog_bg = 0; | |
c2707a25 JT |
3882 | if (ret && ffe_ctl->for_data_reloc) |
3883 | fs_info->data_reloc_bg = 0; | |
3884 | spin_unlock(&fs_info->relocation_bg_lock); | |
40ab3be1 | 3885 | spin_unlock(&fs_info->treelog_bg_lock); |
2eda5708 NA |
3886 | spin_unlock(&block_group->lock); |
3887 | spin_unlock(&space_info->lock); | |
3888 | return ret; | |
3889 | } | |
3890 | ||
c668690d NA |
3891 | static int do_allocation(struct btrfs_block_group *block_group, |
3892 | struct find_free_extent_ctl *ffe_ctl, | |
3893 | struct btrfs_block_group **bg_ret) | |
3894 | { | |
3895 | switch (ffe_ctl->policy) { | |
3896 | case BTRFS_EXTENT_ALLOC_CLUSTERED: | |
3897 | return do_allocation_clustered(block_group, ffe_ctl, bg_ret); | |
2eda5708 NA |
3898 | case BTRFS_EXTENT_ALLOC_ZONED: |
3899 | return do_allocation_zoned(block_group, ffe_ctl, bg_ret); | |
c668690d NA |
3900 | default: |
3901 | BUG(); | |
3902 | } | |
3903 | } | |
3904 | ||
baba5062 NA |
3905 | static void release_block_group(struct btrfs_block_group *block_group, |
3906 | struct find_free_extent_ctl *ffe_ctl, | |
3907 | int delalloc) | |
3908 | { | |
3909 | switch (ffe_ctl->policy) { | |
3910 | case BTRFS_EXTENT_ALLOC_CLUSTERED: | |
3911 | ffe_ctl->retry_clustered = false; | |
3912 | ffe_ctl->retry_unclustered = false; | |
3913 | break; | |
2eda5708 NA |
3914 | case BTRFS_EXTENT_ALLOC_ZONED: |
3915 | /* Nothing to do */ | |
3916 | break; | |
baba5062 NA |
3917 | default: |
3918 | BUG(); | |
3919 | } | |
3920 | ||
3921 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != | |
3922 | ffe_ctl->index); | |
3923 | btrfs_release_block_group(block_group, delalloc); | |
3924 | } | |
3925 | ||
0ab9724b NA |
3926 | static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl, |
3927 | struct btrfs_key *ins) | |
3928 | { | |
3929 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; | |
3930 | ||
3931 | if (!ffe_ctl->use_cluster && last_ptr) { | |
3932 | spin_lock(&last_ptr->lock); | |
3933 | last_ptr->window_start = ins->objectid; | |
3934 | spin_unlock(&last_ptr->lock); | |
3935 | } | |
3936 | } | |
3937 | ||
3938 | static void found_extent(struct find_free_extent_ctl *ffe_ctl, | |
3939 | struct btrfs_key *ins) | |
3940 | { | |
3941 | switch (ffe_ctl->policy) { | |
3942 | case BTRFS_EXTENT_ALLOC_CLUSTERED: | |
3943 | found_extent_clustered(ffe_ctl, ins); | |
3944 | break; | |
2eda5708 NA |
3945 | case BTRFS_EXTENT_ALLOC_ZONED: |
3946 | /* Nothing to do */ | |
3947 | break; | |
0ab9724b NA |
3948 | default: |
3949 | BUG(); | |
3950 | } | |
3951 | } | |
3952 | ||
c70e2139 NA |
3953 | static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl) |
3954 | { | |
3955 | switch (ffe_ctl->policy) { | |
3956 | case BTRFS_EXTENT_ALLOC_CLUSTERED: | |
3957 | /* | |
3958 | * If we can't allocate a new chunk we've already looped through | |
3959 | * at least once, move on to the NO_EMPTY_SIZE case. | |
3960 | */ | |
3961 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; | |
3962 | return 0; | |
2eda5708 NA |
3963 | case BTRFS_EXTENT_ALLOC_ZONED: |
3964 | /* Give up here */ | |
3965 | return -ENOSPC; | |
c70e2139 NA |
3966 | default: |
3967 | BUG(); | |
3968 | } | |
3969 | } | |
3970 | ||
e72d79d6 QW |
3971 | /* |
3972 | * Return >0 means caller needs to re-search for free extent | |
3973 | * Return 0 means we have the needed free extent. | |
3974 | * Return <0 means we failed to locate any free extent. | |
3975 | */ | |
3976 | static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info, | |
e72d79d6 QW |
3977 | struct btrfs_key *ins, |
3978 | struct find_free_extent_ctl *ffe_ctl, | |
15b7ee65 | 3979 | bool full_search) |
e72d79d6 QW |
3980 | { |
3981 | struct btrfs_root *root = fs_info->extent_root; | |
3982 | int ret; | |
3983 | ||
3984 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && | |
3985 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) | |
3986 | ffe_ctl->orig_have_caching_bg = true; | |
3987 | ||
e72d79d6 | 3988 | if (ins->objectid) { |
0ab9724b | 3989 | found_extent(ffe_ctl, ins); |
e72d79d6 QW |
3990 | return 0; |
3991 | } | |
3992 | ||
a85f05e5 NA |
3993 | if (ffe_ctl->max_extent_size >= ffe_ctl->min_alloc_size && |
3994 | !btrfs_can_activate_zone(fs_info->fs_devices, ffe_ctl->index)) { | |
3995 | /* | |
3996 | * If we have enough free space left in an already active block | |
3997 | * group and we can't activate any other zone now, retry the | |
3998 | * active ones with a smaller allocation size. Returning early | |
3999 | * from here will tell btrfs_reserve_extent() to haven the | |
4000 | * size. | |
4001 | */ | |
4002 | return -ENOSPC; | |
4003 | } | |
4004 | ||
4005 | if (ffe_ctl->loop >= LOOP_CACHING_WAIT && ffe_ctl->have_caching_bg) | |
4006 | return 1; | |
4007 | ||
4008 | ffe_ctl->index++; | |
4009 | if (ffe_ctl->index < BTRFS_NR_RAID_TYPES) | |
4010 | return 1; | |
4011 | ||
e72d79d6 QW |
4012 | /* |
4013 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking | |
4014 | * caching kthreads as we move along | |
4015 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching | |
4016 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again | |
4017 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try | |
4018 | * again | |
4019 | */ | |
4020 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { | |
4021 | ffe_ctl->index = 0; | |
4022 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { | |
4023 | /* | |
4024 | * We want to skip the LOOP_CACHING_WAIT step if we | |
4025 | * don't have any uncached bgs and we've already done a | |
4026 | * full search through. | |
4027 | */ | |
4028 | if (ffe_ctl->orig_have_caching_bg || !full_search) | |
4029 | ffe_ctl->loop = LOOP_CACHING_WAIT; | |
4030 | else | |
4031 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; | |
4032 | } else { | |
4033 | ffe_ctl->loop++; | |
4034 | } | |
4035 | ||
4036 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { | |
4037 | struct btrfs_trans_handle *trans; | |
4038 | int exist = 0; | |
4039 | ||
4040 | trans = current->journal_info; | |
4041 | if (trans) | |
4042 | exist = 1; | |
4043 | else | |
4044 | trans = btrfs_join_transaction(root); | |
4045 | ||
4046 | if (IS_ERR(trans)) { | |
4047 | ret = PTR_ERR(trans); | |
4048 | return ret; | |
4049 | } | |
4050 | ||
fc471cb0 JB |
4051 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
4052 | CHUNK_ALLOC_FORCE); | |
e72d79d6 | 4053 | |
e72d79d6 | 4054 | /* Do not bail out on ENOSPC since we can do more. */ |
c70e2139 NA |
4055 | if (ret == -ENOSPC) |
4056 | ret = chunk_allocation_failed(ffe_ctl); | |
4057 | else if (ret < 0) | |
e72d79d6 QW |
4058 | btrfs_abort_transaction(trans, ret); |
4059 | else | |
4060 | ret = 0; | |
4061 | if (!exist) | |
4062 | btrfs_end_transaction(trans); | |
4063 | if (ret) | |
4064 | return ret; | |
4065 | } | |
4066 | ||
4067 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { | |
45d8e033 NA |
4068 | if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED) |
4069 | return -ENOSPC; | |
4070 | ||
e72d79d6 QW |
4071 | /* |
4072 | * Don't loop again if we already have no empty_size and | |
4073 | * no empty_cluster. | |
4074 | */ | |
4075 | if (ffe_ctl->empty_size == 0 && | |
4076 | ffe_ctl->empty_cluster == 0) | |
4077 | return -ENOSPC; | |
4078 | ffe_ctl->empty_size = 0; | |
4079 | ffe_ctl->empty_cluster = 0; | |
4080 | } | |
4081 | return 1; | |
4082 | } | |
4083 | return -ENOSPC; | |
4084 | } | |
4085 | ||
7e895409 NA |
4086 | static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info, |
4087 | struct find_free_extent_ctl *ffe_ctl, | |
4088 | struct btrfs_space_info *space_info, | |
4089 | struct btrfs_key *ins) | |
4090 | { | |
4091 | /* | |
4092 | * If our free space is heavily fragmented we may not be able to make | |
4093 | * big contiguous allocations, so instead of doing the expensive search | |
4094 | * for free space, simply return ENOSPC with our max_extent_size so we | |
4095 | * can go ahead and search for a more manageable chunk. | |
4096 | * | |
4097 | * If our max_extent_size is large enough for our allocation simply | |
4098 | * disable clustering since we will likely not be able to find enough | |
4099 | * space to create a cluster and induce latency trying. | |
4100 | */ | |
4101 | if (space_info->max_extent_size) { | |
4102 | spin_lock(&space_info->lock); | |
4103 | if (space_info->max_extent_size && | |
4104 | ffe_ctl->num_bytes > space_info->max_extent_size) { | |
4105 | ins->offset = space_info->max_extent_size; | |
4106 | spin_unlock(&space_info->lock); | |
4107 | return -ENOSPC; | |
4108 | } else if (space_info->max_extent_size) { | |
4109 | ffe_ctl->use_cluster = false; | |
4110 | } | |
4111 | spin_unlock(&space_info->lock); | |
4112 | } | |
4113 | ||
4114 | ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info, | |
4115 | &ffe_ctl->empty_cluster); | |
4116 | if (ffe_ctl->last_ptr) { | |
4117 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; | |
4118 | ||
4119 | spin_lock(&last_ptr->lock); | |
4120 | if (last_ptr->block_group) | |
4121 | ffe_ctl->hint_byte = last_ptr->window_start; | |
4122 | if (last_ptr->fragmented) { | |
4123 | /* | |
4124 | * We still set window_start so we can keep track of the | |
4125 | * last place we found an allocation to try and save | |
4126 | * some time. | |
4127 | */ | |
4128 | ffe_ctl->hint_byte = last_ptr->window_start; | |
4129 | ffe_ctl->use_cluster = false; | |
4130 | } | |
4131 | spin_unlock(&last_ptr->lock); | |
4132 | } | |
4133 | ||
4134 | return 0; | |
4135 | } | |
4136 | ||
4137 | static int prepare_allocation(struct btrfs_fs_info *fs_info, | |
4138 | struct find_free_extent_ctl *ffe_ctl, | |
4139 | struct btrfs_space_info *space_info, | |
4140 | struct btrfs_key *ins) | |
4141 | { | |
4142 | switch (ffe_ctl->policy) { | |
4143 | case BTRFS_EXTENT_ALLOC_CLUSTERED: | |
4144 | return prepare_allocation_clustered(fs_info, ffe_ctl, | |
4145 | space_info, ins); | |
2eda5708 | 4146 | case BTRFS_EXTENT_ALLOC_ZONED: |
40ab3be1 NA |
4147 | if (ffe_ctl->for_treelog) { |
4148 | spin_lock(&fs_info->treelog_bg_lock); | |
4149 | if (fs_info->treelog_bg) | |
4150 | ffe_ctl->hint_byte = fs_info->treelog_bg; | |
4151 | spin_unlock(&fs_info->treelog_bg_lock); | |
4152 | } | |
c2707a25 JT |
4153 | if (ffe_ctl->for_data_reloc) { |
4154 | spin_lock(&fs_info->relocation_bg_lock); | |
4155 | if (fs_info->data_reloc_bg) | |
4156 | ffe_ctl->hint_byte = fs_info->data_reloc_bg; | |
4157 | spin_unlock(&fs_info->relocation_bg_lock); | |
4158 | } | |
2eda5708 | 4159 | return 0; |
7e895409 NA |
4160 | default: |
4161 | BUG(); | |
4162 | } | |
4163 | } | |
4164 | ||
fec577fb CM |
4165 | /* |
4166 | * walks the btree of allocated extents and find a hole of a given size. | |
4167 | * The key ins is changed to record the hole: | |
a4820398 | 4168 | * ins->objectid == start position |
62e2749e | 4169 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
a4820398 | 4170 | * ins->offset == the size of the hole. |
fec577fb | 4171 | * Any available blocks before search_start are skipped. |
a4820398 MX |
4172 | * |
4173 | * If there is no suitable free space, we will record the max size of | |
4174 | * the free space extent currently. | |
e72d79d6 QW |
4175 | * |
4176 | * The overall logic and call chain: | |
4177 | * | |
4178 | * find_free_extent() | |
4179 | * |- Iterate through all block groups | |
4180 | * | |- Get a valid block group | |
4181 | * | |- Try to do clustered allocation in that block group | |
4182 | * | |- Try to do unclustered allocation in that block group | |
4183 | * | |- Check if the result is valid | |
4184 | * | | |- If valid, then exit | |
4185 | * | |- Jump to next block group | |
4186 | * | | |
4187 | * |- Push harder to find free extents | |
4188 | * |- If not found, re-iterate all block groups | |
fec577fb | 4189 | */ |
437490fe | 4190 | static noinline int find_free_extent(struct btrfs_root *root, |
a12b0dc0 NA |
4191 | struct btrfs_key *ins, |
4192 | struct find_free_extent_ctl *ffe_ctl) | |
fec577fb | 4193 | { |
437490fe | 4194 | struct btrfs_fs_info *fs_info = root->fs_info; |
80eb234a | 4195 | int ret = 0; |
db8fe64f | 4196 | int cache_block_group_error = 0; |
32da5386 | 4197 | struct btrfs_block_group *block_group = NULL; |
80eb234a | 4198 | struct btrfs_space_info *space_info; |
a5e681d9 | 4199 | bool full_search = false; |
fec577fb | 4200 | |
a12b0dc0 | 4201 | WARN_ON(ffe_ctl->num_bytes < fs_info->sectorsize); |
b4bd745d | 4202 | |
a12b0dc0 NA |
4203 | ffe_ctl->search_start = 0; |
4204 | /* For clustered allocation */ | |
4205 | ffe_ctl->empty_cluster = 0; | |
4206 | ffe_ctl->last_ptr = NULL; | |
4207 | ffe_ctl->use_cluster = true; | |
4208 | ffe_ctl->have_caching_bg = false; | |
4209 | ffe_ctl->orig_have_caching_bg = false; | |
4210 | ffe_ctl->index = btrfs_bg_flags_to_raid_index(ffe_ctl->flags); | |
4211 | ffe_ctl->loop = 0; | |
c10859be | 4212 | /* For clustered allocation */ |
a12b0dc0 NA |
4213 | ffe_ctl->retry_clustered = false; |
4214 | ffe_ctl->retry_unclustered = false; | |
4215 | ffe_ctl->cached = 0; | |
4216 | ffe_ctl->max_extent_size = 0; | |
4217 | ffe_ctl->total_free_space = 0; | |
4218 | ffe_ctl->found_offset = 0; | |
4219 | ffe_ctl->policy = BTRFS_EXTENT_ALLOC_CLUSTERED; | |
c10859be | 4220 | |
2eda5708 | 4221 | if (btrfs_is_zoned(fs_info)) |
a12b0dc0 | 4222 | ffe_ctl->policy = BTRFS_EXTENT_ALLOC_ZONED; |
2eda5708 | 4223 | |
962a298f | 4224 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
80eb234a JB |
4225 | ins->objectid = 0; |
4226 | ins->offset = 0; | |
b1a4d965 | 4227 | |
a12b0dc0 NA |
4228 | trace_find_free_extent(root, ffe_ctl->num_bytes, ffe_ctl->empty_size, |
4229 | ffe_ctl->flags); | |
3f7de037 | 4230 | |
a12b0dc0 | 4231 | space_info = btrfs_find_space_info(fs_info, ffe_ctl->flags); |
1b1d1f66 | 4232 | if (!space_info) { |
a12b0dc0 | 4233 | btrfs_err(fs_info, "No space info for %llu", ffe_ctl->flags); |
1b1d1f66 JB |
4234 | return -ENOSPC; |
4235 | } | |
2552d17e | 4236 | |
a12b0dc0 | 4237 | ret = prepare_allocation(fs_info, ffe_ctl, space_info, ins); |
7e895409 NA |
4238 | if (ret < 0) |
4239 | return ret; | |
fa9c0d79 | 4240 | |
a12b0dc0 NA |
4241 | ffe_ctl->search_start = max(ffe_ctl->search_start, |
4242 | first_logical_byte(fs_info, 0)); | |
4243 | ffe_ctl->search_start = max(ffe_ctl->search_start, ffe_ctl->hint_byte); | |
4244 | if (ffe_ctl->search_start == ffe_ctl->hint_byte) { | |
b4bd745d | 4245 | block_group = btrfs_lookup_block_group(fs_info, |
a12b0dc0 | 4246 | ffe_ctl->search_start); |
817d52f8 JB |
4247 | /* |
4248 | * we don't want to use the block group if it doesn't match our | |
4249 | * allocation bits, or if its not cached. | |
ccf0e725 JB |
4250 | * |
4251 | * However if we are re-searching with an ideal block group | |
4252 | * picked out then we don't care that the block group is cached. | |
817d52f8 | 4253 | */ |
a12b0dc0 | 4254 | if (block_group && block_group_bits(block_group, ffe_ctl->flags) && |
285ff5af | 4255 | block_group->cached != BTRFS_CACHE_NO) { |
2552d17e | 4256 | down_read(&space_info->groups_sem); |
44fb5511 CM |
4257 | if (list_empty(&block_group->list) || |
4258 | block_group->ro) { | |
4259 | /* | |
4260 | * someone is removing this block group, | |
4261 | * we can't jump into the have_block_group | |
4262 | * target because our list pointers are not | |
4263 | * valid | |
4264 | */ | |
4265 | btrfs_put_block_group(block_group); | |
4266 | up_read(&space_info->groups_sem); | |
ccf0e725 | 4267 | } else { |
a12b0dc0 NA |
4268 | ffe_ctl->index = btrfs_bg_flags_to_raid_index( |
4269 | block_group->flags); | |
4270 | btrfs_lock_block_group(block_group, | |
4271 | ffe_ctl->delalloc); | |
44fb5511 | 4272 | goto have_block_group; |
ccf0e725 | 4273 | } |
2552d17e | 4274 | } else if (block_group) { |
fa9c0d79 | 4275 | btrfs_put_block_group(block_group); |
2552d17e | 4276 | } |
42e70e7a | 4277 | } |
2552d17e | 4278 | search: |
a12b0dc0 NA |
4279 | ffe_ctl->have_caching_bg = false; |
4280 | if (ffe_ctl->index == btrfs_bg_flags_to_raid_index(ffe_ctl->flags) || | |
4281 | ffe_ctl->index == 0) | |
a5e681d9 | 4282 | full_search = true; |
80eb234a | 4283 | down_read(&space_info->groups_sem); |
b4bd745d | 4284 | list_for_each_entry(block_group, |
a12b0dc0 | 4285 | &space_info->block_groups[ffe_ctl->index], list) { |
c668690d NA |
4286 | struct btrfs_block_group *bg_ret; |
4287 | ||
14443937 | 4288 | /* If the block group is read-only, we can skip it entirely. */ |
40ab3be1 | 4289 | if (unlikely(block_group->ro)) { |
a12b0dc0 | 4290 | if (ffe_ctl->for_treelog) |
40ab3be1 | 4291 | btrfs_clear_treelog_bg(block_group); |
c2707a25 JT |
4292 | if (ffe_ctl->for_data_reloc) |
4293 | btrfs_clear_data_reloc_bg(block_group); | |
14443937 | 4294 | continue; |
40ab3be1 | 4295 | } |
14443937 | 4296 | |
a12b0dc0 NA |
4297 | btrfs_grab_block_group(block_group, ffe_ctl->delalloc); |
4298 | ffe_ctl->search_start = block_group->start; | |
42e70e7a | 4299 | |
83a50de9 CM |
4300 | /* |
4301 | * this can happen if we end up cycling through all the | |
4302 | * raid types, but we want to make sure we only allocate | |
4303 | * for the proper type. | |
4304 | */ | |
a12b0dc0 | 4305 | if (!block_group_bits(block_group, ffe_ctl->flags)) { |
bece2e82 | 4306 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
c7369b3f | 4307 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
a07e8a46 | 4308 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
83a50de9 CM |
4309 | BTRFS_BLOCK_GROUP_RAID10; |
4310 | ||
4311 | /* | |
4312 | * if they asked for extra copies and this block group | |
4313 | * doesn't provide them, bail. This does allow us to | |
4314 | * fill raid0 from raid1. | |
4315 | */ | |
a12b0dc0 | 4316 | if ((ffe_ctl->flags & extra) && !(block_group->flags & extra)) |
83a50de9 | 4317 | goto loop; |
2a28468e QW |
4318 | |
4319 | /* | |
4320 | * This block group has different flags than we want. | |
4321 | * It's possible that we have MIXED_GROUP flag but no | |
4322 | * block group is mixed. Just skip such block group. | |
4323 | */ | |
a12b0dc0 | 4324 | btrfs_release_block_group(block_group, ffe_ctl->delalloc); |
2a28468e | 4325 | continue; |
83a50de9 CM |
4326 | } |
4327 | ||
2552d17e | 4328 | have_block_group: |
a12b0dc0 NA |
4329 | ffe_ctl->cached = btrfs_block_group_done(block_group); |
4330 | if (unlikely(!ffe_ctl->cached)) { | |
4331 | ffe_ctl->have_caching_bg = true; | |
676f1f75 | 4332 | ret = btrfs_cache_block_group(block_group, 0); |
db8fe64f JB |
4333 | |
4334 | /* | |
4335 | * If we get ENOMEM here or something else we want to | |
4336 | * try other block groups, because it may not be fatal. | |
4337 | * However if we can't find anything else we need to | |
4338 | * save our return here so that we return the actual | |
4339 | * error that caused problems, not ENOSPC. | |
4340 | */ | |
4341 | if (ret < 0) { | |
4342 | if (!cache_block_group_error) | |
4343 | cache_block_group_error = ret; | |
4344 | ret = 0; | |
4345 | goto loop; | |
4346 | } | |
1d4284bd | 4347 | ret = 0; |
817d52f8 JB |
4348 | } |
4349 | ||
36cce922 JB |
4350 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
4351 | goto loop; | |
0f9dd46c | 4352 | |
c668690d | 4353 | bg_ret = NULL; |
a12b0dc0 | 4354 | ret = do_allocation(block_group, ffe_ctl, &bg_ret); |
c668690d NA |
4355 | if (ret == 0) { |
4356 | if (bg_ret && bg_ret != block_group) { | |
a12b0dc0 NA |
4357 | btrfs_release_block_group(block_group, |
4358 | ffe_ctl->delalloc); | |
c668690d | 4359 | block_group = bg_ret; |
fa9c0d79 | 4360 | } |
c668690d | 4361 | } else if (ret == -EAGAIN) { |
817d52f8 | 4362 | goto have_block_group; |
c668690d | 4363 | } else if (ret > 0) { |
1cdda9b8 | 4364 | goto loop; |
c668690d NA |
4365 | } |
4366 | ||
4367 | /* Checks */ | |
a12b0dc0 NA |
4368 | ffe_ctl->search_start = round_up(ffe_ctl->found_offset, |
4369 | fs_info->stripesize); | |
25179201 | 4370 | |
2552d17e | 4371 | /* move on to the next group */ |
a12b0dc0 | 4372 | if (ffe_ctl->search_start + ffe_ctl->num_bytes > |
b3470b5d | 4373 | block_group->start + block_group->length) { |
2eda5708 | 4374 | btrfs_add_free_space_unused(block_group, |
a12b0dc0 NA |
4375 | ffe_ctl->found_offset, |
4376 | ffe_ctl->num_bytes); | |
2552d17e | 4377 | goto loop; |
6226cb0a | 4378 | } |
f5a31e16 | 4379 | |
a12b0dc0 | 4380 | if (ffe_ctl->found_offset < ffe_ctl->search_start) |
2eda5708 | 4381 | btrfs_add_free_space_unused(block_group, |
a12b0dc0 NA |
4382 | ffe_ctl->found_offset, |
4383 | ffe_ctl->search_start - ffe_ctl->found_offset); | |
2552d17e | 4384 | |
a12b0dc0 NA |
4385 | ret = btrfs_add_reserved_bytes(block_group, ffe_ctl->ram_bytes, |
4386 | ffe_ctl->num_bytes, | |
4387 | ffe_ctl->delalloc); | |
f0486c68 | 4388 | if (ret == -EAGAIN) { |
2eda5708 | 4389 | btrfs_add_free_space_unused(block_group, |
a12b0dc0 NA |
4390 | ffe_ctl->found_offset, |
4391 | ffe_ctl->num_bytes); | |
2552d17e | 4392 | goto loop; |
0f9dd46c | 4393 | } |
9cfa3e34 | 4394 | btrfs_inc_block_group_reservations(block_group); |
0b86a832 | 4395 | |
f0486c68 | 4396 | /* we are all good, lets return */ |
a12b0dc0 NA |
4397 | ins->objectid = ffe_ctl->search_start; |
4398 | ins->offset = ffe_ctl->num_bytes; | |
d2fb3437 | 4399 | |
a12b0dc0 NA |
4400 | trace_btrfs_reserve_extent(block_group, ffe_ctl->search_start, |
4401 | ffe_ctl->num_bytes); | |
4402 | btrfs_release_block_group(block_group, ffe_ctl->delalloc); | |
2552d17e JB |
4403 | break; |
4404 | loop: | |
a12b0dc0 | 4405 | release_block_group(block_group, ffe_ctl, ffe_ctl->delalloc); |
14443937 | 4406 | cond_resched(); |
2552d17e JB |
4407 | } |
4408 | up_read(&space_info->groups_sem); | |
4409 | ||
a12b0dc0 | 4410 | ret = find_free_extent_update_loop(fs_info, ins, ffe_ctl, full_search); |
e72d79d6 | 4411 | if (ret > 0) |
b742bb82 YZ |
4412 | goto search; |
4413 | ||
db8fe64f | 4414 | if (ret == -ENOSPC && !cache_block_group_error) { |
b4bd745d QW |
4415 | /* |
4416 | * Use ffe_ctl->total_free_space as fallback if we can't find | |
4417 | * any contiguous hole. | |
4418 | */ | |
a12b0dc0 NA |
4419 | if (!ffe_ctl->max_extent_size) |
4420 | ffe_ctl->max_extent_size = ffe_ctl->total_free_space; | |
4f4db217 | 4421 | spin_lock(&space_info->lock); |
a12b0dc0 | 4422 | space_info->max_extent_size = ffe_ctl->max_extent_size; |
4f4db217 | 4423 | spin_unlock(&space_info->lock); |
a12b0dc0 | 4424 | ins->offset = ffe_ctl->max_extent_size; |
db8fe64f JB |
4425 | } else if (ret == -ENOSPC) { |
4426 | ret = cache_block_group_error; | |
4f4db217 | 4427 | } |
0f70abe2 | 4428 | return ret; |
fec577fb | 4429 | } |
ec44a35c | 4430 | |
6f47c706 NB |
4431 | /* |
4432 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a | |
4433 | * hole that is at least as big as @num_bytes. | |
4434 | * | |
4435 | * @root - The root that will contain this extent | |
4436 | * | |
4437 | * @ram_bytes - The amount of space in ram that @num_bytes take. This | |
4438 | * is used for accounting purposes. This value differs | |
4439 | * from @num_bytes only in the case of compressed extents. | |
4440 | * | |
4441 | * @num_bytes - Number of bytes to allocate on-disk. | |
4442 | * | |
4443 | * @min_alloc_size - Indicates the minimum amount of space that the | |
4444 | * allocator should try to satisfy. In some cases | |
4445 | * @num_bytes may be larger than what is required and if | |
4446 | * the filesystem is fragmented then allocation fails. | |
4447 | * However, the presence of @min_alloc_size gives a | |
4448 | * chance to try and satisfy the smaller allocation. | |
4449 | * | |
4450 | * @empty_size - A hint that you plan on doing more COW. This is the | |
4451 | * size in bytes the allocator should try to find free | |
4452 | * next to the block it returns. This is just a hint and | |
4453 | * may be ignored by the allocator. | |
4454 | * | |
4455 | * @hint_byte - Hint to the allocator to start searching above the byte | |
4456 | * address passed. It might be ignored. | |
4457 | * | |
4458 | * @ins - This key is modified to record the found hole. It will | |
4459 | * have the following values: | |
4460 | * ins->objectid == start position | |
4461 | * ins->flags = BTRFS_EXTENT_ITEM_KEY | |
4462 | * ins->offset == the size of the hole. | |
4463 | * | |
4464 | * @is_data - Boolean flag indicating whether an extent is | |
4465 | * allocated for data (true) or metadata (false) | |
4466 | * | |
4467 | * @delalloc - Boolean flag indicating whether this allocation is for | |
4468 | * delalloc or not. If 'true' data_rwsem of block groups | |
4469 | * is going to be acquired. | |
4470 | * | |
4471 | * | |
4472 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In | |
4473 | * case -ENOSPC is returned then @ins->offset will contain the size of the | |
4474 | * largest available hole the allocator managed to find. | |
4475 | */ | |
18513091 | 4476 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
11833d66 YZ |
4477 | u64 num_bytes, u64 min_alloc_size, |
4478 | u64 empty_size, u64 hint_byte, | |
e570fd27 | 4479 | struct btrfs_key *ins, int is_data, int delalloc) |
fec577fb | 4480 | { |
ab8d0fc4 | 4481 | struct btrfs_fs_info *fs_info = root->fs_info; |
a12b0dc0 | 4482 | struct find_free_extent_ctl ffe_ctl = {}; |
36af4e07 | 4483 | bool final_tried = num_bytes == min_alloc_size; |
b6919a58 | 4484 | u64 flags; |
fec577fb | 4485 | int ret; |
40ab3be1 | 4486 | bool for_treelog = (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
c2707a25 | 4487 | bool for_data_reloc = (btrfs_is_data_reloc_root(root) && is_data); |
925baedd | 4488 | |
1b86826d | 4489 | flags = get_alloc_profile_by_root(root, is_data); |
98d20f67 | 4490 | again: |
0b246afa | 4491 | WARN_ON(num_bytes < fs_info->sectorsize); |
a12b0dc0 NA |
4492 | |
4493 | ffe_ctl.ram_bytes = ram_bytes; | |
4494 | ffe_ctl.num_bytes = num_bytes; | |
a85f05e5 | 4495 | ffe_ctl.min_alloc_size = min_alloc_size; |
a12b0dc0 NA |
4496 | ffe_ctl.empty_size = empty_size; |
4497 | ffe_ctl.flags = flags; | |
4498 | ffe_ctl.delalloc = delalloc; | |
4499 | ffe_ctl.hint_byte = hint_byte; | |
4500 | ffe_ctl.for_treelog = for_treelog; | |
c2707a25 | 4501 | ffe_ctl.for_data_reloc = for_data_reloc; |
a12b0dc0 NA |
4502 | |
4503 | ret = find_free_extent(root, ins, &ffe_ctl); | |
9cfa3e34 | 4504 | if (!ret && !is_data) { |
ab8d0fc4 | 4505 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
9cfa3e34 | 4506 | } else if (ret == -ENOSPC) { |
a4820398 MX |
4507 | if (!final_tried && ins->offset) { |
4508 | num_bytes = min(num_bytes >> 1, ins->offset); | |
da17066c | 4509 | num_bytes = round_down(num_bytes, |
0b246afa | 4510 | fs_info->sectorsize); |
9e622d6b | 4511 | num_bytes = max(num_bytes, min_alloc_size); |
18513091 | 4512 | ram_bytes = num_bytes; |
9e622d6b MX |
4513 | if (num_bytes == min_alloc_size) |
4514 | final_tried = true; | |
4515 | goto again; | |
ab8d0fc4 | 4516 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
9e622d6b MX |
4517 | struct btrfs_space_info *sinfo; |
4518 | ||
280c2908 | 4519 | sinfo = btrfs_find_space_info(fs_info, flags); |
0b246afa | 4520 | btrfs_err(fs_info, |
c2707a25 JT |
4521 | "allocation failed flags %llu, wanted %llu tree-log %d, relocation: %d", |
4522 | flags, num_bytes, for_treelog, for_data_reloc); | |
53804280 | 4523 | if (sinfo) |
5da6afeb JB |
4524 | btrfs_dump_space_info(fs_info, sinfo, |
4525 | num_bytes, 1); | |
9e622d6b | 4526 | } |
925baedd | 4527 | } |
0f9dd46c JB |
4528 | |
4529 | return ret; | |
e6dcd2dc CM |
4530 | } |
4531 | ||
a0fbf736 NB |
4532 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
4533 | u64 start, u64 len, int delalloc) | |
65b51a00 | 4534 | { |
32da5386 | 4535 | struct btrfs_block_group *cache; |
0f9dd46c | 4536 | |
0b246afa | 4537 | cache = btrfs_lookup_block_group(fs_info, start); |
0f9dd46c | 4538 | if (!cache) { |
0b246afa JM |
4539 | btrfs_err(fs_info, "Unable to find block group for %llu", |
4540 | start); | |
0f9dd46c JB |
4541 | return -ENOSPC; |
4542 | } | |
1f3c79a2 | 4543 | |
a0fbf736 NB |
4544 | btrfs_add_free_space(cache, start, len); |
4545 | btrfs_free_reserved_bytes(cache, len, delalloc); | |
4546 | trace_btrfs_reserved_extent_free(fs_info, start, len); | |
4547 | ||
fa9c0d79 | 4548 | btrfs_put_block_group(cache); |
a0fbf736 | 4549 | return 0; |
e6dcd2dc CM |
4550 | } |
4551 | ||
7bfc1007 NB |
4552 | int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, |
4553 | u64 len) | |
e688b725 | 4554 | { |
7ef54d54 | 4555 | struct btrfs_block_group *cache; |
a0fbf736 | 4556 | int ret = 0; |
7ef54d54 | 4557 | |
7bfc1007 | 4558 | cache = btrfs_lookup_block_group(trans->fs_info, start); |
7ef54d54 | 4559 | if (!cache) { |
7bfc1007 NB |
4560 | btrfs_err(trans->fs_info, "unable to find block group for %llu", |
4561 | start); | |
7ef54d54 NB |
4562 | return -ENOSPC; |
4563 | } | |
4564 | ||
6690d071 | 4565 | ret = pin_down_extent(trans, cache, start, len, 1); |
7ef54d54 | 4566 | btrfs_put_block_group(cache); |
a0fbf736 | 4567 | return ret; |
e688b725 CM |
4568 | } |
4569 | ||
5d4f98a2 | 4570 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
4571 | u64 parent, u64 root_objectid, |
4572 | u64 flags, u64 owner, u64 offset, | |
4573 | struct btrfs_key *ins, int ref_mod) | |
e6dcd2dc | 4574 | { |
ef89b824 | 4575 | struct btrfs_fs_info *fs_info = trans->fs_info; |
e6dcd2dc | 4576 | int ret; |
e6dcd2dc | 4577 | struct btrfs_extent_item *extent_item; |
5d4f98a2 | 4578 | struct btrfs_extent_inline_ref *iref; |
e6dcd2dc | 4579 | struct btrfs_path *path; |
5d4f98a2 YZ |
4580 | struct extent_buffer *leaf; |
4581 | int type; | |
4582 | u32 size; | |
26b8003f | 4583 | |
5d4f98a2 YZ |
4584 | if (parent > 0) |
4585 | type = BTRFS_SHARED_DATA_REF_KEY; | |
4586 | else | |
4587 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
58176a96 | 4588 | |
5d4f98a2 | 4589 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
7bb86316 CM |
4590 | |
4591 | path = btrfs_alloc_path(); | |
db5b493a TI |
4592 | if (!path) |
4593 | return -ENOMEM; | |
47e4bb98 | 4594 | |
5d4f98a2 YZ |
4595 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
4596 | ins, size); | |
79787eaa JM |
4597 | if (ret) { |
4598 | btrfs_free_path(path); | |
4599 | return ret; | |
4600 | } | |
0f9dd46c | 4601 | |
5d4f98a2 YZ |
4602 | leaf = path->nodes[0]; |
4603 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
47e4bb98 | 4604 | struct btrfs_extent_item); |
5d4f98a2 YZ |
4605 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
4606 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
4607 | btrfs_set_extent_flags(leaf, extent_item, | |
4608 | flags | BTRFS_EXTENT_FLAG_DATA); | |
4609 | ||
4610 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); | |
4611 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
4612 | if (parent > 0) { | |
4613 | struct btrfs_shared_data_ref *ref; | |
4614 | ref = (struct btrfs_shared_data_ref *)(iref + 1); | |
4615 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
4616 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); | |
4617 | } else { | |
4618 | struct btrfs_extent_data_ref *ref; | |
4619 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
4620 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); | |
4621 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
4622 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
4623 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); | |
4624 | } | |
47e4bb98 CM |
4625 | |
4626 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
7bb86316 | 4627 | btrfs_free_path(path); |
f510cfec | 4628 | |
25a356d3 | 4629 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
1e144fb8 OS |
4630 | if (ret) |
4631 | return ret; | |
4632 | ||
ade4b516 | 4633 | ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1); |
79787eaa | 4634 | if (ret) { /* -ENOENT, logic error */ |
c2cf52eb | 4635 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
c1c9ff7c | 4636 | ins->objectid, ins->offset); |
f5947066 CM |
4637 | BUG(); |
4638 | } | |
71ff6437 | 4639 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
e6dcd2dc CM |
4640 | return ret; |
4641 | } | |
4642 | ||
5d4f98a2 | 4643 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
4e6bd4e0 | 4644 | struct btrfs_delayed_ref_node *node, |
21ebfbe7 | 4645 | struct btrfs_delayed_extent_op *extent_op) |
e6dcd2dc | 4646 | { |
9dcdbe01 | 4647 | struct btrfs_fs_info *fs_info = trans->fs_info; |
e6dcd2dc | 4648 | int ret; |
5d4f98a2 | 4649 | struct btrfs_extent_item *extent_item; |
4e6bd4e0 | 4650 | struct btrfs_key extent_key; |
5d4f98a2 YZ |
4651 | struct btrfs_tree_block_info *block_info; |
4652 | struct btrfs_extent_inline_ref *iref; | |
4653 | struct btrfs_path *path; | |
4654 | struct extent_buffer *leaf; | |
4e6bd4e0 | 4655 | struct btrfs_delayed_tree_ref *ref; |
3173a18f | 4656 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
4e6bd4e0 | 4657 | u64 num_bytes; |
21ebfbe7 | 4658 | u64 flags = extent_op->flags_to_set; |
0b246afa | 4659 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
3173a18f | 4660 | |
4e6bd4e0 NB |
4661 | ref = btrfs_delayed_node_to_tree_ref(node); |
4662 | ||
4e6bd4e0 NB |
4663 | extent_key.objectid = node->bytenr; |
4664 | if (skinny_metadata) { | |
4665 | extent_key.offset = ref->level; | |
4666 | extent_key.type = BTRFS_METADATA_ITEM_KEY; | |
4667 | num_bytes = fs_info->nodesize; | |
4668 | } else { | |
4669 | extent_key.offset = node->num_bytes; | |
4670 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; | |
3173a18f | 4671 | size += sizeof(*block_info); |
4e6bd4e0 NB |
4672 | num_bytes = node->num_bytes; |
4673 | } | |
1c2308f8 | 4674 | |
5d4f98a2 | 4675 | path = btrfs_alloc_path(); |
80ee54bf | 4676 | if (!path) |
d8926bb3 | 4677 | return -ENOMEM; |
56bec294 | 4678 | |
5d4f98a2 | 4679 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
4e6bd4e0 | 4680 | &extent_key, size); |
79787eaa | 4681 | if (ret) { |
dd825259 | 4682 | btrfs_free_path(path); |
79787eaa JM |
4683 | return ret; |
4684 | } | |
5d4f98a2 YZ |
4685 | |
4686 | leaf = path->nodes[0]; | |
4687 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
4688 | struct btrfs_extent_item); | |
4689 | btrfs_set_extent_refs(leaf, extent_item, 1); | |
4690 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
4691 | btrfs_set_extent_flags(leaf, extent_item, | |
4692 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); | |
5d4f98a2 | 4693 | |
3173a18f JB |
4694 | if (skinny_metadata) { |
4695 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); | |
4696 | } else { | |
4697 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); | |
21ebfbe7 | 4698 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
4e6bd4e0 | 4699 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
3173a18f JB |
4700 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
4701 | } | |
5d4f98a2 | 4702 | |
d4b20733 | 4703 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
5d4f98a2 YZ |
4704 | btrfs_set_extent_inline_ref_type(leaf, iref, |
4705 | BTRFS_SHARED_BLOCK_REF_KEY); | |
d4b20733 | 4706 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
5d4f98a2 YZ |
4707 | } else { |
4708 | btrfs_set_extent_inline_ref_type(leaf, iref, | |
4709 | BTRFS_TREE_BLOCK_REF_KEY); | |
4e6bd4e0 | 4710 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
5d4f98a2 YZ |
4711 | } |
4712 | ||
4713 | btrfs_mark_buffer_dirty(leaf); | |
4714 | btrfs_free_path(path); | |
4715 | ||
4e6bd4e0 NB |
4716 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
4717 | num_bytes); | |
1e144fb8 OS |
4718 | if (ret) |
4719 | return ret; | |
4720 | ||
ade4b516 JB |
4721 | ret = btrfs_update_block_group(trans, extent_key.objectid, |
4722 | fs_info->nodesize, 1); | |
79787eaa | 4723 | if (ret) { /* -ENOENT, logic error */ |
c2cf52eb | 4724 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
4e6bd4e0 | 4725 | extent_key.objectid, extent_key.offset); |
5d4f98a2 YZ |
4726 | BUG(); |
4727 | } | |
0be5dc67 | 4728 | |
4e6bd4e0 | 4729 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
0b246afa | 4730 | fs_info->nodesize); |
5d4f98a2 YZ |
4731 | return ret; |
4732 | } | |
4733 | ||
4734 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
84f7d8e6 | 4735 | struct btrfs_root *root, u64 owner, |
5846a3c2 QW |
4736 | u64 offset, u64 ram_bytes, |
4737 | struct btrfs_key *ins) | |
5d4f98a2 | 4738 | { |
76675593 | 4739 | struct btrfs_ref generic_ref = { 0 }; |
5d4f98a2 | 4740 | |
84f7d8e6 | 4741 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
5d4f98a2 | 4742 | |
76675593 QW |
4743 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
4744 | ins->objectid, ins->offset, 0); | |
4745 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); | |
8a5040f7 | 4746 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
2187374f JB |
4747 | |
4748 | return btrfs_add_delayed_data_ref(trans, &generic_ref, ram_bytes); | |
e6dcd2dc | 4749 | } |
e02119d5 CM |
4750 | |
4751 | /* | |
4752 | * this is used by the tree logging recovery code. It records that | |
4753 | * an extent has been allocated and makes sure to clear the free | |
4754 | * space cache bits as well | |
4755 | */ | |
5d4f98a2 | 4756 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
4757 | u64 root_objectid, u64 owner, u64 offset, |
4758 | struct btrfs_key *ins) | |
e02119d5 | 4759 | { |
61da2abf | 4760 | struct btrfs_fs_info *fs_info = trans->fs_info; |
e02119d5 | 4761 | int ret; |
32da5386 | 4762 | struct btrfs_block_group *block_group; |
ed7a6948 | 4763 | struct btrfs_space_info *space_info; |
11833d66 | 4764 | |
8c2a1a30 JB |
4765 | /* |
4766 | * Mixed block groups will exclude before processing the log so we only | |
01327610 | 4767 | * need to do the exclude dance if this fs isn't mixed. |
8c2a1a30 | 4768 | */ |
0b246afa | 4769 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
2ff7e61e JM |
4770 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
4771 | ins->offset); | |
b50c6e25 | 4772 | if (ret) |
8c2a1a30 | 4773 | return ret; |
11833d66 YZ |
4774 | } |
4775 | ||
0b246afa | 4776 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
8c2a1a30 JB |
4777 | if (!block_group) |
4778 | return -EINVAL; | |
4779 | ||
ed7a6948 WX |
4780 | space_info = block_group->space_info; |
4781 | spin_lock(&space_info->lock); | |
4782 | spin_lock(&block_group->lock); | |
4783 | space_info->bytes_reserved += ins->offset; | |
4784 | block_group->reserved += ins->offset; | |
4785 | spin_unlock(&block_group->lock); | |
4786 | spin_unlock(&space_info->lock); | |
4787 | ||
ef89b824 NB |
4788 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
4789 | offset, ins, 1); | |
bd727173 | 4790 | if (ret) |
ab9b2c7b | 4791 | btrfs_pin_extent(trans, ins->objectid, ins->offset, 1); |
b50c6e25 | 4792 | btrfs_put_block_group(block_group); |
e02119d5 CM |
4793 | return ret; |
4794 | } | |
4795 | ||
48a3b636 ES |
4796 | static struct extent_buffer * |
4797 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
9631e4cc JB |
4798 | u64 bytenr, int level, u64 owner, |
4799 | enum btrfs_lock_nesting nest) | |
65b51a00 | 4800 | { |
0b246afa | 4801 | struct btrfs_fs_info *fs_info = root->fs_info; |
65b51a00 CM |
4802 | struct extent_buffer *buf; |
4803 | ||
3fbaf258 | 4804 | buf = btrfs_find_create_tree_block(fs_info, bytenr, owner, level); |
c871b0f2 LB |
4805 | if (IS_ERR(buf)) |
4806 | return buf; | |
4807 | ||
b72c3aba QW |
4808 | /* |
4809 | * Extra safety check in case the extent tree is corrupted and extent | |
4810 | * allocator chooses to use a tree block which is already used and | |
4811 | * locked. | |
4812 | */ | |
4813 | if (buf->lock_owner == current->pid) { | |
4814 | btrfs_err_rl(fs_info, | |
4815 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", | |
4816 | buf->start, btrfs_header_owner(buf), current->pid); | |
4817 | free_extent_buffer(buf); | |
4818 | return ERR_PTR(-EUCLEAN); | |
4819 | } | |
4820 | ||
e114c545 JB |
4821 | /* |
4822 | * This needs to stay, because we could allocate a freed block from an | |
4823 | * old tree into a new tree, so we need to make sure this new block is | |
4824 | * set to the appropriate level and owner. | |
4825 | */ | |
ad244665 | 4826 | btrfs_set_buffer_lockdep_class(owner, buf, level); |
9631e4cc | 4827 | __btrfs_tree_lock(buf, nest); |
6a884d7d | 4828 | btrfs_clean_tree_block(buf); |
3083ee2e | 4829 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
d3575156 | 4830 | clear_bit(EXTENT_BUFFER_NO_CHECK, &buf->bflags); |
b4ce94de | 4831 | |
4db8c528 | 4832 | set_extent_buffer_uptodate(buf); |
b4ce94de | 4833 | |
bc877d28 NB |
4834 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
4835 | btrfs_set_header_level(buf, level); | |
4836 | btrfs_set_header_bytenr(buf, buf->start); | |
4837 | btrfs_set_header_generation(buf, trans->transid); | |
4838 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); | |
4839 | btrfs_set_header_owner(buf, owner); | |
de37aa51 | 4840 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
bc877d28 | 4841 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
d0c803c4 | 4842 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
656f30db | 4843 | buf->log_index = root->log_transid % 2; |
8cef4e16 YZ |
4844 | /* |
4845 | * we allow two log transactions at a time, use different | |
52042d8e | 4846 | * EXTENT bit to differentiate dirty pages. |
8cef4e16 | 4847 | */ |
656f30db | 4848 | if (buf->log_index == 0) |
8cef4e16 YZ |
4849 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
4850 | buf->start + buf->len - 1, GFP_NOFS); | |
4851 | else | |
4852 | set_extent_new(&root->dirty_log_pages, buf->start, | |
3744dbeb | 4853 | buf->start + buf->len - 1); |
d0c803c4 | 4854 | } else { |
656f30db | 4855 | buf->log_index = -1; |
d0c803c4 | 4856 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
65b51a00 | 4857 | buf->start + buf->len - 1, GFP_NOFS); |
d0c803c4 | 4858 | } |
b4ce94de | 4859 | /* this returns a buffer locked for blocking */ |
65b51a00 CM |
4860 | return buf; |
4861 | } | |
4862 | ||
fec577fb | 4863 | /* |
f0486c68 | 4864 | * finds a free extent and does all the dirty work required for allocation |
67b7859e | 4865 | * returns the tree buffer or an ERR_PTR on error. |
fec577fb | 4866 | */ |
4d75f8a9 | 4867 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
310712b2 OS |
4868 | struct btrfs_root *root, |
4869 | u64 parent, u64 root_objectid, | |
4870 | const struct btrfs_disk_key *key, | |
4871 | int level, u64 hint, | |
9631e4cc JB |
4872 | u64 empty_size, |
4873 | enum btrfs_lock_nesting nest) | |
fec577fb | 4874 | { |
0b246afa | 4875 | struct btrfs_fs_info *fs_info = root->fs_info; |
e2fa7227 | 4876 | struct btrfs_key ins; |
f0486c68 | 4877 | struct btrfs_block_rsv *block_rsv; |
5f39d397 | 4878 | struct extent_buffer *buf; |
67b7859e | 4879 | struct btrfs_delayed_extent_op *extent_op; |
ed4f255b | 4880 | struct btrfs_ref generic_ref = { 0 }; |
f0486c68 YZ |
4881 | u64 flags = 0; |
4882 | int ret; | |
0b246afa JM |
4883 | u32 blocksize = fs_info->nodesize; |
4884 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); | |
fec577fb | 4885 | |
05653ef3 | 4886 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
0b246afa | 4887 | if (btrfs_is_testing(fs_info)) { |
faa2dbf0 | 4888 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
9631e4cc | 4889 | level, root_objectid, nest); |
faa2dbf0 JB |
4890 | if (!IS_ERR(buf)) |
4891 | root->alloc_bytenr += blocksize; | |
4892 | return buf; | |
4893 | } | |
05653ef3 | 4894 | #endif |
fccb84c9 | 4895 | |
67f9c220 | 4896 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
f0486c68 YZ |
4897 | if (IS_ERR(block_rsv)) |
4898 | return ERR_CAST(block_rsv); | |
4899 | ||
18513091 | 4900 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
e570fd27 | 4901 | empty_size, hint, &ins, 0, 0); |
67b7859e OS |
4902 | if (ret) |
4903 | goto out_unuse; | |
55c69072 | 4904 | |
bc877d28 | 4905 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
9631e4cc | 4906 | root_objectid, nest); |
67b7859e OS |
4907 | if (IS_ERR(buf)) { |
4908 | ret = PTR_ERR(buf); | |
4909 | goto out_free_reserved; | |
4910 | } | |
f0486c68 YZ |
4911 | |
4912 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { | |
4913 | if (parent == 0) | |
4914 | parent = ins.objectid; | |
4915 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
4916 | } else | |
4917 | BUG_ON(parent > 0); | |
4918 | ||
4919 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { | |
78a6184a | 4920 | extent_op = btrfs_alloc_delayed_extent_op(); |
67b7859e OS |
4921 | if (!extent_op) { |
4922 | ret = -ENOMEM; | |
4923 | goto out_free_buf; | |
4924 | } | |
f0486c68 YZ |
4925 | if (key) |
4926 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); | |
4927 | else | |
4928 | memset(&extent_op->key, 0, sizeof(extent_op->key)); | |
4929 | extent_op->flags_to_set = flags; | |
35b3ad50 DS |
4930 | extent_op->update_key = skinny_metadata ? false : true; |
4931 | extent_op->update_flags = true; | |
4932 | extent_op->is_data = false; | |
b1c79e09 | 4933 | extent_op->level = level; |
f0486c68 | 4934 | |
ed4f255b QW |
4935 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
4936 | ins.objectid, ins.offset, parent); | |
4937 | generic_ref.real_root = root->root_key.objectid; | |
4938 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); | |
8a5040f7 | 4939 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
2187374f | 4940 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, extent_op); |
67b7859e OS |
4941 | if (ret) |
4942 | goto out_free_delayed; | |
f0486c68 | 4943 | } |
fec577fb | 4944 | return buf; |
67b7859e OS |
4945 | |
4946 | out_free_delayed: | |
4947 | btrfs_free_delayed_extent_op(extent_op); | |
4948 | out_free_buf: | |
19ea40dd | 4949 | btrfs_tree_unlock(buf); |
67b7859e OS |
4950 | free_extent_buffer(buf); |
4951 | out_free_reserved: | |
2ff7e61e | 4952 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
67b7859e | 4953 | out_unuse: |
67f9c220 | 4954 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
67b7859e | 4955 | return ERR_PTR(ret); |
fec577fb | 4956 | } |
a28ec197 | 4957 | |
2c47e605 YZ |
4958 | struct walk_control { |
4959 | u64 refs[BTRFS_MAX_LEVEL]; | |
4960 | u64 flags[BTRFS_MAX_LEVEL]; | |
4961 | struct btrfs_key update_progress; | |
aea6f028 JB |
4962 | struct btrfs_key drop_progress; |
4963 | int drop_level; | |
2c47e605 YZ |
4964 | int stage; |
4965 | int level; | |
4966 | int shared_level; | |
4967 | int update_ref; | |
4968 | int keep_locks; | |
1c4850e2 YZ |
4969 | int reada_slot; |
4970 | int reada_count; | |
78c52d9e | 4971 | int restarted; |
2c47e605 YZ |
4972 | }; |
4973 | ||
4974 | #define DROP_REFERENCE 1 | |
4975 | #define UPDATE_BACKREF 2 | |
4976 | ||
1c4850e2 YZ |
4977 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
4978 | struct btrfs_root *root, | |
4979 | struct walk_control *wc, | |
4980 | struct btrfs_path *path) | |
6407bf6d | 4981 | { |
0b246afa | 4982 | struct btrfs_fs_info *fs_info = root->fs_info; |
1c4850e2 YZ |
4983 | u64 bytenr; |
4984 | u64 generation; | |
4985 | u64 refs; | |
94fcca9f | 4986 | u64 flags; |
5d4f98a2 | 4987 | u32 nritems; |
1c4850e2 YZ |
4988 | struct btrfs_key key; |
4989 | struct extent_buffer *eb; | |
6407bf6d | 4990 | int ret; |
1c4850e2 YZ |
4991 | int slot; |
4992 | int nread = 0; | |
6407bf6d | 4993 | |
1c4850e2 YZ |
4994 | if (path->slots[wc->level] < wc->reada_slot) { |
4995 | wc->reada_count = wc->reada_count * 2 / 3; | |
4996 | wc->reada_count = max(wc->reada_count, 2); | |
4997 | } else { | |
4998 | wc->reada_count = wc->reada_count * 3 / 2; | |
4999 | wc->reada_count = min_t(int, wc->reada_count, | |
0b246afa | 5000 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
1c4850e2 | 5001 | } |
7bb86316 | 5002 | |
1c4850e2 YZ |
5003 | eb = path->nodes[wc->level]; |
5004 | nritems = btrfs_header_nritems(eb); | |
bd56b302 | 5005 | |
1c4850e2 YZ |
5006 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
5007 | if (nread >= wc->reada_count) | |
5008 | break; | |
bd56b302 | 5009 | |
2dd3e67b | 5010 | cond_resched(); |
1c4850e2 YZ |
5011 | bytenr = btrfs_node_blockptr(eb, slot); |
5012 | generation = btrfs_node_ptr_generation(eb, slot); | |
2dd3e67b | 5013 | |
1c4850e2 YZ |
5014 | if (slot == path->slots[wc->level]) |
5015 | goto reada; | |
5d4f98a2 | 5016 | |
1c4850e2 YZ |
5017 | if (wc->stage == UPDATE_BACKREF && |
5018 | generation <= root->root_key.offset) | |
bd56b302 CM |
5019 | continue; |
5020 | ||
94fcca9f | 5021 | /* We don't lock the tree block, it's OK to be racy here */ |
2ff7e61e | 5022 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
3173a18f JB |
5023 | wc->level - 1, 1, &refs, |
5024 | &flags); | |
79787eaa JM |
5025 | /* We don't care about errors in readahead. */ |
5026 | if (ret < 0) | |
5027 | continue; | |
94fcca9f YZ |
5028 | BUG_ON(refs == 0); |
5029 | ||
1c4850e2 | 5030 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 YZ |
5031 | if (refs == 1) |
5032 | goto reada; | |
bd56b302 | 5033 | |
94fcca9f YZ |
5034 | if (wc->level == 1 && |
5035 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
5036 | continue; | |
1c4850e2 YZ |
5037 | if (!wc->update_ref || |
5038 | generation <= root->root_key.offset) | |
5039 | continue; | |
5040 | btrfs_node_key_to_cpu(eb, &key, slot); | |
5041 | ret = btrfs_comp_cpu_keys(&key, | |
5042 | &wc->update_progress); | |
5043 | if (ret < 0) | |
5044 | continue; | |
94fcca9f YZ |
5045 | } else { |
5046 | if (wc->level == 1 && | |
5047 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
5048 | continue; | |
6407bf6d | 5049 | } |
1c4850e2 | 5050 | reada: |
bfb484d9 | 5051 | btrfs_readahead_node_child(eb, slot); |
1c4850e2 | 5052 | nread++; |
20524f02 | 5053 | } |
1c4850e2 | 5054 | wc->reada_slot = slot; |
20524f02 | 5055 | } |
2c47e605 | 5056 | |
f82d02d9 | 5057 | /* |
2c016dc2 | 5058 | * helper to process tree block while walking down the tree. |
2c47e605 | 5059 | * |
2c47e605 YZ |
5060 | * when wc->stage == UPDATE_BACKREF, this function updates |
5061 | * back refs for pointers in the block. | |
5062 | * | |
5063 | * NOTE: return value 1 means we should stop walking down. | |
f82d02d9 | 5064 | */ |
2c47e605 | 5065 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
5d4f98a2 | 5066 | struct btrfs_root *root, |
2c47e605 | 5067 | struct btrfs_path *path, |
94fcca9f | 5068 | struct walk_control *wc, int lookup_info) |
f82d02d9 | 5069 | { |
2ff7e61e | 5070 | struct btrfs_fs_info *fs_info = root->fs_info; |
2c47e605 YZ |
5071 | int level = wc->level; |
5072 | struct extent_buffer *eb = path->nodes[level]; | |
2c47e605 | 5073 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
f82d02d9 YZ |
5074 | int ret; |
5075 | ||
2c47e605 YZ |
5076 | if (wc->stage == UPDATE_BACKREF && |
5077 | btrfs_header_owner(eb) != root->root_key.objectid) | |
5078 | return 1; | |
f82d02d9 | 5079 | |
2c47e605 YZ |
5080 | /* |
5081 | * when reference count of tree block is 1, it won't increase | |
5082 | * again. once full backref flag is set, we never clear it. | |
5083 | */ | |
94fcca9f YZ |
5084 | if (lookup_info && |
5085 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || | |
5086 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { | |
2c47e605 | 5087 | BUG_ON(!path->locks[level]); |
2ff7e61e | 5088 | ret = btrfs_lookup_extent_info(trans, fs_info, |
3173a18f | 5089 | eb->start, level, 1, |
2c47e605 YZ |
5090 | &wc->refs[level], |
5091 | &wc->flags[level]); | |
79787eaa JM |
5092 | BUG_ON(ret == -ENOMEM); |
5093 | if (ret) | |
5094 | return ret; | |
2c47e605 YZ |
5095 | BUG_ON(wc->refs[level] == 0); |
5096 | } | |
5d4f98a2 | 5097 | |
2c47e605 YZ |
5098 | if (wc->stage == DROP_REFERENCE) { |
5099 | if (wc->refs[level] > 1) | |
5100 | return 1; | |
f82d02d9 | 5101 | |
2c47e605 | 5102 | if (path->locks[level] && !wc->keep_locks) { |
bd681513 | 5103 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
5104 | path->locks[level] = 0; |
5105 | } | |
5106 | return 0; | |
5107 | } | |
f82d02d9 | 5108 | |
2c47e605 YZ |
5109 | /* wc->stage == UPDATE_BACKREF */ |
5110 | if (!(wc->flags[level] & flag)) { | |
5111 | BUG_ON(!path->locks[level]); | |
e339a6b0 | 5112 | ret = btrfs_inc_ref(trans, root, eb, 1); |
79787eaa | 5113 | BUG_ON(ret); /* -ENOMEM */ |
e339a6b0 | 5114 | ret = btrfs_dec_ref(trans, root, eb, 0); |
79787eaa | 5115 | BUG_ON(ret); /* -ENOMEM */ |
42c9d0b5 | 5116 | ret = btrfs_set_disk_extent_flags(trans, eb, flag, |
b1c79e09 | 5117 | btrfs_header_level(eb), 0); |
79787eaa | 5118 | BUG_ON(ret); /* -ENOMEM */ |
2c47e605 YZ |
5119 | wc->flags[level] |= flag; |
5120 | } | |
5121 | ||
5122 | /* | |
5123 | * the block is shared by multiple trees, so it's not good to | |
5124 | * keep the tree lock | |
5125 | */ | |
5126 | if (path->locks[level] && level > 0) { | |
bd681513 | 5127 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
5128 | path->locks[level] = 0; |
5129 | } | |
5130 | return 0; | |
5131 | } | |
5132 | ||
78c52d9e JB |
5133 | /* |
5134 | * This is used to verify a ref exists for this root to deal with a bug where we | |
5135 | * would have a drop_progress key that hadn't been updated properly. | |
5136 | */ | |
5137 | static int check_ref_exists(struct btrfs_trans_handle *trans, | |
5138 | struct btrfs_root *root, u64 bytenr, u64 parent, | |
5139 | int level) | |
5140 | { | |
5141 | struct btrfs_path *path; | |
5142 | struct btrfs_extent_inline_ref *iref; | |
5143 | int ret; | |
5144 | ||
5145 | path = btrfs_alloc_path(); | |
5146 | if (!path) | |
5147 | return -ENOMEM; | |
5148 | ||
5149 | ret = lookup_extent_backref(trans, path, &iref, bytenr, | |
5150 | root->fs_info->nodesize, parent, | |
5151 | root->root_key.objectid, level, 0); | |
5152 | btrfs_free_path(path); | |
5153 | if (ret == -ENOENT) | |
5154 | return 0; | |
5155 | if (ret < 0) | |
5156 | return ret; | |
5157 | return 1; | |
5158 | } | |
5159 | ||
1c4850e2 | 5160 | /* |
2c016dc2 | 5161 | * helper to process tree block pointer. |
1c4850e2 YZ |
5162 | * |
5163 | * when wc->stage == DROP_REFERENCE, this function checks | |
5164 | * reference count of the block pointed to. if the block | |
5165 | * is shared and we need update back refs for the subtree | |
5166 | * rooted at the block, this function changes wc->stage to | |
5167 | * UPDATE_BACKREF. if the block is shared and there is no | |
5168 | * need to update back, this function drops the reference | |
5169 | * to the block. | |
5170 | * | |
5171 | * NOTE: return value 1 means we should stop walking down. | |
5172 | */ | |
5173 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, | |
5174 | struct btrfs_root *root, | |
5175 | struct btrfs_path *path, | |
94fcca9f | 5176 | struct walk_control *wc, int *lookup_info) |
1c4850e2 | 5177 | { |
0b246afa | 5178 | struct btrfs_fs_info *fs_info = root->fs_info; |
1c4850e2 YZ |
5179 | u64 bytenr; |
5180 | u64 generation; | |
5181 | u64 parent; | |
1c4850e2 | 5182 | struct btrfs_key key; |
581c1760 | 5183 | struct btrfs_key first_key; |
ffd4bb2a | 5184 | struct btrfs_ref ref = { 0 }; |
1c4850e2 YZ |
5185 | struct extent_buffer *next; |
5186 | int level = wc->level; | |
5187 | int reada = 0; | |
5188 | int ret = 0; | |
1152651a | 5189 | bool need_account = false; |
1c4850e2 YZ |
5190 | |
5191 | generation = btrfs_node_ptr_generation(path->nodes[level], | |
5192 | path->slots[level]); | |
5193 | /* | |
5194 | * if the lower level block was created before the snapshot | |
5195 | * was created, we know there is no need to update back refs | |
5196 | * for the subtree | |
5197 | */ | |
5198 | if (wc->stage == UPDATE_BACKREF && | |
94fcca9f YZ |
5199 | generation <= root->root_key.offset) { |
5200 | *lookup_info = 1; | |
1c4850e2 | 5201 | return 1; |
94fcca9f | 5202 | } |
1c4850e2 YZ |
5203 | |
5204 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); | |
581c1760 QW |
5205 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
5206 | path->slots[level]); | |
1c4850e2 | 5207 | |
0b246afa | 5208 | next = find_extent_buffer(fs_info, bytenr); |
1c4850e2 | 5209 | if (!next) { |
3fbaf258 JB |
5210 | next = btrfs_find_create_tree_block(fs_info, bytenr, |
5211 | root->root_key.objectid, level - 1); | |
c871b0f2 LB |
5212 | if (IS_ERR(next)) |
5213 | return PTR_ERR(next); | |
1c4850e2 YZ |
5214 | reada = 1; |
5215 | } | |
5216 | btrfs_tree_lock(next); | |
1c4850e2 | 5217 | |
2ff7e61e | 5218 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
94fcca9f YZ |
5219 | &wc->refs[level - 1], |
5220 | &wc->flags[level - 1]); | |
4867268c JB |
5221 | if (ret < 0) |
5222 | goto out_unlock; | |
79787eaa | 5223 | |
c2cf52eb | 5224 | if (unlikely(wc->refs[level - 1] == 0)) { |
0b246afa | 5225 | btrfs_err(fs_info, "Missing references."); |
4867268c JB |
5226 | ret = -EIO; |
5227 | goto out_unlock; | |
c2cf52eb | 5228 | } |
94fcca9f | 5229 | *lookup_info = 0; |
1c4850e2 | 5230 | |
94fcca9f | 5231 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 | 5232 | if (wc->refs[level - 1] > 1) { |
1152651a | 5233 | need_account = true; |
94fcca9f YZ |
5234 | if (level == 1 && |
5235 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
5236 | goto skip; | |
5237 | ||
1c4850e2 YZ |
5238 | if (!wc->update_ref || |
5239 | generation <= root->root_key.offset) | |
5240 | goto skip; | |
5241 | ||
5242 | btrfs_node_key_to_cpu(path->nodes[level], &key, | |
5243 | path->slots[level]); | |
5244 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); | |
5245 | if (ret < 0) | |
5246 | goto skip; | |
5247 | ||
5248 | wc->stage = UPDATE_BACKREF; | |
5249 | wc->shared_level = level - 1; | |
5250 | } | |
94fcca9f YZ |
5251 | } else { |
5252 | if (level == 1 && | |
5253 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
5254 | goto skip; | |
1c4850e2 YZ |
5255 | } |
5256 | ||
b9fab919 | 5257 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
1c4850e2 YZ |
5258 | btrfs_tree_unlock(next); |
5259 | free_extent_buffer(next); | |
5260 | next = NULL; | |
94fcca9f | 5261 | *lookup_info = 1; |
1c4850e2 YZ |
5262 | } |
5263 | ||
5264 | if (!next) { | |
5265 | if (reada && level == 1) | |
5266 | reada_walk_down(trans, root, wc, path); | |
1b7ec85e JB |
5267 | next = read_tree_block(fs_info, bytenr, root->root_key.objectid, |
5268 | generation, level - 1, &first_key); | |
64c043de LB |
5269 | if (IS_ERR(next)) { |
5270 | return PTR_ERR(next); | |
5271 | } else if (!extent_buffer_uptodate(next)) { | |
416bc658 | 5272 | free_extent_buffer(next); |
97d9a8a4 | 5273 | return -EIO; |
416bc658 | 5274 | } |
1c4850e2 | 5275 | btrfs_tree_lock(next); |
1c4850e2 YZ |
5276 | } |
5277 | ||
5278 | level--; | |
4867268c JB |
5279 | ASSERT(level == btrfs_header_level(next)); |
5280 | if (level != btrfs_header_level(next)) { | |
5281 | btrfs_err(root->fs_info, "mismatched level"); | |
5282 | ret = -EIO; | |
5283 | goto out_unlock; | |
5284 | } | |
1c4850e2 YZ |
5285 | path->nodes[level] = next; |
5286 | path->slots[level] = 0; | |
ac5887c8 | 5287 | path->locks[level] = BTRFS_WRITE_LOCK; |
1c4850e2 YZ |
5288 | wc->level = level; |
5289 | if (wc->level == 1) | |
5290 | wc->reada_slot = 0; | |
5291 | return 0; | |
5292 | skip: | |
5293 | wc->refs[level - 1] = 0; | |
5294 | wc->flags[level - 1] = 0; | |
94fcca9f YZ |
5295 | if (wc->stage == DROP_REFERENCE) { |
5296 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { | |
5297 | parent = path->nodes[level]->start; | |
5298 | } else { | |
4867268c | 5299 | ASSERT(root->root_key.objectid == |
94fcca9f | 5300 | btrfs_header_owner(path->nodes[level])); |
4867268c JB |
5301 | if (root->root_key.objectid != |
5302 | btrfs_header_owner(path->nodes[level])) { | |
5303 | btrfs_err(root->fs_info, | |
5304 | "mismatched block owner"); | |
5305 | ret = -EIO; | |
5306 | goto out_unlock; | |
5307 | } | |
94fcca9f YZ |
5308 | parent = 0; |
5309 | } | |
1c4850e2 | 5310 | |
78c52d9e JB |
5311 | /* |
5312 | * If we had a drop_progress we need to verify the refs are set | |
5313 | * as expected. If we find our ref then we know that from here | |
5314 | * on out everything should be correct, and we can clear the | |
5315 | * ->restarted flag. | |
5316 | */ | |
5317 | if (wc->restarted) { | |
5318 | ret = check_ref_exists(trans, root, bytenr, parent, | |
5319 | level - 1); | |
5320 | if (ret < 0) | |
5321 | goto out_unlock; | |
5322 | if (ret == 0) | |
5323 | goto no_delete; | |
5324 | ret = 0; | |
5325 | wc->restarted = 0; | |
5326 | } | |
5327 | ||
2cd86d30 QW |
5328 | /* |
5329 | * Reloc tree doesn't contribute to qgroup numbers, and we have | |
5330 | * already accounted them at merge time (replace_path), | |
5331 | * thus we could skip expensive subtree trace here. | |
5332 | */ | |
5333 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && | |
5334 | need_account) { | |
deb40627 | 5335 | ret = btrfs_qgroup_trace_subtree(trans, next, |
33d1f05c | 5336 | generation, level - 1); |
1152651a | 5337 | if (ret) { |
0b246afa | 5338 | btrfs_err_rl(fs_info, |
5d163e0e JM |
5339 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
5340 | ret); | |
1152651a MF |
5341 | } |
5342 | } | |
aea6f028 JB |
5343 | |
5344 | /* | |
5345 | * We need to update the next key in our walk control so we can | |
5346 | * update the drop_progress key accordingly. We don't care if | |
5347 | * find_next_key doesn't find a key because that means we're at | |
5348 | * the end and are going to clean up now. | |
5349 | */ | |
5350 | wc->drop_level = level; | |
5351 | find_next_key(path, level, &wc->drop_progress); | |
5352 | ||
ffd4bb2a QW |
5353 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
5354 | fs_info->nodesize, parent); | |
5355 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); | |
5356 | ret = btrfs_free_extent(trans, &ref); | |
4867268c JB |
5357 | if (ret) |
5358 | goto out_unlock; | |
1c4850e2 | 5359 | } |
78c52d9e | 5360 | no_delete: |
4867268c JB |
5361 | *lookup_info = 1; |
5362 | ret = 1; | |
5363 | ||
5364 | out_unlock: | |
1c4850e2 YZ |
5365 | btrfs_tree_unlock(next); |
5366 | free_extent_buffer(next); | |
4867268c JB |
5367 | |
5368 | return ret; | |
1c4850e2 YZ |
5369 | } |
5370 | ||
2c47e605 | 5371 | /* |
2c016dc2 | 5372 | * helper to process tree block while walking up the tree. |
2c47e605 YZ |
5373 | * |
5374 | * when wc->stage == DROP_REFERENCE, this function drops | |
5375 | * reference count on the block. | |
5376 | * | |
5377 | * when wc->stage == UPDATE_BACKREF, this function changes | |
5378 | * wc->stage back to DROP_REFERENCE if we changed wc->stage | |
5379 | * to UPDATE_BACKREF previously while processing the block. | |
5380 | * | |
5381 | * NOTE: return value 1 means we should stop walking up. | |
5382 | */ | |
5383 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, | |
5384 | struct btrfs_root *root, | |
5385 | struct btrfs_path *path, | |
5386 | struct walk_control *wc) | |
5387 | { | |
0b246afa | 5388 | struct btrfs_fs_info *fs_info = root->fs_info; |
f0486c68 | 5389 | int ret; |
2c47e605 YZ |
5390 | int level = wc->level; |
5391 | struct extent_buffer *eb = path->nodes[level]; | |
5392 | u64 parent = 0; | |
5393 | ||
5394 | if (wc->stage == UPDATE_BACKREF) { | |
5395 | BUG_ON(wc->shared_level < level); | |
5396 | if (level < wc->shared_level) | |
5397 | goto out; | |
5398 | ||
2c47e605 YZ |
5399 | ret = find_next_key(path, level + 1, &wc->update_progress); |
5400 | if (ret > 0) | |
5401 | wc->update_ref = 0; | |
5402 | ||
5403 | wc->stage = DROP_REFERENCE; | |
5404 | wc->shared_level = -1; | |
5405 | path->slots[level] = 0; | |
5406 | ||
5407 | /* | |
5408 | * check reference count again if the block isn't locked. | |
5409 | * we should start walking down the tree again if reference | |
5410 | * count is one. | |
5411 | */ | |
5412 | if (!path->locks[level]) { | |
5413 | BUG_ON(level == 0); | |
5414 | btrfs_tree_lock(eb); | |
ac5887c8 | 5415 | path->locks[level] = BTRFS_WRITE_LOCK; |
2c47e605 | 5416 | |
2ff7e61e | 5417 | ret = btrfs_lookup_extent_info(trans, fs_info, |
3173a18f | 5418 | eb->start, level, 1, |
2c47e605 YZ |
5419 | &wc->refs[level], |
5420 | &wc->flags[level]); | |
79787eaa JM |
5421 | if (ret < 0) { |
5422 | btrfs_tree_unlock_rw(eb, path->locks[level]); | |
3268a246 | 5423 | path->locks[level] = 0; |
79787eaa JM |
5424 | return ret; |
5425 | } | |
2c47e605 YZ |
5426 | BUG_ON(wc->refs[level] == 0); |
5427 | if (wc->refs[level] == 1) { | |
bd681513 | 5428 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
3268a246 | 5429 | path->locks[level] = 0; |
2c47e605 YZ |
5430 | return 1; |
5431 | } | |
f82d02d9 | 5432 | } |
2c47e605 | 5433 | } |
f82d02d9 | 5434 | |
2c47e605 YZ |
5435 | /* wc->stage == DROP_REFERENCE */ |
5436 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); | |
5d4f98a2 | 5437 | |
2c47e605 YZ |
5438 | if (wc->refs[level] == 1) { |
5439 | if (level == 0) { | |
5440 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
e339a6b0 | 5441 | ret = btrfs_dec_ref(trans, root, eb, 1); |
2c47e605 | 5442 | else |
e339a6b0 | 5443 | ret = btrfs_dec_ref(trans, root, eb, 0); |
79787eaa | 5444 | BUG_ON(ret); /* -ENOMEM */ |
c4140cbf QW |
5445 | if (is_fstree(root->root_key.objectid)) { |
5446 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); | |
5447 | if (ret) { | |
5448 | btrfs_err_rl(fs_info, | |
5449 | "error %d accounting leaf items, quota is out of sync, rescan required", | |
5d163e0e | 5450 | ret); |
c4140cbf | 5451 | } |
1152651a | 5452 | } |
2c47e605 | 5453 | } |
6a884d7d | 5454 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
2c47e605 YZ |
5455 | if (!path->locks[level] && |
5456 | btrfs_header_generation(eb) == trans->transid) { | |
5457 | btrfs_tree_lock(eb); | |
ac5887c8 | 5458 | path->locks[level] = BTRFS_WRITE_LOCK; |
2c47e605 | 5459 | } |
6a884d7d | 5460 | btrfs_clean_tree_block(eb); |
2c47e605 YZ |
5461 | } |
5462 | ||
5463 | if (eb == root->node) { | |
5464 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
5465 | parent = eb->start; | |
65c6e82b QW |
5466 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
5467 | goto owner_mismatch; | |
2c47e605 YZ |
5468 | } else { |
5469 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
5470 | parent = path->nodes[level + 1]->start; | |
65c6e82b QW |
5471 | else if (root->root_key.objectid != |
5472 | btrfs_header_owner(path->nodes[level + 1])) | |
5473 | goto owner_mismatch; | |
f82d02d9 | 5474 | } |
f82d02d9 | 5475 | |
5581a51a | 5476 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
2c47e605 YZ |
5477 | out: |
5478 | wc->refs[level] = 0; | |
5479 | wc->flags[level] = 0; | |
f0486c68 | 5480 | return 0; |
65c6e82b QW |
5481 | |
5482 | owner_mismatch: | |
5483 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", | |
5484 | btrfs_header_owner(eb), root->root_key.objectid); | |
5485 | return -EUCLEAN; | |
2c47e605 YZ |
5486 | } |
5487 | ||
5488 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |
5489 | struct btrfs_root *root, | |
5490 | struct btrfs_path *path, | |
5491 | struct walk_control *wc) | |
5492 | { | |
2c47e605 | 5493 | int level = wc->level; |
94fcca9f | 5494 | int lookup_info = 1; |
2c47e605 YZ |
5495 | int ret; |
5496 | ||
5497 | while (level >= 0) { | |
94fcca9f | 5498 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
2c47e605 YZ |
5499 | if (ret > 0) |
5500 | break; | |
5501 | ||
5502 | if (level == 0) | |
5503 | break; | |
5504 | ||
7a7965f8 YZ |
5505 | if (path->slots[level] >= |
5506 | btrfs_header_nritems(path->nodes[level])) | |
5507 | break; | |
5508 | ||
94fcca9f | 5509 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
1c4850e2 YZ |
5510 | if (ret > 0) { |
5511 | path->slots[level]++; | |
5512 | continue; | |
90d2c51d MX |
5513 | } else if (ret < 0) |
5514 | return ret; | |
1c4850e2 | 5515 | level = wc->level; |
f82d02d9 | 5516 | } |
f82d02d9 YZ |
5517 | return 0; |
5518 | } | |
5519 | ||
d397712b | 5520 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
98ed5174 | 5521 | struct btrfs_root *root, |
f82d02d9 | 5522 | struct btrfs_path *path, |
2c47e605 | 5523 | struct walk_control *wc, int max_level) |
20524f02 | 5524 | { |
2c47e605 | 5525 | int level = wc->level; |
20524f02 | 5526 | int ret; |
9f3a7427 | 5527 | |
2c47e605 YZ |
5528 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
5529 | while (level < max_level && path->nodes[level]) { | |
5530 | wc->level = level; | |
5531 | if (path->slots[level] + 1 < | |
5532 | btrfs_header_nritems(path->nodes[level])) { | |
5533 | path->slots[level]++; | |
20524f02 CM |
5534 | return 0; |
5535 | } else { | |
2c47e605 YZ |
5536 | ret = walk_up_proc(trans, root, path, wc); |
5537 | if (ret > 0) | |
5538 | return 0; | |
65c6e82b QW |
5539 | if (ret < 0) |
5540 | return ret; | |
bd56b302 | 5541 | |
2c47e605 | 5542 | if (path->locks[level]) { |
bd681513 CM |
5543 | btrfs_tree_unlock_rw(path->nodes[level], |
5544 | path->locks[level]); | |
2c47e605 | 5545 | path->locks[level] = 0; |
f82d02d9 | 5546 | } |
2c47e605 YZ |
5547 | free_extent_buffer(path->nodes[level]); |
5548 | path->nodes[level] = NULL; | |
5549 | level++; | |
20524f02 CM |
5550 | } |
5551 | } | |
5552 | return 1; | |
5553 | } | |
5554 | ||
9aca1d51 | 5555 | /* |
2c47e605 YZ |
5556 | * drop a subvolume tree. |
5557 | * | |
5558 | * this function traverses the tree freeing any blocks that only | |
5559 | * referenced by the tree. | |
5560 | * | |
5561 | * when a shared tree block is found. this function decreases its | |
5562 | * reference count by one. if update_ref is true, this function | |
5563 | * also make sure backrefs for the shared block and all lower level | |
5564 | * blocks are properly updated. | |
9d1a2a3a DS |
5565 | * |
5566 | * If called with for_reloc == 0, may exit early with -EAGAIN | |
9aca1d51 | 5567 | */ |
0078a9f9 | 5568 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc) |
20524f02 | 5569 | { |
ab8d0fc4 | 5570 | struct btrfs_fs_info *fs_info = root->fs_info; |
5caf2a00 | 5571 | struct btrfs_path *path; |
2c47e605 | 5572 | struct btrfs_trans_handle *trans; |
ab8d0fc4 | 5573 | struct btrfs_root *tree_root = fs_info->tree_root; |
9f3a7427 | 5574 | struct btrfs_root_item *root_item = &root->root_item; |
2c47e605 YZ |
5575 | struct walk_control *wc; |
5576 | struct btrfs_key key; | |
5577 | int err = 0; | |
5578 | int ret; | |
5579 | int level; | |
d29a9f62 | 5580 | bool root_dropped = false; |
20524f02 | 5581 | |
4fd786e6 | 5582 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
1152651a | 5583 | |
5caf2a00 | 5584 | path = btrfs_alloc_path(); |
cb1b69f4 TI |
5585 | if (!path) { |
5586 | err = -ENOMEM; | |
5587 | goto out; | |
5588 | } | |
20524f02 | 5589 | |
2c47e605 | 5590 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
38a1a919 MF |
5591 | if (!wc) { |
5592 | btrfs_free_path(path); | |
cb1b69f4 TI |
5593 | err = -ENOMEM; |
5594 | goto out; | |
38a1a919 | 5595 | } |
2c47e605 | 5596 | |
f3e3d9cc QW |
5597 | /* |
5598 | * Use join to avoid potential EINTR from transaction start. See | |
5599 | * wait_reserve_ticket and the whole reservation callchain. | |
5600 | */ | |
5601 | if (for_reloc) | |
5602 | trans = btrfs_join_transaction(tree_root); | |
5603 | else | |
5604 | trans = btrfs_start_transaction(tree_root, 0); | |
79787eaa JM |
5605 | if (IS_ERR(trans)) { |
5606 | err = PTR_ERR(trans); | |
5607 | goto out_free; | |
5608 | } | |
98d5dc13 | 5609 | |
0568e82d JB |
5610 | err = btrfs_run_delayed_items(trans); |
5611 | if (err) | |
5612 | goto out_end_trans; | |
5613 | ||
83354f07 JB |
5614 | /* |
5615 | * This will help us catch people modifying the fs tree while we're | |
5616 | * dropping it. It is unsafe to mess with the fs tree while it's being | |
5617 | * dropped as we unlock the root node and parent nodes as we walk down | |
5618 | * the tree, assuming nothing will change. If something does change | |
5619 | * then we'll have stale information and drop references to blocks we've | |
5620 | * already dropped. | |
5621 | */ | |
5622 | set_bit(BTRFS_ROOT_DELETING, &root->state); | |
9f3a7427 | 5623 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
2c47e605 | 5624 | level = btrfs_header_level(root->node); |
5d4f98a2 | 5625 | path->nodes[level] = btrfs_lock_root_node(root); |
9f3a7427 | 5626 | path->slots[level] = 0; |
ac5887c8 | 5627 | path->locks[level] = BTRFS_WRITE_LOCK; |
2c47e605 YZ |
5628 | memset(&wc->update_progress, 0, |
5629 | sizeof(wc->update_progress)); | |
9f3a7427 | 5630 | } else { |
9f3a7427 | 5631 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
2c47e605 YZ |
5632 | memcpy(&wc->update_progress, &key, |
5633 | sizeof(wc->update_progress)); | |
5634 | ||
c8422684 | 5635 | level = btrfs_root_drop_level(root_item); |
2c47e605 | 5636 | BUG_ON(level == 0); |
6702ed49 | 5637 | path->lowest_level = level; |
2c47e605 YZ |
5638 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5639 | path->lowest_level = 0; | |
5640 | if (ret < 0) { | |
5641 | err = ret; | |
79787eaa | 5642 | goto out_end_trans; |
9f3a7427 | 5643 | } |
1c4850e2 | 5644 | WARN_ON(ret > 0); |
2c47e605 | 5645 | |
7d9eb12c CM |
5646 | /* |
5647 | * unlock our path, this is safe because only this | |
5648 | * function is allowed to delete this snapshot | |
5649 | */ | |
5d4f98a2 | 5650 | btrfs_unlock_up_safe(path, 0); |
2c47e605 YZ |
5651 | |
5652 | level = btrfs_header_level(root->node); | |
5653 | while (1) { | |
5654 | btrfs_tree_lock(path->nodes[level]); | |
ac5887c8 | 5655 | path->locks[level] = BTRFS_WRITE_LOCK; |
2c47e605 | 5656 | |
2ff7e61e | 5657 | ret = btrfs_lookup_extent_info(trans, fs_info, |
2c47e605 | 5658 | path->nodes[level]->start, |
3173a18f | 5659 | level, 1, &wc->refs[level], |
2c47e605 | 5660 | &wc->flags[level]); |
79787eaa JM |
5661 | if (ret < 0) { |
5662 | err = ret; | |
5663 | goto out_end_trans; | |
5664 | } | |
2c47e605 YZ |
5665 | BUG_ON(wc->refs[level] == 0); |
5666 | ||
c8422684 | 5667 | if (level == btrfs_root_drop_level(root_item)) |
2c47e605 YZ |
5668 | break; |
5669 | ||
5670 | btrfs_tree_unlock(path->nodes[level]); | |
fec386ac | 5671 | path->locks[level] = 0; |
2c47e605 YZ |
5672 | WARN_ON(wc->refs[level] != 1); |
5673 | level--; | |
5674 | } | |
9f3a7427 | 5675 | } |
2c47e605 | 5676 | |
78c52d9e | 5677 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
2c47e605 YZ |
5678 | wc->level = level; |
5679 | wc->shared_level = -1; | |
5680 | wc->stage = DROP_REFERENCE; | |
5681 | wc->update_ref = update_ref; | |
5682 | wc->keep_locks = 0; | |
0b246afa | 5683 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
2c47e605 | 5684 | |
d397712b | 5685 | while (1) { |
9d1a2a3a | 5686 | |
2c47e605 YZ |
5687 | ret = walk_down_tree(trans, root, path, wc); |
5688 | if (ret < 0) { | |
5689 | err = ret; | |
20524f02 | 5690 | break; |
2c47e605 | 5691 | } |
9aca1d51 | 5692 | |
2c47e605 YZ |
5693 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
5694 | if (ret < 0) { | |
5695 | err = ret; | |
20524f02 | 5696 | break; |
2c47e605 YZ |
5697 | } |
5698 | ||
5699 | if (ret > 0) { | |
5700 | BUG_ON(wc->stage != DROP_REFERENCE); | |
e7a84565 CM |
5701 | break; |
5702 | } | |
2c47e605 YZ |
5703 | |
5704 | if (wc->stage == DROP_REFERENCE) { | |
aea6f028 JB |
5705 | wc->drop_level = wc->level; |
5706 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], | |
5707 | &wc->drop_progress, | |
5708 | path->slots[wc->drop_level]); | |
5709 | } | |
5710 | btrfs_cpu_key_to_disk(&root_item->drop_progress, | |
5711 | &wc->drop_progress); | |
c8422684 | 5712 | btrfs_set_root_drop_level(root_item, wc->drop_level); |
2c47e605 YZ |
5713 | |
5714 | BUG_ON(wc->level == 0); | |
3a45bb20 | 5715 | if (btrfs_should_end_transaction(trans) || |
2ff7e61e | 5716 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
2c47e605 YZ |
5717 | ret = btrfs_update_root(trans, tree_root, |
5718 | &root->root_key, | |
5719 | root_item); | |
79787eaa | 5720 | if (ret) { |
66642832 | 5721 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
5722 | err = ret; |
5723 | goto out_end_trans; | |
5724 | } | |
2c47e605 | 5725 | |
3a45bb20 | 5726 | btrfs_end_transaction_throttle(trans); |
2ff7e61e | 5727 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
ab8d0fc4 JM |
5728 | btrfs_debug(fs_info, |
5729 | "drop snapshot early exit"); | |
3c8f2422 JB |
5730 | err = -EAGAIN; |
5731 | goto out_free; | |
5732 | } | |
5733 | ||
18d3bff4 JB |
5734 | /* |
5735 | * Use join to avoid potential EINTR from transaction | |
5736 | * start. See wait_reserve_ticket and the whole | |
5737 | * reservation callchain. | |
5738 | */ | |
5739 | if (for_reloc) | |
5740 | trans = btrfs_join_transaction(tree_root); | |
5741 | else | |
5742 | trans = btrfs_start_transaction(tree_root, 0); | |
79787eaa JM |
5743 | if (IS_ERR(trans)) { |
5744 | err = PTR_ERR(trans); | |
5745 | goto out_free; | |
5746 | } | |
c3e69d58 | 5747 | } |
20524f02 | 5748 | } |
b3b4aa74 | 5749 | btrfs_release_path(path); |
79787eaa JM |
5750 | if (err) |
5751 | goto out_end_trans; | |
2c47e605 | 5752 | |
ab9ce7d4 | 5753 | ret = btrfs_del_root(trans, &root->root_key); |
79787eaa | 5754 | if (ret) { |
66642832 | 5755 | btrfs_abort_transaction(trans, ret); |
e19182c0 | 5756 | err = ret; |
79787eaa JM |
5757 | goto out_end_trans; |
5758 | } | |
2c47e605 | 5759 | |
76dda93c | 5760 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
cb517eab MX |
5761 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
5762 | NULL, NULL); | |
79787eaa | 5763 | if (ret < 0) { |
66642832 | 5764 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
5765 | err = ret; |
5766 | goto out_end_trans; | |
5767 | } else if (ret > 0) { | |
84cd948c JB |
5768 | /* if we fail to delete the orphan item this time |
5769 | * around, it'll get picked up the next time. | |
5770 | * | |
5771 | * The most common failure here is just -ENOENT. | |
5772 | */ | |
5773 | btrfs_del_orphan_item(trans, tree_root, | |
5774 | root->root_key.objectid); | |
76dda93c YZ |
5775 | } |
5776 | } | |
5777 | ||
a3cf0e43 QW |
5778 | /* |
5779 | * This subvolume is going to be completely dropped, and won't be | |
5780 | * recorded as dirty roots, thus pertrans meta rsv will not be freed at | |
5781 | * commit transaction time. So free it here manually. | |
5782 | */ | |
5783 | btrfs_qgroup_convert_reserved_meta(root, INT_MAX); | |
5784 | btrfs_qgroup_free_meta_all_pertrans(root); | |
5785 | ||
8c38938c | 5786 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) |
2b9dbef2 | 5787 | btrfs_add_dropped_root(trans, root); |
8c38938c | 5788 | else |
00246528 | 5789 | btrfs_put_root(root); |
d29a9f62 | 5790 | root_dropped = true; |
79787eaa | 5791 | out_end_trans: |
3a45bb20 | 5792 | btrfs_end_transaction_throttle(trans); |
79787eaa | 5793 | out_free: |
2c47e605 | 5794 | kfree(wc); |
5caf2a00 | 5795 | btrfs_free_path(path); |
cb1b69f4 | 5796 | out: |
d29a9f62 JB |
5797 | /* |
5798 | * So if we need to stop dropping the snapshot for whatever reason we | |
5799 | * need to make sure to add it back to the dead root list so that we | |
5800 | * keep trying to do the work later. This also cleans up roots if we | |
5801 | * don't have it in the radix (like when we recover after a power fail | |
5802 | * or unmount) so we don't leak memory. | |
5803 | */ | |
897ca819 | 5804 | if (!for_reloc && !root_dropped) |
d29a9f62 | 5805 | btrfs_add_dead_root(root); |
2c536799 | 5806 | return err; |
20524f02 | 5807 | } |
9078a3e1 | 5808 | |
2c47e605 YZ |
5809 | /* |
5810 | * drop subtree rooted at tree block 'node'. | |
5811 | * | |
5812 | * NOTE: this function will unlock and release tree block 'node' | |
66d7e7f0 | 5813 | * only used by relocation code |
2c47e605 | 5814 | */ |
f82d02d9 YZ |
5815 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
5816 | struct btrfs_root *root, | |
5817 | struct extent_buffer *node, | |
5818 | struct extent_buffer *parent) | |
5819 | { | |
0b246afa | 5820 | struct btrfs_fs_info *fs_info = root->fs_info; |
f82d02d9 | 5821 | struct btrfs_path *path; |
2c47e605 | 5822 | struct walk_control *wc; |
f82d02d9 YZ |
5823 | int level; |
5824 | int parent_level; | |
5825 | int ret = 0; | |
5826 | int wret; | |
5827 | ||
2c47e605 YZ |
5828 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
5829 | ||
f82d02d9 | 5830 | path = btrfs_alloc_path(); |
db5b493a TI |
5831 | if (!path) |
5832 | return -ENOMEM; | |
f82d02d9 | 5833 | |
2c47e605 | 5834 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
db5b493a TI |
5835 | if (!wc) { |
5836 | btrfs_free_path(path); | |
5837 | return -ENOMEM; | |
5838 | } | |
2c47e605 | 5839 | |
b9447ef8 | 5840 | btrfs_assert_tree_locked(parent); |
f82d02d9 | 5841 | parent_level = btrfs_header_level(parent); |
67439dad | 5842 | atomic_inc(&parent->refs); |
f82d02d9 YZ |
5843 | path->nodes[parent_level] = parent; |
5844 | path->slots[parent_level] = btrfs_header_nritems(parent); | |
5845 | ||
b9447ef8 | 5846 | btrfs_assert_tree_locked(node); |
f82d02d9 | 5847 | level = btrfs_header_level(node); |
f82d02d9 YZ |
5848 | path->nodes[level] = node; |
5849 | path->slots[level] = 0; | |
ac5887c8 | 5850 | path->locks[level] = BTRFS_WRITE_LOCK; |
2c47e605 YZ |
5851 | |
5852 | wc->refs[parent_level] = 1; | |
5853 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
5854 | wc->level = level; | |
5855 | wc->shared_level = -1; | |
5856 | wc->stage = DROP_REFERENCE; | |
5857 | wc->update_ref = 0; | |
5858 | wc->keep_locks = 1; | |
0b246afa | 5859 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
f82d02d9 YZ |
5860 | |
5861 | while (1) { | |
2c47e605 YZ |
5862 | wret = walk_down_tree(trans, root, path, wc); |
5863 | if (wret < 0) { | |
f82d02d9 | 5864 | ret = wret; |
f82d02d9 | 5865 | break; |
2c47e605 | 5866 | } |
f82d02d9 | 5867 | |
2c47e605 | 5868 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
f82d02d9 YZ |
5869 | if (wret < 0) |
5870 | ret = wret; | |
5871 | if (wret != 0) | |
5872 | break; | |
5873 | } | |
5874 | ||
2c47e605 | 5875 | kfree(wc); |
f82d02d9 YZ |
5876 | btrfs_free_path(path); |
5877 | return ret; | |
5878 | } | |
5879 | ||
6d07bcec MX |
5880 | /* |
5881 | * helper to account the unused space of all the readonly block group in the | |
633c0aad | 5882 | * space_info. takes mirrors into account. |
6d07bcec | 5883 | */ |
633c0aad | 5884 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
6d07bcec | 5885 | { |
32da5386 | 5886 | struct btrfs_block_group *block_group; |
6d07bcec MX |
5887 | u64 free_bytes = 0; |
5888 | int factor; | |
5889 | ||
01327610 | 5890 | /* It's df, we don't care if it's racy */ |
633c0aad JB |
5891 | if (list_empty(&sinfo->ro_bgs)) |
5892 | return 0; | |
5893 | ||
5894 | spin_lock(&sinfo->lock); | |
5895 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { | |
6d07bcec MX |
5896 | spin_lock(&block_group->lock); |
5897 | ||
5898 | if (!block_group->ro) { | |
5899 | spin_unlock(&block_group->lock); | |
5900 | continue; | |
5901 | } | |
5902 | ||
46df06b8 | 5903 | factor = btrfs_bg_type_to_factor(block_group->flags); |
b3470b5d | 5904 | free_bytes += (block_group->length - |
bf38be65 | 5905 | block_group->used) * factor; |
6d07bcec MX |
5906 | |
5907 | spin_unlock(&block_group->lock); | |
5908 | } | |
6d07bcec MX |
5909 | spin_unlock(&sinfo->lock); |
5910 | ||
5911 | return free_bytes; | |
5912 | } | |
5913 | ||
2ff7e61e JM |
5914 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
5915 | u64 start, u64 end) | |
acce952b | 5916 | { |
2ff7e61e | 5917 | return unpin_extent_range(fs_info, start, end, false); |
acce952b | 5918 | } |
5919 | ||
499f377f JM |
5920 | /* |
5921 | * It used to be that old block groups would be left around forever. | |
5922 | * Iterating over them would be enough to trim unused space. Since we | |
5923 | * now automatically remove them, we also need to iterate over unallocated | |
5924 | * space. | |
5925 | * | |
5926 | * We don't want a transaction for this since the discard may take a | |
5927 | * substantial amount of time. We don't require that a transaction be | |
5928 | * running, but we do need to take a running transaction into account | |
fee7acc3 JM |
5929 | * to ensure that we're not discarding chunks that were released or |
5930 | * allocated in the current transaction. | |
499f377f JM |
5931 | * |
5932 | * Holding the chunks lock will prevent other threads from allocating | |
5933 | * or releasing chunks, but it won't prevent a running transaction | |
5934 | * from committing and releasing the memory that the pending chunks | |
5935 | * list head uses. For that, we need to take a reference to the | |
fee7acc3 JM |
5936 | * transaction and hold the commit root sem. We only need to hold |
5937 | * it while performing the free space search since we have already | |
5938 | * held back allocations. | |
499f377f | 5939 | */ |
8103d10b | 5940 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
499f377f | 5941 | { |
8103d10b | 5942 | u64 start = SZ_1M, len = 0, end = 0; |
499f377f JM |
5943 | int ret; |
5944 | ||
5945 | *trimmed = 0; | |
5946 | ||
0be88e36 JM |
5947 | /* Discard not supported = nothing to do. */ |
5948 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) | |
5949 | return 0; | |
5950 | ||
52042d8e | 5951 | /* Not writable = nothing to do. */ |
ebbede42 | 5952 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
499f377f JM |
5953 | return 0; |
5954 | ||
5955 | /* No free space = nothing to do. */ | |
5956 | if (device->total_bytes <= device->bytes_used) | |
5957 | return 0; | |
5958 | ||
5959 | ret = 0; | |
5960 | ||
5961 | while (1) { | |
fb456252 | 5962 | struct btrfs_fs_info *fs_info = device->fs_info; |
499f377f JM |
5963 | u64 bytes; |
5964 | ||
5965 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); | |
5966 | if (ret) | |
fee7acc3 | 5967 | break; |
499f377f | 5968 | |
929be17a NB |
5969 | find_first_clear_extent_bit(&device->alloc_state, start, |
5970 | &start, &end, | |
5971 | CHUNK_TRIMMED | CHUNK_ALLOCATED); | |
53460a45 | 5972 | |
c57dd1f2 QW |
5973 | /* Check if there are any CHUNK_* bits left */ |
5974 | if (start > device->total_bytes) { | |
5975 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
5976 | btrfs_warn_in_rcu(fs_info, | |
5977 | "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu", | |
5978 | start, end - start + 1, | |
5979 | rcu_str_deref(device->name), | |
5980 | device->total_bytes); | |
5981 | mutex_unlock(&fs_info->chunk_mutex); | |
5982 | ret = 0; | |
5983 | break; | |
5984 | } | |
5985 | ||
53460a45 NB |
5986 | /* Ensure we skip the reserved area in the first 1M */ |
5987 | start = max_t(u64, start, SZ_1M); | |
5988 | ||
929be17a NB |
5989 | /* |
5990 | * If find_first_clear_extent_bit find a range that spans the | |
5991 | * end of the device it will set end to -1, in this case it's up | |
5992 | * to the caller to trim the value to the size of the device. | |
5993 | */ | |
5994 | end = min(end, device->total_bytes - 1); | |
53460a45 | 5995 | |
929be17a | 5996 | len = end - start + 1; |
499f377f | 5997 | |
929be17a NB |
5998 | /* We didn't find any extents */ |
5999 | if (!len) { | |
499f377f | 6000 | mutex_unlock(&fs_info->chunk_mutex); |
929be17a | 6001 | ret = 0; |
499f377f JM |
6002 | break; |
6003 | } | |
6004 | ||
929be17a NB |
6005 | ret = btrfs_issue_discard(device->bdev, start, len, |
6006 | &bytes); | |
6007 | if (!ret) | |
6008 | set_extent_bits(&device->alloc_state, start, | |
6009 | start + bytes - 1, | |
6010 | CHUNK_TRIMMED); | |
499f377f JM |
6011 | mutex_unlock(&fs_info->chunk_mutex); |
6012 | ||
6013 | if (ret) | |
6014 | break; | |
6015 | ||
6016 | start += len; | |
6017 | *trimmed += bytes; | |
6018 | ||
6019 | if (fatal_signal_pending(current)) { | |
6020 | ret = -ERESTARTSYS; | |
6021 | break; | |
6022 | } | |
6023 | ||
6024 | cond_resched(); | |
6025 | } | |
6026 | ||
6027 | return ret; | |
6028 | } | |
6029 | ||
93bba24d QW |
6030 | /* |
6031 | * Trim the whole filesystem by: | |
6032 | * 1) trimming the free space in each block group | |
6033 | * 2) trimming the unallocated space on each device | |
6034 | * | |
6035 | * This will also continue trimming even if a block group or device encounters | |
6036 | * an error. The return value will be the last error, or 0 if nothing bad | |
6037 | * happens. | |
6038 | */ | |
2ff7e61e | 6039 | int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) |
f7039b1d | 6040 | { |
23608d51 | 6041 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
32da5386 | 6042 | struct btrfs_block_group *cache = NULL; |
499f377f | 6043 | struct btrfs_device *device; |
f7039b1d | 6044 | u64 group_trimmed; |
07301df7 | 6045 | u64 range_end = U64_MAX; |
f7039b1d LD |
6046 | u64 start; |
6047 | u64 end; | |
6048 | u64 trimmed = 0; | |
93bba24d QW |
6049 | u64 bg_failed = 0; |
6050 | u64 dev_failed = 0; | |
6051 | int bg_ret = 0; | |
6052 | int dev_ret = 0; | |
f7039b1d LD |
6053 | int ret = 0; |
6054 | ||
07301df7 QW |
6055 | /* |
6056 | * Check range overflow if range->len is set. | |
6057 | * The default range->len is U64_MAX. | |
6058 | */ | |
6059 | if (range->len != U64_MAX && | |
6060 | check_add_overflow(range->start, range->len, &range_end)) | |
6061 | return -EINVAL; | |
6062 | ||
6ba9fc8e | 6063 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
2e405ad8 | 6064 | for (; cache; cache = btrfs_next_block_group(cache)) { |
b3470b5d | 6065 | if (cache->start >= range_end) { |
f7039b1d LD |
6066 | btrfs_put_block_group(cache); |
6067 | break; | |
6068 | } | |
6069 | ||
b3470b5d DS |
6070 | start = max(range->start, cache->start); |
6071 | end = min(range_end, cache->start + cache->length); | |
f7039b1d LD |
6072 | |
6073 | if (end - start >= range->minlen) { | |
32da5386 | 6074 | if (!btrfs_block_group_done(cache)) { |
676f1f75 | 6075 | ret = btrfs_cache_block_group(cache, 0); |
1be41b78 | 6076 | if (ret) { |
93bba24d QW |
6077 | bg_failed++; |
6078 | bg_ret = ret; | |
6079 | continue; | |
1be41b78 | 6080 | } |
676f1f75 | 6081 | ret = btrfs_wait_block_group_cache_done(cache); |
1be41b78 | 6082 | if (ret) { |
93bba24d QW |
6083 | bg_failed++; |
6084 | bg_ret = ret; | |
6085 | continue; | |
1be41b78 | 6086 | } |
f7039b1d LD |
6087 | } |
6088 | ret = btrfs_trim_block_group(cache, | |
6089 | &group_trimmed, | |
6090 | start, | |
6091 | end, | |
6092 | range->minlen); | |
6093 | ||
6094 | trimmed += group_trimmed; | |
6095 | if (ret) { | |
93bba24d QW |
6096 | bg_failed++; |
6097 | bg_ret = ret; | |
6098 | continue; | |
f7039b1d LD |
6099 | } |
6100 | } | |
f7039b1d LD |
6101 | } |
6102 | ||
93bba24d QW |
6103 | if (bg_failed) |
6104 | btrfs_warn(fs_info, | |
6105 | "failed to trim %llu block group(s), last error %d", | |
6106 | bg_failed, bg_ret); | |
23608d51 AJ |
6107 | |
6108 | mutex_lock(&fs_devices->device_list_mutex); | |
6109 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
16a200f6 AJ |
6110 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
6111 | continue; | |
6112 | ||
8103d10b | 6113 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
93bba24d QW |
6114 | if (ret) { |
6115 | dev_failed++; | |
6116 | dev_ret = ret; | |
499f377f | 6117 | break; |
93bba24d | 6118 | } |
499f377f JM |
6119 | |
6120 | trimmed += group_trimmed; | |
6121 | } | |
23608d51 | 6122 | mutex_unlock(&fs_devices->device_list_mutex); |
499f377f | 6123 | |
93bba24d QW |
6124 | if (dev_failed) |
6125 | btrfs_warn(fs_info, | |
6126 | "failed to trim %llu device(s), last error %d", | |
6127 | dev_failed, dev_ret); | |
f7039b1d | 6128 | range->len = trimmed; |
93bba24d QW |
6129 | if (bg_ret) |
6130 | return bg_ret; | |
6131 | return dev_ret; | |
f7039b1d | 6132 | } |