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