1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6e20d96d6SChris Mason #include <linux/fs.h> 7d98237b3SChris Mason #include <linux/blkdev.h> 8fc7cbcd4SDavid Sterba #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10ce9adaa5SChris Mason #include <linux/workqueue.h> 11a74a4b97SChris Mason #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13784b4e29SChris Mason #include <linux/migrate.h> 147a36ddecSDavid Sterba #include <linux/ratelimit.h> 156463fe58SStefan Behrens #include <linux/uuid.h> 16803b2f54SStefan Behrens #include <linux/semaphore.h> 17540adea3SMasami Hiramatsu #include <linux/error-injection.h> 189678c543SNikolay Borisov #include <linux/crc32c.h> 19b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 207e75bf3fSDavid Sterba #include <asm/unaligned.h> 216d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 3221adbd5cSStefan Behrens #include "check-integrity.h" 33606686eeSJosef Bacik #include "rcu-string.h" 348dabb742SStefan Behrens #include "dev-replace.h" 3553b381b3SDavid Woodhouse #include "raid56.h" 365ac1d209SJeff Mahoney #include "sysfs.h" 37fcebe456SJosef Bacik #include "qgroup.h" 38ebb8765bSAnand Jain #include "compression.h" 39557ea5ddSQu Wenruo #include "tree-checker.h" 40fd708b81SJosef Bacik #include "ref-verify.h" 41aac0023cSJosef Bacik #include "block-group.h" 42b0643e59SDennis Zhou #include "discard.h" 43f603bb94SNikolay Borisov #include "space-info.h" 44b70f5097SNaohiro Aota #include "zoned.h" 45139e8cd3SQu Wenruo #include "subpage.h" 46eb60ceacSChris Mason 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 54143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 55acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 562ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 57143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 582ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 59acce952bSliubo struct extent_io_tree *dirty_pages, 60acce952bSliubo int mark); 612ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 62acce952bSliubo struct extent_io_tree *pinned_extents); 632ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 642ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 65ce9adaa5SChris Mason 66141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 67141386e1SJosef Bacik { 68141386e1SJosef Bacik if (fs_info->csum_shash) 69141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 70141386e1SJosef Bacik } 71141386e1SJosef Bacik 72d352ac68SChris Mason /* 73d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 74d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 75d352ac68SChris Mason * just before they are sent down the IO stack. 76d352ac68SChris Mason */ 7744b8bd7eSChris Mason struct async_submit_bio { 788896a08dSQu Wenruo struct inode *inode; 7944b8bd7eSChris Mason struct bio *bio; 80a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 8144b8bd7eSChris Mason int mirror_num; 821941b64bSQu Wenruo 831941b64bSQu Wenruo /* Optional parameter for submit_bio_start used by direct io */ 841941b64bSQu Wenruo u64 dio_file_offset; 858b712842SChris Mason struct btrfs_work work; 864e4cbee9SChristoph Hellwig blk_status_t status; 8744b8bd7eSChris Mason }; 8844b8bd7eSChris Mason 8985d4e461SChris Mason /* 9085d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 9185d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 9285d4e461SChris Mason * the level the eb occupies in the tree. 934008c04aSChris Mason * 9485d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 9585d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 9685d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 974fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 984fd786e6SMisono Tomohiro * roots have separate keysets. 994008c04aSChris Mason * 10085d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 10185d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 10285d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1034008c04aSChris Mason * 10485d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 10585d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 10685d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 10785d4e461SChris Mason * 10885d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 10985d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 11085d4e461SChris Mason * needs update as well. 1114008c04aSChris Mason */ 1124008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1134008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1144008c04aSChris Mason # error 1154008c04aSChris Mason # endif 11685d4e461SChris Mason 117ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 118ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 119ab1405aaSDavid Sterba 120ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 121ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 122ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 123ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 124ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 125ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 126ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 127ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 128ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 129ab1405aaSDavid Sterba 13085d4e461SChris Mason static struct btrfs_lockdep_keyset { 13185d4e461SChris Mason u64 id; /* root objectid */ 132ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 133387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 134387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 13585d4e461SChris Mason } btrfs_lockdep_keysets[] = { 136ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 137ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 138ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 139ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 140ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 141ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 142ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 143ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 144ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 145ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 146ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 147ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1484008c04aSChris Mason }; 14985d4e461SChris Mason 150ab1405aaSDavid Sterba #undef DEFINE_LEVEL 151ab1405aaSDavid Sterba #undef DEFINE_NAME 15285d4e461SChris Mason 15385d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 15485d4e461SChris Mason int level) 15585d4e461SChris Mason { 15685d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 15785d4e461SChris Mason 15885d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 15985d4e461SChris Mason 16085d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 16185d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 16285d4e461SChris Mason if (ks->id == objectid) 16385d4e461SChris Mason break; 16485d4e461SChris Mason 16585d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 16685d4e461SChris Mason &ks->keys[level], ks->names[level]); 16785d4e461SChris Mason } 16885d4e461SChris Mason 1694008c04aSChris Mason #endif 1704008c04aSChris Mason 171d352ac68SChris Mason /* 1722996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 173d352ac68SChris Mason */ 174c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 17519c00ddcSChris Mason { 176d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 1777280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 178a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 179d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 18019c00ddcSChris Mason char *kaddr; 181e9be5a30SDavid Sterba int i; 182d5178578SJohannes Thumshirn 183d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 184d5178578SJohannes Thumshirn crypto_shash_init(shash); 185a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 186e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 187a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 18819c00ddcSChris Mason 189e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 190e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 191e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 19219c00ddcSChris Mason } 19371a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 194d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 19519c00ddcSChris Mason } 19619c00ddcSChris Mason 197d352ac68SChris Mason /* 198d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 199d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 200d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 201d352ac68SChris Mason * in the wrong place. 202d352ac68SChris Mason */ 2031259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 204b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 205b9fab919SChris Mason int atomic) 2061259ab75SChris Mason { 2072ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2081259ab75SChris Mason int ret; 2091259ab75SChris Mason 2101259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2111259ab75SChris Mason return 0; 2121259ab75SChris Mason 213b9fab919SChris Mason if (atomic) 214b9fab919SChris Mason return -EAGAIN; 215b9fab919SChris Mason 2162ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 217ff13db41SDavid Sterba &cached_state); 2180b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2191259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2201259ab75SChris Mason ret = 0; 2211259ab75SChris Mason goto out; 2221259ab75SChris Mason } 22394647322SDavid Sterba btrfs_err_rl(eb->fs_info, 22494647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 22594647322SDavid Sterba eb->start, 22629549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2271259ab75SChris Mason ret = 1; 2280b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 22933958dc6SChris Mason out: 2302ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 231e43bbe5eSDavid Sterba &cached_state); 2321259ab75SChris Mason return ret; 2331259ab75SChris Mason } 2341259ab75SChris Mason 235e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 236e7e16f48SJohannes Thumshirn { 237e7e16f48SJohannes Thumshirn switch (csum_type) { 238e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2393951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2403831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 241352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 242e7e16f48SJohannes Thumshirn return true; 243e7e16f48SJohannes Thumshirn default: 244e7e16f48SJohannes Thumshirn return false; 245e7e16f48SJohannes Thumshirn } 246e7e16f48SJohannes Thumshirn } 247e7e16f48SJohannes Thumshirn 248d352ac68SChris Mason /* 2491104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 2501104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 2511104a885SDavid Sterba */ 252ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 253ab8d0fc4SJeff Mahoney char *raw_disk_sb) 2541104a885SDavid Sterba { 2551104a885SDavid Sterba struct btrfs_super_block *disk_sb = 2561104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 25751bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 258d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 259d5178578SJohannes Thumshirn 260d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 2611104a885SDavid Sterba 2621104a885SDavid Sterba /* 2631104a885SDavid Sterba * The super_block structure does not span the whole 26451bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 26551bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 2661104a885SDavid Sterba */ 267fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 268fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 2691104a885SDavid Sterba 27055fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 271e7e16f48SJohannes Thumshirn return 1; 2721104a885SDavid Sterba 273e7e16f48SJohannes Thumshirn return 0; 2741104a885SDavid Sterba } 2751104a885SDavid Sterba 276e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 277ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 278581c1760SQu Wenruo { 279e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 280581c1760SQu Wenruo int found_level; 281581c1760SQu Wenruo struct btrfs_key found_key; 282581c1760SQu Wenruo int ret; 283581c1760SQu Wenruo 284581c1760SQu Wenruo found_level = btrfs_header_level(eb); 285581c1760SQu Wenruo if (found_level != level) { 28663489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 28763489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 288581c1760SQu Wenruo btrfs_err(fs_info, 289581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 290581c1760SQu Wenruo eb->start, level, found_level); 291581c1760SQu Wenruo return -EIO; 292581c1760SQu Wenruo } 293581c1760SQu Wenruo 294581c1760SQu Wenruo if (!first_key) 295581c1760SQu Wenruo return 0; 296581c1760SQu Wenruo 2975d41be6fSQu Wenruo /* 2985d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 2995d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3005d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3015d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3025d41be6fSQu Wenruo */ 3035d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3045d41be6fSQu Wenruo return 0; 30562fdaa52SQu Wenruo 30662fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 30762fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 30862fdaa52SQu Wenruo btrfs_err(fs_info, 30962fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 31062fdaa52SQu Wenruo eb->start); 31162fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 31262fdaa52SQu Wenruo return -EUCLEAN; 31362fdaa52SQu Wenruo } 31462fdaa52SQu Wenruo 315581c1760SQu Wenruo if (found_level) 316581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 317581c1760SQu Wenruo else 318581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 319581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 320581c1760SQu Wenruo 321581c1760SQu Wenruo if (ret) { 32263489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 32363489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 324581c1760SQu Wenruo btrfs_err(fs_info, 325ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 326ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 327ff76a864SLiu Bo first_key->type, first_key->offset, 328ff76a864SLiu Bo found_key.objectid, found_key.type, 329ff76a864SLiu Bo found_key.offset); 330581c1760SQu Wenruo } 331581c1760SQu Wenruo return ret; 332581c1760SQu Wenruo } 333581c1760SQu Wenruo 3341104a885SDavid Sterba /* 335d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 336d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 337581c1760SQu Wenruo * 338581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 339581c1760SQu Wenruo * @level: expected level, mandatory check 340581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 341d352ac68SChris Mason */ 3426a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb, 343581c1760SQu Wenruo u64 parent_transid, int level, 344581c1760SQu Wenruo struct btrfs_key *first_key) 345f188591eSChris Mason { 3465ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 347f188591eSChris Mason struct extent_io_tree *io_tree; 348ea466794SJosef Bacik int failed = 0; 349f188591eSChris Mason int ret; 350f188591eSChris Mason int num_copies = 0; 351f188591eSChris Mason int mirror_num = 0; 352ea466794SJosef Bacik int failed_mirror = 0; 353f188591eSChris Mason 3540b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 355f188591eSChris Mason while (1) { 356f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 357c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 358256dd1bbSStefan Behrens if (!ret) { 359581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 360b9fab919SChris Mason parent_transid, 0)) 361256dd1bbSStefan Behrens ret = -EIO; 362e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 363ff76a864SLiu Bo first_key, parent_transid)) 364581c1760SQu Wenruo ret = -EUCLEAN; 365581c1760SQu Wenruo else 366581c1760SQu Wenruo break; 367256dd1bbSStefan Behrens } 368d397712bSChris Mason 3690b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 370f188591eSChris Mason eb->start, eb->len); 3714235298eSChris Mason if (num_copies == 1) 372ea466794SJosef Bacik break; 3734235298eSChris Mason 3745cf1ab56SJosef Bacik if (!failed_mirror) { 3755cf1ab56SJosef Bacik failed = 1; 3765cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 3775cf1ab56SJosef Bacik } 3785cf1ab56SJosef Bacik 379f188591eSChris Mason mirror_num++; 380ea466794SJosef Bacik if (mirror_num == failed_mirror) 381ea466794SJosef Bacik mirror_num++; 382ea466794SJosef Bacik 3834235298eSChris Mason if (mirror_num > num_copies) 384ea466794SJosef Bacik break; 385f188591eSChris Mason } 386ea466794SJosef Bacik 387c0901581SStefan Behrens if (failed && !ret && failed_mirror) 38820a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 389ea466794SJosef Bacik 390ea466794SJosef Bacik return ret; 391f188591eSChris Mason } 39219c00ddcSChris Mason 393eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 394eca0f6f6SQu Wenruo { 395eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 396eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 397eca0f6f6SQu Wenruo int ret; 398eca0f6f6SQu Wenruo 399eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 400eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 401eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 402eca0f6f6SQu Wenruo csum_tree_block(eb, result); 403eca0f6f6SQu Wenruo 404eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 405eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 406eca0f6f6SQu Wenruo else 407eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 408eca0f6f6SQu Wenruo 4093777369fSQu Wenruo if (ret < 0) 4103777369fSQu Wenruo goto error; 4113777369fSQu Wenruo 4123777369fSQu Wenruo /* 4133777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than 4143777369fSQu Wenruo * last committed. Or something seriously wrong happened. 4153777369fSQu Wenruo */ 4163777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { 4173777369fSQu Wenruo ret = -EUCLEAN; 418eca0f6f6SQu Wenruo btrfs_err(fs_info, 4193777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu", 4203777369fSQu Wenruo eb->start, btrfs_header_generation(eb), 4213777369fSQu Wenruo fs_info->last_trans_committed); 4223777369fSQu Wenruo goto error; 423eca0f6f6SQu Wenruo } 424eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 425eca0f6f6SQu Wenruo 426eca0f6f6SQu Wenruo return 0; 4273777369fSQu Wenruo 4283777369fSQu Wenruo error: 4293777369fSQu Wenruo btrfs_print_tree(eb, 0); 4303777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected", 4313777369fSQu Wenruo eb->start); 4323777369fSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4333777369fSQu Wenruo return ret; 434eca0f6f6SQu Wenruo } 435eca0f6f6SQu Wenruo 436eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 437eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 438eca0f6f6SQu Wenruo struct bio_vec *bvec) 439eca0f6f6SQu Wenruo { 440eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 441eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 442eca0f6f6SQu Wenruo u64 cur; 443eca0f6f6SQu Wenruo int ret = 0; 444eca0f6f6SQu Wenruo 445eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 446eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 447eca0f6f6SQu Wenruo struct extent_buffer *eb; 448eca0f6f6SQu Wenruo bool uptodate; 449eca0f6f6SQu Wenruo 450eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 451eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 452eca0f6f6SQu Wenruo fs_info->nodesize); 453eca0f6f6SQu Wenruo 45401cd3909SDavid Sterba /* A dirty eb shouldn't disappear from buffer_radix */ 455eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 456eca0f6f6SQu Wenruo return -EUCLEAN; 457eca0f6f6SQu Wenruo 458eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 459eca0f6f6SQu Wenruo free_extent_buffer(eb); 460eca0f6f6SQu Wenruo return -EUCLEAN; 461eca0f6f6SQu Wenruo } 462eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 463eca0f6f6SQu Wenruo free_extent_buffer(eb); 464eca0f6f6SQu Wenruo return -EUCLEAN; 465eca0f6f6SQu Wenruo } 466eca0f6f6SQu Wenruo 467eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 468eca0f6f6SQu Wenruo free_extent_buffer(eb); 469eca0f6f6SQu Wenruo if (ret < 0) 470eca0f6f6SQu Wenruo return ret; 471eca0f6f6SQu Wenruo } 472eca0f6f6SQu Wenruo return ret; 473eca0f6f6SQu Wenruo } 474eca0f6f6SQu Wenruo 475d352ac68SChris Mason /* 476ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 477ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 478ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 479d352ac68SChris Mason */ 480ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 48119c00ddcSChris Mason { 482ac303b69SQu Wenruo struct page *page = bvec->bv_page; 4834eee4fa4SMiao Xie u64 start = page_offset(page); 48419c00ddcSChris Mason u64 found_start; 48519c00ddcSChris Mason struct extent_buffer *eb; 486eca0f6f6SQu Wenruo 487fbca46ebSQu Wenruo if (fs_info->nodesize < PAGE_SIZE) 488eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 489f188591eSChris Mason 4904f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4914f2de97aSJosef Bacik if (page != eb->pages[0]) 4924f2de97aSJosef Bacik return 0; 4930f805531SAlex Lyakas 49419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 495d3575156SNaohiro Aota 496d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 497d3575156SNaohiro Aota WARN_ON(found_start != 0); 498d3575156SNaohiro Aota return 0; 499d3575156SNaohiro Aota } 500d3575156SNaohiro Aota 5010f805531SAlex Lyakas /* 5020f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5030f805531SAlex Lyakas * It is useful to know what went wrong. 5040f805531SAlex Lyakas */ 5050f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5060f805531SAlex Lyakas return -EUCLEAN; 5070f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5080f805531SAlex Lyakas return -EUCLEAN; 5090f805531SAlex Lyakas 510eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 51119c00ddcSChris Mason } 51219c00ddcSChris Mason 513b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5142b82032cSYan Zheng { 515b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 516944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 51744880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 518944d3f9fSNikolay Borisov u8 *metadata_uuid; 5192b82032cSYan Zheng 5209a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5219a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5227239ff4bSNikolay Borisov /* 523944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 524944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 525944d3f9fSNikolay Borisov * ->fsid in this case is fine 5267239ff4bSNikolay Borisov */ 527944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5287239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5297239ff4bSNikolay Borisov else 5307239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5317239ff4bSNikolay Borisov 532944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 533944d3f9fSNikolay Borisov return 0; 534944d3f9fSNikolay Borisov 535944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 536944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 537944d3f9fSNikolay Borisov return 0; 538944d3f9fSNikolay Borisov 539944d3f9fSNikolay Borisov return 1; 5402b82032cSYan Zheng } 5412b82032cSYan Zheng 54277bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 54377bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 544ce9adaa5SChris Mason { 54577bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 546ce9adaa5SChris Mason u64 found_start; 54777bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 54877bf40a2SQu Wenruo u8 found_level; 5492996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 550dfd29eedSDavid Sterba const u8 *header_csum; 55177bf40a2SQu Wenruo int ret = 0; 552ea466794SJosef Bacik 553ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 554727011e0SChris Mason if (found_start != eb->start) { 555893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 556893bf4b1SSu Yue eb->start, found_start); 557f188591eSChris Mason ret = -EIO; 55877bf40a2SQu Wenruo goto out; 559ce9adaa5SChris Mason } 560b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 56102873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 56294647322SDavid Sterba eb->start); 5631259ab75SChris Mason ret = -EIO; 56477bf40a2SQu Wenruo goto out; 5651259ab75SChris Mason } 566ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5671c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 568893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 569893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5701c24c3ceSJosef Bacik ret = -EIO; 57177bf40a2SQu Wenruo goto out; 5721c24c3ceSJosef Bacik } 573ce9adaa5SChris Mason 574c67b3892SDavid Sterba csum_tree_block(eb, result); 575dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 576dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 577a826d6dcSJosef Bacik 578dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 5792996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 580ff14aa79SDavid Sterba "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 581ff14aa79SDavid Sterba eb->start, 582dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 58335be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 58435be8851SJohannes Thumshirn btrfs_header_level(eb)); 5852996e1f8SJohannes Thumshirn ret = -EUCLEAN; 58677bf40a2SQu Wenruo goto out; 5872996e1f8SJohannes Thumshirn } 5882996e1f8SJohannes Thumshirn 589a826d6dcSJosef Bacik /* 590a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 591a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 592a826d6dcSJosef Bacik * return -EIO. 593a826d6dcSJosef Bacik */ 5941c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 595a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 596a826d6dcSJosef Bacik ret = -EIO; 597a826d6dcSJosef Bacik } 598ce9adaa5SChris Mason 599813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 600053ab70fSLiu Bo ret = -EIO; 601053ab70fSLiu Bo 6020b32f4bbSJosef Bacik if (!ret) 6030b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 60475391f0dSQu Wenruo else 60575391f0dSQu Wenruo btrfs_err(fs_info, 60675391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 60775391f0dSQu Wenruo eb->start); 60877bf40a2SQu Wenruo out: 60977bf40a2SQu Wenruo return ret; 61077bf40a2SQu Wenruo } 61177bf40a2SQu Wenruo 612371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 613371cdc07SQu Wenruo int mirror) 614371cdc07SQu Wenruo { 615371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 616371cdc07SQu Wenruo struct extent_buffer *eb; 617371cdc07SQu Wenruo bool reads_done; 618371cdc07SQu Wenruo int ret = 0; 619371cdc07SQu Wenruo 620371cdc07SQu Wenruo /* 621371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 622371cdc07SQu Wenruo * only get one eb for each endio hook. 623371cdc07SQu Wenruo */ 624371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 625371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 626371cdc07SQu Wenruo 627371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 628371cdc07SQu Wenruo /* 629371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 630371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 631371cdc07SQu Wenruo */ 632371cdc07SQu Wenruo ASSERT(eb); 633371cdc07SQu Wenruo 634371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 635371cdc07SQu Wenruo /* Subpage read must finish in page read */ 636371cdc07SQu Wenruo ASSERT(reads_done); 637371cdc07SQu Wenruo 638371cdc07SQu Wenruo eb->read_mirror = mirror; 639371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 640371cdc07SQu Wenruo ret = -EIO; 641371cdc07SQu Wenruo goto err; 642371cdc07SQu Wenruo } 643371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 644371cdc07SQu Wenruo if (ret < 0) 645371cdc07SQu Wenruo goto err; 646371cdc07SQu Wenruo 647371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 648371cdc07SQu Wenruo 649371cdc07SQu Wenruo free_extent_buffer(eb); 650371cdc07SQu Wenruo return ret; 651371cdc07SQu Wenruo err: 652371cdc07SQu Wenruo /* 653371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 654371cdc07SQu Wenruo * make sure it has something to decrement. 655371cdc07SQu Wenruo */ 656371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 657371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 658371cdc07SQu Wenruo free_extent_buffer(eb); 659371cdc07SQu Wenruo return ret; 660371cdc07SQu Wenruo } 661371cdc07SQu Wenruo 662c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 66377bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 66477bf40a2SQu Wenruo int mirror) 66577bf40a2SQu Wenruo { 66677bf40a2SQu Wenruo struct extent_buffer *eb; 66777bf40a2SQu Wenruo int ret = 0; 66877bf40a2SQu Wenruo int reads_done; 66977bf40a2SQu Wenruo 67077bf40a2SQu Wenruo ASSERT(page->private); 671371cdc07SQu Wenruo 672fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 673371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 674371cdc07SQu Wenruo 67577bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 67677bf40a2SQu Wenruo 67777bf40a2SQu Wenruo /* 67877bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 67977bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 68077bf40a2SQu Wenruo */ 68177bf40a2SQu Wenruo atomic_inc(&eb->refs); 68277bf40a2SQu Wenruo 68377bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 68477bf40a2SQu Wenruo if (!reads_done) 68577bf40a2SQu Wenruo goto err; 68677bf40a2SQu Wenruo 68777bf40a2SQu Wenruo eb->read_mirror = mirror; 68877bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 68977bf40a2SQu Wenruo ret = -EIO; 69077bf40a2SQu Wenruo goto err; 69177bf40a2SQu Wenruo } 69277bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 693ce9adaa5SChris Mason err: 69453b381b3SDavid Woodhouse if (ret) { 69553b381b3SDavid Woodhouse /* 69653b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 69753b381b3SDavid Woodhouse * again, we have to make sure it has something 69853b381b3SDavid Woodhouse * to decrement 69953b381b3SDavid Woodhouse */ 70053b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7010b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 70253b381b3SDavid Woodhouse } 7030b32f4bbSJosef Bacik free_extent_buffer(eb); 70477bf40a2SQu Wenruo 705f188591eSChris Mason return ret; 706ce9adaa5SChris Mason } 707ce9adaa5SChris Mason 7084a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7094a69a410SChris Mason { 7104a69a410SChris Mason struct async_submit_bio *async; 7114e4cbee9SChristoph Hellwig blk_status_t ret; 7124a69a410SChris Mason 7134a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7141941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 7151941b64bSQu Wenruo async->dio_file_offset); 71679787eaaSJeff Mahoney if (ret) 7174e4cbee9SChristoph Hellwig async->status = ret; 7184a69a410SChris Mason } 7194a69a410SChris Mason 72006ea01b1SDavid Sterba /* 72106ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 72206ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 72306ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 72406ea01b1SDavid Sterba * 72506ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 72606ea01b1SDavid Sterba * inserted into the tree. 72706ea01b1SDavid Sterba */ 7284a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7298b712842SChris Mason { 7308b712842SChris Mason struct async_submit_bio *async; 73106ea01b1SDavid Sterba struct inode *inode; 73206ea01b1SDavid Sterba blk_status_t ret; 7338b712842SChris Mason 7348b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7358896a08dSQu Wenruo inode = async->inode; 7364854ddd0SChris Mason 737bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7384e4cbee9SChristoph Hellwig if (async->status) { 7394e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7404246a0b6SChristoph Hellwig bio_endio(async->bio); 74179787eaaSJeff Mahoney return; 74279787eaaSJeff Mahoney } 74379787eaaSJeff Mahoney 744ec39f769SChris Mason /* 745ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 746ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 747ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 748ec39f769SChris Mason */ 749ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 75008635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 75106ea01b1SDavid Sterba if (ret) { 75206ea01b1SDavid Sterba async->bio->bi_status = ret; 75306ea01b1SDavid Sterba bio_endio(async->bio); 75406ea01b1SDavid Sterba } 7554a69a410SChris Mason } 7564a69a410SChris Mason 7574a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7584a69a410SChris Mason { 7594a69a410SChris Mason struct async_submit_bio *async; 7604a69a410SChris Mason 7614a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7628b712842SChris Mason kfree(async); 7638b712842SChris Mason } 7648b712842SChris Mason 7658896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 766a6f5e39eSDavid Sterba int mirror_num, u64 dio_file_offset, 767e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 76844b8bd7eSChris Mason { 7698896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 77044b8bd7eSChris Mason struct async_submit_bio *async; 77144b8bd7eSChris Mason 77244b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 77344b8bd7eSChris Mason if (!async) 7744e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 77544b8bd7eSChris Mason 7768896a08dSQu Wenruo async->inode = inode; 77744b8bd7eSChris Mason async->bio = bio; 77844b8bd7eSChris Mason async->mirror_num = mirror_num; 7794a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7804a69a410SChris Mason 781a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 782a0cac0ecSOmar Sandoval run_one_async_free); 7834a69a410SChris Mason 7841941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 7858c8bee1dSChris Mason 7864e4cbee9SChristoph Hellwig async->status = 0; 78779787eaaSJeff Mahoney 78867f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 789a31b4a43SChristoph Hellwig btrfs_queue_work(fs_info->hipri_workers, &async->work); 790a31b4a43SChristoph Hellwig else 7915cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 79244b8bd7eSChris Mason return 0; 79344b8bd7eSChris Mason } 79444b8bd7eSChris Mason 7954e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 796ce3ed71aSChris Mason { 7972c30c71bSKent Overstreet struct bio_vec *bvec; 798ce3ed71aSChris Mason struct btrfs_root *root; 7992b070cfeSChristoph Hellwig int ret = 0; 8006dc4f100SMing Lei struct bvec_iter_all iter_all; 801ce3ed71aSChris Mason 802c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8032b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 804ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 805ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 80679787eaaSJeff Mahoney if (ret) 80779787eaaSJeff Mahoney break; 808ce3ed71aSChris Mason } 8092c30c71bSKent Overstreet 8104e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 811ce3ed71aSChris Mason } 812ce3ed71aSChris Mason 8138896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 8141941b64bSQu Wenruo u64 dio_file_offset) 81522c59948SChris Mason { 8168b712842SChris Mason /* 8178b712842SChris Mason * when we're called for a write, we're already in the async 8185443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8198b712842SChris Mason */ 82079787eaaSJeff Mahoney return btree_csum_one_bio(bio); 82122c59948SChris Mason } 82222c59948SChris Mason 823f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 8249b4e675aSDavid Sterba struct btrfs_inode *bi) 825de0022b9SJosef Bacik { 8264eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 827f4dcfb30SJohannes Thumshirn return false; 8286300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 829f4dcfb30SJohannes Thumshirn return false; 8309b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 831f4dcfb30SJohannes Thumshirn return false; 832f4dcfb30SJohannes Thumshirn return true; 833de0022b9SJosef Bacik } 834de0022b9SJosef Bacik 83594d9e11bSChristoph Hellwig void btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, int mirror_num) 83644b8bd7eSChris Mason { 8370b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8384e4cbee9SChristoph Hellwig blk_status_t ret; 839cad321adSChris Mason 84008a6f464SChristoph Hellwig bio->bi_opf |= REQ_META; 84108a6f464SChristoph Hellwig 842cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 84308635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 844f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 845de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 84694d9e11bSChristoph Hellwig if (!ret) 84708635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 84861891923SStefan Behrens } else { 849cad321adSChris Mason /* 85061891923SStefan Behrens * kthread helpers are used to submit writes so that 85161891923SStefan Behrens * checksumming can happen in parallel across all CPUs 852cad321adSChris Mason */ 8538896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 854a6f5e39eSDavid Sterba btree_submit_bio_start); 85544b8bd7eSChris Mason } 85644b8bd7eSChris Mason 85794d9e11bSChristoph Hellwig if (ret) { 8584e4cbee9SChristoph Hellwig bio->bi_status = ret; 8594246a0b6SChristoph Hellwig bio_endio(bio); 86094d9e11bSChristoph Hellwig } 86161891923SStefan Behrens } 86261891923SStefan Behrens 8633dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 864784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 865a6bc32b8SMel Gorman struct page *newpage, struct page *page, 866a6bc32b8SMel Gorman enum migrate_mode mode) 867784b4e29SChris Mason { 868784b4e29SChris Mason /* 869784b4e29SChris Mason * we can't safely write a btree page from here, 870784b4e29SChris Mason * we haven't done the locking hook 871784b4e29SChris Mason */ 872784b4e29SChris Mason if (PageDirty(page)) 873784b4e29SChris Mason return -EAGAIN; 874784b4e29SChris Mason /* 875784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 876784b4e29SChris Mason * We must have no buffers or drop them. 877784b4e29SChris Mason */ 878784b4e29SChris Mason if (page_has_private(page) && 879784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 880784b4e29SChris Mason return -EAGAIN; 881a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 882784b4e29SChris Mason } 8833dd1462eSJan Beulich #endif 884784b4e29SChris Mason 8850da5468fSChris Mason 8860da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8870da5468fSChris Mason struct writeback_control *wbc) 8880da5468fSChris Mason { 889e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 890e2d84521SMiao Xie int ret; 891e2d84521SMiao Xie 892d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 893448d640bSChris Mason 894448d640bSChris Mason if (wbc->for_kupdate) 895448d640bSChris Mason return 0; 896448d640bSChris Mason 897e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 898b9473439SChris Mason /* this is a bit racy, but that's ok */ 899d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 900d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 901d814a491SEthan Lien fs_info->dirty_metadata_batch); 902e2d84521SMiao Xie if (ret < 0) 903793955bcSChris Mason return 0; 904793955bcSChris Mason } 9050b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9060da5468fSChris Mason } 9070da5468fSChris Mason 908f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags) 9095f39d397SChris Mason { 910f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio)) 911f913cff3SMatthew Wilcox (Oracle) return false; 9120c4e538bSDavid Sterba 913f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page); 914d98237b3SChris Mason } 915d98237b3SChris Mason 916895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 917895586ebSMatthew Wilcox (Oracle) size_t length) 918d98237b3SChris Mason { 919d1310b2eSChris Mason struct extent_io_tree *tree; 920895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 921895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 922f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS); 923895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 924895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 925895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 926895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 927895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 9289ad6b7bcSChris Mason } 929d98237b3SChris Mason } 930d98237b3SChris Mason 931bb146eb2SJosef Bacik #ifdef DEBUG 9320079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 9330079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 9340079c3b1SMatthew Wilcox (Oracle) { 9350079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 936139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 9370b32f4bbSJosef Bacik struct extent_buffer *eb; 938139e8cd3SQu Wenruo int cur_bit = 0; 9390079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 9400b32f4bbSJosef Bacik 941139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 9420079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 9430b32f4bbSJosef Bacik BUG_ON(!eb); 9440b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9450b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 94649d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 9470079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 948139e8cd3SQu Wenruo } 9490079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 950139e8cd3SQu Wenruo 951139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 952139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 953139e8cd3SQu Wenruo unsigned long flags; 954139e8cd3SQu Wenruo u64 cur; 955139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 956139e8cd3SQu Wenruo 957139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 958139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 959139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 960139e8cd3SQu Wenruo cur_bit++; 961139e8cd3SQu Wenruo continue; 962139e8cd3SQu Wenruo } 963139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 964139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 965139e8cd3SQu Wenruo 966139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 967139e8cd3SQu Wenruo ASSERT(eb); 968139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 969139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 97049d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 971139e8cd3SQu Wenruo free_extent_buffer(eb); 972139e8cd3SQu Wenruo 973139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 974139e8cd3SQu Wenruo } 9750079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 9760b32f4bbSJosef Bacik } 9770079c3b1SMatthew Wilcox (Oracle) #else 9780079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 9790079c3b1SMatthew Wilcox (Oracle) #endif 9800b32f4bbSJosef Bacik 9817f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9820da5468fSChris Mason .writepages = btree_writepages, 983f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio, 984895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 9855a92bc88SChris Mason #ifdef CONFIG_MIGRATION 986784b4e29SChris Mason .migratepage = btree_migratepage, 9875a92bc88SChris Mason #endif 9880079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 989d98237b3SChris Mason }; 990123abc88SChris Mason 9912ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9922ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9933fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 9943fbaf258SJosef Bacik int level) 9950999df54SChris Mason { 9960b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9970b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 9983fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 9990999df54SChris Mason } 10000999df54SChris Mason 1001581c1760SQu Wenruo /* 1002581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1003581c1760SQu Wenruo * verification. 1004581c1760SQu Wenruo * 10051b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1006581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1007581c1760SQu Wenruo * @level: expected level, mandatory check 1008581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1009581c1760SQu Wenruo */ 10102ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 10111b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 10121b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1013e20d96d6SChris Mason { 10145f39d397SChris Mason struct extent_buffer *buf = NULL; 101519c00ddcSChris Mason int ret; 101619c00ddcSChris Mason 10173fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1018c871b0f2SLiu Bo if (IS_ERR(buf)) 1019c871b0f2SLiu Bo return buf; 1020e4204dedSChris Mason 10216a2e9dc4SFilipe Manana ret = btrfs_read_extent_buffer(buf, parent_transid, level, first_key); 10220f0fe8f7SFilipe David Borba Manana if (ret) { 1023537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 102464c043deSLiu Bo return ERR_PTR(ret); 10250f0fe8f7SFilipe David Borba Manana } 102688c602abSQu Wenruo if (btrfs_check_eb_owner(buf, owner_root)) { 102788c602abSQu Wenruo free_extent_buffer_stale(buf); 102888c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 102988c602abSQu Wenruo } 10305f39d397SChris Mason return buf; 1031ce9adaa5SChris Mason 1032eb60ceacSChris Mason } 1033eb60ceacSChris Mason 10346a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1035ed2ff2cbSChris Mason { 10366a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 103755c69072SChris Mason if (btrfs_header_generation(buf) == 1038e2d84521SMiao Xie fs_info->running_transaction->transid) { 103949d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1040b4ce94deSChris Mason 1041b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1042104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1043e2d84521SMiao Xie -buf->len, 1044e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10450b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1046925baeddSChris Mason } 10475f39d397SChris Mason } 1048ed7b63ebSJosef Bacik } 10495f39d397SChris Mason 1050da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1051e20d96d6SChris Mason u64 objectid) 1052d97e63b6SChris Mason { 10537c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 10542e608bd1SJosef Bacik 10552e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 10562e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 10572e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 105896dfcb46SJosef Bacik root->fs_info = fs_info; 10592e608bd1SJosef Bacik root->root_key.objectid = objectid; 1060cfaa7295SChris Mason root->node = NULL; 1061a28ec197SChris Mason root->commit_root = NULL; 106227cdeb70SMiao Xie root->state = 0; 1063abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 10640b86a832SChris Mason 10650f7d52f4SChris Mason root->last_trans = 0; 10666b8fad57SNikolay Borisov root->free_objectid = 0; 1067eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1068199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10696bef4d31SEric Paris root->inode_tree = RB_ROOT; 1070088aea3bSDavid Sterba INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 10712e608bd1SJosef Bacik 10722e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 10730b86a832SChris Mason 10740b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10755d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1076eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1077eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1078199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1079199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1080d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10812ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10822ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10835d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1084eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1085199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1086f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10872ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10882ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10898287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1090a2135011SChris Mason mutex_init(&root->objectid_mutex); 1091e02119d5SChris Mason mutex_init(&root->log_mutex); 109231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1093573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1094c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10957237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10967237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10977237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 10988b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 10998b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11007237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11017237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11027237f183SYan Zheng atomic_set(&root->log_writers, 0); 11032ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11040700cea7SElena Reshetova refcount_set(&root->refs, 1); 11058ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1106eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11077237f183SYan Zheng root->log_transid = 0; 1108d1433debSMiao Xie root->log_transid_committed = -1; 1109257c62e1SChris Mason root->last_log_commit = 0; 11102e608bd1SJosef Bacik root->anon_dev = 0; 1111e289f03eSFilipe Manana if (!dummy) { 111243eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 111343eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1114e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1115e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1116e289f03eSFilipe Manana } 1117017e5369SChris Mason 11185f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1119370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1120bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1121bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1122fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1123bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1124fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1125bd647ce3SJosef Bacik #endif 11263768f368SChris Mason } 11273768f368SChris Mason 112874e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 112996dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11306f07e42eSAl Viro { 113174e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11326f07e42eSAl Viro if (root) 113396dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11346f07e42eSAl Viro return root; 11356f07e42eSAl Viro } 11366f07e42eSAl Viro 113706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 113806ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1139da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 114006ea65a3SJosef Bacik { 114106ea65a3SJosef Bacik struct btrfs_root *root; 114206ea65a3SJosef Bacik 11437c0260eeSJeff Mahoney if (!fs_info) 11447c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11457c0260eeSJeff Mahoney 114696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 114706ea65a3SJosef Bacik if (!root) 114806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1149da17066cSJeff Mahoney 1150b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1151faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 115206ea65a3SJosef Bacik 115306ea65a3SJosef Bacik return root; 115406ea65a3SJosef Bacik } 115506ea65a3SJosef Bacik #endif 115606ea65a3SJosef Bacik 1157abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1158abed4aaaSJosef Bacik { 1159abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1160abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1161abed4aaaSJosef Bacik 1162abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1163abed4aaaSJosef Bacik } 1164abed4aaaSJosef Bacik 1165abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1166abed4aaaSJosef Bacik { 1167abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1168abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1169abed4aaaSJosef Bacik 1170abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1171abed4aaaSJosef Bacik } 1172abed4aaaSJosef Bacik 1173abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1174abed4aaaSJosef Bacik { 1175abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1176abed4aaaSJosef Bacik struct rb_node *tmp; 1177abed4aaaSJosef Bacik 1178abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1179abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1180abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1181abed4aaaSJosef Bacik ASSERT(!tmp); 1182abed4aaaSJosef Bacik 1183abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1184abed4aaaSJosef Bacik } 1185abed4aaaSJosef Bacik 1186abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1187abed4aaaSJosef Bacik { 1188abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1189abed4aaaSJosef Bacik 1190abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1191abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1192abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1193abed4aaaSJosef Bacik } 1194abed4aaaSJosef Bacik 1195abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1196abed4aaaSJosef Bacik struct btrfs_key *key) 1197abed4aaaSJosef Bacik { 1198abed4aaaSJosef Bacik struct rb_node *node; 1199abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1200abed4aaaSJosef Bacik 1201abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1202abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1203abed4aaaSJosef Bacik if (node) 1204abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1205abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1206abed4aaaSJosef Bacik 1207abed4aaaSJosef Bacik return root; 1208abed4aaaSJosef Bacik } 1209abed4aaaSJosef Bacik 1210f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1211f7238e50SJosef Bacik { 1212f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1213f7238e50SJosef Bacik u64 ret; 1214f7238e50SJosef Bacik 1215f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1216f7238e50SJosef Bacik return 0; 1217f7238e50SJosef Bacik 1218f7238e50SJosef Bacik if (bytenr) 1219f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1220f7238e50SJosef Bacik else 1221f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1222f7238e50SJosef Bacik ASSERT(block_group); 1223f7238e50SJosef Bacik if (!block_group) 1224f7238e50SJosef Bacik return 0; 1225f7238e50SJosef Bacik ret = block_group->global_root_id; 1226f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1227f7238e50SJosef Bacik 1228f7238e50SJosef Bacik return ret; 1229f7238e50SJosef Bacik } 1230f7238e50SJosef Bacik 1231abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1232abed4aaaSJosef Bacik { 1233abed4aaaSJosef Bacik struct btrfs_key key = { 1234abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1235abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1236f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1237abed4aaaSJosef Bacik }; 1238abed4aaaSJosef Bacik 1239abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1240abed4aaaSJosef Bacik } 1241abed4aaaSJosef Bacik 1242abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1243abed4aaaSJosef Bacik { 1244abed4aaaSJosef Bacik struct btrfs_key key = { 1245abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1246abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1247f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1248abed4aaaSJosef Bacik }; 1249abed4aaaSJosef Bacik 1250abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1251abed4aaaSJosef Bacik } 1252abed4aaaSJosef Bacik 125320897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 125420897f5cSArne Jansen u64 objectid) 125520897f5cSArne Jansen { 12569b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 125720897f5cSArne Jansen struct extent_buffer *leaf; 125820897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 125920897f5cSArne Jansen struct btrfs_root *root; 126020897f5cSArne Jansen struct btrfs_key key; 1261b89f6d1fSFilipe Manana unsigned int nofs_flag; 126220897f5cSArne Jansen int ret = 0; 126320897f5cSArne Jansen 1264b89f6d1fSFilipe Manana /* 1265b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1266b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1267b89f6d1fSFilipe Manana */ 1268b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 126996dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1270b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 127120897f5cSArne Jansen if (!root) 127220897f5cSArne Jansen return ERR_PTR(-ENOMEM); 127320897f5cSArne Jansen 127420897f5cSArne Jansen root->root_key.objectid = objectid; 127520897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 127620897f5cSArne Jansen root->root_key.offset = 0; 127720897f5cSArne Jansen 12789631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12799631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 128020897f5cSArne Jansen if (IS_ERR(leaf)) { 128120897f5cSArne Jansen ret = PTR_ERR(leaf); 12821dd05682STsutomu Itoh leaf = NULL; 12838a6a87cdSBoris Burkov goto fail_unlock; 128420897f5cSArne Jansen } 128520897f5cSArne Jansen 128620897f5cSArne Jansen root->node = leaf; 128720897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 128820897f5cSArne Jansen 128920897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129027cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 129120897f5cSArne Jansen 1292f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1293f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 129420897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 129520897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 129620897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 129720897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 129820897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 129920897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130020897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130133d85fdaSQu Wenruo if (is_fstree(objectid)) 1302807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1303807fc790SAndy Shevchenko else 1304807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1305c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 130620897f5cSArne Jansen 13078a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13088a6a87cdSBoris Burkov 130920897f5cSArne Jansen key.objectid = objectid; 131020897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131120897f5cSArne Jansen key.offset = 0; 131220897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 131320897f5cSArne Jansen if (ret) 131420897f5cSArne Jansen goto fail; 131520897f5cSArne Jansen 131620897f5cSArne Jansen return root; 13171dd05682STsutomu Itoh 13188a6a87cdSBoris Burkov fail_unlock: 13198c38938cSJosef Bacik if (leaf) 13201dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13218a6a87cdSBoris Burkov fail: 132200246528SJosef Bacik btrfs_put_root(root); 13231dd05682STsutomu Itoh 13241dd05682STsutomu Itoh return ERR_PTR(ret); 132520897f5cSArne Jansen } 132620897f5cSArne Jansen 13277237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1328e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13290f7d52f4SChris Mason { 13300f7d52f4SChris Mason struct btrfs_root *root; 1331e02119d5SChris Mason 133296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1333e02119d5SChris Mason if (!root) 13347237f183SYan Zheng return ERR_PTR(-ENOMEM); 1335e02119d5SChris Mason 1336e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1337e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1338e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 133927cdeb70SMiao Xie 13406ab6ebb7SNaohiro Aota return root; 13416ab6ebb7SNaohiro Aota } 13426ab6ebb7SNaohiro Aota 13436ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 13446ab6ebb7SNaohiro Aota struct btrfs_root *root) 13456ab6ebb7SNaohiro Aota { 13466ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 13476ab6ebb7SNaohiro Aota 13487237f183SYan Zheng /* 134992a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 135027cdeb70SMiao Xie * 135192a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 135292a7cc42SQu Wenruo * and they go away before a real commit is actually done. 135392a7cc42SQu Wenruo * 135492a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 135592a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 13567237f183SYan Zheng */ 1357e02119d5SChris Mason 13584d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13599631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 13606ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 13616ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1362e02119d5SChris Mason 13637237f183SYan Zheng root->node = leaf; 1364e02119d5SChris Mason 1365e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1366e02119d5SChris Mason btrfs_tree_unlock(root->node); 13676ab6ebb7SNaohiro Aota 13686ab6ebb7SNaohiro Aota return 0; 13697237f183SYan Zheng } 13707237f183SYan Zheng 13717237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13727237f183SYan Zheng struct btrfs_fs_info *fs_info) 13737237f183SYan Zheng { 13747237f183SYan Zheng struct btrfs_root *log_root; 13757237f183SYan Zheng 13767237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13777237f183SYan Zheng if (IS_ERR(log_root)) 13787237f183SYan Zheng return PTR_ERR(log_root); 13796ab6ebb7SNaohiro Aota 13803ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 13813ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 13823ddebf27SNaohiro Aota 13836ab6ebb7SNaohiro Aota if (ret) { 13846ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13856ab6ebb7SNaohiro Aota return ret; 13866ab6ebb7SNaohiro Aota } 13873ddebf27SNaohiro Aota } 13886ab6ebb7SNaohiro Aota 13897237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13907237f183SYan Zheng fs_info->log_root_tree = log_root; 13917237f183SYan Zheng return 0; 13927237f183SYan Zheng } 13937237f183SYan Zheng 13947237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13957237f183SYan Zheng struct btrfs_root *root) 13967237f183SYan Zheng { 13970b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13987237f183SYan Zheng struct btrfs_root *log_root; 13997237f183SYan Zheng struct btrfs_inode_item *inode_item; 14006ab6ebb7SNaohiro Aota int ret; 14017237f183SYan Zheng 14020b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14037237f183SYan Zheng if (IS_ERR(log_root)) 14047237f183SYan Zheng return PTR_ERR(log_root); 14057237f183SYan Zheng 14066ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14076ab6ebb7SNaohiro Aota if (ret) { 14086ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14096ab6ebb7SNaohiro Aota return ret; 14106ab6ebb7SNaohiro Aota } 14116ab6ebb7SNaohiro Aota 14127237f183SYan Zheng log_root->last_trans = trans->transid; 14137237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14147237f183SYan Zheng 14157237f183SYan Zheng inode_item = &log_root->root_item.inode; 14163cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14173cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14183cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1419da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14200b246afaSJeff Mahoney fs_info->nodesize); 14213cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14227237f183SYan Zheng 14235d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14247237f183SYan Zheng 14257237f183SYan Zheng WARN_ON(root->log_root); 14267237f183SYan Zheng root->log_root = log_root; 14277237f183SYan Zheng root->log_transid = 0; 1428d1433debSMiao Xie root->log_transid_committed = -1; 1429257c62e1SChris Mason root->last_log_commit = 0; 1430e02119d5SChris Mason return 0; 1431e02119d5SChris Mason } 1432e02119d5SChris Mason 143349d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 143449d11beaSJosef Bacik struct btrfs_path *path, 1435cb517eabSMiao Xie struct btrfs_key *key) 1436e02119d5SChris Mason { 1437e02119d5SChris Mason struct btrfs_root *root; 1438e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 143984234f3aSYan Zheng u64 generation; 1440cb517eabSMiao Xie int ret; 1441581c1760SQu Wenruo int level; 1442cb517eabSMiao Xie 144396dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 144449d11beaSJosef Bacik if (!root) 144549d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 14460f7d52f4SChris Mason 1447cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1448cb517eabSMiao Xie &root->root_item, &root->root_key); 14490f7d52f4SChris Mason if (ret) { 145013a8a7c8SYan, Zheng if (ret > 0) 145113a8a7c8SYan, Zheng ret = -ENOENT; 145249d11beaSJosef Bacik goto fail; 14530f7d52f4SChris Mason } 145413a8a7c8SYan, Zheng 145584234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1456581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14572ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14582ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 14591b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 146064c043deSLiu Bo if (IS_ERR(root->node)) { 146164c043deSLiu Bo ret = PTR_ERR(root->node); 14628c38938cSJosef Bacik root->node = NULL; 146349d11beaSJosef Bacik goto fail; 14644eb150d6SQu Wenruo } 14654eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1466cb517eabSMiao Xie ret = -EIO; 146749d11beaSJosef Bacik goto fail; 1468416bc658SJosef Bacik } 146988c602abSQu Wenruo 147088c602abSQu Wenruo /* 147188c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 147288c602abSQu Wenruo * match its root node owner 147388c602abSQu Wenruo */ 147488c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 147588c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 147688c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 147788c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 147888c602abSQu Wenruo btrfs_crit(fs_info, 147988c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 148088c602abSQu Wenruo root->root_key.objectid, root->node->start, 148188c602abSQu Wenruo btrfs_header_owner(root->node), 148288c602abSQu Wenruo root->root_key.objectid); 148388c602abSQu Wenruo ret = -EUCLEAN; 148488c602abSQu Wenruo goto fail; 148588c602abSQu Wenruo } 14865d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1487cb517eabSMiao Xie return root; 148849d11beaSJosef Bacik fail: 148900246528SJosef Bacik btrfs_put_root(root); 149049d11beaSJosef Bacik return ERR_PTR(ret); 149149d11beaSJosef Bacik } 149249d11beaSJosef Bacik 149349d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 149449d11beaSJosef Bacik struct btrfs_key *key) 149549d11beaSJosef Bacik { 149649d11beaSJosef Bacik struct btrfs_root *root; 149749d11beaSJosef Bacik struct btrfs_path *path; 149849d11beaSJosef Bacik 149949d11beaSJosef Bacik path = btrfs_alloc_path(); 150049d11beaSJosef Bacik if (!path) 150149d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 150249d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 150349d11beaSJosef Bacik btrfs_free_path(path); 150449d11beaSJosef Bacik 150549d11beaSJosef Bacik return root; 1506cb517eabSMiao Xie } 1507cb517eabSMiao Xie 15082dfb1e43SQu Wenruo /* 15092dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15102dfb1e43SQu Wenruo * 15112dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 15122dfb1e43SQu Wenruo */ 15132dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1514cb517eabSMiao Xie { 1515cb517eabSMiao Xie int ret; 1516dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1517cb517eabSMiao Xie 1518dcc3eb96SNikolay Borisov /* 1519dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1520dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1521dcc3eb96SNikolay Borisov */ 1522dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1523dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1524dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1525dcc3eb96SNikolay Borisov if (ret) 15268257b2dcSMiao Xie goto fail; 15278257b2dcSMiao Xie 1528aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 152937f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 153092a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1531f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1532f39e4571SJosef Bacik } 1533f39e4571SJosef Bacik 1534851fd730SQu Wenruo /* 1535851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1536851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1537851fd730SQu Wenruo */ 1538851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1539851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 15402dfb1e43SQu Wenruo if (!anon_dev) { 1541cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1542cb517eabSMiao Xie if (ret) 1543876d2cf1SLiu Bo goto fail; 15442dfb1e43SQu Wenruo } else { 15452dfb1e43SQu Wenruo root->anon_dev = anon_dev; 15462dfb1e43SQu Wenruo } 1547851fd730SQu Wenruo } 1548f32e48e9SChandan Rajendra 1549f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1550453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1551f32e48e9SChandan Rajendra if (ret) { 1552f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1553876d2cf1SLiu Bo goto fail; 1554f32e48e9SChandan Rajendra } 1555f32e48e9SChandan Rajendra 15566b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1557f32e48e9SChandan Rajendra 1558f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1559f32e48e9SChandan Rajendra 1560cb517eabSMiao Xie return 0; 1561cb517eabSMiao Xie fail: 156284db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1563cb517eabSMiao Xie return ret; 1564cb517eabSMiao Xie } 1565cb517eabSMiao Xie 1566a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1567cb517eabSMiao Xie u64 root_id) 1568cb517eabSMiao Xie { 1569cb517eabSMiao Xie struct btrfs_root *root; 1570cb517eabSMiao Xie 1571fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1572fc7cbcd4SDavid Sterba root = radix_tree_lookup(&fs_info->fs_roots_radix, 1573fc7cbcd4SDavid Sterba (unsigned long)root_id); 1574bc44d7c4SJosef Bacik if (root) 157500246528SJosef Bacik root = btrfs_grab_root(root); 1576fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1577cb517eabSMiao Xie return root; 1578cb517eabSMiao Xie } 1579cb517eabSMiao Xie 158049d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 158149d11beaSJosef Bacik u64 objectid) 158249d11beaSJosef Bacik { 1583abed4aaaSJosef Bacik struct btrfs_key key = { 1584abed4aaaSJosef Bacik .objectid = objectid, 1585abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1586abed4aaaSJosef Bacik .offset = 0, 1587abed4aaaSJosef Bacik }; 1588abed4aaaSJosef Bacik 158949d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 159049d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 159149d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1592abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 159349d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 159449d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 159549d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 159649d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 159749d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1598abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 159949d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 160049d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 160149d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 160249d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 160349d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 160449d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 1605abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1606abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1607abed4aaaSJosef Bacik 1608abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1609abed4aaaSJosef Bacik } 161049d11beaSJosef Bacik return NULL; 161149d11beaSJosef Bacik } 161249d11beaSJosef Bacik 1613cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1614cb517eabSMiao Xie struct btrfs_root *root) 1615cb517eabSMiao Xie { 1616cb517eabSMiao Xie int ret; 1617cb517eabSMiao Xie 1618fc7cbcd4SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1619fc7cbcd4SDavid Sterba if (ret) 1620fc7cbcd4SDavid Sterba return ret; 1621fc7cbcd4SDavid Sterba 1622fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1623fc7cbcd4SDavid Sterba ret = radix_tree_insert(&fs_info->fs_roots_radix, 1624fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid, 1625fc7cbcd4SDavid Sterba root); 1626af01d2e5SJosef Bacik if (ret == 0) { 162700246528SJosef Bacik btrfs_grab_root(root); 1628fc7cbcd4SDavid Sterba set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1629af01d2e5SJosef Bacik } 1630fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1631fc7cbcd4SDavid Sterba radix_tree_preload_end(); 1632cb517eabSMiao Xie 1633cb517eabSMiao Xie return ret; 1634cb517eabSMiao Xie } 1635cb517eabSMiao Xie 1636bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1637bd647ce3SJosef Bacik { 1638bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1639bd647ce3SJosef Bacik struct btrfs_root *root; 1640bd647ce3SJosef Bacik 1641bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1642457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1643457f1864SJosef Bacik 1644bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1645bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1646457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 164771008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1648bd647ce3SJosef Bacik refcount_read(&root->refs)); 1649bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 165000246528SJosef Bacik btrfs_put_root(root); 165100246528SJosef Bacik btrfs_put_root(root); 1652bd647ce3SJosef Bacik } 1653bd647ce3SJosef Bacik #endif 1654bd647ce3SJosef Bacik } 1655bd647ce3SJosef Bacik 1656abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1657abed4aaaSJosef Bacik { 1658abed4aaaSJosef Bacik struct btrfs_root *root; 1659abed4aaaSJosef Bacik struct rb_node *node; 1660abed4aaaSJosef Bacik 1661abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1662abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1663abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1664abed4aaaSJosef Bacik btrfs_put_root(root); 1665abed4aaaSJosef Bacik } 1666abed4aaaSJosef Bacik } 1667abed4aaaSJosef Bacik 16680d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 16690d4b0463SJosef Bacik { 1670141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1671141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 16725deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1673141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1674141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1675141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1676141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 16770d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 16780d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1679abed4aaaSJosef Bacik free_global_roots(fs_info); 168000246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 168100246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 168200246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 168300246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 168400246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 168500246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1686aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 16879c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1688bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 16893fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 16900d4b0463SJosef Bacik kfree(fs_info->super_copy); 16910d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 16928481dd80SQu Wenruo kfree(fs_info->subpage_info); 16930d4b0463SJosef Bacik kvfree(fs_info); 16940d4b0463SJosef Bacik } 16950d4b0463SJosef Bacik 16960d4b0463SJosef Bacik 16972dfb1e43SQu Wenruo /* 16982dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 16992dfb1e43SQu Wenruo * 17002dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 17012dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 17022dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 17032dfb1e43SQu Wenruo * 17042dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 17052dfb1e43SQu Wenruo * 17062dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 17072dfb1e43SQu Wenruo * same root objectid. 17082dfb1e43SQu Wenruo * 17092dfb1e43SQu Wenruo * @objectid: root id 17102dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 17112dfb1e43SQu Wenruo * pass 0 for new allocation. 17122dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 17132dfb1e43SQu Wenruo * for orphan roots 17142dfb1e43SQu Wenruo */ 17152dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 17162dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 17172dfb1e43SQu Wenruo bool check_ref) 17185eda7b5eSChris Mason { 17195eda7b5eSChris Mason struct btrfs_root *root; 1720381cf658SDavid Sterba struct btrfs_path *path; 17211d4c08e0SDavid Sterba struct btrfs_key key; 17225eda7b5eSChris Mason int ret; 17235eda7b5eSChris Mason 172449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 172549d11beaSJosef Bacik if (root) 172649d11beaSJosef Bacik return root; 17274df27c4dSYan, Zheng again: 172856e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 172948475471SStefan Behrens if (root) { 17302dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17312dfb1e43SQu Wenruo ASSERT(!anon_dev); 1732bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 173300246528SJosef Bacik btrfs_put_root(root); 173448475471SStefan Behrens return ERR_PTR(-ENOENT); 1735bc44d7c4SJosef Bacik } 17365eda7b5eSChris Mason return root; 173748475471SStefan Behrens } 17385eda7b5eSChris Mason 173956e9357aSDavid Sterba key.objectid = objectid; 174056e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 174156e9357aSDavid Sterba key.offset = (u64)-1; 174256e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 17435eda7b5eSChris Mason if (IS_ERR(root)) 17445eda7b5eSChris Mason return root; 17453394e160SChris Mason 1746c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1747d68fc57bSYan, Zheng ret = -ENOENT; 1748d68fc57bSYan, Zheng goto fail; 1749d68fc57bSYan, Zheng } 1750d68fc57bSYan, Zheng 17512dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1752cb517eabSMiao Xie if (ret) 1753cb517eabSMiao Xie goto fail; 1754cb517eabSMiao Xie 1755381cf658SDavid Sterba path = btrfs_alloc_path(); 1756381cf658SDavid Sterba if (!path) { 1757381cf658SDavid Sterba ret = -ENOMEM; 1758381cf658SDavid Sterba goto fail; 1759381cf658SDavid Sterba } 17601d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17611d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 176256e9357aSDavid Sterba key.offset = objectid; 17631d4c08e0SDavid Sterba 17641d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1765381cf658SDavid Sterba btrfs_free_path(path); 1766d68fc57bSYan, Zheng if (ret < 0) 1767d68fc57bSYan, Zheng goto fail; 1768d68fc57bSYan, Zheng if (ret == 0) 176927cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1770d68fc57bSYan, Zheng 1771cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17720f7d52f4SChris Mason if (ret) { 1773168a2f77SJia-Ju Bai if (ret == -EEXIST) { 177400246528SJosef Bacik btrfs_put_root(root); 17754df27c4dSYan, Zheng goto again; 1776168a2f77SJia-Ju Bai } 17774df27c4dSYan, Zheng goto fail; 17784df27c4dSYan, Zheng } 1779edbd8d4eSChris Mason return root; 17804df27c4dSYan, Zheng fail: 178133fab972SFilipe Manana /* 178233fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 1783143823cfSDavid Sterba * responsibility to free it in case we fail. So we have to set our 178433fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 178533fab972SFilipe Manana * and once again by our caller. 178633fab972SFilipe Manana */ 178733fab972SFilipe Manana if (anon_dev) 178833fab972SFilipe Manana root->anon_dev = 0; 17898c38938cSJosef Bacik btrfs_put_root(root); 17904df27c4dSYan, Zheng return ERR_PTR(ret); 1791edbd8d4eSChris Mason } 1792edbd8d4eSChris Mason 17932dfb1e43SQu Wenruo /* 17942dfb1e43SQu Wenruo * Get in-memory reference of a root structure 17952dfb1e43SQu Wenruo * 17962dfb1e43SQu Wenruo * @objectid: tree objectid 17972dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 17982dfb1e43SQu Wenruo * one reference 17992dfb1e43SQu Wenruo */ 18002dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 18012dfb1e43SQu Wenruo u64 objectid, bool check_ref) 18022dfb1e43SQu Wenruo { 18032dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 18042dfb1e43SQu Wenruo } 18052dfb1e43SQu Wenruo 18062dfb1e43SQu Wenruo /* 18072dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 18082dfb1e43SQu Wenruo * the anonymous block device id 18092dfb1e43SQu Wenruo * 18102dfb1e43SQu Wenruo * @objectid: tree objectid 18112dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 18122dfb1e43SQu Wenruo * parameter value 18132dfb1e43SQu Wenruo */ 18142dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 18152dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 18162dfb1e43SQu Wenruo { 18172dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 18182dfb1e43SQu Wenruo } 18192dfb1e43SQu Wenruo 18208b712842SChris Mason /* 182149d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 182249d11beaSJosef Bacik * @fs_info: the fs_info 182349d11beaSJosef Bacik * @objectid: the objectid we need to lookup 182449d11beaSJosef Bacik * 182549d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 182649d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 182749d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 182849d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 182949d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 183049d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 183149d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 183249d11beaSJosef Bacik * backref code is finished using the root. 183349d11beaSJosef Bacik */ 183449d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 183549d11beaSJosef Bacik struct btrfs_path *path, 183649d11beaSJosef Bacik u64 objectid) 183749d11beaSJosef Bacik { 183849d11beaSJosef Bacik struct btrfs_root *root; 183949d11beaSJosef Bacik struct btrfs_key key; 184049d11beaSJosef Bacik 184149d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 184249d11beaSJosef Bacik 184349d11beaSJosef Bacik /* 184449d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 184549d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 184649d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 184749d11beaSJosef Bacik * != NULL just return it. 184849d11beaSJosef Bacik */ 184949d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 185049d11beaSJosef Bacik if (root) 185149d11beaSJosef Bacik return root; 185249d11beaSJosef Bacik 185349d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 185449d11beaSJosef Bacik if (root) 185549d11beaSJosef Bacik return root; 185649d11beaSJosef Bacik 185749d11beaSJosef Bacik key.objectid = objectid; 185849d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 185949d11beaSJosef Bacik key.offset = (u64)-1; 186049d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 186149d11beaSJosef Bacik btrfs_release_path(path); 186249d11beaSJosef Bacik 186349d11beaSJosef Bacik return root; 186449d11beaSJosef Bacik } 186549d11beaSJosef Bacik 1866a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1867a74a4b97SChris Mason { 18680d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1869d0278245SMiao Xie int again; 1870a74a4b97SChris Mason 1871d6fd0ae2SOmar Sandoval while (1) { 1872d0278245SMiao Xie again = 0; 18739d1a2a3aSDavid Sterba 1874fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1875fd340d0fSJosef Bacik 1876d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18772ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1878d0278245SMiao Xie goto sleep; 1879d0278245SMiao Xie 188090c711abSZygo Blaxell /* 188190c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 188290c711abSZygo Blaxell * before we have finished mounting the filesystem. 188390c711abSZygo Blaxell */ 18840b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 188590c711abSZygo Blaxell goto sleep; 188690c711abSZygo Blaxell 18870b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1888d0278245SMiao Xie goto sleep; 1889d0278245SMiao Xie 1890dc7f370cSMiao Xie /* 1891dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1892dc7f370cSMiao Xie * during the above check and trylock. 1893dc7f370cSMiao Xie */ 18942ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18950b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1896dc7f370cSMiao Xie goto sleep; 1897dc7f370cSMiao Xie } 1898dc7f370cSMiao Xie 18992ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1900c2d6cb16SFilipe Manana 190133c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 19020b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1903a74a4b97SChris Mason 1904d0278245SMiao Xie /* 190505323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 190605323cd1SMiao Xie * needn't do anything special here. 1907d0278245SMiao Xie */ 19080b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 190967c5e7d4SFilipe Manana 191067c5e7d4SFilipe Manana /* 1911f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 191267c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 191367c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1914f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 191567c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 191667c5e7d4SFilipe Manana * unused block groups. 191767c5e7d4SFilipe Manana */ 19180b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 191918bb8bbfSJohannes Thumshirn 192018bb8bbfSJohannes Thumshirn /* 192118bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 192218bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 192318bb8bbfSJohannes Thumshirn * space. 192418bb8bbfSJohannes Thumshirn */ 192518bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 1926d0278245SMiao Xie sleep: 1927a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1928d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1929d6fd0ae2SOmar Sandoval kthread_parkme(); 1930d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1931d6fd0ae2SOmar Sandoval return 0; 1932838fe188SJiri Kosina if (!again) { 1933a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1934a74a4b97SChris Mason schedule(); 1935a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1936a74a4b97SChris Mason } 1937da288d28SFilipe Manana } 1938a74a4b97SChris Mason } 1939a74a4b97SChris Mason 1940a74a4b97SChris Mason static int transaction_kthread(void *arg) 1941a74a4b97SChris Mason { 1942a74a4b97SChris Mason struct btrfs_root *root = arg; 19430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1944a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1945a74a4b97SChris Mason struct btrfs_transaction *cur; 19468929ecfaSYan, Zheng u64 transid; 1947643900beSNikolay Borisov time64_t delta; 1948a74a4b97SChris Mason unsigned long delay; 1949914b2007SJan Kara bool cannot_commit; 1950a74a4b97SChris Mason 1951a74a4b97SChris Mason do { 1952914b2007SJan Kara cannot_commit = false; 1953ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 19540b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1955a74a4b97SChris Mason 19560b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19570b246afaSJeff Mahoney cur = fs_info->running_transaction; 1958a74a4b97SChris Mason if (!cur) { 19590b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1960a74a4b97SChris Mason goto sleep; 1961a74a4b97SChris Mason } 196231153d81SYan Zheng 1963643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 1964fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 1965fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 1966643900beSNikolay Borisov delta < fs_info->commit_interval) { 19670b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1968fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1969fb8a7e94SNikolay Borisov delay = min(delay, 1970fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1971a74a4b97SChris Mason goto sleep; 1972a74a4b97SChris Mason } 19738929ecfaSYan, Zheng transid = cur->transid; 19740b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 197556bec294SChris Mason 197679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1977354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1978914b2007SJan Kara if (IS_ERR(trans)) { 1979354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1980914b2007SJan Kara cannot_commit = true; 198179787eaaSJeff Mahoney goto sleep; 1982914b2007SJan Kara } 19838929ecfaSYan, Zheng if (transid == trans->transid) { 19843a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19858929ecfaSYan, Zheng } else { 19863a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19878929ecfaSYan, Zheng } 1988a74a4b97SChris Mason sleep: 19890b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19900b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1991a74a4b97SChris Mason 199284961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 19932ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 19948929ecfaSYan, Zheng if (!kthread_should_stop() && 19950b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1996914b2007SJan Kara cannot_commit)) 1997bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1998a74a4b97SChris Mason } while (!kthread_should_stop()); 1999a74a4b97SChris Mason return 0; 2000a74a4b97SChris Mason } 2001a74a4b97SChris Mason 2002af31f5e5SChris Mason /* 200301f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 200401f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 200501f0f9daSNikolay Borisov * anything. 2006af31f5e5SChris Mason * 2007af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2008af31f5e5SChris Mason * generation of the latest root in the array with the generation 2009af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2010af31f5e5SChris Mason */ 201101f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2012af31f5e5SChris Mason { 201301f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2014af31f5e5SChris Mason u64 cur; 2015af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2016af31f5e5SChris Mason int i; 2017af31f5e5SChris Mason 2018af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2019af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2020af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2021af31f5e5SChris Mason if (cur == newest_gen) 202201f0f9daSNikolay Borisov return i; 2023af31f5e5SChris Mason } 2024af31f5e5SChris Mason 202501f0f9daSNikolay Borisov return -EINVAL; 2026af31f5e5SChris Mason } 2027af31f5e5SChris Mason 2028af31f5e5SChris Mason /* 2029af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2030af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2031af31f5e5SChris Mason * done 2032af31f5e5SChris Mason */ 2033af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2034af31f5e5SChris Mason { 20356ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2036af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2037af31f5e5SChris Mason 2038af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2039af31f5e5SChris Mason 2040af31f5e5SChris Mason /* 2041af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2042af31f5e5SChris Mason * regardless of which ones we use today 2043af31f5e5SChris Mason */ 2044af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2045af31f5e5SChris Mason 2046af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2047af31f5e5SChris Mason 2048af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2049af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2050af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2051af31f5e5SChris Mason 2052af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2053af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2054af31f5e5SChris Mason 2055af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2056af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2057af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2058af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2059af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2060af31f5e5SChris Mason 20619c54e80dSJosef Bacik if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) { 20629c54e80dSJosef Bacik btrfs_set_backup_block_group_root(root_backup, 20639c54e80dSJosef Bacik info->block_group_root->node->start); 20649c54e80dSJosef Bacik btrfs_set_backup_block_group_root_gen(root_backup, 20659c54e80dSJosef Bacik btrfs_header_generation(info->block_group_root->node)); 20669c54e80dSJosef Bacik btrfs_set_backup_block_group_root_level(root_backup, 20679c54e80dSJosef Bacik btrfs_header_level(info->block_group_root->node)); 20689c54e80dSJosef Bacik } else { 20699c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2070f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 20719c54e80dSJosef Bacik 20729c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 20739c54e80dSJosef Bacik extent_root->node->start); 2074af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 207529cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2076af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 207729cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2078af31f5e5SChris Mason 2079f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2080f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2081f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2082f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2083f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 20849c54e80dSJosef Bacik } 2085af31f5e5SChris Mason 20867c7e82a7SChris Mason /* 20877c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20887c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20897c7e82a7SChris Mason */ 20907c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20917c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20927c7e82a7SChris Mason info->fs_root->node->start); 2093af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2094af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2095af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2096af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 20977c7e82a7SChris Mason } 2098af31f5e5SChris Mason 2099af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2100af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2101af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2102af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2103af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2104af31f5e5SChris Mason 2105af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2106af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2107af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2108af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2109af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2110af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2111af31f5e5SChris Mason 2112af31f5e5SChris Mason /* 2113af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2114af31f5e5SChris Mason * for the next commit 2115af31f5e5SChris Mason */ 2116af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2117af31f5e5SChris Mason &info->super_for_commit->super_roots, 2118af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2119af31f5e5SChris Mason } 2120af31f5e5SChris Mason 2121af31f5e5SChris Mason /* 2122bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2123bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2124bd2336b2SNikolay Borisov * 2125bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2126bd2336b2SNikolay Borisov * priority - priority of backup root required 2127bd2336b2SNikolay Borisov * 2128bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2129bd2336b2SNikolay Borisov */ 2130bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2131bd2336b2SNikolay Borisov { 2132bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2133bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2134bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2135bd2336b2SNikolay Borisov 2136bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2137bd2336b2SNikolay Borisov if (priority == 0) 2138bd2336b2SNikolay Borisov return backup_index; 2139bd2336b2SNikolay Borisov 2140bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2141bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2142bd2336b2SNikolay Borisov } else { 2143bd2336b2SNikolay Borisov return -EINVAL; 2144bd2336b2SNikolay Borisov } 2145bd2336b2SNikolay Borisov 2146bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2147bd2336b2SNikolay Borisov 2148bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2149bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2150bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2151bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2152bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2153bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2154bd2336b2SNikolay Borisov 2155bd2336b2SNikolay Borisov /* 2156bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2157bd2336b2SNikolay Borisov * need a fsck 2158bd2336b2SNikolay Borisov */ 2159bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2160bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2161bd2336b2SNikolay Borisov 2162bd2336b2SNikolay Borisov return backup_index; 2163bd2336b2SNikolay Borisov } 2164bd2336b2SNikolay Borisov 21657abadb64SLiu Bo /* helper to cleanup workers */ 21667abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21677abadb64SLiu Bo { 2168dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2169afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 2170a31b4a43SChristoph Hellwig btrfs_destroy_workqueue(fs_info->hipri_workers); 21715cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2172*d7b9416fSChristoph Hellwig if (fs_info->endio_workers) 2173*d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_workers); 2174d34e123dSChristoph Hellwig if (fs_info->endio_raid56_workers) 2175d34e123dSChristoph Hellwig destroy_workqueue(fs_info->endio_raid56_workers); 2176385de0efSChristoph Hellwig if (fs_info->rmw_workers) 2177385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers); 2178fed8a72dSChristoph Hellwig if (fs_info->compressed_write_workers) 2179fed8a72dSChristoph Hellwig destroy_workqueue(fs_info->compressed_write_workers); 2180fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2181fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 21825b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2183e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2184a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2185fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2186b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2187b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2188a9b9477dSFilipe Manana /* 2189a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2190a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2191a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2192a9b9477dSFilipe Manana */ 2193*d7b9416fSChristoph Hellwig if (fs_info->endio_meta_workers) 2194*d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_meta_workers); 21957abadb64SLiu Bo } 21967abadb64SLiu Bo 21972e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 21982e9f5954SRashika { 21992e9f5954SRashika if (root) { 22002e9f5954SRashika free_extent_buffer(root->node); 22012e9f5954SRashika free_extent_buffer(root->commit_root); 22022e9f5954SRashika root->node = NULL; 22032e9f5954SRashika root->commit_root = NULL; 22042e9f5954SRashika } 22052e9f5954SRashika } 22062e9f5954SRashika 2207abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2208abed4aaaSJosef Bacik { 2209abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2210abed4aaaSJosef Bacik 2211abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2212abed4aaaSJosef Bacik &fs_info->global_root_tree, 2213abed4aaaSJosef Bacik rb_node) 2214abed4aaaSJosef Bacik free_root_extent_buffers(root); 2215abed4aaaSJosef Bacik } 2216abed4aaaSJosef Bacik 2217af31f5e5SChris Mason /* helper to cleanup tree roots */ 22184273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2219af31f5e5SChris Mason { 22202e9f5954SRashika free_root_extent_buffers(info->tree_root); 2221655b09feSJosef Bacik 2222abed4aaaSJosef Bacik free_global_root_pointers(info); 22232e9f5954SRashika free_root_extent_buffers(info->dev_root); 22242e9f5954SRashika free_root_extent_buffers(info->quota_root); 22252e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22268c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2227aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 22289c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 22294273eaffSAnand Jain if (free_chunk_root) 22302e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2231af31f5e5SChris Mason } 2232af31f5e5SChris Mason 22338c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 22348c38938cSJosef Bacik { 22358c38938cSJosef Bacik if (!root) 22368c38938cSJosef Bacik return; 22378c38938cSJosef Bacik 22388c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 22398c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 22401dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 22418c38938cSJosef Bacik if (root->anon_dev) 22428c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 22438c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2244923eb523SJohannes Thumshirn free_root_extent_buffers(root); 22458c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2246fc7cbcd4SDavid Sterba spin_lock(&root->fs_info->fs_roots_radix_lock); 22478c38938cSJosef Bacik list_del_init(&root->leak_list); 2248fc7cbcd4SDavid Sterba spin_unlock(&root->fs_info->fs_roots_radix_lock); 22498c38938cSJosef Bacik #endif 22508c38938cSJosef Bacik kfree(root); 22518c38938cSJosef Bacik } 22528c38938cSJosef Bacik } 22538c38938cSJosef Bacik 2254faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2255171f6537SJosef Bacik { 2256fc7cbcd4SDavid Sterba int ret; 2257fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 2258fc7cbcd4SDavid Sterba int i; 2259171f6537SJosef Bacik 2260171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2261fc7cbcd4SDavid Sterba gang[0] = list_entry(fs_info->dead_roots.next, 2262171f6537SJosef Bacik struct btrfs_root, root_list); 2263fc7cbcd4SDavid Sterba list_del(&gang[0]->root_list); 2264171f6537SJosef Bacik 2265fc7cbcd4SDavid Sterba if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2266fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2267fc7cbcd4SDavid Sterba btrfs_put_root(gang[0]); 2268171f6537SJosef Bacik } 2269171f6537SJosef Bacik 2270fc7cbcd4SDavid Sterba while (1) { 2271fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2272fc7cbcd4SDavid Sterba (void **)gang, 0, 2273fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 2274fc7cbcd4SDavid Sterba if (!ret) 2275fc7cbcd4SDavid Sterba break; 2276fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 2277fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2278171f6537SJosef Bacik } 2279171f6537SJosef Bacik } 2280af31f5e5SChris Mason 2281638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2282638aa7edSEric Sandeen { 2283638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2284638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2285638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2286638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2287638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2288638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2289ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2290638aa7edSEric Sandeen } 2291638aa7edSEric Sandeen 2292779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2293779a65a4SEric Sandeen { 2294779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2295779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2296779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2297779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2298779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2299779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2300907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2301779a65a4SEric Sandeen } 2302779a65a4SEric Sandeen 23036bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2304f37938e0SEric Sandeen { 23052ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23062ff7e61eSJeff Mahoney 23072ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23082ff7e61eSJeff Mahoney set_nlink(inode, 1); 2309f37938e0SEric Sandeen /* 2310f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2311f37938e0SEric Sandeen * the real end of the address space is determined by all of 2312f37938e0SEric Sandeen * the devices in the system 2313f37938e0SEric Sandeen */ 23142ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23152ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2316f37938e0SEric Sandeen 23172ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 231843eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 23192c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 23207b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 23212ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2322f37938e0SEric Sandeen 23235c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 23242ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 23252ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 23262ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2327f37938e0SEric Sandeen } 2328f37938e0SEric Sandeen 2329ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2330ad618368SEric Sandeen { 2331ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2332129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 23337f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2334ad618368SEric Sandeen } 2335ad618368SEric Sandeen 2336f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2337f9e92e40SEric Sandeen { 2338f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2339f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2340f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2341f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2342f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2343f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2344d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2345f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2346f9e92e40SEric Sandeen } 2347f9e92e40SEric Sandeen 2348d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 23492a458198SEric Sandeen { 2350f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 23516f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23522a458198SEric Sandeen 23532a458198SEric Sandeen fs_info->workers = 2354a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16); 2355a31b4a43SChristoph Hellwig fs_info->hipri_workers = 2356a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker-high", 2357cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23582a458198SEric Sandeen 23592a458198SEric Sandeen fs_info->delalloc_workers = 2360cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2361cb001095SJeff Mahoney flags, max_active, 2); 23622a458198SEric Sandeen 23632a458198SEric Sandeen fs_info->flush_workers = 2364cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2365cb001095SJeff Mahoney flags, max_active, 0); 23662a458198SEric Sandeen 23672a458198SEric Sandeen fs_info->caching_workers = 2368cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23692a458198SEric Sandeen 23702a458198SEric Sandeen fs_info->fixup_workers = 2371cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23722a458198SEric Sandeen 23732a458198SEric Sandeen fs_info->endio_workers = 2374*d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio", flags, max_active); 23752a458198SEric Sandeen fs_info->endio_meta_workers = 2376*d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio-meta", flags, max_active); 23772a458198SEric Sandeen fs_info->endio_raid56_workers = 2378d34e123dSChristoph Hellwig alloc_workqueue("btrfs-endio-raid56", flags, max_active); 2379385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active); 23802a458198SEric Sandeen fs_info->endio_write_workers = 2381cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2382cb001095SJeff Mahoney max_active, 2); 2383fed8a72dSChristoph Hellwig fs_info->compressed_write_workers = 2384fed8a72dSChristoph Hellwig alloc_workqueue("btrfs-compressed-write", flags, max_active); 23852a458198SEric Sandeen fs_info->endio_freespace_worker = 2386cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2387cb001095SJeff Mahoney max_active, 0); 23882a458198SEric Sandeen fs_info->delayed_workers = 2389cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2390cb001095SJeff Mahoney max_active, 0); 23912a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2392cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2393b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2394b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 23952a458198SEric Sandeen 2396a31b4a43SChristoph Hellwig if (!(fs_info->workers && fs_info->hipri_workers && 2397a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers && 23982a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 2399fed8a72dSChristoph Hellwig fs_info->compressed_write_workers && 24002a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24012a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2402f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2403f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2404b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 24052a458198SEric Sandeen return -ENOMEM; 24062a458198SEric Sandeen } 24072a458198SEric Sandeen 24082a458198SEric Sandeen return 0; 24092a458198SEric Sandeen } 24102a458198SEric Sandeen 24116d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 24126d97c6e3SJohannes Thumshirn { 24136d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2414b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 24156d97c6e3SJohannes Thumshirn 2416b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 24176d97c6e3SJohannes Thumshirn 24186d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 24196d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2420b4e967beSDavid Sterba csum_driver); 24216d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 24226d97c6e3SJohannes Thumshirn } 24236d97c6e3SJohannes Thumshirn 24246d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 24256d97c6e3SJohannes Thumshirn 24266d97c6e3SJohannes Thumshirn return 0; 24276d97c6e3SJohannes Thumshirn } 24286d97c6e3SJohannes Thumshirn 242963443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 243063443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 243163443bf5SEric Sandeen { 243263443bf5SEric Sandeen int ret; 243363443bf5SEric Sandeen struct btrfs_root *log_tree_root; 243463443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 243563443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2436581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 243763443bf5SEric Sandeen 243863443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2439f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 244063443bf5SEric Sandeen return -EIO; 244163443bf5SEric Sandeen } 244263443bf5SEric Sandeen 244396dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 244496dfcb46SJosef Bacik GFP_KERNEL); 244563443bf5SEric Sandeen if (!log_tree_root) 244663443bf5SEric Sandeen return -ENOMEM; 244763443bf5SEric Sandeen 24482ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 24491b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 24501b7ec85eSJosef Bacik fs_info->generation + 1, level, 24511b7ec85eSJosef Bacik NULL); 245264c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2453f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24540eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 24558c38938cSJosef Bacik log_tree_root->node = NULL; 245600246528SJosef Bacik btrfs_put_root(log_tree_root); 24570eeff236SLiu Bo return ret; 24584eb150d6SQu Wenruo } 24594eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2460f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 246100246528SJosef Bacik btrfs_put_root(log_tree_root); 246263443bf5SEric Sandeen return -EIO; 246363443bf5SEric Sandeen } 24644eb150d6SQu Wenruo 246563443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 246663443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 246763443bf5SEric Sandeen if (ret) { 24680b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 246963443bf5SEric Sandeen "Failed to recover log tree"); 247000246528SJosef Bacik btrfs_put_root(log_tree_root); 247163443bf5SEric Sandeen return ret; 247263443bf5SEric Sandeen } 247363443bf5SEric Sandeen 2474bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 24756bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 247663443bf5SEric Sandeen if (ret) 247763443bf5SEric Sandeen return ret; 247863443bf5SEric Sandeen } 247963443bf5SEric Sandeen 248063443bf5SEric Sandeen return 0; 248163443bf5SEric Sandeen } 248263443bf5SEric Sandeen 2483abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2484abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2485abed4aaaSJosef Bacik const char *name) 2486abed4aaaSJosef Bacik { 2487abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2488abed4aaaSJosef Bacik struct btrfs_root *root; 2489f7238e50SJosef Bacik u64 max_global_id = 0; 2490abed4aaaSJosef Bacik int ret; 2491abed4aaaSJosef Bacik struct btrfs_key key = { 2492abed4aaaSJosef Bacik .objectid = objectid, 2493abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2494abed4aaaSJosef Bacik .offset = 0, 2495abed4aaaSJosef Bacik }; 2496abed4aaaSJosef Bacik bool found = false; 2497abed4aaaSJosef Bacik 2498abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2499abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2500abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2501abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2502abed4aaaSJosef Bacik return 0; 2503abed4aaaSJosef Bacik } 2504abed4aaaSJosef Bacik 2505abed4aaaSJosef Bacik while (1) { 2506abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2507abed4aaaSJosef Bacik if (ret < 0) 2508abed4aaaSJosef Bacik break; 2509abed4aaaSJosef Bacik 2510abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2511abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2512abed4aaaSJosef Bacik if (ret) { 2513abed4aaaSJosef Bacik if (ret > 0) 2514abed4aaaSJosef Bacik ret = 0; 2515abed4aaaSJosef Bacik break; 2516abed4aaaSJosef Bacik } 2517abed4aaaSJosef Bacik } 2518abed4aaaSJosef Bacik ret = 0; 2519abed4aaaSJosef Bacik 2520abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2521abed4aaaSJosef Bacik if (key.objectid != objectid) 2522abed4aaaSJosef Bacik break; 2523abed4aaaSJosef Bacik btrfs_release_path(path); 2524abed4aaaSJosef Bacik 2525f7238e50SJosef Bacik /* 2526f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2527f7238e50SJosef Bacik * everybody. 2528f7238e50SJosef Bacik */ 2529f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2530f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2531f7238e50SJosef Bacik 2532abed4aaaSJosef Bacik found = true; 2533abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2534abed4aaaSJosef Bacik if (IS_ERR(root)) { 2535abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2536abed4aaaSJosef Bacik ret = PTR_ERR(root); 2537abed4aaaSJosef Bacik break; 2538abed4aaaSJosef Bacik } 2539abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2540abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2541abed4aaaSJosef Bacik if (ret) { 2542abed4aaaSJosef Bacik btrfs_put_root(root); 2543abed4aaaSJosef Bacik break; 2544abed4aaaSJosef Bacik } 2545abed4aaaSJosef Bacik key.offset++; 2546abed4aaaSJosef Bacik } 2547abed4aaaSJosef Bacik btrfs_release_path(path); 2548abed4aaaSJosef Bacik 2549f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2550f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2551f7238e50SJosef Bacik 2552abed4aaaSJosef Bacik if (!found || ret) { 2553abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2554abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2555abed4aaaSJosef Bacik 2556abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2557abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2558abed4aaaSJosef Bacik else 2559abed4aaaSJosef Bacik ret = 0; 2560abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2561abed4aaaSJosef Bacik } 2562abed4aaaSJosef Bacik return ret; 2563abed4aaaSJosef Bacik } 2564abed4aaaSJosef Bacik 2565abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2566abed4aaaSJosef Bacik { 2567abed4aaaSJosef Bacik struct btrfs_path *path; 2568abed4aaaSJosef Bacik int ret = 0; 2569abed4aaaSJosef Bacik 2570abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2571abed4aaaSJosef Bacik if (!path) 2572abed4aaaSJosef Bacik return -ENOMEM; 2573abed4aaaSJosef Bacik 2574abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2575abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2576abed4aaaSJosef Bacik if (ret) 2577abed4aaaSJosef Bacik goto out; 2578abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2579abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2580abed4aaaSJosef Bacik if (ret) 2581abed4aaaSJosef Bacik goto out; 2582abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2583abed4aaaSJosef Bacik goto out; 2584abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2585abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2586abed4aaaSJosef Bacik "free space"); 2587abed4aaaSJosef Bacik out: 2588abed4aaaSJosef Bacik btrfs_free_path(path); 2589abed4aaaSJosef Bacik return ret; 2590abed4aaaSJosef Bacik } 2591abed4aaaSJosef Bacik 25926bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 25934bbcaa64SEric Sandeen { 25946bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2595a4f3d2c4SDavid Sterba struct btrfs_root *root; 25964bbcaa64SEric Sandeen struct btrfs_key location; 25974bbcaa64SEric Sandeen int ret; 25984bbcaa64SEric Sandeen 25996bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 26006bccf3abSJeff Mahoney 2601abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2602abed4aaaSJosef Bacik if (ret) 2603abed4aaaSJosef Bacik return ret; 2604abed4aaaSJosef Bacik 2605abed4aaaSJosef Bacik location.objectid = BTRFS_DEV_TREE_OBJECTID; 26064bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 26074bbcaa64SEric Sandeen location.offset = 0; 26084bbcaa64SEric Sandeen 2609a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2610f50f4353SLiu Bo if (IS_ERR(root)) { 261142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2612f50f4353SLiu Bo ret = PTR_ERR(root); 2613f50f4353SLiu Bo goto out; 2614f50f4353SLiu Bo } 261542437a63SJosef Bacik } else { 2616a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2617a4f3d2c4SDavid Sterba fs_info->dev_root = root; 261842437a63SJosef Bacik } 2619820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2620820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 26214bbcaa64SEric Sandeen 2622aeb935a4SQu Wenruo /* 2623aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2624aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2625aeb935a4SQu Wenruo */ 262656e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 262756e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2628aeb935a4SQu Wenruo if (IS_ERR(root)) { 262942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2630aeb935a4SQu Wenruo ret = PTR_ERR(root); 2631aeb935a4SQu Wenruo goto out; 2632aeb935a4SQu Wenruo } 263342437a63SJosef Bacik } else { 2634aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2635aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 263642437a63SJosef Bacik } 2637aeb935a4SQu Wenruo 26384bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2639a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2640a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2641a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2642afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2643a4f3d2c4SDavid Sterba fs_info->quota_root = root; 26444bbcaa64SEric Sandeen } 26454bbcaa64SEric Sandeen 26464bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2647a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2648a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 264942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2650a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 26514bbcaa64SEric Sandeen if (ret != -ENOENT) 2652f50f4353SLiu Bo goto out; 265342437a63SJosef Bacik } 26544bbcaa64SEric Sandeen } else { 2655a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2656a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 26574bbcaa64SEric Sandeen } 26584bbcaa64SEric Sandeen 26594bbcaa64SEric Sandeen return 0; 2660f50f4353SLiu Bo out: 2661f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2662f50f4353SLiu Bo location.objectid, ret); 2663f50f4353SLiu Bo return ret; 26644bbcaa64SEric Sandeen } 26654bbcaa64SEric Sandeen 2666069ec957SQu Wenruo /* 2667069ec957SQu Wenruo * Real super block validation 2668069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2669069ec957SQu Wenruo * 2670069ec957SQu Wenruo * @sb: super block to check 2671069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2672069ec957SQu Wenruo * 0 the primary (1st) sb 2673069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2674069ec957SQu Wenruo * -1 skip bytenr check 2675069ec957SQu Wenruo */ 2676069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2677069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 267821a852b0SQu Wenruo { 267921a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 268021a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 268121a852b0SQu Wenruo int ret = 0; 268221a852b0SQu Wenruo 268321a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 268421a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 268521a852b0SQu Wenruo ret = -EINVAL; 268621a852b0SQu Wenruo } 268721a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 268821a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 268921a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 269021a852b0SQu Wenruo ret = -EINVAL; 269121a852b0SQu Wenruo } 269221a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 269321a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 269421a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 269521a852b0SQu Wenruo ret = -EINVAL; 269621a852b0SQu Wenruo } 269721a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 269821a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 269921a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 270021a852b0SQu Wenruo ret = -EINVAL; 270121a852b0SQu Wenruo } 270221a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 270321a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 270421a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 270521a852b0SQu Wenruo ret = -EINVAL; 270621a852b0SQu Wenruo } 270721a852b0SQu Wenruo 270821a852b0SQu Wenruo /* 270921a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 271021a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 271121a852b0SQu Wenruo */ 271221a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 271321a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 271421a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 271521a852b0SQu Wenruo ret = -EINVAL; 271621a852b0SQu Wenruo } 27170bb3eb3eSQu Wenruo 27180bb3eb3eSQu Wenruo /* 27191a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 27201a42daabSQu Wenruo * 27211a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 27221a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 27231a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 27241a42daabSQu Wenruo * beginning. 27250bb3eb3eSQu Wenruo */ 27261a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 272721a852b0SQu Wenruo btrfs_err(fs_info, 27280bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 272921a852b0SQu Wenruo sectorsize, PAGE_SIZE); 273021a852b0SQu Wenruo ret = -EINVAL; 273121a852b0SQu Wenruo } 27320bb3eb3eSQu Wenruo 273321a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 273421a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 273521a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 273621a852b0SQu Wenruo ret = -EINVAL; 273721a852b0SQu Wenruo } 273821a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 273921a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 274021a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 274121a852b0SQu Wenruo ret = -EINVAL; 274221a852b0SQu Wenruo } 274321a852b0SQu Wenruo 274421a852b0SQu Wenruo /* Root alignment check */ 274521a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 274621a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 274721a852b0SQu Wenruo btrfs_super_root(sb)); 274821a852b0SQu Wenruo ret = -EINVAL; 274921a852b0SQu Wenruo } 275021a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 275121a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 275221a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 275321a852b0SQu Wenruo ret = -EINVAL; 275421a852b0SQu Wenruo } 275521a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 275621a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 275721a852b0SQu Wenruo btrfs_super_log_root(sb)); 275821a852b0SQu Wenruo ret = -EINVAL; 275921a852b0SQu Wenruo } 276021a852b0SQu Wenruo 2761aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2762aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2763aefd7f70SNikolay Borisov btrfs_err(fs_info, 2764aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2765aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2766aefd7f70SNikolay Borisov ret = -EINVAL; 2767aefd7f70SNikolay Borisov } 2768aefd7f70SNikolay Borisov 2769aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2770aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2771aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2772aefd7f70SNikolay Borisov btrfs_err(fs_info, 2773aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2774aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2775aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2776aefd7f70SNikolay Borisov ret = -EINVAL; 2777aefd7f70SNikolay Borisov } 2778aefd7f70SNikolay Borisov 2779de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 27807239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 278121a852b0SQu Wenruo btrfs_err(fs_info, 27827239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2783de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 278421a852b0SQu Wenruo ret = -EINVAL; 278521a852b0SQu Wenruo } 278621a852b0SQu Wenruo 278721a852b0SQu Wenruo /* 278821a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 278921a852b0SQu Wenruo * done later 279021a852b0SQu Wenruo */ 279121a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 279221a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 279321a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 279421a852b0SQu Wenruo ret = -EINVAL; 279521a852b0SQu Wenruo } 279621a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 279721a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 279821a852b0SQu Wenruo btrfs_super_stripesize(sb)); 279921a852b0SQu Wenruo ret = -EINVAL; 280021a852b0SQu Wenruo } 280121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 280221a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 280321a852b0SQu Wenruo btrfs_super_num_devices(sb)); 280421a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 280521a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 280621a852b0SQu Wenruo ret = -EINVAL; 280721a852b0SQu Wenruo } 280821a852b0SQu Wenruo 2809069ec957SQu Wenruo if (mirror_num >= 0 && 2810069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 281121a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 281221a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 281321a852b0SQu Wenruo ret = -EINVAL; 281421a852b0SQu Wenruo } 281521a852b0SQu Wenruo 281621a852b0SQu Wenruo /* 281721a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 281821a852b0SQu Wenruo * and one chunk 281921a852b0SQu Wenruo */ 282021a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 282121a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 282221a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 282321a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 282421a852b0SQu Wenruo ret = -EINVAL; 282521a852b0SQu Wenruo } 282621a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 282721a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 282821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 282921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 283021a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 283121a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 283221a852b0SQu Wenruo ret = -EINVAL; 283321a852b0SQu Wenruo } 283421a852b0SQu Wenruo 283521a852b0SQu Wenruo /* 283621a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 283721a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 283821a852b0SQu Wenruo */ 283921a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 284021a852b0SQu Wenruo btrfs_warn(fs_info, 284121a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 284221a852b0SQu Wenruo btrfs_super_generation(sb), 284321a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 284421a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 284521a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 284621a852b0SQu Wenruo btrfs_warn(fs_info, 284721a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 284821a852b0SQu Wenruo btrfs_super_generation(sb), 284921a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 285021a852b0SQu Wenruo 285121a852b0SQu Wenruo return ret; 285221a852b0SQu Wenruo } 285321a852b0SQu Wenruo 2854069ec957SQu Wenruo /* 2855069ec957SQu Wenruo * Validation of super block at mount time. 2856069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2857069ec957SQu Wenruo * flags will be skipped. 2858069ec957SQu Wenruo */ 2859069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2860069ec957SQu Wenruo { 2861069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2862069ec957SQu Wenruo } 2863069ec957SQu Wenruo 286475cb857dSQu Wenruo /* 286575cb857dSQu Wenruo * Validation of super block at write time. 286675cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 286775cb857dSQu Wenruo * overwritten soon. 286875cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 286975cb857dSQu Wenruo */ 287075cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 287175cb857dSQu Wenruo struct btrfs_super_block *sb) 287275cb857dSQu Wenruo { 287375cb857dSQu Wenruo int ret; 287475cb857dSQu Wenruo 287575cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 287675cb857dSQu Wenruo if (ret < 0) 287775cb857dSQu Wenruo goto out; 2878e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 287975cb857dSQu Wenruo ret = -EUCLEAN; 288075cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 288175cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 288275cb857dSQu Wenruo goto out; 288375cb857dSQu Wenruo } 288475cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 288575cb857dSQu Wenruo ret = -EUCLEAN; 288675cb857dSQu Wenruo btrfs_err(fs_info, 288775cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 288875cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 288975cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 289075cb857dSQu Wenruo goto out; 289175cb857dSQu Wenruo } 289275cb857dSQu Wenruo out: 289375cb857dSQu Wenruo if (ret < 0) 289475cb857dSQu Wenruo btrfs_err(fs_info, 289575cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 289675cb857dSQu Wenruo return ret; 289775cb857dSQu Wenruo } 289875cb857dSQu Wenruo 2899bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2900bd676446SJosef Bacik { 2901bd676446SJosef Bacik int ret = 0; 2902bd676446SJosef Bacik 2903bd676446SJosef Bacik root->node = read_tree_block(root->fs_info, bytenr, 2904bd676446SJosef Bacik root->root_key.objectid, gen, level, NULL); 2905bd676446SJosef Bacik if (IS_ERR(root->node)) { 2906bd676446SJosef Bacik ret = PTR_ERR(root->node); 2907bd676446SJosef Bacik root->node = NULL; 29084eb150d6SQu Wenruo return ret; 29094eb150d6SQu Wenruo } 29104eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 2911bd676446SJosef Bacik free_extent_buffer(root->node); 2912bd676446SJosef Bacik root->node = NULL; 29134eb150d6SQu Wenruo return -EIO; 2914bd676446SJosef Bacik } 2915bd676446SJosef Bacik 2916bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 2917bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 2918bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 2919bd676446SJosef Bacik return ret; 2920bd676446SJosef Bacik } 2921bd676446SJosef Bacik 2922bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 2923bd676446SJosef Bacik { 2924bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 2925bd676446SJosef Bacik u64 gen, bytenr; 2926bd676446SJosef Bacik int level, ret; 2927bd676446SJosef Bacik 2928bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 2929bd676446SJosef Bacik gen = btrfs_super_generation(sb); 2930bd676446SJosef Bacik level = btrfs_super_root_level(sb); 2931bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 29329c54e80dSJosef Bacik if (ret) { 2933bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 2934bd676446SJosef Bacik return ret; 2935bd676446SJosef Bacik } 2936bd676446SJosef Bacik 29379c54e80dSJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 29389c54e80dSJosef Bacik return 0; 29399c54e80dSJosef Bacik 29409c54e80dSJosef Bacik bytenr = btrfs_super_block_group_root(sb); 29419c54e80dSJosef Bacik gen = btrfs_super_block_group_root_generation(sb); 29429c54e80dSJosef Bacik level = btrfs_super_block_group_root_level(sb); 29439c54e80dSJosef Bacik ret = load_super_root(fs_info->block_group_root, bytenr, gen, level); 29449c54e80dSJosef Bacik if (ret) 29459c54e80dSJosef Bacik btrfs_warn(fs_info, "couldn't read block group root"); 29469c54e80dSJosef Bacik return ret; 29479c54e80dSJosef Bacik } 29489c54e80dSJosef Bacik 29496ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2950b8522a1eSNikolay Borisov { 29516ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2952b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2953b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2954b8522a1eSNikolay Borisov bool handle_error = false; 2955b8522a1eSNikolay Borisov int ret = 0; 2956b8522a1eSNikolay Borisov int i; 2957b8522a1eSNikolay Borisov 29589c54e80dSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 29599c54e80dSJosef Bacik struct btrfs_root *root; 2960b8522a1eSNikolay Borisov 29619c54e80dSJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID, 29629c54e80dSJosef Bacik GFP_KERNEL); 29639c54e80dSJosef Bacik if (!root) 29649c54e80dSJosef Bacik return -ENOMEM; 29659c54e80dSJosef Bacik fs_info->block_group_root = root; 29669c54e80dSJosef Bacik } 29679c54e80dSJosef Bacik 2968b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2969b8522a1eSNikolay Borisov if (handle_error) { 2970b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2971b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2972b8522a1eSNikolay Borisov tree_root->node = NULL; 2973b8522a1eSNikolay Borisov 2974b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2975b8522a1eSNikolay Borisov break; 2976b8522a1eSNikolay Borisov 2977b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2978b8522a1eSNikolay Borisov 2979b8522a1eSNikolay Borisov /* 2980b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2981b8522a1eSNikolay Borisov * valid 2982b8522a1eSNikolay Borisov */ 2983b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2984b8522a1eSNikolay Borisov 2985b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2986b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2987b8522a1eSNikolay Borisov 2988b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 29896ef108ddSNikolay Borisov backup_index = ret; 2990b8522a1eSNikolay Borisov if (ret < 0) 2991b8522a1eSNikolay Borisov return ret; 2992b8522a1eSNikolay Borisov } 2993b8522a1eSNikolay Borisov 2994bd676446SJosef Bacik ret = load_important_roots(fs_info); 2995bd676446SJosef Bacik if (ret) { 2996217f5004SNikolay Borisov handle_error = true; 2997b8522a1eSNikolay Borisov continue; 2998b8522a1eSNikolay Borisov } 2999b8522a1eSNikolay Borisov 3000336a0d8dSNikolay Borisov /* 3001336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3002336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3003336a0d8dSNikolay Borisov */ 3004453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3005b8522a1eSNikolay Borisov if (ret < 0) { 3006b8522a1eSNikolay Borisov handle_error = true; 3007b8522a1eSNikolay Borisov continue; 3008b8522a1eSNikolay Borisov } 3009b8522a1eSNikolay Borisov 30106b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3011b8522a1eSNikolay Borisov 3012b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3013b8522a1eSNikolay Borisov if (ret < 0) { 3014b8522a1eSNikolay Borisov handle_error = true; 3015b8522a1eSNikolay Borisov continue; 3016b8522a1eSNikolay Borisov } 3017b8522a1eSNikolay Borisov 3018b8522a1eSNikolay Borisov /* All successful */ 3019bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3020bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3021d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 30226ef108ddSNikolay Borisov 30236ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 30246ef108ddSNikolay Borisov if (backup_index < 0) { 30256ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 30266ef108ddSNikolay Borisov } else { 30276ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 30286ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 30296ef108ddSNikolay Borisov } 3030b8522a1eSNikolay Borisov break; 3031b8522a1eSNikolay Borisov } 3032b8522a1eSNikolay Borisov 3033b8522a1eSNikolay Borisov return ret; 3034b8522a1eSNikolay Borisov } 3035b8522a1eSNikolay Borisov 30368260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3037eb60ceacSChris Mason { 3038fc7cbcd4SDavid Sterba INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 303901cd3909SDavid Sterba INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 30408fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3041facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 304224bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3043eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 304411833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3045eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3046a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 3047fc7cbcd4SDavid Sterba spin_lock_init(&fs_info->fs_roots_radix_lock); 304824bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 30494cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3050ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3051f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 305247ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 305340ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3054afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3055c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3056f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3057abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3058d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3059f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 30607585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3061573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 30620bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 30635f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3064de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 306519c00ddcSChris Mason 30660b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 30676324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3068f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 306947ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 307018bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3071afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3072bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3073bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 30743fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 30753fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3076bd647ce3SJosef Bacik #endif 3077c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 307866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 307966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 308066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 308166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 308266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 308366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 308466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3085ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3086ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3087ba2c4d4eSJosef Bacik 3088771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 30894cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3090034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3091fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3092abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 309395ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 30949ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 30954cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3096a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3097f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 30988b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3099f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3100fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3101c8b97818SChris Mason 3102b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3103b34b086cSChris Mason num_online_cpus() + 2, 8); 31040afbaf8cSChris Mason 3105199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3106199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 310769fe2d75SJosef Bacik 3108638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 310921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 311021adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 311121adbd5cSStefan Behrens #endif 3112779a65a4SEric Sandeen btrfs_init_balance(fs_info); 311357056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3114a2de733cSArne Jansen 311516b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock); 311608dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED; 31170f9dd46cSJosef Bacik 3118fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 3119fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 312039279cc3SChris Mason 31215a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3122e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3123925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3124a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3125a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 31261bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 31279e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3128c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 312976dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3130803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3131fa9c0d79SChris Mason 3132ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3133f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3134b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3135416ac51dSArne Jansen 3136fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3137fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3138fa9c0d79SChris Mason 3139e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3140f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3141bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 31424854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3143034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 31443768f368SChris Mason 3145da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3146da17066cSJeff Mahoney fs_info->nodesize = 4096; 3147da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3148ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3149da17066cSJeff Mahoney fs_info->stripesize = 4096; 3150da17066cSJeff Mahoney 3151eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3152eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3153eede2bf3SOmar Sandoval 315418bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 315518bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 31568260edbaSJosef Bacik } 31578260edbaSJosef Bacik 31588260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 31598260edbaSJosef Bacik { 31608260edbaSJosef Bacik int ret; 31618260edbaSJosef Bacik 31628260edbaSJosef Bacik fs_info->sb = sb; 31638260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 31648260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 31659e967495SFilipe Manana 31665deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3167ae18c37aSJosef Bacik if (ret) 3168ae18c37aSJosef Bacik return ret; 3169ae18c37aSJosef Bacik 3170ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3171ae18c37aSJosef Bacik if (ret) 3172c75e8394SJosef Bacik return ret; 3173ae18c37aSJosef Bacik 3174ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3175ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3176ae18c37aSJosef Bacik 3177ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3178ae18c37aSJosef Bacik if (ret) 3179c75e8394SJosef Bacik return ret; 3180ae18c37aSJosef Bacik 3181ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3182ae18c37aSJosef Bacik GFP_KERNEL); 3183ae18c37aSJosef Bacik if (ret) 3184c75e8394SJosef Bacik return ret; 3185ae18c37aSJosef Bacik 3186ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3187ae18c37aSJosef Bacik GFP_KERNEL); 3188c75e8394SJosef Bacik if (!fs_info->delayed_root) 3189c75e8394SJosef Bacik return -ENOMEM; 3190ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3191ae18c37aSJosef Bacik 3192a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3193a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3194a0a1db70SFilipe Manana 3195c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3196ae18c37aSJosef Bacik } 3197ae18c37aSJosef Bacik 319897f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 319997f4dd09SNikolay Borisov { 32000d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 320197f4dd09SNikolay Borisov int ret; 320297f4dd09SNikolay Borisov 320397f4dd09SNikolay Borisov /* 320497f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 320597f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 320697f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 320797f4dd09SNikolay Borisov */ 320897f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 320997f4dd09SNikolay Borisov if (ret < 0) { 3210c94bec2cSJosef Bacik if (ret != -EINTR) 3211c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3212c94bec2cSJosef Bacik ret); 321397f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 321497f4dd09SNikolay Borisov return ret; 321597f4dd09SNikolay Borisov } 321697f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 321797f4dd09SNikolay Borisov } 321897f4dd09SNikolay Borisov 321997f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 322097f4dd09SNikolay Borisov { 322197f4dd09SNikolay Borisov struct task_struct *task; 322297f4dd09SNikolay Borisov 322397f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 322497f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 322597f4dd09SNikolay Borisov if (IS_ERR(task)) { 322697f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 322797f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 322897f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 322997f4dd09SNikolay Borisov return PTR_ERR(task); 323097f4dd09SNikolay Borisov } 323197f4dd09SNikolay Borisov 323297f4dd09SNikolay Borisov return 0; 323397f4dd09SNikolay Borisov } 323497f4dd09SNikolay Borisov 323544c0ca21SBoris Burkov /* 32368cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 32378cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 32388cd29088SBoris Burkov * code paths. 32398cd29088SBoris Burkov */ 32408cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 32418cd29088SBoris Burkov { 32428cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32438b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 32448cd29088SBoris Burkov } 32458cd29088SBoris Burkov 32468cd29088SBoris Burkov /* 324744c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 324844c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 324944c0ca21SBoris Burkov */ 325044c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 325144c0ca21SBoris Burkov { 325244c0ca21SBoris Burkov int ret; 325394846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 32548b228324SBoris Burkov bool clear_free_space_tree = false; 32558b228324SBoris Burkov 32568b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 32578b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32588b228324SBoris Burkov clear_free_space_tree = true; 32598b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32608b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32618b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 32628b228324SBoris Burkov clear_free_space_tree = true; 32638b228324SBoris Burkov } 32648b228324SBoris Burkov 32658b228324SBoris Burkov if (clear_free_space_tree) { 32668b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 32678b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 32688b228324SBoris Burkov if (ret) { 32698b228324SBoris Burkov btrfs_warn(fs_info, 32708b228324SBoris Burkov "failed to clear free space tree: %d", ret); 32718b228324SBoris Burkov goto out; 32728b228324SBoris Burkov } 32738b228324SBoris Burkov } 327444c0ca21SBoris Burkov 32758d488a8cSFilipe Manana /* 32768d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 32778d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 3278fc7cbcd4SDavid Sterba * them into the fs_info->fs_roots_radix tree. This must be done before 32798d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 32808d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 32818d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 32828d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 32838d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 32848d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 32858d488a8cSFilipe Manana */ 32868d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 32878d488a8cSFilipe Manana if (ret) 32888d488a8cSFilipe Manana goto out; 32898d488a8cSFilipe Manana 329044c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 329144c0ca21SBoris Burkov if (ret) 329244c0ca21SBoris Burkov goto out; 329344c0ca21SBoris Burkov 32948f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 32958f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 32968f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 32978f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 32988f1c21d7SBoris Burkov goto out; 32998f1c21d7SBoris Burkov } 33008f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33018f1c21d7SBoris Burkov 330244c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 33037eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 330444c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 330544c0ca21SBoris Burkov if (ret < 0) { 330644c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 330744c0ca21SBoris Burkov goto out; 330844c0ca21SBoris Burkov } 330944c0ca21SBoris Burkov 33105011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 33115011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33125011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 33135011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 33145011139aSBoris Burkov if (ret) { 33155011139aSBoris Burkov btrfs_warn(fs_info, 33165011139aSBoris Burkov "failed to create free space tree: %d", ret); 33175011139aSBoris Burkov goto out; 33185011139aSBoris Burkov } 33195011139aSBoris Burkov } 33205011139aSBoris Burkov 332194846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 332294846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 332394846229SBoris Burkov if (ret) 332494846229SBoris Burkov goto out; 332594846229SBoris Burkov } 332694846229SBoris Burkov 332744c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 332844c0ca21SBoris Burkov if (ret) 332944c0ca21SBoris Burkov goto out; 333044c0ca21SBoris Burkov 333144c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 333244c0ca21SBoris Burkov if (ret) { 333344c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 333444c0ca21SBoris Burkov goto out; 333544c0ca21SBoris Burkov } 333644c0ca21SBoris Burkov 333744c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 333844c0ca21SBoris Burkov 333944c0ca21SBoris Burkov if (!fs_info->uuid_root) { 334044c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 334144c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 334244c0ca21SBoris Burkov if (ret) { 334344c0ca21SBoris Burkov btrfs_warn(fs_info, 334444c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 334544c0ca21SBoris Burkov goto out; 334644c0ca21SBoris Burkov } 334744c0ca21SBoris Burkov } 334844c0ca21SBoris Burkov 334944c0ca21SBoris Burkov out: 335044c0ca21SBoris Burkov return ret; 335144c0ca21SBoris Burkov } 335244c0ca21SBoris Burkov 3353ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3354ae18c37aSJosef Bacik char *options) 3355ae18c37aSJosef Bacik { 3356ae18c37aSJosef Bacik u32 sectorsize; 3357ae18c37aSJosef Bacik u32 nodesize; 3358ae18c37aSJosef Bacik u32 stripesize; 3359ae18c37aSJosef Bacik u64 generation; 3360ae18c37aSJosef Bacik u64 features; 3361ae18c37aSJosef Bacik u16 csum_type; 3362ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3363ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3364ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3365ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3366ae18c37aSJosef Bacik int ret; 3367ae18c37aSJosef Bacik int err = -EINVAL; 3368ae18c37aSJosef Bacik int level; 3369ae18c37aSJosef Bacik 33708260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 337153b381b3SDavid Woodhouse if (ret) { 337283c8266aSDavid Sterba err = ret; 3373ae18c37aSJosef Bacik goto fail; 337453b381b3SDavid Woodhouse } 337553b381b3SDavid Woodhouse 3376ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3377ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3378ae18c37aSJosef Bacik GFP_KERNEL); 3379ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3380ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3381ae18c37aSJosef Bacik GFP_KERNEL); 3382ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3383ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3384ae18c37aSJosef Bacik err = -ENOMEM; 3385c75e8394SJosef Bacik goto fail; 3386ae18c37aSJosef Bacik } 3387ae18c37aSJosef Bacik 3388ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3389ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3390ae18c37aSJosef Bacik err = -ENOMEM; 3391c75e8394SJosef Bacik goto fail; 3392ae18c37aSJosef Bacik } 3393ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3394ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3395ae18c37aSJosef Bacik 3396d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 33971104a885SDavid Sterba 33981104a885SDavid Sterba /* 33991104a885SDavid Sterba * Read super block and check the signature bytes only 34001104a885SDavid Sterba */ 3401d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 34028f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 34038f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 340416cdcec7SMiao Xie goto fail_alloc; 340520b45077SDave Young } 340639279cc3SChris Mason 34071104a885SDavid Sterba /* 3408260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 34098dc3f22cSJohannes Thumshirn * corrupted, we'll find out 34108dc3f22cSJohannes Thumshirn */ 34118f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 341251bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 34138dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 341451bce6c9SJohannes Thumshirn csum_type); 34158dc3f22cSJohannes Thumshirn err = -EINVAL; 34168f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34178dc3f22cSJohannes Thumshirn goto fail_alloc; 34188dc3f22cSJohannes Thumshirn } 34198dc3f22cSJohannes Thumshirn 342083c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 342183c68bbcSSu Yue 34226d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 34236d97c6e3SJohannes Thumshirn if (ret) { 34246d97c6e3SJohannes Thumshirn err = ret; 34258f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34266d97c6e3SJohannes Thumshirn goto fail_alloc; 34276d97c6e3SJohannes Thumshirn } 34286d97c6e3SJohannes Thumshirn 34298dc3f22cSJohannes Thumshirn /* 34301104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 34311104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 34321104a885SDavid Sterba */ 34338f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 343405135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 34351104a885SDavid Sterba err = -EINVAL; 34368f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3437141386e1SJosef Bacik goto fail_alloc; 34381104a885SDavid Sterba } 34391104a885SDavid Sterba 34401104a885SDavid Sterba /* 34411104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 34421104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 34431104a885SDavid Sterba * the whole block of INFO_SIZE 34441104a885SDavid Sterba */ 34458f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 34468f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34475f39d397SChris Mason 3448fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3449fbc6feaeSNikolay Borisov 34500b86a832SChris Mason 3451fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3452fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3453fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3454fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3455fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3456fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3457fbc6feaeSNikolay Borisov } 3458fbc6feaeSNikolay Borisov 3459fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3460fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3461de37aa51SNikolay Borisov 3462069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 34631104a885SDavid Sterba if (ret) { 346405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 34651104a885SDavid Sterba err = -EINVAL; 3466141386e1SJosef Bacik goto fail_alloc; 34671104a885SDavid Sterba } 34681104a885SDavid Sterba 34690f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3470141386e1SJosef Bacik goto fail_alloc; 34710f7d52f4SChris Mason 3472acce952bSliubo /* check FS state, whether FS is broken. */ 347387533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 347487533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3475acce952bSliubo 347675e7cb7fSLiu Bo /* 347775e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 347875e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 347975e7cb7fSLiu Bo */ 348075e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 348175e7cb7fSLiu Bo 34826f93e834SAnand Jain /* 34836f93e834SAnand Jain * Flag our filesystem as having big metadata blocks if they are bigger 34846f93e834SAnand Jain * than the page size. 34856f93e834SAnand Jain */ 34866f93e834SAnand Jain if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 34876f93e834SAnand Jain if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 34886f93e834SAnand Jain btrfs_info(fs_info, 34896f93e834SAnand Jain "flagging fs with big metadata feature"); 34906f93e834SAnand Jain features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 34916f93e834SAnand Jain } 34926f93e834SAnand Jain 34936f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 34946f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 34956f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 34966f93e834SAnand Jain stripesize = sectorsize; 34976f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 34986f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 34996f93e834SAnand Jain 35006f93e834SAnand Jain fs_info->nodesize = nodesize; 35016f93e834SAnand Jain fs_info->sectorsize = sectorsize; 35026f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 35036f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 35046f93e834SAnand Jain fs_info->stripesize = stripesize; 35056f93e834SAnand Jain 35062ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 35072b82032cSYan Zheng if (ret) { 35082b82032cSYan Zheng err = ret; 3509141386e1SJosef Bacik goto fail_alloc; 35102b82032cSYan Zheng } 3511dfe25020SChris Mason 3512f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3513f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3514f2b636e8SJosef Bacik if (features) { 351505135f59SDavid Sterba btrfs_err(fs_info, 3516d5321a0fSQu Wenruo "cannot mount because of unsupported optional features (0x%llx)", 3517c1c9ff7cSGeert Uytterhoeven features); 3518f2b636e8SJosef Bacik err = -EINVAL; 3519141386e1SJosef Bacik goto fail_alloc; 3520f2b636e8SJosef Bacik } 3521f2b636e8SJosef Bacik 35225d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 35235d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 35240b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3525a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 35265c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 35275c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3528727011e0SChris Mason 35293173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 353005135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 35313173a18fSJosef Bacik 3532727011e0SChris Mason /* 3533bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3534bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3535bc3f116fSChris Mason */ 3536bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3537707e8a07SDavid Sterba (sectorsize != nodesize)) { 353805135f59SDavid Sterba btrfs_err(fs_info, 353905135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 354005135f59SDavid Sterba nodesize, sectorsize); 3541141386e1SJosef Bacik goto fail_alloc; 3542bc3f116fSChris Mason } 3543bc3f116fSChris Mason 3544ceda0864SMiao Xie /* 3545ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3546ceda0864SMiao Xie * update the flag. 3547ceda0864SMiao Xie */ 35485d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 35495d4f98a2SYan Zheng 3550f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3551f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3552bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 355305135f59SDavid Sterba btrfs_err(fs_info, 3554d5321a0fSQu Wenruo "cannot mount read-write because of unsupported optional features (0x%llx)", 3555c1c9ff7cSGeert Uytterhoeven features); 3556f2b636e8SJosef Bacik err = -EINVAL; 3557141386e1SJosef Bacik goto fail_alloc; 3558f2b636e8SJosef Bacik } 355961d92c32SChris Mason 35608481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 35618481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 35628481dd80SQu Wenruo 35639f73f1aeSQu Wenruo /* 35649f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 35659f73f1aeSQu Wenruo * going to be deprecated. 35669f73f1aeSQu Wenruo * 35679f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 35689f73f1aeSQu Wenruo */ 35699f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 35709f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 35719f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 35729f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 35739f73f1aeSQu Wenruo 357495ea0486SQu Wenruo btrfs_warn(fs_info, 357595ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 35760bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 35778481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 35788481dd80SQu Wenruo if (!subpage_info) 35798481dd80SQu Wenruo goto fail_alloc; 35808481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 35818481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3582c8050b3bSQu Wenruo } 35830bb3eb3eSQu Wenruo 3584d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 35852a458198SEric Sandeen if (ret) { 35862a458198SEric Sandeen err = ret; 35870dc3b84aSJosef Bacik goto fail_sb_buffer; 35880dc3b84aSJosef Bacik } 35894543df7eSChris Mason 35909e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 35919e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 35924575c9ccSChris Mason 3593a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3594a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3595de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3596db94535dSChris Mason 3597925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 35986bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3599925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 360084eed90fSChris Mason if (ret) { 360105135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36025d4f98a2SYan Zheng goto fail_sb_buffer; 360384eed90fSChris Mason } 36040b86a832SChris Mason 360584234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3606581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3607bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3608bd676446SJosef Bacik generation, level); 3609bd676446SJosef Bacik if (ret) { 361005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3611af31f5e5SChris Mason goto fail_tree_roots; 361283121942SDavid Woodhouse } 36130b86a832SChris Mason 3614e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3615c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3616c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3617e17cade2SChris Mason 36185b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 36192b82032cSYan Zheng if (ret) { 362005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3621af31f5e5SChris Mason goto fail_tree_roots; 36222b82032cSYan Zheng } 36230b86a832SChris Mason 36248dabb742SStefan Behrens /* 3625bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3626bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3627bacce86aSAnand Jain * target is required. So free devices that are not part of this 36281a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3629bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 36308dabb742SStefan Behrens */ 3631bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3632d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 363305135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3634a6b0d5c8SChris Mason goto fail_tree_roots; 3635a6b0d5c8SChris Mason } 3636a6b0d5c8SChris Mason 3637b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 36384bbcaa64SEric Sandeen if (ret) 3639b8522a1eSNikolay Borisov goto fail_tree_roots; 36408929ecfaSYan, Zheng 364175ec1db8SJosef Bacik /* 364273651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 364373651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 364473651042SNaohiro Aota * zoned incompat feature flag set. 364573651042SNaohiro Aota */ 364673651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 364773651042SNaohiro Aota if (ret) { 364873651042SNaohiro Aota btrfs_err(fs_info, 364973651042SNaohiro Aota "zoned: failed to read device zone info: %d", 365073651042SNaohiro Aota ret); 365173651042SNaohiro Aota goto fail_block_groups; 365273651042SNaohiro Aota } 365373651042SNaohiro Aota 365473651042SNaohiro Aota /* 365575ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 365675ec1db8SJosef Bacik * check the generation here so we can set the 365775ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 365875ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 365975ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 366075ec1db8SJosef Bacik * even though it was perfectly fine. 366175ec1db8SJosef Bacik */ 366275ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 366375ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 366475ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 366575ec1db8SJosef Bacik 3666cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3667cf90d884SQu Wenruo if (ret) { 3668cf90d884SQu Wenruo btrfs_err(fs_info, 3669cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3670cf90d884SQu Wenruo ret); 3671cf90d884SQu Wenruo goto fail_block_groups; 3672cf90d884SQu Wenruo } 367368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 367468310a5eSIlya Dryomov if (ret) { 367505135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 367668310a5eSIlya Dryomov goto fail_block_groups; 367768310a5eSIlya Dryomov } 367868310a5eSIlya Dryomov 3679733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3680733f4fbbSStefan Behrens if (ret) { 368105135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3682733f4fbbSStefan Behrens goto fail_block_groups; 3683733f4fbbSStefan Behrens } 3684733f4fbbSStefan Behrens 36858dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 36868dabb742SStefan Behrens if (ret) { 368705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 36888dabb742SStefan Behrens goto fail_block_groups; 36898dabb742SStefan Behrens } 36908dabb742SStefan Behrens 3691b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3692b70f5097SNaohiro Aota if (ret) { 3693b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3694b70f5097SNaohiro Aota ret); 3695b70f5097SNaohiro Aota goto fail_block_groups; 3696b70f5097SNaohiro Aota } 3697b70f5097SNaohiro Aota 3698c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3699b7c35e81SAnand Jain if (ret) { 370005135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 370105135f59SDavid Sterba ret); 3702b7c35e81SAnand Jain goto fail_block_groups; 3703b7c35e81SAnand Jain } 3704b7c35e81SAnand Jain 370596f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 37065ac1d209SJeff Mahoney if (ret) { 370705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3708b7c35e81SAnand Jain goto fail_fsdev_sysfs; 37095ac1d209SJeff Mahoney } 37105ac1d209SJeff Mahoney 3711c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3712c59021f8Sliubo if (ret) { 371305135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 37142365dd3cSAnand Jain goto fail_sysfs; 3715c59021f8Sliubo } 3716c59021f8Sliubo 37175b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 37181b1d1f66SJosef Bacik if (ret) { 371905135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 37202365dd3cSAnand Jain goto fail_sysfs; 37211b1d1f66SJosef Bacik } 37224330e183SQu Wenruo 372316beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 372416beac87SNaohiro Aota 37255c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 37265c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 372705135f59SDavid Sterba btrfs_warn(fs_info, 372852042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 37292365dd3cSAnand Jain goto fail_sysfs; 3730292fd7fcSStefan Behrens } 37319078a3e1SChris Mason 373233c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3733a74a4b97SChris Mason "btrfs-cleaner"); 373457506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 37352365dd3cSAnand Jain goto fail_sysfs; 3736a74a4b97SChris Mason 3737a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3738a74a4b97SChris Mason tree_root, 3739a74a4b97SChris Mason "btrfs-transaction"); 374057506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 37413f157a2fSChris Mason goto fail_cleaner; 3742a74a4b97SChris Mason 3743583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3744c289811cSChris Mason !fs_info->fs_devices->rotating) { 3745583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3746c289811cSChris Mason } 3747c289811cSChris Mason 3748572d9ab7SDavid Sterba /* 374901327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3750572d9ab7SDavid Sterba * not have to wait for transaction commit 3751572d9ab7SDavid Sterba */ 3752572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 37533818aea2SQu Wenruo 375421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 37550b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 37562ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 37570b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3758cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 375921adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 376021adbd5cSStefan Behrens if (ret) 376105135f59SDavid Sterba btrfs_warn(fs_info, 376205135f59SDavid Sterba "failed to initialize integrity check module: %d", 376305135f59SDavid Sterba ret); 376421adbd5cSStefan Behrens } 376521adbd5cSStefan Behrens #endif 3766bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3767bcef60f2SArne Jansen if (ret) 3768bcef60f2SArne Jansen goto fail_trans_kthread; 376921adbd5cSStefan Behrens 3770fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3771fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3772fd708b81SJosef Bacik 377396da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 377496da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 37750b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3776e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 377763443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 377879787eaaSJeff Mahoney if (ret) { 377963443bf5SEric Sandeen err = ret; 378028c16cbbSWang Shilong goto fail_qgroup; 3781e556ce2cSYan Zheng } 3782e02119d5SChris Mason } 37831a40e23bSZheng Yan 378456e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 37853140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 37863140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3787f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3788315bf8efSJosef Bacik fs_info->fs_root = NULL; 3789bcef60f2SArne Jansen goto fail_qgroup; 37903140c9a3SDan Carpenter } 3791c289811cSChris Mason 3792bc98a42cSDavid Howells if (sb_rdonly(sb)) 37938cd29088SBoris Burkov goto clear_oneshot; 37942b6ba629SIlya Dryomov 379544c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 37962b6ba629SIlya Dryomov if (ret) { 37976bccf3abSJeff Mahoney close_ctree(fs_info); 37982b6ba629SIlya Dryomov return ret; 3799e3acc2a6SJosef Bacik } 3800b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3801b382a324SJan Schmidt 380244c0ca21SBoris Burkov if (fs_info->uuid_root && 380344c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 380444c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 380505135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 380670f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 380770f80175SStefan Behrens if (ret) { 380805135f59SDavid Sterba btrfs_warn(fs_info, 380905135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 38106bccf3abSJeff Mahoney close_ctree(fs_info); 381170f80175SStefan Behrens return ret; 381270f80175SStefan Behrens } 3813f7a81ea4SStefan Behrens } 381494846229SBoris Burkov 3815afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 381647ab2a6cSJosef Bacik 3817b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3818b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3819b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3820b4be6aefSJosef Bacik 38218cd29088SBoris Burkov clear_oneshot: 38228cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3823ad2b2c80SAl Viro return 0; 382439279cc3SChris Mason 3825bcef60f2SArne Jansen fail_qgroup: 3826bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 38277c2ca468SChris Mason fail_trans_kthread: 38287c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 38292ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3830faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 38313f157a2fSChris Mason fail_cleaner: 3832a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 38337c2ca468SChris Mason 38347c2ca468SChris Mason /* 38357c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 38367c2ca468SChris Mason * kthreads 38377c2ca468SChris Mason */ 38387c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 38397c2ca468SChris Mason 38402365dd3cSAnand Jain fail_sysfs: 38416618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 38422365dd3cSAnand Jain 3843b7c35e81SAnand Jain fail_fsdev_sysfs: 3844b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3845b7c35e81SAnand Jain 38461b1d1f66SJosef Bacik fail_block_groups: 384754067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3848af31f5e5SChris Mason 3849af31f5e5SChris Mason fail_tree_roots: 38509e3aa805SJosef Bacik if (fs_info->data_reloc_root) 38519e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 38524273eaffSAnand Jain free_root_pointers(fs_info, true); 38532b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3854af31f5e5SChris Mason 385539279cc3SChris Mason fail_sb_buffer: 38567abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 38575cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 385816cdcec7SMiao Xie fail_alloc: 3859586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3860586e46e2SIlya Dryomov 38614543df7eSChris Mason iput(fs_info->btree_inode); 38627e662854SQinghuang Feng fail: 3863586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3864ad2b2c80SAl Viro return err; 3865eb60ceacSChris Mason } 3866663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3867eb60ceacSChris Mason 3868314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3869f2984462SChris Mason { 3870314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3871314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3872314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3873314b6dd0SJohannes Thumshirn struct page *page; 3874442a4f63SStefan Behrens 3875314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3876314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3877314b6dd0SJohannes Thumshirn 3878314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3879fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3880314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3881314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3882314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3883314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3884314b6dd0SJohannes Thumshirn SetPageError(page); 3885314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3886314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3887314b6dd0SJohannes Thumshirn } else { 3888314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3889f2984462SChris Mason } 3890314b6dd0SJohannes Thumshirn 3891314b6dd0SJohannes Thumshirn put_page(page); 3892314b6dd0SJohannes Thumshirn unlock_page(page); 3893314b6dd0SJohannes Thumshirn } 3894314b6dd0SJohannes Thumshirn 3895314b6dd0SJohannes Thumshirn bio_put(bio); 3896f2984462SChris Mason } 3897f2984462SChris Mason 38988f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 38998f32380dSJohannes Thumshirn int copy_num) 390029c36d72SAnand Jain { 390129c36d72SAnand Jain struct btrfs_super_block *super; 39028f32380dSJohannes Thumshirn struct page *page; 390312659251SNaohiro Aota u64 bytenr, bytenr_orig; 39048f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 390512659251SNaohiro Aota int ret; 390629c36d72SAnand Jain 390712659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 390812659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 390912659251SNaohiro Aota if (ret == -ENOENT) 391012659251SNaohiro Aota return ERR_PTR(-EINVAL); 391112659251SNaohiro Aota else if (ret) 391212659251SNaohiro Aota return ERR_PTR(ret); 391312659251SNaohiro Aota 3914cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 39158f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 391629c36d72SAnand Jain 39178f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 39188f32380dSJohannes Thumshirn if (IS_ERR(page)) 39198f32380dSJohannes Thumshirn return ERR_CAST(page); 392029c36d72SAnand Jain 39218f32380dSJohannes Thumshirn super = page_address(page); 392296c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 392396c2e067SAnand Jain btrfs_release_disk_super(super); 392496c2e067SAnand Jain return ERR_PTR(-ENODATA); 392596c2e067SAnand Jain } 392696c2e067SAnand Jain 392712659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 39288f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 39298f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 393029c36d72SAnand Jain } 393129c36d72SAnand Jain 39328f32380dSJohannes Thumshirn return super; 393329c36d72SAnand Jain } 393429c36d72SAnand Jain 393529c36d72SAnand Jain 39368f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3937a512bbf8SYan Zheng { 39388f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3939a512bbf8SYan Zheng int i; 3940a512bbf8SYan Zheng u64 transid = 0; 3941a512bbf8SYan Zheng 3942a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3943a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3944a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3945a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3946a512bbf8SYan Zheng */ 3947a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 39488f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 39498f32380dSJohannes Thumshirn if (IS_ERR(super)) 3950a512bbf8SYan Zheng continue; 3951a512bbf8SYan Zheng 3952a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 39538f32380dSJohannes Thumshirn if (latest) 39548f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 39558f32380dSJohannes Thumshirn 39568f32380dSJohannes Thumshirn latest = super; 3957a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3958a512bbf8SYan Zheng } 3959a512bbf8SYan Zheng } 396092fc03fbSAnand Jain 39618f32380dSJohannes Thumshirn return super; 3962a512bbf8SYan Zheng } 3963a512bbf8SYan Zheng 39644eedeb75SHisashi Hifumi /* 3965abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3966314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 39674eedeb75SHisashi Hifumi * 3968abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3969abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3970abbb3b8eSDavid Sterba * same for write and wait phases. 39714eedeb75SHisashi Hifumi * 3972314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 39734eedeb75SHisashi Hifumi */ 3974a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3975abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3976a512bbf8SYan Zheng { 3977d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3978314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3979d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3980a512bbf8SYan Zheng int i; 3981a512bbf8SYan Zheng int errors = 0; 398212659251SNaohiro Aota int ret; 398312659251SNaohiro Aota u64 bytenr, bytenr_orig; 3984a512bbf8SYan Zheng 3985a512bbf8SYan Zheng if (max_mirrors == 0) 3986a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3987a512bbf8SYan Zheng 3988d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3989d5178578SJohannes Thumshirn 3990a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3991314b6dd0SJohannes Thumshirn struct page *page; 3992314b6dd0SJohannes Thumshirn struct bio *bio; 3993314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3994314b6dd0SJohannes Thumshirn 399512659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 399612659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 399712659251SNaohiro Aota if (ret == -ENOENT) { 399812659251SNaohiro Aota continue; 399912659251SNaohiro Aota } else if (ret < 0) { 400012659251SNaohiro Aota btrfs_err(device->fs_info, 400112659251SNaohiro Aota "couldn't get super block location for mirror %d", 400212659251SNaohiro Aota i); 400312659251SNaohiro Aota errors++; 400412659251SNaohiro Aota continue; 400512659251SNaohiro Aota } 4006935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4007935e5cc9SMiao Xie device->commit_total_bytes) 4008a512bbf8SYan Zheng break; 4009a512bbf8SYan Zheng 401012659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4011a512bbf8SYan Zheng 4012fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4013fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4014fd08001fSEric Biggers sb->csum); 4015a512bbf8SYan Zheng 4016314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4017314b6dd0SJohannes Thumshirn GFP_NOFS); 4018314b6dd0SJohannes Thumshirn if (!page) { 4019fb456252SJeff Mahoney btrfs_err(device->fs_info, 4020314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4021f14d104dSDavid Sterba bytenr); 4022634554dcSJosef Bacik errors++; 4023634554dcSJosef Bacik continue; 4024634554dcSJosef Bacik } 4025634554dcSJosef Bacik 4026314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4027314b6dd0SJohannes Thumshirn get_page(page); 4028a512bbf8SYan Zheng 4029314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4030314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4031a512bbf8SYan Zheng 4032387125fcSChris Mason /* 4033314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4034314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4035314b6dd0SJohannes Thumshirn * checking. 4036387125fcSChris Mason */ 403707888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 403807888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 403907888c66SChristoph Hellwig GFP_NOFS); 4040314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4041314b6dd0SJohannes Thumshirn bio->bi_private = device; 4042314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4043314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4044314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4045314b6dd0SJohannes Thumshirn 4046314b6dd0SJohannes Thumshirn /* 4047314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4048314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4049314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4050314b6dd0SJohannes Thumshirn */ 40511b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4052314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4053314b6dd0SJohannes Thumshirn 405458ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 405558ff51f1SChristoph Hellwig submit_bio(bio); 40568376d9e1SNaohiro Aota 40578376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 40588376d9e1SNaohiro Aota errors++; 4059a512bbf8SYan Zheng } 4060a512bbf8SYan Zheng return errors < i ? 0 : -1; 4061a512bbf8SYan Zheng } 4062a512bbf8SYan Zheng 4063387125fcSChris Mason /* 4064abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4065abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4066abbb3b8eSDavid Sterba * 4067314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4068abbb3b8eSDavid Sterba * date. 4069abbb3b8eSDavid Sterba */ 4070abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4071abbb3b8eSDavid Sterba { 4072abbb3b8eSDavid Sterba int i; 4073abbb3b8eSDavid Sterba int errors = 0; 4074b6a535faSHoward McLauchlan bool primary_failed = false; 407512659251SNaohiro Aota int ret; 4076abbb3b8eSDavid Sterba u64 bytenr; 4077abbb3b8eSDavid Sterba 4078abbb3b8eSDavid Sterba if (max_mirrors == 0) 4079abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4080abbb3b8eSDavid Sterba 4081abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4082314b6dd0SJohannes Thumshirn struct page *page; 4083314b6dd0SJohannes Thumshirn 408412659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 408512659251SNaohiro Aota if (ret == -ENOENT) { 408612659251SNaohiro Aota break; 408712659251SNaohiro Aota } else if (ret < 0) { 408812659251SNaohiro Aota errors++; 408912659251SNaohiro Aota if (i == 0) 409012659251SNaohiro Aota primary_failed = true; 409112659251SNaohiro Aota continue; 409212659251SNaohiro Aota } 4093abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4094abbb3b8eSDavid Sterba device->commit_total_bytes) 4095abbb3b8eSDavid Sterba break; 4096abbb3b8eSDavid Sterba 4097314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4098314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4099314b6dd0SJohannes Thumshirn if (!page) { 4100abbb3b8eSDavid Sterba errors++; 4101b6a535faSHoward McLauchlan if (i == 0) 4102b6a535faSHoward McLauchlan primary_failed = true; 4103abbb3b8eSDavid Sterba continue; 4104abbb3b8eSDavid Sterba } 4105314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4106314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4107314b6dd0SJohannes Thumshirn if (PageError(page)) { 4108abbb3b8eSDavid Sterba errors++; 4109b6a535faSHoward McLauchlan if (i == 0) 4110b6a535faSHoward McLauchlan primary_failed = true; 4111b6a535faSHoward McLauchlan } 4112abbb3b8eSDavid Sterba 4113314b6dd0SJohannes Thumshirn /* Drop our reference */ 4114314b6dd0SJohannes Thumshirn put_page(page); 4115abbb3b8eSDavid Sterba 4116314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4117314b6dd0SJohannes Thumshirn put_page(page); 4118abbb3b8eSDavid Sterba } 4119abbb3b8eSDavid Sterba 4120b6a535faSHoward McLauchlan /* log error, force error return */ 4121b6a535faSHoward McLauchlan if (primary_failed) { 4122b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4123b6a535faSHoward McLauchlan device->devid); 4124b6a535faSHoward McLauchlan return -1; 4125b6a535faSHoward McLauchlan } 4126b6a535faSHoward McLauchlan 4127abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4128abbb3b8eSDavid Sterba } 4129abbb3b8eSDavid Sterba 4130abbb3b8eSDavid Sterba /* 4131387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4132387125fcSChris Mason * for the barrier when it is done 4133387125fcSChris Mason */ 41344246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4135387125fcSChris Mason { 4136f9e69aa9SChristoph Hellwig bio_uninit(bio); 4137387125fcSChris Mason complete(bio->bi_private); 4138387125fcSChris Mason } 4139387125fcSChris Mason 4140387125fcSChris Mason /* 41414fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 41424fc6441aSAnand Jain * done in the waiting counterpart. 4143387125fcSChris Mason */ 41444fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4145387125fcSChris Mason { 4146f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 4147387125fcSChris Mason 4148a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4149a91cf0ffSWang Yugui /* 4150a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4151a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4152a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4153a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4154a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4155a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4156a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4157a91cf0ffSWang Yugui * non-debug builds. 4158a91cf0ffSWang Yugui */ 415908e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev)) 41604fc6441aSAnand Jain return; 4161a91cf0ffSWang Yugui #endif 4162387125fcSChris Mason 4163f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0, 4164f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4165387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4166387125fcSChris Mason init_completion(&device->flush_wait); 4167387125fcSChris Mason bio->bi_private = &device->flush_wait; 4168387125fcSChris Mason 416958ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 417058ff51f1SChristoph Hellwig submit_bio(bio); 41711c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 41724fc6441aSAnand Jain } 4173387125fcSChris Mason 41744fc6441aSAnand Jain /* 41754fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 41764fc6441aSAnand Jain */ 41778c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 41784fc6441aSAnand Jain { 4179f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 41804fc6441aSAnand Jain 41811c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 418258efbc9fSOmar Sandoval return BLK_STS_OK; 41834fc6441aSAnand Jain 41841c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 41852980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 41864fc6441aSAnand Jain 41878c27cb35SLinus Torvalds return bio->bi_status; 4188387125fcSChris Mason } 4189387125fcSChris Mason 4190d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4191401b41e5SAnand Jain { 41926528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4193401b41e5SAnand Jain return -EIO; 4194387125fcSChris Mason return 0; 4195387125fcSChris Mason } 4196387125fcSChris Mason 4197387125fcSChris Mason /* 4198387125fcSChris Mason * send an empty flush down to each device in parallel, 4199387125fcSChris Mason * then wait for them 4200387125fcSChris Mason */ 4201387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4202387125fcSChris Mason { 4203387125fcSChris Mason struct list_head *head; 4204387125fcSChris Mason struct btrfs_device *dev; 42055af3e8ccSStefan Behrens int errors_wait = 0; 42064e4cbee9SChristoph Hellwig blk_status_t ret; 4207387125fcSChris Mason 42081538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4209387125fcSChris Mason /* send down all the barriers */ 4210387125fcSChris Mason head = &info->fs_devices->devices; 42111538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4212e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4213f88ba6a2SHidetoshi Seto continue; 4214cea7c8bfSAnand Jain if (!dev->bdev) 4215387125fcSChris Mason continue; 4216e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4217ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4218387125fcSChris Mason continue; 4219387125fcSChris Mason 42204fc6441aSAnand Jain write_dev_flush(dev); 422158efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4222387125fcSChris Mason } 4223387125fcSChris Mason 4224387125fcSChris Mason /* wait for all the barriers */ 42251538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4226e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4227f88ba6a2SHidetoshi Seto continue; 4228387125fcSChris Mason if (!dev->bdev) { 42295af3e8ccSStefan Behrens errors_wait++; 4230387125fcSChris Mason continue; 4231387125fcSChris Mason } 4232e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4233ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4234387125fcSChris Mason continue; 4235387125fcSChris Mason 42364fc6441aSAnand Jain ret = wait_dev_flush(dev); 4237401b41e5SAnand Jain if (ret) { 4238401b41e5SAnand Jain dev->last_flush_error = ret; 423966b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 424066b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 42415af3e8ccSStefan Behrens errors_wait++; 4242387125fcSChris Mason } 4243401b41e5SAnand Jain } 4244401b41e5SAnand Jain 4245cea7c8bfSAnand Jain if (errors_wait) { 4246401b41e5SAnand Jain /* 4247401b41e5SAnand Jain * At some point we need the status of all disks 4248401b41e5SAnand Jain * to arrive at the volume status. So error checking 4249401b41e5SAnand Jain * is being pushed to a separate loop. 4250401b41e5SAnand Jain */ 4251d10b82feSQu Wenruo return check_barrier_error(info); 4252401b41e5SAnand Jain } 4253387125fcSChris Mason return 0; 4254387125fcSChris Mason } 4255387125fcSChris Mason 4256943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4257943c6e99SZhao Lei { 42588789f4feSZhao Lei int raid_type; 42598789f4feSZhao Lei int min_tolerated = INT_MAX; 4260943c6e99SZhao Lei 42618789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 42628789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 42638c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 42648789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 42658789f4feSZhao Lei tolerated_failures); 4266943c6e99SZhao Lei 42678789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 42688789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 42698789f4feSZhao Lei continue; 427041a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 42718789f4feSZhao Lei continue; 42728c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 42738789f4feSZhao Lei btrfs_raid_array[raid_type]. 42748789f4feSZhao Lei tolerated_failures); 42758789f4feSZhao Lei } 4276943c6e99SZhao Lei 42778789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4278ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 42798789f4feSZhao Lei min_tolerated = 0; 42808789f4feSZhao Lei } 42818789f4feSZhao Lei 42828789f4feSZhao Lei return min_tolerated; 4283943c6e99SZhao Lei } 4284943c6e99SZhao Lei 4285eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4286f2984462SChris Mason { 4287e5e9a520SChris Mason struct list_head *head; 4288f2984462SChris Mason struct btrfs_device *dev; 4289a061fc8dSChris Mason struct btrfs_super_block *sb; 4290f2984462SChris Mason struct btrfs_dev_item *dev_item; 4291f2984462SChris Mason int ret; 4292f2984462SChris Mason int do_barriers; 4293a236aed1SChris Mason int max_errors; 4294a236aed1SChris Mason int total_errors = 0; 4295a061fc8dSChris Mason u64 flags; 4296f2984462SChris Mason 42970b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4298fed3b381SLiu Bo 4299fed3b381SLiu Bo /* 4300fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4301fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4302fed3b381SLiu Bo * been consistent on disk. 4303fed3b381SLiu Bo */ 4304fed3b381SLiu Bo if (max_mirrors == 0) 43050b246afaSJeff Mahoney backup_super_roots(fs_info); 4306f2984462SChris Mason 43070b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4308a061fc8dSChris Mason dev_item = &sb->dev_item; 4309e5e9a520SChris Mason 43100b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 43110b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 43120b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4313387125fcSChris Mason 43145af3e8ccSStefan Behrens if (do_barriers) { 43150b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 43165af3e8ccSStefan Behrens if (ret) { 43175af3e8ccSStefan Behrens mutex_unlock( 43180b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 43190b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 43205af3e8ccSStefan Behrens "errors while submitting device barriers."); 43215af3e8ccSStefan Behrens return ret; 43225af3e8ccSStefan Behrens } 43235af3e8ccSStefan Behrens } 4324387125fcSChris Mason 43251538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4326dfe25020SChris Mason if (!dev->bdev) { 4327dfe25020SChris Mason total_errors++; 4328dfe25020SChris Mason continue; 4329dfe25020SChris Mason } 4330e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4331ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4332dfe25020SChris Mason continue; 4333dfe25020SChris Mason 43342b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4335a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4336a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 43377df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4338935e5cc9SMiao Xie dev->commit_total_bytes); 4339ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4340ce7213c7SMiao Xie dev->commit_bytes_used); 4341a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4342a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4343a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4344a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 43457239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 43467239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4347a512bbf8SYan Zheng 4348a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4349a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4350f2984462SChris Mason 435175cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 435275cb857dSQu Wenruo if (ret < 0) { 435375cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 435475cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 435575cb857dSQu Wenruo "unexpected superblock corruption detected"); 435675cb857dSQu Wenruo return -EUCLEAN; 435775cb857dSQu Wenruo } 435875cb857dSQu Wenruo 4359abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4360a236aed1SChris Mason if (ret) 4361a236aed1SChris Mason total_errors++; 4362f2984462SChris Mason } 4363a236aed1SChris Mason if (total_errors > max_errors) { 43640b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4365d397712bSChris Mason total_errors); 43660b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 436779787eaaSJeff Mahoney 43689d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 43690b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 43700b246afaSJeff Mahoney "%d errors while writing supers", 43710b246afaSJeff Mahoney total_errors); 43729d565ba4SStefan Behrens return -EIO; 4373a236aed1SChris Mason } 4374f2984462SChris Mason 4375a512bbf8SYan Zheng total_errors = 0; 43761538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4377dfe25020SChris Mason if (!dev->bdev) 4378dfe25020SChris Mason continue; 4379e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4380ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4381dfe25020SChris Mason continue; 4382dfe25020SChris Mason 4383abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4384a512bbf8SYan Zheng if (ret) 43851259ab75SChris Mason total_errors++; 4386f2984462SChris Mason } 43870b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4388a236aed1SChris Mason if (total_errors > max_errors) { 43890b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 43900b246afaSJeff Mahoney "%d errors while writing supers", 43910b246afaSJeff Mahoney total_errors); 439279787eaaSJeff Mahoney return -EIO; 4393a236aed1SChris Mason } 4394f2984462SChris Mason return 0; 4395f2984462SChris Mason } 4396f2984462SChris Mason 4397cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4398cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4399cb517eabSMiao Xie struct btrfs_root *root) 44002619ba1fSChris Mason { 44014785e24fSJosef Bacik bool drop_ref = false; 44024785e24fSJosef Bacik 4403fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4404fc7cbcd4SDavid Sterba radix_tree_delete(&fs_info->fs_roots_radix, 4405fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid); 4406fc7cbcd4SDavid Sterba if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 44074785e24fSJosef Bacik drop_ref = true; 4408fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 440976dda93cSYan, Zheng 441084961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4411ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 44121c1ea4f7SLiu Bo if (root->reloc_root) { 441300246528SJosef Bacik btrfs_put_root(root->reloc_root); 44141c1ea4f7SLiu Bo root->reloc_root = NULL; 44151c1ea4f7SLiu Bo } 44161c1ea4f7SLiu Bo } 44173321719eSLiu Bo 44184785e24fSJosef Bacik if (drop_ref) 441900246528SJosef Bacik btrfs_put_root(root); 44202619ba1fSChris Mason } 44212619ba1fSChris Mason 4422c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4423c146afadSYan Zheng { 4424fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4425fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4426fc7cbcd4SDavid Sterba int i = 0; 442765d33fd7SQu Wenruo int err = 0; 4428fc7cbcd4SDavid Sterba unsigned int ret = 0; 4429c146afadSYan Zheng 4430c146afadSYan Zheng while (1) { 4431fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4432fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4433fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4434fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 4435fc7cbcd4SDavid Sterba if (!ret) { 4436fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4437c146afadSYan Zheng break; 443865d33fd7SQu Wenruo } 4439fc7cbcd4SDavid Sterba root_objectid = gang[ret - 1]->root_key.objectid + 1; 444066b4ffd1SJosef Bacik 4441fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4442fc7cbcd4SDavid Sterba /* Avoid to grab roots in dead_roots */ 4443fc7cbcd4SDavid Sterba if (btrfs_root_refs(&gang[i]->root_item) == 0) { 4444fc7cbcd4SDavid Sterba gang[i] = NULL; 444565d33fd7SQu Wenruo continue; 4446c146afadSYan Zheng } 4447fc7cbcd4SDavid Sterba /* grab all the search result for later use */ 4448fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4449fc7cbcd4SDavid Sterba } 4450fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4451fc7cbcd4SDavid Sterba 4452fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4453fc7cbcd4SDavid Sterba if (!gang[i]) 4454fc7cbcd4SDavid Sterba continue; 4455fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4456fc7cbcd4SDavid Sterba err = btrfs_orphan_cleanup(gang[i]); 4457fc7cbcd4SDavid Sterba if (err) 4458fc7cbcd4SDavid Sterba break; 4459fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4460fc7cbcd4SDavid Sterba } 4461fc7cbcd4SDavid Sterba root_objectid++; 4462c146afadSYan Zheng } 446365d33fd7SQu Wenruo 4464fc7cbcd4SDavid Sterba /* release the uncleaned roots due to error */ 4465fc7cbcd4SDavid Sterba for (; i < ret; i++) { 4466fc7cbcd4SDavid Sterba if (gang[i]) 4467fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 446865d33fd7SQu Wenruo } 446965d33fd7SQu Wenruo return err; 4470c146afadSYan Zheng } 4471c146afadSYan Zheng 44726bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4473c146afadSYan Zheng { 44746bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4475c146afadSYan Zheng struct btrfs_trans_handle *trans; 4476c146afadSYan Zheng 44770b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 44782ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 44790b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 44800b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4481c71bf099SYan, Zheng 4482c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 44830b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 44840b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4485c71bf099SYan, Zheng 44867a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 44873612b495STsutomu Itoh if (IS_ERR(trans)) 44883612b495STsutomu Itoh return PTR_ERR(trans); 44893a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4490c146afadSYan Zheng } 4491c146afadSYan Zheng 449236c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 449336c86a9eSQu Wenruo { 449436c86a9eSQu Wenruo struct btrfs_transaction *trans; 449536c86a9eSQu Wenruo struct btrfs_transaction *tmp; 449636c86a9eSQu Wenruo bool found = false; 449736c86a9eSQu Wenruo 449836c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 449936c86a9eSQu Wenruo return; 450036c86a9eSQu Wenruo 450136c86a9eSQu Wenruo /* 450236c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 450336c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 450436c86a9eSQu Wenruo */ 450536c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 450636c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 450736c86a9eSQu Wenruo struct extent_state *cached = NULL; 450836c86a9eSQu Wenruo u64 dirty_bytes = 0; 450936c86a9eSQu Wenruo u64 cur = 0; 451036c86a9eSQu Wenruo u64 found_start; 451136c86a9eSQu Wenruo u64 found_end; 451236c86a9eSQu Wenruo 451336c86a9eSQu Wenruo found = true; 451436c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 451536c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 451636c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 451736c86a9eSQu Wenruo cur = found_end + 1; 451836c86a9eSQu Wenruo } 451936c86a9eSQu Wenruo btrfs_warn(fs_info, 452036c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 452136c86a9eSQu Wenruo trans->transid, dirty_bytes); 452236c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 452336c86a9eSQu Wenruo 452436c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 452536c86a9eSQu Wenruo fs_info->running_transaction = NULL; 452636c86a9eSQu Wenruo list_del_init(&trans->list); 452736c86a9eSQu Wenruo 452836c86a9eSQu Wenruo btrfs_put_transaction(trans); 452936c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 453036c86a9eSQu Wenruo } 453136c86a9eSQu Wenruo ASSERT(!found); 453236c86a9eSQu Wenruo } 453336c86a9eSQu Wenruo 4534b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4535eb60ceacSChris Mason { 4536c146afadSYan Zheng int ret; 4537e089f05cSChris Mason 4538afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 453931e70e52SFilipe Manana 454031e70e52SFilipe Manana /* 454131e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group, 454231e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park 454331e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after 454431e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang 454531e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is 454631e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when 454731e70e52SFilipe Manana * trying to stop the async reclaim task. 454831e70e52SFilipe Manana */ 454931e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work); 4550d6fd0ae2SOmar Sandoval /* 4551d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4552d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4553d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4554d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4555d6fd0ae2SOmar Sandoval */ 4556d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4557a2135011SChris Mason 4558b4be6aefSJosef Bacik /* 4559b4be6aefSJosef Bacik * If we had UNFINISHED_DROPS we could still be processing them, so 4560b4be6aefSJosef Bacik * clear that bit and wake up relocation so it can stop. 4561b4be6aefSJosef Bacik */ 4562b4be6aefSJosef Bacik btrfs_wake_unfinished_drop(fs_info); 4563b4be6aefSJosef Bacik 45647343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4565d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 45667343dd61SJustin Maggard 4567803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4568803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4569803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4570803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4571803b2f54SStefan Behrens 4572837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4573aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4574837d5b6eSIlya Dryomov 45758dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 45768dabb742SStefan Behrens 4577aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 45784cb5300bSChris Mason 45794cb5300bSChris Mason /* wait for any defraggers to finish */ 45804cb5300bSChris Mason wait_event(fs_info->transaction_wait, 45814cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 45824cb5300bSChris Mason 45834cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 458426176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 45854cb5300bSChris Mason 458621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 458757056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4588576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 458921c7e756SMiao Xie 4590b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4591b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4592b0643e59SDennis Zhou 4593bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4594e44163e1SJeff Mahoney /* 4595d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4596d6fd0ae2SOmar Sandoval * unused block groups. 4597e44163e1SJeff Mahoney */ 45980b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4599e44163e1SJeff Mahoney 4600f0cc2cd7SFilipe Manana /* 4601f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4602f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4603f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4604f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4605f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4606f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4607f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4608f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4609f0cc2cd7SFilipe Manana * This is a very rare case. 4610f0cc2cd7SFilipe Manana */ 4611f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4612f0cc2cd7SFilipe Manana 46136bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4614d397712bSChris Mason if (ret) 461504892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4616c146afadSYan Zheng } 4617ed2ff2cbSChris Mason 461884961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 46192ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4620acce952bSliubo 4621e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4622e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 46238929ecfaSYan, Zheng 4624e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4625afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4626f25784b3SYan Zheng 46275958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 46285958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 46295958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 46305958253cSQu Wenruo } 46315958253cSQu Wenruo 463204892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4633fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4634bcef60f2SArne Jansen 4635963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 463604892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4637963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4638b0c68f8bSChris Mason } 463931153d81SYan Zheng 46405deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 46414297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 46425deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 46434297ff84SJosef Bacik 46446618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4645b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 46465ac1d209SJeff Mahoney 46471a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 46481a4319ccSLiu Bo 4649de348ee0SWang Shilong /* 4650de348ee0SWang Shilong * we must make sure there is not any read request to 4651de348ee0SWang Shilong * submit after we stopping all workers. 4652de348ee0SWang Shilong */ 4653de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 465496192499SJosef Bacik btrfs_stop_all_workers(fs_info); 465596192499SJosef Bacik 46560a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 465736c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 46580a31daa4SFilipe Manana 4659afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 46604273eaffSAnand Jain free_root_pointers(fs_info, true); 46618c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 46629ad6b7bcSChris Mason 46634e19443dSJosef Bacik /* 46644e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 46654e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 46664e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 46674e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 46684e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 46694e19443dSJosef Bacik */ 46704e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 46714e19443dSJosef Bacik 467213e6c37bSJosef Bacik iput(fs_info->btree_inode); 4673d6bfde87SChris Mason 467421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 46750b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 46762ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 467721adbd5cSStefan Behrens #endif 467821adbd5cSStefan Behrens 46790b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 468068c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4681eb60ceacSChris Mason } 4682eb60ceacSChris Mason 4683b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4684b9fab919SChris Mason int atomic) 4685ccd467d6SChris Mason { 46861259ab75SChris Mason int ret; 4687727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 46881259ab75SChris Mason 46890b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 46901259ab75SChris Mason if (!ret) 46911259ab75SChris Mason return ret; 46921259ab75SChris Mason 46931259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4694b9fab919SChris Mason parent_transid, atomic); 4695b9fab919SChris Mason if (ret == -EAGAIN) 4696b9fab919SChris Mason return ret; 46971259ab75SChris Mason return !ret; 46985f39d397SChris Mason } 46996702ed49SChris Mason 47005f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 47015f39d397SChris Mason { 47022f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 47035f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4704b9473439SChris Mason int was_dirty; 4705b4ce94deSChris Mason 470606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 470706ea65a3SJosef Bacik /* 470806ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 470952042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 471006ea65a3SJosef Bacik * outside of the sanity tests. 471106ea65a3SJosef Bacik */ 4712b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 471306ea65a3SJosef Bacik return; 471406ea65a3SJosef Bacik #endif 471549d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 47160b246afaSJeff Mahoney if (transid != fs_info->generation) 47175d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 47180b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 47190b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4720e2d84521SMiao Xie if (!was_dirty) 4721104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4722e2d84521SMiao Xie buf->len, 47230b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 47241f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 472569fc6cbbSQu Wenruo /* 472669fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 472769fc6cbbSQu Wenruo * but item data not updated. 472869fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 472969fc6cbbSQu Wenruo */ 473069fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4731cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4732a4f78750SDavid Sterba btrfs_print_leaf(buf); 47331f21ef0aSFilipe Manana ASSERT(0); 47341f21ef0aSFilipe Manana } 47351f21ef0aSFilipe Manana #endif 4736eb60ceacSChris Mason } 4737eb60ceacSChris Mason 47382ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4739b53d3f5dSLiu Bo int flush_delayed) 474035b7e476SChris Mason { 4741188de649SChris Mason /* 4742188de649SChris Mason * looks as though older kernels can get into trouble with 4743188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4744188de649SChris Mason */ 4745e2d84521SMiao Xie int ret; 4746d6bfde87SChris Mason 47476933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4748d6bfde87SChris Mason return; 4749d6bfde87SChris Mason 4750b53d3f5dSLiu Bo if (flush_delayed) 47512ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 475216cdcec7SMiao Xie 4753d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4754d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4755d814a491SEthan Lien fs_info->dirty_metadata_batch); 4756e2d84521SMiao Xie if (ret > 0) { 47570b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 475816cdcec7SMiao Xie } 475916cdcec7SMiao Xie } 476016cdcec7SMiao Xie 47612ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 476216cdcec7SMiao Xie { 47632ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 476435b7e476SChris Mason } 4765b53d3f5dSLiu Bo 47662ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4767b53d3f5dSLiu Bo { 47682ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4769d6bfde87SChris Mason } 47706b80053dSChris Mason 47712ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4772acce952bSliubo { 4773fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4774fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4775fe816d0fSNikolay Borisov 47760b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 47772ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 47780b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4779acce952bSliubo 47800b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 47810b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4782acce952bSliubo } 4783acce952bSliubo 4784ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4785ef67963dSJosef Bacik { 4786fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4787fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4788fc7cbcd4SDavid Sterba int ret; 4789ef67963dSJosef Bacik 4790fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4791fc7cbcd4SDavid Sterba while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4792fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4793fc7cbcd4SDavid Sterba ARRAY_SIZE(gang))) != 0) { 4794fc7cbcd4SDavid Sterba int i; 4795ef67963dSJosef Bacik 4796fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 4797fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4798fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4799fc7cbcd4SDavid Sterba 4800fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4801fc7cbcd4SDavid Sterba if (!gang[i]) 4802ef67963dSJosef Bacik continue; 4803fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4804fc7cbcd4SDavid Sterba btrfs_free_log(NULL, gang[i]); 4805fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4806ef67963dSJosef Bacik } 4807fc7cbcd4SDavid Sterba root_objectid++; 4808fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4809ef67963dSJosef Bacik } 4810fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4811ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4812ef67963dSJosef Bacik } 4813ef67963dSJosef Bacik 4814143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4815acce952bSliubo { 4816acce952bSliubo struct btrfs_ordered_extent *ordered; 4817acce952bSliubo 4818199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4819779880efSJosef Bacik /* 4820779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4821779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4822779880efSJosef Bacik */ 4823199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4824779880efSJosef Bacik root_extent_list) 4825779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4826199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4827199c2a9cSMiao Xie } 4828199c2a9cSMiao Xie 4829199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4830199c2a9cSMiao Xie { 4831199c2a9cSMiao Xie struct btrfs_root *root; 4832199c2a9cSMiao Xie struct list_head splice; 4833199c2a9cSMiao Xie 4834199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4835199c2a9cSMiao Xie 4836199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4837199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4838199c2a9cSMiao Xie while (!list_empty(&splice)) { 4839199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4840199c2a9cSMiao Xie ordered_root); 48411de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 48421de2cfdeSJosef Bacik &fs_info->ordered_roots); 4843199c2a9cSMiao Xie 48442a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4845199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4846199c2a9cSMiao Xie 48472a85d9caSLiu Bo cond_resched(); 48482a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4849199c2a9cSMiao Xie } 4850199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 485174d5d229SJosef Bacik 485274d5d229SJosef Bacik /* 485374d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 485474d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 485574d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 485674d5d229SJosef Bacik * actually get run and error out properly. 485774d5d229SJosef Bacik */ 485874d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4859acce952bSliubo } 4860acce952bSliubo 486135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 48622ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4863acce952bSliubo { 4864acce952bSliubo struct rb_node *node; 4865acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4866acce952bSliubo struct btrfs_delayed_ref_node *ref; 4867acce952bSliubo int ret = 0; 4868acce952bSliubo 4869acce952bSliubo delayed_refs = &trans->delayed_refs; 4870acce952bSliubo 4871acce952bSliubo spin_lock(&delayed_refs->lock); 4872d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4873cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4874b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4875acce952bSliubo return ret; 4876acce952bSliubo } 4877acce952bSliubo 48785c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4879d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 48800e0adbcfSJosef Bacik struct rb_node *n; 4881e78417d1SJosef Bacik bool pin_bytes = false; 4882acce952bSliubo 4883d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4884d7df2c79SJosef Bacik href_node); 48853069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4886b939d1abSJosef Bacik continue; 48873069bd26SJosef Bacik 4888d7df2c79SJosef Bacik spin_lock(&head->lock); 4889e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 48900e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 48910e0adbcfSJosef Bacik ref_node); 4892d7df2c79SJosef Bacik ref->in_tree = 0; 4893e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 48940e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 48951d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 48961d57ee94SWang Xiaoguang list_del(&ref->add_list); 4897d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4898d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4899d7df2c79SJosef Bacik } 490054067ae9SJosef Bacik if (head->must_insert_reserved) 4901e78417d1SJosef Bacik pin_bytes = true; 490278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4903fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4904d7df2c79SJosef Bacik spin_unlock(&head->lock); 4905acce952bSliubo spin_unlock(&delayed_refs->lock); 4906e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4907acce952bSliubo 4908f603bb94SNikolay Borisov if (pin_bytes) { 4909f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4910f603bb94SNikolay Borisov 4911f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4912f603bb94SNikolay Borisov BUG_ON(!cache); 4913f603bb94SNikolay Borisov 4914f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4915f603bb94SNikolay Borisov spin_lock(&cache->lock); 4916f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4917f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4918f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4919f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4920f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4921f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4922f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4923f603bb94SNikolay Borisov 4924f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4925f603bb94SNikolay Borisov 4926f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4927f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4928f603bb94SNikolay Borisov } 492931890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4930d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4931acce952bSliubo cond_resched(); 4932acce952bSliubo spin_lock(&delayed_refs->lock); 4933acce952bSliubo } 493481f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4935acce952bSliubo 4936acce952bSliubo spin_unlock(&delayed_refs->lock); 4937acce952bSliubo 4938acce952bSliubo return ret; 4939acce952bSliubo } 4940acce952bSliubo 4941143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4942acce952bSliubo { 4943acce952bSliubo struct btrfs_inode *btrfs_inode; 4944acce952bSliubo struct list_head splice; 4945acce952bSliubo 4946acce952bSliubo INIT_LIST_HEAD(&splice); 4947acce952bSliubo 4948eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4949eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4950acce952bSliubo 4951acce952bSliubo while (!list_empty(&splice)) { 4952fe816d0fSNikolay Borisov struct inode *inode = NULL; 4953eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4954acce952bSliubo delalloc_inodes); 4955fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4956eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4957acce952bSliubo 4958fe816d0fSNikolay Borisov /* 4959fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4960fe816d0fSNikolay Borisov * meanwhile. 4961fe816d0fSNikolay Borisov */ 4962fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4963fe816d0fSNikolay Borisov if (inode) { 4964fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4965fe816d0fSNikolay Borisov iput(inode); 4966fe816d0fSNikolay Borisov } 4967eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4968acce952bSliubo } 4969eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4970eb73c1b7SMiao Xie } 4971eb73c1b7SMiao Xie 4972eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4973eb73c1b7SMiao Xie { 4974eb73c1b7SMiao Xie struct btrfs_root *root; 4975eb73c1b7SMiao Xie struct list_head splice; 4976eb73c1b7SMiao Xie 4977eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4978eb73c1b7SMiao Xie 4979eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4980eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4981eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4982eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4983eb73c1b7SMiao Xie delalloc_root); 498400246528SJosef Bacik root = btrfs_grab_root(root); 4985eb73c1b7SMiao Xie BUG_ON(!root); 4986eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4987eb73c1b7SMiao Xie 4988eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 498900246528SJosef Bacik btrfs_put_root(root); 4990eb73c1b7SMiao Xie 4991eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4992eb73c1b7SMiao Xie } 4993eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4994acce952bSliubo } 4995acce952bSliubo 49962ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4997acce952bSliubo struct extent_io_tree *dirty_pages, 4998acce952bSliubo int mark) 4999acce952bSliubo { 5000acce952bSliubo int ret; 5001acce952bSliubo struct extent_buffer *eb; 5002acce952bSliubo u64 start = 0; 5003acce952bSliubo u64 end; 5004acce952bSliubo 5005acce952bSliubo while (1) { 5006acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5007e6138876SJosef Bacik mark, NULL); 5008acce952bSliubo if (ret) 5009acce952bSliubo break; 5010acce952bSliubo 501191166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5012acce952bSliubo while (start <= end) { 50130b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 50140b246afaSJeff Mahoney start += fs_info->nodesize; 5015fd8b2b61SJosef Bacik if (!eb) 5016acce952bSliubo continue; 5017fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5018acce952bSliubo 5019fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5020fd8b2b61SJosef Bacik &eb->bflags)) 5021fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5022fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5023acce952bSliubo } 5024acce952bSliubo } 5025acce952bSliubo 5026acce952bSliubo return ret; 5027acce952bSliubo } 5028acce952bSliubo 50292ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5030fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5031acce952bSliubo { 5032acce952bSliubo u64 start; 5033acce952bSliubo u64 end; 5034acce952bSliubo int ret; 5035acce952bSliubo 5036acce952bSliubo while (1) { 50370e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 50380e6ec385SFilipe Manana 5039fcd5e742SLu Fengqi /* 5040fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5041fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5042fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5043fcd5e742SLu Fengqi * the same extent range. 5044fcd5e742SLu Fengqi */ 5045fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5046acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 50470e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5048fcd5e742SLu Fengqi if (ret) { 5049fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5050acce952bSliubo break; 5051fcd5e742SLu Fengqi } 5052acce952bSliubo 50530e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 50540e6ec385SFilipe Manana free_extent_state(cached_state); 50552ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5056fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5057acce952bSliubo cond_resched(); 5058acce952bSliubo } 5059acce952bSliubo 5060acce952bSliubo return 0; 5061acce952bSliubo } 5062acce952bSliubo 506332da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5064c79a1751SLiu Bo { 5065c79a1751SLiu Bo struct inode *inode; 5066c79a1751SLiu Bo 5067c79a1751SLiu Bo inode = cache->io_ctl.inode; 5068c79a1751SLiu Bo if (inode) { 5069c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5070c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5071c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5072c79a1751SLiu Bo iput(inode); 5073c79a1751SLiu Bo } 5074bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5075c79a1751SLiu Bo btrfs_put_block_group(cache); 5076c79a1751SLiu Bo } 5077c79a1751SLiu Bo 5078c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 50792ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5080c79a1751SLiu Bo { 508132da5386SDavid Sterba struct btrfs_block_group *cache; 5082c79a1751SLiu Bo 5083c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5084c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5085c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 508632da5386SDavid Sterba struct btrfs_block_group, 5087c79a1751SLiu Bo dirty_list); 5088c79a1751SLiu Bo 5089c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5090c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5091c79a1751SLiu Bo list_del_init(&cache->io_list); 5092c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5093c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5094c79a1751SLiu Bo } 5095c79a1751SLiu Bo 5096c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5097c79a1751SLiu Bo spin_lock(&cache->lock); 5098c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5099c79a1751SLiu Bo spin_unlock(&cache->lock); 5100c79a1751SLiu Bo 5101c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5102c79a1751SLiu Bo btrfs_put_block_group(cache); 5103ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5104c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5105c79a1751SLiu Bo } 5106c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5107c79a1751SLiu Bo 510845ae2c18SNikolay Borisov /* 510945ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 511045ae2c18SNikolay Borisov * to use it without any locking 511145ae2c18SNikolay Borisov */ 5112c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5113c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 511432da5386SDavid Sterba struct btrfs_block_group, 5115c79a1751SLiu Bo io_list); 5116c79a1751SLiu Bo 5117c79a1751SLiu Bo list_del_init(&cache->io_list); 5118c79a1751SLiu Bo spin_lock(&cache->lock); 5119c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5120c79a1751SLiu Bo spin_unlock(&cache->lock); 5121c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5122c79a1751SLiu Bo } 5123c79a1751SLiu Bo } 5124c79a1751SLiu Bo 512549b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 51262ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 512749b25e05SJeff Mahoney { 5128bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5129bbbf7243SNikolay Borisov 51302ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5131c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5132c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5133c79a1751SLiu Bo 5134bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5135bbbf7243SNikolay Borisov post_commit_list) { 5136bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5137bbbf7243SNikolay Borisov } 5138bbbf7243SNikolay Borisov 51392ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 514049b25e05SJeff Mahoney 51414a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 51420b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 514349b25e05SJeff Mahoney 51444a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 51450b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 514649b25e05SJeff Mahoney 5147ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 514867cde344SMiao Xie 51492ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 515049b25e05SJeff Mahoney EXTENT_DIRTY); 5151fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 515249b25e05SJeff Mahoney 5153d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5154d3575156SNaohiro Aota 51554a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 51564a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 515749b25e05SJeff Mahoney } 515849b25e05SJeff Mahoney 51592ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5160acce952bSliubo { 5161acce952bSliubo struct btrfs_transaction *t; 5162acce952bSliubo 51630b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5164acce952bSliubo 51650b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 51660b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 51670b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5168724e2315SJosef Bacik struct btrfs_transaction, list); 5169724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 51709b64f57dSElena Reshetova refcount_inc(&t->use_count); 51710b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 51722ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5173724e2315SJosef Bacik btrfs_put_transaction(t); 51740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5175724e2315SJosef Bacik continue; 5176724e2315SJosef Bacik } 51770b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5178724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 51790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5180724e2315SJosef Bacik /* 5181724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5182724e2315SJosef Bacik * handle open currently for this transaction. 5183724e2315SJosef Bacik */ 5184724e2315SJosef Bacik wait_event(t->writer_wait, 5185724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5186724e2315SJosef Bacik } else { 51870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5188724e2315SJosef Bacik } 51892ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5190724e2315SJosef Bacik 51910b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 51920b246afaSJeff Mahoney if (t == fs_info->running_transaction) 51930b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5194724e2315SJosef Bacik list_del_init(&t->list); 51950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5196a4abeea4SJosef Bacik 5197724e2315SJosef Bacik btrfs_put_transaction(t); 51982e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 51990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5200724e2315SJosef Bacik } 52010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52020b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5203ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5204ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 52050b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5206ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 52070b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5208acce952bSliubo 5209acce952bSliubo return 0; 5210acce952bSliubo } 5211ec7d6dfdSNikolay Borisov 5212453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5213ec7d6dfdSNikolay Borisov { 5214ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5215ec7d6dfdSNikolay Borisov int ret; 5216ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5217ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5218ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5219ec7d6dfdSNikolay Borisov int slot; 5220ec7d6dfdSNikolay Borisov 5221ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5222ec7d6dfdSNikolay Borisov if (!path) 5223ec7d6dfdSNikolay Borisov return -ENOMEM; 5224ec7d6dfdSNikolay Borisov 5225ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5226ec7d6dfdSNikolay Borisov search_key.type = -1; 5227ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5228ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5229ec7d6dfdSNikolay Borisov if (ret < 0) 5230ec7d6dfdSNikolay Borisov goto error; 5231ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5232ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5233ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5234ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5235ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 523623125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 523723125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5238ec7d6dfdSNikolay Borisov } else { 523923125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5240ec7d6dfdSNikolay Borisov } 5241ec7d6dfdSNikolay Borisov ret = 0; 5242ec7d6dfdSNikolay Borisov error: 5243ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5244ec7d6dfdSNikolay Borisov return ret; 5245ec7d6dfdSNikolay Borisov } 5246ec7d6dfdSNikolay Borisov 5247543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5248ec7d6dfdSNikolay Borisov { 5249ec7d6dfdSNikolay Borisov int ret; 5250ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5251ec7d6dfdSNikolay Borisov 52526b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5253ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5254ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5255ec7d6dfdSNikolay Borisov root->root_key.objectid); 5256ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5257ec7d6dfdSNikolay Borisov goto out; 5258ec7d6dfdSNikolay Borisov } 5259ec7d6dfdSNikolay Borisov 526023125104SNikolay Borisov *objectid = root->free_objectid++; 5261ec7d6dfdSNikolay Borisov ret = 0; 5262ec7d6dfdSNikolay Borisov out: 5263ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5264ec7d6dfdSNikolay Borisov return ret; 5265ec7d6dfdSNikolay Borisov } 5266