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