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> 80f7d52f4SChris Mason #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10ce9adaa5SChris Mason #include <linux/workqueue.h> 11a74a4b97SChris Mason #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13784b4e29SChris Mason #include <linux/migrate.h> 147a36ddecSDavid Sterba #include <linux/ratelimit.h> 156463fe58SStefan Behrens #include <linux/uuid.h> 16803b2f54SStefan Behrens #include <linux/semaphore.h> 17540adea3SMasami Hiramatsu #include <linux/error-injection.h> 189678c543SNikolay Borisov #include <linux/crc32c.h> 19b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 207e75bf3fSDavid Sterba #include <asm/unaligned.h> 216d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 3221adbd5cSStefan Behrens #include "check-integrity.h" 33606686eeSJosef Bacik #include "rcu-string.h" 348dabb742SStefan Behrens #include "dev-replace.h" 3553b381b3SDavid Woodhouse #include "raid56.h" 365ac1d209SJeff Mahoney #include "sysfs.h" 37fcebe456SJosef Bacik #include "qgroup.h" 38ebb8765bSAnand Jain #include "compression.h" 39557ea5ddSQu Wenruo #include "tree-checker.h" 40fd708b81SJosef Bacik #include "ref-verify.h" 41aac0023cSJosef Bacik #include "block-group.h" 42b0643e59SDennis Zhou #include "discard.h" 43f603bb94SNikolay Borisov #include "space-info.h" 44b70f5097SNaohiro Aota #include "zoned.h" 45139e8cd3SQu Wenruo #include "subpage.h" 46eb60ceacSChris Mason 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 548b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 55143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 56acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 58143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 592ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 60acce952bSliubo struct extent_io_tree *dirty_pages, 61acce952bSliubo int mark); 622ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 63acce952bSliubo struct extent_io_tree *pinned_extents); 642ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 66ce9adaa5SChris Mason 67d352ac68SChris Mason /* 6897eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6997eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 70d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 71d352ac68SChris Mason */ 7297eb6b69SDavid Sterba struct btrfs_end_io_wq { 73ce9adaa5SChris Mason struct bio *bio; 74ce9adaa5SChris Mason bio_end_io_t *end_io; 75ce9adaa5SChris Mason void *private; 76ce9adaa5SChris Mason struct btrfs_fs_info *info; 774e4cbee9SChristoph Hellwig blk_status_t status; 78bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 798b712842SChris Mason struct btrfs_work work; 80ce9adaa5SChris Mason }; 810da5468fSChris Mason 8297eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8397eb6b69SDavid Sterba 8497eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8597eb6b69SDavid Sterba { 8697eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8797eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8897eb6b69SDavid Sterba 0, 89fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9097eb6b69SDavid Sterba NULL); 9197eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9297eb6b69SDavid Sterba return -ENOMEM; 9397eb6b69SDavid Sterba return 0; 9497eb6b69SDavid Sterba } 9597eb6b69SDavid Sterba 96e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9797eb6b69SDavid Sterba { 9897eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9997eb6b69SDavid Sterba } 10097eb6b69SDavid Sterba 101141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 102141386e1SJosef Bacik { 103141386e1SJosef Bacik if (fs_info->csum_shash) 104141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 105141386e1SJosef Bacik } 106141386e1SJosef Bacik 107d352ac68SChris Mason /* 108d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 109d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 110d352ac68SChris Mason * just before they are sent down the IO stack. 111d352ac68SChris Mason */ 11244b8bd7eSChris Mason struct async_submit_bio { 1138896a08dSQu Wenruo struct inode *inode; 11444b8bd7eSChris Mason struct bio *bio; 115a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11644b8bd7eSChris Mason int mirror_num; 1171941b64bSQu Wenruo 1181941b64bSQu Wenruo /* Optional parameter for submit_bio_start used by direct io */ 1191941b64bSQu Wenruo u64 dio_file_offset; 1208b712842SChris Mason struct btrfs_work work; 1214e4cbee9SChristoph Hellwig blk_status_t status; 12244b8bd7eSChris Mason }; 12344b8bd7eSChris Mason 12485d4e461SChris Mason /* 12585d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12685d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12785d4e461SChris Mason * the level the eb occupies in the tree. 1284008c04aSChris Mason * 12985d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13085d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13185d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1324fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1334fd786e6SMisono Tomohiro * roots have separate keysets. 1344008c04aSChris Mason * 13585d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13685d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13785d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1384008c04aSChris Mason * 13985d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14085d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14185d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14285d4e461SChris Mason * 14385d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14485d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14585d4e461SChris Mason * needs update as well. 1464008c04aSChris Mason */ 1474008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1484008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1494008c04aSChris Mason # error 1504008c04aSChris Mason # endif 15185d4e461SChris Mason 152ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 153ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 154ab1405aaSDavid Sterba 155ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 156ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 157ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 163ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 164ab1405aaSDavid Sterba 16585d4e461SChris Mason static struct btrfs_lockdep_keyset { 16685d4e461SChris Mason u64 id; /* root objectid */ 167ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 168387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 169387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 17085d4e461SChris Mason } btrfs_lockdep_keysets[] = { 171ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 172ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 173ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 174ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 175ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 176ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 177ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 178ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 179ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 180ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 181ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 182ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1834008c04aSChris Mason }; 18485d4e461SChris Mason 185ab1405aaSDavid Sterba #undef DEFINE_LEVEL 186ab1405aaSDavid Sterba #undef DEFINE_NAME 18785d4e461SChris Mason 18885d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18985d4e461SChris Mason int level) 19085d4e461SChris Mason { 19185d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19285d4e461SChris Mason 19385d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19485d4e461SChris Mason 19585d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19685d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19785d4e461SChris Mason if (ks->id == objectid) 19885d4e461SChris Mason break; 19985d4e461SChris Mason 20085d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20185d4e461SChris Mason &ks->keys[level], ks->names[level]); 20285d4e461SChris Mason } 20385d4e461SChris Mason 2044008c04aSChris Mason #endif 2054008c04aSChris Mason 206d352ac68SChris Mason /* 2072996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 208d352ac68SChris Mason */ 209c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 21019c00ddcSChris Mason { 211d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 2127280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 213a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 214d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 21519c00ddcSChris Mason char *kaddr; 216e9be5a30SDavid Sterba int i; 217d5178578SJohannes Thumshirn 218d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 219d5178578SJohannes Thumshirn crypto_shash_init(shash); 220a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 221e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 222a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 22319c00ddcSChris Mason 224e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 225e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 226e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 22719c00ddcSChris Mason } 22871a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 229d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 23019c00ddcSChris Mason } 23119c00ddcSChris Mason 232d352ac68SChris Mason /* 233d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 234d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 235d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 236d352ac68SChris Mason * in the wrong place. 237d352ac68SChris Mason */ 2381259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 239b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 240b9fab919SChris Mason int atomic) 2411259ab75SChris Mason { 2422ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2431259ab75SChris Mason int ret; 2441259ab75SChris Mason 2451259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2461259ab75SChris Mason return 0; 2471259ab75SChris Mason 248b9fab919SChris Mason if (atomic) 249b9fab919SChris Mason return -EAGAIN; 250b9fab919SChris Mason 2512ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 252ff13db41SDavid Sterba &cached_state); 2530b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2541259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2551259ab75SChris Mason ret = 0; 2561259ab75SChris Mason goto out; 2571259ab75SChris Mason } 25894647322SDavid Sterba btrfs_err_rl(eb->fs_info, 25994647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 26094647322SDavid Sterba eb->start, 26129549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2621259ab75SChris Mason ret = 1; 2630b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 26433958dc6SChris Mason out: 2652ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 266e43bbe5eSDavid Sterba &cached_state); 2671259ab75SChris Mason return ret; 2681259ab75SChris Mason } 2691259ab75SChris Mason 270e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 271e7e16f48SJohannes Thumshirn { 272e7e16f48SJohannes Thumshirn switch (csum_type) { 273e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2743951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2753831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 276352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 277e7e16f48SJohannes Thumshirn return true; 278e7e16f48SJohannes Thumshirn default: 279e7e16f48SJohannes Thumshirn return false; 280e7e16f48SJohannes Thumshirn } 281e7e16f48SJohannes Thumshirn } 282e7e16f48SJohannes Thumshirn 283d352ac68SChris Mason /* 2841104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 2851104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 2861104a885SDavid Sterba */ 287ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 288ab8d0fc4SJeff Mahoney char *raw_disk_sb) 2891104a885SDavid Sterba { 2901104a885SDavid Sterba struct btrfs_super_block *disk_sb = 2911104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 29251bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 293d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 294d5178578SJohannes Thumshirn 295d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 2961104a885SDavid Sterba 2971104a885SDavid Sterba /* 2981104a885SDavid Sterba * The super_block structure does not span the whole 29951bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 30051bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3011104a885SDavid Sterba */ 302fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 303fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3041104a885SDavid Sterba 30555fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 306e7e16f48SJohannes Thumshirn return 1; 3071104a885SDavid Sterba 308e7e16f48SJohannes Thumshirn return 0; 3091104a885SDavid Sterba } 3101104a885SDavid Sterba 311e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 312ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 313581c1760SQu Wenruo { 314e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 315581c1760SQu Wenruo int found_level; 316581c1760SQu Wenruo struct btrfs_key found_key; 317581c1760SQu Wenruo int ret; 318581c1760SQu Wenruo 319581c1760SQu Wenruo found_level = btrfs_header_level(eb); 320581c1760SQu Wenruo if (found_level != level) { 32163489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 32263489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 323581c1760SQu Wenruo btrfs_err(fs_info, 324581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 325581c1760SQu Wenruo eb->start, level, found_level); 326581c1760SQu Wenruo return -EIO; 327581c1760SQu Wenruo } 328581c1760SQu Wenruo 329581c1760SQu Wenruo if (!first_key) 330581c1760SQu Wenruo return 0; 331581c1760SQu Wenruo 3325d41be6fSQu Wenruo /* 3335d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3345d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3355d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3365d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3375d41be6fSQu Wenruo */ 3385d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3395d41be6fSQu Wenruo return 0; 34062fdaa52SQu Wenruo 34162fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 34262fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 34362fdaa52SQu Wenruo btrfs_err(fs_info, 34462fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 34562fdaa52SQu Wenruo eb->start); 34662fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 34762fdaa52SQu Wenruo return -EUCLEAN; 34862fdaa52SQu Wenruo } 34962fdaa52SQu Wenruo 350581c1760SQu Wenruo if (found_level) 351581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 352581c1760SQu Wenruo else 353581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 354581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 355581c1760SQu Wenruo 356581c1760SQu Wenruo if (ret) { 35763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 35863489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 359581c1760SQu Wenruo btrfs_err(fs_info, 360ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 361ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 362ff76a864SLiu Bo first_key->type, first_key->offset, 363ff76a864SLiu Bo found_key.objectid, found_key.type, 364ff76a864SLiu Bo found_key.offset); 365581c1760SQu Wenruo } 366581c1760SQu Wenruo return ret; 367581c1760SQu Wenruo } 368581c1760SQu Wenruo 3691104a885SDavid Sterba /* 370d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 371d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 372581c1760SQu Wenruo * 373581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 374581c1760SQu Wenruo * @level: expected level, mandatory check 375581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 376d352ac68SChris Mason */ 3775ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 378581c1760SQu Wenruo u64 parent_transid, int level, 379581c1760SQu Wenruo struct btrfs_key *first_key) 380f188591eSChris Mason { 3815ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 382f188591eSChris Mason struct extent_io_tree *io_tree; 383ea466794SJosef Bacik int failed = 0; 384f188591eSChris Mason int ret; 385f188591eSChris Mason int num_copies = 0; 386f188591eSChris Mason int mirror_num = 0; 387ea466794SJosef Bacik int failed_mirror = 0; 388f188591eSChris Mason 3890b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 390f188591eSChris Mason while (1) { 391f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 392c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 393256dd1bbSStefan Behrens if (!ret) { 394581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 395b9fab919SChris Mason parent_transid, 0)) 396256dd1bbSStefan Behrens ret = -EIO; 397e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 398ff76a864SLiu Bo first_key, parent_transid)) 399581c1760SQu Wenruo ret = -EUCLEAN; 400581c1760SQu Wenruo else 401581c1760SQu Wenruo break; 402256dd1bbSStefan Behrens } 403d397712bSChris Mason 4040b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 405f188591eSChris Mason eb->start, eb->len); 4064235298eSChris Mason if (num_copies == 1) 407ea466794SJosef Bacik break; 4084235298eSChris Mason 4095cf1ab56SJosef Bacik if (!failed_mirror) { 4105cf1ab56SJosef Bacik failed = 1; 4115cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4125cf1ab56SJosef Bacik } 4135cf1ab56SJosef Bacik 414f188591eSChris Mason mirror_num++; 415ea466794SJosef Bacik if (mirror_num == failed_mirror) 416ea466794SJosef Bacik mirror_num++; 417ea466794SJosef Bacik 4184235298eSChris Mason if (mirror_num > num_copies) 419ea466794SJosef Bacik break; 420f188591eSChris Mason } 421ea466794SJosef Bacik 422c0901581SStefan Behrens if (failed && !ret && failed_mirror) 42320a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 424ea466794SJosef Bacik 425ea466794SJosef Bacik return ret; 426f188591eSChris Mason } 42719c00ddcSChris Mason 428eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 429eca0f6f6SQu Wenruo { 430eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 431eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 432eca0f6f6SQu Wenruo int ret; 433eca0f6f6SQu Wenruo 434eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 435eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 436eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 437eca0f6f6SQu Wenruo csum_tree_block(eb, result); 438eca0f6f6SQu Wenruo 439eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 440eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 441eca0f6f6SQu Wenruo else 442eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 443eca0f6f6SQu Wenruo 444eca0f6f6SQu Wenruo if (ret < 0) { 445eca0f6f6SQu Wenruo btrfs_print_tree(eb, 0); 446eca0f6f6SQu Wenruo btrfs_err(fs_info, 447eca0f6f6SQu Wenruo "block=%llu write time tree block corruption detected", 448eca0f6f6SQu Wenruo eb->start); 449eca0f6f6SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 450eca0f6f6SQu Wenruo return ret; 451eca0f6f6SQu Wenruo } 452eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 453eca0f6f6SQu Wenruo 454eca0f6f6SQu Wenruo return 0; 455eca0f6f6SQu Wenruo } 456eca0f6f6SQu Wenruo 457eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 458eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 459eca0f6f6SQu Wenruo struct bio_vec *bvec) 460eca0f6f6SQu Wenruo { 461eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 462eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 463eca0f6f6SQu Wenruo u64 cur; 464eca0f6f6SQu Wenruo int ret = 0; 465eca0f6f6SQu Wenruo 466eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 467eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 468eca0f6f6SQu Wenruo struct extent_buffer *eb; 469eca0f6f6SQu Wenruo bool uptodate; 470eca0f6f6SQu Wenruo 471eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 472eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 473eca0f6f6SQu Wenruo fs_info->nodesize); 474eca0f6f6SQu Wenruo 475eca0f6f6SQu Wenruo /* A dirty eb shouldn't disappear from buffer_radix */ 476eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 477eca0f6f6SQu Wenruo return -EUCLEAN; 478eca0f6f6SQu Wenruo 479eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 480eca0f6f6SQu Wenruo free_extent_buffer(eb); 481eca0f6f6SQu Wenruo return -EUCLEAN; 482eca0f6f6SQu Wenruo } 483eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 484eca0f6f6SQu Wenruo free_extent_buffer(eb); 485eca0f6f6SQu Wenruo return -EUCLEAN; 486eca0f6f6SQu Wenruo } 487eca0f6f6SQu Wenruo 488eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 489eca0f6f6SQu Wenruo free_extent_buffer(eb); 490eca0f6f6SQu Wenruo if (ret < 0) 491eca0f6f6SQu Wenruo return ret; 492eca0f6f6SQu Wenruo } 493eca0f6f6SQu Wenruo return ret; 494eca0f6f6SQu Wenruo } 495eca0f6f6SQu Wenruo 496d352ac68SChris Mason /* 497ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 498ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 499ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 500d352ac68SChris Mason */ 501ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 50219c00ddcSChris Mason { 503ac303b69SQu Wenruo struct page *page = bvec->bv_page; 5044eee4fa4SMiao Xie u64 start = page_offset(page); 50519c00ddcSChris Mason u64 found_start; 50619c00ddcSChris Mason struct extent_buffer *eb; 507eca0f6f6SQu Wenruo 508eca0f6f6SQu Wenruo if (fs_info->sectorsize < PAGE_SIZE) 509eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 510f188591eSChris Mason 5114f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5124f2de97aSJosef Bacik if (page != eb->pages[0]) 5134f2de97aSJosef Bacik return 0; 5140f805531SAlex Lyakas 51519c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 516d3575156SNaohiro Aota 517d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 518d3575156SNaohiro Aota WARN_ON(found_start != 0); 519d3575156SNaohiro Aota return 0; 520d3575156SNaohiro Aota } 521d3575156SNaohiro Aota 5220f805531SAlex Lyakas /* 5230f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5240f805531SAlex Lyakas * It is useful to know what went wrong. 5250f805531SAlex Lyakas */ 5260f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5270f805531SAlex Lyakas return -EUCLEAN; 5280f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5290f805531SAlex Lyakas return -EUCLEAN; 5300f805531SAlex Lyakas 531eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 53219c00ddcSChris Mason } 53319c00ddcSChris Mason 534b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5352b82032cSYan Zheng { 536b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 537944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 53844880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 539944d3f9fSNikolay Borisov u8 *metadata_uuid; 5402b82032cSYan Zheng 5419a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5429a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5437239ff4bSNikolay Borisov /* 544944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 545944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 546944d3f9fSNikolay Borisov * ->fsid in this case is fine 5477239ff4bSNikolay Borisov */ 548944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5497239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5507239ff4bSNikolay Borisov else 5517239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5527239ff4bSNikolay Borisov 553944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 554944d3f9fSNikolay Borisov return 0; 555944d3f9fSNikolay Borisov 556944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 557944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 558944d3f9fSNikolay Borisov return 0; 559944d3f9fSNikolay Borisov 560944d3f9fSNikolay Borisov return 1; 5612b82032cSYan Zheng } 5622b82032cSYan Zheng 56377bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 56477bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 565ce9adaa5SChris Mason { 56677bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 567ce9adaa5SChris Mason u64 found_start; 56877bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 56977bf40a2SQu Wenruo u8 found_level; 5702996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 571dfd29eedSDavid Sterba const u8 *header_csum; 57277bf40a2SQu Wenruo int ret = 0; 573ea466794SJosef Bacik 574ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 575727011e0SChris Mason if (found_start != eb->start) { 576893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 577893bf4b1SSu Yue eb->start, found_start); 578f188591eSChris Mason ret = -EIO; 57977bf40a2SQu Wenruo goto out; 580ce9adaa5SChris Mason } 581b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 58202873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 58394647322SDavid Sterba eb->start); 5841259ab75SChris Mason ret = -EIO; 58577bf40a2SQu Wenruo goto out; 5861259ab75SChris Mason } 587ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5881c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 589893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 590893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5911c24c3ceSJosef Bacik ret = -EIO; 59277bf40a2SQu Wenruo goto out; 5931c24c3ceSJosef Bacik } 594ce9adaa5SChris Mason 595c67b3892SDavid Sterba csum_tree_block(eb, result); 596dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 597dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 598a826d6dcSJosef Bacik 599dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 6002996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 601ff14aa79SDavid Sterba "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 602ff14aa79SDavid Sterba eb->start, 603dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 60435be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 60535be8851SJohannes Thumshirn btrfs_header_level(eb)); 6062996e1f8SJohannes Thumshirn ret = -EUCLEAN; 60777bf40a2SQu Wenruo goto out; 6082996e1f8SJohannes Thumshirn } 6092996e1f8SJohannes Thumshirn 610a826d6dcSJosef Bacik /* 611a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 612a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 613a826d6dcSJosef Bacik * return -EIO. 614a826d6dcSJosef Bacik */ 6151c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 616a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 617a826d6dcSJosef Bacik ret = -EIO; 618a826d6dcSJosef Bacik } 619ce9adaa5SChris Mason 620813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 621053ab70fSLiu Bo ret = -EIO; 622053ab70fSLiu Bo 6230b32f4bbSJosef Bacik if (!ret) 6240b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 62575391f0dSQu Wenruo else 62675391f0dSQu Wenruo btrfs_err(fs_info, 62775391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 62875391f0dSQu Wenruo eb->start); 62977bf40a2SQu Wenruo out: 63077bf40a2SQu Wenruo return ret; 63177bf40a2SQu Wenruo } 63277bf40a2SQu Wenruo 633371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 634371cdc07SQu Wenruo int mirror) 635371cdc07SQu Wenruo { 636371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 637371cdc07SQu Wenruo struct extent_buffer *eb; 638371cdc07SQu Wenruo bool reads_done; 639371cdc07SQu Wenruo int ret = 0; 640371cdc07SQu Wenruo 641371cdc07SQu Wenruo /* 642371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 643371cdc07SQu Wenruo * only get one eb for each endio hook. 644371cdc07SQu Wenruo */ 645371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 646371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 647371cdc07SQu Wenruo 648371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 649371cdc07SQu Wenruo /* 650371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 651371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 652371cdc07SQu Wenruo */ 653371cdc07SQu Wenruo ASSERT(eb); 654371cdc07SQu Wenruo 655371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 656371cdc07SQu Wenruo /* Subpage read must finish in page read */ 657371cdc07SQu Wenruo ASSERT(reads_done); 658371cdc07SQu Wenruo 659371cdc07SQu Wenruo eb->read_mirror = mirror; 660371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 661371cdc07SQu Wenruo ret = -EIO; 662371cdc07SQu Wenruo goto err; 663371cdc07SQu Wenruo } 664371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 665371cdc07SQu Wenruo if (ret < 0) 666371cdc07SQu Wenruo goto err; 667371cdc07SQu Wenruo 668371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 669371cdc07SQu Wenruo 670371cdc07SQu Wenruo free_extent_buffer(eb); 671371cdc07SQu Wenruo return ret; 672371cdc07SQu Wenruo err: 673371cdc07SQu Wenruo /* 674371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 675371cdc07SQu Wenruo * make sure it has something to decrement. 676371cdc07SQu Wenruo */ 677371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 678371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 679371cdc07SQu Wenruo free_extent_buffer(eb); 680371cdc07SQu Wenruo return ret; 681371cdc07SQu Wenruo } 682371cdc07SQu Wenruo 683c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 68477bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 68577bf40a2SQu Wenruo int mirror) 68677bf40a2SQu Wenruo { 68777bf40a2SQu Wenruo struct extent_buffer *eb; 68877bf40a2SQu Wenruo int ret = 0; 68977bf40a2SQu Wenruo int reads_done; 69077bf40a2SQu Wenruo 69177bf40a2SQu Wenruo ASSERT(page->private); 692371cdc07SQu Wenruo 693371cdc07SQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE) 694371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 695371cdc07SQu Wenruo 69677bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 69777bf40a2SQu Wenruo 69877bf40a2SQu Wenruo /* 69977bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 70077bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 70177bf40a2SQu Wenruo */ 70277bf40a2SQu Wenruo atomic_inc(&eb->refs); 70377bf40a2SQu Wenruo 70477bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 70577bf40a2SQu Wenruo if (!reads_done) 70677bf40a2SQu Wenruo goto err; 70777bf40a2SQu Wenruo 70877bf40a2SQu Wenruo eb->read_mirror = mirror; 70977bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 71077bf40a2SQu Wenruo ret = -EIO; 71177bf40a2SQu Wenruo goto err; 71277bf40a2SQu Wenruo } 71377bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 714ce9adaa5SChris Mason err: 71553b381b3SDavid Woodhouse if (ret) { 71653b381b3SDavid Woodhouse /* 71753b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 71853b381b3SDavid Woodhouse * again, we have to make sure it has something 71953b381b3SDavid Woodhouse * to decrement 72053b381b3SDavid Woodhouse */ 72153b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7220b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 72353b381b3SDavid Woodhouse } 7240b32f4bbSJosef Bacik free_extent_buffer(eb); 72577bf40a2SQu Wenruo 726f188591eSChris Mason return ret; 727ce9adaa5SChris Mason } 728ce9adaa5SChris Mason 7294246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 730ce9adaa5SChris Mason { 73197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 732ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7339e0af237SLiu Bo struct btrfs_workqueue *wq; 734ce9adaa5SChris Mason 735ce9adaa5SChris Mason fs_info = end_io_wq->info; 7364e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 737d20f7043SChris Mason 738cfe94440SNaohiro Aota if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 739a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7409e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 741a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7429e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 743a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7449e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 745a0cac0ecSOmar Sandoval else 7469e0af237SLiu Bo wq = fs_info->endio_write_workers; 7479e0af237SLiu Bo } else { 7485c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7499e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 750a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7519e0af237SLiu Bo wq = fs_info->endio_meta_workers; 752a0cac0ecSOmar Sandoval else 7539e0af237SLiu Bo wq = fs_info->endio_workers; 7549e0af237SLiu Bo } 7559e0af237SLiu Bo 756a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7579e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 758ce9adaa5SChris Mason } 759ce9adaa5SChris Mason 7604e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 761bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7620b86a832SChris Mason { 76397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7648b110e39SMiao Xie 76597eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 766ce9adaa5SChris Mason if (!end_io_wq) 7674e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 768ce9adaa5SChris Mason 769ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 770ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 77122c59948SChris Mason end_io_wq->info = info; 7724e4cbee9SChristoph Hellwig end_io_wq->status = 0; 773ce9adaa5SChris Mason end_io_wq->bio = bio; 77422c59948SChris Mason end_io_wq->metadata = metadata; 775ce9adaa5SChris Mason 776ce9adaa5SChris Mason bio->bi_private = end_io_wq; 777ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 77822c59948SChris Mason return 0; 77922c59948SChris Mason } 78022c59948SChris Mason 7814a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7824a69a410SChris Mason { 7834a69a410SChris Mason struct async_submit_bio *async; 7844e4cbee9SChristoph Hellwig blk_status_t ret; 7854a69a410SChris Mason 7864a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7871941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 7881941b64bSQu Wenruo async->dio_file_offset); 78979787eaaSJeff Mahoney if (ret) 7904e4cbee9SChristoph Hellwig async->status = ret; 7914a69a410SChris Mason } 7924a69a410SChris Mason 79306ea01b1SDavid Sterba /* 79406ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 79506ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 79606ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 79706ea01b1SDavid Sterba * 79806ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 79906ea01b1SDavid Sterba * inserted into the tree. 80006ea01b1SDavid Sterba */ 8014a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8028b712842SChris Mason { 8038b712842SChris Mason struct async_submit_bio *async; 80406ea01b1SDavid Sterba struct inode *inode; 80506ea01b1SDavid Sterba blk_status_t ret; 8068b712842SChris Mason 8078b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8088896a08dSQu Wenruo inode = async->inode; 8094854ddd0SChris Mason 810bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 8114e4cbee9SChristoph Hellwig if (async->status) { 8124e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 8134246a0b6SChristoph Hellwig bio_endio(async->bio); 81479787eaaSJeff Mahoney return; 81579787eaaSJeff Mahoney } 81679787eaaSJeff Mahoney 817ec39f769SChris Mason /* 818ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 819ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 820ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 821ec39f769SChris Mason */ 822ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 82308635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 82406ea01b1SDavid Sterba if (ret) { 82506ea01b1SDavid Sterba async->bio->bi_status = ret; 82606ea01b1SDavid Sterba bio_endio(async->bio); 82706ea01b1SDavid Sterba } 8284a69a410SChris Mason } 8294a69a410SChris Mason 8304a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8314a69a410SChris Mason { 8324a69a410SChris Mason struct async_submit_bio *async; 8334a69a410SChris Mason 8344a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8358b712842SChris Mason kfree(async); 8368b712842SChris Mason } 8378b712842SChris Mason 8388896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 839c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 8401941b64bSQu Wenruo u64 dio_file_offset, 841e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 84244b8bd7eSChris Mason { 8438896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 84444b8bd7eSChris Mason struct async_submit_bio *async; 84544b8bd7eSChris Mason 84644b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 84744b8bd7eSChris Mason if (!async) 8484e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 84944b8bd7eSChris Mason 8508896a08dSQu Wenruo async->inode = inode; 85144b8bd7eSChris Mason async->bio = bio; 85244b8bd7eSChris Mason async->mirror_num = mirror_num; 8534a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8544a69a410SChris Mason 855a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 856a0cac0ecSOmar Sandoval run_one_async_free); 8574a69a410SChris Mason 8581941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8598c8bee1dSChris Mason 8604e4cbee9SChristoph Hellwig async->status = 0; 86179787eaaSJeff Mahoney 86267f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8635cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 864d313d7a3SChris Mason 8655cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 86644b8bd7eSChris Mason return 0; 86744b8bd7eSChris Mason } 86844b8bd7eSChris Mason 8694e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 870ce3ed71aSChris Mason { 8712c30c71bSKent Overstreet struct bio_vec *bvec; 872ce3ed71aSChris Mason struct btrfs_root *root; 8732b070cfeSChristoph Hellwig int ret = 0; 8746dc4f100SMing Lei struct bvec_iter_all iter_all; 875ce3ed71aSChris Mason 876c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8772b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 878ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 879ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 88079787eaaSJeff Mahoney if (ret) 88179787eaaSJeff Mahoney break; 882ce3ed71aSChris Mason } 8832c30c71bSKent Overstreet 8844e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 885ce3ed71aSChris Mason } 886ce3ed71aSChris Mason 8878896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 8881941b64bSQu Wenruo u64 dio_file_offset) 88922c59948SChris Mason { 8908b712842SChris Mason /* 8918b712842SChris Mason * when we're called for a write, we're already in the async 8925443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8938b712842SChris Mason */ 89479787eaaSJeff Mahoney return btree_csum_one_bio(bio); 89522c59948SChris Mason } 89622c59948SChris Mason 897f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 8989b4e675aSDavid Sterba struct btrfs_inode *bi) 899de0022b9SJosef Bacik { 9004eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 901f4dcfb30SJohannes Thumshirn return false; 9026300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 903f4dcfb30SJohannes Thumshirn return false; 9049b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 905f4dcfb30SJohannes Thumshirn return false; 906f4dcfb30SJohannes Thumshirn return true; 907de0022b9SJosef Bacik } 908de0022b9SJosef Bacik 9091b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 9101b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 91144b8bd7eSChris Mason { 9120b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 9134e4cbee9SChristoph Hellwig blk_status_t ret; 914cad321adSChris Mason 915cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 916cad321adSChris Mason /* 917cad321adSChris Mason * called for a read, do the setup so that checksum validation 918cad321adSChris Mason * can happen in the async kernel threads 919cad321adSChris Mason */ 9200b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9210b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9221d4284bdSChris Mason if (ret) 92361891923SStefan Behrens goto out_w_error; 92408635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 925f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 926de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 927de0022b9SJosef Bacik if (ret) 92861891923SStefan Behrens goto out_w_error; 92908635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 93061891923SStefan Behrens } else { 931cad321adSChris Mason /* 93261891923SStefan Behrens * kthread helpers are used to submit writes so that 93361891923SStefan Behrens * checksumming can happen in parallel across all CPUs 934cad321adSChris Mason */ 9358896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 9368896a08dSQu Wenruo 0, btree_submit_bio_start); 93744b8bd7eSChris Mason } 93844b8bd7eSChris Mason 9394246a0b6SChristoph Hellwig if (ret) 9404246a0b6SChristoph Hellwig goto out_w_error; 9414246a0b6SChristoph Hellwig return 0; 9424246a0b6SChristoph Hellwig 94361891923SStefan Behrens out_w_error: 9444e4cbee9SChristoph Hellwig bio->bi_status = ret; 9454246a0b6SChristoph Hellwig bio_endio(bio); 94661891923SStefan Behrens return ret; 94761891923SStefan Behrens } 94861891923SStefan Behrens 9493dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 950784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 951a6bc32b8SMel Gorman struct page *newpage, struct page *page, 952a6bc32b8SMel Gorman enum migrate_mode mode) 953784b4e29SChris Mason { 954784b4e29SChris Mason /* 955784b4e29SChris Mason * we can't safely write a btree page from here, 956784b4e29SChris Mason * we haven't done the locking hook 957784b4e29SChris Mason */ 958784b4e29SChris Mason if (PageDirty(page)) 959784b4e29SChris Mason return -EAGAIN; 960784b4e29SChris Mason /* 961784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 962784b4e29SChris Mason * We must have no buffers or drop them. 963784b4e29SChris Mason */ 964784b4e29SChris Mason if (page_has_private(page) && 965784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 966784b4e29SChris Mason return -EAGAIN; 967a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 968784b4e29SChris Mason } 9693dd1462eSJan Beulich #endif 970784b4e29SChris Mason 9710da5468fSChris Mason 9720da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9730da5468fSChris Mason struct writeback_control *wbc) 9740da5468fSChris Mason { 975e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 976e2d84521SMiao Xie int ret; 977e2d84521SMiao Xie 978d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 979448d640bSChris Mason 980448d640bSChris Mason if (wbc->for_kupdate) 981448d640bSChris Mason return 0; 982448d640bSChris Mason 983e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 984b9473439SChris Mason /* this is a bit racy, but that's ok */ 985d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 986d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 987d814a491SEthan Lien fs_info->dirty_metadata_batch); 988e2d84521SMiao Xie if (ret < 0) 989793955bcSChris Mason return 0; 990793955bcSChris Mason } 9910b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9920da5468fSChris Mason } 9930da5468fSChris Mason 99470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9955f39d397SChris Mason { 99698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 99798509cfcSChris Mason return 0; 9980c4e538bSDavid Sterba 999f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1000d98237b3SChris Mason } 1001d98237b3SChris Mason 1002d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1003d47992f8SLukas Czerner unsigned int length) 1004d98237b3SChris Mason { 1005d1310b2eSChris Mason struct extent_io_tree *tree; 1006d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10075f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10085f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10099ad6b7bcSChris Mason if (PagePrivate(page)) { 1010efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1011efe120a0SFrank Holton "page private not zero on page %llu", 1012efe120a0SFrank Holton (unsigned long long)page_offset(page)); 1013d1b89bc0SGuoqing Jiang detach_page_private(page); 10149ad6b7bcSChris Mason } 1015d98237b3SChris Mason } 1016d98237b3SChris Mason 10170b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10180b32f4bbSJosef Bacik { 1019bb146eb2SJosef Bacik #ifdef DEBUG 1020139e8cd3SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 1021139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 10220b32f4bbSJosef Bacik struct extent_buffer *eb; 1023139e8cd3SQu Wenruo int cur_bit = 0; 1024139e8cd3SQu Wenruo u64 page_start = page_offset(page); 10250b32f4bbSJosef Bacik 1026139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 10270b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10280b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10290b32f4bbSJosef Bacik BUG_ON(!eb); 10300b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10310b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 103249d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 1033139e8cd3SQu Wenruo return __set_page_dirty_nobuffers(page); 1034139e8cd3SQu Wenruo } 1035139e8cd3SQu Wenruo ASSERT(PagePrivate(page) && page->private); 1036139e8cd3SQu Wenruo subpage = (struct btrfs_subpage *)page->private; 1037139e8cd3SQu Wenruo 1038139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 1039139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 1040139e8cd3SQu Wenruo unsigned long flags; 1041139e8cd3SQu Wenruo u64 cur; 1042139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 1043139e8cd3SQu Wenruo 1044139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 1045139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 1046139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1047139e8cd3SQu Wenruo cur_bit++; 1048139e8cd3SQu Wenruo continue; 1049139e8cd3SQu Wenruo } 1050139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1051139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 1052139e8cd3SQu Wenruo 1053139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 1054139e8cd3SQu Wenruo ASSERT(eb); 1055139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 1056139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 105749d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 1058139e8cd3SQu Wenruo free_extent_buffer(eb); 1059139e8cd3SQu Wenruo 1060139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 1061139e8cd3SQu Wenruo } 1062bb146eb2SJosef Bacik #endif 10630b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10640b32f4bbSJosef Bacik } 10650b32f4bbSJosef Bacik 10667f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10670da5468fSChris Mason .writepages = btree_writepages, 10685f39d397SChris Mason .releasepage = btree_releasepage, 10695f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10705a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1071784b4e29SChris Mason .migratepage = btree_migratepage, 10725a92bc88SChris Mason #endif 10730b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1074d98237b3SChris Mason }; 1075123abc88SChris Mason 10762ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10772ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10783fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 10793fbaf258SJosef Bacik int level) 10800999df54SChris Mason { 10810b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10820b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10833fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10840999df54SChris Mason } 10850999df54SChris Mason 1086581c1760SQu Wenruo /* 1087581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1088581c1760SQu Wenruo * verification. 1089581c1760SQu Wenruo * 10901b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1091581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1092581c1760SQu Wenruo * @level: expected level, mandatory check 1093581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1094581c1760SQu Wenruo */ 10952ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 10961b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 10971b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1098e20d96d6SChris Mason { 10995f39d397SChris Mason struct extent_buffer *buf = NULL; 110019c00ddcSChris Mason int ret; 110119c00ddcSChris Mason 11023fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1103c871b0f2SLiu Bo if (IS_ERR(buf)) 1104c871b0f2SLiu Bo return buf; 1105e4204dedSChris Mason 11065ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1107581c1760SQu Wenruo level, first_key); 11080f0fe8f7SFilipe David Borba Manana if (ret) { 1109537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 111064c043deSLiu Bo return ERR_PTR(ret); 11110f0fe8f7SFilipe David Borba Manana } 11125f39d397SChris Mason return buf; 1113ce9adaa5SChris Mason 1114eb60ceacSChris Mason } 1115eb60ceacSChris Mason 11166a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1117ed2ff2cbSChris Mason { 11186a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 111955c69072SChris Mason if (btrfs_header_generation(buf) == 1120e2d84521SMiao Xie fs_info->running_transaction->transid) { 112149d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1122b4ce94deSChris Mason 1123b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1124104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1125e2d84521SMiao Xie -buf->len, 1126e2d84521SMiao Xie fs_info->dirty_metadata_batch); 11270b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1128925baeddSChris Mason } 11295f39d397SChris Mason } 1130ed7b63ebSJosef Bacik } 11315f39d397SChris Mason 1132da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1133e20d96d6SChris Mason u64 objectid) 1134d97e63b6SChris Mason { 11357c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 11362e608bd1SJosef Bacik 11372e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 11382e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 11392e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 114096dfcb46SJosef Bacik root->fs_info = fs_info; 11412e608bd1SJosef Bacik root->root_key.objectid = objectid; 1142cfaa7295SChris Mason root->node = NULL; 1143a28ec197SChris Mason root->commit_root = NULL; 114427cdeb70SMiao Xie root->state = 0; 1145abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 11460b86a832SChris Mason 11470f7d52f4SChris Mason root->last_trans = 0; 11486b8fad57SNikolay Borisov root->free_objectid = 0; 1149eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1150199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11516bef4d31SEric Paris root->inode_tree = RB_ROOT; 115216cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 11532e608bd1SJosef Bacik 11542e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 11550b86a832SChris Mason 11560b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11575d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1158eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1159eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1160199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1161199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1162d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11632ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11642ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11655d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1166eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1167199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1168f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11692ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11702ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11718287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1172a2135011SChris Mason mutex_init(&root->objectid_mutex); 1173e02119d5SChris Mason mutex_init(&root->log_mutex); 117431f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1175573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1176c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11777237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11787237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11797237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11808b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11818b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11827237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11837237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11847237f183SYan Zheng atomic_set(&root->log_writers, 0); 11852ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11860700cea7SElena Reshetova refcount_set(&root->refs, 1); 11878ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1188eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11897237f183SYan Zheng root->log_transid = 0; 1190d1433debSMiao Xie root->log_transid_committed = -1; 1191257c62e1SChris Mason root->last_log_commit = 0; 11922e608bd1SJosef Bacik root->anon_dev = 0; 1193e289f03eSFilipe Manana if (!dummy) { 119443eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 119543eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1196e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1197e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1198e289f03eSFilipe Manana } 1199017e5369SChris Mason 12005f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1201370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1202bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1203bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1204bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1205bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1206bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1207bd647ce3SJosef Bacik #endif 12083768f368SChris Mason } 12093768f368SChris Mason 121074e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 121196dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12126f07e42eSAl Viro { 121374e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12146f07e42eSAl Viro if (root) 121596dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12166f07e42eSAl Viro return root; 12176f07e42eSAl Viro } 12186f07e42eSAl Viro 121906ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 122006ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1221da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 122206ea65a3SJosef Bacik { 122306ea65a3SJosef Bacik struct btrfs_root *root; 122406ea65a3SJosef Bacik 12257c0260eeSJeff Mahoney if (!fs_info) 12267c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12277c0260eeSJeff Mahoney 122896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 122906ea65a3SJosef Bacik if (!root) 123006ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1231da17066cSJeff Mahoney 1232b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1233faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 123406ea65a3SJosef Bacik 123506ea65a3SJosef Bacik return root; 123606ea65a3SJosef Bacik } 123706ea65a3SJosef Bacik #endif 123806ea65a3SJosef Bacik 1239abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1240abed4aaaSJosef Bacik { 1241abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1242abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1243abed4aaaSJosef Bacik 1244abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1245abed4aaaSJosef Bacik } 1246abed4aaaSJosef Bacik 1247abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1248abed4aaaSJosef Bacik { 1249abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1250abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1251abed4aaaSJosef Bacik 1252abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1253abed4aaaSJosef Bacik } 1254abed4aaaSJosef Bacik 1255abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1256abed4aaaSJosef Bacik { 1257abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1258abed4aaaSJosef Bacik struct rb_node *tmp; 1259abed4aaaSJosef Bacik 1260abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1261abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1262abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1263abed4aaaSJosef Bacik ASSERT(!tmp); 1264abed4aaaSJosef Bacik 1265abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1266abed4aaaSJosef Bacik } 1267abed4aaaSJosef Bacik 1268abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1269abed4aaaSJosef Bacik { 1270abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1271abed4aaaSJosef Bacik 1272abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1273abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1274abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1275abed4aaaSJosef Bacik } 1276abed4aaaSJosef Bacik 1277abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1278abed4aaaSJosef Bacik struct btrfs_key *key) 1279abed4aaaSJosef Bacik { 1280abed4aaaSJosef Bacik struct rb_node *node; 1281abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1282abed4aaaSJosef Bacik 1283abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1284abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1285abed4aaaSJosef Bacik if (node) 1286abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1287abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1288abed4aaaSJosef Bacik 1289abed4aaaSJosef Bacik return root; 1290abed4aaaSJosef Bacik } 1291abed4aaaSJosef Bacik 1292f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1293f7238e50SJosef Bacik { 1294f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1295f7238e50SJosef Bacik u64 ret; 1296f7238e50SJosef Bacik 1297f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1298f7238e50SJosef Bacik return 0; 1299f7238e50SJosef Bacik 1300f7238e50SJosef Bacik if (bytenr) 1301f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1302f7238e50SJosef Bacik else 1303f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1304f7238e50SJosef Bacik ASSERT(block_group); 1305f7238e50SJosef Bacik if (!block_group) 1306f7238e50SJosef Bacik return 0; 1307f7238e50SJosef Bacik ret = block_group->global_root_id; 1308f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1309f7238e50SJosef Bacik 1310f7238e50SJosef Bacik return ret; 1311f7238e50SJosef Bacik } 1312f7238e50SJosef Bacik 1313abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1314abed4aaaSJosef Bacik { 1315abed4aaaSJosef Bacik struct btrfs_key key = { 1316abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1317abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1318f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1319abed4aaaSJosef Bacik }; 1320abed4aaaSJosef Bacik 1321abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1322abed4aaaSJosef Bacik } 1323abed4aaaSJosef Bacik 1324abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1325abed4aaaSJosef Bacik { 1326abed4aaaSJosef Bacik struct btrfs_key key = { 1327abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1328abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1329f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1330abed4aaaSJosef Bacik }; 1331abed4aaaSJosef Bacik 1332abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1333abed4aaaSJosef Bacik } 1334abed4aaaSJosef Bacik 133520897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 133620897f5cSArne Jansen u64 objectid) 133720897f5cSArne Jansen { 13389b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 133920897f5cSArne Jansen struct extent_buffer *leaf; 134020897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 134120897f5cSArne Jansen struct btrfs_root *root; 134220897f5cSArne Jansen struct btrfs_key key; 1343b89f6d1fSFilipe Manana unsigned int nofs_flag; 134420897f5cSArne Jansen int ret = 0; 134520897f5cSArne Jansen 1346b89f6d1fSFilipe Manana /* 1347b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1348b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1349b89f6d1fSFilipe Manana */ 1350b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 135196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1352b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 135320897f5cSArne Jansen if (!root) 135420897f5cSArne Jansen return ERR_PTR(-ENOMEM); 135520897f5cSArne Jansen 135620897f5cSArne Jansen root->root_key.objectid = objectid; 135720897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 135820897f5cSArne Jansen root->root_key.offset = 0; 135920897f5cSArne Jansen 13609631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 13619631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 136220897f5cSArne Jansen if (IS_ERR(leaf)) { 136320897f5cSArne Jansen ret = PTR_ERR(leaf); 13641dd05682STsutomu Itoh leaf = NULL; 13658a6a87cdSBoris Burkov goto fail_unlock; 136620897f5cSArne Jansen } 136720897f5cSArne Jansen 136820897f5cSArne Jansen root->node = leaf; 136920897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 137020897f5cSArne Jansen 137120897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 137227cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 137320897f5cSArne Jansen 1374f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1375f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 137620897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 137720897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 137820897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 137920897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 138020897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 138120897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 138220897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 138333d85fdaSQu Wenruo if (is_fstree(objectid)) 1384807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1385807fc790SAndy Shevchenko else 1386807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1387c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 138820897f5cSArne Jansen 13898a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13908a6a87cdSBoris Burkov 139120897f5cSArne Jansen key.objectid = objectid; 139220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 139320897f5cSArne Jansen key.offset = 0; 139420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 139520897f5cSArne Jansen if (ret) 139620897f5cSArne Jansen goto fail; 139720897f5cSArne Jansen 139820897f5cSArne Jansen return root; 13991dd05682STsutomu Itoh 14008a6a87cdSBoris Burkov fail_unlock: 14018c38938cSJosef Bacik if (leaf) 14021dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 14038a6a87cdSBoris Burkov fail: 140400246528SJosef Bacik btrfs_put_root(root); 14051dd05682STsutomu Itoh 14061dd05682STsutomu Itoh return ERR_PTR(ret); 140720897f5cSArne Jansen } 140820897f5cSArne Jansen 14097237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1410e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14110f7d52f4SChris Mason { 14120f7d52f4SChris Mason struct btrfs_root *root; 1413e02119d5SChris Mason 141496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1415e02119d5SChris Mason if (!root) 14167237f183SYan Zheng return ERR_PTR(-ENOMEM); 1417e02119d5SChris Mason 1418e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1419e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1420e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 142127cdeb70SMiao Xie 14226ab6ebb7SNaohiro Aota return root; 14236ab6ebb7SNaohiro Aota } 14246ab6ebb7SNaohiro Aota 14256ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 14266ab6ebb7SNaohiro Aota struct btrfs_root *root) 14276ab6ebb7SNaohiro Aota { 14286ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 14296ab6ebb7SNaohiro Aota 14307237f183SYan Zheng /* 143192a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 143227cdeb70SMiao Xie * 143392a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 143492a7cc42SQu Wenruo * and they go away before a real commit is actually done. 143592a7cc42SQu Wenruo * 143692a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 143792a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 14387237f183SYan Zheng */ 1439e02119d5SChris Mason 14404d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14419631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 14426ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 14436ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1444e02119d5SChris Mason 14457237f183SYan Zheng root->node = leaf; 1446e02119d5SChris Mason 1447e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1448e02119d5SChris Mason btrfs_tree_unlock(root->node); 14496ab6ebb7SNaohiro Aota 14506ab6ebb7SNaohiro Aota return 0; 14517237f183SYan Zheng } 14527237f183SYan Zheng 14537237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14547237f183SYan Zheng struct btrfs_fs_info *fs_info) 14557237f183SYan Zheng { 14567237f183SYan Zheng struct btrfs_root *log_root; 14577237f183SYan Zheng 14587237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14597237f183SYan Zheng if (IS_ERR(log_root)) 14607237f183SYan Zheng return PTR_ERR(log_root); 14616ab6ebb7SNaohiro Aota 14623ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 14633ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 14643ddebf27SNaohiro Aota 14656ab6ebb7SNaohiro Aota if (ret) { 14666ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14676ab6ebb7SNaohiro Aota return ret; 14686ab6ebb7SNaohiro Aota } 14693ddebf27SNaohiro Aota } 14706ab6ebb7SNaohiro Aota 14717237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14727237f183SYan Zheng fs_info->log_root_tree = log_root; 14737237f183SYan Zheng return 0; 14747237f183SYan Zheng } 14757237f183SYan Zheng 14767237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14777237f183SYan Zheng struct btrfs_root *root) 14787237f183SYan Zheng { 14790b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14807237f183SYan Zheng struct btrfs_root *log_root; 14817237f183SYan Zheng struct btrfs_inode_item *inode_item; 14826ab6ebb7SNaohiro Aota int ret; 14837237f183SYan Zheng 14840b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14857237f183SYan Zheng if (IS_ERR(log_root)) 14867237f183SYan Zheng return PTR_ERR(log_root); 14877237f183SYan Zheng 14886ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14896ab6ebb7SNaohiro Aota if (ret) { 14906ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14916ab6ebb7SNaohiro Aota return ret; 14926ab6ebb7SNaohiro Aota } 14936ab6ebb7SNaohiro Aota 14947237f183SYan Zheng log_root->last_trans = trans->transid; 14957237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14967237f183SYan Zheng 14977237f183SYan Zheng inode_item = &log_root->root_item.inode; 14983cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14993cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15003cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1501da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15020b246afaSJeff Mahoney fs_info->nodesize); 15033cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15047237f183SYan Zheng 15055d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15067237f183SYan Zheng 15077237f183SYan Zheng WARN_ON(root->log_root); 15087237f183SYan Zheng root->log_root = log_root; 15097237f183SYan Zheng root->log_transid = 0; 1510d1433debSMiao Xie root->log_transid_committed = -1; 1511257c62e1SChris Mason root->last_log_commit = 0; 1512e02119d5SChris Mason return 0; 1513e02119d5SChris Mason } 1514e02119d5SChris Mason 151549d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 151649d11beaSJosef Bacik struct btrfs_path *path, 1517cb517eabSMiao Xie struct btrfs_key *key) 1518e02119d5SChris Mason { 1519e02119d5SChris Mason struct btrfs_root *root; 1520e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 152184234f3aSYan Zheng u64 generation; 1522cb517eabSMiao Xie int ret; 1523581c1760SQu Wenruo int level; 1524cb517eabSMiao Xie 152596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 152649d11beaSJosef Bacik if (!root) 152749d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 15280f7d52f4SChris Mason 1529cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1530cb517eabSMiao Xie &root->root_item, &root->root_key); 15310f7d52f4SChris Mason if (ret) { 153213a8a7c8SYan, Zheng if (ret > 0) 153313a8a7c8SYan, Zheng ret = -ENOENT; 153449d11beaSJosef Bacik goto fail; 15350f7d52f4SChris Mason } 153613a8a7c8SYan, Zheng 153784234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1538581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 15392ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15402ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 15411b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 154264c043deSLiu Bo if (IS_ERR(root->node)) { 154364c043deSLiu Bo ret = PTR_ERR(root->node); 15448c38938cSJosef Bacik root->node = NULL; 154549d11beaSJosef Bacik goto fail; 1546cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1547cb517eabSMiao Xie ret = -EIO; 154849d11beaSJosef Bacik goto fail; 1549416bc658SJosef Bacik } 15505d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1551cb517eabSMiao Xie return root; 155249d11beaSJosef Bacik fail: 155300246528SJosef Bacik btrfs_put_root(root); 155449d11beaSJosef Bacik return ERR_PTR(ret); 155549d11beaSJosef Bacik } 155649d11beaSJosef Bacik 155749d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 155849d11beaSJosef Bacik struct btrfs_key *key) 155949d11beaSJosef Bacik { 156049d11beaSJosef Bacik struct btrfs_root *root; 156149d11beaSJosef Bacik struct btrfs_path *path; 156249d11beaSJosef Bacik 156349d11beaSJosef Bacik path = btrfs_alloc_path(); 156449d11beaSJosef Bacik if (!path) 156549d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 156649d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 156749d11beaSJosef Bacik btrfs_free_path(path); 156849d11beaSJosef Bacik 156949d11beaSJosef Bacik return root; 1570cb517eabSMiao Xie } 1571cb517eabSMiao Xie 15722dfb1e43SQu Wenruo /* 15732dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15742dfb1e43SQu Wenruo * 15752dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 15762dfb1e43SQu Wenruo */ 15772dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1578cb517eabSMiao Xie { 1579cb517eabSMiao Xie int ret; 1580dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1581cb517eabSMiao Xie 1582dcc3eb96SNikolay Borisov /* 1583dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1584dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1585dcc3eb96SNikolay Borisov */ 1586dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1587dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1588dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1589dcc3eb96SNikolay Borisov if (ret) 15908257b2dcSMiao Xie goto fail; 15918257b2dcSMiao Xie 1592aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 159337f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 159492a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1595f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1596f39e4571SJosef Bacik } 1597f39e4571SJosef Bacik 1598851fd730SQu Wenruo /* 1599851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1600851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1601851fd730SQu Wenruo */ 1602851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1603851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 16042dfb1e43SQu Wenruo if (!anon_dev) { 1605cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1606cb517eabSMiao Xie if (ret) 1607876d2cf1SLiu Bo goto fail; 16082dfb1e43SQu Wenruo } else { 16092dfb1e43SQu Wenruo root->anon_dev = anon_dev; 16102dfb1e43SQu Wenruo } 1611851fd730SQu Wenruo } 1612f32e48e9SChandan Rajendra 1613f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1614453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1615f32e48e9SChandan Rajendra if (ret) { 1616f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1617876d2cf1SLiu Bo goto fail; 1618f32e48e9SChandan Rajendra } 1619f32e48e9SChandan Rajendra 16206b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1621f32e48e9SChandan Rajendra 1622f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1623f32e48e9SChandan Rajendra 1624cb517eabSMiao Xie return 0; 1625cb517eabSMiao Xie fail: 162684db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1627cb517eabSMiao Xie return ret; 1628cb517eabSMiao Xie } 1629cb517eabSMiao Xie 1630a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1631cb517eabSMiao Xie u64 root_id) 1632cb517eabSMiao Xie { 1633cb517eabSMiao Xie struct btrfs_root *root; 1634cb517eabSMiao Xie 1635cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1636cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1637cb517eabSMiao Xie (unsigned long)root_id); 1638bc44d7c4SJosef Bacik if (root) 163900246528SJosef Bacik root = btrfs_grab_root(root); 1640cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1641cb517eabSMiao Xie return root; 1642cb517eabSMiao Xie } 1643cb517eabSMiao Xie 164449d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 164549d11beaSJosef Bacik u64 objectid) 164649d11beaSJosef Bacik { 1647abed4aaaSJosef Bacik struct btrfs_key key = { 1648abed4aaaSJosef Bacik .objectid = objectid, 1649abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1650abed4aaaSJosef Bacik .offset = 0, 1651abed4aaaSJosef Bacik }; 1652abed4aaaSJosef Bacik 165349d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 165449d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 165549d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1656abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 165749d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 165849d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 165949d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 166049d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 166149d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1662abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 166349d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 166449d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 166549d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 166649d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 166749d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 166849d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 1669abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1670abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1671abed4aaaSJosef Bacik 1672abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1673abed4aaaSJosef Bacik } 167449d11beaSJosef Bacik return NULL; 167549d11beaSJosef Bacik } 167649d11beaSJosef Bacik 1677cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1678cb517eabSMiao Xie struct btrfs_root *root) 1679cb517eabSMiao Xie { 1680cb517eabSMiao Xie int ret; 1681cb517eabSMiao Xie 1682e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1683cb517eabSMiao Xie if (ret) 1684cb517eabSMiao Xie return ret; 1685cb517eabSMiao Xie 1686cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1687cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1688cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1689cb517eabSMiao Xie root); 1690af01d2e5SJosef Bacik if (ret == 0) { 169100246528SJosef Bacik btrfs_grab_root(root); 169227cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1693af01d2e5SJosef Bacik } 1694cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1695cb517eabSMiao Xie radix_tree_preload_end(); 1696cb517eabSMiao Xie 1697cb517eabSMiao Xie return ret; 1698cb517eabSMiao Xie } 1699cb517eabSMiao Xie 1700bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1701bd647ce3SJosef Bacik { 1702bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1703bd647ce3SJosef Bacik struct btrfs_root *root; 1704bd647ce3SJosef Bacik 1705bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1706457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1707457f1864SJosef Bacik 1708bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1709bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1710457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 171171008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1712bd647ce3SJosef Bacik refcount_read(&root->refs)); 1713bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 171400246528SJosef Bacik btrfs_put_root(root); 171500246528SJosef Bacik btrfs_put_root(root); 1716bd647ce3SJosef Bacik } 1717bd647ce3SJosef Bacik #endif 1718bd647ce3SJosef Bacik } 1719bd647ce3SJosef Bacik 1720abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1721abed4aaaSJosef Bacik { 1722abed4aaaSJosef Bacik struct btrfs_root *root; 1723abed4aaaSJosef Bacik struct rb_node *node; 1724abed4aaaSJosef Bacik 1725abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1726abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1727abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1728abed4aaaSJosef Bacik btrfs_put_root(root); 1729abed4aaaSJosef Bacik } 1730abed4aaaSJosef Bacik } 1731abed4aaaSJosef Bacik 17320d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 17330d4b0463SJosef Bacik { 1734141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1735141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 17365deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1737141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1738141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1739141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1740141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 17410d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 17420d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1743abed4aaaSJosef Bacik free_global_roots(fs_info); 174400246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 174500246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 174600246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 174700246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 174800246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 174900246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1750aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 17519c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1752bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 17533fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 17540d4b0463SJosef Bacik kfree(fs_info->super_copy); 17550d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 17568481dd80SQu Wenruo kfree(fs_info->subpage_info); 17570d4b0463SJosef Bacik kvfree(fs_info); 17580d4b0463SJosef Bacik } 17590d4b0463SJosef Bacik 17600d4b0463SJosef Bacik 17612dfb1e43SQu Wenruo /* 17622dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 17632dfb1e43SQu Wenruo * 17642dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 17652dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 17662dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 17672dfb1e43SQu Wenruo * 17682dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 17692dfb1e43SQu Wenruo * 17702dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 17712dfb1e43SQu Wenruo * same root objectid. 17722dfb1e43SQu Wenruo * 17732dfb1e43SQu Wenruo * @objectid: root id 17742dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 17752dfb1e43SQu Wenruo * pass 0 for new allocation. 17762dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 17772dfb1e43SQu Wenruo * for orphan roots 17782dfb1e43SQu Wenruo */ 17792dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 17802dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 17812dfb1e43SQu Wenruo bool check_ref) 17825eda7b5eSChris Mason { 17835eda7b5eSChris Mason struct btrfs_root *root; 1784381cf658SDavid Sterba struct btrfs_path *path; 17851d4c08e0SDavid Sterba struct btrfs_key key; 17865eda7b5eSChris Mason int ret; 17875eda7b5eSChris Mason 178849d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 178949d11beaSJosef Bacik if (root) 179049d11beaSJosef Bacik return root; 17914df27c4dSYan, Zheng again: 179256e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 179348475471SStefan Behrens if (root) { 17942dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17952dfb1e43SQu Wenruo ASSERT(!anon_dev); 1796bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 179700246528SJosef Bacik btrfs_put_root(root); 179848475471SStefan Behrens return ERR_PTR(-ENOENT); 1799bc44d7c4SJosef Bacik } 18005eda7b5eSChris Mason return root; 180148475471SStefan Behrens } 18025eda7b5eSChris Mason 180356e9357aSDavid Sterba key.objectid = objectid; 180456e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 180556e9357aSDavid Sterba key.offset = (u64)-1; 180656e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 18075eda7b5eSChris Mason if (IS_ERR(root)) 18085eda7b5eSChris Mason return root; 18093394e160SChris Mason 1810c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1811d68fc57bSYan, Zheng ret = -ENOENT; 1812d68fc57bSYan, Zheng goto fail; 1813d68fc57bSYan, Zheng } 1814d68fc57bSYan, Zheng 18152dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1816cb517eabSMiao Xie if (ret) 1817cb517eabSMiao Xie goto fail; 1818cb517eabSMiao Xie 1819381cf658SDavid Sterba path = btrfs_alloc_path(); 1820381cf658SDavid Sterba if (!path) { 1821381cf658SDavid Sterba ret = -ENOMEM; 1822381cf658SDavid Sterba goto fail; 1823381cf658SDavid Sterba } 18241d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 18251d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 182656e9357aSDavid Sterba key.offset = objectid; 18271d4c08e0SDavid Sterba 18281d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1829381cf658SDavid Sterba btrfs_free_path(path); 1830d68fc57bSYan, Zheng if (ret < 0) 1831d68fc57bSYan, Zheng goto fail; 1832d68fc57bSYan, Zheng if (ret == 0) 183327cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1834d68fc57bSYan, Zheng 1835cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 18360f7d52f4SChris Mason if (ret) { 183700246528SJosef Bacik btrfs_put_root(root); 18384785e24fSJosef Bacik if (ret == -EEXIST) 18394df27c4dSYan, Zheng goto again; 18404df27c4dSYan, Zheng goto fail; 18414df27c4dSYan, Zheng } 1842edbd8d4eSChris Mason return root; 18434df27c4dSYan, Zheng fail: 184433fab972SFilipe Manana /* 184533fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 184633fab972SFilipe Manana * responsability to free it in case we fail. So we have to set our 184733fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 184833fab972SFilipe Manana * and once again by our caller. 184933fab972SFilipe Manana */ 185033fab972SFilipe Manana if (anon_dev) 185133fab972SFilipe Manana root->anon_dev = 0; 18528c38938cSJosef Bacik btrfs_put_root(root); 18534df27c4dSYan, Zheng return ERR_PTR(ret); 1854edbd8d4eSChris Mason } 1855edbd8d4eSChris Mason 18562dfb1e43SQu Wenruo /* 18572dfb1e43SQu Wenruo * Get in-memory reference of a root structure 18582dfb1e43SQu Wenruo * 18592dfb1e43SQu Wenruo * @objectid: tree objectid 18602dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 18612dfb1e43SQu Wenruo * one reference 18622dfb1e43SQu Wenruo */ 18632dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 18642dfb1e43SQu Wenruo u64 objectid, bool check_ref) 18652dfb1e43SQu Wenruo { 18662dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 18672dfb1e43SQu Wenruo } 18682dfb1e43SQu Wenruo 18692dfb1e43SQu Wenruo /* 18702dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 18712dfb1e43SQu Wenruo * the anonymous block device id 18722dfb1e43SQu Wenruo * 18732dfb1e43SQu Wenruo * @objectid: tree objectid 18742dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 18752dfb1e43SQu Wenruo * parameter value 18762dfb1e43SQu Wenruo */ 18772dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 18782dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 18792dfb1e43SQu Wenruo { 18802dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 18812dfb1e43SQu Wenruo } 18822dfb1e43SQu Wenruo 18838b712842SChris Mason /* 188449d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 188549d11beaSJosef Bacik * @fs_info: the fs_info 188649d11beaSJosef Bacik * @objectid: the objectid we need to lookup 188749d11beaSJosef Bacik * 188849d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 188949d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 189049d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 189149d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 189249d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 189349d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 189449d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 189549d11beaSJosef Bacik * backref code is finished using the root. 189649d11beaSJosef Bacik */ 189749d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 189849d11beaSJosef Bacik struct btrfs_path *path, 189949d11beaSJosef Bacik u64 objectid) 190049d11beaSJosef Bacik { 190149d11beaSJosef Bacik struct btrfs_root *root; 190249d11beaSJosef Bacik struct btrfs_key key; 190349d11beaSJosef Bacik 190449d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 190549d11beaSJosef Bacik 190649d11beaSJosef Bacik /* 190749d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 190849d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 190949d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 191049d11beaSJosef Bacik * != NULL just return it. 191149d11beaSJosef Bacik */ 191249d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 191349d11beaSJosef Bacik if (root) 191449d11beaSJosef Bacik return root; 191549d11beaSJosef Bacik 191649d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 191749d11beaSJosef Bacik if (root) 191849d11beaSJosef Bacik return root; 191949d11beaSJosef Bacik 192049d11beaSJosef Bacik key.objectid = objectid; 192149d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 192249d11beaSJosef Bacik key.offset = (u64)-1; 192349d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 192449d11beaSJosef Bacik btrfs_release_path(path); 192549d11beaSJosef Bacik 192649d11beaSJosef Bacik return root; 192749d11beaSJosef Bacik } 192849d11beaSJosef Bacik 192949d11beaSJosef Bacik /* 19308b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 19318b712842SChris Mason * functions. This is where read checksum verification actually happens 19328b712842SChris Mason */ 19338b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1934ce9adaa5SChris Mason { 1935ce9adaa5SChris Mason struct bio *bio; 193697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1937ce9adaa5SChris Mason 193897eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1939ce9adaa5SChris Mason bio = end_io_wq->bio; 19408b712842SChris Mason 19414e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1942ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1943ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 19444246a0b6SChristoph Hellwig bio_endio(bio); 19459be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1946ce9adaa5SChris Mason } 194744b8bd7eSChris Mason 1948a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1949a74a4b97SChris Mason { 1950*33c44184SJosef Bacik struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)arg; 1951d0278245SMiao Xie int again; 1952a74a4b97SChris Mason 1953d6fd0ae2SOmar Sandoval while (1) { 1954d0278245SMiao Xie again = 0; 19559d1a2a3aSDavid Sterba 1956fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1957fd340d0fSJosef Bacik 1958d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 19592ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1960d0278245SMiao Xie goto sleep; 1961d0278245SMiao Xie 196290c711abSZygo Blaxell /* 196390c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 196490c711abSZygo Blaxell * before we have finished mounting the filesystem. 196590c711abSZygo Blaxell */ 19660b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 196790c711abSZygo Blaxell goto sleep; 196890c711abSZygo Blaxell 19690b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1970d0278245SMiao Xie goto sleep; 1971d0278245SMiao Xie 1972dc7f370cSMiao Xie /* 1973dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1974dc7f370cSMiao Xie * during the above check and trylock. 1975dc7f370cSMiao Xie */ 19762ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 19770b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1978dc7f370cSMiao Xie goto sleep; 1979dc7f370cSMiao Xie } 1980dc7f370cSMiao Xie 19812ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1982c2d6cb16SFilipe Manana 1983*33c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 19840b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1985a74a4b97SChris Mason 1986d0278245SMiao Xie /* 198705323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 198805323cd1SMiao Xie * needn't do anything special here. 1989d0278245SMiao Xie */ 19900b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 199167c5e7d4SFilipe Manana 199267c5e7d4SFilipe Manana /* 1993f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 199467c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 199567c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1996f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 199767c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 199867c5e7d4SFilipe Manana * unused block groups. 199967c5e7d4SFilipe Manana */ 20000b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 200118bb8bbfSJohannes Thumshirn 200218bb8bbfSJohannes Thumshirn /* 200318bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 200418bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 200518bb8bbfSJohannes Thumshirn * space. 200618bb8bbfSJohannes Thumshirn */ 200718bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 2008d0278245SMiao Xie sleep: 2009a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 2010d6fd0ae2SOmar Sandoval if (kthread_should_park()) 2011d6fd0ae2SOmar Sandoval kthread_parkme(); 2012d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 2013d6fd0ae2SOmar Sandoval return 0; 2014838fe188SJiri Kosina if (!again) { 2015a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 2016a74a4b97SChris Mason schedule(); 2017a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 2018a74a4b97SChris Mason } 2019da288d28SFilipe Manana } 2020a74a4b97SChris Mason } 2021a74a4b97SChris Mason 2022a74a4b97SChris Mason static int transaction_kthread(void *arg) 2023a74a4b97SChris Mason { 2024a74a4b97SChris Mason struct btrfs_root *root = arg; 20250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2026a74a4b97SChris Mason struct btrfs_trans_handle *trans; 2027a74a4b97SChris Mason struct btrfs_transaction *cur; 20288929ecfaSYan, Zheng u64 transid; 2029643900beSNikolay Borisov time64_t delta; 2030a74a4b97SChris Mason unsigned long delay; 2031914b2007SJan Kara bool cannot_commit; 2032a74a4b97SChris Mason 2033a74a4b97SChris Mason do { 2034914b2007SJan Kara cannot_commit = false; 2035ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 20360b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 2037a74a4b97SChris Mason 20380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20390b246afaSJeff Mahoney cur = fs_info->running_transaction; 2040a74a4b97SChris Mason if (!cur) { 20410b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2042a74a4b97SChris Mason goto sleep; 2043a74a4b97SChris Mason } 204431153d81SYan Zheng 2045643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 2046fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 2047fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 2048643900beSNikolay Borisov delta < fs_info->commit_interval) { 20490b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2050fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 2051fb8a7e94SNikolay Borisov delay = min(delay, 2052fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 2053a74a4b97SChris Mason goto sleep; 2054a74a4b97SChris Mason } 20558929ecfaSYan, Zheng transid = cur->transid; 20560b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 205756bec294SChris Mason 205879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 2059354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 2060914b2007SJan Kara if (IS_ERR(trans)) { 2061354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 2062914b2007SJan Kara cannot_commit = true; 206379787eaaSJeff Mahoney goto sleep; 2064914b2007SJan Kara } 20658929ecfaSYan, Zheng if (transid == trans->transid) { 20663a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 20678929ecfaSYan, Zheng } else { 20683a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20698929ecfaSYan, Zheng } 2070a74a4b97SChris Mason sleep: 20710b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 20720b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 2073a74a4b97SChris Mason 207484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 20752ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 20768929ecfaSYan, Zheng if (!kthread_should_stop() && 20770b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 2078914b2007SJan Kara cannot_commit)) 2079bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 2080a74a4b97SChris Mason } while (!kthread_should_stop()); 2081a74a4b97SChris Mason return 0; 2082a74a4b97SChris Mason } 2083a74a4b97SChris Mason 2084af31f5e5SChris Mason /* 208501f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 208601f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 208701f0f9daSNikolay Borisov * anything. 2088af31f5e5SChris Mason * 2089af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2090af31f5e5SChris Mason * generation of the latest root in the array with the generation 2091af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2092af31f5e5SChris Mason */ 209301f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2094af31f5e5SChris Mason { 209501f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2096af31f5e5SChris Mason u64 cur; 2097af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2098af31f5e5SChris Mason int i; 2099af31f5e5SChris Mason 2100af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2101af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2102af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2103af31f5e5SChris Mason if (cur == newest_gen) 210401f0f9daSNikolay Borisov return i; 2105af31f5e5SChris Mason } 2106af31f5e5SChris Mason 210701f0f9daSNikolay Borisov return -EINVAL; 2108af31f5e5SChris Mason } 2109af31f5e5SChris Mason 2110af31f5e5SChris Mason /* 2111af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2112af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2113af31f5e5SChris Mason * done 2114af31f5e5SChris Mason */ 2115af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2116af31f5e5SChris Mason { 21176ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2118af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2119af31f5e5SChris Mason 2120af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2121af31f5e5SChris Mason 2122af31f5e5SChris Mason /* 2123af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2124af31f5e5SChris Mason * regardless of which ones we use today 2125af31f5e5SChris Mason */ 2126af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2127af31f5e5SChris Mason 2128af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2129af31f5e5SChris Mason 2130af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2131af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2132af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2133af31f5e5SChris Mason 2134af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2135af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2136af31f5e5SChris Mason 2137af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2138af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2139af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2140af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2141af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2142af31f5e5SChris Mason 21439c54e80dSJosef Bacik if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) { 21449c54e80dSJosef Bacik btrfs_set_backup_block_group_root(root_backup, 21459c54e80dSJosef Bacik info->block_group_root->node->start); 21469c54e80dSJosef Bacik btrfs_set_backup_block_group_root_gen(root_backup, 21479c54e80dSJosef Bacik btrfs_header_generation(info->block_group_root->node)); 21489c54e80dSJosef Bacik btrfs_set_backup_block_group_root_level(root_backup, 21499c54e80dSJosef Bacik btrfs_header_level(info->block_group_root->node)); 21509c54e80dSJosef Bacik } else { 21519c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2152f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 21539c54e80dSJosef Bacik 21549c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 21559c54e80dSJosef Bacik extent_root->node->start); 2156af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 215729cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2158af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 215929cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2160f7238e50SJosef Bacik 2161f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2162f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2163f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2164f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2165f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 21669c54e80dSJosef Bacik } 2167af31f5e5SChris Mason 21687c7e82a7SChris Mason /* 21697c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 21707c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 21717c7e82a7SChris Mason */ 21727c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 21737c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 21747c7e82a7SChris Mason info->fs_root->node->start); 2175af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2176af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2177af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2178af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21797c7e82a7SChris Mason } 2180af31f5e5SChris Mason 2181af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2182af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2183af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2184af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2185af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2186af31f5e5SChris Mason 2187af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2188af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2189af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2190af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2191af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2192af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2193af31f5e5SChris Mason 2194af31f5e5SChris Mason /* 2195af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2196af31f5e5SChris Mason * for the next commit 2197af31f5e5SChris Mason */ 2198af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2199af31f5e5SChris Mason &info->super_for_commit->super_roots, 2200af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2201af31f5e5SChris Mason } 2202af31f5e5SChris Mason 2203af31f5e5SChris Mason /* 2204bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2205bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2206bd2336b2SNikolay Borisov * 2207bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2208bd2336b2SNikolay Borisov * priority - priority of backup root required 2209bd2336b2SNikolay Borisov * 2210bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2211bd2336b2SNikolay Borisov */ 2212bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2213bd2336b2SNikolay Borisov { 2214bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2215bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2216bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2217bd2336b2SNikolay Borisov 2218bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2219bd2336b2SNikolay Borisov if (priority == 0) 2220bd2336b2SNikolay Borisov return backup_index; 2221bd2336b2SNikolay Borisov 2222bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2223bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2224bd2336b2SNikolay Borisov } else { 2225bd2336b2SNikolay Borisov return -EINVAL; 2226bd2336b2SNikolay Borisov } 2227bd2336b2SNikolay Borisov 2228bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2229bd2336b2SNikolay Borisov 2230bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2231bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2232bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2233bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2234bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2235bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2236bd2336b2SNikolay Borisov 2237bd2336b2SNikolay Borisov /* 2238bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2239bd2336b2SNikolay Borisov * need a fsck 2240bd2336b2SNikolay Borisov */ 2241bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2242bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2243bd2336b2SNikolay Borisov 2244bd2336b2SNikolay Borisov return backup_index; 2245bd2336b2SNikolay Borisov } 2246bd2336b2SNikolay Borisov 22477abadb64SLiu Bo /* helper to cleanup workers */ 22487abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 22497abadb64SLiu Bo { 2250dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2251afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 22525cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2253fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2254fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2255d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2256fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2257fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 22585b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2259e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2260a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2261fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2262b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2263b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2264a9b9477dSFilipe Manana /* 2265a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2266a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2267a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2268a9b9477dSFilipe Manana */ 2269a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2270a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 22717abadb64SLiu Bo } 22727abadb64SLiu Bo 22732e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22742e9f5954SRashika { 22752e9f5954SRashika if (root) { 22762e9f5954SRashika free_extent_buffer(root->node); 22772e9f5954SRashika free_extent_buffer(root->commit_root); 22782e9f5954SRashika root->node = NULL; 22792e9f5954SRashika root->commit_root = NULL; 22802e9f5954SRashika } 22812e9f5954SRashika } 22822e9f5954SRashika 2283abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2284abed4aaaSJosef Bacik { 2285abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2286abed4aaaSJosef Bacik 2287abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2288abed4aaaSJosef Bacik &fs_info->global_root_tree, 2289abed4aaaSJosef Bacik rb_node) 2290abed4aaaSJosef Bacik free_root_extent_buffers(root); 2291abed4aaaSJosef Bacik } 2292abed4aaaSJosef Bacik 2293af31f5e5SChris Mason /* helper to cleanup tree roots */ 22944273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2295af31f5e5SChris Mason { 22962e9f5954SRashika free_root_extent_buffers(info->tree_root); 2297655b09feSJosef Bacik 2298abed4aaaSJosef Bacik free_global_root_pointers(info); 22992e9f5954SRashika free_root_extent_buffers(info->dev_root); 23002e9f5954SRashika free_root_extent_buffers(info->quota_root); 23012e9f5954SRashika free_root_extent_buffers(info->uuid_root); 23028c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2303aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 23049c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 23054273eaffSAnand Jain if (free_chunk_root) 23062e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2307af31f5e5SChris Mason } 2308af31f5e5SChris Mason 23098c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 23108c38938cSJosef Bacik { 23118c38938cSJosef Bacik if (!root) 23128c38938cSJosef Bacik return; 23138c38938cSJosef Bacik 23148c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 23158c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 23161dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 23178c38938cSJosef Bacik if (root->anon_dev) 23188c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 23198c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2320923eb523SJohannes Thumshirn free_root_extent_buffers(root); 23218c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 23228c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 23238c38938cSJosef Bacik list_del_init(&root->leak_list); 23248c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 23258c38938cSJosef Bacik #endif 23268c38938cSJosef Bacik kfree(root); 23278c38938cSJosef Bacik } 23288c38938cSJosef Bacik } 23298c38938cSJosef Bacik 2330faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2331171f6537SJosef Bacik { 2332171f6537SJosef Bacik int ret; 2333171f6537SJosef Bacik struct btrfs_root *gang[8]; 2334171f6537SJosef Bacik int i; 2335171f6537SJosef Bacik 2336171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2337171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2338171f6537SJosef Bacik struct btrfs_root, root_list); 2339171f6537SJosef Bacik list_del(&gang[0]->root_list); 2340171f6537SJosef Bacik 23418c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2342cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 234300246528SJosef Bacik btrfs_put_root(gang[0]); 2344171f6537SJosef Bacik } 2345171f6537SJosef Bacik 2346171f6537SJosef Bacik while (1) { 2347171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2348171f6537SJosef Bacik (void **)gang, 0, 2349171f6537SJosef Bacik ARRAY_SIZE(gang)); 2350171f6537SJosef Bacik if (!ret) 2351171f6537SJosef Bacik break; 2352171f6537SJosef Bacik for (i = 0; i < ret; i++) 2353cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2354171f6537SJosef Bacik } 2355171f6537SJosef Bacik } 2356af31f5e5SChris Mason 2357638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2358638aa7edSEric Sandeen { 2359638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2360638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2361638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2362638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2363638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2364638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2365ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2366638aa7edSEric Sandeen } 2367638aa7edSEric Sandeen 2368779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2369779a65a4SEric Sandeen { 2370779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2371779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2372779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2373779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2374779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2375779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2376907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2377779a65a4SEric Sandeen } 2378779a65a4SEric Sandeen 23796bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2380f37938e0SEric Sandeen { 23812ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23822ff7e61eSJeff Mahoney 23832ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23842ff7e61eSJeff Mahoney set_nlink(inode, 1); 2385f37938e0SEric Sandeen /* 2386f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2387f37938e0SEric Sandeen * the real end of the address space is determined by all of 2388f37938e0SEric Sandeen * the devices in the system 2389f37938e0SEric Sandeen */ 23902ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23912ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2392f37938e0SEric Sandeen 23932ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 239443eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 23952c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 23967b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 23972ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2398f37938e0SEric Sandeen 23995c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 24002ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 24012ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 24022ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2403f37938e0SEric Sandeen } 2404f37938e0SEric Sandeen 2405ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2406ad618368SEric Sandeen { 2407ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2408129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 24097f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2410ad618368SEric Sandeen } 2411ad618368SEric Sandeen 2412f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2413f9e92e40SEric Sandeen { 2414f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2415f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2416f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2417f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2418f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2419f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2420d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2421f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2422f9e92e40SEric Sandeen } 2423f9e92e40SEric Sandeen 2424d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 24252a458198SEric Sandeen { 2426f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 24276f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 24282a458198SEric Sandeen 24292a458198SEric Sandeen fs_info->workers = 2430cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2431cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 24322a458198SEric Sandeen 24332a458198SEric Sandeen fs_info->delalloc_workers = 2434cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2435cb001095SJeff Mahoney flags, max_active, 2); 24362a458198SEric Sandeen 24372a458198SEric Sandeen fs_info->flush_workers = 2438cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2439cb001095SJeff Mahoney flags, max_active, 0); 24402a458198SEric Sandeen 24412a458198SEric Sandeen fs_info->caching_workers = 2442cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 24432a458198SEric Sandeen 24442a458198SEric Sandeen fs_info->fixup_workers = 2445cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 24462a458198SEric Sandeen 24472a458198SEric Sandeen /* 24482a458198SEric Sandeen * endios are largely parallel and should have a very 24492a458198SEric Sandeen * low idle thresh 24502a458198SEric Sandeen */ 24512a458198SEric Sandeen fs_info->endio_workers = 2452cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 24532a458198SEric Sandeen fs_info->endio_meta_workers = 2454cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2455cb001095SJeff Mahoney max_active, 4); 24562a458198SEric Sandeen fs_info->endio_meta_write_workers = 2457cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2458cb001095SJeff Mahoney max_active, 2); 24592a458198SEric Sandeen fs_info->endio_raid56_workers = 2460cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2461cb001095SJeff Mahoney max_active, 4); 24622a458198SEric Sandeen fs_info->rmw_workers = 2463cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 24642a458198SEric Sandeen fs_info->endio_write_workers = 2465cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2466cb001095SJeff Mahoney max_active, 2); 24672a458198SEric Sandeen fs_info->endio_freespace_worker = 2468cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2469cb001095SJeff Mahoney max_active, 0); 24702a458198SEric Sandeen fs_info->delayed_workers = 2471cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2472cb001095SJeff Mahoney max_active, 0); 24732a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2474cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2475b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2476b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 24772a458198SEric Sandeen 24782a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2479ba8a9d07SChris Mason fs_info->flush_workers && 24802a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 24812a458198SEric Sandeen fs_info->endio_meta_write_workers && 24822a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24832a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2484f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2485f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2486b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 24872a458198SEric Sandeen return -ENOMEM; 24882a458198SEric Sandeen } 24892a458198SEric Sandeen 24902a458198SEric Sandeen return 0; 24912a458198SEric Sandeen } 24922a458198SEric Sandeen 24936d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 24946d97c6e3SJohannes Thumshirn { 24956d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2496b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 24976d97c6e3SJohannes Thumshirn 2498b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 24996d97c6e3SJohannes Thumshirn 25006d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 25016d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2502b4e967beSDavid Sterba csum_driver); 25036d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 25046d97c6e3SJohannes Thumshirn } 25056d97c6e3SJohannes Thumshirn 25066d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 25076d97c6e3SJohannes Thumshirn 25086d97c6e3SJohannes Thumshirn return 0; 25096d97c6e3SJohannes Thumshirn } 25106d97c6e3SJohannes Thumshirn 251163443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 251263443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 251363443bf5SEric Sandeen { 251463443bf5SEric Sandeen int ret; 251563443bf5SEric Sandeen struct btrfs_root *log_tree_root; 251663443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 251763443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2518581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 251963443bf5SEric Sandeen 252063443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2521f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 252263443bf5SEric Sandeen return -EIO; 252363443bf5SEric Sandeen } 252463443bf5SEric Sandeen 252596dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 252696dfcb46SJosef Bacik GFP_KERNEL); 252763443bf5SEric Sandeen if (!log_tree_root) 252863443bf5SEric Sandeen return -ENOMEM; 252963443bf5SEric Sandeen 25302ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 25311b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 25321b7ec85eSJosef Bacik fs_info->generation + 1, level, 25331b7ec85eSJosef Bacik NULL); 253464c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2535f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 25360eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 25378c38938cSJosef Bacik log_tree_root->node = NULL; 253800246528SJosef Bacik btrfs_put_root(log_tree_root); 25390eeff236SLiu Bo return ret; 254064c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2541f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 254200246528SJosef Bacik btrfs_put_root(log_tree_root); 254363443bf5SEric Sandeen return -EIO; 254463443bf5SEric Sandeen } 254563443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 254663443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 254763443bf5SEric Sandeen if (ret) { 25480b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 254963443bf5SEric Sandeen "Failed to recover log tree"); 255000246528SJosef Bacik btrfs_put_root(log_tree_root); 255163443bf5SEric Sandeen return ret; 255263443bf5SEric Sandeen } 255363443bf5SEric Sandeen 2554bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 25556bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 255663443bf5SEric Sandeen if (ret) 255763443bf5SEric Sandeen return ret; 255863443bf5SEric Sandeen } 255963443bf5SEric Sandeen 256063443bf5SEric Sandeen return 0; 256163443bf5SEric Sandeen } 256263443bf5SEric Sandeen 2563abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2564abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2565abed4aaaSJosef Bacik const char *name) 2566abed4aaaSJosef Bacik { 2567abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2568abed4aaaSJosef Bacik struct btrfs_root *root; 2569f7238e50SJosef Bacik u64 max_global_id = 0; 2570abed4aaaSJosef Bacik int ret; 2571abed4aaaSJosef Bacik struct btrfs_key key = { 2572abed4aaaSJosef Bacik .objectid = objectid, 2573abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2574abed4aaaSJosef Bacik .offset = 0, 2575abed4aaaSJosef Bacik }; 2576abed4aaaSJosef Bacik bool found = false; 2577abed4aaaSJosef Bacik 2578abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2579abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2580abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2581abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2582abed4aaaSJosef Bacik return 0; 2583abed4aaaSJosef Bacik } 2584abed4aaaSJosef Bacik 2585abed4aaaSJosef Bacik while (1) { 2586abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2587abed4aaaSJosef Bacik if (ret < 0) 2588abed4aaaSJosef Bacik break; 2589abed4aaaSJosef Bacik 2590abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2591abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2592abed4aaaSJosef Bacik if (ret) { 2593abed4aaaSJosef Bacik if (ret > 0) 2594abed4aaaSJosef Bacik ret = 0; 2595abed4aaaSJosef Bacik break; 2596abed4aaaSJosef Bacik } 2597abed4aaaSJosef Bacik } 2598abed4aaaSJosef Bacik ret = 0; 2599abed4aaaSJosef Bacik 2600abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2601abed4aaaSJosef Bacik if (key.objectid != objectid) 2602abed4aaaSJosef Bacik break; 2603abed4aaaSJosef Bacik btrfs_release_path(path); 2604abed4aaaSJosef Bacik 2605f7238e50SJosef Bacik /* 2606f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2607f7238e50SJosef Bacik * everybody. 2608f7238e50SJosef Bacik */ 2609f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2610f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2611f7238e50SJosef Bacik 2612abed4aaaSJosef Bacik found = true; 2613abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2614abed4aaaSJosef Bacik if (IS_ERR(root)) { 2615abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2616abed4aaaSJosef Bacik ret = PTR_ERR(root); 2617abed4aaaSJosef Bacik break; 2618abed4aaaSJosef Bacik } 2619abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2620abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2621abed4aaaSJosef Bacik if (ret) { 2622abed4aaaSJosef Bacik btrfs_put_root(root); 2623abed4aaaSJosef Bacik break; 2624abed4aaaSJosef Bacik } 2625abed4aaaSJosef Bacik key.offset++; 2626abed4aaaSJosef Bacik } 2627abed4aaaSJosef Bacik btrfs_release_path(path); 2628abed4aaaSJosef Bacik 2629f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2630f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2631f7238e50SJosef Bacik 2632abed4aaaSJosef Bacik if (!found || ret) { 2633abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2634abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2635abed4aaaSJosef Bacik 2636abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2637abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2638abed4aaaSJosef Bacik else 2639abed4aaaSJosef Bacik ret = 0; 2640abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2641abed4aaaSJosef Bacik } 2642abed4aaaSJosef Bacik return ret; 2643abed4aaaSJosef Bacik } 2644abed4aaaSJosef Bacik 2645abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2646abed4aaaSJosef Bacik { 2647abed4aaaSJosef Bacik struct btrfs_path *path; 2648abed4aaaSJosef Bacik int ret = 0; 2649abed4aaaSJosef Bacik 2650abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2651abed4aaaSJosef Bacik if (!path) 2652abed4aaaSJosef Bacik return -ENOMEM; 2653abed4aaaSJosef Bacik 2654abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2655abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2656abed4aaaSJosef Bacik if (ret) 2657abed4aaaSJosef Bacik goto out; 2658abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2659abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2660abed4aaaSJosef Bacik if (ret) 2661abed4aaaSJosef Bacik goto out; 2662abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2663abed4aaaSJosef Bacik goto out; 2664abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2665abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2666abed4aaaSJosef Bacik "free space"); 2667abed4aaaSJosef Bacik out: 2668abed4aaaSJosef Bacik btrfs_free_path(path); 2669abed4aaaSJosef Bacik return ret; 2670abed4aaaSJosef Bacik } 2671abed4aaaSJosef Bacik 26726bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 26734bbcaa64SEric Sandeen { 26746bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2675a4f3d2c4SDavid Sterba struct btrfs_root *root; 26764bbcaa64SEric Sandeen struct btrfs_key location; 26774bbcaa64SEric Sandeen int ret; 26784bbcaa64SEric Sandeen 26796bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 26806bccf3abSJeff Mahoney 2681abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2682abed4aaaSJosef Bacik if (ret) 2683abed4aaaSJosef Bacik return ret; 2684abed4aaaSJosef Bacik 2685abed4aaaSJosef Bacik location.objectid = BTRFS_DEV_TREE_OBJECTID; 26864bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 26874bbcaa64SEric Sandeen location.offset = 0; 26884bbcaa64SEric Sandeen 2689a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2690f50f4353SLiu Bo if (IS_ERR(root)) { 269142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2692f50f4353SLiu Bo ret = PTR_ERR(root); 2693f50f4353SLiu Bo goto out; 2694f50f4353SLiu Bo } 269542437a63SJosef Bacik } else { 2696a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2697a4f3d2c4SDavid Sterba fs_info->dev_root = root; 269842437a63SJosef Bacik } 2699820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2700820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 27014bbcaa64SEric Sandeen 2702aeb935a4SQu Wenruo /* 2703aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2704aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2705aeb935a4SQu Wenruo */ 270656e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 270756e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2708aeb935a4SQu Wenruo if (IS_ERR(root)) { 270942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2710aeb935a4SQu Wenruo ret = PTR_ERR(root); 2711aeb935a4SQu Wenruo goto out; 2712aeb935a4SQu Wenruo } 271342437a63SJosef Bacik } else { 2714aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2715aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 271642437a63SJosef Bacik } 2717aeb935a4SQu Wenruo 27184bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2719a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2720a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2721a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2722afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2723a4f3d2c4SDavid Sterba fs_info->quota_root = root; 27244bbcaa64SEric Sandeen } 27254bbcaa64SEric Sandeen 27264bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2727a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2728a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 272942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2730a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 27314bbcaa64SEric Sandeen if (ret != -ENOENT) 2732f50f4353SLiu Bo goto out; 273342437a63SJosef Bacik } 27344bbcaa64SEric Sandeen } else { 2735a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2736a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 27374bbcaa64SEric Sandeen } 27384bbcaa64SEric Sandeen 27394bbcaa64SEric Sandeen return 0; 2740f50f4353SLiu Bo out: 2741f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2742f50f4353SLiu Bo location.objectid, ret); 2743f50f4353SLiu Bo return ret; 27444bbcaa64SEric Sandeen } 27454bbcaa64SEric Sandeen 2746069ec957SQu Wenruo /* 2747069ec957SQu Wenruo * Real super block validation 2748069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2749069ec957SQu Wenruo * 2750069ec957SQu Wenruo * @sb: super block to check 2751069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2752069ec957SQu Wenruo * 0 the primary (1st) sb 2753069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2754069ec957SQu Wenruo * -1 skip bytenr check 2755069ec957SQu Wenruo */ 2756069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2757069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 275821a852b0SQu Wenruo { 275921a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 276021a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 276121a852b0SQu Wenruo int ret = 0; 276221a852b0SQu Wenruo 276321a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 276421a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 276521a852b0SQu Wenruo ret = -EINVAL; 276621a852b0SQu Wenruo } 276721a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 276821a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 276921a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 277021a852b0SQu Wenruo ret = -EINVAL; 277121a852b0SQu Wenruo } 277221a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 277321a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 277421a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 277521a852b0SQu Wenruo ret = -EINVAL; 277621a852b0SQu Wenruo } 277721a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 277821a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 277921a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 278021a852b0SQu Wenruo ret = -EINVAL; 278121a852b0SQu Wenruo } 278221a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 278321a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 278421a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 278521a852b0SQu Wenruo ret = -EINVAL; 278621a852b0SQu Wenruo } 278721a852b0SQu Wenruo 278821a852b0SQu Wenruo /* 278921a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 279021a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 279121a852b0SQu Wenruo */ 279221a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 279321a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 279421a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 279521a852b0SQu Wenruo ret = -EINVAL; 279621a852b0SQu Wenruo } 27970bb3eb3eSQu Wenruo 27980bb3eb3eSQu Wenruo /* 27990bb3eb3eSQu Wenruo * For 4K page size, we only support 4K sector size. 280050780d9bSAnand Jain * For 64K page size, we support 64K and 4K sector sizes. 28010bb3eb3eSQu Wenruo */ 28020bb3eb3eSQu Wenruo if ((PAGE_SIZE == SZ_4K && sectorsize != PAGE_SIZE) || 28030bb3eb3eSQu Wenruo (PAGE_SIZE == SZ_64K && (sectorsize != SZ_4K && 28040bb3eb3eSQu Wenruo sectorsize != SZ_64K))) { 280521a852b0SQu Wenruo btrfs_err(fs_info, 28060bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 280721a852b0SQu Wenruo sectorsize, PAGE_SIZE); 280821a852b0SQu Wenruo ret = -EINVAL; 280921a852b0SQu Wenruo } 28100bb3eb3eSQu Wenruo 281121a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 281221a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 281321a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 281421a852b0SQu Wenruo ret = -EINVAL; 281521a852b0SQu Wenruo } 281621a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 281721a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 281821a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 281921a852b0SQu Wenruo ret = -EINVAL; 282021a852b0SQu Wenruo } 282121a852b0SQu Wenruo 282221a852b0SQu Wenruo /* Root alignment check */ 282321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 282421a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 282521a852b0SQu Wenruo btrfs_super_root(sb)); 282621a852b0SQu Wenruo ret = -EINVAL; 282721a852b0SQu Wenruo } 282821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 282921a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 283021a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 283121a852b0SQu Wenruo ret = -EINVAL; 283221a852b0SQu Wenruo } 283321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 283421a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 283521a852b0SQu Wenruo btrfs_super_log_root(sb)); 283621a852b0SQu Wenruo ret = -EINVAL; 283721a852b0SQu Wenruo } 283821a852b0SQu Wenruo 2839aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2840aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2841aefd7f70SNikolay Borisov btrfs_err(fs_info, 2842aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2843aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2844aefd7f70SNikolay Borisov ret = -EINVAL; 2845aefd7f70SNikolay Borisov } 2846aefd7f70SNikolay Borisov 2847aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2848aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2849aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2850aefd7f70SNikolay Borisov btrfs_err(fs_info, 2851aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2852aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2853aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2854aefd7f70SNikolay Borisov ret = -EINVAL; 2855aefd7f70SNikolay Borisov } 2856aefd7f70SNikolay Borisov 2857de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 28587239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 285921a852b0SQu Wenruo btrfs_err(fs_info, 28607239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2861de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 286221a852b0SQu Wenruo ret = -EINVAL; 286321a852b0SQu Wenruo } 286421a852b0SQu Wenruo 286521a852b0SQu Wenruo /* 286621a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 286721a852b0SQu Wenruo * done later 286821a852b0SQu Wenruo */ 286921a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 287021a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 287121a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 287221a852b0SQu Wenruo ret = -EINVAL; 287321a852b0SQu Wenruo } 287421a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 287521a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 287621a852b0SQu Wenruo btrfs_super_stripesize(sb)); 287721a852b0SQu Wenruo ret = -EINVAL; 287821a852b0SQu Wenruo } 287921a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 288021a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 288121a852b0SQu Wenruo btrfs_super_num_devices(sb)); 288221a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 288321a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 288421a852b0SQu Wenruo ret = -EINVAL; 288521a852b0SQu Wenruo } 288621a852b0SQu Wenruo 2887069ec957SQu Wenruo if (mirror_num >= 0 && 2888069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 288921a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 289021a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 289121a852b0SQu Wenruo ret = -EINVAL; 289221a852b0SQu Wenruo } 289321a852b0SQu Wenruo 289421a852b0SQu Wenruo /* 289521a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 289621a852b0SQu Wenruo * and one chunk 289721a852b0SQu Wenruo */ 289821a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 289921a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 290021a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 290121a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 290221a852b0SQu Wenruo ret = -EINVAL; 290321a852b0SQu Wenruo } 290421a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 290521a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 290621a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 290721a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 290821a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 290921a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 291021a852b0SQu Wenruo ret = -EINVAL; 291121a852b0SQu Wenruo } 291221a852b0SQu Wenruo 291321a852b0SQu Wenruo /* 291421a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 291521a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 291621a852b0SQu Wenruo */ 291721a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 291821a852b0SQu Wenruo btrfs_warn(fs_info, 291921a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 292021a852b0SQu Wenruo btrfs_super_generation(sb), 292121a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 292221a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 292321a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 292421a852b0SQu Wenruo btrfs_warn(fs_info, 292521a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 292621a852b0SQu Wenruo btrfs_super_generation(sb), 292721a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 292821a852b0SQu Wenruo 292921a852b0SQu Wenruo return ret; 293021a852b0SQu Wenruo } 293121a852b0SQu Wenruo 2932069ec957SQu Wenruo /* 2933069ec957SQu Wenruo * Validation of super block at mount time. 2934069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2935069ec957SQu Wenruo * flags will be skipped. 2936069ec957SQu Wenruo */ 2937069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2938069ec957SQu Wenruo { 2939069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2940069ec957SQu Wenruo } 2941069ec957SQu Wenruo 294275cb857dSQu Wenruo /* 294375cb857dSQu Wenruo * Validation of super block at write time. 294475cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 294575cb857dSQu Wenruo * overwritten soon. 294675cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 294775cb857dSQu Wenruo */ 294875cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 294975cb857dSQu Wenruo struct btrfs_super_block *sb) 295075cb857dSQu Wenruo { 295175cb857dSQu Wenruo int ret; 295275cb857dSQu Wenruo 295375cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 295475cb857dSQu Wenruo if (ret < 0) 295575cb857dSQu Wenruo goto out; 2956e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 295775cb857dSQu Wenruo ret = -EUCLEAN; 295875cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 295975cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 296075cb857dSQu Wenruo goto out; 296175cb857dSQu Wenruo } 296275cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 296375cb857dSQu Wenruo ret = -EUCLEAN; 296475cb857dSQu Wenruo btrfs_err(fs_info, 296575cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 296675cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 296775cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 296875cb857dSQu Wenruo goto out; 296975cb857dSQu Wenruo } 297075cb857dSQu Wenruo out: 297175cb857dSQu Wenruo if (ret < 0) 297275cb857dSQu Wenruo btrfs_err(fs_info, 297375cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 297475cb857dSQu Wenruo return ret; 297575cb857dSQu Wenruo } 297675cb857dSQu Wenruo 2977bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2978bd676446SJosef Bacik { 2979bd676446SJosef Bacik int ret = 0; 2980bd676446SJosef Bacik 2981bd676446SJosef Bacik root->node = read_tree_block(root->fs_info, bytenr, 2982bd676446SJosef Bacik root->root_key.objectid, gen, level, NULL); 2983bd676446SJosef Bacik if (IS_ERR(root->node)) { 2984bd676446SJosef Bacik ret = PTR_ERR(root->node); 2985bd676446SJosef Bacik root->node = NULL; 2986bd676446SJosef Bacik } else if (!extent_buffer_uptodate(root->node)) { 2987bd676446SJosef Bacik free_extent_buffer(root->node); 2988bd676446SJosef Bacik root->node = NULL; 2989bd676446SJosef Bacik ret = -EIO; 2990bd676446SJosef Bacik } 2991bd676446SJosef Bacik 2992bd676446SJosef Bacik if (ret) 2993bd676446SJosef Bacik return ret; 2994bd676446SJosef Bacik 2995bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 2996bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 2997bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 2998bd676446SJosef Bacik return ret; 2999bd676446SJosef Bacik } 3000bd676446SJosef Bacik 3001bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 3002bd676446SJosef Bacik { 3003bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 3004bd676446SJosef Bacik u64 gen, bytenr; 3005bd676446SJosef Bacik int level, ret; 3006bd676446SJosef Bacik 3007bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 3008bd676446SJosef Bacik gen = btrfs_super_generation(sb); 3009bd676446SJosef Bacik level = btrfs_super_root_level(sb); 3010bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 30119c54e80dSJosef Bacik if (ret) { 3012bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 3013bd676446SJosef Bacik return ret; 3014bd676446SJosef Bacik } 3015bd676446SJosef Bacik 30169c54e80dSJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 30179c54e80dSJosef Bacik return 0; 30189c54e80dSJosef Bacik 30199c54e80dSJosef Bacik bytenr = btrfs_super_block_group_root(sb); 30209c54e80dSJosef Bacik gen = btrfs_super_block_group_root_generation(sb); 30219c54e80dSJosef Bacik level = btrfs_super_block_group_root_level(sb); 30229c54e80dSJosef Bacik ret = load_super_root(fs_info->block_group_root, bytenr, gen, level); 30239c54e80dSJosef Bacik if (ret) 30249c54e80dSJosef Bacik btrfs_warn(fs_info, "couldn't read block group root"); 30259c54e80dSJosef Bacik return ret; 30269c54e80dSJosef Bacik } 30279c54e80dSJosef Bacik 30286ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 3029b8522a1eSNikolay Borisov { 30306ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 3031b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 3032b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 3033b8522a1eSNikolay Borisov bool handle_error = false; 3034b8522a1eSNikolay Borisov int ret = 0; 3035b8522a1eSNikolay Borisov int i; 3036b8522a1eSNikolay Borisov 30379c54e80dSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 30389c54e80dSJosef Bacik struct btrfs_root *root; 30399c54e80dSJosef Bacik 30409c54e80dSJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID, 30419c54e80dSJosef Bacik GFP_KERNEL); 30429c54e80dSJosef Bacik if (!root) 30439c54e80dSJosef Bacik return -ENOMEM; 30449c54e80dSJosef Bacik fs_info->block_group_root = root; 30459c54e80dSJosef Bacik } 30469c54e80dSJosef Bacik 3047b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 3048b8522a1eSNikolay Borisov if (handle_error) { 3049b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 3050b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 3051b8522a1eSNikolay Borisov tree_root->node = NULL; 3052b8522a1eSNikolay Borisov 3053b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3054b8522a1eSNikolay Borisov break; 3055b8522a1eSNikolay Borisov 3056b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 3057b8522a1eSNikolay Borisov 3058b8522a1eSNikolay Borisov /* 3059b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 3060b8522a1eSNikolay Borisov * valid 3061b8522a1eSNikolay Borisov */ 3062b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 3063b8522a1eSNikolay Borisov 3064b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 3065b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3066b8522a1eSNikolay Borisov 3067b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 30686ef108ddSNikolay Borisov backup_index = ret; 3069b8522a1eSNikolay Borisov if (ret < 0) 3070b8522a1eSNikolay Borisov return ret; 3071b8522a1eSNikolay Borisov } 3072b8522a1eSNikolay Borisov 3073bd676446SJosef Bacik ret = load_important_roots(fs_info); 3074bd676446SJosef Bacik if (ret) { 3075217f5004SNikolay Borisov handle_error = true; 3076b8522a1eSNikolay Borisov continue; 3077b8522a1eSNikolay Borisov } 3078b8522a1eSNikolay Borisov 3079336a0d8dSNikolay Borisov /* 3080336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3081336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3082336a0d8dSNikolay Borisov */ 3083453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3084b8522a1eSNikolay Borisov if (ret < 0) { 3085b8522a1eSNikolay Borisov handle_error = true; 3086b8522a1eSNikolay Borisov continue; 3087b8522a1eSNikolay Borisov } 3088b8522a1eSNikolay Borisov 30896b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3090b8522a1eSNikolay Borisov 3091b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3092b8522a1eSNikolay Borisov if (ret < 0) { 3093b8522a1eSNikolay Borisov handle_error = true; 3094b8522a1eSNikolay Borisov continue; 3095b8522a1eSNikolay Borisov } 3096b8522a1eSNikolay Borisov 3097b8522a1eSNikolay Borisov /* All successful */ 3098bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3099bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3100d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 31016ef108ddSNikolay Borisov 31026ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 31036ef108ddSNikolay Borisov if (backup_index < 0) { 31046ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 31056ef108ddSNikolay Borisov } else { 31066ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 31076ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 31086ef108ddSNikolay Borisov } 3109b8522a1eSNikolay Borisov break; 3110b8522a1eSNikolay Borisov } 3111b8522a1eSNikolay Borisov 3112b8522a1eSNikolay Borisov return ret; 3113b8522a1eSNikolay Borisov } 3114b8522a1eSNikolay Borisov 31158260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3116eb60ceacSChris Mason { 311776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 3118f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 31198fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3120facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 312124bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3122eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 312311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3124eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3125a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 312676dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 312724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 31284cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3129ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3130f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 313147ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 313240ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3133afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3134c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3135f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3136abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3137d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3138f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 31397585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3140573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 31410bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 3142de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 314319c00ddcSChris Mason 31440b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 31456324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3146f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 314747ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 314818bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3149afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3150bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3151bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 31523fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 31533fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3154bd647ce3SJosef Bacik #endif 3155c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 315666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 315766d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 315866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 315966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 316066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 316166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 316266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3163ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3164ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3165ba2c4d4eSJosef Bacik 3166771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 31674cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3168034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3169fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3170abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 317195ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 31729ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 31734cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3174a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3175f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 31768b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3177f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3178fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3179c8b97818SChris Mason 3180b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3181b34b086cSChris Mason num_online_cpus() + 2, 8); 31820afbaf8cSChris Mason 3183199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3184199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 318569fe2d75SJosef Bacik 3186638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 318721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 318821adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 318921adbd5cSStefan Behrens #endif 3190779a65a4SEric Sandeen btrfs_init_balance(fs_info); 319157056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3192a2de733cSArne Jansen 31930f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 31946bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 3195a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 31960f9dd46cSJosef Bacik 3197fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 3198fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 319939279cc3SChris Mason 32005a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3201e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3202925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3203a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3204a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 32051bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 32069e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3207c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 320876dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3209803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3210fa9c0d79SChris Mason 3211ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3212f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3213b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3214416ac51dSArne Jansen 3215fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3216fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3217fa9c0d79SChris Mason 3218e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3219f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3220bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 32214854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3222034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 32233768f368SChris Mason 3224da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3225da17066cSJeff Mahoney fs_info->nodesize = 4096; 3226da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3227ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3228da17066cSJeff Mahoney fs_info->stripesize = 4096; 3229da17066cSJeff Mahoney 3230eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3231eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3232eede2bf3SOmar Sandoval 323318bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 323418bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 32358260edbaSJosef Bacik } 32368260edbaSJosef Bacik 32378260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 32388260edbaSJosef Bacik { 32398260edbaSJosef Bacik int ret; 32408260edbaSJosef Bacik 32418260edbaSJosef Bacik fs_info->sb = sb; 32428260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 32438260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 32449e967495SFilipe Manana 32455deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3246ae18c37aSJosef Bacik if (ret) 3247ae18c37aSJosef Bacik return ret; 3248ae18c37aSJosef Bacik 3249ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3250ae18c37aSJosef Bacik if (ret) 3251c75e8394SJosef Bacik return ret; 3252ae18c37aSJosef Bacik 3253ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3254ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3255ae18c37aSJosef Bacik 3256ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3257ae18c37aSJosef Bacik if (ret) 3258c75e8394SJosef Bacik return ret; 3259ae18c37aSJosef Bacik 3260ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3261ae18c37aSJosef Bacik GFP_KERNEL); 3262ae18c37aSJosef Bacik if (ret) 3263c75e8394SJosef Bacik return ret; 3264ae18c37aSJosef Bacik 3265ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3266ae18c37aSJosef Bacik GFP_KERNEL); 3267c75e8394SJosef Bacik if (!fs_info->delayed_root) 3268c75e8394SJosef Bacik return -ENOMEM; 3269ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3270ae18c37aSJosef Bacik 3271a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3272a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3273a0a1db70SFilipe Manana 3274c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3275ae18c37aSJosef Bacik } 3276ae18c37aSJosef Bacik 327797f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 327897f4dd09SNikolay Borisov { 327997f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 328097f4dd09SNikolay Borisov int ret; 328197f4dd09SNikolay Borisov 328297f4dd09SNikolay Borisov /* 328397f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 328497f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 328597f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 328697f4dd09SNikolay Borisov */ 328797f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 328897f4dd09SNikolay Borisov if (ret < 0) { 3289c94bec2cSJosef Bacik if (ret != -EINTR) 3290c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3291c94bec2cSJosef Bacik ret); 329297f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 329397f4dd09SNikolay Borisov return ret; 329497f4dd09SNikolay Borisov } 329597f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 329697f4dd09SNikolay Borisov } 329797f4dd09SNikolay Borisov 329897f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 329997f4dd09SNikolay Borisov { 330097f4dd09SNikolay Borisov struct task_struct *task; 330197f4dd09SNikolay Borisov 330297f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 330397f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 330497f4dd09SNikolay Borisov if (IS_ERR(task)) { 330597f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 330697f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 330797f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 330897f4dd09SNikolay Borisov return PTR_ERR(task); 330997f4dd09SNikolay Borisov } 331097f4dd09SNikolay Borisov 331197f4dd09SNikolay Borisov return 0; 331297f4dd09SNikolay Borisov } 331397f4dd09SNikolay Borisov 331444c0ca21SBoris Burkov /* 33158cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 33168cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 33178cd29088SBoris Burkov * code paths. 33188cd29088SBoris Burkov */ 33198cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 33208cd29088SBoris Burkov { 33218cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33228b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 33238cd29088SBoris Burkov } 33248cd29088SBoris Burkov 33258cd29088SBoris Burkov /* 332644c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 332744c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 332844c0ca21SBoris Burkov */ 332944c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 333044c0ca21SBoris Burkov { 333144c0ca21SBoris Burkov int ret; 333294846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 33338b228324SBoris Burkov bool clear_free_space_tree = false; 33348b228324SBoris Burkov 33358b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 33368b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33378b228324SBoris Burkov clear_free_space_tree = true; 33388b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33398b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33408b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 33418b228324SBoris Burkov clear_free_space_tree = true; 33428b228324SBoris Burkov } 33438b228324SBoris Burkov 33448b228324SBoris Burkov if (clear_free_space_tree) { 33458b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 33468b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 33478b228324SBoris Burkov if (ret) { 33488b228324SBoris Burkov btrfs_warn(fs_info, 33498b228324SBoris Burkov "failed to clear free space tree: %d", ret); 33508b228324SBoris Burkov goto out; 33518b228324SBoris Burkov } 33528b228324SBoris Burkov } 335344c0ca21SBoris Burkov 33548d488a8cSFilipe Manana /* 33558d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 33568d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 33578d488a8cSFilipe Manana * them into the fs_info->fs_roots_radix tree. This must be done before 33588d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 33598d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 33608d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 33618d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 33628d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 33638d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 33648d488a8cSFilipe Manana */ 33658d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 33668d488a8cSFilipe Manana if (ret) 33678d488a8cSFilipe Manana goto out; 33688d488a8cSFilipe Manana 336944c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 337044c0ca21SBoris Burkov if (ret) 337144c0ca21SBoris Burkov goto out; 337244c0ca21SBoris Burkov 33738f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 33748f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33758f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33768f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33778f1c21d7SBoris Burkov goto out; 33788f1c21d7SBoris Burkov } 33798f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33808f1c21d7SBoris Burkov 338144c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 338244c0ca21SBoris Burkov ret = btrfs_recover_relocation(fs_info->tree_root); 338344c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 338444c0ca21SBoris Burkov if (ret < 0) { 338544c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 338644c0ca21SBoris Burkov goto out; 338744c0ca21SBoris Burkov } 338844c0ca21SBoris Burkov 33895011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 33905011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33915011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 33925011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 33935011139aSBoris Burkov if (ret) { 33945011139aSBoris Burkov btrfs_warn(fs_info, 33955011139aSBoris Burkov "failed to create free space tree: %d", ret); 33965011139aSBoris Burkov goto out; 33975011139aSBoris Burkov } 33985011139aSBoris Burkov } 33995011139aSBoris Burkov 340094846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 340194846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 340294846229SBoris Burkov if (ret) 340394846229SBoris Burkov goto out; 340494846229SBoris Burkov } 340594846229SBoris Burkov 340644c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 340744c0ca21SBoris Burkov if (ret) 340844c0ca21SBoris Burkov goto out; 340944c0ca21SBoris Burkov 341044c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 341144c0ca21SBoris Burkov if (ret) { 341244c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 341344c0ca21SBoris Burkov goto out; 341444c0ca21SBoris Burkov } 341544c0ca21SBoris Burkov 341644c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 341744c0ca21SBoris Burkov 341844c0ca21SBoris Burkov if (!fs_info->uuid_root) { 341944c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 342044c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 342144c0ca21SBoris Burkov if (ret) { 342244c0ca21SBoris Burkov btrfs_warn(fs_info, 342344c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 342444c0ca21SBoris Burkov goto out; 342544c0ca21SBoris Burkov } 342644c0ca21SBoris Burkov } 342744c0ca21SBoris Burkov 342844c0ca21SBoris Burkov out: 342944c0ca21SBoris Burkov return ret; 343044c0ca21SBoris Burkov } 343144c0ca21SBoris Burkov 3432ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3433ae18c37aSJosef Bacik char *options) 3434ae18c37aSJosef Bacik { 3435ae18c37aSJosef Bacik u32 sectorsize; 3436ae18c37aSJosef Bacik u32 nodesize; 3437ae18c37aSJosef Bacik u32 stripesize; 3438ae18c37aSJosef Bacik u64 generation; 3439ae18c37aSJosef Bacik u64 features; 3440ae18c37aSJosef Bacik u16 csum_type; 3441ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3442ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3443ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3444ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3445ae18c37aSJosef Bacik int ret; 3446ae18c37aSJosef Bacik int err = -EINVAL; 3447ae18c37aSJosef Bacik int level; 3448ae18c37aSJosef Bacik 34498260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 345053b381b3SDavid Woodhouse if (ret) { 345183c8266aSDavid Sterba err = ret; 3452ae18c37aSJosef Bacik goto fail; 345353b381b3SDavid Woodhouse } 345453b381b3SDavid Woodhouse 3455ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3456ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3457ae18c37aSJosef Bacik GFP_KERNEL); 3458ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3459ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3460ae18c37aSJosef Bacik GFP_KERNEL); 3461ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3462ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3463ae18c37aSJosef Bacik err = -ENOMEM; 3464c75e8394SJosef Bacik goto fail; 3465ae18c37aSJosef Bacik } 3466ae18c37aSJosef Bacik 3467ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3468ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3469ae18c37aSJosef Bacik err = -ENOMEM; 3470c75e8394SJosef Bacik goto fail; 3471ae18c37aSJosef Bacik } 3472ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3473ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3474ae18c37aSJosef Bacik 3475d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 34761104a885SDavid Sterba 34771104a885SDavid Sterba /* 34781104a885SDavid Sterba * Read super block and check the signature bytes only 34791104a885SDavid Sterba */ 3480d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 34818f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 34828f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 348316cdcec7SMiao Xie goto fail_alloc; 348420b45077SDave Young } 348539279cc3SChris Mason 34861104a885SDavid Sterba /* 3487260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 34888dc3f22cSJohannes Thumshirn * corrupted, we'll find out 34898dc3f22cSJohannes Thumshirn */ 34908f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 349151bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 34928dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 349351bce6c9SJohannes Thumshirn csum_type); 34948dc3f22cSJohannes Thumshirn err = -EINVAL; 34958f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 34968dc3f22cSJohannes Thumshirn goto fail_alloc; 34978dc3f22cSJohannes Thumshirn } 34988dc3f22cSJohannes Thumshirn 349983c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 350083c68bbcSSu Yue 35016d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 35026d97c6e3SJohannes Thumshirn if (ret) { 35036d97c6e3SJohannes Thumshirn err = ret; 35048f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35056d97c6e3SJohannes Thumshirn goto fail_alloc; 35066d97c6e3SJohannes Thumshirn } 35076d97c6e3SJohannes Thumshirn 35088dc3f22cSJohannes Thumshirn /* 35091104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 35101104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 35111104a885SDavid Sterba */ 35128f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 351305135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 35141104a885SDavid Sterba err = -EINVAL; 35158f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3516141386e1SJosef Bacik goto fail_alloc; 35171104a885SDavid Sterba } 35181104a885SDavid Sterba 35191104a885SDavid Sterba /* 35201104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 35211104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 35221104a885SDavid Sterba * the whole block of INFO_SIZE 35231104a885SDavid Sterba */ 35248f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 35258f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35265f39d397SChris Mason 3527fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3528fbc6feaeSNikolay Borisov 35290b86a832SChris Mason 3530fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3531fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3532fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3533fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3534fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3535fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3536fbc6feaeSNikolay Borisov } 3537fbc6feaeSNikolay Borisov 3538fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3539fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3540de37aa51SNikolay Borisov 3541069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 35421104a885SDavid Sterba if (ret) { 354305135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 35441104a885SDavid Sterba err = -EINVAL; 3545141386e1SJosef Bacik goto fail_alloc; 35461104a885SDavid Sterba } 35471104a885SDavid Sterba 35480f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3549141386e1SJosef Bacik goto fail_alloc; 35500f7d52f4SChris Mason 3551acce952bSliubo /* check FS state, whether FS is broken. */ 355287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 355387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3554acce952bSliubo 355575e7cb7fSLiu Bo /* 355675e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 355775e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 355875e7cb7fSLiu Bo */ 355975e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 356075e7cb7fSLiu Bo 35616f93e834SAnand Jain /* 35626f93e834SAnand Jain * Flag our filesystem as having big metadata blocks if they are bigger 35636f93e834SAnand Jain * than the page size. 35646f93e834SAnand Jain */ 35656f93e834SAnand Jain if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 35666f93e834SAnand Jain if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 35676f93e834SAnand Jain btrfs_info(fs_info, 35686f93e834SAnand Jain "flagging fs with big metadata feature"); 35696f93e834SAnand Jain features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 35706f93e834SAnand Jain } 35716f93e834SAnand Jain 35726f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 35736f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 35746f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 35756f93e834SAnand Jain stripesize = sectorsize; 35766f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 35776f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 35786f93e834SAnand Jain 35796f93e834SAnand Jain fs_info->nodesize = nodesize; 35806f93e834SAnand Jain fs_info->sectorsize = sectorsize; 35816f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 35826f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 35836f93e834SAnand Jain fs_info->stripesize = stripesize; 35846f93e834SAnand Jain 35852ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 35862b82032cSYan Zheng if (ret) { 35872b82032cSYan Zheng err = ret; 3588141386e1SJosef Bacik goto fail_alloc; 35892b82032cSYan Zheng } 3590dfe25020SChris Mason 3591f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3592f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3593f2b636e8SJosef Bacik if (features) { 359405135f59SDavid Sterba btrfs_err(fs_info, 359505135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3596c1c9ff7cSGeert Uytterhoeven features); 3597f2b636e8SJosef Bacik err = -EINVAL; 3598141386e1SJosef Bacik goto fail_alloc; 3599f2b636e8SJosef Bacik } 3600f2b636e8SJosef Bacik 36015d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 36025d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 36030b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3604a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 36055c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 36065c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3607727011e0SChris Mason 36083173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 360905135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 36103173a18fSJosef Bacik 3611727011e0SChris Mason /* 3612bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3613bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3614bc3f116fSChris Mason */ 3615bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3616707e8a07SDavid Sterba (sectorsize != nodesize)) { 361705135f59SDavid Sterba btrfs_err(fs_info, 361805135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 361905135f59SDavid Sterba nodesize, sectorsize); 3620141386e1SJosef Bacik goto fail_alloc; 3621bc3f116fSChris Mason } 3622bc3f116fSChris Mason 3623ceda0864SMiao Xie /* 3624ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3625ceda0864SMiao Xie * update the flag. 3626ceda0864SMiao Xie */ 36275d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 36285d4f98a2SYan Zheng 3629f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3630f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3631bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 363205135f59SDavid Sterba btrfs_err(fs_info, 363305135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3634c1c9ff7cSGeert Uytterhoeven features); 3635f2b636e8SJosef Bacik err = -EINVAL; 3636141386e1SJosef Bacik goto fail_alloc; 3637f2b636e8SJosef Bacik } 363861d92c32SChris Mason 36398481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 36408481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 36418481dd80SQu Wenruo 364295ea0486SQu Wenruo btrfs_warn(fs_info, 364395ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 36440bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 3645c8050b3bSQu Wenruo if (btrfs_super_incompat_flags(fs_info->super_copy) & 3646c8050b3bSQu Wenruo BTRFS_FEATURE_INCOMPAT_RAID56) { 3647c8050b3bSQu Wenruo btrfs_err(fs_info, 3648c8050b3bSQu Wenruo "RAID56 is not yet supported for sector size %u with page size %lu", 3649c8050b3bSQu Wenruo sectorsize, PAGE_SIZE); 3650c8050b3bSQu Wenruo err = -EINVAL; 3651c8050b3bSQu Wenruo goto fail_alloc; 3652c8050b3bSQu Wenruo } 36538481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 36548481dd80SQu Wenruo if (!subpage_info) 36558481dd80SQu Wenruo goto fail_alloc; 36568481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 36578481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3658c8050b3bSQu Wenruo } 36590bb3eb3eSQu Wenruo 3660d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 36612a458198SEric Sandeen if (ret) { 36622a458198SEric Sandeen err = ret; 36630dc3b84aSJosef Bacik goto fail_sb_buffer; 36640dc3b84aSJosef Bacik } 36654543df7eSChris Mason 36669e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 36679e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 36684575c9ccSChris Mason 3669a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3670a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3671de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3672db94535dSChris Mason 3673925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 36746bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3675925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 367684eed90fSChris Mason if (ret) { 367705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36785d4f98a2SYan Zheng goto fail_sb_buffer; 367984eed90fSChris Mason } 36800b86a832SChris Mason 368184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3682581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3683bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3684bd676446SJosef Bacik generation, level); 3685bd676446SJosef Bacik if (ret) { 368605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3687af31f5e5SChris Mason goto fail_tree_roots; 368883121942SDavid Woodhouse } 36890b86a832SChris Mason 3690e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3691c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3692c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3693e17cade2SChris Mason 36945b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 36952b82032cSYan Zheng if (ret) { 369605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3697af31f5e5SChris Mason goto fail_tree_roots; 36982b82032cSYan Zheng } 36990b86a832SChris Mason 37008dabb742SStefan Behrens /* 3701bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3702bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3703bacce86aSAnand Jain * target is required. So free devices that are not part of this 37041a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3705bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 37068dabb742SStefan Behrens */ 3707bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3708d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 370905135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3710a6b0d5c8SChris Mason goto fail_tree_roots; 3711a6b0d5c8SChris Mason } 3712a6b0d5c8SChris Mason 3713b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 37144bbcaa64SEric Sandeen if (ret) 3715b8522a1eSNikolay Borisov goto fail_tree_roots; 37168929ecfaSYan, Zheng 371775ec1db8SJosef Bacik /* 371873651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 371973651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 372073651042SNaohiro Aota * zoned incompat feature flag set. 372173651042SNaohiro Aota */ 372273651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 372373651042SNaohiro Aota if (ret) { 372473651042SNaohiro Aota btrfs_err(fs_info, 372573651042SNaohiro Aota "zoned: failed to read device zone info: %d", 372673651042SNaohiro Aota ret); 372773651042SNaohiro Aota goto fail_block_groups; 372873651042SNaohiro Aota } 372973651042SNaohiro Aota 373073651042SNaohiro Aota /* 373175ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 373275ec1db8SJosef Bacik * check the generation here so we can set the 373375ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 373475ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 373575ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 373675ec1db8SJosef Bacik * even though it was perfectly fine. 373775ec1db8SJosef Bacik */ 373875ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 373975ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 374075ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 374175ec1db8SJosef Bacik 3742cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3743cf90d884SQu Wenruo if (ret) { 3744cf90d884SQu Wenruo btrfs_err(fs_info, 3745cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3746cf90d884SQu Wenruo ret); 3747cf90d884SQu Wenruo goto fail_block_groups; 3748cf90d884SQu Wenruo } 374968310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 375068310a5eSIlya Dryomov if (ret) { 375105135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 375268310a5eSIlya Dryomov goto fail_block_groups; 375368310a5eSIlya Dryomov } 375468310a5eSIlya Dryomov 3755733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3756733f4fbbSStefan Behrens if (ret) { 375705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3758733f4fbbSStefan Behrens goto fail_block_groups; 3759733f4fbbSStefan Behrens } 3760733f4fbbSStefan Behrens 37618dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 37628dabb742SStefan Behrens if (ret) { 376305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 37648dabb742SStefan Behrens goto fail_block_groups; 37658dabb742SStefan Behrens } 37668dabb742SStefan Behrens 3767b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3768b70f5097SNaohiro Aota if (ret) { 3769b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3770b70f5097SNaohiro Aota ret); 3771b70f5097SNaohiro Aota goto fail_block_groups; 3772b70f5097SNaohiro Aota } 3773b70f5097SNaohiro Aota 3774c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3775b7c35e81SAnand Jain if (ret) { 377605135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 377705135f59SDavid Sterba ret); 3778b7c35e81SAnand Jain goto fail_block_groups; 3779b7c35e81SAnand Jain } 3780b7c35e81SAnand Jain 378196f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 37825ac1d209SJeff Mahoney if (ret) { 378305135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3784b7c35e81SAnand Jain goto fail_fsdev_sysfs; 37855ac1d209SJeff Mahoney } 37865ac1d209SJeff Mahoney 3787c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3788c59021f8Sliubo if (ret) { 378905135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 37902365dd3cSAnand Jain goto fail_sysfs; 3791c59021f8Sliubo } 3792c59021f8Sliubo 37935b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 37941b1d1f66SJosef Bacik if (ret) { 379505135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 37962365dd3cSAnand Jain goto fail_sysfs; 37971b1d1f66SJosef Bacik } 37984330e183SQu Wenruo 379916beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 380016beac87SNaohiro Aota 38015c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 38025c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 380305135f59SDavid Sterba btrfs_warn(fs_info, 380452042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 38052365dd3cSAnand Jain goto fail_sysfs; 3806292fd7fcSStefan Behrens } 38079078a3e1SChris Mason 3808*33c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3809a74a4b97SChris Mason "btrfs-cleaner"); 381057506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 38112365dd3cSAnand Jain goto fail_sysfs; 3812a74a4b97SChris Mason 3813a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3814a74a4b97SChris Mason tree_root, 3815a74a4b97SChris Mason "btrfs-transaction"); 381657506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 38173f157a2fSChris Mason goto fail_cleaner; 3818a74a4b97SChris Mason 3819583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3820c289811cSChris Mason !fs_info->fs_devices->rotating) { 3821583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3822c289811cSChris Mason } 3823c289811cSChris Mason 3824572d9ab7SDavid Sterba /* 382501327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3826572d9ab7SDavid Sterba * not have to wait for transaction commit 3827572d9ab7SDavid Sterba */ 3828572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 38293818aea2SQu Wenruo 383021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38310b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 38322ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 38330b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3834cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 383521adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 383621adbd5cSStefan Behrens if (ret) 383705135f59SDavid Sterba btrfs_warn(fs_info, 383805135f59SDavid Sterba "failed to initialize integrity check module: %d", 383905135f59SDavid Sterba ret); 384021adbd5cSStefan Behrens } 384121adbd5cSStefan Behrens #endif 3842bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3843bcef60f2SArne Jansen if (ret) 3844bcef60f2SArne Jansen goto fail_trans_kthread; 384521adbd5cSStefan Behrens 3846fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3847fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3848fd708b81SJosef Bacik 384996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 385096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 38510b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3852e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 385363443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 385479787eaaSJeff Mahoney if (ret) { 385563443bf5SEric Sandeen err = ret; 385628c16cbbSWang Shilong goto fail_qgroup; 3857e556ce2cSYan Zheng } 3858e02119d5SChris Mason } 38591a40e23bSZheng Yan 386056e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 38613140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 38623140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3863f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3864315bf8efSJosef Bacik fs_info->fs_root = NULL; 3865bcef60f2SArne Jansen goto fail_qgroup; 38663140c9a3SDan Carpenter } 3867c289811cSChris Mason 3868bc98a42cSDavid Howells if (sb_rdonly(sb)) 38698cd29088SBoris Burkov goto clear_oneshot; 38702b6ba629SIlya Dryomov 387144c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 38722b6ba629SIlya Dryomov if (ret) { 38736bccf3abSJeff Mahoney close_ctree(fs_info); 38742b6ba629SIlya Dryomov return ret; 3875e3acc2a6SJosef Bacik } 3876b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3877b382a324SJan Schmidt 387844c0ca21SBoris Burkov if (fs_info->uuid_root && 387944c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 388044c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 388105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 388270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 388370f80175SStefan Behrens if (ret) { 388405135f59SDavid Sterba btrfs_warn(fs_info, 388505135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 38866bccf3abSJeff Mahoney close_ctree(fs_info); 388770f80175SStefan Behrens return ret; 388870f80175SStefan Behrens } 3889f7a81ea4SStefan Behrens } 389094846229SBoris Burkov 3891afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 389247ab2a6cSJosef Bacik 3893b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3894b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3895b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3896b4be6aefSJosef Bacik 38978cd29088SBoris Burkov clear_oneshot: 38988cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3899ad2b2c80SAl Viro return 0; 390039279cc3SChris Mason 3901bcef60f2SArne Jansen fail_qgroup: 3902bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 39037c2ca468SChris Mason fail_trans_kthread: 39047c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 39052ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3906faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 39073f157a2fSChris Mason fail_cleaner: 3908a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 39097c2ca468SChris Mason 39107c2ca468SChris Mason /* 39117c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 39127c2ca468SChris Mason * kthreads 39137c2ca468SChris Mason */ 39147c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 39157c2ca468SChris Mason 39162365dd3cSAnand Jain fail_sysfs: 39176618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 39182365dd3cSAnand Jain 3919b7c35e81SAnand Jain fail_fsdev_sysfs: 3920b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3921b7c35e81SAnand Jain 39221b1d1f66SJosef Bacik fail_block_groups: 392354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3924af31f5e5SChris Mason 3925af31f5e5SChris Mason fail_tree_roots: 39269e3aa805SJosef Bacik if (fs_info->data_reloc_root) 39279e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 39284273eaffSAnand Jain free_root_pointers(fs_info, true); 39292b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3930af31f5e5SChris Mason 393139279cc3SChris Mason fail_sb_buffer: 39327abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 39335cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 393416cdcec7SMiao Xie fail_alloc: 3935586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3936586e46e2SIlya Dryomov 39374543df7eSChris Mason iput(fs_info->btree_inode); 39387e662854SQinghuang Feng fail: 3939586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3940ad2b2c80SAl Viro return err; 3941eb60ceacSChris Mason } 3942663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3943eb60ceacSChris Mason 3944314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3945f2984462SChris Mason { 3946314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3947314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3948314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3949314b6dd0SJohannes Thumshirn struct page *page; 3950442a4f63SStefan Behrens 3951314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3952314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3953314b6dd0SJohannes Thumshirn 3954314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3955fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3956314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3957314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3958314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3959314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3960314b6dd0SJohannes Thumshirn SetPageError(page); 3961314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3962314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3963314b6dd0SJohannes Thumshirn } else { 3964314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3965f2984462SChris Mason } 3966314b6dd0SJohannes Thumshirn 3967314b6dd0SJohannes Thumshirn put_page(page); 3968314b6dd0SJohannes Thumshirn unlock_page(page); 3969314b6dd0SJohannes Thumshirn } 3970314b6dd0SJohannes Thumshirn 3971314b6dd0SJohannes Thumshirn bio_put(bio); 3972f2984462SChris Mason } 3973f2984462SChris Mason 39748f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 39758f32380dSJohannes Thumshirn int copy_num) 397629c36d72SAnand Jain { 397729c36d72SAnand Jain struct btrfs_super_block *super; 39788f32380dSJohannes Thumshirn struct page *page; 397912659251SNaohiro Aota u64 bytenr, bytenr_orig; 39808f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 398112659251SNaohiro Aota int ret; 398229c36d72SAnand Jain 398312659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 398412659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 398512659251SNaohiro Aota if (ret == -ENOENT) 398612659251SNaohiro Aota return ERR_PTR(-EINVAL); 398712659251SNaohiro Aota else if (ret) 398812659251SNaohiro Aota return ERR_PTR(ret); 398912659251SNaohiro Aota 3990cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 39918f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 399229c36d72SAnand Jain 39938f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 39948f32380dSJohannes Thumshirn if (IS_ERR(page)) 39958f32380dSJohannes Thumshirn return ERR_CAST(page); 399629c36d72SAnand Jain 39978f32380dSJohannes Thumshirn super = page_address(page); 399896c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 399996c2e067SAnand Jain btrfs_release_disk_super(super); 400096c2e067SAnand Jain return ERR_PTR(-ENODATA); 400196c2e067SAnand Jain } 400296c2e067SAnand Jain 400312659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 40048f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40058f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 400629c36d72SAnand Jain } 400729c36d72SAnand Jain 40088f32380dSJohannes Thumshirn return super; 400929c36d72SAnand Jain } 401029c36d72SAnand Jain 401129c36d72SAnand Jain 40128f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 4013a512bbf8SYan Zheng { 40148f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 4015a512bbf8SYan Zheng int i; 4016a512bbf8SYan Zheng u64 transid = 0; 4017a512bbf8SYan Zheng 4018a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 4019a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 4020a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 4021a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 4022a512bbf8SYan Zheng */ 4023a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 40248f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 40258f32380dSJohannes Thumshirn if (IS_ERR(super)) 4026a512bbf8SYan Zheng continue; 4027a512bbf8SYan Zheng 4028a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 40298f32380dSJohannes Thumshirn if (latest) 40308f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40318f32380dSJohannes Thumshirn 40328f32380dSJohannes Thumshirn latest = super; 4033a512bbf8SYan Zheng transid = btrfs_super_generation(super); 4034a512bbf8SYan Zheng } 4035a512bbf8SYan Zheng } 403692fc03fbSAnand Jain 40378f32380dSJohannes Thumshirn return super; 4038a512bbf8SYan Zheng } 4039a512bbf8SYan Zheng 40404eedeb75SHisashi Hifumi /* 4041abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 4042314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 40434eedeb75SHisashi Hifumi * 4044abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 4045abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 4046abbb3b8eSDavid Sterba * same for write and wait phases. 40474eedeb75SHisashi Hifumi * 4048314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 40494eedeb75SHisashi Hifumi */ 4050a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 4051abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 4052a512bbf8SYan Zheng { 4053d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 4054314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 4055d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 4056a512bbf8SYan Zheng int i; 4057a512bbf8SYan Zheng int errors = 0; 405812659251SNaohiro Aota int ret; 405912659251SNaohiro Aota u64 bytenr, bytenr_orig; 4060a512bbf8SYan Zheng 4061a512bbf8SYan Zheng if (max_mirrors == 0) 4062a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4063a512bbf8SYan Zheng 4064d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4065d5178578SJohannes Thumshirn 4066a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4067314b6dd0SJohannes Thumshirn struct page *page; 4068314b6dd0SJohannes Thumshirn struct bio *bio; 4069314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4070314b6dd0SJohannes Thumshirn 407112659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 407212659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 407312659251SNaohiro Aota if (ret == -ENOENT) { 407412659251SNaohiro Aota continue; 407512659251SNaohiro Aota } else if (ret < 0) { 407612659251SNaohiro Aota btrfs_err(device->fs_info, 407712659251SNaohiro Aota "couldn't get super block location for mirror %d", 407812659251SNaohiro Aota i); 407912659251SNaohiro Aota errors++; 408012659251SNaohiro Aota continue; 408112659251SNaohiro Aota } 4082935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4083935e5cc9SMiao Xie device->commit_total_bytes) 4084a512bbf8SYan Zheng break; 4085a512bbf8SYan Zheng 408612659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4087a512bbf8SYan Zheng 4088fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4089fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4090fd08001fSEric Biggers sb->csum); 4091a512bbf8SYan Zheng 4092314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4093314b6dd0SJohannes Thumshirn GFP_NOFS); 4094314b6dd0SJohannes Thumshirn if (!page) { 4095fb456252SJeff Mahoney btrfs_err(device->fs_info, 4096314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4097f14d104dSDavid Sterba bytenr); 4098634554dcSJosef Bacik errors++; 4099634554dcSJosef Bacik continue; 4100634554dcSJosef Bacik } 4101634554dcSJosef Bacik 4102314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4103314b6dd0SJohannes Thumshirn get_page(page); 4104a512bbf8SYan Zheng 4105314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4106314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4107a512bbf8SYan Zheng 4108387125fcSChris Mason /* 4109314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4110314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4111314b6dd0SJohannes Thumshirn * checking. 4112387125fcSChris Mason */ 4113314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 4114314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 4115314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4116314b6dd0SJohannes Thumshirn bio->bi_private = device; 4117314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4118314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4119314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4120314b6dd0SJohannes Thumshirn 4121314b6dd0SJohannes Thumshirn /* 4122314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4123314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4124314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4125314b6dd0SJohannes Thumshirn */ 4126314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 41271b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4128314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4129314b6dd0SJohannes Thumshirn 4130314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 41318376d9e1SNaohiro Aota 41328376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 41338376d9e1SNaohiro Aota errors++; 4134a512bbf8SYan Zheng } 4135a512bbf8SYan Zheng return errors < i ? 0 : -1; 4136a512bbf8SYan Zheng } 4137a512bbf8SYan Zheng 4138387125fcSChris Mason /* 4139abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4140abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4141abbb3b8eSDavid Sterba * 4142314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4143abbb3b8eSDavid Sterba * date. 4144abbb3b8eSDavid Sterba */ 4145abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4146abbb3b8eSDavid Sterba { 4147abbb3b8eSDavid Sterba int i; 4148abbb3b8eSDavid Sterba int errors = 0; 4149b6a535faSHoward McLauchlan bool primary_failed = false; 415012659251SNaohiro Aota int ret; 4151abbb3b8eSDavid Sterba u64 bytenr; 4152abbb3b8eSDavid Sterba 4153abbb3b8eSDavid Sterba if (max_mirrors == 0) 4154abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4155abbb3b8eSDavid Sterba 4156abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4157314b6dd0SJohannes Thumshirn struct page *page; 4158314b6dd0SJohannes Thumshirn 415912659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 416012659251SNaohiro Aota if (ret == -ENOENT) { 416112659251SNaohiro Aota break; 416212659251SNaohiro Aota } else if (ret < 0) { 416312659251SNaohiro Aota errors++; 416412659251SNaohiro Aota if (i == 0) 416512659251SNaohiro Aota primary_failed = true; 416612659251SNaohiro Aota continue; 416712659251SNaohiro Aota } 4168abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4169abbb3b8eSDavid Sterba device->commit_total_bytes) 4170abbb3b8eSDavid Sterba break; 4171abbb3b8eSDavid Sterba 4172314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4173314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4174314b6dd0SJohannes Thumshirn if (!page) { 4175abbb3b8eSDavid Sterba errors++; 4176b6a535faSHoward McLauchlan if (i == 0) 4177b6a535faSHoward McLauchlan primary_failed = true; 4178abbb3b8eSDavid Sterba continue; 4179abbb3b8eSDavid Sterba } 4180314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4181314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4182314b6dd0SJohannes Thumshirn if (PageError(page)) { 4183abbb3b8eSDavid Sterba errors++; 4184b6a535faSHoward McLauchlan if (i == 0) 4185b6a535faSHoward McLauchlan primary_failed = true; 4186b6a535faSHoward McLauchlan } 4187abbb3b8eSDavid Sterba 4188314b6dd0SJohannes Thumshirn /* Drop our reference */ 4189314b6dd0SJohannes Thumshirn put_page(page); 4190abbb3b8eSDavid Sterba 4191314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4192314b6dd0SJohannes Thumshirn put_page(page); 4193abbb3b8eSDavid Sterba } 4194abbb3b8eSDavid Sterba 4195b6a535faSHoward McLauchlan /* log error, force error return */ 4196b6a535faSHoward McLauchlan if (primary_failed) { 4197b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4198b6a535faSHoward McLauchlan device->devid); 4199b6a535faSHoward McLauchlan return -1; 4200b6a535faSHoward McLauchlan } 4201b6a535faSHoward McLauchlan 4202abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4203abbb3b8eSDavid Sterba } 4204abbb3b8eSDavid Sterba 4205abbb3b8eSDavid Sterba /* 4206387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4207387125fcSChris Mason * for the barrier when it is done 4208387125fcSChris Mason */ 42094246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4210387125fcSChris Mason { 4211387125fcSChris Mason complete(bio->bi_private); 4212387125fcSChris Mason } 4213387125fcSChris Mason 4214387125fcSChris Mason /* 42154fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 42164fc6441aSAnand Jain * done in the waiting counterpart. 4217387125fcSChris Mason */ 42184fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4219387125fcSChris Mason { 4220e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 4221387125fcSChris Mason 4222a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4223a91cf0ffSWang Yugui /* 4224a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4225a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4226a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4227a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4228a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4229a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4230a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4231a91cf0ffSWang Yugui * non-debug builds. 4232a91cf0ffSWang Yugui */ 4233a91cf0ffSWang Yugui struct request_queue *q = bdev_get_queue(device->bdev); 4234c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 42354fc6441aSAnand Jain return; 4236a91cf0ffSWang Yugui #endif 4237387125fcSChris Mason 4238e0ae9994SDavid Sterba bio_reset(bio); 4239387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 424074d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 42418d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 4242387125fcSChris Mason init_completion(&device->flush_wait); 4243387125fcSChris Mason bio->bi_private = &device->flush_wait; 4244387125fcSChris Mason 424543a01111SLu Fengqi btrfsic_submit_bio(bio); 42461c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42474fc6441aSAnand Jain } 4248387125fcSChris Mason 42494fc6441aSAnand Jain /* 42504fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 42514fc6441aSAnand Jain */ 42528c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 42534fc6441aSAnand Jain { 42544fc6441aSAnand Jain struct bio *bio = device->flush_bio; 42554fc6441aSAnand Jain 42561c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 425758efbc9fSOmar Sandoval return BLK_STS_OK; 42584fc6441aSAnand Jain 42591c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42602980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 42614fc6441aSAnand Jain 42628c27cb35SLinus Torvalds return bio->bi_status; 4263387125fcSChris Mason } 4264387125fcSChris Mason 4265d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4266401b41e5SAnand Jain { 42676528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4268401b41e5SAnand Jain return -EIO; 4269387125fcSChris Mason return 0; 4270387125fcSChris Mason } 4271387125fcSChris Mason 4272387125fcSChris Mason /* 4273387125fcSChris Mason * send an empty flush down to each device in parallel, 4274387125fcSChris Mason * then wait for them 4275387125fcSChris Mason */ 4276387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4277387125fcSChris Mason { 4278387125fcSChris Mason struct list_head *head; 4279387125fcSChris Mason struct btrfs_device *dev; 42805af3e8ccSStefan Behrens int errors_wait = 0; 42814e4cbee9SChristoph Hellwig blk_status_t ret; 4282387125fcSChris Mason 42831538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4284387125fcSChris Mason /* send down all the barriers */ 4285387125fcSChris Mason head = &info->fs_devices->devices; 42861538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4287e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4288f88ba6a2SHidetoshi Seto continue; 4289cea7c8bfSAnand Jain if (!dev->bdev) 4290387125fcSChris Mason continue; 4291e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4292ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4293387125fcSChris Mason continue; 4294387125fcSChris Mason 42954fc6441aSAnand Jain write_dev_flush(dev); 429658efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4297387125fcSChris Mason } 4298387125fcSChris Mason 4299387125fcSChris Mason /* wait for all the barriers */ 43001538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4301e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4302f88ba6a2SHidetoshi Seto continue; 4303387125fcSChris Mason if (!dev->bdev) { 43045af3e8ccSStefan Behrens errors_wait++; 4305387125fcSChris Mason continue; 4306387125fcSChris Mason } 4307e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4308ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4309387125fcSChris Mason continue; 4310387125fcSChris Mason 43114fc6441aSAnand Jain ret = wait_dev_flush(dev); 4312401b41e5SAnand Jain if (ret) { 4313401b41e5SAnand Jain dev->last_flush_error = ret; 431466b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 431566b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 43165af3e8ccSStefan Behrens errors_wait++; 4317387125fcSChris Mason } 4318401b41e5SAnand Jain } 4319401b41e5SAnand Jain 4320cea7c8bfSAnand Jain if (errors_wait) { 4321401b41e5SAnand Jain /* 4322401b41e5SAnand Jain * At some point we need the status of all disks 4323401b41e5SAnand Jain * to arrive at the volume status. So error checking 4324401b41e5SAnand Jain * is being pushed to a separate loop. 4325401b41e5SAnand Jain */ 4326d10b82feSQu Wenruo return check_barrier_error(info); 4327401b41e5SAnand Jain } 4328387125fcSChris Mason return 0; 4329387125fcSChris Mason } 4330387125fcSChris Mason 4331943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4332943c6e99SZhao Lei { 43338789f4feSZhao Lei int raid_type; 43348789f4feSZhao Lei int min_tolerated = INT_MAX; 4335943c6e99SZhao Lei 43368789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 43378789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 43388c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43398789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 43408789f4feSZhao Lei tolerated_failures); 4341943c6e99SZhao Lei 43428789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 43438789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 43448789f4feSZhao Lei continue; 434541a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 43468789f4feSZhao Lei continue; 43478c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43488789f4feSZhao Lei btrfs_raid_array[raid_type]. 43498789f4feSZhao Lei tolerated_failures); 43508789f4feSZhao Lei } 4351943c6e99SZhao Lei 43528789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4353ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 43548789f4feSZhao Lei min_tolerated = 0; 43558789f4feSZhao Lei } 43568789f4feSZhao Lei 43578789f4feSZhao Lei return min_tolerated; 4358943c6e99SZhao Lei } 4359943c6e99SZhao Lei 4360eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4361f2984462SChris Mason { 4362e5e9a520SChris Mason struct list_head *head; 4363f2984462SChris Mason struct btrfs_device *dev; 4364a061fc8dSChris Mason struct btrfs_super_block *sb; 4365f2984462SChris Mason struct btrfs_dev_item *dev_item; 4366f2984462SChris Mason int ret; 4367f2984462SChris Mason int do_barriers; 4368a236aed1SChris Mason int max_errors; 4369a236aed1SChris Mason int total_errors = 0; 4370a061fc8dSChris Mason u64 flags; 4371f2984462SChris Mason 43720b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4373fed3b381SLiu Bo 4374fed3b381SLiu Bo /* 4375fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4376fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4377fed3b381SLiu Bo * been consistent on disk. 4378fed3b381SLiu Bo */ 4379fed3b381SLiu Bo if (max_mirrors == 0) 43800b246afaSJeff Mahoney backup_super_roots(fs_info); 4381f2984462SChris Mason 43820b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4383a061fc8dSChris Mason dev_item = &sb->dev_item; 4384e5e9a520SChris Mason 43850b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 43860b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 43870b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4388387125fcSChris Mason 43895af3e8ccSStefan Behrens if (do_barriers) { 43900b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 43915af3e8ccSStefan Behrens if (ret) { 43925af3e8ccSStefan Behrens mutex_unlock( 43930b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 43940b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 43955af3e8ccSStefan Behrens "errors while submitting device barriers."); 43965af3e8ccSStefan Behrens return ret; 43975af3e8ccSStefan Behrens } 43985af3e8ccSStefan Behrens } 4399387125fcSChris Mason 44001538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4401dfe25020SChris Mason if (!dev->bdev) { 4402dfe25020SChris Mason total_errors++; 4403dfe25020SChris Mason continue; 4404dfe25020SChris Mason } 4405e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4406ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4407dfe25020SChris Mason continue; 4408dfe25020SChris Mason 44092b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4410a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4411a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 44127df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4413935e5cc9SMiao Xie dev->commit_total_bytes); 4414ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4415ce7213c7SMiao Xie dev->commit_bytes_used); 4416a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4417a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4418a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4419a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 44207239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 44217239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4422a512bbf8SYan Zheng 4423a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4424a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4425f2984462SChris Mason 442675cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 442775cb857dSQu Wenruo if (ret < 0) { 442875cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 442975cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 443075cb857dSQu Wenruo "unexpected superblock corruption detected"); 443175cb857dSQu Wenruo return -EUCLEAN; 443275cb857dSQu Wenruo } 443375cb857dSQu Wenruo 4434abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4435a236aed1SChris Mason if (ret) 4436a236aed1SChris Mason total_errors++; 4437f2984462SChris Mason } 4438a236aed1SChris Mason if (total_errors > max_errors) { 44390b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4440d397712bSChris Mason total_errors); 44410b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 444279787eaaSJeff Mahoney 44439d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 44440b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44450b246afaSJeff Mahoney "%d errors while writing supers", 44460b246afaSJeff Mahoney total_errors); 44479d565ba4SStefan Behrens return -EIO; 4448a236aed1SChris Mason } 4449f2984462SChris Mason 4450a512bbf8SYan Zheng total_errors = 0; 44511538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4452dfe25020SChris Mason if (!dev->bdev) 4453dfe25020SChris Mason continue; 4454e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4455ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4456dfe25020SChris Mason continue; 4457dfe25020SChris Mason 4458abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4459a512bbf8SYan Zheng if (ret) 44601259ab75SChris Mason total_errors++; 4461f2984462SChris Mason } 44620b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4463a236aed1SChris Mason if (total_errors > max_errors) { 44640b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44650b246afaSJeff Mahoney "%d errors while writing supers", 44660b246afaSJeff Mahoney total_errors); 446779787eaaSJeff Mahoney return -EIO; 4468a236aed1SChris Mason } 4469f2984462SChris Mason return 0; 4470f2984462SChris Mason } 4471f2984462SChris Mason 4472cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4473cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4474cb517eabSMiao Xie struct btrfs_root *root) 44752619ba1fSChris Mason { 44764785e24fSJosef Bacik bool drop_ref = false; 44774785e24fSJosef Bacik 44784df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 44792619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 44802619ba1fSChris Mason (unsigned long)root->root_key.objectid); 4481af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 44824785e24fSJosef Bacik drop_ref = true; 44834df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 448476dda93cSYan, Zheng 448584961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4486ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 44871c1ea4f7SLiu Bo if (root->reloc_root) { 448800246528SJosef Bacik btrfs_put_root(root->reloc_root); 44891c1ea4f7SLiu Bo root->reloc_root = NULL; 44901c1ea4f7SLiu Bo } 44911c1ea4f7SLiu Bo } 44923321719eSLiu Bo 44934785e24fSJosef Bacik if (drop_ref) 449400246528SJosef Bacik btrfs_put_root(root); 44952619ba1fSChris Mason } 44962619ba1fSChris Mason 4497c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4498c146afadSYan Zheng { 4499c146afadSYan Zheng u64 root_objectid = 0; 4500c146afadSYan Zheng struct btrfs_root *gang[8]; 450165d33fd7SQu Wenruo int i = 0; 450265d33fd7SQu Wenruo int err = 0; 450365d33fd7SQu Wenruo unsigned int ret = 0; 4504c146afadSYan Zheng 4505c146afadSYan Zheng while (1) { 4506efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4507c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4508c146afadSYan Zheng (void **)gang, root_objectid, 4509c146afadSYan Zheng ARRAY_SIZE(gang)); 451065d33fd7SQu Wenruo if (!ret) { 4511efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4512c146afadSYan Zheng break; 451365d33fd7SQu Wenruo } 45145d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 451566b4ffd1SJosef Bacik 451665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 451765d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 451865d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 451965d33fd7SQu Wenruo gang[i] = NULL; 452065d33fd7SQu Wenruo continue; 452165d33fd7SQu Wenruo } 452265d33fd7SQu Wenruo /* grab all the search result for later use */ 452300246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 452465d33fd7SQu Wenruo } 4525efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 452665d33fd7SQu Wenruo 452765d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 452865d33fd7SQu Wenruo if (!gang[i]) 452965d33fd7SQu Wenruo continue; 4530c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 453166b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 453266b4ffd1SJosef Bacik if (err) 453365d33fd7SQu Wenruo break; 453400246528SJosef Bacik btrfs_put_root(gang[i]); 4535c146afadSYan Zheng } 4536c146afadSYan Zheng root_objectid++; 4537c146afadSYan Zheng } 453865d33fd7SQu Wenruo 453965d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 454065d33fd7SQu Wenruo for (; i < ret; i++) { 454165d33fd7SQu Wenruo if (gang[i]) 454200246528SJosef Bacik btrfs_put_root(gang[i]); 454365d33fd7SQu Wenruo } 454465d33fd7SQu Wenruo return err; 4545c146afadSYan Zheng } 4546c146afadSYan Zheng 45476bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4548c146afadSYan Zheng { 45496bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4550c146afadSYan Zheng struct btrfs_trans_handle *trans; 4551c146afadSYan Zheng 45520b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 45532ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 45540b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 45550b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4556c71bf099SYan, Zheng 4557c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 45580b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45590b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4560c71bf099SYan, Zheng 45617a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 45623612b495STsutomu Itoh if (IS_ERR(trans)) 45633612b495STsutomu Itoh return PTR_ERR(trans); 45643a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4565c146afadSYan Zheng } 4566c146afadSYan Zheng 456736c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 456836c86a9eSQu Wenruo { 456936c86a9eSQu Wenruo struct btrfs_transaction *trans; 457036c86a9eSQu Wenruo struct btrfs_transaction *tmp; 457136c86a9eSQu Wenruo bool found = false; 457236c86a9eSQu Wenruo 457336c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 457436c86a9eSQu Wenruo return; 457536c86a9eSQu Wenruo 457636c86a9eSQu Wenruo /* 457736c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 457836c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 457936c86a9eSQu Wenruo */ 458036c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 458136c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 458236c86a9eSQu Wenruo struct extent_state *cached = NULL; 458336c86a9eSQu Wenruo u64 dirty_bytes = 0; 458436c86a9eSQu Wenruo u64 cur = 0; 458536c86a9eSQu Wenruo u64 found_start; 458636c86a9eSQu Wenruo u64 found_end; 458736c86a9eSQu Wenruo 458836c86a9eSQu Wenruo found = true; 458936c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 459036c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 459136c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 459236c86a9eSQu Wenruo cur = found_end + 1; 459336c86a9eSQu Wenruo } 459436c86a9eSQu Wenruo btrfs_warn(fs_info, 459536c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 459636c86a9eSQu Wenruo trans->transid, dirty_bytes); 459736c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 459836c86a9eSQu Wenruo 459936c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 460036c86a9eSQu Wenruo fs_info->running_transaction = NULL; 460136c86a9eSQu Wenruo list_del_init(&trans->list); 460236c86a9eSQu Wenruo 460336c86a9eSQu Wenruo btrfs_put_transaction(trans); 460436c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 460536c86a9eSQu Wenruo } 460636c86a9eSQu Wenruo ASSERT(!found); 460736c86a9eSQu Wenruo } 460836c86a9eSQu Wenruo 4609b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4610eb60ceacSChris Mason { 4611c146afadSYan Zheng int ret; 4612e089f05cSChris Mason 4613afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4614d6fd0ae2SOmar Sandoval /* 4615d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4616d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4617d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4618d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4619d6fd0ae2SOmar Sandoval */ 4620d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4621a2135011SChris Mason 4622b4be6aefSJosef Bacik /* 4623b4be6aefSJosef Bacik * If we had UNFINISHED_DROPS we could still be processing them, so 4624b4be6aefSJosef Bacik * clear that bit and wake up relocation so it can stop. 4625b4be6aefSJosef Bacik */ 4626b4be6aefSJosef Bacik btrfs_wake_unfinished_drop(fs_info); 4627b4be6aefSJosef Bacik 46287343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4629d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 46307343dd61SJustin Maggard 4631803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4632803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4633803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4634803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4635803b2f54SStefan Behrens 4636837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4637aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4638837d5b6eSIlya Dryomov 46398dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 46408dabb742SStefan Behrens 4641aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 46424cb5300bSChris Mason 46434cb5300bSChris Mason /* wait for any defraggers to finish */ 46444cb5300bSChris Mason wait_event(fs_info->transaction_wait, 46454cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 46464cb5300bSChris Mason 46474cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 464826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 46494cb5300bSChris Mason 465021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 465157056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4652576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 465321c7e756SMiao Xie 465418bb8bbfSJohannes Thumshirn cancel_work_sync(&fs_info->reclaim_bgs_work); 465518bb8bbfSJohannes Thumshirn 4656b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4657b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4658b0643e59SDennis Zhou 4659bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4660e44163e1SJeff Mahoney /* 4661d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4662d6fd0ae2SOmar Sandoval * unused block groups. 4663e44163e1SJeff Mahoney */ 46640b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4665e44163e1SJeff Mahoney 4666f0cc2cd7SFilipe Manana /* 4667f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4668f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4669f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4670f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4671f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4672f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4673f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4674f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4675f0cc2cd7SFilipe Manana * This is a very rare case. 4676f0cc2cd7SFilipe Manana */ 4677f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4678f0cc2cd7SFilipe Manana 46796bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4680d397712bSChris Mason if (ret) 468104892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4682c146afadSYan Zheng } 4683ed2ff2cbSChris Mason 468484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 46852ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4686acce952bSliubo 4687e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4688e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 46898929ecfaSYan, Zheng 4690e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4691afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4692f25784b3SYan Zheng 46935958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 46945958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 46955958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 46965958253cSQu Wenruo } 46975958253cSQu Wenruo 469804892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4699fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4700bcef60f2SArne Jansen 4701963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 470204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4703963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4704b0c68f8bSChris Mason } 470531153d81SYan Zheng 47065deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 47074297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 47085deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 47094297ff84SJosef Bacik 47106618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4711b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 47125ac1d209SJeff Mahoney 47131a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 47141a4319ccSLiu Bo 4715de348ee0SWang Shilong /* 4716de348ee0SWang Shilong * we must make sure there is not any read request to 4717de348ee0SWang Shilong * submit after we stopping all workers. 4718de348ee0SWang Shilong */ 4719de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 472096192499SJosef Bacik btrfs_stop_all_workers(fs_info); 472196192499SJosef Bacik 47220a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 472336c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 47240a31daa4SFilipe Manana 4725afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 47264273eaffSAnand Jain free_root_pointers(fs_info, true); 47278c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 47289ad6b7bcSChris Mason 47294e19443dSJosef Bacik /* 47304e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 47314e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 47324e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 47334e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 47344e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 47354e19443dSJosef Bacik */ 47364e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 47374e19443dSJosef Bacik 473813e6c37bSJosef Bacik iput(fs_info->btree_inode); 4739d6bfde87SChris Mason 474021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 47410b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 47422ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 474321adbd5cSStefan Behrens #endif 474421adbd5cSStefan Behrens 47450b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 474668c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4747eb60ceacSChris Mason } 4748eb60ceacSChris Mason 4749b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4750b9fab919SChris Mason int atomic) 4751ccd467d6SChris Mason { 47521259ab75SChris Mason int ret; 4753727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 47541259ab75SChris Mason 47550b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 47561259ab75SChris Mason if (!ret) 47571259ab75SChris Mason return ret; 47581259ab75SChris Mason 47591259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4760b9fab919SChris Mason parent_transid, atomic); 4761b9fab919SChris Mason if (ret == -EAGAIN) 4762b9fab919SChris Mason return ret; 47631259ab75SChris Mason return !ret; 47645f39d397SChris Mason } 47656702ed49SChris Mason 47665f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 47675f39d397SChris Mason { 47682f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 47695f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4770b9473439SChris Mason int was_dirty; 4771b4ce94deSChris Mason 477206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 477306ea65a3SJosef Bacik /* 477406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 477552042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 477606ea65a3SJosef Bacik * outside of the sanity tests. 477706ea65a3SJosef Bacik */ 4778b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 477906ea65a3SJosef Bacik return; 478006ea65a3SJosef Bacik #endif 478149d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 47820b246afaSJeff Mahoney if (transid != fs_info->generation) 47835d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 47840b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 47850b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4786e2d84521SMiao Xie if (!was_dirty) 4787104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4788e2d84521SMiao Xie buf->len, 47890b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 47901f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 479169fc6cbbSQu Wenruo /* 479269fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 479369fc6cbbSQu Wenruo * but item data not updated. 479469fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 479569fc6cbbSQu Wenruo */ 479669fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4797cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4798a4f78750SDavid Sterba btrfs_print_leaf(buf); 47991f21ef0aSFilipe Manana ASSERT(0); 48001f21ef0aSFilipe Manana } 48011f21ef0aSFilipe Manana #endif 4802eb60ceacSChris Mason } 4803eb60ceacSChris Mason 48042ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4805b53d3f5dSLiu Bo int flush_delayed) 480635b7e476SChris Mason { 4807188de649SChris Mason /* 4808188de649SChris Mason * looks as though older kernels can get into trouble with 4809188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4810188de649SChris Mason */ 4811e2d84521SMiao Xie int ret; 4812d6bfde87SChris Mason 48136933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4814d6bfde87SChris Mason return; 4815d6bfde87SChris Mason 4816b53d3f5dSLiu Bo if (flush_delayed) 48172ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 481816cdcec7SMiao Xie 4819d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4820d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4821d814a491SEthan Lien fs_info->dirty_metadata_batch); 4822e2d84521SMiao Xie if (ret > 0) { 48230b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 482416cdcec7SMiao Xie } 482516cdcec7SMiao Xie } 482616cdcec7SMiao Xie 48272ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 482816cdcec7SMiao Xie { 48292ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 483035b7e476SChris Mason } 4831b53d3f5dSLiu Bo 48322ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4833b53d3f5dSLiu Bo { 48342ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4835d6bfde87SChris Mason } 48366b80053dSChris Mason 4837581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4838581c1760SQu Wenruo struct btrfs_key *first_key) 48396b80053dSChris Mason { 48405ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4841581c1760SQu Wenruo level, first_key); 48426b80053dSChris Mason } 48430da5468fSChris Mason 48442ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4845acce952bSliubo { 4846fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4847fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4848fe816d0fSNikolay Borisov 48490b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 48502ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 48510b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4852acce952bSliubo 48530b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 48540b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4855acce952bSliubo } 4856acce952bSliubo 4857ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4858ef67963dSJosef Bacik { 4859ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4860ef67963dSJosef Bacik u64 root_objectid = 0; 4861ef67963dSJosef Bacik int ret; 4862ef67963dSJosef Bacik 4863ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4864ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4865ef67963dSJosef Bacik (void **)gang, root_objectid, 4866ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4867ef67963dSJosef Bacik int i; 4868ef67963dSJosef Bacik 4869ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4870ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4871ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4872ef67963dSJosef Bacik 4873ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4874ef67963dSJosef Bacik if (!gang[i]) 4875ef67963dSJosef Bacik continue; 4876ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4877ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4878ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4879ef67963dSJosef Bacik } 4880ef67963dSJosef Bacik root_objectid++; 4881ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4882ef67963dSJosef Bacik } 4883ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4884ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4885ef67963dSJosef Bacik } 4886ef67963dSJosef Bacik 4887143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4888acce952bSliubo { 4889acce952bSliubo struct btrfs_ordered_extent *ordered; 4890acce952bSliubo 4891199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4892779880efSJosef Bacik /* 4893779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4894779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4895779880efSJosef Bacik */ 4896199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4897779880efSJosef Bacik root_extent_list) 4898779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4899199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4900199c2a9cSMiao Xie } 4901199c2a9cSMiao Xie 4902199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4903199c2a9cSMiao Xie { 4904199c2a9cSMiao Xie struct btrfs_root *root; 4905199c2a9cSMiao Xie struct list_head splice; 4906199c2a9cSMiao Xie 4907199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4908199c2a9cSMiao Xie 4909199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4910199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4911199c2a9cSMiao Xie while (!list_empty(&splice)) { 4912199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4913199c2a9cSMiao Xie ordered_root); 49141de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 49151de2cfdeSJosef Bacik &fs_info->ordered_roots); 4916199c2a9cSMiao Xie 49172a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4918199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4919199c2a9cSMiao Xie 49202a85d9caSLiu Bo cond_resched(); 49212a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4922199c2a9cSMiao Xie } 4923199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 492474d5d229SJosef Bacik 492574d5d229SJosef Bacik /* 492674d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 492774d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 492874d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 492974d5d229SJosef Bacik * actually get run and error out properly. 493074d5d229SJosef Bacik */ 493174d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4932acce952bSliubo } 4933acce952bSliubo 493435a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 49352ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4936acce952bSliubo { 4937acce952bSliubo struct rb_node *node; 4938acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4939acce952bSliubo struct btrfs_delayed_ref_node *ref; 4940acce952bSliubo int ret = 0; 4941acce952bSliubo 4942acce952bSliubo delayed_refs = &trans->delayed_refs; 4943acce952bSliubo 4944acce952bSliubo spin_lock(&delayed_refs->lock); 4945d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4946cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4947b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4948acce952bSliubo return ret; 4949acce952bSliubo } 4950acce952bSliubo 49515c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4952d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 49530e0adbcfSJosef Bacik struct rb_node *n; 4954e78417d1SJosef Bacik bool pin_bytes = false; 4955acce952bSliubo 4956d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4957d7df2c79SJosef Bacik href_node); 49583069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4959b939d1abSJosef Bacik continue; 49603069bd26SJosef Bacik 4961d7df2c79SJosef Bacik spin_lock(&head->lock); 4962e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 49630e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 49640e0adbcfSJosef Bacik ref_node); 4965d7df2c79SJosef Bacik ref->in_tree = 0; 4966e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 49670e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 49681d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 49691d57ee94SWang Xiaoguang list_del(&ref->add_list); 4970d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4971d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4972d7df2c79SJosef Bacik } 497354067ae9SJosef Bacik if (head->must_insert_reserved) 4974e78417d1SJosef Bacik pin_bytes = true; 497578a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4976fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4977d7df2c79SJosef Bacik spin_unlock(&head->lock); 4978acce952bSliubo spin_unlock(&delayed_refs->lock); 4979e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4980acce952bSliubo 4981f603bb94SNikolay Borisov if (pin_bytes) { 4982f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4983f603bb94SNikolay Borisov 4984f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4985f603bb94SNikolay Borisov BUG_ON(!cache); 4986f603bb94SNikolay Borisov 4987f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4988f603bb94SNikolay Borisov spin_lock(&cache->lock); 4989f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4990f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4991f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4992f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4993f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4994f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4995f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4996f603bb94SNikolay Borisov 4997f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4998f603bb94SNikolay Borisov 4999f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 5000f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 5001f603bb94SNikolay Borisov } 500231890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 5003d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 5004acce952bSliubo cond_resched(); 5005acce952bSliubo spin_lock(&delayed_refs->lock); 5006acce952bSliubo } 500781f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 5008acce952bSliubo 5009acce952bSliubo spin_unlock(&delayed_refs->lock); 5010acce952bSliubo 5011acce952bSliubo return ret; 5012acce952bSliubo } 5013acce952bSliubo 5014143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 5015acce952bSliubo { 5016acce952bSliubo struct btrfs_inode *btrfs_inode; 5017acce952bSliubo struct list_head splice; 5018acce952bSliubo 5019acce952bSliubo INIT_LIST_HEAD(&splice); 5020acce952bSliubo 5021eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5022eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 5023acce952bSliubo 5024acce952bSliubo while (!list_empty(&splice)) { 5025fe816d0fSNikolay Borisov struct inode *inode = NULL; 5026eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 5027acce952bSliubo delalloc_inodes); 5028fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 5029eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5030acce952bSliubo 5031fe816d0fSNikolay Borisov /* 5032fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 5033fe816d0fSNikolay Borisov * meanwhile. 5034fe816d0fSNikolay Borisov */ 5035fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 5036fe816d0fSNikolay Borisov if (inode) { 5037fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 5038fe816d0fSNikolay Borisov iput(inode); 5039fe816d0fSNikolay Borisov } 5040eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5041acce952bSliubo } 5042eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5043eb73c1b7SMiao Xie } 5044eb73c1b7SMiao Xie 5045eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 5046eb73c1b7SMiao Xie { 5047eb73c1b7SMiao Xie struct btrfs_root *root; 5048eb73c1b7SMiao Xie struct list_head splice; 5049eb73c1b7SMiao Xie 5050eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 5051eb73c1b7SMiao Xie 5052eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5053eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 5054eb73c1b7SMiao Xie while (!list_empty(&splice)) { 5055eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 5056eb73c1b7SMiao Xie delalloc_root); 505700246528SJosef Bacik root = btrfs_grab_root(root); 5058eb73c1b7SMiao Xie BUG_ON(!root); 5059eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5060eb73c1b7SMiao Xie 5061eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 506200246528SJosef Bacik btrfs_put_root(root); 5063eb73c1b7SMiao Xie 5064eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5065eb73c1b7SMiao Xie } 5066eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5067acce952bSliubo } 5068acce952bSliubo 50692ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5070acce952bSliubo struct extent_io_tree *dirty_pages, 5071acce952bSliubo int mark) 5072acce952bSliubo { 5073acce952bSliubo int ret; 5074acce952bSliubo struct extent_buffer *eb; 5075acce952bSliubo u64 start = 0; 5076acce952bSliubo u64 end; 5077acce952bSliubo 5078acce952bSliubo while (1) { 5079acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5080e6138876SJosef Bacik mark, NULL); 5081acce952bSliubo if (ret) 5082acce952bSliubo break; 5083acce952bSliubo 508491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5085acce952bSliubo while (start <= end) { 50860b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 50870b246afaSJeff Mahoney start += fs_info->nodesize; 5088fd8b2b61SJosef Bacik if (!eb) 5089acce952bSliubo continue; 5090fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5091acce952bSliubo 5092fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5093fd8b2b61SJosef Bacik &eb->bflags)) 5094fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5095fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5096acce952bSliubo } 5097acce952bSliubo } 5098acce952bSliubo 5099acce952bSliubo return ret; 5100acce952bSliubo } 5101acce952bSliubo 51022ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5103fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5104acce952bSliubo { 5105acce952bSliubo u64 start; 5106acce952bSliubo u64 end; 5107acce952bSliubo int ret; 5108acce952bSliubo 5109acce952bSliubo while (1) { 51100e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 51110e6ec385SFilipe Manana 5112fcd5e742SLu Fengqi /* 5113fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5114fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5115fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5116fcd5e742SLu Fengqi * the same extent range. 5117fcd5e742SLu Fengqi */ 5118fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5119acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 51200e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5121fcd5e742SLu Fengqi if (ret) { 5122fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5123acce952bSliubo break; 5124fcd5e742SLu Fengqi } 5125acce952bSliubo 51260e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 51270e6ec385SFilipe Manana free_extent_state(cached_state); 51282ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5129fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5130acce952bSliubo cond_resched(); 5131acce952bSliubo } 5132acce952bSliubo 5133acce952bSliubo return 0; 5134acce952bSliubo } 5135acce952bSliubo 513632da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5137c79a1751SLiu Bo { 5138c79a1751SLiu Bo struct inode *inode; 5139c79a1751SLiu Bo 5140c79a1751SLiu Bo inode = cache->io_ctl.inode; 5141c79a1751SLiu Bo if (inode) { 5142c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5143c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5144c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5145c79a1751SLiu Bo iput(inode); 5146c79a1751SLiu Bo } 5147bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5148c79a1751SLiu Bo btrfs_put_block_group(cache); 5149c79a1751SLiu Bo } 5150c79a1751SLiu Bo 5151c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 51522ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5153c79a1751SLiu Bo { 515432da5386SDavid Sterba struct btrfs_block_group *cache; 5155c79a1751SLiu Bo 5156c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5157c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5158c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 515932da5386SDavid Sterba struct btrfs_block_group, 5160c79a1751SLiu Bo dirty_list); 5161c79a1751SLiu Bo 5162c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5163c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5164c79a1751SLiu Bo list_del_init(&cache->io_list); 5165c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5166c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5167c79a1751SLiu Bo } 5168c79a1751SLiu Bo 5169c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5170c79a1751SLiu Bo spin_lock(&cache->lock); 5171c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5172c79a1751SLiu Bo spin_unlock(&cache->lock); 5173c79a1751SLiu Bo 5174c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5175c79a1751SLiu Bo btrfs_put_block_group(cache); 5176ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5177c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5178c79a1751SLiu Bo } 5179c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5180c79a1751SLiu Bo 518145ae2c18SNikolay Borisov /* 518245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 518345ae2c18SNikolay Borisov * to use it without any locking 518445ae2c18SNikolay Borisov */ 5185c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5186c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 518732da5386SDavid Sterba struct btrfs_block_group, 5188c79a1751SLiu Bo io_list); 5189c79a1751SLiu Bo 5190c79a1751SLiu Bo list_del_init(&cache->io_list); 5191c79a1751SLiu Bo spin_lock(&cache->lock); 5192c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5193c79a1751SLiu Bo spin_unlock(&cache->lock); 5194c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5195c79a1751SLiu Bo } 5196c79a1751SLiu Bo } 5197c79a1751SLiu Bo 519849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 51992ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 520049b25e05SJeff Mahoney { 5201bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5202bbbf7243SNikolay Borisov 52032ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5204c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5205c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5206c79a1751SLiu Bo 5207bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5208bbbf7243SNikolay Borisov post_commit_list) { 5209bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5210bbbf7243SNikolay Borisov } 5211bbbf7243SNikolay Borisov 52122ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 521349b25e05SJeff Mahoney 52144a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 52150b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 521649b25e05SJeff Mahoney 52174a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 52180b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 521949b25e05SJeff Mahoney 5220ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 522167cde344SMiao Xie 52222ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 522349b25e05SJeff Mahoney EXTENT_DIRTY); 5224fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 522549b25e05SJeff Mahoney 5226d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5227d3575156SNaohiro Aota 52284a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 52294a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 523049b25e05SJeff Mahoney } 523149b25e05SJeff Mahoney 52322ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5233acce952bSliubo { 5234acce952bSliubo struct btrfs_transaction *t; 5235acce952bSliubo 52360b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5237acce952bSliubo 52380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52390b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 52400b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5241724e2315SJosef Bacik struct btrfs_transaction, list); 5242724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 52439b64f57dSElena Reshetova refcount_inc(&t->use_count); 52440b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52452ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5246724e2315SJosef Bacik btrfs_put_transaction(t); 52470b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5248724e2315SJosef Bacik continue; 5249724e2315SJosef Bacik } 52500b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5251724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 52520b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5253724e2315SJosef Bacik /* 5254724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5255724e2315SJosef Bacik * handle open currently for this transaction. 5256724e2315SJosef Bacik */ 5257724e2315SJosef Bacik wait_event(t->writer_wait, 5258724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5259724e2315SJosef Bacik } else { 52600b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5261724e2315SJosef Bacik } 52622ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5263724e2315SJosef Bacik 52640b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52650b246afaSJeff Mahoney if (t == fs_info->running_transaction) 52660b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5267724e2315SJosef Bacik list_del_init(&t->list); 52680b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5269a4abeea4SJosef Bacik 5270724e2315SJosef Bacik btrfs_put_transaction(t); 52712e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 52720b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5273724e2315SJosef Bacik } 52740b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52750b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5276ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5277ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 52780b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5279ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 52800b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5281acce952bSliubo 5282acce952bSliubo return 0; 5283acce952bSliubo } 5284ec7d6dfdSNikolay Borisov 5285453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5286ec7d6dfdSNikolay Borisov { 5287ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5288ec7d6dfdSNikolay Borisov int ret; 5289ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5290ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5291ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5292ec7d6dfdSNikolay Borisov int slot; 5293ec7d6dfdSNikolay Borisov 5294ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5295ec7d6dfdSNikolay Borisov if (!path) 5296ec7d6dfdSNikolay Borisov return -ENOMEM; 5297ec7d6dfdSNikolay Borisov 5298ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5299ec7d6dfdSNikolay Borisov search_key.type = -1; 5300ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5301ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5302ec7d6dfdSNikolay Borisov if (ret < 0) 5303ec7d6dfdSNikolay Borisov goto error; 5304ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5305ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5306ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5307ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5308ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 530923125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 531023125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5311ec7d6dfdSNikolay Borisov } else { 531223125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5313ec7d6dfdSNikolay Borisov } 5314ec7d6dfdSNikolay Borisov ret = 0; 5315ec7d6dfdSNikolay Borisov error: 5316ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5317ec7d6dfdSNikolay Borisov return ret; 5318ec7d6dfdSNikolay Borisov } 5319ec7d6dfdSNikolay Borisov 5320543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5321ec7d6dfdSNikolay Borisov { 5322ec7d6dfdSNikolay Borisov int ret; 5323ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5324ec7d6dfdSNikolay Borisov 53256b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5326ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5327ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5328ec7d6dfdSNikolay Borisov root->root_key.objectid); 5329ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5330ec7d6dfdSNikolay Borisov goto out; 5331ec7d6dfdSNikolay Borisov } 5332ec7d6dfdSNikolay Borisov 533323125104SNikolay Borisov *objectid = root->free_objectid++; 5334ec7d6dfdSNikolay Borisov ret = 0; 5335ec7d6dfdSNikolay Borisov out: 5336ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5337ec7d6dfdSNikolay Borisov return ret; 5338ec7d6dfdSNikolay Borisov } 5339