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" 32581bb050SLi Zefan #include "inode-map.h" 3321adbd5cSStefan Behrens #include "check-integrity.h" 34606686eeSJosef Bacik #include "rcu-string.h" 358dabb742SStefan Behrens #include "dev-replace.h" 3653b381b3SDavid Woodhouse #include "raid56.h" 375ac1d209SJeff Mahoney #include "sysfs.h" 38fcebe456SJosef Bacik #include "qgroup.h" 39ebb8765bSAnand Jain #include "compression.h" 40557ea5ddSQu Wenruo #include "tree-checker.h" 41fd708b81SJosef Bacik #include "ref-verify.h" 42aac0023cSJosef Bacik #include "block-group.h" 43b0643e59SDennis Zhou #include "discard.h" 44f603bb94SNikolay Borisov #include "space-info.h" 45eb60ceacSChris Mason 46319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 47319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 48319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 50e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 52319e4d06SQu Wenruo 53e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 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 { 113c6100a4bSJosef Bacik void *private_data; 11444b8bd7eSChris Mason struct bio *bio; 115a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11644b8bd7eSChris Mason int mirror_num; 117eaf25d93SChris Mason /* 118eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 119eaf25d93SChris Mason * can't tell us where in the file the bio should go 120eaf25d93SChris Mason */ 121eaf25d93SChris Mason u64 bio_offset; 1228b712842SChris Mason struct btrfs_work work; 1234e4cbee9SChristoph Hellwig blk_status_t status; 12444b8bd7eSChris Mason }; 12544b8bd7eSChris Mason 12685d4e461SChris Mason /* 12785d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12885d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12985d4e461SChris Mason * the level the eb occupies in the tree. 1304008c04aSChris Mason * 13185d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13285d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13385d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1344fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1354fd786e6SMisono Tomohiro * roots have separate keysets. 1364008c04aSChris Mason * 13785d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13885d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13985d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1404008c04aSChris Mason * 14185d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14285d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14385d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14485d4e461SChris Mason * 14585d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14685d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14785d4e461SChris Mason * needs update as well. 1484008c04aSChris Mason */ 1494008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1504008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1514008c04aSChris Mason # error 1524008c04aSChris Mason # endif 15385d4e461SChris Mason 15485d4e461SChris Mason static struct btrfs_lockdep_keyset { 15585d4e461SChris Mason u64 id; /* root objectid */ 15685d4e461SChris Mason const char *name_stem; /* lock name stem */ 15785d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 15885d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 15985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 16085d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 16185d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 16285d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 16385d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 16485d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 16585d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 16660b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 16785d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 16885d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 16985d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 17013fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1716b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 17285d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1734008c04aSChris Mason }; 17485d4e461SChris Mason 17585d4e461SChris Mason void __init btrfs_init_lockdep(void) 17685d4e461SChris Mason { 17785d4e461SChris Mason int i, j; 17885d4e461SChris Mason 17985d4e461SChris Mason /* initialize lockdep class names */ 18085d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 18185d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 18285d4e461SChris Mason 18385d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 18485d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 18585d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 18685d4e461SChris Mason } 18785d4e461SChris Mason } 18885d4e461SChris Mason 18985d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 19085d4e461SChris Mason int level) 19185d4e461SChris Mason { 19285d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19385d4e461SChris Mason 19485d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19585d4e461SChris Mason 19685d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19785d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19885d4e461SChris Mason if (ks->id == objectid) 19985d4e461SChris Mason break; 20085d4e461SChris Mason 20185d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20285d4e461SChris Mason &ks->keys[level], ks->names[level]); 20385d4e461SChris Mason } 20485d4e461SChris Mason 2054008c04aSChris Mason #endif 2064008c04aSChris Mason 207d352ac68SChris Mason /* 208d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 209d352ac68SChris Mason * that covers the entire device 210d352ac68SChris Mason */ 2116af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 21239b07b5dSOmar Sandoval struct page *page, size_t pg_offset, 21339b07b5dSOmar Sandoval u64 start, u64 len) 2145f39d397SChris Mason { 215fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2165f39d397SChris Mason struct extent_map *em; 2175f39d397SChris Mason int ret; 2185f39d397SChris Mason 219890871beSChris Mason read_lock(&em_tree->lock); 220d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 221a061fc8dSChris Mason if (em) { 222890871beSChris Mason read_unlock(&em_tree->lock); 2235f39d397SChris Mason goto out; 224a061fc8dSChris Mason } 225890871beSChris Mason read_unlock(&em_tree->lock); 2267b13b7b1SChris Mason 227172ddd60SDavid Sterba em = alloc_extent_map(); 2285f39d397SChris Mason if (!em) { 2295f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2305f39d397SChris Mason goto out; 2315f39d397SChris Mason } 2325f39d397SChris Mason em->start = 0; 2330afbaf8cSChris Mason em->len = (u64)-1; 234c8b97818SChris Mason em->block_len = (u64)-1; 2355f39d397SChris Mason em->block_start = 0; 236d1310b2eSChris Mason 237890871beSChris Mason write_lock(&em_tree->lock); 23809a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2395f39d397SChris Mason if (ret == -EEXIST) { 2405f39d397SChris Mason free_extent_map(em); 2417b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 242b4f359abSTsutomu Itoh if (!em) 2430433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2445f39d397SChris Mason } else if (ret) { 2457b13b7b1SChris Mason free_extent_map(em); 2460433f20dSTsutomu Itoh em = ERR_PTR(ret); 2475f39d397SChris Mason } 248890871beSChris Mason write_unlock(&em_tree->lock); 2497b13b7b1SChris Mason 2505f39d397SChris Mason out: 2515f39d397SChris Mason return em; 2525f39d397SChris Mason } 2535f39d397SChris Mason 254d352ac68SChris Mason /* 2552996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 256d352ac68SChris Mason */ 257c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 25819c00ddcSChris Mason { 259d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 260e9be5a30SDavid Sterba const int num_pages = fs_info->nodesize >> PAGE_SHIFT; 261d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 26219c00ddcSChris Mason char *kaddr; 263e9be5a30SDavid Sterba int i; 264d5178578SJohannes Thumshirn 265d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 266d5178578SJohannes Thumshirn crypto_shash_init(shash); 267e9be5a30SDavid Sterba kaddr = page_address(buf->pages[0]); 268e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 269e9be5a30SDavid Sterba PAGE_SIZE - BTRFS_CSUM_SIZE); 27019c00ddcSChris Mason 271e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 272e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 273e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 27419c00ddcSChris Mason } 27571a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 276d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 27719c00ddcSChris Mason } 27819c00ddcSChris Mason 279d352ac68SChris Mason /* 280d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 281d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 282d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 283d352ac68SChris Mason * in the wrong place. 284d352ac68SChris Mason */ 2851259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 286b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 287b9fab919SChris Mason int atomic) 2881259ab75SChris Mason { 2892ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2901259ab75SChris Mason int ret; 2912755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 2921259ab75SChris Mason 2931259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2941259ab75SChris Mason return 0; 2951259ab75SChris Mason 296b9fab919SChris Mason if (atomic) 297b9fab919SChris Mason return -EAGAIN; 298b9fab919SChris Mason 299a26e8c9fSJosef Bacik if (need_lock) { 300a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 301300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 302a26e8c9fSJosef Bacik } 303a26e8c9fSJosef Bacik 3042ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 305ff13db41SDavid Sterba &cached_state); 3060b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3071259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3081259ab75SChris Mason ret = 0; 3091259ab75SChris Mason goto out; 3101259ab75SChris Mason } 31194647322SDavid Sterba btrfs_err_rl(eb->fs_info, 31294647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 31394647322SDavid Sterba eb->start, 31429549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3151259ab75SChris Mason ret = 1; 316a26e8c9fSJosef Bacik 317a26e8c9fSJosef Bacik /* 318a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 319a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 32001327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 321a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 322a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 323a26e8c9fSJosef Bacik * making everybody very sad. 324a26e8c9fSJosef Bacik */ 325a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3260b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 32733958dc6SChris Mason out: 3282ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 329e43bbe5eSDavid Sterba &cached_state); 330472b909fSJosef Bacik if (need_lock) 331a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3321259ab75SChris Mason return ret; 3331259ab75SChris Mason } 3341259ab75SChris Mason 335e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 336e7e16f48SJohannes Thumshirn { 337e7e16f48SJohannes Thumshirn switch (csum_type) { 338e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 3393951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 3403831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 341352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 342e7e16f48SJohannes Thumshirn return true; 343e7e16f48SJohannes Thumshirn default: 344e7e16f48SJohannes Thumshirn return false; 345e7e16f48SJohannes Thumshirn } 346e7e16f48SJohannes Thumshirn } 347e7e16f48SJohannes Thumshirn 348d352ac68SChris Mason /* 3491104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3501104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3511104a885SDavid Sterba */ 352ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 353ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3541104a885SDavid Sterba { 3551104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3561104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 35751bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 358d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 359d5178578SJohannes Thumshirn 360d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 361d5178578SJohannes Thumshirn crypto_shash_init(shash); 3621104a885SDavid Sterba 3631104a885SDavid Sterba /* 3641104a885SDavid Sterba * The super_block structure does not span the whole 36551bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 36651bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3671104a885SDavid Sterba */ 368d5178578SJohannes Thumshirn crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 369d5178578SJohannes Thumshirn BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 370d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 3711104a885SDavid Sterba 37251bce6c9SJohannes Thumshirn if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb))) 373e7e16f48SJohannes Thumshirn return 1; 3741104a885SDavid Sterba 375e7e16f48SJohannes Thumshirn return 0; 3761104a885SDavid Sterba } 3771104a885SDavid Sterba 378e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 379ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 380581c1760SQu Wenruo { 381e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 382581c1760SQu Wenruo int found_level; 383581c1760SQu Wenruo struct btrfs_key found_key; 384581c1760SQu Wenruo int ret; 385581c1760SQu Wenruo 386581c1760SQu Wenruo found_level = btrfs_header_level(eb); 387581c1760SQu Wenruo if (found_level != level) { 38863489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 38963489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 390581c1760SQu Wenruo btrfs_err(fs_info, 391581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 392581c1760SQu Wenruo eb->start, level, found_level); 393581c1760SQu Wenruo return -EIO; 394581c1760SQu Wenruo } 395581c1760SQu Wenruo 396581c1760SQu Wenruo if (!first_key) 397581c1760SQu Wenruo return 0; 398581c1760SQu Wenruo 3995d41be6fSQu Wenruo /* 4005d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 4015d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 4025d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 4035d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 4045d41be6fSQu Wenruo */ 4055d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 4065d41be6fSQu Wenruo return 0; 40762fdaa52SQu Wenruo 40862fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 40962fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 41062fdaa52SQu Wenruo btrfs_err(fs_info, 41162fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 41262fdaa52SQu Wenruo eb->start); 41362fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 41462fdaa52SQu Wenruo return -EUCLEAN; 41562fdaa52SQu Wenruo } 41662fdaa52SQu Wenruo 417581c1760SQu Wenruo if (found_level) 418581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 419581c1760SQu Wenruo else 420581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 421581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 422581c1760SQu Wenruo 423581c1760SQu Wenruo if (ret) { 42463489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 42563489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 426581c1760SQu Wenruo btrfs_err(fs_info, 427ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 428ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 429ff76a864SLiu Bo first_key->type, first_key->offset, 430ff76a864SLiu Bo found_key.objectid, found_key.type, 431ff76a864SLiu Bo found_key.offset); 432581c1760SQu Wenruo } 433581c1760SQu Wenruo return ret; 434581c1760SQu Wenruo } 435581c1760SQu Wenruo 4361104a885SDavid Sterba /* 437d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 438d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 439581c1760SQu Wenruo * 440581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 441581c1760SQu Wenruo * @level: expected level, mandatory check 442581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 443d352ac68SChris Mason */ 4445ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 445581c1760SQu Wenruo u64 parent_transid, int level, 446581c1760SQu Wenruo struct btrfs_key *first_key) 447f188591eSChris Mason { 4485ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 449f188591eSChris Mason struct extent_io_tree *io_tree; 450ea466794SJosef Bacik int failed = 0; 451f188591eSChris Mason int ret; 452f188591eSChris Mason int num_copies = 0; 453f188591eSChris Mason int mirror_num = 0; 454ea466794SJosef Bacik int failed_mirror = 0; 455f188591eSChris Mason 4560b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 457f188591eSChris Mason while (1) { 458f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 459c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 460256dd1bbSStefan Behrens if (!ret) { 461581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 462b9fab919SChris Mason parent_transid, 0)) 463256dd1bbSStefan Behrens ret = -EIO; 464e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 465ff76a864SLiu Bo first_key, parent_transid)) 466581c1760SQu Wenruo ret = -EUCLEAN; 467581c1760SQu Wenruo else 468581c1760SQu Wenruo break; 469256dd1bbSStefan Behrens } 470d397712bSChris Mason 4710b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 472f188591eSChris Mason eb->start, eb->len); 4734235298eSChris Mason if (num_copies == 1) 474ea466794SJosef Bacik break; 4754235298eSChris Mason 4765cf1ab56SJosef Bacik if (!failed_mirror) { 4775cf1ab56SJosef Bacik failed = 1; 4785cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4795cf1ab56SJosef Bacik } 4805cf1ab56SJosef Bacik 481f188591eSChris Mason mirror_num++; 482ea466794SJosef Bacik if (mirror_num == failed_mirror) 483ea466794SJosef Bacik mirror_num++; 484ea466794SJosef Bacik 4854235298eSChris Mason if (mirror_num > num_copies) 486ea466794SJosef Bacik break; 487f188591eSChris Mason } 488ea466794SJosef Bacik 489c0901581SStefan Behrens if (failed && !ret && failed_mirror) 49020a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 491ea466794SJosef Bacik 492ea466794SJosef Bacik return ret; 493f188591eSChris Mason } 49419c00ddcSChris Mason 495d352ac68SChris Mason /* 496d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 497d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 498d352ac68SChris Mason */ 499d397712bSChris Mason 50001d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 50119c00ddcSChris Mason { 5024eee4fa4SMiao Xie u64 start = page_offset(page); 50319c00ddcSChris Mason u64 found_start; 5042996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 5052996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 50619c00ddcSChris Mason struct extent_buffer *eb; 5078d47a0d8SQu Wenruo int ret; 508f188591eSChris Mason 5094f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5104f2de97aSJosef Bacik if (page != eb->pages[0]) 5114f2de97aSJosef Bacik return 0; 5120f805531SAlex Lyakas 51319c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5140f805531SAlex Lyakas /* 5150f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5160f805531SAlex Lyakas * It is useful to know what went wrong. 5170f805531SAlex Lyakas */ 5180f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5190f805531SAlex Lyakas return -EUCLEAN; 5200f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5210f805531SAlex Lyakas return -EUCLEAN; 5220f805531SAlex Lyakas 523de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 5249a8658e3SDavid Sterba offsetof(struct btrfs_header, fsid), 5259a8658e3SDavid Sterba BTRFS_FSID_SIZE) == 0); 5260f805531SAlex Lyakas 527c67b3892SDavid Sterba csum_tree_block(eb, result); 5282996e1f8SJohannes Thumshirn 5298d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 5308d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 5318d47a0d8SQu Wenruo else 5328d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 5338d47a0d8SQu Wenruo 5348d47a0d8SQu Wenruo if (ret < 0) { 535c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 5368d47a0d8SQu Wenruo btrfs_err(fs_info, 5378d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 5388d47a0d8SQu Wenruo eb->start); 539c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 5408d47a0d8SQu Wenruo return ret; 5418d47a0d8SQu Wenruo } 5422996e1f8SJohannes Thumshirn write_extent_buffer(eb, result, 0, csum_size); 5438d47a0d8SQu Wenruo 5442996e1f8SJohannes Thumshirn return 0; 54519c00ddcSChris Mason } 54619c00ddcSChris Mason 547b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5482b82032cSYan Zheng { 549b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 55001d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 55144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5522b82032cSYan Zheng int ret = 1; 5532b82032cSYan Zheng 5549a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5559a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5562b82032cSYan Zheng while (fs_devices) { 5577239ff4bSNikolay Borisov u8 *metadata_uuid; 5587239ff4bSNikolay Borisov 5597239ff4bSNikolay Borisov /* 5607239ff4bSNikolay Borisov * Checking the incompat flag is only valid for the current 5617239ff4bSNikolay Borisov * fs. For seed devices it's forbidden to have their uuid 5627239ff4bSNikolay Borisov * changed so reading ->fsid in this case is fine 5637239ff4bSNikolay Borisov */ 5647239ff4bSNikolay Borisov if (fs_devices == fs_info->fs_devices && 5657239ff4bSNikolay Borisov btrfs_fs_incompat(fs_info, METADATA_UUID)) 5667239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5677239ff4bSNikolay Borisov else 5687239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5697239ff4bSNikolay Borisov 5707239ff4bSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) { 5712b82032cSYan Zheng ret = 0; 5722b82032cSYan Zheng break; 5732b82032cSYan Zheng } 5742b82032cSYan Zheng fs_devices = fs_devices->seed; 5752b82032cSYan Zheng } 5762b82032cSYan Zheng return ret; 5772b82032cSYan Zheng } 5782b82032cSYan Zheng 579facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 580facc8a22SMiao Xie u64 phy_offset, struct page *page, 581facc8a22SMiao Xie u64 start, u64 end, int mirror) 582ce9adaa5SChris Mason { 583ce9adaa5SChris Mason u64 found_start; 584ce9adaa5SChris Mason int found_level; 585ce9adaa5SChris Mason struct extent_buffer *eb; 58615b6e8a8SDavid Sterba struct btrfs_fs_info *fs_info; 58715b6e8a8SDavid Sterba u16 csum_size; 588f188591eSChris Mason int ret = 0; 5892996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 590727011e0SChris Mason int reads_done; 591ce9adaa5SChris Mason 592ce9adaa5SChris Mason if (!page->private) 593ce9adaa5SChris Mason goto out; 594d397712bSChris Mason 5954f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 59615b6e8a8SDavid Sterba fs_info = eb->fs_info; 59715b6e8a8SDavid Sterba csum_size = btrfs_super_csum_size(fs_info->super_copy); 598d397712bSChris Mason 5990b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6000b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6010b32f4bbSJosef Bacik */ 60267439dadSDavid Sterba atomic_inc(&eb->refs); 6030b32f4bbSJosef Bacik 6040b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 605727011e0SChris Mason if (!reads_done) 606727011e0SChris Mason goto err; 607f188591eSChris Mason 6085cf1ab56SJosef Bacik eb->read_mirror = mirror; 609656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 610ea466794SJosef Bacik ret = -EIO; 611ea466794SJosef Bacik goto err; 612ea466794SJosef Bacik } 613ea466794SJosef Bacik 614ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 615727011e0SChris Mason if (found_start != eb->start) { 616893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 617893bf4b1SSu Yue eb->start, found_start); 618f188591eSChris Mason ret = -EIO; 619ce9adaa5SChris Mason goto err; 620ce9adaa5SChris Mason } 621b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 62202873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 62394647322SDavid Sterba eb->start); 6241259ab75SChris Mason ret = -EIO; 6251259ab75SChris Mason goto err; 6261259ab75SChris Mason } 627ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6281c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 629893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 630893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6311c24c3ceSJosef Bacik ret = -EIO; 6321c24c3ceSJosef Bacik goto err; 6331c24c3ceSJosef Bacik } 634ce9adaa5SChris Mason 63585d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 63685d4e461SChris Mason eb, found_level); 6374008c04aSChris Mason 638c67b3892SDavid Sterba csum_tree_block(eb, result); 639a826d6dcSJosef Bacik 6402996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 6412996e1f8SJohannes Thumshirn u32 val; 6422996e1f8SJohannes Thumshirn u32 found = 0; 6432996e1f8SJohannes Thumshirn 6442996e1f8SJohannes Thumshirn memcpy(&found, result, csum_size); 6452996e1f8SJohannes Thumshirn 6462996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 6472996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 6482996e1f8SJohannes Thumshirn "%s checksum verify failed on %llu wanted %x found %x level %d", 6492996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 6502996e1f8SJohannes Thumshirn val, found, btrfs_header_level(eb)); 6512996e1f8SJohannes Thumshirn ret = -EUCLEAN; 6522996e1f8SJohannes Thumshirn goto err; 6532996e1f8SJohannes Thumshirn } 6542996e1f8SJohannes Thumshirn 655a826d6dcSJosef Bacik /* 656a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 657a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 658a826d6dcSJosef Bacik * return -EIO. 659a826d6dcSJosef Bacik */ 6601c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 661a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 662a826d6dcSJosef Bacik ret = -EIO; 663a826d6dcSJosef Bacik } 664ce9adaa5SChris Mason 665813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 666053ab70fSLiu Bo ret = -EIO; 667053ab70fSLiu Bo 6680b32f4bbSJosef Bacik if (!ret) 6690b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 67075391f0dSQu Wenruo else 67175391f0dSQu Wenruo btrfs_err(fs_info, 67275391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 67375391f0dSQu Wenruo eb->start); 674ce9adaa5SChris Mason err: 67579fb65a1SJosef Bacik if (reads_done && 67679fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 677d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6784bb31e92SArne Jansen 67953b381b3SDavid Woodhouse if (ret) { 68053b381b3SDavid Woodhouse /* 68153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 68253b381b3SDavid Woodhouse * again, we have to make sure it has something 68353b381b3SDavid Woodhouse * to decrement 68453b381b3SDavid Woodhouse */ 68553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6860b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 68753b381b3SDavid Woodhouse } 6880b32f4bbSJosef Bacik free_extent_buffer(eb); 689ce9adaa5SChris Mason out: 690f188591eSChris Mason return ret; 691ce9adaa5SChris Mason } 692ce9adaa5SChris Mason 6934246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 694ce9adaa5SChris Mason { 69597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 696ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6979e0af237SLiu Bo struct btrfs_workqueue *wq; 698ce9adaa5SChris Mason 699ce9adaa5SChris Mason fs_info = end_io_wq->info; 7004e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 701d20f7043SChris Mason 70237226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 703a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7049e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 705a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7069e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 707a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7089e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 709a0cac0ecSOmar Sandoval else 7109e0af237SLiu Bo wq = fs_info->endio_write_workers; 7119e0af237SLiu Bo } else { 712a0cac0ecSOmar Sandoval if (unlikely(end_io_wq->metadata == BTRFS_WQ_ENDIO_DIO_REPAIR)) 7138b110e39SMiao Xie wq = fs_info->endio_repair_workers; 714a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7159e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 716a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7179e0af237SLiu Bo wq = fs_info->endio_meta_workers; 718a0cac0ecSOmar Sandoval else 7199e0af237SLiu Bo wq = fs_info->endio_workers; 7209e0af237SLiu Bo } 7219e0af237SLiu Bo 722a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7239e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 724ce9adaa5SChris Mason } 725ce9adaa5SChris Mason 7264e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 727bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7280b86a832SChris Mason { 72997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7308b110e39SMiao Xie 73197eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 732ce9adaa5SChris Mason if (!end_io_wq) 7334e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 734ce9adaa5SChris Mason 735ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 736ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 73722c59948SChris Mason end_io_wq->info = info; 7384e4cbee9SChristoph Hellwig end_io_wq->status = 0; 739ce9adaa5SChris Mason end_io_wq->bio = bio; 74022c59948SChris Mason end_io_wq->metadata = metadata; 741ce9adaa5SChris Mason 742ce9adaa5SChris Mason bio->bi_private = end_io_wq; 743ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 74422c59948SChris Mason return 0; 74522c59948SChris Mason } 74622c59948SChris Mason 7474a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7484a69a410SChris Mason { 7494a69a410SChris Mason struct async_submit_bio *async; 7504e4cbee9SChristoph Hellwig blk_status_t ret; 7514a69a410SChris Mason 7524a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 753c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 754eaf25d93SChris Mason async->bio_offset); 75579787eaaSJeff Mahoney if (ret) 7564e4cbee9SChristoph Hellwig async->status = ret; 7574a69a410SChris Mason } 7584a69a410SChris Mason 75906ea01b1SDavid Sterba /* 76006ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 76106ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 76206ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 76306ea01b1SDavid Sterba * 76406ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 76506ea01b1SDavid Sterba * inserted into the tree. 76606ea01b1SDavid Sterba */ 7674a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7688b712842SChris Mason { 7698b712842SChris Mason struct async_submit_bio *async; 77006ea01b1SDavid Sterba struct inode *inode; 77106ea01b1SDavid Sterba blk_status_t ret; 7728b712842SChris Mason 7738b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 77406ea01b1SDavid Sterba inode = async->private_data; 7754854ddd0SChris Mason 776bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7774e4cbee9SChristoph Hellwig if (async->status) { 7784e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7794246a0b6SChristoph Hellwig bio_endio(async->bio); 78079787eaaSJeff Mahoney return; 78179787eaaSJeff Mahoney } 78279787eaaSJeff Mahoney 783ec39f769SChris Mason /* 784ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 785ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 786ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 787ec39f769SChris Mason */ 788ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 78908635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 79006ea01b1SDavid Sterba if (ret) { 79106ea01b1SDavid Sterba async->bio->bi_status = ret; 79206ea01b1SDavid Sterba bio_endio(async->bio); 79306ea01b1SDavid Sterba } 7944a69a410SChris Mason } 7954a69a410SChris Mason 7964a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7974a69a410SChris Mason { 7984a69a410SChris Mason struct async_submit_bio *async; 7994a69a410SChris Mason 8004a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8018b712842SChris Mason kfree(async); 8028b712842SChris Mason } 8038b712842SChris Mason 8048c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 805c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 806c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 807e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 80844b8bd7eSChris Mason { 80944b8bd7eSChris Mason struct async_submit_bio *async; 81044b8bd7eSChris Mason 81144b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81244b8bd7eSChris Mason if (!async) 8134e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 81444b8bd7eSChris Mason 815c6100a4bSJosef Bacik async->private_data = private_data; 81644b8bd7eSChris Mason async->bio = bio; 81744b8bd7eSChris Mason async->mirror_num = mirror_num; 8184a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8194a69a410SChris Mason 820a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 821a0cac0ecSOmar Sandoval run_one_async_free); 8224a69a410SChris Mason 823eaf25d93SChris Mason async->bio_offset = bio_offset; 8248c8bee1dSChris Mason 8254e4cbee9SChristoph Hellwig async->status = 0; 82679787eaaSJeff Mahoney 82767f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8285cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 829d313d7a3SChris Mason 8305cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 83144b8bd7eSChris Mason return 0; 83244b8bd7eSChris Mason } 83344b8bd7eSChris Mason 8344e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 835ce3ed71aSChris Mason { 8362c30c71bSKent Overstreet struct bio_vec *bvec; 837ce3ed71aSChris Mason struct btrfs_root *root; 8382b070cfeSChristoph Hellwig int ret = 0; 8396dc4f100SMing Lei struct bvec_iter_all iter_all; 840ce3ed71aSChris Mason 841c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8422b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 843ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 84401d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 84579787eaaSJeff Mahoney if (ret) 84679787eaaSJeff Mahoney break; 847ce3ed71aSChris Mason } 8482c30c71bSKent Overstreet 8494e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 850ce3ed71aSChris Mason } 851ce3ed71aSChris Mason 852d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 853eaf25d93SChris Mason u64 bio_offset) 85422c59948SChris Mason { 8558b712842SChris Mason /* 8568b712842SChris Mason * when we're called for a write, we're already in the async 8575443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8588b712842SChris Mason */ 85979787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86022c59948SChris Mason } 86122c59948SChris Mason 8629b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8639b4e675aSDavid Sterba struct btrfs_inode *bi) 864de0022b9SJosef Bacik { 8656300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8666300463bSLiu Bo return 0; 8679b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 868de0022b9SJosef Bacik return 0; 869de0022b9SJosef Bacik return 1; 870de0022b9SJosef Bacik } 871de0022b9SJosef Bacik 872a56b1c7bSNikolay Borisov static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio, 87350489a57SNikolay Borisov int mirror_num, 87450489a57SNikolay Borisov unsigned long bio_flags) 87544b8bd7eSChris Mason { 8760b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8779b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8784e4cbee9SChristoph Hellwig blk_status_t ret; 879cad321adSChris Mason 88037226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 881cad321adSChris Mason /* 882cad321adSChris Mason * called for a read, do the setup so that checksum validation 883cad321adSChris Mason * can happen in the async kernel threads 884cad321adSChris Mason */ 8850b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8860b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8871d4284bdSChris Mason if (ret) 88861891923SStefan Behrens goto out_w_error; 88908635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 890de0022b9SJosef Bacik } else if (!async) { 891de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 892de0022b9SJosef Bacik if (ret) 89361891923SStefan Behrens goto out_w_error; 89408635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 89561891923SStefan Behrens } else { 896cad321adSChris Mason /* 89761891923SStefan Behrens * kthread helpers are used to submit writes so that 89861891923SStefan Behrens * checksumming can happen in parallel across all CPUs 899cad321adSChris Mason */ 900c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 901e68f2ee7SNikolay Borisov 0, inode, btree_submit_bio_start); 90244b8bd7eSChris Mason } 90344b8bd7eSChris Mason 9044246a0b6SChristoph Hellwig if (ret) 9054246a0b6SChristoph Hellwig goto out_w_error; 9064246a0b6SChristoph Hellwig return 0; 9074246a0b6SChristoph Hellwig 90861891923SStefan Behrens out_w_error: 9094e4cbee9SChristoph Hellwig bio->bi_status = ret; 9104246a0b6SChristoph Hellwig bio_endio(bio); 91161891923SStefan Behrens return ret; 91261891923SStefan Behrens } 91361891923SStefan Behrens 9143dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 915784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 916a6bc32b8SMel Gorman struct page *newpage, struct page *page, 917a6bc32b8SMel Gorman enum migrate_mode mode) 918784b4e29SChris Mason { 919784b4e29SChris Mason /* 920784b4e29SChris Mason * we can't safely write a btree page from here, 921784b4e29SChris Mason * we haven't done the locking hook 922784b4e29SChris Mason */ 923784b4e29SChris Mason if (PageDirty(page)) 924784b4e29SChris Mason return -EAGAIN; 925784b4e29SChris Mason /* 926784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 927784b4e29SChris Mason * We must have no buffers or drop them. 928784b4e29SChris Mason */ 929784b4e29SChris Mason if (page_has_private(page) && 930784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 931784b4e29SChris Mason return -EAGAIN; 932a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 933784b4e29SChris Mason } 9343dd1462eSJan Beulich #endif 935784b4e29SChris Mason 9360da5468fSChris Mason 9370da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9380da5468fSChris Mason struct writeback_control *wbc) 9390da5468fSChris Mason { 940e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 941e2d84521SMiao Xie int ret; 942e2d84521SMiao Xie 943d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 944448d640bSChris Mason 945448d640bSChris Mason if (wbc->for_kupdate) 946448d640bSChris Mason return 0; 947448d640bSChris Mason 948e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 949b9473439SChris Mason /* this is a bit racy, but that's ok */ 950d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 951d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 952d814a491SEthan Lien fs_info->dirty_metadata_batch); 953e2d84521SMiao Xie if (ret < 0) 954793955bcSChris Mason return 0; 955793955bcSChris Mason } 9560b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9570da5468fSChris Mason } 9580da5468fSChris Mason 959b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9605f39d397SChris Mason { 96171ad38b4SDavid Sterba return extent_read_full_page(page, btree_get_extent, 0); 9625f39d397SChris Mason } 9635f39d397SChris Mason 96470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9655f39d397SChris Mason { 96698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 96798509cfcSChris Mason return 0; 9680c4e538bSDavid Sterba 969f7a52a40SDavid Sterba return try_release_extent_buffer(page); 970d98237b3SChris Mason } 971d98237b3SChris Mason 972d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 973d47992f8SLukas Czerner unsigned int length) 974d98237b3SChris Mason { 975d1310b2eSChris Mason struct extent_io_tree *tree; 976d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9775f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9785f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9799ad6b7bcSChris Mason if (PagePrivate(page)) { 980efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 981efe120a0SFrank Holton "page private not zero on page %llu", 982efe120a0SFrank Holton (unsigned long long)page_offset(page)); 9839ad6b7bcSChris Mason ClearPagePrivate(page); 9849ad6b7bcSChris Mason set_page_private(page, 0); 98509cbfeafSKirill A. Shutemov put_page(page); 9869ad6b7bcSChris Mason } 987d98237b3SChris Mason } 988d98237b3SChris Mason 9890b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9900b32f4bbSJosef Bacik { 991bb146eb2SJosef Bacik #ifdef DEBUG 9920b32f4bbSJosef Bacik struct extent_buffer *eb; 9930b32f4bbSJosef Bacik 9940b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9950b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9960b32f4bbSJosef Bacik BUG_ON(!eb); 9970b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9980b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9990b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1000bb146eb2SJosef Bacik #endif 10010b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10020b32f4bbSJosef Bacik } 10030b32f4bbSJosef Bacik 10047f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1005d98237b3SChris Mason .readpage = btree_readpage, 10060da5468fSChris Mason .writepages = btree_writepages, 10075f39d397SChris Mason .releasepage = btree_releasepage, 10085f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10095a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1010784b4e29SChris Mason .migratepage = btree_migratepage, 10115a92bc88SChris Mason #endif 10120b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1013d98237b3SChris Mason }; 1014123abc88SChris Mason 10152ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1016090d1875SChris Mason { 10175f39d397SChris Mason struct extent_buffer *buf = NULL; 1018537f38f0SNikolay Borisov int ret; 1019090d1875SChris Mason 10202ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1021c871b0f2SLiu Bo if (IS_ERR(buf)) 10226197d86eSDavid Sterba return; 1023537f38f0SNikolay Borisov 1024c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 1025537f38f0SNikolay Borisov if (ret < 0) 1026537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1027537f38f0SNikolay Borisov else 10285f39d397SChris Mason free_extent_buffer(buf); 1029090d1875SChris Mason } 1030090d1875SChris Mason 10312ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10322ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10330308af44SDavid Sterba u64 bytenr) 10340999df54SChris Mason { 10350b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10360b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10370b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10380999df54SChris Mason } 10390999df54SChris Mason 1040581c1760SQu Wenruo /* 1041581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1042581c1760SQu Wenruo * verification. 1043581c1760SQu Wenruo * 1044581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1045581c1760SQu Wenruo * @level: expected level, mandatory check 1046581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1047581c1760SQu Wenruo */ 10482ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1049581c1760SQu Wenruo u64 parent_transid, int level, 1050581c1760SQu Wenruo struct btrfs_key *first_key) 1051e20d96d6SChris Mason { 10525f39d397SChris Mason struct extent_buffer *buf = NULL; 105319c00ddcSChris Mason int ret; 105419c00ddcSChris Mason 10552ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1056c871b0f2SLiu Bo if (IS_ERR(buf)) 1057c871b0f2SLiu Bo return buf; 1058e4204dedSChris Mason 10595ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1060581c1760SQu Wenruo level, first_key); 10610f0fe8f7SFilipe David Borba Manana if (ret) { 1062537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 106364c043deSLiu Bo return ERR_PTR(ret); 10640f0fe8f7SFilipe David Borba Manana } 10655f39d397SChris Mason return buf; 1066ce9adaa5SChris Mason 1067eb60ceacSChris Mason } 1068eb60ceacSChris Mason 10696a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1070ed2ff2cbSChris Mason { 10716a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 107255c69072SChris Mason if (btrfs_header_generation(buf) == 1073e2d84521SMiao Xie fs_info->running_transaction->transid) { 1074b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1075b4ce94deSChris Mason 1076b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1077104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1078e2d84521SMiao Xie -buf->len, 1079e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1080b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 10818bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 10820b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1083925baeddSChris Mason } 10845f39d397SChris Mason } 1085ed7b63ebSJosef Bacik } 10865f39d397SChris Mason 1087da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1088e20d96d6SChris Mason u64 objectid) 1089d97e63b6SChris Mason { 10907c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 109196dfcb46SJosef Bacik root->fs_info = fs_info; 1092cfaa7295SChris Mason root->node = NULL; 1093a28ec197SChris Mason root->commit_root = NULL; 109427cdeb70SMiao Xie root->state = 0; 1095d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 10960b86a832SChris Mason 10970f7d52f4SChris Mason root->last_trans = 0; 109813a8a7c8SYan, Zheng root->highest_objectid = 0; 1099eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1100199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11016bef4d31SEric Paris root->inode_tree = RB_ROOT; 110216cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1103f0486c68SYan, Zheng root->block_rsv = NULL; 11040b86a832SChris Mason 11050b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11065d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1107eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1108eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1109199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1110199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1111d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11122ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11132ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11145d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1115eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1116199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1117f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11182ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11192ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11208287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1121a2135011SChris Mason mutex_init(&root->objectid_mutex); 1122e02119d5SChris Mason mutex_init(&root->log_mutex); 112331f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1124573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11257237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11267237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11277237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11288b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11298b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11307237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11317237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11327237f183SYan Zheng atomic_set(&root->log_writers, 0); 11332ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11340700cea7SElena Reshetova refcount_set(&root->refs, 1); 11358ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1136eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11377237f183SYan Zheng root->log_transid = 0; 1138d1433debSMiao Xie root->log_transid_committed = -1; 1139257c62e1SChris Mason root->last_log_commit = 0; 11407c0260eeSJeff Mahoney if (!dummy) 114143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 114243eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1143017e5369SChris Mason 11443768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11453768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11466702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11477c0260eeSJeff Mahoney if (!dummy) 11483f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 114906ea65a3SJosef Bacik else 115006ea65a3SJosef Bacik root->defrag_trans_start = 0; 11514d775673SChris Mason root->root_key.objectid = objectid; 11520ee5dc67SAl Viro root->anon_dev = 0; 11538ea05e3aSAlexander Block 11545f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1155370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1156bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1157bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1158bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1159bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1160bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1161bd647ce3SJosef Bacik #endif 11623768f368SChris Mason } 11633768f368SChris Mason 116474e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 116596dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11666f07e42eSAl Viro { 116774e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11686f07e42eSAl Viro if (root) 116996dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11706f07e42eSAl Viro return root; 11716f07e42eSAl Viro } 11726f07e42eSAl Viro 117306ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 117406ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1175da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 117606ea65a3SJosef Bacik { 117706ea65a3SJosef Bacik struct btrfs_root *root; 117806ea65a3SJosef Bacik 11797c0260eeSJeff Mahoney if (!fs_info) 11807c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11817c0260eeSJeff Mahoney 118296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 118306ea65a3SJosef Bacik if (!root) 118406ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1185da17066cSJeff Mahoney 1186b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1187faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 118806ea65a3SJosef Bacik 118906ea65a3SJosef Bacik return root; 119006ea65a3SJosef Bacik } 119106ea65a3SJosef Bacik #endif 119206ea65a3SJosef Bacik 119320897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 119420897f5cSArne Jansen u64 objectid) 119520897f5cSArne Jansen { 11969b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 119720897f5cSArne Jansen struct extent_buffer *leaf; 119820897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 119920897f5cSArne Jansen struct btrfs_root *root; 120020897f5cSArne Jansen struct btrfs_key key; 1201b89f6d1fSFilipe Manana unsigned int nofs_flag; 120220897f5cSArne Jansen int ret = 0; 120320897f5cSArne Jansen 1204b89f6d1fSFilipe Manana /* 1205b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1206b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1207b89f6d1fSFilipe Manana */ 1208b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 120996dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1210b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 121120897f5cSArne Jansen if (!root) 121220897f5cSArne Jansen return ERR_PTR(-ENOMEM); 121320897f5cSArne Jansen 121420897f5cSArne Jansen root->root_key.objectid = objectid; 121520897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 121620897f5cSArne Jansen root->root_key.offset = 0; 121720897f5cSArne Jansen 12184d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 121920897f5cSArne Jansen if (IS_ERR(leaf)) { 122020897f5cSArne Jansen ret = PTR_ERR(leaf); 12211dd05682STsutomu Itoh leaf = NULL; 122220897f5cSArne Jansen goto fail; 122320897f5cSArne Jansen } 122420897f5cSArne Jansen 122520897f5cSArne Jansen root->node = leaf; 122620897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 122720897f5cSArne Jansen 122820897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 122927cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 123020897f5cSArne Jansen 123120897f5cSArne Jansen root->root_item.flags = 0; 123220897f5cSArne Jansen root->root_item.byte_limit = 0; 123320897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 123420897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 123520897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 123620897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 123720897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 123820897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 123920897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 124033d85fdaSQu Wenruo if (is_fstree(objectid)) 1241807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1242807fc790SAndy Shevchenko else 1243807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 124420897f5cSArne Jansen root->root_item.drop_level = 0; 124520897f5cSArne Jansen 124620897f5cSArne Jansen key.objectid = objectid; 124720897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 124820897f5cSArne Jansen key.offset = 0; 124920897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 125020897f5cSArne Jansen if (ret) 125120897f5cSArne Jansen goto fail; 125220897f5cSArne Jansen 125320897f5cSArne Jansen btrfs_tree_unlock(leaf); 125420897f5cSArne Jansen 125520897f5cSArne Jansen return root; 12561dd05682STsutomu Itoh 12571dd05682STsutomu Itoh fail: 12581dd05682STsutomu Itoh if (leaf) { 12591dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 126059885b39STsutomu Itoh free_extent_buffer(root->commit_root); 12611dd05682STsutomu Itoh free_extent_buffer(leaf); 12621dd05682STsutomu Itoh } 126300246528SJosef Bacik btrfs_put_root(root); 12641dd05682STsutomu Itoh 12651dd05682STsutomu Itoh return ERR_PTR(ret); 126620897f5cSArne Jansen } 126720897f5cSArne Jansen 12687237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1269e02119d5SChris Mason struct btrfs_fs_info *fs_info) 12700f7d52f4SChris Mason { 12710f7d52f4SChris Mason struct btrfs_root *root; 12727237f183SYan Zheng struct extent_buffer *leaf; 1273e02119d5SChris Mason 127496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1275e02119d5SChris Mason if (!root) 12767237f183SYan Zheng return ERR_PTR(-ENOMEM); 1277e02119d5SChris Mason 1278e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1279e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1280e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 128127cdeb70SMiao Xie 12827237f183SYan Zheng /* 128327cdeb70SMiao Xie * DON'T set REF_COWS for log trees 128427cdeb70SMiao Xie * 12857237f183SYan Zheng * log trees do not get reference counted because they go away 12867237f183SYan Zheng * before a real commit is actually done. They do store pointers 12877237f183SYan Zheng * to file data extents, and those reference counts still get 12887237f183SYan Zheng * updated (along with back refs to the log tree). 12897237f183SYan Zheng */ 1290e02119d5SChris Mason 12914d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 12924d75f8a9SDavid Sterba NULL, 0, 0, 0); 12937237f183SYan Zheng if (IS_ERR(leaf)) { 129400246528SJosef Bacik btrfs_put_root(root); 12957237f183SYan Zheng return ERR_CAST(leaf); 12967237f183SYan Zheng } 1297e02119d5SChris Mason 12987237f183SYan Zheng root->node = leaf; 1299e02119d5SChris Mason 1300e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1301e02119d5SChris Mason btrfs_tree_unlock(root->node); 13027237f183SYan Zheng return root; 13037237f183SYan Zheng } 13047237f183SYan Zheng 13057237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13067237f183SYan Zheng struct btrfs_fs_info *fs_info) 13077237f183SYan Zheng { 13087237f183SYan Zheng struct btrfs_root *log_root; 13097237f183SYan Zheng 13107237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13117237f183SYan Zheng if (IS_ERR(log_root)) 13127237f183SYan Zheng return PTR_ERR(log_root); 13137237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13147237f183SYan Zheng fs_info->log_root_tree = log_root; 13157237f183SYan Zheng return 0; 13167237f183SYan Zheng } 13177237f183SYan Zheng 13187237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13197237f183SYan Zheng struct btrfs_root *root) 13207237f183SYan Zheng { 13210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13227237f183SYan Zheng struct btrfs_root *log_root; 13237237f183SYan Zheng struct btrfs_inode_item *inode_item; 13247237f183SYan Zheng 13250b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13267237f183SYan Zheng if (IS_ERR(log_root)) 13277237f183SYan Zheng return PTR_ERR(log_root); 13287237f183SYan Zheng 13297237f183SYan Zheng log_root->last_trans = trans->transid; 13307237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13317237f183SYan Zheng 13327237f183SYan Zheng inode_item = &log_root->root_item.inode; 13333cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13343cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13353cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1336da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13370b246afaSJeff Mahoney fs_info->nodesize); 13383cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13397237f183SYan Zheng 13405d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13417237f183SYan Zheng 13427237f183SYan Zheng WARN_ON(root->log_root); 13437237f183SYan Zheng root->log_root = log_root; 13447237f183SYan Zheng root->log_transid = 0; 1345d1433debSMiao Xie root->log_transid_committed = -1; 1346257c62e1SChris Mason root->last_log_commit = 0; 1347e02119d5SChris Mason return 0; 1348e02119d5SChris Mason } 1349e02119d5SChris Mason 135062a2c73eSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1351cb517eabSMiao Xie struct btrfs_key *key) 1352e02119d5SChris Mason { 1353e02119d5SChris Mason struct btrfs_root *root; 1354e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 13550f7d52f4SChris Mason struct btrfs_path *path; 135684234f3aSYan Zheng u64 generation; 1357cb517eabSMiao Xie int ret; 1358581c1760SQu Wenruo int level; 1359cb517eabSMiao Xie 1360cb517eabSMiao Xie path = btrfs_alloc_path(); 1361cb517eabSMiao Xie if (!path) 1362cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 13630f7d52f4SChris Mason 136496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 1365cb517eabSMiao Xie if (!root) { 1366cb517eabSMiao Xie ret = -ENOMEM; 1367cb517eabSMiao Xie goto alloc_fail; 13680f7d52f4SChris Mason } 13690f7d52f4SChris Mason 1370cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1371cb517eabSMiao Xie &root->root_item, &root->root_key); 13720f7d52f4SChris Mason if (ret) { 137313a8a7c8SYan, Zheng if (ret > 0) 137413a8a7c8SYan, Zheng ret = -ENOENT; 1375cb517eabSMiao Xie goto find_fail; 13760f7d52f4SChris Mason } 137713a8a7c8SYan, Zheng 137884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1379581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 13802ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 13812ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1382581c1760SQu Wenruo generation, level, NULL); 138364c043deSLiu Bo if (IS_ERR(root->node)) { 138464c043deSLiu Bo ret = PTR_ERR(root->node); 1385cb517eabSMiao Xie goto find_fail; 1386cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1387cb517eabSMiao Xie ret = -EIO; 138864c043deSLiu Bo free_extent_buffer(root->node); 138964c043deSLiu Bo goto find_fail; 1390416bc658SJosef Bacik } 13915d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 139213a8a7c8SYan, Zheng out: 1393cb517eabSMiao Xie btrfs_free_path(path); 1394cb517eabSMiao Xie return root; 1395cb517eabSMiao Xie 1396cb517eabSMiao Xie find_fail: 139700246528SJosef Bacik btrfs_put_root(root); 1398cb517eabSMiao Xie alloc_fail: 1399cb517eabSMiao Xie root = ERR_PTR(ret); 1400cb517eabSMiao Xie goto out; 1401cb517eabSMiao Xie } 1402cb517eabSMiao Xie 1403a98db0f3SJosef Bacik static int btrfs_init_fs_root(struct btrfs_root *root) 1404cb517eabSMiao Xie { 1405cb517eabSMiao Xie int ret; 1406dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1407cb517eabSMiao Xie 1408cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1409cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1410cb517eabSMiao Xie GFP_NOFS); 1411cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1412cb517eabSMiao Xie ret = -ENOMEM; 1413cb517eabSMiao Xie goto fail; 1414cb517eabSMiao Xie } 1415cb517eabSMiao Xie 1416dcc3eb96SNikolay Borisov /* 1417dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1418dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1419dcc3eb96SNikolay Borisov */ 1420dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1421dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1422dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1423dcc3eb96SNikolay Borisov if (ret) 14248257b2dcSMiao Xie goto fail; 14258257b2dcSMiao Xie 1426f39e4571SJosef Bacik if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 1427f39e4571SJosef Bacik set_bit(BTRFS_ROOT_REF_COWS, &root->state); 1428f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1429f39e4571SJosef Bacik } 1430f39e4571SJosef Bacik 1431cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 143257cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 143357cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1434cb517eabSMiao Xie 1435cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1436cb517eabSMiao Xie if (ret) 1437876d2cf1SLiu Bo goto fail; 1438f32e48e9SChandan Rajendra 1439f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1440f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1441f32e48e9SChandan Rajendra &root->highest_objectid); 1442f32e48e9SChandan Rajendra if (ret) { 1443f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1444876d2cf1SLiu Bo goto fail; 1445f32e48e9SChandan Rajendra } 1446f32e48e9SChandan Rajendra 1447f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1448f32e48e9SChandan Rajendra 1449f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1450f32e48e9SChandan Rajendra 1451cb517eabSMiao Xie return 0; 1452cb517eabSMiao Xie fail: 145384db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1454cb517eabSMiao Xie return ret; 1455cb517eabSMiao Xie } 1456cb517eabSMiao Xie 1457a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1458cb517eabSMiao Xie u64 root_id) 1459cb517eabSMiao Xie { 1460cb517eabSMiao Xie struct btrfs_root *root; 1461cb517eabSMiao Xie 1462cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1463cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1464cb517eabSMiao Xie (unsigned long)root_id); 1465bc44d7c4SJosef Bacik if (root) 146600246528SJosef Bacik root = btrfs_grab_root(root); 1467cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1468cb517eabSMiao Xie return root; 1469cb517eabSMiao Xie } 1470cb517eabSMiao Xie 1471cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1472cb517eabSMiao Xie struct btrfs_root *root) 1473cb517eabSMiao Xie { 1474cb517eabSMiao Xie int ret; 1475cb517eabSMiao Xie 1476e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1477cb517eabSMiao Xie if (ret) 1478cb517eabSMiao Xie return ret; 1479cb517eabSMiao Xie 1480cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1481cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1482cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1483cb517eabSMiao Xie root); 1484af01d2e5SJosef Bacik if (ret == 0) { 148500246528SJosef Bacik btrfs_grab_root(root); 148627cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1487af01d2e5SJosef Bacik } 1488cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1489cb517eabSMiao Xie radix_tree_preload_end(); 1490cb517eabSMiao Xie 1491cb517eabSMiao Xie return ret; 1492cb517eabSMiao Xie } 1493cb517eabSMiao Xie 1494bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1495bd647ce3SJosef Bacik { 1496bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1497bd647ce3SJosef Bacik struct btrfs_root *root; 1498bd647ce3SJosef Bacik 1499bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1500bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1501bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1502bd647ce3SJosef Bacik btrfs_err(fs_info, "leaked root %llu-%llu refcount %d", 1503bd647ce3SJosef Bacik root->root_key.objectid, root->root_key.offset, 1504bd647ce3SJosef Bacik refcount_read(&root->refs)); 1505bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 150600246528SJosef Bacik btrfs_put_root(root); 150700246528SJosef Bacik btrfs_put_root(root); 1508bd647ce3SJosef Bacik } 1509bd647ce3SJosef Bacik #endif 1510bd647ce3SJosef Bacik } 1511bd647ce3SJosef Bacik 15120d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15130d4b0463SJosef Bacik { 1514141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1515141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1516141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1517141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1518141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1519141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1520141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15210d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15220d4b0463SJosef Bacik kfree(fs_info->delayed_root); 152300246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 152400246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 152500246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 152600246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 152700246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 152800246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 152900246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 153000246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 153100246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1532bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15333fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 15340d4b0463SJosef Bacik kfree(fs_info->super_copy); 15350d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15360d4b0463SJosef Bacik kvfree(fs_info); 15370d4b0463SJosef Bacik } 15380d4b0463SJosef Bacik 15390d4b0463SJosef Bacik 1540c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1541c00869f1SMiao Xie struct btrfs_key *location, 1542c00869f1SMiao Xie bool check_ref) 15435eda7b5eSChris Mason { 15445eda7b5eSChris Mason struct btrfs_root *root; 1545381cf658SDavid Sterba struct btrfs_path *path; 15461d4c08e0SDavid Sterba struct btrfs_key key; 15475eda7b5eSChris Mason int ret; 15485eda7b5eSChris Mason 1549edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 155000246528SJosef Bacik return btrfs_grab_root(fs_info->tree_root); 1551edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 155200246528SJosef Bacik return btrfs_grab_root(fs_info->extent_root); 15538f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 155400246528SJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 15558f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 155600246528SJosef Bacik return btrfs_grab_root(fs_info->dev_root); 15570403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 155800246528SJosef Bacik return btrfs_grab_root(fs_info->csum_root); 1559bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 156000246528SJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 1561bc44d7c4SJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 1562f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 156300246528SJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 1564bc44d7c4SJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 156570f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 156600246528SJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 1567bc44d7c4SJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 15684df27c4dSYan, Zheng again: 1569cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 157048475471SStefan Behrens if (root) { 1571bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 157200246528SJosef Bacik btrfs_put_root(root); 157348475471SStefan Behrens return ERR_PTR(-ENOENT); 1574bc44d7c4SJosef Bacik } 15755eda7b5eSChris Mason return root; 157648475471SStefan Behrens } 15775eda7b5eSChris Mason 157883db2aadSJosef Bacik root = btrfs_read_tree_root(fs_info->tree_root, location); 15795eda7b5eSChris Mason if (IS_ERR(root)) 15805eda7b5eSChris Mason return root; 15813394e160SChris Mason 1582c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1583d68fc57bSYan, Zheng ret = -ENOENT; 1584d68fc57bSYan, Zheng goto fail; 1585d68fc57bSYan, Zheng } 1586d68fc57bSYan, Zheng 1587cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1588cb517eabSMiao Xie if (ret) 1589cb517eabSMiao Xie goto fail; 1590cb517eabSMiao Xie 1591381cf658SDavid Sterba path = btrfs_alloc_path(); 1592381cf658SDavid Sterba if (!path) { 1593381cf658SDavid Sterba ret = -ENOMEM; 1594381cf658SDavid Sterba goto fail; 1595381cf658SDavid Sterba } 15961d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 15971d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 15981d4c08e0SDavid Sterba key.offset = location->objectid; 15991d4c08e0SDavid Sterba 16001d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1601381cf658SDavid Sterba btrfs_free_path(path); 1602d68fc57bSYan, Zheng if (ret < 0) 1603d68fc57bSYan, Zheng goto fail; 1604d68fc57bSYan, Zheng if (ret == 0) 160527cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1606d68fc57bSYan, Zheng 1607bc44d7c4SJosef Bacik /* 1608bc44d7c4SJosef Bacik * All roots have two refs on them at all times, one for the mounted fs, 1609bc44d7c4SJosef Bacik * and one for being in the radix tree. This way we only free the root 1610bc44d7c4SJosef Bacik * when we are unmounting or deleting the subvolume. We get one ref 1611bc44d7c4SJosef Bacik * from __setup_root, one for inserting it into the radix tree, and then 1612bc44d7c4SJosef Bacik * we have the third for returning it, and the caller will put it when 1613bc44d7c4SJosef Bacik * it's done with the root. 1614bc44d7c4SJosef Bacik */ 161500246528SJosef Bacik btrfs_grab_root(root); 1616cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16170f7d52f4SChris Mason if (ret) { 161800246528SJosef Bacik btrfs_put_root(root); 16194df27c4dSYan, Zheng if (ret == -EEXIST) { 162084db5ccfSDavid Sterba btrfs_free_fs_root(root); 16214df27c4dSYan, Zheng goto again; 16220f7d52f4SChris Mason } 16234df27c4dSYan, Zheng goto fail; 16244df27c4dSYan, Zheng } 1625edbd8d4eSChris Mason return root; 16264df27c4dSYan, Zheng fail: 162784db5ccfSDavid Sterba btrfs_free_fs_root(root); 16284df27c4dSYan, Zheng return ERR_PTR(ret); 1629edbd8d4eSChris Mason } 1630edbd8d4eSChris Mason 163104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 163204160088SChris Mason { 163304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 163404160088SChris Mason int ret = 0; 163504160088SChris Mason struct btrfs_device *device; 163604160088SChris Mason struct backing_dev_info *bdi; 1637b7967db7SChris Mason 16381f78160cSXiao Guangrong rcu_read_lock(); 16391f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1640dfe25020SChris Mason if (!device->bdev) 1641dfe25020SChris Mason continue; 1642efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1643ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 164404160088SChris Mason ret = 1; 164504160088SChris Mason break; 164604160088SChris Mason } 164704160088SChris Mason } 16481f78160cSXiao Guangrong rcu_read_unlock(); 164904160088SChris Mason return ret; 165004160088SChris Mason } 165104160088SChris Mason 16528b712842SChris Mason /* 16538b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16548b712842SChris Mason * functions. This is where read checksum verification actually happens 16558b712842SChris Mason */ 16568b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1657ce9adaa5SChris Mason { 1658ce9adaa5SChris Mason struct bio *bio; 165997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1660ce9adaa5SChris Mason 166197eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1662ce9adaa5SChris Mason bio = end_io_wq->bio; 16638b712842SChris Mason 16644e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1665ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1666ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 16674246a0b6SChristoph Hellwig bio_endio(bio); 16689be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1669ce9adaa5SChris Mason } 167044b8bd7eSChris Mason 1671a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1672a74a4b97SChris Mason { 1673a74a4b97SChris Mason struct btrfs_root *root = arg; 16740b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1675d0278245SMiao Xie int again; 1676a74a4b97SChris Mason 1677d6fd0ae2SOmar Sandoval while (1) { 1678d0278245SMiao Xie again = 0; 16799d1a2a3aSDavid Sterba 1680fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1681fd340d0fSJosef Bacik 1682d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 16832ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1684d0278245SMiao Xie goto sleep; 1685d0278245SMiao Xie 168690c711abSZygo Blaxell /* 168790c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 168890c711abSZygo Blaxell * before we have finished mounting the filesystem. 168990c711abSZygo Blaxell */ 16900b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 169190c711abSZygo Blaxell goto sleep; 169290c711abSZygo Blaxell 16930b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1694d0278245SMiao Xie goto sleep; 1695d0278245SMiao Xie 1696dc7f370cSMiao Xie /* 1697dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1698dc7f370cSMiao Xie * during the above check and trylock. 1699dc7f370cSMiao Xie */ 17002ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17010b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1702dc7f370cSMiao Xie goto sleep; 1703dc7f370cSMiao Xie } 1704dc7f370cSMiao Xie 17052ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1706c2d6cb16SFilipe Manana 17079d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17080b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1709a74a4b97SChris Mason 1710d0278245SMiao Xie /* 171105323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 171205323cd1SMiao Xie * needn't do anything special here. 1713d0278245SMiao Xie */ 17140b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 171567c5e7d4SFilipe Manana 171667c5e7d4SFilipe Manana /* 171767c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 171867c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 171967c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 172067c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 172167c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 172267c5e7d4SFilipe Manana * unused block groups. 172367c5e7d4SFilipe Manana */ 17240b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1725d0278245SMiao Xie sleep: 1726fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1727d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1728d6fd0ae2SOmar Sandoval kthread_parkme(); 1729d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1730d6fd0ae2SOmar Sandoval return 0; 1731838fe188SJiri Kosina if (!again) { 1732a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1733a74a4b97SChris Mason schedule(); 1734a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1735a74a4b97SChris Mason } 1736da288d28SFilipe Manana } 1737a74a4b97SChris Mason } 1738a74a4b97SChris Mason 1739a74a4b97SChris Mason static int transaction_kthread(void *arg) 1740a74a4b97SChris Mason { 1741a74a4b97SChris Mason struct btrfs_root *root = arg; 17420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1743a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1744a74a4b97SChris Mason struct btrfs_transaction *cur; 17458929ecfaSYan, Zheng u64 transid; 1746a944442cSAllen Pais time64_t now; 1747a74a4b97SChris Mason unsigned long delay; 1748914b2007SJan Kara bool cannot_commit; 1749a74a4b97SChris Mason 1750a74a4b97SChris Mason do { 1751914b2007SJan Kara cannot_commit = false; 17520b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17530b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1754a74a4b97SChris Mason 17550b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17560b246afaSJeff Mahoney cur = fs_info->running_transaction; 1757a74a4b97SChris Mason if (!cur) { 17580b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1759a74a4b97SChris Mason goto sleep; 1760a74a4b97SChris Mason } 176131153d81SYan Zheng 1762afd48513SArnd Bergmann now = ktime_get_seconds(); 17633296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1764a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 17658b87dc17SDavid Sterba (now < cur->start_time || 17660b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17670b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1768a74a4b97SChris Mason delay = HZ * 5; 1769a74a4b97SChris Mason goto sleep; 1770a74a4b97SChris Mason } 17718929ecfaSYan, Zheng transid = cur->transid; 17720b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 177356bec294SChris Mason 177479787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1775354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1776914b2007SJan Kara if (IS_ERR(trans)) { 1777354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1778914b2007SJan Kara cannot_commit = true; 177979787eaaSJeff Mahoney goto sleep; 1780914b2007SJan Kara } 17818929ecfaSYan, Zheng if (transid == trans->transid) { 17823a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 17838929ecfaSYan, Zheng } else { 17843a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17858929ecfaSYan, Zheng } 1786a74a4b97SChris Mason sleep: 17870b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 17880b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1789a74a4b97SChris Mason 17904e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 17910b246afaSJeff Mahoney &fs_info->fs_state))) 17922ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 17938929ecfaSYan, Zheng if (!kthread_should_stop() && 17940b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1795914b2007SJan Kara cannot_commit)) 1796bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1797a74a4b97SChris Mason } while (!kthread_should_stop()); 1798a74a4b97SChris Mason return 0; 1799a74a4b97SChris Mason } 1800a74a4b97SChris Mason 1801af31f5e5SChris Mason /* 180201f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 180301f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 180401f0f9daSNikolay Borisov * anything. 1805af31f5e5SChris Mason * 1806af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1807af31f5e5SChris Mason * generation of the latest root in the array with the generation 1808af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1809af31f5e5SChris Mason */ 181001f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1811af31f5e5SChris Mason { 181201f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1813af31f5e5SChris Mason u64 cur; 1814af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1815af31f5e5SChris Mason int i; 1816af31f5e5SChris Mason 1817af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1818af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1819af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1820af31f5e5SChris Mason if (cur == newest_gen) 182101f0f9daSNikolay Borisov return i; 1822af31f5e5SChris Mason } 1823af31f5e5SChris Mason 182401f0f9daSNikolay Borisov return -EINVAL; 1825af31f5e5SChris Mason } 1826af31f5e5SChris Mason 1827af31f5e5SChris Mason /* 1828af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1829af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1830af31f5e5SChris Mason * done 1831af31f5e5SChris Mason */ 1832af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1833af31f5e5SChris Mason { 18346ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1835af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1836af31f5e5SChris Mason 1837af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1838af31f5e5SChris Mason 1839af31f5e5SChris Mason /* 1840af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1841af31f5e5SChris Mason * regardless of which ones we use today 1842af31f5e5SChris Mason */ 1843af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1844af31f5e5SChris Mason 1845af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1846af31f5e5SChris Mason 1847af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1848af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1849af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1850af31f5e5SChris Mason 1851af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1852af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1853af31f5e5SChris Mason 1854af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1855af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1856af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1857af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1858af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1859af31f5e5SChris Mason 1860af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1861af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1862af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1863af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1864af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1865af31f5e5SChris Mason 18667c7e82a7SChris Mason /* 18677c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18687c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18697c7e82a7SChris Mason */ 18707c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18717c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18727c7e82a7SChris Mason info->fs_root->node->start); 1873af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1874af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1875af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1876af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18777c7e82a7SChris Mason } 1878af31f5e5SChris Mason 1879af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1880af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1881af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1882af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1883af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1884af31f5e5SChris Mason 1885af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1886af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1887af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1888af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1889af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1890af31f5e5SChris Mason 1891af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1892af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1893af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1894af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1895af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1896af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason /* 1899af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1900af31f5e5SChris Mason * for the next commit 1901af31f5e5SChris Mason */ 1902af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1903af31f5e5SChris Mason &info->super_for_commit->super_roots, 1904af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1905af31f5e5SChris Mason } 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason /* 1908bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1909bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1910bd2336b2SNikolay Borisov * 1911bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1912bd2336b2SNikolay Borisov * priority - priority of backup root required 1913bd2336b2SNikolay Borisov * 1914bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1915bd2336b2SNikolay Borisov */ 1916bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1917bd2336b2SNikolay Borisov { 1918bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1919bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1920bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1921bd2336b2SNikolay Borisov 1922bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1923bd2336b2SNikolay Borisov if (priority == 0) 1924bd2336b2SNikolay Borisov return backup_index; 1925bd2336b2SNikolay Borisov 1926bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1927bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1928bd2336b2SNikolay Borisov } else { 1929bd2336b2SNikolay Borisov return -EINVAL; 1930bd2336b2SNikolay Borisov } 1931bd2336b2SNikolay Borisov 1932bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1933bd2336b2SNikolay Borisov 1934bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1935bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1936bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1937bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1938bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1939bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1940bd2336b2SNikolay Borisov 1941bd2336b2SNikolay Borisov /* 1942bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1943bd2336b2SNikolay Borisov * need a fsck 1944bd2336b2SNikolay Borisov */ 1945bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1946bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1947bd2336b2SNikolay Borisov 1948bd2336b2SNikolay Borisov return backup_index; 1949bd2336b2SNikolay Borisov } 1950bd2336b2SNikolay Borisov 19517abadb64SLiu Bo /* helper to cleanup workers */ 19527abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19537abadb64SLiu Bo { 1954dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1955afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 19565cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 1957fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 1958fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 19598b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 1960d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 1961fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 1962fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 19635b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 1964e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 1965736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 1966a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 1967fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 1968b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 1969b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 1970a9b9477dSFilipe Manana /* 1971a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 1972a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 1973a9b9477dSFilipe Manana * queues can do metadata I/O operations. 1974a9b9477dSFilipe Manana */ 1975a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 1976a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 19777abadb64SLiu Bo } 19787abadb64SLiu Bo 19792e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 19802e9f5954SRashika { 19812e9f5954SRashika if (root) { 19822e9f5954SRashika free_extent_buffer(root->node); 19832e9f5954SRashika free_extent_buffer(root->commit_root); 19842e9f5954SRashika root->node = NULL; 19852e9f5954SRashika root->commit_root = NULL; 19862e9f5954SRashika } 19872e9f5954SRashika } 19882e9f5954SRashika 1989af31f5e5SChris Mason /* helper to cleanup tree roots */ 19904273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 1991af31f5e5SChris Mason { 19922e9f5954SRashika free_root_extent_buffers(info->tree_root); 1993655b09feSJosef Bacik 19942e9f5954SRashika free_root_extent_buffers(info->dev_root); 19952e9f5954SRashika free_root_extent_buffers(info->extent_root); 19962e9f5954SRashika free_root_extent_buffers(info->csum_root); 19972e9f5954SRashika free_root_extent_buffers(info->quota_root); 19982e9f5954SRashika free_root_extent_buffers(info->uuid_root); 19994273eaffSAnand Jain if (free_chunk_root) 20002e9f5954SRashika free_root_extent_buffers(info->chunk_root); 200170f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2002af31f5e5SChris Mason } 2003af31f5e5SChris Mason 2004faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2005171f6537SJosef Bacik { 2006171f6537SJosef Bacik int ret; 2007171f6537SJosef Bacik struct btrfs_root *gang[8]; 2008171f6537SJosef Bacik int i; 2009171f6537SJosef Bacik 2010171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2011171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2012171f6537SJosef Bacik struct btrfs_root, root_list); 2013171f6537SJosef Bacik list_del(&gang[0]->root_list); 2014171f6537SJosef Bacik 201527cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2016cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2017171f6537SJosef Bacik } else { 2018171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2019171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 202000246528SJosef Bacik btrfs_put_root(gang[0]); 2021171f6537SJosef Bacik } 2022171f6537SJosef Bacik } 2023171f6537SJosef Bacik 2024171f6537SJosef Bacik while (1) { 2025171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2026171f6537SJosef Bacik (void **)gang, 0, 2027171f6537SJosef Bacik ARRAY_SIZE(gang)); 2028171f6537SJosef Bacik if (!ret) 2029171f6537SJosef Bacik break; 2030171f6537SJosef Bacik for (i = 0; i < ret; i++) 2031cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2032171f6537SJosef Bacik } 20331a4319ccSLiu Bo 2034fe119a6eSNikolay Borisov if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 20351a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 2036171f6537SJosef Bacik } 2037af31f5e5SChris Mason 2038638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2039638aa7edSEric Sandeen { 2040638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2041638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2042638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2043638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2044638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2045638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2046ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2047638aa7edSEric Sandeen } 2048638aa7edSEric Sandeen 2049779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2050779a65a4SEric Sandeen { 2051779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2052779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2053779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2054779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2055779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2056779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2057779a65a4SEric Sandeen } 2058779a65a4SEric Sandeen 20596bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2060f37938e0SEric Sandeen { 20612ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 20622ff7e61eSJeff Mahoney 20632ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 20642ff7e61eSJeff Mahoney set_nlink(inode, 1); 2065f37938e0SEric Sandeen /* 2066f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2067f37938e0SEric Sandeen * the real end of the address space is determined by all of 2068f37938e0SEric Sandeen * the devices in the system 2069f37938e0SEric Sandeen */ 20702ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 20712ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2072f37938e0SEric Sandeen 20732ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 207443eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 207543eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 20767b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 20772ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2078f37938e0SEric Sandeen 20792ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2080f37938e0SEric Sandeen 20812ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 20822ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 20832ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 20842ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2085f37938e0SEric Sandeen } 2086f37938e0SEric Sandeen 2087ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2088ad618368SEric Sandeen { 2089ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2090129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 20917f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2092ad618368SEric Sandeen } 2093ad618368SEric Sandeen 2094f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2095f9e92e40SEric Sandeen { 2096f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2097f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2098f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2099f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2100f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2101f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2102d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2103f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2104f9e92e40SEric Sandeen } 2105f9e92e40SEric Sandeen 21062a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21072a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21082a458198SEric Sandeen { 2109f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21106f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21112a458198SEric Sandeen 21122a458198SEric Sandeen fs_info->workers = 2113cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2114cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21152a458198SEric Sandeen 21162a458198SEric Sandeen fs_info->delalloc_workers = 2117cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2118cb001095SJeff Mahoney flags, max_active, 2); 21192a458198SEric Sandeen 21202a458198SEric Sandeen fs_info->flush_workers = 2121cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2122cb001095SJeff Mahoney flags, max_active, 0); 21232a458198SEric Sandeen 21242a458198SEric Sandeen fs_info->caching_workers = 2125cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21262a458198SEric Sandeen 21272a458198SEric Sandeen fs_info->fixup_workers = 2128cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21292a458198SEric Sandeen 21302a458198SEric Sandeen /* 21312a458198SEric Sandeen * endios are largely parallel and should have a very 21322a458198SEric Sandeen * low idle thresh 21332a458198SEric Sandeen */ 21342a458198SEric Sandeen fs_info->endio_workers = 2135cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21362a458198SEric Sandeen fs_info->endio_meta_workers = 2137cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2138cb001095SJeff Mahoney max_active, 4); 21392a458198SEric Sandeen fs_info->endio_meta_write_workers = 2140cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2141cb001095SJeff Mahoney max_active, 2); 21422a458198SEric Sandeen fs_info->endio_raid56_workers = 2143cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2144cb001095SJeff Mahoney max_active, 4); 21452a458198SEric Sandeen fs_info->endio_repair_workers = 2146cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 21472a458198SEric Sandeen fs_info->rmw_workers = 2148cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21492a458198SEric Sandeen fs_info->endio_write_workers = 2150cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2151cb001095SJeff Mahoney max_active, 2); 21522a458198SEric Sandeen fs_info->endio_freespace_worker = 2153cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2154cb001095SJeff Mahoney max_active, 0); 21552a458198SEric Sandeen fs_info->delayed_workers = 2156cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2157cb001095SJeff Mahoney max_active, 0); 21582a458198SEric Sandeen fs_info->readahead_workers = 2159cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2160cb001095SJeff Mahoney max_active, 2); 21612a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2162cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2163b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2164b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 21652a458198SEric Sandeen 21662a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2167ba8a9d07SChris Mason fs_info->flush_workers && 21682a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 21692a458198SEric Sandeen fs_info->endio_meta_write_workers && 21702a458198SEric Sandeen fs_info->endio_repair_workers && 21712a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 21722a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 21732a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 21742a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2175b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2176b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 21772a458198SEric Sandeen return -ENOMEM; 21782a458198SEric Sandeen } 21792a458198SEric Sandeen 21802a458198SEric Sandeen return 0; 21812a458198SEric Sandeen } 21822a458198SEric Sandeen 21836d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 21846d97c6e3SJohannes Thumshirn { 21856d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2186b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 21876d97c6e3SJohannes Thumshirn 2188b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 21896d97c6e3SJohannes Thumshirn 21906d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 21916d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2192b4e967beSDavid Sterba csum_driver); 21936d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 21946d97c6e3SJohannes Thumshirn } 21956d97c6e3SJohannes Thumshirn 21966d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 21976d97c6e3SJohannes Thumshirn 21986d97c6e3SJohannes Thumshirn return 0; 21996d97c6e3SJohannes Thumshirn } 22006d97c6e3SJohannes Thumshirn 220163443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 220263443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 220363443bf5SEric Sandeen { 220463443bf5SEric Sandeen int ret; 220563443bf5SEric Sandeen struct btrfs_root *log_tree_root; 220663443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 220763443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2208581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 220963443bf5SEric Sandeen 221063443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2211f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 221263443bf5SEric Sandeen return -EIO; 221363443bf5SEric Sandeen } 221463443bf5SEric Sandeen 221596dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 221696dfcb46SJosef Bacik GFP_KERNEL); 221763443bf5SEric Sandeen if (!log_tree_root) 221863443bf5SEric Sandeen return -ENOMEM; 221963443bf5SEric Sandeen 22202ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2221581c1760SQu Wenruo fs_info->generation + 1, 2222581c1760SQu Wenruo level, NULL); 222364c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2224f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22250eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 222600246528SJosef Bacik btrfs_put_root(log_tree_root); 22270eeff236SLiu Bo return ret; 222864c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2229f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 223063443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 223100246528SJosef Bacik btrfs_put_root(log_tree_root); 223263443bf5SEric Sandeen return -EIO; 223363443bf5SEric Sandeen } 223463443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 223563443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 223663443bf5SEric Sandeen if (ret) { 22370b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 223863443bf5SEric Sandeen "Failed to recover log tree"); 223963443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 224000246528SJosef Bacik btrfs_put_root(log_tree_root); 224163443bf5SEric Sandeen return ret; 224263443bf5SEric Sandeen } 224363443bf5SEric Sandeen 2244bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22456bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 224663443bf5SEric Sandeen if (ret) 224763443bf5SEric Sandeen return ret; 224863443bf5SEric Sandeen } 224963443bf5SEric Sandeen 225063443bf5SEric Sandeen return 0; 225163443bf5SEric Sandeen } 225263443bf5SEric Sandeen 22536bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 22544bbcaa64SEric Sandeen { 22556bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2256a4f3d2c4SDavid Sterba struct btrfs_root *root; 22574bbcaa64SEric Sandeen struct btrfs_key location; 22584bbcaa64SEric Sandeen int ret; 22594bbcaa64SEric Sandeen 22606bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 22616bccf3abSJeff Mahoney 22624bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 22634bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 22644bbcaa64SEric Sandeen location.offset = 0; 22654bbcaa64SEric Sandeen 2266a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2267f50f4353SLiu Bo if (IS_ERR(root)) { 2268f50f4353SLiu Bo ret = PTR_ERR(root); 2269f50f4353SLiu Bo goto out; 2270f50f4353SLiu Bo } 2271a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2272a4f3d2c4SDavid Sterba fs_info->extent_root = root; 22734bbcaa64SEric Sandeen 22744bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2275a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2276f50f4353SLiu Bo if (IS_ERR(root)) { 2277f50f4353SLiu Bo ret = PTR_ERR(root); 2278f50f4353SLiu Bo goto out; 2279f50f4353SLiu Bo } 2280a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2281a4f3d2c4SDavid Sterba fs_info->dev_root = root; 22824bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 22834bbcaa64SEric Sandeen 22844bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2285a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2286f50f4353SLiu Bo if (IS_ERR(root)) { 2287f50f4353SLiu Bo ret = PTR_ERR(root); 2288f50f4353SLiu Bo goto out; 2289f50f4353SLiu Bo } 2290a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2291a4f3d2c4SDavid Sterba fs_info->csum_root = root; 22924bbcaa64SEric Sandeen 22934bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2294a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2295a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2296a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2297afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2298a4f3d2c4SDavid Sterba fs_info->quota_root = root; 22994bbcaa64SEric Sandeen } 23004bbcaa64SEric Sandeen 23014bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2302a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2303a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2304a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23054bbcaa64SEric Sandeen if (ret != -ENOENT) 2306f50f4353SLiu Bo goto out; 23074bbcaa64SEric Sandeen } else { 2308a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2309a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23104bbcaa64SEric Sandeen } 23114bbcaa64SEric Sandeen 231270f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 231370f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 231470f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2315f50f4353SLiu Bo if (IS_ERR(root)) { 2316f50f4353SLiu Bo ret = PTR_ERR(root); 2317f50f4353SLiu Bo goto out; 2318f50f4353SLiu Bo } 231970f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 232070f6d82eSOmar Sandoval fs_info->free_space_root = root; 232170f6d82eSOmar Sandoval } 232270f6d82eSOmar Sandoval 23234bbcaa64SEric Sandeen return 0; 2324f50f4353SLiu Bo out: 2325f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2326f50f4353SLiu Bo location.objectid, ret); 2327f50f4353SLiu Bo return ret; 23284bbcaa64SEric Sandeen } 23294bbcaa64SEric Sandeen 2330069ec957SQu Wenruo /* 2331069ec957SQu Wenruo * Real super block validation 2332069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2333069ec957SQu Wenruo * 2334069ec957SQu Wenruo * @sb: super block to check 2335069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2336069ec957SQu Wenruo * 0 the primary (1st) sb 2337069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2338069ec957SQu Wenruo * -1 skip bytenr check 2339069ec957SQu Wenruo */ 2340069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2341069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 234221a852b0SQu Wenruo { 234321a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 234421a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 234521a852b0SQu Wenruo int ret = 0; 234621a852b0SQu Wenruo 234721a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 234821a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 234921a852b0SQu Wenruo ret = -EINVAL; 235021a852b0SQu Wenruo } 235121a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 235221a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 235321a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 235421a852b0SQu Wenruo ret = -EINVAL; 235521a852b0SQu Wenruo } 235621a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 235721a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 235821a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 235921a852b0SQu Wenruo ret = -EINVAL; 236021a852b0SQu Wenruo } 236121a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 236221a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 236321a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 236421a852b0SQu Wenruo ret = -EINVAL; 236521a852b0SQu Wenruo } 236621a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 236721a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 236821a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 236921a852b0SQu Wenruo ret = -EINVAL; 237021a852b0SQu Wenruo } 237121a852b0SQu Wenruo 237221a852b0SQu Wenruo /* 237321a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 237421a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 237521a852b0SQu Wenruo */ 237621a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 237721a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 237821a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 237921a852b0SQu Wenruo ret = -EINVAL; 238021a852b0SQu Wenruo } 238121a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 238221a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 238321a852b0SQu Wenruo btrfs_err(fs_info, 238421a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 238521a852b0SQu Wenruo sectorsize, PAGE_SIZE); 238621a852b0SQu Wenruo ret = -EINVAL; 238721a852b0SQu Wenruo } 238821a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 238921a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 239021a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 239121a852b0SQu Wenruo ret = -EINVAL; 239221a852b0SQu Wenruo } 239321a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 239421a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 239521a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 239621a852b0SQu Wenruo ret = -EINVAL; 239721a852b0SQu Wenruo } 239821a852b0SQu Wenruo 239921a852b0SQu Wenruo /* Root alignment check */ 240021a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 240121a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 240221a852b0SQu Wenruo btrfs_super_root(sb)); 240321a852b0SQu Wenruo ret = -EINVAL; 240421a852b0SQu Wenruo } 240521a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 240621a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 240721a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 240821a852b0SQu Wenruo ret = -EINVAL; 240921a852b0SQu Wenruo } 241021a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 241121a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 241221a852b0SQu Wenruo btrfs_super_log_root(sb)); 241321a852b0SQu Wenruo ret = -EINVAL; 241421a852b0SQu Wenruo } 241521a852b0SQu Wenruo 2416de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24177239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 241821a852b0SQu Wenruo btrfs_err(fs_info, 24197239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2420de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 242121a852b0SQu Wenruo ret = -EINVAL; 242221a852b0SQu Wenruo } 242321a852b0SQu Wenruo 242421a852b0SQu Wenruo /* 242521a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 242621a852b0SQu Wenruo * done later 242721a852b0SQu Wenruo */ 242821a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 242921a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 243021a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 243121a852b0SQu Wenruo ret = -EINVAL; 243221a852b0SQu Wenruo } 243321a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 243421a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 243521a852b0SQu Wenruo btrfs_super_stripesize(sb)); 243621a852b0SQu Wenruo ret = -EINVAL; 243721a852b0SQu Wenruo } 243821a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 243921a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 244021a852b0SQu Wenruo btrfs_super_num_devices(sb)); 244121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 244221a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 244321a852b0SQu Wenruo ret = -EINVAL; 244421a852b0SQu Wenruo } 244521a852b0SQu Wenruo 2446069ec957SQu Wenruo if (mirror_num >= 0 && 2447069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 244821a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 244921a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 245021a852b0SQu Wenruo ret = -EINVAL; 245121a852b0SQu Wenruo } 245221a852b0SQu Wenruo 245321a852b0SQu Wenruo /* 245421a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 245521a852b0SQu Wenruo * and one chunk 245621a852b0SQu Wenruo */ 245721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 245821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 245921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 246021a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 246121a852b0SQu Wenruo ret = -EINVAL; 246221a852b0SQu Wenruo } 246321a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 246421a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 246521a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 246621a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 246721a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 246821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 246921a852b0SQu Wenruo ret = -EINVAL; 247021a852b0SQu Wenruo } 247121a852b0SQu Wenruo 247221a852b0SQu Wenruo /* 247321a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 247421a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 247521a852b0SQu Wenruo */ 247621a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 247721a852b0SQu Wenruo btrfs_warn(fs_info, 247821a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 247921a852b0SQu Wenruo btrfs_super_generation(sb), 248021a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 248121a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 248221a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 248321a852b0SQu Wenruo btrfs_warn(fs_info, 248421a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 248521a852b0SQu Wenruo btrfs_super_generation(sb), 248621a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 248721a852b0SQu Wenruo 248821a852b0SQu Wenruo return ret; 248921a852b0SQu Wenruo } 249021a852b0SQu Wenruo 2491069ec957SQu Wenruo /* 2492069ec957SQu Wenruo * Validation of super block at mount time. 2493069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2494069ec957SQu Wenruo * flags will be skipped. 2495069ec957SQu Wenruo */ 2496069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2497069ec957SQu Wenruo { 2498069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2499069ec957SQu Wenruo } 2500069ec957SQu Wenruo 250175cb857dSQu Wenruo /* 250275cb857dSQu Wenruo * Validation of super block at write time. 250375cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 250475cb857dSQu Wenruo * overwritten soon. 250575cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 250675cb857dSQu Wenruo */ 250775cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 250875cb857dSQu Wenruo struct btrfs_super_block *sb) 250975cb857dSQu Wenruo { 251075cb857dSQu Wenruo int ret; 251175cb857dSQu Wenruo 251275cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 251375cb857dSQu Wenruo if (ret < 0) 251475cb857dSQu Wenruo goto out; 2515e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 251675cb857dSQu Wenruo ret = -EUCLEAN; 251775cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 251875cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 251975cb857dSQu Wenruo goto out; 252075cb857dSQu Wenruo } 252175cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 252275cb857dSQu Wenruo ret = -EUCLEAN; 252375cb857dSQu Wenruo btrfs_err(fs_info, 252475cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 252575cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 252675cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 252775cb857dSQu Wenruo goto out; 252875cb857dSQu Wenruo } 252975cb857dSQu Wenruo out: 253075cb857dSQu Wenruo if (ret < 0) 253175cb857dSQu Wenruo btrfs_err(fs_info, 253275cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 253375cb857dSQu Wenruo return ret; 253475cb857dSQu Wenruo } 253575cb857dSQu Wenruo 25366ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2537b8522a1eSNikolay Borisov { 25386ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2539b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2540b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2541b8522a1eSNikolay Borisov bool handle_error = false; 2542b8522a1eSNikolay Borisov int ret = 0; 2543b8522a1eSNikolay Borisov int i; 2544b8522a1eSNikolay Borisov 2545b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2546b8522a1eSNikolay Borisov u64 generation; 2547b8522a1eSNikolay Borisov int level; 2548b8522a1eSNikolay Borisov 2549b8522a1eSNikolay Borisov if (handle_error) { 2550b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2551b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2552b8522a1eSNikolay Borisov tree_root->node = NULL; 2553b8522a1eSNikolay Borisov 2554b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2555b8522a1eSNikolay Borisov break; 2556b8522a1eSNikolay Borisov 2557b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2558b8522a1eSNikolay Borisov 2559b8522a1eSNikolay Borisov /* 2560b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2561b8522a1eSNikolay Borisov * valid 2562b8522a1eSNikolay Borisov */ 2563b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2564b8522a1eSNikolay Borisov 2565b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2566b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2567b8522a1eSNikolay Borisov 2568b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 25696ef108ddSNikolay Borisov backup_index = ret; 2570b8522a1eSNikolay Borisov if (ret < 0) 2571b8522a1eSNikolay Borisov return ret; 2572b8522a1eSNikolay Borisov } 2573b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2574b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2575b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 2576b8522a1eSNikolay Borisov generation, level, NULL); 2577b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node) || 2578b8522a1eSNikolay Borisov !extent_buffer_uptodate(tree_root->node)) { 2579b8522a1eSNikolay Borisov handle_error = true; 2580b8522a1eSNikolay Borisov 2581b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node)) 2582b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 2583b8522a1eSNikolay Borisov else if (!extent_buffer_uptodate(tree_root->node)) 2584b8522a1eSNikolay Borisov ret = -EUCLEAN; 2585b8522a1eSNikolay Borisov 2586b8522a1eSNikolay Borisov btrfs_warn(fs_info, "failed to read tree root"); 2587b8522a1eSNikolay Borisov continue; 2588b8522a1eSNikolay Borisov } 2589b8522a1eSNikolay Borisov 2590b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2591b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2592b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2593b8522a1eSNikolay Borisov 2594336a0d8dSNikolay Borisov /* 2595336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2596336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2597336a0d8dSNikolay Borisov */ 2598b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2599b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2600b8522a1eSNikolay Borisov if (ret < 0) { 2601b8522a1eSNikolay Borisov handle_error = true; 2602b8522a1eSNikolay Borisov continue; 2603b8522a1eSNikolay Borisov } 2604b8522a1eSNikolay Borisov 2605b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2606b8522a1eSNikolay Borisov 2607b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2608b8522a1eSNikolay Borisov if (ret < 0) { 2609b8522a1eSNikolay Borisov handle_error = true; 2610b8522a1eSNikolay Borisov continue; 2611b8522a1eSNikolay Borisov } 2612b8522a1eSNikolay Borisov 2613b8522a1eSNikolay Borisov /* All successful */ 2614b8522a1eSNikolay Borisov fs_info->generation = generation; 2615b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26166ef108ddSNikolay Borisov 26176ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26186ef108ddSNikolay Borisov if (backup_index < 0) { 26196ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26206ef108ddSNikolay Borisov } else { 26216ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26226ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26236ef108ddSNikolay Borisov } 2624b8522a1eSNikolay Borisov break; 2625b8522a1eSNikolay Borisov } 2626b8522a1eSNikolay Borisov 2627b8522a1eSNikolay Borisov return ret; 2628b8522a1eSNikolay Borisov } 2629b8522a1eSNikolay Borisov 26308260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2631eb60ceacSChris Mason { 263276dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2633f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26348fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2635facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 263624bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2637eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 263811833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2639eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2640a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 264176dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 264224bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26434cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2644ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2645f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 264647ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2647f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2648d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 264967c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26507585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2651573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2652de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 265319c00ddcSChris Mason 26540b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26556324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2656f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 265747ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2658bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2659bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 26603fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 26613fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2662bd647ce3SJosef Bacik #endif 2663c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 266466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 266566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 266666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 266766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 266866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 266966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 267066d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2671ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2672ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2673ba2c4d4eSJosef Bacik 2674771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 26754cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 26762fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2677034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2678fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 267995ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 26809ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 26814cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2682a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2683f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 26848b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2685f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 268690519d66SArne Jansen /* readahead state */ 2687d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 268890519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2689fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2690c8b97818SChris Mason 2691b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2692b34b086cSChris Mason num_online_cpus() + 2, 8); 26930afbaf8cSChris Mason 2694199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2695199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 269669fe2d75SJosef Bacik 2697638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 269821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 269921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 270021adbd5cSStefan Behrens #endif 2701779a65a4SEric Sandeen btrfs_init_balance(fs_info); 270221c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2703a2de733cSArne Jansen 27040f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 27056bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2706a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 27070f9dd46cSJosef Bacik 2708fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2709fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2710afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 271139279cc3SChris Mason 27125a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2713e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2714925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2715a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2716a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27171bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27189e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2719c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 272076dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2721803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2722fa9c0d79SChris Mason 2723ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2724f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2725b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2726416ac51dSArne Jansen 2727fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2728fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2729fa9c0d79SChris Mason 2730e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2731f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2732bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27334854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2734034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 27353768f368SChris Mason 2736da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2737da17066cSJeff Mahoney fs_info->nodesize = 4096; 2738da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2739da17066cSJeff Mahoney fs_info->stripesize = 4096; 2740da17066cSJeff Mahoney 2741eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2742eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2743eede2bf3SOmar Sandoval 27449e967495SFilipe Manana fs_info->send_in_progress = 0; 27458260edbaSJosef Bacik } 27468260edbaSJosef Bacik 27478260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 27488260edbaSJosef Bacik { 27498260edbaSJosef Bacik int ret; 27508260edbaSJosef Bacik 27518260edbaSJosef Bacik fs_info->sb = sb; 27528260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 27538260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 27549e967495SFilipe Manana 2755ae18c37aSJosef Bacik ret = init_srcu_struct(&fs_info->subvol_srcu); 2756ae18c37aSJosef Bacik if (ret) 2757ae18c37aSJosef Bacik return ret; 2758ae18c37aSJosef Bacik 2759ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2760ae18c37aSJosef Bacik if (ret) 2761ae18c37aSJosef Bacik goto fail; 2762ae18c37aSJosef Bacik 2763ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2764ae18c37aSJosef Bacik if (ret) 2765ae18c37aSJosef Bacik goto fail; 2766ae18c37aSJosef Bacik 2767ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2768ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2769ae18c37aSJosef Bacik 2770ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2771ae18c37aSJosef Bacik if (ret) 2772ae18c37aSJosef Bacik goto fail; 2773ae18c37aSJosef Bacik 2774ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2775ae18c37aSJosef Bacik GFP_KERNEL); 2776ae18c37aSJosef Bacik if (ret) 2777ae18c37aSJosef Bacik goto fail; 2778ae18c37aSJosef Bacik 2779ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2780ae18c37aSJosef Bacik GFP_KERNEL); 2781ae18c37aSJosef Bacik if (!fs_info->delayed_root) { 2782ae18c37aSJosef Bacik ret = -ENOMEM; 2783ae18c37aSJosef Bacik goto fail; 2784ae18c37aSJosef Bacik } 2785ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2786ae18c37aSJosef Bacik 278753b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 2788ae18c37aSJosef Bacik if (ret) 2789ae18c37aSJosef Bacik goto fail; 2790ae18c37aSJosef Bacik 2791ae18c37aSJosef Bacik return 0; 2792ae18c37aSJosef Bacik fail: 2793ae18c37aSJosef Bacik cleanup_srcu_struct(&fs_info->subvol_srcu); 2794ae18c37aSJosef Bacik return ret; 2795ae18c37aSJosef Bacik } 2796ae18c37aSJosef Bacik 279797f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 279897f4dd09SNikolay Borisov { 279997f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 280097f4dd09SNikolay Borisov int ret; 280197f4dd09SNikolay Borisov 280297f4dd09SNikolay Borisov /* 280397f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 280497f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 280597f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 280697f4dd09SNikolay Borisov */ 280797f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 280897f4dd09SNikolay Borisov if (ret < 0) { 2809c94bec2cSJosef Bacik if (ret != -EINTR) 2810c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 2811c94bec2cSJosef Bacik ret); 281297f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 281397f4dd09SNikolay Borisov return ret; 281497f4dd09SNikolay Borisov } 281597f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 281697f4dd09SNikolay Borisov } 281797f4dd09SNikolay Borisov 281897f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 281997f4dd09SNikolay Borisov { 282097f4dd09SNikolay Borisov struct task_struct *task; 282197f4dd09SNikolay Borisov 282297f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 282397f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 282497f4dd09SNikolay Borisov if (IS_ERR(task)) { 282597f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 282697f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 282797f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 282897f4dd09SNikolay Borisov return PTR_ERR(task); 282997f4dd09SNikolay Borisov } 283097f4dd09SNikolay Borisov 283197f4dd09SNikolay Borisov return 0; 283297f4dd09SNikolay Borisov } 283397f4dd09SNikolay Borisov 2834ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 2835ae18c37aSJosef Bacik char *options) 2836ae18c37aSJosef Bacik { 2837ae18c37aSJosef Bacik u32 sectorsize; 2838ae18c37aSJosef Bacik u32 nodesize; 2839ae18c37aSJosef Bacik u32 stripesize; 2840ae18c37aSJosef Bacik u64 generation; 2841ae18c37aSJosef Bacik u64 features; 2842ae18c37aSJosef Bacik u16 csum_type; 2843ae18c37aSJosef Bacik struct btrfs_key location; 2844ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 2845ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2846ae18c37aSJosef Bacik struct btrfs_root *tree_root; 2847ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 2848ae18c37aSJosef Bacik int ret; 2849ae18c37aSJosef Bacik int err = -EINVAL; 2850ae18c37aSJosef Bacik int clear_free_space_tree = 0; 2851ae18c37aSJosef Bacik int level; 2852ae18c37aSJosef Bacik 28538260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 285453b381b3SDavid Woodhouse if (ret) { 285583c8266aSDavid Sterba err = ret; 2856ae18c37aSJosef Bacik goto fail; 285753b381b3SDavid Woodhouse } 285853b381b3SDavid Woodhouse 2859ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 2860ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 2861ae18c37aSJosef Bacik GFP_KERNEL); 2862ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 2863ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 2864ae18c37aSJosef Bacik GFP_KERNEL); 2865ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 2866ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 2867ae18c37aSJosef Bacik err = -ENOMEM; 2868ae18c37aSJosef Bacik goto fail_srcu; 2869ae18c37aSJosef Bacik } 2870ae18c37aSJosef Bacik 2871ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 2872ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 2873ae18c37aSJosef Bacik err = -ENOMEM; 2874ae18c37aSJosef Bacik goto fail_srcu; 2875ae18c37aSJosef Bacik } 2876ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 2877ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 2878ae18c37aSJosef Bacik 28793c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 28801104a885SDavid Sterba 28811104a885SDavid Sterba /* 28821104a885SDavid Sterba * Read super block and check the signature bytes only 28831104a885SDavid Sterba */ 28848f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 28858f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 28868f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 288716cdcec7SMiao Xie goto fail_alloc; 288820b45077SDave Young } 288939279cc3SChris Mason 28901104a885SDavid Sterba /* 28918dc3f22cSJohannes Thumshirn * Verify the type first, if that or the the checksum value are 28928dc3f22cSJohannes Thumshirn * corrupted, we'll find out 28938dc3f22cSJohannes Thumshirn */ 28948f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 289551bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 28968dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 289751bce6c9SJohannes Thumshirn csum_type); 28988dc3f22cSJohannes Thumshirn err = -EINVAL; 28998f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29008dc3f22cSJohannes Thumshirn goto fail_alloc; 29018dc3f22cSJohannes Thumshirn } 29028dc3f22cSJohannes Thumshirn 29036d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 29046d97c6e3SJohannes Thumshirn if (ret) { 29056d97c6e3SJohannes Thumshirn err = ret; 29068f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29076d97c6e3SJohannes Thumshirn goto fail_alloc; 29086d97c6e3SJohannes Thumshirn } 29096d97c6e3SJohannes Thumshirn 29108dc3f22cSJohannes Thumshirn /* 29111104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 29121104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 29131104a885SDavid Sterba */ 29148f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 291505135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 29161104a885SDavid Sterba err = -EINVAL; 29178f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 2918141386e1SJosef Bacik goto fail_alloc; 29191104a885SDavid Sterba } 29201104a885SDavid Sterba 29211104a885SDavid Sterba /* 29221104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 29231104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29241104a885SDavid Sterba * the whole block of INFO_SIZE 29251104a885SDavid Sterba */ 29268f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 29278f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29285f39d397SChris Mason 2929fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2930fbc6feaeSNikolay Borisov 2931de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2932de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2933de37aa51SNikolay Borisov 29347239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2935de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2936de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2937de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29387239ff4bSNikolay Borisov } 29390b86a832SChris Mason 2940fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2941fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2942fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2943fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2944fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2945fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2946fbc6feaeSNikolay Borisov } 2947fbc6feaeSNikolay Borisov 2948fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2949fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2950de37aa51SNikolay Borisov 2951069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 29521104a885SDavid Sterba if (ret) { 295305135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 29541104a885SDavid Sterba err = -EINVAL; 2955141386e1SJosef Bacik goto fail_alloc; 29561104a885SDavid Sterba } 29571104a885SDavid Sterba 29580f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 2959141386e1SJosef Bacik goto fail_alloc; 29600f7d52f4SChris Mason 2961acce952bSliubo /* check FS state, whether FS is broken. */ 296287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 296387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2964acce952bSliubo 296575e7cb7fSLiu Bo /* 296675e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 296775e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 296875e7cb7fSLiu Bo */ 296975e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 297075e7cb7fSLiu Bo 29712ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 29722b82032cSYan Zheng if (ret) { 29732b82032cSYan Zheng err = ret; 2974141386e1SJosef Bacik goto fail_alloc; 29752b82032cSYan Zheng } 2976dfe25020SChris Mason 2977f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2978f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2979f2b636e8SJosef Bacik if (features) { 298005135f59SDavid Sterba btrfs_err(fs_info, 298105135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2982c1c9ff7cSGeert Uytterhoeven features); 2983f2b636e8SJosef Bacik err = -EINVAL; 2984141386e1SJosef Bacik goto fail_alloc; 2985f2b636e8SJosef Bacik } 2986f2b636e8SJosef Bacik 29875d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 29885d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 29890b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2990a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 29915c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 29925c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2993727011e0SChris Mason 29943173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 299505135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 29963173a18fSJosef Bacik 2997727011e0SChris Mason /* 2998727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2999727011e0SChris Mason * they are bigger than the page size 3000727011e0SChris Mason */ 300109cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3002727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 300305135f59SDavid Sterba btrfs_info(fs_info, 300405135f59SDavid Sterba "flagging fs with big metadata feature"); 3005727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3006727011e0SChris Mason } 3007727011e0SChris Mason 3008bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3009bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3010b7f67055SChandan Rajendra stripesize = sectorsize; 3011707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3012963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3013bc3f116fSChris Mason 3014da17066cSJeff Mahoney /* Cache block sizes */ 3015da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3016da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3017da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3018da17066cSJeff Mahoney 3019bc3f116fSChris Mason /* 3020bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3021bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3022bc3f116fSChris Mason */ 3023bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3024707e8a07SDavid Sterba (sectorsize != nodesize)) { 302505135f59SDavid Sterba btrfs_err(fs_info, 302605135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 302705135f59SDavid Sterba nodesize, sectorsize); 3028141386e1SJosef Bacik goto fail_alloc; 3029bc3f116fSChris Mason } 3030bc3f116fSChris Mason 3031ceda0864SMiao Xie /* 3032ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3033ceda0864SMiao Xie * update the flag. 3034ceda0864SMiao Xie */ 30355d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 30365d4f98a2SYan Zheng 3037f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3038f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3039bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 304005135f59SDavid Sterba btrfs_err(fs_info, 304105135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3042c1c9ff7cSGeert Uytterhoeven features); 3043f2b636e8SJosef Bacik err = -EINVAL; 3044141386e1SJosef Bacik goto fail_alloc; 3045f2b636e8SJosef Bacik } 304661d92c32SChris Mason 30472a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 30482a458198SEric Sandeen if (ret) { 30492a458198SEric Sandeen err = ret; 30500dc3b84aSJosef Bacik goto fail_sb_buffer; 30510dc3b84aSJosef Bacik } 30524543df7eSChris Mason 30539e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 30549e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 30559e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 3056b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 30579e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 30589e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 30594575c9ccSChris Mason 3060a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3061a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3062de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3063db94535dSChris Mason 3064925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 30656bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3066925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 306784eed90fSChris Mason if (ret) { 306805135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 30695d4f98a2SYan Zheng goto fail_sb_buffer; 307084eed90fSChris Mason } 30710b86a832SChris Mason 307284234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3073581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 30740b86a832SChris Mason 30752ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 30760b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3077581c1760SQu Wenruo generation, level, NULL); 307864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 307964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 308005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3081e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3082e5fffbacSchandan free_extent_buffer(chunk_root->node); 308395ab1f64SZhao Lei chunk_root->node = NULL; 3084af31f5e5SChris Mason goto fail_tree_roots; 308583121942SDavid Woodhouse } 30865d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 30875d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 30880b86a832SChris Mason 3089e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3090c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3091c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3092e17cade2SChris Mason 30935b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 30942b82032cSYan Zheng if (ret) { 309505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3096af31f5e5SChris Mason goto fail_tree_roots; 30972b82032cSYan Zheng } 30980b86a832SChris Mason 30998dabb742SStefan Behrens /* 31009b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 31019b99b115SAnand Jain * device replace procedure 31028dabb742SStefan Behrens */ 31039b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3104dfe25020SChris Mason 3105a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 310605135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3107a6b0d5c8SChris Mason goto fail_tree_roots; 3108a6b0d5c8SChris Mason } 3109a6b0d5c8SChris Mason 3110b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 31114bbcaa64SEric Sandeen if (ret) 3112b8522a1eSNikolay Borisov goto fail_tree_roots; 31138929ecfaSYan, Zheng 311475ec1db8SJosef Bacik /* 311575ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 311675ec1db8SJosef Bacik * check the generation here so we can set the 311775ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 311875ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 311975ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 312075ec1db8SJosef Bacik * even though it was perfectly fine. 312175ec1db8SJosef Bacik */ 312275ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 312375ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 312475ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 312575ec1db8SJosef Bacik 3126cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3127cf90d884SQu Wenruo if (ret) { 3128cf90d884SQu Wenruo btrfs_err(fs_info, 3129cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3130cf90d884SQu Wenruo ret); 3131cf90d884SQu Wenruo goto fail_block_groups; 3132cf90d884SQu Wenruo } 313368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 313468310a5eSIlya Dryomov if (ret) { 313505135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 313668310a5eSIlya Dryomov goto fail_block_groups; 313768310a5eSIlya Dryomov } 313868310a5eSIlya Dryomov 3139733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3140733f4fbbSStefan Behrens if (ret) { 314105135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3142733f4fbbSStefan Behrens goto fail_block_groups; 3143733f4fbbSStefan Behrens } 3144733f4fbbSStefan Behrens 31458dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 31468dabb742SStefan Behrens if (ret) { 314705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 31488dabb742SStefan Behrens goto fail_block_groups; 31498dabb742SStefan Behrens } 31508dabb742SStefan Behrens 31519b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 31528dabb742SStefan Behrens 3153c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3154b7c35e81SAnand Jain if (ret) { 315505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 315605135f59SDavid Sterba ret); 3157b7c35e81SAnand Jain goto fail_block_groups; 3158b7c35e81SAnand Jain } 3159b7c35e81SAnand Jain 316096f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31615ac1d209SJeff Mahoney if (ret) { 316205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3163b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31645ac1d209SJeff Mahoney } 31655ac1d209SJeff Mahoney 3166c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3167c59021f8Sliubo if (ret) { 316805135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31692365dd3cSAnand Jain goto fail_sysfs; 3170c59021f8Sliubo } 3171c59021f8Sliubo 31725b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31731b1d1f66SJosef Bacik if (ret) { 317405135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 31752365dd3cSAnand Jain goto fail_sysfs; 31761b1d1f66SJosef Bacik } 31774330e183SQu Wenruo 31786528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 317905135f59SDavid Sterba btrfs_warn(fs_info, 318052042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 31812365dd3cSAnand Jain goto fail_sysfs; 3182292fd7fcSStefan Behrens } 31839078a3e1SChris Mason 3184a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3185a74a4b97SChris Mason "btrfs-cleaner"); 318657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 31872365dd3cSAnand Jain goto fail_sysfs; 3188a74a4b97SChris Mason 3189a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3190a74a4b97SChris Mason tree_root, 3191a74a4b97SChris Mason "btrfs-transaction"); 319257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 31933f157a2fSChris Mason goto fail_cleaner; 3194a74a4b97SChris Mason 3195583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3196c289811cSChris Mason !fs_info->fs_devices->rotating) { 3197583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3198c289811cSChris Mason } 3199c289811cSChris Mason 3200572d9ab7SDavid Sterba /* 320101327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3202572d9ab7SDavid Sterba * not have to wait for transaction commit 3203572d9ab7SDavid Sterba */ 3204572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 32053818aea2SQu Wenruo 320621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 32070b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 32082ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 32090b246afaSJeff Mahoney btrfs_test_opt(fs_info, 321021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 321121adbd5cSStefan Behrens 1 : 0, 321221adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 321321adbd5cSStefan Behrens if (ret) 321405135f59SDavid Sterba btrfs_warn(fs_info, 321505135f59SDavid Sterba "failed to initialize integrity check module: %d", 321605135f59SDavid Sterba ret); 321721adbd5cSStefan Behrens } 321821adbd5cSStefan Behrens #endif 3219bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3220bcef60f2SArne Jansen if (ret) 3221bcef60f2SArne Jansen goto fail_trans_kthread; 322221adbd5cSStefan Behrens 3223fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3224fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3225fd708b81SJosef Bacik 322696da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 322796da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32280b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3229e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 323063443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 323179787eaaSJeff Mahoney if (ret) { 323263443bf5SEric Sandeen err = ret; 323328c16cbbSWang Shilong goto fail_qgroup; 3234e556ce2cSYan Zheng } 3235e02119d5SChris Mason } 32361a40e23bSZheng Yan 32376bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 323879787eaaSJeff Mahoney if (ret) 323928c16cbbSWang Shilong goto fail_qgroup; 324076dda93cSYan, Zheng 3241bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3242d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 324344c44af2SIlya Dryomov if (ret) 324428c16cbbSWang Shilong goto fail_qgroup; 324590c711abSZygo Blaxell 324690c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 32475d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 324890c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3249d7ce5843SMiao Xie if (ret < 0) { 325005135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 325105135f59SDavid Sterba ret); 3252d7ce5843SMiao Xie err = -EINVAL; 3253bcef60f2SArne Jansen goto fail_qgroup; 3254d7ce5843SMiao Xie } 32557c2ca468SChris Mason } 32561a40e23bSZheng Yan 32573de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32583de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3259cb517eabSMiao Xie location.offset = 0; 32603de4586cSChris Mason 32613619c94fSJosef Bacik fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true); 32623140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32633140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3264f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3265315bf8efSJosef Bacik fs_info->fs_root = NULL; 3266bcef60f2SArne Jansen goto fail_qgroup; 32673140c9a3SDan Carpenter } 3268c289811cSChris Mason 3269bc98a42cSDavid Howells if (sb_rdonly(sb)) 32702b6ba629SIlya Dryomov return 0; 32712b6ba629SIlya Dryomov 3272f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3273f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32746675df31SOmar Sandoval clear_free_space_tree = 1; 32756675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32766675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32776675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 32786675df31SOmar Sandoval clear_free_space_tree = 1; 32796675df31SOmar Sandoval } 32806675df31SOmar Sandoval 32816675df31SOmar Sandoval if (clear_free_space_tree) { 3282f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3283f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3284f8d468a1SOmar Sandoval if (ret) { 3285f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3286f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 32876bccf3abSJeff Mahoney close_ctree(fs_info); 3288f8d468a1SOmar Sandoval return ret; 3289f8d468a1SOmar Sandoval } 3290f8d468a1SOmar Sandoval } 3291f8d468a1SOmar Sandoval 32920b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3293511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 329405135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3295511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3296511711afSChris Mason if (ret) { 329705135f59SDavid Sterba btrfs_warn(fs_info, 329805135f59SDavid Sterba "failed to create free space tree: %d", ret); 32996bccf3abSJeff Mahoney close_ctree(fs_info); 3300511711afSChris Mason return ret; 3301511711afSChris Mason } 3302511711afSChris Mason } 3303511711afSChris Mason 3304e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 33052b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33062b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33072b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 33086bccf3abSJeff Mahoney close_ctree(fs_info); 33092b6ba629SIlya Dryomov return ret; 33102b6ba629SIlya Dryomov } 3311e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 331259641015SIlya Dryomov 33132b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 33142b6ba629SIlya Dryomov if (ret) { 331505135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 33166bccf3abSJeff Mahoney close_ctree(fs_info); 33172b6ba629SIlya Dryomov return ret; 3318e3acc2a6SJosef Bacik } 3319e3acc2a6SJosef Bacik 33208dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 33218dabb742SStefan Behrens if (ret) { 332205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33236bccf3abSJeff Mahoney close_ctree(fs_info); 33248dabb742SStefan Behrens return ret; 33258dabb742SStefan Behrens } 33268dabb742SStefan Behrens 3327b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3328b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3329b382a324SJan Schmidt 33304bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 333105135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3332f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3333f7a81ea4SStefan Behrens if (ret) { 333405135f59SDavid Sterba btrfs_warn(fs_info, 333505135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33366bccf3abSJeff Mahoney close_ctree(fs_info); 3337f7a81ea4SStefan Behrens return ret; 3338f7a81ea4SStefan Behrens } 33390b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33404bbcaa64SEric Sandeen fs_info->generation != 33414bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 334205135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 334370f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 334470f80175SStefan Behrens if (ret) { 334505135f59SDavid Sterba btrfs_warn(fs_info, 334605135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 33476bccf3abSJeff Mahoney close_ctree(fs_info); 334870f80175SStefan Behrens return ret; 334970f80175SStefan Behrens } 3350f7a81ea4SStefan Behrens } 3351afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 335247ab2a6cSJosef Bacik 33538dcddfa0SQu Wenruo /* 33548dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33558dcddfa0SQu Wenruo * no need to keep the flag 33568dcddfa0SQu Wenruo */ 33578dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33588dcddfa0SQu Wenruo 3359ad2b2c80SAl Viro return 0; 336039279cc3SChris Mason 3361bcef60f2SArne Jansen fail_qgroup: 3362bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33637c2ca468SChris Mason fail_trans_kthread: 33647c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33652ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3366faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33673f157a2fSChris Mason fail_cleaner: 3368a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33697c2ca468SChris Mason 33707c2ca468SChris Mason /* 33717c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 33727c2ca468SChris Mason * kthreads 33737c2ca468SChris Mason */ 33747c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 33757c2ca468SChris Mason 33762365dd3cSAnand Jain fail_sysfs: 33776618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 33782365dd3cSAnand Jain 3379b7c35e81SAnand Jain fail_fsdev_sysfs: 3380b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3381b7c35e81SAnand Jain 33821b1d1f66SJosef Bacik fail_block_groups: 338354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3384af31f5e5SChris Mason 3385af31f5e5SChris Mason fail_tree_roots: 33864273eaffSAnand Jain free_root_pointers(fs_info, true); 33872b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3388af31f5e5SChris Mason 338939279cc3SChris Mason fail_sb_buffer: 33907abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 33915cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 339216cdcec7SMiao Xie fail_alloc: 3393586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3394586e46e2SIlya Dryomov 33954543df7eSChris Mason iput(fs_info->btree_inode); 339676dda93cSYan, Zheng fail_srcu: 339776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 33987e662854SQinghuang Feng fail: 3399586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3400ad2b2c80SAl Viro return err; 3401eb60ceacSChris Mason } 3402663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3403eb60ceacSChris Mason 3404314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3405f2984462SChris Mason { 3406314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3407314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3408314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3409314b6dd0SJohannes Thumshirn struct page *page; 3410442a4f63SStefan Behrens 3411314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3412314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3413314b6dd0SJohannes Thumshirn 3414314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3415fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3416314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3417314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3418314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3419314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3420314b6dd0SJohannes Thumshirn SetPageError(page); 3421314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3422314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3423314b6dd0SJohannes Thumshirn } else { 3424314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3425f2984462SChris Mason } 3426314b6dd0SJohannes Thumshirn 3427314b6dd0SJohannes Thumshirn put_page(page); 3428314b6dd0SJohannes Thumshirn unlock_page(page); 3429314b6dd0SJohannes Thumshirn } 3430314b6dd0SJohannes Thumshirn 3431314b6dd0SJohannes Thumshirn bio_put(bio); 3432f2984462SChris Mason } 3433f2984462SChris Mason 34348f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 34358f32380dSJohannes Thumshirn int copy_num) 343629c36d72SAnand Jain { 343729c36d72SAnand Jain struct btrfs_super_block *super; 34388f32380dSJohannes Thumshirn struct page *page; 343929c36d72SAnand Jain u64 bytenr; 34408f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 344129c36d72SAnand Jain 344229c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 344329c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 34448f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 344529c36d72SAnand Jain 34468f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 34478f32380dSJohannes Thumshirn if (IS_ERR(page)) 34488f32380dSJohannes Thumshirn return ERR_CAST(page); 344929c36d72SAnand Jain 34508f32380dSJohannes Thumshirn super = page_address(page); 345129c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 345229c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 34538f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 34548f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 345529c36d72SAnand Jain } 345629c36d72SAnand Jain 34578f32380dSJohannes Thumshirn return super; 345829c36d72SAnand Jain } 345929c36d72SAnand Jain 346029c36d72SAnand Jain 34618f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3462a512bbf8SYan Zheng { 34638f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3464a512bbf8SYan Zheng int i; 3465a512bbf8SYan Zheng u64 transid = 0; 3466a512bbf8SYan Zheng 3467a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3468a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3469a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3470a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3471a512bbf8SYan Zheng */ 3472a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 34738f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 34748f32380dSJohannes Thumshirn if (IS_ERR(super)) 3475a512bbf8SYan Zheng continue; 3476a512bbf8SYan Zheng 3477a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 34788f32380dSJohannes Thumshirn if (latest) 34798f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 34808f32380dSJohannes Thumshirn 34818f32380dSJohannes Thumshirn latest = super; 3482a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3483a512bbf8SYan Zheng } 3484a512bbf8SYan Zheng } 348592fc03fbSAnand Jain 34868f32380dSJohannes Thumshirn return super; 3487a512bbf8SYan Zheng } 3488a512bbf8SYan Zheng 34894eedeb75SHisashi Hifumi /* 3490abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3491314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 34924eedeb75SHisashi Hifumi * 3493abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3494abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3495abbb3b8eSDavid Sterba * same for write and wait phases. 34964eedeb75SHisashi Hifumi * 3497314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 34984eedeb75SHisashi Hifumi */ 3499a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3500abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3501a512bbf8SYan Zheng { 3502d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3503314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3504d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3505a512bbf8SYan Zheng int i; 3506a512bbf8SYan Zheng int errors = 0; 3507a512bbf8SYan Zheng u64 bytenr; 3508a512bbf8SYan Zheng 3509a512bbf8SYan Zheng if (max_mirrors == 0) 3510a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3511a512bbf8SYan Zheng 3512d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3513d5178578SJohannes Thumshirn 3514a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3515314b6dd0SJohannes Thumshirn struct page *page; 3516314b6dd0SJohannes Thumshirn struct bio *bio; 3517314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3518314b6dd0SJohannes Thumshirn 3519a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3520935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3521935e5cc9SMiao Xie device->commit_total_bytes) 3522a512bbf8SYan Zheng break; 3523a512bbf8SYan Zheng 3524a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3525a512bbf8SYan Zheng 3526d5178578SJohannes Thumshirn crypto_shash_init(shash); 3527d5178578SJohannes Thumshirn crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3528abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3529d5178578SJohannes Thumshirn crypto_shash_final(shash, sb->csum); 3530a512bbf8SYan Zheng 3531314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3532314b6dd0SJohannes Thumshirn GFP_NOFS); 3533314b6dd0SJohannes Thumshirn if (!page) { 3534fb456252SJeff Mahoney btrfs_err(device->fs_info, 3535314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3536f14d104dSDavid Sterba bytenr); 3537634554dcSJosef Bacik errors++; 3538634554dcSJosef Bacik continue; 3539634554dcSJosef Bacik } 3540634554dcSJosef Bacik 3541314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3542314b6dd0SJohannes Thumshirn get_page(page); 3543a512bbf8SYan Zheng 3544314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3545314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3546a512bbf8SYan Zheng 3547387125fcSChris Mason /* 3548314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3549314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3550314b6dd0SJohannes Thumshirn * checking. 3551387125fcSChris Mason */ 3552314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3553314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3554314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3555314b6dd0SJohannes Thumshirn bio->bi_private = device; 3556314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3557314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3558314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3559314b6dd0SJohannes Thumshirn 3560314b6dd0SJohannes Thumshirn /* 3561314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3562314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3563314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3564314b6dd0SJohannes Thumshirn */ 3565314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 35661b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3567314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3568314b6dd0SJohannes Thumshirn 3569314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 3570a512bbf8SYan Zheng } 3571a512bbf8SYan Zheng return errors < i ? 0 : -1; 3572a512bbf8SYan Zheng } 3573a512bbf8SYan Zheng 3574387125fcSChris Mason /* 3575abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3576abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3577abbb3b8eSDavid Sterba * 3578314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3579abbb3b8eSDavid Sterba * date. 3580abbb3b8eSDavid Sterba */ 3581abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3582abbb3b8eSDavid Sterba { 3583abbb3b8eSDavid Sterba int i; 3584abbb3b8eSDavid Sterba int errors = 0; 3585b6a535faSHoward McLauchlan bool primary_failed = false; 3586abbb3b8eSDavid Sterba u64 bytenr; 3587abbb3b8eSDavid Sterba 3588abbb3b8eSDavid Sterba if (max_mirrors == 0) 3589abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3590abbb3b8eSDavid Sterba 3591abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3592314b6dd0SJohannes Thumshirn struct page *page; 3593314b6dd0SJohannes Thumshirn 3594abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3595abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3596abbb3b8eSDavid Sterba device->commit_total_bytes) 3597abbb3b8eSDavid Sterba break; 3598abbb3b8eSDavid Sterba 3599314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3600314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3601314b6dd0SJohannes Thumshirn if (!page) { 3602abbb3b8eSDavid Sterba errors++; 3603b6a535faSHoward McLauchlan if (i == 0) 3604b6a535faSHoward McLauchlan primary_failed = true; 3605abbb3b8eSDavid Sterba continue; 3606abbb3b8eSDavid Sterba } 3607314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3608314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3609314b6dd0SJohannes Thumshirn if (PageError(page)) { 3610abbb3b8eSDavid Sterba errors++; 3611b6a535faSHoward McLauchlan if (i == 0) 3612b6a535faSHoward McLauchlan primary_failed = true; 3613b6a535faSHoward McLauchlan } 3614abbb3b8eSDavid Sterba 3615314b6dd0SJohannes Thumshirn /* Drop our reference */ 3616314b6dd0SJohannes Thumshirn put_page(page); 3617abbb3b8eSDavid Sterba 3618314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3619314b6dd0SJohannes Thumshirn put_page(page); 3620abbb3b8eSDavid Sterba } 3621abbb3b8eSDavid Sterba 3622b6a535faSHoward McLauchlan /* log error, force error return */ 3623b6a535faSHoward McLauchlan if (primary_failed) { 3624b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3625b6a535faSHoward McLauchlan device->devid); 3626b6a535faSHoward McLauchlan return -1; 3627b6a535faSHoward McLauchlan } 3628b6a535faSHoward McLauchlan 3629abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3630abbb3b8eSDavid Sterba } 3631abbb3b8eSDavid Sterba 3632abbb3b8eSDavid Sterba /* 3633387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3634387125fcSChris Mason * for the barrier when it is done 3635387125fcSChris Mason */ 36364246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3637387125fcSChris Mason { 3638387125fcSChris Mason complete(bio->bi_private); 3639387125fcSChris Mason } 3640387125fcSChris Mason 3641387125fcSChris Mason /* 36424fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36434fc6441aSAnand Jain * done in the waiting counterpart. 3644387125fcSChris Mason */ 36454fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3646387125fcSChris Mason { 3647c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3648e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3649387125fcSChris Mason 3650c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36514fc6441aSAnand Jain return; 3652387125fcSChris Mason 3653e0ae9994SDavid Sterba bio_reset(bio); 3654387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 365574d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36568d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3657387125fcSChris Mason init_completion(&device->flush_wait); 3658387125fcSChris Mason bio->bi_private = &device->flush_wait; 3659387125fcSChris Mason 366043a01111SLu Fengqi btrfsic_submit_bio(bio); 36611c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36624fc6441aSAnand Jain } 3663387125fcSChris Mason 36644fc6441aSAnand Jain /* 36654fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36664fc6441aSAnand Jain */ 36678c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36684fc6441aSAnand Jain { 36694fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36704fc6441aSAnand Jain 36711c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 367258efbc9fSOmar Sandoval return BLK_STS_OK; 36734fc6441aSAnand Jain 36741c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36752980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 36764fc6441aSAnand Jain 36778c27cb35SLinus Torvalds return bio->bi_status; 3678387125fcSChris Mason } 3679387125fcSChris Mason 3680d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3681401b41e5SAnand Jain { 36826528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3683401b41e5SAnand Jain return -EIO; 3684387125fcSChris Mason return 0; 3685387125fcSChris Mason } 3686387125fcSChris Mason 3687387125fcSChris Mason /* 3688387125fcSChris Mason * send an empty flush down to each device in parallel, 3689387125fcSChris Mason * then wait for them 3690387125fcSChris Mason */ 3691387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3692387125fcSChris Mason { 3693387125fcSChris Mason struct list_head *head; 3694387125fcSChris Mason struct btrfs_device *dev; 36955af3e8ccSStefan Behrens int errors_wait = 0; 36964e4cbee9SChristoph Hellwig blk_status_t ret; 3697387125fcSChris Mason 36981538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3699387125fcSChris Mason /* send down all the barriers */ 3700387125fcSChris Mason head = &info->fs_devices->devices; 37011538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3702e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3703f88ba6a2SHidetoshi Seto continue; 3704cea7c8bfSAnand Jain if (!dev->bdev) 3705387125fcSChris Mason continue; 3706e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3707ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3708387125fcSChris Mason continue; 3709387125fcSChris Mason 37104fc6441aSAnand Jain write_dev_flush(dev); 371158efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3712387125fcSChris Mason } 3713387125fcSChris Mason 3714387125fcSChris Mason /* wait for all the barriers */ 37151538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3716e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3717f88ba6a2SHidetoshi Seto continue; 3718387125fcSChris Mason if (!dev->bdev) { 37195af3e8ccSStefan Behrens errors_wait++; 3720387125fcSChris Mason continue; 3721387125fcSChris Mason } 3722e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3723ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3724387125fcSChris Mason continue; 3725387125fcSChris Mason 37264fc6441aSAnand Jain ret = wait_dev_flush(dev); 3727401b41e5SAnand Jain if (ret) { 3728401b41e5SAnand Jain dev->last_flush_error = ret; 372966b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 373066b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 37315af3e8ccSStefan Behrens errors_wait++; 3732387125fcSChris Mason } 3733401b41e5SAnand Jain } 3734401b41e5SAnand Jain 3735cea7c8bfSAnand Jain if (errors_wait) { 3736401b41e5SAnand Jain /* 3737401b41e5SAnand Jain * At some point we need the status of all disks 3738401b41e5SAnand Jain * to arrive at the volume status. So error checking 3739401b41e5SAnand Jain * is being pushed to a separate loop. 3740401b41e5SAnand Jain */ 3741d10b82feSQu Wenruo return check_barrier_error(info); 3742401b41e5SAnand Jain } 3743387125fcSChris Mason return 0; 3744387125fcSChris Mason } 3745387125fcSChris Mason 3746943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3747943c6e99SZhao Lei { 37488789f4feSZhao Lei int raid_type; 37498789f4feSZhao Lei int min_tolerated = INT_MAX; 3750943c6e99SZhao Lei 37518789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37528789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37538c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37548789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37558789f4feSZhao Lei tolerated_failures); 3756943c6e99SZhao Lei 37578789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37588789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37598789f4feSZhao Lei continue; 376041a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37618789f4feSZhao Lei continue; 37628c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37638789f4feSZhao Lei btrfs_raid_array[raid_type]. 37648789f4feSZhao Lei tolerated_failures); 37658789f4feSZhao Lei } 3766943c6e99SZhao Lei 37678789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3768ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37698789f4feSZhao Lei min_tolerated = 0; 37708789f4feSZhao Lei } 37718789f4feSZhao Lei 37728789f4feSZhao Lei return min_tolerated; 3773943c6e99SZhao Lei } 3774943c6e99SZhao Lei 3775eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3776f2984462SChris Mason { 3777e5e9a520SChris Mason struct list_head *head; 3778f2984462SChris Mason struct btrfs_device *dev; 3779a061fc8dSChris Mason struct btrfs_super_block *sb; 3780f2984462SChris Mason struct btrfs_dev_item *dev_item; 3781f2984462SChris Mason int ret; 3782f2984462SChris Mason int do_barriers; 3783a236aed1SChris Mason int max_errors; 3784a236aed1SChris Mason int total_errors = 0; 3785a061fc8dSChris Mason u64 flags; 3786f2984462SChris Mason 37870b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3788fed3b381SLiu Bo 3789fed3b381SLiu Bo /* 3790fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3791fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3792fed3b381SLiu Bo * been consistent on disk. 3793fed3b381SLiu Bo */ 3794fed3b381SLiu Bo if (max_mirrors == 0) 37950b246afaSJeff Mahoney backup_super_roots(fs_info); 3796f2984462SChris Mason 37970b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3798a061fc8dSChris Mason dev_item = &sb->dev_item; 3799e5e9a520SChris Mason 38000b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 38010b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 38020b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3803387125fcSChris Mason 38045af3e8ccSStefan Behrens if (do_barriers) { 38050b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 38065af3e8ccSStefan Behrens if (ret) { 38075af3e8ccSStefan Behrens mutex_unlock( 38080b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38090b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38105af3e8ccSStefan Behrens "errors while submitting device barriers."); 38115af3e8ccSStefan Behrens return ret; 38125af3e8ccSStefan Behrens } 38135af3e8ccSStefan Behrens } 3814387125fcSChris Mason 38151538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3816dfe25020SChris Mason if (!dev->bdev) { 3817dfe25020SChris Mason total_errors++; 3818dfe25020SChris Mason continue; 3819dfe25020SChris Mason } 3820e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3821ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3822dfe25020SChris Mason continue; 3823dfe25020SChris Mason 38242b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3825a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3826a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 38277df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3828935e5cc9SMiao Xie dev->commit_total_bytes); 3829ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3830ce7213c7SMiao Xie dev->commit_bytes_used); 3831a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3832a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3833a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3834a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 38357239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38367239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3837a512bbf8SYan Zheng 3838a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3839a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3840f2984462SChris Mason 384175cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 384275cb857dSQu Wenruo if (ret < 0) { 384375cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 384475cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 384575cb857dSQu Wenruo "unexpected superblock corruption detected"); 384675cb857dSQu Wenruo return -EUCLEAN; 384775cb857dSQu Wenruo } 384875cb857dSQu Wenruo 3849abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3850a236aed1SChris Mason if (ret) 3851a236aed1SChris Mason total_errors++; 3852f2984462SChris Mason } 3853a236aed1SChris Mason if (total_errors > max_errors) { 38540b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3855d397712bSChris Mason total_errors); 38560b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 385779787eaaSJeff Mahoney 38589d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38590b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38600b246afaSJeff Mahoney "%d errors while writing supers", 38610b246afaSJeff Mahoney total_errors); 38629d565ba4SStefan Behrens return -EIO; 3863a236aed1SChris Mason } 3864f2984462SChris Mason 3865a512bbf8SYan Zheng total_errors = 0; 38661538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3867dfe25020SChris Mason if (!dev->bdev) 3868dfe25020SChris Mason continue; 3869e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3870ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3871dfe25020SChris Mason continue; 3872dfe25020SChris Mason 3873abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3874a512bbf8SYan Zheng if (ret) 38751259ab75SChris Mason total_errors++; 3876f2984462SChris Mason } 38770b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3878a236aed1SChris Mason if (total_errors > max_errors) { 38790b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38800b246afaSJeff Mahoney "%d errors while writing supers", 38810b246afaSJeff Mahoney total_errors); 388279787eaaSJeff Mahoney return -EIO; 3883a236aed1SChris Mason } 3884f2984462SChris Mason return 0; 3885f2984462SChris Mason } 3886f2984462SChris Mason 3887cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3888cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3889cb517eabSMiao Xie struct btrfs_root *root) 38902619ba1fSChris Mason { 38914df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 38922619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 38932619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3894af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 389500246528SJosef Bacik btrfs_put_root(root); 38964df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 389776dda93cSYan, Zheng 389876dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 389976dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 390076dda93cSYan, Zheng 39011c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 39023321719eSLiu Bo btrfs_free_log(NULL, root); 39031c1ea4f7SLiu Bo if (root->reloc_root) { 39041c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 39051c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 390600246528SJosef Bacik btrfs_put_root(root->reloc_root); 39071c1ea4f7SLiu Bo root->reloc_root = NULL; 39081c1ea4f7SLiu Bo } 39091c1ea4f7SLiu Bo } 39103321719eSLiu Bo 3911faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3912581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3913faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3914581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 391584db5ccfSDavid Sterba btrfs_free_fs_root(root); 39164df27c4dSYan, Zheng } 39174df27c4dSYan, Zheng 391884db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 39194df27c4dSYan, Zheng { 392057cdc8dbSDavid Sterba iput(root->ino_cache_inode); 39214df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 39220ee5dc67SAl Viro if (root->anon_dev) 39230ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 3924dcc3eb96SNikolay Borisov btrfs_drew_lock_destroy(&root->snapshot_lock); 39255f39d397SChris Mason free_extent_buffer(root->node); 39265f39d397SChris Mason free_extent_buffer(root->commit_root); 3927581bb050SLi Zefan kfree(root->free_ino_ctl); 3928581bb050SLi Zefan kfree(root->free_ino_pinned); 392900246528SJosef Bacik btrfs_put_root(root); 39302619ba1fSChris Mason } 39312619ba1fSChris Mason 3932c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3933c146afadSYan Zheng { 3934c146afadSYan Zheng u64 root_objectid = 0; 3935c146afadSYan Zheng struct btrfs_root *gang[8]; 393665d33fd7SQu Wenruo int i = 0; 393765d33fd7SQu Wenruo int err = 0; 393865d33fd7SQu Wenruo unsigned int ret = 0; 393965d33fd7SQu Wenruo int index; 3940c146afadSYan Zheng 3941c146afadSYan Zheng while (1) { 394265d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3943c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3944c146afadSYan Zheng (void **)gang, root_objectid, 3945c146afadSYan Zheng ARRAY_SIZE(gang)); 394665d33fd7SQu Wenruo if (!ret) { 394765d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3948c146afadSYan Zheng break; 394965d33fd7SQu Wenruo } 39505d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 395166b4ffd1SJosef Bacik 395265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 395365d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 395465d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 395565d33fd7SQu Wenruo gang[i] = NULL; 395665d33fd7SQu Wenruo continue; 395765d33fd7SQu Wenruo } 395865d33fd7SQu Wenruo /* grab all the search result for later use */ 395900246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 396065d33fd7SQu Wenruo } 396165d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 396265d33fd7SQu Wenruo 396365d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 396465d33fd7SQu Wenruo if (!gang[i]) 396565d33fd7SQu Wenruo continue; 3966c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 396766b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 396866b4ffd1SJosef Bacik if (err) 396965d33fd7SQu Wenruo break; 397000246528SJosef Bacik btrfs_put_root(gang[i]); 3971c146afadSYan Zheng } 3972c146afadSYan Zheng root_objectid++; 3973c146afadSYan Zheng } 397465d33fd7SQu Wenruo 397565d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 397665d33fd7SQu Wenruo for (; i < ret; i++) { 397765d33fd7SQu Wenruo if (gang[i]) 397800246528SJosef Bacik btrfs_put_root(gang[i]); 397965d33fd7SQu Wenruo } 398065d33fd7SQu Wenruo return err; 3981c146afadSYan Zheng } 3982c146afadSYan Zheng 39836bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3984c146afadSYan Zheng { 39856bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3986c146afadSYan Zheng struct btrfs_trans_handle *trans; 3987c146afadSYan Zheng 39880b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39892ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39900b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39910b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3992c71bf099SYan, Zheng 3993c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39940b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39950b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3996c71bf099SYan, Zheng 39977a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39983612b495STsutomu Itoh if (IS_ERR(trans)) 39993612b495STsutomu Itoh return PTR_ERR(trans); 40003a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4001c146afadSYan Zheng } 4002c146afadSYan Zheng 4003b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4004eb60ceacSChris Mason { 4005c146afadSYan Zheng int ret; 4006e089f05cSChris Mason 4007afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4008d6fd0ae2SOmar Sandoval /* 4009d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4010d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4011d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4012d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4013d6fd0ae2SOmar Sandoval */ 4014d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4015a2135011SChris Mason 40167343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4017d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40187343dd61SJustin Maggard 4019803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4020803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4021803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4022803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4023803b2f54SStefan Behrens 4024837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4025aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4026837d5b6eSIlya Dryomov 40278dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40288dabb742SStefan Behrens 4029aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 40304cb5300bSChris Mason 40314cb5300bSChris Mason /* wait for any defraggers to finish */ 40324cb5300bSChris Mason wait_event(fs_info->transaction_wait, 40334cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 40344cb5300bSChris Mason 40354cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 403626176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40374cb5300bSChris Mason 403821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 403921c7e756SMiao Xie 4040b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4041b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4042b0643e59SDennis Zhou 4043bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4044e44163e1SJeff Mahoney /* 4045d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4046d6fd0ae2SOmar Sandoval * unused block groups. 4047e44163e1SJeff Mahoney */ 40480b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4049e44163e1SJeff Mahoney 4050f0cc2cd7SFilipe Manana /* 4051f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4052f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4053f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4054f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4055f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4056f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4057f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4058f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4059f0cc2cd7SFilipe Manana * This is a very rare case. 4060f0cc2cd7SFilipe Manana */ 4061f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4062f0cc2cd7SFilipe Manana 40636bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4064d397712bSChris Mason if (ret) 406504892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4066c146afadSYan Zheng } 4067ed2ff2cbSChris Mason 4068af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4069af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40702ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4071acce952bSliubo 4072e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4073e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40748929ecfaSYan, Zheng 4075e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4076afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4077f25784b3SYan Zheng 407804892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4079fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4080bcef60f2SArne Jansen 4081963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 408204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4083963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4084b0c68f8bSChris Mason } 408531153d81SYan Zheng 40864297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 40874297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 40884297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 40894297ff84SJosef Bacik 40906618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4091b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 40925ac1d209SJeff Mahoney 4093faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4094d10c5f31SChris Mason 40951a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 40961a4319ccSLiu Bo 4097de348ee0SWang Shilong /* 4098de348ee0SWang Shilong * we must make sure there is not any read request to 4099de348ee0SWang Shilong * submit after we stopping all workers. 4100de348ee0SWang Shilong */ 4101de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 410296192499SJosef Bacik btrfs_stop_all_workers(fs_info); 410396192499SJosef Bacik 4104afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 41054273eaffSAnand Jain free_root_pointers(fs_info, true); 41069ad6b7bcSChris Mason 41074e19443dSJosef Bacik /* 41084e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 41094e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 41104e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 41114e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 41124e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 41134e19443dSJosef Bacik */ 41144e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 41154e19443dSJosef Bacik 411613e6c37bSJosef Bacik iput(fs_info->btree_inode); 4117d6bfde87SChris Mason 411821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 41190b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 41202ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 412121adbd5cSStefan Behrens #endif 412221adbd5cSStefan Behrens 41230b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 412468c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 412576dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 4126eb60ceacSChris Mason } 4127eb60ceacSChris Mason 4128b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4129b9fab919SChris Mason int atomic) 4130ccd467d6SChris Mason { 41311259ab75SChris Mason int ret; 4132727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 41331259ab75SChris Mason 41340b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 41351259ab75SChris Mason if (!ret) 41361259ab75SChris Mason return ret; 41371259ab75SChris Mason 41381259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4139b9fab919SChris Mason parent_transid, atomic); 4140b9fab919SChris Mason if (ret == -EAGAIN) 4141b9fab919SChris Mason return ret; 41421259ab75SChris Mason return !ret; 41435f39d397SChris Mason } 41446702ed49SChris Mason 41455f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 41465f39d397SChris Mason { 41470b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 414806ea65a3SJosef Bacik struct btrfs_root *root; 41495f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4150b9473439SChris Mason int was_dirty; 4151b4ce94deSChris Mason 415206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 415306ea65a3SJosef Bacik /* 415406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 415552042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 415606ea65a3SJosef Bacik * outside of the sanity tests. 415706ea65a3SJosef Bacik */ 4158b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 415906ea65a3SJosef Bacik return; 416006ea65a3SJosef Bacik #endif 416106ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41620b246afaSJeff Mahoney fs_info = root->fs_info; 4163b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41640b246afaSJeff Mahoney if (transid != fs_info->generation) 41655d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41660b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41670b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4168e2d84521SMiao Xie if (!was_dirty) 4169104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4170e2d84521SMiao Xie buf->len, 41710b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41721f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 417369fc6cbbSQu Wenruo /* 417469fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 417569fc6cbbSQu Wenruo * but item data not updated. 417669fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 417769fc6cbbSQu Wenruo */ 417869fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4179cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4180a4f78750SDavid Sterba btrfs_print_leaf(buf); 41811f21ef0aSFilipe Manana ASSERT(0); 41821f21ef0aSFilipe Manana } 41831f21ef0aSFilipe Manana #endif 4184eb60ceacSChris Mason } 4185eb60ceacSChris Mason 41862ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4187b53d3f5dSLiu Bo int flush_delayed) 418835b7e476SChris Mason { 4189188de649SChris Mason /* 4190188de649SChris Mason * looks as though older kernels can get into trouble with 4191188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4192188de649SChris Mason */ 4193e2d84521SMiao Xie int ret; 4194d6bfde87SChris Mason 41956933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4196d6bfde87SChris Mason return; 4197d6bfde87SChris Mason 4198b53d3f5dSLiu Bo if (flush_delayed) 41992ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 420016cdcec7SMiao Xie 4201d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4202d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4203d814a491SEthan Lien fs_info->dirty_metadata_batch); 4204e2d84521SMiao Xie if (ret > 0) { 42050b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 420616cdcec7SMiao Xie } 420716cdcec7SMiao Xie } 420816cdcec7SMiao Xie 42092ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 421016cdcec7SMiao Xie { 42112ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 421235b7e476SChris Mason } 4213b53d3f5dSLiu Bo 42142ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4215b53d3f5dSLiu Bo { 42162ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4217d6bfde87SChris Mason } 42186b80053dSChris Mason 4219581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4220581c1760SQu Wenruo struct btrfs_key *first_key) 42216b80053dSChris Mason { 42225ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4223581c1760SQu Wenruo level, first_key); 42246b80053dSChris Mason } 42250da5468fSChris Mason 42262ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4227acce952bSliubo { 4228fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4229fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4230fe816d0fSNikolay Borisov 42310b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42322ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42330b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4234acce952bSliubo 42350b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42360b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4237acce952bSliubo } 4238acce952bSliubo 4239143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4240acce952bSliubo { 4241acce952bSliubo struct btrfs_ordered_extent *ordered; 4242acce952bSliubo 4243199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4244779880efSJosef Bacik /* 4245779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4246779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4247779880efSJosef Bacik */ 4248199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4249779880efSJosef Bacik root_extent_list) 4250779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4251199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4252199c2a9cSMiao Xie } 4253199c2a9cSMiao Xie 4254199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4255199c2a9cSMiao Xie { 4256199c2a9cSMiao Xie struct btrfs_root *root; 4257199c2a9cSMiao Xie struct list_head splice; 4258199c2a9cSMiao Xie 4259199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4260199c2a9cSMiao Xie 4261199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4262199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4263199c2a9cSMiao Xie while (!list_empty(&splice)) { 4264199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4265199c2a9cSMiao Xie ordered_root); 42661de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42671de2cfdeSJosef Bacik &fs_info->ordered_roots); 4268199c2a9cSMiao Xie 42692a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4270199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4271199c2a9cSMiao Xie 42722a85d9caSLiu Bo cond_resched(); 42732a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4274199c2a9cSMiao Xie } 4275199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 427674d5d229SJosef Bacik 427774d5d229SJosef Bacik /* 427874d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 427974d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 428074d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 428174d5d229SJosef Bacik * actually get run and error out properly. 428274d5d229SJosef Bacik */ 428374d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4284acce952bSliubo } 4285acce952bSliubo 428635a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 42872ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4288acce952bSliubo { 4289acce952bSliubo struct rb_node *node; 4290acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4291acce952bSliubo struct btrfs_delayed_ref_node *ref; 4292acce952bSliubo int ret = 0; 4293acce952bSliubo 4294acce952bSliubo delayed_refs = &trans->delayed_refs; 4295acce952bSliubo 4296acce952bSliubo spin_lock(&delayed_refs->lock); 4297d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4298cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4299b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4300acce952bSliubo return ret; 4301acce952bSliubo } 4302acce952bSliubo 43035c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4304d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 43050e0adbcfSJosef Bacik struct rb_node *n; 4306e78417d1SJosef Bacik bool pin_bytes = false; 4307acce952bSliubo 4308d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4309d7df2c79SJosef Bacik href_node); 43103069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4311b939d1abSJosef Bacik continue; 43123069bd26SJosef Bacik 4313d7df2c79SJosef Bacik spin_lock(&head->lock); 4314e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 43150e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 43160e0adbcfSJosef Bacik ref_node); 4317d7df2c79SJosef Bacik ref->in_tree = 0; 4318e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 43190e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 43201d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43211d57ee94SWang Xiaoguang list_del(&ref->add_list); 4322d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4323d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4324d7df2c79SJosef Bacik } 432554067ae9SJosef Bacik if (head->must_insert_reserved) 4326e78417d1SJosef Bacik pin_bytes = true; 432778a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4328fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4329d7df2c79SJosef Bacik spin_unlock(&head->lock); 4330acce952bSliubo spin_unlock(&delayed_refs->lock); 4331e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4332acce952bSliubo 4333f603bb94SNikolay Borisov if (pin_bytes) { 4334f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4335f603bb94SNikolay Borisov 4336f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4337f603bb94SNikolay Borisov BUG_ON(!cache); 4338f603bb94SNikolay Borisov 4339f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4340f603bb94SNikolay Borisov spin_lock(&cache->lock); 4341f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4342f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4343f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4344f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4345f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4346f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4347f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4348f603bb94SNikolay Borisov percpu_counter_add_batch( 4349f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4350f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4351f603bb94SNikolay Borisov 4352f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4353f603bb94SNikolay Borisov 4354f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4355f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4356f603bb94SNikolay Borisov } 435731890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4358d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4359acce952bSliubo cond_resched(); 4360acce952bSliubo spin_lock(&delayed_refs->lock); 4361acce952bSliubo } 436281f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4363acce952bSliubo 4364acce952bSliubo spin_unlock(&delayed_refs->lock); 4365acce952bSliubo 4366acce952bSliubo return ret; 4367acce952bSliubo } 4368acce952bSliubo 4369143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4370acce952bSliubo { 4371acce952bSliubo struct btrfs_inode *btrfs_inode; 4372acce952bSliubo struct list_head splice; 4373acce952bSliubo 4374acce952bSliubo INIT_LIST_HEAD(&splice); 4375acce952bSliubo 4376eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4377eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4378acce952bSliubo 4379acce952bSliubo while (!list_empty(&splice)) { 4380fe816d0fSNikolay Borisov struct inode *inode = NULL; 4381eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4382acce952bSliubo delalloc_inodes); 4383fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4384eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4385acce952bSliubo 4386fe816d0fSNikolay Borisov /* 4387fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4388fe816d0fSNikolay Borisov * meanwhile. 4389fe816d0fSNikolay Borisov */ 4390fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4391fe816d0fSNikolay Borisov if (inode) { 4392fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4393fe816d0fSNikolay Borisov iput(inode); 4394fe816d0fSNikolay Borisov } 4395eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4396acce952bSliubo } 4397eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4398eb73c1b7SMiao Xie } 4399eb73c1b7SMiao Xie 4400eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4401eb73c1b7SMiao Xie { 4402eb73c1b7SMiao Xie struct btrfs_root *root; 4403eb73c1b7SMiao Xie struct list_head splice; 4404eb73c1b7SMiao Xie 4405eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4406eb73c1b7SMiao Xie 4407eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4408eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4409eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4410eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4411eb73c1b7SMiao Xie delalloc_root); 441200246528SJosef Bacik root = btrfs_grab_root(root); 4413eb73c1b7SMiao Xie BUG_ON(!root); 4414eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4415eb73c1b7SMiao Xie 4416eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 441700246528SJosef Bacik btrfs_put_root(root); 4418eb73c1b7SMiao Xie 4419eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4420eb73c1b7SMiao Xie } 4421eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4422acce952bSliubo } 4423acce952bSliubo 44242ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4425acce952bSliubo struct extent_io_tree *dirty_pages, 4426acce952bSliubo int mark) 4427acce952bSliubo { 4428acce952bSliubo int ret; 4429acce952bSliubo struct extent_buffer *eb; 4430acce952bSliubo u64 start = 0; 4431acce952bSliubo u64 end; 4432acce952bSliubo 4433acce952bSliubo while (1) { 4434acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4435e6138876SJosef Bacik mark, NULL); 4436acce952bSliubo if (ret) 4437acce952bSliubo break; 4438acce952bSliubo 443991166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4440acce952bSliubo while (start <= end) { 44410b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44420b246afaSJeff Mahoney start += fs_info->nodesize; 4443fd8b2b61SJosef Bacik if (!eb) 4444acce952bSliubo continue; 4445fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4446acce952bSliubo 4447fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4448fd8b2b61SJosef Bacik &eb->bflags)) 4449fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4450fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4451acce952bSliubo } 4452acce952bSliubo } 4453acce952bSliubo 4454acce952bSliubo return ret; 4455acce952bSliubo } 4456acce952bSliubo 44572ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4458fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4459acce952bSliubo { 4460acce952bSliubo u64 start; 4461acce952bSliubo u64 end; 4462acce952bSliubo int ret; 4463acce952bSliubo 4464acce952bSliubo while (1) { 44650e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 44660e6ec385SFilipe Manana 4467fcd5e742SLu Fengqi /* 4468fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4469fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4470fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4471fcd5e742SLu Fengqi * the same extent range. 4472fcd5e742SLu Fengqi */ 4473fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4474acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44750e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4476fcd5e742SLu Fengqi if (ret) { 4477fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4478acce952bSliubo break; 4479fcd5e742SLu Fengqi } 4480acce952bSliubo 44810e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44820e6ec385SFilipe Manana free_extent_state(cached_state); 44832ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4484fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4485acce952bSliubo cond_resched(); 4486acce952bSliubo } 4487acce952bSliubo 4488acce952bSliubo return 0; 4489acce952bSliubo } 4490acce952bSliubo 449132da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4492c79a1751SLiu Bo { 4493c79a1751SLiu Bo struct inode *inode; 4494c79a1751SLiu Bo 4495c79a1751SLiu Bo inode = cache->io_ctl.inode; 4496c79a1751SLiu Bo if (inode) { 4497c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4498c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4499c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4500c79a1751SLiu Bo iput(inode); 4501c79a1751SLiu Bo } 4502c79a1751SLiu Bo btrfs_put_block_group(cache); 4503c79a1751SLiu Bo } 4504c79a1751SLiu Bo 4505c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45062ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4507c79a1751SLiu Bo { 450832da5386SDavid Sterba struct btrfs_block_group *cache; 4509c79a1751SLiu Bo 4510c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4511c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4512c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 451332da5386SDavid Sterba struct btrfs_block_group, 4514c79a1751SLiu Bo dirty_list); 4515c79a1751SLiu Bo 4516c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4517c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4518c79a1751SLiu Bo list_del_init(&cache->io_list); 4519c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4520c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4521c79a1751SLiu Bo } 4522c79a1751SLiu Bo 4523c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4524c79a1751SLiu Bo spin_lock(&cache->lock); 4525c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4526c79a1751SLiu Bo spin_unlock(&cache->lock); 4527c79a1751SLiu Bo 4528c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4529c79a1751SLiu Bo btrfs_put_block_group(cache); 4530ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4531c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4532c79a1751SLiu Bo } 4533c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4534c79a1751SLiu Bo 453545ae2c18SNikolay Borisov /* 453645ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 453745ae2c18SNikolay Borisov * to use it without any locking 453845ae2c18SNikolay Borisov */ 4539c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4540c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 454132da5386SDavid Sterba struct btrfs_block_group, 4542c79a1751SLiu Bo io_list); 4543c79a1751SLiu Bo 4544c79a1751SLiu Bo list_del_init(&cache->io_list); 4545c79a1751SLiu Bo spin_lock(&cache->lock); 4546c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4547c79a1751SLiu Bo spin_unlock(&cache->lock); 4548c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4549c79a1751SLiu Bo } 4550c79a1751SLiu Bo } 4551c79a1751SLiu Bo 455249b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45532ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 455449b25e05SJeff Mahoney { 4555bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4556bbbf7243SNikolay Borisov 45572ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4558c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4559c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4560c79a1751SLiu Bo 4561bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4562bbbf7243SNikolay Borisov post_commit_list) { 4563bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4564bbbf7243SNikolay Borisov } 4565bbbf7243SNikolay Borisov 45662ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 456749b25e05SJeff Mahoney 45684a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45690b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 457049b25e05SJeff Mahoney 45714a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45720b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 457349b25e05SJeff Mahoney 4574ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 457567cde344SMiao Xie 45762ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 457749b25e05SJeff Mahoney EXTENT_DIRTY); 4578fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 457949b25e05SJeff Mahoney 45804a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45814a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 458249b25e05SJeff Mahoney } 458349b25e05SJeff Mahoney 45842ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4585acce952bSliubo { 4586acce952bSliubo struct btrfs_transaction *t; 4587acce952bSliubo 45880b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4589acce952bSliubo 45900b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45910b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 45920b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4593724e2315SJosef Bacik struct btrfs_transaction, list); 4594724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 45959b64f57dSElena Reshetova refcount_inc(&t->use_count); 45960b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45972ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4598724e2315SJosef Bacik btrfs_put_transaction(t); 45990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4600724e2315SJosef Bacik continue; 4601724e2315SJosef Bacik } 46020b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4603724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46040b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4605724e2315SJosef Bacik /* 4606724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4607724e2315SJosef Bacik * handle open currently for this transaction. 4608724e2315SJosef Bacik */ 4609724e2315SJosef Bacik wait_event(t->writer_wait, 4610724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4611724e2315SJosef Bacik } else { 46120b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4613724e2315SJosef Bacik } 46142ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4615724e2315SJosef Bacik 46160b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46170b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46180b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4619724e2315SJosef Bacik list_del_init(&t->list); 46200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4621a4abeea4SJosef Bacik 4622724e2315SJosef Bacik btrfs_put_transaction(t); 46232ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46240b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4625724e2315SJosef Bacik } 46260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46270b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4628ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4629ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46300b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46310b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4632acce952bSliubo 4633acce952bSliubo return 0; 4634acce952bSliubo } 4635acce952bSliubo 4636e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 46374d53dddbSDavid Sterba /* mandatory callbacks */ 46380b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 46394d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 46400da5468fSChris Mason }; 4641