1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6e20d96d6SChris Mason #include <linux/fs.h> 7d98237b3SChris Mason #include <linux/blkdev.h> 80f7d52f4SChris Mason #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10d397712bSChris Mason #include <linux/buffer_head.h> 11ce9adaa5SChris Mason #include <linux/workqueue.h> 12a74a4b97SChris Mason #include <linux/kthread.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 14784b4e29SChris Mason #include <linux/migrate.h> 157a36ddecSDavid Sterba #include <linux/ratelimit.h> 166463fe58SStefan Behrens #include <linux/uuid.h> 17803b2f54SStefan Behrens #include <linux/semaphore.h> 18540adea3SMasami Hiramatsu #include <linux/error-injection.h> 199678c543SNikolay Borisov #include <linux/crc32c.h> 20b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 217e75bf3fSDavid Sterba #include <asm/unaligned.h> 226d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 23eb60ceacSChris Mason #include "ctree.h" 24eb60ceacSChris Mason #include "disk-io.h" 25e089f05cSChris Mason #include "transaction.h" 260f7d52f4SChris Mason #include "btrfs_inode.h" 270b86a832SChris Mason #include "volumes.h" 28db94535dSChris Mason #include "print-tree.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31fa9c0d79SChris Mason #include "free-space-cache.h" 3270f6d82eSOmar Sandoval #include "free-space-tree.h" 33581bb050SLi Zefan #include "inode-map.h" 3421adbd5cSStefan Behrens #include "check-integrity.h" 35606686eeSJosef Bacik #include "rcu-string.h" 368dabb742SStefan Behrens #include "dev-replace.h" 3753b381b3SDavid Woodhouse #include "raid56.h" 385ac1d209SJeff Mahoney #include "sysfs.h" 39fcebe456SJosef Bacik #include "qgroup.h" 40ebb8765bSAnand Jain #include "compression.h" 41557ea5ddSQu Wenruo #include "tree-checker.h" 42fd708b81SJosef Bacik #include "ref-verify.h" 43aac0023cSJosef Bacik #include "block-group.h" 44b0643e59SDennis Zhou #include "discard.h" 45f603bb94SNikolay Borisov #include "space-info.h" 46eb60ceacSChris Mason 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 54e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 558b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 56143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 57acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 582ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 59143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 602ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 61acce952bSliubo struct extent_io_tree *dirty_pages, 62acce952bSliubo int mark); 632ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 64acce952bSliubo struct extent_io_tree *pinned_extents); 652ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 662ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 67ce9adaa5SChris Mason 68d352ac68SChris Mason /* 6997eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 7097eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 71d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 72d352ac68SChris Mason */ 7397eb6b69SDavid Sterba struct btrfs_end_io_wq { 74ce9adaa5SChris Mason struct bio *bio; 75ce9adaa5SChris Mason bio_end_io_t *end_io; 76ce9adaa5SChris Mason void *private; 77ce9adaa5SChris Mason struct btrfs_fs_info *info; 784e4cbee9SChristoph Hellwig blk_status_t status; 79bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 808b712842SChris Mason struct btrfs_work work; 81ce9adaa5SChris Mason }; 820da5468fSChris Mason 8397eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8497eb6b69SDavid Sterba 8597eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8697eb6b69SDavid Sterba { 8797eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8897eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8997eb6b69SDavid Sterba 0, 90fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9197eb6b69SDavid Sterba NULL); 9297eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9397eb6b69SDavid Sterba return -ENOMEM; 9497eb6b69SDavid Sterba return 0; 9597eb6b69SDavid Sterba } 9697eb6b69SDavid Sterba 97e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9897eb6b69SDavid Sterba { 9997eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 10097eb6b69SDavid Sterba } 10197eb6b69SDavid Sterba 102141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 103141386e1SJosef Bacik { 104141386e1SJosef Bacik if (fs_info->csum_shash) 105141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 106141386e1SJosef Bacik } 107141386e1SJosef Bacik 108d352ac68SChris Mason /* 109d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 110d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 111d352ac68SChris Mason * just before they are sent down the IO stack. 112d352ac68SChris Mason */ 11344b8bd7eSChris Mason struct async_submit_bio { 114c6100a4bSJosef Bacik void *private_data; 11544b8bd7eSChris Mason struct bio *bio; 116a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11744b8bd7eSChris Mason int mirror_num; 118eaf25d93SChris Mason /* 119eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 120eaf25d93SChris Mason * can't tell us where in the file the bio should go 121eaf25d93SChris Mason */ 122eaf25d93SChris Mason u64 bio_offset; 1238b712842SChris Mason struct btrfs_work work; 1244e4cbee9SChristoph Hellwig blk_status_t status; 12544b8bd7eSChris Mason }; 12644b8bd7eSChris Mason 12785d4e461SChris Mason /* 12885d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12985d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 13085d4e461SChris Mason * the level the eb occupies in the tree. 1314008c04aSChris Mason * 13285d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13385d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13485d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1354fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1364fd786e6SMisono Tomohiro * roots have separate keysets. 1374008c04aSChris Mason * 13885d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13985d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 14085d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1414008c04aSChris Mason * 14285d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14385d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14485d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14585d4e461SChris Mason * 14685d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14785d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14885d4e461SChris Mason * needs update as well. 1494008c04aSChris Mason */ 1504008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1514008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1524008c04aSChris Mason # error 1534008c04aSChris Mason # endif 15485d4e461SChris Mason 15585d4e461SChris Mason static struct btrfs_lockdep_keyset { 15685d4e461SChris Mason u64 id; /* root objectid */ 15785d4e461SChris Mason const char *name_stem; /* lock name stem */ 15885d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 15985d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 16085d4e461SChris Mason } btrfs_lockdep_keysets[] = { 16185d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 16285d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 16385d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 16485d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 16585d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 16685d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 16760b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 16885d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 16985d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 17085d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 17113fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1726b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 17385d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1744008c04aSChris Mason }; 17585d4e461SChris Mason 17685d4e461SChris Mason void __init btrfs_init_lockdep(void) 17785d4e461SChris Mason { 17885d4e461SChris Mason int i, j; 17985d4e461SChris Mason 18085d4e461SChris Mason /* initialize lockdep class names */ 18185d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 18285d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 18385d4e461SChris Mason 18485d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 18585d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 18685d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 18785d4e461SChris Mason } 18885d4e461SChris Mason } 18985d4e461SChris Mason 19085d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 19185d4e461SChris Mason int level) 19285d4e461SChris Mason { 19385d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19485d4e461SChris Mason 19585d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19685d4e461SChris Mason 19785d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19885d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19985d4e461SChris Mason if (ks->id == objectid) 20085d4e461SChris Mason break; 20185d4e461SChris Mason 20285d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20385d4e461SChris Mason &ks->keys[level], ks->names[level]); 20485d4e461SChris Mason } 20585d4e461SChris Mason 2064008c04aSChris Mason #endif 2074008c04aSChris Mason 208d352ac68SChris Mason /* 209d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 210d352ac68SChris Mason * that covers the entire device 211d352ac68SChris Mason */ 2126af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 21339b07b5dSOmar Sandoval struct page *page, size_t pg_offset, 21439b07b5dSOmar Sandoval u64 start, u64 len) 2155f39d397SChris Mason { 216fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2175f39d397SChris Mason struct extent_map *em; 2185f39d397SChris Mason int ret; 2195f39d397SChris Mason 220890871beSChris Mason read_lock(&em_tree->lock); 221d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 222a061fc8dSChris Mason if (em) { 223890871beSChris Mason read_unlock(&em_tree->lock); 2245f39d397SChris Mason goto out; 225a061fc8dSChris Mason } 226890871beSChris Mason read_unlock(&em_tree->lock); 2277b13b7b1SChris Mason 228172ddd60SDavid Sterba em = alloc_extent_map(); 2295f39d397SChris Mason if (!em) { 2305f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2315f39d397SChris Mason goto out; 2325f39d397SChris Mason } 2335f39d397SChris Mason em->start = 0; 2340afbaf8cSChris Mason em->len = (u64)-1; 235c8b97818SChris Mason em->block_len = (u64)-1; 2365f39d397SChris Mason em->block_start = 0; 237d1310b2eSChris Mason 238890871beSChris Mason write_lock(&em_tree->lock); 23909a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2405f39d397SChris Mason if (ret == -EEXIST) { 2415f39d397SChris Mason free_extent_map(em); 2427b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 243b4f359abSTsutomu Itoh if (!em) 2440433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2455f39d397SChris Mason } else if (ret) { 2467b13b7b1SChris Mason free_extent_map(em); 2470433f20dSTsutomu Itoh em = ERR_PTR(ret); 2485f39d397SChris Mason } 249890871beSChris Mason write_unlock(&em_tree->lock); 2507b13b7b1SChris Mason 2515f39d397SChris Mason out: 2525f39d397SChris Mason return em; 2535f39d397SChris Mason } 2545f39d397SChris Mason 255d352ac68SChris Mason /* 2562996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 2572996e1f8SJohannes Thumshirn * 2582996e1f8SJohannes Thumshirn * Returns error if the extent buffer cannot be mapped. 259d352ac68SChris Mason */ 2602996e1f8SJohannes Thumshirn static int csum_tree_block(struct extent_buffer *buf, u8 *result) 26119c00ddcSChris Mason { 262d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 263d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 26419c00ddcSChris Mason unsigned long len; 26519c00ddcSChris Mason unsigned long cur_len; 26619c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26719c00ddcSChris Mason char *kaddr; 26819c00ddcSChris Mason unsigned long map_start; 26919c00ddcSChris Mason unsigned long map_len; 27019c00ddcSChris Mason int err; 271d5178578SJohannes Thumshirn 272d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 273d5178578SJohannes Thumshirn crypto_shash_init(shash); 27419c00ddcSChris Mason 27519c00ddcSChris Mason len = buf->len - offset; 276d5178578SJohannes Thumshirn 27719c00ddcSChris Mason while (len > 0) { 278d2e174d5SJohannes Thumshirn /* 279d2e174d5SJohannes Thumshirn * Note: we don't need to check for the err == 1 case here, as 280d2e174d5SJohannes Thumshirn * with the given combination of 'start = BTRFS_CSUM_SIZE (32)' 281d2e174d5SJohannes Thumshirn * and 'min_len = 32' and the currently implemented mapping 282d2e174d5SJohannes Thumshirn * algorithm we cannot cross a page boundary. 283d2e174d5SJohannes Thumshirn */ 28419c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 285a6591715SChris Mason &kaddr, &map_start, &map_len); 286c53839fcSJohannes Thumshirn if (WARN_ON(err)) 2878bd98f0eSAlex Lyakas return err; 28819c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 289d5178578SJohannes Thumshirn crypto_shash_update(shash, kaddr + offset - map_start, cur_len); 29019c00ddcSChris Mason len -= cur_len; 29119c00ddcSChris Mason offset += cur_len; 29219c00ddcSChris Mason } 29371a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 294607d432dSJosef Bacik 295d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 29619c00ddcSChris Mason 29719c00ddcSChris Mason return 0; 29819c00ddcSChris Mason } 29919c00ddcSChris Mason 300d352ac68SChris Mason /* 301d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 302d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 303d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 304d352ac68SChris Mason * in the wrong place. 305d352ac68SChris Mason */ 3061259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 307b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 308b9fab919SChris Mason int atomic) 3091259ab75SChris Mason { 3102ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3111259ab75SChris Mason int ret; 3122755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3131259ab75SChris Mason 3141259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3151259ab75SChris Mason return 0; 3161259ab75SChris Mason 317b9fab919SChris Mason if (atomic) 318b9fab919SChris Mason return -EAGAIN; 319b9fab919SChris Mason 320a26e8c9fSJosef Bacik if (need_lock) { 321a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 322300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 323a26e8c9fSJosef Bacik } 324a26e8c9fSJosef Bacik 3252ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 326ff13db41SDavid Sterba &cached_state); 3270b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3281259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3291259ab75SChris Mason ret = 0; 3301259ab75SChris Mason goto out; 3311259ab75SChris Mason } 33294647322SDavid Sterba btrfs_err_rl(eb->fs_info, 33394647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 33494647322SDavid Sterba eb->start, 33529549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3361259ab75SChris Mason ret = 1; 337a26e8c9fSJosef Bacik 338a26e8c9fSJosef Bacik /* 339a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 340a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 34101327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 342a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 343a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 344a26e8c9fSJosef Bacik * making everybody very sad. 345a26e8c9fSJosef Bacik */ 346a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3470b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 34833958dc6SChris Mason out: 3492ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 350e43bbe5eSDavid Sterba &cached_state); 351472b909fSJosef Bacik if (need_lock) 352a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3531259ab75SChris Mason return ret; 3541259ab75SChris Mason } 3551259ab75SChris Mason 356e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 357e7e16f48SJohannes Thumshirn { 358e7e16f48SJohannes Thumshirn switch (csum_type) { 359e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 3603951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 3613831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 362352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 363e7e16f48SJohannes Thumshirn return true; 364e7e16f48SJohannes Thumshirn default: 365e7e16f48SJohannes Thumshirn return false; 366e7e16f48SJohannes Thumshirn } 367e7e16f48SJohannes Thumshirn } 368e7e16f48SJohannes Thumshirn 369d352ac68SChris Mason /* 3701104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3711104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3721104a885SDavid Sterba */ 373ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 374ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3751104a885SDavid Sterba { 3761104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3771104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 37851bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 379d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 380d5178578SJohannes Thumshirn 381d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 382d5178578SJohannes Thumshirn crypto_shash_init(shash); 3831104a885SDavid Sterba 3841104a885SDavid Sterba /* 3851104a885SDavid Sterba * The super_block structure does not span the whole 38651bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 38751bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3881104a885SDavid Sterba */ 389d5178578SJohannes Thumshirn crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 390d5178578SJohannes Thumshirn BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 391d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 3921104a885SDavid Sterba 39351bce6c9SJohannes Thumshirn if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb))) 394e7e16f48SJohannes Thumshirn return 1; 3951104a885SDavid Sterba 396e7e16f48SJohannes Thumshirn return 0; 3971104a885SDavid Sterba } 3981104a885SDavid Sterba 399e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 400ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 401581c1760SQu Wenruo { 402e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 403581c1760SQu Wenruo int found_level; 404581c1760SQu Wenruo struct btrfs_key found_key; 405581c1760SQu Wenruo int ret; 406581c1760SQu Wenruo 407581c1760SQu Wenruo found_level = btrfs_header_level(eb); 408581c1760SQu Wenruo if (found_level != level) { 40963489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 41063489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 411581c1760SQu Wenruo btrfs_err(fs_info, 412581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 413581c1760SQu Wenruo eb->start, level, found_level); 414581c1760SQu Wenruo return -EIO; 415581c1760SQu Wenruo } 416581c1760SQu Wenruo 417581c1760SQu Wenruo if (!first_key) 418581c1760SQu Wenruo return 0; 419581c1760SQu Wenruo 4205d41be6fSQu Wenruo /* 4215d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 4225d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 4235d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 4245d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 4255d41be6fSQu Wenruo */ 4265d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 4275d41be6fSQu Wenruo return 0; 42862fdaa52SQu Wenruo 42962fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 43062fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 43162fdaa52SQu Wenruo btrfs_err(fs_info, 43262fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 43362fdaa52SQu Wenruo eb->start); 43462fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 43562fdaa52SQu Wenruo return -EUCLEAN; 43662fdaa52SQu Wenruo } 43762fdaa52SQu Wenruo 438581c1760SQu Wenruo if (found_level) 439581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 440581c1760SQu Wenruo else 441581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 442581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 443581c1760SQu Wenruo 444581c1760SQu Wenruo if (ret) { 44563489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 44663489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 447581c1760SQu Wenruo btrfs_err(fs_info, 448ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 449ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 450ff76a864SLiu Bo first_key->type, first_key->offset, 451ff76a864SLiu Bo found_key.objectid, found_key.type, 452ff76a864SLiu Bo found_key.offset); 453581c1760SQu Wenruo } 454581c1760SQu Wenruo return ret; 455581c1760SQu Wenruo } 456581c1760SQu Wenruo 4571104a885SDavid Sterba /* 458d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 459d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 460581c1760SQu Wenruo * 461581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 462581c1760SQu Wenruo * @level: expected level, mandatory check 463581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 464d352ac68SChris Mason */ 4655ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 466581c1760SQu Wenruo u64 parent_transid, int level, 467581c1760SQu Wenruo struct btrfs_key *first_key) 468f188591eSChris Mason { 4695ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 470f188591eSChris Mason struct extent_io_tree *io_tree; 471ea466794SJosef Bacik int failed = 0; 472f188591eSChris Mason int ret; 473f188591eSChris Mason int num_copies = 0; 474f188591eSChris Mason int mirror_num = 0; 475ea466794SJosef Bacik int failed_mirror = 0; 476f188591eSChris Mason 4770b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 478f188591eSChris Mason while (1) { 479f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 480c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 481256dd1bbSStefan Behrens if (!ret) { 482581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 483b9fab919SChris Mason parent_transid, 0)) 484256dd1bbSStefan Behrens ret = -EIO; 485e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 486ff76a864SLiu Bo first_key, parent_transid)) 487581c1760SQu Wenruo ret = -EUCLEAN; 488581c1760SQu Wenruo else 489581c1760SQu Wenruo break; 490256dd1bbSStefan Behrens } 491d397712bSChris Mason 4920b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 493f188591eSChris Mason eb->start, eb->len); 4944235298eSChris Mason if (num_copies == 1) 495ea466794SJosef Bacik break; 4964235298eSChris Mason 4975cf1ab56SJosef Bacik if (!failed_mirror) { 4985cf1ab56SJosef Bacik failed = 1; 4995cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 5005cf1ab56SJosef Bacik } 5015cf1ab56SJosef Bacik 502f188591eSChris Mason mirror_num++; 503ea466794SJosef Bacik if (mirror_num == failed_mirror) 504ea466794SJosef Bacik mirror_num++; 505ea466794SJosef Bacik 5064235298eSChris Mason if (mirror_num > num_copies) 507ea466794SJosef Bacik break; 508f188591eSChris Mason } 509ea466794SJosef Bacik 510c0901581SStefan Behrens if (failed && !ret && failed_mirror) 51120a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 512ea466794SJosef Bacik 513ea466794SJosef Bacik return ret; 514f188591eSChris Mason } 51519c00ddcSChris Mason 516d352ac68SChris Mason /* 517d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 518d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 519d352ac68SChris Mason */ 520d397712bSChris Mason 52101d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 52219c00ddcSChris Mason { 5234eee4fa4SMiao Xie u64 start = page_offset(page); 52419c00ddcSChris Mason u64 found_start; 5252996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 5262996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 52719c00ddcSChris Mason struct extent_buffer *eb; 5288d47a0d8SQu Wenruo int ret; 529f188591eSChris Mason 5304f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5314f2de97aSJosef Bacik if (page != eb->pages[0]) 5324f2de97aSJosef Bacik return 0; 5330f805531SAlex Lyakas 53419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5350f805531SAlex Lyakas /* 5360f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5370f805531SAlex Lyakas * It is useful to know what went wrong. 5380f805531SAlex Lyakas */ 5390f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5400f805531SAlex Lyakas return -EUCLEAN; 5410f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5420f805531SAlex Lyakas return -EUCLEAN; 5430f805531SAlex Lyakas 544de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 5450f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5460f805531SAlex Lyakas 5472996e1f8SJohannes Thumshirn if (csum_tree_block(eb, result)) 5482996e1f8SJohannes Thumshirn return -EINVAL; 5492996e1f8SJohannes Thumshirn 5508d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 5518d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 5528d47a0d8SQu Wenruo else 5538d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 5548d47a0d8SQu Wenruo 5558d47a0d8SQu Wenruo if (ret < 0) { 556c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 5578d47a0d8SQu Wenruo btrfs_err(fs_info, 5588d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 5598d47a0d8SQu Wenruo eb->start); 560c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 5618d47a0d8SQu Wenruo return ret; 5628d47a0d8SQu Wenruo } 5632996e1f8SJohannes Thumshirn write_extent_buffer(eb, result, 0, csum_size); 5648d47a0d8SQu Wenruo 5652996e1f8SJohannes Thumshirn return 0; 56619c00ddcSChris Mason } 56719c00ddcSChris Mason 568b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5692b82032cSYan Zheng { 570b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 57101d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 57244880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5732b82032cSYan Zheng int ret = 1; 5742b82032cSYan Zheng 5750a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5762b82032cSYan Zheng while (fs_devices) { 5777239ff4bSNikolay Borisov u8 *metadata_uuid; 5787239ff4bSNikolay Borisov 5797239ff4bSNikolay Borisov /* 5807239ff4bSNikolay Borisov * Checking the incompat flag is only valid for the current 5817239ff4bSNikolay Borisov * fs. For seed devices it's forbidden to have their uuid 5827239ff4bSNikolay Borisov * changed so reading ->fsid in this case is fine 5837239ff4bSNikolay Borisov */ 5847239ff4bSNikolay Borisov if (fs_devices == fs_info->fs_devices && 5857239ff4bSNikolay Borisov btrfs_fs_incompat(fs_info, METADATA_UUID)) 5867239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5877239ff4bSNikolay Borisov else 5887239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5897239ff4bSNikolay Borisov 5907239ff4bSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) { 5912b82032cSYan Zheng ret = 0; 5922b82032cSYan Zheng break; 5932b82032cSYan Zheng } 5942b82032cSYan Zheng fs_devices = fs_devices->seed; 5952b82032cSYan Zheng } 5962b82032cSYan Zheng return ret; 5972b82032cSYan Zheng } 5982b82032cSYan Zheng 599facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 600facc8a22SMiao Xie u64 phy_offset, struct page *page, 601facc8a22SMiao Xie u64 start, u64 end, int mirror) 602ce9adaa5SChris Mason { 603ce9adaa5SChris Mason u64 found_start; 604ce9adaa5SChris Mason int found_level; 605ce9adaa5SChris Mason struct extent_buffer *eb; 606ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 60702873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 6082996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 609f188591eSChris Mason int ret = 0; 6102996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 611727011e0SChris Mason int reads_done; 612ce9adaa5SChris Mason 613ce9adaa5SChris Mason if (!page->private) 614ce9adaa5SChris Mason goto out; 615d397712bSChris Mason 6164f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 617d397712bSChris Mason 6180b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6190b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6200b32f4bbSJosef Bacik */ 62167439dadSDavid Sterba atomic_inc(&eb->refs); 6220b32f4bbSJosef Bacik 6230b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 624727011e0SChris Mason if (!reads_done) 625727011e0SChris Mason goto err; 626f188591eSChris Mason 6275cf1ab56SJosef Bacik eb->read_mirror = mirror; 628656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 629ea466794SJosef Bacik ret = -EIO; 630ea466794SJosef Bacik goto err; 631ea466794SJosef Bacik } 632ea466794SJosef Bacik 633ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 634727011e0SChris Mason if (found_start != eb->start) { 635893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 636893bf4b1SSu Yue eb->start, found_start); 637f188591eSChris Mason ret = -EIO; 638ce9adaa5SChris Mason goto err; 639ce9adaa5SChris Mason } 640b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 64102873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 64294647322SDavid Sterba eb->start); 6431259ab75SChris Mason ret = -EIO; 6441259ab75SChris Mason goto err; 6451259ab75SChris Mason } 646ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6471c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 648893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 649893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6501c24c3ceSJosef Bacik ret = -EIO; 6511c24c3ceSJosef Bacik goto err; 6521c24c3ceSJosef Bacik } 653ce9adaa5SChris Mason 65485d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 65585d4e461SChris Mason eb, found_level); 6564008c04aSChris Mason 6572996e1f8SJohannes Thumshirn ret = csum_tree_block(eb, result); 6588bd98f0eSAlex Lyakas if (ret) 659a826d6dcSJosef Bacik goto err; 660a826d6dcSJosef Bacik 6612996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 6622996e1f8SJohannes Thumshirn u32 val; 6632996e1f8SJohannes Thumshirn u32 found = 0; 6642996e1f8SJohannes Thumshirn 6652996e1f8SJohannes Thumshirn memcpy(&found, result, csum_size); 6662996e1f8SJohannes Thumshirn 6672996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 6682996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 6692996e1f8SJohannes Thumshirn "%s checksum verify failed on %llu wanted %x found %x level %d", 6702996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 6712996e1f8SJohannes Thumshirn val, found, btrfs_header_level(eb)); 6722996e1f8SJohannes Thumshirn ret = -EUCLEAN; 6732996e1f8SJohannes Thumshirn goto err; 6742996e1f8SJohannes Thumshirn } 6752996e1f8SJohannes Thumshirn 676a826d6dcSJosef Bacik /* 677a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 678a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 679a826d6dcSJosef Bacik * return -EIO. 680a826d6dcSJosef Bacik */ 6811c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 682a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 683a826d6dcSJosef Bacik ret = -EIO; 684a826d6dcSJosef Bacik } 685ce9adaa5SChris Mason 686813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 687053ab70fSLiu Bo ret = -EIO; 688053ab70fSLiu Bo 6890b32f4bbSJosef Bacik if (!ret) 6900b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 69175391f0dSQu Wenruo else 69275391f0dSQu Wenruo btrfs_err(fs_info, 69375391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 69475391f0dSQu Wenruo eb->start); 695ce9adaa5SChris Mason err: 69679fb65a1SJosef Bacik if (reads_done && 69779fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 698d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6994bb31e92SArne Jansen 70053b381b3SDavid Woodhouse if (ret) { 70153b381b3SDavid Woodhouse /* 70253b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 70353b381b3SDavid Woodhouse * again, we have to make sure it has something 70453b381b3SDavid Woodhouse * to decrement 70553b381b3SDavid Woodhouse */ 70653b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7070b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 70853b381b3SDavid Woodhouse } 7090b32f4bbSJosef Bacik free_extent_buffer(eb); 710ce9adaa5SChris Mason out: 711f188591eSChris Mason return ret; 712ce9adaa5SChris Mason } 713ce9adaa5SChris Mason 7144246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 715ce9adaa5SChris Mason { 71697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 717ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7189e0af237SLiu Bo struct btrfs_workqueue *wq; 719ce9adaa5SChris Mason 720ce9adaa5SChris Mason fs_info = end_io_wq->info; 7214e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 722d20f7043SChris Mason 72337226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 724a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7259e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 726a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7279e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 728a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7299e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 730a0cac0ecSOmar Sandoval else 7319e0af237SLiu Bo wq = fs_info->endio_write_workers; 7329e0af237SLiu Bo } else { 733a0cac0ecSOmar Sandoval if (unlikely(end_io_wq->metadata == BTRFS_WQ_ENDIO_DIO_REPAIR)) 7348b110e39SMiao Xie wq = fs_info->endio_repair_workers; 735a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7369e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 737a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7389e0af237SLiu Bo wq = fs_info->endio_meta_workers; 739a0cac0ecSOmar Sandoval else 7409e0af237SLiu Bo wq = fs_info->endio_workers; 7419e0af237SLiu Bo } 7429e0af237SLiu Bo 743a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7449e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 745ce9adaa5SChris Mason } 746ce9adaa5SChris Mason 7474e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 748bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7490b86a832SChris Mason { 75097eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7518b110e39SMiao Xie 75297eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 753ce9adaa5SChris Mason if (!end_io_wq) 7544e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 755ce9adaa5SChris Mason 756ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 757ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 75822c59948SChris Mason end_io_wq->info = info; 7594e4cbee9SChristoph Hellwig end_io_wq->status = 0; 760ce9adaa5SChris Mason end_io_wq->bio = bio; 76122c59948SChris Mason end_io_wq->metadata = metadata; 762ce9adaa5SChris Mason 763ce9adaa5SChris Mason bio->bi_private = end_io_wq; 764ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 76522c59948SChris Mason return 0; 76622c59948SChris Mason } 76722c59948SChris Mason 7684a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7694a69a410SChris Mason { 7704a69a410SChris Mason struct async_submit_bio *async; 7714e4cbee9SChristoph Hellwig blk_status_t ret; 7724a69a410SChris Mason 7734a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 774c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 775eaf25d93SChris Mason async->bio_offset); 77679787eaaSJeff Mahoney if (ret) 7774e4cbee9SChristoph Hellwig async->status = ret; 7784a69a410SChris Mason } 7794a69a410SChris Mason 78006ea01b1SDavid Sterba /* 78106ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 78206ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 78306ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 78406ea01b1SDavid Sterba * 78506ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 78606ea01b1SDavid Sterba * inserted into the tree. 78706ea01b1SDavid Sterba */ 7884a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7898b712842SChris Mason { 7908b712842SChris Mason struct async_submit_bio *async; 79106ea01b1SDavid Sterba struct inode *inode; 79206ea01b1SDavid Sterba blk_status_t ret; 7938b712842SChris Mason 7948b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 79506ea01b1SDavid Sterba inode = async->private_data; 7964854ddd0SChris Mason 797bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7984e4cbee9SChristoph Hellwig if (async->status) { 7994e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 8004246a0b6SChristoph Hellwig bio_endio(async->bio); 80179787eaaSJeff Mahoney return; 80279787eaaSJeff Mahoney } 80379787eaaSJeff Mahoney 804ec39f769SChris Mason /* 805ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 806ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 807ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 808ec39f769SChris Mason */ 809ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 81008635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 81106ea01b1SDavid Sterba if (ret) { 81206ea01b1SDavid Sterba async->bio->bi_status = ret; 81306ea01b1SDavid Sterba bio_endio(async->bio); 81406ea01b1SDavid Sterba } 8154a69a410SChris Mason } 8164a69a410SChris Mason 8174a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8184a69a410SChris Mason { 8194a69a410SChris Mason struct async_submit_bio *async; 8204a69a410SChris Mason 8214a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8228b712842SChris Mason kfree(async); 8238b712842SChris Mason } 8248b712842SChris Mason 8258c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 826c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 827c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 828e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 82944b8bd7eSChris Mason { 83044b8bd7eSChris Mason struct async_submit_bio *async; 83144b8bd7eSChris Mason 83244b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 83344b8bd7eSChris Mason if (!async) 8344e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 83544b8bd7eSChris Mason 836c6100a4bSJosef Bacik async->private_data = private_data; 83744b8bd7eSChris Mason async->bio = bio; 83844b8bd7eSChris Mason async->mirror_num = mirror_num; 8394a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8404a69a410SChris Mason 841a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 842a0cac0ecSOmar Sandoval run_one_async_free); 8434a69a410SChris Mason 844eaf25d93SChris Mason async->bio_offset = bio_offset; 8458c8bee1dSChris Mason 8464e4cbee9SChristoph Hellwig async->status = 0; 84779787eaaSJeff Mahoney 84867f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8495cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 850d313d7a3SChris Mason 8515cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 85244b8bd7eSChris Mason return 0; 85344b8bd7eSChris Mason } 85444b8bd7eSChris Mason 8554e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 856ce3ed71aSChris Mason { 8572c30c71bSKent Overstreet struct bio_vec *bvec; 858ce3ed71aSChris Mason struct btrfs_root *root; 8592b070cfeSChristoph Hellwig int ret = 0; 8606dc4f100SMing Lei struct bvec_iter_all iter_all; 861ce3ed71aSChris Mason 862c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8632b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 864ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 86501d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 86679787eaaSJeff Mahoney if (ret) 86779787eaaSJeff Mahoney break; 868ce3ed71aSChris Mason } 8692c30c71bSKent Overstreet 8704e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 871ce3ed71aSChris Mason } 872ce3ed71aSChris Mason 873d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 874eaf25d93SChris Mason u64 bio_offset) 87522c59948SChris Mason { 8768b712842SChris Mason /* 8778b712842SChris Mason * when we're called for a write, we're already in the async 8785443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8798b712842SChris Mason */ 88079787eaaSJeff Mahoney return btree_csum_one_bio(bio); 88122c59948SChris Mason } 88222c59948SChris Mason 8839b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8849b4e675aSDavid Sterba struct btrfs_inode *bi) 885de0022b9SJosef Bacik { 8866300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8876300463bSLiu Bo return 0; 8889b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 889de0022b9SJosef Bacik return 0; 890de0022b9SJosef Bacik return 1; 891de0022b9SJosef Bacik } 892de0022b9SJosef Bacik 893a56b1c7bSNikolay Borisov static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio, 89450489a57SNikolay Borisov int mirror_num, 89550489a57SNikolay Borisov unsigned long bio_flags) 89644b8bd7eSChris Mason { 8970b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8989b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8994e4cbee9SChristoph Hellwig blk_status_t ret; 900cad321adSChris Mason 90137226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 902cad321adSChris Mason /* 903cad321adSChris Mason * called for a read, do the setup so that checksum validation 904cad321adSChris Mason * can happen in the async kernel threads 905cad321adSChris Mason */ 9060b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9070b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9081d4284bdSChris Mason if (ret) 90961891923SStefan Behrens goto out_w_error; 91008635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 911de0022b9SJosef Bacik } else if (!async) { 912de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 913de0022b9SJosef Bacik if (ret) 91461891923SStefan Behrens goto out_w_error; 91508635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 91661891923SStefan Behrens } else { 917cad321adSChris Mason /* 91861891923SStefan Behrens * kthread helpers are used to submit writes so that 91961891923SStefan Behrens * checksumming can happen in parallel across all CPUs 920cad321adSChris Mason */ 921c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 922e68f2ee7SNikolay Borisov 0, inode, btree_submit_bio_start); 92344b8bd7eSChris Mason } 92444b8bd7eSChris Mason 9254246a0b6SChristoph Hellwig if (ret) 9264246a0b6SChristoph Hellwig goto out_w_error; 9274246a0b6SChristoph Hellwig return 0; 9284246a0b6SChristoph Hellwig 92961891923SStefan Behrens out_w_error: 9304e4cbee9SChristoph Hellwig bio->bi_status = ret; 9314246a0b6SChristoph Hellwig bio_endio(bio); 93261891923SStefan Behrens return ret; 93361891923SStefan Behrens } 93461891923SStefan Behrens 9353dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 936784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 937a6bc32b8SMel Gorman struct page *newpage, struct page *page, 938a6bc32b8SMel Gorman enum migrate_mode mode) 939784b4e29SChris Mason { 940784b4e29SChris Mason /* 941784b4e29SChris Mason * we can't safely write a btree page from here, 942784b4e29SChris Mason * we haven't done the locking hook 943784b4e29SChris Mason */ 944784b4e29SChris Mason if (PageDirty(page)) 945784b4e29SChris Mason return -EAGAIN; 946784b4e29SChris Mason /* 947784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 948784b4e29SChris Mason * We must have no buffers or drop them. 949784b4e29SChris Mason */ 950784b4e29SChris Mason if (page_has_private(page) && 951784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 952784b4e29SChris Mason return -EAGAIN; 953a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 954784b4e29SChris Mason } 9553dd1462eSJan Beulich #endif 956784b4e29SChris Mason 9570da5468fSChris Mason 9580da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9590da5468fSChris Mason struct writeback_control *wbc) 9600da5468fSChris Mason { 961e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 962e2d84521SMiao Xie int ret; 963e2d84521SMiao Xie 964d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 965448d640bSChris Mason 966448d640bSChris Mason if (wbc->for_kupdate) 967448d640bSChris Mason return 0; 968448d640bSChris Mason 969e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 970b9473439SChris Mason /* this is a bit racy, but that's ok */ 971d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 972d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 973d814a491SEthan Lien fs_info->dirty_metadata_batch); 974e2d84521SMiao Xie if (ret < 0) 975793955bcSChris Mason return 0; 976793955bcSChris Mason } 9770b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9780da5468fSChris Mason } 9790da5468fSChris Mason 980b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9815f39d397SChris Mason { 98271ad38b4SDavid Sterba return extent_read_full_page(page, btree_get_extent, 0); 9835f39d397SChris Mason } 9845f39d397SChris Mason 98570dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9865f39d397SChris Mason { 98798509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 98898509cfcSChris Mason return 0; 9890c4e538bSDavid Sterba 990f7a52a40SDavid Sterba return try_release_extent_buffer(page); 991d98237b3SChris Mason } 992d98237b3SChris Mason 993d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 994d47992f8SLukas Czerner unsigned int length) 995d98237b3SChris Mason { 996d1310b2eSChris Mason struct extent_io_tree *tree; 997d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9985f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9995f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10009ad6b7bcSChris Mason if (PagePrivate(page)) { 1001efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1002efe120a0SFrank Holton "page private not zero on page %llu", 1003efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10049ad6b7bcSChris Mason ClearPagePrivate(page); 10059ad6b7bcSChris Mason set_page_private(page, 0); 100609cbfeafSKirill A. Shutemov put_page(page); 10079ad6b7bcSChris Mason } 1008d98237b3SChris Mason } 1009d98237b3SChris Mason 10100b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10110b32f4bbSJosef Bacik { 1012bb146eb2SJosef Bacik #ifdef DEBUG 10130b32f4bbSJosef Bacik struct extent_buffer *eb; 10140b32f4bbSJosef Bacik 10150b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10160b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10170b32f4bbSJosef Bacik BUG_ON(!eb); 10180b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10190b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10200b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1021bb146eb2SJosef Bacik #endif 10220b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10230b32f4bbSJosef Bacik } 10240b32f4bbSJosef Bacik 10257f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1026d98237b3SChris Mason .readpage = btree_readpage, 10270da5468fSChris Mason .writepages = btree_writepages, 10285f39d397SChris Mason .releasepage = btree_releasepage, 10295f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10305a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1031784b4e29SChris Mason .migratepage = btree_migratepage, 10325a92bc88SChris Mason #endif 10330b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1034d98237b3SChris Mason }; 1035123abc88SChris Mason 10362ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1037090d1875SChris Mason { 10385f39d397SChris Mason struct extent_buffer *buf = NULL; 1039537f38f0SNikolay Borisov int ret; 1040090d1875SChris Mason 10412ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1042c871b0f2SLiu Bo if (IS_ERR(buf)) 10436197d86eSDavid Sterba return; 1044537f38f0SNikolay Borisov 1045c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 1046537f38f0SNikolay Borisov if (ret < 0) 1047537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1048537f38f0SNikolay Borisov else 10495f39d397SChris Mason free_extent_buffer(buf); 1050090d1875SChris Mason } 1051090d1875SChris Mason 10522ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10532ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10540308af44SDavid Sterba u64 bytenr) 10550999df54SChris Mason { 10560b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10570b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10580b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10590999df54SChris Mason } 10600999df54SChris Mason 1061581c1760SQu Wenruo /* 1062581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1063581c1760SQu Wenruo * verification. 1064581c1760SQu Wenruo * 1065581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1066581c1760SQu Wenruo * @level: expected level, mandatory check 1067581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1068581c1760SQu Wenruo */ 10692ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1070581c1760SQu Wenruo u64 parent_transid, int level, 1071581c1760SQu Wenruo struct btrfs_key *first_key) 1072e20d96d6SChris Mason { 10735f39d397SChris Mason struct extent_buffer *buf = NULL; 107419c00ddcSChris Mason int ret; 107519c00ddcSChris Mason 10762ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1077c871b0f2SLiu Bo if (IS_ERR(buf)) 1078c871b0f2SLiu Bo return buf; 1079e4204dedSChris Mason 10805ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1081581c1760SQu Wenruo level, first_key); 10820f0fe8f7SFilipe David Borba Manana if (ret) { 1083537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 108464c043deSLiu Bo return ERR_PTR(ret); 10850f0fe8f7SFilipe David Borba Manana } 10865f39d397SChris Mason return buf; 1087ce9adaa5SChris Mason 1088eb60ceacSChris Mason } 1089eb60ceacSChris Mason 10906a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1091ed2ff2cbSChris Mason { 10926a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 109355c69072SChris Mason if (btrfs_header_generation(buf) == 1094e2d84521SMiao Xie fs_info->running_transaction->transid) { 1095b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1096b4ce94deSChris Mason 1097b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1098104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1099e2d84521SMiao Xie -buf->len, 1100e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1101b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 11028bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 11030b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1104925baeddSChris Mason } 11055f39d397SChris Mason } 1106ed7b63ebSJosef Bacik } 11075f39d397SChris Mason 11088257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11098257b2dcSMiao Xie { 11108257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11118257b2dcSMiao Xie int ret; 11128257b2dcSMiao Xie 11138257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11148257b2dcSMiao Xie if (!writers) 11158257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11168257b2dcSMiao Xie 11178a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11188257b2dcSMiao Xie if (ret < 0) { 11198257b2dcSMiao Xie kfree(writers); 11208257b2dcSMiao Xie return ERR_PTR(ret); 11218257b2dcSMiao Xie } 11228257b2dcSMiao Xie 11238257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11248257b2dcSMiao Xie return writers; 11258257b2dcSMiao Xie } 11268257b2dcSMiao Xie 11278257b2dcSMiao Xie static void 11288257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11298257b2dcSMiao Xie { 11308257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11318257b2dcSMiao Xie kfree(writers); 11328257b2dcSMiao Xie } 11338257b2dcSMiao Xie 1134da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1135e20d96d6SChris Mason u64 objectid) 1136d97e63b6SChris Mason { 11377c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 113896dfcb46SJosef Bacik root->fs_info = fs_info; 1139cfaa7295SChris Mason root->node = NULL; 1140a28ec197SChris Mason root->commit_root = NULL; 114127cdeb70SMiao Xie root->state = 0; 1142d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11430b86a832SChris Mason 11440f7d52f4SChris Mason root->last_trans = 0; 114513a8a7c8SYan, Zheng root->highest_objectid = 0; 1146eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1147199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11486bef4d31SEric Paris root->inode_tree = RB_ROOT; 114916cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1150f0486c68SYan, Zheng root->block_rsv = NULL; 11510b86a832SChris Mason 11520b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11535d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1154eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1155eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1156199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1157199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1158d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11592ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11602ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11615d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1162eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1163199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1164f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11652ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11662ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11678287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1168a2135011SChris Mason mutex_init(&root->objectid_mutex); 1169e02119d5SChris Mason mutex_init(&root->log_mutex); 117031f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1171573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11727237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11737237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11747237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11758b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11768b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11777237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11787237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11797237f183SYan Zheng atomic_set(&root->log_writers, 0); 11802ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11810700cea7SElena Reshetova refcount_set(&root->refs, 1); 1182ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 11838ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1184eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11857237f183SYan Zheng root->log_transid = 0; 1186d1433debSMiao Xie root->log_transid_committed = -1; 1187257c62e1SChris Mason root->last_log_commit = 0; 11887c0260eeSJeff Mahoney if (!dummy) 118943eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 119043eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1191017e5369SChris Mason 11923768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11933768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11946702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11957c0260eeSJeff Mahoney if (!dummy) 11963f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119706ea65a3SJosef Bacik else 119806ea65a3SJosef Bacik root->defrag_trans_start = 0; 11994d775673SChris Mason root->root_key.objectid = objectid; 12000ee5dc67SAl Viro root->anon_dev = 0; 12018ea05e3aSAlexander Block 12025f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1203370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1204bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1205bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1206bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1207bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1208bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1209bd647ce3SJosef Bacik #endif 12103768f368SChris Mason } 12113768f368SChris Mason 121274e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 121396dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12146f07e42eSAl Viro { 121574e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12166f07e42eSAl Viro if (root) 121796dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12186f07e42eSAl Viro return root; 12196f07e42eSAl Viro } 12206f07e42eSAl Viro 122106ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 122206ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1223da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 122406ea65a3SJosef Bacik { 122506ea65a3SJosef Bacik struct btrfs_root *root; 122606ea65a3SJosef Bacik 12277c0260eeSJeff Mahoney if (!fs_info) 12287c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12297c0260eeSJeff Mahoney 123096dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 123106ea65a3SJosef Bacik if (!root) 123206ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1233da17066cSJeff Mahoney 1234b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1235faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 123606ea65a3SJosef Bacik 123706ea65a3SJosef Bacik return root; 123806ea65a3SJosef Bacik } 123906ea65a3SJosef Bacik #endif 124006ea65a3SJosef Bacik 124120897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 124220897f5cSArne Jansen u64 objectid) 124320897f5cSArne Jansen { 12449b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 124520897f5cSArne Jansen struct extent_buffer *leaf; 124620897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 124720897f5cSArne Jansen struct btrfs_root *root; 124820897f5cSArne Jansen struct btrfs_key key; 1249b89f6d1fSFilipe Manana unsigned int nofs_flag; 125020897f5cSArne Jansen int ret = 0; 125133d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 125220897f5cSArne Jansen 1253b89f6d1fSFilipe Manana /* 1254b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1255b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1256b89f6d1fSFilipe Manana */ 1257b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 125896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1259b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 126020897f5cSArne Jansen if (!root) 126120897f5cSArne Jansen return ERR_PTR(-ENOMEM); 126220897f5cSArne Jansen 126320897f5cSArne Jansen root->root_key.objectid = objectid; 126420897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 126520897f5cSArne Jansen root->root_key.offset = 0; 126620897f5cSArne Jansen 12674d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 126820897f5cSArne Jansen if (IS_ERR(leaf)) { 126920897f5cSArne Jansen ret = PTR_ERR(leaf); 12701dd05682STsutomu Itoh leaf = NULL; 127120897f5cSArne Jansen goto fail; 127220897f5cSArne Jansen } 127320897f5cSArne Jansen 127420897f5cSArne Jansen root->node = leaf; 127520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 127620897f5cSArne Jansen 127720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 127827cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 127920897f5cSArne Jansen 128020897f5cSArne Jansen root->root_item.flags = 0; 128120897f5cSArne Jansen root->root_item.byte_limit = 0; 128220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 128320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 128420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 128520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 128620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 128720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 128820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 128933d85fdaSQu Wenruo if (is_fstree(objectid)) 12906463fe58SStefan Behrens uuid_le_gen(&uuid); 12916463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 129220897f5cSArne Jansen root->root_item.drop_level = 0; 129320897f5cSArne Jansen 129420897f5cSArne Jansen key.objectid = objectid; 129520897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 129620897f5cSArne Jansen key.offset = 0; 129720897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129820897f5cSArne Jansen if (ret) 129920897f5cSArne Jansen goto fail; 130020897f5cSArne Jansen 130120897f5cSArne Jansen btrfs_tree_unlock(leaf); 130220897f5cSArne Jansen 130320897f5cSArne Jansen return root; 13041dd05682STsutomu Itoh 13051dd05682STsutomu Itoh fail: 13061dd05682STsutomu Itoh if (leaf) { 13071dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 130859885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13091dd05682STsutomu Itoh free_extent_buffer(leaf); 13101dd05682STsutomu Itoh } 131100246528SJosef Bacik btrfs_put_root(root); 13121dd05682STsutomu Itoh 13131dd05682STsutomu Itoh return ERR_PTR(ret); 131420897f5cSArne Jansen } 131520897f5cSArne Jansen 13167237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1317e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13180f7d52f4SChris Mason { 13190f7d52f4SChris Mason struct btrfs_root *root; 13207237f183SYan Zheng struct extent_buffer *leaf; 1321e02119d5SChris Mason 132296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1323e02119d5SChris Mason if (!root) 13247237f183SYan Zheng return ERR_PTR(-ENOMEM); 1325e02119d5SChris Mason 1326e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1327e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1328e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 132927cdeb70SMiao Xie 13307237f183SYan Zheng /* 133127cdeb70SMiao Xie * DON'T set REF_COWS for log trees 133227cdeb70SMiao Xie * 13337237f183SYan Zheng * log trees do not get reference counted because they go away 13347237f183SYan Zheng * before a real commit is actually done. They do store pointers 13357237f183SYan Zheng * to file data extents, and those reference counts still get 13367237f183SYan Zheng * updated (along with back refs to the log tree). 13377237f183SYan Zheng */ 1338e02119d5SChris Mason 13394d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13404d75f8a9SDavid Sterba NULL, 0, 0, 0); 13417237f183SYan Zheng if (IS_ERR(leaf)) { 134200246528SJosef Bacik btrfs_put_root(root); 13437237f183SYan Zheng return ERR_CAST(leaf); 13447237f183SYan Zheng } 1345e02119d5SChris Mason 13467237f183SYan Zheng root->node = leaf; 1347e02119d5SChris Mason 1348e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1349e02119d5SChris Mason btrfs_tree_unlock(root->node); 13507237f183SYan Zheng return root; 13517237f183SYan Zheng } 13527237f183SYan Zheng 13537237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13547237f183SYan Zheng struct btrfs_fs_info *fs_info) 13557237f183SYan Zheng { 13567237f183SYan Zheng struct btrfs_root *log_root; 13577237f183SYan Zheng 13587237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13597237f183SYan Zheng if (IS_ERR(log_root)) 13607237f183SYan Zheng return PTR_ERR(log_root); 13617237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13627237f183SYan Zheng fs_info->log_root_tree = log_root; 13637237f183SYan Zheng return 0; 13647237f183SYan Zheng } 13657237f183SYan Zheng 13667237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13677237f183SYan Zheng struct btrfs_root *root) 13687237f183SYan Zheng { 13690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13707237f183SYan Zheng struct btrfs_root *log_root; 13717237f183SYan Zheng struct btrfs_inode_item *inode_item; 13727237f183SYan Zheng 13730b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13747237f183SYan Zheng if (IS_ERR(log_root)) 13757237f183SYan Zheng return PTR_ERR(log_root); 13767237f183SYan Zheng 13777237f183SYan Zheng log_root->last_trans = trans->transid; 13787237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13797237f183SYan Zheng 13807237f183SYan Zheng inode_item = &log_root->root_item.inode; 13813cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13823cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13833cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1384da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13850b246afaSJeff Mahoney fs_info->nodesize); 13863cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13877237f183SYan Zheng 13885d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13897237f183SYan Zheng 13907237f183SYan Zheng WARN_ON(root->log_root); 13917237f183SYan Zheng root->log_root = log_root; 13927237f183SYan Zheng root->log_transid = 0; 1393d1433debSMiao Xie root->log_transid_committed = -1; 1394257c62e1SChris Mason root->last_log_commit = 0; 1395e02119d5SChris Mason return 0; 1396e02119d5SChris Mason } 1397e02119d5SChris Mason 139862a2c73eSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1399cb517eabSMiao Xie struct btrfs_key *key) 1400e02119d5SChris Mason { 1401e02119d5SChris Mason struct btrfs_root *root; 1402e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14030f7d52f4SChris Mason struct btrfs_path *path; 140484234f3aSYan Zheng u64 generation; 1405cb517eabSMiao Xie int ret; 1406581c1760SQu Wenruo int level; 1407cb517eabSMiao Xie 1408cb517eabSMiao Xie path = btrfs_alloc_path(); 1409cb517eabSMiao Xie if (!path) 1410cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14110f7d52f4SChris Mason 141296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 1413cb517eabSMiao Xie if (!root) { 1414cb517eabSMiao Xie ret = -ENOMEM; 1415cb517eabSMiao Xie goto alloc_fail; 14160f7d52f4SChris Mason } 14170f7d52f4SChris Mason 1418cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1419cb517eabSMiao Xie &root->root_item, &root->root_key); 14200f7d52f4SChris Mason if (ret) { 142113a8a7c8SYan, Zheng if (ret > 0) 142213a8a7c8SYan, Zheng ret = -ENOENT; 1423cb517eabSMiao Xie goto find_fail; 14240f7d52f4SChris Mason } 142513a8a7c8SYan, Zheng 142684234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1427581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14282ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14292ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1430581c1760SQu Wenruo generation, level, NULL); 143164c043deSLiu Bo if (IS_ERR(root->node)) { 143264c043deSLiu Bo ret = PTR_ERR(root->node); 1433cb517eabSMiao Xie goto find_fail; 1434cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1435cb517eabSMiao Xie ret = -EIO; 143664c043deSLiu Bo free_extent_buffer(root->node); 143764c043deSLiu Bo goto find_fail; 1438416bc658SJosef Bacik } 14395d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 144013a8a7c8SYan, Zheng out: 1441cb517eabSMiao Xie btrfs_free_path(path); 1442cb517eabSMiao Xie return root; 1443cb517eabSMiao Xie 1444cb517eabSMiao Xie find_fail: 144500246528SJosef Bacik btrfs_put_root(root); 1446cb517eabSMiao Xie alloc_fail: 1447cb517eabSMiao Xie root = ERR_PTR(ret); 1448cb517eabSMiao Xie goto out; 1449cb517eabSMiao Xie } 1450cb517eabSMiao Xie 1451a98db0f3SJosef Bacik static int btrfs_init_fs_root(struct btrfs_root *root) 1452cb517eabSMiao Xie { 1453cb517eabSMiao Xie int ret; 14548257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1455cb517eabSMiao Xie 1456cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1457cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1458cb517eabSMiao Xie GFP_NOFS); 1459cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1460cb517eabSMiao Xie ret = -ENOMEM; 1461cb517eabSMiao Xie goto fail; 1462cb517eabSMiao Xie } 1463cb517eabSMiao Xie 14648257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14658257b2dcSMiao Xie if (IS_ERR(writers)) { 14668257b2dcSMiao Xie ret = PTR_ERR(writers); 14678257b2dcSMiao Xie goto fail; 14688257b2dcSMiao Xie } 14698257b2dcSMiao Xie root->subv_writers = writers; 14708257b2dcSMiao Xie 1471f39e4571SJosef Bacik if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 1472f39e4571SJosef Bacik set_bit(BTRFS_ROOT_REF_COWS, &root->state); 1473f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1474f39e4571SJosef Bacik } 1475f39e4571SJosef Bacik 1476cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 147757cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 147857cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1479cb517eabSMiao Xie 1480cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1481cb517eabSMiao Xie if (ret) 1482876d2cf1SLiu Bo goto fail; 1483f32e48e9SChandan Rajendra 1484f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1485f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1486f32e48e9SChandan Rajendra &root->highest_objectid); 1487f32e48e9SChandan Rajendra if (ret) { 1488f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1489876d2cf1SLiu Bo goto fail; 1490f32e48e9SChandan Rajendra } 1491f32e48e9SChandan Rajendra 1492f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1493f32e48e9SChandan Rajendra 1494f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1495f32e48e9SChandan Rajendra 1496cb517eabSMiao Xie return 0; 1497cb517eabSMiao Xie fail: 149884db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1499cb517eabSMiao Xie return ret; 1500cb517eabSMiao Xie } 1501cb517eabSMiao Xie 1502a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1503cb517eabSMiao Xie u64 root_id) 1504cb517eabSMiao Xie { 1505cb517eabSMiao Xie struct btrfs_root *root; 1506cb517eabSMiao Xie 1507cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1508cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1509cb517eabSMiao Xie (unsigned long)root_id); 1510bc44d7c4SJosef Bacik if (root) 151100246528SJosef Bacik root = btrfs_grab_root(root); 1512cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1513cb517eabSMiao Xie return root; 1514cb517eabSMiao Xie } 1515cb517eabSMiao Xie 1516cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1517cb517eabSMiao Xie struct btrfs_root *root) 1518cb517eabSMiao Xie { 1519cb517eabSMiao Xie int ret; 1520cb517eabSMiao Xie 1521e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1522cb517eabSMiao Xie if (ret) 1523cb517eabSMiao Xie return ret; 1524cb517eabSMiao Xie 1525cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1526cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1527cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1528cb517eabSMiao Xie root); 1529af01d2e5SJosef Bacik if (ret == 0) { 153000246528SJosef Bacik btrfs_grab_root(root); 153127cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1532af01d2e5SJosef Bacik } 1533cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1534cb517eabSMiao Xie radix_tree_preload_end(); 1535cb517eabSMiao Xie 1536cb517eabSMiao Xie return ret; 1537cb517eabSMiao Xie } 1538cb517eabSMiao Xie 1539bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1540bd647ce3SJosef Bacik { 1541bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1542bd647ce3SJosef Bacik struct btrfs_root *root; 1543bd647ce3SJosef Bacik 1544bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1545bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1546bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1547bd647ce3SJosef Bacik btrfs_err(fs_info, "leaked root %llu-%llu refcount %d", 1548bd647ce3SJosef Bacik root->root_key.objectid, root->root_key.offset, 1549bd647ce3SJosef Bacik refcount_read(&root->refs)); 1550bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 155100246528SJosef Bacik btrfs_put_root(root); 155200246528SJosef Bacik btrfs_put_root(root); 1553bd647ce3SJosef Bacik } 1554bd647ce3SJosef Bacik #endif 1555bd647ce3SJosef Bacik } 1556bd647ce3SJosef Bacik 15570d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15580d4b0463SJosef Bacik { 1559141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1560141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1561141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1562141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1563141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1564141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1565141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15660d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15670d4b0463SJosef Bacik kfree(fs_info->delayed_root); 156800246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 156900246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 157000246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 157100246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 157200246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 157300246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 157400246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 157500246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 157600246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1577bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15780d4b0463SJosef Bacik kfree(fs_info->super_copy); 15790d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15800d4b0463SJosef Bacik kvfree(fs_info); 15810d4b0463SJosef Bacik } 15820d4b0463SJosef Bacik 15830d4b0463SJosef Bacik 1584c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1585c00869f1SMiao Xie struct btrfs_key *location, 1586c00869f1SMiao Xie bool check_ref) 15875eda7b5eSChris Mason { 15885eda7b5eSChris Mason struct btrfs_root *root; 1589381cf658SDavid Sterba struct btrfs_path *path; 15901d4c08e0SDavid Sterba struct btrfs_key key; 15915eda7b5eSChris Mason int ret; 15925eda7b5eSChris Mason 1593edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 159400246528SJosef Bacik return btrfs_grab_root(fs_info->tree_root); 1595edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 159600246528SJosef Bacik return btrfs_grab_root(fs_info->extent_root); 15978f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 159800246528SJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 15998f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 160000246528SJosef Bacik return btrfs_grab_root(fs_info->dev_root); 16010403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 160200246528SJosef Bacik return btrfs_grab_root(fs_info->csum_root); 1603bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 160400246528SJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 1605bc44d7c4SJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 1606f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 160700246528SJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 1608bc44d7c4SJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 160970f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 161000246528SJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 1611bc44d7c4SJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 16124df27c4dSYan, Zheng again: 1613cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 161448475471SStefan Behrens if (root) { 1615bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 161600246528SJosef Bacik btrfs_put_root(root); 161748475471SStefan Behrens return ERR_PTR(-ENOENT); 1618bc44d7c4SJosef Bacik } 16195eda7b5eSChris Mason return root; 162048475471SStefan Behrens } 16215eda7b5eSChris Mason 162283db2aadSJosef Bacik root = btrfs_read_tree_root(fs_info->tree_root, location); 16235eda7b5eSChris Mason if (IS_ERR(root)) 16245eda7b5eSChris Mason return root; 16253394e160SChris Mason 1626c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1627d68fc57bSYan, Zheng ret = -ENOENT; 1628d68fc57bSYan, Zheng goto fail; 1629d68fc57bSYan, Zheng } 1630d68fc57bSYan, Zheng 1631cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1632cb517eabSMiao Xie if (ret) 1633cb517eabSMiao Xie goto fail; 1634cb517eabSMiao Xie 1635381cf658SDavid Sterba path = btrfs_alloc_path(); 1636381cf658SDavid Sterba if (!path) { 1637381cf658SDavid Sterba ret = -ENOMEM; 1638381cf658SDavid Sterba goto fail; 1639381cf658SDavid Sterba } 16401d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16411d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16421d4c08e0SDavid Sterba key.offset = location->objectid; 16431d4c08e0SDavid Sterba 16441d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1645381cf658SDavid Sterba btrfs_free_path(path); 1646d68fc57bSYan, Zheng if (ret < 0) 1647d68fc57bSYan, Zheng goto fail; 1648d68fc57bSYan, Zheng if (ret == 0) 164927cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1650d68fc57bSYan, Zheng 1651bc44d7c4SJosef Bacik /* 1652bc44d7c4SJosef Bacik * All roots have two refs on them at all times, one for the mounted fs, 1653bc44d7c4SJosef Bacik * and one for being in the radix tree. This way we only free the root 1654bc44d7c4SJosef Bacik * when we are unmounting or deleting the subvolume. We get one ref 1655bc44d7c4SJosef Bacik * from __setup_root, one for inserting it into the radix tree, and then 1656bc44d7c4SJosef Bacik * we have the third for returning it, and the caller will put it when 1657bc44d7c4SJosef Bacik * it's done with the root. 1658bc44d7c4SJosef Bacik */ 165900246528SJosef Bacik btrfs_grab_root(root); 1660cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16610f7d52f4SChris Mason if (ret) { 166200246528SJosef Bacik btrfs_put_root(root); 16634df27c4dSYan, Zheng if (ret == -EEXIST) { 166484db5ccfSDavid Sterba btrfs_free_fs_root(root); 16654df27c4dSYan, Zheng goto again; 16660f7d52f4SChris Mason } 16674df27c4dSYan, Zheng goto fail; 16684df27c4dSYan, Zheng } 1669edbd8d4eSChris Mason return root; 16704df27c4dSYan, Zheng fail: 167184db5ccfSDavid Sterba btrfs_free_fs_root(root); 16724df27c4dSYan, Zheng return ERR_PTR(ret); 1673edbd8d4eSChris Mason } 1674edbd8d4eSChris Mason 167504160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 167604160088SChris Mason { 167704160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 167804160088SChris Mason int ret = 0; 167904160088SChris Mason struct btrfs_device *device; 168004160088SChris Mason struct backing_dev_info *bdi; 1681b7967db7SChris Mason 16821f78160cSXiao Guangrong rcu_read_lock(); 16831f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1684dfe25020SChris Mason if (!device->bdev) 1685dfe25020SChris Mason continue; 1686efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1687ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 168804160088SChris Mason ret = 1; 168904160088SChris Mason break; 169004160088SChris Mason } 169104160088SChris Mason } 16921f78160cSXiao Guangrong rcu_read_unlock(); 169304160088SChris Mason return ret; 169404160088SChris Mason } 169504160088SChris Mason 16968b712842SChris Mason /* 16978b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16988b712842SChris Mason * functions. This is where read checksum verification actually happens 16998b712842SChris Mason */ 17008b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1701ce9adaa5SChris Mason { 1702ce9adaa5SChris Mason struct bio *bio; 170397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1704ce9adaa5SChris Mason 170597eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1706ce9adaa5SChris Mason bio = end_io_wq->bio; 17078b712842SChris Mason 17084e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1709ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1710ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 17114246a0b6SChristoph Hellwig bio_endio(bio); 17129be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1713ce9adaa5SChris Mason } 171444b8bd7eSChris Mason 1715a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1716a74a4b97SChris Mason { 1717a74a4b97SChris Mason struct btrfs_root *root = arg; 17180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1719d0278245SMiao Xie int again; 1720a74a4b97SChris Mason 1721d6fd0ae2SOmar Sandoval while (1) { 1722d0278245SMiao Xie again = 0; 17239d1a2a3aSDavid Sterba 1724fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1725fd340d0fSJosef Bacik 1726d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17272ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1728d0278245SMiao Xie goto sleep; 1729d0278245SMiao Xie 173090c711abSZygo Blaxell /* 173190c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 173290c711abSZygo Blaxell * before we have finished mounting the filesystem. 173390c711abSZygo Blaxell */ 17340b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 173590c711abSZygo Blaxell goto sleep; 173690c711abSZygo Blaxell 17370b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1738d0278245SMiao Xie goto sleep; 1739d0278245SMiao Xie 1740dc7f370cSMiao Xie /* 1741dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1742dc7f370cSMiao Xie * during the above check and trylock. 1743dc7f370cSMiao Xie */ 17442ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17450b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1746dc7f370cSMiao Xie goto sleep; 1747dc7f370cSMiao Xie } 1748dc7f370cSMiao Xie 17492ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1750c2d6cb16SFilipe Manana 17519d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17520b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1753a74a4b97SChris Mason 1754d0278245SMiao Xie /* 175505323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 175605323cd1SMiao Xie * needn't do anything special here. 1757d0278245SMiao Xie */ 17580b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 175967c5e7d4SFilipe Manana 176067c5e7d4SFilipe Manana /* 176167c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 176267c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 176367c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 176467c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 176567c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 176667c5e7d4SFilipe Manana * unused block groups. 176767c5e7d4SFilipe Manana */ 17680b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1769d0278245SMiao Xie sleep: 1770fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1771d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1772d6fd0ae2SOmar Sandoval kthread_parkme(); 1773d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1774d6fd0ae2SOmar Sandoval return 0; 1775838fe188SJiri Kosina if (!again) { 1776a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1777a74a4b97SChris Mason schedule(); 1778a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1779a74a4b97SChris Mason } 1780da288d28SFilipe Manana } 1781a74a4b97SChris Mason } 1782a74a4b97SChris Mason 1783a74a4b97SChris Mason static int transaction_kthread(void *arg) 1784a74a4b97SChris Mason { 1785a74a4b97SChris Mason struct btrfs_root *root = arg; 17860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1787a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1788a74a4b97SChris Mason struct btrfs_transaction *cur; 17898929ecfaSYan, Zheng u64 transid; 1790a944442cSAllen Pais time64_t now; 1791a74a4b97SChris Mason unsigned long delay; 1792914b2007SJan Kara bool cannot_commit; 1793a74a4b97SChris Mason 1794a74a4b97SChris Mason do { 1795914b2007SJan Kara cannot_commit = false; 17960b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17970b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1798a74a4b97SChris Mason 17990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18000b246afaSJeff Mahoney cur = fs_info->running_transaction; 1801a74a4b97SChris Mason if (!cur) { 18020b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1803a74a4b97SChris Mason goto sleep; 1804a74a4b97SChris Mason } 180531153d81SYan Zheng 1806afd48513SArnd Bergmann now = ktime_get_seconds(); 18073296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1808a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 18098b87dc17SDavid Sterba (now < cur->start_time || 18100b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 18110b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1812a74a4b97SChris Mason delay = HZ * 5; 1813a74a4b97SChris Mason goto sleep; 1814a74a4b97SChris Mason } 18158929ecfaSYan, Zheng transid = cur->transid; 18160b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 181756bec294SChris Mason 181879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1819354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1820914b2007SJan Kara if (IS_ERR(trans)) { 1821354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1822914b2007SJan Kara cannot_commit = true; 182379787eaaSJeff Mahoney goto sleep; 1824914b2007SJan Kara } 18258929ecfaSYan, Zheng if (transid == trans->transid) { 18263a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18278929ecfaSYan, Zheng } else { 18283a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18298929ecfaSYan, Zheng } 1830a74a4b97SChris Mason sleep: 18310b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18320b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1833a74a4b97SChris Mason 18344e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18350b246afaSJeff Mahoney &fs_info->fs_state))) 18362ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18378929ecfaSYan, Zheng if (!kthread_should_stop() && 18380b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1839914b2007SJan Kara cannot_commit)) 1840bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1841a74a4b97SChris Mason } while (!kthread_should_stop()); 1842a74a4b97SChris Mason return 0; 1843a74a4b97SChris Mason } 1844a74a4b97SChris Mason 1845af31f5e5SChris Mason /* 184601f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 184701f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 184801f0f9daSNikolay Borisov * anything. 1849af31f5e5SChris Mason * 1850af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1851af31f5e5SChris Mason * generation of the latest root in the array with the generation 1852af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1853af31f5e5SChris Mason */ 185401f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1855af31f5e5SChris Mason { 185601f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1857af31f5e5SChris Mason u64 cur; 1858af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1859af31f5e5SChris Mason int i; 1860af31f5e5SChris Mason 1861af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1862af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1863af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1864af31f5e5SChris Mason if (cur == newest_gen) 186501f0f9daSNikolay Borisov return i; 1866af31f5e5SChris Mason } 1867af31f5e5SChris Mason 186801f0f9daSNikolay Borisov return -EINVAL; 1869af31f5e5SChris Mason } 1870af31f5e5SChris Mason 1871af31f5e5SChris Mason /* 1872af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1873af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1874af31f5e5SChris Mason * done 1875af31f5e5SChris Mason */ 1876af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1877af31f5e5SChris Mason { 18786ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1879af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1882af31f5e5SChris Mason 1883af31f5e5SChris Mason /* 1884af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1885af31f5e5SChris Mason * regardless of which ones we use today 1886af31f5e5SChris Mason */ 1887af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1890af31f5e5SChris Mason 1891af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1892af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1893af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1896af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1899af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1900af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1901af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1902af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1903af31f5e5SChris Mason 1904af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1905af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1906af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1907af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1908af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1909af31f5e5SChris Mason 19107c7e82a7SChris Mason /* 19117c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19127c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19137c7e82a7SChris Mason */ 19147c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19157c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19167c7e82a7SChris Mason info->fs_root->node->start); 1917af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1918af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1919af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19217c7e82a7SChris Mason } 1922af31f5e5SChris Mason 1923af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1924af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1925af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1926af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1927af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1928af31f5e5SChris Mason 1929af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1930af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1931af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1932af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1933af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1934af31f5e5SChris Mason 1935af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1936af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1937af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1938af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1939af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1940af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1941af31f5e5SChris Mason 1942af31f5e5SChris Mason /* 1943af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1944af31f5e5SChris Mason * for the next commit 1945af31f5e5SChris Mason */ 1946af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1947af31f5e5SChris Mason &info->super_for_commit->super_roots, 1948af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1949af31f5e5SChris Mason } 1950af31f5e5SChris Mason 1951af31f5e5SChris Mason /* 1952bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1953bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1954bd2336b2SNikolay Borisov * 1955bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1956bd2336b2SNikolay Borisov * priority - priority of backup root required 1957bd2336b2SNikolay Borisov * 1958bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1959bd2336b2SNikolay Borisov */ 1960bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1961bd2336b2SNikolay Borisov { 1962bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1963bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1964bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1965bd2336b2SNikolay Borisov 1966bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1967bd2336b2SNikolay Borisov if (priority == 0) 1968bd2336b2SNikolay Borisov return backup_index; 1969bd2336b2SNikolay Borisov 1970bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1971bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1972bd2336b2SNikolay Borisov } else { 1973bd2336b2SNikolay Borisov return -EINVAL; 1974bd2336b2SNikolay Borisov } 1975bd2336b2SNikolay Borisov 1976bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1977bd2336b2SNikolay Borisov 1978bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1979bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1980bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1981bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1982bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1983bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1984bd2336b2SNikolay Borisov 1985bd2336b2SNikolay Borisov /* 1986bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1987bd2336b2SNikolay Borisov * need a fsck 1988bd2336b2SNikolay Borisov */ 1989bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1990bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1991bd2336b2SNikolay Borisov 1992bd2336b2SNikolay Borisov return backup_index; 1993bd2336b2SNikolay Borisov } 1994bd2336b2SNikolay Borisov 19957abadb64SLiu Bo /* helper to cleanup workers */ 19967abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19977abadb64SLiu Bo { 1998dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1999afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20005cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2001fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2002fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20038b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2004d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2005fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2006fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 20075b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2008e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2009736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2010a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2011fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2012b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2013b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2014a9b9477dSFilipe Manana /* 2015a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2016a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2017a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2018a9b9477dSFilipe Manana */ 2019a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2020a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20217abadb64SLiu Bo } 20227abadb64SLiu Bo 20232e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20242e9f5954SRashika { 20252e9f5954SRashika if (root) { 20262e9f5954SRashika free_extent_buffer(root->node); 20272e9f5954SRashika free_extent_buffer(root->commit_root); 20282e9f5954SRashika root->node = NULL; 20292e9f5954SRashika root->commit_root = NULL; 20302e9f5954SRashika } 20312e9f5954SRashika } 20322e9f5954SRashika 2033af31f5e5SChris Mason /* helper to cleanup tree roots */ 20344273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2035af31f5e5SChris Mason { 20362e9f5954SRashika free_root_extent_buffers(info->tree_root); 2037655b09feSJosef Bacik 20382e9f5954SRashika free_root_extent_buffers(info->dev_root); 20392e9f5954SRashika free_root_extent_buffers(info->extent_root); 20402e9f5954SRashika free_root_extent_buffers(info->csum_root); 20412e9f5954SRashika free_root_extent_buffers(info->quota_root); 20422e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20434273eaffSAnand Jain if (free_chunk_root) 20442e9f5954SRashika free_root_extent_buffers(info->chunk_root); 204570f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2046af31f5e5SChris Mason } 2047af31f5e5SChris Mason 2048faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2049171f6537SJosef Bacik { 2050171f6537SJosef Bacik int ret; 2051171f6537SJosef Bacik struct btrfs_root *gang[8]; 2052171f6537SJosef Bacik int i; 2053171f6537SJosef Bacik 2054171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2055171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2056171f6537SJosef Bacik struct btrfs_root, root_list); 2057171f6537SJosef Bacik list_del(&gang[0]->root_list); 2058171f6537SJosef Bacik 205927cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2060cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2061171f6537SJosef Bacik } else { 2062171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2063171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 206400246528SJosef Bacik btrfs_put_root(gang[0]); 2065171f6537SJosef Bacik } 2066171f6537SJosef Bacik } 2067171f6537SJosef Bacik 2068171f6537SJosef Bacik while (1) { 2069171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2070171f6537SJosef Bacik (void **)gang, 0, 2071171f6537SJosef Bacik ARRAY_SIZE(gang)); 2072171f6537SJosef Bacik if (!ret) 2073171f6537SJosef Bacik break; 2074171f6537SJosef Bacik for (i = 0; i < ret; i++) 2075cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2076171f6537SJosef Bacik } 20771a4319ccSLiu Bo 20781a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20791a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 20802ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 20811a4319ccSLiu Bo } 2082171f6537SJosef Bacik } 2083af31f5e5SChris Mason 2084638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2085638aa7edSEric Sandeen { 2086638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2087638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2088638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2089638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2090638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2091638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2092ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2093638aa7edSEric Sandeen } 2094638aa7edSEric Sandeen 2095779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2096779a65a4SEric Sandeen { 2097779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2098779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2099779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2100779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2101779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2102779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2103779a65a4SEric Sandeen } 2104779a65a4SEric Sandeen 21056bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2106f37938e0SEric Sandeen { 21072ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21082ff7e61eSJeff Mahoney 21092ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21102ff7e61eSJeff Mahoney set_nlink(inode, 1); 2111f37938e0SEric Sandeen /* 2112f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2113f37938e0SEric Sandeen * the real end of the address space is determined by all of 2114f37938e0SEric Sandeen * the devices in the system 2115f37938e0SEric Sandeen */ 21162ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21172ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2118f37938e0SEric Sandeen 21192ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 212043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 212143eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 21227b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21232ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2124f37938e0SEric Sandeen 21252ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2126f37938e0SEric Sandeen 21272ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21282ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21292ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21302ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2131f37938e0SEric Sandeen } 2132f37938e0SEric Sandeen 2133ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2134ad618368SEric Sandeen { 2135ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2136129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21377f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2138ad618368SEric Sandeen } 2139ad618368SEric Sandeen 2140f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2141f9e92e40SEric Sandeen { 2142f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2143f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2144f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2145f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2146f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2147f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2148d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2149f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2150f9e92e40SEric Sandeen } 2151f9e92e40SEric Sandeen 21522a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21532a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21542a458198SEric Sandeen { 2155f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21566f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21572a458198SEric Sandeen 21582a458198SEric Sandeen fs_info->workers = 2159cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2160cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21612a458198SEric Sandeen 21622a458198SEric Sandeen fs_info->delalloc_workers = 2163cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2164cb001095SJeff Mahoney flags, max_active, 2); 21652a458198SEric Sandeen 21662a458198SEric Sandeen fs_info->flush_workers = 2167cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2168cb001095SJeff Mahoney flags, max_active, 0); 21692a458198SEric Sandeen 21702a458198SEric Sandeen fs_info->caching_workers = 2171cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21722a458198SEric Sandeen 21732a458198SEric Sandeen fs_info->fixup_workers = 2174cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21752a458198SEric Sandeen 21762a458198SEric Sandeen /* 21772a458198SEric Sandeen * endios are largely parallel and should have a very 21782a458198SEric Sandeen * low idle thresh 21792a458198SEric Sandeen */ 21802a458198SEric Sandeen fs_info->endio_workers = 2181cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21822a458198SEric Sandeen fs_info->endio_meta_workers = 2183cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2184cb001095SJeff Mahoney max_active, 4); 21852a458198SEric Sandeen fs_info->endio_meta_write_workers = 2186cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2187cb001095SJeff Mahoney max_active, 2); 21882a458198SEric Sandeen fs_info->endio_raid56_workers = 2189cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2190cb001095SJeff Mahoney max_active, 4); 21912a458198SEric Sandeen fs_info->endio_repair_workers = 2192cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 21932a458198SEric Sandeen fs_info->rmw_workers = 2194cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21952a458198SEric Sandeen fs_info->endio_write_workers = 2196cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2197cb001095SJeff Mahoney max_active, 2); 21982a458198SEric Sandeen fs_info->endio_freespace_worker = 2199cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2200cb001095SJeff Mahoney max_active, 0); 22012a458198SEric Sandeen fs_info->delayed_workers = 2202cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2203cb001095SJeff Mahoney max_active, 0); 22042a458198SEric Sandeen fs_info->readahead_workers = 2205cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2206cb001095SJeff Mahoney max_active, 2); 22072a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2208cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2209b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2210b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 22112a458198SEric Sandeen 22122a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2213ba8a9d07SChris Mason fs_info->flush_workers && 22142a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22152a458198SEric Sandeen fs_info->endio_meta_write_workers && 22162a458198SEric Sandeen fs_info->endio_repair_workers && 22172a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22182a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22192a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22202a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2221b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2222b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22232a458198SEric Sandeen return -ENOMEM; 22242a458198SEric Sandeen } 22252a458198SEric Sandeen 22262a458198SEric Sandeen return 0; 22272a458198SEric Sandeen } 22282a458198SEric Sandeen 22296d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22306d97c6e3SJohannes Thumshirn { 22316d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2232b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22336d97c6e3SJohannes Thumshirn 2234b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22356d97c6e3SJohannes Thumshirn 22366d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22376d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2238b4e967beSDavid Sterba csum_driver); 22396d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22406d97c6e3SJohannes Thumshirn } 22416d97c6e3SJohannes Thumshirn 22426d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22436d97c6e3SJohannes Thumshirn 22446d97c6e3SJohannes Thumshirn return 0; 22456d97c6e3SJohannes Thumshirn } 22466d97c6e3SJohannes Thumshirn 224763443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 224863443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 224963443bf5SEric Sandeen { 225063443bf5SEric Sandeen int ret; 225163443bf5SEric Sandeen struct btrfs_root *log_tree_root; 225263443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 225363443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2254581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 225563443bf5SEric Sandeen 225663443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2257f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 225863443bf5SEric Sandeen return -EIO; 225963443bf5SEric Sandeen } 226063443bf5SEric Sandeen 226196dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 226296dfcb46SJosef Bacik GFP_KERNEL); 226363443bf5SEric Sandeen if (!log_tree_root) 226463443bf5SEric Sandeen return -ENOMEM; 226563443bf5SEric Sandeen 22662ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2267581c1760SQu Wenruo fs_info->generation + 1, 2268581c1760SQu Wenruo level, NULL); 226964c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2270f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22710eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 227200246528SJosef Bacik btrfs_put_root(log_tree_root); 22730eeff236SLiu Bo return ret; 227464c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2275f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 227663443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 227700246528SJosef Bacik btrfs_put_root(log_tree_root); 227863443bf5SEric Sandeen return -EIO; 227963443bf5SEric Sandeen } 228063443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 228163443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 228263443bf5SEric Sandeen if (ret) { 22830b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 228463443bf5SEric Sandeen "Failed to recover log tree"); 228563443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 228600246528SJosef Bacik btrfs_put_root(log_tree_root); 228763443bf5SEric Sandeen return ret; 228863443bf5SEric Sandeen } 228963443bf5SEric Sandeen 2290bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22916bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 229263443bf5SEric Sandeen if (ret) 229363443bf5SEric Sandeen return ret; 229463443bf5SEric Sandeen } 229563443bf5SEric Sandeen 229663443bf5SEric Sandeen return 0; 229763443bf5SEric Sandeen } 229863443bf5SEric Sandeen 22996bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23004bbcaa64SEric Sandeen { 23016bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2302a4f3d2c4SDavid Sterba struct btrfs_root *root; 23034bbcaa64SEric Sandeen struct btrfs_key location; 23044bbcaa64SEric Sandeen int ret; 23054bbcaa64SEric Sandeen 23066bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23076bccf3abSJeff Mahoney 23084bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23094bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23104bbcaa64SEric Sandeen location.offset = 0; 23114bbcaa64SEric Sandeen 2312a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2313f50f4353SLiu Bo if (IS_ERR(root)) { 2314f50f4353SLiu Bo ret = PTR_ERR(root); 2315f50f4353SLiu Bo goto out; 2316f50f4353SLiu Bo } 2317a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2318a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23194bbcaa64SEric Sandeen 23204bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2321a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2322f50f4353SLiu Bo if (IS_ERR(root)) { 2323f50f4353SLiu Bo ret = PTR_ERR(root); 2324f50f4353SLiu Bo goto out; 2325f50f4353SLiu Bo } 2326a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2327a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23284bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23294bbcaa64SEric Sandeen 23304bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2331a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2332f50f4353SLiu Bo if (IS_ERR(root)) { 2333f50f4353SLiu Bo ret = PTR_ERR(root); 2334f50f4353SLiu Bo goto out; 2335f50f4353SLiu Bo } 2336a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2337a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23384bbcaa64SEric Sandeen 23394bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2340a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2341a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2342a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2343afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2344a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23454bbcaa64SEric Sandeen } 23464bbcaa64SEric Sandeen 23474bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2348a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2349a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2350a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23514bbcaa64SEric Sandeen if (ret != -ENOENT) 2352f50f4353SLiu Bo goto out; 23534bbcaa64SEric Sandeen } else { 2354a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2355a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23564bbcaa64SEric Sandeen } 23574bbcaa64SEric Sandeen 235870f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 235970f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 236070f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2361f50f4353SLiu Bo if (IS_ERR(root)) { 2362f50f4353SLiu Bo ret = PTR_ERR(root); 2363f50f4353SLiu Bo goto out; 2364f50f4353SLiu Bo } 236570f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 236670f6d82eSOmar Sandoval fs_info->free_space_root = root; 236770f6d82eSOmar Sandoval } 236870f6d82eSOmar Sandoval 23694bbcaa64SEric Sandeen return 0; 2370f50f4353SLiu Bo out: 2371f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2372f50f4353SLiu Bo location.objectid, ret); 2373f50f4353SLiu Bo return ret; 23744bbcaa64SEric Sandeen } 23754bbcaa64SEric Sandeen 2376069ec957SQu Wenruo /* 2377069ec957SQu Wenruo * Real super block validation 2378069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2379069ec957SQu Wenruo * 2380069ec957SQu Wenruo * @sb: super block to check 2381069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2382069ec957SQu Wenruo * 0 the primary (1st) sb 2383069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2384069ec957SQu Wenruo * -1 skip bytenr check 2385069ec957SQu Wenruo */ 2386069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2387069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 238821a852b0SQu Wenruo { 238921a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 239021a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 239121a852b0SQu Wenruo int ret = 0; 239221a852b0SQu Wenruo 239321a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 239421a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 239521a852b0SQu Wenruo ret = -EINVAL; 239621a852b0SQu Wenruo } 239721a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 239821a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 239921a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 240021a852b0SQu Wenruo ret = -EINVAL; 240121a852b0SQu Wenruo } 240221a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 240321a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 240421a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 240521a852b0SQu Wenruo ret = -EINVAL; 240621a852b0SQu Wenruo } 240721a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 240821a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 240921a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 241021a852b0SQu Wenruo ret = -EINVAL; 241121a852b0SQu Wenruo } 241221a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 241321a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 241421a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 241521a852b0SQu Wenruo ret = -EINVAL; 241621a852b0SQu Wenruo } 241721a852b0SQu Wenruo 241821a852b0SQu Wenruo /* 241921a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 242021a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 242121a852b0SQu Wenruo */ 242221a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 242321a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 242421a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 242521a852b0SQu Wenruo ret = -EINVAL; 242621a852b0SQu Wenruo } 242721a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 242821a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 242921a852b0SQu Wenruo btrfs_err(fs_info, 243021a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 243121a852b0SQu Wenruo sectorsize, PAGE_SIZE); 243221a852b0SQu Wenruo ret = -EINVAL; 243321a852b0SQu Wenruo } 243421a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 243521a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 243621a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 243721a852b0SQu Wenruo ret = -EINVAL; 243821a852b0SQu Wenruo } 243921a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 244021a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 244121a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 244221a852b0SQu Wenruo ret = -EINVAL; 244321a852b0SQu Wenruo } 244421a852b0SQu Wenruo 244521a852b0SQu Wenruo /* Root alignment check */ 244621a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 244721a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 244821a852b0SQu Wenruo btrfs_super_root(sb)); 244921a852b0SQu Wenruo ret = -EINVAL; 245021a852b0SQu Wenruo } 245121a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 245221a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 245321a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 245421a852b0SQu Wenruo ret = -EINVAL; 245521a852b0SQu Wenruo } 245621a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 245721a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 245821a852b0SQu Wenruo btrfs_super_log_root(sb)); 245921a852b0SQu Wenruo ret = -EINVAL; 246021a852b0SQu Wenruo } 246121a852b0SQu Wenruo 2462de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24637239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 246421a852b0SQu Wenruo btrfs_err(fs_info, 24657239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2466de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 246721a852b0SQu Wenruo ret = -EINVAL; 246821a852b0SQu Wenruo } 246921a852b0SQu Wenruo 247021a852b0SQu Wenruo /* 247121a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 247221a852b0SQu Wenruo * done later 247321a852b0SQu Wenruo */ 247421a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 247521a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 247621a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 247721a852b0SQu Wenruo ret = -EINVAL; 247821a852b0SQu Wenruo } 247921a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 248021a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 248121a852b0SQu Wenruo btrfs_super_stripesize(sb)); 248221a852b0SQu Wenruo ret = -EINVAL; 248321a852b0SQu Wenruo } 248421a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 248521a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 248621a852b0SQu Wenruo btrfs_super_num_devices(sb)); 248721a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 248821a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 248921a852b0SQu Wenruo ret = -EINVAL; 249021a852b0SQu Wenruo } 249121a852b0SQu Wenruo 2492069ec957SQu Wenruo if (mirror_num >= 0 && 2493069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 249421a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 249521a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 249621a852b0SQu Wenruo ret = -EINVAL; 249721a852b0SQu Wenruo } 249821a852b0SQu Wenruo 249921a852b0SQu Wenruo /* 250021a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 250121a852b0SQu Wenruo * and one chunk 250221a852b0SQu Wenruo */ 250321a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 250421a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 250521a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 250621a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 250721a852b0SQu Wenruo ret = -EINVAL; 250821a852b0SQu Wenruo } 250921a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 251021a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 251121a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 251221a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 251321a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 251421a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 251521a852b0SQu Wenruo ret = -EINVAL; 251621a852b0SQu Wenruo } 251721a852b0SQu Wenruo 251821a852b0SQu Wenruo /* 251921a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 252021a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 252121a852b0SQu Wenruo */ 252221a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 252321a852b0SQu Wenruo btrfs_warn(fs_info, 252421a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 252521a852b0SQu Wenruo btrfs_super_generation(sb), 252621a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 252721a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 252821a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 252921a852b0SQu Wenruo btrfs_warn(fs_info, 253021a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 253121a852b0SQu Wenruo btrfs_super_generation(sb), 253221a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 253321a852b0SQu Wenruo 253421a852b0SQu Wenruo return ret; 253521a852b0SQu Wenruo } 253621a852b0SQu Wenruo 2537069ec957SQu Wenruo /* 2538069ec957SQu Wenruo * Validation of super block at mount time. 2539069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2540069ec957SQu Wenruo * flags will be skipped. 2541069ec957SQu Wenruo */ 2542069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2543069ec957SQu Wenruo { 2544069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2545069ec957SQu Wenruo } 2546069ec957SQu Wenruo 254775cb857dSQu Wenruo /* 254875cb857dSQu Wenruo * Validation of super block at write time. 254975cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 255075cb857dSQu Wenruo * overwritten soon. 255175cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 255275cb857dSQu Wenruo */ 255375cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 255475cb857dSQu Wenruo struct btrfs_super_block *sb) 255575cb857dSQu Wenruo { 255675cb857dSQu Wenruo int ret; 255775cb857dSQu Wenruo 255875cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 255975cb857dSQu Wenruo if (ret < 0) 256075cb857dSQu Wenruo goto out; 2561e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 256275cb857dSQu Wenruo ret = -EUCLEAN; 256375cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 256475cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 256575cb857dSQu Wenruo goto out; 256675cb857dSQu Wenruo } 256775cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 256875cb857dSQu Wenruo ret = -EUCLEAN; 256975cb857dSQu Wenruo btrfs_err(fs_info, 257075cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 257175cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 257275cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 257375cb857dSQu Wenruo goto out; 257475cb857dSQu Wenruo } 257575cb857dSQu Wenruo out: 257675cb857dSQu Wenruo if (ret < 0) 257775cb857dSQu Wenruo btrfs_err(fs_info, 257875cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 257975cb857dSQu Wenruo return ret; 258075cb857dSQu Wenruo } 258175cb857dSQu Wenruo 25826ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2583b8522a1eSNikolay Borisov { 25846ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2585b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2586b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2587b8522a1eSNikolay Borisov bool handle_error = false; 2588b8522a1eSNikolay Borisov int ret = 0; 2589b8522a1eSNikolay Borisov int i; 2590b8522a1eSNikolay Borisov 2591b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2592b8522a1eSNikolay Borisov u64 generation; 2593b8522a1eSNikolay Borisov int level; 2594b8522a1eSNikolay Borisov 2595b8522a1eSNikolay Borisov if (handle_error) { 2596b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2597b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2598b8522a1eSNikolay Borisov tree_root->node = NULL; 2599b8522a1eSNikolay Borisov 2600b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2601b8522a1eSNikolay Borisov break; 2602b8522a1eSNikolay Borisov 2603b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2604b8522a1eSNikolay Borisov 2605b8522a1eSNikolay Borisov /* 2606b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2607b8522a1eSNikolay Borisov * valid 2608b8522a1eSNikolay Borisov */ 2609b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2610b8522a1eSNikolay Borisov 2611b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2612b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2613b8522a1eSNikolay Borisov 2614b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26156ef108ddSNikolay Borisov backup_index = ret; 2616b8522a1eSNikolay Borisov if (ret < 0) 2617b8522a1eSNikolay Borisov return ret; 2618b8522a1eSNikolay Borisov } 2619b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2620b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2621b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 2622b8522a1eSNikolay Borisov generation, level, NULL); 2623b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node) || 2624b8522a1eSNikolay Borisov !extent_buffer_uptodate(tree_root->node)) { 2625b8522a1eSNikolay Borisov handle_error = true; 2626b8522a1eSNikolay Borisov 2627b8522a1eSNikolay Borisov if (IS_ERR(tree_root->node)) 2628b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 2629b8522a1eSNikolay Borisov else if (!extent_buffer_uptodate(tree_root->node)) 2630b8522a1eSNikolay Borisov ret = -EUCLEAN; 2631b8522a1eSNikolay Borisov 2632b8522a1eSNikolay Borisov btrfs_warn(fs_info, "failed to read tree root"); 2633b8522a1eSNikolay Borisov continue; 2634b8522a1eSNikolay Borisov } 2635b8522a1eSNikolay Borisov 2636b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2637b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2638b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2639b8522a1eSNikolay Borisov 2640336a0d8dSNikolay Borisov /* 2641336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2642336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2643336a0d8dSNikolay Borisov */ 2644b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2645b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2646b8522a1eSNikolay Borisov if (ret < 0) { 2647b8522a1eSNikolay Borisov handle_error = true; 2648b8522a1eSNikolay Borisov continue; 2649b8522a1eSNikolay Borisov } 2650b8522a1eSNikolay Borisov 2651b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2652b8522a1eSNikolay Borisov 2653b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2654b8522a1eSNikolay Borisov if (ret < 0) { 2655b8522a1eSNikolay Borisov handle_error = true; 2656b8522a1eSNikolay Borisov continue; 2657b8522a1eSNikolay Borisov } 2658b8522a1eSNikolay Borisov 2659b8522a1eSNikolay Borisov /* All successful */ 2660b8522a1eSNikolay Borisov fs_info->generation = generation; 2661b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26626ef108ddSNikolay Borisov 26636ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26646ef108ddSNikolay Borisov if (backup_index < 0) { 26656ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26666ef108ddSNikolay Borisov } else { 26676ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26686ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26696ef108ddSNikolay Borisov } 2670b8522a1eSNikolay Borisov break; 2671b8522a1eSNikolay Borisov } 2672b8522a1eSNikolay Borisov 2673b8522a1eSNikolay Borisov return ret; 2674b8522a1eSNikolay Borisov } 2675b8522a1eSNikolay Borisov 26768260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2677eb60ceacSChris Mason { 267876dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2679f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26808fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2681facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 268224bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2683eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 268411833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2685eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2686a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 268776dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 268824bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26894cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2690ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2691f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 269247ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2693f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2694d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 269567c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26967585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2697573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2698de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 269919c00ddcSChris Mason 27000b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27016324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2702f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 270347ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2704bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2705bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 2706bd647ce3SJosef Bacik #endif 2707c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 270866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 270966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 271066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 271166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 271266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 271366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 271466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2715ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2716ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2717ba2c4d4eSJosef Bacik 2718771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27194cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27202fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2721034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2722fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 272395ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27249ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27254cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2726a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2727f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27288b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2729f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 273090519d66SArne Jansen /* readahead state */ 2731d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 273290519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2733fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2734c8b97818SChris Mason 2735b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2736b34b086cSChris Mason num_online_cpus() + 2, 8); 27370afbaf8cSChris Mason 2738199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2739199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 274069fe2d75SJosef Bacik 2741638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 274221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 274321adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 274421adbd5cSStefan Behrens #endif 2745779a65a4SEric Sandeen btrfs_init_balance(fs_info); 274621c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2747a2de733cSArne Jansen 27480f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 27496bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2750a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 27510f9dd46cSJosef Bacik 275243eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[0], 275343eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS0, NULL); 275443eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[1], 275543eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS1, NULL); 275611833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2757afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 275839279cc3SChris Mason 27595a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2760e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2761925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2762a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2763a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27641bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27659e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2766c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 276776dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2768803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2769fa9c0d79SChris Mason 2770ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2771f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2772b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2773416ac51dSArne Jansen 2774fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2775fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2776fa9c0d79SChris Mason 2777e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2778f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2779bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27804854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2781034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 27823768f368SChris Mason 2783da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2784da17066cSJeff Mahoney fs_info->nodesize = 4096; 2785da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2786da17066cSJeff Mahoney fs_info->stripesize = 4096; 2787da17066cSJeff Mahoney 2788eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2789eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2790eede2bf3SOmar Sandoval 27919e967495SFilipe Manana fs_info->send_in_progress = 0; 27928260edbaSJosef Bacik } 27938260edbaSJosef Bacik 27948260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 27958260edbaSJosef Bacik { 27968260edbaSJosef Bacik int ret; 27978260edbaSJosef Bacik 27988260edbaSJosef Bacik fs_info->sb = sb; 27998260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28008260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 28019e967495SFilipe Manana 2802ae18c37aSJosef Bacik ret = init_srcu_struct(&fs_info->subvol_srcu); 2803ae18c37aSJosef Bacik if (ret) 2804ae18c37aSJosef Bacik return ret; 2805ae18c37aSJosef Bacik 2806ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2807ae18c37aSJosef Bacik if (ret) 2808ae18c37aSJosef Bacik goto fail; 2809ae18c37aSJosef Bacik 2810ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2811ae18c37aSJosef Bacik if (ret) 2812ae18c37aSJosef Bacik goto fail; 2813ae18c37aSJosef Bacik 2814ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2815ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2816ae18c37aSJosef Bacik 2817ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2818ae18c37aSJosef Bacik if (ret) 2819ae18c37aSJosef Bacik goto fail; 2820ae18c37aSJosef Bacik 2821ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2822ae18c37aSJosef Bacik GFP_KERNEL); 2823ae18c37aSJosef Bacik if (ret) 2824ae18c37aSJosef Bacik goto fail; 2825ae18c37aSJosef Bacik 2826ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2827ae18c37aSJosef Bacik GFP_KERNEL); 2828ae18c37aSJosef Bacik if (!fs_info->delayed_root) { 2829ae18c37aSJosef Bacik ret = -ENOMEM; 2830ae18c37aSJosef Bacik goto fail; 2831ae18c37aSJosef Bacik } 2832ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2833ae18c37aSJosef Bacik 283453b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 2835ae18c37aSJosef Bacik if (ret) 2836ae18c37aSJosef Bacik goto fail; 2837ae18c37aSJosef Bacik 2838ae18c37aSJosef Bacik return 0; 2839ae18c37aSJosef Bacik fail: 2840ae18c37aSJosef Bacik cleanup_srcu_struct(&fs_info->subvol_srcu); 2841ae18c37aSJosef Bacik return ret; 2842ae18c37aSJosef Bacik } 2843ae18c37aSJosef Bacik 2844ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 2845ae18c37aSJosef Bacik char *options) 2846ae18c37aSJosef Bacik { 2847ae18c37aSJosef Bacik u32 sectorsize; 2848ae18c37aSJosef Bacik u32 nodesize; 2849ae18c37aSJosef Bacik u32 stripesize; 2850ae18c37aSJosef Bacik u64 generation; 2851ae18c37aSJosef Bacik u64 features; 2852ae18c37aSJosef Bacik u16 csum_type; 2853ae18c37aSJosef Bacik struct btrfs_key location; 2854ae18c37aSJosef Bacik struct buffer_head *bh; 2855ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 2856ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2857ae18c37aSJosef Bacik struct btrfs_root *tree_root; 2858ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 2859ae18c37aSJosef Bacik int ret; 2860ae18c37aSJosef Bacik int err = -EINVAL; 2861ae18c37aSJosef Bacik int clear_free_space_tree = 0; 2862ae18c37aSJosef Bacik int level; 2863ae18c37aSJosef Bacik 28648260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 286553b381b3SDavid Woodhouse if (ret) { 286683c8266aSDavid Sterba err = ret; 2867ae18c37aSJosef Bacik goto fail; 286853b381b3SDavid Woodhouse } 286953b381b3SDavid Woodhouse 2870ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 2871ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 2872ae18c37aSJosef Bacik GFP_KERNEL); 2873ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 2874ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 2875ae18c37aSJosef Bacik GFP_KERNEL); 2876ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 2877ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 2878ae18c37aSJosef Bacik err = -ENOMEM; 2879ae18c37aSJosef Bacik goto fail_srcu; 2880ae18c37aSJosef Bacik } 2881ae18c37aSJosef Bacik 2882ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 2883ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 2884ae18c37aSJosef Bacik err = -ENOMEM; 2885ae18c37aSJosef Bacik goto fail_srcu; 2886ae18c37aSJosef Bacik } 2887ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 2888ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 2889ae18c37aSJosef Bacik 28903c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 28911104a885SDavid Sterba 28921104a885SDavid Sterba /* 28931104a885SDavid Sterba * Read super block and check the signature bytes only 28941104a885SDavid Sterba */ 2895a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 289692fc03fbSAnand Jain if (IS_ERR(bh)) { 289792fc03fbSAnand Jain err = PTR_ERR(bh); 289816cdcec7SMiao Xie goto fail_alloc; 289920b45077SDave Young } 290039279cc3SChris Mason 29011104a885SDavid Sterba /* 29028dc3f22cSJohannes Thumshirn * Verify the type first, if that or the the checksum value are 29038dc3f22cSJohannes Thumshirn * corrupted, we'll find out 29048dc3f22cSJohannes Thumshirn */ 290551bce6c9SJohannes Thumshirn csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data); 290651bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 29078dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 290851bce6c9SJohannes Thumshirn csum_type); 29098dc3f22cSJohannes Thumshirn err = -EINVAL; 29108dc3f22cSJohannes Thumshirn brelse(bh); 29118dc3f22cSJohannes Thumshirn goto fail_alloc; 29128dc3f22cSJohannes Thumshirn } 29138dc3f22cSJohannes Thumshirn 29146d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 29156d97c6e3SJohannes Thumshirn if (ret) { 29166d97c6e3SJohannes Thumshirn err = ret; 29176d97c6e3SJohannes Thumshirn goto fail_alloc; 29186d97c6e3SJohannes Thumshirn } 29196d97c6e3SJohannes Thumshirn 29208dc3f22cSJohannes Thumshirn /* 29211104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 29221104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 29231104a885SDavid Sterba */ 2924ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 292505135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 29261104a885SDavid Sterba err = -EINVAL; 2927b2acdddfSAnand Jain brelse(bh); 2928141386e1SJosef Bacik goto fail_alloc; 29291104a885SDavid Sterba } 29301104a885SDavid Sterba 29311104a885SDavid Sterba /* 29321104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 29331104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29341104a885SDavid Sterba * the whole block of INFO_SIZE 29351104a885SDavid Sterba */ 29366c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 2937a061fc8dSChris Mason brelse(bh); 29385f39d397SChris Mason 2939fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2940fbc6feaeSNikolay Borisov 2941de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2942de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2943de37aa51SNikolay Borisov 29447239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2945de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2946de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2947de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29487239ff4bSNikolay Borisov } 29490b86a832SChris Mason 2950fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2951fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2952fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2953fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2954fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2955fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2956fbc6feaeSNikolay Borisov } 2957fbc6feaeSNikolay Borisov 2958fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2959fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2960de37aa51SNikolay Borisov 2961069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 29621104a885SDavid Sterba if (ret) { 296305135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 29641104a885SDavid Sterba err = -EINVAL; 2965141386e1SJosef Bacik goto fail_alloc; 29661104a885SDavid Sterba } 29671104a885SDavid Sterba 29680f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 2969141386e1SJosef Bacik goto fail_alloc; 29700f7d52f4SChris Mason 2971acce952bSliubo /* check FS state, whether FS is broken. */ 297287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 297387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2974acce952bSliubo 297575e7cb7fSLiu Bo /* 297675e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 297775e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 297875e7cb7fSLiu Bo */ 297975e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 298075e7cb7fSLiu Bo 29812ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 29822b82032cSYan Zheng if (ret) { 29832b82032cSYan Zheng err = ret; 2984141386e1SJosef Bacik goto fail_alloc; 29852b82032cSYan Zheng } 2986dfe25020SChris Mason 2987f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2988f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2989f2b636e8SJosef Bacik if (features) { 299005135f59SDavid Sterba btrfs_err(fs_info, 299105135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2992c1c9ff7cSGeert Uytterhoeven features); 2993f2b636e8SJosef Bacik err = -EINVAL; 2994141386e1SJosef Bacik goto fail_alloc; 2995f2b636e8SJosef Bacik } 2996f2b636e8SJosef Bacik 29975d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 29985d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 29990b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3000a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 30015c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 30025c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3003727011e0SChris Mason 30043173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 300505135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 30063173a18fSJosef Bacik 3007727011e0SChris Mason /* 3008727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3009727011e0SChris Mason * they are bigger than the page size 3010727011e0SChris Mason */ 301109cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3012727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 301305135f59SDavid Sterba btrfs_info(fs_info, 301405135f59SDavid Sterba "flagging fs with big metadata feature"); 3015727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3016727011e0SChris Mason } 3017727011e0SChris Mason 3018bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3019bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3020b7f67055SChandan Rajendra stripesize = sectorsize; 3021707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3022963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3023bc3f116fSChris Mason 3024da17066cSJeff Mahoney /* Cache block sizes */ 3025da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3026da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3027da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3028da17066cSJeff Mahoney 3029bc3f116fSChris Mason /* 3030bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3031bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3032bc3f116fSChris Mason */ 3033bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3034707e8a07SDavid Sterba (sectorsize != nodesize)) { 303505135f59SDavid Sterba btrfs_err(fs_info, 303605135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 303705135f59SDavid Sterba nodesize, sectorsize); 3038141386e1SJosef Bacik goto fail_alloc; 3039bc3f116fSChris Mason } 3040bc3f116fSChris Mason 3041ceda0864SMiao Xie /* 3042ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3043ceda0864SMiao Xie * update the flag. 3044ceda0864SMiao Xie */ 30455d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 30465d4f98a2SYan Zheng 3047f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3048f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3049bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 305005135f59SDavid Sterba btrfs_err(fs_info, 305105135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3052c1c9ff7cSGeert Uytterhoeven features); 3053f2b636e8SJosef Bacik err = -EINVAL; 3054141386e1SJosef Bacik goto fail_alloc; 3055f2b636e8SJosef Bacik } 305661d92c32SChris Mason 30572a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 30582a458198SEric Sandeen if (ret) { 30592a458198SEric Sandeen err = ret; 30600dc3b84aSJosef Bacik goto fail_sb_buffer; 30610dc3b84aSJosef Bacik } 30624543df7eSChris Mason 30639e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 30649e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 30659e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 3066b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 30679e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 30689e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 30694575c9ccSChris Mason 3070a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3071a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3072de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3073db94535dSChris Mason 3074925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 30756bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3076925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 307784eed90fSChris Mason if (ret) { 307805135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 30795d4f98a2SYan Zheng goto fail_sb_buffer; 308084eed90fSChris Mason } 30810b86a832SChris Mason 308284234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3083581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 30840b86a832SChris Mason 30852ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 30860b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3087581c1760SQu Wenruo generation, level, NULL); 308864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 308964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 309005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3091e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3092e5fffbacSchandan free_extent_buffer(chunk_root->node); 309395ab1f64SZhao Lei chunk_root->node = NULL; 3094af31f5e5SChris Mason goto fail_tree_roots; 309583121942SDavid Woodhouse } 30965d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 30975d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 30980b86a832SChris Mason 3099e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3100b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 3101e17cade2SChris Mason 31025b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 31032b82032cSYan Zheng if (ret) { 310405135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3105af31f5e5SChris Mason goto fail_tree_roots; 31062b82032cSYan Zheng } 31070b86a832SChris Mason 31088dabb742SStefan Behrens /* 31099b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 31109b99b115SAnand Jain * device replace procedure 31118dabb742SStefan Behrens */ 31129b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3113dfe25020SChris Mason 3114a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 311505135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3116a6b0d5c8SChris Mason goto fail_tree_roots; 3117a6b0d5c8SChris Mason } 3118a6b0d5c8SChris Mason 3119b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 31204bbcaa64SEric Sandeen if (ret) 3121b8522a1eSNikolay Borisov goto fail_tree_roots; 31228929ecfaSYan, Zheng 3123cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3124cf90d884SQu Wenruo if (ret) { 3125cf90d884SQu Wenruo btrfs_err(fs_info, 3126cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3127cf90d884SQu Wenruo ret); 3128cf90d884SQu Wenruo goto fail_block_groups; 3129cf90d884SQu Wenruo } 313068310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 313168310a5eSIlya Dryomov if (ret) { 313205135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 313368310a5eSIlya Dryomov goto fail_block_groups; 313468310a5eSIlya Dryomov } 313568310a5eSIlya Dryomov 3136733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3137733f4fbbSStefan Behrens if (ret) { 313805135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3139733f4fbbSStefan Behrens goto fail_block_groups; 3140733f4fbbSStefan Behrens } 3141733f4fbbSStefan Behrens 31428dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 31438dabb742SStefan Behrens if (ret) { 314405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 31458dabb742SStefan Behrens goto fail_block_groups; 31468dabb742SStefan Behrens } 31478dabb742SStefan Behrens 31489b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 31498dabb742SStefan Behrens 3150c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3151b7c35e81SAnand Jain if (ret) { 315205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 315305135f59SDavid Sterba ret); 3154b7c35e81SAnand Jain goto fail_block_groups; 3155b7c35e81SAnand Jain } 3156b7c35e81SAnand Jain 315796f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31585ac1d209SJeff Mahoney if (ret) { 315905135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3160b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31615ac1d209SJeff Mahoney } 31625ac1d209SJeff Mahoney 3163c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3164c59021f8Sliubo if (ret) { 316505135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31662365dd3cSAnand Jain goto fail_sysfs; 3167c59021f8Sliubo } 3168c59021f8Sliubo 31695b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31701b1d1f66SJosef Bacik if (ret) { 317105135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 31722365dd3cSAnand Jain goto fail_sysfs; 31731b1d1f66SJosef Bacik } 31744330e183SQu Wenruo 31756528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 317605135f59SDavid Sterba btrfs_warn(fs_info, 317752042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 31782365dd3cSAnand Jain goto fail_sysfs; 3179292fd7fcSStefan Behrens } 31809078a3e1SChris Mason 3181a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3182a74a4b97SChris Mason "btrfs-cleaner"); 318357506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 31842365dd3cSAnand Jain goto fail_sysfs; 3185a74a4b97SChris Mason 3186a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3187a74a4b97SChris Mason tree_root, 3188a74a4b97SChris Mason "btrfs-transaction"); 318957506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 31903f157a2fSChris Mason goto fail_cleaner; 3191a74a4b97SChris Mason 3192583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3193c289811cSChris Mason !fs_info->fs_devices->rotating) { 3194583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3195c289811cSChris Mason } 3196c289811cSChris Mason 3197572d9ab7SDavid Sterba /* 319801327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3199572d9ab7SDavid Sterba * not have to wait for transaction commit 3200572d9ab7SDavid Sterba */ 3201572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 32023818aea2SQu Wenruo 320321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 32040b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 32052ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 32060b246afaSJeff Mahoney btrfs_test_opt(fs_info, 320721adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 320821adbd5cSStefan Behrens 1 : 0, 320921adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 321021adbd5cSStefan Behrens if (ret) 321105135f59SDavid Sterba btrfs_warn(fs_info, 321205135f59SDavid Sterba "failed to initialize integrity check module: %d", 321305135f59SDavid Sterba ret); 321421adbd5cSStefan Behrens } 321521adbd5cSStefan Behrens #endif 3216bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3217bcef60f2SArne Jansen if (ret) 3218bcef60f2SArne Jansen goto fail_trans_kthread; 321921adbd5cSStefan Behrens 3220fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3221fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3222fd708b81SJosef Bacik 322396da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 322496da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32250b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3226e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 322763443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 322879787eaaSJeff Mahoney if (ret) { 322963443bf5SEric Sandeen err = ret; 323028c16cbbSWang Shilong goto fail_qgroup; 3231e556ce2cSYan Zheng } 3232e02119d5SChris Mason } 32331a40e23bSZheng Yan 32346bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 323579787eaaSJeff Mahoney if (ret) 323628c16cbbSWang Shilong goto fail_qgroup; 323776dda93cSYan, Zheng 3238bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3239d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 324044c44af2SIlya Dryomov if (ret) 324128c16cbbSWang Shilong goto fail_qgroup; 324290c711abSZygo Blaxell 324390c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 32445d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 324590c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3246d7ce5843SMiao Xie if (ret < 0) { 324705135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 324805135f59SDavid Sterba ret); 3249d7ce5843SMiao Xie err = -EINVAL; 3250bcef60f2SArne Jansen goto fail_qgroup; 3251d7ce5843SMiao Xie } 32527c2ca468SChris Mason } 32531a40e23bSZheng Yan 32543de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32553de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3256cb517eabSMiao Xie location.offset = 0; 32573de4586cSChris Mason 32583619c94fSJosef Bacik fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true); 32593140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32603140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3261f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3262315bf8efSJosef Bacik fs_info->fs_root = NULL; 3263bcef60f2SArne Jansen goto fail_qgroup; 32643140c9a3SDan Carpenter } 3265c289811cSChris Mason 3266bc98a42cSDavid Howells if (sb_rdonly(sb)) 32672b6ba629SIlya Dryomov return 0; 32682b6ba629SIlya Dryomov 3269f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3270f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32716675df31SOmar Sandoval clear_free_space_tree = 1; 32726675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32736675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32746675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 32756675df31SOmar Sandoval clear_free_space_tree = 1; 32766675df31SOmar Sandoval } 32776675df31SOmar Sandoval 32786675df31SOmar Sandoval if (clear_free_space_tree) { 3279f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3280f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3281f8d468a1SOmar Sandoval if (ret) { 3282f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3283f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 32846bccf3abSJeff Mahoney close_ctree(fs_info); 3285f8d468a1SOmar Sandoval return ret; 3286f8d468a1SOmar Sandoval } 3287f8d468a1SOmar Sandoval } 3288f8d468a1SOmar Sandoval 32890b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3290511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 329105135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3292511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3293511711afSChris Mason if (ret) { 329405135f59SDavid Sterba btrfs_warn(fs_info, 329505135f59SDavid Sterba "failed to create free space tree: %d", ret); 32966bccf3abSJeff Mahoney close_ctree(fs_info); 3297511711afSChris Mason return ret; 3298511711afSChris Mason } 3299511711afSChris Mason } 3300511711afSChris Mason 3301e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 33022b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33032b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33042b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 33056bccf3abSJeff Mahoney close_ctree(fs_info); 33062b6ba629SIlya Dryomov return ret; 33072b6ba629SIlya Dryomov } 3308e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 330959641015SIlya Dryomov 33102b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 33112b6ba629SIlya Dryomov if (ret) { 331205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 33136bccf3abSJeff Mahoney close_ctree(fs_info); 33142b6ba629SIlya Dryomov return ret; 3315e3acc2a6SJosef Bacik } 3316e3acc2a6SJosef Bacik 33178dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 33188dabb742SStefan Behrens if (ret) { 331905135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33206bccf3abSJeff Mahoney close_ctree(fs_info); 33218dabb742SStefan Behrens return ret; 33228dabb742SStefan Behrens } 33238dabb742SStefan Behrens 3324b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3325b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3326b382a324SJan Schmidt 33274bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 332805135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3329f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3330f7a81ea4SStefan Behrens if (ret) { 333105135f59SDavid Sterba btrfs_warn(fs_info, 333205135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33336bccf3abSJeff Mahoney close_ctree(fs_info); 3334f7a81ea4SStefan Behrens return ret; 3335f7a81ea4SStefan Behrens } 33360b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33374bbcaa64SEric Sandeen fs_info->generation != 33384bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 333905135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 334070f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 334170f80175SStefan Behrens if (ret) { 334205135f59SDavid Sterba btrfs_warn(fs_info, 334305135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 33446bccf3abSJeff Mahoney close_ctree(fs_info); 334570f80175SStefan Behrens return ret; 334670f80175SStefan Behrens } 334770f80175SStefan Behrens } else { 3348afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3349f7a81ea4SStefan Behrens } 3350afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 335147ab2a6cSJosef Bacik 33528dcddfa0SQu Wenruo /* 33538dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33548dcddfa0SQu Wenruo * no need to keep the flag 33558dcddfa0SQu Wenruo */ 33568dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33578dcddfa0SQu Wenruo 3358ad2b2c80SAl Viro return 0; 335939279cc3SChris Mason 3360bcef60f2SArne Jansen fail_qgroup: 3361bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33627c2ca468SChris Mason fail_trans_kthread: 33637c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33642ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3365faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33663f157a2fSChris Mason fail_cleaner: 3367a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33687c2ca468SChris Mason 33697c2ca468SChris Mason /* 33707c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 33717c2ca468SChris Mason * kthreads 33727c2ca468SChris Mason */ 33737c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 33747c2ca468SChris Mason 33752365dd3cSAnand Jain fail_sysfs: 33766618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 33772365dd3cSAnand Jain 3378b7c35e81SAnand Jain fail_fsdev_sysfs: 3379b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3380b7c35e81SAnand Jain 33811b1d1f66SJosef Bacik fail_block_groups: 338254067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3383af31f5e5SChris Mason 3384af31f5e5SChris Mason fail_tree_roots: 33854273eaffSAnand Jain free_root_pointers(fs_info, true); 33862b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3387af31f5e5SChris Mason 338839279cc3SChris Mason fail_sb_buffer: 33897abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 33905cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 339116cdcec7SMiao Xie fail_alloc: 3392586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3393586e46e2SIlya Dryomov 33944543df7eSChris Mason iput(fs_info->btree_inode); 339576dda93cSYan, Zheng fail_srcu: 339676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 33977e662854SQinghuang Feng fail: 3398586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3399ad2b2c80SAl Viro return err; 3400eb60ceacSChris Mason } 3401663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3402eb60ceacSChris Mason 3403f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3404f2984462SChris Mason { 3405f2984462SChris Mason if (uptodate) { 3406f2984462SChris Mason set_buffer_uptodate(bh); 3407f2984462SChris Mason } else { 3408442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3409442a4f63SStefan Behrens bh->b_private; 3410442a4f63SStefan Behrens 3411fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3412b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3413606686eeSJosef Bacik rcu_str_deref(device->name)); 341401327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 34151259ab75SChris Mason * our own ways of dealing with the IO errors 34161259ab75SChris Mason */ 3417f2984462SChris Mason clear_buffer_uptodate(bh); 3418442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3419f2984462SChris Mason } 3420f2984462SChris Mason unlock_buffer(bh); 3421f2984462SChris Mason put_bh(bh); 3422f2984462SChris Mason } 3423f2984462SChris Mason 342429c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 342529c36d72SAnand Jain struct buffer_head **bh_ret) 342629c36d72SAnand Jain { 342729c36d72SAnand Jain struct buffer_head *bh; 342829c36d72SAnand Jain struct btrfs_super_block *super; 342929c36d72SAnand Jain u64 bytenr; 343029c36d72SAnand Jain 343129c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 343229c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 343329c36d72SAnand Jain return -EINVAL; 343429c36d72SAnand Jain 34359f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 343629c36d72SAnand Jain /* 343729c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 343829c36d72SAnand Jain * the best option we have is to mark it EIO. 343929c36d72SAnand Jain */ 344029c36d72SAnand Jain if (!bh) 344129c36d72SAnand Jain return -EIO; 344229c36d72SAnand Jain 344329c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 344429c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 344529c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 344629c36d72SAnand Jain brelse(bh); 344729c36d72SAnand Jain return -EINVAL; 344829c36d72SAnand Jain } 344929c36d72SAnand Jain 345029c36d72SAnand Jain *bh_ret = bh; 345129c36d72SAnand Jain return 0; 345229c36d72SAnand Jain } 345329c36d72SAnand Jain 345429c36d72SAnand Jain 3455a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3456a512bbf8SYan Zheng { 3457a512bbf8SYan Zheng struct buffer_head *bh; 3458a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3459a512bbf8SYan Zheng struct btrfs_super_block *super; 3460a512bbf8SYan Zheng int i; 3461a512bbf8SYan Zheng u64 transid = 0; 346292fc03fbSAnand Jain int ret = -EINVAL; 3463a512bbf8SYan Zheng 3464a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3465a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3466a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3467a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3468a512bbf8SYan Zheng */ 3469a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 347029c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 347129c36d72SAnand Jain if (ret) 3472a512bbf8SYan Zheng continue; 3473a512bbf8SYan Zheng 3474a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3475a512bbf8SYan Zheng 3476a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3477a512bbf8SYan Zheng brelse(latest); 3478a512bbf8SYan Zheng latest = bh; 3479a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3480a512bbf8SYan Zheng } else { 3481a512bbf8SYan Zheng brelse(bh); 3482a512bbf8SYan Zheng } 3483a512bbf8SYan Zheng } 348492fc03fbSAnand Jain 348592fc03fbSAnand Jain if (!latest) 348692fc03fbSAnand Jain return ERR_PTR(ret); 348792fc03fbSAnand Jain 3488a512bbf8SYan Zheng return latest; 3489a512bbf8SYan Zheng } 3490a512bbf8SYan Zheng 34914eedeb75SHisashi Hifumi /* 3492abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3493abbb3b8eSDavid Sterba * buffer heads we write are pinned. 34944eedeb75SHisashi Hifumi * 3495abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3496abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3497abbb3b8eSDavid Sterba * same for write and wait phases. 34984eedeb75SHisashi Hifumi * 3499abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 35004eedeb75SHisashi Hifumi */ 3501a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3502abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3503a512bbf8SYan Zheng { 3504d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3505d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3506a512bbf8SYan Zheng struct buffer_head *bh; 3507a512bbf8SYan Zheng int i; 3508a512bbf8SYan Zheng int ret; 3509a512bbf8SYan Zheng int errors = 0; 3510a512bbf8SYan Zheng u64 bytenr; 35111b9e619cSOmar Sandoval int op_flags; 3512a512bbf8SYan Zheng 3513a512bbf8SYan Zheng if (max_mirrors == 0) 3514a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3515a512bbf8SYan Zheng 3516d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3517d5178578SJohannes Thumshirn 3518a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3519a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3520935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3521935e5cc9SMiao Xie device->commit_total_bytes) 3522a512bbf8SYan Zheng break; 3523a512bbf8SYan Zheng 3524a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3525a512bbf8SYan Zheng 3526d5178578SJohannes Thumshirn crypto_shash_init(shash); 3527d5178578SJohannes Thumshirn crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3528abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3529d5178578SJohannes Thumshirn crypto_shash_final(shash, sb->csum); 3530a512bbf8SYan Zheng 3531abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 35329f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3533a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3534634554dcSJosef Bacik if (!bh) { 3535fb456252SJeff Mahoney btrfs_err(device->fs_info, 3536f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3537f14d104dSDavid Sterba bytenr); 3538634554dcSJosef Bacik errors++; 3539634554dcSJosef Bacik continue; 3540634554dcSJosef Bacik } 3541634554dcSJosef Bacik 3542a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3543a512bbf8SYan Zheng 35444eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3545a512bbf8SYan Zheng get_bh(bh); 35464eedeb75SHisashi Hifumi 35474eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3548a512bbf8SYan Zheng lock_buffer(bh); 3549a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3550442a4f63SStefan Behrens bh->b_private = device; 3551a512bbf8SYan Zheng 3552387125fcSChris Mason /* 3553387125fcSChris Mason * we fua the first super. The others we allow 3554387125fcSChris Mason * to go down lazy. 3555387125fcSChris Mason */ 35561b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 35571b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 35581b9e619cSOmar Sandoval op_flags |= REQ_FUA; 35591b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 35604eedeb75SHisashi Hifumi if (ret) 3561a512bbf8SYan Zheng errors++; 3562a512bbf8SYan Zheng } 3563a512bbf8SYan Zheng return errors < i ? 0 : -1; 3564a512bbf8SYan Zheng } 3565a512bbf8SYan Zheng 3566387125fcSChris Mason /* 3567abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3568abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3569abbb3b8eSDavid Sterba * 3570abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3571abbb3b8eSDavid Sterba * date. 3572abbb3b8eSDavid Sterba */ 3573abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3574abbb3b8eSDavid Sterba { 3575abbb3b8eSDavid Sterba struct buffer_head *bh; 3576abbb3b8eSDavid Sterba int i; 3577abbb3b8eSDavid Sterba int errors = 0; 3578b6a535faSHoward McLauchlan bool primary_failed = false; 3579abbb3b8eSDavid Sterba u64 bytenr; 3580abbb3b8eSDavid Sterba 3581abbb3b8eSDavid Sterba if (max_mirrors == 0) 3582abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3583abbb3b8eSDavid Sterba 3584abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3585abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3586abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3587abbb3b8eSDavid Sterba device->commit_total_bytes) 3588abbb3b8eSDavid Sterba break; 3589abbb3b8eSDavid Sterba 35909f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 35919f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3592abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3593abbb3b8eSDavid Sterba if (!bh) { 3594abbb3b8eSDavid Sterba errors++; 3595b6a535faSHoward McLauchlan if (i == 0) 3596b6a535faSHoward McLauchlan primary_failed = true; 3597abbb3b8eSDavid Sterba continue; 3598abbb3b8eSDavid Sterba } 3599abbb3b8eSDavid Sterba wait_on_buffer(bh); 3600b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3601abbb3b8eSDavid Sterba errors++; 3602b6a535faSHoward McLauchlan if (i == 0) 3603b6a535faSHoward McLauchlan primary_failed = true; 3604b6a535faSHoward McLauchlan } 3605abbb3b8eSDavid Sterba 3606abbb3b8eSDavid Sterba /* drop our reference */ 3607abbb3b8eSDavid Sterba brelse(bh); 3608abbb3b8eSDavid Sterba 3609abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3610abbb3b8eSDavid Sterba brelse(bh); 3611abbb3b8eSDavid Sterba } 3612abbb3b8eSDavid Sterba 3613b6a535faSHoward McLauchlan /* log error, force error return */ 3614b6a535faSHoward McLauchlan if (primary_failed) { 3615b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3616b6a535faSHoward McLauchlan device->devid); 3617b6a535faSHoward McLauchlan return -1; 3618b6a535faSHoward McLauchlan } 3619b6a535faSHoward McLauchlan 3620abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3621abbb3b8eSDavid Sterba } 3622abbb3b8eSDavid Sterba 3623abbb3b8eSDavid Sterba /* 3624387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3625387125fcSChris Mason * for the barrier when it is done 3626387125fcSChris Mason */ 36274246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3628387125fcSChris Mason { 3629387125fcSChris Mason complete(bio->bi_private); 3630387125fcSChris Mason } 3631387125fcSChris Mason 3632387125fcSChris Mason /* 36334fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36344fc6441aSAnand Jain * done in the waiting counterpart. 3635387125fcSChris Mason */ 36364fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3637387125fcSChris Mason { 3638c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3639e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3640387125fcSChris Mason 3641c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36424fc6441aSAnand Jain return; 3643387125fcSChris Mason 3644e0ae9994SDavid Sterba bio_reset(bio); 3645387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 364674d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36478d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3648387125fcSChris Mason init_completion(&device->flush_wait); 3649387125fcSChris Mason bio->bi_private = &device->flush_wait; 3650387125fcSChris Mason 365143a01111SLu Fengqi btrfsic_submit_bio(bio); 36521c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36534fc6441aSAnand Jain } 3654387125fcSChris Mason 36554fc6441aSAnand Jain /* 36564fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36574fc6441aSAnand Jain */ 36588c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36594fc6441aSAnand Jain { 36604fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36614fc6441aSAnand Jain 36621c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 366358efbc9fSOmar Sandoval return BLK_STS_OK; 36644fc6441aSAnand Jain 36651c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36662980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 36674fc6441aSAnand Jain 36688c27cb35SLinus Torvalds return bio->bi_status; 3669387125fcSChris Mason } 3670387125fcSChris Mason 3671d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3672401b41e5SAnand Jain { 36736528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3674401b41e5SAnand Jain return -EIO; 3675387125fcSChris Mason return 0; 3676387125fcSChris Mason } 3677387125fcSChris Mason 3678387125fcSChris Mason /* 3679387125fcSChris Mason * send an empty flush down to each device in parallel, 3680387125fcSChris Mason * then wait for them 3681387125fcSChris Mason */ 3682387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3683387125fcSChris Mason { 3684387125fcSChris Mason struct list_head *head; 3685387125fcSChris Mason struct btrfs_device *dev; 36865af3e8ccSStefan Behrens int errors_wait = 0; 36874e4cbee9SChristoph Hellwig blk_status_t ret; 3688387125fcSChris Mason 36891538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3690387125fcSChris Mason /* send down all the barriers */ 3691387125fcSChris Mason head = &info->fs_devices->devices; 36921538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3693e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3694f88ba6a2SHidetoshi Seto continue; 3695cea7c8bfSAnand Jain if (!dev->bdev) 3696387125fcSChris Mason continue; 3697e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3698ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3699387125fcSChris Mason continue; 3700387125fcSChris Mason 37014fc6441aSAnand Jain write_dev_flush(dev); 370258efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3703387125fcSChris Mason } 3704387125fcSChris Mason 3705387125fcSChris Mason /* wait for all the barriers */ 37061538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3707e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3708f88ba6a2SHidetoshi Seto continue; 3709387125fcSChris Mason if (!dev->bdev) { 37105af3e8ccSStefan Behrens errors_wait++; 3711387125fcSChris Mason continue; 3712387125fcSChris Mason } 3713e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3714ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3715387125fcSChris Mason continue; 3716387125fcSChris Mason 37174fc6441aSAnand Jain ret = wait_dev_flush(dev); 3718401b41e5SAnand Jain if (ret) { 3719401b41e5SAnand Jain dev->last_flush_error = ret; 372066b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 372166b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 37225af3e8ccSStefan Behrens errors_wait++; 3723387125fcSChris Mason } 3724401b41e5SAnand Jain } 3725401b41e5SAnand Jain 3726cea7c8bfSAnand Jain if (errors_wait) { 3727401b41e5SAnand Jain /* 3728401b41e5SAnand Jain * At some point we need the status of all disks 3729401b41e5SAnand Jain * to arrive at the volume status. So error checking 3730401b41e5SAnand Jain * is being pushed to a separate loop. 3731401b41e5SAnand Jain */ 3732d10b82feSQu Wenruo return check_barrier_error(info); 3733401b41e5SAnand Jain } 3734387125fcSChris Mason return 0; 3735387125fcSChris Mason } 3736387125fcSChris Mason 3737943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3738943c6e99SZhao Lei { 37398789f4feSZhao Lei int raid_type; 37408789f4feSZhao Lei int min_tolerated = INT_MAX; 3741943c6e99SZhao Lei 37428789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37438789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37448c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37458789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37468789f4feSZhao Lei tolerated_failures); 3747943c6e99SZhao Lei 37488789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37498789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37508789f4feSZhao Lei continue; 375141a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37528789f4feSZhao Lei continue; 37538c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37548789f4feSZhao Lei btrfs_raid_array[raid_type]. 37558789f4feSZhao Lei tolerated_failures); 37568789f4feSZhao Lei } 3757943c6e99SZhao Lei 37588789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3759ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37608789f4feSZhao Lei min_tolerated = 0; 37618789f4feSZhao Lei } 37628789f4feSZhao Lei 37638789f4feSZhao Lei return min_tolerated; 3764943c6e99SZhao Lei } 3765943c6e99SZhao Lei 3766eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3767f2984462SChris Mason { 3768e5e9a520SChris Mason struct list_head *head; 3769f2984462SChris Mason struct btrfs_device *dev; 3770a061fc8dSChris Mason struct btrfs_super_block *sb; 3771f2984462SChris Mason struct btrfs_dev_item *dev_item; 3772f2984462SChris Mason int ret; 3773f2984462SChris Mason int do_barriers; 3774a236aed1SChris Mason int max_errors; 3775a236aed1SChris Mason int total_errors = 0; 3776a061fc8dSChris Mason u64 flags; 3777f2984462SChris Mason 37780b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3779fed3b381SLiu Bo 3780fed3b381SLiu Bo /* 3781fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3782fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3783fed3b381SLiu Bo * been consistent on disk. 3784fed3b381SLiu Bo */ 3785fed3b381SLiu Bo if (max_mirrors == 0) 37860b246afaSJeff Mahoney backup_super_roots(fs_info); 3787f2984462SChris Mason 37880b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3789a061fc8dSChris Mason dev_item = &sb->dev_item; 3790e5e9a520SChris Mason 37910b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37920b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37930b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3794387125fcSChris Mason 37955af3e8ccSStefan Behrens if (do_barriers) { 37960b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37975af3e8ccSStefan Behrens if (ret) { 37985af3e8ccSStefan Behrens mutex_unlock( 37990b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38000b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38015af3e8ccSStefan Behrens "errors while submitting device barriers."); 38025af3e8ccSStefan Behrens return ret; 38035af3e8ccSStefan Behrens } 38045af3e8ccSStefan Behrens } 3805387125fcSChris Mason 38061538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3807dfe25020SChris Mason if (!dev->bdev) { 3808dfe25020SChris Mason total_errors++; 3809dfe25020SChris Mason continue; 3810dfe25020SChris Mason } 3811e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3812ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3813dfe25020SChris Mason continue; 3814dfe25020SChris Mason 38152b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3816a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3817a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 38187df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3819935e5cc9SMiao Xie dev->commit_total_bytes); 3820ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3821ce7213c7SMiao Xie dev->commit_bytes_used); 3822a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3823a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3824a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3825a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 38267239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38277239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3828a512bbf8SYan Zheng 3829a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3830a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3831f2984462SChris Mason 383275cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 383375cb857dSQu Wenruo if (ret < 0) { 383475cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 383575cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 383675cb857dSQu Wenruo "unexpected superblock corruption detected"); 383775cb857dSQu Wenruo return -EUCLEAN; 383875cb857dSQu Wenruo } 383975cb857dSQu Wenruo 3840abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3841a236aed1SChris Mason if (ret) 3842a236aed1SChris Mason total_errors++; 3843f2984462SChris Mason } 3844a236aed1SChris Mason if (total_errors > max_errors) { 38450b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3846d397712bSChris Mason total_errors); 38470b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 384879787eaaSJeff Mahoney 38499d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38500b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38510b246afaSJeff Mahoney "%d errors while writing supers", 38520b246afaSJeff Mahoney total_errors); 38539d565ba4SStefan Behrens return -EIO; 3854a236aed1SChris Mason } 3855f2984462SChris Mason 3856a512bbf8SYan Zheng total_errors = 0; 38571538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3858dfe25020SChris Mason if (!dev->bdev) 3859dfe25020SChris Mason continue; 3860e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3861ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3862dfe25020SChris Mason continue; 3863dfe25020SChris Mason 3864abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3865a512bbf8SYan Zheng if (ret) 38661259ab75SChris Mason total_errors++; 3867f2984462SChris Mason } 38680b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3869a236aed1SChris Mason if (total_errors > max_errors) { 38700b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38710b246afaSJeff Mahoney "%d errors while writing supers", 38720b246afaSJeff Mahoney total_errors); 387379787eaaSJeff Mahoney return -EIO; 3874a236aed1SChris Mason } 3875f2984462SChris Mason return 0; 3876f2984462SChris Mason } 3877f2984462SChris Mason 3878cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3879cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3880cb517eabSMiao Xie struct btrfs_root *root) 38812619ba1fSChris Mason { 38824df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 38832619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 38842619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3885af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 388600246528SJosef Bacik btrfs_put_root(root); 38874df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 388876dda93cSYan, Zheng 388976dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 389076dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 389176dda93cSYan, Zheng 38921c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 38933321719eSLiu Bo btrfs_free_log(NULL, root); 38941c1ea4f7SLiu Bo if (root->reloc_root) { 38951c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38961c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 389700246528SJosef Bacik btrfs_put_root(root->reloc_root); 38981c1ea4f7SLiu Bo root->reloc_root = NULL; 38991c1ea4f7SLiu Bo } 39001c1ea4f7SLiu Bo } 39013321719eSLiu Bo 3902faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3903581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3904faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3905581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 390684db5ccfSDavid Sterba btrfs_free_fs_root(root); 39074df27c4dSYan, Zheng } 39084df27c4dSYan, Zheng 390984db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 39104df27c4dSYan, Zheng { 391157cdc8dbSDavid Sterba iput(root->ino_cache_inode); 39124df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 39130ee5dc67SAl Viro if (root->anon_dev) 39140ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 39158257b2dcSMiao Xie if (root->subv_writers) 39168257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 39175f39d397SChris Mason free_extent_buffer(root->node); 39185f39d397SChris Mason free_extent_buffer(root->commit_root); 3919581bb050SLi Zefan kfree(root->free_ino_ctl); 3920581bb050SLi Zefan kfree(root->free_ino_pinned); 392100246528SJosef Bacik btrfs_put_root(root); 39222619ba1fSChris Mason } 39232619ba1fSChris Mason 3924c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3925c146afadSYan Zheng { 3926c146afadSYan Zheng u64 root_objectid = 0; 3927c146afadSYan Zheng struct btrfs_root *gang[8]; 392865d33fd7SQu Wenruo int i = 0; 392965d33fd7SQu Wenruo int err = 0; 393065d33fd7SQu Wenruo unsigned int ret = 0; 393165d33fd7SQu Wenruo int index; 3932c146afadSYan Zheng 3933c146afadSYan Zheng while (1) { 393465d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3935c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3936c146afadSYan Zheng (void **)gang, root_objectid, 3937c146afadSYan Zheng ARRAY_SIZE(gang)); 393865d33fd7SQu Wenruo if (!ret) { 393965d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3940c146afadSYan Zheng break; 394165d33fd7SQu Wenruo } 39425d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 394366b4ffd1SJosef Bacik 394465d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 394565d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 394665d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 394765d33fd7SQu Wenruo gang[i] = NULL; 394865d33fd7SQu Wenruo continue; 394965d33fd7SQu Wenruo } 395065d33fd7SQu Wenruo /* grab all the search result for later use */ 395100246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 395265d33fd7SQu Wenruo } 395365d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 395465d33fd7SQu Wenruo 395565d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 395665d33fd7SQu Wenruo if (!gang[i]) 395765d33fd7SQu Wenruo continue; 3958c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 395966b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 396066b4ffd1SJosef Bacik if (err) 396165d33fd7SQu Wenruo break; 396200246528SJosef Bacik btrfs_put_root(gang[i]); 3963c146afadSYan Zheng } 3964c146afadSYan Zheng root_objectid++; 3965c146afadSYan Zheng } 396665d33fd7SQu Wenruo 396765d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 396865d33fd7SQu Wenruo for (; i < ret; i++) { 396965d33fd7SQu Wenruo if (gang[i]) 397000246528SJosef Bacik btrfs_put_root(gang[i]); 397165d33fd7SQu Wenruo } 397265d33fd7SQu Wenruo return err; 3973c146afadSYan Zheng } 3974c146afadSYan Zheng 39756bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3976c146afadSYan Zheng { 39776bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3978c146afadSYan Zheng struct btrfs_trans_handle *trans; 3979c146afadSYan Zheng 39800b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39812ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39820b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39830b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3984c71bf099SYan, Zheng 3985c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39860b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39870b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3988c71bf099SYan, Zheng 39897a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39903612b495STsutomu Itoh if (IS_ERR(trans)) 39913612b495STsutomu Itoh return PTR_ERR(trans); 39923a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3993c146afadSYan Zheng } 3994c146afadSYan Zheng 3995b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 3996eb60ceacSChris Mason { 3997c146afadSYan Zheng int ret; 3998e089f05cSChris Mason 3999afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4000d6fd0ae2SOmar Sandoval /* 4001d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4002d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4003d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4004d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4005d6fd0ae2SOmar Sandoval */ 4006d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4007a2135011SChris Mason 40087343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4009d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40107343dd61SJustin Maggard 4011803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4012803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4013803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4014803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4015803b2f54SStefan Behrens 4016837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4017aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4018837d5b6eSIlya Dryomov 40198dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40208dabb742SStefan Behrens 4021aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 40224cb5300bSChris Mason 40234cb5300bSChris Mason /* wait for any defraggers to finish */ 40244cb5300bSChris Mason wait_event(fs_info->transaction_wait, 40254cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 40264cb5300bSChris Mason 40274cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 402826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40294cb5300bSChris Mason 403021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 403121c7e756SMiao Xie 4032b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4033b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4034b0643e59SDennis Zhou 4035bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4036e44163e1SJeff Mahoney /* 4037d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4038d6fd0ae2SOmar Sandoval * unused block groups. 4039e44163e1SJeff Mahoney */ 40400b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4041e44163e1SJeff Mahoney 40426bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4043d397712bSChris Mason if (ret) 404404892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4045c146afadSYan Zheng } 4046ed2ff2cbSChris Mason 4047af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4048af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40492ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4050acce952bSliubo 4051e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4052e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40538929ecfaSYan, Zheng 4054e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4055afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4056f25784b3SYan Zheng 405704892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4058fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4059bcef60f2SArne Jansen 4060963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 406104892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4062963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4063b0c68f8bSChris Mason } 406431153d81SYan Zheng 40654297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 40664297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 40674297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 40684297ff84SJosef Bacik 40696618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4070b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 40715ac1d209SJeff Mahoney 4072faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4073d10c5f31SChris Mason 40741a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 40751a4319ccSLiu Bo 4076de348ee0SWang Shilong /* 4077de348ee0SWang Shilong * we must make sure there is not any read request to 4078de348ee0SWang Shilong * submit after we stopping all workers. 4079de348ee0SWang Shilong */ 4080de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 408196192499SJosef Bacik btrfs_stop_all_workers(fs_info); 408296192499SJosef Bacik 4083afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 40844273eaffSAnand Jain free_root_pointers(fs_info, true); 40859ad6b7bcSChris Mason 40864e19443dSJosef Bacik /* 40874e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 40884e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 40894e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 40904e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 40914e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 40924e19443dSJosef Bacik */ 40934e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 40944e19443dSJosef Bacik 409513e6c37bSJosef Bacik iput(fs_info->btree_inode); 4096d6bfde87SChris Mason 409721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40980b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 40992ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 410021adbd5cSStefan Behrens #endif 410121adbd5cSStefan Behrens 41020b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 410368c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 410476dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 4105eb60ceacSChris Mason } 4106eb60ceacSChris Mason 4107b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4108b9fab919SChris Mason int atomic) 4109ccd467d6SChris Mason { 41101259ab75SChris Mason int ret; 4111727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 41121259ab75SChris Mason 41130b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 41141259ab75SChris Mason if (!ret) 41151259ab75SChris Mason return ret; 41161259ab75SChris Mason 41171259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4118b9fab919SChris Mason parent_transid, atomic); 4119b9fab919SChris Mason if (ret == -EAGAIN) 4120b9fab919SChris Mason return ret; 41211259ab75SChris Mason return !ret; 41225f39d397SChris Mason } 41236702ed49SChris Mason 41245f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 41255f39d397SChris Mason { 41260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 412706ea65a3SJosef Bacik struct btrfs_root *root; 41285f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4129b9473439SChris Mason int was_dirty; 4130b4ce94deSChris Mason 413106ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 413206ea65a3SJosef Bacik /* 413306ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 413452042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 413506ea65a3SJosef Bacik * outside of the sanity tests. 413606ea65a3SJosef Bacik */ 4137b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 413806ea65a3SJosef Bacik return; 413906ea65a3SJosef Bacik #endif 414006ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41410b246afaSJeff Mahoney fs_info = root->fs_info; 4142b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41430b246afaSJeff Mahoney if (transid != fs_info->generation) 41445d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41450b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41460b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4147e2d84521SMiao Xie if (!was_dirty) 4148104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4149e2d84521SMiao Xie buf->len, 41500b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41511f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 415269fc6cbbSQu Wenruo /* 415369fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 415469fc6cbbSQu Wenruo * but item data not updated. 415569fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 415669fc6cbbSQu Wenruo */ 415769fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4158cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4159a4f78750SDavid Sterba btrfs_print_leaf(buf); 41601f21ef0aSFilipe Manana ASSERT(0); 41611f21ef0aSFilipe Manana } 41621f21ef0aSFilipe Manana #endif 4163eb60ceacSChris Mason } 4164eb60ceacSChris Mason 41652ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4166b53d3f5dSLiu Bo int flush_delayed) 416735b7e476SChris Mason { 4168188de649SChris Mason /* 4169188de649SChris Mason * looks as though older kernels can get into trouble with 4170188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4171188de649SChris Mason */ 4172e2d84521SMiao Xie int ret; 4173d6bfde87SChris Mason 41746933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4175d6bfde87SChris Mason return; 4176d6bfde87SChris Mason 4177b53d3f5dSLiu Bo if (flush_delayed) 41782ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 417916cdcec7SMiao Xie 4180d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4181d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4182d814a491SEthan Lien fs_info->dirty_metadata_batch); 4183e2d84521SMiao Xie if (ret > 0) { 41840b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 418516cdcec7SMiao Xie } 418616cdcec7SMiao Xie } 418716cdcec7SMiao Xie 41882ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 418916cdcec7SMiao Xie { 41902ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 419135b7e476SChris Mason } 4192b53d3f5dSLiu Bo 41932ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4194b53d3f5dSLiu Bo { 41952ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4196d6bfde87SChris Mason } 41976b80053dSChris Mason 4198581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4199581c1760SQu Wenruo struct btrfs_key *first_key) 42006b80053dSChris Mason { 42015ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4202581c1760SQu Wenruo level, first_key); 42036b80053dSChris Mason } 42040da5468fSChris Mason 42052ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4206acce952bSliubo { 4207fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4208fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4209fe816d0fSNikolay Borisov 42100b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42112ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42120b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4213acce952bSliubo 42140b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42150b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4216acce952bSliubo } 4217acce952bSliubo 4218143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4219acce952bSliubo { 4220acce952bSliubo struct btrfs_ordered_extent *ordered; 4221acce952bSliubo 4222199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4223779880efSJosef Bacik /* 4224779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4225779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4226779880efSJosef Bacik */ 4227199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4228779880efSJosef Bacik root_extent_list) 4229779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4230199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4231199c2a9cSMiao Xie } 4232199c2a9cSMiao Xie 4233199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4234199c2a9cSMiao Xie { 4235199c2a9cSMiao Xie struct btrfs_root *root; 4236199c2a9cSMiao Xie struct list_head splice; 4237199c2a9cSMiao Xie 4238199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4239199c2a9cSMiao Xie 4240199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4241199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4242199c2a9cSMiao Xie while (!list_empty(&splice)) { 4243199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4244199c2a9cSMiao Xie ordered_root); 42451de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42461de2cfdeSJosef Bacik &fs_info->ordered_roots); 4247199c2a9cSMiao Xie 42482a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4249199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4250199c2a9cSMiao Xie 42512a85d9caSLiu Bo cond_resched(); 42522a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4253199c2a9cSMiao Xie } 4254199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 425574d5d229SJosef Bacik 425674d5d229SJosef Bacik /* 425774d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 425874d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 425974d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 426074d5d229SJosef Bacik * actually get run and error out properly. 426174d5d229SJosef Bacik */ 426274d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4263acce952bSliubo } 4264acce952bSliubo 426535a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 42662ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4267acce952bSliubo { 4268acce952bSliubo struct rb_node *node; 4269acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4270acce952bSliubo struct btrfs_delayed_ref_node *ref; 4271acce952bSliubo int ret = 0; 4272acce952bSliubo 4273acce952bSliubo delayed_refs = &trans->delayed_refs; 4274acce952bSliubo 4275acce952bSliubo spin_lock(&delayed_refs->lock); 4276d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4277cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 42780b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4279acce952bSliubo return ret; 4280acce952bSliubo } 4281acce952bSliubo 42825c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4283d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 42840e0adbcfSJosef Bacik struct rb_node *n; 4285e78417d1SJosef Bacik bool pin_bytes = false; 4286acce952bSliubo 4287d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4288d7df2c79SJosef Bacik href_node); 42893069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4290b939d1abSJosef Bacik continue; 42913069bd26SJosef Bacik 4292d7df2c79SJosef Bacik spin_lock(&head->lock); 4293e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 42940e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 42950e0adbcfSJosef Bacik ref_node); 4296d7df2c79SJosef Bacik ref->in_tree = 0; 4297e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 42980e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 42991d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43001d57ee94SWang Xiaoguang list_del(&ref->add_list); 4301d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4302d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4303d7df2c79SJosef Bacik } 430454067ae9SJosef Bacik if (head->must_insert_reserved) 4305e78417d1SJosef Bacik pin_bytes = true; 430678a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4307fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4308d7df2c79SJosef Bacik spin_unlock(&head->lock); 4309acce952bSliubo spin_unlock(&delayed_refs->lock); 4310e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4311acce952bSliubo 4312f603bb94SNikolay Borisov if (pin_bytes) { 4313f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4314f603bb94SNikolay Borisov 4315f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4316f603bb94SNikolay Borisov BUG_ON(!cache); 4317f603bb94SNikolay Borisov 4318f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4319f603bb94SNikolay Borisov spin_lock(&cache->lock); 4320f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4321f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4322f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4323f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4324f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4325f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4326f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4327f603bb94SNikolay Borisov percpu_counter_add_batch( 4328f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4329f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4330f603bb94SNikolay Borisov 4331f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4332f603bb94SNikolay Borisov 4333f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4334f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4335f603bb94SNikolay Borisov } 433631890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4337d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4338acce952bSliubo cond_resched(); 4339acce952bSliubo spin_lock(&delayed_refs->lock); 4340acce952bSliubo } 434181f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4342acce952bSliubo 4343acce952bSliubo spin_unlock(&delayed_refs->lock); 4344acce952bSliubo 4345acce952bSliubo return ret; 4346acce952bSliubo } 4347acce952bSliubo 4348143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4349acce952bSliubo { 4350acce952bSliubo struct btrfs_inode *btrfs_inode; 4351acce952bSliubo struct list_head splice; 4352acce952bSliubo 4353acce952bSliubo INIT_LIST_HEAD(&splice); 4354acce952bSliubo 4355eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4356eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4357acce952bSliubo 4358acce952bSliubo while (!list_empty(&splice)) { 4359fe816d0fSNikolay Borisov struct inode *inode = NULL; 4360eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4361acce952bSliubo delalloc_inodes); 4362fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4363eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4364acce952bSliubo 4365fe816d0fSNikolay Borisov /* 4366fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4367fe816d0fSNikolay Borisov * meanwhile. 4368fe816d0fSNikolay Borisov */ 4369fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4370fe816d0fSNikolay Borisov if (inode) { 4371fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4372fe816d0fSNikolay Borisov iput(inode); 4373fe816d0fSNikolay Borisov } 4374eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4375acce952bSliubo } 4376eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4377eb73c1b7SMiao Xie } 4378eb73c1b7SMiao Xie 4379eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4380eb73c1b7SMiao Xie { 4381eb73c1b7SMiao Xie struct btrfs_root *root; 4382eb73c1b7SMiao Xie struct list_head splice; 4383eb73c1b7SMiao Xie 4384eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4385eb73c1b7SMiao Xie 4386eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4387eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4388eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4389eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4390eb73c1b7SMiao Xie delalloc_root); 439100246528SJosef Bacik root = btrfs_grab_root(root); 4392eb73c1b7SMiao Xie BUG_ON(!root); 4393eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4394eb73c1b7SMiao Xie 4395eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 439600246528SJosef Bacik btrfs_put_root(root); 4397eb73c1b7SMiao Xie 4398eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4399eb73c1b7SMiao Xie } 4400eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4401acce952bSliubo } 4402acce952bSliubo 44032ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4404acce952bSliubo struct extent_io_tree *dirty_pages, 4405acce952bSliubo int mark) 4406acce952bSliubo { 4407acce952bSliubo int ret; 4408acce952bSliubo struct extent_buffer *eb; 4409acce952bSliubo u64 start = 0; 4410acce952bSliubo u64 end; 4411acce952bSliubo 4412acce952bSliubo while (1) { 4413acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4414e6138876SJosef Bacik mark, NULL); 4415acce952bSliubo if (ret) 4416acce952bSliubo break; 4417acce952bSliubo 441891166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4419acce952bSliubo while (start <= end) { 44200b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44210b246afaSJeff Mahoney start += fs_info->nodesize; 4422fd8b2b61SJosef Bacik if (!eb) 4423acce952bSliubo continue; 4424fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4425acce952bSliubo 4426fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4427fd8b2b61SJosef Bacik &eb->bflags)) 4428fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4429fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4430acce952bSliubo } 4431acce952bSliubo } 4432acce952bSliubo 4433acce952bSliubo return ret; 4434acce952bSliubo } 4435acce952bSliubo 44362ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4437acce952bSliubo struct extent_io_tree *pinned_extents) 4438acce952bSliubo { 4439acce952bSliubo struct extent_io_tree *unpin; 4440acce952bSliubo u64 start; 4441acce952bSliubo u64 end; 4442acce952bSliubo int ret; 4443ed0eaa14SLiu Bo bool loop = true; 4444acce952bSliubo 4445acce952bSliubo unpin = pinned_extents; 4446ed0eaa14SLiu Bo again: 4447acce952bSliubo while (1) { 44480e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 44490e6ec385SFilipe Manana 4450fcd5e742SLu Fengqi /* 4451fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4452fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4453fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4454fcd5e742SLu Fengqi * the same extent range. 4455fcd5e742SLu Fengqi */ 4456fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4457acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44580e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4459fcd5e742SLu Fengqi if (ret) { 4460fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4461acce952bSliubo break; 4462fcd5e742SLu Fengqi } 4463acce952bSliubo 44640e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44650e6ec385SFilipe Manana free_extent_state(cached_state); 44662ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4467fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4468acce952bSliubo cond_resched(); 4469acce952bSliubo } 4470acce952bSliubo 4471ed0eaa14SLiu Bo if (loop) { 44720b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44730b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4474ed0eaa14SLiu Bo else 44750b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4476ed0eaa14SLiu Bo loop = false; 4477ed0eaa14SLiu Bo goto again; 4478ed0eaa14SLiu Bo } 4479ed0eaa14SLiu Bo 4480acce952bSliubo return 0; 4481acce952bSliubo } 4482acce952bSliubo 448332da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4484c79a1751SLiu Bo { 4485c79a1751SLiu Bo struct inode *inode; 4486c79a1751SLiu Bo 4487c79a1751SLiu Bo inode = cache->io_ctl.inode; 4488c79a1751SLiu Bo if (inode) { 4489c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4490c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4491c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4492c79a1751SLiu Bo iput(inode); 4493c79a1751SLiu Bo } 4494c79a1751SLiu Bo btrfs_put_block_group(cache); 4495c79a1751SLiu Bo } 4496c79a1751SLiu Bo 4497c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 44982ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4499c79a1751SLiu Bo { 450032da5386SDavid Sterba struct btrfs_block_group *cache; 4501c79a1751SLiu Bo 4502c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4503c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4504c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 450532da5386SDavid Sterba struct btrfs_block_group, 4506c79a1751SLiu Bo dirty_list); 4507c79a1751SLiu Bo 4508c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4509c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4510c79a1751SLiu Bo list_del_init(&cache->io_list); 4511c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4512c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4513c79a1751SLiu Bo } 4514c79a1751SLiu Bo 4515c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4516c79a1751SLiu Bo spin_lock(&cache->lock); 4517c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4518c79a1751SLiu Bo spin_unlock(&cache->lock); 4519c79a1751SLiu Bo 4520c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4521c79a1751SLiu Bo btrfs_put_block_group(cache); 4522ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4523c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4524c79a1751SLiu Bo } 4525c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4526c79a1751SLiu Bo 452745ae2c18SNikolay Borisov /* 452845ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 452945ae2c18SNikolay Borisov * to use it without any locking 453045ae2c18SNikolay Borisov */ 4531c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4532c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 453332da5386SDavid Sterba struct btrfs_block_group, 4534c79a1751SLiu Bo io_list); 4535c79a1751SLiu Bo 4536c79a1751SLiu Bo list_del_init(&cache->io_list); 4537c79a1751SLiu Bo spin_lock(&cache->lock); 4538c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4539c79a1751SLiu Bo spin_unlock(&cache->lock); 4540c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4541c79a1751SLiu Bo } 4542c79a1751SLiu Bo } 4543c79a1751SLiu Bo 454449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45452ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 454649b25e05SJeff Mahoney { 4547bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4548bbbf7243SNikolay Borisov 45492ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4550c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4551c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4552c79a1751SLiu Bo 4553bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4554bbbf7243SNikolay Borisov post_commit_list) { 4555bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4556bbbf7243SNikolay Borisov } 4557bbbf7243SNikolay Borisov 45582ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 455949b25e05SJeff Mahoney 45604a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45610b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 456249b25e05SJeff Mahoney 45634a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45640b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 456549b25e05SJeff Mahoney 4566ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 456767cde344SMiao Xie 45682ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 456949b25e05SJeff Mahoney EXTENT_DIRTY); 45702ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45710b246afaSJeff Mahoney fs_info->pinned_extents); 457249b25e05SJeff Mahoney 45734a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45744a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 457549b25e05SJeff Mahoney } 457649b25e05SJeff Mahoney 45772ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4578acce952bSliubo { 4579acce952bSliubo struct btrfs_transaction *t; 4580acce952bSliubo 45810b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4582acce952bSliubo 45830b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45840b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 45850b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4586724e2315SJosef Bacik struct btrfs_transaction, list); 4587724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 45889b64f57dSElena Reshetova refcount_inc(&t->use_count); 45890b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45902ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4591724e2315SJosef Bacik btrfs_put_transaction(t); 45920b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4593724e2315SJosef Bacik continue; 4594724e2315SJosef Bacik } 45950b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4596724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 45970b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4598724e2315SJosef Bacik /* 4599724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4600724e2315SJosef Bacik * handle open currently for this transaction. 4601724e2315SJosef Bacik */ 4602724e2315SJosef Bacik wait_event(t->writer_wait, 4603724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4604724e2315SJosef Bacik } else { 46050b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4606724e2315SJosef Bacik } 46072ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4608724e2315SJosef Bacik 46090b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46100b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46110b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4612724e2315SJosef Bacik list_del_init(&t->list); 46130b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4614a4abeea4SJosef Bacik 4615724e2315SJosef Bacik btrfs_put_transaction(t); 46162ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46170b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4618724e2315SJosef Bacik } 46190b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46200b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4621ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4622ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46232ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 46240b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46250b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4626acce952bSliubo 4627acce952bSliubo return 0; 4628acce952bSliubo } 4629acce952bSliubo 4630e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 46314d53dddbSDavid Sterba /* mandatory callbacks */ 46320b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 46334d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 46340da5468fSChris Mason }; 4635