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