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, 224*8f0ed7d4SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu", 225*8f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 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) { 555*8f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, 556*8f0ed7d4SQu Wenruo "bad tree block start, mirror %u want %llu have %llu", 557*8f0ed7d4SQu Wenruo eb->read_mirror, eb->start, found_start); 558f188591eSChris Mason ret = -EIO; 55977bf40a2SQu Wenruo goto out; 560ce9adaa5SChris Mason } 561b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 562*8f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u", 563*8f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 5641259ab75SChris Mason ret = -EIO; 56577bf40a2SQu Wenruo goto out; 5661259ab75SChris Mason } 567ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5681c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 569*8f0ed7d4SQu Wenruo btrfs_err(fs_info, 570*8f0ed7d4SQu Wenruo "bad tree block level, mirror %u level %d on logical %llu", 571*8f0ed7d4SQu Wenruo eb->read_mirror, btrfs_header_level(eb), eb->start); 5721c24c3ceSJosef Bacik ret = -EIO; 57377bf40a2SQu Wenruo goto out; 5741c24c3ceSJosef Bacik } 575ce9adaa5SChris Mason 576c67b3892SDavid Sterba csum_tree_block(eb, result); 577dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 578dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 579a826d6dcSJosef Bacik 580dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 5812996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 582*8f0ed7d4SQu Wenruo "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d", 583*8f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 584dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 58535be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 58635be8851SJohannes Thumshirn btrfs_header_level(eb)); 5872996e1f8SJohannes Thumshirn ret = -EUCLEAN; 58877bf40a2SQu Wenruo goto out; 5892996e1f8SJohannes Thumshirn } 5902996e1f8SJohannes Thumshirn 591a826d6dcSJosef Bacik /* 592a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 593a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 594a826d6dcSJosef Bacik * return -EIO. 595a826d6dcSJosef Bacik */ 5961c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 597a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 598a826d6dcSJosef Bacik ret = -EIO; 599a826d6dcSJosef Bacik } 600ce9adaa5SChris Mason 601813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 602053ab70fSLiu Bo ret = -EIO; 603053ab70fSLiu Bo 6040b32f4bbSJosef Bacik if (!ret) 6050b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 60675391f0dSQu Wenruo else 60775391f0dSQu Wenruo btrfs_err(fs_info, 608*8f0ed7d4SQu Wenruo "read time tree block corruption detected on logical %llu mirror %u", 609*8f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 61077bf40a2SQu Wenruo out: 61177bf40a2SQu Wenruo return ret; 61277bf40a2SQu Wenruo } 61377bf40a2SQu Wenruo 614371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 615371cdc07SQu Wenruo int mirror) 616371cdc07SQu Wenruo { 617371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 618371cdc07SQu Wenruo struct extent_buffer *eb; 619371cdc07SQu Wenruo bool reads_done; 620371cdc07SQu Wenruo int ret = 0; 621371cdc07SQu Wenruo 622371cdc07SQu Wenruo /* 623371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 624371cdc07SQu Wenruo * only get one eb for each endio hook. 625371cdc07SQu Wenruo */ 626371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 627371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 628371cdc07SQu Wenruo 629371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 630371cdc07SQu Wenruo /* 631371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 632371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 633371cdc07SQu Wenruo */ 634371cdc07SQu Wenruo ASSERT(eb); 635371cdc07SQu Wenruo 636371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 637371cdc07SQu Wenruo /* Subpage read must finish in page read */ 638371cdc07SQu Wenruo ASSERT(reads_done); 639371cdc07SQu Wenruo 640371cdc07SQu Wenruo eb->read_mirror = mirror; 641371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 642371cdc07SQu Wenruo ret = -EIO; 643371cdc07SQu Wenruo goto err; 644371cdc07SQu Wenruo } 645371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 646371cdc07SQu Wenruo if (ret < 0) 647371cdc07SQu Wenruo goto err; 648371cdc07SQu Wenruo 649371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 650371cdc07SQu Wenruo 651371cdc07SQu Wenruo free_extent_buffer(eb); 652371cdc07SQu Wenruo return ret; 653371cdc07SQu Wenruo err: 654371cdc07SQu Wenruo /* 655371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 656371cdc07SQu Wenruo * make sure it has something to decrement. 657371cdc07SQu Wenruo */ 658371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 659371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 660371cdc07SQu Wenruo free_extent_buffer(eb); 661371cdc07SQu Wenruo return ret; 662371cdc07SQu Wenruo } 663371cdc07SQu Wenruo 664c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 66577bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 66677bf40a2SQu Wenruo int mirror) 66777bf40a2SQu Wenruo { 66877bf40a2SQu Wenruo struct extent_buffer *eb; 66977bf40a2SQu Wenruo int ret = 0; 67077bf40a2SQu Wenruo int reads_done; 67177bf40a2SQu Wenruo 67277bf40a2SQu Wenruo ASSERT(page->private); 673371cdc07SQu Wenruo 674fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 675371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 676371cdc07SQu Wenruo 67777bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 67877bf40a2SQu Wenruo 67977bf40a2SQu Wenruo /* 68077bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 68177bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 68277bf40a2SQu Wenruo */ 68377bf40a2SQu Wenruo atomic_inc(&eb->refs); 68477bf40a2SQu Wenruo 68577bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 68677bf40a2SQu Wenruo if (!reads_done) 68777bf40a2SQu Wenruo goto err; 68877bf40a2SQu Wenruo 68977bf40a2SQu Wenruo eb->read_mirror = mirror; 69077bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 69177bf40a2SQu Wenruo ret = -EIO; 69277bf40a2SQu Wenruo goto err; 69377bf40a2SQu Wenruo } 69477bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 695ce9adaa5SChris Mason err: 69653b381b3SDavid Woodhouse if (ret) { 69753b381b3SDavid Woodhouse /* 69853b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 69953b381b3SDavid Woodhouse * again, we have to make sure it has something 70053b381b3SDavid Woodhouse * to decrement 70153b381b3SDavid Woodhouse */ 70253b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7030b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 70453b381b3SDavid Woodhouse } 7050b32f4bbSJosef Bacik free_extent_buffer(eb); 70677bf40a2SQu Wenruo 707f188591eSChris Mason return ret; 708ce9adaa5SChris Mason } 709ce9adaa5SChris Mason 7104a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7114a69a410SChris Mason { 7124a69a410SChris Mason struct async_submit_bio *async; 7134e4cbee9SChristoph Hellwig blk_status_t ret; 7144a69a410SChris Mason 7154a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7161941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 7171941b64bSQu Wenruo async->dio_file_offset); 71879787eaaSJeff Mahoney if (ret) 7194e4cbee9SChristoph Hellwig async->status = ret; 7204a69a410SChris Mason } 7214a69a410SChris Mason 72206ea01b1SDavid Sterba /* 72306ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 72406ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 72506ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 72606ea01b1SDavid Sterba * 72706ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 72806ea01b1SDavid Sterba * inserted into the tree. 72906ea01b1SDavid Sterba */ 7304a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7318b712842SChris Mason { 7328b712842SChris Mason struct async_submit_bio *async; 73306ea01b1SDavid Sterba struct inode *inode; 73406ea01b1SDavid Sterba blk_status_t ret; 7358b712842SChris Mason 7368b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7378896a08dSQu Wenruo inode = async->inode; 7384854ddd0SChris Mason 739bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7404e4cbee9SChristoph Hellwig if (async->status) { 7414e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7424246a0b6SChristoph Hellwig bio_endio(async->bio); 74379787eaaSJeff Mahoney return; 74479787eaaSJeff Mahoney } 74579787eaaSJeff Mahoney 746ec39f769SChris Mason /* 747ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 748ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 749ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 750ec39f769SChris Mason */ 751ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 75208635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 75306ea01b1SDavid Sterba if (ret) { 75406ea01b1SDavid Sterba async->bio->bi_status = ret; 75506ea01b1SDavid Sterba bio_endio(async->bio); 75606ea01b1SDavid Sterba } 7574a69a410SChris Mason } 7584a69a410SChris Mason 7594a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7604a69a410SChris Mason { 7614a69a410SChris Mason struct async_submit_bio *async; 7624a69a410SChris Mason 7634a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7648b712842SChris Mason kfree(async); 7658b712842SChris Mason } 7668b712842SChris Mason 7678896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 768a6f5e39eSDavid Sterba int mirror_num, u64 dio_file_offset, 769e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 77044b8bd7eSChris Mason { 7718896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 77244b8bd7eSChris Mason struct async_submit_bio *async; 77344b8bd7eSChris Mason 77444b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 77544b8bd7eSChris Mason if (!async) 7764e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 77744b8bd7eSChris Mason 7788896a08dSQu Wenruo async->inode = inode; 77944b8bd7eSChris Mason async->bio = bio; 78044b8bd7eSChris Mason async->mirror_num = mirror_num; 7814a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7824a69a410SChris Mason 783a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 784a0cac0ecSOmar Sandoval run_one_async_free); 7854a69a410SChris Mason 7861941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 7878c8bee1dSChris Mason 7884e4cbee9SChristoph Hellwig async->status = 0; 78979787eaaSJeff Mahoney 79067f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 791a31b4a43SChristoph Hellwig btrfs_queue_work(fs_info->hipri_workers, &async->work); 792a31b4a43SChristoph Hellwig else 7935cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 79444b8bd7eSChris Mason return 0; 79544b8bd7eSChris Mason } 79644b8bd7eSChris Mason 7974e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 798ce3ed71aSChris Mason { 7992c30c71bSKent Overstreet struct bio_vec *bvec; 800ce3ed71aSChris Mason struct btrfs_root *root; 8012b070cfeSChristoph Hellwig int ret = 0; 8026dc4f100SMing Lei struct bvec_iter_all iter_all; 803ce3ed71aSChris Mason 804c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8052b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 806ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 807ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 80879787eaaSJeff Mahoney if (ret) 80979787eaaSJeff Mahoney break; 810ce3ed71aSChris Mason } 8112c30c71bSKent Overstreet 8124e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 813ce3ed71aSChris Mason } 814ce3ed71aSChris Mason 8158896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 8161941b64bSQu Wenruo u64 dio_file_offset) 81722c59948SChris Mason { 8188b712842SChris Mason /* 8198b712842SChris Mason * when we're called for a write, we're already in the async 8205443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8218b712842SChris Mason */ 82279787eaaSJeff Mahoney return btree_csum_one_bio(bio); 82322c59948SChris Mason } 82422c59948SChris Mason 825f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 8269b4e675aSDavid Sterba struct btrfs_inode *bi) 827de0022b9SJosef Bacik { 8284eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 829f4dcfb30SJohannes Thumshirn return false; 8306300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 831f4dcfb30SJohannes Thumshirn return false; 8329b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 833f4dcfb30SJohannes Thumshirn return false; 834f4dcfb30SJohannes Thumshirn return true; 835de0022b9SJosef Bacik } 836de0022b9SJosef Bacik 83794d9e11bSChristoph Hellwig void btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, int mirror_num) 83844b8bd7eSChris Mason { 8390b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8404e4cbee9SChristoph Hellwig blk_status_t ret; 841cad321adSChris Mason 84208a6f464SChristoph Hellwig bio->bi_opf |= REQ_META; 84308a6f464SChristoph Hellwig 844cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 84508635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 846f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 847de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 84894d9e11bSChristoph Hellwig if (!ret) 84908635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 85061891923SStefan Behrens } else { 851cad321adSChris Mason /* 85261891923SStefan Behrens * kthread helpers are used to submit writes so that 85361891923SStefan Behrens * checksumming can happen in parallel across all CPUs 854cad321adSChris Mason */ 8558896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 856a6f5e39eSDavid Sterba btree_submit_bio_start); 85744b8bd7eSChris Mason } 85844b8bd7eSChris Mason 85994d9e11bSChristoph Hellwig if (ret) { 8604e4cbee9SChristoph Hellwig bio->bi_status = ret; 8614246a0b6SChristoph Hellwig bio_endio(bio); 86294d9e11bSChristoph Hellwig } 86361891923SStefan Behrens } 86461891923SStefan Behrens 8653dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 866784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 867a6bc32b8SMel Gorman struct page *newpage, struct page *page, 868a6bc32b8SMel Gorman enum migrate_mode mode) 869784b4e29SChris Mason { 870784b4e29SChris Mason /* 871784b4e29SChris Mason * we can't safely write a btree page from here, 872784b4e29SChris Mason * we haven't done the locking hook 873784b4e29SChris Mason */ 874784b4e29SChris Mason if (PageDirty(page)) 875784b4e29SChris Mason return -EAGAIN; 876784b4e29SChris Mason /* 877784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 878784b4e29SChris Mason * We must have no buffers or drop them. 879784b4e29SChris Mason */ 880784b4e29SChris Mason if (page_has_private(page) && 881784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 882784b4e29SChris Mason return -EAGAIN; 883a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 884784b4e29SChris Mason } 8853dd1462eSJan Beulich #endif 886784b4e29SChris Mason 8870da5468fSChris Mason 8880da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8890da5468fSChris Mason struct writeback_control *wbc) 8900da5468fSChris Mason { 891e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 892e2d84521SMiao Xie int ret; 893e2d84521SMiao Xie 894d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 895448d640bSChris Mason 896448d640bSChris Mason if (wbc->for_kupdate) 897448d640bSChris Mason return 0; 898448d640bSChris Mason 899e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 900b9473439SChris Mason /* this is a bit racy, but that's ok */ 901d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 902d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 903d814a491SEthan Lien fs_info->dirty_metadata_batch); 904e2d84521SMiao Xie if (ret < 0) 905793955bcSChris Mason return 0; 906793955bcSChris Mason } 9070b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9080da5468fSChris Mason } 9090da5468fSChris Mason 910f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags) 9115f39d397SChris Mason { 912f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio)) 913f913cff3SMatthew Wilcox (Oracle) return false; 9140c4e538bSDavid Sterba 915f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page); 916d98237b3SChris Mason } 917d98237b3SChris Mason 918895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 919895586ebSMatthew Wilcox (Oracle) size_t length) 920d98237b3SChris Mason { 921d1310b2eSChris Mason struct extent_io_tree *tree; 922895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 923895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 924f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS); 925895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 926895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 927895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 928895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 929895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 9309ad6b7bcSChris Mason } 931d98237b3SChris Mason } 932d98237b3SChris Mason 933bb146eb2SJosef Bacik #ifdef DEBUG 9340079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 9350079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 9360079c3b1SMatthew Wilcox (Oracle) { 9370079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 938139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 9390b32f4bbSJosef Bacik struct extent_buffer *eb; 940139e8cd3SQu Wenruo int cur_bit = 0; 9410079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 9420b32f4bbSJosef Bacik 943139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 9440079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 9450b32f4bbSJosef Bacik BUG_ON(!eb); 9460b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9470b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 94849d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 9490079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 950139e8cd3SQu Wenruo } 9510079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 952139e8cd3SQu Wenruo 953139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 954139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 955139e8cd3SQu Wenruo unsigned long flags; 956139e8cd3SQu Wenruo u64 cur; 957139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 958139e8cd3SQu Wenruo 959139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 960139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 961139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 962139e8cd3SQu Wenruo cur_bit++; 963139e8cd3SQu Wenruo continue; 964139e8cd3SQu Wenruo } 965139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 966139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 967139e8cd3SQu Wenruo 968139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 969139e8cd3SQu Wenruo ASSERT(eb); 970139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 971139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 97249d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 973139e8cd3SQu Wenruo free_extent_buffer(eb); 974139e8cd3SQu Wenruo 975139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 976139e8cd3SQu Wenruo } 9770079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 9780b32f4bbSJosef Bacik } 9790079c3b1SMatthew Wilcox (Oracle) #else 9800079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 9810079c3b1SMatthew Wilcox (Oracle) #endif 9820b32f4bbSJosef Bacik 9837f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9840da5468fSChris Mason .writepages = btree_writepages, 985f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio, 986895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 9875a92bc88SChris Mason #ifdef CONFIG_MIGRATION 988784b4e29SChris Mason .migratepage = btree_migratepage, 9895a92bc88SChris Mason #endif 9900079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 991d98237b3SChris Mason }; 992123abc88SChris Mason 9932ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9942ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9953fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 9963fbaf258SJosef Bacik int level) 9970999df54SChris Mason { 9980b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9990b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10003fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10010999df54SChris Mason } 10020999df54SChris Mason 1003581c1760SQu Wenruo /* 1004581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1005581c1760SQu Wenruo * verification. 1006581c1760SQu Wenruo * 10071b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1008581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1009581c1760SQu Wenruo * @level: expected level, mandatory check 1010581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1011581c1760SQu Wenruo */ 10122ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 10131b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 10141b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1015e20d96d6SChris Mason { 10165f39d397SChris Mason struct extent_buffer *buf = NULL; 101719c00ddcSChris Mason int ret; 101819c00ddcSChris Mason 10193fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1020c871b0f2SLiu Bo if (IS_ERR(buf)) 1021c871b0f2SLiu Bo return buf; 1022e4204dedSChris Mason 10236a2e9dc4SFilipe Manana ret = btrfs_read_extent_buffer(buf, parent_transid, level, first_key); 10240f0fe8f7SFilipe David Borba Manana if (ret) { 1025537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 102664c043deSLiu Bo return ERR_PTR(ret); 10270f0fe8f7SFilipe David Borba Manana } 102888c602abSQu Wenruo if (btrfs_check_eb_owner(buf, owner_root)) { 102988c602abSQu Wenruo free_extent_buffer_stale(buf); 103088c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 103188c602abSQu Wenruo } 10325f39d397SChris Mason return buf; 1033ce9adaa5SChris Mason 1034eb60ceacSChris Mason } 1035eb60ceacSChris Mason 10366a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1037ed2ff2cbSChris Mason { 10386a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 103955c69072SChris Mason if (btrfs_header_generation(buf) == 1040e2d84521SMiao Xie fs_info->running_transaction->transid) { 104149d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1042b4ce94deSChris Mason 1043b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1044104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1045e2d84521SMiao Xie -buf->len, 1046e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10470b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1048925baeddSChris Mason } 10495f39d397SChris Mason } 1050ed7b63ebSJosef Bacik } 10515f39d397SChris Mason 1052da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1053e20d96d6SChris Mason u64 objectid) 1054d97e63b6SChris Mason { 10557c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 10562e608bd1SJosef Bacik 10572e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 10582e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 10592e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 106096dfcb46SJosef Bacik root->fs_info = fs_info; 10612e608bd1SJosef Bacik root->root_key.objectid = objectid; 1062cfaa7295SChris Mason root->node = NULL; 1063a28ec197SChris Mason root->commit_root = NULL; 106427cdeb70SMiao Xie root->state = 0; 1065abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 10660b86a832SChris Mason 10670f7d52f4SChris Mason root->last_trans = 0; 10686b8fad57SNikolay Borisov root->free_objectid = 0; 1069eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1070199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10716bef4d31SEric Paris root->inode_tree = RB_ROOT; 1072088aea3bSDavid Sterba INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 10732e608bd1SJosef Bacik 10742e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 10750b86a832SChris Mason 10760b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10775d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1078eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1079eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1080199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1081199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1082d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10832ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10842ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10855d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1086eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1087199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1088f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10892ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10902ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10918287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1092a2135011SChris Mason mutex_init(&root->objectid_mutex); 1093e02119d5SChris Mason mutex_init(&root->log_mutex); 109431f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1095573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1096c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10977237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10987237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10997237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11008b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11018b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11027237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11037237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11047237f183SYan Zheng atomic_set(&root->log_writers, 0); 11052ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11060700cea7SElena Reshetova refcount_set(&root->refs, 1); 11078ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1108eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11097237f183SYan Zheng root->log_transid = 0; 1110d1433debSMiao Xie root->log_transid_committed = -1; 1111257c62e1SChris Mason root->last_log_commit = 0; 11122e608bd1SJosef Bacik root->anon_dev = 0; 1113e289f03eSFilipe Manana if (!dummy) { 111443eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 111543eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1116e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1117e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1118e289f03eSFilipe Manana } 1119017e5369SChris Mason 11205f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1121370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1122bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1123bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1124fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1125bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1126fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1127bd647ce3SJosef Bacik #endif 11283768f368SChris Mason } 11293768f368SChris Mason 113074e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 113196dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11326f07e42eSAl Viro { 113374e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11346f07e42eSAl Viro if (root) 113596dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11366f07e42eSAl Viro return root; 11376f07e42eSAl Viro } 11386f07e42eSAl Viro 113906ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 114006ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1141da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 114206ea65a3SJosef Bacik { 114306ea65a3SJosef Bacik struct btrfs_root *root; 114406ea65a3SJosef Bacik 11457c0260eeSJeff Mahoney if (!fs_info) 11467c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11477c0260eeSJeff Mahoney 114896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 114906ea65a3SJosef Bacik if (!root) 115006ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1151da17066cSJeff Mahoney 1152b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1153faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 115406ea65a3SJosef Bacik 115506ea65a3SJosef Bacik return root; 115606ea65a3SJosef Bacik } 115706ea65a3SJosef Bacik #endif 115806ea65a3SJosef Bacik 1159abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1160abed4aaaSJosef Bacik { 1161abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1162abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1163abed4aaaSJosef Bacik 1164abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1165abed4aaaSJosef Bacik } 1166abed4aaaSJosef Bacik 1167abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1168abed4aaaSJosef Bacik { 1169abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1170abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1171abed4aaaSJosef Bacik 1172abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1173abed4aaaSJosef Bacik } 1174abed4aaaSJosef Bacik 1175abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1176abed4aaaSJosef Bacik { 1177abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1178abed4aaaSJosef Bacik struct rb_node *tmp; 1179abed4aaaSJosef Bacik 1180abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1181abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1182abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1183abed4aaaSJosef Bacik ASSERT(!tmp); 1184abed4aaaSJosef Bacik 1185abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1186abed4aaaSJosef Bacik } 1187abed4aaaSJosef Bacik 1188abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1189abed4aaaSJosef Bacik { 1190abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1191abed4aaaSJosef Bacik 1192abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1193abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1194abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1195abed4aaaSJosef Bacik } 1196abed4aaaSJosef Bacik 1197abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1198abed4aaaSJosef Bacik struct btrfs_key *key) 1199abed4aaaSJosef Bacik { 1200abed4aaaSJosef Bacik struct rb_node *node; 1201abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1202abed4aaaSJosef Bacik 1203abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1204abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1205abed4aaaSJosef Bacik if (node) 1206abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1207abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1208abed4aaaSJosef Bacik 1209abed4aaaSJosef Bacik return root; 1210abed4aaaSJosef Bacik } 1211abed4aaaSJosef Bacik 1212f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1213f7238e50SJosef Bacik { 1214f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1215f7238e50SJosef Bacik u64 ret; 1216f7238e50SJosef Bacik 1217f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1218f7238e50SJosef Bacik return 0; 1219f7238e50SJosef Bacik 1220f7238e50SJosef Bacik if (bytenr) 1221f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1222f7238e50SJosef Bacik else 1223f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1224f7238e50SJosef Bacik ASSERT(block_group); 1225f7238e50SJosef Bacik if (!block_group) 1226f7238e50SJosef Bacik return 0; 1227f7238e50SJosef Bacik ret = block_group->global_root_id; 1228f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1229f7238e50SJosef Bacik 1230f7238e50SJosef Bacik return ret; 1231f7238e50SJosef Bacik } 1232f7238e50SJosef Bacik 1233abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1234abed4aaaSJosef Bacik { 1235abed4aaaSJosef Bacik struct btrfs_key key = { 1236abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1237abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1238f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1239abed4aaaSJosef Bacik }; 1240abed4aaaSJosef Bacik 1241abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1242abed4aaaSJosef Bacik } 1243abed4aaaSJosef Bacik 1244abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1245abed4aaaSJosef Bacik { 1246abed4aaaSJosef Bacik struct btrfs_key key = { 1247abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1248abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1249f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1250abed4aaaSJosef Bacik }; 1251abed4aaaSJosef Bacik 1252abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1253abed4aaaSJosef Bacik } 1254abed4aaaSJosef Bacik 125520897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 125620897f5cSArne Jansen u64 objectid) 125720897f5cSArne Jansen { 12589b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 125920897f5cSArne Jansen struct extent_buffer *leaf; 126020897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 126120897f5cSArne Jansen struct btrfs_root *root; 126220897f5cSArne Jansen struct btrfs_key key; 1263b89f6d1fSFilipe Manana unsigned int nofs_flag; 126420897f5cSArne Jansen int ret = 0; 126520897f5cSArne Jansen 1266b89f6d1fSFilipe Manana /* 1267b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1268b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1269b89f6d1fSFilipe Manana */ 1270b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 127196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1272b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 127320897f5cSArne Jansen if (!root) 127420897f5cSArne Jansen return ERR_PTR(-ENOMEM); 127520897f5cSArne Jansen 127620897f5cSArne Jansen root->root_key.objectid = objectid; 127720897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 127820897f5cSArne Jansen root->root_key.offset = 0; 127920897f5cSArne Jansen 12809631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12819631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 128220897f5cSArne Jansen if (IS_ERR(leaf)) { 128320897f5cSArne Jansen ret = PTR_ERR(leaf); 12841dd05682STsutomu Itoh leaf = NULL; 12858a6a87cdSBoris Burkov goto fail_unlock; 128620897f5cSArne Jansen } 128720897f5cSArne Jansen 128820897f5cSArne Jansen root->node = leaf; 128920897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 129020897f5cSArne Jansen 129120897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129227cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 129320897f5cSArne Jansen 1294f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1295f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 129620897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 129720897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 129820897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 129920897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 130020897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 130120897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130220897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130333d85fdaSQu Wenruo if (is_fstree(objectid)) 1304807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1305807fc790SAndy Shevchenko else 1306807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1307c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 130820897f5cSArne Jansen 13098a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13108a6a87cdSBoris Burkov 131120897f5cSArne Jansen key.objectid = objectid; 131220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131320897f5cSArne Jansen key.offset = 0; 131420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 131520897f5cSArne Jansen if (ret) 131620897f5cSArne Jansen goto fail; 131720897f5cSArne Jansen 131820897f5cSArne Jansen return root; 13191dd05682STsutomu Itoh 13208a6a87cdSBoris Burkov fail_unlock: 13218c38938cSJosef Bacik if (leaf) 13221dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13238a6a87cdSBoris Burkov fail: 132400246528SJosef Bacik btrfs_put_root(root); 13251dd05682STsutomu Itoh 13261dd05682STsutomu Itoh return ERR_PTR(ret); 132720897f5cSArne Jansen } 132820897f5cSArne Jansen 13297237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1330e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13310f7d52f4SChris Mason { 13320f7d52f4SChris Mason struct btrfs_root *root; 1333e02119d5SChris Mason 133496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1335e02119d5SChris Mason if (!root) 13367237f183SYan Zheng return ERR_PTR(-ENOMEM); 1337e02119d5SChris Mason 1338e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1339e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1340e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 134127cdeb70SMiao Xie 13426ab6ebb7SNaohiro Aota return root; 13436ab6ebb7SNaohiro Aota } 13446ab6ebb7SNaohiro Aota 13456ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 13466ab6ebb7SNaohiro Aota struct btrfs_root *root) 13476ab6ebb7SNaohiro Aota { 13486ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 13496ab6ebb7SNaohiro Aota 13507237f183SYan Zheng /* 135192a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 135227cdeb70SMiao Xie * 135392a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 135492a7cc42SQu Wenruo * and they go away before a real commit is actually done. 135592a7cc42SQu Wenruo * 135692a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 135792a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 13587237f183SYan Zheng */ 1359e02119d5SChris Mason 13604d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13619631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 13626ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 13636ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1364e02119d5SChris Mason 13657237f183SYan Zheng root->node = leaf; 1366e02119d5SChris Mason 1367e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1368e02119d5SChris Mason btrfs_tree_unlock(root->node); 13696ab6ebb7SNaohiro Aota 13706ab6ebb7SNaohiro Aota return 0; 13717237f183SYan Zheng } 13727237f183SYan Zheng 13737237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13747237f183SYan Zheng struct btrfs_fs_info *fs_info) 13757237f183SYan Zheng { 13767237f183SYan Zheng struct btrfs_root *log_root; 13777237f183SYan Zheng 13787237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13797237f183SYan Zheng if (IS_ERR(log_root)) 13807237f183SYan Zheng return PTR_ERR(log_root); 13816ab6ebb7SNaohiro Aota 13823ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 13833ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 13843ddebf27SNaohiro Aota 13856ab6ebb7SNaohiro Aota if (ret) { 13866ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13876ab6ebb7SNaohiro Aota return ret; 13886ab6ebb7SNaohiro Aota } 13893ddebf27SNaohiro Aota } 13906ab6ebb7SNaohiro Aota 13917237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13927237f183SYan Zheng fs_info->log_root_tree = log_root; 13937237f183SYan Zheng return 0; 13947237f183SYan Zheng } 13957237f183SYan Zheng 13967237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13977237f183SYan Zheng struct btrfs_root *root) 13987237f183SYan Zheng { 13990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14007237f183SYan Zheng struct btrfs_root *log_root; 14017237f183SYan Zheng struct btrfs_inode_item *inode_item; 14026ab6ebb7SNaohiro Aota int ret; 14037237f183SYan Zheng 14040b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14057237f183SYan Zheng if (IS_ERR(log_root)) 14067237f183SYan Zheng return PTR_ERR(log_root); 14077237f183SYan Zheng 14086ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14096ab6ebb7SNaohiro Aota if (ret) { 14106ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14116ab6ebb7SNaohiro Aota return ret; 14126ab6ebb7SNaohiro Aota } 14136ab6ebb7SNaohiro Aota 14147237f183SYan Zheng log_root->last_trans = trans->transid; 14157237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14167237f183SYan Zheng 14177237f183SYan Zheng inode_item = &log_root->root_item.inode; 14183cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14193cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14203cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1421da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14220b246afaSJeff Mahoney fs_info->nodesize); 14233cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14247237f183SYan Zheng 14255d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14267237f183SYan Zheng 14277237f183SYan Zheng WARN_ON(root->log_root); 14287237f183SYan Zheng root->log_root = log_root; 14297237f183SYan Zheng root->log_transid = 0; 1430d1433debSMiao Xie root->log_transid_committed = -1; 1431257c62e1SChris Mason root->last_log_commit = 0; 1432e02119d5SChris Mason return 0; 1433e02119d5SChris Mason } 1434e02119d5SChris Mason 143549d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 143649d11beaSJosef Bacik struct btrfs_path *path, 1437cb517eabSMiao Xie struct btrfs_key *key) 1438e02119d5SChris Mason { 1439e02119d5SChris Mason struct btrfs_root *root; 1440e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 144184234f3aSYan Zheng u64 generation; 1442cb517eabSMiao Xie int ret; 1443581c1760SQu Wenruo int level; 1444cb517eabSMiao Xie 144596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 144649d11beaSJosef Bacik if (!root) 144749d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 14480f7d52f4SChris Mason 1449cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1450cb517eabSMiao Xie &root->root_item, &root->root_key); 14510f7d52f4SChris Mason if (ret) { 145213a8a7c8SYan, Zheng if (ret > 0) 145313a8a7c8SYan, Zheng ret = -ENOENT; 145449d11beaSJosef Bacik goto fail; 14550f7d52f4SChris Mason } 145613a8a7c8SYan, Zheng 145784234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1458581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14592ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14602ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 14611b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 146264c043deSLiu Bo if (IS_ERR(root->node)) { 146364c043deSLiu Bo ret = PTR_ERR(root->node); 14648c38938cSJosef Bacik root->node = NULL; 146549d11beaSJosef Bacik goto fail; 14664eb150d6SQu Wenruo } 14674eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1468cb517eabSMiao Xie ret = -EIO; 146949d11beaSJosef Bacik goto fail; 1470416bc658SJosef Bacik } 147188c602abSQu Wenruo 147288c602abSQu Wenruo /* 147388c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 147488c602abSQu Wenruo * match its root node owner 147588c602abSQu Wenruo */ 147688c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 147788c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 147888c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 147988c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 148088c602abSQu Wenruo btrfs_crit(fs_info, 148188c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 148288c602abSQu Wenruo root->root_key.objectid, root->node->start, 148388c602abSQu Wenruo btrfs_header_owner(root->node), 148488c602abSQu Wenruo root->root_key.objectid); 148588c602abSQu Wenruo ret = -EUCLEAN; 148688c602abSQu Wenruo goto fail; 148788c602abSQu Wenruo } 14885d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1489cb517eabSMiao Xie return root; 149049d11beaSJosef Bacik fail: 149100246528SJosef Bacik btrfs_put_root(root); 149249d11beaSJosef Bacik return ERR_PTR(ret); 149349d11beaSJosef Bacik } 149449d11beaSJosef Bacik 149549d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 149649d11beaSJosef Bacik struct btrfs_key *key) 149749d11beaSJosef Bacik { 149849d11beaSJosef Bacik struct btrfs_root *root; 149949d11beaSJosef Bacik struct btrfs_path *path; 150049d11beaSJosef Bacik 150149d11beaSJosef Bacik path = btrfs_alloc_path(); 150249d11beaSJosef Bacik if (!path) 150349d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 150449d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 150549d11beaSJosef Bacik btrfs_free_path(path); 150649d11beaSJosef Bacik 150749d11beaSJosef Bacik return root; 1508cb517eabSMiao Xie } 1509cb517eabSMiao Xie 15102dfb1e43SQu Wenruo /* 15112dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15122dfb1e43SQu Wenruo * 15132dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 15142dfb1e43SQu Wenruo */ 15152dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1516cb517eabSMiao Xie { 1517cb517eabSMiao Xie int ret; 1518dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1519cb517eabSMiao Xie 1520dcc3eb96SNikolay Borisov /* 1521dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1522dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1523dcc3eb96SNikolay Borisov */ 1524dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1525dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1526dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1527dcc3eb96SNikolay Borisov if (ret) 15288257b2dcSMiao Xie goto fail; 15298257b2dcSMiao Xie 1530aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 153137f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 153292a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1533f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1534f39e4571SJosef Bacik } 1535f39e4571SJosef Bacik 1536851fd730SQu Wenruo /* 1537851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1538851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1539851fd730SQu Wenruo */ 1540851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1541851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 15422dfb1e43SQu Wenruo if (!anon_dev) { 1543cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1544cb517eabSMiao Xie if (ret) 1545876d2cf1SLiu Bo goto fail; 15462dfb1e43SQu Wenruo } else { 15472dfb1e43SQu Wenruo root->anon_dev = anon_dev; 15482dfb1e43SQu Wenruo } 1549851fd730SQu Wenruo } 1550f32e48e9SChandan Rajendra 1551f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1552453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1553f32e48e9SChandan Rajendra if (ret) { 1554f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1555876d2cf1SLiu Bo goto fail; 1556f32e48e9SChandan Rajendra } 1557f32e48e9SChandan Rajendra 15586b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1559f32e48e9SChandan Rajendra 1560f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1561f32e48e9SChandan Rajendra 1562cb517eabSMiao Xie return 0; 1563cb517eabSMiao Xie fail: 156484db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1565cb517eabSMiao Xie return ret; 1566cb517eabSMiao Xie } 1567cb517eabSMiao Xie 1568a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1569cb517eabSMiao Xie u64 root_id) 1570cb517eabSMiao Xie { 1571cb517eabSMiao Xie struct btrfs_root *root; 1572cb517eabSMiao Xie 1573fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1574fc7cbcd4SDavid Sterba root = radix_tree_lookup(&fs_info->fs_roots_radix, 1575fc7cbcd4SDavid Sterba (unsigned long)root_id); 1576bc44d7c4SJosef Bacik if (root) 157700246528SJosef Bacik root = btrfs_grab_root(root); 1578fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1579cb517eabSMiao Xie return root; 1580cb517eabSMiao Xie } 1581cb517eabSMiao Xie 158249d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 158349d11beaSJosef Bacik u64 objectid) 158449d11beaSJosef Bacik { 1585abed4aaaSJosef Bacik struct btrfs_key key = { 1586abed4aaaSJosef Bacik .objectid = objectid, 1587abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1588abed4aaaSJosef Bacik .offset = 0, 1589abed4aaaSJosef Bacik }; 1590abed4aaaSJosef Bacik 159149d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 159249d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 159349d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1594abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 159549d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 159649d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 159749d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 159849d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 159949d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1600abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 160149d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 160249d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 160349d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 160449d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 160549d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 160649d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 1607abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1608abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1609abed4aaaSJosef Bacik 1610abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1611abed4aaaSJosef Bacik } 161249d11beaSJosef Bacik return NULL; 161349d11beaSJosef Bacik } 161449d11beaSJosef Bacik 1615cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1616cb517eabSMiao Xie struct btrfs_root *root) 1617cb517eabSMiao Xie { 1618cb517eabSMiao Xie int ret; 1619cb517eabSMiao Xie 1620fc7cbcd4SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1621fc7cbcd4SDavid Sterba if (ret) 1622fc7cbcd4SDavid Sterba return ret; 1623fc7cbcd4SDavid Sterba 1624fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1625fc7cbcd4SDavid Sterba ret = radix_tree_insert(&fs_info->fs_roots_radix, 1626fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid, 1627fc7cbcd4SDavid Sterba root); 1628af01d2e5SJosef Bacik if (ret == 0) { 162900246528SJosef Bacik btrfs_grab_root(root); 1630fc7cbcd4SDavid Sterba set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1631af01d2e5SJosef Bacik } 1632fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1633fc7cbcd4SDavid Sterba radix_tree_preload_end(); 1634cb517eabSMiao Xie 1635cb517eabSMiao Xie return ret; 1636cb517eabSMiao Xie } 1637cb517eabSMiao Xie 1638bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1639bd647ce3SJosef Bacik { 1640bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1641bd647ce3SJosef Bacik struct btrfs_root *root; 1642bd647ce3SJosef Bacik 1643bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1644457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1645457f1864SJosef Bacik 1646bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1647bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1648457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 164971008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1650bd647ce3SJosef Bacik refcount_read(&root->refs)); 1651bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 165200246528SJosef Bacik btrfs_put_root(root); 165300246528SJosef Bacik btrfs_put_root(root); 1654bd647ce3SJosef Bacik } 1655bd647ce3SJosef Bacik #endif 1656bd647ce3SJosef Bacik } 1657bd647ce3SJosef Bacik 1658abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1659abed4aaaSJosef Bacik { 1660abed4aaaSJosef Bacik struct btrfs_root *root; 1661abed4aaaSJosef Bacik struct rb_node *node; 1662abed4aaaSJosef Bacik 1663abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1664abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1665abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1666abed4aaaSJosef Bacik btrfs_put_root(root); 1667abed4aaaSJosef Bacik } 1668abed4aaaSJosef Bacik } 1669abed4aaaSJosef Bacik 16700d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 16710d4b0463SJosef Bacik { 1672141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1673141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 16745deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1675141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1676141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1677141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1678141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 16790d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 16800d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1681abed4aaaSJosef Bacik free_global_roots(fs_info); 168200246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 168300246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 168400246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 168500246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 168600246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 168700246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1688aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 16899c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1690bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 16913fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 16920d4b0463SJosef Bacik kfree(fs_info->super_copy); 16930d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 16948481dd80SQu Wenruo kfree(fs_info->subpage_info); 16950d4b0463SJosef Bacik kvfree(fs_info); 16960d4b0463SJosef Bacik } 16970d4b0463SJosef Bacik 16980d4b0463SJosef Bacik 16992dfb1e43SQu Wenruo /* 17002dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 17012dfb1e43SQu Wenruo * 17022dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 17032dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 17042dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 17052dfb1e43SQu Wenruo * 17062dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 17072dfb1e43SQu Wenruo * 17082dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 17092dfb1e43SQu Wenruo * same root objectid. 17102dfb1e43SQu Wenruo * 17112dfb1e43SQu Wenruo * @objectid: root id 17122dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 17132dfb1e43SQu Wenruo * pass 0 for new allocation. 17142dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 17152dfb1e43SQu Wenruo * for orphan roots 17162dfb1e43SQu Wenruo */ 17172dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 17182dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 17192dfb1e43SQu Wenruo bool check_ref) 17205eda7b5eSChris Mason { 17215eda7b5eSChris Mason struct btrfs_root *root; 1722381cf658SDavid Sterba struct btrfs_path *path; 17231d4c08e0SDavid Sterba struct btrfs_key key; 17245eda7b5eSChris Mason int ret; 17255eda7b5eSChris Mason 172649d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 172749d11beaSJosef Bacik if (root) 172849d11beaSJosef Bacik return root; 17294df27c4dSYan, Zheng again: 173056e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 173148475471SStefan Behrens if (root) { 17322dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17332dfb1e43SQu Wenruo ASSERT(!anon_dev); 1734bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 173500246528SJosef Bacik btrfs_put_root(root); 173648475471SStefan Behrens return ERR_PTR(-ENOENT); 1737bc44d7c4SJosef Bacik } 17385eda7b5eSChris Mason return root; 173948475471SStefan Behrens } 17405eda7b5eSChris Mason 174156e9357aSDavid Sterba key.objectid = objectid; 174256e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 174356e9357aSDavid Sterba key.offset = (u64)-1; 174456e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 17455eda7b5eSChris Mason if (IS_ERR(root)) 17465eda7b5eSChris Mason return root; 17473394e160SChris Mason 1748c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1749d68fc57bSYan, Zheng ret = -ENOENT; 1750d68fc57bSYan, Zheng goto fail; 1751d68fc57bSYan, Zheng } 1752d68fc57bSYan, Zheng 17532dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1754cb517eabSMiao Xie if (ret) 1755cb517eabSMiao Xie goto fail; 1756cb517eabSMiao Xie 1757381cf658SDavid Sterba path = btrfs_alloc_path(); 1758381cf658SDavid Sterba if (!path) { 1759381cf658SDavid Sterba ret = -ENOMEM; 1760381cf658SDavid Sterba goto fail; 1761381cf658SDavid Sterba } 17621d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17631d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 176456e9357aSDavid Sterba key.offset = objectid; 17651d4c08e0SDavid Sterba 17661d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1767381cf658SDavid Sterba btrfs_free_path(path); 1768d68fc57bSYan, Zheng if (ret < 0) 1769d68fc57bSYan, Zheng goto fail; 1770d68fc57bSYan, Zheng if (ret == 0) 177127cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1772d68fc57bSYan, Zheng 1773cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17740f7d52f4SChris Mason if (ret) { 1775168a2f77SJia-Ju Bai if (ret == -EEXIST) { 177600246528SJosef Bacik btrfs_put_root(root); 17774df27c4dSYan, Zheng goto again; 1778168a2f77SJia-Ju Bai } 17794df27c4dSYan, Zheng goto fail; 17804df27c4dSYan, Zheng } 1781edbd8d4eSChris Mason return root; 17824df27c4dSYan, Zheng fail: 178333fab972SFilipe Manana /* 178433fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 1785143823cfSDavid Sterba * responsibility to free it in case we fail. So we have to set our 178633fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 178733fab972SFilipe Manana * and once again by our caller. 178833fab972SFilipe Manana */ 178933fab972SFilipe Manana if (anon_dev) 179033fab972SFilipe Manana root->anon_dev = 0; 17918c38938cSJosef Bacik btrfs_put_root(root); 17924df27c4dSYan, Zheng return ERR_PTR(ret); 1793edbd8d4eSChris Mason } 1794edbd8d4eSChris Mason 17952dfb1e43SQu Wenruo /* 17962dfb1e43SQu Wenruo * Get in-memory reference of a root structure 17972dfb1e43SQu Wenruo * 17982dfb1e43SQu Wenruo * @objectid: tree objectid 17992dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 18002dfb1e43SQu Wenruo * one reference 18012dfb1e43SQu Wenruo */ 18022dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 18032dfb1e43SQu Wenruo u64 objectid, bool check_ref) 18042dfb1e43SQu Wenruo { 18052dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 18062dfb1e43SQu Wenruo } 18072dfb1e43SQu Wenruo 18082dfb1e43SQu Wenruo /* 18092dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 18102dfb1e43SQu Wenruo * the anonymous block device id 18112dfb1e43SQu Wenruo * 18122dfb1e43SQu Wenruo * @objectid: tree objectid 18132dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 18142dfb1e43SQu Wenruo * parameter value 18152dfb1e43SQu Wenruo */ 18162dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 18172dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 18182dfb1e43SQu Wenruo { 18192dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 18202dfb1e43SQu Wenruo } 18212dfb1e43SQu Wenruo 18228b712842SChris Mason /* 182349d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 182449d11beaSJosef Bacik * @fs_info: the fs_info 182549d11beaSJosef Bacik * @objectid: the objectid we need to lookup 182649d11beaSJosef Bacik * 182749d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 182849d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 182949d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 183049d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 183149d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 183249d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 183349d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 183449d11beaSJosef Bacik * backref code is finished using the root. 183549d11beaSJosef Bacik */ 183649d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 183749d11beaSJosef Bacik struct btrfs_path *path, 183849d11beaSJosef Bacik u64 objectid) 183949d11beaSJosef Bacik { 184049d11beaSJosef Bacik struct btrfs_root *root; 184149d11beaSJosef Bacik struct btrfs_key key; 184249d11beaSJosef Bacik 184349d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 184449d11beaSJosef Bacik 184549d11beaSJosef Bacik /* 184649d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 184749d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 184849d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 184949d11beaSJosef Bacik * != NULL just return it. 185049d11beaSJosef Bacik */ 185149d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 185249d11beaSJosef Bacik if (root) 185349d11beaSJosef Bacik return root; 185449d11beaSJosef Bacik 185549d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 185649d11beaSJosef Bacik if (root) 185749d11beaSJosef Bacik return root; 185849d11beaSJosef Bacik 185949d11beaSJosef Bacik key.objectid = objectid; 186049d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 186149d11beaSJosef Bacik key.offset = (u64)-1; 186249d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 186349d11beaSJosef Bacik btrfs_release_path(path); 186449d11beaSJosef Bacik 186549d11beaSJosef Bacik return root; 186649d11beaSJosef Bacik } 186749d11beaSJosef Bacik 1868a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1869a74a4b97SChris Mason { 18700d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1871d0278245SMiao Xie int again; 1872a74a4b97SChris Mason 1873d6fd0ae2SOmar Sandoval while (1) { 1874d0278245SMiao Xie again = 0; 18759d1a2a3aSDavid Sterba 1876fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1877fd340d0fSJosef Bacik 1878d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18792ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1880d0278245SMiao Xie goto sleep; 1881d0278245SMiao Xie 188290c711abSZygo Blaxell /* 188390c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 188490c711abSZygo Blaxell * before we have finished mounting the filesystem. 188590c711abSZygo Blaxell */ 18860b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 188790c711abSZygo Blaxell goto sleep; 188890c711abSZygo Blaxell 18890b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1890d0278245SMiao Xie goto sleep; 1891d0278245SMiao Xie 1892dc7f370cSMiao Xie /* 1893dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1894dc7f370cSMiao Xie * during the above check and trylock. 1895dc7f370cSMiao Xie */ 18962ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18970b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1898dc7f370cSMiao Xie goto sleep; 1899dc7f370cSMiao Xie } 1900dc7f370cSMiao Xie 19012ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1902c2d6cb16SFilipe Manana 190333c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 19040b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1905a74a4b97SChris Mason 1906d0278245SMiao Xie /* 190705323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 190805323cd1SMiao Xie * needn't do anything special here. 1909d0278245SMiao Xie */ 19100b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 191167c5e7d4SFilipe Manana 191267c5e7d4SFilipe Manana /* 1913f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 191467c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 191567c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1916f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 191767c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 191867c5e7d4SFilipe Manana * unused block groups. 191967c5e7d4SFilipe Manana */ 19200b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 192118bb8bbfSJohannes Thumshirn 192218bb8bbfSJohannes Thumshirn /* 192318bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 192418bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 192518bb8bbfSJohannes Thumshirn * space. 192618bb8bbfSJohannes Thumshirn */ 192718bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 1928d0278245SMiao Xie sleep: 1929a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1930d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1931d6fd0ae2SOmar Sandoval kthread_parkme(); 1932d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1933d6fd0ae2SOmar Sandoval return 0; 1934838fe188SJiri Kosina if (!again) { 1935a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1936a74a4b97SChris Mason schedule(); 1937a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1938a74a4b97SChris Mason } 1939da288d28SFilipe Manana } 1940a74a4b97SChris Mason } 1941a74a4b97SChris Mason 1942a74a4b97SChris Mason static int transaction_kthread(void *arg) 1943a74a4b97SChris Mason { 1944a74a4b97SChris Mason struct btrfs_root *root = arg; 19450b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1946a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1947a74a4b97SChris Mason struct btrfs_transaction *cur; 19488929ecfaSYan, Zheng u64 transid; 1949643900beSNikolay Borisov time64_t delta; 1950a74a4b97SChris Mason unsigned long delay; 1951914b2007SJan Kara bool cannot_commit; 1952a74a4b97SChris Mason 1953a74a4b97SChris Mason do { 1954914b2007SJan Kara cannot_commit = false; 1955ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 19560b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1957a74a4b97SChris Mason 19580b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19590b246afaSJeff Mahoney cur = fs_info->running_transaction; 1960a74a4b97SChris Mason if (!cur) { 19610b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1962a74a4b97SChris Mason goto sleep; 1963a74a4b97SChris Mason } 196431153d81SYan Zheng 1965643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 1966fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 1967fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 1968643900beSNikolay Borisov delta < fs_info->commit_interval) { 19690b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1970fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1971fb8a7e94SNikolay Borisov delay = min(delay, 1972fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1973a74a4b97SChris Mason goto sleep; 1974a74a4b97SChris Mason } 19758929ecfaSYan, Zheng transid = cur->transid; 19760b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 197756bec294SChris Mason 197879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1979354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1980914b2007SJan Kara if (IS_ERR(trans)) { 1981354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1982914b2007SJan Kara cannot_commit = true; 198379787eaaSJeff Mahoney goto sleep; 1984914b2007SJan Kara } 19858929ecfaSYan, Zheng if (transid == trans->transid) { 19863a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19878929ecfaSYan, Zheng } else { 19883a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19898929ecfaSYan, Zheng } 1990a74a4b97SChris Mason sleep: 19910b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19920b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1993a74a4b97SChris Mason 199484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 19952ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 19968929ecfaSYan, Zheng if (!kthread_should_stop() && 19970b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1998914b2007SJan Kara cannot_commit)) 1999bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 2000a74a4b97SChris Mason } while (!kthread_should_stop()); 2001a74a4b97SChris Mason return 0; 2002a74a4b97SChris Mason } 2003a74a4b97SChris Mason 2004af31f5e5SChris Mason /* 200501f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 200601f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 200701f0f9daSNikolay Borisov * anything. 2008af31f5e5SChris Mason * 2009af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2010af31f5e5SChris Mason * generation of the latest root in the array with the generation 2011af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2012af31f5e5SChris Mason */ 201301f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2014af31f5e5SChris Mason { 201501f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2016af31f5e5SChris Mason u64 cur; 2017af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2018af31f5e5SChris Mason int i; 2019af31f5e5SChris Mason 2020af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2021af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2022af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2023af31f5e5SChris Mason if (cur == newest_gen) 202401f0f9daSNikolay Borisov return i; 2025af31f5e5SChris Mason } 2026af31f5e5SChris Mason 202701f0f9daSNikolay Borisov return -EINVAL; 2028af31f5e5SChris Mason } 2029af31f5e5SChris Mason 2030af31f5e5SChris Mason /* 2031af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2032af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2033af31f5e5SChris Mason * done 2034af31f5e5SChris Mason */ 2035af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2036af31f5e5SChris Mason { 20376ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2038af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2039af31f5e5SChris Mason 2040af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2041af31f5e5SChris Mason 2042af31f5e5SChris Mason /* 2043af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2044af31f5e5SChris Mason * regardless of which ones we use today 2045af31f5e5SChris Mason */ 2046af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2047af31f5e5SChris Mason 2048af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2049af31f5e5SChris Mason 2050af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2051af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2052af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2053af31f5e5SChris Mason 2054af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2055af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2056af31f5e5SChris Mason 2057af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2058af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2059af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2060af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2061af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2062af31f5e5SChris Mason 20639c54e80dSJosef Bacik if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) { 20649c54e80dSJosef Bacik btrfs_set_backup_block_group_root(root_backup, 20659c54e80dSJosef Bacik info->block_group_root->node->start); 20669c54e80dSJosef Bacik btrfs_set_backup_block_group_root_gen(root_backup, 20679c54e80dSJosef Bacik btrfs_header_generation(info->block_group_root->node)); 20689c54e80dSJosef Bacik btrfs_set_backup_block_group_root_level(root_backup, 20699c54e80dSJosef Bacik btrfs_header_level(info->block_group_root->node)); 20709c54e80dSJosef Bacik } else { 20719c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2072f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 20739c54e80dSJosef Bacik 20749c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 20759c54e80dSJosef Bacik extent_root->node->start); 2076af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 207729cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2078af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 207929cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2080af31f5e5SChris Mason 2081f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2082f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2083f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2084f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2085f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 20869c54e80dSJosef Bacik } 2087af31f5e5SChris Mason 20887c7e82a7SChris Mason /* 20897c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20907c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20917c7e82a7SChris Mason */ 20927c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20937c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20947c7e82a7SChris Mason info->fs_root->node->start); 2095af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2096af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2097af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2098af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 20997c7e82a7SChris Mason } 2100af31f5e5SChris Mason 2101af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2102af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2103af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2104af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2105af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2106af31f5e5SChris Mason 2107af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2108af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2109af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2110af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2111af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2112af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2113af31f5e5SChris Mason 2114af31f5e5SChris Mason /* 2115af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2116af31f5e5SChris Mason * for the next commit 2117af31f5e5SChris Mason */ 2118af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2119af31f5e5SChris Mason &info->super_for_commit->super_roots, 2120af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2121af31f5e5SChris Mason } 2122af31f5e5SChris Mason 2123af31f5e5SChris Mason /* 2124bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2125bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2126bd2336b2SNikolay Borisov * 2127bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2128bd2336b2SNikolay Borisov * priority - priority of backup root required 2129bd2336b2SNikolay Borisov * 2130bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2131bd2336b2SNikolay Borisov */ 2132bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2133bd2336b2SNikolay Borisov { 2134bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2135bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2136bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2137bd2336b2SNikolay Borisov 2138bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2139bd2336b2SNikolay Borisov if (priority == 0) 2140bd2336b2SNikolay Borisov return backup_index; 2141bd2336b2SNikolay Borisov 2142bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2143bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2144bd2336b2SNikolay Borisov } else { 2145bd2336b2SNikolay Borisov return -EINVAL; 2146bd2336b2SNikolay Borisov } 2147bd2336b2SNikolay Borisov 2148bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2149bd2336b2SNikolay Borisov 2150bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2151bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2152bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2153bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2154bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2155bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2156bd2336b2SNikolay Borisov 2157bd2336b2SNikolay Borisov /* 2158bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2159bd2336b2SNikolay Borisov * need a fsck 2160bd2336b2SNikolay Borisov */ 2161bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2162bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2163bd2336b2SNikolay Borisov 2164bd2336b2SNikolay Borisov return backup_index; 2165bd2336b2SNikolay Borisov } 2166bd2336b2SNikolay Borisov 21677abadb64SLiu Bo /* helper to cleanup workers */ 21687abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21697abadb64SLiu Bo { 2170dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2171afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 2172a31b4a43SChristoph Hellwig btrfs_destroy_workqueue(fs_info->hipri_workers); 21735cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2174d7b9416fSChristoph Hellwig if (fs_info->endio_workers) 2175d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_workers); 2176d34e123dSChristoph Hellwig if (fs_info->endio_raid56_workers) 2177d34e123dSChristoph Hellwig destroy_workqueue(fs_info->endio_raid56_workers); 2178385de0efSChristoph Hellwig if (fs_info->rmw_workers) 2179385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers); 2180fed8a72dSChristoph Hellwig if (fs_info->compressed_write_workers) 2181fed8a72dSChristoph Hellwig destroy_workqueue(fs_info->compressed_write_workers); 2182fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2183fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 21845b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2185e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2186a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2187fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2188b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2189b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2190a9b9477dSFilipe Manana /* 2191a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2192a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2193a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2194a9b9477dSFilipe Manana */ 2195d7b9416fSChristoph Hellwig if (fs_info->endio_meta_workers) 2196d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_meta_workers); 21977abadb64SLiu Bo } 21987abadb64SLiu Bo 21992e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22002e9f5954SRashika { 22012e9f5954SRashika if (root) { 22022e9f5954SRashika free_extent_buffer(root->node); 22032e9f5954SRashika free_extent_buffer(root->commit_root); 22042e9f5954SRashika root->node = NULL; 22052e9f5954SRashika root->commit_root = NULL; 22062e9f5954SRashika } 22072e9f5954SRashika } 22082e9f5954SRashika 2209abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2210abed4aaaSJosef Bacik { 2211abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2212abed4aaaSJosef Bacik 2213abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2214abed4aaaSJosef Bacik &fs_info->global_root_tree, 2215abed4aaaSJosef Bacik rb_node) 2216abed4aaaSJosef Bacik free_root_extent_buffers(root); 2217abed4aaaSJosef Bacik } 2218abed4aaaSJosef Bacik 2219af31f5e5SChris Mason /* helper to cleanup tree roots */ 22204273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2221af31f5e5SChris Mason { 22222e9f5954SRashika free_root_extent_buffers(info->tree_root); 2223655b09feSJosef Bacik 2224abed4aaaSJosef Bacik free_global_root_pointers(info); 22252e9f5954SRashika free_root_extent_buffers(info->dev_root); 22262e9f5954SRashika free_root_extent_buffers(info->quota_root); 22272e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22288c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2229aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 22309c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 22314273eaffSAnand Jain if (free_chunk_root) 22322e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2233af31f5e5SChris Mason } 2234af31f5e5SChris Mason 22358c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 22368c38938cSJosef Bacik { 22378c38938cSJosef Bacik if (!root) 22388c38938cSJosef Bacik return; 22398c38938cSJosef Bacik 22408c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 22418c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 22421dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 22438c38938cSJosef Bacik if (root->anon_dev) 22448c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 22458c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2246923eb523SJohannes Thumshirn free_root_extent_buffers(root); 22478c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2248fc7cbcd4SDavid Sterba spin_lock(&root->fs_info->fs_roots_radix_lock); 22498c38938cSJosef Bacik list_del_init(&root->leak_list); 2250fc7cbcd4SDavid Sterba spin_unlock(&root->fs_info->fs_roots_radix_lock); 22518c38938cSJosef Bacik #endif 22528c38938cSJosef Bacik kfree(root); 22538c38938cSJosef Bacik } 22548c38938cSJosef Bacik } 22558c38938cSJosef Bacik 2256faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2257171f6537SJosef Bacik { 2258fc7cbcd4SDavid Sterba int ret; 2259fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 2260fc7cbcd4SDavid Sterba int i; 2261171f6537SJosef Bacik 2262171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2263fc7cbcd4SDavid Sterba gang[0] = list_entry(fs_info->dead_roots.next, 2264171f6537SJosef Bacik struct btrfs_root, root_list); 2265fc7cbcd4SDavid Sterba list_del(&gang[0]->root_list); 2266171f6537SJosef Bacik 2267fc7cbcd4SDavid Sterba if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2268fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2269fc7cbcd4SDavid Sterba btrfs_put_root(gang[0]); 2270171f6537SJosef Bacik } 2271171f6537SJosef Bacik 2272fc7cbcd4SDavid Sterba while (1) { 2273fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2274fc7cbcd4SDavid Sterba (void **)gang, 0, 2275fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 2276fc7cbcd4SDavid Sterba if (!ret) 2277fc7cbcd4SDavid Sterba break; 2278fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 2279fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2280171f6537SJosef Bacik } 2281171f6537SJosef Bacik } 2282af31f5e5SChris Mason 2283638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2284638aa7edSEric Sandeen { 2285638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2286638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2287638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2288638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2289638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2290638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2291ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2292638aa7edSEric Sandeen } 2293638aa7edSEric Sandeen 2294779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2295779a65a4SEric Sandeen { 2296779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2297779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2298779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2299779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2300779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2301779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2302907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2303779a65a4SEric Sandeen } 2304779a65a4SEric Sandeen 23056bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2306f37938e0SEric Sandeen { 23072ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23082ff7e61eSJeff Mahoney 23092ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23102ff7e61eSJeff Mahoney set_nlink(inode, 1); 2311f37938e0SEric Sandeen /* 2312f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2313f37938e0SEric Sandeen * the real end of the address space is determined by all of 2314f37938e0SEric Sandeen * the devices in the system 2315f37938e0SEric Sandeen */ 23162ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23172ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2318f37938e0SEric Sandeen 23192ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 232043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 23212c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 23227b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 23232ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2324f37938e0SEric Sandeen 23255c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 23262ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 23272ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 23282ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2329f37938e0SEric Sandeen } 2330f37938e0SEric Sandeen 2331ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2332ad618368SEric Sandeen { 2333ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2334129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 23357f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2336ad618368SEric Sandeen } 2337ad618368SEric Sandeen 2338f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2339f9e92e40SEric Sandeen { 2340f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2341f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2342f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2343f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2344f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2345f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2346d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2347f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2348f9e92e40SEric Sandeen } 2349f9e92e40SEric Sandeen 2350d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 23512a458198SEric Sandeen { 2352f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 23536f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23542a458198SEric Sandeen 23552a458198SEric Sandeen fs_info->workers = 2356a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16); 2357a31b4a43SChristoph Hellwig fs_info->hipri_workers = 2358a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker-high", 2359cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23602a458198SEric Sandeen 23612a458198SEric Sandeen fs_info->delalloc_workers = 2362cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2363cb001095SJeff Mahoney flags, max_active, 2); 23642a458198SEric Sandeen 23652a458198SEric Sandeen fs_info->flush_workers = 2366cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2367cb001095SJeff Mahoney flags, max_active, 0); 23682a458198SEric Sandeen 23692a458198SEric Sandeen fs_info->caching_workers = 2370cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23712a458198SEric Sandeen 23722a458198SEric Sandeen fs_info->fixup_workers = 2373cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23742a458198SEric Sandeen 23752a458198SEric Sandeen fs_info->endio_workers = 2376d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio", flags, max_active); 23772a458198SEric Sandeen fs_info->endio_meta_workers = 2378d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio-meta", flags, max_active); 23792a458198SEric Sandeen fs_info->endio_raid56_workers = 2380d34e123dSChristoph Hellwig alloc_workqueue("btrfs-endio-raid56", flags, max_active); 2381385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active); 23822a458198SEric Sandeen fs_info->endio_write_workers = 2383cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2384cb001095SJeff Mahoney max_active, 2); 2385fed8a72dSChristoph Hellwig fs_info->compressed_write_workers = 2386fed8a72dSChristoph Hellwig alloc_workqueue("btrfs-compressed-write", flags, max_active); 23872a458198SEric Sandeen fs_info->endio_freespace_worker = 2388cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2389cb001095SJeff Mahoney max_active, 0); 23902a458198SEric Sandeen fs_info->delayed_workers = 2391cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2392cb001095SJeff Mahoney max_active, 0); 23932a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2394cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2395b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2396b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 23972a458198SEric Sandeen 2398a31b4a43SChristoph Hellwig if (!(fs_info->workers && fs_info->hipri_workers && 2399a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers && 24002a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 2401fed8a72dSChristoph Hellwig fs_info->compressed_write_workers && 24022a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24032a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2404f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2405f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2406b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 24072a458198SEric Sandeen return -ENOMEM; 24082a458198SEric Sandeen } 24092a458198SEric Sandeen 24102a458198SEric Sandeen return 0; 24112a458198SEric Sandeen } 24122a458198SEric Sandeen 24136d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 24146d97c6e3SJohannes Thumshirn { 24156d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2416b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 24176d97c6e3SJohannes Thumshirn 2418b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 24196d97c6e3SJohannes Thumshirn 24206d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 24216d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2422b4e967beSDavid Sterba csum_driver); 24236d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 24246d97c6e3SJohannes Thumshirn } 24256d97c6e3SJohannes Thumshirn 24266d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 24276d97c6e3SJohannes Thumshirn 24286d97c6e3SJohannes Thumshirn return 0; 24296d97c6e3SJohannes Thumshirn } 24306d97c6e3SJohannes Thumshirn 243163443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 243263443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 243363443bf5SEric Sandeen { 243463443bf5SEric Sandeen int ret; 243563443bf5SEric Sandeen struct btrfs_root *log_tree_root; 243663443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 243763443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2438581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 243963443bf5SEric Sandeen 244063443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2441f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 244263443bf5SEric Sandeen return -EIO; 244363443bf5SEric Sandeen } 244463443bf5SEric Sandeen 244596dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 244696dfcb46SJosef Bacik GFP_KERNEL); 244763443bf5SEric Sandeen if (!log_tree_root) 244863443bf5SEric Sandeen return -ENOMEM; 244963443bf5SEric Sandeen 24502ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 24511b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 24521b7ec85eSJosef Bacik fs_info->generation + 1, level, 24531b7ec85eSJosef Bacik NULL); 245464c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2455f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24560eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 24578c38938cSJosef Bacik log_tree_root->node = NULL; 245800246528SJosef Bacik btrfs_put_root(log_tree_root); 24590eeff236SLiu Bo return ret; 24604eb150d6SQu Wenruo } 24614eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2462f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 246300246528SJosef Bacik btrfs_put_root(log_tree_root); 246463443bf5SEric Sandeen return -EIO; 246563443bf5SEric Sandeen } 24664eb150d6SQu Wenruo 246763443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 246863443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 246963443bf5SEric Sandeen if (ret) { 24700b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 247163443bf5SEric Sandeen "Failed to recover log tree"); 247200246528SJosef Bacik btrfs_put_root(log_tree_root); 247363443bf5SEric Sandeen return ret; 247463443bf5SEric Sandeen } 247563443bf5SEric Sandeen 2476bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 24776bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 247863443bf5SEric Sandeen if (ret) 247963443bf5SEric Sandeen return ret; 248063443bf5SEric Sandeen } 248163443bf5SEric Sandeen 248263443bf5SEric Sandeen return 0; 248363443bf5SEric Sandeen } 248463443bf5SEric Sandeen 2485abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2486abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2487abed4aaaSJosef Bacik const char *name) 2488abed4aaaSJosef Bacik { 2489abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2490abed4aaaSJosef Bacik struct btrfs_root *root; 2491f7238e50SJosef Bacik u64 max_global_id = 0; 2492abed4aaaSJosef Bacik int ret; 2493abed4aaaSJosef Bacik struct btrfs_key key = { 2494abed4aaaSJosef Bacik .objectid = objectid, 2495abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2496abed4aaaSJosef Bacik .offset = 0, 2497abed4aaaSJosef Bacik }; 2498abed4aaaSJosef Bacik bool found = false; 2499abed4aaaSJosef Bacik 2500abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2501abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2502abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2503abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2504abed4aaaSJosef Bacik return 0; 2505abed4aaaSJosef Bacik } 2506abed4aaaSJosef Bacik 2507abed4aaaSJosef Bacik while (1) { 2508abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2509abed4aaaSJosef Bacik if (ret < 0) 2510abed4aaaSJosef Bacik break; 2511abed4aaaSJosef Bacik 2512abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2513abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2514abed4aaaSJosef Bacik if (ret) { 2515abed4aaaSJosef Bacik if (ret > 0) 2516abed4aaaSJosef Bacik ret = 0; 2517abed4aaaSJosef Bacik break; 2518abed4aaaSJosef Bacik } 2519abed4aaaSJosef Bacik } 2520abed4aaaSJosef Bacik ret = 0; 2521abed4aaaSJosef Bacik 2522abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2523abed4aaaSJosef Bacik if (key.objectid != objectid) 2524abed4aaaSJosef Bacik break; 2525abed4aaaSJosef Bacik btrfs_release_path(path); 2526abed4aaaSJosef Bacik 2527f7238e50SJosef Bacik /* 2528f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2529f7238e50SJosef Bacik * everybody. 2530f7238e50SJosef Bacik */ 2531f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2532f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2533f7238e50SJosef Bacik 2534abed4aaaSJosef Bacik found = true; 2535abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2536abed4aaaSJosef Bacik if (IS_ERR(root)) { 2537abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2538abed4aaaSJosef Bacik ret = PTR_ERR(root); 2539abed4aaaSJosef Bacik break; 2540abed4aaaSJosef Bacik } 2541abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2542abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2543abed4aaaSJosef Bacik if (ret) { 2544abed4aaaSJosef Bacik btrfs_put_root(root); 2545abed4aaaSJosef Bacik break; 2546abed4aaaSJosef Bacik } 2547abed4aaaSJosef Bacik key.offset++; 2548abed4aaaSJosef Bacik } 2549abed4aaaSJosef Bacik btrfs_release_path(path); 2550abed4aaaSJosef Bacik 2551f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2552f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2553f7238e50SJosef Bacik 2554abed4aaaSJosef Bacik if (!found || ret) { 2555abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2556abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2557abed4aaaSJosef Bacik 2558abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2559abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2560abed4aaaSJosef Bacik else 2561abed4aaaSJosef Bacik ret = 0; 2562abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2563abed4aaaSJosef Bacik } 2564abed4aaaSJosef Bacik return ret; 2565abed4aaaSJosef Bacik } 2566abed4aaaSJosef Bacik 2567abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2568abed4aaaSJosef Bacik { 2569abed4aaaSJosef Bacik struct btrfs_path *path; 2570abed4aaaSJosef Bacik int ret = 0; 2571abed4aaaSJosef Bacik 2572abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2573abed4aaaSJosef Bacik if (!path) 2574abed4aaaSJosef Bacik return -ENOMEM; 2575abed4aaaSJosef Bacik 2576abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2577abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2578abed4aaaSJosef Bacik if (ret) 2579abed4aaaSJosef Bacik goto out; 2580abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2581abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2582abed4aaaSJosef Bacik if (ret) 2583abed4aaaSJosef Bacik goto out; 2584abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2585abed4aaaSJosef Bacik goto out; 2586abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2587abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2588abed4aaaSJosef Bacik "free space"); 2589abed4aaaSJosef Bacik out: 2590abed4aaaSJosef Bacik btrfs_free_path(path); 2591abed4aaaSJosef Bacik return ret; 2592abed4aaaSJosef Bacik } 2593abed4aaaSJosef Bacik 25946bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 25954bbcaa64SEric Sandeen { 25966bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2597a4f3d2c4SDavid Sterba struct btrfs_root *root; 25984bbcaa64SEric Sandeen struct btrfs_key location; 25994bbcaa64SEric Sandeen int ret; 26004bbcaa64SEric Sandeen 26016bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 26026bccf3abSJeff Mahoney 2603abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2604abed4aaaSJosef Bacik if (ret) 2605abed4aaaSJosef Bacik return ret; 2606abed4aaaSJosef Bacik 2607abed4aaaSJosef Bacik location.objectid = BTRFS_DEV_TREE_OBJECTID; 26084bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 26094bbcaa64SEric Sandeen location.offset = 0; 26104bbcaa64SEric Sandeen 2611a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2612f50f4353SLiu Bo if (IS_ERR(root)) { 261342437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2614f50f4353SLiu Bo ret = PTR_ERR(root); 2615f50f4353SLiu Bo goto out; 2616f50f4353SLiu Bo } 261742437a63SJosef Bacik } else { 2618a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2619a4f3d2c4SDavid Sterba fs_info->dev_root = root; 262042437a63SJosef Bacik } 2621820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2622820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 26234bbcaa64SEric Sandeen 2624aeb935a4SQu Wenruo /* 2625aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2626aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2627aeb935a4SQu Wenruo */ 262856e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 262956e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2630aeb935a4SQu Wenruo if (IS_ERR(root)) { 263142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2632aeb935a4SQu Wenruo ret = PTR_ERR(root); 2633aeb935a4SQu Wenruo goto out; 2634aeb935a4SQu Wenruo } 263542437a63SJosef Bacik } else { 2636aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2637aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 263842437a63SJosef Bacik } 2639aeb935a4SQu Wenruo 26404bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2641a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2642a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2643a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2644afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2645a4f3d2c4SDavid Sterba fs_info->quota_root = root; 26464bbcaa64SEric Sandeen } 26474bbcaa64SEric Sandeen 26484bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2649a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2650a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 265142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2652a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 26534bbcaa64SEric Sandeen if (ret != -ENOENT) 2654f50f4353SLiu Bo goto out; 265542437a63SJosef Bacik } 26564bbcaa64SEric Sandeen } else { 2657a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2658a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 26594bbcaa64SEric Sandeen } 26604bbcaa64SEric Sandeen 26614bbcaa64SEric Sandeen return 0; 2662f50f4353SLiu Bo out: 2663f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2664f50f4353SLiu Bo location.objectid, ret); 2665f50f4353SLiu Bo return ret; 26664bbcaa64SEric Sandeen } 26674bbcaa64SEric Sandeen 2668069ec957SQu Wenruo /* 2669069ec957SQu Wenruo * Real super block validation 2670069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2671069ec957SQu Wenruo * 2672069ec957SQu Wenruo * @sb: super block to check 2673069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2674069ec957SQu Wenruo * 0 the primary (1st) sb 2675069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2676069ec957SQu Wenruo * -1 skip bytenr check 2677069ec957SQu Wenruo */ 2678069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2679069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 268021a852b0SQu Wenruo { 268121a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 268221a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 268321a852b0SQu Wenruo int ret = 0; 268421a852b0SQu Wenruo 268521a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 268621a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 268721a852b0SQu Wenruo ret = -EINVAL; 268821a852b0SQu Wenruo } 268921a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 269021a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 269121a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 269221a852b0SQu Wenruo ret = -EINVAL; 269321a852b0SQu Wenruo } 269421a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 269521a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 269621a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 269721a852b0SQu Wenruo ret = -EINVAL; 269821a852b0SQu Wenruo } 269921a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 270021a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 270121a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 270221a852b0SQu Wenruo ret = -EINVAL; 270321a852b0SQu Wenruo } 270421a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 270521a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 270621a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 270721a852b0SQu Wenruo ret = -EINVAL; 270821a852b0SQu Wenruo } 270921a852b0SQu Wenruo 271021a852b0SQu Wenruo /* 271121a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 271221a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 271321a852b0SQu Wenruo */ 271421a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 271521a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 271621a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 271721a852b0SQu Wenruo ret = -EINVAL; 271821a852b0SQu Wenruo } 27190bb3eb3eSQu Wenruo 27200bb3eb3eSQu Wenruo /* 27211a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 27221a42daabSQu Wenruo * 27231a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 27241a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 27251a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 27261a42daabSQu Wenruo * beginning. 27270bb3eb3eSQu Wenruo */ 27281a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 272921a852b0SQu Wenruo btrfs_err(fs_info, 27300bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 273121a852b0SQu Wenruo sectorsize, PAGE_SIZE); 273221a852b0SQu Wenruo ret = -EINVAL; 273321a852b0SQu Wenruo } 27340bb3eb3eSQu Wenruo 273521a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 273621a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 273721a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 273821a852b0SQu Wenruo ret = -EINVAL; 273921a852b0SQu Wenruo } 274021a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 274121a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 274221a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 274321a852b0SQu Wenruo ret = -EINVAL; 274421a852b0SQu Wenruo } 274521a852b0SQu Wenruo 274621a852b0SQu Wenruo /* Root alignment check */ 274721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 274821a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 274921a852b0SQu Wenruo btrfs_super_root(sb)); 275021a852b0SQu Wenruo ret = -EINVAL; 275121a852b0SQu Wenruo } 275221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 275321a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 275421a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 275521a852b0SQu Wenruo ret = -EINVAL; 275621a852b0SQu Wenruo } 275721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 275821a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 275921a852b0SQu Wenruo btrfs_super_log_root(sb)); 276021a852b0SQu Wenruo ret = -EINVAL; 276121a852b0SQu Wenruo } 276221a852b0SQu Wenruo 2763aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2764aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2765aefd7f70SNikolay Borisov btrfs_err(fs_info, 2766aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2767aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2768aefd7f70SNikolay Borisov ret = -EINVAL; 2769aefd7f70SNikolay Borisov } 2770aefd7f70SNikolay Borisov 2771aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2772aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2773aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2774aefd7f70SNikolay Borisov btrfs_err(fs_info, 2775aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2776aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2777aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2778aefd7f70SNikolay Borisov ret = -EINVAL; 2779aefd7f70SNikolay Borisov } 2780aefd7f70SNikolay Borisov 2781de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 27827239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 278321a852b0SQu Wenruo btrfs_err(fs_info, 27847239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2785de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 278621a852b0SQu Wenruo ret = -EINVAL; 278721a852b0SQu Wenruo } 278821a852b0SQu Wenruo 278921a852b0SQu Wenruo /* 279021a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 279121a852b0SQu Wenruo * done later 279221a852b0SQu Wenruo */ 279321a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 279421a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 279521a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 279621a852b0SQu Wenruo ret = -EINVAL; 279721a852b0SQu Wenruo } 279821a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 279921a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 280021a852b0SQu Wenruo btrfs_super_stripesize(sb)); 280121a852b0SQu Wenruo ret = -EINVAL; 280221a852b0SQu Wenruo } 280321a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 280421a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 280521a852b0SQu Wenruo btrfs_super_num_devices(sb)); 280621a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 280721a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 280821a852b0SQu Wenruo ret = -EINVAL; 280921a852b0SQu Wenruo } 281021a852b0SQu Wenruo 2811069ec957SQu Wenruo if (mirror_num >= 0 && 2812069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 281321a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 281421a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 281521a852b0SQu Wenruo ret = -EINVAL; 281621a852b0SQu Wenruo } 281721a852b0SQu Wenruo 281821a852b0SQu Wenruo /* 281921a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 282021a852b0SQu Wenruo * and one chunk 282121a852b0SQu Wenruo */ 282221a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 282321a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 282421a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 282521a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 282621a852b0SQu Wenruo ret = -EINVAL; 282721a852b0SQu Wenruo } 282821a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 282921a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 283021a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 283121a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 283221a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 283321a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 283421a852b0SQu Wenruo ret = -EINVAL; 283521a852b0SQu Wenruo } 283621a852b0SQu Wenruo 283721a852b0SQu Wenruo /* 283821a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 283921a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 284021a852b0SQu Wenruo */ 284121a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 284221a852b0SQu Wenruo btrfs_warn(fs_info, 284321a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 284421a852b0SQu Wenruo btrfs_super_generation(sb), 284521a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 284621a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 284721a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 284821a852b0SQu Wenruo btrfs_warn(fs_info, 284921a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 285021a852b0SQu Wenruo btrfs_super_generation(sb), 285121a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 285221a852b0SQu Wenruo 285321a852b0SQu Wenruo return ret; 285421a852b0SQu Wenruo } 285521a852b0SQu Wenruo 2856069ec957SQu Wenruo /* 2857069ec957SQu Wenruo * Validation of super block at mount time. 2858069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2859069ec957SQu Wenruo * flags will be skipped. 2860069ec957SQu Wenruo */ 2861069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2862069ec957SQu Wenruo { 2863069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2864069ec957SQu Wenruo } 2865069ec957SQu Wenruo 286675cb857dSQu Wenruo /* 286775cb857dSQu Wenruo * Validation of super block at write time. 286875cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 286975cb857dSQu Wenruo * overwritten soon. 287075cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 287175cb857dSQu Wenruo */ 287275cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 287375cb857dSQu Wenruo struct btrfs_super_block *sb) 287475cb857dSQu Wenruo { 287575cb857dSQu Wenruo int ret; 287675cb857dSQu Wenruo 287775cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 287875cb857dSQu Wenruo if (ret < 0) 287975cb857dSQu Wenruo goto out; 2880e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 288175cb857dSQu Wenruo ret = -EUCLEAN; 288275cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 288375cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 288475cb857dSQu Wenruo goto out; 288575cb857dSQu Wenruo } 288675cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 288775cb857dSQu Wenruo ret = -EUCLEAN; 288875cb857dSQu Wenruo btrfs_err(fs_info, 288975cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 289075cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 289175cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 289275cb857dSQu Wenruo goto out; 289375cb857dSQu Wenruo } 289475cb857dSQu Wenruo out: 289575cb857dSQu Wenruo if (ret < 0) 289675cb857dSQu Wenruo btrfs_err(fs_info, 289775cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 289875cb857dSQu Wenruo return ret; 289975cb857dSQu Wenruo } 290075cb857dSQu Wenruo 2901bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2902bd676446SJosef Bacik { 2903bd676446SJosef Bacik int ret = 0; 2904bd676446SJosef Bacik 2905bd676446SJosef Bacik root->node = read_tree_block(root->fs_info, bytenr, 2906bd676446SJosef Bacik root->root_key.objectid, gen, level, NULL); 2907bd676446SJosef Bacik if (IS_ERR(root->node)) { 2908bd676446SJosef Bacik ret = PTR_ERR(root->node); 2909bd676446SJosef Bacik root->node = NULL; 29104eb150d6SQu Wenruo return ret; 29114eb150d6SQu Wenruo } 29124eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 2913bd676446SJosef Bacik free_extent_buffer(root->node); 2914bd676446SJosef Bacik root->node = NULL; 29154eb150d6SQu Wenruo return -EIO; 2916bd676446SJosef Bacik } 2917bd676446SJosef Bacik 2918bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 2919bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 2920bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 2921bd676446SJosef Bacik return ret; 2922bd676446SJosef Bacik } 2923bd676446SJosef Bacik 2924bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 2925bd676446SJosef Bacik { 2926bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 2927bd676446SJosef Bacik u64 gen, bytenr; 2928bd676446SJosef Bacik int level, ret; 2929bd676446SJosef Bacik 2930bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 2931bd676446SJosef Bacik gen = btrfs_super_generation(sb); 2932bd676446SJosef Bacik level = btrfs_super_root_level(sb); 2933bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 29349c54e80dSJosef Bacik if (ret) { 2935bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 2936bd676446SJosef Bacik return ret; 2937bd676446SJosef Bacik } 2938bd676446SJosef Bacik 29399c54e80dSJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 29409c54e80dSJosef Bacik return 0; 29419c54e80dSJosef Bacik 29429c54e80dSJosef Bacik bytenr = btrfs_super_block_group_root(sb); 29439c54e80dSJosef Bacik gen = btrfs_super_block_group_root_generation(sb); 29449c54e80dSJosef Bacik level = btrfs_super_block_group_root_level(sb); 29459c54e80dSJosef Bacik ret = load_super_root(fs_info->block_group_root, bytenr, gen, level); 29469c54e80dSJosef Bacik if (ret) 29479c54e80dSJosef Bacik btrfs_warn(fs_info, "couldn't read block group root"); 29489c54e80dSJosef Bacik return ret; 29499c54e80dSJosef Bacik } 29509c54e80dSJosef Bacik 29516ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2952b8522a1eSNikolay Borisov { 29536ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2954b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2955b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2956b8522a1eSNikolay Borisov bool handle_error = false; 2957b8522a1eSNikolay Borisov int ret = 0; 2958b8522a1eSNikolay Borisov int i; 2959b8522a1eSNikolay Borisov 29609c54e80dSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 29619c54e80dSJosef Bacik struct btrfs_root *root; 2962b8522a1eSNikolay Borisov 29639c54e80dSJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID, 29649c54e80dSJosef Bacik GFP_KERNEL); 29659c54e80dSJosef Bacik if (!root) 29669c54e80dSJosef Bacik return -ENOMEM; 29679c54e80dSJosef Bacik fs_info->block_group_root = root; 29689c54e80dSJosef Bacik } 29699c54e80dSJosef Bacik 2970b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2971b8522a1eSNikolay Borisov if (handle_error) { 2972b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2973b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2974b8522a1eSNikolay Borisov tree_root->node = NULL; 2975b8522a1eSNikolay Borisov 2976b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2977b8522a1eSNikolay Borisov break; 2978b8522a1eSNikolay Borisov 2979b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2980b8522a1eSNikolay Borisov 2981b8522a1eSNikolay Borisov /* 2982b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2983b8522a1eSNikolay Borisov * valid 2984b8522a1eSNikolay Borisov */ 2985b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2986b8522a1eSNikolay Borisov 2987b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2988b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2989b8522a1eSNikolay Borisov 2990b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 29916ef108ddSNikolay Borisov backup_index = ret; 2992b8522a1eSNikolay Borisov if (ret < 0) 2993b8522a1eSNikolay Borisov return ret; 2994b8522a1eSNikolay Borisov } 2995b8522a1eSNikolay Borisov 2996bd676446SJosef Bacik ret = load_important_roots(fs_info); 2997bd676446SJosef Bacik if (ret) { 2998217f5004SNikolay Borisov handle_error = true; 2999b8522a1eSNikolay Borisov continue; 3000b8522a1eSNikolay Borisov } 3001b8522a1eSNikolay Borisov 3002336a0d8dSNikolay Borisov /* 3003336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3004336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3005336a0d8dSNikolay Borisov */ 3006453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3007b8522a1eSNikolay Borisov if (ret < 0) { 3008b8522a1eSNikolay Borisov handle_error = true; 3009b8522a1eSNikolay Borisov continue; 3010b8522a1eSNikolay Borisov } 3011b8522a1eSNikolay Borisov 30126b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3013b8522a1eSNikolay Borisov 3014b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3015b8522a1eSNikolay Borisov if (ret < 0) { 3016b8522a1eSNikolay Borisov handle_error = true; 3017b8522a1eSNikolay Borisov continue; 3018b8522a1eSNikolay Borisov } 3019b8522a1eSNikolay Borisov 3020b8522a1eSNikolay Borisov /* All successful */ 3021bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3022bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3023d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 30246ef108ddSNikolay Borisov 30256ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 30266ef108ddSNikolay Borisov if (backup_index < 0) { 30276ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 30286ef108ddSNikolay Borisov } else { 30296ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 30306ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 30316ef108ddSNikolay Borisov } 3032b8522a1eSNikolay Borisov break; 3033b8522a1eSNikolay Borisov } 3034b8522a1eSNikolay Borisov 3035b8522a1eSNikolay Borisov return ret; 3036b8522a1eSNikolay Borisov } 3037b8522a1eSNikolay Borisov 30388260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3039eb60ceacSChris Mason { 3040fc7cbcd4SDavid Sterba INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 304101cd3909SDavid Sterba INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 30428fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3043facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 304424bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3045eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 304611833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3047eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3048a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 3049fc7cbcd4SDavid Sterba spin_lock_init(&fs_info->fs_roots_radix_lock); 305024bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 30514cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3052ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3053f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 305447ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 305540ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3056afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3057c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3058f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3059abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3060d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3061f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 30627585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3063573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 30640bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 30655f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3066de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 306719c00ddcSChris Mason 30680b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 30696324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3070f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 307147ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 307218bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3073afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3074bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3075bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 30763fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 30773fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3078bd647ce3SJosef Bacik #endif 3079c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 308066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 308166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 308266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 308366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 308466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 308566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 308666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3087ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3088ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3089ba2c4d4eSJosef Bacik 3090771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 30914cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3092034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3093fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3094abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 309595ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 30969ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 30974cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3098a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3099f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 31008b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3101f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3102fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3103c8b97818SChris Mason 3104b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3105b34b086cSChris Mason num_online_cpus() + 2, 8); 31060afbaf8cSChris Mason 3107199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3108199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 310969fe2d75SJosef Bacik 3110638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 311121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 311221adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 311321adbd5cSStefan Behrens #endif 3114779a65a4SEric Sandeen btrfs_init_balance(fs_info); 311557056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3116a2de733cSArne Jansen 311716b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock); 311808dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED; 31190f9dd46cSJosef Bacik 3120fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 3121fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 312239279cc3SChris Mason 31235a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3124e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3125925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3126a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3127a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 31281bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 31299e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3130c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 313176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3132803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3133fa9c0d79SChris Mason 3134ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3135f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3136b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3137416ac51dSArne Jansen 3138fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3139fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3140fa9c0d79SChris Mason 3141e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3142f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3143bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 31444854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3145034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 31463768f368SChris Mason 3147da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3148da17066cSJeff Mahoney fs_info->nodesize = 4096; 3149da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3150ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3151da17066cSJeff Mahoney fs_info->stripesize = 4096; 3152da17066cSJeff Mahoney 3153eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3154eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3155eede2bf3SOmar Sandoval 315618bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 315718bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 31588260edbaSJosef Bacik } 31598260edbaSJosef Bacik 31608260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 31618260edbaSJosef Bacik { 31628260edbaSJosef Bacik int ret; 31638260edbaSJosef Bacik 31648260edbaSJosef Bacik fs_info->sb = sb; 31658260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 31668260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 31679e967495SFilipe Manana 31685deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3169ae18c37aSJosef Bacik if (ret) 3170ae18c37aSJosef Bacik return ret; 3171ae18c37aSJosef Bacik 3172ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3173ae18c37aSJosef Bacik if (ret) 3174c75e8394SJosef Bacik return ret; 3175ae18c37aSJosef Bacik 3176ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3177ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3178ae18c37aSJosef Bacik 3179ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3180ae18c37aSJosef Bacik if (ret) 3181c75e8394SJosef Bacik return ret; 3182ae18c37aSJosef Bacik 3183ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3184ae18c37aSJosef Bacik GFP_KERNEL); 3185ae18c37aSJosef Bacik if (ret) 3186c75e8394SJosef Bacik return ret; 3187ae18c37aSJosef Bacik 3188ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3189ae18c37aSJosef Bacik GFP_KERNEL); 3190c75e8394SJosef Bacik if (!fs_info->delayed_root) 3191c75e8394SJosef Bacik return -ENOMEM; 3192ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3193ae18c37aSJosef Bacik 3194a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3195a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3196a0a1db70SFilipe Manana 3197c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3198ae18c37aSJosef Bacik } 3199ae18c37aSJosef Bacik 320097f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 320197f4dd09SNikolay Borisov { 32020d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 320397f4dd09SNikolay Borisov int ret; 320497f4dd09SNikolay Borisov 320597f4dd09SNikolay Borisov /* 320697f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 320797f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 320897f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 320997f4dd09SNikolay Borisov */ 321097f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 321197f4dd09SNikolay Borisov if (ret < 0) { 3212c94bec2cSJosef Bacik if (ret != -EINTR) 3213c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3214c94bec2cSJosef Bacik ret); 321597f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 321697f4dd09SNikolay Borisov return ret; 321797f4dd09SNikolay Borisov } 321897f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 321997f4dd09SNikolay Borisov } 322097f4dd09SNikolay Borisov 322197f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 322297f4dd09SNikolay Borisov { 322397f4dd09SNikolay Borisov struct task_struct *task; 322497f4dd09SNikolay Borisov 322597f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 322697f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 322797f4dd09SNikolay Borisov if (IS_ERR(task)) { 322897f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 322997f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 323097f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 323197f4dd09SNikolay Borisov return PTR_ERR(task); 323297f4dd09SNikolay Borisov } 323397f4dd09SNikolay Borisov 323497f4dd09SNikolay Borisov return 0; 323597f4dd09SNikolay Borisov } 323697f4dd09SNikolay Borisov 323744c0ca21SBoris Burkov /* 32388cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 32398cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 32408cd29088SBoris Burkov * code paths. 32418cd29088SBoris Burkov */ 32428cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 32438cd29088SBoris Burkov { 32448cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32458b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 32468cd29088SBoris Burkov } 32478cd29088SBoris Burkov 32488cd29088SBoris Burkov /* 324944c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 325044c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 325144c0ca21SBoris Burkov */ 325244c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 325344c0ca21SBoris Burkov { 325444c0ca21SBoris Burkov int ret; 325594846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 32568b228324SBoris Burkov bool clear_free_space_tree = false; 32578b228324SBoris Burkov 32588b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 32598b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32608b228324SBoris Burkov clear_free_space_tree = true; 32618b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32628b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32638b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 32648b228324SBoris Burkov clear_free_space_tree = true; 32658b228324SBoris Burkov } 32668b228324SBoris Burkov 32678b228324SBoris Burkov if (clear_free_space_tree) { 32688b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 32698b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 32708b228324SBoris Burkov if (ret) { 32718b228324SBoris Burkov btrfs_warn(fs_info, 32728b228324SBoris Burkov "failed to clear free space tree: %d", ret); 32738b228324SBoris Burkov goto out; 32748b228324SBoris Burkov } 32758b228324SBoris Burkov } 327644c0ca21SBoris Burkov 32778d488a8cSFilipe Manana /* 32788d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 32798d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 3280fc7cbcd4SDavid Sterba * them into the fs_info->fs_roots_radix tree. This must be done before 32818d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 32828d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 32838d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 32848d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 32858d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 32868d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 32878d488a8cSFilipe Manana */ 32888d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 32898d488a8cSFilipe Manana if (ret) 32908d488a8cSFilipe Manana goto out; 32918d488a8cSFilipe Manana 329244c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 329344c0ca21SBoris Burkov if (ret) 329444c0ca21SBoris Burkov goto out; 329544c0ca21SBoris Burkov 32968f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 32978f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 32988f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 32998f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33008f1c21d7SBoris Burkov goto out; 33018f1c21d7SBoris Burkov } 33028f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33038f1c21d7SBoris Burkov 330444c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 33057eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 330644c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 330744c0ca21SBoris Burkov if (ret < 0) { 330844c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 330944c0ca21SBoris Burkov goto out; 331044c0ca21SBoris Burkov } 331144c0ca21SBoris Burkov 33125011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 33135011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33145011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 33155011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 33165011139aSBoris Burkov if (ret) { 33175011139aSBoris Burkov btrfs_warn(fs_info, 33185011139aSBoris Burkov "failed to create free space tree: %d", ret); 33195011139aSBoris Burkov goto out; 33205011139aSBoris Burkov } 33215011139aSBoris Burkov } 33225011139aSBoris Burkov 332394846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 332494846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 332594846229SBoris Burkov if (ret) 332694846229SBoris Burkov goto out; 332794846229SBoris Burkov } 332894846229SBoris Burkov 332944c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 333044c0ca21SBoris Burkov if (ret) 333144c0ca21SBoris Burkov goto out; 333244c0ca21SBoris Burkov 333344c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 333444c0ca21SBoris Burkov if (ret) { 333544c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 333644c0ca21SBoris Burkov goto out; 333744c0ca21SBoris Burkov } 333844c0ca21SBoris Burkov 333944c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 334044c0ca21SBoris Burkov 334144c0ca21SBoris Burkov if (!fs_info->uuid_root) { 334244c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 334344c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 334444c0ca21SBoris Burkov if (ret) { 334544c0ca21SBoris Burkov btrfs_warn(fs_info, 334644c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 334744c0ca21SBoris Burkov goto out; 334844c0ca21SBoris Burkov } 334944c0ca21SBoris Burkov } 335044c0ca21SBoris Burkov 335144c0ca21SBoris Burkov out: 335244c0ca21SBoris Burkov return ret; 335344c0ca21SBoris Burkov } 335444c0ca21SBoris Burkov 3355ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3356ae18c37aSJosef Bacik char *options) 3357ae18c37aSJosef Bacik { 3358ae18c37aSJosef Bacik u32 sectorsize; 3359ae18c37aSJosef Bacik u32 nodesize; 3360ae18c37aSJosef Bacik u32 stripesize; 3361ae18c37aSJosef Bacik u64 generation; 3362ae18c37aSJosef Bacik u64 features; 3363ae18c37aSJosef Bacik u16 csum_type; 3364ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3365ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3366ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3367ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3368ae18c37aSJosef Bacik int ret; 3369ae18c37aSJosef Bacik int err = -EINVAL; 3370ae18c37aSJosef Bacik int level; 3371ae18c37aSJosef Bacik 33728260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 337353b381b3SDavid Woodhouse if (ret) { 337483c8266aSDavid Sterba err = ret; 3375ae18c37aSJosef Bacik goto fail; 337653b381b3SDavid Woodhouse } 337753b381b3SDavid Woodhouse 3378ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3379ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3380ae18c37aSJosef Bacik GFP_KERNEL); 3381ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3382ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3383ae18c37aSJosef Bacik GFP_KERNEL); 3384ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3385ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3386ae18c37aSJosef Bacik err = -ENOMEM; 3387c75e8394SJosef Bacik goto fail; 3388ae18c37aSJosef Bacik } 3389ae18c37aSJosef Bacik 3390ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3391ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3392ae18c37aSJosef Bacik err = -ENOMEM; 3393c75e8394SJosef Bacik goto fail; 3394ae18c37aSJosef Bacik } 3395ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3396ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3397ae18c37aSJosef Bacik 3398d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 33991104a885SDavid Sterba 34001104a885SDavid Sterba /* 34011104a885SDavid Sterba * Read super block and check the signature bytes only 34021104a885SDavid Sterba */ 3403d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 34048f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 34058f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 340616cdcec7SMiao Xie goto fail_alloc; 340720b45077SDave Young } 340839279cc3SChris Mason 34091104a885SDavid Sterba /* 3410260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 34118dc3f22cSJohannes Thumshirn * corrupted, we'll find out 34128dc3f22cSJohannes Thumshirn */ 34138f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 341451bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 34158dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 341651bce6c9SJohannes Thumshirn csum_type); 34178dc3f22cSJohannes Thumshirn err = -EINVAL; 34188f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34198dc3f22cSJohannes Thumshirn goto fail_alloc; 34208dc3f22cSJohannes Thumshirn } 34218dc3f22cSJohannes Thumshirn 342283c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 342383c68bbcSSu Yue 34246d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 34256d97c6e3SJohannes Thumshirn if (ret) { 34266d97c6e3SJohannes Thumshirn err = ret; 34278f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34286d97c6e3SJohannes Thumshirn goto fail_alloc; 34296d97c6e3SJohannes Thumshirn } 34306d97c6e3SJohannes Thumshirn 34318dc3f22cSJohannes Thumshirn /* 34321104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 34331104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 34341104a885SDavid Sterba */ 34358f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 343605135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 34371104a885SDavid Sterba err = -EINVAL; 34388f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3439141386e1SJosef Bacik goto fail_alloc; 34401104a885SDavid Sterba } 34411104a885SDavid Sterba 34421104a885SDavid Sterba /* 34431104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 34441104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 34451104a885SDavid Sterba * the whole block of INFO_SIZE 34461104a885SDavid Sterba */ 34478f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 34488f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34495f39d397SChris Mason 3450fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3451fbc6feaeSNikolay Borisov 34520b86a832SChris Mason 3453fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3454fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3455fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3456fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3457fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3458fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3459fbc6feaeSNikolay Borisov } 3460fbc6feaeSNikolay Borisov 3461fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3462fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3463de37aa51SNikolay Borisov 3464069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 34651104a885SDavid Sterba if (ret) { 346605135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 34671104a885SDavid Sterba err = -EINVAL; 3468141386e1SJosef Bacik goto fail_alloc; 34691104a885SDavid Sterba } 34701104a885SDavid Sterba 34710f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3472141386e1SJosef Bacik goto fail_alloc; 34730f7d52f4SChris Mason 3474acce952bSliubo /* check FS state, whether FS is broken. */ 347587533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 347687533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3477acce952bSliubo 347875e7cb7fSLiu Bo /* 347975e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 348075e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 348175e7cb7fSLiu Bo */ 348275e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 348375e7cb7fSLiu Bo 34846f93e834SAnand Jain /* 34856f93e834SAnand Jain * Flag our filesystem as having big metadata blocks if they are bigger 34866f93e834SAnand Jain * than the page size. 34876f93e834SAnand Jain */ 34886f93e834SAnand Jain if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 34896f93e834SAnand Jain if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 34906f93e834SAnand Jain btrfs_info(fs_info, 34916f93e834SAnand Jain "flagging fs with big metadata feature"); 34926f93e834SAnand Jain features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 34936f93e834SAnand Jain } 34946f93e834SAnand Jain 34956f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 34966f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 34976f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 34986f93e834SAnand Jain stripesize = sectorsize; 34996f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 35006f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 35016f93e834SAnand Jain 35026f93e834SAnand Jain fs_info->nodesize = nodesize; 35036f93e834SAnand Jain fs_info->sectorsize = sectorsize; 35046f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 35056f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 35066f93e834SAnand Jain fs_info->stripesize = stripesize; 35076f93e834SAnand Jain 35082ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 35092b82032cSYan Zheng if (ret) { 35102b82032cSYan Zheng err = ret; 3511141386e1SJosef Bacik goto fail_alloc; 35122b82032cSYan Zheng } 3513dfe25020SChris Mason 3514f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3515f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3516f2b636e8SJosef Bacik if (features) { 351705135f59SDavid Sterba btrfs_err(fs_info, 3518d5321a0fSQu Wenruo "cannot mount because of unsupported optional features (0x%llx)", 3519c1c9ff7cSGeert Uytterhoeven features); 3520f2b636e8SJosef Bacik err = -EINVAL; 3521141386e1SJosef Bacik goto fail_alloc; 3522f2b636e8SJosef Bacik } 3523f2b636e8SJosef Bacik 35245d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 35255d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 35260b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3527a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 35285c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 35295c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3530727011e0SChris Mason 35313173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 353205135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 35333173a18fSJosef Bacik 3534727011e0SChris Mason /* 3535bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3536bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3537bc3f116fSChris Mason */ 3538bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3539707e8a07SDavid Sterba (sectorsize != nodesize)) { 354005135f59SDavid Sterba btrfs_err(fs_info, 354105135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 354205135f59SDavid Sterba nodesize, sectorsize); 3543141386e1SJosef Bacik goto fail_alloc; 3544bc3f116fSChris Mason } 3545bc3f116fSChris Mason 3546ceda0864SMiao Xie /* 3547ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3548ceda0864SMiao Xie * update the flag. 3549ceda0864SMiao Xie */ 35505d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 35515d4f98a2SYan Zheng 3552f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3553f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3554bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 355505135f59SDavid Sterba btrfs_err(fs_info, 3556d5321a0fSQu Wenruo "cannot mount read-write because of unsupported optional features (0x%llx)", 3557c1c9ff7cSGeert Uytterhoeven features); 3558f2b636e8SJosef Bacik err = -EINVAL; 3559141386e1SJosef Bacik goto fail_alloc; 3560f2b636e8SJosef Bacik } 3561dc4d3168SQu Wenruo /* 3562dc4d3168SQu Wenruo * We have unsupported RO compat features, although RO mounted, we 3563dc4d3168SQu Wenruo * should not cause any metadata write, including log replay. 3564dc4d3168SQu Wenruo * Or we could screw up whatever the new feature requires. 3565dc4d3168SQu Wenruo */ 3566dc4d3168SQu Wenruo if (unlikely(features && btrfs_super_log_root(disk_super) && 3567dc4d3168SQu Wenruo !btrfs_test_opt(fs_info, NOLOGREPLAY))) { 3568dc4d3168SQu Wenruo btrfs_err(fs_info, 3569dc4d3168SQu Wenruo "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay", 3570dc4d3168SQu Wenruo features); 3571dc4d3168SQu Wenruo err = -EINVAL; 3572dc4d3168SQu Wenruo goto fail_alloc; 3573dc4d3168SQu Wenruo } 3574dc4d3168SQu Wenruo 357561d92c32SChris Mason 35768481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 35778481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 35788481dd80SQu Wenruo 35799f73f1aeSQu Wenruo /* 35809f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 35819f73f1aeSQu Wenruo * going to be deprecated. 35829f73f1aeSQu Wenruo * 35839f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 35849f73f1aeSQu Wenruo */ 35859f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 35869f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 35879f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 35889f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 35899f73f1aeSQu Wenruo 359095ea0486SQu Wenruo btrfs_warn(fs_info, 359195ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 35920bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 35938481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 35948481dd80SQu Wenruo if (!subpage_info) 35958481dd80SQu Wenruo goto fail_alloc; 35968481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 35978481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3598c8050b3bSQu Wenruo } 35990bb3eb3eSQu Wenruo 3600d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 36012a458198SEric Sandeen if (ret) { 36022a458198SEric Sandeen err = ret; 36030dc3b84aSJosef Bacik goto fail_sb_buffer; 36040dc3b84aSJosef Bacik } 36054543df7eSChris Mason 36069e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 36079e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 36084575c9ccSChris Mason 3609a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3610a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3611de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3612db94535dSChris Mason 3613925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 36146bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3615925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 361684eed90fSChris Mason if (ret) { 361705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36185d4f98a2SYan Zheng goto fail_sb_buffer; 361984eed90fSChris Mason } 36200b86a832SChris Mason 362184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3622581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3623bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3624bd676446SJosef Bacik generation, level); 3625bd676446SJosef Bacik if (ret) { 362605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3627af31f5e5SChris Mason goto fail_tree_roots; 362883121942SDavid Woodhouse } 36290b86a832SChris Mason 3630e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3631c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3632c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3633e17cade2SChris Mason 36345b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 36352b82032cSYan Zheng if (ret) { 363605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3637af31f5e5SChris Mason goto fail_tree_roots; 36382b82032cSYan Zheng } 36390b86a832SChris Mason 36408dabb742SStefan Behrens /* 3641bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3642bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3643bacce86aSAnand Jain * target is required. So free devices that are not part of this 36441a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3645bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 36468dabb742SStefan Behrens */ 3647bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3648d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 364905135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3650a6b0d5c8SChris Mason goto fail_tree_roots; 3651a6b0d5c8SChris Mason } 3652a6b0d5c8SChris Mason 3653b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 36544bbcaa64SEric Sandeen if (ret) 3655b8522a1eSNikolay Borisov goto fail_tree_roots; 36568929ecfaSYan, Zheng 365775ec1db8SJosef Bacik /* 365873651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 365973651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 366073651042SNaohiro Aota * zoned incompat feature flag set. 366173651042SNaohiro Aota */ 366273651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 366373651042SNaohiro Aota if (ret) { 366473651042SNaohiro Aota btrfs_err(fs_info, 366573651042SNaohiro Aota "zoned: failed to read device zone info: %d", 366673651042SNaohiro Aota ret); 366773651042SNaohiro Aota goto fail_block_groups; 366873651042SNaohiro Aota } 366973651042SNaohiro Aota 367073651042SNaohiro Aota /* 367175ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 367275ec1db8SJosef Bacik * check the generation here so we can set the 367375ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 367475ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 367575ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 367675ec1db8SJosef Bacik * even though it was perfectly fine. 367775ec1db8SJosef Bacik */ 367875ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 367975ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 368075ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 368175ec1db8SJosef Bacik 3682cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3683cf90d884SQu Wenruo if (ret) { 3684cf90d884SQu Wenruo btrfs_err(fs_info, 3685cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3686cf90d884SQu Wenruo ret); 3687cf90d884SQu Wenruo goto fail_block_groups; 3688cf90d884SQu Wenruo } 368968310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 369068310a5eSIlya Dryomov if (ret) { 369105135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 369268310a5eSIlya Dryomov goto fail_block_groups; 369368310a5eSIlya Dryomov } 369468310a5eSIlya Dryomov 3695733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3696733f4fbbSStefan Behrens if (ret) { 369705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3698733f4fbbSStefan Behrens goto fail_block_groups; 3699733f4fbbSStefan Behrens } 3700733f4fbbSStefan Behrens 37018dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 37028dabb742SStefan Behrens if (ret) { 370305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 37048dabb742SStefan Behrens goto fail_block_groups; 37058dabb742SStefan Behrens } 37068dabb742SStefan Behrens 3707b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3708b70f5097SNaohiro Aota if (ret) { 3709b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3710b70f5097SNaohiro Aota ret); 3711b70f5097SNaohiro Aota goto fail_block_groups; 3712b70f5097SNaohiro Aota } 3713b70f5097SNaohiro Aota 3714c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3715b7c35e81SAnand Jain if (ret) { 371605135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 371705135f59SDavid Sterba ret); 3718b7c35e81SAnand Jain goto fail_block_groups; 3719b7c35e81SAnand Jain } 3720b7c35e81SAnand Jain 372196f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 37225ac1d209SJeff Mahoney if (ret) { 372305135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3724b7c35e81SAnand Jain goto fail_fsdev_sysfs; 37255ac1d209SJeff Mahoney } 37265ac1d209SJeff Mahoney 3727c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3728c59021f8Sliubo if (ret) { 372905135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 37302365dd3cSAnand Jain goto fail_sysfs; 3731c59021f8Sliubo } 3732c59021f8Sliubo 37335b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 37341b1d1f66SJosef Bacik if (ret) { 373505135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 37362365dd3cSAnand Jain goto fail_sysfs; 37371b1d1f66SJosef Bacik } 37384330e183SQu Wenruo 373916beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 374016beac87SNaohiro Aota 37415c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 37425c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 374305135f59SDavid Sterba btrfs_warn(fs_info, 374452042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 37452365dd3cSAnand Jain goto fail_sysfs; 3746292fd7fcSStefan Behrens } 37479078a3e1SChris Mason 374833c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3749a74a4b97SChris Mason "btrfs-cleaner"); 375057506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 37512365dd3cSAnand Jain goto fail_sysfs; 3752a74a4b97SChris Mason 3753a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3754a74a4b97SChris Mason tree_root, 3755a74a4b97SChris Mason "btrfs-transaction"); 375657506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 37573f157a2fSChris Mason goto fail_cleaner; 3758a74a4b97SChris Mason 3759583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3760c289811cSChris Mason !fs_info->fs_devices->rotating) { 3761583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3762c289811cSChris Mason } 3763c289811cSChris Mason 3764572d9ab7SDavid Sterba /* 376501327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3766572d9ab7SDavid Sterba * not have to wait for transaction commit 3767572d9ab7SDavid Sterba */ 3768572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 37693818aea2SQu Wenruo 377021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 37710b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 37722ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 37730b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3774cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 377521adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 377621adbd5cSStefan Behrens if (ret) 377705135f59SDavid Sterba btrfs_warn(fs_info, 377805135f59SDavid Sterba "failed to initialize integrity check module: %d", 377905135f59SDavid Sterba ret); 378021adbd5cSStefan Behrens } 378121adbd5cSStefan Behrens #endif 3782bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3783bcef60f2SArne Jansen if (ret) 3784bcef60f2SArne Jansen goto fail_trans_kthread; 378521adbd5cSStefan Behrens 3786fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3787fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3788fd708b81SJosef Bacik 378996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 379096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 37910b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3792e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 379363443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 379479787eaaSJeff Mahoney if (ret) { 379563443bf5SEric Sandeen err = ret; 379628c16cbbSWang Shilong goto fail_qgroup; 3797e556ce2cSYan Zheng } 3798e02119d5SChris Mason } 37991a40e23bSZheng Yan 380056e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 38013140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 38023140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3803f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3804315bf8efSJosef Bacik fs_info->fs_root = NULL; 3805bcef60f2SArne Jansen goto fail_qgroup; 38063140c9a3SDan Carpenter } 3807c289811cSChris Mason 3808bc98a42cSDavid Howells if (sb_rdonly(sb)) 38098cd29088SBoris Burkov goto clear_oneshot; 38102b6ba629SIlya Dryomov 381144c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 38122b6ba629SIlya Dryomov if (ret) { 38136bccf3abSJeff Mahoney close_ctree(fs_info); 38142b6ba629SIlya Dryomov return ret; 3815e3acc2a6SJosef Bacik } 3816b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3817b382a324SJan Schmidt 381844c0ca21SBoris Burkov if (fs_info->uuid_root && 381944c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 382044c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 382105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 382270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 382370f80175SStefan Behrens if (ret) { 382405135f59SDavid Sterba btrfs_warn(fs_info, 382505135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 38266bccf3abSJeff Mahoney close_ctree(fs_info); 382770f80175SStefan Behrens return ret; 382870f80175SStefan Behrens } 3829f7a81ea4SStefan Behrens } 383094846229SBoris Burkov 3831afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 383247ab2a6cSJosef Bacik 3833b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3834b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3835b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3836b4be6aefSJosef Bacik 38378cd29088SBoris Burkov clear_oneshot: 38388cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3839ad2b2c80SAl Viro return 0; 384039279cc3SChris Mason 3841bcef60f2SArne Jansen fail_qgroup: 3842bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 38437c2ca468SChris Mason fail_trans_kthread: 38447c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 38452ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3846faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 38473f157a2fSChris Mason fail_cleaner: 3848a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 38497c2ca468SChris Mason 38507c2ca468SChris Mason /* 38517c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 38527c2ca468SChris Mason * kthreads 38537c2ca468SChris Mason */ 38547c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 38557c2ca468SChris Mason 38562365dd3cSAnand Jain fail_sysfs: 38576618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 38582365dd3cSAnand Jain 3859b7c35e81SAnand Jain fail_fsdev_sysfs: 3860b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3861b7c35e81SAnand Jain 38621b1d1f66SJosef Bacik fail_block_groups: 386354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3864af31f5e5SChris Mason 3865af31f5e5SChris Mason fail_tree_roots: 38669e3aa805SJosef Bacik if (fs_info->data_reloc_root) 38679e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 38684273eaffSAnand Jain free_root_pointers(fs_info, true); 38692b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3870af31f5e5SChris Mason 387139279cc3SChris Mason fail_sb_buffer: 38727abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 38735cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 387416cdcec7SMiao Xie fail_alloc: 3875586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3876586e46e2SIlya Dryomov 38774543df7eSChris Mason iput(fs_info->btree_inode); 38787e662854SQinghuang Feng fail: 3879586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3880ad2b2c80SAl Viro return err; 3881eb60ceacSChris Mason } 3882663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3883eb60ceacSChris Mason 3884314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3885f2984462SChris Mason { 3886314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3887314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3888314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3889314b6dd0SJohannes Thumshirn struct page *page; 3890442a4f63SStefan Behrens 3891314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3892314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3893314b6dd0SJohannes Thumshirn 3894314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3895fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3896314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3897314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3898314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3899314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3900314b6dd0SJohannes Thumshirn SetPageError(page); 3901314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3902314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3903314b6dd0SJohannes Thumshirn } else { 3904314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3905f2984462SChris Mason } 3906314b6dd0SJohannes Thumshirn 3907314b6dd0SJohannes Thumshirn put_page(page); 3908314b6dd0SJohannes Thumshirn unlock_page(page); 3909314b6dd0SJohannes Thumshirn } 3910314b6dd0SJohannes Thumshirn 3911314b6dd0SJohannes Thumshirn bio_put(bio); 3912f2984462SChris Mason } 3913f2984462SChris Mason 39148f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 39158f32380dSJohannes Thumshirn int copy_num) 391629c36d72SAnand Jain { 391729c36d72SAnand Jain struct btrfs_super_block *super; 39188f32380dSJohannes Thumshirn struct page *page; 391912659251SNaohiro Aota u64 bytenr, bytenr_orig; 39208f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 392112659251SNaohiro Aota int ret; 392229c36d72SAnand Jain 392312659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 392412659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 392512659251SNaohiro Aota if (ret == -ENOENT) 392612659251SNaohiro Aota return ERR_PTR(-EINVAL); 392712659251SNaohiro Aota else if (ret) 392812659251SNaohiro Aota return ERR_PTR(ret); 392912659251SNaohiro Aota 3930cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 39318f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 393229c36d72SAnand Jain 39338f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 39348f32380dSJohannes Thumshirn if (IS_ERR(page)) 39358f32380dSJohannes Thumshirn return ERR_CAST(page); 393629c36d72SAnand Jain 39378f32380dSJohannes Thumshirn super = page_address(page); 393896c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 393996c2e067SAnand Jain btrfs_release_disk_super(super); 394096c2e067SAnand Jain return ERR_PTR(-ENODATA); 394196c2e067SAnand Jain } 394296c2e067SAnand Jain 394312659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 39448f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 39458f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 394629c36d72SAnand Jain } 394729c36d72SAnand Jain 39488f32380dSJohannes Thumshirn return super; 394929c36d72SAnand Jain } 395029c36d72SAnand Jain 395129c36d72SAnand Jain 39528f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3953a512bbf8SYan Zheng { 39548f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3955a512bbf8SYan Zheng int i; 3956a512bbf8SYan Zheng u64 transid = 0; 3957a512bbf8SYan Zheng 3958a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3959a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3960a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3961a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3962a512bbf8SYan Zheng */ 3963a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 39648f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 39658f32380dSJohannes Thumshirn if (IS_ERR(super)) 3966a512bbf8SYan Zheng continue; 3967a512bbf8SYan Zheng 3968a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 39698f32380dSJohannes Thumshirn if (latest) 39708f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 39718f32380dSJohannes Thumshirn 39728f32380dSJohannes Thumshirn latest = super; 3973a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3974a512bbf8SYan Zheng } 3975a512bbf8SYan Zheng } 397692fc03fbSAnand Jain 39778f32380dSJohannes Thumshirn return super; 3978a512bbf8SYan Zheng } 3979a512bbf8SYan Zheng 39804eedeb75SHisashi Hifumi /* 3981abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3982314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 39834eedeb75SHisashi Hifumi * 3984abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3985abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3986abbb3b8eSDavid Sterba * same for write and wait phases. 39874eedeb75SHisashi Hifumi * 3988314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 39894eedeb75SHisashi Hifumi */ 3990a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3991abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3992a512bbf8SYan Zheng { 3993d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3994314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3995d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3996a512bbf8SYan Zheng int i; 3997a512bbf8SYan Zheng int errors = 0; 399812659251SNaohiro Aota int ret; 399912659251SNaohiro Aota u64 bytenr, bytenr_orig; 4000a512bbf8SYan Zheng 4001a512bbf8SYan Zheng if (max_mirrors == 0) 4002a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4003a512bbf8SYan Zheng 4004d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4005d5178578SJohannes Thumshirn 4006a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4007314b6dd0SJohannes Thumshirn struct page *page; 4008314b6dd0SJohannes Thumshirn struct bio *bio; 4009314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4010314b6dd0SJohannes Thumshirn 401112659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 401212659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 401312659251SNaohiro Aota if (ret == -ENOENT) { 401412659251SNaohiro Aota continue; 401512659251SNaohiro Aota } else if (ret < 0) { 401612659251SNaohiro Aota btrfs_err(device->fs_info, 401712659251SNaohiro Aota "couldn't get super block location for mirror %d", 401812659251SNaohiro Aota i); 401912659251SNaohiro Aota errors++; 402012659251SNaohiro Aota continue; 402112659251SNaohiro Aota } 4022935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4023935e5cc9SMiao Xie device->commit_total_bytes) 4024a512bbf8SYan Zheng break; 4025a512bbf8SYan Zheng 402612659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4027a512bbf8SYan Zheng 4028fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4029fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4030fd08001fSEric Biggers sb->csum); 4031a512bbf8SYan Zheng 4032314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4033314b6dd0SJohannes Thumshirn GFP_NOFS); 4034314b6dd0SJohannes Thumshirn if (!page) { 4035fb456252SJeff Mahoney btrfs_err(device->fs_info, 4036314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4037f14d104dSDavid Sterba bytenr); 4038634554dcSJosef Bacik errors++; 4039634554dcSJosef Bacik continue; 4040634554dcSJosef Bacik } 4041634554dcSJosef Bacik 4042314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4043314b6dd0SJohannes Thumshirn get_page(page); 4044a512bbf8SYan Zheng 4045314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4046314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4047a512bbf8SYan Zheng 4048387125fcSChris Mason /* 4049314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4050314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4051314b6dd0SJohannes Thumshirn * checking. 4052387125fcSChris Mason */ 405307888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 405407888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 405507888c66SChristoph Hellwig GFP_NOFS); 4056314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4057314b6dd0SJohannes Thumshirn bio->bi_private = device; 4058314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4059314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4060314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4061314b6dd0SJohannes Thumshirn 4062314b6dd0SJohannes Thumshirn /* 4063314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4064314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4065314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4066314b6dd0SJohannes Thumshirn */ 40671b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4068314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4069314b6dd0SJohannes Thumshirn 407058ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 407158ff51f1SChristoph Hellwig submit_bio(bio); 40728376d9e1SNaohiro Aota 40738376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 40748376d9e1SNaohiro Aota errors++; 4075a512bbf8SYan Zheng } 4076a512bbf8SYan Zheng return errors < i ? 0 : -1; 4077a512bbf8SYan Zheng } 4078a512bbf8SYan Zheng 4079387125fcSChris Mason /* 4080abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4081abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4082abbb3b8eSDavid Sterba * 4083314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4084abbb3b8eSDavid Sterba * date. 4085abbb3b8eSDavid Sterba */ 4086abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4087abbb3b8eSDavid Sterba { 4088abbb3b8eSDavid Sterba int i; 4089abbb3b8eSDavid Sterba int errors = 0; 4090b6a535faSHoward McLauchlan bool primary_failed = false; 409112659251SNaohiro Aota int ret; 4092abbb3b8eSDavid Sterba u64 bytenr; 4093abbb3b8eSDavid Sterba 4094abbb3b8eSDavid Sterba if (max_mirrors == 0) 4095abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4096abbb3b8eSDavid Sterba 4097abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4098314b6dd0SJohannes Thumshirn struct page *page; 4099314b6dd0SJohannes Thumshirn 410012659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 410112659251SNaohiro Aota if (ret == -ENOENT) { 410212659251SNaohiro Aota break; 410312659251SNaohiro Aota } else if (ret < 0) { 410412659251SNaohiro Aota errors++; 410512659251SNaohiro Aota if (i == 0) 410612659251SNaohiro Aota primary_failed = true; 410712659251SNaohiro Aota continue; 410812659251SNaohiro Aota } 4109abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4110abbb3b8eSDavid Sterba device->commit_total_bytes) 4111abbb3b8eSDavid Sterba break; 4112abbb3b8eSDavid Sterba 4113314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4114314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4115314b6dd0SJohannes Thumshirn if (!page) { 4116abbb3b8eSDavid Sterba errors++; 4117b6a535faSHoward McLauchlan if (i == 0) 4118b6a535faSHoward McLauchlan primary_failed = true; 4119abbb3b8eSDavid Sterba continue; 4120abbb3b8eSDavid Sterba } 4121314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4122314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4123314b6dd0SJohannes Thumshirn if (PageError(page)) { 4124abbb3b8eSDavid Sterba errors++; 4125b6a535faSHoward McLauchlan if (i == 0) 4126b6a535faSHoward McLauchlan primary_failed = true; 4127b6a535faSHoward McLauchlan } 4128abbb3b8eSDavid Sterba 4129314b6dd0SJohannes Thumshirn /* Drop our reference */ 4130314b6dd0SJohannes Thumshirn put_page(page); 4131abbb3b8eSDavid Sterba 4132314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4133314b6dd0SJohannes Thumshirn put_page(page); 4134abbb3b8eSDavid Sterba } 4135abbb3b8eSDavid Sterba 4136b6a535faSHoward McLauchlan /* log error, force error return */ 4137b6a535faSHoward McLauchlan if (primary_failed) { 4138b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4139b6a535faSHoward McLauchlan device->devid); 4140b6a535faSHoward McLauchlan return -1; 4141b6a535faSHoward McLauchlan } 4142b6a535faSHoward McLauchlan 4143abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4144abbb3b8eSDavid Sterba } 4145abbb3b8eSDavid Sterba 4146abbb3b8eSDavid Sterba /* 4147387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4148387125fcSChris Mason * for the barrier when it is done 4149387125fcSChris Mason */ 41504246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4151387125fcSChris Mason { 4152f9e69aa9SChristoph Hellwig bio_uninit(bio); 4153387125fcSChris Mason complete(bio->bi_private); 4154387125fcSChris Mason } 4155387125fcSChris Mason 4156387125fcSChris Mason /* 41574fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 41584fc6441aSAnand Jain * done in the waiting counterpart. 4159387125fcSChris Mason */ 41604fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4161387125fcSChris Mason { 4162f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 4163387125fcSChris Mason 4164a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4165a91cf0ffSWang Yugui /* 4166a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4167a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4168a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4169a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4170a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4171a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4172a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4173a91cf0ffSWang Yugui * non-debug builds. 4174a91cf0ffSWang Yugui */ 417508e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev)) 41764fc6441aSAnand Jain return; 4177a91cf0ffSWang Yugui #endif 4178387125fcSChris Mason 4179f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0, 4180f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4181387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4182387125fcSChris Mason init_completion(&device->flush_wait); 4183387125fcSChris Mason bio->bi_private = &device->flush_wait; 4184387125fcSChris Mason 418558ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 418658ff51f1SChristoph Hellwig submit_bio(bio); 41871c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 41884fc6441aSAnand Jain } 4189387125fcSChris Mason 41904fc6441aSAnand Jain /* 41914fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 41924fc6441aSAnand Jain */ 41938c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 41944fc6441aSAnand Jain { 4195f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 41964fc6441aSAnand Jain 41971c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 419858efbc9fSOmar Sandoval return BLK_STS_OK; 41994fc6441aSAnand Jain 42001c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42012980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 42024fc6441aSAnand Jain 42038c27cb35SLinus Torvalds return bio->bi_status; 4204387125fcSChris Mason } 4205387125fcSChris Mason 4206d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4207401b41e5SAnand Jain { 42086528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4209401b41e5SAnand Jain return -EIO; 4210387125fcSChris Mason return 0; 4211387125fcSChris Mason } 4212387125fcSChris Mason 4213387125fcSChris Mason /* 4214387125fcSChris Mason * send an empty flush down to each device in parallel, 4215387125fcSChris Mason * then wait for them 4216387125fcSChris Mason */ 4217387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4218387125fcSChris Mason { 4219387125fcSChris Mason struct list_head *head; 4220387125fcSChris Mason struct btrfs_device *dev; 42215af3e8ccSStefan Behrens int errors_wait = 0; 42224e4cbee9SChristoph Hellwig blk_status_t ret; 4223387125fcSChris Mason 42241538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4225387125fcSChris Mason /* send down all the barriers */ 4226387125fcSChris Mason head = &info->fs_devices->devices; 42271538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4228e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4229f88ba6a2SHidetoshi Seto continue; 4230cea7c8bfSAnand Jain if (!dev->bdev) 4231387125fcSChris Mason continue; 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 write_dev_flush(dev); 423758efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4238387125fcSChris Mason } 4239387125fcSChris Mason 4240387125fcSChris Mason /* wait for all the barriers */ 42411538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4242e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4243f88ba6a2SHidetoshi Seto continue; 4244387125fcSChris Mason if (!dev->bdev) { 42455af3e8ccSStefan Behrens errors_wait++; 4246387125fcSChris Mason continue; 4247387125fcSChris Mason } 4248e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4249ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4250387125fcSChris Mason continue; 4251387125fcSChris Mason 42524fc6441aSAnand Jain ret = wait_dev_flush(dev); 4253401b41e5SAnand Jain if (ret) { 4254401b41e5SAnand Jain dev->last_flush_error = ret; 425566b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 425666b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 42575af3e8ccSStefan Behrens errors_wait++; 4258387125fcSChris Mason } 4259401b41e5SAnand Jain } 4260401b41e5SAnand Jain 4261cea7c8bfSAnand Jain if (errors_wait) { 4262401b41e5SAnand Jain /* 4263401b41e5SAnand Jain * At some point we need the status of all disks 4264401b41e5SAnand Jain * to arrive at the volume status. So error checking 4265401b41e5SAnand Jain * is being pushed to a separate loop. 4266401b41e5SAnand Jain */ 4267d10b82feSQu Wenruo return check_barrier_error(info); 4268401b41e5SAnand Jain } 4269387125fcSChris Mason return 0; 4270387125fcSChris Mason } 4271387125fcSChris Mason 4272943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4273943c6e99SZhao Lei { 42748789f4feSZhao Lei int raid_type; 42758789f4feSZhao Lei int min_tolerated = INT_MAX; 4276943c6e99SZhao Lei 42778789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 42788789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 42798c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 42808789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 42818789f4feSZhao Lei tolerated_failures); 4282943c6e99SZhao Lei 42838789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 42848789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 42858789f4feSZhao Lei continue; 428641a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 42878789f4feSZhao Lei continue; 42888c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 42898789f4feSZhao Lei btrfs_raid_array[raid_type]. 42908789f4feSZhao Lei tolerated_failures); 42918789f4feSZhao Lei } 4292943c6e99SZhao Lei 42938789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4294ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 42958789f4feSZhao Lei min_tolerated = 0; 42968789f4feSZhao Lei } 42978789f4feSZhao Lei 42988789f4feSZhao Lei return min_tolerated; 4299943c6e99SZhao Lei } 4300943c6e99SZhao Lei 4301eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4302f2984462SChris Mason { 4303e5e9a520SChris Mason struct list_head *head; 4304f2984462SChris Mason struct btrfs_device *dev; 4305a061fc8dSChris Mason struct btrfs_super_block *sb; 4306f2984462SChris Mason struct btrfs_dev_item *dev_item; 4307f2984462SChris Mason int ret; 4308f2984462SChris Mason int do_barriers; 4309a236aed1SChris Mason int max_errors; 4310a236aed1SChris Mason int total_errors = 0; 4311a061fc8dSChris Mason u64 flags; 4312f2984462SChris Mason 43130b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4314fed3b381SLiu Bo 4315fed3b381SLiu Bo /* 4316fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4317fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4318fed3b381SLiu Bo * been consistent on disk. 4319fed3b381SLiu Bo */ 4320fed3b381SLiu Bo if (max_mirrors == 0) 43210b246afaSJeff Mahoney backup_super_roots(fs_info); 4322f2984462SChris Mason 43230b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4324a061fc8dSChris Mason dev_item = &sb->dev_item; 4325e5e9a520SChris Mason 43260b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 43270b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 43280b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4329387125fcSChris Mason 43305af3e8ccSStefan Behrens if (do_barriers) { 43310b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 43325af3e8ccSStefan Behrens if (ret) { 43335af3e8ccSStefan Behrens mutex_unlock( 43340b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 43350b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 43365af3e8ccSStefan Behrens "errors while submitting device barriers."); 43375af3e8ccSStefan Behrens return ret; 43385af3e8ccSStefan Behrens } 43395af3e8ccSStefan Behrens } 4340387125fcSChris Mason 43411538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4342dfe25020SChris Mason if (!dev->bdev) { 4343dfe25020SChris Mason total_errors++; 4344dfe25020SChris Mason continue; 4345dfe25020SChris Mason } 4346e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4347ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4348dfe25020SChris Mason continue; 4349dfe25020SChris Mason 43502b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4351a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4352a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 43537df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4354935e5cc9SMiao Xie dev->commit_total_bytes); 4355ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4356ce7213c7SMiao Xie dev->commit_bytes_used); 4357a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4358a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4359a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4360a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 43617239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 43627239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4363a512bbf8SYan Zheng 4364a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4365a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4366f2984462SChris Mason 436775cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 436875cb857dSQu Wenruo if (ret < 0) { 436975cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 437075cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 437175cb857dSQu Wenruo "unexpected superblock corruption detected"); 437275cb857dSQu Wenruo return -EUCLEAN; 437375cb857dSQu Wenruo } 437475cb857dSQu Wenruo 4375abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4376a236aed1SChris Mason if (ret) 4377a236aed1SChris Mason total_errors++; 4378f2984462SChris Mason } 4379a236aed1SChris Mason if (total_errors > max_errors) { 43800b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4381d397712bSChris Mason total_errors); 43820b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 438379787eaaSJeff Mahoney 43849d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 43850b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 43860b246afaSJeff Mahoney "%d errors while writing supers", 43870b246afaSJeff Mahoney total_errors); 43889d565ba4SStefan Behrens return -EIO; 4389a236aed1SChris Mason } 4390f2984462SChris Mason 4391a512bbf8SYan Zheng total_errors = 0; 43921538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4393dfe25020SChris Mason if (!dev->bdev) 4394dfe25020SChris Mason continue; 4395e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4396ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4397dfe25020SChris Mason continue; 4398dfe25020SChris Mason 4399abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4400a512bbf8SYan Zheng if (ret) 44011259ab75SChris Mason total_errors++; 4402f2984462SChris Mason } 44030b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4404a236aed1SChris Mason if (total_errors > max_errors) { 44050b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44060b246afaSJeff Mahoney "%d errors while writing supers", 44070b246afaSJeff Mahoney total_errors); 440879787eaaSJeff Mahoney return -EIO; 4409a236aed1SChris Mason } 4410f2984462SChris Mason return 0; 4411f2984462SChris Mason } 4412f2984462SChris Mason 4413cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4414cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4415cb517eabSMiao Xie struct btrfs_root *root) 44162619ba1fSChris Mason { 44174785e24fSJosef Bacik bool drop_ref = false; 44184785e24fSJosef Bacik 4419fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4420fc7cbcd4SDavid Sterba radix_tree_delete(&fs_info->fs_roots_radix, 4421fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid); 4422fc7cbcd4SDavid Sterba if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 44234785e24fSJosef Bacik drop_ref = true; 4424fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 442576dda93cSYan, Zheng 442684961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4427ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 44281c1ea4f7SLiu Bo if (root->reloc_root) { 442900246528SJosef Bacik btrfs_put_root(root->reloc_root); 44301c1ea4f7SLiu Bo root->reloc_root = NULL; 44311c1ea4f7SLiu Bo } 44321c1ea4f7SLiu Bo } 44333321719eSLiu Bo 44344785e24fSJosef Bacik if (drop_ref) 443500246528SJosef Bacik btrfs_put_root(root); 44362619ba1fSChris Mason } 44372619ba1fSChris Mason 4438c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4439c146afadSYan Zheng { 4440fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4441fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4442fc7cbcd4SDavid Sterba int i = 0; 444365d33fd7SQu Wenruo int err = 0; 4444fc7cbcd4SDavid Sterba unsigned int ret = 0; 4445c146afadSYan Zheng 4446c146afadSYan Zheng while (1) { 4447fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4448fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4449fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4450fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 4451fc7cbcd4SDavid Sterba if (!ret) { 4452fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4453c146afadSYan Zheng break; 445465d33fd7SQu Wenruo } 4455fc7cbcd4SDavid Sterba root_objectid = gang[ret - 1]->root_key.objectid + 1; 445666b4ffd1SJosef Bacik 4457fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4458fc7cbcd4SDavid Sterba /* Avoid to grab roots in dead_roots */ 4459fc7cbcd4SDavid Sterba if (btrfs_root_refs(&gang[i]->root_item) == 0) { 4460fc7cbcd4SDavid Sterba gang[i] = NULL; 446165d33fd7SQu Wenruo continue; 4462c146afadSYan Zheng } 4463fc7cbcd4SDavid Sterba /* grab all the search result for later use */ 4464fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4465fc7cbcd4SDavid Sterba } 4466fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4467fc7cbcd4SDavid Sterba 4468fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4469fc7cbcd4SDavid Sterba if (!gang[i]) 4470fc7cbcd4SDavid Sterba continue; 4471fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4472fc7cbcd4SDavid Sterba err = btrfs_orphan_cleanup(gang[i]); 4473fc7cbcd4SDavid Sterba if (err) 4474fc7cbcd4SDavid Sterba break; 4475fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4476fc7cbcd4SDavid Sterba } 4477fc7cbcd4SDavid Sterba root_objectid++; 4478c146afadSYan Zheng } 447965d33fd7SQu Wenruo 4480fc7cbcd4SDavid Sterba /* release the uncleaned roots due to error */ 4481fc7cbcd4SDavid Sterba for (; i < ret; i++) { 4482fc7cbcd4SDavid Sterba if (gang[i]) 4483fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 448465d33fd7SQu Wenruo } 448565d33fd7SQu Wenruo return err; 4486c146afadSYan Zheng } 4487c146afadSYan Zheng 44886bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4489c146afadSYan Zheng { 44906bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4491c146afadSYan Zheng struct btrfs_trans_handle *trans; 4492c146afadSYan Zheng 44930b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 44942ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 44950b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 44960b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4497c71bf099SYan, Zheng 4498c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 44990b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45000b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4501c71bf099SYan, Zheng 45027a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 45033612b495STsutomu Itoh if (IS_ERR(trans)) 45043612b495STsutomu Itoh return PTR_ERR(trans); 45053a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4506c146afadSYan Zheng } 4507c146afadSYan Zheng 450836c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 450936c86a9eSQu Wenruo { 451036c86a9eSQu Wenruo struct btrfs_transaction *trans; 451136c86a9eSQu Wenruo struct btrfs_transaction *tmp; 451236c86a9eSQu Wenruo bool found = false; 451336c86a9eSQu Wenruo 451436c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 451536c86a9eSQu Wenruo return; 451636c86a9eSQu Wenruo 451736c86a9eSQu Wenruo /* 451836c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 451936c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 452036c86a9eSQu Wenruo */ 452136c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 452236c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 452336c86a9eSQu Wenruo struct extent_state *cached = NULL; 452436c86a9eSQu Wenruo u64 dirty_bytes = 0; 452536c86a9eSQu Wenruo u64 cur = 0; 452636c86a9eSQu Wenruo u64 found_start; 452736c86a9eSQu Wenruo u64 found_end; 452836c86a9eSQu Wenruo 452936c86a9eSQu Wenruo found = true; 453036c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 453136c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 453236c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 453336c86a9eSQu Wenruo cur = found_end + 1; 453436c86a9eSQu Wenruo } 453536c86a9eSQu Wenruo btrfs_warn(fs_info, 453636c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 453736c86a9eSQu Wenruo trans->transid, dirty_bytes); 453836c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 453936c86a9eSQu Wenruo 454036c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 454136c86a9eSQu Wenruo fs_info->running_transaction = NULL; 454236c86a9eSQu Wenruo list_del_init(&trans->list); 454336c86a9eSQu Wenruo 454436c86a9eSQu Wenruo btrfs_put_transaction(trans); 454536c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 454636c86a9eSQu Wenruo } 454736c86a9eSQu Wenruo ASSERT(!found); 454836c86a9eSQu Wenruo } 454936c86a9eSQu Wenruo 4550b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4551eb60ceacSChris Mason { 4552c146afadSYan Zheng int ret; 4553e089f05cSChris Mason 4554afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 455531e70e52SFilipe Manana 455631e70e52SFilipe Manana /* 455731e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group, 455831e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park 455931e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after 456031e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang 456131e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is 456231e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when 456331e70e52SFilipe Manana * trying to stop the async reclaim task. 456431e70e52SFilipe Manana */ 456531e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work); 4566d6fd0ae2SOmar Sandoval /* 4567d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4568d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4569d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4570d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4571d6fd0ae2SOmar Sandoval */ 4572d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4573a2135011SChris Mason 4574b4be6aefSJosef Bacik /* 4575b4be6aefSJosef Bacik * If we had UNFINISHED_DROPS we could still be processing them, so 4576b4be6aefSJosef Bacik * clear that bit and wake up relocation so it can stop. 4577b4be6aefSJosef Bacik */ 4578b4be6aefSJosef Bacik btrfs_wake_unfinished_drop(fs_info); 4579b4be6aefSJosef Bacik 45807343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4581d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 45827343dd61SJustin Maggard 4583803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4584803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4585803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4586803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4587803b2f54SStefan Behrens 4588837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4589aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4590837d5b6eSIlya Dryomov 45918dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 45928dabb742SStefan Behrens 4593aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 45944cb5300bSChris Mason 45954cb5300bSChris Mason /* wait for any defraggers to finish */ 45964cb5300bSChris Mason wait_event(fs_info->transaction_wait, 45974cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 45984cb5300bSChris Mason 45994cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 460026176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 46014cb5300bSChris Mason 460221c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 460357056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4604576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 460521c7e756SMiao Xie 4606b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4607b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4608b0643e59SDennis Zhou 4609bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4610e44163e1SJeff Mahoney /* 4611d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4612d6fd0ae2SOmar Sandoval * unused block groups. 4613e44163e1SJeff Mahoney */ 46140b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4615e44163e1SJeff Mahoney 4616f0cc2cd7SFilipe Manana /* 4617f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4618f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4619f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4620f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4621f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4622f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4623f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4624f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4625f0cc2cd7SFilipe Manana * This is a very rare case. 4626f0cc2cd7SFilipe Manana */ 4627f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4628f0cc2cd7SFilipe Manana 46296bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4630d397712bSChris Mason if (ret) 463104892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4632c146afadSYan Zheng } 4633ed2ff2cbSChris Mason 463484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 46352ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4636acce952bSliubo 4637e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4638e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 46398929ecfaSYan, Zheng 4640e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4641afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4642f25784b3SYan Zheng 46435958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 46445958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 46455958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 46465958253cSQu Wenruo } 46475958253cSQu Wenruo 464804892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4649fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4650bcef60f2SArne Jansen 4651963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 465204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4653963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4654b0c68f8bSChris Mason } 465531153d81SYan Zheng 46565deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 46574297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 46585deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 46594297ff84SJosef Bacik 46606618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4661b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 46625ac1d209SJeff Mahoney 46631a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 46641a4319ccSLiu Bo 4665de348ee0SWang Shilong /* 4666de348ee0SWang Shilong * we must make sure there is not any read request to 4667de348ee0SWang Shilong * submit after we stopping all workers. 4668de348ee0SWang Shilong */ 4669de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 467096192499SJosef Bacik btrfs_stop_all_workers(fs_info); 467196192499SJosef Bacik 46720a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 467336c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 46740a31daa4SFilipe Manana 4675afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 46764273eaffSAnand Jain free_root_pointers(fs_info, true); 46778c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 46789ad6b7bcSChris Mason 46794e19443dSJosef Bacik /* 46804e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 46814e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 46824e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 46834e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 46844e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 46854e19443dSJosef Bacik */ 46864e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 46874e19443dSJosef Bacik 468813e6c37bSJosef Bacik iput(fs_info->btree_inode); 4689d6bfde87SChris Mason 469021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 46910b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 46922ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 469321adbd5cSStefan Behrens #endif 469421adbd5cSStefan Behrens 46950b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 469668c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4697eb60ceacSChris Mason } 4698eb60ceacSChris Mason 4699b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4700b9fab919SChris Mason int atomic) 4701ccd467d6SChris Mason { 47021259ab75SChris Mason int ret; 4703727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 47041259ab75SChris Mason 47050b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 47061259ab75SChris Mason if (!ret) 47071259ab75SChris Mason return ret; 47081259ab75SChris Mason 47091259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4710b9fab919SChris Mason parent_transid, atomic); 4711b9fab919SChris Mason if (ret == -EAGAIN) 4712b9fab919SChris Mason return ret; 47131259ab75SChris Mason return !ret; 47145f39d397SChris Mason } 47156702ed49SChris Mason 47165f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 47175f39d397SChris Mason { 47182f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 47195f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4720b9473439SChris Mason int was_dirty; 4721b4ce94deSChris Mason 472206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 472306ea65a3SJosef Bacik /* 472406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 472552042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 472606ea65a3SJosef Bacik * outside of the sanity tests. 472706ea65a3SJosef Bacik */ 4728b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 472906ea65a3SJosef Bacik return; 473006ea65a3SJosef Bacik #endif 473149d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 47320b246afaSJeff Mahoney if (transid != fs_info->generation) 47335d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 47340b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 47350b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4736e2d84521SMiao Xie if (!was_dirty) 4737104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4738e2d84521SMiao Xie buf->len, 47390b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 47401f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 474169fc6cbbSQu Wenruo /* 474269fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 474369fc6cbbSQu Wenruo * but item data not updated. 474469fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 474569fc6cbbSQu Wenruo */ 474669fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4747cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4748a4f78750SDavid Sterba btrfs_print_leaf(buf); 47491f21ef0aSFilipe Manana ASSERT(0); 47501f21ef0aSFilipe Manana } 47511f21ef0aSFilipe Manana #endif 4752eb60ceacSChris Mason } 4753eb60ceacSChris Mason 47542ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4755b53d3f5dSLiu Bo int flush_delayed) 475635b7e476SChris Mason { 4757188de649SChris Mason /* 4758188de649SChris Mason * looks as though older kernels can get into trouble with 4759188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4760188de649SChris Mason */ 4761e2d84521SMiao Xie int ret; 4762d6bfde87SChris Mason 47636933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4764d6bfde87SChris Mason return; 4765d6bfde87SChris Mason 4766b53d3f5dSLiu Bo if (flush_delayed) 47672ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 476816cdcec7SMiao Xie 4769d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4770d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4771d814a491SEthan Lien fs_info->dirty_metadata_batch); 4772e2d84521SMiao Xie if (ret > 0) { 47730b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 477416cdcec7SMiao Xie } 477516cdcec7SMiao Xie } 477616cdcec7SMiao Xie 47772ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 477816cdcec7SMiao Xie { 47792ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 478035b7e476SChris Mason } 4781b53d3f5dSLiu Bo 47822ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4783b53d3f5dSLiu Bo { 47842ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4785d6bfde87SChris Mason } 47866b80053dSChris Mason 47872ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4788acce952bSliubo { 4789fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4790fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4791fe816d0fSNikolay Borisov 47920b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 47932ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 47940b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4795acce952bSliubo 47960b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 47970b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4798acce952bSliubo } 4799acce952bSliubo 4800ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4801ef67963dSJosef Bacik { 4802fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4803fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4804fc7cbcd4SDavid Sterba int ret; 4805ef67963dSJosef Bacik 4806fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4807fc7cbcd4SDavid Sterba while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4808fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4809fc7cbcd4SDavid Sterba ARRAY_SIZE(gang))) != 0) { 4810fc7cbcd4SDavid Sterba int i; 4811ef67963dSJosef Bacik 4812fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 4813fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4814fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4815fc7cbcd4SDavid Sterba 4816fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4817fc7cbcd4SDavid Sterba if (!gang[i]) 4818ef67963dSJosef Bacik continue; 4819fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4820fc7cbcd4SDavid Sterba btrfs_free_log(NULL, gang[i]); 4821fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4822ef67963dSJosef Bacik } 4823fc7cbcd4SDavid Sterba root_objectid++; 4824fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4825ef67963dSJosef Bacik } 4826fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4827ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4828ef67963dSJosef Bacik } 4829ef67963dSJosef Bacik 4830143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4831acce952bSliubo { 4832acce952bSliubo struct btrfs_ordered_extent *ordered; 4833acce952bSliubo 4834199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4835779880efSJosef Bacik /* 4836779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4837779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4838779880efSJosef Bacik */ 4839199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4840779880efSJosef Bacik root_extent_list) 4841779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4842199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4843199c2a9cSMiao Xie } 4844199c2a9cSMiao Xie 4845199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4846199c2a9cSMiao Xie { 4847199c2a9cSMiao Xie struct btrfs_root *root; 4848199c2a9cSMiao Xie struct list_head splice; 4849199c2a9cSMiao Xie 4850199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4851199c2a9cSMiao Xie 4852199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4853199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4854199c2a9cSMiao Xie while (!list_empty(&splice)) { 4855199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4856199c2a9cSMiao Xie ordered_root); 48571de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 48581de2cfdeSJosef Bacik &fs_info->ordered_roots); 4859199c2a9cSMiao Xie 48602a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4861199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4862199c2a9cSMiao Xie 48632a85d9caSLiu Bo cond_resched(); 48642a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4865199c2a9cSMiao Xie } 4866199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 486774d5d229SJosef Bacik 486874d5d229SJosef Bacik /* 486974d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 487074d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 487174d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 487274d5d229SJosef Bacik * actually get run and error out properly. 487374d5d229SJosef Bacik */ 487474d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4875acce952bSliubo } 4876acce952bSliubo 487735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 48782ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4879acce952bSliubo { 4880acce952bSliubo struct rb_node *node; 4881acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4882acce952bSliubo struct btrfs_delayed_ref_node *ref; 4883acce952bSliubo int ret = 0; 4884acce952bSliubo 4885acce952bSliubo delayed_refs = &trans->delayed_refs; 4886acce952bSliubo 4887acce952bSliubo spin_lock(&delayed_refs->lock); 4888d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4889cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4890b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4891acce952bSliubo return ret; 4892acce952bSliubo } 4893acce952bSliubo 48945c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4895d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 48960e0adbcfSJosef Bacik struct rb_node *n; 4897e78417d1SJosef Bacik bool pin_bytes = false; 4898acce952bSliubo 4899d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4900d7df2c79SJosef Bacik href_node); 49013069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4902b939d1abSJosef Bacik continue; 49033069bd26SJosef Bacik 4904d7df2c79SJosef Bacik spin_lock(&head->lock); 4905e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 49060e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 49070e0adbcfSJosef Bacik ref_node); 4908d7df2c79SJosef Bacik ref->in_tree = 0; 4909e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 49100e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 49111d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 49121d57ee94SWang Xiaoguang list_del(&ref->add_list); 4913d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4914d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4915d7df2c79SJosef Bacik } 491654067ae9SJosef Bacik if (head->must_insert_reserved) 4917e78417d1SJosef Bacik pin_bytes = true; 491878a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4919fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4920d7df2c79SJosef Bacik spin_unlock(&head->lock); 4921acce952bSliubo spin_unlock(&delayed_refs->lock); 4922e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4923acce952bSliubo 4924f603bb94SNikolay Borisov if (pin_bytes) { 4925f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4926f603bb94SNikolay Borisov 4927f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4928f603bb94SNikolay Borisov BUG_ON(!cache); 4929f603bb94SNikolay Borisov 4930f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4931f603bb94SNikolay Borisov spin_lock(&cache->lock); 4932f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4933f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4934f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4935f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4936f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4937f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4938f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4939f603bb94SNikolay Borisov 4940f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4941f603bb94SNikolay Borisov 4942f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4943f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4944f603bb94SNikolay Borisov } 494531890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4946d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4947acce952bSliubo cond_resched(); 4948acce952bSliubo spin_lock(&delayed_refs->lock); 4949acce952bSliubo } 495081f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4951acce952bSliubo 4952acce952bSliubo spin_unlock(&delayed_refs->lock); 4953acce952bSliubo 4954acce952bSliubo return ret; 4955acce952bSliubo } 4956acce952bSliubo 4957143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4958acce952bSliubo { 4959acce952bSliubo struct btrfs_inode *btrfs_inode; 4960acce952bSliubo struct list_head splice; 4961acce952bSliubo 4962acce952bSliubo INIT_LIST_HEAD(&splice); 4963acce952bSliubo 4964eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4965eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4966acce952bSliubo 4967acce952bSliubo while (!list_empty(&splice)) { 4968fe816d0fSNikolay Borisov struct inode *inode = NULL; 4969eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4970acce952bSliubo delalloc_inodes); 4971fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4972eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4973acce952bSliubo 4974fe816d0fSNikolay Borisov /* 4975fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4976fe816d0fSNikolay Borisov * meanwhile. 4977fe816d0fSNikolay Borisov */ 4978fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4979fe816d0fSNikolay Borisov if (inode) { 4980fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4981fe816d0fSNikolay Borisov iput(inode); 4982fe816d0fSNikolay Borisov } 4983eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4984acce952bSliubo } 4985eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4986eb73c1b7SMiao Xie } 4987eb73c1b7SMiao Xie 4988eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4989eb73c1b7SMiao Xie { 4990eb73c1b7SMiao Xie struct btrfs_root *root; 4991eb73c1b7SMiao Xie struct list_head splice; 4992eb73c1b7SMiao Xie 4993eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4994eb73c1b7SMiao Xie 4995eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4996eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4997eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4998eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4999eb73c1b7SMiao Xie delalloc_root); 500000246528SJosef Bacik root = btrfs_grab_root(root); 5001eb73c1b7SMiao Xie BUG_ON(!root); 5002eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5003eb73c1b7SMiao Xie 5004eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 500500246528SJosef Bacik btrfs_put_root(root); 5006eb73c1b7SMiao Xie 5007eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5008eb73c1b7SMiao Xie } 5009eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5010acce952bSliubo } 5011acce952bSliubo 50122ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5013acce952bSliubo struct extent_io_tree *dirty_pages, 5014acce952bSliubo int mark) 5015acce952bSliubo { 5016acce952bSliubo int ret; 5017acce952bSliubo struct extent_buffer *eb; 5018acce952bSliubo u64 start = 0; 5019acce952bSliubo u64 end; 5020acce952bSliubo 5021acce952bSliubo while (1) { 5022acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5023e6138876SJosef Bacik mark, NULL); 5024acce952bSliubo if (ret) 5025acce952bSliubo break; 5026acce952bSliubo 502791166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5028acce952bSliubo while (start <= end) { 50290b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 50300b246afaSJeff Mahoney start += fs_info->nodesize; 5031fd8b2b61SJosef Bacik if (!eb) 5032acce952bSliubo continue; 5033fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5034acce952bSliubo 5035fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5036fd8b2b61SJosef Bacik &eb->bflags)) 5037fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5038fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5039acce952bSliubo } 5040acce952bSliubo } 5041acce952bSliubo 5042acce952bSliubo return ret; 5043acce952bSliubo } 5044acce952bSliubo 50452ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5046fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5047acce952bSliubo { 5048acce952bSliubo u64 start; 5049acce952bSliubo u64 end; 5050acce952bSliubo int ret; 5051acce952bSliubo 5052acce952bSliubo while (1) { 50530e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 50540e6ec385SFilipe Manana 5055fcd5e742SLu Fengqi /* 5056fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5057fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5058fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5059fcd5e742SLu Fengqi * the same extent range. 5060fcd5e742SLu Fengqi */ 5061fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5062acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 50630e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5064fcd5e742SLu Fengqi if (ret) { 5065fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5066acce952bSliubo break; 5067fcd5e742SLu Fengqi } 5068acce952bSliubo 50690e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 50700e6ec385SFilipe Manana free_extent_state(cached_state); 50712ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5072fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5073acce952bSliubo cond_resched(); 5074acce952bSliubo } 5075acce952bSliubo 5076acce952bSliubo return 0; 5077acce952bSliubo } 5078acce952bSliubo 507932da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5080c79a1751SLiu Bo { 5081c79a1751SLiu Bo struct inode *inode; 5082c79a1751SLiu Bo 5083c79a1751SLiu Bo inode = cache->io_ctl.inode; 5084c79a1751SLiu Bo if (inode) { 5085c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5086c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5087c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5088c79a1751SLiu Bo iput(inode); 5089c79a1751SLiu Bo } 5090bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5091c79a1751SLiu Bo btrfs_put_block_group(cache); 5092c79a1751SLiu Bo } 5093c79a1751SLiu Bo 5094c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 50952ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5096c79a1751SLiu Bo { 509732da5386SDavid Sterba struct btrfs_block_group *cache; 5098c79a1751SLiu Bo 5099c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5100c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5101c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 510232da5386SDavid Sterba struct btrfs_block_group, 5103c79a1751SLiu Bo dirty_list); 5104c79a1751SLiu Bo 5105c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5106c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5107c79a1751SLiu Bo list_del_init(&cache->io_list); 5108c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5109c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5110c79a1751SLiu Bo } 5111c79a1751SLiu Bo 5112c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5113c79a1751SLiu Bo spin_lock(&cache->lock); 5114c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5115c79a1751SLiu Bo spin_unlock(&cache->lock); 5116c79a1751SLiu Bo 5117c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5118c79a1751SLiu Bo btrfs_put_block_group(cache); 5119ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5120c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5121c79a1751SLiu Bo } 5122c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5123c79a1751SLiu Bo 512445ae2c18SNikolay Borisov /* 512545ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 512645ae2c18SNikolay Borisov * to use it without any locking 512745ae2c18SNikolay Borisov */ 5128c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5129c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 513032da5386SDavid Sterba struct btrfs_block_group, 5131c79a1751SLiu Bo io_list); 5132c79a1751SLiu Bo 5133c79a1751SLiu Bo list_del_init(&cache->io_list); 5134c79a1751SLiu Bo spin_lock(&cache->lock); 5135c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5136c79a1751SLiu Bo spin_unlock(&cache->lock); 5137c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5138c79a1751SLiu Bo } 5139c79a1751SLiu Bo } 5140c79a1751SLiu Bo 514149b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 51422ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 514349b25e05SJeff Mahoney { 5144bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5145bbbf7243SNikolay Borisov 51462ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5147c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5148c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5149c79a1751SLiu Bo 5150bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5151bbbf7243SNikolay Borisov post_commit_list) { 5152bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5153bbbf7243SNikolay Borisov } 5154bbbf7243SNikolay Borisov 51552ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 515649b25e05SJeff Mahoney 51574a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 51580b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 515949b25e05SJeff Mahoney 51604a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 51610b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 516249b25e05SJeff Mahoney 5163ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 516467cde344SMiao Xie 51652ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 516649b25e05SJeff Mahoney EXTENT_DIRTY); 5167fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 516849b25e05SJeff Mahoney 5169d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5170d3575156SNaohiro Aota 51714a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 51724a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 517349b25e05SJeff Mahoney } 517449b25e05SJeff Mahoney 51752ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5176acce952bSliubo { 5177acce952bSliubo struct btrfs_transaction *t; 5178acce952bSliubo 51790b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5180acce952bSliubo 51810b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 51820b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 51830b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5184724e2315SJosef Bacik struct btrfs_transaction, list); 5185724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 51869b64f57dSElena Reshetova refcount_inc(&t->use_count); 51870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 51882ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5189724e2315SJosef Bacik btrfs_put_transaction(t); 51900b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5191724e2315SJosef Bacik continue; 5192724e2315SJosef Bacik } 51930b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5194724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 51950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5196724e2315SJosef Bacik /* 5197724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5198724e2315SJosef Bacik * handle open currently for this transaction. 5199724e2315SJosef Bacik */ 5200724e2315SJosef Bacik wait_event(t->writer_wait, 5201724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5202724e2315SJosef Bacik } else { 52030b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5204724e2315SJosef Bacik } 52052ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5206724e2315SJosef Bacik 52070b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52080b246afaSJeff Mahoney if (t == fs_info->running_transaction) 52090b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5210724e2315SJosef Bacik list_del_init(&t->list); 52110b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5212a4abeea4SJosef Bacik 5213724e2315SJosef Bacik btrfs_put_transaction(t); 52142e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 52150b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5216724e2315SJosef Bacik } 52170b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52180b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5219ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5220ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 52210b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5222ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 52230b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5224acce952bSliubo 5225acce952bSliubo return 0; 5226acce952bSliubo } 5227ec7d6dfdSNikolay Borisov 5228453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5229ec7d6dfdSNikolay Borisov { 5230ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5231ec7d6dfdSNikolay Borisov int ret; 5232ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5233ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5234ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5235ec7d6dfdSNikolay Borisov int slot; 5236ec7d6dfdSNikolay Borisov 5237ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5238ec7d6dfdSNikolay Borisov if (!path) 5239ec7d6dfdSNikolay Borisov return -ENOMEM; 5240ec7d6dfdSNikolay Borisov 5241ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5242ec7d6dfdSNikolay Borisov search_key.type = -1; 5243ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5244ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5245ec7d6dfdSNikolay Borisov if (ret < 0) 5246ec7d6dfdSNikolay Borisov goto error; 5247ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5248ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5249ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5250ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5251ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 525223125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 525323125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5254ec7d6dfdSNikolay Borisov } else { 525523125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5256ec7d6dfdSNikolay Borisov } 5257ec7d6dfdSNikolay Borisov ret = 0; 5258ec7d6dfdSNikolay Borisov error: 5259ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5260ec7d6dfdSNikolay Borisov return ret; 5261ec7d6dfdSNikolay Borisov } 5262ec7d6dfdSNikolay Borisov 5263543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5264ec7d6dfdSNikolay Borisov { 5265ec7d6dfdSNikolay Borisov int ret; 5266ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5267ec7d6dfdSNikolay Borisov 52686b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5269ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5270ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5271ec7d6dfdSNikolay Borisov root->root_key.objectid); 5272ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5273ec7d6dfdSNikolay Borisov goto out; 5274ec7d6dfdSNikolay Borisov } 5275ec7d6dfdSNikolay Borisov 527623125104SNikolay Borisov *objectid = root->free_objectid++; 5277ec7d6dfdSNikolay Borisov ret = 0; 5278ec7d6dfdSNikolay Borisov out: 5279ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5280ec7d6dfdSNikolay Borisov return ret; 5281ec7d6dfdSNikolay Borisov } 5282