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