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