]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 CM |
4 | */ |
5 | ||
e20d96d6 | 6 | #include <linux/fs.h> |
d98237b3 | 7 | #include <linux/blkdev.h> |
0f7d52f4 | 8 | #include <linux/radix-tree.h> |
35b7e476 | 9 | #include <linux/writeback.h> |
ce9adaa5 | 10 | #include <linux/workqueue.h> |
a74a4b97 | 11 | #include <linux/kthread.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
784b4e29 | 13 | #include <linux/migrate.h> |
7a36ddec | 14 | #include <linux/ratelimit.h> |
6463fe58 | 15 | #include <linux/uuid.h> |
803b2f54 | 16 | #include <linux/semaphore.h> |
540adea3 | 17 | #include <linux/error-injection.h> |
9678c543 | 18 | #include <linux/crc32c.h> |
b89f6d1f | 19 | #include <linux/sched/mm.h> |
7e75bf3f | 20 | #include <asm/unaligned.h> |
6d97c6e3 | 21 | #include <crypto/hash.h> |
eb60ceac CM |
22 | #include "ctree.h" |
23 | #include "disk-io.h" | |
e089f05c | 24 | #include "transaction.h" |
0f7d52f4 | 25 | #include "btrfs_inode.h" |
0b86a832 | 26 | #include "volumes.h" |
db94535d | 27 | #include "print-tree.h" |
925baedd | 28 | #include "locking.h" |
e02119d5 | 29 | #include "tree-log.h" |
fa9c0d79 | 30 | #include "free-space-cache.h" |
70f6d82e | 31 | #include "free-space-tree.h" |
21adbd5c | 32 | #include "check-integrity.h" |
606686ee | 33 | #include "rcu-string.h" |
8dabb742 | 34 | #include "dev-replace.h" |
53b381b3 | 35 | #include "raid56.h" |
5ac1d209 | 36 | #include "sysfs.h" |
fcebe456 | 37 | #include "qgroup.h" |
ebb8765b | 38 | #include "compression.h" |
557ea5dd | 39 | #include "tree-checker.h" |
fd708b81 | 40 | #include "ref-verify.h" |
aac0023c | 41 | #include "block-group.h" |
b0643e59 | 42 | #include "discard.h" |
f603bb94 | 43 | #include "space-info.h" |
b70f5097 | 44 | #include "zoned.h" |
139e8cd3 | 45 | #include "subpage.h" |
eb60ceac | 46 | |
319e4d06 QW |
47 | #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ |
48 | BTRFS_HEADER_FLAG_RELOC |\ | |
49 | BTRFS_SUPER_FLAG_ERROR |\ | |
50 | BTRFS_SUPER_FLAG_SEEDING |\ | |
e2731e55 AJ |
51 | BTRFS_SUPER_FLAG_METADUMP |\ |
52 | BTRFS_SUPER_FLAG_METADUMP_V2) | |
319e4d06 | 53 | |
8b712842 | 54 | static void end_workqueue_fn(struct btrfs_work *work); |
143bede5 | 55 | static void btrfs_destroy_ordered_extents(struct btrfs_root *root); |
acce952b | 56 | static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, |
2ff7e61e | 57 | struct btrfs_fs_info *fs_info); |
143bede5 | 58 | static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); |
2ff7e61e | 59 | static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, |
acce952b | 60 | struct extent_io_tree *dirty_pages, |
61 | int mark); | |
2ff7e61e | 62 | static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, |
acce952b | 63 | struct extent_io_tree *pinned_extents); |
2ff7e61e JM |
64 | static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); |
65 | static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); | |
ce9adaa5 | 66 | |
d352ac68 | 67 | /* |
97eb6b69 DS |
68 | * btrfs_end_io_wq structs are used to do processing in task context when an IO |
69 | * is complete. This is used during reads to verify checksums, and it is used | |
d352ac68 CM |
70 | * by writes to insert metadata for new file extents after IO is complete. |
71 | */ | |
97eb6b69 | 72 | struct btrfs_end_io_wq { |
ce9adaa5 CM |
73 | struct bio *bio; |
74 | bio_end_io_t *end_io; | |
75 | void *private; | |
76 | struct btrfs_fs_info *info; | |
4e4cbee9 | 77 | blk_status_t status; |
bfebd8b5 | 78 | enum btrfs_wq_endio_type metadata; |
8b712842 | 79 | struct btrfs_work work; |
ce9adaa5 | 80 | }; |
0da5468f | 81 | |
97eb6b69 DS |
82 | static struct kmem_cache *btrfs_end_io_wq_cache; |
83 | ||
84 | int __init btrfs_end_io_wq_init(void) | |
85 | { | |
86 | btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", | |
87 | sizeof(struct btrfs_end_io_wq), | |
88 | 0, | |
fba4b697 | 89 | SLAB_MEM_SPREAD, |
97eb6b69 DS |
90 | NULL); |
91 | if (!btrfs_end_io_wq_cache) | |
92 | return -ENOMEM; | |
93 | return 0; | |
94 | } | |
95 | ||
e67c718b | 96 | void __cold btrfs_end_io_wq_exit(void) |
97eb6b69 | 97 | { |
5598e900 | 98 | kmem_cache_destroy(btrfs_end_io_wq_cache); |
97eb6b69 DS |
99 | } |
100 | ||
141386e1 JB |
101 | static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) |
102 | { | |
103 | if (fs_info->csum_shash) | |
104 | crypto_free_shash(fs_info->csum_shash); | |
105 | } | |
106 | ||
d352ac68 CM |
107 | /* |
108 | * async submit bios are used to offload expensive checksumming | |
109 | * onto the worker threads. They checksum file and metadata bios | |
110 | * just before they are sent down the IO stack. | |
111 | */ | |
44b8bd7e | 112 | struct async_submit_bio { |
8896a08d | 113 | struct inode *inode; |
44b8bd7e | 114 | struct bio *bio; |
a758781d | 115 | extent_submit_bio_start_t *submit_bio_start; |
44b8bd7e | 116 | int mirror_num; |
1941b64b QW |
117 | |
118 | /* Optional parameter for submit_bio_start used by direct io */ | |
119 | u64 dio_file_offset; | |
8b712842 | 120 | struct btrfs_work work; |
4e4cbee9 | 121 | blk_status_t status; |
44b8bd7e CM |
122 | }; |
123 | ||
85d4e461 CM |
124 | /* |
125 | * Lockdep class keys for extent_buffer->lock's in this root. For a given | |
126 | * eb, the lockdep key is determined by the btrfs_root it belongs to and | |
127 | * the level the eb occupies in the tree. | |
128 | * | |
129 | * Different roots are used for different purposes and may nest inside each | |
130 | * other and they require separate keysets. As lockdep keys should be | |
131 | * static, assign keysets according to the purpose of the root as indicated | |
4fd786e6 MT |
132 | * by btrfs_root->root_key.objectid. This ensures that all special purpose |
133 | * roots have separate keysets. | |
4008c04a | 134 | * |
85d4e461 CM |
135 | * Lock-nesting across peer nodes is always done with the immediate parent |
136 | * node locked thus preventing deadlock. As lockdep doesn't know this, use | |
137 | * subclass to avoid triggering lockdep warning in such cases. | |
4008c04a | 138 | * |
85d4e461 CM |
139 | * The key is set by the readpage_end_io_hook after the buffer has passed |
140 | * csum validation but before the pages are unlocked. It is also set by | |
141 | * btrfs_init_new_buffer on freshly allocated blocks. | |
4008c04a | 142 | * |
85d4e461 CM |
143 | * We also add a check to make sure the highest level of the tree is the |
144 | * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code | |
145 | * needs update as well. | |
4008c04a CM |
146 | */ |
147 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
148 | # if BTRFS_MAX_LEVEL != 8 | |
149 | # error | |
150 | # endif | |
85d4e461 | 151 | |
ab1405aa DS |
152 | #define DEFINE_LEVEL(stem, level) \ |
153 | .names[level] = "btrfs-" stem "-0" #level, | |
154 | ||
155 | #define DEFINE_NAME(stem) \ | |
156 | DEFINE_LEVEL(stem, 0) \ | |
157 | DEFINE_LEVEL(stem, 1) \ | |
158 | DEFINE_LEVEL(stem, 2) \ | |
159 | DEFINE_LEVEL(stem, 3) \ | |
160 | DEFINE_LEVEL(stem, 4) \ | |
161 | DEFINE_LEVEL(stem, 5) \ | |
162 | DEFINE_LEVEL(stem, 6) \ | |
163 | DEFINE_LEVEL(stem, 7) | |
164 | ||
85d4e461 CM |
165 | static struct btrfs_lockdep_keyset { |
166 | u64 id; /* root objectid */ | |
ab1405aa | 167 | /* Longest entry: btrfs-free-space-00 */ |
387824af DS |
168 | char names[BTRFS_MAX_LEVEL][20]; |
169 | struct lock_class_key keys[BTRFS_MAX_LEVEL]; | |
85d4e461 | 170 | } btrfs_lockdep_keysets[] = { |
ab1405aa DS |
171 | { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, |
172 | { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, | |
173 | { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, | |
174 | { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, | |
ab1405aa DS |
175 | { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, |
176 | { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, | |
177 | { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, | |
178 | { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, | |
179 | { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, | |
180 | { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, | |
181 | { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, | |
182 | { .id = 0, DEFINE_NAME("tree") }, | |
4008c04a | 183 | }; |
85d4e461 | 184 | |
ab1405aa DS |
185 | #undef DEFINE_LEVEL |
186 | #undef DEFINE_NAME | |
85d4e461 CM |
187 | |
188 | void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, | |
189 | int level) | |
190 | { | |
191 | struct btrfs_lockdep_keyset *ks; | |
192 | ||
193 | BUG_ON(level >= ARRAY_SIZE(ks->keys)); | |
194 | ||
195 | /* find the matching keyset, id 0 is the default entry */ | |
196 | for (ks = btrfs_lockdep_keysets; ks->id; ks++) | |
197 | if (ks->id == objectid) | |
198 | break; | |
199 | ||
200 | lockdep_set_class_and_name(&eb->lock, | |
201 | &ks->keys[level], ks->names[level]); | |
202 | } | |
203 | ||
4008c04a CM |
204 | #endif |
205 | ||
d352ac68 | 206 | /* |
2996e1f8 | 207 | * Compute the csum of a btree block and store the result to provided buffer. |
d352ac68 | 208 | */ |
c67b3892 | 209 | static void csum_tree_block(struct extent_buffer *buf, u8 *result) |
19c00ddc | 210 | { |
d5178578 | 211 | struct btrfs_fs_info *fs_info = buf->fs_info; |
7280305e | 212 | const int num_pages = num_extent_pages(buf); |
a26663e7 | 213 | const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); |
d5178578 | 214 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
19c00ddc | 215 | char *kaddr; |
e9be5a30 | 216 | int i; |
d5178578 JT |
217 | |
218 | shash->tfm = fs_info->csum_shash; | |
219 | crypto_shash_init(shash); | |
a26663e7 | 220 | kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); |
e9be5a30 | 221 | crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, |
a26663e7 | 222 | first_page_part - BTRFS_CSUM_SIZE); |
19c00ddc | 223 | |
e9be5a30 DS |
224 | for (i = 1; i < num_pages; i++) { |
225 | kaddr = page_address(buf->pages[i]); | |
226 | crypto_shash_update(shash, kaddr, PAGE_SIZE); | |
19c00ddc | 227 | } |
71a63551 | 228 | memset(result, 0, BTRFS_CSUM_SIZE); |
d5178578 | 229 | crypto_shash_final(shash, result); |
19c00ddc CM |
230 | } |
231 | ||
d352ac68 CM |
232 | /* |
233 | * we can't consider a given block up to date unless the transid of the | |
234 | * block matches the transid in the parent node's pointer. This is how we | |
235 | * detect blocks that either didn't get written at all or got written | |
236 | * in the wrong place. | |
237 | */ | |
1259ab75 | 238 | static int verify_parent_transid(struct extent_io_tree *io_tree, |
b9fab919 CM |
239 | struct extent_buffer *eb, u64 parent_transid, |
240 | int atomic) | |
1259ab75 | 241 | { |
2ac55d41 | 242 | struct extent_state *cached_state = NULL; |
1259ab75 CM |
243 | int ret; |
244 | ||
245 | if (!parent_transid || btrfs_header_generation(eb) == parent_transid) | |
246 | return 0; | |
247 | ||
b9fab919 CM |
248 | if (atomic) |
249 | return -EAGAIN; | |
250 | ||
2ac55d41 | 251 | lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, |
ff13db41 | 252 | &cached_state); |
0b32f4bb | 253 | if (extent_buffer_uptodate(eb) && |
1259ab75 CM |
254 | btrfs_header_generation(eb) == parent_transid) { |
255 | ret = 0; | |
256 | goto out; | |
257 | } | |
94647322 DS |
258 | btrfs_err_rl(eb->fs_info, |
259 | "parent transid verify failed on %llu wanted %llu found %llu", | |
260 | eb->start, | |
29549aec | 261 | parent_transid, btrfs_header_generation(eb)); |
1259ab75 | 262 | ret = 1; |
35b22c19 | 263 | clear_extent_buffer_uptodate(eb); |
33958dc6 | 264 | out: |
2ac55d41 | 265 | unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, |
e43bbe5e | 266 | &cached_state); |
1259ab75 | 267 | return ret; |
1259ab75 CM |
268 | } |
269 | ||
e7e16f48 JT |
270 | static bool btrfs_supported_super_csum(u16 csum_type) |
271 | { | |
272 | switch (csum_type) { | |
273 | case BTRFS_CSUM_TYPE_CRC32: | |
3951e7f0 | 274 | case BTRFS_CSUM_TYPE_XXHASH: |
3831bf00 | 275 | case BTRFS_CSUM_TYPE_SHA256: |
352ae07b | 276 | case BTRFS_CSUM_TYPE_BLAKE2: |
e7e16f48 JT |
277 | return true; |
278 | default: | |
279 | return false; | |
280 | } | |
281 | } | |
282 | ||
1104a885 DS |
283 | /* |
284 | * Return 0 if the superblock checksum type matches the checksum value of that | |
285 | * algorithm. Pass the raw disk superblock data. | |
286 | */ | |
ab8d0fc4 JM |
287 | static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, |
288 | char *raw_disk_sb) | |
1104a885 DS |
289 | { |
290 | struct btrfs_super_block *disk_sb = | |
291 | (struct btrfs_super_block *)raw_disk_sb; | |
51bce6c9 | 292 | char result[BTRFS_CSUM_SIZE]; |
d5178578 JT |
293 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
294 | ||
295 | shash->tfm = fs_info->csum_shash; | |
1104a885 | 296 | |
51bce6c9 JT |
297 | /* |
298 | * The super_block structure does not span the whole | |
299 | * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is | |
300 | * filled with zeros and is included in the checksum. | |
301 | */ | |
fd08001f EB |
302 | crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, |
303 | BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); | |
1104a885 | 304 | |
55fc29be | 305 | if (memcmp(disk_sb->csum, result, fs_info->csum_size)) |
51bce6c9 | 306 | return 1; |
1104a885 | 307 | |
e7e16f48 | 308 | return 0; |
1104a885 DS |
309 | } |
310 | ||
e064d5e9 | 311 | int btrfs_verify_level_key(struct extent_buffer *eb, int level, |
448de471 | 312 | struct btrfs_key *first_key, u64 parent_transid) |
581c1760 | 313 | { |
e064d5e9 | 314 | struct btrfs_fs_info *fs_info = eb->fs_info; |
581c1760 QW |
315 | int found_level; |
316 | struct btrfs_key found_key; | |
317 | int ret; | |
318 | ||
319 | found_level = btrfs_header_level(eb); | |
320 | if (found_level != level) { | |
63489055 QW |
321 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
322 | KERN_ERR "BTRFS: tree level check failed\n"); | |
581c1760 QW |
323 | btrfs_err(fs_info, |
324 | "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", | |
325 | eb->start, level, found_level); | |
581c1760 QW |
326 | return -EIO; |
327 | } | |
328 | ||
329 | if (!first_key) | |
330 | return 0; | |
331 | ||
5d41be6f QW |
332 | /* |
333 | * For live tree block (new tree blocks in current transaction), | |
334 | * we need proper lock context to avoid race, which is impossible here. | |
335 | * So we only checks tree blocks which is read from disk, whose | |
336 | * generation <= fs_info->last_trans_committed. | |
337 | */ | |
338 | if (btrfs_header_generation(eb) > fs_info->last_trans_committed) | |
339 | return 0; | |
62fdaa52 QW |
340 | |
341 | /* We have @first_key, so this @eb must have at least one item */ | |
342 | if (btrfs_header_nritems(eb) == 0) { | |
343 | btrfs_err(fs_info, | |
344 | "invalid tree nritems, bytenr=%llu nritems=0 expect >0", | |
345 | eb->start); | |
346 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
347 | return -EUCLEAN; | |
348 | } | |
349 | ||
581c1760 QW |
350 | if (found_level) |
351 | btrfs_node_key_to_cpu(eb, &found_key, 0); | |
352 | else | |
353 | btrfs_item_key_to_cpu(eb, &found_key, 0); | |
354 | ret = btrfs_comp_cpu_keys(first_key, &found_key); | |
355 | ||
581c1760 | 356 | if (ret) { |
63489055 QW |
357 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
358 | KERN_ERR "BTRFS: tree first key check failed\n"); | |
581c1760 | 359 | btrfs_err(fs_info, |
ff76a864 LB |
360 | "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", |
361 | eb->start, parent_transid, first_key->objectid, | |
362 | first_key->type, first_key->offset, | |
363 | found_key.objectid, found_key.type, | |
364 | found_key.offset); | |
581c1760 | 365 | } |
581c1760 QW |
366 | return ret; |
367 | } | |
368 | ||
d352ac68 CM |
369 | /* |
370 | * helper to read a given tree block, doing retries as required when | |
371 | * the checksums don't match and we have alternate mirrors to try. | |
581c1760 QW |
372 | * |
373 | * @parent_transid: expected transid, skip check if 0 | |
374 | * @level: expected level, mandatory check | |
375 | * @first_key: expected key of first slot, skip check if NULL | |
d352ac68 | 376 | */ |
5ab12d1f | 377 | static int btree_read_extent_buffer_pages(struct extent_buffer *eb, |
581c1760 QW |
378 | u64 parent_transid, int level, |
379 | struct btrfs_key *first_key) | |
f188591e | 380 | { |
5ab12d1f | 381 | struct btrfs_fs_info *fs_info = eb->fs_info; |
f188591e | 382 | struct extent_io_tree *io_tree; |
ea466794 | 383 | int failed = 0; |
f188591e CM |
384 | int ret; |
385 | int num_copies = 0; | |
386 | int mirror_num = 0; | |
ea466794 | 387 | int failed_mirror = 0; |
f188591e | 388 | |
0b246afa | 389 | io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; |
f188591e | 390 | while (1) { |
f8397d69 | 391 | clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); |
c2ccfbc6 | 392 | ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); |
256dd1bb | 393 | if (!ret) { |
581c1760 | 394 | if (verify_parent_transid(io_tree, eb, |
b9fab919 | 395 | parent_transid, 0)) |
256dd1bb | 396 | ret = -EIO; |
e064d5e9 | 397 | else if (btrfs_verify_level_key(eb, level, |
448de471 | 398 | first_key, parent_transid)) |
581c1760 QW |
399 | ret = -EUCLEAN; |
400 | else | |
401 | break; | |
256dd1bb | 402 | } |
d397712b | 403 | |
0b246afa | 404 | num_copies = btrfs_num_copies(fs_info, |
f188591e | 405 | eb->start, eb->len); |
4235298e | 406 | if (num_copies == 1) |
ea466794 | 407 | break; |
4235298e | 408 | |
5cf1ab56 JB |
409 | if (!failed_mirror) { |
410 | failed = 1; | |
411 | failed_mirror = eb->read_mirror; | |
412 | } | |
413 | ||
f188591e | 414 | mirror_num++; |
ea466794 JB |
415 | if (mirror_num == failed_mirror) |
416 | mirror_num++; | |
417 | ||
4235298e | 418 | if (mirror_num > num_copies) |
ea466794 | 419 | break; |
f188591e | 420 | } |
ea466794 | 421 | |
c0901581 | 422 | if (failed && !ret && failed_mirror) |
20a1fbf9 | 423 | btrfs_repair_eb_io_failure(eb, failed_mirror); |
ea466794 JB |
424 | |
425 | return ret; | |
f188591e | 426 | } |
19c00ddc | 427 | |
eca0f6f6 QW |
428 | static int csum_one_extent_buffer(struct extent_buffer *eb) |
429 | { | |
430 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
431 | u8 result[BTRFS_CSUM_SIZE]; | |
432 | int ret; | |
433 | ||
434 | ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, | |
435 | offsetof(struct btrfs_header, fsid), | |
436 | BTRFS_FSID_SIZE) == 0); | |
437 | csum_tree_block(eb, result); | |
438 | ||
439 | if (btrfs_header_level(eb)) | |
440 | ret = btrfs_check_node(eb); | |
441 | else | |
442 | ret = btrfs_check_leaf_full(eb); | |
443 | ||
1b417c74 QW |
444 | if (ret < 0) |
445 | goto error; | |
446 | ||
447 | /* | |
448 | * Also check the generation, the eb reached here must be newer than | |
449 | * last committed. Or something seriously wrong happened. | |
450 | */ | |
451 | if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { | |
452 | ret = -EUCLEAN; | |
eca0f6f6 | 453 | btrfs_err(fs_info, |
1b417c74 QW |
454 | "block=%llu bad generation, have %llu expect > %llu", |
455 | eb->start, btrfs_header_generation(eb), | |
456 | fs_info->last_trans_committed); | |
457 | goto error; | |
eca0f6f6 QW |
458 | } |
459 | write_extent_buffer(eb, result, 0, fs_info->csum_size); | |
460 | ||
461 | return 0; | |
1b417c74 QW |
462 | |
463 | error: | |
464 | btrfs_print_tree(eb, 0); | |
465 | btrfs_err(fs_info, "block=%llu write time tree block corruption detected", | |
466 | eb->start); | |
467 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
468 | return ret; | |
eca0f6f6 QW |
469 | } |
470 | ||
471 | /* Checksum all dirty extent buffers in one bio_vec */ | |
472 | static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, | |
473 | struct bio_vec *bvec) | |
474 | { | |
475 | struct page *page = bvec->bv_page; | |
476 | u64 bvec_start = page_offset(page) + bvec->bv_offset; | |
477 | u64 cur; | |
478 | int ret = 0; | |
479 | ||
480 | for (cur = bvec_start; cur < bvec_start + bvec->bv_len; | |
481 | cur += fs_info->nodesize) { | |
482 | struct extent_buffer *eb; | |
483 | bool uptodate; | |
484 | ||
485 | eb = find_extent_buffer(fs_info, cur); | |
486 | uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, | |
487 | fs_info->nodesize); | |
488 | ||
489 | /* A dirty eb shouldn't disappear from buffer_radix */ | |
490 | if (WARN_ON(!eb)) | |
491 | return -EUCLEAN; | |
492 | ||
493 | if (WARN_ON(cur != btrfs_header_bytenr(eb))) { | |
494 | free_extent_buffer(eb); | |
495 | return -EUCLEAN; | |
496 | } | |
497 | if (WARN_ON(!uptodate)) { | |
498 | free_extent_buffer(eb); | |
499 | return -EUCLEAN; | |
500 | } | |
501 | ||
502 | ret = csum_one_extent_buffer(eb); | |
503 | free_extent_buffer(eb); | |
504 | if (ret < 0) | |
505 | return ret; | |
506 | } | |
507 | return ret; | |
508 | } | |
509 | ||
d352ac68 | 510 | /* |
ac303b69 QW |
511 | * Checksum a dirty tree block before IO. This has extra checks to make sure |
512 | * we only fill in the checksum field in the first page of a multi-page block. | |
513 | * For subpage extent buffers we need bvec to also read the offset in the page. | |
d352ac68 | 514 | */ |
ac303b69 | 515 | static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) |
19c00ddc | 516 | { |
ac303b69 | 517 | struct page *page = bvec->bv_page; |
4eee4fa4 | 518 | u64 start = page_offset(page); |
19c00ddc | 519 | u64 found_start; |
19c00ddc | 520 | struct extent_buffer *eb; |
eca0f6f6 QW |
521 | |
522 | if (fs_info->sectorsize < PAGE_SIZE) | |
523 | return csum_dirty_subpage_buffers(fs_info, bvec); | |
f188591e | 524 | |
4f2de97a JB |
525 | eb = (struct extent_buffer *)page->private; |
526 | if (page != eb->pages[0]) | |
527 | return 0; | |
0f805531 | 528 | |
19c00ddc | 529 | found_start = btrfs_header_bytenr(eb); |
d3575156 NA |
530 | |
531 | if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { | |
532 | WARN_ON(found_start != 0); | |
533 | return 0; | |
534 | } | |
535 | ||
0f805531 AL |
536 | /* |
537 | * Please do not consolidate these warnings into a single if. | |
538 | * It is useful to know what went wrong. | |
539 | */ | |
540 | if (WARN_ON(found_start != start)) | |
541 | return -EUCLEAN; | |
542 | if (WARN_ON(!PageUptodate(page))) | |
543 | return -EUCLEAN; | |
544 | ||
eca0f6f6 | 545 | return csum_one_extent_buffer(eb); |
19c00ddc CM |
546 | } |
547 | ||
b0c9b3b0 | 548 | static int check_tree_block_fsid(struct extent_buffer *eb) |
2b82032c | 549 | { |
b0c9b3b0 | 550 | struct btrfs_fs_info *fs_info = eb->fs_info; |
944d3f9f | 551 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; |
44880fdc | 552 | u8 fsid[BTRFS_FSID_SIZE]; |
944d3f9f | 553 | u8 *metadata_uuid; |
2b82032c | 554 | |
9a8658e3 DS |
555 | read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), |
556 | BTRFS_FSID_SIZE); | |
944d3f9f NB |
557 | /* |
558 | * Checking the incompat flag is only valid for the current fs. For | |
559 | * seed devices it's forbidden to have their uuid changed so reading | |
560 | * ->fsid in this case is fine | |
561 | */ | |
562 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) | |
563 | metadata_uuid = fs_devices->metadata_uuid; | |
564 | else | |
565 | metadata_uuid = fs_devices->fsid; | |
566 | ||
567 | if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) | |
568 | return 0; | |
569 | ||
570 | list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) | |
571 | if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) | |
572 | return 0; | |
573 | ||
574 | return 1; | |
2b82032c YZ |
575 | } |
576 | ||
77bf40a2 QW |
577 | /* Do basic extent buffer checks at read time */ |
578 | static int validate_extent_buffer(struct extent_buffer *eb) | |
ce9adaa5 | 579 | { |
77bf40a2 | 580 | struct btrfs_fs_info *fs_info = eb->fs_info; |
ce9adaa5 | 581 | u64 found_start; |
77bf40a2 QW |
582 | const u32 csum_size = fs_info->csum_size; |
583 | u8 found_level; | |
2996e1f8 | 584 | u8 result[BTRFS_CSUM_SIZE]; |
dfd29eed | 585 | const u8 *header_csum; |
77bf40a2 | 586 | int ret = 0; |
ea466794 | 587 | |
ce9adaa5 | 588 | found_start = btrfs_header_bytenr(eb); |
727011e0 | 589 | if (found_start != eb->start) { |
893bf4b1 SY |
590 | btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", |
591 | eb->start, found_start); | |
f188591e | 592 | ret = -EIO; |
77bf40a2 | 593 | goto out; |
ce9adaa5 | 594 | } |
b0c9b3b0 | 595 | if (check_tree_block_fsid(eb)) { |
02873e43 ZL |
596 | btrfs_err_rl(fs_info, "bad fsid on block %llu", |
597 | eb->start); | |
1259ab75 | 598 | ret = -EIO; |
77bf40a2 | 599 | goto out; |
1259ab75 | 600 | } |
ce9adaa5 | 601 | found_level = btrfs_header_level(eb); |
1c24c3ce | 602 | if (found_level >= BTRFS_MAX_LEVEL) { |
893bf4b1 SY |
603 | btrfs_err(fs_info, "bad tree block level %d on %llu", |
604 | (int)btrfs_header_level(eb), eb->start); | |
1c24c3ce | 605 | ret = -EIO; |
77bf40a2 | 606 | goto out; |
1c24c3ce | 607 | } |
ce9adaa5 | 608 | |
c67b3892 | 609 | csum_tree_block(eb, result); |
dfd29eed DS |
610 | header_csum = page_address(eb->pages[0]) + |
611 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); | |
a826d6dc | 612 | |
dfd29eed | 613 | if (memcmp(result, header_csum, csum_size) != 0) { |
2996e1f8 | 614 | btrfs_warn_rl(fs_info, |
ff14aa79 DS |
615 | "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", |
616 | eb->start, | |
dfd29eed | 617 | CSUM_FMT_VALUE(csum_size, header_csum), |
35be8851 JT |
618 | CSUM_FMT_VALUE(csum_size, result), |
619 | btrfs_header_level(eb)); | |
2996e1f8 | 620 | ret = -EUCLEAN; |
77bf40a2 | 621 | goto out; |
2996e1f8 JT |
622 | } |
623 | ||
a826d6dc JB |
624 | /* |
625 | * If this is a leaf block and it is corrupt, set the corrupt bit so | |
626 | * that we don't try and read the other copies of this block, just | |
627 | * return -EIO. | |
628 | */ | |
1c4360ee | 629 | if (found_level == 0 && btrfs_check_leaf_full(eb)) { |
a826d6dc JB |
630 | set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); |
631 | ret = -EIO; | |
632 | } | |
ce9adaa5 | 633 | |
813fd1dc | 634 | if (found_level > 0 && btrfs_check_node(eb)) |
053ab70f LB |
635 | ret = -EIO; |
636 | ||
0b32f4bb JB |
637 | if (!ret) |
638 | set_extent_buffer_uptodate(eb); | |
75391f0d QW |
639 | else |
640 | btrfs_err(fs_info, | |
641 | "block=%llu read time tree block corruption detected", | |
642 | eb->start); | |
77bf40a2 QW |
643 | out: |
644 | return ret; | |
645 | } | |
646 | ||
371cdc07 QW |
647 | static int validate_subpage_buffer(struct page *page, u64 start, u64 end, |
648 | int mirror) | |
649 | { | |
650 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); | |
651 | struct extent_buffer *eb; | |
652 | bool reads_done; | |
653 | int ret = 0; | |
654 | ||
655 | /* | |
656 | * We don't allow bio merge for subpage metadata read, so we should | |
657 | * only get one eb for each endio hook. | |
658 | */ | |
659 | ASSERT(end == start + fs_info->nodesize - 1); | |
660 | ASSERT(PagePrivate(page)); | |
661 | ||
662 | eb = find_extent_buffer(fs_info, start); | |
663 | /* | |
664 | * When we are reading one tree block, eb must have been inserted into | |
665 | * the radix tree. If not, something is wrong. | |
666 | */ | |
667 | ASSERT(eb); | |
668 | ||
669 | reads_done = atomic_dec_and_test(&eb->io_pages); | |
670 | /* Subpage read must finish in page read */ | |
671 | ASSERT(reads_done); | |
672 | ||
673 | eb->read_mirror = mirror; | |
674 | if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { | |
675 | ret = -EIO; | |
676 | goto err; | |
677 | } | |
678 | ret = validate_extent_buffer(eb); | |
679 | if (ret < 0) | |
680 | goto err; | |
681 | ||
682 | if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) | |
683 | btree_readahead_hook(eb, ret); | |
684 | ||
685 | set_extent_buffer_uptodate(eb); | |
686 | ||
687 | free_extent_buffer(eb); | |
688 | return ret; | |
689 | err: | |
690 | /* | |
691 | * end_bio_extent_readpage decrements io_pages in case of error, | |
692 | * make sure it has something to decrement. | |
693 | */ | |
694 | atomic_inc(&eb->io_pages); | |
695 | clear_extent_buffer_uptodate(eb); | |
696 | free_extent_buffer(eb); | |
697 | return ret; | |
698 | } | |
699 | ||
8e1dc982 | 700 | int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, |
77bf40a2 QW |
701 | struct page *page, u64 start, u64 end, |
702 | int mirror) | |
703 | { | |
704 | struct extent_buffer *eb; | |
705 | int ret = 0; | |
706 | int reads_done; | |
707 | ||
708 | ASSERT(page->private); | |
371cdc07 QW |
709 | |
710 | if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE) | |
711 | return validate_subpage_buffer(page, start, end, mirror); | |
712 | ||
77bf40a2 QW |
713 | eb = (struct extent_buffer *)page->private; |
714 | ||
715 | /* | |
716 | * The pending IO might have been the only thing that kept this buffer | |
717 | * in memory. Make sure we have a ref for all this other checks | |
718 | */ | |
719 | atomic_inc(&eb->refs); | |
720 | ||
721 | reads_done = atomic_dec_and_test(&eb->io_pages); | |
722 | if (!reads_done) | |
723 | goto err; | |
724 | ||
725 | eb->read_mirror = mirror; | |
726 | if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { | |
727 | ret = -EIO; | |
728 | goto err; | |
729 | } | |
730 | ret = validate_extent_buffer(eb); | |
ce9adaa5 | 731 | err: |
79fb65a1 JB |
732 | if (reads_done && |
733 | test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) | |
d48d71aa | 734 | btree_readahead_hook(eb, ret); |
4bb31e92 | 735 | |
53b381b3 DW |
736 | if (ret) { |
737 | /* | |
738 | * our io error hook is going to dec the io pages | |
739 | * again, we have to make sure it has something | |
740 | * to decrement | |
741 | */ | |
742 | atomic_inc(&eb->io_pages); | |
0b32f4bb | 743 | clear_extent_buffer_uptodate(eb); |
53b381b3 | 744 | } |
0b32f4bb | 745 | free_extent_buffer(eb); |
77bf40a2 | 746 | |
f188591e | 747 | return ret; |
ce9adaa5 CM |
748 | } |
749 | ||
4246a0b6 | 750 | static void end_workqueue_bio(struct bio *bio) |
ce9adaa5 | 751 | { |
97eb6b69 | 752 | struct btrfs_end_io_wq *end_io_wq = bio->bi_private; |
ce9adaa5 | 753 | struct btrfs_fs_info *fs_info; |
9e0af237 | 754 | struct btrfs_workqueue *wq; |
ce9adaa5 | 755 | |
ce9adaa5 | 756 | fs_info = end_io_wq->info; |
4e4cbee9 | 757 | end_io_wq->status = bio->bi_status; |
d20f7043 | 758 | |
cfe94440 | 759 | if (btrfs_op(bio) == BTRFS_MAP_WRITE) { |
a0cac0ec | 760 | if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) |
9e0af237 | 761 | wq = fs_info->endio_meta_write_workers; |
a0cac0ec | 762 | else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) |
9e0af237 | 763 | wq = fs_info->endio_freespace_worker; |
a0cac0ec | 764 | else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) |
9e0af237 | 765 | wq = fs_info->endio_raid56_workers; |
a0cac0ec | 766 | else |
9e0af237 | 767 | wq = fs_info->endio_write_workers; |
d20f7043 | 768 | } else { |
5c047a69 | 769 | if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) |
9e0af237 | 770 | wq = fs_info->endio_raid56_workers; |
a0cac0ec | 771 | else if (end_io_wq->metadata) |
9e0af237 | 772 | wq = fs_info->endio_meta_workers; |
a0cac0ec | 773 | else |
9e0af237 | 774 | wq = fs_info->endio_workers; |
d20f7043 | 775 | } |
9e0af237 | 776 | |
a0cac0ec | 777 | btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); |
9e0af237 | 778 | btrfs_queue_work(wq, &end_io_wq->work); |
ce9adaa5 CM |
779 | } |
780 | ||
4e4cbee9 | 781 | blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, |
bfebd8b5 | 782 | enum btrfs_wq_endio_type metadata) |
0b86a832 | 783 | { |
97eb6b69 | 784 | struct btrfs_end_io_wq *end_io_wq; |
8b110e39 | 785 | |
97eb6b69 | 786 | end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); |
ce9adaa5 | 787 | if (!end_io_wq) |
4e4cbee9 | 788 | return BLK_STS_RESOURCE; |
ce9adaa5 CM |
789 | |
790 | end_io_wq->private = bio->bi_private; | |
791 | end_io_wq->end_io = bio->bi_end_io; | |
22c59948 | 792 | end_io_wq->info = info; |
4e4cbee9 | 793 | end_io_wq->status = 0; |
ce9adaa5 | 794 | end_io_wq->bio = bio; |
22c59948 | 795 | end_io_wq->metadata = metadata; |
ce9adaa5 CM |
796 | |
797 | bio->bi_private = end_io_wq; | |
798 | bio->bi_end_io = end_workqueue_bio; | |
22c59948 CM |
799 | return 0; |
800 | } | |
801 | ||
4a69a410 CM |
802 | static void run_one_async_start(struct btrfs_work *work) |
803 | { | |
4a69a410 | 804 | struct async_submit_bio *async; |
4e4cbee9 | 805 | blk_status_t ret; |
4a69a410 CM |
806 | |
807 | async = container_of(work, struct async_submit_bio, work); | |
1941b64b QW |
808 | ret = async->submit_bio_start(async->inode, async->bio, |
809 | async->dio_file_offset); | |
79787eaa | 810 | if (ret) |
4e4cbee9 | 811 | async->status = ret; |
4a69a410 CM |
812 | } |
813 | ||
06ea01b1 DS |
814 | /* |
815 | * In order to insert checksums into the metadata in large chunks, we wait | |
816 | * until bio submission time. All the pages in the bio are checksummed and | |
817 | * sums are attached onto the ordered extent record. | |
818 | * | |
819 | * At IO completion time the csums attached on the ordered extent record are | |
820 | * inserted into the tree. | |
821 | */ | |
4a69a410 | 822 | static void run_one_async_done(struct btrfs_work *work) |
8b712842 | 823 | { |
8b712842 | 824 | struct async_submit_bio *async; |
06ea01b1 DS |
825 | struct inode *inode; |
826 | blk_status_t ret; | |
8b712842 CM |
827 | |
828 | async = container_of(work, struct async_submit_bio, work); | |
8896a08d | 829 | inode = async->inode; |
4854ddd0 | 830 | |
bb7ab3b9 | 831 | /* If an error occurred we just want to clean up the bio and move on */ |
4e4cbee9 CH |
832 | if (async->status) { |
833 | async->bio->bi_status = async->status; | |
4246a0b6 | 834 | bio_endio(async->bio); |
79787eaa JM |
835 | return; |
836 | } | |
837 | ||
ec39f769 CM |
838 | /* |
839 | * All of the bios that pass through here are from async helpers. | |
840 | * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. | |
841 | * This changes nothing when cgroups aren't in use. | |
842 | */ | |
843 | async->bio->bi_opf |= REQ_CGROUP_PUNT; | |
08635bae | 844 | ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); |
06ea01b1 DS |
845 | if (ret) { |
846 | async->bio->bi_status = ret; | |
847 | bio_endio(async->bio); | |
848 | } | |
4a69a410 CM |
849 | } |
850 | ||
851 | static void run_one_async_free(struct btrfs_work *work) | |
852 | { | |
853 | struct async_submit_bio *async; | |
854 | ||
855 | async = container_of(work, struct async_submit_bio, work); | |
8b712842 CM |
856 | kfree(async); |
857 | } | |
858 | ||
8896a08d | 859 | blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, |
8c27cb35 | 860 | int mirror_num, unsigned long bio_flags, |
1941b64b | 861 | u64 dio_file_offset, |
e288c080 | 862 | extent_submit_bio_start_t *submit_bio_start) |
44b8bd7e | 863 | { |
8896a08d | 864 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
44b8bd7e CM |
865 | struct async_submit_bio *async; |
866 | ||
867 | async = kmalloc(sizeof(*async), GFP_NOFS); | |
868 | if (!async) | |
4e4cbee9 | 869 | return BLK_STS_RESOURCE; |
44b8bd7e | 870 | |
8896a08d | 871 | async->inode = inode; |
44b8bd7e CM |
872 | async->bio = bio; |
873 | async->mirror_num = mirror_num; | |
4a69a410 | 874 | async->submit_bio_start = submit_bio_start; |
4a69a410 | 875 | |
a0cac0ec OS |
876 | btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, |
877 | run_one_async_free); | |
4a69a410 | 878 | |
1941b64b | 879 | async->dio_file_offset = dio_file_offset; |
8c8bee1d | 880 | |
4e4cbee9 | 881 | async->status = 0; |
79787eaa | 882 | |
67f055c7 | 883 | if (op_is_sync(bio->bi_opf)) |
5cdc7ad3 | 884 | btrfs_set_work_high_priority(&async->work); |
d313d7a3 | 885 | |
5cdc7ad3 | 886 | btrfs_queue_work(fs_info->workers, &async->work); |
44b8bd7e CM |
887 | return 0; |
888 | } | |
889 | ||
4e4cbee9 | 890 | static blk_status_t btree_csum_one_bio(struct bio *bio) |
ce3ed71a | 891 | { |
2c30c71b | 892 | struct bio_vec *bvec; |
ce3ed71a | 893 | struct btrfs_root *root; |
2b070cfe | 894 | int ret = 0; |
6dc4f100 | 895 | struct bvec_iter_all iter_all; |
ce3ed71a | 896 | |
c09abff8 | 897 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
2b070cfe | 898 | bio_for_each_segment_all(bvec, bio, iter_all) { |
ce3ed71a | 899 | root = BTRFS_I(bvec->bv_page->mapping->host)->root; |
ac303b69 | 900 | ret = csum_dirty_buffer(root->fs_info, bvec); |
79787eaa JM |
901 | if (ret) |
902 | break; | |
ce3ed71a | 903 | } |
2c30c71b | 904 | |
4e4cbee9 | 905 | return errno_to_blk_status(ret); |
ce3ed71a CM |
906 | } |
907 | ||
8896a08d | 908 | static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, |
1941b64b | 909 | u64 dio_file_offset) |
22c59948 | 910 | { |
8b712842 CM |
911 | /* |
912 | * when we're called for a write, we're already in the async | |
5443be45 | 913 | * submission context. Just jump into btrfs_map_bio |
8b712842 | 914 | */ |
79787eaa | 915 | return btree_csum_one_bio(bio); |
4a69a410 | 916 | } |
22c59948 | 917 | |
f4dcfb30 | 918 | static bool should_async_write(struct btrfs_fs_info *fs_info, |
9b4e675a | 919 | struct btrfs_inode *bi) |
de0022b9 | 920 | { |
4eef29ef | 921 | if (btrfs_is_zoned(fs_info)) |
f4dcfb30 | 922 | return false; |
6300463b | 923 | if (atomic_read(&bi->sync_writers)) |
f4dcfb30 | 924 | return false; |
9b4e675a | 925 | if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) |
f4dcfb30 JT |
926 | return false; |
927 | return true; | |
de0022b9 JB |
928 | } |
929 | ||
1b36294a NB |
930 | blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, |
931 | int mirror_num, unsigned long bio_flags) | |
44b8bd7e | 932 | { |
0b246afa | 933 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4e4cbee9 | 934 | blk_status_t ret; |
cad321ad | 935 | |
cfe94440 | 936 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
4a69a410 CM |
937 | /* |
938 | * called for a read, do the setup so that checksum validation | |
939 | * can happen in the async kernel threads | |
940 | */ | |
0b246afa JM |
941 | ret = btrfs_bio_wq_end_io(fs_info, bio, |
942 | BTRFS_WQ_ENDIO_METADATA); | |
1d4284bd | 943 | if (ret) |
61891923 | 944 | goto out_w_error; |
08635bae | 945 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
f4dcfb30 | 946 | } else if (!should_async_write(fs_info, BTRFS_I(inode))) { |
de0022b9 JB |
947 | ret = btree_csum_one_bio(bio); |
948 | if (ret) | |
61891923 | 949 | goto out_w_error; |
08635bae | 950 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
61891923 SB |
951 | } else { |
952 | /* | |
953 | * kthread helpers are used to submit writes so that | |
954 | * checksumming can happen in parallel across all CPUs | |
955 | */ | |
8896a08d QW |
956 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, |
957 | 0, btree_submit_bio_start); | |
44b8bd7e | 958 | } |
d313d7a3 | 959 | |
4246a0b6 CH |
960 | if (ret) |
961 | goto out_w_error; | |
962 | return 0; | |
963 | ||
61891923 | 964 | out_w_error: |
4e4cbee9 | 965 | bio->bi_status = ret; |
4246a0b6 | 966 | bio_endio(bio); |
61891923 | 967 | return ret; |
44b8bd7e CM |
968 | } |
969 | ||
3dd1462e | 970 | #ifdef CONFIG_MIGRATION |
784b4e29 | 971 | static int btree_migratepage(struct address_space *mapping, |
a6bc32b8 MG |
972 | struct page *newpage, struct page *page, |
973 | enum migrate_mode mode) | |
784b4e29 CM |
974 | { |
975 | /* | |
976 | * we can't safely write a btree page from here, | |
977 | * we haven't done the locking hook | |
978 | */ | |
979 | if (PageDirty(page)) | |
980 | return -EAGAIN; | |
981 | /* | |
982 | * Buffers may be managed in a filesystem specific way. | |
983 | * We must have no buffers or drop them. | |
984 | */ | |
985 | if (page_has_private(page) && | |
986 | !try_to_release_page(page, GFP_KERNEL)) | |
987 | return -EAGAIN; | |
a6bc32b8 | 988 | return migrate_page(mapping, newpage, page, mode); |
784b4e29 | 989 | } |
3dd1462e | 990 | #endif |
784b4e29 | 991 | |
0da5468f CM |
992 | |
993 | static int btree_writepages(struct address_space *mapping, | |
994 | struct writeback_control *wbc) | |
995 | { | |
e2d84521 MX |
996 | struct btrfs_fs_info *fs_info; |
997 | int ret; | |
998 | ||
d8d5f3e1 | 999 | if (wbc->sync_mode == WB_SYNC_NONE) { |
448d640b CM |
1000 | |
1001 | if (wbc->for_kupdate) | |
1002 | return 0; | |
1003 | ||
e2d84521 | 1004 | fs_info = BTRFS_I(mapping->host)->root->fs_info; |
b9473439 | 1005 | /* this is a bit racy, but that's ok */ |
d814a491 EL |
1006 | ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, |
1007 | BTRFS_DIRTY_METADATA_THRESH, | |
1008 | fs_info->dirty_metadata_batch); | |
e2d84521 | 1009 | if (ret < 0) |
793955bc | 1010 | return 0; |
793955bc | 1011 | } |
0b32f4bb | 1012 | return btree_write_cache_pages(mapping, wbc); |
0da5468f CM |
1013 | } |
1014 | ||
70dec807 | 1015 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
5f39d397 | 1016 | { |
98509cfc | 1017 | if (PageWriteback(page) || PageDirty(page)) |
d397712b | 1018 | return 0; |
0c4e538b | 1019 | |
f7a52a40 | 1020 | return try_release_extent_buffer(page); |
d98237b3 CM |
1021 | } |
1022 | ||
d47992f8 LC |
1023 | static void btree_invalidatepage(struct page *page, unsigned int offset, |
1024 | unsigned int length) | |
d98237b3 | 1025 | { |
d1310b2e CM |
1026 | struct extent_io_tree *tree; |
1027 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
1028 | extent_invalidatepage(tree, page, offset); |
1029 | btree_releasepage(page, GFP_NOFS); | |
9ad6b7bc | 1030 | if (PagePrivate(page)) { |
efe120a0 FH |
1031 | btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, |
1032 | "page private not zero on page %llu", | |
1033 | (unsigned long long)page_offset(page)); | |
d1b89bc0 | 1034 | detach_page_private(page); |
9ad6b7bc | 1035 | } |
d98237b3 CM |
1036 | } |
1037 | ||
0b32f4bb JB |
1038 | static int btree_set_page_dirty(struct page *page) |
1039 | { | |
bb146eb2 | 1040 | #ifdef DEBUG |
139e8cd3 QW |
1041 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
1042 | struct btrfs_subpage *subpage; | |
0b32f4bb | 1043 | struct extent_buffer *eb; |
139e8cd3 QW |
1044 | int cur_bit = 0; |
1045 | u64 page_start = page_offset(page); | |
1046 | ||
1047 | if (fs_info->sectorsize == PAGE_SIZE) { | |
1048 | BUG_ON(!PagePrivate(page)); | |
1049 | eb = (struct extent_buffer *)page->private; | |
1050 | BUG_ON(!eb); | |
1051 | BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
1052 | BUG_ON(!atomic_read(&eb->refs)); | |
1053 | btrfs_assert_tree_locked(eb); | |
1054 | return __set_page_dirty_nobuffers(page); | |
1055 | } | |
1056 | ASSERT(PagePrivate(page) && page->private); | |
1057 | subpage = (struct btrfs_subpage *)page->private; | |
1058 | ||
1059 | ASSERT(subpage->dirty_bitmap); | |
1060 | while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { | |
1061 | unsigned long flags; | |
1062 | u64 cur; | |
1063 | u16 tmp = (1 << cur_bit); | |
1064 | ||
1065 | spin_lock_irqsave(&subpage->lock, flags); | |
1066 | if (!(tmp & subpage->dirty_bitmap)) { | |
1067 | spin_unlock_irqrestore(&subpage->lock, flags); | |
1068 | cur_bit++; | |
1069 | continue; | |
1070 | } | |
1071 | spin_unlock_irqrestore(&subpage->lock, flags); | |
1072 | cur = page_start + cur_bit * fs_info->sectorsize; | |
0b32f4bb | 1073 | |
139e8cd3 QW |
1074 | eb = find_extent_buffer(fs_info, cur); |
1075 | ASSERT(eb); | |
1076 | ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
1077 | ASSERT(atomic_read(&eb->refs)); | |
1078 | btrfs_assert_tree_locked(eb); | |
1079 | free_extent_buffer(eb); | |
1080 | ||
1081 | cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); | |
1082 | } | |
bb146eb2 | 1083 | #endif |
0b32f4bb JB |
1084 | return __set_page_dirty_nobuffers(page); |
1085 | } | |
1086 | ||
7f09410b | 1087 | static const struct address_space_operations btree_aops = { |
0da5468f | 1088 | .writepages = btree_writepages, |
5f39d397 CM |
1089 | .releasepage = btree_releasepage, |
1090 | .invalidatepage = btree_invalidatepage, | |
5a92bc88 | 1091 | #ifdef CONFIG_MIGRATION |
784b4e29 | 1092 | .migratepage = btree_migratepage, |
5a92bc88 | 1093 | #endif |
0b32f4bb | 1094 | .set_page_dirty = btree_set_page_dirty, |
d98237b3 CM |
1095 | }; |
1096 | ||
2ff7e61e JM |
1097 | struct extent_buffer *btrfs_find_create_tree_block( |
1098 | struct btrfs_fs_info *fs_info, | |
3fbaf258 JB |
1099 | u64 bytenr, u64 owner_root, |
1100 | int level) | |
0999df54 | 1101 | { |
0b246afa JM |
1102 | if (btrfs_is_testing(fs_info)) |
1103 | return alloc_test_extent_buffer(fs_info, bytenr); | |
3fbaf258 | 1104 | return alloc_extent_buffer(fs_info, bytenr, owner_root, level); |
0999df54 CM |
1105 | } |
1106 | ||
581c1760 QW |
1107 | /* |
1108 | * Read tree block at logical address @bytenr and do variant basic but critical | |
1109 | * verification. | |
1110 | * | |
1b7ec85e | 1111 | * @owner_root: the objectid of the root owner for this block. |
581c1760 QW |
1112 | * @parent_transid: expected transid of this tree block, skip check if 0 |
1113 | * @level: expected level, mandatory check | |
1114 | * @first_key: expected key in slot 0, skip check if NULL | |
1115 | */ | |
2ff7e61e | 1116 | struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, |
1b7ec85e JB |
1117 | u64 owner_root, u64 parent_transid, |
1118 | int level, struct btrfs_key *first_key) | |
0999df54 CM |
1119 | { |
1120 | struct extent_buffer *buf = NULL; | |
0999df54 CM |
1121 | int ret; |
1122 | ||
3fbaf258 | 1123 | buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); |
c871b0f2 LB |
1124 | if (IS_ERR(buf)) |
1125 | return buf; | |
0999df54 | 1126 | |
5ab12d1f | 1127 | ret = btree_read_extent_buffer_pages(buf, parent_transid, |
581c1760 | 1128 | level, first_key); |
0f0fe8f7 | 1129 | if (ret) { |
537f38f0 | 1130 | free_extent_buffer_stale(buf); |
64c043de | 1131 | return ERR_PTR(ret); |
0f0fe8f7 | 1132 | } |
5f39d397 | 1133 | return buf; |
ce9adaa5 | 1134 | |
eb60ceac CM |
1135 | } |
1136 | ||
6a884d7d | 1137 | void btrfs_clean_tree_block(struct extent_buffer *buf) |
ed2ff2cb | 1138 | { |
6a884d7d | 1139 | struct btrfs_fs_info *fs_info = buf->fs_info; |
55c69072 | 1140 | if (btrfs_header_generation(buf) == |
e2d84521 | 1141 | fs_info->running_transaction->transid) { |
b9447ef8 | 1142 | btrfs_assert_tree_locked(buf); |
b4ce94de | 1143 | |
b9473439 | 1144 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { |
104b4e51 NB |
1145 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
1146 | -buf->len, | |
1147 | fs_info->dirty_metadata_batch); | |
ed7b63eb JB |
1148 | clear_extent_buffer_dirty(buf); |
1149 | } | |
925baedd | 1150 | } |
5f39d397 CM |
1151 | } |
1152 | ||
da17066c | 1153 | static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, |
143bede5 | 1154 | u64 objectid) |
d97e63b6 | 1155 | { |
7c0260ee | 1156 | bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); |
96dfcb46 | 1157 | root->fs_info = fs_info; |
cfaa7295 | 1158 | root->node = NULL; |
a28ec197 | 1159 | root->commit_root = NULL; |
27cdeb70 | 1160 | root->state = 0; |
d68fc57b | 1161 | root->orphan_cleanup_state = 0; |
0b86a832 | 1162 | |
0f7d52f4 | 1163 | root->last_trans = 0; |
6b8fad57 | 1164 | root->free_objectid = 0; |
eb73c1b7 | 1165 | root->nr_delalloc_inodes = 0; |
199c2a9c | 1166 | root->nr_ordered_extents = 0; |
6bef4d31 | 1167 | root->inode_tree = RB_ROOT; |
16cdcec7 | 1168 | INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); |
f0486c68 | 1169 | root->block_rsv = NULL; |
0b86a832 CM |
1170 | |
1171 | INIT_LIST_HEAD(&root->dirty_list); | |
5d4f98a2 | 1172 | INIT_LIST_HEAD(&root->root_list); |
eb73c1b7 MX |
1173 | INIT_LIST_HEAD(&root->delalloc_inodes); |
1174 | INIT_LIST_HEAD(&root->delalloc_root); | |
199c2a9c MX |
1175 | INIT_LIST_HEAD(&root->ordered_extents); |
1176 | INIT_LIST_HEAD(&root->ordered_root); | |
d2311e69 | 1177 | INIT_LIST_HEAD(&root->reloc_dirty_list); |
2ab28f32 JB |
1178 | INIT_LIST_HEAD(&root->logged_list[0]); |
1179 | INIT_LIST_HEAD(&root->logged_list[1]); | |
5d4f98a2 | 1180 | spin_lock_init(&root->inode_lock); |
eb73c1b7 | 1181 | spin_lock_init(&root->delalloc_lock); |
199c2a9c | 1182 | spin_lock_init(&root->ordered_extent_lock); |
f0486c68 | 1183 | spin_lock_init(&root->accounting_lock); |
2ab28f32 JB |
1184 | spin_lock_init(&root->log_extents_lock[0]); |
1185 | spin_lock_init(&root->log_extents_lock[1]); | |
8287475a | 1186 | spin_lock_init(&root->qgroup_meta_rsv_lock); |
a2135011 | 1187 | mutex_init(&root->objectid_mutex); |
e02119d5 | 1188 | mutex_init(&root->log_mutex); |
31f3d255 | 1189 | mutex_init(&root->ordered_extent_mutex); |
573bfb72 | 1190 | mutex_init(&root->delalloc_mutex); |
c53e9653 | 1191 | init_waitqueue_head(&root->qgroup_flush_wait); |
7237f183 YZ |
1192 | init_waitqueue_head(&root->log_writer_wait); |
1193 | init_waitqueue_head(&root->log_commit_wait[0]); | |
1194 | init_waitqueue_head(&root->log_commit_wait[1]); | |
8b050d35 MX |
1195 | INIT_LIST_HEAD(&root->log_ctxs[0]); |
1196 | INIT_LIST_HEAD(&root->log_ctxs[1]); | |
7237f183 YZ |
1197 | atomic_set(&root->log_commit[0], 0); |
1198 | atomic_set(&root->log_commit[1], 0); | |
1199 | atomic_set(&root->log_writers, 0); | |
2ecb7923 | 1200 | atomic_set(&root->log_batch, 0); |
0700cea7 | 1201 | refcount_set(&root->refs, 1); |
8ecebf4d | 1202 | atomic_set(&root->snapshot_force_cow, 0); |
eede2bf3 | 1203 | atomic_set(&root->nr_swapfiles, 0); |
7237f183 | 1204 | root->log_transid = 0; |
d1433deb | 1205 | root->log_transid_committed = -1; |
257c62e1 | 1206 | root->last_log_commit = 0; |
e289f03e | 1207 | if (!dummy) { |
43eb5f29 QW |
1208 | extent_io_tree_init(fs_info, &root->dirty_log_pages, |
1209 | IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); | |
e289f03e FM |
1210 | extent_io_tree_init(fs_info, &root->log_csum_range, |
1211 | IO_TREE_LOG_CSUM_RANGE, NULL); | |
1212 | } | |
017e5369 | 1213 | |
3768f368 CM |
1214 | memset(&root->root_key, 0, sizeof(root->root_key)); |
1215 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 1216 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
4d775673 | 1217 | root->root_key.objectid = objectid; |
0ee5dc67 | 1218 | root->anon_dev = 0; |
8ea05e3a | 1219 | |
5f3ab90a | 1220 | spin_lock_init(&root->root_item_lock); |
370a11b8 | 1221 | btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); |
bd647ce3 JB |
1222 | #ifdef CONFIG_BTRFS_DEBUG |
1223 | INIT_LIST_HEAD(&root->leak_list); | |
1224 | spin_lock(&fs_info->fs_roots_radix_lock); | |
1225 | list_add_tail(&root->leak_list, &fs_info->allocated_roots); | |
1226 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
1227 | #endif | |
3768f368 CM |
1228 | } |
1229 | ||
74e4d827 | 1230 | static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, |
96dfcb46 | 1231 | u64 objectid, gfp_t flags) |
6f07e42e | 1232 | { |
74e4d827 | 1233 | struct btrfs_root *root = kzalloc(sizeof(*root), flags); |
6f07e42e | 1234 | if (root) |
96dfcb46 | 1235 | __setup_root(root, fs_info, objectid); |
6f07e42e AV |
1236 | return root; |
1237 | } | |
1238 | ||
06ea65a3 JB |
1239 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
1240 | /* Should only be used by the testing infrastructure */ | |
da17066c | 1241 | struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) |
06ea65a3 JB |
1242 | { |
1243 | struct btrfs_root *root; | |
1244 | ||
7c0260ee JM |
1245 | if (!fs_info) |
1246 | return ERR_PTR(-EINVAL); | |
1247 | ||
96dfcb46 | 1248 | root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); |
06ea65a3 JB |
1249 | if (!root) |
1250 | return ERR_PTR(-ENOMEM); | |
da17066c | 1251 | |
b9ef22de | 1252 | /* We don't use the stripesize in selftest, set it as sectorsize */ |
faa2dbf0 | 1253 | root->alloc_bytenr = 0; |
06ea65a3 JB |
1254 | |
1255 | return root; | |
1256 | } | |
1257 | #endif | |
1258 | ||
20897f5c | 1259 | struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, |
20897f5c AJ |
1260 | u64 objectid) |
1261 | { | |
9b7a2440 | 1262 | struct btrfs_fs_info *fs_info = trans->fs_info; |
20897f5c AJ |
1263 | struct extent_buffer *leaf; |
1264 | struct btrfs_root *tree_root = fs_info->tree_root; | |
1265 | struct btrfs_root *root; | |
1266 | struct btrfs_key key; | |
b89f6d1f | 1267 | unsigned int nofs_flag; |
20897f5c | 1268 | int ret = 0; |
20897f5c | 1269 | |
b89f6d1f FM |
1270 | /* |
1271 | * We're holding a transaction handle, so use a NOFS memory allocation | |
1272 | * context to avoid deadlock if reclaim happens. | |
1273 | */ | |
1274 | nofs_flag = memalloc_nofs_save(); | |
96dfcb46 | 1275 | root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); |
b89f6d1f | 1276 | memalloc_nofs_restore(nofs_flag); |
20897f5c AJ |
1277 | if (!root) |
1278 | return ERR_PTR(-ENOMEM); | |
1279 | ||
20897f5c AJ |
1280 | root->root_key.objectid = objectid; |
1281 | root->root_key.type = BTRFS_ROOT_ITEM_KEY; | |
1282 | root->root_key.offset = 0; | |
1283 | ||
9631e4cc JB |
1284 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, |
1285 | BTRFS_NESTING_NORMAL); | |
20897f5c AJ |
1286 | if (IS_ERR(leaf)) { |
1287 | ret = PTR_ERR(leaf); | |
1dd05682 | 1288 | leaf = NULL; |
8a6a87cd | 1289 | goto fail_unlock; |
20897f5c AJ |
1290 | } |
1291 | ||
20897f5c | 1292 | root->node = leaf; |
20897f5c AJ |
1293 | btrfs_mark_buffer_dirty(leaf); |
1294 | ||
1295 | root->commit_root = btrfs_root_node(root); | |
27cdeb70 | 1296 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); |
20897f5c | 1297 | |
f944d2cb DS |
1298 | btrfs_set_root_flags(&root->root_item, 0); |
1299 | btrfs_set_root_limit(&root->root_item, 0); | |
20897f5c AJ |
1300 | btrfs_set_root_bytenr(&root->root_item, leaf->start); |
1301 | btrfs_set_root_generation(&root->root_item, trans->transid); | |
1302 | btrfs_set_root_level(&root->root_item, 0); | |
1303 | btrfs_set_root_refs(&root->root_item, 1); | |
1304 | btrfs_set_root_used(&root->root_item, leaf->len); | |
1305 | btrfs_set_root_last_snapshot(&root->root_item, 0); | |
1306 | btrfs_set_root_dirid(&root->root_item, 0); | |
33d85fda | 1307 | if (is_fstree(objectid)) |
807fc790 AS |
1308 | generate_random_guid(root->root_item.uuid); |
1309 | else | |
1310 | export_guid(root->root_item.uuid, &guid_null); | |
c8422684 | 1311 | btrfs_set_root_drop_level(&root->root_item, 0); |
20897f5c | 1312 | |
8a6a87cd BB |
1313 | btrfs_tree_unlock(leaf); |
1314 | ||
20897f5c AJ |
1315 | key.objectid = objectid; |
1316 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1317 | key.offset = 0; | |
1318 | ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); | |
1319 | if (ret) | |
1320 | goto fail; | |
1321 | ||
1dd05682 TI |
1322 | return root; |
1323 | ||
8a6a87cd | 1324 | fail_unlock: |
8c38938c | 1325 | if (leaf) |
1dd05682 | 1326 | btrfs_tree_unlock(leaf); |
8a6a87cd | 1327 | fail: |
00246528 | 1328 | btrfs_put_root(root); |
20897f5c | 1329 | |
1dd05682 | 1330 | return ERR_PTR(ret); |
20897f5c AJ |
1331 | } |
1332 | ||
7237f183 YZ |
1333 | static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, |
1334 | struct btrfs_fs_info *fs_info) | |
0f7d52f4 CM |
1335 | { |
1336 | struct btrfs_root *root; | |
e02119d5 | 1337 | |
96dfcb46 | 1338 | root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); |
e02119d5 | 1339 | if (!root) |
7237f183 | 1340 | return ERR_PTR(-ENOMEM); |
e02119d5 | 1341 | |
e02119d5 CM |
1342 | root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; |
1343 | root->root_key.type = BTRFS_ROOT_ITEM_KEY; | |
1344 | root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; | |
27cdeb70 | 1345 | |
6ab6ebb7 NA |
1346 | return root; |
1347 | } | |
1348 | ||
1349 | int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, | |
1350 | struct btrfs_root *root) | |
1351 | { | |
1352 | struct extent_buffer *leaf; | |
1353 | ||
7237f183 | 1354 | /* |
92a7cc42 | 1355 | * DON'T set SHAREABLE bit for log trees. |
27cdeb70 | 1356 | * |
92a7cc42 QW |
1357 | * Log trees are not exposed to user space thus can't be snapshotted, |
1358 | * and they go away before a real commit is actually done. | |
1359 | * | |
1360 | * They do store pointers to file data extents, and those reference | |
1361 | * counts still get updated (along with back refs to the log tree). | |
7237f183 | 1362 | */ |
e02119d5 | 1363 | |
4d75f8a9 | 1364 | leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, |
9631e4cc | 1365 | NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); |
6ab6ebb7 NA |
1366 | if (IS_ERR(leaf)) |
1367 | return PTR_ERR(leaf); | |
e02119d5 | 1368 | |
7237f183 | 1369 | root->node = leaf; |
e02119d5 | 1370 | |
e02119d5 CM |
1371 | btrfs_mark_buffer_dirty(root->node); |
1372 | btrfs_tree_unlock(root->node); | |
6ab6ebb7 NA |
1373 | |
1374 | return 0; | |
7237f183 YZ |
1375 | } |
1376 | ||
1377 | int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, | |
1378 | struct btrfs_fs_info *fs_info) | |
1379 | { | |
1380 | struct btrfs_root *log_root; | |
1381 | ||
1382 | log_root = alloc_log_tree(trans, fs_info); | |
1383 | if (IS_ERR(log_root)) | |
1384 | return PTR_ERR(log_root); | |
6ab6ebb7 | 1385 | |
3ddebf27 NA |
1386 | if (!btrfs_is_zoned(fs_info)) { |
1387 | int ret = btrfs_alloc_log_tree_node(trans, log_root); | |
1388 | ||
1389 | if (ret) { | |
1390 | btrfs_put_root(log_root); | |
1391 | return ret; | |
1392 | } | |
6ab6ebb7 NA |
1393 | } |
1394 | ||
7237f183 YZ |
1395 | WARN_ON(fs_info->log_root_tree); |
1396 | fs_info->log_root_tree = log_root; | |
1397 | return 0; | |
1398 | } | |
1399 | ||
1400 | int btrfs_add_log_tree(struct btrfs_trans_handle *trans, | |
1401 | struct btrfs_root *root) | |
1402 | { | |
0b246afa | 1403 | struct btrfs_fs_info *fs_info = root->fs_info; |
7237f183 YZ |
1404 | struct btrfs_root *log_root; |
1405 | struct btrfs_inode_item *inode_item; | |
6ab6ebb7 | 1406 | int ret; |
7237f183 | 1407 | |
0b246afa | 1408 | log_root = alloc_log_tree(trans, fs_info); |
7237f183 YZ |
1409 | if (IS_ERR(log_root)) |
1410 | return PTR_ERR(log_root); | |
1411 | ||
6ab6ebb7 NA |
1412 | ret = btrfs_alloc_log_tree_node(trans, log_root); |
1413 | if (ret) { | |
1414 | btrfs_put_root(log_root); | |
1415 | return ret; | |
1416 | } | |
1417 | ||
7237f183 YZ |
1418 | log_root->last_trans = trans->transid; |
1419 | log_root->root_key.offset = root->root_key.objectid; | |
1420 | ||
1421 | inode_item = &log_root->root_item.inode; | |
3cae210f QW |
1422 | btrfs_set_stack_inode_generation(inode_item, 1); |
1423 | btrfs_set_stack_inode_size(inode_item, 3); | |
1424 | btrfs_set_stack_inode_nlink(inode_item, 1); | |
da17066c | 1425 | btrfs_set_stack_inode_nbytes(inode_item, |
0b246afa | 1426 | fs_info->nodesize); |
3cae210f | 1427 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
7237f183 | 1428 | |
5d4f98a2 | 1429 | btrfs_set_root_node(&log_root->root_item, log_root->node); |
7237f183 YZ |
1430 | |
1431 | WARN_ON(root->log_root); | |
1432 | root->log_root = log_root; | |
1433 | root->log_transid = 0; | |
d1433deb | 1434 | root->log_transid_committed = -1; |
257c62e1 | 1435 | root->last_log_commit = 0; |
e02119d5 CM |
1436 | return 0; |
1437 | } | |
1438 | ||
49d11bea JB |
1439 | static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, |
1440 | struct btrfs_path *path, | |
1441 | struct btrfs_key *key) | |
e02119d5 CM |
1442 | { |
1443 | struct btrfs_root *root; | |
1444 | struct btrfs_fs_info *fs_info = tree_root->fs_info; | |
84234f3a | 1445 | u64 generation; |
cb517eab | 1446 | int ret; |
581c1760 | 1447 | int level; |
0f7d52f4 | 1448 | |
96dfcb46 | 1449 | root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); |
49d11bea JB |
1450 | if (!root) |
1451 | return ERR_PTR(-ENOMEM); | |
0f7d52f4 | 1452 | |
cb517eab MX |
1453 | ret = btrfs_find_root(tree_root, key, path, |
1454 | &root->root_item, &root->root_key); | |
0f7d52f4 | 1455 | if (ret) { |
13a8a7c8 YZ |
1456 | if (ret > 0) |
1457 | ret = -ENOENT; | |
49d11bea | 1458 | goto fail; |
0f7d52f4 | 1459 | } |
13a8a7c8 | 1460 | |
84234f3a | 1461 | generation = btrfs_root_generation(&root->root_item); |
581c1760 | 1462 | level = btrfs_root_level(&root->root_item); |
2ff7e61e JM |
1463 | root->node = read_tree_block(fs_info, |
1464 | btrfs_root_bytenr(&root->root_item), | |
1b7ec85e | 1465 | key->objectid, generation, level, NULL); |
64c043de LB |
1466 | if (IS_ERR(root->node)) { |
1467 | ret = PTR_ERR(root->node); | |
8c38938c | 1468 | root->node = NULL; |
49d11bea | 1469 | goto fail; |
cb517eab MX |
1470 | } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { |
1471 | ret = -EIO; | |
49d11bea | 1472 | goto fail; |
416bc658 | 1473 | } |
5d4f98a2 | 1474 | root->commit_root = btrfs_root_node(root); |
cb517eab | 1475 | return root; |
49d11bea | 1476 | fail: |
00246528 | 1477 | btrfs_put_root(root); |
49d11bea JB |
1478 | return ERR_PTR(ret); |
1479 | } | |
1480 | ||
1481 | struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, | |
1482 | struct btrfs_key *key) | |
1483 | { | |
1484 | struct btrfs_root *root; | |
1485 | struct btrfs_path *path; | |
1486 | ||
1487 | path = btrfs_alloc_path(); | |
1488 | if (!path) | |
1489 | return ERR_PTR(-ENOMEM); | |
1490 | root = read_tree_root_path(tree_root, path, key); | |
1491 | btrfs_free_path(path); | |
1492 | ||
1493 | return root; | |
cb517eab MX |
1494 | } |
1495 | ||
2dfb1e43 QW |
1496 | /* |
1497 | * Initialize subvolume root in-memory structure | |
1498 | * | |
1499 | * @anon_dev: anonymous device to attach to the root, if zero, allocate new | |
1500 | */ | |
1501 | static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) | |
cb517eab MX |
1502 | { |
1503 | int ret; | |
dcc3eb96 | 1504 | unsigned int nofs_flag; |
cb517eab | 1505 | |
dcc3eb96 NB |
1506 | /* |
1507 | * We might be called under a transaction (e.g. indirect backref | |
1508 | * resolution) which could deadlock if it triggers memory reclaim | |
1509 | */ | |
1510 | nofs_flag = memalloc_nofs_save(); | |
1511 | ret = btrfs_drew_lock_init(&root->snapshot_lock); | |
1512 | memalloc_nofs_restore(nofs_flag); | |
1513 | if (ret) | |
8257b2dc | 1514 | goto fail; |
8257b2dc | 1515 | |
aeb935a4 | 1516 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && |
b8f762a2 | 1517 | !btrfs_is_data_reloc_root(root)) { |
92a7cc42 | 1518 | set_bit(BTRFS_ROOT_SHAREABLE, &root->state); |
f39e4571 JB |
1519 | btrfs_check_and_init_root_item(&root->root_item); |
1520 | } | |
1521 | ||
851fd730 QW |
1522 | /* |
1523 | * Don't assign anonymous block device to roots that are not exposed to | |
1524 | * userspace, the id pool is limited to 1M | |
1525 | */ | |
1526 | if (is_fstree(root->root_key.objectid) && | |
1527 | btrfs_root_refs(&root->root_item) > 0) { | |
2dfb1e43 QW |
1528 | if (!anon_dev) { |
1529 | ret = get_anon_bdev(&root->anon_dev); | |
1530 | if (ret) | |
1531 | goto fail; | |
1532 | } else { | |
1533 | root->anon_dev = anon_dev; | |
1534 | } | |
851fd730 | 1535 | } |
f32e48e9 CR |
1536 | |
1537 | mutex_lock(&root->objectid_mutex); | |
453e4873 | 1538 | ret = btrfs_init_root_free_objectid(root); |
f32e48e9 CR |
1539 | if (ret) { |
1540 | mutex_unlock(&root->objectid_mutex); | |
876d2cf1 | 1541 | goto fail; |
f32e48e9 CR |
1542 | } |
1543 | ||
6b8fad57 | 1544 | ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); |
f32e48e9 CR |
1545 | |
1546 | mutex_unlock(&root->objectid_mutex); | |
1547 | ||
cb517eab MX |
1548 | return 0; |
1549 | fail: | |
84db5ccf | 1550 | /* The caller is responsible to call btrfs_free_fs_root */ |
cb517eab MX |
1551 | return ret; |
1552 | } | |
1553 | ||
a98db0f3 JB |
1554 | static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, |
1555 | u64 root_id) | |
cb517eab MX |
1556 | { |
1557 | struct btrfs_root *root; | |
1558 | ||
1559 | spin_lock(&fs_info->fs_roots_radix_lock); | |
1560 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
1561 | (unsigned long)root_id); | |
bc44d7c4 | 1562 | if (root) |
00246528 | 1563 | root = btrfs_grab_root(root); |
cb517eab MX |
1564 | spin_unlock(&fs_info->fs_roots_radix_lock); |
1565 | return root; | |
1566 | } | |
1567 | ||
49d11bea JB |
1568 | static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, |
1569 | u64 objectid) | |
1570 | { | |
1571 | if (objectid == BTRFS_ROOT_TREE_OBJECTID) | |
1572 | return btrfs_grab_root(fs_info->tree_root); | |
1573 | if (objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
1574 | return btrfs_grab_root(fs_info->extent_root); | |
1575 | if (objectid == BTRFS_CHUNK_TREE_OBJECTID) | |
1576 | return btrfs_grab_root(fs_info->chunk_root); | |
1577 | if (objectid == BTRFS_DEV_TREE_OBJECTID) | |
1578 | return btrfs_grab_root(fs_info->dev_root); | |
1579 | if (objectid == BTRFS_CSUM_TREE_OBJECTID) | |
1580 | return btrfs_grab_root(fs_info->csum_root); | |
1581 | if (objectid == BTRFS_QUOTA_TREE_OBJECTID) | |
1582 | return btrfs_grab_root(fs_info->quota_root) ? | |
1583 | fs_info->quota_root : ERR_PTR(-ENOENT); | |
1584 | if (objectid == BTRFS_UUID_TREE_OBJECTID) | |
1585 | return btrfs_grab_root(fs_info->uuid_root) ? | |
1586 | fs_info->uuid_root : ERR_PTR(-ENOENT); | |
1587 | if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) | |
1588 | return btrfs_grab_root(fs_info->free_space_root) ? | |
1589 | fs_info->free_space_root : ERR_PTR(-ENOENT); | |
1590 | return NULL; | |
1591 | } | |
1592 | ||
cb517eab MX |
1593 | int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, |
1594 | struct btrfs_root *root) | |
1595 | { | |
1596 | int ret; | |
1597 | ||
e1860a77 | 1598 | ret = radix_tree_preload(GFP_NOFS); |
cb517eab MX |
1599 | if (ret) |
1600 | return ret; | |
1601 | ||
1602 | spin_lock(&fs_info->fs_roots_radix_lock); | |
1603 | ret = radix_tree_insert(&fs_info->fs_roots_radix, | |
1604 | (unsigned long)root->root_key.objectid, | |
1605 | root); | |
af01d2e5 | 1606 | if (ret == 0) { |
00246528 | 1607 | btrfs_grab_root(root); |
27cdeb70 | 1608 | set_bit(BTRFS_ROOT_IN_RADIX, &root->state); |
af01d2e5 | 1609 | } |
cb517eab MX |
1610 | spin_unlock(&fs_info->fs_roots_radix_lock); |
1611 | radix_tree_preload_end(); | |
1612 | ||
1613 | return ret; | |
1614 | } | |
1615 | ||
bd647ce3 JB |
1616 | void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) |
1617 | { | |
1618 | #ifdef CONFIG_BTRFS_DEBUG | |
1619 | struct btrfs_root *root; | |
1620 | ||
1621 | while (!list_empty(&fs_info->allocated_roots)) { | |
457f1864 JB |
1622 | char buf[BTRFS_ROOT_NAME_BUF_LEN]; |
1623 | ||
bd647ce3 JB |
1624 | root = list_first_entry(&fs_info->allocated_roots, |
1625 | struct btrfs_root, leak_list); | |
457f1864 | 1626 | btrfs_err(fs_info, "leaked root %s refcount %d", |
71008734 | 1627 | btrfs_root_name(&root->root_key, buf), |
bd647ce3 JB |
1628 | refcount_read(&root->refs)); |
1629 | while (refcount_read(&root->refs) > 1) | |
00246528 JB |
1630 | btrfs_put_root(root); |
1631 | btrfs_put_root(root); | |
bd647ce3 JB |
1632 | } |
1633 | #endif | |
1634 | } | |
1635 | ||
0d4b0463 JB |
1636 | void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) |
1637 | { | |
141386e1 JB |
1638 | percpu_counter_destroy(&fs_info->dirty_metadata_bytes); |
1639 | percpu_counter_destroy(&fs_info->delalloc_bytes); | |
5deb17e1 | 1640 | percpu_counter_destroy(&fs_info->ordered_bytes); |
141386e1 JB |
1641 | percpu_counter_destroy(&fs_info->dev_replace.bio_counter); |
1642 | btrfs_free_csum_hash(fs_info); | |
1643 | btrfs_free_stripe_hash_table(fs_info); | |
1644 | btrfs_free_ref_cache(fs_info); | |
0d4b0463 JB |
1645 | kfree(fs_info->balance_ctl); |
1646 | kfree(fs_info->delayed_root); | |
00246528 JB |
1647 | btrfs_put_root(fs_info->extent_root); |
1648 | btrfs_put_root(fs_info->tree_root); | |
1649 | btrfs_put_root(fs_info->chunk_root); | |
1650 | btrfs_put_root(fs_info->dev_root); | |
1651 | btrfs_put_root(fs_info->csum_root); | |
1652 | btrfs_put_root(fs_info->quota_root); | |
1653 | btrfs_put_root(fs_info->uuid_root); | |
1654 | btrfs_put_root(fs_info->free_space_root); | |
1655 | btrfs_put_root(fs_info->fs_root); | |
aeb935a4 | 1656 | btrfs_put_root(fs_info->data_reloc_root); |
bd647ce3 | 1657 | btrfs_check_leaked_roots(fs_info); |
3fd63727 | 1658 | btrfs_extent_buffer_leak_debug_check(fs_info); |
0d4b0463 JB |
1659 | kfree(fs_info->super_copy); |
1660 | kfree(fs_info->super_for_commit); | |
1661 | kvfree(fs_info); | |
1662 | } | |
1663 | ||
1664 | ||
2dfb1e43 QW |
1665 | /* |
1666 | * Get an in-memory reference of a root structure. | |
1667 | * | |
1668 | * For essential trees like root/extent tree, we grab it from fs_info directly. | |
1669 | * For subvolume trees, we check the cached filesystem roots first. If not | |
1670 | * found, then read it from disk and add it to cached fs roots. | |
1671 | * | |
1672 | * Caller should release the root by calling btrfs_put_root() after the usage. | |
1673 | * | |
1674 | * NOTE: Reloc and log trees can't be read by this function as they share the | |
1675 | * same root objectid. | |
1676 | * | |
1677 | * @objectid: root id | |
1678 | * @anon_dev: preallocated anonymous block device number for new roots, | |
1679 | * pass 0 for new allocation. | |
1680 | * @check_ref: whether to check root item references, If true, return -ENOENT | |
1681 | * for orphan roots | |
1682 | */ | |
1683 | static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, | |
1684 | u64 objectid, dev_t anon_dev, | |
1685 | bool check_ref) | |
5eda7b5e CM |
1686 | { |
1687 | struct btrfs_root *root; | |
381cf658 | 1688 | struct btrfs_path *path; |
1d4c08e0 | 1689 | struct btrfs_key key; |
5eda7b5e CM |
1690 | int ret; |
1691 | ||
49d11bea JB |
1692 | root = btrfs_get_global_root(fs_info, objectid); |
1693 | if (root) | |
1694 | return root; | |
4df27c4d | 1695 | again: |
56e9357a | 1696 | root = btrfs_lookup_fs_root(fs_info, objectid); |
48475471 | 1697 | if (root) { |
2dfb1e43 QW |
1698 | /* Shouldn't get preallocated anon_dev for cached roots */ |
1699 | ASSERT(!anon_dev); | |
bc44d7c4 | 1700 | if (check_ref && btrfs_root_refs(&root->root_item) == 0) { |
00246528 | 1701 | btrfs_put_root(root); |
48475471 | 1702 | return ERR_PTR(-ENOENT); |
bc44d7c4 | 1703 | } |
5eda7b5e | 1704 | return root; |
48475471 | 1705 | } |
5eda7b5e | 1706 | |
56e9357a DS |
1707 | key.objectid = objectid; |
1708 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1709 | key.offset = (u64)-1; | |
1710 | root = btrfs_read_tree_root(fs_info->tree_root, &key); | |
5eda7b5e CM |
1711 | if (IS_ERR(root)) |
1712 | return root; | |
3394e160 | 1713 | |
c00869f1 | 1714 | if (check_ref && btrfs_root_refs(&root->root_item) == 0) { |
cb517eab | 1715 | ret = -ENOENT; |
581bb050 | 1716 | goto fail; |
35a30d7c | 1717 | } |
581bb050 | 1718 | |
2dfb1e43 | 1719 | ret = btrfs_init_fs_root(root, anon_dev); |
ac08aedf CM |
1720 | if (ret) |
1721 | goto fail; | |
3394e160 | 1722 | |
381cf658 DS |
1723 | path = btrfs_alloc_path(); |
1724 | if (!path) { | |
1725 | ret = -ENOMEM; | |
1726 | goto fail; | |
1727 | } | |
1d4c08e0 DS |
1728 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
1729 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
56e9357a | 1730 | key.offset = objectid; |
1d4c08e0 DS |
1731 | |
1732 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); | |
381cf658 | 1733 | btrfs_free_path(path); |
d68fc57b YZ |
1734 | if (ret < 0) |
1735 | goto fail; | |
1736 | if (ret == 0) | |
27cdeb70 | 1737 | set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); |
d68fc57b | 1738 | |
cb517eab | 1739 | ret = btrfs_insert_fs_root(fs_info, root); |
0f7d52f4 | 1740 | if (ret) { |
00246528 | 1741 | btrfs_put_root(root); |
4785e24f | 1742 | if (ret == -EEXIST) |
4df27c4d | 1743 | goto again; |
4df27c4d | 1744 | goto fail; |
0f7d52f4 | 1745 | } |
edbd8d4e | 1746 | return root; |
4df27c4d | 1747 | fail: |
e019bdc9 FM |
1748 | /* |
1749 | * If our caller provided us an anonymous device, then it's his | |
1750 | * responsability to free it in case we fail. So we have to set our | |
1751 | * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() | |
1752 | * and once again by our caller. | |
1753 | */ | |
1754 | if (anon_dev) | |
1755 | root->anon_dev = 0; | |
8c38938c | 1756 | btrfs_put_root(root); |
4df27c4d | 1757 | return ERR_PTR(ret); |
edbd8d4e CM |
1758 | } |
1759 | ||
2dfb1e43 QW |
1760 | /* |
1761 | * Get in-memory reference of a root structure | |
1762 | * | |
1763 | * @objectid: tree objectid | |
1764 | * @check_ref: if set, verify that the tree exists and the item has at least | |
1765 | * one reference | |
1766 | */ | |
1767 | struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, | |
1768 | u64 objectid, bool check_ref) | |
1769 | { | |
1770 | return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); | |
1771 | } | |
1772 | ||
1773 | /* | |
1774 | * Get in-memory reference of a root structure, created as new, optionally pass | |
1775 | * the anonymous block device id | |
1776 | * | |
1777 | * @objectid: tree objectid | |
1778 | * @anon_dev: if zero, allocate a new anonymous block device or use the | |
1779 | * parameter value | |
1780 | */ | |
1781 | struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, | |
1782 | u64 objectid, dev_t anon_dev) | |
1783 | { | |
1784 | return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); | |
1785 | } | |
1786 | ||
49d11bea JB |
1787 | /* |
1788 | * btrfs_get_fs_root_commit_root - return a root for the given objectid | |
1789 | * @fs_info: the fs_info | |
1790 | * @objectid: the objectid we need to lookup | |
1791 | * | |
1792 | * This is exclusively used for backref walking, and exists specifically because | |
1793 | * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref | |
1794 | * creation time, which means we may have to read the tree_root in order to look | |
1795 | * up a fs root that is not in memory. If the root is not in memory we will | |
1796 | * read the tree root commit root and look up the fs root from there. This is a | |
1797 | * temporary root, it will not be inserted into the radix tree as it doesn't | |
1798 | * have the most uptodate information, it'll simply be discarded once the | |
1799 | * backref code is finished using the root. | |
1800 | */ | |
1801 | struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, | |
1802 | struct btrfs_path *path, | |
1803 | u64 objectid) | |
1804 | { | |
1805 | struct btrfs_root *root; | |
1806 | struct btrfs_key key; | |
1807 | ||
1808 | ASSERT(path->search_commit_root && path->skip_locking); | |
1809 | ||
1810 | /* | |
1811 | * This can return -ENOENT if we ask for a root that doesn't exist, but | |
1812 | * since this is called via the backref walking code we won't be looking | |
1813 | * up a root that doesn't exist, unless there's corruption. So if root | |
1814 | * != NULL just return it. | |
1815 | */ | |
1816 | root = btrfs_get_global_root(fs_info, objectid); | |
1817 | if (root) | |
1818 | return root; | |
1819 | ||
1820 | root = btrfs_lookup_fs_root(fs_info, objectid); | |
1821 | if (root) | |
1822 | return root; | |
1823 | ||
1824 | key.objectid = objectid; | |
1825 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1826 | key.offset = (u64)-1; | |
1827 | root = read_tree_root_path(fs_info->tree_root, path, &key); | |
1828 | btrfs_release_path(path); | |
1829 | ||
1830 | return root; | |
1831 | } | |
1832 | ||
8b712842 CM |
1833 | /* |
1834 | * called by the kthread helper functions to finally call the bio end_io | |
1835 | * functions. This is where read checksum verification actually happens | |
1836 | */ | |
1837 | static void end_workqueue_fn(struct btrfs_work *work) | |
ce9adaa5 | 1838 | { |
ce9adaa5 | 1839 | struct bio *bio; |
97eb6b69 | 1840 | struct btrfs_end_io_wq *end_io_wq; |
ce9adaa5 | 1841 | |
97eb6b69 | 1842 | end_io_wq = container_of(work, struct btrfs_end_io_wq, work); |
8b712842 | 1843 | bio = end_io_wq->bio; |
ce9adaa5 | 1844 | |
4e4cbee9 | 1845 | bio->bi_status = end_io_wq->status; |
8b712842 CM |
1846 | bio->bi_private = end_io_wq->private; |
1847 | bio->bi_end_io = end_io_wq->end_io; | |
4246a0b6 | 1848 | bio_endio(bio); |
9be490f1 | 1849 | kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); |
44b8bd7e CM |
1850 | } |
1851 | ||
a74a4b97 CM |
1852 | static int cleaner_kthread(void *arg) |
1853 | { | |
1854 | struct btrfs_root *root = arg; | |
0b246afa | 1855 | struct btrfs_fs_info *fs_info = root->fs_info; |
d0278245 | 1856 | int again; |
a74a4b97 | 1857 | |
d6fd0ae2 | 1858 | while (1) { |
d0278245 | 1859 | again = 0; |
a74a4b97 | 1860 | |
fd340d0f JB |
1861 | set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); |
1862 | ||
d0278245 | 1863 | /* Make the cleaner go to sleep early. */ |
2ff7e61e | 1864 | if (btrfs_need_cleaner_sleep(fs_info)) |
d0278245 MX |
1865 | goto sleep; |
1866 | ||
90c711ab ZB |
1867 | /* |
1868 | * Do not do anything if we might cause open_ctree() to block | |
1869 | * before we have finished mounting the filesystem. | |
1870 | */ | |
0b246afa | 1871 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
90c711ab ZB |
1872 | goto sleep; |
1873 | ||
0b246afa | 1874 | if (!mutex_trylock(&fs_info->cleaner_mutex)) |
d0278245 MX |
1875 | goto sleep; |
1876 | ||
dc7f370c MX |
1877 | /* |
1878 | * Avoid the problem that we change the status of the fs | |
1879 | * during the above check and trylock. | |
1880 | */ | |
2ff7e61e | 1881 | if (btrfs_need_cleaner_sleep(fs_info)) { |
0b246afa | 1882 | mutex_unlock(&fs_info->cleaner_mutex); |
dc7f370c | 1883 | goto sleep; |
76dda93c | 1884 | } |
a74a4b97 | 1885 | |
2ff7e61e | 1886 | btrfs_run_delayed_iputs(fs_info); |
c2d6cb16 | 1887 | |
d0278245 | 1888 | again = btrfs_clean_one_deleted_snapshot(root); |
0b246afa | 1889 | mutex_unlock(&fs_info->cleaner_mutex); |
d0278245 MX |
1890 | |
1891 | /* | |
05323cd1 MX |
1892 | * The defragger has dealt with the R/O remount and umount, |
1893 | * needn't do anything special here. | |
d0278245 | 1894 | */ |
0b246afa | 1895 | btrfs_run_defrag_inodes(fs_info); |
67c5e7d4 FM |
1896 | |
1897 | /* | |
f3372065 | 1898 | * Acquires fs_info->reclaim_bgs_lock to avoid racing |
67c5e7d4 FM |
1899 | * with relocation (btrfs_relocate_chunk) and relocation |
1900 | * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) | |
f3372065 | 1901 | * after acquiring fs_info->reclaim_bgs_lock. So we |
67c5e7d4 FM |
1902 | * can't hold, nor need to, fs_info->cleaner_mutex when deleting |
1903 | * unused block groups. | |
1904 | */ | |
0b246afa | 1905 | btrfs_delete_unused_bgs(fs_info); |
18bb8bbf JT |
1906 | |
1907 | /* | |
1908 | * Reclaim block groups in the reclaim_bgs list after we deleted | |
1909 | * all unused block_groups. This possibly gives us some more free | |
1910 | * space. | |
1911 | */ | |
1912 | btrfs_reclaim_bgs(fs_info); | |
d0278245 | 1913 | sleep: |
a0a1db70 | 1914 | clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); |
d6fd0ae2 OS |
1915 | if (kthread_should_park()) |
1916 | kthread_parkme(); | |
1917 | if (kthread_should_stop()) | |
1918 | return 0; | |
838fe188 | 1919 | if (!again) { |
a74a4b97 | 1920 | set_current_state(TASK_INTERRUPTIBLE); |
d6fd0ae2 | 1921 | schedule(); |
a74a4b97 CM |
1922 | __set_current_state(TASK_RUNNING); |
1923 | } | |
da288d28 | 1924 | } |
a74a4b97 CM |
1925 | } |
1926 | ||
1927 | static int transaction_kthread(void *arg) | |
1928 | { | |
1929 | struct btrfs_root *root = arg; | |
0b246afa | 1930 | struct btrfs_fs_info *fs_info = root->fs_info; |
a74a4b97 CM |
1931 | struct btrfs_trans_handle *trans; |
1932 | struct btrfs_transaction *cur; | |
8929ecfa | 1933 | u64 transid; |
643900be | 1934 | time64_t delta; |
a74a4b97 | 1935 | unsigned long delay; |
914b2007 | 1936 | bool cannot_commit; |
a74a4b97 CM |
1937 | |
1938 | do { | |
914b2007 | 1939 | cannot_commit = false; |
ba1bc00f | 1940 | delay = msecs_to_jiffies(fs_info->commit_interval * 1000); |
0b246afa | 1941 | mutex_lock(&fs_info->transaction_kthread_mutex); |
a74a4b97 | 1942 | |
0b246afa JM |
1943 | spin_lock(&fs_info->trans_lock); |
1944 | cur = fs_info->running_transaction; | |
a74a4b97 | 1945 | if (!cur) { |
0b246afa | 1946 | spin_unlock(&fs_info->trans_lock); |
a74a4b97 CM |
1947 | goto sleep; |
1948 | } | |
31153d81 | 1949 | |
643900be | 1950 | delta = ktime_get_seconds() - cur->start_time; |
3296bf56 | 1951 | if (cur->state < TRANS_STATE_COMMIT_START && |
643900be | 1952 | delta < fs_info->commit_interval) { |
0b246afa | 1953 | spin_unlock(&fs_info->trans_lock); |
fb8a7e94 NB |
1954 | delay -= msecs_to_jiffies((delta - 1) * 1000); |
1955 | delay = min(delay, | |
1956 | msecs_to_jiffies(fs_info->commit_interval * 1000)); | |
a74a4b97 CM |
1957 | goto sleep; |
1958 | } | |
8929ecfa | 1959 | transid = cur->transid; |
0b246afa | 1960 | spin_unlock(&fs_info->trans_lock); |
56bec294 | 1961 | |
79787eaa | 1962 | /* If the file system is aborted, this will always fail. */ |
354aa0fb | 1963 | trans = btrfs_attach_transaction(root); |
914b2007 | 1964 | if (IS_ERR(trans)) { |
354aa0fb MX |
1965 | if (PTR_ERR(trans) != -ENOENT) |
1966 | cannot_commit = true; | |
79787eaa | 1967 | goto sleep; |
914b2007 | 1968 | } |
8929ecfa | 1969 | if (transid == trans->transid) { |
3a45bb20 | 1970 | btrfs_commit_transaction(trans); |
8929ecfa | 1971 | } else { |
3a45bb20 | 1972 | btrfs_end_transaction(trans); |
8929ecfa | 1973 | } |
a74a4b97 | 1974 | sleep: |
0b246afa JM |
1975 | wake_up_process(fs_info->cleaner_kthread); |
1976 | mutex_unlock(&fs_info->transaction_kthread_mutex); | |
a74a4b97 | 1977 | |
4e121c06 | 1978 | if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, |
0b246afa | 1979 | &fs_info->fs_state))) |
2ff7e61e | 1980 | btrfs_cleanup_transaction(fs_info); |
ce63f891 | 1981 | if (!kthread_should_stop() && |
0b246afa | 1982 | (!btrfs_transaction_blocked(fs_info) || |
ce63f891 | 1983 | cannot_commit)) |
bc5511d0 | 1984 | schedule_timeout_interruptible(delay); |
a74a4b97 CM |
1985 | } while (!kthread_should_stop()); |
1986 | return 0; | |
1987 | } | |
1988 | ||
af31f5e5 | 1989 | /* |
01f0f9da NB |
1990 | * This will find the highest generation in the array of root backups. The |
1991 | * index of the highest array is returned, or -EINVAL if we can't find | |
1992 | * anything. | |
af31f5e5 CM |
1993 | * |
1994 | * We check to make sure the array is valid by comparing the | |
1995 | * generation of the latest root in the array with the generation | |
1996 | * in the super block. If they don't match we pitch it. | |
1997 | */ | |
01f0f9da | 1998 | static int find_newest_super_backup(struct btrfs_fs_info *info) |
af31f5e5 | 1999 | { |
01f0f9da | 2000 | const u64 newest_gen = btrfs_super_generation(info->super_copy); |
af31f5e5 | 2001 | u64 cur; |
af31f5e5 CM |
2002 | struct btrfs_root_backup *root_backup; |
2003 | int i; | |
2004 | ||
2005 | for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { | |
2006 | root_backup = info->super_copy->super_roots + i; | |
2007 | cur = btrfs_backup_tree_root_gen(root_backup); | |
2008 | if (cur == newest_gen) | |
01f0f9da | 2009 | return i; |
af31f5e5 CM |
2010 | } |
2011 | ||
01f0f9da | 2012 | return -EINVAL; |
af31f5e5 CM |
2013 | } |
2014 | ||
af31f5e5 CM |
2015 | /* |
2016 | * copy all the root pointers into the super backup array. | |
2017 | * this will bump the backup pointer by one when it is | |
2018 | * done | |
2019 | */ | |
2020 | static void backup_super_roots(struct btrfs_fs_info *info) | |
2021 | { | |
6ef108dd | 2022 | const int next_backup = info->backup_root_index; |
af31f5e5 | 2023 | struct btrfs_root_backup *root_backup; |
af31f5e5 CM |
2024 | |
2025 | root_backup = info->super_for_commit->super_roots + next_backup; | |
2026 | ||
2027 | /* | |
2028 | * make sure all of our padding and empty slots get zero filled | |
2029 | * regardless of which ones we use today | |
2030 | */ | |
2031 | memset(root_backup, 0, sizeof(*root_backup)); | |
2032 | ||
2033 | info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; | |
2034 | ||
2035 | btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); | |
2036 | btrfs_set_backup_tree_root_gen(root_backup, | |
2037 | btrfs_header_generation(info->tree_root->node)); | |
2038 | ||
2039 | btrfs_set_backup_tree_root_level(root_backup, | |
2040 | btrfs_header_level(info->tree_root->node)); | |
2041 | ||
2042 | btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); | |
2043 | btrfs_set_backup_chunk_root_gen(root_backup, | |
2044 | btrfs_header_generation(info->chunk_root->node)); | |
2045 | btrfs_set_backup_chunk_root_level(root_backup, | |
2046 | btrfs_header_level(info->chunk_root->node)); | |
2047 | ||
2048 | btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); | |
2049 | btrfs_set_backup_extent_root_gen(root_backup, | |
2050 | btrfs_header_generation(info->extent_root->node)); | |
2051 | btrfs_set_backup_extent_root_level(root_backup, | |
2052 | btrfs_header_level(info->extent_root->node)); | |
2053 | ||
7c7e82a7 CM |
2054 | /* |
2055 | * we might commit during log recovery, which happens before we set | |
2056 | * the fs_root. Make sure it is valid before we fill it in. | |
2057 | */ | |
2058 | if (info->fs_root && info->fs_root->node) { | |
2059 | btrfs_set_backup_fs_root(root_backup, | |
2060 | info->fs_root->node->start); | |
2061 | btrfs_set_backup_fs_root_gen(root_backup, | |
af31f5e5 | 2062 | btrfs_header_generation(info->fs_root->node)); |
7c7e82a7 | 2063 | btrfs_set_backup_fs_root_level(root_backup, |
af31f5e5 | 2064 | btrfs_header_level(info->fs_root->node)); |
7c7e82a7 | 2065 | } |
af31f5e5 CM |
2066 | |
2067 | btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); | |
2068 | btrfs_set_backup_dev_root_gen(root_backup, | |
2069 | btrfs_header_generation(info->dev_root->node)); | |
2070 | btrfs_set_backup_dev_root_level(root_backup, | |
2071 | btrfs_header_level(info->dev_root->node)); | |
2072 | ||
2073 | btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); | |
2074 | btrfs_set_backup_csum_root_gen(root_backup, | |
2075 | btrfs_header_generation(info->csum_root->node)); | |
2076 | btrfs_set_backup_csum_root_level(root_backup, | |
2077 | btrfs_header_level(info->csum_root->node)); | |
2078 | ||
2079 | btrfs_set_backup_total_bytes(root_backup, | |
2080 | btrfs_super_total_bytes(info->super_copy)); | |
2081 | btrfs_set_backup_bytes_used(root_backup, | |
2082 | btrfs_super_bytes_used(info->super_copy)); | |
2083 | btrfs_set_backup_num_devices(root_backup, | |
2084 | btrfs_super_num_devices(info->super_copy)); | |
2085 | ||
2086 | /* | |
2087 | * if we don't copy this out to the super_copy, it won't get remembered | |
2088 | * for the next commit | |
2089 | */ | |
2090 | memcpy(&info->super_copy->super_roots, | |
2091 | &info->super_for_commit->super_roots, | |
2092 | sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); | |
2093 | } | |
2094 | ||
bd2336b2 NB |
2095 | /* |
2096 | * read_backup_root - Reads a backup root based on the passed priority. Prio 0 | |
2097 | * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots | |
2098 | * | |
2099 | * fs_info - filesystem whose backup roots need to be read | |
2100 | * priority - priority of backup root required | |
2101 | * | |
2102 | * Returns backup root index on success and -EINVAL otherwise. | |
2103 | */ | |
2104 | static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) | |
2105 | { | |
2106 | int backup_index = find_newest_super_backup(fs_info); | |
2107 | struct btrfs_super_block *super = fs_info->super_copy; | |
2108 | struct btrfs_root_backup *root_backup; | |
2109 | ||
2110 | if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { | |
2111 | if (priority == 0) | |
2112 | return backup_index; | |
2113 | ||
2114 | backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; | |
2115 | backup_index %= BTRFS_NUM_BACKUP_ROOTS; | |
2116 | } else { | |
2117 | return -EINVAL; | |
2118 | } | |
2119 | ||
2120 | root_backup = super->super_roots + backup_index; | |
2121 | ||
2122 | btrfs_set_super_generation(super, | |
2123 | btrfs_backup_tree_root_gen(root_backup)); | |
2124 | btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); | |
2125 | btrfs_set_super_root_level(super, | |
2126 | btrfs_backup_tree_root_level(root_backup)); | |
2127 | btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); | |
2128 | ||
2129 | /* | |
2130 | * Fixme: the total bytes and num_devices need to match or we should | |
2131 | * need a fsck | |
2132 | */ | |
2133 | btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); | |
2134 | btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); | |
2135 | ||
2136 | return backup_index; | |
2137 | } | |
2138 | ||
7abadb64 LB |
2139 | /* helper to cleanup workers */ |
2140 | static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) | |
2141 | { | |
dc6e3209 | 2142 | btrfs_destroy_workqueue(fs_info->fixup_workers); |
afe3d242 | 2143 | btrfs_destroy_workqueue(fs_info->delalloc_workers); |
5cdc7ad3 | 2144 | btrfs_destroy_workqueue(fs_info->workers); |
fccb5d86 | 2145 | btrfs_destroy_workqueue(fs_info->endio_workers); |
fccb5d86 | 2146 | btrfs_destroy_workqueue(fs_info->endio_raid56_workers); |
d05a33ac | 2147 | btrfs_destroy_workqueue(fs_info->rmw_workers); |
fccb5d86 QW |
2148 | btrfs_destroy_workqueue(fs_info->endio_write_workers); |
2149 | btrfs_destroy_workqueue(fs_info->endio_freespace_worker); | |
5b3bc44e | 2150 | btrfs_destroy_workqueue(fs_info->delayed_workers); |
e66f0bb1 | 2151 | btrfs_destroy_workqueue(fs_info->caching_workers); |
736cfa15 | 2152 | btrfs_destroy_workqueue(fs_info->readahead_workers); |
a44903ab | 2153 | btrfs_destroy_workqueue(fs_info->flush_workers); |
fc97fab0 | 2154 | btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); |
b0643e59 DZ |
2155 | if (fs_info->discard_ctl.discard_workers) |
2156 | destroy_workqueue(fs_info->discard_ctl.discard_workers); | |
a9b9477d FM |
2157 | /* |
2158 | * Now that all other work queues are destroyed, we can safely destroy | |
2159 | * the queues used for metadata I/O, since tasks from those other work | |
2160 | * queues can do metadata I/O operations. | |
2161 | */ | |
2162 | btrfs_destroy_workqueue(fs_info->endio_meta_workers); | |
2163 | btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); | |
7abadb64 LB |
2164 | } |
2165 | ||
2e9f5954 R |
2166 | static void free_root_extent_buffers(struct btrfs_root *root) |
2167 | { | |
2168 | if (root) { | |
2169 | free_extent_buffer(root->node); | |
2170 | free_extent_buffer(root->commit_root); | |
2171 | root->node = NULL; | |
2172 | root->commit_root = NULL; | |
2173 | } | |
2174 | } | |
2175 | ||
af31f5e5 | 2176 | /* helper to cleanup tree roots */ |
4273eaff | 2177 | static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) |
af31f5e5 | 2178 | { |
2e9f5954 | 2179 | free_root_extent_buffers(info->tree_root); |
655b09fe | 2180 | |
2e9f5954 R |
2181 | free_root_extent_buffers(info->dev_root); |
2182 | free_root_extent_buffers(info->extent_root); | |
2183 | free_root_extent_buffers(info->csum_root); | |
2184 | free_root_extent_buffers(info->quota_root); | |
2185 | free_root_extent_buffers(info->uuid_root); | |
8c38938c | 2186 | free_root_extent_buffers(info->fs_root); |
aeb935a4 | 2187 | free_root_extent_buffers(info->data_reloc_root); |
4273eaff | 2188 | if (free_chunk_root) |
2e9f5954 | 2189 | free_root_extent_buffers(info->chunk_root); |
70f6d82e | 2190 | free_root_extent_buffers(info->free_space_root); |
af31f5e5 CM |
2191 | } |
2192 | ||
8c38938c JB |
2193 | void btrfs_put_root(struct btrfs_root *root) |
2194 | { | |
2195 | if (!root) | |
2196 | return; | |
2197 | ||
2198 | if (refcount_dec_and_test(&root->refs)) { | |
2199 | WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); | |
1dae7e0e | 2200 | WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); |
8c38938c JB |
2201 | if (root->anon_dev) |
2202 | free_anon_bdev(root->anon_dev); | |
2203 | btrfs_drew_lock_destroy(&root->snapshot_lock); | |
923eb523 | 2204 | free_root_extent_buffers(root); |
8c38938c JB |
2205 | #ifdef CONFIG_BTRFS_DEBUG |
2206 | spin_lock(&root->fs_info->fs_roots_radix_lock); | |
2207 | list_del_init(&root->leak_list); | |
2208 | spin_unlock(&root->fs_info->fs_roots_radix_lock); | |
2209 | #endif | |
2210 | kfree(root); | |
2211 | } | |
2212 | } | |
2213 | ||
faa2dbf0 | 2214 | void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) |
171f6537 JB |
2215 | { |
2216 | int ret; | |
2217 | struct btrfs_root *gang[8]; | |
2218 | int i; | |
2219 | ||
2220 | while (!list_empty(&fs_info->dead_roots)) { | |
2221 | gang[0] = list_entry(fs_info->dead_roots.next, | |
2222 | struct btrfs_root, root_list); | |
2223 | list_del(&gang[0]->root_list); | |
2224 | ||
8c38938c | 2225 | if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) |
cb517eab | 2226 | btrfs_drop_and_free_fs_root(fs_info, gang[0]); |
dc9492c1 | 2227 | btrfs_put_root(gang[0]); |
171f6537 JB |
2228 | } |
2229 | ||
2230 | while (1) { | |
2231 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
2232 | (void **)gang, 0, | |
2233 | ARRAY_SIZE(gang)); | |
2234 | if (!ret) | |
2235 | break; | |
2236 | for (i = 0; i < ret; i++) | |
cb517eab | 2237 | btrfs_drop_and_free_fs_root(fs_info, gang[i]); |
171f6537 JB |
2238 | } |
2239 | } | |
af31f5e5 | 2240 | |
638aa7ed ES |
2241 | static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) |
2242 | { | |
2243 | mutex_init(&fs_info->scrub_lock); | |
2244 | atomic_set(&fs_info->scrubs_running, 0); | |
2245 | atomic_set(&fs_info->scrub_pause_req, 0); | |
2246 | atomic_set(&fs_info->scrubs_paused, 0); | |
2247 | atomic_set(&fs_info->scrub_cancel_req, 0); | |
2248 | init_waitqueue_head(&fs_info->scrub_pause_wait); | |
ff09c4ca | 2249 | refcount_set(&fs_info->scrub_workers_refcnt, 0); |
638aa7ed ES |
2250 | } |
2251 | ||
779a65a4 ES |
2252 | static void btrfs_init_balance(struct btrfs_fs_info *fs_info) |
2253 | { | |
2254 | spin_lock_init(&fs_info->balance_lock); | |
2255 | mutex_init(&fs_info->balance_mutex); | |
779a65a4 ES |
2256 | atomic_set(&fs_info->balance_pause_req, 0); |
2257 | atomic_set(&fs_info->balance_cancel_req, 0); | |
2258 | fs_info->balance_ctl = NULL; | |
2259 | init_waitqueue_head(&fs_info->balance_wait_q); | |
907d2710 | 2260 | atomic_set(&fs_info->reloc_cancel_req, 0); |
779a65a4 ES |
2261 | } |
2262 | ||
6bccf3ab | 2263 | static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) |
f37938e0 | 2264 | { |
2ff7e61e JM |
2265 | struct inode *inode = fs_info->btree_inode; |
2266 | ||
2267 | inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; | |
2268 | set_nlink(inode, 1); | |
f37938e0 ES |
2269 | /* |
2270 | * we set the i_size on the btree inode to the max possible int. | |
2271 | * the real end of the address space is determined by all of | |
2272 | * the devices in the system | |
2273 | */ | |
2ff7e61e JM |
2274 | inode->i_size = OFFSET_MAX; |
2275 | inode->i_mapping->a_ops = &btree_aops; | |
f37938e0 | 2276 | |
2ff7e61e | 2277 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
43eb5f29 | 2278 | extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, |
2c53a14d | 2279 | IO_TREE_BTREE_INODE_IO, inode); |
7b439738 | 2280 | BTRFS_I(inode)->io_tree.track_uptodate = false; |
2ff7e61e | 2281 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree); |
f37938e0 | 2282 | |
5c8fd99f | 2283 | BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); |
2ff7e61e JM |
2284 | memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); |
2285 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); | |
2286 | btrfs_insert_inode_hash(inode); | |
f37938e0 ES |
2287 | } |
2288 | ||
ad618368 ES |
2289 | static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) |
2290 | { | |
ad618368 | 2291 | mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); |
129827e3 | 2292 | init_rwsem(&fs_info->dev_replace.rwsem); |
7f8d236a | 2293 | init_waitqueue_head(&fs_info->dev_replace.replace_wait); |
ad618368 ES |
2294 | } |
2295 | ||
f9e92e40 ES |
2296 | static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) |
2297 | { | |
2298 | spin_lock_init(&fs_info->qgroup_lock); | |
2299 | mutex_init(&fs_info->qgroup_ioctl_lock); | |
2300 | fs_info->qgroup_tree = RB_ROOT; | |
f9e92e40 ES |
2301 | INIT_LIST_HEAD(&fs_info->dirty_qgroups); |
2302 | fs_info->qgroup_seq = 1; | |
f9e92e40 | 2303 | fs_info->qgroup_ulist = NULL; |
d2c609b8 | 2304 | fs_info->qgroup_rescan_running = false; |
f9e92e40 ES |
2305 | mutex_init(&fs_info->qgroup_rescan_lock); |
2306 | } | |
2307 | ||
2a458198 ES |
2308 | static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, |
2309 | struct btrfs_fs_devices *fs_devices) | |
2310 | { | |
f7b885be | 2311 | u32 max_active = fs_info->thread_pool_size; |
6f011058 | 2312 | unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; |
2a458198 ES |
2313 | |
2314 | fs_info->workers = | |
cb001095 JM |
2315 | btrfs_alloc_workqueue(fs_info, "worker", |
2316 | flags | WQ_HIGHPRI, max_active, 16); | |
2a458198 ES |
2317 | |
2318 | fs_info->delalloc_workers = | |
cb001095 JM |
2319 | btrfs_alloc_workqueue(fs_info, "delalloc", |
2320 | flags, max_active, 2); | |
2a458198 ES |
2321 | |
2322 | fs_info->flush_workers = | |
cb001095 JM |
2323 | btrfs_alloc_workqueue(fs_info, "flush_delalloc", |
2324 | flags, max_active, 0); | |
2a458198 ES |
2325 | |
2326 | fs_info->caching_workers = | |
cb001095 | 2327 | btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); |
2a458198 | 2328 | |
2a458198 | 2329 | fs_info->fixup_workers = |
cb001095 | 2330 | btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); |
2a458198 ES |
2331 | |
2332 | /* | |
2333 | * endios are largely parallel and should have a very | |
2334 | * low idle thresh | |
2335 | */ | |
2336 | fs_info->endio_workers = | |
cb001095 | 2337 | btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); |
2a458198 | 2338 | fs_info->endio_meta_workers = |
cb001095 JM |
2339 | btrfs_alloc_workqueue(fs_info, "endio-meta", flags, |
2340 | max_active, 4); | |
2a458198 | 2341 | fs_info->endio_meta_write_workers = |
cb001095 JM |
2342 | btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, |
2343 | max_active, 2); | |
2a458198 | 2344 | fs_info->endio_raid56_workers = |
cb001095 JM |
2345 | btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, |
2346 | max_active, 4); | |
2a458198 | 2347 | fs_info->rmw_workers = |
cb001095 | 2348 | btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); |
2a458198 | 2349 | fs_info->endio_write_workers = |
cb001095 JM |
2350 | btrfs_alloc_workqueue(fs_info, "endio-write", flags, |
2351 | max_active, 2); | |
2a458198 | 2352 | fs_info->endio_freespace_worker = |
cb001095 JM |
2353 | btrfs_alloc_workqueue(fs_info, "freespace-write", flags, |
2354 | max_active, 0); | |
2a458198 | 2355 | fs_info->delayed_workers = |
cb001095 JM |
2356 | btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, |
2357 | max_active, 0); | |
2a458198 | 2358 | fs_info->readahead_workers = |
cb001095 JM |
2359 | btrfs_alloc_workqueue(fs_info, "readahead", flags, |
2360 | max_active, 2); | |
2a458198 | 2361 | fs_info->qgroup_rescan_workers = |
cb001095 | 2362 | btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); |
b0643e59 DZ |
2363 | fs_info->discard_ctl.discard_workers = |
2364 | alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); | |
2a458198 ES |
2365 | |
2366 | if (!(fs_info->workers && fs_info->delalloc_workers && | |
ba8a9d07 | 2367 | fs_info->flush_workers && |
2a458198 ES |
2368 | fs_info->endio_workers && fs_info->endio_meta_workers && |
2369 | fs_info->endio_meta_write_workers && | |
2a458198 ES |
2370 | fs_info->endio_write_workers && fs_info->endio_raid56_workers && |
2371 | fs_info->endio_freespace_worker && fs_info->rmw_workers && | |
2372 | fs_info->caching_workers && fs_info->readahead_workers && | |
2373 | fs_info->fixup_workers && fs_info->delayed_workers && | |
b0643e59 DZ |
2374 | fs_info->qgroup_rescan_workers && |
2375 | fs_info->discard_ctl.discard_workers)) { | |
2a458198 ES |
2376 | return -ENOMEM; |
2377 | } | |
2378 | ||
2379 | return 0; | |
2380 | } | |
2381 | ||
6d97c6e3 JT |
2382 | static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) |
2383 | { | |
2384 | struct crypto_shash *csum_shash; | |
b4e967be | 2385 | const char *csum_driver = btrfs_super_csum_driver(csum_type); |
6d97c6e3 | 2386 | |
b4e967be | 2387 | csum_shash = crypto_alloc_shash(csum_driver, 0, 0); |
6d97c6e3 JT |
2388 | |
2389 | if (IS_ERR(csum_shash)) { | |
2390 | btrfs_err(fs_info, "error allocating %s hash for checksum", | |
b4e967be | 2391 | csum_driver); |
6d97c6e3 JT |
2392 | return PTR_ERR(csum_shash); |
2393 | } | |
2394 | ||
2395 | fs_info->csum_shash = csum_shash; | |
2396 | ||
2397 | return 0; | |
2398 | } | |
2399 | ||
63443bf5 ES |
2400 | static int btrfs_replay_log(struct btrfs_fs_info *fs_info, |
2401 | struct btrfs_fs_devices *fs_devices) | |
2402 | { | |
2403 | int ret; | |
63443bf5 ES |
2404 | struct btrfs_root *log_tree_root; |
2405 | struct btrfs_super_block *disk_super = fs_info->super_copy; | |
2406 | u64 bytenr = btrfs_super_log_root(disk_super); | |
581c1760 | 2407 | int level = btrfs_super_log_root_level(disk_super); |
63443bf5 ES |
2408 | |
2409 | if (fs_devices->rw_devices == 0) { | |
f14d104d | 2410 | btrfs_warn(fs_info, "log replay required on RO media"); |
63443bf5 ES |
2411 | return -EIO; |
2412 | } | |
2413 | ||
96dfcb46 JB |
2414 | log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, |
2415 | GFP_KERNEL); | |
63443bf5 ES |
2416 | if (!log_tree_root) |
2417 | return -ENOMEM; | |
2418 | ||
2ff7e61e | 2419 | log_tree_root->node = read_tree_block(fs_info, bytenr, |
1b7ec85e JB |
2420 | BTRFS_TREE_LOG_OBJECTID, |
2421 | fs_info->generation + 1, level, | |
2422 | NULL); | |
64c043de | 2423 | if (IS_ERR(log_tree_root->node)) { |
f14d104d | 2424 | btrfs_warn(fs_info, "failed to read log tree"); |
0eeff236 | 2425 | ret = PTR_ERR(log_tree_root->node); |
8c38938c | 2426 | log_tree_root->node = NULL; |
00246528 | 2427 | btrfs_put_root(log_tree_root); |
0eeff236 | 2428 | return ret; |
64c043de | 2429 | } else if (!extent_buffer_uptodate(log_tree_root->node)) { |
f14d104d | 2430 | btrfs_err(fs_info, "failed to read log tree"); |
00246528 | 2431 | btrfs_put_root(log_tree_root); |
63443bf5 ES |
2432 | return -EIO; |
2433 | } | |
2434 | /* returns with log_tree_root freed on success */ | |
2435 | ret = btrfs_recover_log_trees(log_tree_root); | |
2436 | if (ret) { | |
0b246afa JM |
2437 | btrfs_handle_fs_error(fs_info, ret, |
2438 | "Failed to recover log tree"); | |
00246528 | 2439 | btrfs_put_root(log_tree_root); |
63443bf5 ES |
2440 | return ret; |
2441 | } | |
2442 | ||
bc98a42c | 2443 | if (sb_rdonly(fs_info->sb)) { |
6bccf3ab | 2444 | ret = btrfs_commit_super(fs_info); |
63443bf5 ES |
2445 | if (ret) |
2446 | return ret; | |
2447 | } | |
2448 | ||
2449 | return 0; | |
2450 | } | |
2451 | ||
6bccf3ab | 2452 | static int btrfs_read_roots(struct btrfs_fs_info *fs_info) |
4bbcaa64 | 2453 | { |
6bccf3ab | 2454 | struct btrfs_root *tree_root = fs_info->tree_root; |
a4f3d2c4 | 2455 | struct btrfs_root *root; |
4bbcaa64 ES |
2456 | struct btrfs_key location; |
2457 | int ret; | |
2458 | ||
6bccf3ab JM |
2459 | BUG_ON(!fs_info->tree_root); |
2460 | ||
4bbcaa64 ES |
2461 | location.objectid = BTRFS_EXTENT_TREE_OBJECTID; |
2462 | location.type = BTRFS_ROOT_ITEM_KEY; | |
2463 | location.offset = 0; | |
2464 | ||
a4f3d2c4 | 2465 | root = btrfs_read_tree_root(tree_root, &location); |
f50f4353 | 2466 | if (IS_ERR(root)) { |
42437a63 JB |
2467 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { |
2468 | ret = PTR_ERR(root); | |
2469 | goto out; | |
2470 | } | |
2471 | } else { | |
2472 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
2473 | fs_info->extent_root = root; | |
f50f4353 | 2474 | } |
4bbcaa64 ES |
2475 | |
2476 | location.objectid = BTRFS_DEV_TREE_OBJECTID; | |
a4f3d2c4 | 2477 | root = btrfs_read_tree_root(tree_root, &location); |
f50f4353 | 2478 | if (IS_ERR(root)) { |
42437a63 JB |
2479 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { |
2480 | ret = PTR_ERR(root); | |
2481 | goto out; | |
2482 | } | |
2483 | } else { | |
2484 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
2485 | fs_info->dev_root = root; | |
f50f4353 | 2486 | } |
820a49da JB |
2487 | /* Initialize fs_info for all devices in any case */ |
2488 | btrfs_init_devices_late(fs_info); | |
4bbcaa64 | 2489 | |
882dbe0c JB |
2490 | /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ |
2491 | if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { | |
2492 | location.objectid = BTRFS_CSUM_TREE_OBJECTID; | |
2493 | root = btrfs_read_tree_root(tree_root, &location); | |
2494 | if (IS_ERR(root)) { | |
2495 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { | |
2496 | ret = PTR_ERR(root); | |
2497 | goto out; | |
2498 | } | |
2499 | } else { | |
2500 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
2501 | fs_info->csum_root = root; | |
42437a63 | 2502 | } |
f50f4353 | 2503 | } |
4bbcaa64 | 2504 | |
aeb935a4 QW |
2505 | /* |
2506 | * This tree can share blocks with some other fs tree during relocation | |
2507 | * and we need a proper setup by btrfs_get_fs_root | |
2508 | */ | |
56e9357a DS |
2509 | root = btrfs_get_fs_root(tree_root->fs_info, |
2510 | BTRFS_DATA_RELOC_TREE_OBJECTID, true); | |
aeb935a4 | 2511 | if (IS_ERR(root)) { |
42437a63 JB |
2512 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { |
2513 | ret = PTR_ERR(root); | |
2514 | goto out; | |
2515 | } | |
2516 | } else { | |
2517 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
2518 | fs_info->data_reloc_root = root; | |
aeb935a4 | 2519 | } |
aeb935a4 | 2520 | |
4bbcaa64 | 2521 | location.objectid = BTRFS_QUOTA_TREE_OBJECTID; |
a4f3d2c4 DS |
2522 | root = btrfs_read_tree_root(tree_root, &location); |
2523 | if (!IS_ERR(root)) { | |
2524 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
afcdd129 | 2525 | set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
a4f3d2c4 | 2526 | fs_info->quota_root = root; |
4bbcaa64 ES |
2527 | } |
2528 | ||
2529 | location.objectid = BTRFS_UUID_TREE_OBJECTID; | |
a4f3d2c4 DS |
2530 | root = btrfs_read_tree_root(tree_root, &location); |
2531 | if (IS_ERR(root)) { | |
42437a63 JB |
2532 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { |
2533 | ret = PTR_ERR(root); | |
2534 | if (ret != -ENOENT) | |
2535 | goto out; | |
2536 | } | |
4bbcaa64 | 2537 | } else { |
a4f3d2c4 DS |
2538 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); |
2539 | fs_info->uuid_root = root; | |
4bbcaa64 ES |
2540 | } |
2541 | ||
70f6d82e OS |
2542 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { |
2543 | location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; | |
2544 | root = btrfs_read_tree_root(tree_root, &location); | |
f50f4353 | 2545 | if (IS_ERR(root)) { |
42437a63 JB |
2546 | if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { |
2547 | ret = PTR_ERR(root); | |
2548 | goto out; | |
2549 | } | |
2550 | } else { | |
2551 | set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); | |
2552 | fs_info->free_space_root = root; | |
f50f4353 | 2553 | } |
70f6d82e OS |
2554 | } |
2555 | ||
4bbcaa64 | 2556 | return 0; |
f50f4353 LB |
2557 | out: |
2558 | btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", | |
2559 | location.objectid, ret); | |
2560 | return ret; | |
4bbcaa64 ES |
2561 | } |
2562 | ||
069ec957 QW |
2563 | /* |
2564 | * Real super block validation | |
2565 | * NOTE: super csum type and incompat features will not be checked here. | |
2566 | * | |
2567 | * @sb: super block to check | |
2568 | * @mirror_num: the super block number to check its bytenr: | |
2569 | * 0 the primary (1st) sb | |
2570 | * 1, 2 2nd and 3rd backup copy | |
2571 | * -1 skip bytenr check | |
2572 | */ | |
2573 | static int validate_super(struct btrfs_fs_info *fs_info, | |
2574 | struct btrfs_super_block *sb, int mirror_num) | |
21a852b0 | 2575 | { |
21a852b0 QW |
2576 | u64 nodesize = btrfs_super_nodesize(sb); |
2577 | u64 sectorsize = btrfs_super_sectorsize(sb); | |
2578 | int ret = 0; | |
2579 | ||
2580 | if (btrfs_super_magic(sb) != BTRFS_MAGIC) { | |
2581 | btrfs_err(fs_info, "no valid FS found"); | |
2582 | ret = -EINVAL; | |
2583 | } | |
2584 | if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { | |
2585 | btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", | |
2586 | btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); | |
2587 | ret = -EINVAL; | |
2588 | } | |
2589 | if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { | |
2590 | btrfs_err(fs_info, "tree_root level too big: %d >= %d", | |
2591 | btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); | |
2592 | ret = -EINVAL; | |
2593 | } | |
2594 | if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { | |
2595 | btrfs_err(fs_info, "chunk_root level too big: %d >= %d", | |
2596 | btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); | |
2597 | ret = -EINVAL; | |
2598 | } | |
2599 | if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { | |
2600 | btrfs_err(fs_info, "log_root level too big: %d >= %d", | |
2601 | btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); | |
2602 | ret = -EINVAL; | |
2603 | } | |
2604 | ||
2605 | /* | |
2606 | * Check sectorsize and nodesize first, other check will need it. | |
2607 | * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. | |
2608 | */ | |
2609 | if (!is_power_of_2(sectorsize) || sectorsize < 4096 || | |
2610 | sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { | |
2611 | btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); | |
2612 | ret = -EINVAL; | |
2613 | } | |
0bb3eb3e QW |
2614 | |
2615 | /* | |
2616 | * For 4K page size, we only support 4K sector size. | |
2617 | * For 64K page size, we support read-write for 64K sector size, and | |
2618 | * read-only for 4K sector size. | |
2619 | */ | |
2620 | if ((PAGE_SIZE == SZ_4K && sectorsize != PAGE_SIZE) || | |
2621 | (PAGE_SIZE == SZ_64K && (sectorsize != SZ_4K && | |
2622 | sectorsize != SZ_64K))) { | |
21a852b0 | 2623 | btrfs_err(fs_info, |
0bb3eb3e | 2624 | "sectorsize %llu not yet supported for page size %lu", |
21a852b0 QW |
2625 | sectorsize, PAGE_SIZE); |
2626 | ret = -EINVAL; | |
2627 | } | |
0bb3eb3e | 2628 | |
21a852b0 QW |
2629 | if (!is_power_of_2(nodesize) || nodesize < sectorsize || |
2630 | nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { | |
2631 | btrfs_err(fs_info, "invalid nodesize %llu", nodesize); | |
2632 | ret = -EINVAL; | |
2633 | } | |
2634 | if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { | |
2635 | btrfs_err(fs_info, "invalid leafsize %u, should be %llu", | |
2636 | le32_to_cpu(sb->__unused_leafsize), nodesize); | |
2637 | ret = -EINVAL; | |
2638 | } | |
2639 | ||
2640 | /* Root alignment check */ | |
2641 | if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { | |
2642 | btrfs_warn(fs_info, "tree_root block unaligned: %llu", | |
2643 | btrfs_super_root(sb)); | |
2644 | ret = -EINVAL; | |
2645 | } | |
2646 | if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { | |
2647 | btrfs_warn(fs_info, "chunk_root block unaligned: %llu", | |
2648 | btrfs_super_chunk_root(sb)); | |
2649 | ret = -EINVAL; | |
2650 | } | |
2651 | if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { | |
2652 | btrfs_warn(fs_info, "log_root block unaligned: %llu", | |
2653 | btrfs_super_log_root(sb)); | |
2654 | ret = -EINVAL; | |
2655 | } | |
2656 | ||
aefd7f70 NB |
2657 | if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, |
2658 | BTRFS_FSID_SIZE)) { | |
2659 | btrfs_err(fs_info, | |
2660 | "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", | |
2661 | fs_info->super_copy->fsid, fs_info->fs_devices->fsid); | |
2662 | ret = -EINVAL; | |
2663 | } | |
2664 | ||
2665 | if (btrfs_fs_incompat(fs_info, METADATA_UUID) && | |
2666 | memcmp(fs_info->fs_devices->metadata_uuid, | |
2667 | fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { | |
2668 | btrfs_err(fs_info, | |
2669 | "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", | |
2670 | fs_info->super_copy->metadata_uuid, | |
2671 | fs_info->fs_devices->metadata_uuid); | |
2672 | ret = -EINVAL; | |
2673 | } | |
2674 | ||
de37aa51 | 2675 | if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, |
7239ff4b | 2676 | BTRFS_FSID_SIZE) != 0) { |
21a852b0 | 2677 | btrfs_err(fs_info, |
7239ff4b | 2678 | "dev_item UUID does not match metadata fsid: %pU != %pU", |
de37aa51 | 2679 | fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); |
21a852b0 QW |
2680 | ret = -EINVAL; |
2681 | } | |
2682 | ||
2683 | /* | |
2684 | * Hint to catch really bogus numbers, bitflips or so, more exact checks are | |
2685 | * done later | |
2686 | */ | |
2687 | if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { | |
2688 | btrfs_err(fs_info, "bytes_used is too small %llu", | |
2689 | btrfs_super_bytes_used(sb)); | |
2690 | ret = -EINVAL; | |
2691 | } | |
2692 | if (!is_power_of_2(btrfs_super_stripesize(sb))) { | |
2693 | btrfs_err(fs_info, "invalid stripesize %u", | |
2694 | btrfs_super_stripesize(sb)); | |
2695 | ret = -EINVAL; | |
2696 | } | |
2697 | if (btrfs_super_num_devices(sb) > (1UL << 31)) | |
2698 | btrfs_warn(fs_info, "suspicious number of devices: %llu", | |
2699 | btrfs_super_num_devices(sb)); | |
2700 | if (btrfs_super_num_devices(sb) == 0) { | |
2701 | btrfs_err(fs_info, "number of devices is 0"); | |
2702 | ret = -EINVAL; | |
2703 | } | |
2704 | ||
069ec957 QW |
2705 | if (mirror_num >= 0 && |
2706 | btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { | |
21a852b0 QW |
2707 | btrfs_err(fs_info, "super offset mismatch %llu != %u", |
2708 | btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); | |
2709 | ret = -EINVAL; | |
2710 | } | |
2711 | ||
2712 | /* | |
2713 | * Obvious sys_chunk_array corruptions, it must hold at least one key | |
2714 | * and one chunk | |
2715 | */ | |
2716 | if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { | |
2717 | btrfs_err(fs_info, "system chunk array too big %u > %u", | |
2718 | btrfs_super_sys_array_size(sb), | |
2719 | BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); | |
2720 | ret = -EINVAL; | |
2721 | } | |
2722 | if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) | |
2723 | + sizeof(struct btrfs_chunk)) { | |
2724 | btrfs_err(fs_info, "system chunk array too small %u < %zu", | |
2725 | btrfs_super_sys_array_size(sb), | |
2726 | sizeof(struct btrfs_disk_key) | |
2727 | + sizeof(struct btrfs_chunk)); | |
2728 | ret = -EINVAL; | |
2729 | } | |
2730 | ||
2731 | /* | |
2732 | * The generation is a global counter, we'll trust it more than the others | |
2733 | * but it's still possible that it's the one that's wrong. | |
2734 | */ | |
2735 | if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) | |
2736 | btrfs_warn(fs_info, | |
2737 | "suspicious: generation < chunk_root_generation: %llu < %llu", | |
2738 | btrfs_super_generation(sb), | |
2739 | btrfs_super_chunk_root_generation(sb)); | |
2740 | if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) | |
2741 | && btrfs_super_cache_generation(sb) != (u64)-1) | |
2742 | btrfs_warn(fs_info, | |
2743 | "suspicious: generation < cache_generation: %llu < %llu", | |
2744 | btrfs_super_generation(sb), | |
2745 | btrfs_super_cache_generation(sb)); | |
2746 | ||
2747 | return ret; | |
2748 | } | |
2749 | ||
069ec957 QW |
2750 | /* |
2751 | * Validation of super block at mount time. | |
2752 | * Some checks already done early at mount time, like csum type and incompat | |
2753 | * flags will be skipped. | |
2754 | */ | |
2755 | static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) | |
2756 | { | |
2757 | return validate_super(fs_info, fs_info->super_copy, 0); | |
2758 | } | |
2759 | ||
75cb857d QW |
2760 | /* |
2761 | * Validation of super block at write time. | |
2762 | * Some checks like bytenr check will be skipped as their values will be | |
2763 | * overwritten soon. | |
2764 | * Extra checks like csum type and incompat flags will be done here. | |
2765 | */ | |
2766 | static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, | |
2767 | struct btrfs_super_block *sb) | |
2768 | { | |
2769 | int ret; | |
2770 | ||
2771 | ret = validate_super(fs_info, sb, -1); | |
2772 | if (ret < 0) | |
2773 | goto out; | |
e7e16f48 | 2774 | if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { |
75cb857d QW |
2775 | ret = -EUCLEAN; |
2776 | btrfs_err(fs_info, "invalid csum type, has %u want %u", | |
2777 | btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); | |
2778 | goto out; | |
2779 | } | |
2780 | if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { | |
2781 | ret = -EUCLEAN; | |
2782 | btrfs_err(fs_info, | |
2783 | "invalid incompat flags, has 0x%llx valid mask 0x%llx", | |
2784 | btrfs_super_incompat_flags(sb), | |
2785 | (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); | |
2786 | goto out; | |
2787 | } | |
2788 | out: | |
2789 | if (ret < 0) | |
2790 | btrfs_err(fs_info, | |
2791 | "super block corruption detected before writing it to disk"); | |
2792 | return ret; | |
2793 | } | |
2794 | ||
6ef108dd | 2795 | static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) |
b8522a1e | 2796 | { |
6ef108dd | 2797 | int backup_index = find_newest_super_backup(fs_info); |
b8522a1e NB |
2798 | struct btrfs_super_block *sb = fs_info->super_copy; |
2799 | struct btrfs_root *tree_root = fs_info->tree_root; | |
2800 | bool handle_error = false; | |
2801 | int ret = 0; | |
2802 | int i; | |
2803 | ||
2804 | for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { | |
2805 | u64 generation; | |
2806 | int level; | |
2807 | ||
2808 | if (handle_error) { | |
2809 | if (!IS_ERR(tree_root->node)) | |
2810 | free_extent_buffer(tree_root->node); | |
2811 | tree_root->node = NULL; | |
2812 | ||
2813 | if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) | |
2814 | break; | |
2815 | ||
2816 | free_root_pointers(fs_info, 0); | |
2817 | ||
2818 | /* | |
2819 | * Don't use the log in recovery mode, it won't be | |
2820 | * valid | |
2821 | */ | |
2822 | btrfs_set_super_log_root(sb, 0); | |
2823 | ||
2824 | /* We can't trust the free space cache either */ | |
2825 | btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); | |
2826 | ||
2827 | ret = read_backup_root(fs_info, i); | |
6ef108dd | 2828 | backup_index = ret; |
b8522a1e NB |
2829 | if (ret < 0) |
2830 | return ret; | |
2831 | } | |
2832 | generation = btrfs_super_generation(sb); | |
2833 | level = btrfs_super_root_level(sb); | |
2834 | tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), | |
1b7ec85e | 2835 | BTRFS_ROOT_TREE_OBJECTID, |
b8522a1e | 2836 | generation, level, NULL); |
217f5004 | 2837 | if (IS_ERR(tree_root->node)) { |
b8522a1e | 2838 | handle_error = true; |
217f5004 NB |
2839 | ret = PTR_ERR(tree_root->node); |
2840 | tree_root->node = NULL; | |
2841 | btrfs_warn(fs_info, "couldn't read tree root"); | |
2842 | continue; | |
b8522a1e | 2843 | |
217f5004 NB |
2844 | } else if (!extent_buffer_uptodate(tree_root->node)) { |
2845 | handle_error = true; | |
2846 | ret = -EIO; | |
2847 | btrfs_warn(fs_info, "error while reading tree root"); | |
b8522a1e NB |
2848 | continue; |
2849 | } | |
2850 | ||
2851 | btrfs_set_root_node(&tree_root->root_item, tree_root->node); | |
2852 | tree_root->commit_root = btrfs_root_node(tree_root); | |
2853 | btrfs_set_root_refs(&tree_root->root_item, 1); | |
2854 | ||
336a0d8d NB |
2855 | /* |
2856 | * No need to hold btrfs_root::objectid_mutex since the fs | |
2857 | * hasn't been fully initialised and we are the only user | |
2858 | */ | |
453e4873 | 2859 | ret = btrfs_init_root_free_objectid(tree_root); |
b8522a1e | 2860 | if (ret < 0) { |
b8522a1e NB |
2861 | handle_error = true; |
2862 | continue; | |
2863 | } | |
2864 | ||
6b8fad57 | 2865 | ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); |
b8522a1e NB |
2866 | |
2867 | ret = btrfs_read_roots(fs_info); | |
2868 | if (ret < 0) { | |
2869 | handle_error = true; | |
2870 | continue; | |
2871 | } | |
2872 | ||
2873 | /* All successful */ | |
2874 | fs_info->generation = generation; | |
2875 | fs_info->last_trans_committed = generation; | |
ebde420a | 2876 | fs_info->last_reloc_trans = 0; |
6ef108dd NB |
2877 | |
2878 | /* Always begin writing backup roots after the one being used */ | |
2879 | if (backup_index < 0) { | |
2880 | fs_info->backup_root_index = 0; | |
2881 | } else { | |
2882 | fs_info->backup_root_index = backup_index + 1; | |
2883 | fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; | |
2884 | } | |
b8522a1e NB |
2885 | break; |
2886 | } | |
2887 | ||
2888 | return ret; | |
2889 | } | |
2890 | ||
8260edba | 2891 | void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) |
2e635a27 | 2892 | { |
76dda93c | 2893 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); |
f28491e0 | 2894 | INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); |
8fd17795 | 2895 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 2896 | INIT_LIST_HEAD(&fs_info->dead_roots); |
24bbcf04 | 2897 | INIT_LIST_HEAD(&fs_info->delayed_iputs); |
eb73c1b7 | 2898 | INIT_LIST_HEAD(&fs_info->delalloc_roots); |
11833d66 | 2899 | INIT_LIST_HEAD(&fs_info->caching_block_groups); |
eb73c1b7 | 2900 | spin_lock_init(&fs_info->delalloc_root_lock); |
a4abeea4 | 2901 | spin_lock_init(&fs_info->trans_lock); |
76dda93c | 2902 | spin_lock_init(&fs_info->fs_roots_radix_lock); |
24bbcf04 | 2903 | spin_lock_init(&fs_info->delayed_iput_lock); |
4cb5300b | 2904 | spin_lock_init(&fs_info->defrag_inodes_lock); |
ceda0864 | 2905 | spin_lock_init(&fs_info->super_lock); |
f28491e0 | 2906 | spin_lock_init(&fs_info->buffer_lock); |
47ab2a6c | 2907 | spin_lock_init(&fs_info->unused_bgs_lock); |
40ab3be1 | 2908 | spin_lock_init(&fs_info->treelog_bg_lock); |
763e2672 | 2909 | spin_lock_init(&fs_info->relocation_bg_lock); |
f29021b2 | 2910 | rwlock_init(&fs_info->tree_mod_log_lock); |
d7c15171 | 2911 | mutex_init(&fs_info->unused_bg_unpin_mutex); |
f3372065 | 2912 | mutex_init(&fs_info->reclaim_bgs_lock); |
7585717f | 2913 | mutex_init(&fs_info->reloc_mutex); |
573bfb72 | 2914 | mutex_init(&fs_info->delalloc_root_mutex); |
0bc09ca1 | 2915 | mutex_init(&fs_info->zoned_meta_io_lock); |
de98ced9 | 2916 | seqlock_init(&fs_info->profiles_lock); |
19c00ddc | 2917 | |
0b86a832 | 2918 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
6324fbf3 | 2919 | INIT_LIST_HEAD(&fs_info->space_info); |
f29021b2 | 2920 | INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); |
47ab2a6c | 2921 | INIT_LIST_HEAD(&fs_info->unused_bgs); |
18bb8bbf | 2922 | INIT_LIST_HEAD(&fs_info->reclaim_bgs); |
bd647ce3 JB |
2923 | #ifdef CONFIG_BTRFS_DEBUG |
2924 | INIT_LIST_HEAD(&fs_info->allocated_roots); | |
3fd63727 JB |
2925 | INIT_LIST_HEAD(&fs_info->allocated_ebs); |
2926 | spin_lock_init(&fs_info->eb_leak_lock); | |
bd647ce3 | 2927 | #endif |
c8bf1b67 | 2928 | extent_map_tree_init(&fs_info->mapping_tree); |
66d8f3dd MX |
2929 | btrfs_init_block_rsv(&fs_info->global_block_rsv, |
2930 | BTRFS_BLOCK_RSV_GLOBAL); | |
66d8f3dd MX |
2931 | btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); |
2932 | btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); | |
2933 | btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); | |
2934 | btrfs_init_block_rsv(&fs_info->delayed_block_rsv, | |
2935 | BTRFS_BLOCK_RSV_DELOPS); | |
ba2c4d4e JB |
2936 | btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, |
2937 | BTRFS_BLOCK_RSV_DELREFS); | |
2938 | ||
771ed689 | 2939 | atomic_set(&fs_info->async_delalloc_pages, 0); |
4cb5300b | 2940 | atomic_set(&fs_info->defrag_running, 0); |
2fefd558 | 2941 | atomic_set(&fs_info->reada_works_cnt, 0); |
034f784d | 2942 | atomic_set(&fs_info->nr_delayed_iputs, 0); |
fc36ed7e | 2943 | atomic64_set(&fs_info->tree_mod_seq, 0); |
95ac567a | 2944 | fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; |
9ed74f2d | 2945 | fs_info->metadata_ratio = 0; |
4cb5300b | 2946 | fs_info->defrag_inodes = RB_ROOT; |
a5ed45f8 | 2947 | atomic64_set(&fs_info->free_chunk_space, 0); |
f29021b2 | 2948 | fs_info->tree_mod_log = RB_ROOT; |
8b87dc17 | 2949 | fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; |
f8c269d7 | 2950 | fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ |
90519d66 | 2951 | /* readahead state */ |
d0164adc | 2952 | INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
90519d66 | 2953 | spin_lock_init(&fs_info->reada_lock); |
fd708b81 | 2954 | btrfs_init_ref_verify(fs_info); |
c8b97818 | 2955 | |
b34b086c CM |
2956 | fs_info->thread_pool_size = min_t(unsigned long, |
2957 | num_online_cpus() + 2, 8); | |
0afbaf8c | 2958 | |
199c2a9c MX |
2959 | INIT_LIST_HEAD(&fs_info->ordered_roots); |
2960 | spin_lock_init(&fs_info->ordered_root_lock); | |
69fe2d75 | 2961 | |
638aa7ed | 2962 | btrfs_init_scrub(fs_info); |
21adbd5c SB |
2963 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
2964 | fs_info->check_integrity_print_mask = 0; | |
2965 | #endif | |
779a65a4 | 2966 | btrfs_init_balance(fs_info); |
57056740 | 2967 | btrfs_init_async_reclaim_work(fs_info); |
a2de733c | 2968 | |
0f9dd46c | 2969 | spin_lock_init(&fs_info->block_group_cache_lock); |
6bef4d31 | 2970 | fs_info->block_group_cache_tree = RB_ROOT; |
a1897fdd | 2971 | fs_info->first_logical_byte = (u64)-1; |
0f9dd46c | 2972 | |
fe119a6e NB |
2973 | extent_io_tree_init(fs_info, &fs_info->excluded_extents, |
2974 | IO_TREE_FS_EXCLUDED_EXTENTS, NULL); | |
afcdd129 | 2975 | set_bit(BTRFS_FS_BARRIER, &fs_info->flags); |
39279cc3 | 2976 | |
5a3f23d5 | 2977 | mutex_init(&fs_info->ordered_operations_mutex); |
e02119d5 | 2978 | mutex_init(&fs_info->tree_log_mutex); |
925baedd | 2979 | mutex_init(&fs_info->chunk_mutex); |
a74a4b97 CM |
2980 | mutex_init(&fs_info->transaction_kthread_mutex); |
2981 | mutex_init(&fs_info->cleaner_mutex); | |
1bbc621e | 2982 | mutex_init(&fs_info->ro_block_group_mutex); |
9e351cc8 | 2983 | init_rwsem(&fs_info->commit_root_sem); |
c71bf099 | 2984 | init_rwsem(&fs_info->cleanup_work_sem); |
76dda93c | 2985 | init_rwsem(&fs_info->subvol_sem); |
803b2f54 | 2986 | sema_init(&fs_info->uuid_tree_rescan_sem, 1); |
fa9c0d79 | 2987 | |
ad618368 | 2988 | btrfs_init_dev_replace_locks(fs_info); |
f9e92e40 | 2989 | btrfs_init_qgroup(fs_info); |
b0643e59 | 2990 | btrfs_discard_init(fs_info); |
416ac51d | 2991 | |
fa9c0d79 CM |
2992 | btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); |
2993 | btrfs_init_free_cluster(&fs_info->data_alloc_cluster); | |
2994 | ||
e6dcd2dc | 2995 | init_waitqueue_head(&fs_info->transaction_throttle); |
f9295749 | 2996 | init_waitqueue_head(&fs_info->transaction_wait); |
bb9c12c9 | 2997 | init_waitqueue_head(&fs_info->transaction_blocked_wait); |
4854ddd0 | 2998 | init_waitqueue_head(&fs_info->async_submit_wait); |
034f784d | 2999 | init_waitqueue_head(&fs_info->delayed_iputs_wait); |
3768f368 | 3000 | |
da17066c JM |
3001 | /* Usable values until the real ones are cached from the superblock */ |
3002 | fs_info->nodesize = 4096; | |
3003 | fs_info->sectorsize = 4096; | |
ab108d99 | 3004 | fs_info->sectorsize_bits = ilog2(4096); |
da17066c JM |
3005 | fs_info->stripesize = 4096; |
3006 | ||
eede2bf3 OS |
3007 | spin_lock_init(&fs_info->swapfile_pins_lock); |
3008 | fs_info->swapfile_pins = RB_ROOT; | |
3009 | ||
18bb8bbf JT |
3010 | fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; |
3011 | INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); | |
8260edba JB |
3012 | } |
3013 | ||
3014 | static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) | |
3015 | { | |
3016 | int ret; | |
3017 | ||
3018 | fs_info->sb = sb; | |
3019 | sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; | |
3020 | sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); | |
9e967495 | 3021 | |
5deb17e1 | 3022 | ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); |
ae18c37a | 3023 | if (ret) |
c75e8394 | 3024 | return ret; |
ae18c37a JB |
3025 | |
3026 | ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); | |
3027 | if (ret) | |
c75e8394 | 3028 | return ret; |
ae18c37a JB |
3029 | |
3030 | fs_info->dirty_metadata_batch = PAGE_SIZE * | |
3031 | (1 + ilog2(nr_cpu_ids)); | |
3032 | ||
3033 | ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); | |
3034 | if (ret) | |
c75e8394 | 3035 | return ret; |
ae18c37a JB |
3036 | |
3037 | ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, | |
3038 | GFP_KERNEL); | |
3039 | if (ret) | |
c75e8394 | 3040 | return ret; |
ae18c37a JB |
3041 | |
3042 | fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), | |
3043 | GFP_KERNEL); | |
c75e8394 JB |
3044 | if (!fs_info->delayed_root) |
3045 | return -ENOMEM; | |
ae18c37a JB |
3046 | btrfs_init_delayed_root(fs_info->delayed_root); |
3047 | ||
a0a1db70 FM |
3048 | if (sb_rdonly(sb)) |
3049 | set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); | |
3050 | ||
c75e8394 | 3051 | return btrfs_alloc_stripe_hash_table(fs_info); |
ae18c37a JB |
3052 | } |
3053 | ||
97f4dd09 NB |
3054 | static int btrfs_uuid_rescan_kthread(void *data) |
3055 | { | |
3056 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; | |
3057 | int ret; | |
3058 | ||
3059 | /* | |
3060 | * 1st step is to iterate through the existing UUID tree and | |
3061 | * to delete all entries that contain outdated data. | |
3062 | * 2nd step is to add all missing entries to the UUID tree. | |
3063 | */ | |
3064 | ret = btrfs_uuid_tree_iterate(fs_info); | |
3065 | if (ret < 0) { | |
c94bec2c JB |
3066 | if (ret != -EINTR) |
3067 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", | |
3068 | ret); | |
97f4dd09 NB |
3069 | up(&fs_info->uuid_tree_rescan_sem); |
3070 | return ret; | |
3071 | } | |
3072 | return btrfs_uuid_scan_kthread(data); | |
3073 | } | |
3074 | ||
3075 | static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) | |
3076 | { | |
3077 | struct task_struct *task; | |
3078 | ||
3079 | down(&fs_info->uuid_tree_rescan_sem); | |
3080 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); | |
3081 | if (IS_ERR(task)) { | |
3082 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ | |
3083 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); | |
3084 | up(&fs_info->uuid_tree_rescan_sem); | |
3085 | return PTR_ERR(task); | |
3086 | } | |
3087 | ||
3088 | return 0; | |
3089 | } | |
3090 | ||
8cd29088 BB |
3091 | /* |
3092 | * Some options only have meaning at mount time and shouldn't persist across | |
3093 | * remounts, or be displayed. Clear these at the end of mount and remount | |
3094 | * code paths. | |
3095 | */ | |
3096 | void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) | |
3097 | { | |
3098 | btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); | |
8b228324 | 3099 | btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); |
8cd29088 BB |
3100 | } |
3101 | ||
44c0ca21 BB |
3102 | /* |
3103 | * Mounting logic specific to read-write file systems. Shared by open_ctree | |
3104 | * and btrfs_remount when remounting from read-only to read-write. | |
3105 | */ | |
3106 | int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) | |
3107 | { | |
3108 | int ret; | |
94846229 | 3109 | const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); |
8b228324 BB |
3110 | bool clear_free_space_tree = false; |
3111 | ||
3112 | if (btrfs_test_opt(fs_info, CLEAR_CACHE) && | |
3113 | btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { | |
3114 | clear_free_space_tree = true; | |
3115 | } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && | |
3116 | !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { | |
3117 | btrfs_warn(fs_info, "free space tree is invalid"); | |
3118 | clear_free_space_tree = true; | |
3119 | } | |
3120 | ||
3121 | if (clear_free_space_tree) { | |
3122 | btrfs_info(fs_info, "clearing free space tree"); | |
3123 | ret = btrfs_clear_free_space_tree(fs_info); | |
3124 | if (ret) { | |
3125 | btrfs_warn(fs_info, | |
3126 | "failed to clear free space tree: %d", ret); | |
3127 | goto out; | |
3128 | } | |
3129 | } | |
44c0ca21 | 3130 | |
8d488a8c FM |
3131 | /* |
3132 | * btrfs_find_orphan_roots() is responsible for finding all the dead | |
3133 | * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load | |
3134 | * them into the fs_info->fs_roots_radix tree. This must be done before | |
3135 | * calling btrfs_orphan_cleanup() on the tree root. If we don't do it | |
3136 | * first, then btrfs_orphan_cleanup() will delete a dead root's orphan | |
3137 | * item before the root's tree is deleted - this means that if we unmount | |
3138 | * or crash before the deletion completes, on the next mount we will not | |
3139 | * delete what remains of the tree because the orphan item does not | |
3140 | * exists anymore, which is what tells us we have a pending deletion. | |
3141 | */ | |
3142 | ret = btrfs_find_orphan_roots(fs_info); | |
3143 | if (ret) | |
3144 | goto out; | |
3145 | ||
44c0ca21 BB |
3146 | ret = btrfs_cleanup_fs_roots(fs_info); |
3147 | if (ret) | |
3148 | goto out; | |
3149 | ||
8f1c21d7 BB |
3150 | down_read(&fs_info->cleanup_work_sem); |
3151 | if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || | |
3152 | (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { | |
3153 | up_read(&fs_info->cleanup_work_sem); | |
3154 | goto out; | |
3155 | } | |
3156 | up_read(&fs_info->cleanup_work_sem); | |
3157 | ||
44c0ca21 BB |
3158 | mutex_lock(&fs_info->cleaner_mutex); |
3159 | ret = btrfs_recover_relocation(fs_info->tree_root); | |
3160 | mutex_unlock(&fs_info->cleaner_mutex); | |
3161 | if (ret < 0) { | |
3162 | btrfs_warn(fs_info, "failed to recover relocation: %d", ret); | |
3163 | goto out; | |
3164 | } | |
3165 | ||
5011139a BB |
3166 | if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && |
3167 | !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { | |
3168 | btrfs_info(fs_info, "creating free space tree"); | |
3169 | ret = btrfs_create_free_space_tree(fs_info); | |
3170 | if (ret) { | |
3171 | btrfs_warn(fs_info, | |
3172 | "failed to create free space tree: %d", ret); | |
3173 | goto out; | |
3174 | } | |
3175 | } | |
3176 | ||
94846229 BB |
3177 | if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { |
3178 | ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); | |
3179 | if (ret) | |
3180 | goto out; | |
3181 | } | |
3182 | ||
44c0ca21 BB |
3183 | ret = btrfs_resume_balance_async(fs_info); |
3184 | if (ret) | |
3185 | goto out; | |
3186 | ||
3187 | ret = btrfs_resume_dev_replace_async(fs_info); | |
3188 | if (ret) { | |
3189 | btrfs_warn(fs_info, "failed to resume dev_replace"); | |
3190 | goto out; | |
3191 | } | |
3192 | ||
3193 | btrfs_qgroup_rescan_resume(fs_info); | |
3194 | ||
3195 | if (!fs_info->uuid_root) { | |
3196 | btrfs_info(fs_info, "creating UUID tree"); | |
3197 | ret = btrfs_create_uuid_tree(fs_info); | |
3198 | if (ret) { | |
3199 | btrfs_warn(fs_info, | |
3200 | "failed to create the UUID tree %d", ret); | |
3201 | goto out; | |
3202 | } | |
3203 | } | |
3204 | ||
3205 | out: | |
3206 | return ret; | |
3207 | } | |
3208 | ||
ae18c37a JB |
3209 | int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, |
3210 | char *options) | |
3211 | { | |
3212 | u32 sectorsize; | |
3213 | u32 nodesize; | |
3214 | u32 stripesize; | |
3215 | u64 generation; | |
3216 | u64 features; | |
3217 | u16 csum_type; | |
ae18c37a JB |
3218 | struct btrfs_super_block *disk_super; |
3219 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); | |
3220 | struct btrfs_root *tree_root; | |
3221 | struct btrfs_root *chunk_root; | |
3222 | int ret; | |
3223 | int err = -EINVAL; | |
ae18c37a JB |
3224 | int level; |
3225 | ||
8260edba | 3226 | ret = init_mount_fs_info(fs_info, sb); |
53b381b3 | 3227 | if (ret) { |
83c8266a | 3228 | err = ret; |
ae18c37a | 3229 | goto fail; |
53b381b3 DW |
3230 | } |
3231 | ||
ae18c37a JB |
3232 | /* These need to be init'ed before we start creating inodes and such. */ |
3233 | tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, | |
3234 | GFP_KERNEL); | |
3235 | fs_info->tree_root = tree_root; | |
3236 | chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, | |
3237 | GFP_KERNEL); | |
3238 | fs_info->chunk_root = chunk_root; | |
3239 | if (!tree_root || !chunk_root) { | |
3240 | err = -ENOMEM; | |
c75e8394 | 3241 | goto fail; |
ae18c37a JB |
3242 | } |
3243 | ||
3244 | fs_info->btree_inode = new_inode(sb); | |
3245 | if (!fs_info->btree_inode) { | |
3246 | err = -ENOMEM; | |
c75e8394 | 3247 | goto fail; |
ae18c37a JB |
3248 | } |
3249 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); | |
3250 | btrfs_init_btree_inode(fs_info); | |
3251 | ||
678e71f9 | 3252 | invalidate_bdev(fs_devices->latest_dev->bdev); |
1104a885 DS |
3253 | |
3254 | /* | |
3255 | * Read super block and check the signature bytes only | |
3256 | */ | |
678e71f9 | 3257 | disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); |
8f32380d JT |
3258 | if (IS_ERR(disk_super)) { |
3259 | err = PTR_ERR(disk_super); | |
16cdcec7 | 3260 | goto fail_alloc; |
20b45077 | 3261 | } |
39279cc3 | 3262 | |
8dc3f22c | 3263 | /* |
260db43c | 3264 | * Verify the type first, if that or the checksum value are |
8dc3f22c JT |
3265 | * corrupted, we'll find out |
3266 | */ | |
8f32380d | 3267 | csum_type = btrfs_super_csum_type(disk_super); |
51bce6c9 | 3268 | if (!btrfs_supported_super_csum(csum_type)) { |
8dc3f22c | 3269 | btrfs_err(fs_info, "unsupported checksum algorithm: %u", |
51bce6c9 | 3270 | csum_type); |
8dc3f22c | 3271 | err = -EINVAL; |
8f32380d | 3272 | btrfs_release_disk_super(disk_super); |
8dc3f22c JT |
3273 | goto fail_alloc; |
3274 | } | |
3275 | ||
83c68bbc SY |
3276 | fs_info->csum_size = btrfs_super_csum_size(disk_super); |
3277 | ||
6d97c6e3 JT |
3278 | ret = btrfs_init_csum_hash(fs_info, csum_type); |
3279 | if (ret) { | |
3280 | err = ret; | |
8f32380d | 3281 | btrfs_release_disk_super(disk_super); |
6d97c6e3 JT |
3282 | goto fail_alloc; |
3283 | } | |
3284 | ||
1104a885 DS |
3285 | /* |
3286 | * We want to check superblock checksum, the type is stored inside. | |
3287 | * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). | |
3288 | */ | |
8f32380d | 3289 | if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { |
05135f59 | 3290 | btrfs_err(fs_info, "superblock checksum mismatch"); |
1104a885 | 3291 | err = -EINVAL; |
8f32380d | 3292 | btrfs_release_disk_super(disk_super); |
141386e1 | 3293 | goto fail_alloc; |
1104a885 DS |
3294 | } |
3295 | ||
3296 | /* | |
3297 | * super_copy is zeroed at allocation time and we never touch the | |
3298 | * following bytes up to INFO_SIZE, the checksum is calculated from | |
3299 | * the whole block of INFO_SIZE | |
3300 | */ | |
8f32380d JT |
3301 | memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); |
3302 | btrfs_release_disk_super(disk_super); | |
5f39d397 | 3303 | |
fbc6feae NB |
3304 | disk_super = fs_info->super_copy; |
3305 | ||
0b86a832 | 3306 | |
fbc6feae NB |
3307 | features = btrfs_super_flags(disk_super); |
3308 | if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { | |
3309 | features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; | |
3310 | btrfs_set_super_flags(disk_super, features); | |
3311 | btrfs_info(fs_info, | |
3312 | "found metadata UUID change in progress flag, clearing"); | |
3313 | } | |
3314 | ||
3315 | memcpy(fs_info->super_for_commit, fs_info->super_copy, | |
3316 | sizeof(*fs_info->super_for_commit)); | |
de37aa51 | 3317 | |
069ec957 | 3318 | ret = btrfs_validate_mount_super(fs_info); |
1104a885 | 3319 | if (ret) { |
05135f59 | 3320 | btrfs_err(fs_info, "superblock contains fatal errors"); |
1104a885 | 3321 | err = -EINVAL; |
141386e1 | 3322 | goto fail_alloc; |
1104a885 DS |
3323 | } |
3324 | ||
0f7d52f4 | 3325 | if (!btrfs_super_root(disk_super)) |
141386e1 | 3326 | goto fail_alloc; |
0f7d52f4 | 3327 | |
acce952b | 3328 | /* check FS state, whether FS is broken. */ |
87533c47 MX |
3329 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) |
3330 | set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); | |
acce952b | 3331 | |
75e7cb7f LB |
3332 | /* |
3333 | * In the long term, we'll store the compression type in the super | |
3334 | * block, and it'll be used for per file compression control. | |
3335 | */ | |
3336 | fs_info->compress_type = BTRFS_COMPRESS_ZLIB; | |
3337 | ||
6f93e834 AJ |
3338 | /* |
3339 | * Flag our filesystem as having big metadata blocks if they are bigger | |
3340 | * than the page size. | |
3341 | */ | |
3342 | if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { | |
3343 | if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) | |
3344 | btrfs_info(fs_info, | |
3345 | "flagging fs with big metadata feature"); | |
3346 | features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; | |
3347 | } | |
3348 | ||
3349 | /* Set up fs_info before parsing mount options */ | |
3350 | nodesize = btrfs_super_nodesize(disk_super); | |
3351 | sectorsize = btrfs_super_sectorsize(disk_super); | |
3352 | stripesize = sectorsize; | |
3353 | fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); | |
3354 | fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); | |
3355 | ||
3356 | fs_info->nodesize = nodesize; | |
3357 | fs_info->sectorsize = sectorsize; | |
3358 | fs_info->sectorsize_bits = ilog2(sectorsize); | |
3359 | fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; | |
3360 | fs_info->stripesize = stripesize; | |
3361 | ||
2ff7e61e | 3362 | ret = btrfs_parse_options(fs_info, options, sb->s_flags); |
2b82032c YZ |
3363 | if (ret) { |
3364 | err = ret; | |
141386e1 | 3365 | goto fail_alloc; |
2b82032c | 3366 | } |
dfe25020 | 3367 | |
f2b636e8 JB |
3368 | features = btrfs_super_incompat_flags(disk_super) & |
3369 | ~BTRFS_FEATURE_INCOMPAT_SUPP; | |
3370 | if (features) { | |
05135f59 DS |
3371 | btrfs_err(fs_info, |
3372 | "cannot mount because of unsupported optional features (%llx)", | |
3373 | features); | |
f2b636e8 | 3374 | err = -EINVAL; |
141386e1 | 3375 | goto fail_alloc; |
f2b636e8 JB |
3376 | } |
3377 | ||
5d4f98a2 | 3378 | features = btrfs_super_incompat_flags(disk_super); |
a6fa6fae | 3379 | features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; |
0b246afa | 3380 | if (fs_info->compress_type == BTRFS_COMPRESS_LZO) |
a6fa6fae | 3381 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; |
5c1aab1d NT |
3382 | else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) |
3383 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; | |
727011e0 | 3384 | |
3173a18f | 3385 | if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) |
05135f59 | 3386 | btrfs_info(fs_info, "has skinny extents"); |
3173a18f | 3387 | |
bc3f116f CM |
3388 | /* |
3389 | * mixed block groups end up with duplicate but slightly offset | |
3390 | * extent buffers for the same range. It leads to corruptions | |
3391 | */ | |
3392 | if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && | |
707e8a07 | 3393 | (sectorsize != nodesize)) { |
05135f59 DS |
3394 | btrfs_err(fs_info, |
3395 | "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", | |
3396 | nodesize, sectorsize); | |
141386e1 | 3397 | goto fail_alloc; |
bc3f116f CM |
3398 | } |
3399 | ||
ceda0864 MX |
3400 | /* |
3401 | * Needn't use the lock because there is no other task which will | |
3402 | * update the flag. | |
3403 | */ | |
a6fa6fae | 3404 | btrfs_set_super_incompat_flags(disk_super, features); |
5d4f98a2 | 3405 | |
f2b636e8 JB |
3406 | features = btrfs_super_compat_ro_flags(disk_super) & |
3407 | ~BTRFS_FEATURE_COMPAT_RO_SUPP; | |
bc98a42c | 3408 | if (!sb_rdonly(sb) && features) { |
05135f59 DS |
3409 | btrfs_err(fs_info, |
3410 | "cannot mount read-write because of unsupported optional features (%llx)", | |
c1c9ff7c | 3411 | features); |
f2b636e8 | 3412 | err = -EINVAL; |
141386e1 | 3413 | goto fail_alloc; |
f2b636e8 | 3414 | } |
61d92c32 | 3415 | |
95ea0486 QW |
3416 | if (sectorsize != PAGE_SIZE) { |
3417 | btrfs_warn(fs_info, | |
3418 | "read-write for sector size %u with page size %lu is experimental", | |
3419 | sectorsize, PAGE_SIZE); | |
0bb3eb3e | 3420 | } |
c8050b3b QW |
3421 | if (sectorsize != PAGE_SIZE) { |
3422 | if (btrfs_super_incompat_flags(fs_info->super_copy) & | |
3423 | BTRFS_FEATURE_INCOMPAT_RAID56) { | |
3424 | btrfs_err(fs_info, | |
3425 | "RAID56 is not yet supported for sector size %u with page size %lu", | |
3426 | sectorsize, PAGE_SIZE); | |
3427 | err = -EINVAL; | |
3428 | goto fail_alloc; | |
3429 | } | |
3430 | } | |
0bb3eb3e | 3431 | |
2a458198 ES |
3432 | ret = btrfs_init_workqueues(fs_info, fs_devices); |
3433 | if (ret) { | |
3434 | err = ret; | |
0dc3b84a JB |
3435 | goto fail_sb_buffer; |
3436 | } | |
4543df7e | 3437 | |
9e11ceee JK |
3438 | sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); |
3439 | sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); | |
4575c9cc | 3440 | |
a061fc8d CM |
3441 | sb->s_blocksize = sectorsize; |
3442 | sb->s_blocksize_bits = blksize_bits(sectorsize); | |
de37aa51 | 3443 | memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); |
db94535d | 3444 | |
925baedd | 3445 | mutex_lock(&fs_info->chunk_mutex); |
6bccf3ab | 3446 | ret = btrfs_read_sys_array(fs_info); |
925baedd | 3447 | mutex_unlock(&fs_info->chunk_mutex); |
84eed90f | 3448 | if (ret) { |
05135f59 | 3449 | btrfs_err(fs_info, "failed to read the system array: %d", ret); |
5d4f98a2 | 3450 | goto fail_sb_buffer; |
84eed90f | 3451 | } |
0b86a832 | 3452 | |
84234f3a | 3453 | generation = btrfs_super_chunk_root_generation(disk_super); |
581c1760 | 3454 | level = btrfs_super_chunk_root_level(disk_super); |
0b86a832 | 3455 | |
2ff7e61e | 3456 | chunk_root->node = read_tree_block(fs_info, |
0b86a832 | 3457 | btrfs_super_chunk_root(disk_super), |
1b7ec85e | 3458 | BTRFS_CHUNK_TREE_OBJECTID, |
581c1760 | 3459 | generation, level, NULL); |
64c043de LB |
3460 | if (IS_ERR(chunk_root->node) || |
3461 | !extent_buffer_uptodate(chunk_root->node)) { | |
05135f59 | 3462 | btrfs_err(fs_info, "failed to read chunk root"); |
e5fffbac | 3463 | if (!IS_ERR(chunk_root->node)) |
3464 | free_extent_buffer(chunk_root->node); | |
95ab1f64 | 3465 | chunk_root->node = NULL; |
af31f5e5 | 3466 | goto fail_tree_roots; |
83121942 | 3467 | } |
5d4f98a2 YZ |
3468 | btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); |
3469 | chunk_root->commit_root = btrfs_root_node(chunk_root); | |
0b86a832 | 3470 | |
e17cade2 | 3471 | read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, |
c4ac7541 DS |
3472 | offsetof(struct btrfs_header, chunk_tree_uuid), |
3473 | BTRFS_UUID_SIZE); | |
e17cade2 | 3474 | |
5b4aacef | 3475 | ret = btrfs_read_chunk_tree(fs_info); |
2b82032c | 3476 | if (ret) { |
05135f59 | 3477 | btrfs_err(fs_info, "failed to read chunk tree: %d", ret); |
af31f5e5 | 3478 | goto fail_tree_roots; |
2b82032c | 3479 | } |
0b86a832 | 3480 | |
8dabb742 | 3481 | /* |
bacce86a AJ |
3482 | * At this point we know all the devices that make this filesystem, |
3483 | * including the seed devices but we don't know yet if the replace | |
3484 | * target is required. So free devices that are not part of this | |
1a9fd417 | 3485 | * filesystem but skip the replace target device which is checked |
bacce86a | 3486 | * below in btrfs_init_dev_replace(). |
8dabb742 | 3487 | */ |
bacce86a | 3488 | btrfs_free_extra_devids(fs_devices); |
678e71f9 | 3489 | if (!fs_devices->latest_dev->bdev) { |
05135f59 | 3490 | btrfs_err(fs_info, "failed to read devices"); |
a6b0d5c8 CM |
3491 | goto fail_tree_roots; |
3492 | } | |
3493 | ||
b8522a1e | 3494 | ret = init_tree_roots(fs_info); |
4bbcaa64 | 3495 | if (ret) |
b8522a1e | 3496 | goto fail_tree_roots; |
8929ecfa | 3497 | |
73651042 NA |
3498 | /* |
3499 | * Get zone type information of zoned block devices. This will also | |
3500 | * handle emulation of a zoned filesystem if a regular device has the | |
3501 | * zoned incompat feature flag set. | |
3502 | */ | |
3503 | ret = btrfs_get_dev_zone_info_all_devices(fs_info); | |
3504 | if (ret) { | |
3505 | btrfs_err(fs_info, | |
3506 | "zoned: failed to read device zone info: %d", | |
3507 | ret); | |
3508 | goto fail_block_groups; | |
3509 | } | |
3510 | ||
75ec1db8 JB |
3511 | /* |
3512 | * If we have a uuid root and we're not being told to rescan we need to | |
3513 | * check the generation here so we can set the | |
3514 | * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the | |
3515 | * transaction during a balance or the log replay without updating the | |
3516 | * uuid generation, and then if we crash we would rescan the uuid tree, | |
3517 | * even though it was perfectly fine. | |
3518 | */ | |
3519 | if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && | |
3520 | fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) | |
3521 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); | |
3522 | ||
cf90d884 QW |
3523 | ret = btrfs_verify_dev_extents(fs_info); |
3524 | if (ret) { | |
3525 | btrfs_err(fs_info, | |
3526 | "failed to verify dev extents against chunks: %d", | |
3527 | ret); | |
3528 | goto fail_block_groups; | |
3529 | } | |
68310a5e ID |
3530 | ret = btrfs_recover_balance(fs_info); |
3531 | if (ret) { | |
05135f59 | 3532 | btrfs_err(fs_info, "failed to recover balance: %d", ret); |
68310a5e ID |
3533 | goto fail_block_groups; |
3534 | } | |
3535 | ||
733f4fbb SB |
3536 | ret = btrfs_init_dev_stats(fs_info); |
3537 | if (ret) { | |
05135f59 | 3538 | btrfs_err(fs_info, "failed to init dev_stats: %d", ret); |
733f4fbb SB |
3539 | goto fail_block_groups; |
3540 | } | |
3541 | ||
8dabb742 SB |
3542 | ret = btrfs_init_dev_replace(fs_info); |
3543 | if (ret) { | |
05135f59 | 3544 | btrfs_err(fs_info, "failed to init dev_replace: %d", ret); |
8dabb742 SB |
3545 | goto fail_block_groups; |
3546 | } | |
3547 | ||
b70f5097 NA |
3548 | ret = btrfs_check_zoned_mode(fs_info); |
3549 | if (ret) { | |
3550 | btrfs_err(fs_info, "failed to initialize zoned mode: %d", | |
3551 | ret); | |
3552 | goto fail_block_groups; | |
3553 | } | |
3554 | ||
c6761a9e | 3555 | ret = btrfs_sysfs_add_fsid(fs_devices); |
b7c35e81 | 3556 | if (ret) { |
05135f59 DS |
3557 | btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", |
3558 | ret); | |
b7c35e81 AJ |
3559 | goto fail_block_groups; |
3560 | } | |
3561 | ||
96f3136e | 3562 | ret = btrfs_sysfs_add_mounted(fs_info); |
c59021f8 | 3563 | if (ret) { |
05135f59 | 3564 | btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); |
b7c35e81 | 3565 | goto fail_fsdev_sysfs; |
c59021f8 | 3566 | } |
3567 | ||
c59021f8 | 3568 | ret = btrfs_init_space_info(fs_info); |
3569 | if (ret) { | |
05135f59 | 3570 | btrfs_err(fs_info, "failed to initialize space info: %d", ret); |
2365dd3c | 3571 | goto fail_sysfs; |
c59021f8 | 3572 | } |
3573 | ||
5b4aacef | 3574 | ret = btrfs_read_block_groups(fs_info); |
1b1d1f66 | 3575 | if (ret) { |
05135f59 | 3576 | btrfs_err(fs_info, "failed to read block groups: %d", ret); |
2365dd3c | 3577 | goto fail_sysfs; |
1b1d1f66 | 3578 | } |
4330e183 | 3579 | |
007648db NA |
3580 | btrfs_free_zone_cache(fs_info); |
3581 | ||
510bfab0 AJ |
3582 | if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && |
3583 | !btrfs_check_rw_degradable(fs_info, NULL)) { | |
05135f59 | 3584 | btrfs_warn(fs_info, |
52042d8e | 3585 | "writable mount is not allowed due to too many missing devices"); |
2365dd3c | 3586 | goto fail_sysfs; |
292fd7fc | 3587 | } |
9078a3e1 | 3588 | |
a74a4b97 CM |
3589 | fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, |
3590 | "btrfs-cleaner"); | |
57506d50 | 3591 | if (IS_ERR(fs_info->cleaner_kthread)) |
2365dd3c | 3592 | goto fail_sysfs; |
a74a4b97 CM |
3593 | |
3594 | fs_info->transaction_kthread = kthread_run(transaction_kthread, | |
3595 | tree_root, | |
3596 | "btrfs-transaction"); | |
57506d50 | 3597 | if (IS_ERR(fs_info->transaction_kthread)) |
3f157a2f | 3598 | goto fail_cleaner; |
a74a4b97 | 3599 | |
583b7231 | 3600 | if (!btrfs_test_opt(fs_info, NOSSD) && |
c289811c | 3601 | !fs_info->fs_devices->rotating) { |
583b7231 | 3602 | btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); |
c289811c CM |
3603 | } |
3604 | ||
572d9ab7 | 3605 | /* |
01327610 | 3606 | * Mount does not set all options immediately, we can do it now and do |
572d9ab7 DS |
3607 | * not have to wait for transaction commit |
3608 | */ | |
3609 | btrfs_apply_pending_changes(fs_info); | |
3818aea2 | 3610 | |
21adbd5c | 3611 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
0b246afa | 3612 | if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { |
2ff7e61e | 3613 | ret = btrfsic_mount(fs_info, fs_devices, |
0b246afa | 3614 | btrfs_test_opt(fs_info, |
cbeaae4f | 3615 | CHECK_INTEGRITY_DATA) ? 1 : 0, |
21adbd5c SB |
3616 | fs_info->check_integrity_print_mask); |
3617 | if (ret) | |
05135f59 DS |
3618 | btrfs_warn(fs_info, |
3619 | "failed to initialize integrity check module: %d", | |
3620 | ret); | |
21adbd5c SB |
3621 | } |
3622 | #endif | |
bcef60f2 AJ |
3623 | ret = btrfs_read_qgroup_config(fs_info); |
3624 | if (ret) | |
3625 | goto fail_trans_kthread; | |
21adbd5c | 3626 | |
fd708b81 JB |
3627 | if (btrfs_build_ref_tree(fs_info)) |
3628 | btrfs_err(fs_info, "couldn't build ref tree"); | |
3629 | ||
96da0919 QW |
3630 | /* do not make disk changes in broken FS or nologreplay is given */ |
3631 | if (btrfs_super_log_root(disk_super) != 0 && | |
0b246afa | 3632 | !btrfs_test_opt(fs_info, NOLOGREPLAY)) { |
e8294f2f | 3633 | btrfs_info(fs_info, "start tree-log replay"); |
63443bf5 | 3634 | ret = btrfs_replay_log(fs_info, fs_devices); |
79787eaa | 3635 | if (ret) { |
63443bf5 | 3636 | err = ret; |
28c16cbb | 3637 | goto fail_qgroup; |
79787eaa | 3638 | } |
e02119d5 | 3639 | } |
1a40e23b | 3640 | |
56e9357a | 3641 | fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); |
3140c9a3 DC |
3642 | if (IS_ERR(fs_info->fs_root)) { |
3643 | err = PTR_ERR(fs_info->fs_root); | |
f50f4353 | 3644 | btrfs_warn(fs_info, "failed to read fs tree: %d", err); |
315bf8ef | 3645 | fs_info->fs_root = NULL; |
bcef60f2 | 3646 | goto fail_qgroup; |
3140c9a3 | 3647 | } |
c289811c | 3648 | |
bc98a42c | 3649 | if (sb_rdonly(sb)) |
8cd29088 | 3650 | goto clear_oneshot; |
59641015 | 3651 | |
44c0ca21 | 3652 | ret = btrfs_start_pre_rw_mount(fs_info); |
2b6ba629 | 3653 | if (ret) { |
6bccf3ab | 3654 | close_ctree(fs_info); |
2b6ba629 | 3655 | return ret; |
e3acc2a6 | 3656 | } |
b0643e59 | 3657 | btrfs_discard_resume(fs_info); |
b382a324 | 3658 | |
44c0ca21 BB |
3659 | if (fs_info->uuid_root && |
3660 | (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || | |
3661 | fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { | |
05135f59 | 3662 | btrfs_info(fs_info, "checking UUID tree"); |
70f80175 SB |
3663 | ret = btrfs_check_uuid_tree(fs_info); |
3664 | if (ret) { | |
05135f59 DS |
3665 | btrfs_warn(fs_info, |
3666 | "failed to check the UUID tree: %d", ret); | |
6bccf3ab | 3667 | close_ctree(fs_info); |
70f80175 SB |
3668 | return ret; |
3669 | } | |
f7a81ea4 | 3670 | } |
94846229 | 3671 | |
afcdd129 | 3672 | set_bit(BTRFS_FS_OPEN, &fs_info->flags); |
47ab2a6c | 3673 | |
78db1921 JB |
3674 | /* Kick the cleaner thread so it'll start deleting snapshots. */ |
3675 | if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) | |
3676 | wake_up_process(fs_info->cleaner_kthread); | |
3677 | ||
8cd29088 BB |
3678 | clear_oneshot: |
3679 | btrfs_clear_oneshot_options(fs_info); | |
ad2b2c80 | 3680 | return 0; |
39279cc3 | 3681 | |
bcef60f2 AJ |
3682 | fail_qgroup: |
3683 | btrfs_free_qgroup_config(fs_info); | |
7c2ca468 CM |
3684 | fail_trans_kthread: |
3685 | kthread_stop(fs_info->transaction_kthread); | |
2ff7e61e | 3686 | btrfs_cleanup_transaction(fs_info); |
faa2dbf0 | 3687 | btrfs_free_fs_roots(fs_info); |
3f157a2f | 3688 | fail_cleaner: |
a74a4b97 | 3689 | kthread_stop(fs_info->cleaner_kthread); |
7c2ca468 CM |
3690 | |
3691 | /* | |
3692 | * make sure we're done with the btree inode before we stop our | |
3693 | * kthreads | |
3694 | */ | |
3695 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); | |
7c2ca468 | 3696 | |
2365dd3c | 3697 | fail_sysfs: |
6618a59b | 3698 | btrfs_sysfs_remove_mounted(fs_info); |
2365dd3c | 3699 | |
b7c35e81 AJ |
3700 | fail_fsdev_sysfs: |
3701 | btrfs_sysfs_remove_fsid(fs_info->fs_devices); | |
3702 | ||
1b1d1f66 | 3703 | fail_block_groups: |
54067ae9 | 3704 | btrfs_put_block_group_cache(fs_info); |
af31f5e5 CM |
3705 | |
3706 | fail_tree_roots: | |
9e3aa805 JB |
3707 | if (fs_info->data_reloc_root) |
3708 | btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); | |
4273eaff | 3709 | free_root_pointers(fs_info, true); |
2b8195bb | 3710 | invalidate_inode_pages2(fs_info->btree_inode->i_mapping); |
af31f5e5 | 3711 | |
39279cc3 | 3712 | fail_sb_buffer: |
7abadb64 | 3713 | btrfs_stop_all_workers(fs_info); |
5cdd7db6 | 3714 | btrfs_free_block_groups(fs_info); |
16cdcec7 | 3715 | fail_alloc: |
586e46e2 ID |
3716 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
3717 | ||
4543df7e | 3718 | iput(fs_info->btree_inode); |
7e662854 | 3719 | fail: |
586e46e2 | 3720 | btrfs_close_devices(fs_info->fs_devices); |
ad2b2c80 | 3721 | return err; |
eb60ceac | 3722 | } |
663faf9f | 3723 | ALLOW_ERROR_INJECTION(open_ctree, ERRNO); |
eb60ceac | 3724 | |
314b6dd0 | 3725 | static void btrfs_end_super_write(struct bio *bio) |
f2984462 | 3726 | { |
314b6dd0 JT |
3727 | struct btrfs_device *device = bio->bi_private; |
3728 | struct bio_vec *bvec; | |
3729 | struct bvec_iter_all iter_all; | |
3730 | struct page *page; | |
3731 | ||
3732 | bio_for_each_segment_all(bvec, bio, iter_all) { | |
3733 | page = bvec->bv_page; | |
3734 | ||
3735 | if (bio->bi_status) { | |
3736 | btrfs_warn_rl_in_rcu(device->fs_info, | |
3737 | "lost page write due to IO error on %s (%d)", | |
3738 | rcu_str_deref(device->name), | |
3739 | blk_status_to_errno(bio->bi_status)); | |
3740 | ClearPageUptodate(page); | |
3741 | SetPageError(page); | |
3742 | btrfs_dev_stat_inc_and_print(device, | |
3743 | BTRFS_DEV_STAT_WRITE_ERRS); | |
3744 | } else { | |
3745 | SetPageUptodate(page); | |
3746 | } | |
3747 | ||
3748 | put_page(page); | |
3749 | unlock_page(page); | |
f2984462 | 3750 | } |
314b6dd0 JT |
3751 | |
3752 | bio_put(bio); | |
f2984462 CM |
3753 | } |
3754 | ||
8f32380d JT |
3755 | struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, |
3756 | int copy_num) | |
29c36d72 | 3757 | { |
29c36d72 | 3758 | struct btrfs_super_block *super; |
8f32380d | 3759 | struct page *page; |
12659251 | 3760 | u64 bytenr, bytenr_orig; |
8f32380d | 3761 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
12659251 NA |
3762 | int ret; |
3763 | ||
3764 | bytenr_orig = btrfs_sb_offset(copy_num); | |
3765 | ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); | |
3766 | if (ret == -ENOENT) | |
3767 | return ERR_PTR(-EINVAL); | |
3768 | else if (ret) | |
3769 | return ERR_PTR(ret); | |
29c36d72 | 3770 | |
29c36d72 | 3771 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) |
8f32380d | 3772 | return ERR_PTR(-EINVAL); |
29c36d72 | 3773 | |
8f32380d JT |
3774 | page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); |
3775 | if (IS_ERR(page)) | |
3776 | return ERR_CAST(page); | |
29c36d72 | 3777 | |
8f32380d | 3778 | super = page_address(page); |
96c2e067 AJ |
3779 | if (btrfs_super_magic(super) != BTRFS_MAGIC) { |
3780 | btrfs_release_disk_super(super); | |
3781 | return ERR_PTR(-ENODATA); | |
3782 | } | |
3783 | ||
12659251 | 3784 | if (btrfs_super_bytenr(super) != bytenr_orig) { |
8f32380d JT |
3785 | btrfs_release_disk_super(super); |
3786 | return ERR_PTR(-EINVAL); | |
29c36d72 AJ |
3787 | } |
3788 | ||
8f32380d | 3789 | return super; |
29c36d72 AJ |
3790 | } |
3791 | ||
3792 | ||
8f32380d | 3793 | struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) |
a512bbf8 | 3794 | { |
8f32380d | 3795 | struct btrfs_super_block *super, *latest = NULL; |
a512bbf8 YZ |
3796 | int i; |
3797 | u64 transid = 0; | |
a512bbf8 YZ |
3798 | |
3799 | /* we would like to check all the supers, but that would make | |
3800 | * a btrfs mount succeed after a mkfs from a different FS. | |
3801 | * So, we need to add a special mount option to scan for | |
3802 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead | |
3803 | */ | |
3804 | for (i = 0; i < 1; i++) { | |
8f32380d JT |
3805 | super = btrfs_read_dev_one_super(bdev, i); |
3806 | if (IS_ERR(super)) | |
a512bbf8 YZ |
3807 | continue; |
3808 | ||
a512bbf8 | 3809 | if (!latest || btrfs_super_generation(super) > transid) { |
8f32380d JT |
3810 | if (latest) |
3811 | btrfs_release_disk_super(super); | |
3812 | ||
3813 | latest = super; | |
a512bbf8 | 3814 | transid = btrfs_super_generation(super); |
a512bbf8 YZ |
3815 | } |
3816 | } | |
92fc03fb | 3817 | |
8f32380d | 3818 | return super; |
a512bbf8 YZ |
3819 | } |
3820 | ||
4eedeb75 | 3821 | /* |
abbb3b8e | 3822 | * Write superblock @sb to the @device. Do not wait for completion, all the |
314b6dd0 | 3823 | * pages we use for writing are locked. |
4eedeb75 | 3824 | * |
abbb3b8e DS |
3825 | * Write @max_mirrors copies of the superblock, where 0 means default that fit |
3826 | * the expected device size at commit time. Note that max_mirrors must be | |
3827 | * same for write and wait phases. | |
4eedeb75 | 3828 | * |
314b6dd0 | 3829 | * Return number of errors when page is not found or submission fails. |
4eedeb75 | 3830 | */ |
a512bbf8 | 3831 | static int write_dev_supers(struct btrfs_device *device, |
abbb3b8e | 3832 | struct btrfs_super_block *sb, int max_mirrors) |
a512bbf8 | 3833 | { |
d5178578 | 3834 | struct btrfs_fs_info *fs_info = device->fs_info; |
314b6dd0 | 3835 | struct address_space *mapping = device->bdev->bd_inode->i_mapping; |
d5178578 | 3836 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
a512bbf8 | 3837 | int i; |
a512bbf8 | 3838 | int errors = 0; |
12659251 NA |
3839 | int ret; |
3840 | u64 bytenr, bytenr_orig; | |
a512bbf8 YZ |
3841 | |
3842 | if (max_mirrors == 0) | |
3843 | max_mirrors = BTRFS_SUPER_MIRROR_MAX; | |
3844 | ||
d5178578 JT |
3845 | shash->tfm = fs_info->csum_shash; |
3846 | ||
a512bbf8 | 3847 | for (i = 0; i < max_mirrors; i++) { |
314b6dd0 JT |
3848 | struct page *page; |
3849 | struct bio *bio; | |
3850 | struct btrfs_super_block *disk_super; | |
3851 | ||
12659251 NA |
3852 | bytenr_orig = btrfs_sb_offset(i); |
3853 | ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); | |
3854 | if (ret == -ENOENT) { | |
3855 | continue; | |
3856 | } else if (ret < 0) { | |
3857 | btrfs_err(device->fs_info, | |
3858 | "couldn't get super block location for mirror %d", | |
3859 | i); | |
3860 | errors++; | |
3861 | continue; | |
3862 | } | |
935e5cc9 MX |
3863 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
3864 | device->commit_total_bytes) | |
a512bbf8 YZ |
3865 | break; |
3866 | ||
12659251 | 3867 | btrfs_set_super_bytenr(sb, bytenr_orig); |
4eedeb75 | 3868 | |
fd08001f EB |
3869 | crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, |
3870 | BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, | |
3871 | sb->csum); | |
4eedeb75 | 3872 | |
314b6dd0 JT |
3873 | page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, |
3874 | GFP_NOFS); | |
3875 | if (!page) { | |
abbb3b8e | 3876 | btrfs_err(device->fs_info, |
314b6dd0 | 3877 | "couldn't get super block page for bytenr %llu", |
abbb3b8e DS |
3878 | bytenr); |
3879 | errors++; | |
4eedeb75 | 3880 | continue; |
abbb3b8e | 3881 | } |
634554dc | 3882 | |
314b6dd0 JT |
3883 | /* Bump the refcount for wait_dev_supers() */ |
3884 | get_page(page); | |
a512bbf8 | 3885 | |
314b6dd0 JT |
3886 | disk_super = page_address(page); |
3887 | memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); | |
4eedeb75 | 3888 | |
314b6dd0 JT |
3889 | /* |
3890 | * Directly use bios here instead of relying on the page cache | |
3891 | * to do I/O, so we don't lose the ability to do integrity | |
3892 | * checking. | |
3893 | */ | |
3894 | bio = bio_alloc(GFP_NOFS, 1); | |
3895 | bio_set_dev(bio, device->bdev); | |
3896 | bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; | |
3897 | bio->bi_private = device; | |
3898 | bio->bi_end_io = btrfs_end_super_write; | |
3899 | __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, | |
3900 | offset_in_page(bytenr)); | |
a512bbf8 | 3901 | |
387125fc | 3902 | /* |
314b6dd0 JT |
3903 | * We FUA only the first super block. The others we allow to |
3904 | * go down lazy and there's a short window where the on-disk | |
3905 | * copies might still contain the older version. | |
387125fc | 3906 | */ |
314b6dd0 | 3907 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; |
1b9e619c | 3908 | if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) |
314b6dd0 JT |
3909 | bio->bi_opf |= REQ_FUA; |
3910 | ||
3911 | btrfsic_submit_bio(bio); | |
12659251 | 3912 | btrfs_advance_sb_log(device, i); |
a512bbf8 YZ |
3913 | } |
3914 | return errors < i ? 0 : -1; | |
3915 | } | |
3916 | ||
abbb3b8e DS |
3917 | /* |
3918 | * Wait for write completion of superblocks done by write_dev_supers, | |
3919 | * @max_mirrors same for write and wait phases. | |
3920 | * | |
314b6dd0 | 3921 | * Return number of errors when page is not found or not marked up to |
abbb3b8e DS |
3922 | * date. |
3923 | */ | |
3924 | static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) | |
3925 | { | |
abbb3b8e DS |
3926 | int i; |
3927 | int errors = 0; | |
b6a535fa | 3928 | bool primary_failed = false; |
12659251 | 3929 | int ret; |
abbb3b8e DS |
3930 | u64 bytenr; |
3931 | ||
3932 | if (max_mirrors == 0) | |
3933 | max_mirrors = BTRFS_SUPER_MIRROR_MAX; | |
3934 | ||
3935 | for (i = 0; i < max_mirrors; i++) { | |
314b6dd0 JT |
3936 | struct page *page; |
3937 | ||
12659251 NA |
3938 | ret = btrfs_sb_log_location(device, i, READ, &bytenr); |
3939 | if (ret == -ENOENT) { | |
3940 | break; | |
3941 | } else if (ret < 0) { | |
3942 | errors++; | |
3943 | if (i == 0) | |
3944 | primary_failed = true; | |
3945 | continue; | |
3946 | } | |
abbb3b8e DS |
3947 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
3948 | device->commit_total_bytes) | |
3949 | break; | |
3950 | ||
314b6dd0 JT |
3951 | page = find_get_page(device->bdev->bd_inode->i_mapping, |
3952 | bytenr >> PAGE_SHIFT); | |
3953 | if (!page) { | |
abbb3b8e | 3954 | errors++; |
b6a535fa HM |
3955 | if (i == 0) |
3956 | primary_failed = true; | |
abbb3b8e DS |
3957 | continue; |
3958 | } | |
314b6dd0 JT |
3959 | /* Page is submitted locked and unlocked once the IO completes */ |
3960 | wait_on_page_locked(page); | |
3961 | if (PageError(page)) { | |
abbb3b8e | 3962 | errors++; |
b6a535fa HM |
3963 | if (i == 0) |
3964 | primary_failed = true; | |
3965 | } | |
abbb3b8e | 3966 | |
314b6dd0 JT |
3967 | /* Drop our reference */ |
3968 | put_page(page); | |
abbb3b8e | 3969 | |
314b6dd0 JT |
3970 | /* Drop the reference from the writing run */ |
3971 | put_page(page); | |
abbb3b8e DS |
3972 | } |
3973 | ||
b6a535fa HM |
3974 | /* log error, force error return */ |
3975 | if (primary_failed) { | |
3976 | btrfs_err(device->fs_info, "error writing primary super block to device %llu", | |
3977 | device->devid); | |
3978 | return -1; | |
3979 | } | |
3980 | ||
abbb3b8e DS |
3981 | return errors < i ? 0 : -1; |
3982 | } | |
3983 | ||
387125fc CM |
3984 | /* |
3985 | * endio for the write_dev_flush, this will wake anyone waiting | |
3986 | * for the barrier when it is done | |
3987 | */ | |
4246a0b6 | 3988 | static void btrfs_end_empty_barrier(struct bio *bio) |
387125fc | 3989 | { |
e0ae9994 | 3990 | complete(bio->bi_private); |
387125fc CM |
3991 | } |
3992 | ||
3993 | /* | |
4fc6441a AJ |
3994 | * Submit a flush request to the device if it supports it. Error handling is |
3995 | * done in the waiting counterpart. | |
387125fc | 3996 | */ |
4fc6441a | 3997 | static void write_dev_flush(struct btrfs_device *device) |
387125fc | 3998 | { |
e0ae9994 | 3999 | struct bio *bio = device->flush_bio; |
387125fc | 4000 | |
e26dedd0 WY |
4001 | #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
4002 | /* | |
4003 | * When a disk has write caching disabled, we skip submission of a bio | |
4004 | * with flush and sync requests before writing the superblock, since | |
4005 | * it's not needed. However when the integrity checker is enabled, this | |
4006 | * results in reports that there are metadata blocks referred by a | |
4007 | * superblock that were not properly flushed. So don't skip the bio | |
4008 | * submission only when the integrity checker is enabled for the sake | |
4009 | * of simplicity, since this is a debug tool and not meant for use in | |
4010 | * non-debug builds. | |
4011 | */ | |
4012 | struct request_queue *q = bdev_get_queue(device->bdev); | |
c2a9c7ab | 4013 | if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) |
4fc6441a | 4014 | return; |
e26dedd0 | 4015 | #endif |
387125fc | 4016 | |
e0ae9994 | 4017 | bio_reset(bio); |
387125fc | 4018 | bio->bi_end_io = btrfs_end_empty_barrier; |
74d46992 | 4019 | bio_set_dev(bio, device->bdev); |
8d910125 | 4020 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; |
387125fc CM |
4021 | init_completion(&device->flush_wait); |
4022 | bio->bi_private = &device->flush_wait; | |
387125fc | 4023 | |
43a01111 | 4024 | btrfsic_submit_bio(bio); |
1c3063b6 | 4025 | set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); |
4fc6441a | 4026 | } |
387125fc | 4027 | |
4fc6441a AJ |
4028 | /* |
4029 | * If the flush bio has been submitted by write_dev_flush, wait for it. | |
4030 | */ | |
8c27cb35 | 4031 | static blk_status_t wait_dev_flush(struct btrfs_device *device) |
4fc6441a | 4032 | { |
4fc6441a | 4033 | struct bio *bio = device->flush_bio; |
387125fc | 4034 | |
1c3063b6 | 4035 | if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) |
58efbc9f | 4036 | return BLK_STS_OK; |
387125fc | 4037 | |
1c3063b6 | 4038 | clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); |
2980d574 | 4039 | wait_for_completion_io(&device->flush_wait); |
387125fc | 4040 | |
8c27cb35 | 4041 | return bio->bi_status; |
387125fc | 4042 | } |
387125fc | 4043 | |
d10b82fe | 4044 | static int check_barrier_error(struct btrfs_fs_info *fs_info) |
401b41e5 | 4045 | { |
6528b99d | 4046 | if (!btrfs_check_rw_degradable(fs_info, NULL)) |
401b41e5 | 4047 | return -EIO; |
387125fc CM |
4048 | return 0; |
4049 | } | |
4050 | ||
4051 | /* | |
4052 | * send an empty flush down to each device in parallel, | |
4053 | * then wait for them | |
4054 | */ | |
4055 | static int barrier_all_devices(struct btrfs_fs_info *info) | |
4056 | { | |
4057 | struct list_head *head; | |
4058 | struct btrfs_device *dev; | |
5af3e8cc | 4059 | int errors_wait = 0; |
4e4cbee9 | 4060 | blk_status_t ret; |
387125fc | 4061 | |
1538e6c5 | 4062 | lockdep_assert_held(&info->fs_devices->device_list_mutex); |
387125fc CM |
4063 | /* send down all the barriers */ |
4064 | head = &info->fs_devices->devices; | |
1538e6c5 | 4065 | list_for_each_entry(dev, head, dev_list) { |
e6e674bd | 4066 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) |
f88ba6a2 | 4067 | continue; |
cea7c8bf | 4068 | if (!dev->bdev) |
387125fc | 4069 | continue; |
e12c9621 | 4070 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
ebbede42 | 4071 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) |
387125fc CM |
4072 | continue; |
4073 | ||
4fc6441a | 4074 | write_dev_flush(dev); |
58efbc9f | 4075 | dev->last_flush_error = BLK_STS_OK; |
387125fc CM |
4076 | } |
4077 | ||
4078 | /* wait for all the barriers */ | |
1538e6c5 | 4079 | list_for_each_entry(dev, head, dev_list) { |
e6e674bd | 4080 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) |
f88ba6a2 | 4081 | continue; |
387125fc | 4082 | if (!dev->bdev) { |
5af3e8cc | 4083 | errors_wait++; |
387125fc CM |
4084 | continue; |
4085 | } | |
e12c9621 | 4086 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
ebbede42 | 4087 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) |
387125fc CM |
4088 | continue; |
4089 | ||
4fc6441a | 4090 | ret = wait_dev_flush(dev); |
401b41e5 AJ |
4091 | if (ret) { |
4092 | dev->last_flush_error = ret; | |
66b4993e DS |
4093 | btrfs_dev_stat_inc_and_print(dev, |
4094 | BTRFS_DEV_STAT_FLUSH_ERRS); | |
5af3e8cc | 4095 | errors_wait++; |
401b41e5 AJ |
4096 | } |
4097 | } | |
4098 | ||
cea7c8bf | 4099 | if (errors_wait) { |
401b41e5 AJ |
4100 | /* |
4101 | * At some point we need the status of all disks | |
4102 | * to arrive at the volume status. So error checking | |
4103 | * is being pushed to a separate loop. | |
4104 | */ | |
d10b82fe | 4105 | return check_barrier_error(info); |
387125fc | 4106 | } |
387125fc CM |
4107 | return 0; |
4108 | } | |
4109 | ||
943c6e99 ZL |
4110 | int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) |
4111 | { | |
8789f4fe ZL |
4112 | int raid_type; |
4113 | int min_tolerated = INT_MAX; | |
943c6e99 | 4114 | |
8789f4fe ZL |
4115 | if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || |
4116 | (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) | |
8c3e3582 | 4117 | min_tolerated = min_t(int, min_tolerated, |
8789f4fe ZL |
4118 | btrfs_raid_array[BTRFS_RAID_SINGLE]. |
4119 | tolerated_failures); | |
943c6e99 | 4120 | |
8789f4fe ZL |
4121 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
4122 | if (raid_type == BTRFS_RAID_SINGLE) | |
4123 | continue; | |
41a6e891 | 4124 | if (!(flags & btrfs_raid_array[raid_type].bg_flag)) |
8789f4fe | 4125 | continue; |
8c3e3582 | 4126 | min_tolerated = min_t(int, min_tolerated, |
8789f4fe ZL |
4127 | btrfs_raid_array[raid_type]. |
4128 | tolerated_failures); | |
4129 | } | |
943c6e99 | 4130 | |
8789f4fe | 4131 | if (min_tolerated == INT_MAX) { |
ab8d0fc4 | 4132 | pr_warn("BTRFS: unknown raid flag: %llu", flags); |
8789f4fe ZL |
4133 | min_tolerated = 0; |
4134 | } | |
4135 | ||
4136 | return min_tolerated; | |
943c6e99 ZL |
4137 | } |
4138 | ||
eece6a9c | 4139 | int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) |
f2984462 | 4140 | { |
e5e9a520 | 4141 | struct list_head *head; |
f2984462 | 4142 | struct btrfs_device *dev; |
a061fc8d | 4143 | struct btrfs_super_block *sb; |
f2984462 | 4144 | struct btrfs_dev_item *dev_item; |
f2984462 CM |
4145 | int ret; |
4146 | int do_barriers; | |
a236aed1 CM |
4147 | int max_errors; |
4148 | int total_errors = 0; | |
a061fc8d | 4149 | u64 flags; |
f2984462 | 4150 | |
0b246afa | 4151 | do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); |
fed3b381 LB |
4152 | |
4153 | /* | |
4154 | * max_mirrors == 0 indicates we're from commit_transaction, | |
4155 | * not from fsync where the tree roots in fs_info have not | |
4156 | * been consistent on disk. | |
4157 | */ | |
4158 | if (max_mirrors == 0) | |
4159 | backup_super_roots(fs_info); | |
f2984462 | 4160 | |
0b246afa | 4161 | sb = fs_info->super_for_commit; |
a061fc8d | 4162 | dev_item = &sb->dev_item; |
e5e9a520 | 4163 | |
0b246afa JM |
4164 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
4165 | head = &fs_info->fs_devices->devices; | |
4166 | max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; | |
387125fc | 4167 | |
5af3e8cc | 4168 | if (do_barriers) { |
0b246afa | 4169 | ret = barrier_all_devices(fs_info); |
5af3e8cc SB |
4170 | if (ret) { |
4171 | mutex_unlock( | |
0b246afa JM |
4172 | &fs_info->fs_devices->device_list_mutex); |
4173 | btrfs_handle_fs_error(fs_info, ret, | |
4174 | "errors while submitting device barriers."); | |
5af3e8cc SB |
4175 | return ret; |
4176 | } | |
4177 | } | |
387125fc | 4178 | |
1538e6c5 | 4179 | list_for_each_entry(dev, head, dev_list) { |
dfe25020 CM |
4180 | if (!dev->bdev) { |
4181 | total_errors++; | |
4182 | continue; | |
4183 | } | |
e12c9621 | 4184 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
ebbede42 | 4185 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) |
dfe25020 CM |
4186 | continue; |
4187 | ||
2b82032c | 4188 | btrfs_set_stack_device_generation(dev_item, 0); |
a061fc8d CM |
4189 | btrfs_set_stack_device_type(dev_item, dev->type); |
4190 | btrfs_set_stack_device_id(dev_item, dev->devid); | |
7df69d3e | 4191 | btrfs_set_stack_device_total_bytes(dev_item, |
935e5cc9 | 4192 | dev->commit_total_bytes); |
ce7213c7 MX |
4193 | btrfs_set_stack_device_bytes_used(dev_item, |
4194 | dev->commit_bytes_used); | |
a061fc8d CM |
4195 | btrfs_set_stack_device_io_align(dev_item, dev->io_align); |
4196 | btrfs_set_stack_device_io_width(dev_item, dev->io_width); | |
4197 | btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); | |
4198 | memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); | |
7239ff4b NB |
4199 | memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, |
4200 | BTRFS_FSID_SIZE); | |
a512bbf8 | 4201 | |
a061fc8d CM |
4202 | flags = btrfs_super_flags(sb); |
4203 | btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); | |
4204 | ||
75cb857d QW |
4205 | ret = btrfs_validate_write_super(fs_info, sb); |
4206 | if (ret < 0) { | |
4207 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
4208 | btrfs_handle_fs_error(fs_info, -EUCLEAN, | |
4209 | "unexpected superblock corruption detected"); | |
4210 | return -EUCLEAN; | |
4211 | } | |
4212 | ||
abbb3b8e | 4213 | ret = write_dev_supers(dev, sb, max_mirrors); |
a236aed1 CM |
4214 | if (ret) |
4215 | total_errors++; | |
f2984462 | 4216 | } |
a236aed1 | 4217 | if (total_errors > max_errors) { |
0b246afa JM |
4218 | btrfs_err(fs_info, "%d errors while writing supers", |
4219 | total_errors); | |
4220 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
79787eaa | 4221 | |
9d565ba4 | 4222 | /* FUA is masked off if unsupported and can't be the reason */ |
0b246afa JM |
4223 | btrfs_handle_fs_error(fs_info, -EIO, |
4224 | "%d errors while writing supers", | |
4225 | total_errors); | |
9d565ba4 | 4226 | return -EIO; |
a236aed1 | 4227 | } |
f2984462 | 4228 | |
a512bbf8 | 4229 | total_errors = 0; |
1538e6c5 | 4230 | list_for_each_entry(dev, head, dev_list) { |
dfe25020 CM |
4231 | if (!dev->bdev) |
4232 | continue; | |
e12c9621 | 4233 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
ebbede42 | 4234 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) |
dfe25020 CM |
4235 | continue; |
4236 | ||
abbb3b8e | 4237 | ret = wait_dev_supers(dev, max_mirrors); |
a512bbf8 YZ |
4238 | if (ret) |
4239 | total_errors++; | |
f2984462 | 4240 | } |
0b246afa | 4241 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a236aed1 | 4242 | if (total_errors > max_errors) { |
0b246afa JM |
4243 | btrfs_handle_fs_error(fs_info, -EIO, |
4244 | "%d errors while writing supers", | |
4245 | total_errors); | |
79787eaa | 4246 | return -EIO; |
a236aed1 | 4247 | } |
f2984462 CM |
4248 | return 0; |
4249 | } | |
4250 | ||
cb517eab MX |
4251 | /* Drop a fs root from the radix tree and free it. */ |
4252 | void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, | |
4253 | struct btrfs_root *root) | |
2619ba1f | 4254 | { |
4785e24f JB |
4255 | bool drop_ref = false; |
4256 | ||
4df27c4d | 4257 | spin_lock(&fs_info->fs_roots_radix_lock); |
2619ba1f CM |
4258 | radix_tree_delete(&fs_info->fs_roots_radix, |
4259 | (unsigned long)root->root_key.objectid); | |
af01d2e5 | 4260 | if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) |
4785e24f | 4261 | drop_ref = true; |
4df27c4d | 4262 | spin_unlock(&fs_info->fs_roots_radix_lock); |
76dda93c | 4263 | |
1c1ea4f7 | 4264 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { |
ef67963d | 4265 | ASSERT(root->log_root == NULL); |
1c1ea4f7 | 4266 | if (root->reloc_root) { |
00246528 | 4267 | btrfs_put_root(root->reloc_root); |
1c1ea4f7 LB |
4268 | root->reloc_root = NULL; |
4269 | } | |
4270 | } | |
3321719e | 4271 | |
4785e24f JB |
4272 | if (drop_ref) |
4273 | btrfs_put_root(root); | |
2619ba1f CM |
4274 | } |
4275 | ||
c146afad | 4276 | int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) |
cfaa7295 | 4277 | { |
c146afad YZ |
4278 | u64 root_objectid = 0; |
4279 | struct btrfs_root *gang[8]; | |
65d33fd7 QW |
4280 | int i = 0; |
4281 | int err = 0; | |
4282 | unsigned int ret = 0; | |
e089f05c | 4283 | |
c146afad | 4284 | while (1) { |
efc34534 | 4285 | spin_lock(&fs_info->fs_roots_radix_lock); |
c146afad YZ |
4286 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
4287 | (void **)gang, root_objectid, | |
4288 | ARRAY_SIZE(gang)); | |
65d33fd7 | 4289 | if (!ret) { |
efc34534 | 4290 | spin_unlock(&fs_info->fs_roots_radix_lock); |
c146afad | 4291 | break; |
65d33fd7 | 4292 | } |
5d4f98a2 | 4293 | root_objectid = gang[ret - 1]->root_key.objectid + 1; |
65d33fd7 | 4294 | |
c146afad | 4295 | for (i = 0; i < ret; i++) { |
65d33fd7 QW |
4296 | /* Avoid to grab roots in dead_roots */ |
4297 | if (btrfs_root_refs(&gang[i]->root_item) == 0) { | |
4298 | gang[i] = NULL; | |
4299 | continue; | |
4300 | } | |
4301 | /* grab all the search result for later use */ | |
00246528 | 4302 | gang[i] = btrfs_grab_root(gang[i]); |
65d33fd7 | 4303 | } |
efc34534 | 4304 | spin_unlock(&fs_info->fs_roots_radix_lock); |
66b4ffd1 | 4305 | |
65d33fd7 QW |
4306 | for (i = 0; i < ret; i++) { |
4307 | if (!gang[i]) | |
4308 | continue; | |
c146afad | 4309 | root_objectid = gang[i]->root_key.objectid; |
66b4ffd1 JB |
4310 | err = btrfs_orphan_cleanup(gang[i]); |
4311 | if (err) | |
65d33fd7 | 4312 | break; |
00246528 | 4313 | btrfs_put_root(gang[i]); |
c146afad YZ |
4314 | } |
4315 | root_objectid++; | |
4316 | } | |
65d33fd7 QW |
4317 | |
4318 | /* release the uncleaned roots due to error */ | |
4319 | for (; i < ret; i++) { | |
4320 | if (gang[i]) | |
00246528 | 4321 | btrfs_put_root(gang[i]); |
65d33fd7 QW |
4322 | } |
4323 | return err; | |
c146afad | 4324 | } |
a2135011 | 4325 | |
6bccf3ab | 4326 | int btrfs_commit_super(struct btrfs_fs_info *fs_info) |
c146afad | 4327 | { |
6bccf3ab | 4328 | struct btrfs_root *root = fs_info->tree_root; |
c146afad | 4329 | struct btrfs_trans_handle *trans; |
a74a4b97 | 4330 | |
0b246afa | 4331 | mutex_lock(&fs_info->cleaner_mutex); |
2ff7e61e | 4332 | btrfs_run_delayed_iputs(fs_info); |
0b246afa JM |
4333 | mutex_unlock(&fs_info->cleaner_mutex); |
4334 | wake_up_process(fs_info->cleaner_kthread); | |
c71bf099 YZ |
4335 | |
4336 | /* wait until ongoing cleanup work done */ | |
0b246afa JM |
4337 | down_write(&fs_info->cleanup_work_sem); |
4338 | up_write(&fs_info->cleanup_work_sem); | |
c71bf099 | 4339 | |
7a7eaa40 | 4340 | trans = btrfs_join_transaction(root); |
3612b495 TI |
4341 | if (IS_ERR(trans)) |
4342 | return PTR_ERR(trans); | |
3a45bb20 | 4343 | return btrfs_commit_transaction(trans); |
c146afad YZ |
4344 | } |
4345 | ||
b105e927 | 4346 | void __cold close_ctree(struct btrfs_fs_info *fs_info) |
c146afad | 4347 | { |
c146afad YZ |
4348 | int ret; |
4349 | ||
afcdd129 | 4350 | set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); |
d6fd0ae2 OS |
4351 | /* |
4352 | * We don't want the cleaner to start new transactions, add more delayed | |
4353 | * iputs, etc. while we're closing. We can't use kthread_stop() yet | |
4354 | * because that frees the task_struct, and the transaction kthread might | |
4355 | * still try to wake up the cleaner. | |
4356 | */ | |
4357 | kthread_park(fs_info->cleaner_kthread); | |
c146afad | 4358 | |
78db1921 JB |
4359 | /* |
4360 | * If we had UNFINISHED_DROPS we could still be processing them, so | |
4361 | * clear that bit and wake up relocation so it can stop. | |
4362 | */ | |
4363 | btrfs_wake_unfinished_drop(fs_info); | |
4364 | ||
7343dd61 | 4365 | /* wait for the qgroup rescan worker to stop */ |
d06f23d6 | 4366 | btrfs_qgroup_wait_for_completion(fs_info, false); |
7343dd61 | 4367 | |
803b2f54 SB |
4368 | /* wait for the uuid_scan task to finish */ |
4369 | down(&fs_info->uuid_tree_rescan_sem); | |
4370 | /* avoid complains from lockdep et al., set sem back to initial state */ | |
4371 | up(&fs_info->uuid_tree_rescan_sem); | |
4372 | ||
837d5b6e | 4373 | /* pause restriper - we want to resume on mount */ |
aa1b8cd4 | 4374 | btrfs_pause_balance(fs_info); |
837d5b6e | 4375 | |
8dabb742 SB |
4376 | btrfs_dev_replace_suspend_for_unmount(fs_info); |
4377 | ||
aa1b8cd4 | 4378 | btrfs_scrub_cancel(fs_info); |
4cb5300b CM |
4379 | |
4380 | /* wait for any defraggers to finish */ | |
4381 | wait_event(fs_info->transaction_wait, | |
4382 | (atomic_read(&fs_info->defrag_running) == 0)); | |
4383 | ||
4384 | /* clear out the rbtree of defraggable inodes */ | |
26176e7c | 4385 | btrfs_cleanup_defrag_inodes(fs_info); |
4cb5300b | 4386 | |
21c7e756 | 4387 | cancel_work_sync(&fs_info->async_reclaim_work); |
57056740 | 4388 | cancel_work_sync(&fs_info->async_data_reclaim_work); |
576fa348 | 4389 | cancel_work_sync(&fs_info->preempt_reclaim_work); |
21c7e756 | 4390 | |
18bb8bbf JT |
4391 | cancel_work_sync(&fs_info->reclaim_bgs_work); |
4392 | ||
b0643e59 DZ |
4393 | /* Cancel or finish ongoing discard work */ |
4394 | btrfs_discard_cleanup(fs_info); | |
4395 | ||
bc98a42c | 4396 | if (!sb_rdonly(fs_info->sb)) { |
e44163e1 | 4397 | /* |
d6fd0ae2 OS |
4398 | * The cleaner kthread is stopped, so do one final pass over |
4399 | * unused block groups. | |
e44163e1 | 4400 | */ |
0b246afa | 4401 | btrfs_delete_unused_bgs(fs_info); |
e44163e1 | 4402 | |
f0cc2cd7 FM |
4403 | /* |
4404 | * There might be existing delayed inode workers still running | |
4405 | * and holding an empty delayed inode item. We must wait for | |
4406 | * them to complete first because they can create a transaction. | |
4407 | * This happens when someone calls btrfs_balance_delayed_items() | |
4408 | * and then a transaction commit runs the same delayed nodes | |
4409 | * before any delayed worker has done something with the nodes. | |
4410 | * We must wait for any worker here and not at transaction | |
4411 | * commit time since that could cause a deadlock. | |
4412 | * This is a very rare case. | |
4413 | */ | |
4414 | btrfs_flush_workqueue(fs_info->delayed_workers); | |
4415 | ||
6bccf3ab | 4416 | ret = btrfs_commit_super(fs_info); |
acce952b | 4417 | if (ret) |
04892340 | 4418 | btrfs_err(fs_info, "commit super ret %d", ret); |
acce952b | 4419 | } |
4420 | ||
af722733 LB |
4421 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || |
4422 | test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) | |
2ff7e61e | 4423 | btrfs_error_commit_super(fs_info); |
0f7d52f4 | 4424 | |
e3029d9f AV |
4425 | kthread_stop(fs_info->transaction_kthread); |
4426 | kthread_stop(fs_info->cleaner_kthread); | |
8929ecfa | 4427 | |
e187831e | 4428 | ASSERT(list_empty(&fs_info->delayed_iputs)); |
afcdd129 | 4429 | set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); |
f25784b3 | 4430 | |
5958253c QW |
4431 | if (btrfs_check_quota_leak(fs_info)) { |
4432 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
4433 | btrfs_err(fs_info, "qgroup reserved space leaked"); | |
4434 | } | |
4435 | ||
04892340 | 4436 | btrfs_free_qgroup_config(fs_info); |
fe816d0f | 4437 | ASSERT(list_empty(&fs_info->delalloc_roots)); |
bcef60f2 | 4438 | |
963d678b | 4439 | if (percpu_counter_sum(&fs_info->delalloc_bytes)) { |
04892340 | 4440 | btrfs_info(fs_info, "at unmount delalloc count %lld", |
963d678b | 4441 | percpu_counter_sum(&fs_info->delalloc_bytes)); |
b0c68f8b | 4442 | } |
bcc63abb | 4443 | |
5deb17e1 | 4444 | if (percpu_counter_sum(&fs_info->ordered_bytes)) |
4297ff84 | 4445 | btrfs_info(fs_info, "at unmount dio bytes count %lld", |
5deb17e1 | 4446 | percpu_counter_sum(&fs_info->ordered_bytes)); |
4297ff84 | 4447 | |
6618a59b | 4448 | btrfs_sysfs_remove_mounted(fs_info); |
b7c35e81 | 4449 | btrfs_sysfs_remove_fsid(fs_info->fs_devices); |
5ac1d209 | 4450 | |
1a4319cc LB |
4451 | btrfs_put_block_group_cache(fs_info); |
4452 | ||
de348ee0 WS |
4453 | /* |
4454 | * we must make sure there is not any read request to | |
4455 | * submit after we stopping all workers. | |
4456 | */ | |
4457 | invalidate_inode_pages2(fs_info->btree_inode->i_mapping); | |
96192499 JB |
4458 | btrfs_stop_all_workers(fs_info); |
4459 | ||
0a31daa4 FM |
4460 | /* We shouldn't have any transaction open at this point */ |
4461 | ASSERT(list_empty(&fs_info->trans_list)); | |
4462 | ||
afcdd129 | 4463 | clear_bit(BTRFS_FS_OPEN, &fs_info->flags); |
4273eaff | 4464 | free_root_pointers(fs_info, true); |
8c38938c | 4465 | btrfs_free_fs_roots(fs_info); |
9ad6b7bc | 4466 | |
4e19443d JB |
4467 | /* |
4468 | * We must free the block groups after dropping the fs_roots as we could | |
4469 | * have had an IO error and have left over tree log blocks that aren't | |
4470 | * cleaned up until the fs roots are freed. This makes the block group | |
4471 | * accounting appear to be wrong because there's pending reserved bytes, | |
4472 | * so make sure we do the block group cleanup afterwards. | |
4473 | */ | |
4474 | btrfs_free_block_groups(fs_info); | |
4475 | ||
13e6c37b | 4476 | iput(fs_info->btree_inode); |
d6bfde87 | 4477 | |
21adbd5c | 4478 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
0b246afa | 4479 | if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) |
2ff7e61e | 4480 | btrfsic_unmount(fs_info->fs_devices); |
21adbd5c SB |
4481 | #endif |
4482 | ||
0b86a832 | 4483 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
68c94e55 | 4484 | btrfs_close_devices(fs_info->fs_devices); |
eb60ceac CM |
4485 | } |
4486 | ||
b9fab919 CM |
4487 | int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, |
4488 | int atomic) | |
5f39d397 | 4489 | { |
1259ab75 | 4490 | int ret; |
727011e0 | 4491 | struct inode *btree_inode = buf->pages[0]->mapping->host; |
1259ab75 | 4492 | |
0b32f4bb | 4493 | ret = extent_buffer_uptodate(buf); |
1259ab75 CM |
4494 | if (!ret) |
4495 | return ret; | |
4496 | ||
4497 | ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, | |
b9fab919 CM |
4498 | parent_transid, atomic); |
4499 | if (ret == -EAGAIN) | |
4500 | return ret; | |
1259ab75 | 4501 | return !ret; |
5f39d397 CM |
4502 | } |
4503 | ||
5f39d397 CM |
4504 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
4505 | { | |
2f4d60df | 4506 | struct btrfs_fs_info *fs_info = buf->fs_info; |
5f39d397 | 4507 | u64 transid = btrfs_header_generation(buf); |
b9473439 | 4508 | int was_dirty; |
b4ce94de | 4509 | |
06ea65a3 JB |
4510 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
4511 | /* | |
4512 | * This is a fast path so only do this check if we have sanity tests | |
52042d8e | 4513 | * enabled. Normal people shouldn't be using unmapped buffers as dirty |
06ea65a3 JB |
4514 | * outside of the sanity tests. |
4515 | */ | |
b0132a3b | 4516 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) |
06ea65a3 JB |
4517 | return; |
4518 | #endif | |
b9447ef8 | 4519 | btrfs_assert_tree_locked(buf); |
0b246afa | 4520 | if (transid != fs_info->generation) |
5d163e0e | 4521 | WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", |
0b246afa | 4522 | buf->start, transid, fs_info->generation); |
0b32f4bb | 4523 | was_dirty = set_extent_buffer_dirty(buf); |
e2d84521 | 4524 | if (!was_dirty) |
104b4e51 NB |
4525 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
4526 | buf->len, | |
4527 | fs_info->dirty_metadata_batch); | |
1f21ef0a | 4528 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
69fc6cbb QW |
4529 | /* |
4530 | * Since btrfs_mark_buffer_dirty() can be called with item pointer set | |
4531 | * but item data not updated. | |
4532 | * So here we should only check item pointers, not item data. | |
4533 | */ | |
4534 | if (btrfs_header_level(buf) == 0 && | |
cfdaad5e | 4535 | btrfs_check_leaf_relaxed(buf)) { |
a4f78750 | 4536 | btrfs_print_leaf(buf); |
1f21ef0a FM |
4537 | ASSERT(0); |
4538 | } | |
4539 | #endif | |
eb60ceac CM |
4540 | } |
4541 | ||
2ff7e61e | 4542 | static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, |
b53d3f5d | 4543 | int flush_delayed) |
16cdcec7 MX |
4544 | { |
4545 | /* | |
4546 | * looks as though older kernels can get into trouble with | |
4547 | * this code, they end up stuck in balance_dirty_pages forever | |
4548 | */ | |
e2d84521 | 4549 | int ret; |
16cdcec7 MX |
4550 | |
4551 | if (current->flags & PF_MEMALLOC) | |
4552 | return; | |
4553 | ||
b53d3f5d | 4554 | if (flush_delayed) |
2ff7e61e | 4555 | btrfs_balance_delayed_items(fs_info); |
16cdcec7 | 4556 | |
d814a491 EL |
4557 | ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, |
4558 | BTRFS_DIRTY_METADATA_THRESH, | |
4559 | fs_info->dirty_metadata_batch); | |
e2d84521 | 4560 | if (ret > 0) { |
0b246afa | 4561 | balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); |
16cdcec7 | 4562 | } |
16cdcec7 MX |
4563 | } |
4564 | ||
2ff7e61e | 4565 | void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) |
35b7e476 | 4566 | { |
2ff7e61e | 4567 | __btrfs_btree_balance_dirty(fs_info, 1); |
b53d3f5d | 4568 | } |
585ad2c3 | 4569 | |
2ff7e61e | 4570 | void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) |
b53d3f5d | 4571 | { |
2ff7e61e | 4572 | __btrfs_btree_balance_dirty(fs_info, 0); |
35b7e476 | 4573 | } |
6b80053d | 4574 | |
581c1760 QW |
4575 | int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, |
4576 | struct btrfs_key *first_key) | |
6b80053d | 4577 | { |
5ab12d1f | 4578 | return btree_read_extent_buffer_pages(buf, parent_transid, |
581c1760 | 4579 | level, first_key); |
6b80053d | 4580 | } |
0da5468f | 4581 | |
2ff7e61e | 4582 | static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) |
acce952b | 4583 | { |
fe816d0f NB |
4584 | /* cleanup FS via transaction */ |
4585 | btrfs_cleanup_transaction(fs_info); | |
4586 | ||
0b246afa | 4587 | mutex_lock(&fs_info->cleaner_mutex); |
2ff7e61e | 4588 | btrfs_run_delayed_iputs(fs_info); |
0b246afa | 4589 | mutex_unlock(&fs_info->cleaner_mutex); |
acce952b | 4590 | |
0b246afa JM |
4591 | down_write(&fs_info->cleanup_work_sem); |
4592 | up_write(&fs_info->cleanup_work_sem); | |
acce952b | 4593 | } |
4594 | ||
ef67963d JB |
4595 | static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) |
4596 | { | |
4597 | struct btrfs_root *gang[8]; | |
4598 | u64 root_objectid = 0; | |
4599 | int ret; | |
4600 | ||
4601 | spin_lock(&fs_info->fs_roots_radix_lock); | |
4602 | while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
4603 | (void **)gang, root_objectid, | |
4604 | ARRAY_SIZE(gang))) != 0) { | |
4605 | int i; | |
4606 | ||
4607 | for (i = 0; i < ret; i++) | |
4608 | gang[i] = btrfs_grab_root(gang[i]); | |
4609 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
4610 | ||
4611 | for (i = 0; i < ret; i++) { | |
4612 | if (!gang[i]) | |
4613 | continue; | |
4614 | root_objectid = gang[i]->root_key.objectid; | |
4615 | btrfs_free_log(NULL, gang[i]); | |
4616 | btrfs_put_root(gang[i]); | |
4617 | } | |
4618 | root_objectid++; | |
4619 | spin_lock(&fs_info->fs_roots_radix_lock); | |
4620 | } | |
4621 | spin_unlock(&fs_info->fs_roots_radix_lock); | |
4622 | btrfs_free_log_root_tree(NULL, fs_info); | |
4623 | } | |
4624 | ||
143bede5 | 4625 | static void btrfs_destroy_ordered_extents(struct btrfs_root *root) |
acce952b | 4626 | { |
acce952b | 4627 | struct btrfs_ordered_extent *ordered; |
acce952b | 4628 | |
199c2a9c | 4629 | spin_lock(&root->ordered_extent_lock); |
779880ef JB |
4630 | /* |
4631 | * This will just short circuit the ordered completion stuff which will | |
4632 | * make sure the ordered extent gets properly cleaned up. | |
4633 | */ | |
199c2a9c | 4634 | list_for_each_entry(ordered, &root->ordered_extents, |
779880ef JB |
4635 | root_extent_list) |
4636 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); | |
199c2a9c MX |
4637 | spin_unlock(&root->ordered_extent_lock); |
4638 | } | |
4639 | ||
4640 | static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) | |
4641 | { | |
4642 | struct btrfs_root *root; | |
4643 | struct list_head splice; | |
4644 | ||
4645 | INIT_LIST_HEAD(&splice); | |
4646 | ||
4647 | spin_lock(&fs_info->ordered_root_lock); | |
4648 | list_splice_init(&fs_info->ordered_roots, &splice); | |
4649 | while (!list_empty(&splice)) { | |
4650 | root = list_first_entry(&splice, struct btrfs_root, | |
4651 | ordered_root); | |
1de2cfde JB |
4652 | list_move_tail(&root->ordered_root, |
4653 | &fs_info->ordered_roots); | |
199c2a9c | 4654 | |
2a85d9ca | 4655 | spin_unlock(&fs_info->ordered_root_lock); |
199c2a9c MX |
4656 | btrfs_destroy_ordered_extents(root); |
4657 | ||
2a85d9ca LB |
4658 | cond_resched(); |
4659 | spin_lock(&fs_info->ordered_root_lock); | |
199c2a9c MX |
4660 | } |
4661 | spin_unlock(&fs_info->ordered_root_lock); | |
74d5d229 JB |
4662 | |
4663 | /* | |
4664 | * We need this here because if we've been flipped read-only we won't | |
4665 | * get sync() from the umount, so we need to make sure any ordered | |
4666 | * extents that haven't had their dirty pages IO start writeout yet | |
4667 | * actually get run and error out properly. | |
4668 | */ | |
4669 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); | |
acce952b | 4670 | } |
4671 | ||
35a3621b | 4672 | static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, |
2ff7e61e | 4673 | struct btrfs_fs_info *fs_info) |
acce952b | 4674 | { |
4675 | struct rb_node *node; | |
4676 | struct btrfs_delayed_ref_root *delayed_refs; | |
4677 | struct btrfs_delayed_ref_node *ref; | |
4678 | int ret = 0; | |
4679 | ||
4680 | delayed_refs = &trans->delayed_refs; | |
4681 | ||
4682 | spin_lock(&delayed_refs->lock); | |
d7df2c79 | 4683 | if (atomic_read(&delayed_refs->num_entries) == 0) { |
cfece4db | 4684 | spin_unlock(&delayed_refs->lock); |
b79ce3dd | 4685 | btrfs_debug(fs_info, "delayed_refs has NO entry"); |
acce952b | 4686 | return ret; |
4687 | } | |
4688 | ||
5c9d028b | 4689 | while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { |
d7df2c79 | 4690 | struct btrfs_delayed_ref_head *head; |
0e0adbcf | 4691 | struct rb_node *n; |
e78417d1 | 4692 | bool pin_bytes = false; |
acce952b | 4693 | |
d7df2c79 JB |
4694 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
4695 | href_node); | |
3069bd26 | 4696 | if (btrfs_delayed_ref_lock(delayed_refs, head)) |
d7df2c79 | 4697 | continue; |
3069bd26 | 4698 | |
d7df2c79 | 4699 | spin_lock(&head->lock); |
e3d03965 | 4700 | while ((n = rb_first_cached(&head->ref_tree)) != NULL) { |
0e0adbcf JB |
4701 | ref = rb_entry(n, struct btrfs_delayed_ref_node, |
4702 | ref_node); | |
d7df2c79 | 4703 | ref->in_tree = 0; |
e3d03965 | 4704 | rb_erase_cached(&ref->ref_node, &head->ref_tree); |
0e0adbcf | 4705 | RB_CLEAR_NODE(&ref->ref_node); |
1d57ee94 WX |
4706 | if (!list_empty(&ref->add_list)) |
4707 | list_del(&ref->add_list); | |
d7df2c79 JB |
4708 | atomic_dec(&delayed_refs->num_entries); |
4709 | btrfs_put_delayed_ref(ref); | |
e78417d1 | 4710 | } |
d7df2c79 JB |
4711 | if (head->must_insert_reserved) |
4712 | pin_bytes = true; | |
4713 | btrfs_free_delayed_extent_op(head->extent_op); | |
fa781cea | 4714 | btrfs_delete_ref_head(delayed_refs, head); |
d7df2c79 JB |
4715 | spin_unlock(&head->lock); |
4716 | spin_unlock(&delayed_refs->lock); | |
4717 | mutex_unlock(&head->mutex); | |
acce952b | 4718 | |
f603bb94 NB |
4719 | if (pin_bytes) { |
4720 | struct btrfs_block_group *cache; | |
4721 | ||
4722 | cache = btrfs_lookup_block_group(fs_info, head->bytenr); | |
4723 | BUG_ON(!cache); | |
4724 | ||
4725 | spin_lock(&cache->space_info->lock); | |
4726 | spin_lock(&cache->lock); | |
4727 | cache->pinned += head->num_bytes; | |
4728 | btrfs_space_info_update_bytes_pinned(fs_info, | |
4729 | cache->space_info, head->num_bytes); | |
4730 | cache->reserved -= head->num_bytes; | |
4731 | cache->space_info->bytes_reserved -= head->num_bytes; | |
4732 | spin_unlock(&cache->lock); | |
4733 | spin_unlock(&cache->space_info->lock); | |
f603bb94 NB |
4734 | |
4735 | btrfs_put_block_group(cache); | |
4736 | ||
4737 | btrfs_error_unpin_extent_range(fs_info, head->bytenr, | |
4738 | head->bytenr + head->num_bytes - 1); | |
4739 | } | |
31890da0 | 4740 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
d278850e | 4741 | btrfs_put_delayed_ref_head(head); |
acce952b | 4742 | cond_resched(); |
4743 | spin_lock(&delayed_refs->lock); | |
4744 | } | |
81f7eb00 | 4745 | btrfs_qgroup_destroy_extent_records(trans); |
acce952b | 4746 | |
4747 | spin_unlock(&delayed_refs->lock); | |
4748 | ||
4749 | return ret; | |
4750 | } | |
4751 | ||
143bede5 | 4752 | static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) |
acce952b | 4753 | { |
4754 | struct btrfs_inode *btrfs_inode; | |
4755 | struct list_head splice; | |
4756 | ||
4757 | INIT_LIST_HEAD(&splice); | |
4758 | ||
eb73c1b7 MX |
4759 | spin_lock(&root->delalloc_lock); |
4760 | list_splice_init(&root->delalloc_inodes, &splice); | |
acce952b | 4761 | |
4762 | while (!list_empty(&splice)) { | |
fe816d0f | 4763 | struct inode *inode = NULL; |
eb73c1b7 MX |
4764 | btrfs_inode = list_first_entry(&splice, struct btrfs_inode, |
4765 | delalloc_inodes); | |
fe816d0f | 4766 | __btrfs_del_delalloc_inode(root, btrfs_inode); |
eb73c1b7 | 4767 | spin_unlock(&root->delalloc_lock); |
acce952b | 4768 | |
fe816d0f NB |
4769 | /* |
4770 | * Make sure we get a live inode and that it'll not disappear | |
4771 | * meanwhile. | |
4772 | */ | |
4773 | inode = igrab(&btrfs_inode->vfs_inode); | |
4774 | if (inode) { | |
4775 | invalidate_inode_pages2(inode->i_mapping); | |
4776 | iput(inode); | |
4777 | } | |
eb73c1b7 | 4778 | spin_lock(&root->delalloc_lock); |
acce952b | 4779 | } |
eb73c1b7 MX |
4780 | spin_unlock(&root->delalloc_lock); |
4781 | } | |
4782 | ||
4783 | static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) | |
4784 | { | |
4785 | struct btrfs_root *root; | |
4786 | struct list_head splice; | |
4787 | ||
4788 | INIT_LIST_HEAD(&splice); | |
4789 | ||
4790 | spin_lock(&fs_info->delalloc_root_lock); | |
4791 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
4792 | while (!list_empty(&splice)) { | |
4793 | root = list_first_entry(&splice, struct btrfs_root, | |
4794 | delalloc_root); | |
00246528 | 4795 | root = btrfs_grab_root(root); |
eb73c1b7 MX |
4796 | BUG_ON(!root); |
4797 | spin_unlock(&fs_info->delalloc_root_lock); | |
4798 | ||
4799 | btrfs_destroy_delalloc_inodes(root); | |
00246528 | 4800 | btrfs_put_root(root); |
eb73c1b7 MX |
4801 | |
4802 | spin_lock(&fs_info->delalloc_root_lock); | |
4803 | } | |
4804 | spin_unlock(&fs_info->delalloc_root_lock); | |
acce952b | 4805 | } |
4806 | ||
2ff7e61e | 4807 | static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, |
acce952b | 4808 | struct extent_io_tree *dirty_pages, |
4809 | int mark) | |
4810 | { | |
4811 | int ret; | |
acce952b | 4812 | struct extent_buffer *eb; |
4813 | u64 start = 0; | |
4814 | u64 end; | |
acce952b | 4815 | |
4816 | while (1) { | |
4817 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, | |
e6138876 | 4818 | mark, NULL); |
acce952b | 4819 | if (ret) |
4820 | break; | |
4821 | ||
91166212 | 4822 | clear_extent_bits(dirty_pages, start, end, mark); |
acce952b | 4823 | while (start <= end) { |
0b246afa JM |
4824 | eb = find_extent_buffer(fs_info, start); |
4825 | start += fs_info->nodesize; | |
fd8b2b61 | 4826 | if (!eb) |
acce952b | 4827 | continue; |
fd8b2b61 | 4828 | wait_on_extent_buffer_writeback(eb); |
acce952b | 4829 | |
fd8b2b61 JB |
4830 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, |
4831 | &eb->bflags)) | |
4832 | clear_extent_buffer_dirty(eb); | |
4833 | free_extent_buffer_stale(eb); | |
acce952b | 4834 | } |
4835 | } | |
4836 | ||
4837 | return ret; | |
4838 | } | |
4839 | ||
2ff7e61e | 4840 | static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, |
fe119a6e | 4841 | struct extent_io_tree *unpin) |
acce952b | 4842 | { |
acce952b | 4843 | u64 start; |
4844 | u64 end; | |
4845 | int ret; | |
4846 | ||
acce952b | 4847 | while (1) { |
0e6ec385 FM |
4848 | struct extent_state *cached_state = NULL; |
4849 | ||
fcd5e742 LF |
4850 | /* |
4851 | * The btrfs_finish_extent_commit() may get the same range as | |
4852 | * ours between find_first_extent_bit and clear_extent_dirty. | |
4853 | * Hence, hold the unused_bg_unpin_mutex to avoid double unpin | |
4854 | * the same extent range. | |
4855 | */ | |
4856 | mutex_lock(&fs_info->unused_bg_unpin_mutex); | |
acce952b | 4857 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
0e6ec385 | 4858 | EXTENT_DIRTY, &cached_state); |
fcd5e742 LF |
4859 | if (ret) { |
4860 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
acce952b | 4861 | break; |
fcd5e742 | 4862 | } |
acce952b | 4863 | |
0e6ec385 FM |
4864 | clear_extent_dirty(unpin, start, end, &cached_state); |
4865 | free_extent_state(cached_state); | |
2ff7e61e | 4866 | btrfs_error_unpin_extent_range(fs_info, start, end); |
fcd5e742 | 4867 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
acce952b | 4868 | cond_resched(); |
4869 | } | |
4870 | ||
4871 | return 0; | |
4872 | } | |
4873 | ||
32da5386 | 4874 | static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) |
c79a1751 LB |
4875 | { |
4876 | struct inode *inode; | |
4877 | ||
4878 | inode = cache->io_ctl.inode; | |
4879 | if (inode) { | |
4880 | invalidate_inode_pages2(inode->i_mapping); | |
4881 | BTRFS_I(inode)->generation = 0; | |
4882 | cache->io_ctl.inode = NULL; | |
4883 | iput(inode); | |
4884 | } | |
bbc37d6e | 4885 | ASSERT(cache->io_ctl.pages == NULL); |
c79a1751 LB |
4886 | btrfs_put_block_group(cache); |
4887 | } | |
4888 | ||
4889 | void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, | |
2ff7e61e | 4890 | struct btrfs_fs_info *fs_info) |
c79a1751 | 4891 | { |
32da5386 | 4892 | struct btrfs_block_group *cache; |
c79a1751 LB |
4893 | |
4894 | spin_lock(&cur_trans->dirty_bgs_lock); | |
4895 | while (!list_empty(&cur_trans->dirty_bgs)) { | |
4896 | cache = list_first_entry(&cur_trans->dirty_bgs, | |
32da5386 | 4897 | struct btrfs_block_group, |
c79a1751 | 4898 | dirty_list); |
c79a1751 LB |
4899 | |
4900 | if (!list_empty(&cache->io_list)) { | |
4901 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
4902 | list_del_init(&cache->io_list); | |
4903 | btrfs_cleanup_bg_io(cache); | |
4904 | spin_lock(&cur_trans->dirty_bgs_lock); | |
4905 | } | |
4906 | ||
4907 | list_del_init(&cache->dirty_list); | |
4908 | spin_lock(&cache->lock); | |
4909 | cache->disk_cache_state = BTRFS_DC_ERROR; | |
4910 | spin_unlock(&cache->lock); | |
4911 | ||
4912 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
4913 | btrfs_put_block_group(cache); | |
ba2c4d4e | 4914 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
c79a1751 LB |
4915 | spin_lock(&cur_trans->dirty_bgs_lock); |
4916 | } | |
4917 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
4918 | ||
45ae2c18 NB |
4919 | /* |
4920 | * Refer to the definition of io_bgs member for details why it's safe | |
4921 | * to use it without any locking | |
4922 | */ | |
c79a1751 LB |
4923 | while (!list_empty(&cur_trans->io_bgs)) { |
4924 | cache = list_first_entry(&cur_trans->io_bgs, | |
32da5386 | 4925 | struct btrfs_block_group, |
c79a1751 | 4926 | io_list); |
c79a1751 LB |
4927 | |
4928 | list_del_init(&cache->io_list); | |
4929 | spin_lock(&cache->lock); | |
4930 | cache->disk_cache_state = BTRFS_DC_ERROR; | |
4931 | spin_unlock(&cache->lock); | |
4932 | btrfs_cleanup_bg_io(cache); | |
4933 | } | |
4934 | } | |
4935 | ||
49b25e05 | 4936 | void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, |
2ff7e61e | 4937 | struct btrfs_fs_info *fs_info) |
49b25e05 | 4938 | { |
bbbf7243 NB |
4939 | struct btrfs_device *dev, *tmp; |
4940 | ||
2ff7e61e | 4941 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
c79a1751 LB |
4942 | ASSERT(list_empty(&cur_trans->dirty_bgs)); |
4943 | ASSERT(list_empty(&cur_trans->io_bgs)); | |
4944 | ||
bbbf7243 NB |
4945 | list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, |
4946 | post_commit_list) { | |
4947 | list_del_init(&dev->post_commit_list); | |
4948 | } | |
4949 | ||
2ff7e61e | 4950 | btrfs_destroy_delayed_refs(cur_trans, fs_info); |
49b25e05 | 4951 | |
4a9d8bde | 4952 | cur_trans->state = TRANS_STATE_COMMIT_START; |
0b246afa | 4953 | wake_up(&fs_info->transaction_blocked_wait); |
49b25e05 | 4954 | |
4a9d8bde | 4955 | cur_trans->state = TRANS_STATE_UNBLOCKED; |
0b246afa | 4956 | wake_up(&fs_info->transaction_wait); |
49b25e05 | 4957 | |
ccdf9b30 | 4958 | btrfs_destroy_delayed_inodes(fs_info); |
49b25e05 | 4959 | |
2ff7e61e | 4960 | btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, |
49b25e05 | 4961 | EXTENT_DIRTY); |
fe119a6e | 4962 | btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); |
49b25e05 | 4963 | |
d3575156 NA |
4964 | btrfs_free_redirty_list(cur_trans); |
4965 | ||
4a9d8bde MX |
4966 | cur_trans->state =TRANS_STATE_COMPLETED; |
4967 | wake_up(&cur_trans->commit_wait); | |
49b25e05 JM |
4968 | } |
4969 | ||
2ff7e61e | 4970 | static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) |
acce952b | 4971 | { |
4972 | struct btrfs_transaction *t; | |
acce952b | 4973 | |
0b246afa | 4974 | mutex_lock(&fs_info->transaction_kthread_mutex); |
acce952b | 4975 | |
0b246afa JM |
4976 | spin_lock(&fs_info->trans_lock); |
4977 | while (!list_empty(&fs_info->trans_list)) { | |
4978 | t = list_first_entry(&fs_info->trans_list, | |
724e2315 JB |
4979 | struct btrfs_transaction, list); |
4980 | if (t->state >= TRANS_STATE_COMMIT_START) { | |
9b64f57d | 4981 | refcount_inc(&t->use_count); |
0b246afa | 4982 | spin_unlock(&fs_info->trans_lock); |
2ff7e61e | 4983 | btrfs_wait_for_commit(fs_info, t->transid); |
724e2315 | 4984 | btrfs_put_transaction(t); |
0b246afa | 4985 | spin_lock(&fs_info->trans_lock); |
724e2315 JB |
4986 | continue; |
4987 | } | |
0b246afa | 4988 | if (t == fs_info->running_transaction) { |
724e2315 | 4989 | t->state = TRANS_STATE_COMMIT_DOING; |
0b246afa | 4990 | spin_unlock(&fs_info->trans_lock); |
724e2315 JB |
4991 | /* |
4992 | * We wait for 0 num_writers since we don't hold a trans | |
4993 | * handle open currently for this transaction. | |
4994 | */ | |
4995 | wait_event(t->writer_wait, | |
4996 | atomic_read(&t->num_writers) == 0); | |
4997 | } else { | |
0b246afa | 4998 | spin_unlock(&fs_info->trans_lock); |
724e2315 | 4999 | } |
2ff7e61e | 5000 | btrfs_cleanup_one_transaction(t, fs_info); |
4a9d8bde | 5001 | |
0b246afa JM |
5002 | spin_lock(&fs_info->trans_lock); |
5003 | if (t == fs_info->running_transaction) | |
5004 | fs_info->running_transaction = NULL; | |
acce952b | 5005 | list_del_init(&t->list); |
0b246afa | 5006 | spin_unlock(&fs_info->trans_lock); |
acce952b | 5007 | |
724e2315 | 5008 | btrfs_put_transaction(t); |
2ff7e61e | 5009 | trace_btrfs_transaction_commit(fs_info->tree_root); |
0b246afa | 5010 | spin_lock(&fs_info->trans_lock); |
724e2315 | 5011 | } |
0b246afa JM |
5012 | spin_unlock(&fs_info->trans_lock); |
5013 | btrfs_destroy_all_ordered_extents(fs_info); | |
ccdf9b30 JM |
5014 | btrfs_destroy_delayed_inodes(fs_info); |
5015 | btrfs_assert_delayed_root_empty(fs_info); | |
0b246afa | 5016 | btrfs_destroy_all_delalloc_inodes(fs_info); |
ef67963d | 5017 | btrfs_drop_all_logs(fs_info); |
0b246afa | 5018 | mutex_unlock(&fs_info->transaction_kthread_mutex); |
acce952b | 5019 | |
5020 | return 0; | |
5021 | } | |
ec7d6dfd | 5022 | |
453e4873 | 5023 | int btrfs_init_root_free_objectid(struct btrfs_root *root) |
ec7d6dfd NB |
5024 | { |
5025 | struct btrfs_path *path; | |
5026 | int ret; | |
5027 | struct extent_buffer *l; | |
5028 | struct btrfs_key search_key; | |
5029 | struct btrfs_key found_key; | |
5030 | int slot; | |
5031 | ||
5032 | path = btrfs_alloc_path(); | |
5033 | if (!path) | |
5034 | return -ENOMEM; | |
5035 | ||
5036 | search_key.objectid = BTRFS_LAST_FREE_OBJECTID; | |
5037 | search_key.type = -1; | |
5038 | search_key.offset = (u64)-1; | |
5039 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
5040 | if (ret < 0) | |
5041 | goto error; | |
5042 | BUG_ON(ret == 0); /* Corruption */ | |
5043 | if (path->slots[0] > 0) { | |
5044 | slot = path->slots[0] - 1; | |
5045 | l = path->nodes[0]; | |
5046 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
23125104 NB |
5047 | root->free_objectid = max_t(u64, found_key.objectid + 1, |
5048 | BTRFS_FIRST_FREE_OBJECTID); | |
ec7d6dfd | 5049 | } else { |
23125104 | 5050 | root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; |
ec7d6dfd NB |
5051 | } |
5052 | ret = 0; | |
5053 | error: | |
5054 | btrfs_free_path(path); | |
5055 | return ret; | |
5056 | } | |
5057 | ||
543068a2 | 5058 | int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) |
ec7d6dfd NB |
5059 | { |
5060 | int ret; | |
5061 | mutex_lock(&root->objectid_mutex); | |
5062 | ||
6b8fad57 | 5063 | if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { |
ec7d6dfd NB |
5064 | btrfs_warn(root->fs_info, |
5065 | "the objectid of root %llu reaches its highest value", | |
5066 | root->root_key.objectid); | |
5067 | ret = -ENOSPC; | |
5068 | goto out; | |
5069 | } | |
5070 | ||
23125104 | 5071 | *objectid = root->free_objectid++; |
ec7d6dfd NB |
5072 | ret = 0; |
5073 | out: | |
5074 | mutex_unlock(&root->objectid_mutex); | |
5075 | return ret; | |
5076 | } |