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