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> 835b7e476SChris Mason #include <linux/writeback.h> 9ce9adaa5SChris Mason #include <linux/workqueue.h> 10a74a4b97SChris Mason #include <linux/kthread.h> 115a0e3ad6STejun Heo #include <linux/slab.h> 12784b4e29SChris Mason #include <linux/migrate.h> 137a36ddecSDavid Sterba #include <linux/ratelimit.h> 146463fe58SStefan Behrens #include <linux/uuid.h> 15803b2f54SStefan Behrens #include <linux/semaphore.h> 16540adea3SMasami Hiramatsu #include <linux/error-injection.h> 179678c543SNikolay Borisov #include <linux/crc32c.h> 18b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 197e75bf3fSDavid Sterba #include <asm/unaligned.h> 206d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 21eb60ceacSChris Mason #include "ctree.h" 22eb60ceacSChris Mason #include "disk-io.h" 23e089f05cSChris Mason #include "transaction.h" 240f7d52f4SChris Mason #include "btrfs_inode.h" 250b86a832SChris Mason #include "volumes.h" 26db94535dSChris Mason #include "print-tree.h" 27925baeddSChris Mason #include "locking.h" 28e02119d5SChris Mason #include "tree-log.h" 29fa9c0d79SChris Mason #include "free-space-cache.h" 3070f6d82eSOmar Sandoval #include "free-space-tree.h" 3121adbd5cSStefan Behrens #include "check-integrity.h" 32606686eeSJosef Bacik #include "rcu-string.h" 338dabb742SStefan Behrens #include "dev-replace.h" 3453b381b3SDavid Woodhouse #include "raid56.h" 355ac1d209SJeff Mahoney #include "sysfs.h" 36fcebe456SJosef Bacik #include "qgroup.h" 37ebb8765bSAnand Jain #include "compression.h" 38557ea5ddSQu Wenruo #include "tree-checker.h" 39fd708b81SJosef Bacik #include "ref-verify.h" 40aac0023cSJosef Bacik #include "block-group.h" 41b0643e59SDennis Zhou #include "discard.h" 42f603bb94SNikolay Borisov #include "space-info.h" 43b70f5097SNaohiro Aota #include "zoned.h" 44139e8cd3SQu Wenruo #include "subpage.h" 45eb60ceacSChris Mason 46319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 47319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 48319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 50e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 52319e4d06SQu Wenruo 538b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 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 66d352ac68SChris Mason /* 6797eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6897eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 69d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 70d352ac68SChris Mason */ 7197eb6b69SDavid Sterba struct btrfs_end_io_wq { 72ce9adaa5SChris Mason struct bio *bio; 73ce9adaa5SChris Mason bio_end_io_t *end_io; 74ce9adaa5SChris Mason void *private; 75ce9adaa5SChris Mason struct btrfs_fs_info *info; 764e4cbee9SChristoph Hellwig blk_status_t status; 77bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 788b712842SChris Mason struct btrfs_work work; 79ce9adaa5SChris Mason }; 800da5468fSChris Mason 8197eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8297eb6b69SDavid Sterba 8397eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8497eb6b69SDavid Sterba { 8597eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8697eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8797eb6b69SDavid Sterba 0, 88fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 8997eb6b69SDavid Sterba NULL); 9097eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9197eb6b69SDavid Sterba return -ENOMEM; 9297eb6b69SDavid Sterba return 0; 9397eb6b69SDavid Sterba } 9497eb6b69SDavid Sterba 95e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9697eb6b69SDavid Sterba { 9797eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9897eb6b69SDavid Sterba } 9997eb6b69SDavid Sterba 100141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 101141386e1SJosef Bacik { 102141386e1SJosef Bacik if (fs_info->csum_shash) 103141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 104141386e1SJosef Bacik } 105141386e1SJosef Bacik 106d352ac68SChris Mason /* 107d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 108d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 109d352ac68SChris Mason * just before they are sent down the IO stack. 110d352ac68SChris Mason */ 11144b8bd7eSChris Mason struct async_submit_bio { 1128896a08dSQu Wenruo struct inode *inode; 11344b8bd7eSChris Mason struct bio *bio; 114a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11544b8bd7eSChris Mason int mirror_num; 1161941b64bSQu Wenruo 1171941b64bSQu Wenruo /* Optional parameter for submit_bio_start used by direct io */ 1181941b64bSQu Wenruo u64 dio_file_offset; 1198b712842SChris Mason struct btrfs_work work; 1204e4cbee9SChristoph Hellwig blk_status_t status; 12144b8bd7eSChris Mason }; 12244b8bd7eSChris Mason 12385d4e461SChris Mason /* 12485d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12585d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12685d4e461SChris Mason * the level the eb occupies in the tree. 1274008c04aSChris Mason * 12885d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12985d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13085d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1314fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1324fd786e6SMisono Tomohiro * roots have separate keysets. 1334008c04aSChris Mason * 13485d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13585d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13685d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1374008c04aSChris Mason * 13885d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13985d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14085d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14185d4e461SChris Mason * 14285d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14385d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14485d4e461SChris Mason * needs update as well. 1454008c04aSChris Mason */ 1464008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1474008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1484008c04aSChris Mason # error 1494008c04aSChris Mason # endif 15085d4e461SChris Mason 151ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 152ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 153ab1405aaSDavid Sterba 154ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 155ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 156ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 157ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 163ab1405aaSDavid Sterba 16485d4e461SChris Mason static struct btrfs_lockdep_keyset { 16585d4e461SChris Mason u64 id; /* root objectid */ 166ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 167387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 168387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 16985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 170ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 171ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 172ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 173ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 174ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 175ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 176ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 177ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 178ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 179ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 180ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 181ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1824008c04aSChris Mason }; 18385d4e461SChris Mason 184ab1405aaSDavid Sterba #undef DEFINE_LEVEL 185ab1405aaSDavid Sterba #undef DEFINE_NAME 18685d4e461SChris Mason 18785d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18885d4e461SChris Mason int level) 18985d4e461SChris Mason { 19085d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19185d4e461SChris Mason 19285d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19385d4e461SChris Mason 19485d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19585d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19685d4e461SChris Mason if (ks->id == objectid) 19785d4e461SChris Mason break; 19885d4e461SChris Mason 19985d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20085d4e461SChris Mason &ks->keys[level], ks->names[level]); 20185d4e461SChris Mason } 20285d4e461SChris Mason 2034008c04aSChris Mason #endif 2044008c04aSChris Mason 205d352ac68SChris Mason /* 2062996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 207d352ac68SChris Mason */ 208c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 20919c00ddcSChris Mason { 210d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 2117280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 212a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 213d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 21419c00ddcSChris Mason char *kaddr; 215e9be5a30SDavid Sterba int i; 216d5178578SJohannes Thumshirn 217d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 218d5178578SJohannes Thumshirn crypto_shash_init(shash); 219a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 220e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 221a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 22219c00ddcSChris Mason 223e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 224e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 225e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 22619c00ddcSChris Mason } 22771a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 228d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 22919c00ddcSChris Mason } 23019c00ddcSChris Mason 231d352ac68SChris Mason /* 232d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 233d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 234d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 235d352ac68SChris Mason * in the wrong place. 236d352ac68SChris Mason */ 2371259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 238b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 239b9fab919SChris Mason int atomic) 2401259ab75SChris Mason { 2412ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2421259ab75SChris Mason int ret; 2431259ab75SChris Mason 2441259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2451259ab75SChris Mason return 0; 2461259ab75SChris Mason 247b9fab919SChris Mason if (atomic) 248b9fab919SChris Mason return -EAGAIN; 249b9fab919SChris Mason 2502ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 251ff13db41SDavid Sterba &cached_state); 2520b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2531259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2541259ab75SChris Mason ret = 0; 2551259ab75SChris Mason goto out; 2561259ab75SChris Mason } 25794647322SDavid Sterba btrfs_err_rl(eb->fs_info, 25894647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 25994647322SDavid Sterba eb->start, 26029549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2611259ab75SChris Mason ret = 1; 2620b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 26333958dc6SChris Mason out: 2642ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 265e43bbe5eSDavid Sterba &cached_state); 2661259ab75SChris Mason return ret; 2671259ab75SChris Mason } 2681259ab75SChris Mason 269e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 270e7e16f48SJohannes Thumshirn { 271e7e16f48SJohannes Thumshirn switch (csum_type) { 272e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2733951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2743831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 275352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 276e7e16f48SJohannes Thumshirn return true; 277e7e16f48SJohannes Thumshirn default: 278e7e16f48SJohannes Thumshirn return false; 279e7e16f48SJohannes Thumshirn } 280e7e16f48SJohannes Thumshirn } 281e7e16f48SJohannes Thumshirn 282d352ac68SChris Mason /* 2831104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 2841104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 2851104a885SDavid Sterba */ 286ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 287ab8d0fc4SJeff Mahoney char *raw_disk_sb) 2881104a885SDavid Sterba { 2891104a885SDavid Sterba struct btrfs_super_block *disk_sb = 2901104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 29151bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 292d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 293d5178578SJohannes Thumshirn 294d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 2951104a885SDavid Sterba 2961104a885SDavid Sterba /* 2971104a885SDavid Sterba * The super_block structure does not span the whole 29851bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 29951bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3001104a885SDavid Sterba */ 301fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 302fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3031104a885SDavid Sterba 30455fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 305e7e16f48SJohannes Thumshirn return 1; 3061104a885SDavid Sterba 307e7e16f48SJohannes Thumshirn return 0; 3081104a885SDavid Sterba } 3091104a885SDavid Sterba 310e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 311ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 312581c1760SQu Wenruo { 313e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 314581c1760SQu Wenruo int found_level; 315581c1760SQu Wenruo struct btrfs_key found_key; 316581c1760SQu Wenruo int ret; 317581c1760SQu Wenruo 318581c1760SQu Wenruo found_level = btrfs_header_level(eb); 319581c1760SQu Wenruo if (found_level != level) { 32063489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 32163489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 322581c1760SQu Wenruo btrfs_err(fs_info, 323581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 324581c1760SQu Wenruo eb->start, level, found_level); 325581c1760SQu Wenruo return -EIO; 326581c1760SQu Wenruo } 327581c1760SQu Wenruo 328581c1760SQu Wenruo if (!first_key) 329581c1760SQu Wenruo return 0; 330581c1760SQu Wenruo 3315d41be6fSQu Wenruo /* 3325d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3335d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3345d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3355d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3365d41be6fSQu Wenruo */ 3375d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3385d41be6fSQu Wenruo return 0; 33962fdaa52SQu Wenruo 34062fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 34162fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 34262fdaa52SQu Wenruo btrfs_err(fs_info, 34362fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 34462fdaa52SQu Wenruo eb->start); 34562fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 34662fdaa52SQu Wenruo return -EUCLEAN; 34762fdaa52SQu Wenruo } 34862fdaa52SQu Wenruo 349581c1760SQu Wenruo if (found_level) 350581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 351581c1760SQu Wenruo else 352581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 353581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 354581c1760SQu Wenruo 355581c1760SQu Wenruo if (ret) { 35663489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 35763489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 358581c1760SQu Wenruo btrfs_err(fs_info, 359ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 360ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 361ff76a864SLiu Bo first_key->type, first_key->offset, 362ff76a864SLiu Bo found_key.objectid, found_key.type, 363ff76a864SLiu Bo found_key.offset); 364581c1760SQu Wenruo } 365581c1760SQu Wenruo return ret; 366581c1760SQu Wenruo } 367581c1760SQu Wenruo 3681104a885SDavid Sterba /* 369d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 370d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 371581c1760SQu Wenruo * 372581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 373581c1760SQu Wenruo * @level: expected level, mandatory check 374581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 375d352ac68SChris Mason */ 3766a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb, 377581c1760SQu Wenruo u64 parent_transid, int level, 378581c1760SQu Wenruo struct btrfs_key *first_key) 379f188591eSChris Mason { 3805ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 381f188591eSChris Mason struct extent_io_tree *io_tree; 382ea466794SJosef Bacik int failed = 0; 383f188591eSChris Mason int ret; 384f188591eSChris Mason int num_copies = 0; 385f188591eSChris Mason int mirror_num = 0; 386ea466794SJosef Bacik int failed_mirror = 0; 387f188591eSChris Mason 3880b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 389f188591eSChris Mason while (1) { 390f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 391c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 392256dd1bbSStefan Behrens if (!ret) { 393581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 394b9fab919SChris Mason parent_transid, 0)) 395256dd1bbSStefan Behrens ret = -EIO; 396e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 397ff76a864SLiu Bo first_key, parent_transid)) 398581c1760SQu Wenruo ret = -EUCLEAN; 399581c1760SQu Wenruo else 400581c1760SQu Wenruo break; 401256dd1bbSStefan Behrens } 402d397712bSChris Mason 4030b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 404f188591eSChris Mason eb->start, eb->len); 4054235298eSChris Mason if (num_copies == 1) 406ea466794SJosef Bacik break; 4074235298eSChris Mason 4085cf1ab56SJosef Bacik if (!failed_mirror) { 4095cf1ab56SJosef Bacik failed = 1; 4105cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4115cf1ab56SJosef Bacik } 4125cf1ab56SJosef Bacik 413f188591eSChris Mason mirror_num++; 414ea466794SJosef Bacik if (mirror_num == failed_mirror) 415ea466794SJosef Bacik mirror_num++; 416ea466794SJosef Bacik 4174235298eSChris Mason if (mirror_num > num_copies) 418ea466794SJosef Bacik break; 419f188591eSChris Mason } 420ea466794SJosef Bacik 421c0901581SStefan Behrens if (failed && !ret && failed_mirror) 42220a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 423ea466794SJosef Bacik 424ea466794SJosef Bacik return ret; 425f188591eSChris Mason } 42619c00ddcSChris Mason 427eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 428eca0f6f6SQu Wenruo { 429eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 430eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 431eca0f6f6SQu Wenruo int ret; 432eca0f6f6SQu Wenruo 433eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 434eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 435eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 436eca0f6f6SQu Wenruo csum_tree_block(eb, result); 437eca0f6f6SQu Wenruo 438eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 439eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 440eca0f6f6SQu Wenruo else 441eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 442eca0f6f6SQu Wenruo 4433777369fSQu Wenruo if (ret < 0) 4443777369fSQu Wenruo goto error; 4453777369fSQu Wenruo 4463777369fSQu Wenruo /* 4473777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than 4483777369fSQu Wenruo * last committed. Or something seriously wrong happened. 4493777369fSQu Wenruo */ 4503777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { 4513777369fSQu Wenruo ret = -EUCLEAN; 452eca0f6f6SQu Wenruo btrfs_err(fs_info, 4533777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu", 4543777369fSQu Wenruo eb->start, btrfs_header_generation(eb), 4553777369fSQu Wenruo fs_info->last_trans_committed); 4563777369fSQu Wenruo goto error; 457eca0f6f6SQu Wenruo } 458eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 459eca0f6f6SQu Wenruo 460eca0f6f6SQu Wenruo return 0; 4613777369fSQu Wenruo 4623777369fSQu Wenruo error: 4633777369fSQu Wenruo btrfs_print_tree(eb, 0); 4643777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected", 4653777369fSQu Wenruo eb->start); 4663777369fSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4673777369fSQu Wenruo return ret; 468eca0f6f6SQu Wenruo } 469eca0f6f6SQu Wenruo 470eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 471eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 472eca0f6f6SQu Wenruo struct bio_vec *bvec) 473eca0f6f6SQu Wenruo { 474eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 475eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 476eca0f6f6SQu Wenruo u64 cur; 477eca0f6f6SQu Wenruo int ret = 0; 478eca0f6f6SQu Wenruo 479eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 480eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 481eca0f6f6SQu Wenruo struct extent_buffer *eb; 482eca0f6f6SQu Wenruo bool uptodate; 483eca0f6f6SQu Wenruo 484eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 485eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 486eca0f6f6SQu Wenruo fs_info->nodesize); 487eca0f6f6SQu Wenruo 4888ee92268SGabriel Niebler /* A dirty eb shouldn't disappear from extent_buffers */ 489eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 490eca0f6f6SQu Wenruo return -EUCLEAN; 491eca0f6f6SQu Wenruo 492eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 493eca0f6f6SQu Wenruo free_extent_buffer(eb); 494eca0f6f6SQu Wenruo return -EUCLEAN; 495eca0f6f6SQu Wenruo } 496eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 497eca0f6f6SQu Wenruo free_extent_buffer(eb); 498eca0f6f6SQu Wenruo return -EUCLEAN; 499eca0f6f6SQu Wenruo } 500eca0f6f6SQu Wenruo 501eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 502eca0f6f6SQu Wenruo free_extent_buffer(eb); 503eca0f6f6SQu Wenruo if (ret < 0) 504eca0f6f6SQu Wenruo return ret; 505eca0f6f6SQu Wenruo } 506eca0f6f6SQu Wenruo return ret; 507eca0f6f6SQu Wenruo } 508eca0f6f6SQu Wenruo 509d352ac68SChris Mason /* 510ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 511ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 512ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 513d352ac68SChris Mason */ 514ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 51519c00ddcSChris Mason { 516ac303b69SQu Wenruo struct page *page = bvec->bv_page; 5174eee4fa4SMiao Xie u64 start = page_offset(page); 51819c00ddcSChris Mason u64 found_start; 51919c00ddcSChris Mason struct extent_buffer *eb; 520eca0f6f6SQu Wenruo 521fbca46ebSQu Wenruo if (fs_info->nodesize < PAGE_SIZE) 522eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 523f188591eSChris Mason 5244f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5254f2de97aSJosef Bacik if (page != eb->pages[0]) 5264f2de97aSJosef Bacik return 0; 5270f805531SAlex Lyakas 52819c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 529d3575156SNaohiro Aota 530d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 531d3575156SNaohiro Aota WARN_ON(found_start != 0); 532d3575156SNaohiro Aota return 0; 533d3575156SNaohiro Aota } 534d3575156SNaohiro Aota 5350f805531SAlex Lyakas /* 5360f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5370f805531SAlex Lyakas * It is useful to know what went wrong. 5380f805531SAlex Lyakas */ 5390f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5400f805531SAlex Lyakas return -EUCLEAN; 5410f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5420f805531SAlex Lyakas return -EUCLEAN; 5430f805531SAlex Lyakas 544eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 54519c00ddcSChris Mason } 54619c00ddcSChris Mason 547b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5482b82032cSYan Zheng { 549b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 550944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 55144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 552944d3f9fSNikolay Borisov u8 *metadata_uuid; 5532b82032cSYan Zheng 5549a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5559a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5567239ff4bSNikolay Borisov /* 557944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 558944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 559944d3f9fSNikolay Borisov * ->fsid in this case is fine 5607239ff4bSNikolay Borisov */ 561944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5627239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5637239ff4bSNikolay Borisov else 5647239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5657239ff4bSNikolay Borisov 566944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 567944d3f9fSNikolay Borisov return 0; 568944d3f9fSNikolay Borisov 569944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 570944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 571944d3f9fSNikolay Borisov return 0; 572944d3f9fSNikolay Borisov 573944d3f9fSNikolay Borisov return 1; 5742b82032cSYan Zheng } 5752b82032cSYan Zheng 57677bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 57777bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 578ce9adaa5SChris Mason { 57977bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 580ce9adaa5SChris Mason u64 found_start; 58177bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 58277bf40a2SQu Wenruo u8 found_level; 5832996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 584dfd29eedSDavid Sterba const u8 *header_csum; 58577bf40a2SQu Wenruo int ret = 0; 586ea466794SJosef Bacik 587ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 588727011e0SChris Mason if (found_start != eb->start) { 589893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 590893bf4b1SSu Yue eb->start, found_start); 591f188591eSChris Mason ret = -EIO; 59277bf40a2SQu Wenruo goto out; 593ce9adaa5SChris Mason } 594b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 59502873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 59694647322SDavid Sterba eb->start); 5971259ab75SChris Mason ret = -EIO; 59877bf40a2SQu Wenruo goto out; 5991259ab75SChris Mason } 600ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6011c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 602893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 603893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6041c24c3ceSJosef Bacik ret = -EIO; 60577bf40a2SQu Wenruo goto out; 6061c24c3ceSJosef Bacik } 607ce9adaa5SChris Mason 608c67b3892SDavid Sterba csum_tree_block(eb, result); 609dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 610dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 611a826d6dcSJosef Bacik 612dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 6132996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 614ff14aa79SDavid Sterba "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 615ff14aa79SDavid Sterba eb->start, 616dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 61735be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 61835be8851SJohannes Thumshirn btrfs_header_level(eb)); 6192996e1f8SJohannes Thumshirn ret = -EUCLEAN; 62077bf40a2SQu Wenruo goto out; 6212996e1f8SJohannes Thumshirn } 6222996e1f8SJohannes Thumshirn 623a826d6dcSJosef Bacik /* 624a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 625a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 626a826d6dcSJosef Bacik * return -EIO. 627a826d6dcSJosef Bacik */ 6281c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 629a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 630a826d6dcSJosef Bacik ret = -EIO; 631a826d6dcSJosef Bacik } 632ce9adaa5SChris Mason 633813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 634053ab70fSLiu Bo ret = -EIO; 635053ab70fSLiu Bo 6360b32f4bbSJosef Bacik if (!ret) 6370b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 63875391f0dSQu Wenruo else 63975391f0dSQu Wenruo btrfs_err(fs_info, 64075391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 64175391f0dSQu Wenruo eb->start); 64277bf40a2SQu Wenruo out: 64377bf40a2SQu Wenruo return ret; 64477bf40a2SQu Wenruo } 64577bf40a2SQu Wenruo 646371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 647371cdc07SQu Wenruo int mirror) 648371cdc07SQu Wenruo { 649371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 650371cdc07SQu Wenruo struct extent_buffer *eb; 651371cdc07SQu Wenruo bool reads_done; 652371cdc07SQu Wenruo int ret = 0; 653371cdc07SQu Wenruo 654371cdc07SQu Wenruo /* 655371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 656371cdc07SQu Wenruo * only get one eb for each endio hook. 657371cdc07SQu Wenruo */ 658371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 659371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 660371cdc07SQu Wenruo 661371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 662371cdc07SQu Wenruo /* 663371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 664371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 665371cdc07SQu Wenruo */ 666371cdc07SQu Wenruo ASSERT(eb); 667371cdc07SQu Wenruo 668371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 669371cdc07SQu Wenruo /* Subpage read must finish in page read */ 670371cdc07SQu Wenruo ASSERT(reads_done); 671371cdc07SQu Wenruo 672371cdc07SQu Wenruo eb->read_mirror = mirror; 673371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 674371cdc07SQu Wenruo ret = -EIO; 675371cdc07SQu Wenruo goto err; 676371cdc07SQu Wenruo } 677371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 678371cdc07SQu Wenruo if (ret < 0) 679371cdc07SQu Wenruo goto err; 680371cdc07SQu Wenruo 681371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 682371cdc07SQu Wenruo 683371cdc07SQu Wenruo free_extent_buffer(eb); 684371cdc07SQu Wenruo return ret; 685371cdc07SQu Wenruo err: 686371cdc07SQu Wenruo /* 687371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 688371cdc07SQu Wenruo * make sure it has something to decrement. 689371cdc07SQu Wenruo */ 690371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 691371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 692371cdc07SQu Wenruo free_extent_buffer(eb); 693371cdc07SQu Wenruo return ret; 694371cdc07SQu Wenruo } 695371cdc07SQu Wenruo 696c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 69777bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 69877bf40a2SQu Wenruo int mirror) 69977bf40a2SQu Wenruo { 70077bf40a2SQu Wenruo struct extent_buffer *eb; 70177bf40a2SQu Wenruo int ret = 0; 70277bf40a2SQu Wenruo int reads_done; 70377bf40a2SQu Wenruo 70477bf40a2SQu Wenruo ASSERT(page->private); 705371cdc07SQu Wenruo 706fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 707371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 708371cdc07SQu Wenruo 70977bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 71077bf40a2SQu Wenruo 71177bf40a2SQu Wenruo /* 71277bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 71377bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 71477bf40a2SQu Wenruo */ 71577bf40a2SQu Wenruo atomic_inc(&eb->refs); 71677bf40a2SQu Wenruo 71777bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 71877bf40a2SQu Wenruo if (!reads_done) 71977bf40a2SQu Wenruo goto err; 72077bf40a2SQu Wenruo 72177bf40a2SQu Wenruo eb->read_mirror = mirror; 72277bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 72377bf40a2SQu Wenruo ret = -EIO; 72477bf40a2SQu Wenruo goto err; 72577bf40a2SQu Wenruo } 72677bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 727ce9adaa5SChris Mason err: 72853b381b3SDavid Woodhouse if (ret) { 72953b381b3SDavid Woodhouse /* 73053b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 73153b381b3SDavid Woodhouse * again, we have to make sure it has something 73253b381b3SDavid Woodhouse * to decrement 73353b381b3SDavid Woodhouse */ 73453b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7350b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 73653b381b3SDavid Woodhouse } 7370b32f4bbSJosef Bacik free_extent_buffer(eb); 73877bf40a2SQu Wenruo 739f188591eSChris Mason return ret; 740ce9adaa5SChris Mason } 741ce9adaa5SChris Mason 7424246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 743ce9adaa5SChris Mason { 74497eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 745ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7469e0af237SLiu Bo struct btrfs_workqueue *wq; 747ce9adaa5SChris Mason 748ce9adaa5SChris Mason fs_info = end_io_wq->info; 7494e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 750d20f7043SChris Mason 751cfe94440SNaohiro Aota if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 752a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7539e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 754a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7559e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 756a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7579e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 758a0cac0ecSOmar Sandoval else 7599e0af237SLiu Bo wq = fs_info->endio_write_workers; 7609e0af237SLiu Bo } else { 7615c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7629e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 763a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7649e0af237SLiu Bo wq = fs_info->endio_meta_workers; 765a0cac0ecSOmar Sandoval else 7669e0af237SLiu Bo wq = fs_info->endio_workers; 7679e0af237SLiu Bo } 7689e0af237SLiu Bo 769a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7709e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 771ce9adaa5SChris Mason } 772ce9adaa5SChris Mason 7734e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 774bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7750b86a832SChris Mason { 77697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7778b110e39SMiao Xie 77897eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 779ce9adaa5SChris Mason if (!end_io_wq) 7804e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 781ce9adaa5SChris Mason 782ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 783ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 78422c59948SChris Mason end_io_wq->info = info; 7854e4cbee9SChristoph Hellwig end_io_wq->status = 0; 786ce9adaa5SChris Mason end_io_wq->bio = bio; 78722c59948SChris Mason end_io_wq->metadata = metadata; 788ce9adaa5SChris Mason 789ce9adaa5SChris Mason bio->bi_private = end_io_wq; 790ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 79122c59948SChris Mason return 0; 79222c59948SChris Mason } 79322c59948SChris Mason 7944a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7954a69a410SChris Mason { 7964a69a410SChris Mason struct async_submit_bio *async; 7974e4cbee9SChristoph Hellwig blk_status_t ret; 7984a69a410SChris Mason 7994a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8001941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 8011941b64bSQu Wenruo async->dio_file_offset); 80279787eaaSJeff Mahoney if (ret) 8034e4cbee9SChristoph Hellwig async->status = ret; 8044a69a410SChris Mason } 8054a69a410SChris Mason 80606ea01b1SDavid Sterba /* 80706ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 80806ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 80906ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 81006ea01b1SDavid Sterba * 81106ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 81206ea01b1SDavid Sterba * inserted into the tree. 81306ea01b1SDavid Sterba */ 8144a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8158b712842SChris Mason { 8168b712842SChris Mason struct async_submit_bio *async; 81706ea01b1SDavid Sterba struct inode *inode; 81806ea01b1SDavid Sterba blk_status_t ret; 8198b712842SChris Mason 8208b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8218896a08dSQu Wenruo inode = async->inode; 8224854ddd0SChris Mason 823bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 8244e4cbee9SChristoph Hellwig if (async->status) { 8254e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 8264246a0b6SChristoph Hellwig bio_endio(async->bio); 82779787eaaSJeff Mahoney return; 82879787eaaSJeff Mahoney } 82979787eaaSJeff Mahoney 830ec39f769SChris Mason /* 831ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 832ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 833ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 834ec39f769SChris Mason */ 835ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 83608635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 83706ea01b1SDavid Sterba if (ret) { 83806ea01b1SDavid Sterba async->bio->bi_status = ret; 83906ea01b1SDavid Sterba bio_endio(async->bio); 84006ea01b1SDavid Sterba } 8414a69a410SChris Mason } 8424a69a410SChris Mason 8434a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8444a69a410SChris Mason { 8454a69a410SChris Mason struct async_submit_bio *async; 8464a69a410SChris Mason 8474a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8488b712842SChris Mason kfree(async); 8498b712842SChris Mason } 8508b712842SChris Mason 8518896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 852a6f5e39eSDavid Sterba int mirror_num, u64 dio_file_offset, 853e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 85444b8bd7eSChris Mason { 8558896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 85644b8bd7eSChris Mason struct async_submit_bio *async; 85744b8bd7eSChris Mason 85844b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 85944b8bd7eSChris Mason if (!async) 8604e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 86144b8bd7eSChris Mason 8628896a08dSQu Wenruo async->inode = inode; 86344b8bd7eSChris Mason async->bio = bio; 86444b8bd7eSChris Mason async->mirror_num = mirror_num; 8654a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8664a69a410SChris Mason 867a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 868a0cac0ecSOmar Sandoval run_one_async_free); 8694a69a410SChris Mason 8701941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8718c8bee1dSChris Mason 8724e4cbee9SChristoph Hellwig async->status = 0; 87379787eaaSJeff Mahoney 87467f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 875a31b4a43SChristoph Hellwig btrfs_queue_work(fs_info->hipri_workers, &async->work); 876a31b4a43SChristoph Hellwig else 8775cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 87844b8bd7eSChris Mason return 0; 87944b8bd7eSChris Mason } 88044b8bd7eSChris Mason 8814e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 882ce3ed71aSChris Mason { 8832c30c71bSKent Overstreet struct bio_vec *bvec; 884ce3ed71aSChris Mason struct btrfs_root *root; 8852b070cfeSChristoph Hellwig int ret = 0; 8866dc4f100SMing Lei struct bvec_iter_all iter_all; 887ce3ed71aSChris Mason 888c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8892b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 890ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 891ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 89279787eaaSJeff Mahoney if (ret) 89379787eaaSJeff Mahoney break; 894ce3ed71aSChris Mason } 8952c30c71bSKent Overstreet 8964e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 897ce3ed71aSChris Mason } 898ce3ed71aSChris Mason 8998896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 9001941b64bSQu Wenruo u64 dio_file_offset) 90122c59948SChris Mason { 9028b712842SChris Mason /* 9038b712842SChris Mason * when we're called for a write, we're already in the async 9045443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9058b712842SChris Mason */ 90679787eaaSJeff Mahoney return btree_csum_one_bio(bio); 90722c59948SChris Mason } 90822c59948SChris Mason 909f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 9109b4e675aSDavid Sterba struct btrfs_inode *bi) 911de0022b9SJosef Bacik { 9124eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 913f4dcfb30SJohannes Thumshirn return false; 9146300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 915f4dcfb30SJohannes Thumshirn return false; 9169b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 917f4dcfb30SJohannes Thumshirn return false; 918f4dcfb30SJohannes Thumshirn return true; 919de0022b9SJosef Bacik } 920de0022b9SJosef Bacik 92194d9e11bSChristoph Hellwig void btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, int mirror_num) 92244b8bd7eSChris Mason { 9230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 9244e4cbee9SChristoph Hellwig blk_status_t ret; 925cad321adSChris Mason 926cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 927cad321adSChris Mason /* 928cad321adSChris Mason * called for a read, do the setup so that checksum validation 929cad321adSChris Mason * can happen in the async kernel threads 930cad321adSChris Mason */ 9310b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9320b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 93394d9e11bSChristoph Hellwig if (!ret) 93408635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 935f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 936de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 93794d9e11bSChristoph Hellwig if (!ret) 93808635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 93961891923SStefan Behrens } else { 940cad321adSChris Mason /* 94161891923SStefan Behrens * kthread helpers are used to submit writes so that 94261891923SStefan Behrens * checksumming can happen in parallel across all CPUs 943cad321adSChris Mason */ 9448896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 945a6f5e39eSDavid Sterba btree_submit_bio_start); 94644b8bd7eSChris Mason } 94744b8bd7eSChris Mason 94894d9e11bSChristoph Hellwig if (ret) { 9494e4cbee9SChristoph Hellwig bio->bi_status = ret; 9504246a0b6SChristoph Hellwig bio_endio(bio); 95194d9e11bSChristoph Hellwig } 95261891923SStefan Behrens } 95361891923SStefan Behrens 9543dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 9558958b551SMatthew Wilcox (Oracle) static int btree_migrate_folio(struct address_space *mapping, 9568958b551SMatthew Wilcox (Oracle) struct folio *dst, struct folio *src, enum migrate_mode mode) 957784b4e29SChris Mason { 958784b4e29SChris Mason /* 959784b4e29SChris Mason * we can't safely write a btree page from here, 960784b4e29SChris Mason * we haven't done the locking hook 961784b4e29SChris Mason */ 9628958b551SMatthew Wilcox (Oracle) if (folio_test_dirty(src)) 963784b4e29SChris Mason return -EAGAIN; 964784b4e29SChris Mason /* 965784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 966784b4e29SChris Mason * We must have no buffers or drop them. 967784b4e29SChris Mason */ 9688958b551SMatthew Wilcox (Oracle) if (folio_get_private(src) && 9698958b551SMatthew Wilcox (Oracle) !filemap_release_folio(src, GFP_KERNEL)) 970784b4e29SChris Mason return -EAGAIN; 971*54184650SMatthew Wilcox (Oracle) return migrate_folio(mapping, dst, src, mode); 972784b4e29SChris Mason } 9738958b551SMatthew Wilcox (Oracle) #else 9748958b551SMatthew Wilcox (Oracle) #define btree_migrate_folio NULL 9753dd1462eSJan Beulich #endif 976784b4e29SChris Mason 9770da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9780da5468fSChris Mason struct writeback_control *wbc) 9790da5468fSChris Mason { 980e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 981e2d84521SMiao Xie int ret; 982e2d84521SMiao Xie 983d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 984448d640bSChris Mason 985448d640bSChris Mason if (wbc->for_kupdate) 986448d640bSChris Mason return 0; 987448d640bSChris Mason 988e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 989b9473439SChris Mason /* this is a bit racy, but that's ok */ 990d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 991d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 992d814a491SEthan Lien fs_info->dirty_metadata_batch); 993e2d84521SMiao Xie if (ret < 0) 994793955bcSChris Mason return 0; 995793955bcSChris Mason } 9960b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9970da5468fSChris Mason } 9980da5468fSChris Mason 999f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags) 10005f39d397SChris Mason { 1001f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio)) 1002f913cff3SMatthew Wilcox (Oracle) return false; 10030c4e538bSDavid Sterba 1004f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page); 1005d98237b3SChris Mason } 1006d98237b3SChris Mason 1007895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 1008895586ebSMatthew Wilcox (Oracle) size_t length) 1009d98237b3SChris Mason { 1010d1310b2eSChris Mason struct extent_io_tree *tree; 1011895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 1012895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 1013f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS); 1014895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 1015895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 1016895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 1017895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 1018895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 10199ad6b7bcSChris Mason } 1020d98237b3SChris Mason } 1021d98237b3SChris Mason 1022bb146eb2SJosef Bacik #ifdef DEBUG 10230079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 10240079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 10250079c3b1SMatthew Wilcox (Oracle) { 10260079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 1027139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 10280b32f4bbSJosef Bacik struct extent_buffer *eb; 1029139e8cd3SQu Wenruo int cur_bit = 0; 10300079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 10310b32f4bbSJosef Bacik 1032139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 10330079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 10340b32f4bbSJosef Bacik BUG_ON(!eb); 10350b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10360b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 103749d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 10380079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 1039139e8cd3SQu Wenruo } 10400079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 1041139e8cd3SQu Wenruo 1042139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 1043139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 1044139e8cd3SQu Wenruo unsigned long flags; 1045139e8cd3SQu Wenruo u64 cur; 1046139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 1047139e8cd3SQu Wenruo 1048139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 1049139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 1050139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1051139e8cd3SQu Wenruo cur_bit++; 1052139e8cd3SQu Wenruo continue; 1053139e8cd3SQu Wenruo } 1054139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1055139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 1056139e8cd3SQu Wenruo 1057139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 1058139e8cd3SQu Wenruo ASSERT(eb); 1059139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 1060139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 106149d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 1062139e8cd3SQu Wenruo free_extent_buffer(eb); 1063139e8cd3SQu Wenruo 1064139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 1065139e8cd3SQu Wenruo } 10660079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 10670b32f4bbSJosef Bacik } 10680079c3b1SMatthew Wilcox (Oracle) #else 10690079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 10700079c3b1SMatthew Wilcox (Oracle) #endif 10710b32f4bbSJosef Bacik 10727f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10730da5468fSChris Mason .writepages = btree_writepages, 1074f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio, 1075895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 10768958b551SMatthew Wilcox (Oracle) .migrate_folio = btree_migrate_folio, 10770079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 1078d98237b3SChris Mason }; 1079123abc88SChris Mason 10802ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10812ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10823fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 10833fbaf258SJosef Bacik int level) 10840999df54SChris Mason { 10850b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10860b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10873fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10880999df54SChris Mason } 10890999df54SChris Mason 1090581c1760SQu Wenruo /* 1091581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1092581c1760SQu Wenruo * verification. 1093581c1760SQu Wenruo * 10941b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1095581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1096581c1760SQu Wenruo * @level: expected level, mandatory check 1097581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1098581c1760SQu Wenruo */ 10992ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 11001b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 11011b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1102e20d96d6SChris Mason { 11035f39d397SChris Mason struct extent_buffer *buf = NULL; 110419c00ddcSChris Mason int ret; 110519c00ddcSChris Mason 11063fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1107c871b0f2SLiu Bo if (IS_ERR(buf)) 1108c871b0f2SLiu Bo return buf; 1109e4204dedSChris Mason 11106a2e9dc4SFilipe Manana ret = btrfs_read_extent_buffer(buf, parent_transid, level, first_key); 11110f0fe8f7SFilipe David Borba Manana if (ret) { 1112537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 111364c043deSLiu Bo return ERR_PTR(ret); 11140f0fe8f7SFilipe David Borba Manana } 111588c602abSQu Wenruo if (btrfs_check_eb_owner(buf, owner_root)) { 111688c602abSQu Wenruo free_extent_buffer_stale(buf); 111788c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 111888c602abSQu Wenruo } 11195f39d397SChris Mason return buf; 1120ce9adaa5SChris Mason 1121eb60ceacSChris Mason } 1122eb60ceacSChris Mason 11236a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1124ed2ff2cbSChris Mason { 11256a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 112655c69072SChris Mason if (btrfs_header_generation(buf) == 1127e2d84521SMiao Xie fs_info->running_transaction->transid) { 112849d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1129b4ce94deSChris Mason 1130b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1131104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1132e2d84521SMiao Xie -buf->len, 1133e2d84521SMiao Xie fs_info->dirty_metadata_batch); 11340b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1135925baeddSChris Mason } 11365f39d397SChris Mason } 1137ed7b63ebSJosef Bacik } 11385f39d397SChris Mason 1139da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1140e20d96d6SChris Mason u64 objectid) 1141d97e63b6SChris Mason { 11427c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 11432e608bd1SJosef Bacik 11442e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 11452e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 11462e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 114796dfcb46SJosef Bacik root->fs_info = fs_info; 11482e608bd1SJosef Bacik root->root_key.objectid = objectid; 1149cfaa7295SChris Mason root->node = NULL; 1150a28ec197SChris Mason root->commit_root = NULL; 115127cdeb70SMiao Xie root->state = 0; 1152abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 11530b86a832SChris Mason 11540f7d52f4SChris Mason root->last_trans = 0; 11556b8fad57SNikolay Borisov root->free_objectid = 0; 1156eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1157199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11586bef4d31SEric Paris root->inode_tree = RB_ROOT; 1159253bf575SGabriel Niebler xa_init_flags(&root->delayed_nodes, GFP_ATOMIC); 11602e608bd1SJosef Bacik 11612e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 11620b86a832SChris Mason 11630b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11645d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1165eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1166eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1167199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1168199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1169d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11702ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11712ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11725d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1173eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1174199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1175f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11762ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11772ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11788287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1179a2135011SChris Mason mutex_init(&root->objectid_mutex); 1180e02119d5SChris Mason mutex_init(&root->log_mutex); 118131f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1182573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1183c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11847237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11857237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11867237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11878b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11888b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11897237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11907237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11917237f183SYan Zheng atomic_set(&root->log_writers, 0); 11922ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11930700cea7SElena Reshetova refcount_set(&root->refs, 1); 11948ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1195eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11967237f183SYan Zheng root->log_transid = 0; 1197d1433debSMiao Xie root->log_transid_committed = -1; 1198257c62e1SChris Mason root->last_log_commit = 0; 11992e608bd1SJosef Bacik root->anon_dev = 0; 1200e289f03eSFilipe Manana if (!dummy) { 120143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 120243eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1203e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1204e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1205e289f03eSFilipe Manana } 1206017e5369SChris Mason 12075f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1208370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1209bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1210bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 121148b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 1212bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 121348b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 1214bd647ce3SJosef Bacik #endif 12153768f368SChris Mason } 12163768f368SChris Mason 121774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 121896dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12196f07e42eSAl Viro { 122074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12216f07e42eSAl Viro if (root) 122296dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12236f07e42eSAl Viro return root; 12246f07e42eSAl Viro } 12256f07e42eSAl Viro 122606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 122706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1228da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 122906ea65a3SJosef Bacik { 123006ea65a3SJosef Bacik struct btrfs_root *root; 123106ea65a3SJosef Bacik 12327c0260eeSJeff Mahoney if (!fs_info) 12337c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12347c0260eeSJeff Mahoney 123596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 123606ea65a3SJosef Bacik if (!root) 123706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1238da17066cSJeff Mahoney 1239b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1240faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 124106ea65a3SJosef Bacik 124206ea65a3SJosef Bacik return root; 124306ea65a3SJosef Bacik } 124406ea65a3SJosef Bacik #endif 124506ea65a3SJosef Bacik 1246abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1247abed4aaaSJosef Bacik { 1248abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1249abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1250abed4aaaSJosef Bacik 1251abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1252abed4aaaSJosef Bacik } 1253abed4aaaSJosef Bacik 1254abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1255abed4aaaSJosef Bacik { 1256abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1257abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1258abed4aaaSJosef Bacik 1259abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1260abed4aaaSJosef Bacik } 1261abed4aaaSJosef Bacik 1262abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1263abed4aaaSJosef Bacik { 1264abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1265abed4aaaSJosef Bacik struct rb_node *tmp; 1266abed4aaaSJosef Bacik 1267abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1268abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1269abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1270abed4aaaSJosef Bacik ASSERT(!tmp); 1271abed4aaaSJosef Bacik 1272abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1273abed4aaaSJosef Bacik } 1274abed4aaaSJosef Bacik 1275abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1276abed4aaaSJosef Bacik { 1277abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1278abed4aaaSJosef Bacik 1279abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1280abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1281abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1282abed4aaaSJosef Bacik } 1283abed4aaaSJosef Bacik 1284abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1285abed4aaaSJosef Bacik struct btrfs_key *key) 1286abed4aaaSJosef Bacik { 1287abed4aaaSJosef Bacik struct rb_node *node; 1288abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1289abed4aaaSJosef Bacik 1290abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1291abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1292abed4aaaSJosef Bacik if (node) 1293abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1294abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1295abed4aaaSJosef Bacik 1296abed4aaaSJosef Bacik return root; 1297abed4aaaSJosef Bacik } 1298abed4aaaSJosef Bacik 1299f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1300f7238e50SJosef Bacik { 1301f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1302f7238e50SJosef Bacik u64 ret; 1303f7238e50SJosef Bacik 1304f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1305f7238e50SJosef Bacik return 0; 1306f7238e50SJosef Bacik 1307f7238e50SJosef Bacik if (bytenr) 1308f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1309f7238e50SJosef Bacik else 1310f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1311f7238e50SJosef Bacik ASSERT(block_group); 1312f7238e50SJosef Bacik if (!block_group) 1313f7238e50SJosef Bacik return 0; 1314f7238e50SJosef Bacik ret = block_group->global_root_id; 1315f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1316f7238e50SJosef Bacik 1317f7238e50SJosef Bacik return ret; 1318f7238e50SJosef Bacik } 1319f7238e50SJosef Bacik 1320abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1321abed4aaaSJosef Bacik { 1322abed4aaaSJosef Bacik struct btrfs_key key = { 1323abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1324abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1325f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1326abed4aaaSJosef Bacik }; 1327abed4aaaSJosef Bacik 1328abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1329abed4aaaSJosef Bacik } 1330abed4aaaSJosef Bacik 1331abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1332abed4aaaSJosef Bacik { 1333abed4aaaSJosef Bacik struct btrfs_key key = { 1334abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1335abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1336f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1337abed4aaaSJosef Bacik }; 1338abed4aaaSJosef Bacik 1339abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1340abed4aaaSJosef Bacik } 1341abed4aaaSJosef Bacik 134220897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 134320897f5cSArne Jansen u64 objectid) 134420897f5cSArne Jansen { 13459b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 134620897f5cSArne Jansen struct extent_buffer *leaf; 134720897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 134820897f5cSArne Jansen struct btrfs_root *root; 134920897f5cSArne Jansen struct btrfs_key key; 1350b89f6d1fSFilipe Manana unsigned int nofs_flag; 135120897f5cSArne Jansen int ret = 0; 135220897f5cSArne Jansen 1353b89f6d1fSFilipe Manana /* 1354b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1355b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1356b89f6d1fSFilipe Manana */ 1357b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 135896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1359b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 136020897f5cSArne Jansen if (!root) 136120897f5cSArne Jansen return ERR_PTR(-ENOMEM); 136220897f5cSArne Jansen 136320897f5cSArne Jansen root->root_key.objectid = objectid; 136420897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 136520897f5cSArne Jansen root->root_key.offset = 0; 136620897f5cSArne Jansen 13679631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 13689631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 136920897f5cSArne Jansen if (IS_ERR(leaf)) { 137020897f5cSArne Jansen ret = PTR_ERR(leaf); 13711dd05682STsutomu Itoh leaf = NULL; 13728a6a87cdSBoris Burkov goto fail_unlock; 137320897f5cSArne Jansen } 137420897f5cSArne Jansen 137520897f5cSArne Jansen root->node = leaf; 137620897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 137720897f5cSArne Jansen 137820897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 137927cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 138020897f5cSArne Jansen 1381f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1382f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 138320897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 138420897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 138520897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 138620897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 138720897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 138820897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 138920897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 139033d85fdaSQu Wenruo if (is_fstree(objectid)) 1391807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1392807fc790SAndy Shevchenko else 1393807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1394c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 139520897f5cSArne Jansen 13968a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13978a6a87cdSBoris Burkov 139820897f5cSArne Jansen key.objectid = objectid; 139920897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 140020897f5cSArne Jansen key.offset = 0; 140120897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 140220897f5cSArne Jansen if (ret) 140320897f5cSArne Jansen goto fail; 140420897f5cSArne Jansen 140520897f5cSArne Jansen return root; 14061dd05682STsutomu Itoh 14078a6a87cdSBoris Burkov fail_unlock: 14088c38938cSJosef Bacik if (leaf) 14091dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 14108a6a87cdSBoris Burkov fail: 141100246528SJosef Bacik btrfs_put_root(root); 14121dd05682STsutomu Itoh 14131dd05682STsutomu Itoh return ERR_PTR(ret); 141420897f5cSArne Jansen } 141520897f5cSArne Jansen 14167237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1417e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14180f7d52f4SChris Mason { 14190f7d52f4SChris Mason struct btrfs_root *root; 1420e02119d5SChris Mason 142196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1422e02119d5SChris Mason if (!root) 14237237f183SYan Zheng return ERR_PTR(-ENOMEM); 1424e02119d5SChris Mason 1425e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1426e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1427e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 142827cdeb70SMiao Xie 14296ab6ebb7SNaohiro Aota return root; 14306ab6ebb7SNaohiro Aota } 14316ab6ebb7SNaohiro Aota 14326ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 14336ab6ebb7SNaohiro Aota struct btrfs_root *root) 14346ab6ebb7SNaohiro Aota { 14356ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 14366ab6ebb7SNaohiro Aota 14377237f183SYan Zheng /* 143892a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 143927cdeb70SMiao Xie * 144092a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 144192a7cc42SQu Wenruo * and they go away before a real commit is actually done. 144292a7cc42SQu Wenruo * 144392a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 144492a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 14457237f183SYan Zheng */ 1446e02119d5SChris Mason 14474d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14489631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 14496ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 14506ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1451e02119d5SChris Mason 14527237f183SYan Zheng root->node = leaf; 1453e02119d5SChris Mason 1454e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1455e02119d5SChris Mason btrfs_tree_unlock(root->node); 14566ab6ebb7SNaohiro Aota 14576ab6ebb7SNaohiro Aota return 0; 14587237f183SYan Zheng } 14597237f183SYan Zheng 14607237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14617237f183SYan Zheng struct btrfs_fs_info *fs_info) 14627237f183SYan Zheng { 14637237f183SYan Zheng struct btrfs_root *log_root; 14647237f183SYan Zheng 14657237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14667237f183SYan Zheng if (IS_ERR(log_root)) 14677237f183SYan Zheng return PTR_ERR(log_root); 14686ab6ebb7SNaohiro Aota 14693ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 14703ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 14713ddebf27SNaohiro Aota 14726ab6ebb7SNaohiro Aota if (ret) { 14736ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14746ab6ebb7SNaohiro Aota return ret; 14756ab6ebb7SNaohiro Aota } 14763ddebf27SNaohiro Aota } 14776ab6ebb7SNaohiro Aota 14787237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14797237f183SYan Zheng fs_info->log_root_tree = log_root; 14807237f183SYan Zheng return 0; 14817237f183SYan Zheng } 14827237f183SYan Zheng 14837237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14847237f183SYan Zheng struct btrfs_root *root) 14857237f183SYan Zheng { 14860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14877237f183SYan Zheng struct btrfs_root *log_root; 14887237f183SYan Zheng struct btrfs_inode_item *inode_item; 14896ab6ebb7SNaohiro Aota int ret; 14907237f183SYan Zheng 14910b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14927237f183SYan Zheng if (IS_ERR(log_root)) 14937237f183SYan Zheng return PTR_ERR(log_root); 14947237f183SYan Zheng 14956ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14966ab6ebb7SNaohiro Aota if (ret) { 14976ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14986ab6ebb7SNaohiro Aota return ret; 14996ab6ebb7SNaohiro Aota } 15006ab6ebb7SNaohiro Aota 15017237f183SYan Zheng log_root->last_trans = trans->transid; 15027237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 15037237f183SYan Zheng 15047237f183SYan Zheng inode_item = &log_root->root_item.inode; 15053cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 15063cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15073cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1508da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15090b246afaSJeff Mahoney fs_info->nodesize); 15103cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15117237f183SYan Zheng 15125d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15137237f183SYan Zheng 15147237f183SYan Zheng WARN_ON(root->log_root); 15157237f183SYan Zheng root->log_root = log_root; 15167237f183SYan Zheng root->log_transid = 0; 1517d1433debSMiao Xie root->log_transid_committed = -1; 1518257c62e1SChris Mason root->last_log_commit = 0; 1519e02119d5SChris Mason return 0; 1520e02119d5SChris Mason } 1521e02119d5SChris Mason 152249d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 152349d11beaSJosef Bacik struct btrfs_path *path, 1524cb517eabSMiao Xie struct btrfs_key *key) 1525e02119d5SChris Mason { 1526e02119d5SChris Mason struct btrfs_root *root; 1527e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 152884234f3aSYan Zheng u64 generation; 1529cb517eabSMiao Xie int ret; 1530581c1760SQu Wenruo int level; 1531cb517eabSMiao Xie 153296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 153349d11beaSJosef Bacik if (!root) 153449d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 15350f7d52f4SChris Mason 1536cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1537cb517eabSMiao Xie &root->root_item, &root->root_key); 15380f7d52f4SChris Mason if (ret) { 153913a8a7c8SYan, Zheng if (ret > 0) 154013a8a7c8SYan, Zheng ret = -ENOENT; 154149d11beaSJosef Bacik goto fail; 15420f7d52f4SChris Mason } 154313a8a7c8SYan, Zheng 154484234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1545581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 15462ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15472ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 15481b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 154964c043deSLiu Bo if (IS_ERR(root->node)) { 155064c043deSLiu Bo ret = PTR_ERR(root->node); 15518c38938cSJosef Bacik root->node = NULL; 155249d11beaSJosef Bacik goto fail; 15534eb150d6SQu Wenruo } 15544eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1555cb517eabSMiao Xie ret = -EIO; 155649d11beaSJosef Bacik goto fail; 1557416bc658SJosef Bacik } 155888c602abSQu Wenruo 155988c602abSQu Wenruo /* 156088c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 156188c602abSQu Wenruo * match its root node owner 156288c602abSQu Wenruo */ 156388c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 156488c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 156588c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 156688c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 156788c602abSQu Wenruo btrfs_crit(fs_info, 156888c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 156988c602abSQu Wenruo root->root_key.objectid, root->node->start, 157088c602abSQu Wenruo btrfs_header_owner(root->node), 157188c602abSQu Wenruo root->root_key.objectid); 157288c602abSQu Wenruo ret = -EUCLEAN; 157388c602abSQu Wenruo goto fail; 157488c602abSQu Wenruo } 15755d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1576cb517eabSMiao Xie return root; 157749d11beaSJosef Bacik fail: 157800246528SJosef Bacik btrfs_put_root(root); 157949d11beaSJosef Bacik return ERR_PTR(ret); 158049d11beaSJosef Bacik } 158149d11beaSJosef Bacik 158249d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 158349d11beaSJosef Bacik struct btrfs_key *key) 158449d11beaSJosef Bacik { 158549d11beaSJosef Bacik struct btrfs_root *root; 158649d11beaSJosef Bacik struct btrfs_path *path; 158749d11beaSJosef Bacik 158849d11beaSJosef Bacik path = btrfs_alloc_path(); 158949d11beaSJosef Bacik if (!path) 159049d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 159149d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 159249d11beaSJosef Bacik btrfs_free_path(path); 159349d11beaSJosef Bacik 159449d11beaSJosef Bacik return root; 1595cb517eabSMiao Xie } 1596cb517eabSMiao Xie 15972dfb1e43SQu Wenruo /* 15982dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15992dfb1e43SQu Wenruo * 16002dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 16012dfb1e43SQu Wenruo */ 16022dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1603cb517eabSMiao Xie { 1604cb517eabSMiao Xie int ret; 1605dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1606cb517eabSMiao Xie 1607dcc3eb96SNikolay Borisov /* 1608dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1609dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1610dcc3eb96SNikolay Borisov */ 1611dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1612dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1613dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1614dcc3eb96SNikolay Borisov if (ret) 16158257b2dcSMiao Xie goto fail; 16168257b2dcSMiao Xie 1617aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 161837f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 161992a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1620f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1621f39e4571SJosef Bacik } 1622f39e4571SJosef Bacik 1623851fd730SQu Wenruo /* 1624851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1625851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1626851fd730SQu Wenruo */ 1627851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1628851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 16292dfb1e43SQu Wenruo if (!anon_dev) { 1630cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1631cb517eabSMiao Xie if (ret) 1632876d2cf1SLiu Bo goto fail; 16332dfb1e43SQu Wenruo } else { 16342dfb1e43SQu Wenruo root->anon_dev = anon_dev; 16352dfb1e43SQu Wenruo } 1636851fd730SQu Wenruo } 1637f32e48e9SChandan Rajendra 1638f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1639453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1640f32e48e9SChandan Rajendra if (ret) { 1641f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1642876d2cf1SLiu Bo goto fail; 1643f32e48e9SChandan Rajendra } 1644f32e48e9SChandan Rajendra 16456b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1646f32e48e9SChandan Rajendra 1647f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1648f32e48e9SChandan Rajendra 1649cb517eabSMiao Xie return 0; 1650cb517eabSMiao Xie fail: 165184db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1652cb517eabSMiao Xie return ret; 1653cb517eabSMiao Xie } 1654cb517eabSMiao Xie 1655a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1656cb517eabSMiao Xie u64 root_id) 1657cb517eabSMiao Xie { 1658cb517eabSMiao Xie struct btrfs_root *root; 1659cb517eabSMiao Xie 166048b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 166148b36a60SGabriel Niebler root = xa_load(&fs_info->fs_roots, (unsigned long)root_id); 1662bc44d7c4SJosef Bacik if (root) 166300246528SJosef Bacik root = btrfs_grab_root(root); 166448b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 1665cb517eabSMiao Xie return root; 1666cb517eabSMiao Xie } 1667cb517eabSMiao Xie 166849d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 166949d11beaSJosef Bacik u64 objectid) 167049d11beaSJosef Bacik { 1671abed4aaaSJosef Bacik struct btrfs_key key = { 1672abed4aaaSJosef Bacik .objectid = objectid, 1673abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1674abed4aaaSJosef Bacik .offset = 0, 1675abed4aaaSJosef Bacik }; 1676abed4aaaSJosef Bacik 167749d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 167849d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 167949d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1680abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 168149d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 168249d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 168349d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 168449d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 168549d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1686abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 168749d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 168849d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 168949d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 169049d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 169149d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 169249d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 1693abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1694abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1695abed4aaaSJosef Bacik 1696abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1697abed4aaaSJosef Bacik } 169849d11beaSJosef Bacik return NULL; 169949d11beaSJosef Bacik } 170049d11beaSJosef Bacik 1701cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1702cb517eabSMiao Xie struct btrfs_root *root) 1703cb517eabSMiao Xie { 1704cb517eabSMiao Xie int ret; 1705cb517eabSMiao Xie 170648b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 170748b36a60SGabriel Niebler ret = xa_insert(&fs_info->fs_roots, (unsigned long)root->root_key.objectid, 170848b36a60SGabriel Niebler root, GFP_NOFS); 1709af01d2e5SJosef Bacik if (ret == 0) { 171000246528SJosef Bacik btrfs_grab_root(root); 171148b36a60SGabriel Niebler set_bit(BTRFS_ROOT_REGISTERED, &root->state); 1712af01d2e5SJosef Bacik } 171348b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 1714cb517eabSMiao Xie 1715cb517eabSMiao Xie return ret; 1716cb517eabSMiao Xie } 1717cb517eabSMiao Xie 1718bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1719bd647ce3SJosef Bacik { 1720bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1721bd647ce3SJosef Bacik struct btrfs_root *root; 1722bd647ce3SJosef Bacik 1723bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1724457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1725457f1864SJosef Bacik 1726bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1727bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1728457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 172971008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1730bd647ce3SJosef Bacik refcount_read(&root->refs)); 1731bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 173200246528SJosef Bacik btrfs_put_root(root); 173300246528SJosef Bacik btrfs_put_root(root); 1734bd647ce3SJosef Bacik } 1735bd647ce3SJosef Bacik #endif 1736bd647ce3SJosef Bacik } 1737bd647ce3SJosef Bacik 1738abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1739abed4aaaSJosef Bacik { 1740abed4aaaSJosef Bacik struct btrfs_root *root; 1741abed4aaaSJosef Bacik struct rb_node *node; 1742abed4aaaSJosef Bacik 1743abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1744abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1745abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1746abed4aaaSJosef Bacik btrfs_put_root(root); 1747abed4aaaSJosef Bacik } 1748abed4aaaSJosef Bacik } 1749abed4aaaSJosef Bacik 17500d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 17510d4b0463SJosef Bacik { 1752141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1753141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 17545deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1755141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1756141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1757141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1758141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 17590d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 17600d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1761abed4aaaSJosef Bacik free_global_roots(fs_info); 176200246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 176300246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 176400246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 176500246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 176600246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 176700246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1768aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 17699c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1770bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 17713fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 17720d4b0463SJosef Bacik kfree(fs_info->super_copy); 17730d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 17748481dd80SQu Wenruo kfree(fs_info->subpage_info); 17750d4b0463SJosef Bacik kvfree(fs_info); 17760d4b0463SJosef Bacik } 17770d4b0463SJosef Bacik 17780d4b0463SJosef Bacik 17792dfb1e43SQu Wenruo /* 17802dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 17812dfb1e43SQu Wenruo * 17822dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 17832dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 17842dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 17852dfb1e43SQu Wenruo * 17862dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 17872dfb1e43SQu Wenruo * 17882dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 17892dfb1e43SQu Wenruo * same root objectid. 17902dfb1e43SQu Wenruo * 17912dfb1e43SQu Wenruo * @objectid: root id 17922dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 17932dfb1e43SQu Wenruo * pass 0 for new allocation. 17942dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 17952dfb1e43SQu Wenruo * for orphan roots 17962dfb1e43SQu Wenruo */ 17972dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 17982dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 17992dfb1e43SQu Wenruo bool check_ref) 18005eda7b5eSChris Mason { 18015eda7b5eSChris Mason struct btrfs_root *root; 1802381cf658SDavid Sterba struct btrfs_path *path; 18031d4c08e0SDavid Sterba struct btrfs_key key; 18045eda7b5eSChris Mason int ret; 18055eda7b5eSChris Mason 180649d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 180749d11beaSJosef Bacik if (root) 180849d11beaSJosef Bacik return root; 18094df27c4dSYan, Zheng again: 181056e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 181148475471SStefan Behrens if (root) { 18122dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 18132dfb1e43SQu Wenruo ASSERT(!anon_dev); 1814bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 181500246528SJosef Bacik btrfs_put_root(root); 181648475471SStefan Behrens return ERR_PTR(-ENOENT); 1817bc44d7c4SJosef Bacik } 18185eda7b5eSChris Mason return root; 181948475471SStefan Behrens } 18205eda7b5eSChris Mason 182156e9357aSDavid Sterba key.objectid = objectid; 182256e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 182356e9357aSDavid Sterba key.offset = (u64)-1; 182456e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 18255eda7b5eSChris Mason if (IS_ERR(root)) 18265eda7b5eSChris Mason return root; 18273394e160SChris Mason 1828c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1829d68fc57bSYan, Zheng ret = -ENOENT; 1830d68fc57bSYan, Zheng goto fail; 1831d68fc57bSYan, Zheng } 1832d68fc57bSYan, Zheng 18332dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1834cb517eabSMiao Xie if (ret) 1835cb517eabSMiao Xie goto fail; 1836cb517eabSMiao Xie 1837381cf658SDavid Sterba path = btrfs_alloc_path(); 1838381cf658SDavid Sterba if (!path) { 1839381cf658SDavid Sterba ret = -ENOMEM; 1840381cf658SDavid Sterba goto fail; 1841381cf658SDavid Sterba } 18421d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 18431d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 184456e9357aSDavid Sterba key.offset = objectid; 18451d4c08e0SDavid Sterba 18461d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1847381cf658SDavid Sterba btrfs_free_path(path); 1848d68fc57bSYan, Zheng if (ret < 0) 1849d68fc57bSYan, Zheng goto fail; 1850d68fc57bSYan, Zheng if (ret == 0) 185127cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1852d68fc57bSYan, Zheng 1853cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 18540f7d52f4SChris Mason if (ret) { 1855168a2f77SJia-Ju Bai if (ret == -EEXIST) { 185600246528SJosef Bacik btrfs_put_root(root); 18574df27c4dSYan, Zheng goto again; 1858168a2f77SJia-Ju Bai } 18594df27c4dSYan, Zheng goto fail; 18604df27c4dSYan, Zheng } 1861edbd8d4eSChris Mason return root; 18624df27c4dSYan, Zheng fail: 186333fab972SFilipe Manana /* 186433fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 186533fab972SFilipe Manana * responsability to free it in case we fail. So we have to set our 186633fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 186733fab972SFilipe Manana * and once again by our caller. 186833fab972SFilipe Manana */ 186933fab972SFilipe Manana if (anon_dev) 187033fab972SFilipe Manana root->anon_dev = 0; 18718c38938cSJosef Bacik btrfs_put_root(root); 18724df27c4dSYan, Zheng return ERR_PTR(ret); 1873edbd8d4eSChris Mason } 1874edbd8d4eSChris Mason 18752dfb1e43SQu Wenruo /* 18762dfb1e43SQu Wenruo * Get in-memory reference of a root structure 18772dfb1e43SQu Wenruo * 18782dfb1e43SQu Wenruo * @objectid: tree objectid 18792dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 18802dfb1e43SQu Wenruo * one reference 18812dfb1e43SQu Wenruo */ 18822dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 18832dfb1e43SQu Wenruo u64 objectid, bool check_ref) 18842dfb1e43SQu Wenruo { 18852dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 18862dfb1e43SQu Wenruo } 18872dfb1e43SQu Wenruo 18882dfb1e43SQu Wenruo /* 18892dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 18902dfb1e43SQu Wenruo * the anonymous block device id 18912dfb1e43SQu Wenruo * 18922dfb1e43SQu Wenruo * @objectid: tree objectid 18932dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 18942dfb1e43SQu Wenruo * parameter value 18952dfb1e43SQu Wenruo */ 18962dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 18972dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 18982dfb1e43SQu Wenruo { 18992dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 19002dfb1e43SQu Wenruo } 19012dfb1e43SQu Wenruo 19028b712842SChris Mason /* 190349d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 190449d11beaSJosef Bacik * @fs_info: the fs_info 190549d11beaSJosef Bacik * @objectid: the objectid we need to lookup 190649d11beaSJosef Bacik * 190749d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 190849d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 190949d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 191049d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 191149d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 191249d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 191349d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 191449d11beaSJosef Bacik * backref code is finished using the root. 191549d11beaSJosef Bacik */ 191649d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 191749d11beaSJosef Bacik struct btrfs_path *path, 191849d11beaSJosef Bacik u64 objectid) 191949d11beaSJosef Bacik { 192049d11beaSJosef Bacik struct btrfs_root *root; 192149d11beaSJosef Bacik struct btrfs_key key; 192249d11beaSJosef Bacik 192349d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 192449d11beaSJosef Bacik 192549d11beaSJosef Bacik /* 192649d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 192749d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 192849d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 192949d11beaSJosef Bacik * != NULL just return it. 193049d11beaSJosef Bacik */ 193149d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 193249d11beaSJosef Bacik if (root) 193349d11beaSJosef Bacik return root; 193449d11beaSJosef Bacik 193549d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 193649d11beaSJosef Bacik if (root) 193749d11beaSJosef Bacik return root; 193849d11beaSJosef Bacik 193949d11beaSJosef Bacik key.objectid = objectid; 194049d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 194149d11beaSJosef Bacik key.offset = (u64)-1; 194249d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 194349d11beaSJosef Bacik btrfs_release_path(path); 194449d11beaSJosef Bacik 194549d11beaSJosef Bacik return root; 194649d11beaSJosef Bacik } 194749d11beaSJosef Bacik 194849d11beaSJosef Bacik /* 19498b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 19508b712842SChris Mason * functions. This is where read checksum verification actually happens 19518b712842SChris Mason */ 19528b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1953ce9adaa5SChris Mason { 1954ce9adaa5SChris Mason struct bio *bio; 195597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1956ce9adaa5SChris Mason 195797eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1958ce9adaa5SChris Mason bio = end_io_wq->bio; 19598b712842SChris Mason 19604e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1961ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1962ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 19634246a0b6SChristoph Hellwig bio_endio(bio); 19649be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1965ce9adaa5SChris Mason } 196644b8bd7eSChris Mason 1967a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1968a74a4b97SChris Mason { 19690d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1970d0278245SMiao Xie int again; 1971a74a4b97SChris Mason 1972d6fd0ae2SOmar Sandoval while (1) { 1973d0278245SMiao Xie again = 0; 19749d1a2a3aSDavid Sterba 1975fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1976fd340d0fSJosef Bacik 1977d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 19782ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1979d0278245SMiao Xie goto sleep; 1980d0278245SMiao Xie 198190c711abSZygo Blaxell /* 198290c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 198390c711abSZygo Blaxell * before we have finished mounting the filesystem. 198490c711abSZygo Blaxell */ 19850b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 198690c711abSZygo Blaxell goto sleep; 198790c711abSZygo Blaxell 19880b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1989d0278245SMiao Xie goto sleep; 1990d0278245SMiao Xie 1991dc7f370cSMiao Xie /* 1992dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1993dc7f370cSMiao Xie * during the above check and trylock. 1994dc7f370cSMiao Xie */ 19952ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 19960b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1997dc7f370cSMiao Xie goto sleep; 1998dc7f370cSMiao Xie } 1999dc7f370cSMiao Xie 20002ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 2001c2d6cb16SFilipe Manana 200233c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 20030b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 2004a74a4b97SChris Mason 2005d0278245SMiao Xie /* 200605323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 200705323cd1SMiao Xie * needn't do anything special here. 2008d0278245SMiao Xie */ 20090b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 201067c5e7d4SFilipe Manana 201167c5e7d4SFilipe Manana /* 2012f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 201367c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 201467c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 2015f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 201667c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 201767c5e7d4SFilipe Manana * unused block groups. 201867c5e7d4SFilipe Manana */ 20190b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 202018bb8bbfSJohannes Thumshirn 202118bb8bbfSJohannes Thumshirn /* 202218bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 202318bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 202418bb8bbfSJohannes Thumshirn * space. 202518bb8bbfSJohannes Thumshirn */ 202618bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 2027d0278245SMiao Xie sleep: 2028a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 2029d6fd0ae2SOmar Sandoval if (kthread_should_park()) 2030d6fd0ae2SOmar Sandoval kthread_parkme(); 2031d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 2032d6fd0ae2SOmar Sandoval return 0; 2033838fe188SJiri Kosina if (!again) { 2034a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 2035a74a4b97SChris Mason schedule(); 2036a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 2037a74a4b97SChris Mason } 2038da288d28SFilipe Manana } 2039a74a4b97SChris Mason } 2040a74a4b97SChris Mason 2041a74a4b97SChris Mason static int transaction_kthread(void *arg) 2042a74a4b97SChris Mason { 2043a74a4b97SChris Mason struct btrfs_root *root = arg; 20440b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2045a74a4b97SChris Mason struct btrfs_trans_handle *trans; 2046a74a4b97SChris Mason struct btrfs_transaction *cur; 20478929ecfaSYan, Zheng u64 transid; 2048643900beSNikolay Borisov time64_t delta; 2049a74a4b97SChris Mason unsigned long delay; 2050914b2007SJan Kara bool cannot_commit; 2051a74a4b97SChris Mason 2052a74a4b97SChris Mason do { 2053914b2007SJan Kara cannot_commit = false; 2054ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 20550b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 2056a74a4b97SChris Mason 20570b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20580b246afaSJeff Mahoney cur = fs_info->running_transaction; 2059a74a4b97SChris Mason if (!cur) { 20600b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2061a74a4b97SChris Mason goto sleep; 2062a74a4b97SChris Mason } 206331153d81SYan Zheng 2064643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 2065fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 2066fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 2067643900beSNikolay Borisov delta < fs_info->commit_interval) { 20680b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2069fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 2070fb8a7e94SNikolay Borisov delay = min(delay, 2071fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 2072a74a4b97SChris Mason goto sleep; 2073a74a4b97SChris Mason } 20748929ecfaSYan, Zheng transid = cur->transid; 20750b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 207656bec294SChris Mason 207779787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 2078354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 2079914b2007SJan Kara if (IS_ERR(trans)) { 2080354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 2081914b2007SJan Kara cannot_commit = true; 208279787eaaSJeff Mahoney goto sleep; 2083914b2007SJan Kara } 20848929ecfaSYan, Zheng if (transid == trans->transid) { 20853a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 20868929ecfaSYan, Zheng } else { 20873a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20888929ecfaSYan, Zheng } 2089a74a4b97SChris Mason sleep: 20900b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 20910b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 2092a74a4b97SChris Mason 209384961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 20942ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 20958929ecfaSYan, Zheng if (!kthread_should_stop() && 20960b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 2097914b2007SJan Kara cannot_commit)) 2098bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 2099a74a4b97SChris Mason } while (!kthread_should_stop()); 2100a74a4b97SChris Mason return 0; 2101a74a4b97SChris Mason } 2102a74a4b97SChris Mason 2103af31f5e5SChris Mason /* 210401f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 210501f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 210601f0f9daSNikolay Borisov * anything. 2107af31f5e5SChris Mason * 2108af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2109af31f5e5SChris Mason * generation of the latest root in the array with the generation 2110af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2111af31f5e5SChris Mason */ 211201f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2113af31f5e5SChris Mason { 211401f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2115af31f5e5SChris Mason u64 cur; 2116af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2117af31f5e5SChris Mason int i; 2118af31f5e5SChris Mason 2119af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2120af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2121af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2122af31f5e5SChris Mason if (cur == newest_gen) 212301f0f9daSNikolay Borisov return i; 2124af31f5e5SChris Mason } 2125af31f5e5SChris Mason 212601f0f9daSNikolay Borisov return -EINVAL; 2127af31f5e5SChris Mason } 2128af31f5e5SChris Mason 2129af31f5e5SChris Mason /* 2130af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2131af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2132af31f5e5SChris Mason * done 2133af31f5e5SChris Mason */ 2134af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2135af31f5e5SChris Mason { 21366ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2137af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2138af31f5e5SChris Mason 2139af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2140af31f5e5SChris Mason 2141af31f5e5SChris Mason /* 2142af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2143af31f5e5SChris Mason * regardless of which ones we use today 2144af31f5e5SChris Mason */ 2145af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2146af31f5e5SChris Mason 2147af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2148af31f5e5SChris Mason 2149af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2150af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2151af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2152af31f5e5SChris Mason 2153af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2154af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2155af31f5e5SChris Mason 2156af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2157af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2158af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2159af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2160af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2161af31f5e5SChris Mason 21629c54e80dSJosef Bacik if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) { 21639c54e80dSJosef Bacik btrfs_set_backup_block_group_root(root_backup, 21649c54e80dSJosef Bacik info->block_group_root->node->start); 21659c54e80dSJosef Bacik btrfs_set_backup_block_group_root_gen(root_backup, 21669c54e80dSJosef Bacik btrfs_header_generation(info->block_group_root->node)); 21679c54e80dSJosef Bacik btrfs_set_backup_block_group_root_level(root_backup, 21689c54e80dSJosef Bacik btrfs_header_level(info->block_group_root->node)); 21699c54e80dSJosef Bacik } else { 21709c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2171f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 21729c54e80dSJosef Bacik 21739c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 21749c54e80dSJosef Bacik extent_root->node->start); 2175af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 217629cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2177af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 217829cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2179af31f5e5SChris Mason 2180f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2181f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2182f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2183f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2184f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 21859c54e80dSJosef Bacik } 2186af31f5e5SChris Mason 21877c7e82a7SChris Mason /* 21887c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 21897c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 21907c7e82a7SChris Mason */ 21917c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 21927c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 21937c7e82a7SChris Mason info->fs_root->node->start); 2194af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2195af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2196af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2197af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21987c7e82a7SChris Mason } 2199af31f5e5SChris Mason 2200af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2201af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2202af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2203af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2204af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2205af31f5e5SChris Mason 2206af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2207af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2208af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2209af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2210af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2211af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2212af31f5e5SChris Mason 2213af31f5e5SChris Mason /* 2214af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2215af31f5e5SChris Mason * for the next commit 2216af31f5e5SChris Mason */ 2217af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2218af31f5e5SChris Mason &info->super_for_commit->super_roots, 2219af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2220af31f5e5SChris Mason } 2221af31f5e5SChris Mason 2222af31f5e5SChris Mason /* 2223bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2224bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2225bd2336b2SNikolay Borisov * 2226bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2227bd2336b2SNikolay Borisov * priority - priority of backup root required 2228bd2336b2SNikolay Borisov * 2229bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2230bd2336b2SNikolay Borisov */ 2231bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2232bd2336b2SNikolay Borisov { 2233bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2234bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2235bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2236bd2336b2SNikolay Borisov 2237bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2238bd2336b2SNikolay Borisov if (priority == 0) 2239bd2336b2SNikolay Borisov return backup_index; 2240bd2336b2SNikolay Borisov 2241bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2242bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2243bd2336b2SNikolay Borisov } else { 2244bd2336b2SNikolay Borisov return -EINVAL; 2245bd2336b2SNikolay Borisov } 2246bd2336b2SNikolay Borisov 2247bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2248bd2336b2SNikolay Borisov 2249bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2250bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2251bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2252bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2253bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2254bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2255bd2336b2SNikolay Borisov 2256bd2336b2SNikolay Borisov /* 2257bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2258bd2336b2SNikolay Borisov * need a fsck 2259bd2336b2SNikolay Borisov */ 2260bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2261bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2262bd2336b2SNikolay Borisov 2263bd2336b2SNikolay Borisov return backup_index; 2264bd2336b2SNikolay Borisov } 2265bd2336b2SNikolay Borisov 22667abadb64SLiu Bo /* helper to cleanup workers */ 22677abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 22687abadb64SLiu Bo { 2269dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2270afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 2271a31b4a43SChristoph Hellwig btrfs_destroy_workqueue(fs_info->hipri_workers); 22725cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2273fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2274fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2275385de0efSChristoph Hellwig if (fs_info->rmw_workers) 2276385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers); 2277fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2278fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 22795b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2280e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2281a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2282fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2283b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2284b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2285a9b9477dSFilipe Manana /* 2286a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2287a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2288a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2289a9b9477dSFilipe Manana */ 2290a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2291a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 22927abadb64SLiu Bo } 22937abadb64SLiu Bo 22942e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22952e9f5954SRashika { 22962e9f5954SRashika if (root) { 22972e9f5954SRashika free_extent_buffer(root->node); 22982e9f5954SRashika free_extent_buffer(root->commit_root); 22992e9f5954SRashika root->node = NULL; 23002e9f5954SRashika root->commit_root = NULL; 23012e9f5954SRashika } 23022e9f5954SRashika } 23032e9f5954SRashika 2304abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2305abed4aaaSJosef Bacik { 2306abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2307abed4aaaSJosef Bacik 2308abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2309abed4aaaSJosef Bacik &fs_info->global_root_tree, 2310abed4aaaSJosef Bacik rb_node) 2311abed4aaaSJosef Bacik free_root_extent_buffers(root); 2312abed4aaaSJosef Bacik } 2313abed4aaaSJosef Bacik 2314af31f5e5SChris Mason /* helper to cleanup tree roots */ 23154273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2316af31f5e5SChris Mason { 23172e9f5954SRashika free_root_extent_buffers(info->tree_root); 2318655b09feSJosef Bacik 2319abed4aaaSJosef Bacik free_global_root_pointers(info); 23202e9f5954SRashika free_root_extent_buffers(info->dev_root); 23212e9f5954SRashika free_root_extent_buffers(info->quota_root); 23222e9f5954SRashika free_root_extent_buffers(info->uuid_root); 23238c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2324aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 23259c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 23264273eaffSAnand Jain if (free_chunk_root) 23272e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2328af31f5e5SChris Mason } 2329af31f5e5SChris Mason 23308c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 23318c38938cSJosef Bacik { 23328c38938cSJosef Bacik if (!root) 23338c38938cSJosef Bacik return; 23348c38938cSJosef Bacik 23358c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 23368c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 23371dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 23388c38938cSJosef Bacik if (root->anon_dev) 23398c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 23408c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2341923eb523SJohannes Thumshirn free_root_extent_buffers(root); 23428c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 234348b36a60SGabriel Niebler spin_lock(&root->fs_info->fs_roots_lock); 23448c38938cSJosef Bacik list_del_init(&root->leak_list); 234548b36a60SGabriel Niebler spin_unlock(&root->fs_info->fs_roots_lock); 23468c38938cSJosef Bacik #endif 23478c38938cSJosef Bacik kfree(root); 23488c38938cSJosef Bacik } 23498c38938cSJosef Bacik } 23508c38938cSJosef Bacik 2351faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2352171f6537SJosef Bacik { 235348b36a60SGabriel Niebler struct btrfs_root *root; 235448b36a60SGabriel Niebler unsigned long index = 0; 2355171f6537SJosef Bacik 2356171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 235748b36a60SGabriel Niebler root = list_entry(fs_info->dead_roots.next, 2358171f6537SJosef Bacik struct btrfs_root, root_list); 235948b36a60SGabriel Niebler list_del(&root->root_list); 2360171f6537SJosef Bacik 236148b36a60SGabriel Niebler if (test_bit(BTRFS_ROOT_REGISTERED, &root->state)) 236248b36a60SGabriel Niebler btrfs_drop_and_free_fs_root(fs_info, root); 236348b36a60SGabriel Niebler btrfs_put_root(root); 2364171f6537SJosef Bacik } 2365171f6537SJosef Bacik 236648b36a60SGabriel Niebler xa_for_each(&fs_info->fs_roots, index, root) { 236748b36a60SGabriel Niebler btrfs_drop_and_free_fs_root(fs_info, root); 2368171f6537SJosef Bacik } 2369171f6537SJosef Bacik } 2370af31f5e5SChris Mason 2371638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2372638aa7edSEric Sandeen { 2373638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2374638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2375638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2376638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2377638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2378638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2379ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2380638aa7edSEric Sandeen } 2381638aa7edSEric Sandeen 2382779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2383779a65a4SEric Sandeen { 2384779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2385779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2386779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2387779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2388779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2389779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2390907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2391779a65a4SEric Sandeen } 2392779a65a4SEric Sandeen 23936bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2394f37938e0SEric Sandeen { 23952ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23962ff7e61eSJeff Mahoney 23972ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23982ff7e61eSJeff Mahoney set_nlink(inode, 1); 2399f37938e0SEric Sandeen /* 2400f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2401f37938e0SEric Sandeen * the real end of the address space is determined by all of 2402f37938e0SEric Sandeen * the devices in the system 2403f37938e0SEric Sandeen */ 24042ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 24052ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2406f37938e0SEric Sandeen 24072ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 240843eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 24092c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 24107b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 24112ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2412f37938e0SEric Sandeen 24135c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 24142ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 24152ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 24162ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2417f37938e0SEric Sandeen } 2418f37938e0SEric Sandeen 2419ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2420ad618368SEric Sandeen { 2421ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2422129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 24237f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2424ad618368SEric Sandeen } 2425ad618368SEric Sandeen 2426f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2427f9e92e40SEric Sandeen { 2428f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2429f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2430f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2431f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2432f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2433f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2434d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2435f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2436f9e92e40SEric Sandeen } 2437f9e92e40SEric Sandeen 2438d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 24392a458198SEric Sandeen { 2440f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 24416f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 24422a458198SEric Sandeen 24432a458198SEric Sandeen fs_info->workers = 2444a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16); 2445a31b4a43SChristoph Hellwig fs_info->hipri_workers = 2446a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker-high", 2447cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 24482a458198SEric Sandeen 24492a458198SEric Sandeen fs_info->delalloc_workers = 2450cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2451cb001095SJeff Mahoney flags, max_active, 2); 24522a458198SEric Sandeen 24532a458198SEric Sandeen fs_info->flush_workers = 2454cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2455cb001095SJeff Mahoney flags, max_active, 0); 24562a458198SEric Sandeen 24572a458198SEric Sandeen fs_info->caching_workers = 2458cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 24592a458198SEric Sandeen 24602a458198SEric Sandeen fs_info->fixup_workers = 2461cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 24622a458198SEric Sandeen 24632a458198SEric Sandeen /* 24642a458198SEric Sandeen * endios are largely parallel and should have a very 24652a458198SEric Sandeen * low idle thresh 24662a458198SEric Sandeen */ 24672a458198SEric Sandeen fs_info->endio_workers = 2468cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 24692a458198SEric Sandeen fs_info->endio_meta_workers = 2470cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2471cb001095SJeff Mahoney max_active, 4); 24722a458198SEric Sandeen fs_info->endio_meta_write_workers = 2473cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2474cb001095SJeff Mahoney max_active, 2); 24752a458198SEric Sandeen fs_info->endio_raid56_workers = 2476cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2477cb001095SJeff Mahoney max_active, 4); 2478385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active); 24792a458198SEric Sandeen fs_info->endio_write_workers = 2480cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2481cb001095SJeff Mahoney max_active, 2); 24822a458198SEric Sandeen fs_info->endio_freespace_worker = 2483cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2484cb001095SJeff Mahoney max_active, 0); 24852a458198SEric Sandeen fs_info->delayed_workers = 2486cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2487cb001095SJeff Mahoney max_active, 0); 24882a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2489cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2490b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2491b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 24922a458198SEric Sandeen 2493a31b4a43SChristoph Hellwig if (!(fs_info->workers && fs_info->hipri_workers && 2494a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers && 24952a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 24962a458198SEric Sandeen fs_info->endio_meta_write_workers && 24972a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24982a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2499f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2500f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2501b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 25022a458198SEric Sandeen return -ENOMEM; 25032a458198SEric Sandeen } 25042a458198SEric Sandeen 25052a458198SEric Sandeen return 0; 25062a458198SEric Sandeen } 25072a458198SEric Sandeen 25086d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 25096d97c6e3SJohannes Thumshirn { 25106d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2511b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 25126d97c6e3SJohannes Thumshirn 2513b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 25146d97c6e3SJohannes Thumshirn 25156d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 25166d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2517b4e967beSDavid Sterba csum_driver); 25186d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 25196d97c6e3SJohannes Thumshirn } 25206d97c6e3SJohannes Thumshirn 25216d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 25226d97c6e3SJohannes Thumshirn 25236d97c6e3SJohannes Thumshirn return 0; 25246d97c6e3SJohannes Thumshirn } 25256d97c6e3SJohannes Thumshirn 252663443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 252763443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 252863443bf5SEric Sandeen { 252963443bf5SEric Sandeen int ret; 253063443bf5SEric Sandeen struct btrfs_root *log_tree_root; 253163443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 253263443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2533581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 253463443bf5SEric Sandeen 253563443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2536f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 253763443bf5SEric Sandeen return -EIO; 253863443bf5SEric Sandeen } 253963443bf5SEric Sandeen 254096dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 254196dfcb46SJosef Bacik GFP_KERNEL); 254263443bf5SEric Sandeen if (!log_tree_root) 254363443bf5SEric Sandeen return -ENOMEM; 254463443bf5SEric Sandeen 25452ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 25461b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 25471b7ec85eSJosef Bacik fs_info->generation + 1, level, 25481b7ec85eSJosef Bacik NULL); 254964c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2550f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 25510eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 25528c38938cSJosef Bacik log_tree_root->node = NULL; 255300246528SJosef Bacik btrfs_put_root(log_tree_root); 25540eeff236SLiu Bo return ret; 25554eb150d6SQu Wenruo } 25564eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2557f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 255800246528SJosef Bacik btrfs_put_root(log_tree_root); 255963443bf5SEric Sandeen return -EIO; 256063443bf5SEric Sandeen } 25614eb150d6SQu Wenruo 256263443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 256363443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 256463443bf5SEric Sandeen if (ret) { 25650b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 256663443bf5SEric Sandeen "Failed to recover log tree"); 256700246528SJosef Bacik btrfs_put_root(log_tree_root); 256863443bf5SEric Sandeen return ret; 256963443bf5SEric Sandeen } 257063443bf5SEric Sandeen 2571bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 25726bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 257363443bf5SEric Sandeen if (ret) 257463443bf5SEric Sandeen return ret; 257563443bf5SEric Sandeen } 257663443bf5SEric Sandeen 257763443bf5SEric Sandeen return 0; 257863443bf5SEric Sandeen } 257963443bf5SEric Sandeen 2580abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2581abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2582abed4aaaSJosef Bacik const char *name) 2583abed4aaaSJosef Bacik { 2584abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2585abed4aaaSJosef Bacik struct btrfs_root *root; 2586f7238e50SJosef Bacik u64 max_global_id = 0; 2587abed4aaaSJosef Bacik int ret; 2588abed4aaaSJosef Bacik struct btrfs_key key = { 2589abed4aaaSJosef Bacik .objectid = objectid, 2590abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2591abed4aaaSJosef Bacik .offset = 0, 2592abed4aaaSJosef Bacik }; 2593abed4aaaSJosef Bacik bool found = false; 2594abed4aaaSJosef Bacik 2595abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2596abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2597abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2598abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2599abed4aaaSJosef Bacik return 0; 2600abed4aaaSJosef Bacik } 2601abed4aaaSJosef Bacik 2602abed4aaaSJosef Bacik while (1) { 2603abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2604abed4aaaSJosef Bacik if (ret < 0) 2605abed4aaaSJosef Bacik break; 2606abed4aaaSJosef Bacik 2607abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2608abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2609abed4aaaSJosef Bacik if (ret) { 2610abed4aaaSJosef Bacik if (ret > 0) 2611abed4aaaSJosef Bacik ret = 0; 2612abed4aaaSJosef Bacik break; 2613abed4aaaSJosef Bacik } 2614abed4aaaSJosef Bacik } 2615abed4aaaSJosef Bacik ret = 0; 2616abed4aaaSJosef Bacik 2617abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2618abed4aaaSJosef Bacik if (key.objectid != objectid) 2619abed4aaaSJosef Bacik break; 2620abed4aaaSJosef Bacik btrfs_release_path(path); 2621abed4aaaSJosef Bacik 2622f7238e50SJosef Bacik /* 2623f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2624f7238e50SJosef Bacik * everybody. 2625f7238e50SJosef Bacik */ 2626f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2627f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2628f7238e50SJosef Bacik 2629abed4aaaSJosef Bacik found = true; 2630abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2631abed4aaaSJosef Bacik if (IS_ERR(root)) { 2632abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2633abed4aaaSJosef Bacik ret = PTR_ERR(root); 2634abed4aaaSJosef Bacik break; 2635abed4aaaSJosef Bacik } 2636abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2637abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2638abed4aaaSJosef Bacik if (ret) { 2639abed4aaaSJosef Bacik btrfs_put_root(root); 2640abed4aaaSJosef Bacik break; 2641abed4aaaSJosef Bacik } 2642abed4aaaSJosef Bacik key.offset++; 2643abed4aaaSJosef Bacik } 2644abed4aaaSJosef Bacik btrfs_release_path(path); 2645abed4aaaSJosef Bacik 2646f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2647f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2648f7238e50SJosef Bacik 2649abed4aaaSJosef Bacik if (!found || ret) { 2650abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2651abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2652abed4aaaSJosef Bacik 2653abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2654abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2655abed4aaaSJosef Bacik else 2656abed4aaaSJosef Bacik ret = 0; 2657abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2658abed4aaaSJosef Bacik } 2659abed4aaaSJosef Bacik return ret; 2660abed4aaaSJosef Bacik } 2661abed4aaaSJosef Bacik 2662abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2663abed4aaaSJosef Bacik { 2664abed4aaaSJosef Bacik struct btrfs_path *path; 2665abed4aaaSJosef Bacik int ret = 0; 2666abed4aaaSJosef Bacik 2667abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2668abed4aaaSJosef Bacik if (!path) 2669abed4aaaSJosef Bacik return -ENOMEM; 2670abed4aaaSJosef Bacik 2671abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2672abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2673abed4aaaSJosef Bacik if (ret) 2674abed4aaaSJosef Bacik goto out; 2675abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2676abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2677abed4aaaSJosef Bacik if (ret) 2678abed4aaaSJosef Bacik goto out; 2679abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2680abed4aaaSJosef Bacik goto out; 2681abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2682abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2683abed4aaaSJosef Bacik "free space"); 2684abed4aaaSJosef Bacik out: 2685abed4aaaSJosef Bacik btrfs_free_path(path); 2686abed4aaaSJosef Bacik return ret; 2687abed4aaaSJosef Bacik } 2688abed4aaaSJosef Bacik 26896bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 26904bbcaa64SEric Sandeen { 26916bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2692a4f3d2c4SDavid Sterba struct btrfs_root *root; 26934bbcaa64SEric Sandeen struct btrfs_key location; 26944bbcaa64SEric Sandeen int ret; 26954bbcaa64SEric Sandeen 26966bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 26976bccf3abSJeff Mahoney 2698abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2699abed4aaaSJosef Bacik if (ret) 2700abed4aaaSJosef Bacik return ret; 2701abed4aaaSJosef Bacik 2702abed4aaaSJosef Bacik location.objectid = BTRFS_DEV_TREE_OBJECTID; 27034bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 27044bbcaa64SEric Sandeen location.offset = 0; 27054bbcaa64SEric Sandeen 2706a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2707f50f4353SLiu Bo if (IS_ERR(root)) { 270842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2709f50f4353SLiu Bo ret = PTR_ERR(root); 2710f50f4353SLiu Bo goto out; 2711f50f4353SLiu Bo } 271242437a63SJosef Bacik } else { 2713a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2714a4f3d2c4SDavid Sterba fs_info->dev_root = root; 271542437a63SJosef Bacik } 2716820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2717820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 27184bbcaa64SEric Sandeen 2719aeb935a4SQu Wenruo /* 2720aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2721aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2722aeb935a4SQu Wenruo */ 272356e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 272456e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2725aeb935a4SQu Wenruo if (IS_ERR(root)) { 272642437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2727aeb935a4SQu Wenruo ret = PTR_ERR(root); 2728aeb935a4SQu Wenruo goto out; 2729aeb935a4SQu Wenruo } 273042437a63SJosef Bacik } else { 2731aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2732aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 273342437a63SJosef Bacik } 2734aeb935a4SQu Wenruo 27354bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2736a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2737a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2738a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2739afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2740a4f3d2c4SDavid Sterba fs_info->quota_root = root; 27414bbcaa64SEric Sandeen } 27424bbcaa64SEric Sandeen 27434bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2744a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2745a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 274642437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2747a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 27484bbcaa64SEric Sandeen if (ret != -ENOENT) 2749f50f4353SLiu Bo goto out; 275042437a63SJosef Bacik } 27514bbcaa64SEric Sandeen } else { 2752a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2753a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 27544bbcaa64SEric Sandeen } 27554bbcaa64SEric Sandeen 27564bbcaa64SEric Sandeen return 0; 2757f50f4353SLiu Bo out: 2758f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2759f50f4353SLiu Bo location.objectid, ret); 2760f50f4353SLiu Bo return ret; 27614bbcaa64SEric Sandeen } 27624bbcaa64SEric Sandeen 2763069ec957SQu Wenruo /* 2764069ec957SQu Wenruo * Real super block validation 2765069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2766069ec957SQu Wenruo * 2767069ec957SQu Wenruo * @sb: super block to check 2768069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2769069ec957SQu Wenruo * 0 the primary (1st) sb 2770069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2771069ec957SQu Wenruo * -1 skip bytenr check 2772069ec957SQu Wenruo */ 2773069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2774069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 277521a852b0SQu Wenruo { 277621a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 277721a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 277821a852b0SQu Wenruo int ret = 0; 277921a852b0SQu Wenruo 278021a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 278121a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 278221a852b0SQu Wenruo ret = -EINVAL; 278321a852b0SQu Wenruo } 278421a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 278521a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 278621a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 278721a852b0SQu Wenruo ret = -EINVAL; 278821a852b0SQu Wenruo } 278921a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 279021a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 279121a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 279221a852b0SQu Wenruo ret = -EINVAL; 279321a852b0SQu Wenruo } 279421a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 279521a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 279621a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 279721a852b0SQu Wenruo ret = -EINVAL; 279821a852b0SQu Wenruo } 279921a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 280021a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 280121a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 280221a852b0SQu Wenruo ret = -EINVAL; 280321a852b0SQu Wenruo } 280421a852b0SQu Wenruo 280521a852b0SQu Wenruo /* 280621a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 280721a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 280821a852b0SQu Wenruo */ 280921a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 281021a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 281121a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 281221a852b0SQu Wenruo ret = -EINVAL; 281321a852b0SQu Wenruo } 28140bb3eb3eSQu Wenruo 28150bb3eb3eSQu Wenruo /* 28161a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 28171a42daabSQu Wenruo * 28181a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 28191a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 28201a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 28211a42daabSQu Wenruo * beginning. 28220bb3eb3eSQu Wenruo */ 28231a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 282421a852b0SQu Wenruo btrfs_err(fs_info, 28250bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 282621a852b0SQu Wenruo sectorsize, PAGE_SIZE); 282721a852b0SQu Wenruo ret = -EINVAL; 282821a852b0SQu Wenruo } 28290bb3eb3eSQu Wenruo 283021a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 283121a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 283221a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 283321a852b0SQu Wenruo ret = -EINVAL; 283421a852b0SQu Wenruo } 283521a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 283621a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 283721a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 283821a852b0SQu Wenruo ret = -EINVAL; 283921a852b0SQu Wenruo } 284021a852b0SQu Wenruo 284121a852b0SQu Wenruo /* Root alignment check */ 284221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 284321a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 284421a852b0SQu Wenruo btrfs_super_root(sb)); 284521a852b0SQu Wenruo ret = -EINVAL; 284621a852b0SQu Wenruo } 284721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 284821a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 284921a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 285021a852b0SQu Wenruo ret = -EINVAL; 285121a852b0SQu Wenruo } 285221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 285321a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 285421a852b0SQu Wenruo btrfs_super_log_root(sb)); 285521a852b0SQu Wenruo ret = -EINVAL; 285621a852b0SQu Wenruo } 285721a852b0SQu Wenruo 2858aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2859aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2860aefd7f70SNikolay Borisov btrfs_err(fs_info, 2861aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2862aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2863aefd7f70SNikolay Borisov ret = -EINVAL; 2864aefd7f70SNikolay Borisov } 2865aefd7f70SNikolay Borisov 2866aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2867aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2868aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2869aefd7f70SNikolay Borisov btrfs_err(fs_info, 2870aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2871aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2872aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2873aefd7f70SNikolay Borisov ret = -EINVAL; 2874aefd7f70SNikolay Borisov } 2875aefd7f70SNikolay Borisov 2876de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 28777239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 287821a852b0SQu Wenruo btrfs_err(fs_info, 28797239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2880de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 288121a852b0SQu Wenruo ret = -EINVAL; 288221a852b0SQu Wenruo } 288321a852b0SQu Wenruo 288421a852b0SQu Wenruo /* 288521a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 288621a852b0SQu Wenruo * done later 288721a852b0SQu Wenruo */ 288821a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 288921a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 289021a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 289121a852b0SQu Wenruo ret = -EINVAL; 289221a852b0SQu Wenruo } 289321a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 289421a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 289521a852b0SQu Wenruo btrfs_super_stripesize(sb)); 289621a852b0SQu Wenruo ret = -EINVAL; 289721a852b0SQu Wenruo } 289821a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 289921a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 290021a852b0SQu Wenruo btrfs_super_num_devices(sb)); 290121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 290221a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 290321a852b0SQu Wenruo ret = -EINVAL; 290421a852b0SQu Wenruo } 290521a852b0SQu Wenruo 2906069ec957SQu Wenruo if (mirror_num >= 0 && 2907069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 290821a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 290921a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 291021a852b0SQu Wenruo ret = -EINVAL; 291121a852b0SQu Wenruo } 291221a852b0SQu Wenruo 291321a852b0SQu Wenruo /* 291421a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 291521a852b0SQu Wenruo * and one chunk 291621a852b0SQu Wenruo */ 291721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 291821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 291921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 292021a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 292121a852b0SQu Wenruo ret = -EINVAL; 292221a852b0SQu Wenruo } 292321a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 292421a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 292521a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 292621a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 292721a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 292821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 292921a852b0SQu Wenruo ret = -EINVAL; 293021a852b0SQu Wenruo } 293121a852b0SQu Wenruo 293221a852b0SQu Wenruo /* 293321a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 293421a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 293521a852b0SQu Wenruo */ 293621a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 293721a852b0SQu Wenruo btrfs_warn(fs_info, 293821a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 293921a852b0SQu Wenruo btrfs_super_generation(sb), 294021a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 294121a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 294221a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 294321a852b0SQu Wenruo btrfs_warn(fs_info, 294421a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 294521a852b0SQu Wenruo btrfs_super_generation(sb), 294621a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 294721a852b0SQu Wenruo 294821a852b0SQu Wenruo return ret; 294921a852b0SQu Wenruo } 295021a852b0SQu Wenruo 2951069ec957SQu Wenruo /* 2952069ec957SQu Wenruo * Validation of super block at mount time. 2953069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2954069ec957SQu Wenruo * flags will be skipped. 2955069ec957SQu Wenruo */ 2956069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2957069ec957SQu Wenruo { 2958069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2959069ec957SQu Wenruo } 2960069ec957SQu Wenruo 296175cb857dSQu Wenruo /* 296275cb857dSQu Wenruo * Validation of super block at write time. 296375cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 296475cb857dSQu Wenruo * overwritten soon. 296575cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 296675cb857dSQu Wenruo */ 296775cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 296875cb857dSQu Wenruo struct btrfs_super_block *sb) 296975cb857dSQu Wenruo { 297075cb857dSQu Wenruo int ret; 297175cb857dSQu Wenruo 297275cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 297375cb857dSQu Wenruo if (ret < 0) 297475cb857dSQu Wenruo goto out; 2975e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 297675cb857dSQu Wenruo ret = -EUCLEAN; 297775cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 297875cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 297975cb857dSQu Wenruo goto out; 298075cb857dSQu Wenruo } 298175cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 298275cb857dSQu Wenruo ret = -EUCLEAN; 298375cb857dSQu Wenruo btrfs_err(fs_info, 298475cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 298575cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 298675cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 298775cb857dSQu Wenruo goto out; 298875cb857dSQu Wenruo } 298975cb857dSQu Wenruo out: 299075cb857dSQu Wenruo if (ret < 0) 299175cb857dSQu Wenruo btrfs_err(fs_info, 299275cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 299375cb857dSQu Wenruo return ret; 299475cb857dSQu Wenruo } 299575cb857dSQu Wenruo 2996bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2997bd676446SJosef Bacik { 2998bd676446SJosef Bacik int ret = 0; 2999bd676446SJosef Bacik 3000bd676446SJosef Bacik root->node = read_tree_block(root->fs_info, bytenr, 3001bd676446SJosef Bacik root->root_key.objectid, gen, level, NULL); 3002bd676446SJosef Bacik if (IS_ERR(root->node)) { 3003bd676446SJosef Bacik ret = PTR_ERR(root->node); 3004bd676446SJosef Bacik root->node = NULL; 30054eb150d6SQu Wenruo return ret; 30064eb150d6SQu Wenruo } 30074eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 3008bd676446SJosef Bacik free_extent_buffer(root->node); 3009bd676446SJosef Bacik root->node = NULL; 30104eb150d6SQu Wenruo return -EIO; 3011bd676446SJosef Bacik } 3012bd676446SJosef Bacik 3013bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 3014bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 3015bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 3016bd676446SJosef Bacik return ret; 3017bd676446SJosef Bacik } 3018bd676446SJosef Bacik 3019bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 3020bd676446SJosef Bacik { 3021bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 3022bd676446SJosef Bacik u64 gen, bytenr; 3023bd676446SJosef Bacik int level, ret; 3024bd676446SJosef Bacik 3025bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 3026bd676446SJosef Bacik gen = btrfs_super_generation(sb); 3027bd676446SJosef Bacik level = btrfs_super_root_level(sb); 3028bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 30299c54e80dSJosef Bacik if (ret) { 3030bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 3031bd676446SJosef Bacik return ret; 3032bd676446SJosef Bacik } 3033bd676446SJosef Bacik 30349c54e80dSJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 30359c54e80dSJosef Bacik return 0; 30369c54e80dSJosef Bacik 30379c54e80dSJosef Bacik bytenr = btrfs_super_block_group_root(sb); 30389c54e80dSJosef Bacik gen = btrfs_super_block_group_root_generation(sb); 30399c54e80dSJosef Bacik level = btrfs_super_block_group_root_level(sb); 30409c54e80dSJosef Bacik ret = load_super_root(fs_info->block_group_root, bytenr, gen, level); 30419c54e80dSJosef Bacik if (ret) 30429c54e80dSJosef Bacik btrfs_warn(fs_info, "couldn't read block group root"); 30439c54e80dSJosef Bacik return ret; 30449c54e80dSJosef Bacik } 30459c54e80dSJosef Bacik 30466ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 3047b8522a1eSNikolay Borisov { 30486ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 3049b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 3050b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 3051b8522a1eSNikolay Borisov bool handle_error = false; 3052b8522a1eSNikolay Borisov int ret = 0; 3053b8522a1eSNikolay Borisov int i; 3054b8522a1eSNikolay Borisov 30559c54e80dSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 30569c54e80dSJosef Bacik struct btrfs_root *root; 3057b8522a1eSNikolay Borisov 30589c54e80dSJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID, 30599c54e80dSJosef Bacik GFP_KERNEL); 30609c54e80dSJosef Bacik if (!root) 30619c54e80dSJosef Bacik return -ENOMEM; 30629c54e80dSJosef Bacik fs_info->block_group_root = root; 30639c54e80dSJosef Bacik } 30649c54e80dSJosef Bacik 3065b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 3066b8522a1eSNikolay Borisov if (handle_error) { 3067b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 3068b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 3069b8522a1eSNikolay Borisov tree_root->node = NULL; 3070b8522a1eSNikolay Borisov 3071b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3072b8522a1eSNikolay Borisov break; 3073b8522a1eSNikolay Borisov 3074b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 3075b8522a1eSNikolay Borisov 3076b8522a1eSNikolay Borisov /* 3077b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 3078b8522a1eSNikolay Borisov * valid 3079b8522a1eSNikolay Borisov */ 3080b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 3081b8522a1eSNikolay Borisov 3082b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 3083b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3084b8522a1eSNikolay Borisov 3085b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 30866ef108ddSNikolay Borisov backup_index = ret; 3087b8522a1eSNikolay Borisov if (ret < 0) 3088b8522a1eSNikolay Borisov return ret; 3089b8522a1eSNikolay Borisov } 3090b8522a1eSNikolay Borisov 3091bd676446SJosef Bacik ret = load_important_roots(fs_info); 3092bd676446SJosef Bacik if (ret) { 3093217f5004SNikolay Borisov handle_error = true; 3094b8522a1eSNikolay Borisov continue; 3095b8522a1eSNikolay Borisov } 3096b8522a1eSNikolay Borisov 3097336a0d8dSNikolay Borisov /* 3098336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3099336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3100336a0d8dSNikolay Borisov */ 3101453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3102b8522a1eSNikolay Borisov if (ret < 0) { 3103b8522a1eSNikolay Borisov handle_error = true; 3104b8522a1eSNikolay Borisov continue; 3105b8522a1eSNikolay Borisov } 3106b8522a1eSNikolay Borisov 31076b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3108b8522a1eSNikolay Borisov 3109b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3110b8522a1eSNikolay Borisov if (ret < 0) { 3111b8522a1eSNikolay Borisov handle_error = true; 3112b8522a1eSNikolay Borisov continue; 3113b8522a1eSNikolay Borisov } 3114b8522a1eSNikolay Borisov 3115b8522a1eSNikolay Borisov /* All successful */ 3116bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3117bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3118d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 31196ef108ddSNikolay Borisov 31206ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 31216ef108ddSNikolay Borisov if (backup_index < 0) { 31226ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 31236ef108ddSNikolay Borisov } else { 31246ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 31256ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 31266ef108ddSNikolay Borisov } 3127b8522a1eSNikolay Borisov break; 3128b8522a1eSNikolay Borisov } 3129b8522a1eSNikolay Borisov 3130b8522a1eSNikolay Borisov return ret; 3131b8522a1eSNikolay Borisov } 3132b8522a1eSNikolay Borisov 31338260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3134eb60ceacSChris Mason { 313548b36a60SGabriel Niebler xa_init_flags(&fs_info->fs_roots, GFP_ATOMIC); 31368ee92268SGabriel Niebler xa_init_flags(&fs_info->extent_buffers, GFP_ATOMIC); 31378fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3138facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 313924bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3140eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 314111833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3142eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3143a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 314448b36a60SGabriel Niebler spin_lock_init(&fs_info->fs_roots_lock); 314524bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 31464cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3147ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3148f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 314947ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 315040ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3151afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3152c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3153f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3154abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3155d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3156f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 31577585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3158573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 31590bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 31605f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3161de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 316219c00ddcSChris Mason 31630b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 31646324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3165f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 316647ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 316718bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3168afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3169bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3170bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 31713fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 31723fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3173bd647ce3SJosef Bacik #endif 3174c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 317566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 317666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 317766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 317866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 317966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 318066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 318166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3182ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3183ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3184ba2c4d4eSJosef Bacik 3185771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 31864cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3187034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3188fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3189abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 319095ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 31919ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 31924cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3193a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3194f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 31958b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3196f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3197fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3198c8b97818SChris Mason 3199b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3200b34b086cSChris Mason num_online_cpus() + 2, 8); 32010afbaf8cSChris Mason 3202199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3203199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 320469fe2d75SJosef Bacik 3205638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 320621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 320721adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 320821adbd5cSStefan Behrens #endif 3209779a65a4SEric Sandeen btrfs_init_balance(fs_info); 321057056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3211a2de733cSArne Jansen 321216b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock); 321308dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED; 32140f9dd46cSJosef Bacik 3215fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 3216fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 321739279cc3SChris Mason 32185a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3219e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3220925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3221a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3222a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 32231bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 32249e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3225c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 322676dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3227803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3228fa9c0d79SChris Mason 3229ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3230f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3231b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3232416ac51dSArne Jansen 3233fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3234fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3235fa9c0d79SChris Mason 3236e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3237f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3238bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 32394854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3240034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 32413768f368SChris Mason 3242da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3243da17066cSJeff Mahoney fs_info->nodesize = 4096; 3244da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3245ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3246da17066cSJeff Mahoney fs_info->stripesize = 4096; 3247da17066cSJeff Mahoney 3248eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3249eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3250eede2bf3SOmar Sandoval 325118bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 325218bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 32538260edbaSJosef Bacik } 32548260edbaSJosef Bacik 32558260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 32568260edbaSJosef Bacik { 32578260edbaSJosef Bacik int ret; 32588260edbaSJosef Bacik 32598260edbaSJosef Bacik fs_info->sb = sb; 32608260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 32618260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 32629e967495SFilipe Manana 32635deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3264ae18c37aSJosef Bacik if (ret) 3265ae18c37aSJosef Bacik return ret; 3266ae18c37aSJosef Bacik 3267ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3268ae18c37aSJosef Bacik if (ret) 3269c75e8394SJosef Bacik return ret; 3270ae18c37aSJosef Bacik 3271ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3272ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3273ae18c37aSJosef Bacik 3274ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3275ae18c37aSJosef Bacik if (ret) 3276c75e8394SJosef Bacik return ret; 3277ae18c37aSJosef Bacik 3278ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3279ae18c37aSJosef Bacik GFP_KERNEL); 3280ae18c37aSJosef Bacik if (ret) 3281c75e8394SJosef Bacik return ret; 3282ae18c37aSJosef Bacik 3283ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3284ae18c37aSJosef Bacik GFP_KERNEL); 3285c75e8394SJosef Bacik if (!fs_info->delayed_root) 3286c75e8394SJosef Bacik return -ENOMEM; 3287ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3288ae18c37aSJosef Bacik 3289a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3290a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3291a0a1db70SFilipe Manana 3292c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3293ae18c37aSJosef Bacik } 3294ae18c37aSJosef Bacik 329597f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 329697f4dd09SNikolay Borisov { 32970d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 329897f4dd09SNikolay Borisov int ret; 329997f4dd09SNikolay Borisov 330097f4dd09SNikolay Borisov /* 330197f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 330297f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 330397f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 330497f4dd09SNikolay Borisov */ 330597f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 330697f4dd09SNikolay Borisov if (ret < 0) { 3307c94bec2cSJosef Bacik if (ret != -EINTR) 3308c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3309c94bec2cSJosef Bacik ret); 331097f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 331197f4dd09SNikolay Borisov return ret; 331297f4dd09SNikolay Borisov } 331397f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 331497f4dd09SNikolay Borisov } 331597f4dd09SNikolay Borisov 331697f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 331797f4dd09SNikolay Borisov { 331897f4dd09SNikolay Borisov struct task_struct *task; 331997f4dd09SNikolay Borisov 332097f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 332197f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 332297f4dd09SNikolay Borisov if (IS_ERR(task)) { 332397f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 332497f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 332597f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 332697f4dd09SNikolay Borisov return PTR_ERR(task); 332797f4dd09SNikolay Borisov } 332897f4dd09SNikolay Borisov 332997f4dd09SNikolay Borisov return 0; 333097f4dd09SNikolay Borisov } 333197f4dd09SNikolay Borisov 333244c0ca21SBoris Burkov /* 33338cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 33348cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 33358cd29088SBoris Burkov * code paths. 33368cd29088SBoris Burkov */ 33378cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 33388cd29088SBoris Burkov { 33398cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33408b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 33418cd29088SBoris Burkov } 33428cd29088SBoris Burkov 33438cd29088SBoris Burkov /* 334444c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 334544c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 334644c0ca21SBoris Burkov */ 334744c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 334844c0ca21SBoris Burkov { 334944c0ca21SBoris Burkov int ret; 335094846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 33518b228324SBoris Burkov bool clear_free_space_tree = false; 33528b228324SBoris Burkov 33538b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 33548b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33558b228324SBoris Burkov clear_free_space_tree = true; 33568b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33578b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33588b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 33598b228324SBoris Burkov clear_free_space_tree = true; 33608b228324SBoris Burkov } 33618b228324SBoris Burkov 33628b228324SBoris Burkov if (clear_free_space_tree) { 33638b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 33648b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 33658b228324SBoris Burkov if (ret) { 33668b228324SBoris Burkov btrfs_warn(fs_info, 33678b228324SBoris Burkov "failed to clear free space tree: %d", ret); 33688b228324SBoris Burkov goto out; 33698b228324SBoris Burkov } 33708b228324SBoris Burkov } 337144c0ca21SBoris Burkov 33728d488a8cSFilipe Manana /* 33738d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 33748d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 337548b36a60SGabriel Niebler * them into the fs_info->fs_roots. This must be done before 33768d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 33778d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 33788d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 33798d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 33808d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 33818d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 33828d488a8cSFilipe Manana */ 33838d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 33848d488a8cSFilipe Manana if (ret) 33858d488a8cSFilipe Manana goto out; 33868d488a8cSFilipe Manana 338744c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 338844c0ca21SBoris Burkov if (ret) 338944c0ca21SBoris Burkov goto out; 339044c0ca21SBoris Burkov 33918f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 33928f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33938f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33948f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33958f1c21d7SBoris Burkov goto out; 33968f1c21d7SBoris Burkov } 33978f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33988f1c21d7SBoris Burkov 339944c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 34007eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 340144c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 340244c0ca21SBoris Burkov if (ret < 0) { 340344c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 340444c0ca21SBoris Burkov goto out; 340544c0ca21SBoris Burkov } 340644c0ca21SBoris Burkov 34075011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 34085011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 34095011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 34105011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 34115011139aSBoris Burkov if (ret) { 34125011139aSBoris Burkov btrfs_warn(fs_info, 34135011139aSBoris Burkov "failed to create free space tree: %d", ret); 34145011139aSBoris Burkov goto out; 34155011139aSBoris Burkov } 34165011139aSBoris Burkov } 34175011139aSBoris Burkov 341894846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 341994846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 342094846229SBoris Burkov if (ret) 342194846229SBoris Burkov goto out; 342294846229SBoris Burkov } 342394846229SBoris Burkov 342444c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 342544c0ca21SBoris Burkov if (ret) 342644c0ca21SBoris Burkov goto out; 342744c0ca21SBoris Burkov 342844c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 342944c0ca21SBoris Burkov if (ret) { 343044c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 343144c0ca21SBoris Burkov goto out; 343244c0ca21SBoris Burkov } 343344c0ca21SBoris Burkov 343444c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 343544c0ca21SBoris Burkov 343644c0ca21SBoris Burkov if (!fs_info->uuid_root) { 343744c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 343844c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 343944c0ca21SBoris Burkov if (ret) { 344044c0ca21SBoris Burkov btrfs_warn(fs_info, 344144c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 344244c0ca21SBoris Burkov goto out; 344344c0ca21SBoris Burkov } 344444c0ca21SBoris Burkov } 344544c0ca21SBoris Burkov 344644c0ca21SBoris Burkov out: 344744c0ca21SBoris Burkov return ret; 344844c0ca21SBoris Burkov } 344944c0ca21SBoris Burkov 3450ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3451ae18c37aSJosef Bacik char *options) 3452ae18c37aSJosef Bacik { 3453ae18c37aSJosef Bacik u32 sectorsize; 3454ae18c37aSJosef Bacik u32 nodesize; 3455ae18c37aSJosef Bacik u32 stripesize; 3456ae18c37aSJosef Bacik u64 generation; 3457ae18c37aSJosef Bacik u64 features; 3458ae18c37aSJosef Bacik u16 csum_type; 3459ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3460ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3461ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3462ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3463ae18c37aSJosef Bacik int ret; 3464ae18c37aSJosef Bacik int err = -EINVAL; 3465ae18c37aSJosef Bacik int level; 3466ae18c37aSJosef Bacik 34678260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 346853b381b3SDavid Woodhouse if (ret) { 346983c8266aSDavid Sterba err = ret; 3470ae18c37aSJosef Bacik goto fail; 347153b381b3SDavid Woodhouse } 347253b381b3SDavid Woodhouse 3473ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3474ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3475ae18c37aSJosef Bacik GFP_KERNEL); 3476ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3477ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3478ae18c37aSJosef Bacik GFP_KERNEL); 3479ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3480ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3481ae18c37aSJosef Bacik err = -ENOMEM; 3482c75e8394SJosef Bacik goto fail; 3483ae18c37aSJosef Bacik } 3484ae18c37aSJosef Bacik 3485ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3486ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3487ae18c37aSJosef Bacik err = -ENOMEM; 3488c75e8394SJosef Bacik goto fail; 3489ae18c37aSJosef Bacik } 3490ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3491ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3492ae18c37aSJosef Bacik 3493d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 34941104a885SDavid Sterba 34951104a885SDavid Sterba /* 34961104a885SDavid Sterba * Read super block and check the signature bytes only 34971104a885SDavid Sterba */ 3498d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 34998f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 35008f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 350116cdcec7SMiao Xie goto fail_alloc; 350220b45077SDave Young } 350339279cc3SChris Mason 35041104a885SDavid Sterba /* 3505260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 35068dc3f22cSJohannes Thumshirn * corrupted, we'll find out 35078dc3f22cSJohannes Thumshirn */ 35088f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 350951bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 35108dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 351151bce6c9SJohannes Thumshirn csum_type); 35128dc3f22cSJohannes Thumshirn err = -EINVAL; 35138f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35148dc3f22cSJohannes Thumshirn goto fail_alloc; 35158dc3f22cSJohannes Thumshirn } 35168dc3f22cSJohannes Thumshirn 351783c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 351883c68bbcSSu Yue 35196d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 35206d97c6e3SJohannes Thumshirn if (ret) { 35216d97c6e3SJohannes Thumshirn err = ret; 35228f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35236d97c6e3SJohannes Thumshirn goto fail_alloc; 35246d97c6e3SJohannes Thumshirn } 35256d97c6e3SJohannes Thumshirn 35268dc3f22cSJohannes Thumshirn /* 35271104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 35281104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 35291104a885SDavid Sterba */ 35308f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 353105135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 35321104a885SDavid Sterba err = -EINVAL; 35338f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3534141386e1SJosef Bacik goto fail_alloc; 35351104a885SDavid Sterba } 35361104a885SDavid Sterba 35371104a885SDavid Sterba /* 35381104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 35391104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 35401104a885SDavid Sterba * the whole block of INFO_SIZE 35411104a885SDavid Sterba */ 35428f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 35438f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35445f39d397SChris Mason 3545fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3546fbc6feaeSNikolay Borisov 35470b86a832SChris Mason 3548fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3549fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3550fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3551fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3552fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3553fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3554fbc6feaeSNikolay Borisov } 3555fbc6feaeSNikolay Borisov 3556fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3557fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3558de37aa51SNikolay Borisov 3559069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 35601104a885SDavid Sterba if (ret) { 356105135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 35621104a885SDavid Sterba err = -EINVAL; 3563141386e1SJosef Bacik goto fail_alloc; 35641104a885SDavid Sterba } 35651104a885SDavid Sterba 35660f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3567141386e1SJosef Bacik goto fail_alloc; 35680f7d52f4SChris Mason 3569acce952bSliubo /* check FS state, whether FS is broken. */ 357087533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 357187533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3572acce952bSliubo 357375e7cb7fSLiu Bo /* 357475e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 357575e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 357675e7cb7fSLiu Bo */ 357775e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 357875e7cb7fSLiu Bo 35796f93e834SAnand Jain /* 35806f93e834SAnand Jain * Flag our filesystem as having big metadata blocks if they are bigger 35816f93e834SAnand Jain * than the page size. 35826f93e834SAnand Jain */ 35836f93e834SAnand Jain if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 35846f93e834SAnand Jain if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 35856f93e834SAnand Jain btrfs_info(fs_info, 35866f93e834SAnand Jain "flagging fs with big metadata feature"); 35876f93e834SAnand Jain features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 35886f93e834SAnand Jain } 35896f93e834SAnand Jain 35906f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 35916f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 35926f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 35936f93e834SAnand Jain stripesize = sectorsize; 35946f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 35956f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 35966f93e834SAnand Jain 35976f93e834SAnand Jain fs_info->nodesize = nodesize; 35986f93e834SAnand Jain fs_info->sectorsize = sectorsize; 35996f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 36006f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 36016f93e834SAnand Jain fs_info->stripesize = stripesize; 36026f93e834SAnand Jain 36032ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 36042b82032cSYan Zheng if (ret) { 36052b82032cSYan Zheng err = ret; 3606141386e1SJosef Bacik goto fail_alloc; 36072b82032cSYan Zheng } 3608dfe25020SChris Mason 3609f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3610f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3611f2b636e8SJosef Bacik if (features) { 361205135f59SDavid Sterba btrfs_err(fs_info, 3613d5321a0fSQu Wenruo "cannot mount because of unsupported optional features (0x%llx)", 3614c1c9ff7cSGeert Uytterhoeven features); 3615f2b636e8SJosef Bacik err = -EINVAL; 3616141386e1SJosef Bacik goto fail_alloc; 3617f2b636e8SJosef Bacik } 3618f2b636e8SJosef Bacik 36195d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 36205d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 36210b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3622a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 36235c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 36245c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3625727011e0SChris Mason 36263173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 362705135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 36283173a18fSJosef Bacik 3629727011e0SChris Mason /* 3630bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3631bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3632bc3f116fSChris Mason */ 3633bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3634707e8a07SDavid Sterba (sectorsize != nodesize)) { 363505135f59SDavid Sterba btrfs_err(fs_info, 363605135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 363705135f59SDavid Sterba nodesize, sectorsize); 3638141386e1SJosef Bacik goto fail_alloc; 3639bc3f116fSChris Mason } 3640bc3f116fSChris Mason 3641ceda0864SMiao Xie /* 3642ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3643ceda0864SMiao Xie * update the flag. 3644ceda0864SMiao Xie */ 36455d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 36465d4f98a2SYan Zheng 3647f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3648f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3649bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 365005135f59SDavid Sterba btrfs_err(fs_info, 3651d5321a0fSQu Wenruo "cannot mount read-write because of unsupported optional features (0x%llx)", 3652c1c9ff7cSGeert Uytterhoeven features); 3653f2b636e8SJosef Bacik err = -EINVAL; 3654141386e1SJosef Bacik goto fail_alloc; 3655f2b636e8SJosef Bacik } 365661d92c32SChris Mason 36578481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 36588481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 36598481dd80SQu Wenruo 36609f73f1aeSQu Wenruo /* 36619f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 36629f73f1aeSQu Wenruo * going to be deprecated. 36639f73f1aeSQu Wenruo * 36649f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 36659f73f1aeSQu Wenruo */ 36669f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 36679f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 36689f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 36699f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 36709f73f1aeSQu Wenruo 367195ea0486SQu Wenruo btrfs_warn(fs_info, 367295ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 36730bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 36748481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 36758481dd80SQu Wenruo if (!subpage_info) 36768481dd80SQu Wenruo goto fail_alloc; 36778481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 36788481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3679c8050b3bSQu Wenruo } 36800bb3eb3eSQu Wenruo 3681d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 36822a458198SEric Sandeen if (ret) { 36832a458198SEric Sandeen err = ret; 36840dc3b84aSJosef Bacik goto fail_sb_buffer; 36850dc3b84aSJosef Bacik } 36864543df7eSChris Mason 36879e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 36889e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 36894575c9ccSChris Mason 3690a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3691a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3692de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3693db94535dSChris Mason 3694925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 36956bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3696925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 369784eed90fSChris Mason if (ret) { 369805135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36995d4f98a2SYan Zheng goto fail_sb_buffer; 370084eed90fSChris Mason } 37010b86a832SChris Mason 370284234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3703581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3704bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3705bd676446SJosef Bacik generation, level); 3706bd676446SJosef Bacik if (ret) { 370705135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3708af31f5e5SChris Mason goto fail_tree_roots; 370983121942SDavid Woodhouse } 37100b86a832SChris Mason 3711e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3712c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3713c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3714e17cade2SChris Mason 37155b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 37162b82032cSYan Zheng if (ret) { 371705135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3718af31f5e5SChris Mason goto fail_tree_roots; 37192b82032cSYan Zheng } 37200b86a832SChris Mason 37218dabb742SStefan Behrens /* 3722bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3723bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3724bacce86aSAnand Jain * target is required. So free devices that are not part of this 37251a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3726bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 37278dabb742SStefan Behrens */ 3728bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3729d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 373005135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3731a6b0d5c8SChris Mason goto fail_tree_roots; 3732a6b0d5c8SChris Mason } 3733a6b0d5c8SChris Mason 3734b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 37354bbcaa64SEric Sandeen if (ret) 3736b8522a1eSNikolay Borisov goto fail_tree_roots; 37378929ecfaSYan, Zheng 373875ec1db8SJosef Bacik /* 373973651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 374073651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 374173651042SNaohiro Aota * zoned incompat feature flag set. 374273651042SNaohiro Aota */ 374373651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 374473651042SNaohiro Aota if (ret) { 374573651042SNaohiro Aota btrfs_err(fs_info, 374673651042SNaohiro Aota "zoned: failed to read device zone info: %d", 374773651042SNaohiro Aota ret); 374873651042SNaohiro Aota goto fail_block_groups; 374973651042SNaohiro Aota } 375073651042SNaohiro Aota 375173651042SNaohiro Aota /* 375275ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 375375ec1db8SJosef Bacik * check the generation here so we can set the 375475ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 375575ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 375675ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 375775ec1db8SJosef Bacik * even though it was perfectly fine. 375875ec1db8SJosef Bacik */ 375975ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 376075ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 376175ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 376275ec1db8SJosef Bacik 3763cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3764cf90d884SQu Wenruo if (ret) { 3765cf90d884SQu Wenruo btrfs_err(fs_info, 3766cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3767cf90d884SQu Wenruo ret); 3768cf90d884SQu Wenruo goto fail_block_groups; 3769cf90d884SQu Wenruo } 377068310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 377168310a5eSIlya Dryomov if (ret) { 377205135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 377368310a5eSIlya Dryomov goto fail_block_groups; 377468310a5eSIlya Dryomov } 377568310a5eSIlya Dryomov 3776733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3777733f4fbbSStefan Behrens if (ret) { 377805135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3779733f4fbbSStefan Behrens goto fail_block_groups; 3780733f4fbbSStefan Behrens } 3781733f4fbbSStefan Behrens 37828dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 37838dabb742SStefan Behrens if (ret) { 378405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 37858dabb742SStefan Behrens goto fail_block_groups; 37868dabb742SStefan Behrens } 37878dabb742SStefan Behrens 3788b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3789b70f5097SNaohiro Aota if (ret) { 3790b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3791b70f5097SNaohiro Aota ret); 3792b70f5097SNaohiro Aota goto fail_block_groups; 3793b70f5097SNaohiro Aota } 3794b70f5097SNaohiro Aota 3795c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3796b7c35e81SAnand Jain if (ret) { 379705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 379805135f59SDavid Sterba ret); 3799b7c35e81SAnand Jain goto fail_block_groups; 3800b7c35e81SAnand Jain } 3801b7c35e81SAnand Jain 380296f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 38035ac1d209SJeff Mahoney if (ret) { 380405135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3805b7c35e81SAnand Jain goto fail_fsdev_sysfs; 38065ac1d209SJeff Mahoney } 38075ac1d209SJeff Mahoney 3808c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3809c59021f8Sliubo if (ret) { 381005135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 38112365dd3cSAnand Jain goto fail_sysfs; 3812c59021f8Sliubo } 3813c59021f8Sliubo 38145b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 38151b1d1f66SJosef Bacik if (ret) { 381605135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 38172365dd3cSAnand Jain goto fail_sysfs; 38181b1d1f66SJosef Bacik } 38194330e183SQu Wenruo 382016beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 382116beac87SNaohiro Aota 38225c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 38235c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 382405135f59SDavid Sterba btrfs_warn(fs_info, 382552042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 38262365dd3cSAnand Jain goto fail_sysfs; 3827292fd7fcSStefan Behrens } 38289078a3e1SChris Mason 382933c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3830a74a4b97SChris Mason "btrfs-cleaner"); 383157506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 38322365dd3cSAnand Jain goto fail_sysfs; 3833a74a4b97SChris Mason 3834a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3835a74a4b97SChris Mason tree_root, 3836a74a4b97SChris Mason "btrfs-transaction"); 383757506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 38383f157a2fSChris Mason goto fail_cleaner; 3839a74a4b97SChris Mason 3840583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3841c289811cSChris Mason !fs_info->fs_devices->rotating) { 3842583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3843c289811cSChris Mason } 3844c289811cSChris Mason 3845572d9ab7SDavid Sterba /* 384601327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3847572d9ab7SDavid Sterba * not have to wait for transaction commit 3848572d9ab7SDavid Sterba */ 3849572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 38503818aea2SQu Wenruo 385121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38520b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 38532ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 38540b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3855cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 385621adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 385721adbd5cSStefan Behrens if (ret) 385805135f59SDavid Sterba btrfs_warn(fs_info, 385905135f59SDavid Sterba "failed to initialize integrity check module: %d", 386005135f59SDavid Sterba ret); 386121adbd5cSStefan Behrens } 386221adbd5cSStefan Behrens #endif 3863bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3864bcef60f2SArne Jansen if (ret) 3865bcef60f2SArne Jansen goto fail_trans_kthread; 386621adbd5cSStefan Behrens 3867fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3868fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3869fd708b81SJosef Bacik 387096da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 387196da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 38720b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3873e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 387463443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 387579787eaaSJeff Mahoney if (ret) { 387663443bf5SEric Sandeen err = ret; 387728c16cbbSWang Shilong goto fail_qgroup; 3878e556ce2cSYan Zheng } 3879e02119d5SChris Mason } 38801a40e23bSZheng Yan 388156e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 38823140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 38833140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3884f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3885315bf8efSJosef Bacik fs_info->fs_root = NULL; 3886bcef60f2SArne Jansen goto fail_qgroup; 38873140c9a3SDan Carpenter } 3888c289811cSChris Mason 3889bc98a42cSDavid Howells if (sb_rdonly(sb)) 38908cd29088SBoris Burkov goto clear_oneshot; 38912b6ba629SIlya Dryomov 389244c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 38932b6ba629SIlya Dryomov if (ret) { 38946bccf3abSJeff Mahoney close_ctree(fs_info); 38952b6ba629SIlya Dryomov return ret; 3896e3acc2a6SJosef Bacik } 3897b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3898b382a324SJan Schmidt 389944c0ca21SBoris Burkov if (fs_info->uuid_root && 390044c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 390144c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 390205135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 390370f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 390470f80175SStefan Behrens if (ret) { 390505135f59SDavid Sterba btrfs_warn(fs_info, 390605135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 39076bccf3abSJeff Mahoney close_ctree(fs_info); 390870f80175SStefan Behrens return ret; 390970f80175SStefan Behrens } 3910f7a81ea4SStefan Behrens } 391194846229SBoris Burkov 3912afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 391347ab2a6cSJosef Bacik 3914b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3915b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3916b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3917b4be6aefSJosef Bacik 39188cd29088SBoris Burkov clear_oneshot: 39198cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3920ad2b2c80SAl Viro return 0; 392139279cc3SChris Mason 3922bcef60f2SArne Jansen fail_qgroup: 3923bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 39247c2ca468SChris Mason fail_trans_kthread: 39257c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 39262ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3927faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 39283f157a2fSChris Mason fail_cleaner: 3929a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 39307c2ca468SChris Mason 39317c2ca468SChris Mason /* 39327c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 39337c2ca468SChris Mason * kthreads 39347c2ca468SChris Mason */ 39357c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 39367c2ca468SChris Mason 39372365dd3cSAnand Jain fail_sysfs: 39386618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 39392365dd3cSAnand Jain 3940b7c35e81SAnand Jain fail_fsdev_sysfs: 3941b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3942b7c35e81SAnand Jain 39431b1d1f66SJosef Bacik fail_block_groups: 394454067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3945af31f5e5SChris Mason 3946af31f5e5SChris Mason fail_tree_roots: 39479e3aa805SJosef Bacik if (fs_info->data_reloc_root) 39489e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 39494273eaffSAnand Jain free_root_pointers(fs_info, true); 39502b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3951af31f5e5SChris Mason 395239279cc3SChris Mason fail_sb_buffer: 39537abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 39545cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 395516cdcec7SMiao Xie fail_alloc: 3956586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3957586e46e2SIlya Dryomov 39584543df7eSChris Mason iput(fs_info->btree_inode); 39597e662854SQinghuang Feng fail: 3960586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3961ad2b2c80SAl Viro return err; 3962eb60ceacSChris Mason } 3963663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3964eb60ceacSChris Mason 3965314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3966f2984462SChris Mason { 3967314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3968314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3969314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3970314b6dd0SJohannes Thumshirn struct page *page; 3971442a4f63SStefan Behrens 3972314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3973314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3974314b6dd0SJohannes Thumshirn 3975314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3976fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3977314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3978314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3979314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3980314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3981314b6dd0SJohannes Thumshirn SetPageError(page); 3982314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3983314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3984314b6dd0SJohannes Thumshirn } else { 3985314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3986f2984462SChris Mason } 3987314b6dd0SJohannes Thumshirn 3988314b6dd0SJohannes Thumshirn put_page(page); 3989314b6dd0SJohannes Thumshirn unlock_page(page); 3990314b6dd0SJohannes Thumshirn } 3991314b6dd0SJohannes Thumshirn 3992314b6dd0SJohannes Thumshirn bio_put(bio); 3993f2984462SChris Mason } 3994f2984462SChris Mason 39958f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 39968f32380dSJohannes Thumshirn int copy_num) 399729c36d72SAnand Jain { 399829c36d72SAnand Jain struct btrfs_super_block *super; 39998f32380dSJohannes Thumshirn struct page *page; 400012659251SNaohiro Aota u64 bytenr, bytenr_orig; 40018f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 400212659251SNaohiro Aota int ret; 400329c36d72SAnand Jain 400412659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 400512659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 400612659251SNaohiro Aota if (ret == -ENOENT) 400712659251SNaohiro Aota return ERR_PTR(-EINVAL); 400812659251SNaohiro Aota else if (ret) 400912659251SNaohiro Aota return ERR_PTR(ret); 401012659251SNaohiro Aota 4011cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 40128f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 401329c36d72SAnand Jain 40148f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 40158f32380dSJohannes Thumshirn if (IS_ERR(page)) 40168f32380dSJohannes Thumshirn return ERR_CAST(page); 401729c36d72SAnand Jain 40188f32380dSJohannes Thumshirn super = page_address(page); 401996c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 402096c2e067SAnand Jain btrfs_release_disk_super(super); 402196c2e067SAnand Jain return ERR_PTR(-ENODATA); 402296c2e067SAnand Jain } 402396c2e067SAnand Jain 402412659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 40258f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40268f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 402729c36d72SAnand Jain } 402829c36d72SAnand Jain 40298f32380dSJohannes Thumshirn return super; 403029c36d72SAnand Jain } 403129c36d72SAnand Jain 403229c36d72SAnand Jain 40338f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 4034a512bbf8SYan Zheng { 40358f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 4036a512bbf8SYan Zheng int i; 4037a512bbf8SYan Zheng u64 transid = 0; 4038a512bbf8SYan Zheng 4039a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 4040a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 4041a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 4042a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 4043a512bbf8SYan Zheng */ 4044a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 40458f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 40468f32380dSJohannes Thumshirn if (IS_ERR(super)) 4047a512bbf8SYan Zheng continue; 4048a512bbf8SYan Zheng 4049a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 40508f32380dSJohannes Thumshirn if (latest) 40518f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40528f32380dSJohannes Thumshirn 40538f32380dSJohannes Thumshirn latest = super; 4054a512bbf8SYan Zheng transid = btrfs_super_generation(super); 4055a512bbf8SYan Zheng } 4056a512bbf8SYan Zheng } 405792fc03fbSAnand Jain 40588f32380dSJohannes Thumshirn return super; 4059a512bbf8SYan Zheng } 4060a512bbf8SYan Zheng 40614eedeb75SHisashi Hifumi /* 4062abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 4063314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 40644eedeb75SHisashi Hifumi * 4065abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 4066abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 4067abbb3b8eSDavid Sterba * same for write and wait phases. 40684eedeb75SHisashi Hifumi * 4069314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 40704eedeb75SHisashi Hifumi */ 4071a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 4072abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 4073a512bbf8SYan Zheng { 4074d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 4075314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 4076d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 4077a512bbf8SYan Zheng int i; 4078a512bbf8SYan Zheng int errors = 0; 407912659251SNaohiro Aota int ret; 408012659251SNaohiro Aota u64 bytenr, bytenr_orig; 4081a512bbf8SYan Zheng 4082a512bbf8SYan Zheng if (max_mirrors == 0) 4083a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4084a512bbf8SYan Zheng 4085d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4086d5178578SJohannes Thumshirn 4087a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4088314b6dd0SJohannes Thumshirn struct page *page; 4089314b6dd0SJohannes Thumshirn struct bio *bio; 4090314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4091314b6dd0SJohannes Thumshirn 409212659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 409312659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 409412659251SNaohiro Aota if (ret == -ENOENT) { 409512659251SNaohiro Aota continue; 409612659251SNaohiro Aota } else if (ret < 0) { 409712659251SNaohiro Aota btrfs_err(device->fs_info, 409812659251SNaohiro Aota "couldn't get super block location for mirror %d", 409912659251SNaohiro Aota i); 410012659251SNaohiro Aota errors++; 410112659251SNaohiro Aota continue; 410212659251SNaohiro Aota } 4103935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4104935e5cc9SMiao Xie device->commit_total_bytes) 4105a512bbf8SYan Zheng break; 4106a512bbf8SYan Zheng 410712659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4108a512bbf8SYan Zheng 4109fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4110fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4111fd08001fSEric Biggers sb->csum); 4112a512bbf8SYan Zheng 4113314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4114314b6dd0SJohannes Thumshirn GFP_NOFS); 4115314b6dd0SJohannes Thumshirn if (!page) { 4116fb456252SJeff Mahoney btrfs_err(device->fs_info, 4117314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4118f14d104dSDavid Sterba bytenr); 4119634554dcSJosef Bacik errors++; 4120634554dcSJosef Bacik continue; 4121634554dcSJosef Bacik } 4122634554dcSJosef Bacik 4123314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4124314b6dd0SJohannes Thumshirn get_page(page); 4125a512bbf8SYan Zheng 4126314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4127314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4128a512bbf8SYan Zheng 4129387125fcSChris Mason /* 4130314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4131314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4132314b6dd0SJohannes Thumshirn * checking. 4133387125fcSChris Mason */ 413407888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 413507888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 413607888c66SChristoph Hellwig GFP_NOFS); 4137314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4138314b6dd0SJohannes Thumshirn bio->bi_private = device; 4139314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4140314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4141314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4142314b6dd0SJohannes Thumshirn 4143314b6dd0SJohannes Thumshirn /* 4144314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4145314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4146314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4147314b6dd0SJohannes Thumshirn */ 41481b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4149314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4150314b6dd0SJohannes Thumshirn 415158ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 415258ff51f1SChristoph Hellwig submit_bio(bio); 41538376d9e1SNaohiro Aota 41548376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 41558376d9e1SNaohiro Aota errors++; 4156a512bbf8SYan Zheng } 4157a512bbf8SYan Zheng return errors < i ? 0 : -1; 4158a512bbf8SYan Zheng } 4159a512bbf8SYan Zheng 4160387125fcSChris Mason /* 4161abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4162abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4163abbb3b8eSDavid Sterba * 4164314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4165abbb3b8eSDavid Sterba * date. 4166abbb3b8eSDavid Sterba */ 4167abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4168abbb3b8eSDavid Sterba { 4169abbb3b8eSDavid Sterba int i; 4170abbb3b8eSDavid Sterba int errors = 0; 4171b6a535faSHoward McLauchlan bool primary_failed = false; 417212659251SNaohiro Aota int ret; 4173abbb3b8eSDavid Sterba u64 bytenr; 4174abbb3b8eSDavid Sterba 4175abbb3b8eSDavid Sterba if (max_mirrors == 0) 4176abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4177abbb3b8eSDavid Sterba 4178abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4179314b6dd0SJohannes Thumshirn struct page *page; 4180314b6dd0SJohannes Thumshirn 418112659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 418212659251SNaohiro Aota if (ret == -ENOENT) { 418312659251SNaohiro Aota break; 418412659251SNaohiro Aota } else if (ret < 0) { 418512659251SNaohiro Aota errors++; 418612659251SNaohiro Aota if (i == 0) 418712659251SNaohiro Aota primary_failed = true; 418812659251SNaohiro Aota continue; 418912659251SNaohiro Aota } 4190abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4191abbb3b8eSDavid Sterba device->commit_total_bytes) 4192abbb3b8eSDavid Sterba break; 4193abbb3b8eSDavid Sterba 4194314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4195314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4196314b6dd0SJohannes Thumshirn if (!page) { 4197abbb3b8eSDavid Sterba errors++; 4198b6a535faSHoward McLauchlan if (i == 0) 4199b6a535faSHoward McLauchlan primary_failed = true; 4200abbb3b8eSDavid Sterba continue; 4201abbb3b8eSDavid Sterba } 4202314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4203314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4204314b6dd0SJohannes Thumshirn if (PageError(page)) { 4205abbb3b8eSDavid Sterba errors++; 4206b6a535faSHoward McLauchlan if (i == 0) 4207b6a535faSHoward McLauchlan primary_failed = true; 4208b6a535faSHoward McLauchlan } 4209abbb3b8eSDavid Sterba 4210314b6dd0SJohannes Thumshirn /* Drop our reference */ 4211314b6dd0SJohannes Thumshirn put_page(page); 4212abbb3b8eSDavid Sterba 4213314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4214314b6dd0SJohannes Thumshirn put_page(page); 4215abbb3b8eSDavid Sterba } 4216abbb3b8eSDavid Sterba 4217b6a535faSHoward McLauchlan /* log error, force error return */ 4218b6a535faSHoward McLauchlan if (primary_failed) { 4219b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4220b6a535faSHoward McLauchlan device->devid); 4221b6a535faSHoward McLauchlan return -1; 4222b6a535faSHoward McLauchlan } 4223b6a535faSHoward McLauchlan 4224abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4225abbb3b8eSDavid Sterba } 4226abbb3b8eSDavid Sterba 4227abbb3b8eSDavid Sterba /* 4228387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4229387125fcSChris Mason * for the barrier when it is done 4230387125fcSChris Mason */ 42314246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4232387125fcSChris Mason { 4233f9e69aa9SChristoph Hellwig bio_uninit(bio); 4234387125fcSChris Mason complete(bio->bi_private); 4235387125fcSChris Mason } 4236387125fcSChris Mason 4237387125fcSChris Mason /* 42384fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 42394fc6441aSAnand Jain * done in the waiting counterpart. 4240387125fcSChris Mason */ 42414fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4242387125fcSChris Mason { 4243f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 4244387125fcSChris Mason 4245a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4246a91cf0ffSWang Yugui /* 4247a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4248a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4249a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4250a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4251a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4252a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4253a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4254a91cf0ffSWang Yugui * non-debug builds. 4255a91cf0ffSWang Yugui */ 425608e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev)) 42574fc6441aSAnand Jain return; 4258a91cf0ffSWang Yugui #endif 4259387125fcSChris Mason 4260f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0, 4261f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4262387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4263387125fcSChris Mason init_completion(&device->flush_wait); 4264387125fcSChris Mason bio->bi_private = &device->flush_wait; 4265387125fcSChris Mason 426658ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 426758ff51f1SChristoph Hellwig submit_bio(bio); 42681c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42694fc6441aSAnand Jain } 4270387125fcSChris Mason 42714fc6441aSAnand Jain /* 42724fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 42734fc6441aSAnand Jain */ 42748c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 42754fc6441aSAnand Jain { 4276f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 42774fc6441aSAnand Jain 42781c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 427958efbc9fSOmar Sandoval return BLK_STS_OK; 42804fc6441aSAnand Jain 42811c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42822980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 42834fc6441aSAnand Jain 42848c27cb35SLinus Torvalds return bio->bi_status; 4285387125fcSChris Mason } 4286387125fcSChris Mason 4287d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4288401b41e5SAnand Jain { 42896528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4290401b41e5SAnand Jain return -EIO; 4291387125fcSChris Mason return 0; 4292387125fcSChris Mason } 4293387125fcSChris Mason 4294387125fcSChris Mason /* 4295387125fcSChris Mason * send an empty flush down to each device in parallel, 4296387125fcSChris Mason * then wait for them 4297387125fcSChris Mason */ 4298387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4299387125fcSChris Mason { 4300387125fcSChris Mason struct list_head *head; 4301387125fcSChris Mason struct btrfs_device *dev; 43025af3e8ccSStefan Behrens int errors_wait = 0; 43034e4cbee9SChristoph Hellwig blk_status_t ret; 4304387125fcSChris Mason 43051538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4306387125fcSChris Mason /* send down all the barriers */ 4307387125fcSChris Mason head = &info->fs_devices->devices; 43081538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4309e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4310f88ba6a2SHidetoshi Seto continue; 4311cea7c8bfSAnand Jain if (!dev->bdev) 4312387125fcSChris Mason continue; 4313e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4314ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4315387125fcSChris Mason continue; 4316387125fcSChris Mason 43174fc6441aSAnand Jain write_dev_flush(dev); 431858efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4319387125fcSChris Mason } 4320387125fcSChris Mason 4321387125fcSChris Mason /* wait for all the barriers */ 43221538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4323e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4324f88ba6a2SHidetoshi Seto continue; 4325387125fcSChris Mason if (!dev->bdev) { 43265af3e8ccSStefan Behrens errors_wait++; 4327387125fcSChris Mason continue; 4328387125fcSChris Mason } 4329e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4330ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4331387125fcSChris Mason continue; 4332387125fcSChris Mason 43334fc6441aSAnand Jain ret = wait_dev_flush(dev); 4334401b41e5SAnand Jain if (ret) { 4335401b41e5SAnand Jain dev->last_flush_error = ret; 433666b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 433766b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 43385af3e8ccSStefan Behrens errors_wait++; 4339387125fcSChris Mason } 4340401b41e5SAnand Jain } 4341401b41e5SAnand Jain 4342cea7c8bfSAnand Jain if (errors_wait) { 4343401b41e5SAnand Jain /* 4344401b41e5SAnand Jain * At some point we need the status of all disks 4345401b41e5SAnand Jain * to arrive at the volume status. So error checking 4346401b41e5SAnand Jain * is being pushed to a separate loop. 4347401b41e5SAnand Jain */ 4348d10b82feSQu Wenruo return check_barrier_error(info); 4349401b41e5SAnand Jain } 4350387125fcSChris Mason return 0; 4351387125fcSChris Mason } 4352387125fcSChris Mason 4353943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4354943c6e99SZhao Lei { 43558789f4feSZhao Lei int raid_type; 43568789f4feSZhao Lei int min_tolerated = INT_MAX; 4357943c6e99SZhao Lei 43588789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 43598789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 43608c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43618789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 43628789f4feSZhao Lei tolerated_failures); 4363943c6e99SZhao Lei 43648789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 43658789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 43668789f4feSZhao Lei continue; 436741a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 43688789f4feSZhao Lei continue; 43698c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43708789f4feSZhao Lei btrfs_raid_array[raid_type]. 43718789f4feSZhao Lei tolerated_failures); 43728789f4feSZhao Lei } 4373943c6e99SZhao Lei 43748789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4375ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 43768789f4feSZhao Lei min_tolerated = 0; 43778789f4feSZhao Lei } 43788789f4feSZhao Lei 43798789f4feSZhao Lei return min_tolerated; 4380943c6e99SZhao Lei } 4381943c6e99SZhao Lei 4382eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4383f2984462SChris Mason { 4384e5e9a520SChris Mason struct list_head *head; 4385f2984462SChris Mason struct btrfs_device *dev; 4386a061fc8dSChris Mason struct btrfs_super_block *sb; 4387f2984462SChris Mason struct btrfs_dev_item *dev_item; 4388f2984462SChris Mason int ret; 4389f2984462SChris Mason int do_barriers; 4390a236aed1SChris Mason int max_errors; 4391a236aed1SChris Mason int total_errors = 0; 4392a061fc8dSChris Mason u64 flags; 4393f2984462SChris Mason 43940b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4395fed3b381SLiu Bo 4396fed3b381SLiu Bo /* 4397fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4398fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4399fed3b381SLiu Bo * been consistent on disk. 4400fed3b381SLiu Bo */ 4401fed3b381SLiu Bo if (max_mirrors == 0) 44020b246afaSJeff Mahoney backup_super_roots(fs_info); 4403f2984462SChris Mason 44040b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4405a061fc8dSChris Mason dev_item = &sb->dev_item; 4406e5e9a520SChris Mason 44070b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 44080b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 44090b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4410387125fcSChris Mason 44115af3e8ccSStefan Behrens if (do_barriers) { 44120b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 44135af3e8ccSStefan Behrens if (ret) { 44145af3e8ccSStefan Behrens mutex_unlock( 44150b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 44160b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 44175af3e8ccSStefan Behrens "errors while submitting device barriers."); 44185af3e8ccSStefan Behrens return ret; 44195af3e8ccSStefan Behrens } 44205af3e8ccSStefan Behrens } 4421387125fcSChris Mason 44221538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4423dfe25020SChris Mason if (!dev->bdev) { 4424dfe25020SChris Mason total_errors++; 4425dfe25020SChris Mason continue; 4426dfe25020SChris Mason } 4427e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4428ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4429dfe25020SChris Mason continue; 4430dfe25020SChris Mason 44312b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4432a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4433a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 44347df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4435935e5cc9SMiao Xie dev->commit_total_bytes); 4436ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4437ce7213c7SMiao Xie dev->commit_bytes_used); 4438a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4439a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4440a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4441a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 44427239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 44437239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4444a512bbf8SYan Zheng 4445a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4446a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4447f2984462SChris Mason 444875cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 444975cb857dSQu Wenruo if (ret < 0) { 445075cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 445175cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 445275cb857dSQu Wenruo "unexpected superblock corruption detected"); 445375cb857dSQu Wenruo return -EUCLEAN; 445475cb857dSQu Wenruo } 445575cb857dSQu Wenruo 4456abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4457a236aed1SChris Mason if (ret) 4458a236aed1SChris Mason total_errors++; 4459f2984462SChris Mason } 4460a236aed1SChris Mason if (total_errors > max_errors) { 44610b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4462d397712bSChris Mason total_errors); 44630b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 446479787eaaSJeff Mahoney 44659d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 44660b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44670b246afaSJeff Mahoney "%d errors while writing supers", 44680b246afaSJeff Mahoney total_errors); 44699d565ba4SStefan Behrens return -EIO; 4470a236aed1SChris Mason } 4471f2984462SChris Mason 4472a512bbf8SYan Zheng total_errors = 0; 44731538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4474dfe25020SChris Mason if (!dev->bdev) 4475dfe25020SChris Mason continue; 4476e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4477ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4478dfe25020SChris Mason continue; 4479dfe25020SChris Mason 4480abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4481a512bbf8SYan Zheng if (ret) 44821259ab75SChris Mason total_errors++; 4483f2984462SChris Mason } 44840b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4485a236aed1SChris Mason if (total_errors > max_errors) { 44860b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44870b246afaSJeff Mahoney "%d errors while writing supers", 44880b246afaSJeff Mahoney total_errors); 448979787eaaSJeff Mahoney return -EIO; 4490a236aed1SChris Mason } 4491f2984462SChris Mason return 0; 4492f2984462SChris Mason } 4493f2984462SChris Mason 4494cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4495cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4496cb517eabSMiao Xie struct btrfs_root *root) 44972619ba1fSChris Mason { 44984785e24fSJosef Bacik bool drop_ref = false; 44994785e24fSJosef Bacik 450048b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 450148b36a60SGabriel Niebler xa_erase(&fs_info->fs_roots, (unsigned long)root->root_key.objectid); 450248b36a60SGabriel Niebler if (test_and_clear_bit(BTRFS_ROOT_REGISTERED, &root->state)) 45034785e24fSJosef Bacik drop_ref = true; 450448b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 450576dda93cSYan, Zheng 450684961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4507ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 45081c1ea4f7SLiu Bo if (root->reloc_root) { 450900246528SJosef Bacik btrfs_put_root(root->reloc_root); 45101c1ea4f7SLiu Bo root->reloc_root = NULL; 45111c1ea4f7SLiu Bo } 45121c1ea4f7SLiu Bo } 45133321719eSLiu Bo 45144785e24fSJosef Bacik if (drop_ref) 451500246528SJosef Bacik btrfs_put_root(root); 45162619ba1fSChris Mason } 45172619ba1fSChris Mason 4518c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4519c146afadSYan Zheng { 452048b36a60SGabriel Niebler struct btrfs_root *roots[8]; 452148b36a60SGabriel Niebler unsigned long index = 0; 452248b36a60SGabriel Niebler int i; 452365d33fd7SQu Wenruo int err = 0; 452448b36a60SGabriel Niebler int grabbed; 4525c146afadSYan Zheng 4526c146afadSYan Zheng while (1) { 452748b36a60SGabriel Niebler struct btrfs_root *root; 452848b36a60SGabriel Niebler 452948b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 453048b36a60SGabriel Niebler if (!xa_find(&fs_info->fs_roots, &index, ULONG_MAX, XA_PRESENT)) { 453148b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 453248b36a60SGabriel Niebler return err; 453348b36a60SGabriel Niebler } 453448b36a60SGabriel Niebler 453548b36a60SGabriel Niebler grabbed = 0; 453648b36a60SGabriel Niebler xa_for_each_start(&fs_info->fs_roots, index, root, index) { 453748b36a60SGabriel Niebler /* Avoid grabbing roots in dead_roots */ 453848b36a60SGabriel Niebler if (btrfs_root_refs(&root->root_item) > 0) 453948b36a60SGabriel Niebler roots[grabbed++] = btrfs_grab_root(root); 454048b36a60SGabriel Niebler if (grabbed >= ARRAY_SIZE(roots)) 4541c146afadSYan Zheng break; 454265d33fd7SQu Wenruo } 454348b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 454466b4ffd1SJosef Bacik 454548b36a60SGabriel Niebler for (i = 0; i < grabbed; i++) { 454648b36a60SGabriel Niebler if (!roots[i]) 454765d33fd7SQu Wenruo continue; 454848b36a60SGabriel Niebler index = roots[i]->root_key.objectid; 454948b36a60SGabriel Niebler err = btrfs_orphan_cleanup(roots[i]); 455066b4ffd1SJosef Bacik if (err) 455148b36a60SGabriel Niebler goto out; 455248b36a60SGabriel Niebler btrfs_put_root(roots[i]); 4553c146afadSYan Zheng } 455448b36a60SGabriel Niebler index++; 4555c146afadSYan Zheng } 455665d33fd7SQu Wenruo 455748b36a60SGabriel Niebler out: 455848b36a60SGabriel Niebler /* Release the roots that remain uncleaned due to error */ 455948b36a60SGabriel Niebler for (; i < grabbed; i++) { 456048b36a60SGabriel Niebler if (roots[i]) 456148b36a60SGabriel Niebler btrfs_put_root(roots[i]); 456265d33fd7SQu Wenruo } 456365d33fd7SQu Wenruo return err; 4564c146afadSYan Zheng } 4565c146afadSYan Zheng 45666bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4567c146afadSYan Zheng { 45686bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4569c146afadSYan Zheng struct btrfs_trans_handle *trans; 4570c146afadSYan Zheng 45710b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 45722ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 45730b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 45740b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4575c71bf099SYan, Zheng 4576c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 45770b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45780b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4579c71bf099SYan, Zheng 45807a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 45813612b495STsutomu Itoh if (IS_ERR(trans)) 45823612b495STsutomu Itoh return PTR_ERR(trans); 45833a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4584c146afadSYan Zheng } 4585c146afadSYan Zheng 458636c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 458736c86a9eSQu Wenruo { 458836c86a9eSQu Wenruo struct btrfs_transaction *trans; 458936c86a9eSQu Wenruo struct btrfs_transaction *tmp; 459036c86a9eSQu Wenruo bool found = false; 459136c86a9eSQu Wenruo 459236c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 459336c86a9eSQu Wenruo return; 459436c86a9eSQu Wenruo 459536c86a9eSQu Wenruo /* 459636c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 459736c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 459836c86a9eSQu Wenruo */ 459936c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 460036c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 460136c86a9eSQu Wenruo struct extent_state *cached = NULL; 460236c86a9eSQu Wenruo u64 dirty_bytes = 0; 460336c86a9eSQu Wenruo u64 cur = 0; 460436c86a9eSQu Wenruo u64 found_start; 460536c86a9eSQu Wenruo u64 found_end; 460636c86a9eSQu Wenruo 460736c86a9eSQu Wenruo found = true; 460836c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 460936c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 461036c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 461136c86a9eSQu Wenruo cur = found_end + 1; 461236c86a9eSQu Wenruo } 461336c86a9eSQu Wenruo btrfs_warn(fs_info, 461436c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 461536c86a9eSQu Wenruo trans->transid, dirty_bytes); 461636c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 461736c86a9eSQu Wenruo 461836c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 461936c86a9eSQu Wenruo fs_info->running_transaction = NULL; 462036c86a9eSQu Wenruo list_del_init(&trans->list); 462136c86a9eSQu Wenruo 462236c86a9eSQu Wenruo btrfs_put_transaction(trans); 462336c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 462436c86a9eSQu Wenruo } 462536c86a9eSQu Wenruo ASSERT(!found); 462636c86a9eSQu Wenruo } 462736c86a9eSQu Wenruo 4628b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4629eb60ceacSChris Mason { 4630c146afadSYan Zheng int ret; 4631e089f05cSChris Mason 4632afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 463331e70e52SFilipe Manana 463431e70e52SFilipe Manana /* 463531e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group, 463631e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park 463731e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after 463831e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang 463931e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is 464031e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when 464131e70e52SFilipe Manana * trying to stop the async reclaim task. 464231e70e52SFilipe Manana */ 464331e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work); 4644d6fd0ae2SOmar Sandoval /* 4645d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4646d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4647d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4648d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4649d6fd0ae2SOmar Sandoval */ 4650d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4651a2135011SChris Mason 4652b4be6aefSJosef Bacik /* 4653b4be6aefSJosef Bacik * If we had UNFINISHED_DROPS we could still be processing them, so 4654b4be6aefSJosef Bacik * clear that bit and wake up relocation so it can stop. 4655b4be6aefSJosef Bacik */ 4656b4be6aefSJosef Bacik btrfs_wake_unfinished_drop(fs_info); 4657b4be6aefSJosef Bacik 46587343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4659d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 46607343dd61SJustin Maggard 4661803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4662803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4663803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4664803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4665803b2f54SStefan Behrens 4666837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4667aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4668837d5b6eSIlya Dryomov 46698dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 46708dabb742SStefan Behrens 4671aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 46724cb5300bSChris Mason 46734cb5300bSChris Mason /* wait for any defraggers to finish */ 46744cb5300bSChris Mason wait_event(fs_info->transaction_wait, 46754cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 46764cb5300bSChris Mason 46774cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 467826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 46794cb5300bSChris Mason 468021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 468157056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4682576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 468321c7e756SMiao Xie 4684b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4685b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4686b0643e59SDennis Zhou 4687bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4688e44163e1SJeff Mahoney /* 4689d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4690d6fd0ae2SOmar Sandoval * unused block groups. 4691e44163e1SJeff Mahoney */ 46920b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4693e44163e1SJeff Mahoney 4694f0cc2cd7SFilipe Manana /* 4695f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4696f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4697f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4698f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4699f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4700f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4701f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4702f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4703f0cc2cd7SFilipe Manana * This is a very rare case. 4704f0cc2cd7SFilipe Manana */ 4705f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4706f0cc2cd7SFilipe Manana 47076bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4708d397712bSChris Mason if (ret) 470904892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4710c146afadSYan Zheng } 4711ed2ff2cbSChris Mason 471284961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 47132ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4714acce952bSliubo 4715e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4716e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 47178929ecfaSYan, Zheng 4718e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4719afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4720f25784b3SYan Zheng 47215958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 47225958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 47235958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 47245958253cSQu Wenruo } 47255958253cSQu Wenruo 472604892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4727fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4728bcef60f2SArne Jansen 4729963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 473004892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4731963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4732b0c68f8bSChris Mason } 473331153d81SYan Zheng 47345deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 47354297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 47365deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 47374297ff84SJosef Bacik 47386618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4739b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 47405ac1d209SJeff Mahoney 47411a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 47421a4319ccSLiu Bo 4743de348ee0SWang Shilong /* 4744de348ee0SWang Shilong * we must make sure there is not any read request to 4745de348ee0SWang Shilong * submit after we stopping all workers. 4746de348ee0SWang Shilong */ 4747de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 474896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 474996192499SJosef Bacik 47500a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 475136c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 47520a31daa4SFilipe Manana 4753afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 47544273eaffSAnand Jain free_root_pointers(fs_info, true); 47558c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 47569ad6b7bcSChris Mason 47574e19443dSJosef Bacik /* 47584e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 47594e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 47604e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 47614e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 47624e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 47634e19443dSJosef Bacik */ 47644e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 47654e19443dSJosef Bacik 476613e6c37bSJosef Bacik iput(fs_info->btree_inode); 4767d6bfde87SChris Mason 476821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 47690b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 47702ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 477121adbd5cSStefan Behrens #endif 477221adbd5cSStefan Behrens 47730b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 477468c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4775eb60ceacSChris Mason } 4776eb60ceacSChris Mason 4777b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4778b9fab919SChris Mason int atomic) 4779ccd467d6SChris Mason { 47801259ab75SChris Mason int ret; 4781727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 47821259ab75SChris Mason 47830b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 47841259ab75SChris Mason if (!ret) 47851259ab75SChris Mason return ret; 47861259ab75SChris Mason 47871259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4788b9fab919SChris Mason parent_transid, atomic); 4789b9fab919SChris Mason if (ret == -EAGAIN) 4790b9fab919SChris Mason return ret; 47911259ab75SChris Mason return !ret; 47925f39d397SChris Mason } 47936702ed49SChris Mason 47945f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 47955f39d397SChris Mason { 47962f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 47975f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4798b9473439SChris Mason int was_dirty; 4799b4ce94deSChris Mason 480006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 480106ea65a3SJosef Bacik /* 480206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 480352042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 480406ea65a3SJosef Bacik * outside of the sanity tests. 480506ea65a3SJosef Bacik */ 4806b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 480706ea65a3SJosef Bacik return; 480806ea65a3SJosef Bacik #endif 480949d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 48100b246afaSJeff Mahoney if (transid != fs_info->generation) 48115d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 48120b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 48130b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4814e2d84521SMiao Xie if (!was_dirty) 4815104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4816e2d84521SMiao Xie buf->len, 48170b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 48181f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 481969fc6cbbSQu Wenruo /* 482069fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 482169fc6cbbSQu Wenruo * but item data not updated. 482269fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 482369fc6cbbSQu Wenruo */ 482469fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4825cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4826a4f78750SDavid Sterba btrfs_print_leaf(buf); 48271f21ef0aSFilipe Manana ASSERT(0); 48281f21ef0aSFilipe Manana } 48291f21ef0aSFilipe Manana #endif 4830eb60ceacSChris Mason } 4831eb60ceacSChris Mason 48322ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4833b53d3f5dSLiu Bo int flush_delayed) 483435b7e476SChris Mason { 4835188de649SChris Mason /* 4836188de649SChris Mason * looks as though older kernels can get into trouble with 4837188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4838188de649SChris Mason */ 4839e2d84521SMiao Xie int ret; 4840d6bfde87SChris Mason 48416933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4842d6bfde87SChris Mason return; 4843d6bfde87SChris Mason 4844b53d3f5dSLiu Bo if (flush_delayed) 48452ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 484616cdcec7SMiao Xie 4847d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4848d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4849d814a491SEthan Lien fs_info->dirty_metadata_batch); 4850e2d84521SMiao Xie if (ret > 0) { 48510b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 485216cdcec7SMiao Xie } 485316cdcec7SMiao Xie } 485416cdcec7SMiao Xie 48552ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 485616cdcec7SMiao Xie { 48572ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 485835b7e476SChris Mason } 4859b53d3f5dSLiu Bo 48602ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4861b53d3f5dSLiu Bo { 48622ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4863d6bfde87SChris Mason } 48646b80053dSChris Mason 48652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4866acce952bSliubo { 4867fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4868fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4869fe816d0fSNikolay Borisov 48700b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 48712ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 48720b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4873acce952bSliubo 48740b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 48750b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4876acce952bSliubo } 4877acce952bSliubo 4878ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4879ef67963dSJosef Bacik { 488048b36a60SGabriel Niebler unsigned long index = 0; 488148b36a60SGabriel Niebler int grabbed = 0; 488248b36a60SGabriel Niebler struct btrfs_root *roots[8]; 4883ef67963dSJosef Bacik 488448b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 488548b36a60SGabriel Niebler while ((grabbed = xa_extract(&fs_info->fs_roots, (void **)roots, index, 488648b36a60SGabriel Niebler ULONG_MAX, 8, XA_PRESENT))) { 488748b36a60SGabriel Niebler for (int i = 0; i < grabbed; i++) 488848b36a60SGabriel Niebler roots[i] = btrfs_grab_root(roots[i]); 488948b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 4890ef67963dSJosef Bacik 489148b36a60SGabriel Niebler for (int i = 0; i < grabbed; i++) { 489248b36a60SGabriel Niebler if (!roots[i]) 4893ef67963dSJosef Bacik continue; 489448b36a60SGabriel Niebler index = roots[i]->root_key.objectid; 489548b36a60SGabriel Niebler btrfs_free_log(NULL, roots[i]); 489648b36a60SGabriel Niebler btrfs_put_root(roots[i]); 4897ef67963dSJosef Bacik } 489848b36a60SGabriel Niebler index++; 489948b36a60SGabriel Niebler spin_lock(&fs_info->fs_roots_lock); 4900ef67963dSJosef Bacik } 490148b36a60SGabriel Niebler spin_unlock(&fs_info->fs_roots_lock); 4902ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4903ef67963dSJosef Bacik } 4904ef67963dSJosef Bacik 4905143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4906acce952bSliubo { 4907acce952bSliubo struct btrfs_ordered_extent *ordered; 4908acce952bSliubo 4909199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4910779880efSJosef Bacik /* 4911779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4912779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4913779880efSJosef Bacik */ 4914199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4915779880efSJosef Bacik root_extent_list) 4916779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4917199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4918199c2a9cSMiao Xie } 4919199c2a9cSMiao Xie 4920199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4921199c2a9cSMiao Xie { 4922199c2a9cSMiao Xie struct btrfs_root *root; 4923199c2a9cSMiao Xie struct list_head splice; 4924199c2a9cSMiao Xie 4925199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4926199c2a9cSMiao Xie 4927199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4928199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4929199c2a9cSMiao Xie while (!list_empty(&splice)) { 4930199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4931199c2a9cSMiao Xie ordered_root); 49321de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 49331de2cfdeSJosef Bacik &fs_info->ordered_roots); 4934199c2a9cSMiao Xie 49352a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4936199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4937199c2a9cSMiao Xie 49382a85d9caSLiu Bo cond_resched(); 49392a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4940199c2a9cSMiao Xie } 4941199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 494274d5d229SJosef Bacik 494374d5d229SJosef Bacik /* 494474d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 494574d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 494674d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 494774d5d229SJosef Bacik * actually get run and error out properly. 494874d5d229SJosef Bacik */ 494974d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4950acce952bSliubo } 4951acce952bSliubo 495235a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 49532ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4954acce952bSliubo { 4955acce952bSliubo struct rb_node *node; 4956acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4957acce952bSliubo struct btrfs_delayed_ref_node *ref; 4958acce952bSliubo int ret = 0; 4959acce952bSliubo 4960acce952bSliubo delayed_refs = &trans->delayed_refs; 4961acce952bSliubo 4962acce952bSliubo spin_lock(&delayed_refs->lock); 4963d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4964cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4965b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4966acce952bSliubo return ret; 4967acce952bSliubo } 4968acce952bSliubo 49695c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4970d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 49710e0adbcfSJosef Bacik struct rb_node *n; 4972e78417d1SJosef Bacik bool pin_bytes = false; 4973acce952bSliubo 4974d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4975d7df2c79SJosef Bacik href_node); 49763069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4977b939d1abSJosef Bacik continue; 49783069bd26SJosef Bacik 4979d7df2c79SJosef Bacik spin_lock(&head->lock); 4980e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 49810e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 49820e0adbcfSJosef Bacik ref_node); 4983d7df2c79SJosef Bacik ref->in_tree = 0; 4984e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 49850e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 49861d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 49871d57ee94SWang Xiaoguang list_del(&ref->add_list); 4988d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4989d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4990d7df2c79SJosef Bacik } 499154067ae9SJosef Bacik if (head->must_insert_reserved) 4992e78417d1SJosef Bacik pin_bytes = true; 499378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4994fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4995d7df2c79SJosef Bacik spin_unlock(&head->lock); 4996acce952bSliubo spin_unlock(&delayed_refs->lock); 4997e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4998acce952bSliubo 4999f603bb94SNikolay Borisov if (pin_bytes) { 5000f603bb94SNikolay Borisov struct btrfs_block_group *cache; 5001f603bb94SNikolay Borisov 5002f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 5003f603bb94SNikolay Borisov BUG_ON(!cache); 5004f603bb94SNikolay Borisov 5005f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 5006f603bb94SNikolay Borisov spin_lock(&cache->lock); 5007f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 5008f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 5009f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 5010f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 5011f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 5012f603bb94SNikolay Borisov spin_unlock(&cache->lock); 5013f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 5014f603bb94SNikolay Borisov 5015f603bb94SNikolay Borisov btrfs_put_block_group(cache); 5016f603bb94SNikolay Borisov 5017f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 5018f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 5019f603bb94SNikolay Borisov } 502031890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 5021d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 5022acce952bSliubo cond_resched(); 5023acce952bSliubo spin_lock(&delayed_refs->lock); 5024acce952bSliubo } 502581f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 5026acce952bSliubo 5027acce952bSliubo spin_unlock(&delayed_refs->lock); 5028acce952bSliubo 5029acce952bSliubo return ret; 5030acce952bSliubo } 5031acce952bSliubo 5032143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 5033acce952bSliubo { 5034acce952bSliubo struct btrfs_inode *btrfs_inode; 5035acce952bSliubo struct list_head splice; 5036acce952bSliubo 5037acce952bSliubo INIT_LIST_HEAD(&splice); 5038acce952bSliubo 5039eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5040eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 5041acce952bSliubo 5042acce952bSliubo while (!list_empty(&splice)) { 5043fe816d0fSNikolay Borisov struct inode *inode = NULL; 5044eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 5045acce952bSliubo delalloc_inodes); 5046fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 5047eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5048acce952bSliubo 5049fe816d0fSNikolay Borisov /* 5050fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 5051fe816d0fSNikolay Borisov * meanwhile. 5052fe816d0fSNikolay Borisov */ 5053fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 5054fe816d0fSNikolay Borisov if (inode) { 5055fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 5056fe816d0fSNikolay Borisov iput(inode); 5057fe816d0fSNikolay Borisov } 5058eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5059acce952bSliubo } 5060eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5061eb73c1b7SMiao Xie } 5062eb73c1b7SMiao Xie 5063eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 5064eb73c1b7SMiao Xie { 5065eb73c1b7SMiao Xie struct btrfs_root *root; 5066eb73c1b7SMiao Xie struct list_head splice; 5067eb73c1b7SMiao Xie 5068eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 5069eb73c1b7SMiao Xie 5070eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5071eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 5072eb73c1b7SMiao Xie while (!list_empty(&splice)) { 5073eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 5074eb73c1b7SMiao Xie delalloc_root); 507500246528SJosef Bacik root = btrfs_grab_root(root); 5076eb73c1b7SMiao Xie BUG_ON(!root); 5077eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5078eb73c1b7SMiao Xie 5079eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 508000246528SJosef Bacik btrfs_put_root(root); 5081eb73c1b7SMiao Xie 5082eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5083eb73c1b7SMiao Xie } 5084eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5085acce952bSliubo } 5086acce952bSliubo 50872ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5088acce952bSliubo struct extent_io_tree *dirty_pages, 5089acce952bSliubo int mark) 5090acce952bSliubo { 5091acce952bSliubo int ret; 5092acce952bSliubo struct extent_buffer *eb; 5093acce952bSliubo u64 start = 0; 5094acce952bSliubo u64 end; 5095acce952bSliubo 5096acce952bSliubo while (1) { 5097acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5098e6138876SJosef Bacik mark, NULL); 5099acce952bSliubo if (ret) 5100acce952bSliubo break; 5101acce952bSliubo 510291166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5103acce952bSliubo while (start <= end) { 51040b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 51050b246afaSJeff Mahoney start += fs_info->nodesize; 5106fd8b2b61SJosef Bacik if (!eb) 5107acce952bSliubo continue; 5108fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5109acce952bSliubo 5110fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5111fd8b2b61SJosef Bacik &eb->bflags)) 5112fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5113fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5114acce952bSliubo } 5115acce952bSliubo } 5116acce952bSliubo 5117acce952bSliubo return ret; 5118acce952bSliubo } 5119acce952bSliubo 51202ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5121fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5122acce952bSliubo { 5123acce952bSliubo u64 start; 5124acce952bSliubo u64 end; 5125acce952bSliubo int ret; 5126acce952bSliubo 5127acce952bSliubo while (1) { 51280e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 51290e6ec385SFilipe Manana 5130fcd5e742SLu Fengqi /* 5131fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5132fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5133fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5134fcd5e742SLu Fengqi * the same extent range. 5135fcd5e742SLu Fengqi */ 5136fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5137acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 51380e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5139fcd5e742SLu Fengqi if (ret) { 5140fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5141acce952bSliubo break; 5142fcd5e742SLu Fengqi } 5143acce952bSliubo 51440e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 51450e6ec385SFilipe Manana free_extent_state(cached_state); 51462ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5147fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5148acce952bSliubo cond_resched(); 5149acce952bSliubo } 5150acce952bSliubo 5151acce952bSliubo return 0; 5152acce952bSliubo } 5153acce952bSliubo 515432da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5155c79a1751SLiu Bo { 5156c79a1751SLiu Bo struct inode *inode; 5157c79a1751SLiu Bo 5158c79a1751SLiu Bo inode = cache->io_ctl.inode; 5159c79a1751SLiu Bo if (inode) { 5160c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5161c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5162c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5163c79a1751SLiu Bo iput(inode); 5164c79a1751SLiu Bo } 5165bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5166c79a1751SLiu Bo btrfs_put_block_group(cache); 5167c79a1751SLiu Bo } 5168c79a1751SLiu Bo 5169c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 51702ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5171c79a1751SLiu Bo { 517232da5386SDavid Sterba struct btrfs_block_group *cache; 5173c79a1751SLiu Bo 5174c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5175c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5176c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 517732da5386SDavid Sterba struct btrfs_block_group, 5178c79a1751SLiu Bo dirty_list); 5179c79a1751SLiu Bo 5180c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5181c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5182c79a1751SLiu Bo list_del_init(&cache->io_list); 5183c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5184c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5185c79a1751SLiu Bo } 5186c79a1751SLiu Bo 5187c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5188c79a1751SLiu Bo spin_lock(&cache->lock); 5189c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5190c79a1751SLiu Bo spin_unlock(&cache->lock); 5191c79a1751SLiu Bo 5192c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5193c79a1751SLiu Bo btrfs_put_block_group(cache); 5194ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5195c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5196c79a1751SLiu Bo } 5197c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5198c79a1751SLiu Bo 519945ae2c18SNikolay Borisov /* 520045ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 520145ae2c18SNikolay Borisov * to use it without any locking 520245ae2c18SNikolay Borisov */ 5203c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5204c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 520532da5386SDavid Sterba struct btrfs_block_group, 5206c79a1751SLiu Bo io_list); 5207c79a1751SLiu Bo 5208c79a1751SLiu Bo list_del_init(&cache->io_list); 5209c79a1751SLiu Bo spin_lock(&cache->lock); 5210c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5211c79a1751SLiu Bo spin_unlock(&cache->lock); 5212c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5213c79a1751SLiu Bo } 5214c79a1751SLiu Bo } 5215c79a1751SLiu Bo 521649b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 52172ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 521849b25e05SJeff Mahoney { 5219bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5220bbbf7243SNikolay Borisov 52212ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5222c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5223c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5224c79a1751SLiu Bo 5225bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5226bbbf7243SNikolay Borisov post_commit_list) { 5227bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5228bbbf7243SNikolay Borisov } 5229bbbf7243SNikolay Borisov 52302ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 523149b25e05SJeff Mahoney 52324a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 52330b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 523449b25e05SJeff Mahoney 52354a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 52360b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 523749b25e05SJeff Mahoney 5238ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 523967cde344SMiao Xie 52402ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 524149b25e05SJeff Mahoney EXTENT_DIRTY); 5242fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 524349b25e05SJeff Mahoney 5244d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5245d3575156SNaohiro Aota 52464a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 52474a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 524849b25e05SJeff Mahoney } 524949b25e05SJeff Mahoney 52502ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5251acce952bSliubo { 5252acce952bSliubo struct btrfs_transaction *t; 5253acce952bSliubo 52540b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5255acce952bSliubo 52560b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52570b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 52580b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5259724e2315SJosef Bacik struct btrfs_transaction, list); 5260724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 52619b64f57dSElena Reshetova refcount_inc(&t->use_count); 52620b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52632ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5264724e2315SJosef Bacik btrfs_put_transaction(t); 52650b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5266724e2315SJosef Bacik continue; 5267724e2315SJosef Bacik } 52680b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5269724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 52700b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5271724e2315SJosef Bacik /* 5272724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5273724e2315SJosef Bacik * handle open currently for this transaction. 5274724e2315SJosef Bacik */ 5275724e2315SJosef Bacik wait_event(t->writer_wait, 5276724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5277724e2315SJosef Bacik } else { 52780b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5279724e2315SJosef Bacik } 52802ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5281724e2315SJosef Bacik 52820b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52830b246afaSJeff Mahoney if (t == fs_info->running_transaction) 52840b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5285724e2315SJosef Bacik list_del_init(&t->list); 52860b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5287a4abeea4SJosef Bacik 5288724e2315SJosef Bacik btrfs_put_transaction(t); 52892e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 52900b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5291724e2315SJosef Bacik } 52920b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52930b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5294ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5295ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 52960b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5297ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 52980b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5299acce952bSliubo 5300acce952bSliubo return 0; 5301acce952bSliubo } 5302ec7d6dfdSNikolay Borisov 5303453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5304ec7d6dfdSNikolay Borisov { 5305ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5306ec7d6dfdSNikolay Borisov int ret; 5307ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5308ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5309ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5310ec7d6dfdSNikolay Borisov int slot; 5311ec7d6dfdSNikolay Borisov 5312ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5313ec7d6dfdSNikolay Borisov if (!path) 5314ec7d6dfdSNikolay Borisov return -ENOMEM; 5315ec7d6dfdSNikolay Borisov 5316ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5317ec7d6dfdSNikolay Borisov search_key.type = -1; 5318ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5319ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5320ec7d6dfdSNikolay Borisov if (ret < 0) 5321ec7d6dfdSNikolay Borisov goto error; 5322ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5323ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5324ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5325ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5326ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 532723125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 532823125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5329ec7d6dfdSNikolay Borisov } else { 533023125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5331ec7d6dfdSNikolay Borisov } 5332ec7d6dfdSNikolay Borisov ret = 0; 5333ec7d6dfdSNikolay Borisov error: 5334ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5335ec7d6dfdSNikolay Borisov return ret; 5336ec7d6dfdSNikolay Borisov } 5337ec7d6dfdSNikolay Borisov 5338543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5339ec7d6dfdSNikolay Borisov { 5340ec7d6dfdSNikolay Borisov int ret; 5341ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5342ec7d6dfdSNikolay Borisov 53436b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5344ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5345ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5346ec7d6dfdSNikolay Borisov root->root_key.objectid); 5347ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5348ec7d6dfdSNikolay Borisov goto out; 5349ec7d6dfdSNikolay Borisov } 5350ec7d6dfdSNikolay Borisov 535123125104SNikolay Borisov *objectid = root->free_objectid++; 5352ec7d6dfdSNikolay Borisov ret = 0; 5353ec7d6dfdSNikolay Borisov out: 5354ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5355ec7d6dfdSNikolay Borisov return ret; 5356ec7d6dfdSNikolay Borisov } 5357