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