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