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