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> 10d397712bSChris Mason #include <linux/buffer_head.h> 11ce9adaa5SChris Mason #include <linux/workqueue.h> 12a74a4b97SChris Mason #include <linux/kthread.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 14784b4e29SChris Mason #include <linux/migrate.h> 157a36ddecSDavid Sterba #include <linux/ratelimit.h> 166463fe58SStefan Behrens #include <linux/uuid.h> 17803b2f54SStefan Behrens #include <linux/semaphore.h> 18540adea3SMasami Hiramatsu #include <linux/error-injection.h> 199678c543SNikolay Borisov #include <linux/crc32c.h> 20b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 217e75bf3fSDavid Sterba #include <asm/unaligned.h> 226d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 23eb60ceacSChris Mason #include "ctree.h" 24eb60ceacSChris Mason #include "disk-io.h" 25e089f05cSChris Mason #include "transaction.h" 260f7d52f4SChris Mason #include "btrfs_inode.h" 270b86a832SChris Mason #include "volumes.h" 28db94535dSChris Mason #include "print-tree.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31fa9c0d79SChris Mason #include "free-space-cache.h" 3270f6d82eSOmar Sandoval #include "free-space-tree.h" 33581bb050SLi Zefan #include "inode-map.h" 3421adbd5cSStefan Behrens #include "check-integrity.h" 35606686eeSJosef Bacik #include "rcu-string.h" 368dabb742SStefan Behrens #include "dev-replace.h" 3753b381b3SDavid Woodhouse #include "raid56.h" 385ac1d209SJeff Mahoney #include "sysfs.h" 39fcebe456SJosef Bacik #include "qgroup.h" 40ebb8765bSAnand Jain #include "compression.h" 41557ea5ddSQu Wenruo #include "tree-checker.h" 42fd708b81SJosef Bacik #include "ref-verify.h" 43aac0023cSJosef Bacik #include "block-group.h" 44b0643e59SDennis Zhou #include "discard.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. 2562996e1f8SJohannes Thumshirn * 2572996e1f8SJohannes Thumshirn * Returns error if the extent buffer cannot be mapped. 258d352ac68SChris Mason */ 2592996e1f8SJohannes Thumshirn static int csum_tree_block(struct extent_buffer *buf, u8 *result) 26019c00ddcSChris Mason { 261d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 262d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 26319c00ddcSChris Mason unsigned long len; 26419c00ddcSChris Mason unsigned long cur_len; 26519c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26619c00ddcSChris Mason char *kaddr; 26719c00ddcSChris Mason unsigned long map_start; 26819c00ddcSChris Mason unsigned long map_len; 26919c00ddcSChris Mason int err; 270d5178578SJohannes Thumshirn 271d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 272d5178578SJohannes Thumshirn crypto_shash_init(shash); 27319c00ddcSChris Mason 27419c00ddcSChris Mason len = buf->len - offset; 275d5178578SJohannes Thumshirn 27619c00ddcSChris Mason while (len > 0) { 277d2e174d5SJohannes Thumshirn /* 278d2e174d5SJohannes Thumshirn * Note: we don't need to check for the err == 1 case here, as 279d2e174d5SJohannes Thumshirn * with the given combination of 'start = BTRFS_CSUM_SIZE (32)' 280d2e174d5SJohannes Thumshirn * and 'min_len = 32' and the currently implemented mapping 281d2e174d5SJohannes Thumshirn * algorithm we cannot cross a page boundary. 282d2e174d5SJohannes Thumshirn */ 28319c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 284a6591715SChris Mason &kaddr, &map_start, &map_len); 285c53839fcSJohannes Thumshirn if (WARN_ON(err)) 2868bd98f0eSAlex Lyakas return err; 28719c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 288d5178578SJohannes Thumshirn crypto_shash_update(shash, kaddr + offset - map_start, cur_len); 28919c00ddcSChris Mason len -= cur_len; 29019c00ddcSChris Mason offset += cur_len; 29119c00ddcSChris Mason } 29271a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 293607d432dSJosef Bacik 294d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 29519c00ddcSChris Mason 29619c00ddcSChris Mason return 0; 29719c00ddcSChris Mason } 29819c00ddcSChris Mason 299d352ac68SChris Mason /* 300d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 301d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 302d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 303d352ac68SChris Mason * in the wrong place. 304d352ac68SChris Mason */ 3051259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 306b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 307b9fab919SChris Mason int atomic) 3081259ab75SChris Mason { 3092ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3101259ab75SChris Mason int ret; 3112755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3121259ab75SChris Mason 3131259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3141259ab75SChris Mason return 0; 3151259ab75SChris Mason 316b9fab919SChris Mason if (atomic) 317b9fab919SChris Mason return -EAGAIN; 318b9fab919SChris Mason 319a26e8c9fSJosef Bacik if (need_lock) { 320a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 321300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 322a26e8c9fSJosef Bacik } 323a26e8c9fSJosef Bacik 3242ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 325ff13db41SDavid Sterba &cached_state); 3260b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3271259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3281259ab75SChris Mason ret = 0; 3291259ab75SChris Mason goto out; 3301259ab75SChris Mason } 33194647322SDavid Sterba btrfs_err_rl(eb->fs_info, 33294647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 33394647322SDavid Sterba eb->start, 33429549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3351259ab75SChris Mason ret = 1; 336a26e8c9fSJosef Bacik 337a26e8c9fSJosef Bacik /* 338a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 339a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 34001327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 341a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 342a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 343a26e8c9fSJosef Bacik * making everybody very sad. 344a26e8c9fSJosef Bacik */ 345a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3460b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 34733958dc6SChris Mason out: 3482ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 349e43bbe5eSDavid Sterba &cached_state); 350472b909fSJosef Bacik if (need_lock) 351a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3521259ab75SChris Mason return ret; 3531259ab75SChris Mason } 3541259ab75SChris Mason 355e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 356e7e16f48SJohannes Thumshirn { 357e7e16f48SJohannes Thumshirn switch (csum_type) { 358e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 3593951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 3603831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 361352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 362e7e16f48SJohannes Thumshirn return true; 363e7e16f48SJohannes Thumshirn default: 364e7e16f48SJohannes Thumshirn return false; 365e7e16f48SJohannes Thumshirn } 366e7e16f48SJohannes Thumshirn } 367e7e16f48SJohannes Thumshirn 368d352ac68SChris Mason /* 3691104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3701104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3711104a885SDavid Sterba */ 372ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 373ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3741104a885SDavid Sterba { 3751104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3761104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 37751bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 378d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 379d5178578SJohannes Thumshirn 380d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 381d5178578SJohannes Thumshirn crypto_shash_init(shash); 3821104a885SDavid Sterba 3831104a885SDavid Sterba /* 3841104a885SDavid Sterba * The super_block structure does not span the whole 38551bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 38651bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3871104a885SDavid Sterba */ 388d5178578SJohannes Thumshirn crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 389d5178578SJohannes Thumshirn BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 390d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 3911104a885SDavid Sterba 39251bce6c9SJohannes Thumshirn if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb))) 393e7e16f48SJohannes Thumshirn return 1; 3941104a885SDavid Sterba 395e7e16f48SJohannes Thumshirn return 0; 3961104a885SDavid Sterba } 3971104a885SDavid Sterba 398e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 399ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 400581c1760SQu Wenruo { 401e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 402581c1760SQu Wenruo int found_level; 403581c1760SQu Wenruo struct btrfs_key found_key; 404581c1760SQu Wenruo int ret; 405581c1760SQu Wenruo 406581c1760SQu Wenruo found_level = btrfs_header_level(eb); 407581c1760SQu Wenruo if (found_level != level) { 40863489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 40963489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 410581c1760SQu Wenruo btrfs_err(fs_info, 411581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 412581c1760SQu Wenruo eb->start, level, found_level); 413581c1760SQu Wenruo return -EIO; 414581c1760SQu Wenruo } 415581c1760SQu Wenruo 416581c1760SQu Wenruo if (!first_key) 417581c1760SQu Wenruo return 0; 418581c1760SQu Wenruo 4195d41be6fSQu Wenruo /* 4205d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 4215d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 4225d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 4235d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 4245d41be6fSQu Wenruo */ 4255d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 4265d41be6fSQu Wenruo return 0; 42762fdaa52SQu Wenruo 42862fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 42962fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 43062fdaa52SQu Wenruo btrfs_err(fs_info, 43162fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 43262fdaa52SQu Wenruo eb->start); 43362fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 43462fdaa52SQu Wenruo return -EUCLEAN; 43562fdaa52SQu Wenruo } 43662fdaa52SQu Wenruo 437581c1760SQu Wenruo if (found_level) 438581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 439581c1760SQu Wenruo else 440581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 441581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 442581c1760SQu Wenruo 443581c1760SQu Wenruo if (ret) { 44463489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 44563489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 446581c1760SQu Wenruo btrfs_err(fs_info, 447ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 448ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 449ff76a864SLiu Bo first_key->type, first_key->offset, 450ff76a864SLiu Bo found_key.objectid, found_key.type, 451ff76a864SLiu Bo found_key.offset); 452581c1760SQu Wenruo } 453581c1760SQu Wenruo return ret; 454581c1760SQu Wenruo } 455581c1760SQu Wenruo 4561104a885SDavid Sterba /* 457d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 458d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 459581c1760SQu Wenruo * 460581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 461581c1760SQu Wenruo * @level: expected level, mandatory check 462581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 463d352ac68SChris Mason */ 4645ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 465581c1760SQu Wenruo u64 parent_transid, int level, 466581c1760SQu Wenruo struct btrfs_key *first_key) 467f188591eSChris Mason { 4685ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 469f188591eSChris Mason struct extent_io_tree *io_tree; 470ea466794SJosef Bacik int failed = 0; 471f188591eSChris Mason int ret; 472f188591eSChris Mason int num_copies = 0; 473f188591eSChris Mason int mirror_num = 0; 474ea466794SJosef Bacik int failed_mirror = 0; 475f188591eSChris Mason 4760b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 477f188591eSChris Mason while (1) { 478f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 479c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 480256dd1bbSStefan Behrens if (!ret) { 481581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 482b9fab919SChris Mason parent_transid, 0)) 483256dd1bbSStefan Behrens ret = -EIO; 484e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 485ff76a864SLiu Bo first_key, parent_transid)) 486581c1760SQu Wenruo ret = -EUCLEAN; 487581c1760SQu Wenruo else 488581c1760SQu Wenruo break; 489256dd1bbSStefan Behrens } 490d397712bSChris Mason 4910b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 492f188591eSChris Mason eb->start, eb->len); 4934235298eSChris Mason if (num_copies == 1) 494ea466794SJosef Bacik break; 4954235298eSChris Mason 4965cf1ab56SJosef Bacik if (!failed_mirror) { 4975cf1ab56SJosef Bacik failed = 1; 4985cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4995cf1ab56SJosef Bacik } 5005cf1ab56SJosef Bacik 501f188591eSChris Mason mirror_num++; 502ea466794SJosef Bacik if (mirror_num == failed_mirror) 503ea466794SJosef Bacik mirror_num++; 504ea466794SJosef Bacik 5054235298eSChris Mason if (mirror_num > num_copies) 506ea466794SJosef Bacik break; 507f188591eSChris Mason } 508ea466794SJosef Bacik 509c0901581SStefan Behrens if (failed && !ret && failed_mirror) 51020a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 511ea466794SJosef Bacik 512ea466794SJosef Bacik return ret; 513f188591eSChris Mason } 51419c00ddcSChris Mason 515d352ac68SChris Mason /* 516d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 517d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 518d352ac68SChris Mason */ 519d397712bSChris Mason 52001d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 52119c00ddcSChris Mason { 5224eee4fa4SMiao Xie u64 start = page_offset(page); 52319c00ddcSChris Mason u64 found_start; 5242996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 5252996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 52619c00ddcSChris Mason struct extent_buffer *eb; 5278d47a0d8SQu Wenruo int ret; 528f188591eSChris Mason 5294f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5304f2de97aSJosef Bacik if (page != eb->pages[0]) 5314f2de97aSJosef Bacik return 0; 5320f805531SAlex Lyakas 53319c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5340f805531SAlex Lyakas /* 5350f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5360f805531SAlex Lyakas * It is useful to know what went wrong. 5370f805531SAlex Lyakas */ 5380f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5390f805531SAlex Lyakas return -EUCLEAN; 5400f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5410f805531SAlex Lyakas return -EUCLEAN; 5420f805531SAlex Lyakas 543de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 5440f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5450f805531SAlex Lyakas 5462996e1f8SJohannes Thumshirn if (csum_tree_block(eb, result)) 5472996e1f8SJohannes Thumshirn return -EINVAL; 5482996e1f8SJohannes Thumshirn 5498d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 5508d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 5518d47a0d8SQu Wenruo else 5528d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 5538d47a0d8SQu Wenruo 5548d47a0d8SQu Wenruo if (ret < 0) { 555c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 5568d47a0d8SQu Wenruo btrfs_err(fs_info, 5578d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 5588d47a0d8SQu Wenruo eb->start); 559c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 5608d47a0d8SQu Wenruo return ret; 5618d47a0d8SQu Wenruo } 5622996e1f8SJohannes Thumshirn write_extent_buffer(eb, result, 0, csum_size); 5638d47a0d8SQu Wenruo 5642996e1f8SJohannes Thumshirn return 0; 56519c00ddcSChris Mason } 56619c00ddcSChris Mason 567b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5682b82032cSYan Zheng { 569b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 57001d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 57144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5722b82032cSYan Zheng int ret = 1; 5732b82032cSYan Zheng 5740a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5752b82032cSYan Zheng while (fs_devices) { 5767239ff4bSNikolay Borisov u8 *metadata_uuid; 5777239ff4bSNikolay Borisov 5787239ff4bSNikolay Borisov /* 5797239ff4bSNikolay Borisov * Checking the incompat flag is only valid for the current 5807239ff4bSNikolay Borisov * fs. For seed devices it's forbidden to have their uuid 5817239ff4bSNikolay Borisov * changed so reading ->fsid in this case is fine 5827239ff4bSNikolay Borisov */ 5837239ff4bSNikolay Borisov if (fs_devices == fs_info->fs_devices && 5847239ff4bSNikolay Borisov btrfs_fs_incompat(fs_info, METADATA_UUID)) 5857239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5867239ff4bSNikolay Borisov else 5877239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5887239ff4bSNikolay Borisov 5897239ff4bSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) { 5902b82032cSYan Zheng ret = 0; 5912b82032cSYan Zheng break; 5922b82032cSYan Zheng } 5932b82032cSYan Zheng fs_devices = fs_devices->seed; 5942b82032cSYan Zheng } 5952b82032cSYan Zheng return ret; 5962b82032cSYan Zheng } 5972b82032cSYan Zheng 598facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 599facc8a22SMiao Xie u64 phy_offset, struct page *page, 600facc8a22SMiao Xie u64 start, u64 end, int mirror) 601ce9adaa5SChris Mason { 602ce9adaa5SChris Mason u64 found_start; 603ce9adaa5SChris Mason int found_level; 604ce9adaa5SChris Mason struct extent_buffer *eb; 605ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 60602873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 6072996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 608f188591eSChris Mason int ret = 0; 6092996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 610727011e0SChris Mason int reads_done; 611ce9adaa5SChris Mason 612ce9adaa5SChris Mason if (!page->private) 613ce9adaa5SChris Mason goto out; 614d397712bSChris Mason 6154f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 616d397712bSChris Mason 6170b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6180b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6190b32f4bbSJosef Bacik */ 62067439dadSDavid Sterba atomic_inc(&eb->refs); 6210b32f4bbSJosef Bacik 6220b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 623727011e0SChris Mason if (!reads_done) 624727011e0SChris Mason goto err; 625f188591eSChris Mason 6265cf1ab56SJosef Bacik eb->read_mirror = mirror; 627656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 628ea466794SJosef Bacik ret = -EIO; 629ea466794SJosef Bacik goto err; 630ea466794SJosef Bacik } 631ea466794SJosef Bacik 632ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 633727011e0SChris Mason if (found_start != eb->start) { 634893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 635893bf4b1SSu Yue eb->start, found_start); 636f188591eSChris Mason ret = -EIO; 637ce9adaa5SChris Mason goto err; 638ce9adaa5SChris Mason } 639b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 64002873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 64194647322SDavid Sterba eb->start); 6421259ab75SChris Mason ret = -EIO; 6431259ab75SChris Mason goto err; 6441259ab75SChris Mason } 645ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6461c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 647893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 648893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6491c24c3ceSJosef Bacik ret = -EIO; 6501c24c3ceSJosef Bacik goto err; 6511c24c3ceSJosef Bacik } 652ce9adaa5SChris Mason 65385d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 65485d4e461SChris Mason eb, found_level); 6554008c04aSChris Mason 6562996e1f8SJohannes Thumshirn ret = csum_tree_block(eb, result); 6578bd98f0eSAlex Lyakas if (ret) 658a826d6dcSJosef Bacik goto err; 659a826d6dcSJosef Bacik 6602996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 6612996e1f8SJohannes Thumshirn u32 val; 6622996e1f8SJohannes Thumshirn u32 found = 0; 6632996e1f8SJohannes Thumshirn 6642996e1f8SJohannes Thumshirn memcpy(&found, result, csum_size); 6652996e1f8SJohannes Thumshirn 6662996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 6672996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 6682996e1f8SJohannes Thumshirn "%s checksum verify failed on %llu wanted %x found %x level %d", 6692996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 6702996e1f8SJohannes Thumshirn val, found, btrfs_header_level(eb)); 6712996e1f8SJohannes Thumshirn ret = -EUCLEAN; 6722996e1f8SJohannes Thumshirn goto err; 6732996e1f8SJohannes Thumshirn } 6742996e1f8SJohannes Thumshirn 675a826d6dcSJosef Bacik /* 676a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 677a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 678a826d6dcSJosef Bacik * return -EIO. 679a826d6dcSJosef Bacik */ 6801c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 681a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 682a826d6dcSJosef Bacik ret = -EIO; 683a826d6dcSJosef Bacik } 684ce9adaa5SChris Mason 685813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 686053ab70fSLiu Bo ret = -EIO; 687053ab70fSLiu Bo 6880b32f4bbSJosef Bacik if (!ret) 6890b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 69075391f0dSQu Wenruo else 69175391f0dSQu Wenruo btrfs_err(fs_info, 69275391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 69375391f0dSQu Wenruo eb->start); 694ce9adaa5SChris Mason err: 69579fb65a1SJosef Bacik if (reads_done && 69679fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 697d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6984bb31e92SArne Jansen 69953b381b3SDavid Woodhouse if (ret) { 70053b381b3SDavid Woodhouse /* 70153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 70253b381b3SDavid Woodhouse * again, we have to make sure it has something 70353b381b3SDavid Woodhouse * to decrement 70453b381b3SDavid Woodhouse */ 70553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7060b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 70753b381b3SDavid Woodhouse } 7080b32f4bbSJosef Bacik free_extent_buffer(eb); 709ce9adaa5SChris Mason out: 710f188591eSChris Mason return ret; 711ce9adaa5SChris Mason } 712ce9adaa5SChris Mason 7134246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 714ce9adaa5SChris Mason { 71597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 716ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7179e0af237SLiu Bo struct btrfs_workqueue *wq; 718ce9adaa5SChris Mason 719ce9adaa5SChris Mason fs_info = end_io_wq->info; 7204e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 721d20f7043SChris Mason 72237226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 723a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7249e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 725a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7269e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 727a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7289e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 729a0cac0ecSOmar Sandoval else 7309e0af237SLiu Bo wq = fs_info->endio_write_workers; 7319e0af237SLiu Bo } else { 732a0cac0ecSOmar Sandoval if (unlikely(end_io_wq->metadata == BTRFS_WQ_ENDIO_DIO_REPAIR)) 7338b110e39SMiao Xie wq = fs_info->endio_repair_workers; 734a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7359e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 736a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7379e0af237SLiu Bo wq = fs_info->endio_meta_workers; 738a0cac0ecSOmar Sandoval else 7399e0af237SLiu Bo wq = fs_info->endio_workers; 7409e0af237SLiu Bo } 7419e0af237SLiu Bo 742a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7439e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 744ce9adaa5SChris Mason } 745ce9adaa5SChris Mason 7464e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 747bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7480b86a832SChris Mason { 74997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7508b110e39SMiao Xie 75197eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 752ce9adaa5SChris Mason if (!end_io_wq) 7534e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 754ce9adaa5SChris Mason 755ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 756ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 75722c59948SChris Mason end_io_wq->info = info; 7584e4cbee9SChristoph Hellwig end_io_wq->status = 0; 759ce9adaa5SChris Mason end_io_wq->bio = bio; 76022c59948SChris Mason end_io_wq->metadata = metadata; 761ce9adaa5SChris Mason 762ce9adaa5SChris Mason bio->bi_private = end_io_wq; 763ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 76422c59948SChris Mason return 0; 76522c59948SChris Mason } 76622c59948SChris Mason 7674a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7684a69a410SChris Mason { 7694a69a410SChris Mason struct async_submit_bio *async; 7704e4cbee9SChristoph Hellwig blk_status_t ret; 7714a69a410SChris Mason 7724a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 773c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 774eaf25d93SChris Mason async->bio_offset); 77579787eaaSJeff Mahoney if (ret) 7764e4cbee9SChristoph Hellwig async->status = ret; 7774a69a410SChris Mason } 7784a69a410SChris Mason 77906ea01b1SDavid Sterba /* 78006ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 78106ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 78206ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 78306ea01b1SDavid Sterba * 78406ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 78506ea01b1SDavid Sterba * inserted into the tree. 78606ea01b1SDavid Sterba */ 7874a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7888b712842SChris Mason { 7898b712842SChris Mason struct async_submit_bio *async; 79006ea01b1SDavid Sterba struct inode *inode; 79106ea01b1SDavid Sterba blk_status_t ret; 7928b712842SChris Mason 7938b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 79406ea01b1SDavid Sterba inode = async->private_data; 7954854ddd0SChris Mason 796bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7974e4cbee9SChristoph Hellwig if (async->status) { 7984e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7994246a0b6SChristoph Hellwig bio_endio(async->bio); 80079787eaaSJeff Mahoney return; 80179787eaaSJeff Mahoney } 80279787eaaSJeff Mahoney 803ec39f769SChris Mason /* 804ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 805ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 806ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 807ec39f769SChris Mason */ 808ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 80908635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 81006ea01b1SDavid Sterba if (ret) { 81106ea01b1SDavid Sterba async->bio->bi_status = ret; 81206ea01b1SDavid Sterba bio_endio(async->bio); 81306ea01b1SDavid Sterba } 8144a69a410SChris Mason } 8154a69a410SChris Mason 8164a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8174a69a410SChris Mason { 8184a69a410SChris Mason struct async_submit_bio *async; 8194a69a410SChris Mason 8204a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8218b712842SChris Mason kfree(async); 8228b712842SChris Mason } 8238b712842SChris Mason 8248c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 825c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 826c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 827e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 82844b8bd7eSChris Mason { 82944b8bd7eSChris Mason struct async_submit_bio *async; 83044b8bd7eSChris Mason 83144b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 83244b8bd7eSChris Mason if (!async) 8334e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 83444b8bd7eSChris Mason 835c6100a4bSJosef Bacik async->private_data = private_data; 83644b8bd7eSChris Mason async->bio = bio; 83744b8bd7eSChris Mason async->mirror_num = mirror_num; 8384a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8394a69a410SChris Mason 840a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 841a0cac0ecSOmar Sandoval run_one_async_free); 8424a69a410SChris Mason 843eaf25d93SChris Mason async->bio_offset = bio_offset; 8448c8bee1dSChris Mason 8454e4cbee9SChristoph Hellwig async->status = 0; 84679787eaaSJeff Mahoney 84767f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8485cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 849d313d7a3SChris Mason 8505cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 85144b8bd7eSChris Mason return 0; 85244b8bd7eSChris Mason } 85344b8bd7eSChris Mason 8544e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 855ce3ed71aSChris Mason { 8562c30c71bSKent Overstreet struct bio_vec *bvec; 857ce3ed71aSChris Mason struct btrfs_root *root; 8582b070cfeSChristoph Hellwig int ret = 0; 8596dc4f100SMing Lei struct bvec_iter_all iter_all; 860ce3ed71aSChris Mason 861c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8622b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 863ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 86401d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 86579787eaaSJeff Mahoney if (ret) 86679787eaaSJeff Mahoney break; 867ce3ed71aSChris Mason } 8682c30c71bSKent Overstreet 8694e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 870ce3ed71aSChris Mason } 871ce3ed71aSChris Mason 872d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 873eaf25d93SChris Mason u64 bio_offset) 87422c59948SChris Mason { 8758b712842SChris Mason /* 8768b712842SChris Mason * when we're called for a write, we're already in the async 8775443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8788b712842SChris Mason */ 87979787eaaSJeff Mahoney return btree_csum_one_bio(bio); 88022c59948SChris Mason } 88122c59948SChris Mason 8829b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8839b4e675aSDavid Sterba struct btrfs_inode *bi) 884de0022b9SJosef Bacik { 8856300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8866300463bSLiu Bo return 0; 8879b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 888de0022b9SJosef Bacik return 0; 889de0022b9SJosef Bacik return 1; 890de0022b9SJosef Bacik } 891de0022b9SJosef Bacik 892a56b1c7bSNikolay Borisov static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio, 89350489a57SNikolay Borisov int mirror_num, 89450489a57SNikolay Borisov unsigned long bio_flags) 89544b8bd7eSChris Mason { 8960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8979b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8984e4cbee9SChristoph Hellwig blk_status_t ret; 899cad321adSChris Mason 90037226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 901cad321adSChris Mason /* 902cad321adSChris Mason * called for a read, do the setup so that checksum validation 903cad321adSChris Mason * can happen in the async kernel threads 904cad321adSChris Mason */ 9050b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9060b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9071d4284bdSChris Mason if (ret) 90861891923SStefan Behrens goto out_w_error; 90908635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 910de0022b9SJosef Bacik } else if (!async) { 911de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 912de0022b9SJosef Bacik if (ret) 91361891923SStefan Behrens goto out_w_error; 91408635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 91561891923SStefan Behrens } else { 916cad321adSChris Mason /* 91761891923SStefan Behrens * kthread helpers are used to submit writes so that 91861891923SStefan Behrens * checksumming can happen in parallel across all CPUs 919cad321adSChris Mason */ 920c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 921e68f2ee7SNikolay Borisov 0, inode, btree_submit_bio_start); 92244b8bd7eSChris Mason } 92344b8bd7eSChris Mason 9244246a0b6SChristoph Hellwig if (ret) 9254246a0b6SChristoph Hellwig goto out_w_error; 9264246a0b6SChristoph Hellwig return 0; 9274246a0b6SChristoph Hellwig 92861891923SStefan Behrens out_w_error: 9294e4cbee9SChristoph Hellwig bio->bi_status = ret; 9304246a0b6SChristoph Hellwig bio_endio(bio); 93161891923SStefan Behrens return ret; 93261891923SStefan Behrens } 93361891923SStefan Behrens 9343dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 935784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 936a6bc32b8SMel Gorman struct page *newpage, struct page *page, 937a6bc32b8SMel Gorman enum migrate_mode mode) 938784b4e29SChris Mason { 939784b4e29SChris Mason /* 940784b4e29SChris Mason * we can't safely write a btree page from here, 941784b4e29SChris Mason * we haven't done the locking hook 942784b4e29SChris Mason */ 943784b4e29SChris Mason if (PageDirty(page)) 944784b4e29SChris Mason return -EAGAIN; 945784b4e29SChris Mason /* 946784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 947784b4e29SChris Mason * We must have no buffers or drop them. 948784b4e29SChris Mason */ 949784b4e29SChris Mason if (page_has_private(page) && 950784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 951784b4e29SChris Mason return -EAGAIN; 952a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 953784b4e29SChris Mason } 9543dd1462eSJan Beulich #endif 955784b4e29SChris Mason 9560da5468fSChris Mason 9570da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9580da5468fSChris Mason struct writeback_control *wbc) 9590da5468fSChris Mason { 960e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 961e2d84521SMiao Xie int ret; 962e2d84521SMiao Xie 963d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 964448d640bSChris Mason 965448d640bSChris Mason if (wbc->for_kupdate) 966448d640bSChris Mason return 0; 967448d640bSChris Mason 968e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 969b9473439SChris Mason /* this is a bit racy, but that's ok */ 970d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 971d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 972d814a491SEthan Lien fs_info->dirty_metadata_batch); 973e2d84521SMiao Xie if (ret < 0) 974793955bcSChris Mason return 0; 975793955bcSChris Mason } 9760b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9770da5468fSChris Mason } 9780da5468fSChris Mason 979b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9805f39d397SChris Mason { 981d1310b2eSChris Mason struct extent_io_tree *tree; 982d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9838ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9845f39d397SChris Mason } 9855f39d397SChris Mason 98670dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9875f39d397SChris Mason { 98898509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 98998509cfcSChris Mason return 0; 9900c4e538bSDavid Sterba 991f7a52a40SDavid Sterba return try_release_extent_buffer(page); 992d98237b3SChris Mason } 993d98237b3SChris Mason 994d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 995d47992f8SLukas Czerner unsigned int length) 996d98237b3SChris Mason { 997d1310b2eSChris Mason struct extent_io_tree *tree; 998d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9995f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10005f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10019ad6b7bcSChris Mason if (PagePrivate(page)) { 1002efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1003efe120a0SFrank Holton "page private not zero on page %llu", 1004efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10059ad6b7bcSChris Mason ClearPagePrivate(page); 10069ad6b7bcSChris Mason set_page_private(page, 0); 100709cbfeafSKirill A. Shutemov put_page(page); 10089ad6b7bcSChris Mason } 1009d98237b3SChris Mason } 1010d98237b3SChris Mason 10110b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10120b32f4bbSJosef Bacik { 1013bb146eb2SJosef Bacik #ifdef DEBUG 10140b32f4bbSJosef Bacik struct extent_buffer *eb; 10150b32f4bbSJosef Bacik 10160b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10170b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10180b32f4bbSJosef Bacik BUG_ON(!eb); 10190b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10200b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10210b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1022bb146eb2SJosef Bacik #endif 10230b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10240b32f4bbSJosef Bacik } 10250b32f4bbSJosef Bacik 10267f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1027d98237b3SChris Mason .readpage = btree_readpage, 10280da5468fSChris Mason .writepages = btree_writepages, 10295f39d397SChris Mason .releasepage = btree_releasepage, 10305f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10315a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1032784b4e29SChris Mason .migratepage = btree_migratepage, 10335a92bc88SChris Mason #endif 10340b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1035d98237b3SChris Mason }; 1036123abc88SChris Mason 10372ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1038090d1875SChris Mason { 10395f39d397SChris Mason struct extent_buffer *buf = NULL; 1040537f38f0SNikolay Borisov int ret; 1041090d1875SChris Mason 10422ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1043c871b0f2SLiu Bo if (IS_ERR(buf)) 10446197d86eSDavid Sterba return; 1045537f38f0SNikolay Borisov 1046c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 1047537f38f0SNikolay Borisov if (ret < 0) 1048537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1049537f38f0SNikolay Borisov else 10505f39d397SChris Mason free_extent_buffer(buf); 1051090d1875SChris Mason } 1052090d1875SChris Mason 10532ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10542ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10550308af44SDavid Sterba u64 bytenr) 10560999df54SChris Mason { 10570b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10580b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10590b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10600999df54SChris Mason } 10610999df54SChris Mason 1062581c1760SQu Wenruo /* 1063581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1064581c1760SQu Wenruo * verification. 1065581c1760SQu Wenruo * 1066581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1067581c1760SQu Wenruo * @level: expected level, mandatory check 1068581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1069581c1760SQu Wenruo */ 10702ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1071581c1760SQu Wenruo u64 parent_transid, int level, 1072581c1760SQu Wenruo struct btrfs_key *first_key) 1073e20d96d6SChris Mason { 10745f39d397SChris Mason struct extent_buffer *buf = NULL; 107519c00ddcSChris Mason int ret; 107619c00ddcSChris Mason 10772ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1078c871b0f2SLiu Bo if (IS_ERR(buf)) 1079c871b0f2SLiu Bo return buf; 1080e4204dedSChris Mason 10815ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1082581c1760SQu Wenruo level, first_key); 10830f0fe8f7SFilipe David Borba Manana if (ret) { 1084537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 108564c043deSLiu Bo return ERR_PTR(ret); 10860f0fe8f7SFilipe David Borba Manana } 10875f39d397SChris Mason return buf; 1088ce9adaa5SChris Mason 1089eb60ceacSChris Mason } 1090eb60ceacSChris Mason 10916a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1092ed2ff2cbSChris Mason { 10936a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 109455c69072SChris Mason if (btrfs_header_generation(buf) == 1095e2d84521SMiao Xie fs_info->running_transaction->transid) { 1096b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1097b4ce94deSChris Mason 1098b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1099104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1100e2d84521SMiao Xie -buf->len, 1101e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1102b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 11038bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 11040b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1105925baeddSChris Mason } 11065f39d397SChris Mason } 1107ed7b63ebSJosef Bacik } 11085f39d397SChris Mason 11098257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11108257b2dcSMiao Xie { 11118257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11128257b2dcSMiao Xie int ret; 11138257b2dcSMiao Xie 11148257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11158257b2dcSMiao Xie if (!writers) 11168257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11178257b2dcSMiao Xie 11188a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11198257b2dcSMiao Xie if (ret < 0) { 11208257b2dcSMiao Xie kfree(writers); 11218257b2dcSMiao Xie return ERR_PTR(ret); 11228257b2dcSMiao Xie } 11238257b2dcSMiao Xie 11248257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11258257b2dcSMiao Xie return writers; 11268257b2dcSMiao Xie } 11278257b2dcSMiao Xie 11288257b2dcSMiao Xie static void 11298257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11308257b2dcSMiao Xie { 11318257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11328257b2dcSMiao Xie kfree(writers); 11338257b2dcSMiao Xie } 11348257b2dcSMiao Xie 1135da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1136e20d96d6SChris Mason u64 objectid) 1137d97e63b6SChris Mason { 11387c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 113996dfcb46SJosef Bacik root->fs_info = fs_info; 1140cfaa7295SChris Mason root->node = NULL; 1141a28ec197SChris Mason root->commit_root = NULL; 114227cdeb70SMiao Xie root->state = 0; 1143d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11440b86a832SChris Mason 11450f7d52f4SChris Mason root->last_trans = 0; 114613a8a7c8SYan, Zheng root->highest_objectid = 0; 1147eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1148199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11496bef4d31SEric Paris root->inode_tree = RB_ROOT; 115016cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1151f0486c68SYan, Zheng root->block_rsv = NULL; 11520b86a832SChris Mason 11530b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11545d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1155eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1156eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1157199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1158199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1159d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11602ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11612ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11625d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1163eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1164199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1165f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11662ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11672ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11688287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1169a2135011SChris Mason mutex_init(&root->objectid_mutex); 1170e02119d5SChris Mason mutex_init(&root->log_mutex); 117131f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1172573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11737237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11747237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11757237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11768b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11778b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11787237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11797237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11807237f183SYan Zheng atomic_set(&root->log_writers, 0); 11812ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11820700cea7SElena Reshetova refcount_set(&root->refs, 1); 1183ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 11848ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1185eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11867237f183SYan Zheng root->log_transid = 0; 1187d1433debSMiao Xie root->log_transid_committed = -1; 1188257c62e1SChris Mason root->last_log_commit = 0; 11897c0260eeSJeff Mahoney if (!dummy) 119043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 119143eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1192017e5369SChris Mason 11933768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11943768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11956702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11967c0260eeSJeff Mahoney if (!dummy) 11973f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119806ea65a3SJosef Bacik else 119906ea65a3SJosef Bacik root->defrag_trans_start = 0; 12004d775673SChris Mason root->root_key.objectid = objectid; 12010ee5dc67SAl Viro root->anon_dev = 0; 12028ea05e3aSAlexander Block 12035f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1204370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 12053768f368SChris Mason } 12063768f368SChris Mason 120774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 120896dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12096f07e42eSAl Viro { 121074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12116f07e42eSAl Viro if (root) 121296dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12136f07e42eSAl Viro return root; 12146f07e42eSAl Viro } 12156f07e42eSAl Viro 121606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 121706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1218da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 121906ea65a3SJosef Bacik { 122006ea65a3SJosef Bacik struct btrfs_root *root; 122106ea65a3SJosef Bacik 12227c0260eeSJeff Mahoney if (!fs_info) 12237c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12247c0260eeSJeff Mahoney 122596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 122606ea65a3SJosef Bacik if (!root) 122706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1228da17066cSJeff Mahoney 1229b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1230faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 123106ea65a3SJosef Bacik 123206ea65a3SJosef Bacik return root; 123306ea65a3SJosef Bacik } 123406ea65a3SJosef Bacik #endif 123506ea65a3SJosef Bacik 123620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 123720897f5cSArne Jansen u64 objectid) 123820897f5cSArne Jansen { 12399b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 124020897f5cSArne Jansen struct extent_buffer *leaf; 124120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 124220897f5cSArne Jansen struct btrfs_root *root; 124320897f5cSArne Jansen struct btrfs_key key; 1244b89f6d1fSFilipe Manana unsigned int nofs_flag; 124520897f5cSArne Jansen int ret = 0; 124633d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 124720897f5cSArne Jansen 1248b89f6d1fSFilipe Manana /* 1249b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1250b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1251b89f6d1fSFilipe Manana */ 1252b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 125396dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1254b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 125520897f5cSArne Jansen if (!root) 125620897f5cSArne Jansen return ERR_PTR(-ENOMEM); 125720897f5cSArne Jansen 125820897f5cSArne Jansen root->root_key.objectid = objectid; 125920897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 126020897f5cSArne Jansen root->root_key.offset = 0; 126120897f5cSArne Jansen 12624d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 126320897f5cSArne Jansen if (IS_ERR(leaf)) { 126420897f5cSArne Jansen ret = PTR_ERR(leaf); 12651dd05682STsutomu Itoh leaf = NULL; 126620897f5cSArne Jansen goto fail; 126720897f5cSArne Jansen } 126820897f5cSArne Jansen 126920897f5cSArne Jansen root->node = leaf; 127020897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 127120897f5cSArne Jansen 127220897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 127327cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 127420897f5cSArne Jansen 127520897f5cSArne Jansen root->root_item.flags = 0; 127620897f5cSArne Jansen root->root_item.byte_limit = 0; 127720897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 127820897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 127920897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 128020897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 128120897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 128220897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 128320897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 128433d85fdaSQu Wenruo if (is_fstree(objectid)) 12856463fe58SStefan Behrens uuid_le_gen(&uuid); 12866463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 128720897f5cSArne Jansen root->root_item.drop_level = 0; 128820897f5cSArne Jansen 128920897f5cSArne Jansen key.objectid = objectid; 129020897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 129120897f5cSArne Jansen key.offset = 0; 129220897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129320897f5cSArne Jansen if (ret) 129420897f5cSArne Jansen goto fail; 129520897f5cSArne Jansen 129620897f5cSArne Jansen btrfs_tree_unlock(leaf); 129720897f5cSArne Jansen 129820897f5cSArne Jansen return root; 12991dd05682STsutomu Itoh 13001dd05682STsutomu Itoh fail: 13011dd05682STsutomu Itoh if (leaf) { 13021dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 130359885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13041dd05682STsutomu Itoh free_extent_buffer(leaf); 13051dd05682STsutomu Itoh } 130681f096edSJosef Bacik btrfs_put_fs_root(root); 13071dd05682STsutomu Itoh 13081dd05682STsutomu Itoh return ERR_PTR(ret); 130920897f5cSArne Jansen } 131020897f5cSArne Jansen 13117237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1312e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13130f7d52f4SChris Mason { 13140f7d52f4SChris Mason struct btrfs_root *root; 13157237f183SYan Zheng struct extent_buffer *leaf; 1316e02119d5SChris Mason 131796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1318e02119d5SChris Mason if (!root) 13197237f183SYan Zheng return ERR_PTR(-ENOMEM); 1320e02119d5SChris Mason 1321e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1322e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1323e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 132427cdeb70SMiao Xie 13257237f183SYan Zheng /* 132627cdeb70SMiao Xie * DON'T set REF_COWS for log trees 132727cdeb70SMiao Xie * 13287237f183SYan Zheng * log trees do not get reference counted because they go away 13297237f183SYan Zheng * before a real commit is actually done. They do store pointers 13307237f183SYan Zheng * to file data extents, and those reference counts still get 13317237f183SYan Zheng * updated (along with back refs to the log tree). 13327237f183SYan Zheng */ 1333e02119d5SChris Mason 13344d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13354d75f8a9SDavid Sterba NULL, 0, 0, 0); 13367237f183SYan Zheng if (IS_ERR(leaf)) { 133781f096edSJosef Bacik btrfs_put_fs_root(root); 13387237f183SYan Zheng return ERR_CAST(leaf); 13397237f183SYan Zheng } 1340e02119d5SChris Mason 13417237f183SYan Zheng root->node = leaf; 1342e02119d5SChris Mason 1343e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1344e02119d5SChris Mason btrfs_tree_unlock(root->node); 13457237f183SYan Zheng return root; 13467237f183SYan Zheng } 13477237f183SYan Zheng 13487237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13497237f183SYan Zheng struct btrfs_fs_info *fs_info) 13507237f183SYan Zheng { 13517237f183SYan Zheng struct btrfs_root *log_root; 13527237f183SYan Zheng 13537237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13547237f183SYan Zheng if (IS_ERR(log_root)) 13557237f183SYan Zheng return PTR_ERR(log_root); 13567237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13577237f183SYan Zheng fs_info->log_root_tree = log_root; 13587237f183SYan Zheng return 0; 13597237f183SYan Zheng } 13607237f183SYan Zheng 13617237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13627237f183SYan Zheng struct btrfs_root *root) 13637237f183SYan Zheng { 13640b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13657237f183SYan Zheng struct btrfs_root *log_root; 13667237f183SYan Zheng struct btrfs_inode_item *inode_item; 13677237f183SYan Zheng 13680b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13697237f183SYan Zheng if (IS_ERR(log_root)) 13707237f183SYan Zheng return PTR_ERR(log_root); 13717237f183SYan Zheng 13727237f183SYan Zheng log_root->last_trans = trans->transid; 13737237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13747237f183SYan Zheng 13757237f183SYan Zheng inode_item = &log_root->root_item.inode; 13763cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13773cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13783cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1379da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13800b246afaSJeff Mahoney fs_info->nodesize); 13813cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13827237f183SYan Zheng 13835d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13847237f183SYan Zheng 13857237f183SYan Zheng WARN_ON(root->log_root); 13867237f183SYan Zheng root->log_root = log_root; 13877237f183SYan Zheng root->log_transid = 0; 1388d1433debSMiao Xie root->log_transid_committed = -1; 1389257c62e1SChris Mason root->last_log_commit = 0; 1390e02119d5SChris Mason return 0; 1391e02119d5SChris Mason } 1392e02119d5SChris Mason 139362a2c73eSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1394cb517eabSMiao Xie struct btrfs_key *key) 1395e02119d5SChris Mason { 1396e02119d5SChris Mason struct btrfs_root *root; 1397e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 13980f7d52f4SChris Mason struct btrfs_path *path; 139984234f3aSYan Zheng u64 generation; 1400cb517eabSMiao Xie int ret; 1401581c1760SQu Wenruo int level; 1402cb517eabSMiao Xie 1403cb517eabSMiao Xie path = btrfs_alloc_path(); 1404cb517eabSMiao Xie if (!path) 1405cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14060f7d52f4SChris Mason 140796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 1408cb517eabSMiao Xie if (!root) { 1409cb517eabSMiao Xie ret = -ENOMEM; 1410cb517eabSMiao Xie goto alloc_fail; 14110f7d52f4SChris Mason } 14120f7d52f4SChris Mason 1413cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1414cb517eabSMiao Xie &root->root_item, &root->root_key); 14150f7d52f4SChris Mason if (ret) { 141613a8a7c8SYan, Zheng if (ret > 0) 141713a8a7c8SYan, Zheng ret = -ENOENT; 1418cb517eabSMiao Xie goto find_fail; 14190f7d52f4SChris Mason } 142013a8a7c8SYan, Zheng 142184234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1422581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14232ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14242ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1425581c1760SQu Wenruo generation, level, NULL); 142664c043deSLiu Bo if (IS_ERR(root->node)) { 142764c043deSLiu Bo ret = PTR_ERR(root->node); 1428cb517eabSMiao Xie goto find_fail; 1429cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1430cb517eabSMiao Xie ret = -EIO; 143164c043deSLiu Bo free_extent_buffer(root->node); 143264c043deSLiu Bo goto find_fail; 1433416bc658SJosef Bacik } 14345d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 143513a8a7c8SYan, Zheng out: 1436cb517eabSMiao Xie btrfs_free_path(path); 1437cb517eabSMiao Xie return root; 1438cb517eabSMiao Xie 1439cb517eabSMiao Xie find_fail: 144081f096edSJosef Bacik btrfs_put_fs_root(root); 1441cb517eabSMiao Xie alloc_fail: 1442cb517eabSMiao Xie root = ERR_PTR(ret); 1443cb517eabSMiao Xie goto out; 1444cb517eabSMiao Xie } 1445cb517eabSMiao Xie 1446a98db0f3SJosef Bacik static int btrfs_init_fs_root(struct btrfs_root *root) 1447cb517eabSMiao Xie { 1448cb517eabSMiao Xie int ret; 14498257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1450cb517eabSMiao Xie 1451cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1452cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1453cb517eabSMiao Xie GFP_NOFS); 1454cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1455cb517eabSMiao Xie ret = -ENOMEM; 1456cb517eabSMiao Xie goto fail; 1457cb517eabSMiao Xie } 1458cb517eabSMiao Xie 14598257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14608257b2dcSMiao Xie if (IS_ERR(writers)) { 14618257b2dcSMiao Xie ret = PTR_ERR(writers); 14628257b2dcSMiao Xie goto fail; 14638257b2dcSMiao Xie } 14648257b2dcSMiao Xie root->subv_writers = writers; 14658257b2dcSMiao Xie 1466f39e4571SJosef Bacik if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 1467f39e4571SJosef Bacik set_bit(BTRFS_ROOT_REF_COWS, &root->state); 1468f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1469f39e4571SJosef Bacik } 1470f39e4571SJosef Bacik 1471cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 147257cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 147357cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1474cb517eabSMiao Xie 1475cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1476cb517eabSMiao Xie if (ret) 1477876d2cf1SLiu Bo goto fail; 1478f32e48e9SChandan Rajendra 1479f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1480f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1481f32e48e9SChandan Rajendra &root->highest_objectid); 1482f32e48e9SChandan Rajendra if (ret) { 1483f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1484876d2cf1SLiu Bo goto fail; 1485f32e48e9SChandan Rajendra } 1486f32e48e9SChandan Rajendra 1487f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1488f32e48e9SChandan Rajendra 1489f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1490f32e48e9SChandan Rajendra 1491cb517eabSMiao Xie return 0; 1492cb517eabSMiao Xie fail: 149384db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1494cb517eabSMiao Xie return ret; 1495cb517eabSMiao Xie } 1496cb517eabSMiao Xie 1497a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1498cb517eabSMiao Xie u64 root_id) 1499cb517eabSMiao Xie { 1500cb517eabSMiao Xie struct btrfs_root *root; 1501cb517eabSMiao Xie 1502cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1503cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1504cb517eabSMiao Xie (unsigned long)root_id); 1505bc44d7c4SJosef Bacik if (root) 1506bc44d7c4SJosef Bacik root = btrfs_grab_fs_root(root); 1507cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1508cb517eabSMiao Xie return root; 1509cb517eabSMiao Xie } 1510cb517eabSMiao Xie 1511cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1512cb517eabSMiao Xie struct btrfs_root *root) 1513cb517eabSMiao Xie { 1514cb517eabSMiao Xie int ret; 1515cb517eabSMiao Xie 1516e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1517cb517eabSMiao Xie if (ret) 1518cb517eabSMiao Xie return ret; 1519cb517eabSMiao Xie 1520cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1521cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1522cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1523cb517eabSMiao Xie root); 1524af01d2e5SJosef Bacik if (ret == 0) { 1525af01d2e5SJosef Bacik btrfs_grab_fs_root(root); 152627cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1527af01d2e5SJosef Bacik } 1528cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1529cb517eabSMiao Xie radix_tree_preload_end(); 1530cb517eabSMiao Xie 1531cb517eabSMiao Xie return ret; 1532cb517eabSMiao Xie } 1533cb517eabSMiao Xie 15340d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15350d4b0463SJosef Bacik { 1536141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1537141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1538141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1539141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1540141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1541141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1542141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15430d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15440d4b0463SJosef Bacik kfree(fs_info->delayed_root); 154581f096edSJosef Bacik btrfs_put_fs_root(fs_info->extent_root); 154681f096edSJosef Bacik btrfs_put_fs_root(fs_info->tree_root); 154781f096edSJosef Bacik btrfs_put_fs_root(fs_info->chunk_root); 154881f096edSJosef Bacik btrfs_put_fs_root(fs_info->dev_root); 154981f096edSJosef Bacik btrfs_put_fs_root(fs_info->csum_root); 155081f096edSJosef Bacik btrfs_put_fs_root(fs_info->quota_root); 155181f096edSJosef Bacik btrfs_put_fs_root(fs_info->uuid_root); 155281f096edSJosef Bacik btrfs_put_fs_root(fs_info->free_space_root); 155381f096edSJosef Bacik btrfs_put_fs_root(fs_info->fs_root); 15540d4b0463SJosef Bacik kfree(fs_info->super_copy); 15550d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15560d4b0463SJosef Bacik kvfree(fs_info); 15570d4b0463SJosef Bacik } 15580d4b0463SJosef Bacik 15590d4b0463SJosef Bacik 1560c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1561c00869f1SMiao Xie struct btrfs_key *location, 1562c00869f1SMiao Xie bool check_ref) 15635eda7b5eSChris Mason { 15645eda7b5eSChris Mason struct btrfs_root *root; 1565381cf658SDavid Sterba struct btrfs_path *path; 15661d4c08e0SDavid Sterba struct btrfs_key key; 15675eda7b5eSChris Mason int ret; 15685eda7b5eSChris Mason 1569edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1570bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->tree_root); 1571edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1572bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->extent_root); 15738f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 1574bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->chunk_root); 15758f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 1576bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->dev_root); 15770403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 1578bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->csum_root); 1579bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1580bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->quota_root) ? 1581bc44d7c4SJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 1582f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1583bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->uuid_root) ? 1584bc44d7c4SJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 158570f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 1586bc44d7c4SJosef Bacik return btrfs_grab_fs_root(fs_info->free_space_root) ? 1587bc44d7c4SJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 15884df27c4dSYan, Zheng again: 1589cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 159048475471SStefan Behrens if (root) { 1591bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1592bc44d7c4SJosef Bacik btrfs_put_fs_root(root); 159348475471SStefan Behrens return ERR_PTR(-ENOENT); 1594bc44d7c4SJosef Bacik } 15955eda7b5eSChris Mason return root; 159648475471SStefan Behrens } 15975eda7b5eSChris Mason 159883db2aadSJosef Bacik root = btrfs_read_tree_root(fs_info->tree_root, location); 15995eda7b5eSChris Mason if (IS_ERR(root)) 16005eda7b5eSChris Mason return root; 16013394e160SChris Mason 1602c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1603d68fc57bSYan, Zheng ret = -ENOENT; 1604d68fc57bSYan, Zheng goto fail; 1605d68fc57bSYan, Zheng } 1606d68fc57bSYan, Zheng 1607cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1608cb517eabSMiao Xie if (ret) 1609cb517eabSMiao Xie goto fail; 1610cb517eabSMiao Xie 1611381cf658SDavid Sterba path = btrfs_alloc_path(); 1612381cf658SDavid Sterba if (!path) { 1613381cf658SDavid Sterba ret = -ENOMEM; 1614381cf658SDavid Sterba goto fail; 1615381cf658SDavid Sterba } 16161d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16171d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16181d4c08e0SDavid Sterba key.offset = location->objectid; 16191d4c08e0SDavid Sterba 16201d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1621381cf658SDavid Sterba btrfs_free_path(path); 1622d68fc57bSYan, Zheng if (ret < 0) 1623d68fc57bSYan, Zheng goto fail; 1624d68fc57bSYan, Zheng if (ret == 0) 162527cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1626d68fc57bSYan, Zheng 1627bc44d7c4SJosef Bacik /* 1628bc44d7c4SJosef Bacik * All roots have two refs on them at all times, one for the mounted fs, 1629bc44d7c4SJosef Bacik * and one for being in the radix tree. This way we only free the root 1630bc44d7c4SJosef Bacik * when we are unmounting or deleting the subvolume. We get one ref 1631bc44d7c4SJosef Bacik * from __setup_root, one for inserting it into the radix tree, and then 1632bc44d7c4SJosef Bacik * we have the third for returning it, and the caller will put it when 1633bc44d7c4SJosef Bacik * it's done with the root. 1634bc44d7c4SJosef Bacik */ 1635bc44d7c4SJosef Bacik btrfs_grab_fs_root(root); 1636cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16370f7d52f4SChris Mason if (ret) { 1638bc44d7c4SJosef Bacik btrfs_put_fs_root(root); 16394df27c4dSYan, Zheng if (ret == -EEXIST) { 164084db5ccfSDavid Sterba btrfs_free_fs_root(root); 16414df27c4dSYan, Zheng goto again; 16420f7d52f4SChris Mason } 16434df27c4dSYan, Zheng goto fail; 16444df27c4dSYan, Zheng } 1645edbd8d4eSChris Mason return root; 16464df27c4dSYan, Zheng fail: 164784db5ccfSDavid Sterba btrfs_free_fs_root(root); 16484df27c4dSYan, Zheng return ERR_PTR(ret); 1649edbd8d4eSChris Mason } 1650edbd8d4eSChris Mason 165104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 165204160088SChris Mason { 165304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 165404160088SChris Mason int ret = 0; 165504160088SChris Mason struct btrfs_device *device; 165604160088SChris Mason struct backing_dev_info *bdi; 1657b7967db7SChris Mason 16581f78160cSXiao Guangrong rcu_read_lock(); 16591f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1660dfe25020SChris Mason if (!device->bdev) 1661dfe25020SChris Mason continue; 1662efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1663ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 166404160088SChris Mason ret = 1; 166504160088SChris Mason break; 166604160088SChris Mason } 166704160088SChris Mason } 16681f78160cSXiao Guangrong rcu_read_unlock(); 166904160088SChris Mason return ret; 167004160088SChris Mason } 167104160088SChris Mason 16728b712842SChris Mason /* 16738b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16748b712842SChris Mason * functions. This is where read checksum verification actually happens 16758b712842SChris Mason */ 16768b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1677ce9adaa5SChris Mason { 1678ce9adaa5SChris Mason struct bio *bio; 167997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1680ce9adaa5SChris Mason 168197eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1682ce9adaa5SChris Mason bio = end_io_wq->bio; 16838b712842SChris Mason 16844e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1685ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1686ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 16874246a0b6SChristoph Hellwig bio_endio(bio); 16889be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1689ce9adaa5SChris Mason } 169044b8bd7eSChris Mason 1691a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1692a74a4b97SChris Mason { 1693a74a4b97SChris Mason struct btrfs_root *root = arg; 16940b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1695d0278245SMiao Xie int again; 1696a74a4b97SChris Mason 1697d6fd0ae2SOmar Sandoval while (1) { 1698d0278245SMiao Xie again = 0; 16999d1a2a3aSDavid Sterba 1700fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1701fd340d0fSJosef Bacik 1702d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17032ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1704d0278245SMiao Xie goto sleep; 1705d0278245SMiao Xie 170690c711abSZygo Blaxell /* 170790c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 170890c711abSZygo Blaxell * before we have finished mounting the filesystem. 170990c711abSZygo Blaxell */ 17100b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 171190c711abSZygo Blaxell goto sleep; 171290c711abSZygo Blaxell 17130b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1714d0278245SMiao Xie goto sleep; 1715d0278245SMiao Xie 1716dc7f370cSMiao Xie /* 1717dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1718dc7f370cSMiao Xie * during the above check and trylock. 1719dc7f370cSMiao Xie */ 17202ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17210b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1722dc7f370cSMiao Xie goto sleep; 1723dc7f370cSMiao Xie } 1724dc7f370cSMiao Xie 17252ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1726c2d6cb16SFilipe Manana 17279d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17280b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1729a74a4b97SChris Mason 1730d0278245SMiao Xie /* 173105323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 173205323cd1SMiao Xie * needn't do anything special here. 1733d0278245SMiao Xie */ 17340b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 173567c5e7d4SFilipe Manana 173667c5e7d4SFilipe Manana /* 173767c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 173867c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 173967c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 174067c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 174167c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 174267c5e7d4SFilipe Manana * unused block groups. 174367c5e7d4SFilipe Manana */ 17440b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1745d0278245SMiao Xie sleep: 1746fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1747d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1748d6fd0ae2SOmar Sandoval kthread_parkme(); 1749d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1750d6fd0ae2SOmar Sandoval return 0; 1751838fe188SJiri Kosina if (!again) { 1752a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1753a74a4b97SChris Mason schedule(); 1754a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1755a74a4b97SChris Mason } 1756da288d28SFilipe Manana } 1757a74a4b97SChris Mason } 1758a74a4b97SChris Mason 1759a74a4b97SChris Mason static int transaction_kthread(void *arg) 1760a74a4b97SChris Mason { 1761a74a4b97SChris Mason struct btrfs_root *root = arg; 17620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1763a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1764a74a4b97SChris Mason struct btrfs_transaction *cur; 17658929ecfaSYan, Zheng u64 transid; 1766a944442cSAllen Pais time64_t now; 1767a74a4b97SChris Mason unsigned long delay; 1768914b2007SJan Kara bool cannot_commit; 1769a74a4b97SChris Mason 1770a74a4b97SChris Mason do { 1771914b2007SJan Kara cannot_commit = false; 17720b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17730b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1774a74a4b97SChris Mason 17750b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17760b246afaSJeff Mahoney cur = fs_info->running_transaction; 1777a74a4b97SChris Mason if (!cur) { 17780b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1779a74a4b97SChris Mason goto sleep; 1780a74a4b97SChris Mason } 178131153d81SYan Zheng 1782afd48513SArnd Bergmann now = ktime_get_seconds(); 17833296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1784a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 17858b87dc17SDavid Sterba (now < cur->start_time || 17860b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1788a74a4b97SChris Mason delay = HZ * 5; 1789a74a4b97SChris Mason goto sleep; 1790a74a4b97SChris Mason } 17918929ecfaSYan, Zheng transid = cur->transid; 17920b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 179356bec294SChris Mason 179479787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1795354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1796914b2007SJan Kara if (IS_ERR(trans)) { 1797354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1798914b2007SJan Kara cannot_commit = true; 179979787eaaSJeff Mahoney goto sleep; 1800914b2007SJan Kara } 18018929ecfaSYan, Zheng if (transid == trans->transid) { 18023a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18038929ecfaSYan, Zheng } else { 18043a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18058929ecfaSYan, Zheng } 1806a74a4b97SChris Mason sleep: 18070b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18080b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1809a74a4b97SChris Mason 18104e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18110b246afaSJeff Mahoney &fs_info->fs_state))) 18122ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18138929ecfaSYan, Zheng if (!kthread_should_stop() && 18140b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1815914b2007SJan Kara cannot_commit)) 1816bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1817a74a4b97SChris Mason } while (!kthread_should_stop()); 1818a74a4b97SChris Mason return 0; 1819a74a4b97SChris Mason } 1820a74a4b97SChris Mason 1821af31f5e5SChris Mason /* 182201f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 182301f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 182401f0f9daSNikolay Borisov * anything. 1825af31f5e5SChris Mason * 1826af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1827af31f5e5SChris Mason * generation of the latest root in the array with the generation 1828af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1829af31f5e5SChris Mason */ 183001f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1831af31f5e5SChris Mason { 183201f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1833af31f5e5SChris Mason u64 cur; 1834af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1835af31f5e5SChris Mason int i; 1836af31f5e5SChris Mason 1837af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1838af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1839af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1840af31f5e5SChris Mason if (cur == newest_gen) 184101f0f9daSNikolay Borisov return i; 1842af31f5e5SChris Mason } 1843af31f5e5SChris Mason 184401f0f9daSNikolay Borisov return -EINVAL; 1845af31f5e5SChris Mason } 1846af31f5e5SChris Mason 1847af31f5e5SChris Mason /* 1848af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1849af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1850af31f5e5SChris Mason * done 1851af31f5e5SChris Mason */ 1852af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1853af31f5e5SChris Mason { 18546ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1855af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1856af31f5e5SChris Mason 1857af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1858af31f5e5SChris Mason 1859af31f5e5SChris Mason /* 1860af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1861af31f5e5SChris Mason * regardless of which ones we use today 1862af31f5e5SChris Mason */ 1863af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1864af31f5e5SChris Mason 1865af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1866af31f5e5SChris Mason 1867af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1868af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1869af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1870af31f5e5SChris Mason 1871af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1872af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1873af31f5e5SChris Mason 1874af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1875af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1876af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1877af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1878af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1879af31f5e5SChris Mason 1880af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1881af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1882af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1883af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1884af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1885af31f5e5SChris Mason 18867c7e82a7SChris Mason /* 18877c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18887c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18897c7e82a7SChris Mason */ 18907c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18917c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18927c7e82a7SChris Mason info->fs_root->node->start); 1893af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1894af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1895af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1896af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18977c7e82a7SChris Mason } 1898af31f5e5SChris Mason 1899af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1900af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1901af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1902af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1903af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1904af31f5e5SChris Mason 1905af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1906af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1907af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1908af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1909af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1910af31f5e5SChris Mason 1911af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1912af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1913af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1914af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1915af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1916af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1917af31f5e5SChris Mason 1918af31f5e5SChris Mason /* 1919af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1920af31f5e5SChris Mason * for the next commit 1921af31f5e5SChris Mason */ 1922af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1923af31f5e5SChris Mason &info->super_for_commit->super_roots, 1924af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1925af31f5e5SChris Mason } 1926af31f5e5SChris Mason 1927af31f5e5SChris Mason /* 1928bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1929bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1930bd2336b2SNikolay Borisov * 1931bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1932bd2336b2SNikolay Borisov * priority - priority of backup root required 1933bd2336b2SNikolay Borisov * 1934bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1935bd2336b2SNikolay Borisov */ 1936bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1937bd2336b2SNikolay Borisov { 1938bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1939bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1940bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1941bd2336b2SNikolay Borisov 1942bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1943bd2336b2SNikolay Borisov if (priority == 0) 1944bd2336b2SNikolay Borisov return backup_index; 1945bd2336b2SNikolay Borisov 1946bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1947bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1948bd2336b2SNikolay Borisov } else { 1949bd2336b2SNikolay Borisov return -EINVAL; 1950bd2336b2SNikolay Borisov } 1951bd2336b2SNikolay Borisov 1952bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1953bd2336b2SNikolay Borisov 1954bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1955bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1956bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1957bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1958bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1959bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1960bd2336b2SNikolay Borisov 1961bd2336b2SNikolay Borisov /* 1962bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1963bd2336b2SNikolay Borisov * need a fsck 1964bd2336b2SNikolay Borisov */ 1965bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1966bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1967bd2336b2SNikolay Borisov 1968bd2336b2SNikolay Borisov return backup_index; 1969bd2336b2SNikolay Borisov } 1970bd2336b2SNikolay Borisov 19717abadb64SLiu Bo /* helper to cleanup workers */ 19727abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19737abadb64SLiu Bo { 1974dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1975afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 19765cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 1977fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 1978fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 19798b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 1980d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 1981fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 1982fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 19835b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 1984e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 1985736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 1986a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 1987fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 1988b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 1989b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 1990a9b9477dSFilipe Manana /* 1991a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 1992a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 1993a9b9477dSFilipe Manana * queues can do metadata I/O operations. 1994a9b9477dSFilipe Manana */ 1995a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 1996a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 19977abadb64SLiu Bo } 19987abadb64SLiu Bo 19992e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20002e9f5954SRashika { 20012e9f5954SRashika if (root) { 20022e9f5954SRashika free_extent_buffer(root->node); 20032e9f5954SRashika free_extent_buffer(root->commit_root); 20042e9f5954SRashika root->node = NULL; 20052e9f5954SRashika root->commit_root = NULL; 20062e9f5954SRashika } 20072e9f5954SRashika } 20082e9f5954SRashika 2009af31f5e5SChris Mason /* helper to cleanup tree roots */ 20104273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2011af31f5e5SChris Mason { 20122e9f5954SRashika free_root_extent_buffers(info->tree_root); 2013655b09feSJosef Bacik 20142e9f5954SRashika free_root_extent_buffers(info->dev_root); 20152e9f5954SRashika free_root_extent_buffers(info->extent_root); 20162e9f5954SRashika free_root_extent_buffers(info->csum_root); 20172e9f5954SRashika free_root_extent_buffers(info->quota_root); 20182e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20194273eaffSAnand Jain if (free_chunk_root) 20202e9f5954SRashika free_root_extent_buffers(info->chunk_root); 202170f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2022af31f5e5SChris Mason } 2023af31f5e5SChris Mason 2024faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2025171f6537SJosef Bacik { 2026171f6537SJosef Bacik int ret; 2027171f6537SJosef Bacik struct btrfs_root *gang[8]; 2028171f6537SJosef Bacik int i; 2029171f6537SJosef Bacik 2030171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2031171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2032171f6537SJosef Bacik struct btrfs_root, root_list); 2033171f6537SJosef Bacik list_del(&gang[0]->root_list); 2034171f6537SJosef Bacik 203527cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2036cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2037171f6537SJosef Bacik } else { 2038171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2039171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2040b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2041171f6537SJosef Bacik } 2042171f6537SJosef Bacik } 2043171f6537SJosef Bacik 2044171f6537SJosef Bacik while (1) { 2045171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2046171f6537SJosef Bacik (void **)gang, 0, 2047171f6537SJosef Bacik ARRAY_SIZE(gang)); 2048171f6537SJosef Bacik if (!ret) 2049171f6537SJosef Bacik break; 2050171f6537SJosef Bacik for (i = 0; i < ret; i++) 2051cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2052171f6537SJosef Bacik } 20531a4319ccSLiu Bo 20541a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20551a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 20562ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 20571a4319ccSLiu Bo } 2058171f6537SJosef Bacik } 2059af31f5e5SChris Mason 2060638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2061638aa7edSEric Sandeen { 2062638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2063638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2064638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2065638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2066638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2067638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2068ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2069638aa7edSEric Sandeen } 2070638aa7edSEric Sandeen 2071779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2072779a65a4SEric Sandeen { 2073779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2074779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2075779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2076779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2077779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2078779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2079779a65a4SEric Sandeen } 2080779a65a4SEric Sandeen 20816bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2082f37938e0SEric Sandeen { 20832ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 20842ff7e61eSJeff Mahoney 20852ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 20862ff7e61eSJeff Mahoney set_nlink(inode, 1); 2087f37938e0SEric Sandeen /* 2088f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2089f37938e0SEric Sandeen * the real end of the address space is determined by all of 2090f37938e0SEric Sandeen * the devices in the system 2091f37938e0SEric Sandeen */ 20922ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 20932ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2094f37938e0SEric Sandeen 20952ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 209643eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 209743eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 20987b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 20992ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2100f37938e0SEric Sandeen 21012ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2102f37938e0SEric Sandeen 21032ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21042ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21052ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21062ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2107f37938e0SEric Sandeen } 2108f37938e0SEric Sandeen 2109ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2110ad618368SEric Sandeen { 2111ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2112129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21137f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2114ad618368SEric Sandeen } 2115ad618368SEric Sandeen 2116f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2117f9e92e40SEric Sandeen { 2118f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2119f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2120f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2121f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2122f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2123f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2124d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2125f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2126f9e92e40SEric Sandeen } 2127f9e92e40SEric Sandeen 21282a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21292a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21302a458198SEric Sandeen { 2131f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21326f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21332a458198SEric Sandeen 21342a458198SEric Sandeen fs_info->workers = 2135cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2136cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21372a458198SEric Sandeen 21382a458198SEric Sandeen fs_info->delalloc_workers = 2139cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2140cb001095SJeff Mahoney flags, max_active, 2); 21412a458198SEric Sandeen 21422a458198SEric Sandeen fs_info->flush_workers = 2143cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2144cb001095SJeff Mahoney flags, max_active, 0); 21452a458198SEric Sandeen 21462a458198SEric Sandeen fs_info->caching_workers = 2147cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21482a458198SEric Sandeen 21492a458198SEric Sandeen fs_info->fixup_workers = 2150cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21512a458198SEric Sandeen 21522a458198SEric Sandeen /* 21532a458198SEric Sandeen * endios are largely parallel and should have a very 21542a458198SEric Sandeen * low idle thresh 21552a458198SEric Sandeen */ 21562a458198SEric Sandeen fs_info->endio_workers = 2157cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21582a458198SEric Sandeen fs_info->endio_meta_workers = 2159cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2160cb001095SJeff Mahoney max_active, 4); 21612a458198SEric Sandeen fs_info->endio_meta_write_workers = 2162cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2163cb001095SJeff Mahoney max_active, 2); 21642a458198SEric Sandeen fs_info->endio_raid56_workers = 2165cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2166cb001095SJeff Mahoney max_active, 4); 21672a458198SEric Sandeen fs_info->endio_repair_workers = 2168cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 21692a458198SEric Sandeen fs_info->rmw_workers = 2170cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21712a458198SEric Sandeen fs_info->endio_write_workers = 2172cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2173cb001095SJeff Mahoney max_active, 2); 21742a458198SEric Sandeen fs_info->endio_freespace_worker = 2175cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2176cb001095SJeff Mahoney max_active, 0); 21772a458198SEric Sandeen fs_info->delayed_workers = 2178cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2179cb001095SJeff Mahoney max_active, 0); 21802a458198SEric Sandeen fs_info->readahead_workers = 2181cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2182cb001095SJeff Mahoney max_active, 2); 21832a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2184cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2185b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2186b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 21872a458198SEric Sandeen 21882a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2189ba8a9d07SChris Mason fs_info->flush_workers && 21902a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 21912a458198SEric Sandeen fs_info->endio_meta_write_workers && 21922a458198SEric Sandeen fs_info->endio_repair_workers && 21932a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 21942a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 21952a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 21962a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2197b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2198b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 21992a458198SEric Sandeen return -ENOMEM; 22002a458198SEric Sandeen } 22012a458198SEric Sandeen 22022a458198SEric Sandeen return 0; 22032a458198SEric Sandeen } 22042a458198SEric Sandeen 22056d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22066d97c6e3SJohannes Thumshirn { 22076d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2208b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22096d97c6e3SJohannes Thumshirn 2210b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22116d97c6e3SJohannes Thumshirn 22126d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22136d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2214b4e967beSDavid Sterba csum_driver); 22156d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22166d97c6e3SJohannes Thumshirn } 22176d97c6e3SJohannes Thumshirn 22186d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22196d97c6e3SJohannes Thumshirn 22206d97c6e3SJohannes Thumshirn return 0; 22216d97c6e3SJohannes Thumshirn } 22226d97c6e3SJohannes Thumshirn 222363443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 222463443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 222563443bf5SEric Sandeen { 222663443bf5SEric Sandeen int ret; 222763443bf5SEric Sandeen struct btrfs_root *log_tree_root; 222863443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 222963443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2230581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 223163443bf5SEric Sandeen 223263443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2233f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 223463443bf5SEric Sandeen return -EIO; 223563443bf5SEric Sandeen } 223663443bf5SEric Sandeen 223796dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 223896dfcb46SJosef Bacik GFP_KERNEL); 223963443bf5SEric Sandeen if (!log_tree_root) 224063443bf5SEric Sandeen return -ENOMEM; 224163443bf5SEric Sandeen 22422ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2243581c1760SQu Wenruo fs_info->generation + 1, 2244581c1760SQu Wenruo level, NULL); 224564c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2246f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22470eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 224881f096edSJosef Bacik btrfs_put_fs_root(log_tree_root); 22490eeff236SLiu Bo return ret; 225064c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2251f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 225263443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 225381f096edSJosef Bacik btrfs_put_fs_root(log_tree_root); 225463443bf5SEric Sandeen return -EIO; 225563443bf5SEric Sandeen } 225663443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 225763443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 225863443bf5SEric Sandeen if (ret) { 22590b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 226063443bf5SEric Sandeen "Failed to recover log tree"); 226163443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 226281f096edSJosef Bacik btrfs_put_fs_root(log_tree_root); 226363443bf5SEric Sandeen return ret; 226463443bf5SEric Sandeen } 226563443bf5SEric Sandeen 2266bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22676bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 226863443bf5SEric Sandeen if (ret) 226963443bf5SEric Sandeen return ret; 227063443bf5SEric Sandeen } 227163443bf5SEric Sandeen 227263443bf5SEric Sandeen return 0; 227363443bf5SEric Sandeen } 227463443bf5SEric Sandeen 22756bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 22764bbcaa64SEric Sandeen { 22776bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2278a4f3d2c4SDavid Sterba struct btrfs_root *root; 22794bbcaa64SEric Sandeen struct btrfs_key location; 22804bbcaa64SEric Sandeen int ret; 22814bbcaa64SEric Sandeen 22826bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 22836bccf3abSJeff Mahoney 22844bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 22854bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 22864bbcaa64SEric Sandeen location.offset = 0; 22874bbcaa64SEric Sandeen 2288a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2289f50f4353SLiu Bo if (IS_ERR(root)) { 2290f50f4353SLiu Bo ret = PTR_ERR(root); 2291f50f4353SLiu Bo goto out; 2292f50f4353SLiu Bo } 2293a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2294a4f3d2c4SDavid Sterba fs_info->extent_root = root; 22954bbcaa64SEric Sandeen 22964bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2297a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2298f50f4353SLiu Bo if (IS_ERR(root)) { 2299f50f4353SLiu Bo ret = PTR_ERR(root); 2300f50f4353SLiu Bo goto out; 2301f50f4353SLiu Bo } 2302a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2303a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23044bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23054bbcaa64SEric Sandeen 23064bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2307a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2308f50f4353SLiu Bo if (IS_ERR(root)) { 2309f50f4353SLiu Bo ret = PTR_ERR(root); 2310f50f4353SLiu Bo goto out; 2311f50f4353SLiu Bo } 2312a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2313a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23144bbcaa64SEric Sandeen 23154bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2316a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2317a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2318a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2319afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2320a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23214bbcaa64SEric Sandeen } 23224bbcaa64SEric Sandeen 23234bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2324a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2325a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2326a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23274bbcaa64SEric Sandeen if (ret != -ENOENT) 2328f50f4353SLiu Bo goto out; 23294bbcaa64SEric Sandeen } else { 2330a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2331a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23324bbcaa64SEric Sandeen } 23334bbcaa64SEric Sandeen 233470f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 233570f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 233670f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2337f50f4353SLiu Bo if (IS_ERR(root)) { 2338f50f4353SLiu Bo ret = PTR_ERR(root); 2339f50f4353SLiu Bo goto out; 2340f50f4353SLiu Bo } 234170f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 234270f6d82eSOmar Sandoval fs_info->free_space_root = root; 234370f6d82eSOmar Sandoval } 234470f6d82eSOmar Sandoval 23454bbcaa64SEric Sandeen return 0; 2346f50f4353SLiu Bo out: 2347f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2348f50f4353SLiu Bo location.objectid, ret); 2349f50f4353SLiu Bo return ret; 23504bbcaa64SEric Sandeen } 23514bbcaa64SEric Sandeen 2352069ec957SQu Wenruo /* 2353069ec957SQu Wenruo * Real super block validation 2354069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2355069ec957SQu Wenruo * 2356069ec957SQu Wenruo * @sb: super block to check 2357069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2358069ec957SQu Wenruo * 0 the primary (1st) sb 2359069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2360069ec957SQu Wenruo * -1 skip bytenr check 2361069ec957SQu Wenruo */ 2362069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2363069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 236421a852b0SQu Wenruo { 236521a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 236621a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 236721a852b0SQu Wenruo int ret = 0; 236821a852b0SQu Wenruo 236921a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 237021a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 237121a852b0SQu Wenruo ret = -EINVAL; 237221a852b0SQu Wenruo } 237321a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 237421a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 237521a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 237621a852b0SQu Wenruo ret = -EINVAL; 237721a852b0SQu Wenruo } 237821a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 237921a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 238021a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 238121a852b0SQu Wenruo ret = -EINVAL; 238221a852b0SQu Wenruo } 238321a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 238421a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 238521a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 238621a852b0SQu Wenruo ret = -EINVAL; 238721a852b0SQu Wenruo } 238821a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 238921a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 239021a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 239121a852b0SQu Wenruo ret = -EINVAL; 239221a852b0SQu Wenruo } 239321a852b0SQu Wenruo 239421a852b0SQu Wenruo /* 239521a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 239621a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 239721a852b0SQu Wenruo */ 239821a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 239921a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 240021a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 240121a852b0SQu Wenruo ret = -EINVAL; 240221a852b0SQu Wenruo } 240321a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 240421a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 240521a852b0SQu Wenruo btrfs_err(fs_info, 240621a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 240721a852b0SQu Wenruo sectorsize, PAGE_SIZE); 240821a852b0SQu Wenruo ret = -EINVAL; 240921a852b0SQu Wenruo } 241021a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 241121a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 241221a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 241321a852b0SQu Wenruo ret = -EINVAL; 241421a852b0SQu Wenruo } 241521a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 241621a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 241721a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 241821a852b0SQu Wenruo ret = -EINVAL; 241921a852b0SQu Wenruo } 242021a852b0SQu Wenruo 242121a852b0SQu Wenruo /* Root alignment check */ 242221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 242321a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 242421a852b0SQu Wenruo btrfs_super_root(sb)); 242521a852b0SQu Wenruo ret = -EINVAL; 242621a852b0SQu Wenruo } 242721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 242821a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 242921a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 243021a852b0SQu Wenruo ret = -EINVAL; 243121a852b0SQu Wenruo } 243221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 243321a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 243421a852b0SQu Wenruo btrfs_super_log_root(sb)); 243521a852b0SQu Wenruo ret = -EINVAL; 243621a852b0SQu Wenruo } 243721a852b0SQu Wenruo 2438de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24397239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 244021a852b0SQu Wenruo btrfs_err(fs_info, 24417239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2442de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 244321a852b0SQu Wenruo ret = -EINVAL; 244421a852b0SQu Wenruo } 244521a852b0SQu Wenruo 244621a852b0SQu Wenruo /* 244721a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 244821a852b0SQu Wenruo * done later 244921a852b0SQu Wenruo */ 245021a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 245121a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 245221a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 245321a852b0SQu Wenruo ret = -EINVAL; 245421a852b0SQu Wenruo } 245521a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 245621a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 245721a852b0SQu Wenruo btrfs_super_stripesize(sb)); 245821a852b0SQu Wenruo ret = -EINVAL; 245921a852b0SQu Wenruo } 246021a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 246121a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 246221a852b0SQu Wenruo btrfs_super_num_devices(sb)); 246321a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 246421a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 246521a852b0SQu Wenruo ret = -EINVAL; 246621a852b0SQu Wenruo } 246721a852b0SQu Wenruo 2468069ec957SQu Wenruo if (mirror_num >= 0 && 2469069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 247021a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 247121a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 247221a852b0SQu Wenruo ret = -EINVAL; 247321a852b0SQu Wenruo } 247421a852b0SQu Wenruo 247521a852b0SQu Wenruo /* 247621a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 247721a852b0SQu Wenruo * and one chunk 247821a852b0SQu Wenruo */ 247921a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 248021a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 248121a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 248221a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 248321a852b0SQu Wenruo ret = -EINVAL; 248421a852b0SQu Wenruo } 248521a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 248621a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 248721a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 248821a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 248921a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 249021a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 249121a852b0SQu Wenruo ret = -EINVAL; 249221a852b0SQu Wenruo } 249321a852b0SQu Wenruo 249421a852b0SQu Wenruo /* 249521a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 249621a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 249721a852b0SQu Wenruo */ 249821a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 249921a852b0SQu Wenruo btrfs_warn(fs_info, 250021a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 250121a852b0SQu Wenruo btrfs_super_generation(sb), 250221a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 250321a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 250421a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 250521a852b0SQu Wenruo btrfs_warn(fs_info, 250621a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 250721a852b0SQu Wenruo btrfs_super_generation(sb), 250821a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 250921a852b0SQu Wenruo 251021a852b0SQu Wenruo return ret; 251121a852b0SQu Wenruo } 251221a852b0SQu Wenruo 2513069ec957SQu Wenruo /* 2514069ec957SQu Wenruo * Validation of super block at mount time. 2515069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2516069ec957SQu Wenruo * flags will be skipped. 2517069ec957SQu Wenruo */ 2518069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2519069ec957SQu Wenruo { 2520069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2521069ec957SQu Wenruo } 2522069ec957SQu Wenruo 252375cb857dSQu Wenruo /* 252475cb857dSQu Wenruo * Validation of super block at write time. 252575cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 252675cb857dSQu Wenruo * overwritten soon. 252775cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 252875cb857dSQu Wenruo */ 252975cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 253075cb857dSQu Wenruo struct btrfs_super_block *sb) 253175cb857dSQu Wenruo { 253275cb857dSQu Wenruo int ret; 253375cb857dSQu Wenruo 253475cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 253575cb857dSQu Wenruo if (ret < 0) 253675cb857dSQu Wenruo goto out; 2537e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 253875cb857dSQu Wenruo ret = -EUCLEAN; 253975cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 254075cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 254175cb857dSQu Wenruo goto out; 254275cb857dSQu Wenruo } 254375cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 254475cb857dSQu Wenruo ret = -EUCLEAN; 254575cb857dSQu Wenruo btrfs_err(fs_info, 254675cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 254775cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 254875cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 254975cb857dSQu Wenruo goto out; 255075cb857dSQu Wenruo } 255175cb857dSQu Wenruo out: 255275cb857dSQu Wenruo if (ret < 0) 255375cb857dSQu Wenruo btrfs_err(fs_info, 255475cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 255575cb857dSQu Wenruo return ret; 255675cb857dSQu Wenruo } 255775cb857dSQu Wenruo 25586ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2559b8522a1eSNikolay Borisov { 25606ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2561b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2562b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2563b8522a1eSNikolay Borisov bool handle_error = false; 2564b8522a1eSNikolay Borisov int ret = 0; 2565b8522a1eSNikolay Borisov int i; 2566b8522a1eSNikolay Borisov 2567b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2568b8522a1eSNikolay Borisov u64 generation; 2569b8522a1eSNikolay Borisov int level; 2570b8522a1eSNikolay Borisov 2571b8522a1eSNikolay Borisov if (handle_error) { 2572b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2573b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2574b8522a1eSNikolay Borisov tree_root->node = NULL; 2575b8522a1eSNikolay Borisov 2576b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2577b8522a1eSNikolay Borisov break; 2578b8522a1eSNikolay Borisov 2579b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2580b8522a1eSNikolay Borisov 2581b8522a1eSNikolay Borisov /* 2582b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2583b8522a1eSNikolay Borisov * valid 2584b8522a1eSNikolay Borisov */ 2585b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2586b8522a1eSNikolay Borisov 2587b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2588b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2589b8522a1eSNikolay Borisov 2590b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 25916ef108ddSNikolay Borisov backup_index = ret; 2592b8522a1eSNikolay Borisov if (ret < 0) 2593b8522a1eSNikolay Borisov return ret; 2594b8522a1eSNikolay Borisov } 2595b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2596b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2597b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 2598b8522a1eSNikolay Borisov generation, level, NULL); 2599b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node) || 2600b8522a1eSNikolay Borisov !extent_buffer_uptodate(tree_root->node)) { 2601b8522a1eSNikolay Borisov handle_error = true; 2602b8522a1eSNikolay Borisov 2603b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node)) 2604b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 2605b8522a1eSNikolay Borisov else if (!extent_buffer_uptodate(tree_root->node)) 2606b8522a1eSNikolay Borisov ret = -EUCLEAN; 2607b8522a1eSNikolay Borisov 2608b8522a1eSNikolay Borisov btrfs_warn(fs_info, "failed to read tree root"); 2609b8522a1eSNikolay Borisov continue; 2610b8522a1eSNikolay Borisov } 2611b8522a1eSNikolay Borisov 2612b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2613b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2614b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2615b8522a1eSNikolay Borisov 2616336a0d8dSNikolay Borisov /* 2617336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2618336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2619336a0d8dSNikolay Borisov */ 2620b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2621b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2622b8522a1eSNikolay Borisov if (ret < 0) { 2623b8522a1eSNikolay Borisov handle_error = true; 2624b8522a1eSNikolay Borisov continue; 2625b8522a1eSNikolay Borisov } 2626b8522a1eSNikolay Borisov 2627b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2628b8522a1eSNikolay Borisov 2629b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2630b8522a1eSNikolay Borisov if (ret < 0) { 2631b8522a1eSNikolay Borisov handle_error = true; 2632b8522a1eSNikolay Borisov continue; 2633b8522a1eSNikolay Borisov } 2634b8522a1eSNikolay Borisov 2635b8522a1eSNikolay Borisov /* All successful */ 2636b8522a1eSNikolay Borisov fs_info->generation = generation; 2637b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26386ef108ddSNikolay Borisov 26396ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26406ef108ddSNikolay Borisov if (backup_index < 0) { 26416ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26426ef108ddSNikolay Borisov } else { 26436ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26446ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26456ef108ddSNikolay Borisov } 2646b8522a1eSNikolay Borisov break; 2647b8522a1eSNikolay Borisov } 2648b8522a1eSNikolay Borisov 2649b8522a1eSNikolay Borisov return ret; 2650b8522a1eSNikolay Borisov } 2651b8522a1eSNikolay Borisov 2652b105e927SDavid Sterba int __cold open_ctree(struct super_block *sb, 2653dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2654dfe25020SChris Mason char *options) 2655eb60ceacSChris Mason { 2656db94535dSChris Mason u32 sectorsize; 2657db94535dSChris Mason u32 nodesize; 265887ee04ebSChris Mason u32 stripesize; 265984234f3aSYan Zheng u64 generation; 2660f2b636e8SJosef Bacik u64 features; 266151bce6c9SJohannes Thumshirn u16 csum_type; 26623de4586cSChris Mason struct btrfs_key location; 2663a061fc8dSChris Mason struct buffer_head *bh; 26644d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2665815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2666f84a8bd6SAl Viro struct btrfs_root *tree_root; 26674d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2668eb60ceacSChris Mason int ret; 2669e58ca020SYan int err = -EINVAL; 26706675df31SOmar Sandoval int clear_free_space_tree = 0; 2671581c1760SQu Wenruo int level; 26724543df7eSChris Mason 267396dfcb46SJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 267496dfcb46SJosef Bacik GFP_KERNEL); 267596dfcb46SJosef Bacik fs_info->tree_root = tree_root; 267696dfcb46SJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 267796dfcb46SJosef Bacik GFP_KERNEL); 267896dfcb46SJosef Bacik fs_info->chunk_root = chunk_root; 2679cb517eabSMiao Xie if (!tree_root || !chunk_root) { 268039279cc3SChris Mason err = -ENOMEM; 268139279cc3SChris Mason goto fail; 268239279cc3SChris Mason } 268376dda93cSYan, Zheng 268476dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 268576dda93cSYan, Zheng if (ret) { 268676dda93cSYan, Zheng err = ret; 268776dda93cSYan, Zheng goto fail; 268876dda93cSYan, Zheng } 268976dda93cSYan, Zheng 26904297ff84SJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2691e2d84521SMiao Xie if (ret) { 2692e2d84521SMiao Xie err = ret; 26939e11ceeeSJan Kara goto fail_srcu; 2694e2d84521SMiao Xie } 26954297ff84SJosef Bacik 26964297ff84SJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 26974297ff84SJosef Bacik if (ret) { 26984297ff84SJosef Bacik err = ret; 2699141386e1SJosef Bacik goto fail_srcu; 27004297ff84SJosef Bacik } 270109cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2702e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2703e2d84521SMiao Xie 2704908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2705963d678bSMiao Xie if (ret) { 2706963d678bSMiao Xie err = ret; 2707141386e1SJosef Bacik goto fail_srcu; 2708963d678bSMiao Xie } 2709963d678bSMiao Xie 27107f8d236aSDavid Sterba ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 27117f8d236aSDavid Sterba GFP_KERNEL); 2712c404e0dcSMiao Xie if (ret) { 2713c404e0dcSMiao Xie err = ret; 2714141386e1SJosef Bacik goto fail_srcu; 2715c404e0dcSMiao Xie } 2716c404e0dcSMiao Xie 271776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2718f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 27198fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2720facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 272124bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2722eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 272311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2724eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2725a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 272676dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 272724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 27284cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2729ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2730f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 273147ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2732f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2733d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 273467c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 27357585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2736573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2737de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 273819c00ddcSChris Mason 27390b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27406324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2741f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 274247ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2743c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 274466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 274566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 274666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 274766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 274866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 274966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 275066d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2751ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2752ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2753ba2c4d4eSJosef Bacik 2754771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27554cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27562fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2757034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2758fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2759e20d96d6SChris Mason fs_info->sb = sb; 276095ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27619ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27624cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2763a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2764f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27658b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2766f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 276790519d66SArne Jansen /* readahead state */ 2768d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 276990519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2770fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2771c8b97818SChris Mason 2772b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2773b34b086cSChris Mason num_online_cpus() + 2, 8); 27740afbaf8cSChris Mason 2775199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2776199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 277769fe2d75SJosef Bacik 277869fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 277969fe2d75SJosef Bacik if (!fs_info->btree_inode) { 278069fe2d75SJosef Bacik err = -ENOMEM; 2781141386e1SJosef Bacik goto fail_srcu; 278269fe2d75SJosef Bacik } 278369fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 278469fe2d75SJosef Bacik 278516cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 278674e4d827SDavid Sterba GFP_KERNEL); 278716cdcec7SMiao Xie if (!fs_info->delayed_root) { 278816cdcec7SMiao Xie err = -ENOMEM; 278916cdcec7SMiao Xie goto fail_iput; 279016cdcec7SMiao Xie } 279116cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 27923eaa2885SChris Mason 2793638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 279421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 279521adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 279621adbd5cSStefan Behrens #endif 2797779a65a4SEric Sandeen btrfs_init_balance(fs_info); 279821c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2799a2de733cSArne Jansen 28009f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28019f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2802a061fc8dSChris Mason 28036bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 280439279cc3SChris Mason 2805e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 28066bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2807a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2808925baeddSChris Mason 280943eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[0], 281043eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS0, NULL); 281143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[1], 281243eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS1, NULL); 281311833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2814afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2815509659cdSChris Mason 28165a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 281779154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2818925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2819a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2820a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 28211bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 28229e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2823c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 282476dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2825803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2826fa9c0d79SChris Mason 2827ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2828f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2829b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2830416ac51dSArne Jansen 2831fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2832fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2833fa9c0d79SChris Mason 2834e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2835f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2836bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 28374854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2838034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28393768f368SChris Mason 2840da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2841da17066cSJeff Mahoney fs_info->nodesize = 4096; 2842da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2843da17066cSJeff Mahoney fs_info->stripesize = 4096; 2844da17066cSJeff Mahoney 2845eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2846eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2847eede2bf3SOmar Sandoval 28489e967495SFilipe Manana fs_info->send_in_progress = 0; 28499e967495SFilipe Manana 285053b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 285153b381b3SDavid Woodhouse if (ret) { 285283c8266aSDavid Sterba err = ret; 285353b381b3SDavid Woodhouse goto fail_alloc; 285453b381b3SDavid Woodhouse } 285553b381b3SDavid Woodhouse 28563c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 28571104a885SDavid Sterba 28581104a885SDavid Sterba /* 28591104a885SDavid Sterba * Read super block and check the signature bytes only 28601104a885SDavid Sterba */ 2861a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 286292fc03fbSAnand Jain if (IS_ERR(bh)) { 286392fc03fbSAnand Jain err = PTR_ERR(bh); 286416cdcec7SMiao Xie goto fail_alloc; 286520b45077SDave Young } 286639279cc3SChris Mason 28671104a885SDavid Sterba /* 28688dc3f22cSJohannes Thumshirn * Verify the type first, if that or the the checksum value are 28698dc3f22cSJohannes Thumshirn * corrupted, we'll find out 28708dc3f22cSJohannes Thumshirn */ 287151bce6c9SJohannes Thumshirn csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data); 287251bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 28738dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 287451bce6c9SJohannes Thumshirn csum_type); 28758dc3f22cSJohannes Thumshirn err = -EINVAL; 28768dc3f22cSJohannes Thumshirn brelse(bh); 28778dc3f22cSJohannes Thumshirn goto fail_alloc; 28788dc3f22cSJohannes Thumshirn } 28798dc3f22cSJohannes Thumshirn 28806d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 28816d97c6e3SJohannes Thumshirn if (ret) { 28826d97c6e3SJohannes Thumshirn err = ret; 28836d97c6e3SJohannes Thumshirn goto fail_alloc; 28846d97c6e3SJohannes Thumshirn } 28856d97c6e3SJohannes Thumshirn 28868dc3f22cSJohannes Thumshirn /* 28871104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 28881104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 28891104a885SDavid Sterba */ 2890ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 289105135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 28921104a885SDavid Sterba err = -EINVAL; 2893b2acdddfSAnand Jain brelse(bh); 2894141386e1SJosef Bacik goto fail_alloc; 28951104a885SDavid Sterba } 28961104a885SDavid Sterba 28971104a885SDavid Sterba /* 28981104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 28991104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29001104a885SDavid Sterba * the whole block of INFO_SIZE 29011104a885SDavid Sterba */ 29026c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 2903a061fc8dSChris Mason brelse(bh); 29045f39d397SChris Mason 2905fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2906fbc6feaeSNikolay Borisov 2907de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2908de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2909de37aa51SNikolay Borisov 29107239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2911de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2912de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2913de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29147239ff4bSNikolay Borisov } 29150b86a832SChris Mason 2916fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2917fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2918fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2919fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2920fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2921fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2922fbc6feaeSNikolay Borisov } 2923fbc6feaeSNikolay Borisov 2924fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2925fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2926de37aa51SNikolay Borisov 2927069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 29281104a885SDavid Sterba if (ret) { 292905135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 29301104a885SDavid Sterba err = -EINVAL; 2931141386e1SJosef Bacik goto fail_alloc; 29321104a885SDavid Sterba } 29331104a885SDavid Sterba 29340f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 2935141386e1SJosef Bacik goto fail_alloc; 29360f7d52f4SChris Mason 2937acce952bSliubo /* check FS state, whether FS is broken. */ 293887533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 293987533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2940acce952bSliubo 294175e7cb7fSLiu Bo /* 294275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 294375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 294475e7cb7fSLiu Bo */ 294575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 294675e7cb7fSLiu Bo 29472ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 29482b82032cSYan Zheng if (ret) { 29492b82032cSYan Zheng err = ret; 2950141386e1SJosef Bacik goto fail_alloc; 29512b82032cSYan Zheng } 2952dfe25020SChris Mason 2953f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2954f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2955f2b636e8SJosef Bacik if (features) { 295605135f59SDavid Sterba btrfs_err(fs_info, 295705135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2958c1c9ff7cSGeert Uytterhoeven features); 2959f2b636e8SJosef Bacik err = -EINVAL; 2960141386e1SJosef Bacik goto fail_alloc; 2961f2b636e8SJosef Bacik } 2962f2b636e8SJosef Bacik 29635d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 29645d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 29650b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2966a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 29675c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 29685c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2969727011e0SChris Mason 29703173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 297105135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 29723173a18fSJosef Bacik 2973727011e0SChris Mason /* 2974727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2975727011e0SChris Mason * they are bigger than the page size 2976727011e0SChris Mason */ 297709cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2978727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 297905135f59SDavid Sterba btrfs_info(fs_info, 298005135f59SDavid Sterba "flagging fs with big metadata feature"); 2981727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2982727011e0SChris Mason } 2983727011e0SChris Mason 2984bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2985bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2986b7f67055SChandan Rajendra stripesize = sectorsize; 2987707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2988963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2989bc3f116fSChris Mason 2990da17066cSJeff Mahoney /* Cache block sizes */ 2991da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2992da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2993da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2994da17066cSJeff Mahoney 2995bc3f116fSChris Mason /* 2996bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2997bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2998bc3f116fSChris Mason */ 2999bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3000707e8a07SDavid Sterba (sectorsize != nodesize)) { 300105135f59SDavid Sterba btrfs_err(fs_info, 300205135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 300305135f59SDavid Sterba nodesize, sectorsize); 3004141386e1SJosef Bacik goto fail_alloc; 3005bc3f116fSChris Mason } 3006bc3f116fSChris Mason 3007ceda0864SMiao Xie /* 3008ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3009ceda0864SMiao Xie * update the flag. 3010ceda0864SMiao Xie */ 30115d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 30125d4f98a2SYan Zheng 3013f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3014f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3015bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 301605135f59SDavid Sterba btrfs_err(fs_info, 301705135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3018c1c9ff7cSGeert Uytterhoeven features); 3019f2b636e8SJosef Bacik err = -EINVAL; 3020141386e1SJosef Bacik goto fail_alloc; 3021f2b636e8SJosef Bacik } 302261d92c32SChris Mason 30232a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 30242a458198SEric Sandeen if (ret) { 30252a458198SEric Sandeen err = ret; 30260dc3b84aSJosef Bacik goto fail_sb_buffer; 30270dc3b84aSJosef Bacik } 30284543df7eSChris Mason 30299e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 30309e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 30319e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 3032b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 30339e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 30349e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 30354575c9ccSChris Mason 3036a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3037a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3038de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3039db94535dSChris Mason 3040925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 30416bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3042925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 304384eed90fSChris Mason if (ret) { 304405135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 30455d4f98a2SYan Zheng goto fail_sb_buffer; 304684eed90fSChris Mason } 30470b86a832SChris Mason 304884234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3049581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 30500b86a832SChris Mason 30512ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 30520b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3053581c1760SQu Wenruo generation, level, NULL); 305464c043deSLiu Bo if (IS_ERR(chunk_root->node) || 305564c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 305605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3057e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3058e5fffbacSchandan free_extent_buffer(chunk_root->node); 305995ab1f64SZhao Lei chunk_root->node = NULL; 3060af31f5e5SChris Mason goto fail_tree_roots; 306183121942SDavid Woodhouse } 30625d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 30635d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 30640b86a832SChris Mason 3065e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3066b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 3067e17cade2SChris Mason 30685b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 30692b82032cSYan Zheng if (ret) { 307005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3071af31f5e5SChris Mason goto fail_tree_roots; 30722b82032cSYan Zheng } 30730b86a832SChris Mason 30748dabb742SStefan Behrens /* 30759b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 30769b99b115SAnand Jain * device replace procedure 30778dabb742SStefan Behrens */ 30789b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3079dfe25020SChris Mason 3080a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 308105135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3082a6b0d5c8SChris Mason goto fail_tree_roots; 3083a6b0d5c8SChris Mason } 3084a6b0d5c8SChris Mason 3085b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 30864bbcaa64SEric Sandeen if (ret) 3087b8522a1eSNikolay Borisov goto fail_tree_roots; 30888929ecfaSYan, Zheng 3089cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3090cf90d884SQu Wenruo if (ret) { 3091cf90d884SQu Wenruo btrfs_err(fs_info, 3092cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3093cf90d884SQu Wenruo ret); 3094cf90d884SQu Wenruo goto fail_block_groups; 3095cf90d884SQu Wenruo } 309668310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 309768310a5eSIlya Dryomov if (ret) { 309805135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 309968310a5eSIlya Dryomov goto fail_block_groups; 310068310a5eSIlya Dryomov } 310168310a5eSIlya Dryomov 3102733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3103733f4fbbSStefan Behrens if (ret) { 310405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3105733f4fbbSStefan Behrens goto fail_block_groups; 3106733f4fbbSStefan Behrens } 3107733f4fbbSStefan Behrens 31088dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 31098dabb742SStefan Behrens if (ret) { 311005135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 31118dabb742SStefan Behrens goto fail_block_groups; 31128dabb742SStefan Behrens } 31138dabb742SStefan Behrens 31149b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 31158dabb742SStefan Behrens 3116c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3117b7c35e81SAnand Jain if (ret) { 311805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 311905135f59SDavid Sterba ret); 3120b7c35e81SAnand Jain goto fail_block_groups; 3121b7c35e81SAnand Jain } 3122b7c35e81SAnand Jain 312396f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31245ac1d209SJeff Mahoney if (ret) { 312505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3126b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31275ac1d209SJeff Mahoney } 31285ac1d209SJeff Mahoney 3129c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3130c59021f8Sliubo if (ret) { 313105135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31322365dd3cSAnand Jain goto fail_sysfs; 3133c59021f8Sliubo } 3134c59021f8Sliubo 31355b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31361b1d1f66SJosef Bacik if (ret) { 313705135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 31382365dd3cSAnand Jain goto fail_sysfs; 31391b1d1f66SJosef Bacik } 31404330e183SQu Wenruo 31416528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 314205135f59SDavid Sterba btrfs_warn(fs_info, 314352042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 31442365dd3cSAnand Jain goto fail_sysfs; 3145292fd7fcSStefan Behrens } 31469078a3e1SChris Mason 3147a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3148a74a4b97SChris Mason "btrfs-cleaner"); 314957506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 31502365dd3cSAnand Jain goto fail_sysfs; 3151a74a4b97SChris Mason 3152a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3153a74a4b97SChris Mason tree_root, 3154a74a4b97SChris Mason "btrfs-transaction"); 315557506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 31563f157a2fSChris Mason goto fail_cleaner; 3157a74a4b97SChris Mason 3158583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3159c289811cSChris Mason !fs_info->fs_devices->rotating) { 3160583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3161c289811cSChris Mason } 3162c289811cSChris Mason 3163572d9ab7SDavid Sterba /* 316401327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3165572d9ab7SDavid Sterba * not have to wait for transaction commit 3166572d9ab7SDavid Sterba */ 3167572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 31683818aea2SQu Wenruo 316921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 31700b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 31712ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 31720b246afaSJeff Mahoney btrfs_test_opt(fs_info, 317321adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 317421adbd5cSStefan Behrens 1 : 0, 317521adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 317621adbd5cSStefan Behrens if (ret) 317705135f59SDavid Sterba btrfs_warn(fs_info, 317805135f59SDavid Sterba "failed to initialize integrity check module: %d", 317905135f59SDavid Sterba ret); 318021adbd5cSStefan Behrens } 318121adbd5cSStefan Behrens #endif 3182bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3183bcef60f2SArne Jansen if (ret) 3184bcef60f2SArne Jansen goto fail_trans_kthread; 318521adbd5cSStefan Behrens 3186fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3187fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3188fd708b81SJosef Bacik 318996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 319096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 31910b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3192e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 319363443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 319479787eaaSJeff Mahoney if (ret) { 319563443bf5SEric Sandeen err = ret; 319628c16cbbSWang Shilong goto fail_qgroup; 3197e556ce2cSYan Zheng } 3198e02119d5SChris Mason } 31991a40e23bSZheng Yan 32006bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 320179787eaaSJeff Mahoney if (ret) 320228c16cbbSWang Shilong goto fail_qgroup; 320376dda93cSYan, Zheng 3204bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3205d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 320644c44af2SIlya Dryomov if (ret) 320728c16cbbSWang Shilong goto fail_qgroup; 320890c711abSZygo Blaxell 320990c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 32105d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 321190c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3212d7ce5843SMiao Xie if (ret < 0) { 321305135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 321405135f59SDavid Sterba ret); 3215d7ce5843SMiao Xie err = -EINVAL; 3216bcef60f2SArne Jansen goto fail_qgroup; 3217d7ce5843SMiao Xie } 32187c2ca468SChris Mason } 32191a40e23bSZheng Yan 32203de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32213de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3222cb517eabSMiao Xie location.offset = 0; 32233de4586cSChris Mason 32243619c94fSJosef Bacik fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true); 32253140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32263140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3227f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3228315bf8efSJosef Bacik fs_info->fs_root = NULL; 3229bcef60f2SArne Jansen goto fail_qgroup; 32303140c9a3SDan Carpenter } 3231c289811cSChris Mason 3232bc98a42cSDavid Howells if (sb_rdonly(sb)) 32332b6ba629SIlya Dryomov return 0; 32342b6ba629SIlya Dryomov 3235f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3236f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32376675df31SOmar Sandoval clear_free_space_tree = 1; 32386675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32396675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32406675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 32416675df31SOmar Sandoval clear_free_space_tree = 1; 32426675df31SOmar Sandoval } 32436675df31SOmar Sandoval 32446675df31SOmar Sandoval if (clear_free_space_tree) { 3245f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3246f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3247f8d468a1SOmar Sandoval if (ret) { 3248f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3249f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 32506bccf3abSJeff Mahoney close_ctree(fs_info); 3251f8d468a1SOmar Sandoval return ret; 3252f8d468a1SOmar Sandoval } 3253f8d468a1SOmar Sandoval } 3254f8d468a1SOmar Sandoval 32550b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3256511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 325705135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3258511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3259511711afSChris Mason if (ret) { 326005135f59SDavid Sterba btrfs_warn(fs_info, 326105135f59SDavid Sterba "failed to create free space tree: %d", ret); 32626bccf3abSJeff Mahoney close_ctree(fs_info); 3263511711afSChris Mason return ret; 3264511711afSChris Mason } 3265511711afSChris Mason } 3266511711afSChris Mason 3267e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 32682b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 32692b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 32702b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 32716bccf3abSJeff Mahoney close_ctree(fs_info); 32722b6ba629SIlya Dryomov return ret; 32732b6ba629SIlya Dryomov } 3274e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 327559641015SIlya Dryomov 32762b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 32772b6ba629SIlya Dryomov if (ret) { 327805135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 32796bccf3abSJeff Mahoney close_ctree(fs_info); 32802b6ba629SIlya Dryomov return ret; 3281e3acc2a6SJosef Bacik } 3282e3acc2a6SJosef Bacik 32838dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 32848dabb742SStefan Behrens if (ret) { 328505135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 32866bccf3abSJeff Mahoney close_ctree(fs_info); 32878dabb742SStefan Behrens return ret; 32888dabb742SStefan Behrens } 32898dabb742SStefan Behrens 3290b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3291b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3292b382a324SJan Schmidt 32934bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 329405135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3295f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3296f7a81ea4SStefan Behrens if (ret) { 329705135f59SDavid Sterba btrfs_warn(fs_info, 329805135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 32996bccf3abSJeff Mahoney close_ctree(fs_info); 3300f7a81ea4SStefan Behrens return ret; 3301f7a81ea4SStefan Behrens } 33020b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33034bbcaa64SEric Sandeen fs_info->generation != 33044bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 330505135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 330670f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 330770f80175SStefan Behrens if (ret) { 330805135f59SDavid Sterba btrfs_warn(fs_info, 330905135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 33106bccf3abSJeff Mahoney close_ctree(fs_info); 331170f80175SStefan Behrens return ret; 331270f80175SStefan Behrens } 331370f80175SStefan Behrens } else { 3314afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3315f7a81ea4SStefan Behrens } 3316afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 331747ab2a6cSJosef Bacik 33188dcddfa0SQu Wenruo /* 33198dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33208dcddfa0SQu Wenruo * no need to keep the flag 33218dcddfa0SQu Wenruo */ 33228dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33238dcddfa0SQu Wenruo 3324ad2b2c80SAl Viro return 0; 332539279cc3SChris Mason 3326bcef60f2SArne Jansen fail_qgroup: 3327bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33287c2ca468SChris Mason fail_trans_kthread: 33297c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33302ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3331faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33323f157a2fSChris Mason fail_cleaner: 3333a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33347c2ca468SChris Mason 33357c2ca468SChris Mason /* 33367c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 33377c2ca468SChris Mason * kthreads 33387c2ca468SChris Mason */ 33397c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 33407c2ca468SChris Mason 33412365dd3cSAnand Jain fail_sysfs: 33426618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 33432365dd3cSAnand Jain 3344b7c35e81SAnand Jain fail_fsdev_sysfs: 3345b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3346b7c35e81SAnand Jain 33471b1d1f66SJosef Bacik fail_block_groups: 334854067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3349af31f5e5SChris Mason 3350af31f5e5SChris Mason fail_tree_roots: 33514273eaffSAnand Jain free_root_pointers(fs_info, true); 33522b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3353af31f5e5SChris Mason 335439279cc3SChris Mason fail_sb_buffer: 33557abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 33565cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 335716cdcec7SMiao Xie fail_alloc: 33584543df7eSChris Mason fail_iput: 3359586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3360586e46e2SIlya Dryomov 33614543df7eSChris Mason iput(fs_info->btree_inode); 336276dda93cSYan, Zheng fail_srcu: 336376dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 33647e662854SQinghuang Feng fail: 3365586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3366ad2b2c80SAl Viro return err; 3367eb60ceacSChris Mason } 3368663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3369eb60ceacSChris Mason 3370f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3371f2984462SChris Mason { 3372f2984462SChris Mason if (uptodate) { 3373f2984462SChris Mason set_buffer_uptodate(bh); 3374f2984462SChris Mason } else { 3375442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3376442a4f63SStefan Behrens bh->b_private; 3377442a4f63SStefan Behrens 3378fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3379b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3380606686eeSJosef Bacik rcu_str_deref(device->name)); 338101327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 33821259ab75SChris Mason * our own ways of dealing with the IO errors 33831259ab75SChris Mason */ 3384f2984462SChris Mason clear_buffer_uptodate(bh); 3385442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3386f2984462SChris Mason } 3387f2984462SChris Mason unlock_buffer(bh); 3388f2984462SChris Mason put_bh(bh); 3389f2984462SChris Mason } 3390f2984462SChris Mason 339129c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 339229c36d72SAnand Jain struct buffer_head **bh_ret) 339329c36d72SAnand Jain { 339429c36d72SAnand Jain struct buffer_head *bh; 339529c36d72SAnand Jain struct btrfs_super_block *super; 339629c36d72SAnand Jain u64 bytenr; 339729c36d72SAnand Jain 339829c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 339929c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 340029c36d72SAnand Jain return -EINVAL; 340129c36d72SAnand Jain 34029f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 340329c36d72SAnand Jain /* 340429c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 340529c36d72SAnand Jain * the best option we have is to mark it EIO. 340629c36d72SAnand Jain */ 340729c36d72SAnand Jain if (!bh) 340829c36d72SAnand Jain return -EIO; 340929c36d72SAnand Jain 341029c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 341129c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 341229c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 341329c36d72SAnand Jain brelse(bh); 341429c36d72SAnand Jain return -EINVAL; 341529c36d72SAnand Jain } 341629c36d72SAnand Jain 341729c36d72SAnand Jain *bh_ret = bh; 341829c36d72SAnand Jain return 0; 341929c36d72SAnand Jain } 342029c36d72SAnand Jain 342129c36d72SAnand Jain 3422a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3423a512bbf8SYan Zheng { 3424a512bbf8SYan Zheng struct buffer_head *bh; 3425a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3426a512bbf8SYan Zheng struct btrfs_super_block *super; 3427a512bbf8SYan Zheng int i; 3428a512bbf8SYan Zheng u64 transid = 0; 342992fc03fbSAnand Jain int ret = -EINVAL; 3430a512bbf8SYan Zheng 3431a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3432a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3433a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3434a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3435a512bbf8SYan Zheng */ 3436a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 343729c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 343829c36d72SAnand Jain if (ret) 3439a512bbf8SYan Zheng continue; 3440a512bbf8SYan Zheng 3441a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3442a512bbf8SYan Zheng 3443a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3444a512bbf8SYan Zheng brelse(latest); 3445a512bbf8SYan Zheng latest = bh; 3446a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3447a512bbf8SYan Zheng } else { 3448a512bbf8SYan Zheng brelse(bh); 3449a512bbf8SYan Zheng } 3450a512bbf8SYan Zheng } 345192fc03fbSAnand Jain 345292fc03fbSAnand Jain if (!latest) 345392fc03fbSAnand Jain return ERR_PTR(ret); 345492fc03fbSAnand Jain 3455a512bbf8SYan Zheng return latest; 3456a512bbf8SYan Zheng } 3457a512bbf8SYan Zheng 34584eedeb75SHisashi Hifumi /* 3459abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3460abbb3b8eSDavid Sterba * buffer heads we write are pinned. 34614eedeb75SHisashi Hifumi * 3462abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3463abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3464abbb3b8eSDavid Sterba * same for write and wait phases. 34654eedeb75SHisashi Hifumi * 3466abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 34674eedeb75SHisashi Hifumi */ 3468a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3469abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3470a512bbf8SYan Zheng { 3471d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3472d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3473a512bbf8SYan Zheng struct buffer_head *bh; 3474a512bbf8SYan Zheng int i; 3475a512bbf8SYan Zheng int ret; 3476a512bbf8SYan Zheng int errors = 0; 3477a512bbf8SYan Zheng u64 bytenr; 34781b9e619cSOmar Sandoval int op_flags; 3479a512bbf8SYan Zheng 3480a512bbf8SYan Zheng if (max_mirrors == 0) 3481a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3482a512bbf8SYan Zheng 3483d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3484d5178578SJohannes Thumshirn 3485a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3486a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3487935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3488935e5cc9SMiao Xie device->commit_total_bytes) 3489a512bbf8SYan Zheng break; 3490a512bbf8SYan Zheng 3491a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3492a512bbf8SYan Zheng 3493d5178578SJohannes Thumshirn crypto_shash_init(shash); 3494d5178578SJohannes Thumshirn crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3495abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3496d5178578SJohannes Thumshirn crypto_shash_final(shash, sb->csum); 3497a512bbf8SYan Zheng 3498abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 34999f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3500a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3501634554dcSJosef Bacik if (!bh) { 3502fb456252SJeff Mahoney btrfs_err(device->fs_info, 3503f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3504f14d104dSDavid Sterba bytenr); 3505634554dcSJosef Bacik errors++; 3506634554dcSJosef Bacik continue; 3507634554dcSJosef Bacik } 3508634554dcSJosef Bacik 3509a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3510a512bbf8SYan Zheng 35114eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3512a512bbf8SYan Zheng get_bh(bh); 35134eedeb75SHisashi Hifumi 35144eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3515a512bbf8SYan Zheng lock_buffer(bh); 3516a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3517442a4f63SStefan Behrens bh->b_private = device; 3518a512bbf8SYan Zheng 3519387125fcSChris Mason /* 3520387125fcSChris Mason * we fua the first super. The others we allow 3521387125fcSChris Mason * to go down lazy. 3522387125fcSChris Mason */ 35231b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 35241b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 35251b9e619cSOmar Sandoval op_flags |= REQ_FUA; 35261b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 35274eedeb75SHisashi Hifumi if (ret) 3528a512bbf8SYan Zheng errors++; 3529a512bbf8SYan Zheng } 3530a512bbf8SYan Zheng return errors < i ? 0 : -1; 3531a512bbf8SYan Zheng } 3532a512bbf8SYan Zheng 3533387125fcSChris Mason /* 3534abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3535abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3536abbb3b8eSDavid Sterba * 3537abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3538abbb3b8eSDavid Sterba * date. 3539abbb3b8eSDavid Sterba */ 3540abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3541abbb3b8eSDavid Sterba { 3542abbb3b8eSDavid Sterba struct buffer_head *bh; 3543abbb3b8eSDavid Sterba int i; 3544abbb3b8eSDavid Sterba int errors = 0; 3545b6a535faSHoward McLauchlan bool primary_failed = false; 3546abbb3b8eSDavid Sterba u64 bytenr; 3547abbb3b8eSDavid Sterba 3548abbb3b8eSDavid Sterba if (max_mirrors == 0) 3549abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3550abbb3b8eSDavid Sterba 3551abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3552abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3553abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3554abbb3b8eSDavid Sterba device->commit_total_bytes) 3555abbb3b8eSDavid Sterba break; 3556abbb3b8eSDavid Sterba 35579f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 35589f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3559abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3560abbb3b8eSDavid Sterba if (!bh) { 3561abbb3b8eSDavid Sterba errors++; 3562b6a535faSHoward McLauchlan if (i == 0) 3563b6a535faSHoward McLauchlan primary_failed = true; 3564abbb3b8eSDavid Sterba continue; 3565abbb3b8eSDavid Sterba } 3566abbb3b8eSDavid Sterba wait_on_buffer(bh); 3567b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3568abbb3b8eSDavid Sterba errors++; 3569b6a535faSHoward McLauchlan if (i == 0) 3570b6a535faSHoward McLauchlan primary_failed = true; 3571b6a535faSHoward McLauchlan } 3572abbb3b8eSDavid Sterba 3573abbb3b8eSDavid Sterba /* drop our reference */ 3574abbb3b8eSDavid Sterba brelse(bh); 3575abbb3b8eSDavid Sterba 3576abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3577abbb3b8eSDavid Sterba brelse(bh); 3578abbb3b8eSDavid Sterba } 3579abbb3b8eSDavid Sterba 3580b6a535faSHoward McLauchlan /* log error, force error return */ 3581b6a535faSHoward McLauchlan if (primary_failed) { 3582b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3583b6a535faSHoward McLauchlan device->devid); 3584b6a535faSHoward McLauchlan return -1; 3585b6a535faSHoward McLauchlan } 3586b6a535faSHoward McLauchlan 3587abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3588abbb3b8eSDavid Sterba } 3589abbb3b8eSDavid Sterba 3590abbb3b8eSDavid Sterba /* 3591387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3592387125fcSChris Mason * for the barrier when it is done 3593387125fcSChris Mason */ 35944246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3595387125fcSChris Mason { 3596387125fcSChris Mason complete(bio->bi_private); 3597387125fcSChris Mason } 3598387125fcSChris Mason 3599387125fcSChris Mason /* 36004fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36014fc6441aSAnand Jain * done in the waiting counterpart. 3602387125fcSChris Mason */ 36034fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3604387125fcSChris Mason { 3605c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3606e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3607387125fcSChris Mason 3608c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36094fc6441aSAnand Jain return; 3610387125fcSChris Mason 3611e0ae9994SDavid Sterba bio_reset(bio); 3612387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 361374d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36148d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3615387125fcSChris Mason init_completion(&device->flush_wait); 3616387125fcSChris Mason bio->bi_private = &device->flush_wait; 3617387125fcSChris Mason 361843a01111SLu Fengqi btrfsic_submit_bio(bio); 36191c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36204fc6441aSAnand Jain } 3621387125fcSChris Mason 36224fc6441aSAnand Jain /* 36234fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36244fc6441aSAnand Jain */ 36258c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36264fc6441aSAnand Jain { 36274fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36284fc6441aSAnand Jain 36291c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 363058efbc9fSOmar Sandoval return BLK_STS_OK; 36314fc6441aSAnand Jain 36321c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36332980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 36344fc6441aSAnand Jain 36358c27cb35SLinus Torvalds return bio->bi_status; 3636387125fcSChris Mason } 3637387125fcSChris Mason 3638d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3639401b41e5SAnand Jain { 36406528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3641401b41e5SAnand Jain return -EIO; 3642387125fcSChris Mason return 0; 3643387125fcSChris Mason } 3644387125fcSChris Mason 3645387125fcSChris Mason /* 3646387125fcSChris Mason * send an empty flush down to each device in parallel, 3647387125fcSChris Mason * then wait for them 3648387125fcSChris Mason */ 3649387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3650387125fcSChris Mason { 3651387125fcSChris Mason struct list_head *head; 3652387125fcSChris Mason struct btrfs_device *dev; 36535af3e8ccSStefan Behrens int errors_wait = 0; 36544e4cbee9SChristoph Hellwig blk_status_t ret; 3655387125fcSChris Mason 36561538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3657387125fcSChris Mason /* send down all the barriers */ 3658387125fcSChris Mason head = &info->fs_devices->devices; 36591538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3660e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3661f88ba6a2SHidetoshi Seto continue; 3662cea7c8bfSAnand Jain if (!dev->bdev) 3663387125fcSChris Mason continue; 3664e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3665ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3666387125fcSChris Mason continue; 3667387125fcSChris Mason 36684fc6441aSAnand Jain write_dev_flush(dev); 366958efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3670387125fcSChris Mason } 3671387125fcSChris Mason 3672387125fcSChris Mason /* wait for all the barriers */ 36731538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3674e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3675f88ba6a2SHidetoshi Seto continue; 3676387125fcSChris Mason if (!dev->bdev) { 36775af3e8ccSStefan Behrens errors_wait++; 3678387125fcSChris Mason continue; 3679387125fcSChris Mason } 3680e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3681ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3682387125fcSChris Mason continue; 3683387125fcSChris Mason 36844fc6441aSAnand Jain ret = wait_dev_flush(dev); 3685401b41e5SAnand Jain if (ret) { 3686401b41e5SAnand Jain dev->last_flush_error = ret; 368766b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 368866b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 36895af3e8ccSStefan Behrens errors_wait++; 3690387125fcSChris Mason } 3691401b41e5SAnand Jain } 3692401b41e5SAnand Jain 3693cea7c8bfSAnand Jain if (errors_wait) { 3694401b41e5SAnand Jain /* 3695401b41e5SAnand Jain * At some point we need the status of all disks 3696401b41e5SAnand Jain * to arrive at the volume status. So error checking 3697401b41e5SAnand Jain * is being pushed to a separate loop. 3698401b41e5SAnand Jain */ 3699d10b82feSQu Wenruo return check_barrier_error(info); 3700401b41e5SAnand Jain } 3701387125fcSChris Mason return 0; 3702387125fcSChris Mason } 3703387125fcSChris Mason 3704943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3705943c6e99SZhao Lei { 37068789f4feSZhao Lei int raid_type; 37078789f4feSZhao Lei int min_tolerated = INT_MAX; 3708943c6e99SZhao Lei 37098789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37108789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37118c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37128789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37138789f4feSZhao Lei tolerated_failures); 3714943c6e99SZhao Lei 37158789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37168789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37178789f4feSZhao Lei continue; 371841a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37198789f4feSZhao Lei continue; 37208c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37218789f4feSZhao Lei btrfs_raid_array[raid_type]. 37228789f4feSZhao Lei tolerated_failures); 37238789f4feSZhao Lei } 3724943c6e99SZhao Lei 37258789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3726ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37278789f4feSZhao Lei min_tolerated = 0; 37288789f4feSZhao Lei } 37298789f4feSZhao Lei 37308789f4feSZhao Lei return min_tolerated; 3731943c6e99SZhao Lei } 3732943c6e99SZhao Lei 3733eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3734f2984462SChris Mason { 3735e5e9a520SChris Mason struct list_head *head; 3736f2984462SChris Mason struct btrfs_device *dev; 3737a061fc8dSChris Mason struct btrfs_super_block *sb; 3738f2984462SChris Mason struct btrfs_dev_item *dev_item; 3739f2984462SChris Mason int ret; 3740f2984462SChris Mason int do_barriers; 3741a236aed1SChris Mason int max_errors; 3742a236aed1SChris Mason int total_errors = 0; 3743a061fc8dSChris Mason u64 flags; 3744f2984462SChris Mason 37450b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3746fed3b381SLiu Bo 3747fed3b381SLiu Bo /* 3748fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3749fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3750fed3b381SLiu Bo * been consistent on disk. 3751fed3b381SLiu Bo */ 3752fed3b381SLiu Bo if (max_mirrors == 0) 37530b246afaSJeff Mahoney backup_super_roots(fs_info); 3754f2984462SChris Mason 37550b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3756a061fc8dSChris Mason dev_item = &sb->dev_item; 3757e5e9a520SChris Mason 37580b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37590b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37600b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3761387125fcSChris Mason 37625af3e8ccSStefan Behrens if (do_barriers) { 37630b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37645af3e8ccSStefan Behrens if (ret) { 37655af3e8ccSStefan Behrens mutex_unlock( 37660b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 37670b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 37685af3e8ccSStefan Behrens "errors while submitting device barriers."); 37695af3e8ccSStefan Behrens return ret; 37705af3e8ccSStefan Behrens } 37715af3e8ccSStefan Behrens } 3772387125fcSChris Mason 37731538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3774dfe25020SChris Mason if (!dev->bdev) { 3775dfe25020SChris Mason total_errors++; 3776dfe25020SChris Mason continue; 3777dfe25020SChris Mason } 3778e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3779ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3780dfe25020SChris Mason continue; 3781dfe25020SChris Mason 37822b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3783a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3784a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 37857df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3786935e5cc9SMiao Xie dev->commit_total_bytes); 3787ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3788ce7213c7SMiao Xie dev->commit_bytes_used); 3789a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3790a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3791a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3792a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 37937239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 37947239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3795a512bbf8SYan Zheng 3796a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3797a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3798f2984462SChris Mason 379975cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 380075cb857dSQu Wenruo if (ret < 0) { 380175cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 380275cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 380375cb857dSQu Wenruo "unexpected superblock corruption detected"); 380475cb857dSQu Wenruo return -EUCLEAN; 380575cb857dSQu Wenruo } 380675cb857dSQu Wenruo 3807abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3808a236aed1SChris Mason if (ret) 3809a236aed1SChris Mason total_errors++; 3810f2984462SChris Mason } 3811a236aed1SChris Mason if (total_errors > max_errors) { 38120b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3813d397712bSChris Mason total_errors); 38140b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 381579787eaaSJeff Mahoney 38169d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38170b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38180b246afaSJeff Mahoney "%d errors while writing supers", 38190b246afaSJeff Mahoney total_errors); 38209d565ba4SStefan Behrens return -EIO; 3821a236aed1SChris Mason } 3822f2984462SChris Mason 3823a512bbf8SYan Zheng total_errors = 0; 38241538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3825dfe25020SChris Mason if (!dev->bdev) 3826dfe25020SChris Mason continue; 3827e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3828ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3829dfe25020SChris Mason continue; 3830dfe25020SChris Mason 3831abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3832a512bbf8SYan Zheng if (ret) 38331259ab75SChris Mason total_errors++; 3834f2984462SChris Mason } 38350b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3836a236aed1SChris Mason if (total_errors > max_errors) { 38370b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38380b246afaSJeff Mahoney "%d errors while writing supers", 38390b246afaSJeff Mahoney total_errors); 384079787eaaSJeff Mahoney return -EIO; 3841a236aed1SChris Mason } 3842f2984462SChris Mason return 0; 3843f2984462SChris Mason } 3844f2984462SChris Mason 3845cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3846cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3847cb517eabSMiao Xie struct btrfs_root *root) 38482619ba1fSChris Mason { 38494df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 38502619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 38512619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3852af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 3853af01d2e5SJosef Bacik btrfs_put_fs_root(root); 38544df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 385576dda93cSYan, Zheng 385676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 385776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 385876dda93cSYan, Zheng 38591c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 38603321719eSLiu Bo btrfs_free_log(NULL, root); 38611c1ea4f7SLiu Bo if (root->reloc_root) { 38621c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38631c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 38641c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 38651c1ea4f7SLiu Bo root->reloc_root = NULL; 38661c1ea4f7SLiu Bo } 38671c1ea4f7SLiu Bo } 38683321719eSLiu Bo 3869faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3870581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3871faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3872581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 387384db5ccfSDavid Sterba btrfs_free_fs_root(root); 38744df27c4dSYan, Zheng } 38754df27c4dSYan, Zheng 387684db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 38774df27c4dSYan, Zheng { 387857cdc8dbSDavid Sterba iput(root->ino_cache_inode); 38794df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 38800ee5dc67SAl Viro if (root->anon_dev) 38810ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 38828257b2dcSMiao Xie if (root->subv_writers) 38838257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 38845f39d397SChris Mason free_extent_buffer(root->node); 38855f39d397SChris Mason free_extent_buffer(root->commit_root); 3886581bb050SLi Zefan kfree(root->free_ino_ctl); 3887581bb050SLi Zefan kfree(root->free_ino_pinned); 3888b0feb9d9SMiao Xie btrfs_put_fs_root(root); 38892619ba1fSChris Mason } 38902619ba1fSChris Mason 3891c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3892c146afadSYan Zheng { 3893c146afadSYan Zheng u64 root_objectid = 0; 3894c146afadSYan Zheng struct btrfs_root *gang[8]; 389565d33fd7SQu Wenruo int i = 0; 389665d33fd7SQu Wenruo int err = 0; 389765d33fd7SQu Wenruo unsigned int ret = 0; 389865d33fd7SQu Wenruo int index; 3899c146afadSYan Zheng 3900c146afadSYan Zheng while (1) { 390165d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3902c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3903c146afadSYan Zheng (void **)gang, root_objectid, 3904c146afadSYan Zheng ARRAY_SIZE(gang)); 390565d33fd7SQu Wenruo if (!ret) { 390665d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3907c146afadSYan Zheng break; 390865d33fd7SQu Wenruo } 39095d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 391066b4ffd1SJosef Bacik 391165d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 391265d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 391365d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 391465d33fd7SQu Wenruo gang[i] = NULL; 391565d33fd7SQu Wenruo continue; 391665d33fd7SQu Wenruo } 391765d33fd7SQu Wenruo /* grab all the search result for later use */ 391865d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 391965d33fd7SQu Wenruo } 392065d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 392165d33fd7SQu Wenruo 392265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 392365d33fd7SQu Wenruo if (!gang[i]) 392465d33fd7SQu Wenruo continue; 3925c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 392666b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 392766b4ffd1SJosef Bacik if (err) 392865d33fd7SQu Wenruo break; 392965d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3930c146afadSYan Zheng } 3931c146afadSYan Zheng root_objectid++; 3932c146afadSYan Zheng } 393365d33fd7SQu Wenruo 393465d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 393565d33fd7SQu Wenruo for (; i < ret; i++) { 393665d33fd7SQu Wenruo if (gang[i]) 393765d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 393865d33fd7SQu Wenruo } 393965d33fd7SQu Wenruo return err; 3940c146afadSYan Zheng } 3941c146afadSYan Zheng 39426bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3943c146afadSYan Zheng { 39446bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3945c146afadSYan Zheng struct btrfs_trans_handle *trans; 3946c146afadSYan Zheng 39470b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39482ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39490b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39500b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3951c71bf099SYan, Zheng 3952c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39530b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39540b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3955c71bf099SYan, Zheng 39567a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39573612b495STsutomu Itoh if (IS_ERR(trans)) 39583612b495STsutomu Itoh return PTR_ERR(trans); 39593a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3960c146afadSYan Zheng } 3961c146afadSYan Zheng 3962b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 3963eb60ceacSChris Mason { 3964c146afadSYan Zheng int ret; 3965e089f05cSChris Mason 3966afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3967d6fd0ae2SOmar Sandoval /* 3968d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 3969d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 3970d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 3971d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 3972d6fd0ae2SOmar Sandoval */ 3973d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 3974a2135011SChris Mason 39757343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3976d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 39777343dd61SJustin Maggard 3978803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3979803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3980803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3981803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3982803b2f54SStefan Behrens 3983837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3984aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3985837d5b6eSIlya Dryomov 39868dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 39878dabb742SStefan Behrens 3988aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 39894cb5300bSChris Mason 39904cb5300bSChris Mason /* wait for any defraggers to finish */ 39914cb5300bSChris Mason wait_event(fs_info->transaction_wait, 39924cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 39934cb5300bSChris Mason 39944cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 399526176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 39964cb5300bSChris Mason 399721c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 399821c7e756SMiao Xie 3999b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4000b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4001b0643e59SDennis Zhou 4002bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4003e44163e1SJeff Mahoney /* 4004d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4005d6fd0ae2SOmar Sandoval * unused block groups. 4006e44163e1SJeff Mahoney */ 40070b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4008e44163e1SJeff Mahoney 40096bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4010d397712bSChris Mason if (ret) 401104892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4012c146afadSYan Zheng } 4013ed2ff2cbSChris Mason 4014af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4015af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40162ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4017acce952bSliubo 4018e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4019e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40208929ecfaSYan, Zheng 4021e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4022afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4023f25784b3SYan Zheng 402404892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4025fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4026bcef60f2SArne Jansen 4027963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 402804892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4029963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4030b0c68f8bSChris Mason } 403131153d81SYan Zheng 40324297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 40334297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 40344297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 40354297ff84SJosef Bacik 40366618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4037b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 40385ac1d209SJeff Mahoney 4039faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4040d10c5f31SChris Mason 40411a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 40421a4319ccSLiu Bo 4043de348ee0SWang Shilong /* 4044de348ee0SWang Shilong * we must make sure there is not any read request to 4045de348ee0SWang Shilong * submit after we stopping all workers. 4046de348ee0SWang Shilong */ 4047de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 404896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 404996192499SJosef Bacik 4050afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 40514273eaffSAnand Jain free_root_pointers(fs_info, true); 40529ad6b7bcSChris Mason 40534e19443dSJosef Bacik /* 40544e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 40554e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 40564e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 40574e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 40584e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 40594e19443dSJosef Bacik */ 40604e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 40614e19443dSJosef Bacik 406213e6c37bSJosef Bacik iput(fs_info->btree_inode); 4063d6bfde87SChris Mason 406421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40650b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 40662ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 406721adbd5cSStefan Behrens #endif 406821adbd5cSStefan Behrens 40690b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 407068c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 407176dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 4072eb60ceacSChris Mason } 4073eb60ceacSChris Mason 4074b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4075b9fab919SChris Mason int atomic) 4076ccd467d6SChris Mason { 40771259ab75SChris Mason int ret; 4078727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 40791259ab75SChris Mason 40800b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 40811259ab75SChris Mason if (!ret) 40821259ab75SChris Mason return ret; 40831259ab75SChris Mason 40841259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4085b9fab919SChris Mason parent_transid, atomic); 4086b9fab919SChris Mason if (ret == -EAGAIN) 4087b9fab919SChris Mason return ret; 40881259ab75SChris Mason return !ret; 40895f39d397SChris Mason } 40906702ed49SChris Mason 40915f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 40925f39d397SChris Mason { 40930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 409406ea65a3SJosef Bacik struct btrfs_root *root; 40955f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4096b9473439SChris Mason int was_dirty; 4097b4ce94deSChris Mason 409806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 409906ea65a3SJosef Bacik /* 410006ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 410152042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 410206ea65a3SJosef Bacik * outside of the sanity tests. 410306ea65a3SJosef Bacik */ 4104b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 410506ea65a3SJosef Bacik return; 410606ea65a3SJosef Bacik #endif 410706ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41080b246afaSJeff Mahoney fs_info = root->fs_info; 4109b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41100b246afaSJeff Mahoney if (transid != fs_info->generation) 41115d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41120b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41130b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4114e2d84521SMiao Xie if (!was_dirty) 4115104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4116e2d84521SMiao Xie buf->len, 41170b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41181f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 411969fc6cbbSQu Wenruo /* 412069fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 412169fc6cbbSQu Wenruo * but item data not updated. 412269fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 412369fc6cbbSQu Wenruo */ 412469fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4125cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4126a4f78750SDavid Sterba btrfs_print_leaf(buf); 41271f21ef0aSFilipe Manana ASSERT(0); 41281f21ef0aSFilipe Manana } 41291f21ef0aSFilipe Manana #endif 4130eb60ceacSChris Mason } 4131eb60ceacSChris Mason 41322ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4133b53d3f5dSLiu Bo int flush_delayed) 413435b7e476SChris Mason { 4135188de649SChris Mason /* 4136188de649SChris Mason * looks as though older kernels can get into trouble with 4137188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4138188de649SChris Mason */ 4139e2d84521SMiao Xie int ret; 4140d6bfde87SChris Mason 41416933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4142d6bfde87SChris Mason return; 4143d6bfde87SChris Mason 4144b53d3f5dSLiu Bo if (flush_delayed) 41452ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 414616cdcec7SMiao Xie 4147d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4148d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4149d814a491SEthan Lien fs_info->dirty_metadata_batch); 4150e2d84521SMiao Xie if (ret > 0) { 41510b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 415216cdcec7SMiao Xie } 415316cdcec7SMiao Xie } 415416cdcec7SMiao Xie 41552ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 415616cdcec7SMiao Xie { 41572ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 415835b7e476SChris Mason } 4159b53d3f5dSLiu Bo 41602ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4161b53d3f5dSLiu Bo { 41622ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4163d6bfde87SChris Mason } 41646b80053dSChris Mason 4165581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4166581c1760SQu Wenruo struct btrfs_key *first_key) 41676b80053dSChris Mason { 41685ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4169581c1760SQu Wenruo level, first_key); 41706b80053dSChris Mason } 41710da5468fSChris Mason 41722ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4173acce952bSliubo { 4174fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4175fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4176fe816d0fSNikolay Borisov 41770b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 41782ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 41790b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4180acce952bSliubo 41810b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 41820b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4183acce952bSliubo } 4184acce952bSliubo 4185143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4186acce952bSliubo { 4187acce952bSliubo struct btrfs_ordered_extent *ordered; 4188acce952bSliubo 4189199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4190779880efSJosef Bacik /* 4191779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4192779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4193779880efSJosef Bacik */ 4194199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4195779880efSJosef Bacik root_extent_list) 4196779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4197199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4198199c2a9cSMiao Xie } 4199199c2a9cSMiao Xie 4200199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4201199c2a9cSMiao Xie { 4202199c2a9cSMiao Xie struct btrfs_root *root; 4203199c2a9cSMiao Xie struct list_head splice; 4204199c2a9cSMiao Xie 4205199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4206199c2a9cSMiao Xie 4207199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4208199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4209199c2a9cSMiao Xie while (!list_empty(&splice)) { 4210199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4211199c2a9cSMiao Xie ordered_root); 42121de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42131de2cfdeSJosef Bacik &fs_info->ordered_roots); 4214199c2a9cSMiao Xie 42152a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4216199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4217199c2a9cSMiao Xie 42182a85d9caSLiu Bo cond_resched(); 42192a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4220199c2a9cSMiao Xie } 4221199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 422274d5d229SJosef Bacik 422374d5d229SJosef Bacik /* 422474d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 422574d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 422674d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 422774d5d229SJosef Bacik * actually get run and error out properly. 422874d5d229SJosef Bacik */ 422974d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4230acce952bSliubo } 4231acce952bSliubo 423235a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 42332ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4234acce952bSliubo { 4235acce952bSliubo struct rb_node *node; 4236acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4237acce952bSliubo struct btrfs_delayed_ref_node *ref; 4238acce952bSliubo int ret = 0; 4239acce952bSliubo 4240acce952bSliubo delayed_refs = &trans->delayed_refs; 4241acce952bSliubo 4242acce952bSliubo spin_lock(&delayed_refs->lock); 4243d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4244cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 42450b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4246acce952bSliubo return ret; 4247acce952bSliubo } 4248acce952bSliubo 42495c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4250d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 42510e0adbcfSJosef Bacik struct rb_node *n; 4252e78417d1SJosef Bacik bool pin_bytes = false; 4253acce952bSliubo 4254d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4255d7df2c79SJosef Bacik href_node); 42563069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4257b939d1abSJosef Bacik continue; 42583069bd26SJosef Bacik 4259d7df2c79SJosef Bacik spin_lock(&head->lock); 4260e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 42610e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 42620e0adbcfSJosef Bacik ref_node); 4263d7df2c79SJosef Bacik ref->in_tree = 0; 4264e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 42650e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 42661d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 42671d57ee94SWang Xiaoguang list_del(&ref->add_list); 4268d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4269d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4270d7df2c79SJosef Bacik } 427154067ae9SJosef Bacik if (head->must_insert_reserved) 4272e78417d1SJosef Bacik pin_bytes = true; 427378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4274fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4275d7df2c79SJosef Bacik spin_unlock(&head->lock); 4276acce952bSliubo spin_unlock(&delayed_refs->lock); 4277e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4278acce952bSliubo 4279d7df2c79SJosef Bacik if (pin_bytes) 4280d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4281d278850eSJosef Bacik head->num_bytes, 1); 428231890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4283d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4284acce952bSliubo cond_resched(); 4285acce952bSliubo spin_lock(&delayed_refs->lock); 4286acce952bSliubo } 428781f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4288acce952bSliubo 4289acce952bSliubo spin_unlock(&delayed_refs->lock); 4290acce952bSliubo 4291acce952bSliubo return ret; 4292acce952bSliubo } 4293acce952bSliubo 4294143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4295acce952bSliubo { 4296acce952bSliubo struct btrfs_inode *btrfs_inode; 4297acce952bSliubo struct list_head splice; 4298acce952bSliubo 4299acce952bSliubo INIT_LIST_HEAD(&splice); 4300acce952bSliubo 4301eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4302eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4303acce952bSliubo 4304acce952bSliubo while (!list_empty(&splice)) { 4305fe816d0fSNikolay Borisov struct inode *inode = NULL; 4306eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4307acce952bSliubo delalloc_inodes); 4308fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4309eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4310acce952bSliubo 4311fe816d0fSNikolay Borisov /* 4312fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4313fe816d0fSNikolay Borisov * meanwhile. 4314fe816d0fSNikolay Borisov */ 4315fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4316fe816d0fSNikolay Borisov if (inode) { 4317fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4318fe816d0fSNikolay Borisov iput(inode); 4319fe816d0fSNikolay Borisov } 4320eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4321acce952bSliubo } 4322eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4323eb73c1b7SMiao Xie } 4324eb73c1b7SMiao Xie 4325eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4326eb73c1b7SMiao Xie { 4327eb73c1b7SMiao Xie struct btrfs_root *root; 4328eb73c1b7SMiao Xie struct list_head splice; 4329eb73c1b7SMiao Xie 4330eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4331eb73c1b7SMiao Xie 4332eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4333eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4334eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4335eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4336eb73c1b7SMiao Xie delalloc_root); 4337eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4338eb73c1b7SMiao Xie BUG_ON(!root); 4339eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4340eb73c1b7SMiao Xie 4341eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4342eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4343eb73c1b7SMiao Xie 4344eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4345eb73c1b7SMiao Xie } 4346eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4347acce952bSliubo } 4348acce952bSliubo 43492ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4350acce952bSliubo struct extent_io_tree *dirty_pages, 4351acce952bSliubo int mark) 4352acce952bSliubo { 4353acce952bSliubo int ret; 4354acce952bSliubo struct extent_buffer *eb; 4355acce952bSliubo u64 start = 0; 4356acce952bSliubo u64 end; 4357acce952bSliubo 4358acce952bSliubo while (1) { 4359acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4360e6138876SJosef Bacik mark, NULL); 4361acce952bSliubo if (ret) 4362acce952bSliubo break; 4363acce952bSliubo 436491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4365acce952bSliubo while (start <= end) { 43660b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 43670b246afaSJeff Mahoney start += fs_info->nodesize; 4368fd8b2b61SJosef Bacik if (!eb) 4369acce952bSliubo continue; 4370fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4371acce952bSliubo 4372fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4373fd8b2b61SJosef Bacik &eb->bflags)) 4374fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4375fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4376acce952bSliubo } 4377acce952bSliubo } 4378acce952bSliubo 4379acce952bSliubo return ret; 4380acce952bSliubo } 4381acce952bSliubo 43822ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4383acce952bSliubo struct extent_io_tree *pinned_extents) 4384acce952bSliubo { 4385acce952bSliubo struct extent_io_tree *unpin; 4386acce952bSliubo u64 start; 4387acce952bSliubo u64 end; 4388acce952bSliubo int ret; 4389ed0eaa14SLiu Bo bool loop = true; 4390acce952bSliubo 4391acce952bSliubo unpin = pinned_extents; 4392ed0eaa14SLiu Bo again: 4393acce952bSliubo while (1) { 43940e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 43950e6ec385SFilipe Manana 4396fcd5e742SLu Fengqi /* 4397fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4398fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4399fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4400fcd5e742SLu Fengqi * the same extent range. 4401fcd5e742SLu Fengqi */ 4402fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4403acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44040e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4405fcd5e742SLu Fengqi if (ret) { 4406fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4407acce952bSliubo break; 4408fcd5e742SLu Fengqi } 4409acce952bSliubo 44100e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44110e6ec385SFilipe Manana free_extent_state(cached_state); 44122ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4413fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4414acce952bSliubo cond_resched(); 4415acce952bSliubo } 4416acce952bSliubo 4417ed0eaa14SLiu Bo if (loop) { 44180b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44190b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4420ed0eaa14SLiu Bo else 44210b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4422ed0eaa14SLiu Bo loop = false; 4423ed0eaa14SLiu Bo goto again; 4424ed0eaa14SLiu Bo } 4425ed0eaa14SLiu Bo 4426acce952bSliubo return 0; 4427acce952bSliubo } 4428acce952bSliubo 442932da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4430c79a1751SLiu Bo { 4431c79a1751SLiu Bo struct inode *inode; 4432c79a1751SLiu Bo 4433c79a1751SLiu Bo inode = cache->io_ctl.inode; 4434c79a1751SLiu Bo if (inode) { 4435c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4436c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4437c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4438c79a1751SLiu Bo iput(inode); 4439c79a1751SLiu Bo } 4440c79a1751SLiu Bo btrfs_put_block_group(cache); 4441c79a1751SLiu Bo } 4442c79a1751SLiu Bo 4443c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 44442ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4445c79a1751SLiu Bo { 444632da5386SDavid Sterba struct btrfs_block_group *cache; 4447c79a1751SLiu Bo 4448c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4449c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4450c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 445132da5386SDavid Sterba struct btrfs_block_group, 4452c79a1751SLiu Bo dirty_list); 4453c79a1751SLiu Bo 4454c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4455c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4456c79a1751SLiu Bo list_del_init(&cache->io_list); 4457c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4458c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4459c79a1751SLiu Bo } 4460c79a1751SLiu Bo 4461c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4462c79a1751SLiu Bo spin_lock(&cache->lock); 4463c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4464c79a1751SLiu Bo spin_unlock(&cache->lock); 4465c79a1751SLiu Bo 4466c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4467c79a1751SLiu Bo btrfs_put_block_group(cache); 4468ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4469c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4470c79a1751SLiu Bo } 4471c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4472c79a1751SLiu Bo 447345ae2c18SNikolay Borisov /* 447445ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 447545ae2c18SNikolay Borisov * to use it without any locking 447645ae2c18SNikolay Borisov */ 4477c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4478c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 447932da5386SDavid Sterba struct btrfs_block_group, 4480c79a1751SLiu Bo io_list); 4481c79a1751SLiu Bo 4482c79a1751SLiu Bo list_del_init(&cache->io_list); 4483c79a1751SLiu Bo spin_lock(&cache->lock); 4484c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4485c79a1751SLiu Bo spin_unlock(&cache->lock); 4486c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4487c79a1751SLiu Bo } 4488c79a1751SLiu Bo } 4489c79a1751SLiu Bo 449049b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 44912ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 449249b25e05SJeff Mahoney { 4493bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4494bbbf7243SNikolay Borisov 44952ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4496c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4497c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4498c79a1751SLiu Bo 4499bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4500bbbf7243SNikolay Borisov post_commit_list) { 4501bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4502bbbf7243SNikolay Borisov } 4503bbbf7243SNikolay Borisov 45042ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 450549b25e05SJeff Mahoney 45064a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45070b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 450849b25e05SJeff Mahoney 45094a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45100b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 451149b25e05SJeff Mahoney 4512ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 451367cde344SMiao Xie 45142ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 451549b25e05SJeff Mahoney EXTENT_DIRTY); 45162ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45170b246afaSJeff Mahoney fs_info->pinned_extents); 451849b25e05SJeff Mahoney 45194a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45204a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 452149b25e05SJeff Mahoney } 452249b25e05SJeff Mahoney 45232ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4524acce952bSliubo { 4525acce952bSliubo struct btrfs_transaction *t; 4526acce952bSliubo 45270b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4528acce952bSliubo 45290b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45300b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 45310b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4532724e2315SJosef Bacik struct btrfs_transaction, list); 4533724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 45349b64f57dSElena Reshetova refcount_inc(&t->use_count); 45350b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45362ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4537724e2315SJosef Bacik btrfs_put_transaction(t); 45380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4539724e2315SJosef Bacik continue; 4540724e2315SJosef Bacik } 45410b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4542724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 45430b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4544724e2315SJosef Bacik /* 4545724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4546724e2315SJosef Bacik * handle open currently for this transaction. 4547724e2315SJosef Bacik */ 4548724e2315SJosef Bacik wait_event(t->writer_wait, 4549724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4550724e2315SJosef Bacik } else { 45510b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4552724e2315SJosef Bacik } 45532ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4554724e2315SJosef Bacik 45550b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45560b246afaSJeff Mahoney if (t == fs_info->running_transaction) 45570b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4558724e2315SJosef Bacik list_del_init(&t->list); 45590b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4560a4abeea4SJosef Bacik 4561724e2315SJosef Bacik btrfs_put_transaction(t); 45622ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 45630b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4564724e2315SJosef Bacik } 45650b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45660b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4567ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4568ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 45692ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 45700b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 45710b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4572acce952bSliubo 4573acce952bSliubo return 0; 4574acce952bSliubo } 4575acce952bSliubo 4576e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 45774d53dddbSDavid Sterba /* mandatory callbacks */ 45780b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 45794d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 45800da5468fSChris Mason }; 4581