1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6e20d96d6SChris Mason #include <linux/fs.h> 7d98237b3SChris Mason #include <linux/blkdev.h> 80f7d52f4SChris Mason #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10ce9adaa5SChris Mason #include <linux/workqueue.h> 11a74a4b97SChris Mason #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13784b4e29SChris Mason #include <linux/migrate.h> 147a36ddecSDavid Sterba #include <linux/ratelimit.h> 156463fe58SStefan Behrens #include <linux/uuid.h> 16803b2f54SStefan Behrens #include <linux/semaphore.h> 17540adea3SMasami Hiramatsu #include <linux/error-injection.h> 189678c543SNikolay Borisov #include <linux/crc32c.h> 19b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 207e75bf3fSDavid Sterba #include <asm/unaligned.h> 216d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 32581bb050SLi Zefan #include "inode-map.h" 3321adbd5cSStefan Behrens #include "check-integrity.h" 34606686eeSJosef Bacik #include "rcu-string.h" 358dabb742SStefan Behrens #include "dev-replace.h" 3653b381b3SDavid Woodhouse #include "raid56.h" 375ac1d209SJeff Mahoney #include "sysfs.h" 38fcebe456SJosef Bacik #include "qgroup.h" 39ebb8765bSAnand Jain #include "compression.h" 40557ea5ddSQu Wenruo #include "tree-checker.h" 41fd708b81SJosef Bacik #include "ref-verify.h" 42aac0023cSJosef Bacik #include "block-group.h" 43b0643e59SDennis Zhou #include "discard.h" 44f603bb94SNikolay Borisov #include "space-info.h" 45eb60ceacSChris Mason 46319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 47319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 48319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 50e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 52319e4d06SQu Wenruo 53e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 548b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 55143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 56acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 58143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 592ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 60acce952bSliubo struct extent_io_tree *dirty_pages, 61acce952bSliubo int mark); 622ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 63acce952bSliubo struct extent_io_tree *pinned_extents); 642ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 66ce9adaa5SChris Mason 67d352ac68SChris Mason /* 6897eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6997eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 70d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 71d352ac68SChris Mason */ 7297eb6b69SDavid Sterba struct btrfs_end_io_wq { 73ce9adaa5SChris Mason struct bio *bio; 74ce9adaa5SChris Mason bio_end_io_t *end_io; 75ce9adaa5SChris Mason void *private; 76ce9adaa5SChris Mason struct btrfs_fs_info *info; 774e4cbee9SChristoph Hellwig blk_status_t status; 78bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 798b712842SChris Mason struct btrfs_work work; 80ce9adaa5SChris Mason }; 810da5468fSChris Mason 8297eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8397eb6b69SDavid Sterba 8497eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8597eb6b69SDavid Sterba { 8697eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8797eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8897eb6b69SDavid Sterba 0, 89fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9097eb6b69SDavid Sterba NULL); 9197eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9297eb6b69SDavid Sterba return -ENOMEM; 9397eb6b69SDavid Sterba return 0; 9497eb6b69SDavid Sterba } 9597eb6b69SDavid Sterba 96e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9797eb6b69SDavid Sterba { 9897eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9997eb6b69SDavid Sterba } 10097eb6b69SDavid Sterba 101141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 102141386e1SJosef Bacik { 103141386e1SJosef Bacik if (fs_info->csum_shash) 104141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 105141386e1SJosef Bacik } 106141386e1SJosef Bacik 107d352ac68SChris Mason /* 108d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 109d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 110d352ac68SChris Mason * just before they are sent down the IO stack. 111d352ac68SChris Mason */ 11244b8bd7eSChris Mason struct async_submit_bio { 113c6100a4bSJosef Bacik void *private_data; 11444b8bd7eSChris Mason struct bio *bio; 115a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11644b8bd7eSChris Mason int mirror_num; 117eaf25d93SChris Mason /* 118eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 119eaf25d93SChris Mason * can't tell us where in the file the bio should go 120eaf25d93SChris Mason */ 121eaf25d93SChris Mason u64 bio_offset; 1228b712842SChris Mason struct btrfs_work work; 1234e4cbee9SChristoph Hellwig blk_status_t status; 12444b8bd7eSChris Mason }; 12544b8bd7eSChris Mason 12685d4e461SChris Mason /* 12785d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12885d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12985d4e461SChris Mason * the level the eb occupies in the tree. 1304008c04aSChris Mason * 13185d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13285d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13385d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1344fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1354fd786e6SMisono Tomohiro * roots have separate keysets. 1364008c04aSChris Mason * 13785d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13885d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13985d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1404008c04aSChris Mason * 14185d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14285d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14385d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14485d4e461SChris Mason * 14585d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14685d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14785d4e461SChris Mason * needs update as well. 1484008c04aSChris Mason */ 1494008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1504008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1514008c04aSChris Mason # error 1524008c04aSChris Mason # endif 15385d4e461SChris Mason 15485d4e461SChris Mason static struct btrfs_lockdep_keyset { 15585d4e461SChris Mason u64 id; /* root objectid */ 15685d4e461SChris Mason const char *name_stem; /* lock name stem */ 15785d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 15885d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 15985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 16085d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 16185d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 16285d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 16385d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 16485d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 16585d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 16660b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 16785d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 16885d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 16985d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 17013fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1716b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 17285d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1734008c04aSChris Mason }; 17485d4e461SChris Mason 17585d4e461SChris Mason void __init btrfs_init_lockdep(void) 17685d4e461SChris Mason { 17785d4e461SChris Mason int i, j; 17885d4e461SChris Mason 17985d4e461SChris Mason /* initialize lockdep class names */ 18085d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 18185d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 18285d4e461SChris Mason 18385d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 18485d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 18585d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 18685d4e461SChris Mason } 18785d4e461SChris Mason } 18885d4e461SChris Mason 18985d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 19085d4e461SChris Mason int level) 19185d4e461SChris Mason { 19285d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19385d4e461SChris Mason 19485d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19585d4e461SChris Mason 19685d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19785d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19885d4e461SChris Mason if (ks->id == objectid) 19985d4e461SChris Mason break; 20085d4e461SChris Mason 20185d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20285d4e461SChris Mason &ks->keys[level], ks->names[level]); 20385d4e461SChris Mason } 20485d4e461SChris Mason 2054008c04aSChris Mason #endif 2064008c04aSChris Mason 207d352ac68SChris Mason /* 208d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 209d352ac68SChris Mason * that covers the entire device 210d352ac68SChris Mason */ 2116af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 21239b07b5dSOmar Sandoval struct page *page, size_t pg_offset, 21339b07b5dSOmar Sandoval u64 start, u64 len) 2145f39d397SChris Mason { 215fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2165f39d397SChris Mason struct extent_map *em; 2175f39d397SChris Mason int ret; 2185f39d397SChris Mason 219890871beSChris Mason read_lock(&em_tree->lock); 220d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 221a061fc8dSChris Mason if (em) { 222890871beSChris Mason read_unlock(&em_tree->lock); 2235f39d397SChris Mason goto out; 224a061fc8dSChris Mason } 225890871beSChris Mason read_unlock(&em_tree->lock); 2267b13b7b1SChris Mason 227172ddd60SDavid Sterba em = alloc_extent_map(); 2285f39d397SChris Mason if (!em) { 2295f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2305f39d397SChris Mason goto out; 2315f39d397SChris Mason } 2325f39d397SChris Mason em->start = 0; 2330afbaf8cSChris Mason em->len = (u64)-1; 234c8b97818SChris Mason em->block_len = (u64)-1; 2355f39d397SChris Mason em->block_start = 0; 236d1310b2eSChris Mason 237890871beSChris Mason write_lock(&em_tree->lock); 23809a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2395f39d397SChris Mason if (ret == -EEXIST) { 2405f39d397SChris Mason free_extent_map(em); 2417b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 242b4f359abSTsutomu Itoh if (!em) 2430433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2445f39d397SChris Mason } else if (ret) { 2457b13b7b1SChris Mason free_extent_map(em); 2460433f20dSTsutomu Itoh em = ERR_PTR(ret); 2475f39d397SChris Mason } 248890871beSChris Mason write_unlock(&em_tree->lock); 2497b13b7b1SChris Mason 2505f39d397SChris Mason out: 2515f39d397SChris Mason return em; 2525f39d397SChris Mason } 2535f39d397SChris Mason 254d352ac68SChris Mason /* 2552996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 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 { 98171ad38b4SDavid Sterba return extent_read_full_page(page, btree_get_extent, 0); 9825f39d397SChris Mason } 9835f39d397SChris Mason 98470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9855f39d397SChris Mason { 98698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 98798509cfcSChris Mason return 0; 9880c4e538bSDavid Sterba 989f7a52a40SDavid Sterba return try_release_extent_buffer(page); 990d98237b3SChris Mason } 991d98237b3SChris Mason 992d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 993d47992f8SLukas Czerner unsigned int length) 994d98237b3SChris Mason { 995d1310b2eSChris Mason struct extent_io_tree *tree; 996d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9975f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9985f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9999ad6b7bcSChris Mason if (PagePrivate(page)) { 1000efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1001efe120a0SFrank Holton "page private not zero on page %llu", 1002efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10039ad6b7bcSChris Mason ClearPagePrivate(page); 10049ad6b7bcSChris Mason set_page_private(page, 0); 100509cbfeafSKirill A. Shutemov put_page(page); 10069ad6b7bcSChris Mason } 1007d98237b3SChris Mason } 1008d98237b3SChris Mason 10090b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10100b32f4bbSJosef Bacik { 1011bb146eb2SJosef Bacik #ifdef DEBUG 10120b32f4bbSJosef Bacik struct extent_buffer *eb; 10130b32f4bbSJosef Bacik 10140b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10150b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10160b32f4bbSJosef Bacik BUG_ON(!eb); 10170b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10180b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10190b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1020bb146eb2SJosef Bacik #endif 10210b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10220b32f4bbSJosef Bacik } 10230b32f4bbSJosef Bacik 10247f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1025d98237b3SChris Mason .readpage = btree_readpage, 10260da5468fSChris Mason .writepages = btree_writepages, 10275f39d397SChris Mason .releasepage = btree_releasepage, 10285f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10295a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1030784b4e29SChris Mason .migratepage = btree_migratepage, 10315a92bc88SChris Mason #endif 10320b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1033d98237b3SChris Mason }; 1034123abc88SChris Mason 10352ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1036090d1875SChris Mason { 10375f39d397SChris Mason struct extent_buffer *buf = NULL; 1038537f38f0SNikolay Borisov int ret; 1039090d1875SChris Mason 10402ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1041c871b0f2SLiu Bo if (IS_ERR(buf)) 10426197d86eSDavid Sterba return; 1043537f38f0SNikolay Borisov 1044c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 1045537f38f0SNikolay Borisov if (ret < 0) 1046537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1047537f38f0SNikolay Borisov else 10485f39d397SChris Mason free_extent_buffer(buf); 1049090d1875SChris Mason } 1050090d1875SChris Mason 10512ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10522ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10530308af44SDavid Sterba u64 bytenr) 10540999df54SChris Mason { 10550b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10560b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10570b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10580999df54SChris Mason } 10590999df54SChris Mason 1060581c1760SQu Wenruo /* 1061581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1062581c1760SQu Wenruo * verification. 1063581c1760SQu Wenruo * 1064581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1065581c1760SQu Wenruo * @level: expected level, mandatory check 1066581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1067581c1760SQu Wenruo */ 10682ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1069581c1760SQu Wenruo u64 parent_transid, int level, 1070581c1760SQu Wenruo struct btrfs_key *first_key) 1071e20d96d6SChris Mason { 10725f39d397SChris Mason struct extent_buffer *buf = NULL; 107319c00ddcSChris Mason int ret; 107419c00ddcSChris Mason 10752ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1076c871b0f2SLiu Bo if (IS_ERR(buf)) 1077c871b0f2SLiu Bo return buf; 1078e4204dedSChris Mason 10795ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1080581c1760SQu Wenruo level, first_key); 10810f0fe8f7SFilipe David Borba Manana if (ret) { 1082537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 108364c043deSLiu Bo return ERR_PTR(ret); 10840f0fe8f7SFilipe David Borba Manana } 10855f39d397SChris Mason return buf; 1086ce9adaa5SChris Mason 1087eb60ceacSChris Mason } 1088eb60ceacSChris Mason 10896a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1090ed2ff2cbSChris Mason { 10916a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 109255c69072SChris Mason if (btrfs_header_generation(buf) == 1093e2d84521SMiao Xie fs_info->running_transaction->transid) { 1094b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1095b4ce94deSChris Mason 1096b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1097104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1098e2d84521SMiao Xie -buf->len, 1099e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1100b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 11018bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 11020b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1103925baeddSChris Mason } 11045f39d397SChris Mason } 1105ed7b63ebSJosef Bacik } 11065f39d397SChris Mason 11078257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11088257b2dcSMiao Xie { 11098257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11108257b2dcSMiao Xie int ret; 11118257b2dcSMiao Xie 11128257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11138257b2dcSMiao Xie if (!writers) 11148257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11158257b2dcSMiao Xie 11168a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11178257b2dcSMiao Xie if (ret < 0) { 11188257b2dcSMiao Xie kfree(writers); 11198257b2dcSMiao Xie return ERR_PTR(ret); 11208257b2dcSMiao Xie } 11218257b2dcSMiao Xie 11228257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11238257b2dcSMiao Xie return writers; 11248257b2dcSMiao Xie } 11258257b2dcSMiao Xie 11268257b2dcSMiao Xie static void 11278257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11288257b2dcSMiao Xie { 11298257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11308257b2dcSMiao Xie kfree(writers); 11318257b2dcSMiao Xie } 11328257b2dcSMiao Xie 1133da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1134e20d96d6SChris Mason u64 objectid) 1135d97e63b6SChris Mason { 11367c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 113796dfcb46SJosef Bacik root->fs_info = fs_info; 1138cfaa7295SChris Mason root->node = NULL; 1139a28ec197SChris Mason root->commit_root = NULL; 114027cdeb70SMiao Xie root->state = 0; 1141d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11420b86a832SChris Mason 11430f7d52f4SChris Mason root->last_trans = 0; 114413a8a7c8SYan, Zheng root->highest_objectid = 0; 1145eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1146199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11476bef4d31SEric Paris root->inode_tree = RB_ROOT; 114816cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1149f0486c68SYan, Zheng root->block_rsv = NULL; 11500b86a832SChris Mason 11510b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11525d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1153eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1154eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1155199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1156199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1157d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11582ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11592ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11605d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1161eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1162199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1163f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11642ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11652ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11668287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1167a2135011SChris Mason mutex_init(&root->objectid_mutex); 1168e02119d5SChris Mason mutex_init(&root->log_mutex); 116931f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1170573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11717237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11727237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11737237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11748b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11758b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11767237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11777237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11787237f183SYan Zheng atomic_set(&root->log_writers, 0); 11792ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11800700cea7SElena Reshetova refcount_set(&root->refs, 1); 1181ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 11828ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1183eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11847237f183SYan Zheng root->log_transid = 0; 1185d1433debSMiao Xie root->log_transid_committed = -1; 1186257c62e1SChris Mason root->last_log_commit = 0; 11877c0260eeSJeff Mahoney if (!dummy) 118843eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 118943eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1190017e5369SChris Mason 11913768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11923768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11936702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11947c0260eeSJeff Mahoney if (!dummy) 11953f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119606ea65a3SJosef Bacik else 119706ea65a3SJosef Bacik root->defrag_trans_start = 0; 11984d775673SChris Mason root->root_key.objectid = objectid; 11990ee5dc67SAl Viro root->anon_dev = 0; 12008ea05e3aSAlexander Block 12015f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1202370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1203bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1204bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1205bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1206bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1207bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1208bd647ce3SJosef Bacik #endif 12093768f368SChris Mason } 12103768f368SChris Mason 121174e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 121296dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12136f07e42eSAl Viro { 121474e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12156f07e42eSAl Viro if (root) 121696dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12176f07e42eSAl Viro return root; 12186f07e42eSAl Viro } 12196f07e42eSAl Viro 122006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 122106ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1222da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 122306ea65a3SJosef Bacik { 122406ea65a3SJosef Bacik struct btrfs_root *root; 122506ea65a3SJosef Bacik 12267c0260eeSJeff Mahoney if (!fs_info) 12277c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12287c0260eeSJeff Mahoney 122996dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 123006ea65a3SJosef Bacik if (!root) 123106ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1232da17066cSJeff Mahoney 1233b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1234faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 123506ea65a3SJosef Bacik 123606ea65a3SJosef Bacik return root; 123706ea65a3SJosef Bacik } 123806ea65a3SJosef Bacik #endif 123906ea65a3SJosef Bacik 124020897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 124120897f5cSArne Jansen u64 objectid) 124220897f5cSArne Jansen { 12439b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 124420897f5cSArne Jansen struct extent_buffer *leaf; 124520897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 124620897f5cSArne Jansen struct btrfs_root *root; 124720897f5cSArne Jansen struct btrfs_key key; 1248b89f6d1fSFilipe Manana unsigned int nofs_flag; 124920897f5cSArne Jansen int ret = 0; 125033d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 125120897f5cSArne Jansen 1252b89f6d1fSFilipe Manana /* 1253b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1254b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1255b89f6d1fSFilipe Manana */ 1256b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 125796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1258b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 125920897f5cSArne Jansen if (!root) 126020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 126120897f5cSArne Jansen 126220897f5cSArne Jansen root->root_key.objectid = objectid; 126320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 126420897f5cSArne Jansen root->root_key.offset = 0; 126520897f5cSArne Jansen 12664d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 126720897f5cSArne Jansen if (IS_ERR(leaf)) { 126820897f5cSArne Jansen ret = PTR_ERR(leaf); 12691dd05682STsutomu Itoh leaf = NULL; 127020897f5cSArne Jansen goto fail; 127120897f5cSArne Jansen } 127220897f5cSArne Jansen 127320897f5cSArne Jansen root->node = leaf; 127420897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 127520897f5cSArne Jansen 127620897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 127727cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 127820897f5cSArne Jansen 127920897f5cSArne Jansen root->root_item.flags = 0; 128020897f5cSArne Jansen root->root_item.byte_limit = 0; 128120897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 128220897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 128320897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 128420897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 128520897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 128620897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 128720897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 128833d85fdaSQu Wenruo if (is_fstree(objectid)) 12896463fe58SStefan Behrens uuid_le_gen(&uuid); 12906463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 129120897f5cSArne Jansen root->root_item.drop_level = 0; 129220897f5cSArne Jansen 129320897f5cSArne Jansen key.objectid = objectid; 129420897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 129520897f5cSArne Jansen key.offset = 0; 129620897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129720897f5cSArne Jansen if (ret) 129820897f5cSArne Jansen goto fail; 129920897f5cSArne Jansen 130020897f5cSArne Jansen btrfs_tree_unlock(leaf); 130120897f5cSArne Jansen 130220897f5cSArne Jansen return root; 13031dd05682STsutomu Itoh 13041dd05682STsutomu Itoh fail: 13051dd05682STsutomu Itoh if (leaf) { 13061dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 130759885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13081dd05682STsutomu Itoh free_extent_buffer(leaf); 13091dd05682STsutomu Itoh } 131000246528SJosef Bacik btrfs_put_root(root); 13111dd05682STsutomu Itoh 13121dd05682STsutomu Itoh return ERR_PTR(ret); 131320897f5cSArne Jansen } 131420897f5cSArne Jansen 13157237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1316e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13170f7d52f4SChris Mason { 13180f7d52f4SChris Mason struct btrfs_root *root; 13197237f183SYan Zheng struct extent_buffer *leaf; 1320e02119d5SChris Mason 132196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1322e02119d5SChris Mason if (!root) 13237237f183SYan Zheng return ERR_PTR(-ENOMEM); 1324e02119d5SChris Mason 1325e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1326e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1327e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 132827cdeb70SMiao Xie 13297237f183SYan Zheng /* 133027cdeb70SMiao Xie * DON'T set REF_COWS for log trees 133127cdeb70SMiao Xie * 13327237f183SYan Zheng * log trees do not get reference counted because they go away 13337237f183SYan Zheng * before a real commit is actually done. They do store pointers 13347237f183SYan Zheng * to file data extents, and those reference counts still get 13357237f183SYan Zheng * updated (along with back refs to the log tree). 13367237f183SYan Zheng */ 1337e02119d5SChris Mason 13384d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13394d75f8a9SDavid Sterba NULL, 0, 0, 0); 13407237f183SYan Zheng if (IS_ERR(leaf)) { 134100246528SJosef Bacik btrfs_put_root(root); 13427237f183SYan Zheng return ERR_CAST(leaf); 13437237f183SYan Zheng } 1344e02119d5SChris Mason 13457237f183SYan Zheng root->node = leaf; 1346e02119d5SChris Mason 1347e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1348e02119d5SChris Mason btrfs_tree_unlock(root->node); 13497237f183SYan Zheng return root; 13507237f183SYan Zheng } 13517237f183SYan Zheng 13527237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13537237f183SYan Zheng struct btrfs_fs_info *fs_info) 13547237f183SYan Zheng { 13557237f183SYan Zheng struct btrfs_root *log_root; 13567237f183SYan Zheng 13577237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13587237f183SYan Zheng if (IS_ERR(log_root)) 13597237f183SYan Zheng return PTR_ERR(log_root); 13607237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13617237f183SYan Zheng fs_info->log_root_tree = log_root; 13627237f183SYan Zheng return 0; 13637237f183SYan Zheng } 13647237f183SYan Zheng 13657237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13667237f183SYan Zheng struct btrfs_root *root) 13677237f183SYan Zheng { 13680b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13697237f183SYan Zheng struct btrfs_root *log_root; 13707237f183SYan Zheng struct btrfs_inode_item *inode_item; 13717237f183SYan Zheng 13720b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13737237f183SYan Zheng if (IS_ERR(log_root)) 13747237f183SYan Zheng return PTR_ERR(log_root); 13757237f183SYan Zheng 13767237f183SYan Zheng log_root->last_trans = trans->transid; 13777237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13787237f183SYan Zheng 13797237f183SYan Zheng inode_item = &log_root->root_item.inode; 13803cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13813cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13823cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1383da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13840b246afaSJeff Mahoney fs_info->nodesize); 13853cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13867237f183SYan Zheng 13875d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13887237f183SYan Zheng 13897237f183SYan Zheng WARN_ON(root->log_root); 13907237f183SYan Zheng root->log_root = log_root; 13917237f183SYan Zheng root->log_transid = 0; 1392d1433debSMiao Xie root->log_transid_committed = -1; 1393257c62e1SChris Mason root->last_log_commit = 0; 1394e02119d5SChris Mason return 0; 1395e02119d5SChris Mason } 1396e02119d5SChris Mason 139762a2c73eSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1398cb517eabSMiao Xie struct btrfs_key *key) 1399e02119d5SChris Mason { 1400e02119d5SChris Mason struct btrfs_root *root; 1401e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14020f7d52f4SChris Mason struct btrfs_path *path; 140384234f3aSYan Zheng u64 generation; 1404cb517eabSMiao Xie int ret; 1405581c1760SQu Wenruo int level; 1406cb517eabSMiao Xie 1407cb517eabSMiao Xie path = btrfs_alloc_path(); 1408cb517eabSMiao Xie if (!path) 1409cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14100f7d52f4SChris Mason 141196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 1412cb517eabSMiao Xie if (!root) { 1413cb517eabSMiao Xie ret = -ENOMEM; 1414cb517eabSMiao Xie goto alloc_fail; 14150f7d52f4SChris Mason } 14160f7d52f4SChris Mason 1417cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1418cb517eabSMiao Xie &root->root_item, &root->root_key); 14190f7d52f4SChris Mason if (ret) { 142013a8a7c8SYan, Zheng if (ret > 0) 142113a8a7c8SYan, Zheng ret = -ENOENT; 1422cb517eabSMiao Xie goto find_fail; 14230f7d52f4SChris Mason } 142413a8a7c8SYan, Zheng 142584234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1426581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14272ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14282ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1429581c1760SQu Wenruo generation, level, NULL); 143064c043deSLiu Bo if (IS_ERR(root->node)) { 143164c043deSLiu Bo ret = PTR_ERR(root->node); 1432cb517eabSMiao Xie goto find_fail; 1433cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1434cb517eabSMiao Xie ret = -EIO; 143564c043deSLiu Bo free_extent_buffer(root->node); 143664c043deSLiu Bo goto find_fail; 1437416bc658SJosef Bacik } 14385d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 143913a8a7c8SYan, Zheng out: 1440cb517eabSMiao Xie btrfs_free_path(path); 1441cb517eabSMiao Xie return root; 1442cb517eabSMiao Xie 1443cb517eabSMiao Xie find_fail: 144400246528SJosef Bacik btrfs_put_root(root); 1445cb517eabSMiao Xie alloc_fail: 1446cb517eabSMiao Xie root = ERR_PTR(ret); 1447cb517eabSMiao Xie goto out; 1448cb517eabSMiao Xie } 1449cb517eabSMiao Xie 1450a98db0f3SJosef Bacik static int btrfs_init_fs_root(struct btrfs_root *root) 1451cb517eabSMiao Xie { 1452cb517eabSMiao Xie int ret; 14538257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1454cb517eabSMiao Xie 1455cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1456cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1457cb517eabSMiao Xie GFP_NOFS); 1458cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1459cb517eabSMiao Xie ret = -ENOMEM; 1460cb517eabSMiao Xie goto fail; 1461cb517eabSMiao Xie } 1462cb517eabSMiao Xie 14638257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14648257b2dcSMiao Xie if (IS_ERR(writers)) { 14658257b2dcSMiao Xie ret = PTR_ERR(writers); 14668257b2dcSMiao Xie goto fail; 14678257b2dcSMiao Xie } 14688257b2dcSMiao Xie root->subv_writers = writers; 14698257b2dcSMiao Xie 1470f39e4571SJosef Bacik if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 1471f39e4571SJosef Bacik set_bit(BTRFS_ROOT_REF_COWS, &root->state); 1472f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1473f39e4571SJosef Bacik } 1474f39e4571SJosef Bacik 1475cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 147657cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 147757cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1478cb517eabSMiao Xie 1479cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1480cb517eabSMiao Xie if (ret) 1481876d2cf1SLiu Bo goto fail; 1482f32e48e9SChandan Rajendra 1483f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1484f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1485f32e48e9SChandan Rajendra &root->highest_objectid); 1486f32e48e9SChandan Rajendra if (ret) { 1487f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1488876d2cf1SLiu Bo goto fail; 1489f32e48e9SChandan Rajendra } 1490f32e48e9SChandan Rajendra 1491f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1492f32e48e9SChandan Rajendra 1493f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1494f32e48e9SChandan Rajendra 1495cb517eabSMiao Xie return 0; 1496cb517eabSMiao Xie fail: 149784db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1498cb517eabSMiao Xie return ret; 1499cb517eabSMiao Xie } 1500cb517eabSMiao Xie 1501a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1502cb517eabSMiao Xie u64 root_id) 1503cb517eabSMiao Xie { 1504cb517eabSMiao Xie struct btrfs_root *root; 1505cb517eabSMiao Xie 1506cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1507cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1508cb517eabSMiao Xie (unsigned long)root_id); 1509bc44d7c4SJosef Bacik if (root) 151000246528SJosef Bacik root = btrfs_grab_root(root); 1511cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1512cb517eabSMiao Xie return root; 1513cb517eabSMiao Xie } 1514cb517eabSMiao Xie 1515cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1516cb517eabSMiao Xie struct btrfs_root *root) 1517cb517eabSMiao Xie { 1518cb517eabSMiao Xie int ret; 1519cb517eabSMiao Xie 1520e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1521cb517eabSMiao Xie if (ret) 1522cb517eabSMiao Xie return ret; 1523cb517eabSMiao Xie 1524cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1525cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1526cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1527cb517eabSMiao Xie root); 1528af01d2e5SJosef Bacik if (ret == 0) { 152900246528SJosef Bacik btrfs_grab_root(root); 153027cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1531af01d2e5SJosef Bacik } 1532cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1533cb517eabSMiao Xie radix_tree_preload_end(); 1534cb517eabSMiao Xie 1535cb517eabSMiao Xie return ret; 1536cb517eabSMiao Xie } 1537cb517eabSMiao Xie 1538bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1539bd647ce3SJosef Bacik { 1540bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1541bd647ce3SJosef Bacik struct btrfs_root *root; 1542bd647ce3SJosef Bacik 1543bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1544bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1545bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1546bd647ce3SJosef Bacik btrfs_err(fs_info, "leaked root %llu-%llu refcount %d", 1547bd647ce3SJosef Bacik root->root_key.objectid, root->root_key.offset, 1548bd647ce3SJosef Bacik refcount_read(&root->refs)); 1549bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 155000246528SJosef Bacik btrfs_put_root(root); 155100246528SJosef Bacik btrfs_put_root(root); 1552bd647ce3SJosef Bacik } 1553bd647ce3SJosef Bacik #endif 1554bd647ce3SJosef Bacik } 1555bd647ce3SJosef Bacik 15560d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15570d4b0463SJosef Bacik { 1558141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1559141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1560141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1561141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1562141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1563141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1564141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15650d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15660d4b0463SJosef Bacik kfree(fs_info->delayed_root); 156700246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 156800246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 156900246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 157000246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 157100246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 157200246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 157300246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 157400246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 157500246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1576bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15770d4b0463SJosef Bacik kfree(fs_info->super_copy); 15780d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15790d4b0463SJosef Bacik kvfree(fs_info); 15800d4b0463SJosef Bacik } 15810d4b0463SJosef Bacik 15820d4b0463SJosef Bacik 1583c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1584c00869f1SMiao Xie struct btrfs_key *location, 1585c00869f1SMiao Xie bool check_ref) 15865eda7b5eSChris Mason { 15875eda7b5eSChris Mason struct btrfs_root *root; 1588381cf658SDavid Sterba struct btrfs_path *path; 15891d4c08e0SDavid Sterba struct btrfs_key key; 15905eda7b5eSChris Mason int ret; 15915eda7b5eSChris Mason 1592edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 159300246528SJosef Bacik return btrfs_grab_root(fs_info->tree_root); 1594edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 159500246528SJosef Bacik return btrfs_grab_root(fs_info->extent_root); 15968f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 159700246528SJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 15988f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 159900246528SJosef Bacik return btrfs_grab_root(fs_info->dev_root); 16000403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 160100246528SJosef Bacik return btrfs_grab_root(fs_info->csum_root); 1602bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 160300246528SJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 1604bc44d7c4SJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 1605f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 160600246528SJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 1607bc44d7c4SJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 160870f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 160900246528SJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 1610bc44d7c4SJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 16114df27c4dSYan, Zheng again: 1612cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 161348475471SStefan Behrens if (root) { 1614bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 161500246528SJosef Bacik btrfs_put_root(root); 161648475471SStefan Behrens return ERR_PTR(-ENOENT); 1617bc44d7c4SJosef Bacik } 16185eda7b5eSChris Mason return root; 161948475471SStefan Behrens } 16205eda7b5eSChris Mason 162183db2aadSJosef Bacik root = btrfs_read_tree_root(fs_info->tree_root, location); 16225eda7b5eSChris Mason if (IS_ERR(root)) 16235eda7b5eSChris Mason return root; 16243394e160SChris Mason 1625c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1626d68fc57bSYan, Zheng ret = -ENOENT; 1627d68fc57bSYan, Zheng goto fail; 1628d68fc57bSYan, Zheng } 1629d68fc57bSYan, Zheng 1630cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1631cb517eabSMiao Xie if (ret) 1632cb517eabSMiao Xie goto fail; 1633cb517eabSMiao Xie 1634381cf658SDavid Sterba path = btrfs_alloc_path(); 1635381cf658SDavid Sterba if (!path) { 1636381cf658SDavid Sterba ret = -ENOMEM; 1637381cf658SDavid Sterba goto fail; 1638381cf658SDavid Sterba } 16391d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16401d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16411d4c08e0SDavid Sterba key.offset = location->objectid; 16421d4c08e0SDavid Sterba 16431d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1644381cf658SDavid Sterba btrfs_free_path(path); 1645d68fc57bSYan, Zheng if (ret < 0) 1646d68fc57bSYan, Zheng goto fail; 1647d68fc57bSYan, Zheng if (ret == 0) 164827cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1649d68fc57bSYan, Zheng 1650bc44d7c4SJosef Bacik /* 1651bc44d7c4SJosef Bacik * All roots have two refs on them at all times, one for the mounted fs, 1652bc44d7c4SJosef Bacik * and one for being in the radix tree. This way we only free the root 1653bc44d7c4SJosef Bacik * when we are unmounting or deleting the subvolume. We get one ref 1654bc44d7c4SJosef Bacik * from __setup_root, one for inserting it into the radix tree, and then 1655bc44d7c4SJosef Bacik * we have the third for returning it, and the caller will put it when 1656bc44d7c4SJosef Bacik * it's done with the root. 1657bc44d7c4SJosef Bacik */ 165800246528SJosef Bacik btrfs_grab_root(root); 1659cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16600f7d52f4SChris Mason if (ret) { 166100246528SJosef Bacik btrfs_put_root(root); 16624df27c4dSYan, Zheng if (ret == -EEXIST) { 166384db5ccfSDavid Sterba btrfs_free_fs_root(root); 16644df27c4dSYan, Zheng goto again; 16650f7d52f4SChris Mason } 16664df27c4dSYan, Zheng goto fail; 16674df27c4dSYan, Zheng } 1668edbd8d4eSChris Mason return root; 16694df27c4dSYan, Zheng fail: 167084db5ccfSDavid Sterba btrfs_free_fs_root(root); 16714df27c4dSYan, Zheng return ERR_PTR(ret); 1672edbd8d4eSChris Mason } 1673edbd8d4eSChris Mason 167404160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 167504160088SChris Mason { 167604160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 167704160088SChris Mason int ret = 0; 167804160088SChris Mason struct btrfs_device *device; 167904160088SChris Mason struct backing_dev_info *bdi; 1680b7967db7SChris Mason 16811f78160cSXiao Guangrong rcu_read_lock(); 16821f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1683dfe25020SChris Mason if (!device->bdev) 1684dfe25020SChris Mason continue; 1685efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1686ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 168704160088SChris Mason ret = 1; 168804160088SChris Mason break; 168904160088SChris Mason } 169004160088SChris Mason } 16911f78160cSXiao Guangrong rcu_read_unlock(); 169204160088SChris Mason return ret; 169304160088SChris Mason } 169404160088SChris Mason 16958b712842SChris Mason /* 16968b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16978b712842SChris Mason * functions. This is where read checksum verification actually happens 16988b712842SChris Mason */ 16998b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1700ce9adaa5SChris Mason { 1701ce9adaa5SChris Mason struct bio *bio; 170297eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1703ce9adaa5SChris Mason 170497eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1705ce9adaa5SChris Mason bio = end_io_wq->bio; 17068b712842SChris Mason 17074e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1708ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1709ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 17104246a0b6SChristoph Hellwig bio_endio(bio); 17119be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1712ce9adaa5SChris Mason } 171344b8bd7eSChris Mason 1714a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1715a74a4b97SChris Mason { 1716a74a4b97SChris Mason struct btrfs_root *root = arg; 17170b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1718d0278245SMiao Xie int again; 1719a74a4b97SChris Mason 1720d6fd0ae2SOmar Sandoval while (1) { 1721d0278245SMiao Xie again = 0; 17229d1a2a3aSDavid Sterba 1723fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1724fd340d0fSJosef Bacik 1725d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17262ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1727d0278245SMiao Xie goto sleep; 1728d0278245SMiao Xie 172990c711abSZygo Blaxell /* 173090c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 173190c711abSZygo Blaxell * before we have finished mounting the filesystem. 173290c711abSZygo Blaxell */ 17330b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 173490c711abSZygo Blaxell goto sleep; 173590c711abSZygo Blaxell 17360b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1737d0278245SMiao Xie goto sleep; 1738d0278245SMiao Xie 1739dc7f370cSMiao Xie /* 1740dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1741dc7f370cSMiao Xie * during the above check and trylock. 1742dc7f370cSMiao Xie */ 17432ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17440b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1745dc7f370cSMiao Xie goto sleep; 1746dc7f370cSMiao Xie } 1747dc7f370cSMiao Xie 17482ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1749c2d6cb16SFilipe Manana 17509d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17510b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1752a74a4b97SChris Mason 1753d0278245SMiao Xie /* 175405323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 175505323cd1SMiao Xie * needn't do anything special here. 1756d0278245SMiao Xie */ 17570b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 175867c5e7d4SFilipe Manana 175967c5e7d4SFilipe Manana /* 176067c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 176167c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 176267c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 176367c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 176467c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 176567c5e7d4SFilipe Manana * unused block groups. 176667c5e7d4SFilipe Manana */ 17670b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1768d0278245SMiao Xie sleep: 1769fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1770d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1771d6fd0ae2SOmar Sandoval kthread_parkme(); 1772d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1773d6fd0ae2SOmar Sandoval return 0; 1774838fe188SJiri Kosina if (!again) { 1775a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1776a74a4b97SChris Mason schedule(); 1777a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1778a74a4b97SChris Mason } 1779da288d28SFilipe Manana } 1780a74a4b97SChris Mason } 1781a74a4b97SChris Mason 1782a74a4b97SChris Mason static int transaction_kthread(void *arg) 1783a74a4b97SChris Mason { 1784a74a4b97SChris Mason struct btrfs_root *root = arg; 17850b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1786a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1787a74a4b97SChris Mason struct btrfs_transaction *cur; 17888929ecfaSYan, Zheng u64 transid; 1789a944442cSAllen Pais time64_t now; 1790a74a4b97SChris Mason unsigned long delay; 1791914b2007SJan Kara bool cannot_commit; 1792a74a4b97SChris Mason 1793a74a4b97SChris Mason do { 1794914b2007SJan Kara cannot_commit = false; 17950b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17960b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1797a74a4b97SChris Mason 17980b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17990b246afaSJeff Mahoney cur = fs_info->running_transaction; 1800a74a4b97SChris Mason if (!cur) { 18010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1802a74a4b97SChris Mason goto sleep; 1803a74a4b97SChris Mason } 180431153d81SYan Zheng 1805afd48513SArnd Bergmann now = ktime_get_seconds(); 18063296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1807a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 18088b87dc17SDavid Sterba (now < cur->start_time || 18090b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 18100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1811a74a4b97SChris Mason delay = HZ * 5; 1812a74a4b97SChris Mason goto sleep; 1813a74a4b97SChris Mason } 18148929ecfaSYan, Zheng transid = cur->transid; 18150b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 181656bec294SChris Mason 181779787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1818354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1819914b2007SJan Kara if (IS_ERR(trans)) { 1820354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1821914b2007SJan Kara cannot_commit = true; 182279787eaaSJeff Mahoney goto sleep; 1823914b2007SJan Kara } 18248929ecfaSYan, Zheng if (transid == trans->transid) { 18253a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18268929ecfaSYan, Zheng } else { 18273a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18288929ecfaSYan, Zheng } 1829a74a4b97SChris Mason sleep: 18300b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18310b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1832a74a4b97SChris Mason 18334e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18340b246afaSJeff Mahoney &fs_info->fs_state))) 18352ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18368929ecfaSYan, Zheng if (!kthread_should_stop() && 18370b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1838914b2007SJan Kara cannot_commit)) 1839bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1840a74a4b97SChris Mason } while (!kthread_should_stop()); 1841a74a4b97SChris Mason return 0; 1842a74a4b97SChris Mason } 1843a74a4b97SChris Mason 1844af31f5e5SChris Mason /* 184501f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 184601f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 184701f0f9daSNikolay Borisov * anything. 1848af31f5e5SChris Mason * 1849af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1850af31f5e5SChris Mason * generation of the latest root in the array with the generation 1851af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1852af31f5e5SChris Mason */ 185301f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1854af31f5e5SChris Mason { 185501f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1856af31f5e5SChris Mason u64 cur; 1857af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1858af31f5e5SChris Mason int i; 1859af31f5e5SChris Mason 1860af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1861af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1862af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1863af31f5e5SChris Mason if (cur == newest_gen) 186401f0f9daSNikolay Borisov return i; 1865af31f5e5SChris Mason } 1866af31f5e5SChris Mason 186701f0f9daSNikolay Borisov return -EINVAL; 1868af31f5e5SChris Mason } 1869af31f5e5SChris Mason 1870af31f5e5SChris Mason /* 1871af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1872af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1873af31f5e5SChris Mason * done 1874af31f5e5SChris Mason */ 1875af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1876af31f5e5SChris Mason { 18776ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1878af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1879af31f5e5SChris Mason 1880af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1881af31f5e5SChris Mason 1882af31f5e5SChris Mason /* 1883af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1884af31f5e5SChris Mason * regardless of which ones we use today 1885af31f5e5SChris Mason */ 1886af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1887af31f5e5SChris Mason 1888af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1889af31f5e5SChris Mason 1890af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1891af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1892af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1893af31f5e5SChris Mason 1894af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1895af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1896af31f5e5SChris Mason 1897af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1898af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1899af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1900af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1901af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1902af31f5e5SChris Mason 1903af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1904af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1905af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1906af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1907af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1908af31f5e5SChris Mason 19097c7e82a7SChris Mason /* 19107c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19117c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19127c7e82a7SChris Mason */ 19137c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19147c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19157c7e82a7SChris Mason info->fs_root->node->start); 1916af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1917af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1918af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1919af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19207c7e82a7SChris Mason } 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1927af31f5e5SChris Mason 1928af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1929af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1930af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1931af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1932af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1933af31f5e5SChris Mason 1934af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1935af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1936af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1937af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1938af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1939af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1940af31f5e5SChris Mason 1941af31f5e5SChris Mason /* 1942af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1943af31f5e5SChris Mason * for the next commit 1944af31f5e5SChris Mason */ 1945af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1946af31f5e5SChris Mason &info->super_for_commit->super_roots, 1947af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1948af31f5e5SChris Mason } 1949af31f5e5SChris Mason 1950af31f5e5SChris Mason /* 1951bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1952bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1953bd2336b2SNikolay Borisov * 1954bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1955bd2336b2SNikolay Borisov * priority - priority of backup root required 1956bd2336b2SNikolay Borisov * 1957bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1958bd2336b2SNikolay Borisov */ 1959bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1960bd2336b2SNikolay Borisov { 1961bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1962bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1963bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1964bd2336b2SNikolay Borisov 1965bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1966bd2336b2SNikolay Borisov if (priority == 0) 1967bd2336b2SNikolay Borisov return backup_index; 1968bd2336b2SNikolay Borisov 1969bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1970bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1971bd2336b2SNikolay Borisov } else { 1972bd2336b2SNikolay Borisov return -EINVAL; 1973bd2336b2SNikolay Borisov } 1974bd2336b2SNikolay Borisov 1975bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1976bd2336b2SNikolay Borisov 1977bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1978bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1979bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1980bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1981bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1982bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1983bd2336b2SNikolay Borisov 1984bd2336b2SNikolay Borisov /* 1985bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1986bd2336b2SNikolay Borisov * need a fsck 1987bd2336b2SNikolay Borisov */ 1988bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1989bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1990bd2336b2SNikolay Borisov 1991bd2336b2SNikolay Borisov return backup_index; 1992bd2336b2SNikolay Borisov } 1993bd2336b2SNikolay Borisov 19947abadb64SLiu Bo /* helper to cleanup workers */ 19957abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19967abadb64SLiu Bo { 1997dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1998afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 19995cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2000fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2001fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20028b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2003d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2004fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2005fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 20065b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2007e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2008736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2009a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2010fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2011b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2012b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2013a9b9477dSFilipe Manana /* 2014a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2015a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2016a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2017a9b9477dSFilipe Manana */ 2018a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2019a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20207abadb64SLiu Bo } 20217abadb64SLiu Bo 20222e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20232e9f5954SRashika { 20242e9f5954SRashika if (root) { 20252e9f5954SRashika free_extent_buffer(root->node); 20262e9f5954SRashika free_extent_buffer(root->commit_root); 20272e9f5954SRashika root->node = NULL; 20282e9f5954SRashika root->commit_root = NULL; 20292e9f5954SRashika } 20302e9f5954SRashika } 20312e9f5954SRashika 2032af31f5e5SChris Mason /* helper to cleanup tree roots */ 20334273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2034af31f5e5SChris Mason { 20352e9f5954SRashika free_root_extent_buffers(info->tree_root); 2036655b09feSJosef Bacik 20372e9f5954SRashika free_root_extent_buffers(info->dev_root); 20382e9f5954SRashika free_root_extent_buffers(info->extent_root); 20392e9f5954SRashika free_root_extent_buffers(info->csum_root); 20402e9f5954SRashika free_root_extent_buffers(info->quota_root); 20412e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20424273eaffSAnand Jain if (free_chunk_root) 20432e9f5954SRashika free_root_extent_buffers(info->chunk_root); 204470f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2045af31f5e5SChris Mason } 2046af31f5e5SChris Mason 2047faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2048171f6537SJosef Bacik { 2049171f6537SJosef Bacik int ret; 2050171f6537SJosef Bacik struct btrfs_root *gang[8]; 2051171f6537SJosef Bacik int i; 2052171f6537SJosef Bacik 2053171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2054171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2055171f6537SJosef Bacik struct btrfs_root, root_list); 2056171f6537SJosef Bacik list_del(&gang[0]->root_list); 2057171f6537SJosef Bacik 205827cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2059cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2060171f6537SJosef Bacik } else { 2061171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2062171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 206300246528SJosef Bacik btrfs_put_root(gang[0]); 2064171f6537SJosef Bacik } 2065171f6537SJosef Bacik } 2066171f6537SJosef Bacik 2067171f6537SJosef Bacik while (1) { 2068171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2069171f6537SJosef Bacik (void **)gang, 0, 2070171f6537SJosef Bacik ARRAY_SIZE(gang)); 2071171f6537SJosef Bacik if (!ret) 2072171f6537SJosef Bacik break; 2073171f6537SJosef Bacik for (i = 0; i < ret; i++) 2074cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2075171f6537SJosef Bacik } 20761a4319ccSLiu Bo 2077fe119a6eSNikolay Borisov if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 20781a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 2079171f6537SJosef Bacik } 2080af31f5e5SChris Mason 2081638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2082638aa7edSEric Sandeen { 2083638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2084638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2085638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2086638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2087638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2088638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2089ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2090638aa7edSEric Sandeen } 2091638aa7edSEric Sandeen 2092779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2093779a65a4SEric Sandeen { 2094779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2095779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2096779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2097779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2098779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2099779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2100779a65a4SEric Sandeen } 2101779a65a4SEric Sandeen 21026bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2103f37938e0SEric Sandeen { 21042ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21052ff7e61eSJeff Mahoney 21062ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21072ff7e61eSJeff Mahoney set_nlink(inode, 1); 2108f37938e0SEric Sandeen /* 2109f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2110f37938e0SEric Sandeen * the real end of the address space is determined by all of 2111f37938e0SEric Sandeen * the devices in the system 2112f37938e0SEric Sandeen */ 21132ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21142ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2115f37938e0SEric Sandeen 21162ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 211743eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 211843eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 21197b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21202ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2121f37938e0SEric Sandeen 21222ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2123f37938e0SEric Sandeen 21242ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21252ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21262ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21272ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2128f37938e0SEric Sandeen } 2129f37938e0SEric Sandeen 2130ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2131ad618368SEric Sandeen { 2132ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2133129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21347f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2135ad618368SEric Sandeen } 2136ad618368SEric Sandeen 2137f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2138f9e92e40SEric Sandeen { 2139f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2140f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2141f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2142f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2143f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2144f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2145d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2146f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2147f9e92e40SEric Sandeen } 2148f9e92e40SEric Sandeen 21492a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21502a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21512a458198SEric Sandeen { 2152f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21536f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21542a458198SEric Sandeen 21552a458198SEric Sandeen fs_info->workers = 2156cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2157cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21582a458198SEric Sandeen 21592a458198SEric Sandeen fs_info->delalloc_workers = 2160cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2161cb001095SJeff Mahoney flags, max_active, 2); 21622a458198SEric Sandeen 21632a458198SEric Sandeen fs_info->flush_workers = 2164cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2165cb001095SJeff Mahoney flags, max_active, 0); 21662a458198SEric Sandeen 21672a458198SEric Sandeen fs_info->caching_workers = 2168cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21692a458198SEric Sandeen 21702a458198SEric Sandeen fs_info->fixup_workers = 2171cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21722a458198SEric Sandeen 21732a458198SEric Sandeen /* 21742a458198SEric Sandeen * endios are largely parallel and should have a very 21752a458198SEric Sandeen * low idle thresh 21762a458198SEric Sandeen */ 21772a458198SEric Sandeen fs_info->endio_workers = 2178cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21792a458198SEric Sandeen fs_info->endio_meta_workers = 2180cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2181cb001095SJeff Mahoney max_active, 4); 21822a458198SEric Sandeen fs_info->endio_meta_write_workers = 2183cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2184cb001095SJeff Mahoney max_active, 2); 21852a458198SEric Sandeen fs_info->endio_raid56_workers = 2186cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2187cb001095SJeff Mahoney max_active, 4); 21882a458198SEric Sandeen fs_info->endio_repair_workers = 2189cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 21902a458198SEric Sandeen fs_info->rmw_workers = 2191cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21922a458198SEric Sandeen fs_info->endio_write_workers = 2193cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2194cb001095SJeff Mahoney max_active, 2); 21952a458198SEric Sandeen fs_info->endio_freespace_worker = 2196cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2197cb001095SJeff Mahoney max_active, 0); 21982a458198SEric Sandeen fs_info->delayed_workers = 2199cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2200cb001095SJeff Mahoney max_active, 0); 22012a458198SEric Sandeen fs_info->readahead_workers = 2202cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2203cb001095SJeff Mahoney max_active, 2); 22042a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2205cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2206b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2207b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 22082a458198SEric Sandeen 22092a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2210ba8a9d07SChris Mason fs_info->flush_workers && 22112a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22122a458198SEric Sandeen fs_info->endio_meta_write_workers && 22132a458198SEric Sandeen fs_info->endio_repair_workers && 22142a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22152a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22162a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22172a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2218b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2219b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22202a458198SEric Sandeen return -ENOMEM; 22212a458198SEric Sandeen } 22222a458198SEric Sandeen 22232a458198SEric Sandeen return 0; 22242a458198SEric Sandeen } 22252a458198SEric Sandeen 22266d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22276d97c6e3SJohannes Thumshirn { 22286d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2229b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22306d97c6e3SJohannes Thumshirn 2231b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22326d97c6e3SJohannes Thumshirn 22336d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22346d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2235b4e967beSDavid Sterba csum_driver); 22366d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22376d97c6e3SJohannes Thumshirn } 22386d97c6e3SJohannes Thumshirn 22396d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22406d97c6e3SJohannes Thumshirn 22416d97c6e3SJohannes Thumshirn return 0; 22426d97c6e3SJohannes Thumshirn } 22436d97c6e3SJohannes Thumshirn 224463443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 224563443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 224663443bf5SEric Sandeen { 224763443bf5SEric Sandeen int ret; 224863443bf5SEric Sandeen struct btrfs_root *log_tree_root; 224963443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 225063443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2251581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 225263443bf5SEric Sandeen 225363443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2254f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 225563443bf5SEric Sandeen return -EIO; 225663443bf5SEric Sandeen } 225763443bf5SEric Sandeen 225896dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 225996dfcb46SJosef Bacik GFP_KERNEL); 226063443bf5SEric Sandeen if (!log_tree_root) 226163443bf5SEric Sandeen return -ENOMEM; 226263443bf5SEric Sandeen 22632ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2264581c1760SQu Wenruo fs_info->generation + 1, 2265581c1760SQu Wenruo level, NULL); 226664c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2267f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22680eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 226900246528SJosef Bacik btrfs_put_root(log_tree_root); 22700eeff236SLiu Bo return ret; 227164c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2272f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 227363443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 227400246528SJosef Bacik btrfs_put_root(log_tree_root); 227563443bf5SEric Sandeen return -EIO; 227663443bf5SEric Sandeen } 227763443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 227863443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 227963443bf5SEric Sandeen if (ret) { 22800b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 228163443bf5SEric Sandeen "Failed to recover log tree"); 228263443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 228300246528SJosef Bacik btrfs_put_root(log_tree_root); 228463443bf5SEric Sandeen return ret; 228563443bf5SEric Sandeen } 228663443bf5SEric Sandeen 2287bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22886bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 228963443bf5SEric Sandeen if (ret) 229063443bf5SEric Sandeen return ret; 229163443bf5SEric Sandeen } 229263443bf5SEric Sandeen 229363443bf5SEric Sandeen return 0; 229463443bf5SEric Sandeen } 229563443bf5SEric Sandeen 22966bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 22974bbcaa64SEric Sandeen { 22986bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2299a4f3d2c4SDavid Sterba struct btrfs_root *root; 23004bbcaa64SEric Sandeen struct btrfs_key location; 23014bbcaa64SEric Sandeen int ret; 23024bbcaa64SEric Sandeen 23036bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23046bccf3abSJeff Mahoney 23054bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23064bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23074bbcaa64SEric Sandeen location.offset = 0; 23084bbcaa64SEric Sandeen 2309a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2310f50f4353SLiu Bo if (IS_ERR(root)) { 2311f50f4353SLiu Bo ret = PTR_ERR(root); 2312f50f4353SLiu Bo goto out; 2313f50f4353SLiu Bo } 2314a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2315a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23164bbcaa64SEric Sandeen 23174bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2318a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2319f50f4353SLiu Bo if (IS_ERR(root)) { 2320f50f4353SLiu Bo ret = PTR_ERR(root); 2321f50f4353SLiu Bo goto out; 2322f50f4353SLiu Bo } 2323a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2324a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23254bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23264bbcaa64SEric Sandeen 23274bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2328a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2329f50f4353SLiu Bo if (IS_ERR(root)) { 2330f50f4353SLiu Bo ret = PTR_ERR(root); 2331f50f4353SLiu Bo goto out; 2332f50f4353SLiu Bo } 2333a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2334a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23354bbcaa64SEric Sandeen 23364bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2337a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2338a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2339a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2340afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2341a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23424bbcaa64SEric Sandeen } 23434bbcaa64SEric Sandeen 23444bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2345a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2346a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2347a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23484bbcaa64SEric Sandeen if (ret != -ENOENT) 2349f50f4353SLiu Bo goto out; 23504bbcaa64SEric Sandeen } else { 2351a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2352a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23534bbcaa64SEric Sandeen } 23544bbcaa64SEric Sandeen 235570f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 235670f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 235770f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2358f50f4353SLiu Bo if (IS_ERR(root)) { 2359f50f4353SLiu Bo ret = PTR_ERR(root); 2360f50f4353SLiu Bo goto out; 2361f50f4353SLiu Bo } 236270f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 236370f6d82eSOmar Sandoval fs_info->free_space_root = root; 236470f6d82eSOmar Sandoval } 236570f6d82eSOmar Sandoval 23664bbcaa64SEric Sandeen return 0; 2367f50f4353SLiu Bo out: 2368f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2369f50f4353SLiu Bo location.objectid, ret); 2370f50f4353SLiu Bo return ret; 23714bbcaa64SEric Sandeen } 23724bbcaa64SEric Sandeen 2373069ec957SQu Wenruo /* 2374069ec957SQu Wenruo * Real super block validation 2375069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2376069ec957SQu Wenruo * 2377069ec957SQu Wenruo * @sb: super block to check 2378069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2379069ec957SQu Wenruo * 0 the primary (1st) sb 2380069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2381069ec957SQu Wenruo * -1 skip bytenr check 2382069ec957SQu Wenruo */ 2383069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2384069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 238521a852b0SQu Wenruo { 238621a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 238721a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 238821a852b0SQu Wenruo int ret = 0; 238921a852b0SQu Wenruo 239021a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 239121a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 239221a852b0SQu Wenruo ret = -EINVAL; 239321a852b0SQu Wenruo } 239421a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 239521a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 239621a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 239721a852b0SQu Wenruo ret = -EINVAL; 239821a852b0SQu Wenruo } 239921a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 240021a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 240121a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 240221a852b0SQu Wenruo ret = -EINVAL; 240321a852b0SQu Wenruo } 240421a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 240521a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 240621a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 240721a852b0SQu Wenruo ret = -EINVAL; 240821a852b0SQu Wenruo } 240921a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 241021a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 241121a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 241221a852b0SQu Wenruo ret = -EINVAL; 241321a852b0SQu Wenruo } 241421a852b0SQu Wenruo 241521a852b0SQu Wenruo /* 241621a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 241721a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 241821a852b0SQu Wenruo */ 241921a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 242021a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 242121a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 242221a852b0SQu Wenruo ret = -EINVAL; 242321a852b0SQu Wenruo } 242421a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 242521a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 242621a852b0SQu Wenruo btrfs_err(fs_info, 242721a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 242821a852b0SQu Wenruo sectorsize, PAGE_SIZE); 242921a852b0SQu Wenruo ret = -EINVAL; 243021a852b0SQu Wenruo } 243121a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 243221a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 243321a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 243421a852b0SQu Wenruo ret = -EINVAL; 243521a852b0SQu Wenruo } 243621a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 243721a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 243821a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 243921a852b0SQu Wenruo ret = -EINVAL; 244021a852b0SQu Wenruo } 244121a852b0SQu Wenruo 244221a852b0SQu Wenruo /* Root alignment check */ 244321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 244421a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 244521a852b0SQu Wenruo btrfs_super_root(sb)); 244621a852b0SQu Wenruo ret = -EINVAL; 244721a852b0SQu Wenruo } 244821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 244921a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 245021a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 245121a852b0SQu Wenruo ret = -EINVAL; 245221a852b0SQu Wenruo } 245321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 245421a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 245521a852b0SQu Wenruo btrfs_super_log_root(sb)); 245621a852b0SQu Wenruo ret = -EINVAL; 245721a852b0SQu Wenruo } 245821a852b0SQu Wenruo 2459de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24607239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 246121a852b0SQu Wenruo btrfs_err(fs_info, 24627239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2463de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 246421a852b0SQu Wenruo ret = -EINVAL; 246521a852b0SQu Wenruo } 246621a852b0SQu Wenruo 246721a852b0SQu Wenruo /* 246821a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 246921a852b0SQu Wenruo * done later 247021a852b0SQu Wenruo */ 247121a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 247221a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 247321a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 247421a852b0SQu Wenruo ret = -EINVAL; 247521a852b0SQu Wenruo } 247621a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 247721a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 247821a852b0SQu Wenruo btrfs_super_stripesize(sb)); 247921a852b0SQu Wenruo ret = -EINVAL; 248021a852b0SQu Wenruo } 248121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 248221a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 248321a852b0SQu Wenruo btrfs_super_num_devices(sb)); 248421a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 248521a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 248621a852b0SQu Wenruo ret = -EINVAL; 248721a852b0SQu Wenruo } 248821a852b0SQu Wenruo 2489069ec957SQu Wenruo if (mirror_num >= 0 && 2490069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 249121a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 249221a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 249321a852b0SQu Wenruo ret = -EINVAL; 249421a852b0SQu Wenruo } 249521a852b0SQu Wenruo 249621a852b0SQu Wenruo /* 249721a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 249821a852b0SQu Wenruo * and one chunk 249921a852b0SQu Wenruo */ 250021a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 250121a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 250221a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 250321a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 250421a852b0SQu Wenruo ret = -EINVAL; 250521a852b0SQu Wenruo } 250621a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 250721a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 250821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 250921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 251021a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 251121a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 251221a852b0SQu Wenruo ret = -EINVAL; 251321a852b0SQu Wenruo } 251421a852b0SQu Wenruo 251521a852b0SQu Wenruo /* 251621a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 251721a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 251821a852b0SQu Wenruo */ 251921a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 252021a852b0SQu Wenruo btrfs_warn(fs_info, 252121a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 252221a852b0SQu Wenruo btrfs_super_generation(sb), 252321a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 252421a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 252521a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 252621a852b0SQu Wenruo btrfs_warn(fs_info, 252721a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 252821a852b0SQu Wenruo btrfs_super_generation(sb), 252921a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 253021a852b0SQu Wenruo 253121a852b0SQu Wenruo return ret; 253221a852b0SQu Wenruo } 253321a852b0SQu Wenruo 2534069ec957SQu Wenruo /* 2535069ec957SQu Wenruo * Validation of super block at mount time. 2536069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2537069ec957SQu Wenruo * flags will be skipped. 2538069ec957SQu Wenruo */ 2539069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2540069ec957SQu Wenruo { 2541069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2542069ec957SQu Wenruo } 2543069ec957SQu Wenruo 254475cb857dSQu Wenruo /* 254575cb857dSQu Wenruo * Validation of super block at write time. 254675cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 254775cb857dSQu Wenruo * overwritten soon. 254875cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 254975cb857dSQu Wenruo */ 255075cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 255175cb857dSQu Wenruo struct btrfs_super_block *sb) 255275cb857dSQu Wenruo { 255375cb857dSQu Wenruo int ret; 255475cb857dSQu Wenruo 255575cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 255675cb857dSQu Wenruo if (ret < 0) 255775cb857dSQu Wenruo goto out; 2558e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 255975cb857dSQu Wenruo ret = -EUCLEAN; 256075cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 256175cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 256275cb857dSQu Wenruo goto out; 256375cb857dSQu Wenruo } 256475cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 256575cb857dSQu Wenruo ret = -EUCLEAN; 256675cb857dSQu Wenruo btrfs_err(fs_info, 256775cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 256875cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 256975cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 257075cb857dSQu Wenruo goto out; 257175cb857dSQu Wenruo } 257275cb857dSQu Wenruo out: 257375cb857dSQu Wenruo if (ret < 0) 257475cb857dSQu Wenruo btrfs_err(fs_info, 257575cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 257675cb857dSQu Wenruo return ret; 257775cb857dSQu Wenruo } 257875cb857dSQu Wenruo 25796ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2580b8522a1eSNikolay Borisov { 25816ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2582b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2583b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2584b8522a1eSNikolay Borisov bool handle_error = false; 2585b8522a1eSNikolay Borisov int ret = 0; 2586b8522a1eSNikolay Borisov int i; 2587b8522a1eSNikolay Borisov 2588b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2589b8522a1eSNikolay Borisov u64 generation; 2590b8522a1eSNikolay Borisov int level; 2591b8522a1eSNikolay Borisov 2592b8522a1eSNikolay Borisov if (handle_error) { 2593b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2594b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2595b8522a1eSNikolay Borisov tree_root->node = NULL; 2596b8522a1eSNikolay Borisov 2597b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2598b8522a1eSNikolay Borisov break; 2599b8522a1eSNikolay Borisov 2600b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2601b8522a1eSNikolay Borisov 2602b8522a1eSNikolay Borisov /* 2603b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2604b8522a1eSNikolay Borisov * valid 2605b8522a1eSNikolay Borisov */ 2606b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2607b8522a1eSNikolay Borisov 2608b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2609b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2610b8522a1eSNikolay Borisov 2611b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26126ef108ddSNikolay Borisov backup_index = ret; 2613b8522a1eSNikolay Borisov if (ret < 0) 2614b8522a1eSNikolay Borisov return ret; 2615b8522a1eSNikolay Borisov } 2616b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2617b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2618b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 2619b8522a1eSNikolay Borisov generation, level, NULL); 2620b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node) || 2621b8522a1eSNikolay Borisov !extent_buffer_uptodate(tree_root->node)) { 2622b8522a1eSNikolay Borisov handle_error = true; 2623b8522a1eSNikolay Borisov 2624b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node)) 2625b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 2626b8522a1eSNikolay Borisov else if (!extent_buffer_uptodate(tree_root->node)) 2627b8522a1eSNikolay Borisov ret = -EUCLEAN; 2628b8522a1eSNikolay Borisov 2629b8522a1eSNikolay Borisov btrfs_warn(fs_info, "failed to read tree root"); 2630b8522a1eSNikolay Borisov continue; 2631b8522a1eSNikolay Borisov } 2632b8522a1eSNikolay Borisov 2633b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2634b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2635b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2636b8522a1eSNikolay Borisov 2637336a0d8dSNikolay Borisov /* 2638336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2639336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2640336a0d8dSNikolay Borisov */ 2641b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2642b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2643b8522a1eSNikolay Borisov if (ret < 0) { 2644b8522a1eSNikolay Borisov handle_error = true; 2645b8522a1eSNikolay Borisov continue; 2646b8522a1eSNikolay Borisov } 2647b8522a1eSNikolay Borisov 2648b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2649b8522a1eSNikolay Borisov 2650b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2651b8522a1eSNikolay Borisov if (ret < 0) { 2652b8522a1eSNikolay Borisov handle_error = true; 2653b8522a1eSNikolay Borisov continue; 2654b8522a1eSNikolay Borisov } 2655b8522a1eSNikolay Borisov 2656b8522a1eSNikolay Borisov /* All successful */ 2657b8522a1eSNikolay Borisov fs_info->generation = generation; 2658b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26596ef108ddSNikolay Borisov 26606ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26616ef108ddSNikolay Borisov if (backup_index < 0) { 26626ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26636ef108ddSNikolay Borisov } else { 26646ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26656ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26666ef108ddSNikolay Borisov } 2667b8522a1eSNikolay Borisov break; 2668b8522a1eSNikolay Borisov } 2669b8522a1eSNikolay Borisov 2670b8522a1eSNikolay Borisov return ret; 2671b8522a1eSNikolay Borisov } 2672b8522a1eSNikolay Borisov 26738260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2674eb60ceacSChris Mason { 267576dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2676f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26778fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2678facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 267924bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2680eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 268111833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2682eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2683a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 268476dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 268524bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26864cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2687ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2688f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 268947ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2690f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2691d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 269267c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26937585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2694573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2695de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 269619c00ddcSChris Mason 26970b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26986324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2699f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 270047ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2701bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2702bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 2703bd647ce3SJosef Bacik #endif 2704c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 270566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 270666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 270766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 270866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 270966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 271066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 271166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2712ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2713ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2714ba2c4d4eSJosef Bacik 2715771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27164cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27172fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2718034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2719fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 272095ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27219ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27224cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2723a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2724f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27258b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2726f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 272790519d66SArne Jansen /* readahead state */ 2728d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 272990519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2730fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2731c8b97818SChris Mason 2732b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2733b34b086cSChris Mason num_online_cpus() + 2, 8); 27340afbaf8cSChris Mason 2735199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2736199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 273769fe2d75SJosef Bacik 2738638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 273921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 274021adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 274121adbd5cSStefan Behrens #endif 2742779a65a4SEric Sandeen btrfs_init_balance(fs_info); 274321c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2744a2de733cSArne Jansen 27450f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 27466bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2747a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 27480f9dd46cSJosef Bacik 2749fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2750fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2751afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 275239279cc3SChris Mason 27535a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2754e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2755925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2756a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2757a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27581bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27599e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2760c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 276176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2762803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2763fa9c0d79SChris Mason 2764ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2765f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2766b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2767416ac51dSArne Jansen 2768fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2769fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2770fa9c0d79SChris Mason 2771e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2772f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2773bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27744854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2775034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 27763768f368SChris Mason 2777da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2778da17066cSJeff Mahoney fs_info->nodesize = 4096; 2779da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2780da17066cSJeff Mahoney fs_info->stripesize = 4096; 2781da17066cSJeff Mahoney 2782eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2783eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2784eede2bf3SOmar Sandoval 27859e967495SFilipe Manana fs_info->send_in_progress = 0; 27868260edbaSJosef Bacik } 27878260edbaSJosef Bacik 27888260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 27898260edbaSJosef Bacik { 27908260edbaSJosef Bacik int ret; 27918260edbaSJosef Bacik 27928260edbaSJosef Bacik fs_info->sb = sb; 27938260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 27948260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 27959e967495SFilipe Manana 2796ae18c37aSJosef Bacik ret = init_srcu_struct(&fs_info->subvol_srcu); 2797ae18c37aSJosef Bacik if (ret) 2798ae18c37aSJosef Bacik return ret; 2799ae18c37aSJosef Bacik 2800ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2801ae18c37aSJosef Bacik if (ret) 2802ae18c37aSJosef Bacik goto fail; 2803ae18c37aSJosef Bacik 2804ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2805ae18c37aSJosef Bacik if (ret) 2806ae18c37aSJosef Bacik goto fail; 2807ae18c37aSJosef Bacik 2808ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2809ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2810ae18c37aSJosef Bacik 2811ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2812ae18c37aSJosef Bacik if (ret) 2813ae18c37aSJosef Bacik goto fail; 2814ae18c37aSJosef Bacik 2815ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2816ae18c37aSJosef Bacik GFP_KERNEL); 2817ae18c37aSJosef Bacik if (ret) 2818ae18c37aSJosef Bacik goto fail; 2819ae18c37aSJosef Bacik 2820ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2821ae18c37aSJosef Bacik GFP_KERNEL); 2822ae18c37aSJosef Bacik if (!fs_info->delayed_root) { 2823ae18c37aSJosef Bacik ret = -ENOMEM; 2824ae18c37aSJosef Bacik goto fail; 2825ae18c37aSJosef Bacik } 2826ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2827ae18c37aSJosef Bacik 282853b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 2829ae18c37aSJosef Bacik if (ret) 2830ae18c37aSJosef Bacik goto fail; 2831ae18c37aSJosef Bacik 2832ae18c37aSJosef Bacik return 0; 2833ae18c37aSJosef Bacik fail: 2834ae18c37aSJosef Bacik cleanup_srcu_struct(&fs_info->subvol_srcu); 2835ae18c37aSJosef Bacik return ret; 2836ae18c37aSJosef Bacik } 2837ae18c37aSJosef Bacik 283897f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 283997f4dd09SNikolay Borisov { 284097f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 284197f4dd09SNikolay Borisov int ret; 284297f4dd09SNikolay Borisov 284397f4dd09SNikolay Borisov /* 284497f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 284597f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 284697f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 284797f4dd09SNikolay Borisov */ 284897f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 284997f4dd09SNikolay Borisov if (ret < 0) { 285097f4dd09SNikolay Borisov btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); 285197f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 285297f4dd09SNikolay Borisov return ret; 285397f4dd09SNikolay Borisov } 285497f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 285597f4dd09SNikolay Borisov } 285697f4dd09SNikolay Borisov 285797f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 285897f4dd09SNikolay Borisov { 285997f4dd09SNikolay Borisov struct task_struct *task; 286097f4dd09SNikolay Borisov 286197f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 286297f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 286397f4dd09SNikolay Borisov if (IS_ERR(task)) { 286497f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 286597f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 286697f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 286797f4dd09SNikolay Borisov return PTR_ERR(task); 286897f4dd09SNikolay Borisov } 286997f4dd09SNikolay Borisov 287097f4dd09SNikolay Borisov return 0; 287197f4dd09SNikolay Borisov } 287297f4dd09SNikolay Borisov 2873ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 2874ae18c37aSJosef Bacik char *options) 2875ae18c37aSJosef Bacik { 2876ae18c37aSJosef Bacik u32 sectorsize; 2877ae18c37aSJosef Bacik u32 nodesize; 2878ae18c37aSJosef Bacik u32 stripesize; 2879ae18c37aSJosef Bacik u64 generation; 2880ae18c37aSJosef Bacik u64 features; 2881ae18c37aSJosef Bacik u16 csum_type; 2882ae18c37aSJosef Bacik struct btrfs_key location; 2883ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 2884ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2885ae18c37aSJosef Bacik struct btrfs_root *tree_root; 2886ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 2887ae18c37aSJosef Bacik int ret; 2888ae18c37aSJosef Bacik int err = -EINVAL; 2889ae18c37aSJosef Bacik int clear_free_space_tree = 0; 2890ae18c37aSJosef Bacik int level; 2891ae18c37aSJosef Bacik 28928260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 289353b381b3SDavid Woodhouse if (ret) { 289483c8266aSDavid Sterba err = ret; 2895ae18c37aSJosef Bacik goto fail; 289653b381b3SDavid Woodhouse } 289753b381b3SDavid Woodhouse 2898ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 2899ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 2900ae18c37aSJosef Bacik GFP_KERNEL); 2901ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 2902ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 2903ae18c37aSJosef Bacik GFP_KERNEL); 2904ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 2905ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 2906ae18c37aSJosef Bacik err = -ENOMEM; 2907ae18c37aSJosef Bacik goto fail_srcu; 2908ae18c37aSJosef Bacik } 2909ae18c37aSJosef Bacik 2910ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 2911ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 2912ae18c37aSJosef Bacik err = -ENOMEM; 2913ae18c37aSJosef Bacik goto fail_srcu; 2914ae18c37aSJosef Bacik } 2915ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 2916ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 2917ae18c37aSJosef Bacik 29183c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 29191104a885SDavid Sterba 29201104a885SDavid Sterba /* 29211104a885SDavid Sterba * Read super block and check the signature bytes only 29221104a885SDavid Sterba */ 29238f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 29248f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 29258f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 292616cdcec7SMiao Xie goto fail_alloc; 292720b45077SDave Young } 292839279cc3SChris Mason 29291104a885SDavid Sterba /* 29308dc3f22cSJohannes Thumshirn * Verify the type first, if that or the the checksum value are 29318dc3f22cSJohannes Thumshirn * corrupted, we'll find out 29328dc3f22cSJohannes Thumshirn */ 29338f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 293451bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 29358dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 293651bce6c9SJohannes Thumshirn csum_type); 29378dc3f22cSJohannes Thumshirn err = -EINVAL; 29388f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29398dc3f22cSJohannes Thumshirn goto fail_alloc; 29408dc3f22cSJohannes Thumshirn } 29418dc3f22cSJohannes Thumshirn 29426d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 29436d97c6e3SJohannes Thumshirn if (ret) { 29446d97c6e3SJohannes Thumshirn err = ret; 29458f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29466d97c6e3SJohannes Thumshirn goto fail_alloc; 29476d97c6e3SJohannes Thumshirn } 29486d97c6e3SJohannes Thumshirn 29498dc3f22cSJohannes Thumshirn /* 29501104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 29511104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 29521104a885SDavid Sterba */ 29538f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 295405135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 29551104a885SDavid Sterba err = -EINVAL; 29568f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 2957141386e1SJosef Bacik goto fail_alloc; 29581104a885SDavid Sterba } 29591104a885SDavid Sterba 29601104a885SDavid Sterba /* 29611104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 29621104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29631104a885SDavid Sterba * the whole block of INFO_SIZE 29641104a885SDavid Sterba */ 29658f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 29668f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29675f39d397SChris Mason 2968fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2969fbc6feaeSNikolay Borisov 2970de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2971de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2972de37aa51SNikolay Borisov 29737239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2974de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2975de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2976de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29777239ff4bSNikolay Borisov } 29780b86a832SChris Mason 2979fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2980fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2981fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2982fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2983fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2984fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2985fbc6feaeSNikolay Borisov } 2986fbc6feaeSNikolay Borisov 2987fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2988fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2989de37aa51SNikolay Borisov 2990069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 29911104a885SDavid Sterba if (ret) { 299205135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 29931104a885SDavid Sterba err = -EINVAL; 2994141386e1SJosef Bacik goto fail_alloc; 29951104a885SDavid Sterba } 29961104a885SDavid Sterba 29970f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 2998141386e1SJosef Bacik goto fail_alloc; 29990f7d52f4SChris Mason 3000acce952bSliubo /* check FS state, whether FS is broken. */ 300187533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 300287533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3003acce952bSliubo 300475e7cb7fSLiu Bo /* 300575e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 300675e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 300775e7cb7fSLiu Bo */ 300875e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 300975e7cb7fSLiu Bo 30102ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 30112b82032cSYan Zheng if (ret) { 30122b82032cSYan Zheng err = ret; 3013141386e1SJosef Bacik goto fail_alloc; 30142b82032cSYan Zheng } 3015dfe25020SChris Mason 3016f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3017f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3018f2b636e8SJosef Bacik if (features) { 301905135f59SDavid Sterba btrfs_err(fs_info, 302005135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3021c1c9ff7cSGeert Uytterhoeven features); 3022f2b636e8SJosef Bacik err = -EINVAL; 3023141386e1SJosef Bacik goto fail_alloc; 3024f2b636e8SJosef Bacik } 3025f2b636e8SJosef Bacik 30265d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 30275d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 30280b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3029a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 30305c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 30315c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3032727011e0SChris Mason 30333173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 303405135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 30353173a18fSJosef Bacik 3036727011e0SChris Mason /* 3037727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3038727011e0SChris Mason * they are bigger than the page size 3039727011e0SChris Mason */ 304009cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3041727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 304205135f59SDavid Sterba btrfs_info(fs_info, 304305135f59SDavid Sterba "flagging fs with big metadata feature"); 3044727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3045727011e0SChris Mason } 3046727011e0SChris Mason 3047bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3048bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3049b7f67055SChandan Rajendra stripesize = sectorsize; 3050707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3051963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3052bc3f116fSChris Mason 3053da17066cSJeff Mahoney /* Cache block sizes */ 3054da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3055da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3056da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3057da17066cSJeff Mahoney 3058bc3f116fSChris Mason /* 3059bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3060bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3061bc3f116fSChris Mason */ 3062bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3063707e8a07SDavid Sterba (sectorsize != nodesize)) { 306405135f59SDavid Sterba btrfs_err(fs_info, 306505135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 306605135f59SDavid Sterba nodesize, sectorsize); 3067141386e1SJosef Bacik goto fail_alloc; 3068bc3f116fSChris Mason } 3069bc3f116fSChris Mason 3070ceda0864SMiao Xie /* 3071ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3072ceda0864SMiao Xie * update the flag. 3073ceda0864SMiao Xie */ 30745d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 30755d4f98a2SYan Zheng 3076f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3077f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3078bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 307905135f59SDavid Sterba btrfs_err(fs_info, 308005135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3081c1c9ff7cSGeert Uytterhoeven features); 3082f2b636e8SJosef Bacik err = -EINVAL; 3083141386e1SJosef Bacik goto fail_alloc; 3084f2b636e8SJosef Bacik } 308561d92c32SChris Mason 30862a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 30872a458198SEric Sandeen if (ret) { 30882a458198SEric Sandeen err = ret; 30890dc3b84aSJosef Bacik goto fail_sb_buffer; 30900dc3b84aSJosef Bacik } 30914543df7eSChris Mason 30929e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 30939e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 30949e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 3095b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 30969e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 30979e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 30984575c9ccSChris Mason 3099a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3100a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3101de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3102db94535dSChris Mason 3103925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 31046bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3105925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 310684eed90fSChris Mason if (ret) { 310705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 31085d4f98a2SYan Zheng goto fail_sb_buffer; 310984eed90fSChris Mason } 31100b86a832SChris Mason 311184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3112581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 31130b86a832SChris Mason 31142ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 31150b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3116581c1760SQu Wenruo generation, level, NULL); 311764c043deSLiu Bo if (IS_ERR(chunk_root->node) || 311864c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 311905135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3120e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3121e5fffbacSchandan free_extent_buffer(chunk_root->node); 312295ab1f64SZhao Lei chunk_root->node = NULL; 3123af31f5e5SChris Mason goto fail_tree_roots; 312483121942SDavid Woodhouse } 31255d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 31265d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 31270b86a832SChris Mason 3128e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3129b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 3130e17cade2SChris Mason 31315b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 31322b82032cSYan Zheng if (ret) { 313305135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3134af31f5e5SChris Mason goto fail_tree_roots; 31352b82032cSYan Zheng } 31360b86a832SChris Mason 31378dabb742SStefan Behrens /* 31389b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 31399b99b115SAnand Jain * device replace procedure 31408dabb742SStefan Behrens */ 31419b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3142dfe25020SChris Mason 3143a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 314405135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3145a6b0d5c8SChris Mason goto fail_tree_roots; 3146a6b0d5c8SChris Mason } 3147a6b0d5c8SChris Mason 3148b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 31494bbcaa64SEric Sandeen if (ret) 3150b8522a1eSNikolay Borisov goto fail_tree_roots; 31518929ecfaSYan, Zheng 3152cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3153cf90d884SQu Wenruo if (ret) { 3154cf90d884SQu Wenruo btrfs_err(fs_info, 3155cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3156cf90d884SQu Wenruo ret); 3157cf90d884SQu Wenruo goto fail_block_groups; 3158cf90d884SQu Wenruo } 315968310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 316068310a5eSIlya Dryomov if (ret) { 316105135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 316268310a5eSIlya Dryomov goto fail_block_groups; 316368310a5eSIlya Dryomov } 316468310a5eSIlya Dryomov 3165733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3166733f4fbbSStefan Behrens if (ret) { 316705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3168733f4fbbSStefan Behrens goto fail_block_groups; 3169733f4fbbSStefan Behrens } 3170733f4fbbSStefan Behrens 31718dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 31728dabb742SStefan Behrens if (ret) { 317305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 31748dabb742SStefan Behrens goto fail_block_groups; 31758dabb742SStefan Behrens } 31768dabb742SStefan Behrens 31779b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 31788dabb742SStefan Behrens 3179c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3180b7c35e81SAnand Jain if (ret) { 318105135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 318205135f59SDavid Sterba ret); 3183b7c35e81SAnand Jain goto fail_block_groups; 3184b7c35e81SAnand Jain } 3185b7c35e81SAnand Jain 318696f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31875ac1d209SJeff Mahoney if (ret) { 318805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3189b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31905ac1d209SJeff Mahoney } 31915ac1d209SJeff Mahoney 3192c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3193c59021f8Sliubo if (ret) { 319405135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31952365dd3cSAnand Jain goto fail_sysfs; 3196c59021f8Sliubo } 3197c59021f8Sliubo 31985b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31991b1d1f66SJosef Bacik if (ret) { 320005135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 32012365dd3cSAnand Jain goto fail_sysfs; 32021b1d1f66SJosef Bacik } 32034330e183SQu Wenruo 32046528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 320505135f59SDavid Sterba btrfs_warn(fs_info, 320652042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 32072365dd3cSAnand Jain goto fail_sysfs; 3208292fd7fcSStefan Behrens } 32099078a3e1SChris Mason 3210a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3211a74a4b97SChris Mason "btrfs-cleaner"); 321257506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 32132365dd3cSAnand Jain goto fail_sysfs; 3214a74a4b97SChris Mason 3215a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3216a74a4b97SChris Mason tree_root, 3217a74a4b97SChris Mason "btrfs-transaction"); 321857506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 32193f157a2fSChris Mason goto fail_cleaner; 3220a74a4b97SChris Mason 3221583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3222c289811cSChris Mason !fs_info->fs_devices->rotating) { 3223583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3224c289811cSChris Mason } 3225c289811cSChris Mason 3226572d9ab7SDavid Sterba /* 322701327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3228572d9ab7SDavid Sterba * not have to wait for transaction commit 3229572d9ab7SDavid Sterba */ 3230572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 32313818aea2SQu Wenruo 323221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 32330b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 32342ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 32350b246afaSJeff Mahoney btrfs_test_opt(fs_info, 323621adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 323721adbd5cSStefan Behrens 1 : 0, 323821adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 323921adbd5cSStefan Behrens if (ret) 324005135f59SDavid Sterba btrfs_warn(fs_info, 324105135f59SDavid Sterba "failed to initialize integrity check module: %d", 324205135f59SDavid Sterba ret); 324321adbd5cSStefan Behrens } 324421adbd5cSStefan Behrens #endif 3245bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3246bcef60f2SArne Jansen if (ret) 3247bcef60f2SArne Jansen goto fail_trans_kthread; 324821adbd5cSStefan Behrens 3249fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3250fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3251fd708b81SJosef Bacik 325296da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 325396da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32540b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3255e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 325663443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 325779787eaaSJeff Mahoney if (ret) { 325863443bf5SEric Sandeen err = ret; 325928c16cbbSWang Shilong goto fail_qgroup; 3260e556ce2cSYan Zheng } 3261e02119d5SChris Mason } 32621a40e23bSZheng Yan 32636bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 326479787eaaSJeff Mahoney if (ret) 326528c16cbbSWang Shilong goto fail_qgroup; 326676dda93cSYan, Zheng 3267bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3268d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 326944c44af2SIlya Dryomov if (ret) 327028c16cbbSWang Shilong goto fail_qgroup; 327190c711abSZygo Blaxell 327290c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 32735d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 327490c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3275d7ce5843SMiao Xie if (ret < 0) { 327605135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 327705135f59SDavid Sterba ret); 3278d7ce5843SMiao Xie err = -EINVAL; 3279bcef60f2SArne Jansen goto fail_qgroup; 3280d7ce5843SMiao Xie } 32817c2ca468SChris Mason } 32821a40e23bSZheng Yan 32833de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32843de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3285cb517eabSMiao Xie location.offset = 0; 32863de4586cSChris Mason 32873619c94fSJosef Bacik fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true); 32883140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32893140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3290f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3291315bf8efSJosef Bacik fs_info->fs_root = NULL; 3292bcef60f2SArne Jansen goto fail_qgroup; 32933140c9a3SDan Carpenter } 3294c289811cSChris Mason 3295bc98a42cSDavid Howells if (sb_rdonly(sb)) 32962b6ba629SIlya Dryomov return 0; 32972b6ba629SIlya Dryomov 3298f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3299f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33006675df31SOmar Sandoval clear_free_space_tree = 1; 33016675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33026675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33036675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 33046675df31SOmar Sandoval clear_free_space_tree = 1; 33056675df31SOmar Sandoval } 33066675df31SOmar Sandoval 33076675df31SOmar Sandoval if (clear_free_space_tree) { 3308f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3309f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3310f8d468a1SOmar Sandoval if (ret) { 3311f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3312f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 33136bccf3abSJeff Mahoney close_ctree(fs_info); 3314f8d468a1SOmar Sandoval return ret; 3315f8d468a1SOmar Sandoval } 3316f8d468a1SOmar Sandoval } 3317f8d468a1SOmar Sandoval 33180b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3319511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 332005135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3321511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3322511711afSChris Mason if (ret) { 332305135f59SDavid Sterba btrfs_warn(fs_info, 332405135f59SDavid Sterba "failed to create free space tree: %d", ret); 33256bccf3abSJeff Mahoney close_ctree(fs_info); 3326511711afSChris Mason return ret; 3327511711afSChris Mason } 3328511711afSChris Mason } 3329511711afSChris Mason 3330e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 33312b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33322b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33332b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 33346bccf3abSJeff Mahoney close_ctree(fs_info); 33352b6ba629SIlya Dryomov return ret; 33362b6ba629SIlya Dryomov } 3337e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 333859641015SIlya Dryomov 33392b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 33402b6ba629SIlya Dryomov if (ret) { 334105135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 33426bccf3abSJeff Mahoney close_ctree(fs_info); 33432b6ba629SIlya Dryomov return ret; 3344e3acc2a6SJosef Bacik } 3345e3acc2a6SJosef Bacik 33468dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 33478dabb742SStefan Behrens if (ret) { 334805135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33496bccf3abSJeff Mahoney close_ctree(fs_info); 33508dabb742SStefan Behrens return ret; 33518dabb742SStefan Behrens } 33528dabb742SStefan Behrens 3353b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3354b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3355b382a324SJan Schmidt 33564bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 335705135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3358f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3359f7a81ea4SStefan Behrens if (ret) { 336005135f59SDavid Sterba btrfs_warn(fs_info, 336105135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33626bccf3abSJeff Mahoney close_ctree(fs_info); 3363f7a81ea4SStefan Behrens return ret; 3364f7a81ea4SStefan Behrens } 33650b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33664bbcaa64SEric Sandeen fs_info->generation != 33674bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 336805135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 336970f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 337070f80175SStefan Behrens if (ret) { 337105135f59SDavid Sterba btrfs_warn(fs_info, 337205135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 33736bccf3abSJeff Mahoney close_ctree(fs_info); 337470f80175SStefan Behrens return ret; 337570f80175SStefan Behrens } 337670f80175SStefan Behrens } else { 3377afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3378f7a81ea4SStefan Behrens } 3379afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 338047ab2a6cSJosef Bacik 33818dcddfa0SQu Wenruo /* 33828dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33838dcddfa0SQu Wenruo * no need to keep the flag 33848dcddfa0SQu Wenruo */ 33858dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33868dcddfa0SQu Wenruo 3387ad2b2c80SAl Viro return 0; 338839279cc3SChris Mason 3389bcef60f2SArne Jansen fail_qgroup: 3390bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33917c2ca468SChris Mason fail_trans_kthread: 33927c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33932ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3394faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33953f157a2fSChris Mason fail_cleaner: 3396a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33977c2ca468SChris Mason 33987c2ca468SChris Mason /* 33997c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 34007c2ca468SChris Mason * kthreads 34017c2ca468SChris Mason */ 34027c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 34037c2ca468SChris Mason 34042365dd3cSAnand Jain fail_sysfs: 34056618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 34062365dd3cSAnand Jain 3407b7c35e81SAnand Jain fail_fsdev_sysfs: 3408b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3409b7c35e81SAnand Jain 34101b1d1f66SJosef Bacik fail_block_groups: 341154067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3412af31f5e5SChris Mason 3413af31f5e5SChris Mason fail_tree_roots: 34144273eaffSAnand Jain free_root_pointers(fs_info, true); 34152b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3416af31f5e5SChris Mason 341739279cc3SChris Mason fail_sb_buffer: 34187abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 34195cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 342016cdcec7SMiao Xie fail_alloc: 3421586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3422586e46e2SIlya Dryomov 34234543df7eSChris Mason iput(fs_info->btree_inode); 342476dda93cSYan, Zheng fail_srcu: 342576dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 34267e662854SQinghuang Feng fail: 3427586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3428ad2b2c80SAl Viro return err; 3429eb60ceacSChris Mason } 3430663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3431eb60ceacSChris Mason 3432314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3433f2984462SChris Mason { 3434314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3435314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3436314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3437314b6dd0SJohannes Thumshirn struct page *page; 3438442a4f63SStefan Behrens 3439314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3440314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3441314b6dd0SJohannes Thumshirn 3442314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3443fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3444314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3445314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3446314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3447314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3448314b6dd0SJohannes Thumshirn SetPageError(page); 3449314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3450314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3451314b6dd0SJohannes Thumshirn } else { 3452314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3453f2984462SChris Mason } 3454314b6dd0SJohannes Thumshirn 3455314b6dd0SJohannes Thumshirn put_page(page); 3456314b6dd0SJohannes Thumshirn unlock_page(page); 3457314b6dd0SJohannes Thumshirn } 3458314b6dd0SJohannes Thumshirn 3459314b6dd0SJohannes Thumshirn bio_put(bio); 3460f2984462SChris Mason } 3461f2984462SChris Mason 34628f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 34638f32380dSJohannes Thumshirn int copy_num) 346429c36d72SAnand Jain { 346529c36d72SAnand Jain struct btrfs_super_block *super; 34668f32380dSJohannes Thumshirn struct page *page; 346729c36d72SAnand Jain u64 bytenr; 34688f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 346929c36d72SAnand Jain 347029c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 347129c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 34728f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 347329c36d72SAnand Jain 34748f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 34758f32380dSJohannes Thumshirn if (IS_ERR(page)) 34768f32380dSJohannes Thumshirn return ERR_CAST(page); 347729c36d72SAnand Jain 34788f32380dSJohannes Thumshirn super = page_address(page); 347929c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 348029c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 34818f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 34828f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 348329c36d72SAnand Jain } 348429c36d72SAnand Jain 34858f32380dSJohannes Thumshirn return super; 348629c36d72SAnand Jain } 348729c36d72SAnand Jain 348829c36d72SAnand Jain 34898f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3490a512bbf8SYan Zheng { 34918f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3492a512bbf8SYan Zheng int i; 3493a512bbf8SYan Zheng u64 transid = 0; 3494a512bbf8SYan Zheng 3495a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3496a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3497a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3498a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3499a512bbf8SYan Zheng */ 3500a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 35018f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 35028f32380dSJohannes Thumshirn if (IS_ERR(super)) 3503a512bbf8SYan Zheng continue; 3504a512bbf8SYan Zheng 3505a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 35068f32380dSJohannes Thumshirn if (latest) 35078f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 35088f32380dSJohannes Thumshirn 35098f32380dSJohannes Thumshirn latest = super; 3510a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3511a512bbf8SYan Zheng } 3512a512bbf8SYan Zheng } 351392fc03fbSAnand Jain 35148f32380dSJohannes Thumshirn return super; 3515a512bbf8SYan Zheng } 3516a512bbf8SYan Zheng 35174eedeb75SHisashi Hifumi /* 3518abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3519314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 35204eedeb75SHisashi Hifumi * 3521abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3522abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3523abbb3b8eSDavid Sterba * same for write and wait phases. 35244eedeb75SHisashi Hifumi * 3525314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 35264eedeb75SHisashi Hifumi */ 3527a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3528abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3529a512bbf8SYan Zheng { 3530d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3531314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3532d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3533a512bbf8SYan Zheng int i; 3534a512bbf8SYan Zheng int errors = 0; 3535a512bbf8SYan Zheng u64 bytenr; 3536a512bbf8SYan Zheng 3537a512bbf8SYan Zheng if (max_mirrors == 0) 3538a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3539a512bbf8SYan Zheng 3540d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3541d5178578SJohannes Thumshirn 3542a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3543314b6dd0SJohannes Thumshirn struct page *page; 3544314b6dd0SJohannes Thumshirn struct bio *bio; 3545314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3546314b6dd0SJohannes Thumshirn 3547a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3548935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3549935e5cc9SMiao Xie device->commit_total_bytes) 3550a512bbf8SYan Zheng break; 3551a512bbf8SYan Zheng 3552a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3553a512bbf8SYan Zheng 3554d5178578SJohannes Thumshirn crypto_shash_init(shash); 3555d5178578SJohannes Thumshirn crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3556abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3557d5178578SJohannes Thumshirn crypto_shash_final(shash, sb->csum); 3558a512bbf8SYan Zheng 3559314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3560314b6dd0SJohannes Thumshirn GFP_NOFS); 3561314b6dd0SJohannes Thumshirn if (!page) { 3562fb456252SJeff Mahoney btrfs_err(device->fs_info, 3563314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3564f14d104dSDavid Sterba bytenr); 3565634554dcSJosef Bacik errors++; 3566634554dcSJosef Bacik continue; 3567634554dcSJosef Bacik } 3568634554dcSJosef Bacik 3569314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3570314b6dd0SJohannes Thumshirn get_page(page); 3571a512bbf8SYan Zheng 3572314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3573314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3574a512bbf8SYan Zheng 3575387125fcSChris Mason /* 3576314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3577314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3578314b6dd0SJohannes Thumshirn * checking. 3579387125fcSChris Mason */ 3580314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3581314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3582314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3583314b6dd0SJohannes Thumshirn bio->bi_private = device; 3584314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3585314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3586314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3587314b6dd0SJohannes Thumshirn 3588314b6dd0SJohannes Thumshirn /* 3589314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3590314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3591314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3592314b6dd0SJohannes Thumshirn */ 3593314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 35941b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3595314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3596314b6dd0SJohannes Thumshirn 3597314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 3598a512bbf8SYan Zheng } 3599a512bbf8SYan Zheng return errors < i ? 0 : -1; 3600a512bbf8SYan Zheng } 3601a512bbf8SYan Zheng 3602387125fcSChris Mason /* 3603abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3604abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3605abbb3b8eSDavid Sterba * 3606314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3607abbb3b8eSDavid Sterba * date. 3608abbb3b8eSDavid Sterba */ 3609abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3610abbb3b8eSDavid Sterba { 3611abbb3b8eSDavid Sterba int i; 3612abbb3b8eSDavid Sterba int errors = 0; 3613b6a535faSHoward McLauchlan bool primary_failed = false; 3614abbb3b8eSDavid Sterba u64 bytenr; 3615abbb3b8eSDavid Sterba 3616abbb3b8eSDavid Sterba if (max_mirrors == 0) 3617abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3618abbb3b8eSDavid Sterba 3619abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3620314b6dd0SJohannes Thumshirn struct page *page; 3621314b6dd0SJohannes Thumshirn 3622abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3623abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3624abbb3b8eSDavid Sterba device->commit_total_bytes) 3625abbb3b8eSDavid Sterba break; 3626abbb3b8eSDavid Sterba 3627314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3628314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3629314b6dd0SJohannes Thumshirn if (!page) { 3630abbb3b8eSDavid Sterba errors++; 3631b6a535faSHoward McLauchlan if (i == 0) 3632b6a535faSHoward McLauchlan primary_failed = true; 3633abbb3b8eSDavid Sterba continue; 3634abbb3b8eSDavid Sterba } 3635314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3636314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3637314b6dd0SJohannes Thumshirn if (PageError(page)) { 3638abbb3b8eSDavid Sterba errors++; 3639b6a535faSHoward McLauchlan if (i == 0) 3640b6a535faSHoward McLauchlan primary_failed = true; 3641b6a535faSHoward McLauchlan } 3642abbb3b8eSDavid Sterba 3643314b6dd0SJohannes Thumshirn /* Drop our reference */ 3644314b6dd0SJohannes Thumshirn put_page(page); 3645abbb3b8eSDavid Sterba 3646314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3647314b6dd0SJohannes Thumshirn put_page(page); 3648abbb3b8eSDavid Sterba } 3649abbb3b8eSDavid Sterba 3650b6a535faSHoward McLauchlan /* log error, force error return */ 3651b6a535faSHoward McLauchlan if (primary_failed) { 3652b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3653b6a535faSHoward McLauchlan device->devid); 3654b6a535faSHoward McLauchlan return -1; 3655b6a535faSHoward McLauchlan } 3656b6a535faSHoward McLauchlan 3657abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3658abbb3b8eSDavid Sterba } 3659abbb3b8eSDavid Sterba 3660abbb3b8eSDavid Sterba /* 3661387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3662387125fcSChris Mason * for the barrier when it is done 3663387125fcSChris Mason */ 36644246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3665387125fcSChris Mason { 3666387125fcSChris Mason complete(bio->bi_private); 3667387125fcSChris Mason } 3668387125fcSChris Mason 3669387125fcSChris Mason /* 36704fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36714fc6441aSAnand Jain * done in the waiting counterpart. 3672387125fcSChris Mason */ 36734fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3674387125fcSChris Mason { 3675c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3676e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3677387125fcSChris Mason 3678c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36794fc6441aSAnand Jain return; 3680387125fcSChris Mason 3681e0ae9994SDavid Sterba bio_reset(bio); 3682387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 368374d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36848d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3685387125fcSChris Mason init_completion(&device->flush_wait); 3686387125fcSChris Mason bio->bi_private = &device->flush_wait; 3687387125fcSChris Mason 368843a01111SLu Fengqi btrfsic_submit_bio(bio); 36891c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36904fc6441aSAnand Jain } 3691387125fcSChris Mason 36924fc6441aSAnand Jain /* 36934fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36944fc6441aSAnand Jain */ 36958c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36964fc6441aSAnand Jain { 36974fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36984fc6441aSAnand Jain 36991c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 370058efbc9fSOmar Sandoval return BLK_STS_OK; 37014fc6441aSAnand Jain 37021c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 37032980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 37044fc6441aSAnand Jain 37058c27cb35SLinus Torvalds return bio->bi_status; 3706387125fcSChris Mason } 3707387125fcSChris Mason 3708d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3709401b41e5SAnand Jain { 37106528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3711401b41e5SAnand Jain return -EIO; 3712387125fcSChris Mason return 0; 3713387125fcSChris Mason } 3714387125fcSChris Mason 3715387125fcSChris Mason /* 3716387125fcSChris Mason * send an empty flush down to each device in parallel, 3717387125fcSChris Mason * then wait for them 3718387125fcSChris Mason */ 3719387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3720387125fcSChris Mason { 3721387125fcSChris Mason struct list_head *head; 3722387125fcSChris Mason struct btrfs_device *dev; 37235af3e8ccSStefan Behrens int errors_wait = 0; 37244e4cbee9SChristoph Hellwig blk_status_t ret; 3725387125fcSChris Mason 37261538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3727387125fcSChris Mason /* send down all the barriers */ 3728387125fcSChris Mason head = &info->fs_devices->devices; 37291538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3730e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3731f88ba6a2SHidetoshi Seto continue; 3732cea7c8bfSAnand Jain if (!dev->bdev) 3733387125fcSChris Mason continue; 3734e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3735ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3736387125fcSChris Mason continue; 3737387125fcSChris Mason 37384fc6441aSAnand Jain write_dev_flush(dev); 373958efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3740387125fcSChris Mason } 3741387125fcSChris Mason 3742387125fcSChris Mason /* wait for all the barriers */ 37431538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3744e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3745f88ba6a2SHidetoshi Seto continue; 3746387125fcSChris Mason if (!dev->bdev) { 37475af3e8ccSStefan Behrens errors_wait++; 3748387125fcSChris Mason continue; 3749387125fcSChris Mason } 3750e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3751ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3752387125fcSChris Mason continue; 3753387125fcSChris Mason 37544fc6441aSAnand Jain ret = wait_dev_flush(dev); 3755401b41e5SAnand Jain if (ret) { 3756401b41e5SAnand Jain dev->last_flush_error = ret; 375766b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 375866b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 37595af3e8ccSStefan Behrens errors_wait++; 3760387125fcSChris Mason } 3761401b41e5SAnand Jain } 3762401b41e5SAnand Jain 3763cea7c8bfSAnand Jain if (errors_wait) { 3764401b41e5SAnand Jain /* 3765401b41e5SAnand Jain * At some point we need the status of all disks 3766401b41e5SAnand Jain * to arrive at the volume status. So error checking 3767401b41e5SAnand Jain * is being pushed to a separate loop. 3768401b41e5SAnand Jain */ 3769d10b82feSQu Wenruo return check_barrier_error(info); 3770401b41e5SAnand Jain } 3771387125fcSChris Mason return 0; 3772387125fcSChris Mason } 3773387125fcSChris Mason 3774943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3775943c6e99SZhao Lei { 37768789f4feSZhao Lei int raid_type; 37778789f4feSZhao Lei int min_tolerated = INT_MAX; 3778943c6e99SZhao Lei 37798789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37808789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37818c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37828789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37838789f4feSZhao Lei tolerated_failures); 3784943c6e99SZhao Lei 37858789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37868789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37878789f4feSZhao Lei continue; 378841a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37898789f4feSZhao Lei continue; 37908c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37918789f4feSZhao Lei btrfs_raid_array[raid_type]. 37928789f4feSZhao Lei tolerated_failures); 37938789f4feSZhao Lei } 3794943c6e99SZhao Lei 37958789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3796ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37978789f4feSZhao Lei min_tolerated = 0; 37988789f4feSZhao Lei } 37998789f4feSZhao Lei 38008789f4feSZhao Lei return min_tolerated; 3801943c6e99SZhao Lei } 3802943c6e99SZhao Lei 3803eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3804f2984462SChris Mason { 3805e5e9a520SChris Mason struct list_head *head; 3806f2984462SChris Mason struct btrfs_device *dev; 3807a061fc8dSChris Mason struct btrfs_super_block *sb; 3808f2984462SChris Mason struct btrfs_dev_item *dev_item; 3809f2984462SChris Mason int ret; 3810f2984462SChris Mason int do_barriers; 3811a236aed1SChris Mason int max_errors; 3812a236aed1SChris Mason int total_errors = 0; 3813a061fc8dSChris Mason u64 flags; 3814f2984462SChris Mason 38150b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3816fed3b381SLiu Bo 3817fed3b381SLiu Bo /* 3818fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3819fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3820fed3b381SLiu Bo * been consistent on disk. 3821fed3b381SLiu Bo */ 3822fed3b381SLiu Bo if (max_mirrors == 0) 38230b246afaSJeff Mahoney backup_super_roots(fs_info); 3824f2984462SChris Mason 38250b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3826a061fc8dSChris Mason dev_item = &sb->dev_item; 3827e5e9a520SChris Mason 38280b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 38290b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 38300b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3831387125fcSChris Mason 38325af3e8ccSStefan Behrens if (do_barriers) { 38330b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 38345af3e8ccSStefan Behrens if (ret) { 38355af3e8ccSStefan Behrens mutex_unlock( 38360b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38370b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38385af3e8ccSStefan Behrens "errors while submitting device barriers."); 38395af3e8ccSStefan Behrens return ret; 38405af3e8ccSStefan Behrens } 38415af3e8ccSStefan Behrens } 3842387125fcSChris Mason 38431538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3844dfe25020SChris Mason if (!dev->bdev) { 3845dfe25020SChris Mason total_errors++; 3846dfe25020SChris Mason continue; 3847dfe25020SChris Mason } 3848e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3849ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3850dfe25020SChris Mason continue; 3851dfe25020SChris Mason 38522b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3853a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3854a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 38557df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3856935e5cc9SMiao Xie dev->commit_total_bytes); 3857ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3858ce7213c7SMiao Xie dev->commit_bytes_used); 3859a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3860a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3861a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3862a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 38637239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38647239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3865a512bbf8SYan Zheng 3866a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3867a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3868f2984462SChris Mason 386975cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 387075cb857dSQu Wenruo if (ret < 0) { 387175cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 387275cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 387375cb857dSQu Wenruo "unexpected superblock corruption detected"); 387475cb857dSQu Wenruo return -EUCLEAN; 387575cb857dSQu Wenruo } 387675cb857dSQu Wenruo 3877abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3878a236aed1SChris Mason if (ret) 3879a236aed1SChris Mason total_errors++; 3880f2984462SChris Mason } 3881a236aed1SChris Mason if (total_errors > max_errors) { 38820b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3883d397712bSChris Mason total_errors); 38840b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 388579787eaaSJeff Mahoney 38869d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38870b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38880b246afaSJeff Mahoney "%d errors while writing supers", 38890b246afaSJeff Mahoney total_errors); 38909d565ba4SStefan Behrens return -EIO; 3891a236aed1SChris Mason } 3892f2984462SChris Mason 3893a512bbf8SYan Zheng total_errors = 0; 38941538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3895dfe25020SChris Mason if (!dev->bdev) 3896dfe25020SChris Mason continue; 3897e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3898ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3899dfe25020SChris Mason continue; 3900dfe25020SChris Mason 3901abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3902a512bbf8SYan Zheng if (ret) 39031259ab75SChris Mason total_errors++; 3904f2984462SChris Mason } 39050b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3906a236aed1SChris Mason if (total_errors > max_errors) { 39070b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 39080b246afaSJeff Mahoney "%d errors while writing supers", 39090b246afaSJeff Mahoney total_errors); 391079787eaaSJeff Mahoney return -EIO; 3911a236aed1SChris Mason } 3912f2984462SChris Mason return 0; 3913f2984462SChris Mason } 3914f2984462SChris Mason 3915cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3916cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3917cb517eabSMiao Xie struct btrfs_root *root) 39182619ba1fSChris Mason { 39194df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 39202619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 39212619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3922af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 392300246528SJosef Bacik btrfs_put_root(root); 39244df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 392576dda93cSYan, Zheng 392676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 392776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 392876dda93cSYan, Zheng 39291c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 39303321719eSLiu Bo btrfs_free_log(NULL, root); 39311c1ea4f7SLiu Bo if (root->reloc_root) { 39321c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 39331c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 393400246528SJosef Bacik btrfs_put_root(root->reloc_root); 39351c1ea4f7SLiu Bo root->reloc_root = NULL; 39361c1ea4f7SLiu Bo } 39371c1ea4f7SLiu Bo } 39383321719eSLiu Bo 3939faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3940581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3941faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3942581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 394384db5ccfSDavid Sterba btrfs_free_fs_root(root); 39444df27c4dSYan, Zheng } 39454df27c4dSYan, Zheng 394684db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 39474df27c4dSYan, Zheng { 394857cdc8dbSDavid Sterba iput(root->ino_cache_inode); 39494df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 39500ee5dc67SAl Viro if (root->anon_dev) 39510ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 39528257b2dcSMiao Xie if (root->subv_writers) 39538257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 39545f39d397SChris Mason free_extent_buffer(root->node); 39555f39d397SChris Mason free_extent_buffer(root->commit_root); 3956581bb050SLi Zefan kfree(root->free_ino_ctl); 3957581bb050SLi Zefan kfree(root->free_ino_pinned); 395800246528SJosef Bacik btrfs_put_root(root); 39592619ba1fSChris Mason } 39602619ba1fSChris Mason 3961c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3962c146afadSYan Zheng { 3963c146afadSYan Zheng u64 root_objectid = 0; 3964c146afadSYan Zheng struct btrfs_root *gang[8]; 396565d33fd7SQu Wenruo int i = 0; 396665d33fd7SQu Wenruo int err = 0; 396765d33fd7SQu Wenruo unsigned int ret = 0; 396865d33fd7SQu Wenruo int index; 3969c146afadSYan Zheng 3970c146afadSYan Zheng while (1) { 397165d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3972c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3973c146afadSYan Zheng (void **)gang, root_objectid, 3974c146afadSYan Zheng ARRAY_SIZE(gang)); 397565d33fd7SQu Wenruo if (!ret) { 397665d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3977c146afadSYan Zheng break; 397865d33fd7SQu Wenruo } 39795d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 398066b4ffd1SJosef Bacik 398165d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 398265d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 398365d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 398465d33fd7SQu Wenruo gang[i] = NULL; 398565d33fd7SQu Wenruo continue; 398665d33fd7SQu Wenruo } 398765d33fd7SQu Wenruo /* grab all the search result for later use */ 398800246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 398965d33fd7SQu Wenruo } 399065d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 399165d33fd7SQu Wenruo 399265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 399365d33fd7SQu Wenruo if (!gang[i]) 399465d33fd7SQu Wenruo continue; 3995c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 399666b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 399766b4ffd1SJosef Bacik if (err) 399865d33fd7SQu Wenruo break; 399900246528SJosef Bacik btrfs_put_root(gang[i]); 4000c146afadSYan Zheng } 4001c146afadSYan Zheng root_objectid++; 4002c146afadSYan Zheng } 400365d33fd7SQu Wenruo 400465d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 400565d33fd7SQu Wenruo for (; i < ret; i++) { 400665d33fd7SQu Wenruo if (gang[i]) 400700246528SJosef Bacik btrfs_put_root(gang[i]); 400865d33fd7SQu Wenruo } 400965d33fd7SQu Wenruo return err; 4010c146afadSYan Zheng } 4011c146afadSYan Zheng 40126bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4013c146afadSYan Zheng { 40146bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4015c146afadSYan Zheng struct btrfs_trans_handle *trans; 4016c146afadSYan Zheng 40170b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 40182ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 40190b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 40200b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4021c71bf099SYan, Zheng 4022c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 40230b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 40240b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4025c71bf099SYan, Zheng 40267a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 40273612b495STsutomu Itoh if (IS_ERR(trans)) 40283612b495STsutomu Itoh return PTR_ERR(trans); 40293a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4030c146afadSYan Zheng } 4031c146afadSYan Zheng 4032b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4033eb60ceacSChris Mason { 4034c146afadSYan Zheng int ret; 4035e089f05cSChris Mason 4036afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4037d6fd0ae2SOmar Sandoval /* 4038d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4039d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4040d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4041d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4042d6fd0ae2SOmar Sandoval */ 4043d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4044a2135011SChris Mason 40457343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4046d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40477343dd61SJustin Maggard 4048803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4049803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4050803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4051803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4052803b2f54SStefan Behrens 4053837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4054aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4055837d5b6eSIlya Dryomov 40568dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40578dabb742SStefan Behrens 4058aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 40594cb5300bSChris Mason 40604cb5300bSChris Mason /* wait for any defraggers to finish */ 40614cb5300bSChris Mason wait_event(fs_info->transaction_wait, 40624cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 40634cb5300bSChris Mason 40644cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 406526176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40664cb5300bSChris Mason 406721c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 406821c7e756SMiao Xie 4069b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4070b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4071b0643e59SDennis Zhou 4072bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4073e44163e1SJeff Mahoney /* 4074d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4075d6fd0ae2SOmar Sandoval * unused block groups. 4076e44163e1SJeff Mahoney */ 40770b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4078e44163e1SJeff Mahoney 40796bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4080d397712bSChris Mason if (ret) 408104892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4082c146afadSYan Zheng } 4083ed2ff2cbSChris Mason 4084af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4085af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40862ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4087acce952bSliubo 4088e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4089e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40908929ecfaSYan, Zheng 4091e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4092afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4093f25784b3SYan Zheng 409404892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4095fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4096bcef60f2SArne Jansen 4097963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 409804892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4099963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4100b0c68f8bSChris Mason } 410131153d81SYan Zheng 41024297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 41034297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 41044297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 41054297ff84SJosef Bacik 41066618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4107b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 41085ac1d209SJeff Mahoney 4109faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4110d10c5f31SChris Mason 41111a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 41121a4319ccSLiu Bo 4113de348ee0SWang Shilong /* 4114de348ee0SWang Shilong * we must make sure there is not any read request to 4115de348ee0SWang Shilong * submit after we stopping all workers. 4116de348ee0SWang Shilong */ 4117de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 411896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 411996192499SJosef Bacik 4120afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 41214273eaffSAnand Jain free_root_pointers(fs_info, true); 41229ad6b7bcSChris Mason 41234e19443dSJosef Bacik /* 41244e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 41254e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 41264e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 41274e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 41284e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 41294e19443dSJosef Bacik */ 41304e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 41314e19443dSJosef Bacik 413213e6c37bSJosef Bacik iput(fs_info->btree_inode); 4133d6bfde87SChris Mason 413421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 41350b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 41362ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 413721adbd5cSStefan Behrens #endif 413821adbd5cSStefan Behrens 41390b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 414068c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 414176dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 4142eb60ceacSChris Mason } 4143eb60ceacSChris Mason 4144b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4145b9fab919SChris Mason int atomic) 4146ccd467d6SChris Mason { 41471259ab75SChris Mason int ret; 4148727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 41491259ab75SChris Mason 41500b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 41511259ab75SChris Mason if (!ret) 41521259ab75SChris Mason return ret; 41531259ab75SChris Mason 41541259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4155b9fab919SChris Mason parent_transid, atomic); 4156b9fab919SChris Mason if (ret == -EAGAIN) 4157b9fab919SChris Mason return ret; 41581259ab75SChris Mason return !ret; 41595f39d397SChris Mason } 41606702ed49SChris Mason 41615f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 41625f39d397SChris Mason { 41630b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 416406ea65a3SJosef Bacik struct btrfs_root *root; 41655f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4166b9473439SChris Mason int was_dirty; 4167b4ce94deSChris Mason 416806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 416906ea65a3SJosef Bacik /* 417006ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 417152042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 417206ea65a3SJosef Bacik * outside of the sanity tests. 417306ea65a3SJosef Bacik */ 4174b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 417506ea65a3SJosef Bacik return; 417606ea65a3SJosef Bacik #endif 417706ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41780b246afaSJeff Mahoney fs_info = root->fs_info; 4179b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41800b246afaSJeff Mahoney if (transid != fs_info->generation) 41815d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41820b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41830b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4184e2d84521SMiao Xie if (!was_dirty) 4185104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4186e2d84521SMiao Xie buf->len, 41870b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41881f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 418969fc6cbbSQu Wenruo /* 419069fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 419169fc6cbbSQu Wenruo * but item data not updated. 419269fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 419369fc6cbbSQu Wenruo */ 419469fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4195cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4196a4f78750SDavid Sterba btrfs_print_leaf(buf); 41971f21ef0aSFilipe Manana ASSERT(0); 41981f21ef0aSFilipe Manana } 41991f21ef0aSFilipe Manana #endif 4200eb60ceacSChris Mason } 4201eb60ceacSChris Mason 42022ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4203b53d3f5dSLiu Bo int flush_delayed) 420435b7e476SChris Mason { 4205188de649SChris Mason /* 4206188de649SChris Mason * looks as though older kernels can get into trouble with 4207188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4208188de649SChris Mason */ 4209e2d84521SMiao Xie int ret; 4210d6bfde87SChris Mason 42116933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4212d6bfde87SChris Mason return; 4213d6bfde87SChris Mason 4214b53d3f5dSLiu Bo if (flush_delayed) 42152ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 421616cdcec7SMiao Xie 4217d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4218d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4219d814a491SEthan Lien fs_info->dirty_metadata_batch); 4220e2d84521SMiao Xie if (ret > 0) { 42210b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 422216cdcec7SMiao Xie } 422316cdcec7SMiao Xie } 422416cdcec7SMiao Xie 42252ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 422616cdcec7SMiao Xie { 42272ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 422835b7e476SChris Mason } 4229b53d3f5dSLiu Bo 42302ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4231b53d3f5dSLiu Bo { 42322ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4233d6bfde87SChris Mason } 42346b80053dSChris Mason 4235581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4236581c1760SQu Wenruo struct btrfs_key *first_key) 42376b80053dSChris Mason { 42385ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4239581c1760SQu Wenruo level, first_key); 42406b80053dSChris Mason } 42410da5468fSChris Mason 42422ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4243acce952bSliubo { 4244fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4245fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4246fe816d0fSNikolay Borisov 42470b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42482ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42490b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4250acce952bSliubo 42510b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42520b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4253acce952bSliubo } 4254acce952bSliubo 4255143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4256acce952bSliubo { 4257acce952bSliubo struct btrfs_ordered_extent *ordered; 4258acce952bSliubo 4259199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4260779880efSJosef Bacik /* 4261779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4262779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4263779880efSJosef Bacik */ 4264199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4265779880efSJosef Bacik root_extent_list) 4266779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4267199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4268199c2a9cSMiao Xie } 4269199c2a9cSMiao Xie 4270199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4271199c2a9cSMiao Xie { 4272199c2a9cSMiao Xie struct btrfs_root *root; 4273199c2a9cSMiao Xie struct list_head splice; 4274199c2a9cSMiao Xie 4275199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4276199c2a9cSMiao Xie 4277199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4278199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4279199c2a9cSMiao Xie while (!list_empty(&splice)) { 4280199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4281199c2a9cSMiao Xie ordered_root); 42821de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42831de2cfdeSJosef Bacik &fs_info->ordered_roots); 4284199c2a9cSMiao Xie 42852a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4286199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4287199c2a9cSMiao Xie 42882a85d9caSLiu Bo cond_resched(); 42892a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4290199c2a9cSMiao Xie } 4291199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 429274d5d229SJosef Bacik 429374d5d229SJosef Bacik /* 429474d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 429574d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 429674d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 429774d5d229SJosef Bacik * actually get run and error out properly. 429874d5d229SJosef Bacik */ 429974d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4300acce952bSliubo } 4301acce952bSliubo 430235a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43032ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4304acce952bSliubo { 4305acce952bSliubo struct rb_node *node; 4306acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4307acce952bSliubo struct btrfs_delayed_ref_node *ref; 4308acce952bSliubo int ret = 0; 4309acce952bSliubo 4310acce952bSliubo delayed_refs = &trans->delayed_refs; 4311acce952bSliubo 4312acce952bSliubo spin_lock(&delayed_refs->lock); 4313d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4314cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 43150b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4316acce952bSliubo return ret; 4317acce952bSliubo } 4318acce952bSliubo 43195c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4320d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 43210e0adbcfSJosef Bacik struct rb_node *n; 4322e78417d1SJosef Bacik bool pin_bytes = false; 4323acce952bSliubo 4324d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4325d7df2c79SJosef Bacik href_node); 43263069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4327b939d1abSJosef Bacik continue; 43283069bd26SJosef Bacik 4329d7df2c79SJosef Bacik spin_lock(&head->lock); 4330e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 43310e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 43320e0adbcfSJosef Bacik ref_node); 4333d7df2c79SJosef Bacik ref->in_tree = 0; 4334e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 43350e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 43361d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43371d57ee94SWang Xiaoguang list_del(&ref->add_list); 4338d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4339d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4340d7df2c79SJosef Bacik } 434154067ae9SJosef Bacik if (head->must_insert_reserved) 4342e78417d1SJosef Bacik pin_bytes = true; 434378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4344fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4345d7df2c79SJosef Bacik spin_unlock(&head->lock); 4346acce952bSliubo spin_unlock(&delayed_refs->lock); 4347e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4348acce952bSliubo 4349f603bb94SNikolay Borisov if (pin_bytes) { 4350f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4351f603bb94SNikolay Borisov 4352f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4353f603bb94SNikolay Borisov BUG_ON(!cache); 4354f603bb94SNikolay Borisov 4355f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4356f603bb94SNikolay Borisov spin_lock(&cache->lock); 4357f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4358f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4359f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4360f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4361f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4362f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4363f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4364f603bb94SNikolay Borisov percpu_counter_add_batch( 4365f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4366f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4367f603bb94SNikolay Borisov 4368f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4369f603bb94SNikolay Borisov 4370f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4371f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4372f603bb94SNikolay Borisov } 437331890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4374d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4375acce952bSliubo cond_resched(); 4376acce952bSliubo spin_lock(&delayed_refs->lock); 4377acce952bSliubo } 437881f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4379acce952bSliubo 4380acce952bSliubo spin_unlock(&delayed_refs->lock); 4381acce952bSliubo 4382acce952bSliubo return ret; 4383acce952bSliubo } 4384acce952bSliubo 4385143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4386acce952bSliubo { 4387acce952bSliubo struct btrfs_inode *btrfs_inode; 4388acce952bSliubo struct list_head splice; 4389acce952bSliubo 4390acce952bSliubo INIT_LIST_HEAD(&splice); 4391acce952bSliubo 4392eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4393eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4394acce952bSliubo 4395acce952bSliubo while (!list_empty(&splice)) { 4396fe816d0fSNikolay Borisov struct inode *inode = NULL; 4397eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4398acce952bSliubo delalloc_inodes); 4399fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4400eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4401acce952bSliubo 4402fe816d0fSNikolay Borisov /* 4403fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4404fe816d0fSNikolay Borisov * meanwhile. 4405fe816d0fSNikolay Borisov */ 4406fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4407fe816d0fSNikolay Borisov if (inode) { 4408fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4409fe816d0fSNikolay Borisov iput(inode); 4410fe816d0fSNikolay Borisov } 4411eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4412acce952bSliubo } 4413eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4414eb73c1b7SMiao Xie } 4415eb73c1b7SMiao Xie 4416eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4417eb73c1b7SMiao Xie { 4418eb73c1b7SMiao Xie struct btrfs_root *root; 4419eb73c1b7SMiao Xie struct list_head splice; 4420eb73c1b7SMiao Xie 4421eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4422eb73c1b7SMiao Xie 4423eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4424eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4425eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4426eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4427eb73c1b7SMiao Xie delalloc_root); 442800246528SJosef Bacik root = btrfs_grab_root(root); 4429eb73c1b7SMiao Xie BUG_ON(!root); 4430eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4431eb73c1b7SMiao Xie 4432eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 443300246528SJosef Bacik btrfs_put_root(root); 4434eb73c1b7SMiao Xie 4435eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4436eb73c1b7SMiao Xie } 4437eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4438acce952bSliubo } 4439acce952bSliubo 44402ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4441acce952bSliubo struct extent_io_tree *dirty_pages, 4442acce952bSliubo int mark) 4443acce952bSliubo { 4444acce952bSliubo int ret; 4445acce952bSliubo struct extent_buffer *eb; 4446acce952bSliubo u64 start = 0; 4447acce952bSliubo u64 end; 4448acce952bSliubo 4449acce952bSliubo while (1) { 4450acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4451e6138876SJosef Bacik mark, NULL); 4452acce952bSliubo if (ret) 4453acce952bSliubo break; 4454acce952bSliubo 445591166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4456acce952bSliubo while (start <= end) { 44570b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44580b246afaSJeff Mahoney start += fs_info->nodesize; 4459fd8b2b61SJosef Bacik if (!eb) 4460acce952bSliubo continue; 4461fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4462acce952bSliubo 4463fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4464fd8b2b61SJosef Bacik &eb->bflags)) 4465fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4466fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4467acce952bSliubo } 4468acce952bSliubo } 4469acce952bSliubo 4470acce952bSliubo return ret; 4471acce952bSliubo } 4472acce952bSliubo 44732ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4474fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4475acce952bSliubo { 4476acce952bSliubo u64 start; 4477acce952bSliubo u64 end; 4478acce952bSliubo int ret; 4479acce952bSliubo 4480acce952bSliubo while (1) { 44810e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 44820e6ec385SFilipe Manana 4483fcd5e742SLu Fengqi /* 4484fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4485fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4486fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4487fcd5e742SLu Fengqi * the same extent range. 4488fcd5e742SLu Fengqi */ 4489fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4490acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44910e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4492fcd5e742SLu Fengqi if (ret) { 4493fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4494acce952bSliubo break; 4495fcd5e742SLu Fengqi } 4496acce952bSliubo 44970e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44980e6ec385SFilipe Manana free_extent_state(cached_state); 44992ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4500fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4501acce952bSliubo cond_resched(); 4502acce952bSliubo } 4503acce952bSliubo 4504acce952bSliubo return 0; 4505acce952bSliubo } 4506acce952bSliubo 450732da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4508c79a1751SLiu Bo { 4509c79a1751SLiu Bo struct inode *inode; 4510c79a1751SLiu Bo 4511c79a1751SLiu Bo inode = cache->io_ctl.inode; 4512c79a1751SLiu Bo if (inode) { 4513c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4514c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4515c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4516c79a1751SLiu Bo iput(inode); 4517c79a1751SLiu Bo } 4518c79a1751SLiu Bo btrfs_put_block_group(cache); 4519c79a1751SLiu Bo } 4520c79a1751SLiu Bo 4521c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45222ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4523c79a1751SLiu Bo { 452432da5386SDavid Sterba struct btrfs_block_group *cache; 4525c79a1751SLiu Bo 4526c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4527c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4528c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 452932da5386SDavid Sterba struct btrfs_block_group, 4530c79a1751SLiu Bo dirty_list); 4531c79a1751SLiu Bo 4532c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4533c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4534c79a1751SLiu Bo list_del_init(&cache->io_list); 4535c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4536c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4537c79a1751SLiu Bo } 4538c79a1751SLiu Bo 4539c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4540c79a1751SLiu Bo spin_lock(&cache->lock); 4541c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4542c79a1751SLiu Bo spin_unlock(&cache->lock); 4543c79a1751SLiu Bo 4544c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4545c79a1751SLiu Bo btrfs_put_block_group(cache); 4546ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4547c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4548c79a1751SLiu Bo } 4549c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4550c79a1751SLiu Bo 455145ae2c18SNikolay Borisov /* 455245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 455345ae2c18SNikolay Borisov * to use it without any locking 455445ae2c18SNikolay Borisov */ 4555c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4556c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 455732da5386SDavid Sterba struct btrfs_block_group, 4558c79a1751SLiu Bo io_list); 4559c79a1751SLiu Bo 4560c79a1751SLiu Bo list_del_init(&cache->io_list); 4561c79a1751SLiu Bo spin_lock(&cache->lock); 4562c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4563c79a1751SLiu Bo spin_unlock(&cache->lock); 4564c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4565c79a1751SLiu Bo } 4566c79a1751SLiu Bo } 4567c79a1751SLiu Bo 456849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45692ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 457049b25e05SJeff Mahoney { 4571bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4572bbbf7243SNikolay Borisov 45732ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4574c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4575c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4576c79a1751SLiu Bo 4577bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4578bbbf7243SNikolay Borisov post_commit_list) { 4579bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4580bbbf7243SNikolay Borisov } 4581bbbf7243SNikolay Borisov 45822ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 458349b25e05SJeff Mahoney 45844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45850b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 458649b25e05SJeff Mahoney 45874a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45880b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 458949b25e05SJeff Mahoney 4590ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 459167cde344SMiao Xie 45922ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 459349b25e05SJeff Mahoney EXTENT_DIRTY); 4594fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 459549b25e05SJeff Mahoney 45964a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45974a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 459849b25e05SJeff Mahoney } 459949b25e05SJeff Mahoney 46002ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4601acce952bSliubo { 4602acce952bSliubo struct btrfs_transaction *t; 4603acce952bSliubo 46040b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4605acce952bSliubo 46060b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46070b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46080b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4609724e2315SJosef Bacik struct btrfs_transaction, list); 4610724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 46119b64f57dSElena Reshetova refcount_inc(&t->use_count); 46120b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46132ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4614724e2315SJosef Bacik btrfs_put_transaction(t); 46150b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4616724e2315SJosef Bacik continue; 4617724e2315SJosef Bacik } 46180b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4619724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4621724e2315SJosef Bacik /* 4622724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4623724e2315SJosef Bacik * handle open currently for this transaction. 4624724e2315SJosef Bacik */ 4625724e2315SJosef Bacik wait_event(t->writer_wait, 4626724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4627724e2315SJosef Bacik } else { 46280b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4629724e2315SJosef Bacik } 46302ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4631724e2315SJosef Bacik 46320b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46330b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46340b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4635724e2315SJosef Bacik list_del_init(&t->list); 46360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4637a4abeea4SJosef Bacik 4638724e2315SJosef Bacik btrfs_put_transaction(t); 46392ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46400b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4641724e2315SJosef Bacik } 46420b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46430b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4644ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4645ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46460b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46470b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4648acce952bSliubo 4649acce952bSliubo return 0; 4650acce952bSliubo } 4651acce952bSliubo 4652e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 46534d53dddbSDavid Sterba /* mandatory callbacks */ 46540b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 46554d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 46560da5468fSChris Mason }; 4657