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" 44eb60ceacSChris Mason 45319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 46319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 47319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 48319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 49e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 50e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 51319e4d06SQu Wenruo 52e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 538b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 54143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 55acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 562ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 57143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 582ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 59acce952bSliubo struct extent_io_tree *dirty_pages, 60acce952bSliubo int mark); 612ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 62acce952bSliubo struct extent_io_tree *pinned_extents); 632ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 642ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 65ce9adaa5SChris Mason 66d352ac68SChris Mason /* 6797eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6897eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 69d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 70d352ac68SChris Mason */ 7197eb6b69SDavid Sterba struct btrfs_end_io_wq { 72ce9adaa5SChris Mason struct bio *bio; 73ce9adaa5SChris Mason bio_end_io_t *end_io; 74ce9adaa5SChris Mason void *private; 75ce9adaa5SChris Mason struct btrfs_fs_info *info; 764e4cbee9SChristoph Hellwig blk_status_t status; 77bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 788b712842SChris Mason struct btrfs_work work; 79ce9adaa5SChris Mason }; 800da5468fSChris Mason 8197eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8297eb6b69SDavid Sterba 8397eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8497eb6b69SDavid Sterba { 8597eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8697eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8797eb6b69SDavid Sterba 0, 88fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 8997eb6b69SDavid Sterba NULL); 9097eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9197eb6b69SDavid Sterba return -ENOMEM; 9297eb6b69SDavid Sterba return 0; 9397eb6b69SDavid Sterba } 9497eb6b69SDavid Sterba 95e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9697eb6b69SDavid Sterba { 9797eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9897eb6b69SDavid Sterba } 9997eb6b69SDavid Sterba 100d352ac68SChris Mason /* 101d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 102d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 103d352ac68SChris Mason * just before they are sent down the IO stack. 104d352ac68SChris Mason */ 10544b8bd7eSChris Mason struct async_submit_bio { 106c6100a4bSJosef Bacik void *private_data; 10744b8bd7eSChris Mason struct bio *bio; 108a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 10944b8bd7eSChris Mason int mirror_num; 110eaf25d93SChris Mason /* 111eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 112eaf25d93SChris Mason * can't tell us where in the file the bio should go 113eaf25d93SChris Mason */ 114eaf25d93SChris Mason u64 bio_offset; 1158b712842SChris Mason struct btrfs_work work; 1164e4cbee9SChristoph Hellwig blk_status_t status; 11744b8bd7eSChris Mason }; 11844b8bd7eSChris Mason 11985d4e461SChris Mason /* 12085d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12185d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12285d4e461SChris Mason * the level the eb occupies in the tree. 1234008c04aSChris Mason * 12485d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12585d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 12685d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1274fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1284fd786e6SMisono Tomohiro * roots have separate keysets. 1294008c04aSChris Mason * 13085d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13185d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13285d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1334008c04aSChris Mason * 13485d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13585d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 13685d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 13785d4e461SChris Mason * 13885d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 13985d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14085d4e461SChris Mason * needs update as well. 1414008c04aSChris Mason */ 1424008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1434008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1444008c04aSChris Mason # error 1454008c04aSChris Mason # endif 14685d4e461SChris Mason 14785d4e461SChris Mason static struct btrfs_lockdep_keyset { 14885d4e461SChris Mason u64 id; /* root objectid */ 14985d4e461SChris Mason const char *name_stem; /* lock name stem */ 15085d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 15185d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 15285d4e461SChris Mason } btrfs_lockdep_keysets[] = { 15385d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 15485d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 15585d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 15685d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 15785d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 15885d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 15960b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 16085d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 16185d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 16285d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 16313fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1646b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 16585d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1664008c04aSChris Mason }; 16785d4e461SChris Mason 16885d4e461SChris Mason void __init btrfs_init_lockdep(void) 16985d4e461SChris Mason { 17085d4e461SChris Mason int i, j; 17185d4e461SChris Mason 17285d4e461SChris Mason /* initialize lockdep class names */ 17385d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 17485d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 17585d4e461SChris Mason 17685d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 17785d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 17885d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 17985d4e461SChris Mason } 18085d4e461SChris Mason } 18185d4e461SChris Mason 18285d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18385d4e461SChris Mason int level) 18485d4e461SChris Mason { 18585d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 18685d4e461SChris Mason 18785d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 18885d4e461SChris Mason 18985d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19085d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19185d4e461SChris Mason if (ks->id == objectid) 19285d4e461SChris Mason break; 19385d4e461SChris Mason 19485d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 19585d4e461SChris Mason &ks->keys[level], ks->names[level]); 19685d4e461SChris Mason } 19785d4e461SChris Mason 1984008c04aSChris Mason #endif 1994008c04aSChris Mason 200d352ac68SChris Mason /* 201d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 202d352ac68SChris Mason * that covers the entire device 203d352ac68SChris Mason */ 2046af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 205306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2065f39d397SChris Mason int create) 2075f39d397SChris Mason { 2083ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 209fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2105f39d397SChris Mason struct extent_map *em; 2115f39d397SChris Mason int ret; 2125f39d397SChris Mason 213890871beSChris Mason read_lock(&em_tree->lock); 214d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 215a061fc8dSChris Mason if (em) { 2160b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 217890871beSChris Mason read_unlock(&em_tree->lock); 2185f39d397SChris Mason goto out; 219a061fc8dSChris Mason } 220890871beSChris Mason read_unlock(&em_tree->lock); 2217b13b7b1SChris Mason 222172ddd60SDavid Sterba em = alloc_extent_map(); 2235f39d397SChris Mason if (!em) { 2245f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2255f39d397SChris Mason goto out; 2265f39d397SChris Mason } 2275f39d397SChris Mason em->start = 0; 2280afbaf8cSChris Mason em->len = (u64)-1; 229c8b97818SChris Mason em->block_len = (u64)-1; 2305f39d397SChris Mason em->block_start = 0; 2310b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 232d1310b2eSChris Mason 233890871beSChris Mason write_lock(&em_tree->lock); 23409a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2355f39d397SChris Mason if (ret == -EEXIST) { 2365f39d397SChris Mason free_extent_map(em); 2377b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 238b4f359abSTsutomu Itoh if (!em) 2390433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2405f39d397SChris Mason } else if (ret) { 2417b13b7b1SChris Mason free_extent_map(em); 2420433f20dSTsutomu Itoh em = ERR_PTR(ret); 2435f39d397SChris Mason } 244890871beSChris Mason write_unlock(&em_tree->lock); 2457b13b7b1SChris Mason 2465f39d397SChris Mason out: 2475f39d397SChris Mason return em; 2485f39d397SChris Mason } 2495f39d397SChris Mason 250d352ac68SChris Mason /* 2512996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 2522996e1f8SJohannes Thumshirn * 2532996e1f8SJohannes Thumshirn * Returns error if the extent buffer cannot be mapped. 254d352ac68SChris Mason */ 2552996e1f8SJohannes Thumshirn static int csum_tree_block(struct extent_buffer *buf, u8 *result) 25619c00ddcSChris Mason { 257d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 258d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 25919c00ddcSChris Mason unsigned long len; 26019c00ddcSChris Mason unsigned long cur_len; 26119c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26219c00ddcSChris Mason char *kaddr; 26319c00ddcSChris Mason unsigned long map_start; 26419c00ddcSChris Mason unsigned long map_len; 26519c00ddcSChris Mason int err; 266d5178578SJohannes Thumshirn 267d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 268d5178578SJohannes Thumshirn crypto_shash_init(shash); 26919c00ddcSChris Mason 27019c00ddcSChris Mason len = buf->len - offset; 271d5178578SJohannes Thumshirn 27219c00ddcSChris Mason while (len > 0) { 273d2e174d5SJohannes Thumshirn /* 274d2e174d5SJohannes Thumshirn * Note: we don't need to check for the err == 1 case here, as 275d2e174d5SJohannes Thumshirn * with the given combination of 'start = BTRFS_CSUM_SIZE (32)' 276d2e174d5SJohannes Thumshirn * and 'min_len = 32' and the currently implemented mapping 277d2e174d5SJohannes Thumshirn * algorithm we cannot cross a page boundary. 278d2e174d5SJohannes Thumshirn */ 27919c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 280a6591715SChris Mason &kaddr, &map_start, &map_len); 281c53839fcSJohannes Thumshirn if (WARN_ON(err)) 2828bd98f0eSAlex Lyakas return err; 28319c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 284d5178578SJohannes Thumshirn crypto_shash_update(shash, kaddr + offset - map_start, cur_len); 28519c00ddcSChris Mason len -= cur_len; 28619c00ddcSChris Mason offset += cur_len; 28719c00ddcSChris Mason } 28871a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 289607d432dSJosef Bacik 290d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 29119c00ddcSChris Mason 29219c00ddcSChris Mason return 0; 29319c00ddcSChris Mason } 29419c00ddcSChris Mason 295d352ac68SChris Mason /* 296d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 297d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 298d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 299d352ac68SChris Mason * in the wrong place. 300d352ac68SChris Mason */ 3011259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 302b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 303b9fab919SChris Mason int atomic) 3041259ab75SChris Mason { 3052ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3061259ab75SChris Mason int ret; 3072755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3081259ab75SChris Mason 3091259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3101259ab75SChris Mason return 0; 3111259ab75SChris Mason 312b9fab919SChris Mason if (atomic) 313b9fab919SChris Mason return -EAGAIN; 314b9fab919SChris Mason 315a26e8c9fSJosef Bacik if (need_lock) { 316a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 317300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 318a26e8c9fSJosef Bacik } 319a26e8c9fSJosef Bacik 3202ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 321ff13db41SDavid Sterba &cached_state); 3220b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3231259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3241259ab75SChris Mason ret = 0; 3251259ab75SChris Mason goto out; 3261259ab75SChris Mason } 32794647322SDavid Sterba btrfs_err_rl(eb->fs_info, 32894647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 32994647322SDavid Sterba eb->start, 33029549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3311259ab75SChris Mason ret = 1; 332a26e8c9fSJosef Bacik 333a26e8c9fSJosef Bacik /* 334a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 335a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 33601327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 337a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 338a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 339a26e8c9fSJosef Bacik * making everybody very sad. 340a26e8c9fSJosef Bacik */ 341a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3420b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 34333958dc6SChris Mason out: 3442ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 345e43bbe5eSDavid Sterba &cached_state); 346472b909fSJosef Bacik if (need_lock) 347a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3481259ab75SChris Mason return ret; 3491259ab75SChris Mason } 3501259ab75SChris Mason 351e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 352e7e16f48SJohannes Thumshirn { 353e7e16f48SJohannes Thumshirn switch (csum_type) { 354e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 355e7e16f48SJohannes Thumshirn return true; 356e7e16f48SJohannes Thumshirn default: 357e7e16f48SJohannes Thumshirn return false; 358e7e16f48SJohannes Thumshirn } 359e7e16f48SJohannes Thumshirn } 360e7e16f48SJohannes Thumshirn 361d352ac68SChris Mason /* 3621104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3631104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3641104a885SDavid Sterba */ 365ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 366ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3671104a885SDavid Sterba { 3681104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3691104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 37051bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 371d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 372d5178578SJohannes Thumshirn 373d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 374d5178578SJohannes Thumshirn crypto_shash_init(shash); 3751104a885SDavid Sterba 3761104a885SDavid Sterba /* 3771104a885SDavid Sterba * The super_block structure does not span the whole 37851bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 37951bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3801104a885SDavid Sterba */ 381d5178578SJohannes Thumshirn crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 382d5178578SJohannes Thumshirn BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 383d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 3841104a885SDavid Sterba 38551bce6c9SJohannes Thumshirn if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb))) 386e7e16f48SJohannes Thumshirn return 1; 3871104a885SDavid Sterba 388e7e16f48SJohannes Thumshirn return 0; 3891104a885SDavid Sterba } 3901104a885SDavid Sterba 391e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 392ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 393581c1760SQu Wenruo { 394e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 395581c1760SQu Wenruo int found_level; 396581c1760SQu Wenruo struct btrfs_key found_key; 397581c1760SQu Wenruo int ret; 398581c1760SQu Wenruo 399581c1760SQu Wenruo found_level = btrfs_header_level(eb); 400581c1760SQu Wenruo if (found_level != level) { 40163489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 40263489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 403581c1760SQu Wenruo btrfs_err(fs_info, 404581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 405581c1760SQu Wenruo eb->start, level, found_level); 406581c1760SQu Wenruo return -EIO; 407581c1760SQu Wenruo } 408581c1760SQu Wenruo 409581c1760SQu Wenruo if (!first_key) 410581c1760SQu Wenruo return 0; 411581c1760SQu Wenruo 4125d41be6fSQu Wenruo /* 4135d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 4145d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 4155d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 4165d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 4175d41be6fSQu Wenruo */ 4185d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 4195d41be6fSQu Wenruo return 0; 420581c1760SQu Wenruo if (found_level) 421581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 422581c1760SQu Wenruo else 423581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 424581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 425581c1760SQu Wenruo 426581c1760SQu Wenruo if (ret) { 42763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 42863489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 429581c1760SQu Wenruo btrfs_err(fs_info, 430ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 431ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 432ff76a864SLiu Bo first_key->type, first_key->offset, 433ff76a864SLiu Bo found_key.objectid, found_key.type, 434ff76a864SLiu Bo found_key.offset); 435581c1760SQu Wenruo } 436581c1760SQu Wenruo return ret; 437581c1760SQu Wenruo } 438581c1760SQu Wenruo 4391104a885SDavid Sterba /* 440d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 441d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 442581c1760SQu Wenruo * 443581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 444581c1760SQu Wenruo * @level: expected level, mandatory check 445581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 446d352ac68SChris Mason */ 4475ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 448581c1760SQu Wenruo u64 parent_transid, int level, 449581c1760SQu Wenruo struct btrfs_key *first_key) 450f188591eSChris Mason { 4515ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 452f188591eSChris Mason struct extent_io_tree *io_tree; 453ea466794SJosef Bacik int failed = 0; 454f188591eSChris Mason int ret; 455f188591eSChris Mason int num_copies = 0; 456f188591eSChris Mason int mirror_num = 0; 457ea466794SJosef Bacik int failed_mirror = 0; 458f188591eSChris Mason 4590b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 460f188591eSChris Mason while (1) { 461f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 462c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 463256dd1bbSStefan Behrens if (!ret) { 464581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 465b9fab919SChris Mason parent_transid, 0)) 466256dd1bbSStefan Behrens ret = -EIO; 467e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 468ff76a864SLiu Bo first_key, parent_transid)) 469581c1760SQu Wenruo ret = -EUCLEAN; 470581c1760SQu Wenruo else 471581c1760SQu Wenruo break; 472256dd1bbSStefan Behrens } 473d397712bSChris Mason 4740b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 475f188591eSChris Mason eb->start, eb->len); 4764235298eSChris Mason if (num_copies == 1) 477ea466794SJosef Bacik break; 4784235298eSChris Mason 4795cf1ab56SJosef Bacik if (!failed_mirror) { 4805cf1ab56SJosef Bacik failed = 1; 4815cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4825cf1ab56SJosef Bacik } 4835cf1ab56SJosef Bacik 484f188591eSChris Mason mirror_num++; 485ea466794SJosef Bacik if (mirror_num == failed_mirror) 486ea466794SJosef Bacik mirror_num++; 487ea466794SJosef Bacik 4884235298eSChris Mason if (mirror_num > num_copies) 489ea466794SJosef Bacik break; 490f188591eSChris Mason } 491ea466794SJosef Bacik 492c0901581SStefan Behrens if (failed && !ret && failed_mirror) 49320a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 494ea466794SJosef Bacik 495ea466794SJosef Bacik return ret; 496f188591eSChris Mason } 49719c00ddcSChris Mason 498d352ac68SChris Mason /* 499d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 500d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 501d352ac68SChris Mason */ 502d397712bSChris Mason 50301d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 50419c00ddcSChris Mason { 5054eee4fa4SMiao Xie u64 start = page_offset(page); 50619c00ddcSChris Mason u64 found_start; 5072996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 5082996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 50919c00ddcSChris Mason struct extent_buffer *eb; 5108d47a0d8SQu Wenruo int ret; 511f188591eSChris Mason 5124f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5134f2de97aSJosef Bacik if (page != eb->pages[0]) 5144f2de97aSJosef Bacik return 0; 5150f805531SAlex Lyakas 51619c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5170f805531SAlex Lyakas /* 5180f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5190f805531SAlex Lyakas * It is useful to know what went wrong. 5200f805531SAlex Lyakas */ 5210f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5220f805531SAlex Lyakas return -EUCLEAN; 5230f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5240f805531SAlex Lyakas return -EUCLEAN; 5250f805531SAlex Lyakas 526de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 5270f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5280f805531SAlex Lyakas 5292996e1f8SJohannes Thumshirn if (csum_tree_block(eb, result)) 5302996e1f8SJohannes Thumshirn return -EINVAL; 5312996e1f8SJohannes Thumshirn 5328d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 5338d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 5348d47a0d8SQu Wenruo else 5358d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 5368d47a0d8SQu Wenruo 5378d47a0d8SQu Wenruo if (ret < 0) { 5388d47a0d8SQu Wenruo btrfs_err(fs_info, 5398d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 5408d47a0d8SQu Wenruo eb->start); 5418d47a0d8SQu Wenruo return ret; 5428d47a0d8SQu Wenruo } 5432996e1f8SJohannes Thumshirn write_extent_buffer(eb, result, 0, csum_size); 5448d47a0d8SQu Wenruo 5452996e1f8SJohannes Thumshirn return 0; 54619c00ddcSChris Mason } 54719c00ddcSChris Mason 548b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5492b82032cSYan Zheng { 550b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 55101d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 55244880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5532b82032cSYan Zheng int ret = 1; 5542b82032cSYan Zheng 5550a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5562b82032cSYan Zheng while (fs_devices) { 5577239ff4bSNikolay Borisov u8 *metadata_uuid; 5587239ff4bSNikolay Borisov 5597239ff4bSNikolay Borisov /* 5607239ff4bSNikolay Borisov * Checking the incompat flag is only valid for the current 5617239ff4bSNikolay Borisov * fs. For seed devices it's forbidden to have their uuid 5627239ff4bSNikolay Borisov * changed so reading ->fsid in this case is fine 5637239ff4bSNikolay Borisov */ 5647239ff4bSNikolay Borisov if (fs_devices == fs_info->fs_devices && 5657239ff4bSNikolay Borisov btrfs_fs_incompat(fs_info, METADATA_UUID)) 5667239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5677239ff4bSNikolay Borisov else 5687239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5697239ff4bSNikolay Borisov 5707239ff4bSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) { 5712b82032cSYan Zheng ret = 0; 5722b82032cSYan Zheng break; 5732b82032cSYan Zheng } 5742b82032cSYan Zheng fs_devices = fs_devices->seed; 5752b82032cSYan Zheng } 5762b82032cSYan Zheng return ret; 5772b82032cSYan Zheng } 5782b82032cSYan Zheng 579facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 580facc8a22SMiao Xie u64 phy_offset, struct page *page, 581facc8a22SMiao Xie u64 start, u64 end, int mirror) 582ce9adaa5SChris Mason { 583ce9adaa5SChris Mason u64 found_start; 584ce9adaa5SChris Mason int found_level; 585ce9adaa5SChris Mason struct extent_buffer *eb; 586ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 58702873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 5882996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 589f188591eSChris Mason int ret = 0; 5902996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 591727011e0SChris Mason int reads_done; 592ce9adaa5SChris Mason 593ce9adaa5SChris Mason if (!page->private) 594ce9adaa5SChris Mason goto out; 595d397712bSChris Mason 5964f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 597d397712bSChris Mason 5980b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5990b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6000b32f4bbSJosef Bacik */ 6010b32f4bbSJosef Bacik extent_buffer_get(eb); 6020b32f4bbSJosef Bacik 6030b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 604727011e0SChris Mason if (!reads_done) 605727011e0SChris Mason goto err; 606f188591eSChris Mason 6075cf1ab56SJosef Bacik eb->read_mirror = mirror; 608656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 609ea466794SJosef Bacik ret = -EIO; 610ea466794SJosef Bacik goto err; 611ea466794SJosef Bacik } 612ea466794SJosef Bacik 613ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 614727011e0SChris Mason if (found_start != eb->start) { 615893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 616893bf4b1SSu Yue eb->start, found_start); 617f188591eSChris Mason ret = -EIO; 618ce9adaa5SChris Mason goto err; 619ce9adaa5SChris Mason } 620b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 62102873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 62294647322SDavid Sterba eb->start); 6231259ab75SChris Mason ret = -EIO; 6241259ab75SChris Mason goto err; 6251259ab75SChris Mason } 626ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6271c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 628893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 629893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6301c24c3ceSJosef Bacik ret = -EIO; 6311c24c3ceSJosef Bacik goto err; 6321c24c3ceSJosef Bacik } 633ce9adaa5SChris Mason 63485d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 63585d4e461SChris Mason eb, found_level); 6364008c04aSChris Mason 6372996e1f8SJohannes Thumshirn ret = csum_tree_block(eb, result); 6388bd98f0eSAlex Lyakas if (ret) 639a826d6dcSJosef Bacik goto err; 640a826d6dcSJosef Bacik 6412996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 6422996e1f8SJohannes Thumshirn u32 val; 6432996e1f8SJohannes Thumshirn u32 found = 0; 6442996e1f8SJohannes Thumshirn 6452996e1f8SJohannes Thumshirn memcpy(&found, result, csum_size); 6462996e1f8SJohannes Thumshirn 6472996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 6482996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 6492996e1f8SJohannes Thumshirn "%s checksum verify failed on %llu wanted %x found %x level %d", 6502996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 6512996e1f8SJohannes Thumshirn val, found, btrfs_header_level(eb)); 6522996e1f8SJohannes Thumshirn ret = -EUCLEAN; 6532996e1f8SJohannes Thumshirn goto err; 6542996e1f8SJohannes Thumshirn } 6552996e1f8SJohannes Thumshirn 656a826d6dcSJosef Bacik /* 657a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 658a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 659a826d6dcSJosef Bacik * return -EIO. 660a826d6dcSJosef Bacik */ 6611c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 662a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 663a826d6dcSJosef Bacik ret = -EIO; 664a826d6dcSJosef Bacik } 665ce9adaa5SChris Mason 666813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 667053ab70fSLiu Bo ret = -EIO; 668053ab70fSLiu Bo 6690b32f4bbSJosef Bacik if (!ret) 6700b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 67175391f0dSQu Wenruo else 67275391f0dSQu Wenruo btrfs_err(fs_info, 67375391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 67475391f0dSQu Wenruo eb->start); 675ce9adaa5SChris Mason err: 67679fb65a1SJosef Bacik if (reads_done && 67779fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 678d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6794bb31e92SArne Jansen 68053b381b3SDavid Woodhouse if (ret) { 68153b381b3SDavid Woodhouse /* 68253b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 68353b381b3SDavid Woodhouse * again, we have to make sure it has something 68453b381b3SDavid Woodhouse * to decrement 68553b381b3SDavid Woodhouse */ 68653b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6870b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 68853b381b3SDavid Woodhouse } 6890b32f4bbSJosef Bacik free_extent_buffer(eb); 690ce9adaa5SChris Mason out: 691f188591eSChris Mason return ret; 692ce9adaa5SChris Mason } 693ce9adaa5SChris Mason 6944246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 695ce9adaa5SChris Mason { 69697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 697ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6989e0af237SLiu Bo struct btrfs_workqueue *wq; 6999e0af237SLiu Bo btrfs_work_func_t func; 700ce9adaa5SChris Mason 701ce9adaa5SChris Mason fs_info = end_io_wq->info; 7024e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 703d20f7043SChris Mason 70437226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 7059e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 7069e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 7079e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 7089e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 7099e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 7109e0af237SLiu Bo func = btrfs_freespace_write_helper; 7119e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7129e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7139e0af237SLiu Bo func = btrfs_endio_raid56_helper; 714d20f7043SChris Mason } else { 7159e0af237SLiu Bo wq = fs_info->endio_write_workers; 7169e0af237SLiu Bo func = btrfs_endio_write_helper; 717d20f7043SChris Mason } 7189e0af237SLiu Bo } else { 7198b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 7208b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 7218b110e39SMiao Xie wq = fs_info->endio_repair_workers; 7228b110e39SMiao Xie func = btrfs_endio_repair_helper; 7238b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7249e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7259e0af237SLiu Bo func = btrfs_endio_raid56_helper; 7269e0af237SLiu Bo } else if (end_io_wq->metadata) { 7279e0af237SLiu Bo wq = fs_info->endio_meta_workers; 7289e0af237SLiu Bo func = btrfs_endio_meta_helper; 7299e0af237SLiu Bo } else { 7309e0af237SLiu Bo wq = fs_info->endio_workers; 7319e0af237SLiu Bo func = btrfs_endio_helper; 7329e0af237SLiu Bo } 7339e0af237SLiu Bo } 7349e0af237SLiu Bo 7359e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 7369e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 737ce9adaa5SChris Mason } 738ce9adaa5SChris Mason 7394e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 740bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7410b86a832SChris Mason { 74297eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7438b110e39SMiao Xie 74497eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 745ce9adaa5SChris Mason if (!end_io_wq) 7464e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 747ce9adaa5SChris Mason 748ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 749ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 75022c59948SChris Mason end_io_wq->info = info; 7514e4cbee9SChristoph Hellwig end_io_wq->status = 0; 752ce9adaa5SChris Mason end_io_wq->bio = bio; 75322c59948SChris Mason end_io_wq->metadata = metadata; 754ce9adaa5SChris Mason 755ce9adaa5SChris Mason bio->bi_private = end_io_wq; 756ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 75722c59948SChris Mason return 0; 75822c59948SChris Mason } 75922c59948SChris Mason 7604a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7614a69a410SChris Mason { 7624a69a410SChris Mason struct async_submit_bio *async; 7634e4cbee9SChristoph Hellwig blk_status_t ret; 7644a69a410SChris Mason 7654a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 766c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 767eaf25d93SChris Mason async->bio_offset); 76879787eaaSJeff Mahoney if (ret) 7694e4cbee9SChristoph Hellwig async->status = ret; 7704a69a410SChris Mason } 7714a69a410SChris Mason 77206ea01b1SDavid Sterba /* 77306ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 77406ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 77506ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 77606ea01b1SDavid Sterba * 77706ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 77806ea01b1SDavid Sterba * inserted into the tree. 77906ea01b1SDavid Sterba */ 7804a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7818b712842SChris Mason { 7828b712842SChris Mason struct async_submit_bio *async; 78306ea01b1SDavid Sterba struct inode *inode; 78406ea01b1SDavid Sterba blk_status_t ret; 7858b712842SChris Mason 7868b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 78706ea01b1SDavid Sterba inode = async->private_data; 7884854ddd0SChris Mason 789bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7904e4cbee9SChristoph Hellwig if (async->status) { 7914e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7924246a0b6SChristoph Hellwig bio_endio(async->bio); 79379787eaaSJeff Mahoney return; 79479787eaaSJeff Mahoney } 79579787eaaSJeff Mahoney 79606ea01b1SDavid Sterba ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, 79706ea01b1SDavid Sterba async->mirror_num, 1); 79806ea01b1SDavid Sterba if (ret) { 79906ea01b1SDavid Sterba async->bio->bi_status = ret; 80006ea01b1SDavid Sterba bio_endio(async->bio); 80106ea01b1SDavid Sterba } 8024a69a410SChris Mason } 8034a69a410SChris Mason 8044a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8054a69a410SChris Mason { 8064a69a410SChris Mason struct async_submit_bio *async; 8074a69a410SChris Mason 8084a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8098b712842SChris Mason kfree(async); 8108b712842SChris Mason } 8118b712842SChris Mason 8128c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 813c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 814c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 815e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 81644b8bd7eSChris Mason { 81744b8bd7eSChris Mason struct async_submit_bio *async; 81844b8bd7eSChris Mason 81944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 82044b8bd7eSChris Mason if (!async) 8214e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 82244b8bd7eSChris Mason 823c6100a4bSJosef Bacik async->private_data = private_data; 82444b8bd7eSChris Mason async->bio = bio; 82544b8bd7eSChris Mason async->mirror_num = mirror_num; 8264a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8274a69a410SChris Mason 8289e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 8295cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 8304a69a410SChris Mason 831eaf25d93SChris Mason async->bio_offset = bio_offset; 8328c8bee1dSChris Mason 8334e4cbee9SChristoph Hellwig async->status = 0; 83479787eaaSJeff Mahoney 83567f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8365cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 837d313d7a3SChris Mason 8385cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 83944b8bd7eSChris Mason return 0; 84044b8bd7eSChris Mason } 84144b8bd7eSChris Mason 8424e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 843ce3ed71aSChris Mason { 8442c30c71bSKent Overstreet struct bio_vec *bvec; 845ce3ed71aSChris Mason struct btrfs_root *root; 8462b070cfeSChristoph Hellwig int ret = 0; 8476dc4f100SMing Lei struct bvec_iter_all iter_all; 848ce3ed71aSChris Mason 849c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8502b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 851ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 85201d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 85379787eaaSJeff Mahoney if (ret) 85479787eaaSJeff Mahoney break; 855ce3ed71aSChris Mason } 8562c30c71bSKent Overstreet 8574e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 858ce3ed71aSChris Mason } 859ce3ed71aSChris Mason 860d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 861eaf25d93SChris Mason u64 bio_offset) 86222c59948SChris Mason { 8638b712842SChris Mason /* 8648b712842SChris Mason * when we're called for a write, we're already in the async 8655443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8668b712842SChris Mason */ 86779787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86822c59948SChris Mason } 86922c59948SChris Mason 8709b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8719b4e675aSDavid Sterba struct btrfs_inode *bi) 872de0022b9SJosef Bacik { 8736300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8746300463bSLiu Bo return 0; 8759b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 876de0022b9SJosef Bacik return 0; 877de0022b9SJosef Bacik return 1; 878de0022b9SJosef Bacik } 879de0022b9SJosef Bacik 880a56b1c7bSNikolay Borisov static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio, 88150489a57SNikolay Borisov int mirror_num, 88250489a57SNikolay Borisov unsigned long bio_flags) 88344b8bd7eSChris Mason { 8840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8859b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8864e4cbee9SChristoph Hellwig blk_status_t ret; 887cad321adSChris Mason 88837226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 889cad321adSChris Mason /* 890cad321adSChris Mason * called for a read, do the setup so that checksum validation 891cad321adSChris Mason * can happen in the async kernel threads 892cad321adSChris Mason */ 8930b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8940b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8951d4284bdSChris Mason if (ret) 89661891923SStefan Behrens goto out_w_error; 8972ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 898de0022b9SJosef Bacik } else if (!async) { 899de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 900de0022b9SJosef Bacik if (ret) 90161891923SStefan Behrens goto out_w_error; 9022ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 90361891923SStefan Behrens } else { 904cad321adSChris Mason /* 90561891923SStefan Behrens * kthread helpers are used to submit writes so that 90661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 907cad321adSChris Mason */ 908c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 909e68f2ee7SNikolay Borisov 0, inode, btree_submit_bio_start); 91044b8bd7eSChris Mason } 91144b8bd7eSChris Mason 9124246a0b6SChristoph Hellwig if (ret) 9134246a0b6SChristoph Hellwig goto out_w_error; 9144246a0b6SChristoph Hellwig return 0; 9154246a0b6SChristoph Hellwig 91661891923SStefan Behrens out_w_error: 9174e4cbee9SChristoph Hellwig bio->bi_status = ret; 9184246a0b6SChristoph Hellwig bio_endio(bio); 91961891923SStefan Behrens return ret; 92061891923SStefan Behrens } 92161891923SStefan Behrens 9223dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 923784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 924a6bc32b8SMel Gorman struct page *newpage, struct page *page, 925a6bc32b8SMel Gorman enum migrate_mode mode) 926784b4e29SChris Mason { 927784b4e29SChris Mason /* 928784b4e29SChris Mason * we can't safely write a btree page from here, 929784b4e29SChris Mason * we haven't done the locking hook 930784b4e29SChris Mason */ 931784b4e29SChris Mason if (PageDirty(page)) 932784b4e29SChris Mason return -EAGAIN; 933784b4e29SChris Mason /* 934784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 935784b4e29SChris Mason * We must have no buffers or drop them. 936784b4e29SChris Mason */ 937784b4e29SChris Mason if (page_has_private(page) && 938784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 939784b4e29SChris Mason return -EAGAIN; 940a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 941784b4e29SChris Mason } 9423dd1462eSJan Beulich #endif 943784b4e29SChris Mason 9440da5468fSChris Mason 9450da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9460da5468fSChris Mason struct writeback_control *wbc) 9470da5468fSChris Mason { 948e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 949e2d84521SMiao Xie int ret; 950e2d84521SMiao Xie 951d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 952448d640bSChris Mason 953448d640bSChris Mason if (wbc->for_kupdate) 954448d640bSChris Mason return 0; 955448d640bSChris Mason 956e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 957b9473439SChris Mason /* this is a bit racy, but that's ok */ 958d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 959d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 960d814a491SEthan Lien fs_info->dirty_metadata_batch); 961e2d84521SMiao Xie if (ret < 0) 962793955bcSChris Mason return 0; 963793955bcSChris Mason } 9640b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9650da5468fSChris Mason } 9660da5468fSChris Mason 967b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9685f39d397SChris Mason { 969d1310b2eSChris Mason struct extent_io_tree *tree; 970d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9718ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9725f39d397SChris Mason } 9735f39d397SChris Mason 97470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9755f39d397SChris Mason { 97698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 97798509cfcSChris Mason return 0; 9780c4e538bSDavid Sterba 979f7a52a40SDavid Sterba return try_release_extent_buffer(page); 980d98237b3SChris Mason } 981d98237b3SChris Mason 982d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 983d47992f8SLukas Czerner unsigned int length) 984d98237b3SChris Mason { 985d1310b2eSChris Mason struct extent_io_tree *tree; 986d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9875f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9885f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9899ad6b7bcSChris Mason if (PagePrivate(page)) { 990efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 991efe120a0SFrank Holton "page private not zero on page %llu", 992efe120a0SFrank Holton (unsigned long long)page_offset(page)); 9939ad6b7bcSChris Mason ClearPagePrivate(page); 9949ad6b7bcSChris Mason set_page_private(page, 0); 99509cbfeafSKirill A. Shutemov put_page(page); 9969ad6b7bcSChris Mason } 997d98237b3SChris Mason } 998d98237b3SChris Mason 9990b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10000b32f4bbSJosef Bacik { 1001bb146eb2SJosef Bacik #ifdef DEBUG 10020b32f4bbSJosef Bacik struct extent_buffer *eb; 10030b32f4bbSJosef Bacik 10040b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10050b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10060b32f4bbSJosef Bacik BUG_ON(!eb); 10070b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10080b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10090b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1010bb146eb2SJosef Bacik #endif 10110b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10120b32f4bbSJosef Bacik } 10130b32f4bbSJosef Bacik 10147f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1015d98237b3SChris Mason .readpage = btree_readpage, 10160da5468fSChris Mason .writepages = btree_writepages, 10175f39d397SChris Mason .releasepage = btree_releasepage, 10185f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10195a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1020784b4e29SChris Mason .migratepage = btree_migratepage, 10215a92bc88SChris Mason #endif 10220b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1023d98237b3SChris Mason }; 1024123abc88SChris Mason 10252ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1026090d1875SChris Mason { 10275f39d397SChris Mason struct extent_buffer *buf = NULL; 1028537f38f0SNikolay Borisov int ret; 1029090d1875SChris Mason 10302ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1031c871b0f2SLiu Bo if (IS_ERR(buf)) 10326197d86eSDavid Sterba return; 1033537f38f0SNikolay Borisov 1034c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 1035537f38f0SNikolay Borisov if (ret < 0) 1036537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1037537f38f0SNikolay Borisov else 10385f39d397SChris Mason free_extent_buffer(buf); 1039090d1875SChris Mason } 1040090d1875SChris Mason 10412ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1042ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1043ab0fff03SArne Jansen { 1044ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1045ab0fff03SArne Jansen int ret; 1046ab0fff03SArne Jansen 10472ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1048c871b0f2SLiu Bo if (IS_ERR(buf)) 1049ab0fff03SArne Jansen return 0; 1050ab0fff03SArne Jansen 1051ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1052ab0fff03SArne Jansen 1053c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_PAGE_LOCK, mirror_num); 1054ab0fff03SArne Jansen if (ret) { 1055537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1056ab0fff03SArne Jansen return ret; 1057ab0fff03SArne Jansen } 1058ab0fff03SArne Jansen 1059ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1060537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1061ab0fff03SArne Jansen return -EIO; 10620b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1063ab0fff03SArne Jansen *eb = buf; 1064ab0fff03SArne Jansen } else { 1065ab0fff03SArne Jansen free_extent_buffer(buf); 1066ab0fff03SArne Jansen } 1067ab0fff03SArne Jansen return 0; 1068ab0fff03SArne Jansen } 1069ab0fff03SArne Jansen 10702ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10712ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10720308af44SDavid Sterba u64 bytenr) 10730999df54SChris Mason { 10740b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10750b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10760b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10770999df54SChris Mason } 10780999df54SChris Mason 1079581c1760SQu Wenruo /* 1080581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1081581c1760SQu Wenruo * verification. 1082581c1760SQu Wenruo * 1083581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1084581c1760SQu Wenruo * @level: expected level, mandatory check 1085581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1086581c1760SQu Wenruo */ 10872ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1088581c1760SQu Wenruo u64 parent_transid, int level, 1089581c1760SQu Wenruo struct btrfs_key *first_key) 1090e20d96d6SChris Mason { 10915f39d397SChris Mason struct extent_buffer *buf = NULL; 109219c00ddcSChris Mason int ret; 109319c00ddcSChris Mason 10942ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1095c871b0f2SLiu Bo if (IS_ERR(buf)) 1096c871b0f2SLiu Bo return buf; 1097e4204dedSChris Mason 10985ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1099581c1760SQu Wenruo level, first_key); 11000f0fe8f7SFilipe David Borba Manana if (ret) { 1101537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 110264c043deSLiu Bo return ERR_PTR(ret); 11030f0fe8f7SFilipe David Borba Manana } 11045f39d397SChris Mason return buf; 1105ce9adaa5SChris Mason 1106eb60ceacSChris Mason } 1107eb60ceacSChris Mason 11086a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1109ed2ff2cbSChris Mason { 11106a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 111155c69072SChris Mason if (btrfs_header_generation(buf) == 1112e2d84521SMiao Xie fs_info->running_transaction->transid) { 1113b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1114b4ce94deSChris Mason 1115b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1116104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1117e2d84521SMiao Xie -buf->len, 1118e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1119b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 11208bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 11210b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1122925baeddSChris Mason } 11235f39d397SChris Mason } 1124ed7b63ebSJosef Bacik } 11255f39d397SChris Mason 11268257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11278257b2dcSMiao Xie { 11288257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11298257b2dcSMiao Xie int ret; 11308257b2dcSMiao Xie 11318257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11328257b2dcSMiao Xie if (!writers) 11338257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11348257b2dcSMiao Xie 11358a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11368257b2dcSMiao Xie if (ret < 0) { 11378257b2dcSMiao Xie kfree(writers); 11388257b2dcSMiao Xie return ERR_PTR(ret); 11398257b2dcSMiao Xie } 11408257b2dcSMiao Xie 11418257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11428257b2dcSMiao Xie return writers; 11438257b2dcSMiao Xie } 11448257b2dcSMiao Xie 11458257b2dcSMiao Xie static void 11468257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11478257b2dcSMiao Xie { 11488257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11498257b2dcSMiao Xie kfree(writers); 11508257b2dcSMiao Xie } 11518257b2dcSMiao Xie 1152da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1153e20d96d6SChris Mason u64 objectid) 1154d97e63b6SChris Mason { 11557c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1156cfaa7295SChris Mason root->node = NULL; 1157a28ec197SChris Mason root->commit_root = NULL; 115827cdeb70SMiao Xie root->state = 0; 1159d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11600b86a832SChris Mason 11610f7d52f4SChris Mason root->last_trans = 0; 116213a8a7c8SYan, Zheng root->highest_objectid = 0; 1163eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1164199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11656bef4d31SEric Paris root->inode_tree = RB_ROOT; 116616cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1167f0486c68SYan, Zheng root->block_rsv = NULL; 11680b86a832SChris Mason 11690b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11705d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1171eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1172eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1173199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1174199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1175d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11762ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11772ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11785d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1179eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1180199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1181f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11822ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11832ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11848287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1185a2135011SChris Mason mutex_init(&root->objectid_mutex); 1186e02119d5SChris Mason mutex_init(&root->log_mutex); 118731f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1188573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11897237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11907237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11917237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11928b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11938b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11947237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11957237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11967237f183SYan Zheng atomic_set(&root->log_writers, 0); 11972ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11980700cea7SElena Reshetova refcount_set(&root->refs, 1); 1199ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 12008ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1201eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 12027237f183SYan Zheng root->log_transid = 0; 1203d1433debSMiao Xie root->log_transid_committed = -1; 1204257c62e1SChris Mason root->last_log_commit = 0; 12057c0260eeSJeff Mahoney if (!dummy) 120643eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 120743eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1208017e5369SChris Mason 12093768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12103768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12116702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 12127c0260eeSJeff Mahoney if (!dummy) 12133f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 121406ea65a3SJosef Bacik else 121506ea65a3SJosef Bacik root->defrag_trans_start = 0; 12164d775673SChris Mason root->root_key.objectid = objectid; 12170ee5dc67SAl Viro root->anon_dev = 0; 12188ea05e3aSAlexander Block 12195f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1220370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 12213768f368SChris Mason } 12223768f368SChris Mason 122374e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 122474e4d827SDavid Sterba gfp_t flags) 12256f07e42eSAl Viro { 122674e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12276f07e42eSAl Viro if (root) 12286f07e42eSAl Viro root->fs_info = fs_info; 12296f07e42eSAl Viro return root; 12306f07e42eSAl Viro } 12316f07e42eSAl Viro 123206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 123306ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1234da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 123506ea65a3SJosef Bacik { 123606ea65a3SJosef Bacik struct btrfs_root *root; 123706ea65a3SJosef Bacik 12387c0260eeSJeff Mahoney if (!fs_info) 12397c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12407c0260eeSJeff Mahoney 12417c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 124206ea65a3SJosef Bacik if (!root) 124306ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1244da17066cSJeff Mahoney 1245b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1246da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1247faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 124806ea65a3SJosef Bacik 124906ea65a3SJosef Bacik return root; 125006ea65a3SJosef Bacik } 125106ea65a3SJosef Bacik #endif 125206ea65a3SJosef Bacik 125320897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 125420897f5cSArne Jansen u64 objectid) 125520897f5cSArne Jansen { 12569b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 125720897f5cSArne Jansen struct extent_buffer *leaf; 125820897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 125920897f5cSArne Jansen struct btrfs_root *root; 126020897f5cSArne Jansen struct btrfs_key key; 1261b89f6d1fSFilipe Manana unsigned int nofs_flag; 126220897f5cSArne Jansen int ret = 0; 126333d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 126420897f5cSArne Jansen 1265b89f6d1fSFilipe Manana /* 1266b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1267b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1268b89f6d1fSFilipe Manana */ 1269b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 127074e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 1271b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 127220897f5cSArne Jansen if (!root) 127320897f5cSArne Jansen return ERR_PTR(-ENOMEM); 127420897f5cSArne Jansen 1275da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 127620897f5cSArne Jansen root->root_key.objectid = objectid; 127720897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 127820897f5cSArne Jansen root->root_key.offset = 0; 127920897f5cSArne Jansen 12804d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 128120897f5cSArne Jansen if (IS_ERR(leaf)) { 128220897f5cSArne Jansen ret = PTR_ERR(leaf); 12831dd05682STsutomu Itoh leaf = NULL; 128420897f5cSArne Jansen goto fail; 128520897f5cSArne Jansen } 128620897f5cSArne Jansen 128720897f5cSArne Jansen root->node = leaf; 128820897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 128920897f5cSArne Jansen 129020897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129127cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 129220897f5cSArne Jansen 129320897f5cSArne Jansen root->root_item.flags = 0; 129420897f5cSArne Jansen root->root_item.byte_limit = 0; 129520897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 129620897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 129720897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 129820897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 129920897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 130020897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130120897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130233d85fdaSQu Wenruo if (is_fstree(objectid)) 13036463fe58SStefan Behrens uuid_le_gen(&uuid); 13046463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 130520897f5cSArne Jansen root->root_item.drop_level = 0; 130620897f5cSArne Jansen 130720897f5cSArne Jansen key.objectid = objectid; 130820897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 130920897f5cSArne Jansen key.offset = 0; 131020897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 131120897f5cSArne Jansen if (ret) 131220897f5cSArne Jansen goto fail; 131320897f5cSArne Jansen 131420897f5cSArne Jansen btrfs_tree_unlock(leaf); 131520897f5cSArne Jansen 131620897f5cSArne Jansen return root; 13171dd05682STsutomu Itoh 13181dd05682STsutomu Itoh fail: 13191dd05682STsutomu Itoh if (leaf) { 13201dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 132159885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13221dd05682STsutomu Itoh free_extent_buffer(leaf); 13231dd05682STsutomu Itoh } 13241dd05682STsutomu Itoh kfree(root); 13251dd05682STsutomu Itoh 13261dd05682STsutomu Itoh return ERR_PTR(ret); 132720897f5cSArne Jansen } 132820897f5cSArne Jansen 13297237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1330e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13310f7d52f4SChris Mason { 13320f7d52f4SChris Mason struct btrfs_root *root; 13337237f183SYan Zheng struct extent_buffer *leaf; 1334e02119d5SChris Mason 133574e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1336e02119d5SChris Mason if (!root) 13377237f183SYan Zheng return ERR_PTR(-ENOMEM); 1338e02119d5SChris Mason 1339da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1340e02119d5SChris Mason 1341e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1342e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1343e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 134427cdeb70SMiao Xie 13457237f183SYan Zheng /* 134627cdeb70SMiao Xie * DON'T set REF_COWS for log trees 134727cdeb70SMiao Xie * 13487237f183SYan Zheng * log trees do not get reference counted because they go away 13497237f183SYan Zheng * before a real commit is actually done. They do store pointers 13507237f183SYan Zheng * to file data extents, and those reference counts still get 13517237f183SYan Zheng * updated (along with back refs to the log tree). 13527237f183SYan Zheng */ 1353e02119d5SChris Mason 13544d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13554d75f8a9SDavid Sterba NULL, 0, 0, 0); 13567237f183SYan Zheng if (IS_ERR(leaf)) { 13577237f183SYan Zheng kfree(root); 13587237f183SYan Zheng return ERR_CAST(leaf); 13597237f183SYan Zheng } 1360e02119d5SChris Mason 13617237f183SYan Zheng root->node = leaf; 1362e02119d5SChris Mason 1363e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1364e02119d5SChris Mason btrfs_tree_unlock(root->node); 13657237f183SYan Zheng return root; 13667237f183SYan Zheng } 13677237f183SYan Zheng 13687237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13697237f183SYan Zheng struct btrfs_fs_info *fs_info) 13707237f183SYan Zheng { 13717237f183SYan Zheng struct btrfs_root *log_root; 13727237f183SYan Zheng 13737237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13747237f183SYan Zheng if (IS_ERR(log_root)) 13757237f183SYan Zheng return PTR_ERR(log_root); 13767237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13777237f183SYan Zheng fs_info->log_root_tree = log_root; 13787237f183SYan Zheng return 0; 13797237f183SYan Zheng } 13807237f183SYan Zheng 13817237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13827237f183SYan Zheng struct btrfs_root *root) 13837237f183SYan Zheng { 13840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13857237f183SYan Zheng struct btrfs_root *log_root; 13867237f183SYan Zheng struct btrfs_inode_item *inode_item; 13877237f183SYan Zheng 13880b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13897237f183SYan Zheng if (IS_ERR(log_root)) 13907237f183SYan Zheng return PTR_ERR(log_root); 13917237f183SYan Zheng 13927237f183SYan Zheng log_root->last_trans = trans->transid; 13937237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13947237f183SYan Zheng 13957237f183SYan Zheng inode_item = &log_root->root_item.inode; 13963cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13973cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13983cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1399da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14000b246afaSJeff Mahoney fs_info->nodesize); 14013cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14027237f183SYan Zheng 14035d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14047237f183SYan Zheng 14057237f183SYan Zheng WARN_ON(root->log_root); 14067237f183SYan Zheng root->log_root = log_root; 14077237f183SYan Zheng root->log_transid = 0; 1408d1433debSMiao Xie root->log_transid_committed = -1; 1409257c62e1SChris Mason root->last_log_commit = 0; 1410e02119d5SChris Mason return 0; 1411e02119d5SChris Mason } 1412e02119d5SChris Mason 141335a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1414cb517eabSMiao Xie struct btrfs_key *key) 1415e02119d5SChris Mason { 1416e02119d5SChris Mason struct btrfs_root *root; 1417e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14180f7d52f4SChris Mason struct btrfs_path *path; 141984234f3aSYan Zheng u64 generation; 1420cb517eabSMiao Xie int ret; 1421581c1760SQu Wenruo int level; 1422cb517eabSMiao Xie 1423cb517eabSMiao Xie path = btrfs_alloc_path(); 1424cb517eabSMiao Xie if (!path) 1425cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14260f7d52f4SChris Mason 142774e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1428cb517eabSMiao Xie if (!root) { 1429cb517eabSMiao Xie ret = -ENOMEM; 1430cb517eabSMiao Xie goto alloc_fail; 14310f7d52f4SChris Mason } 14320f7d52f4SChris Mason 1433da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 14340f7d52f4SChris Mason 1435cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1436cb517eabSMiao Xie &root->root_item, &root->root_key); 14370f7d52f4SChris Mason if (ret) { 143813a8a7c8SYan, Zheng if (ret > 0) 143913a8a7c8SYan, Zheng ret = -ENOENT; 1440cb517eabSMiao Xie goto find_fail; 14410f7d52f4SChris Mason } 144213a8a7c8SYan, Zheng 144384234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1444581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14452ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14462ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1447581c1760SQu Wenruo generation, level, NULL); 144864c043deSLiu Bo if (IS_ERR(root->node)) { 144964c043deSLiu Bo ret = PTR_ERR(root->node); 1450cb517eabSMiao Xie goto find_fail; 1451cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1452cb517eabSMiao Xie ret = -EIO; 145364c043deSLiu Bo free_extent_buffer(root->node); 145464c043deSLiu Bo goto find_fail; 1455416bc658SJosef Bacik } 14565d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 145713a8a7c8SYan, Zheng out: 1458cb517eabSMiao Xie btrfs_free_path(path); 1459cb517eabSMiao Xie return root; 1460cb517eabSMiao Xie 1461cb517eabSMiao Xie find_fail: 1462cb517eabSMiao Xie kfree(root); 1463cb517eabSMiao Xie alloc_fail: 1464cb517eabSMiao Xie root = ERR_PTR(ret); 1465cb517eabSMiao Xie goto out; 1466cb517eabSMiao Xie } 1467cb517eabSMiao Xie 1468cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1469cb517eabSMiao Xie struct btrfs_key *location) 1470cb517eabSMiao Xie { 1471cb517eabSMiao Xie struct btrfs_root *root; 1472cb517eabSMiao Xie 1473cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1474cb517eabSMiao Xie if (IS_ERR(root)) 1475cb517eabSMiao Xie return root; 1476cb517eabSMiao Xie 1477cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 147827cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 147908fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 148008fe4db1SLi Zefan } 148113a8a7c8SYan, Zheng 14825eda7b5eSChris Mason return root; 14835eda7b5eSChris Mason } 14845eda7b5eSChris Mason 1485cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1486cb517eabSMiao Xie { 1487cb517eabSMiao Xie int ret; 14888257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1489cb517eabSMiao Xie 1490cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1491cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1492cb517eabSMiao Xie GFP_NOFS); 1493cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1494cb517eabSMiao Xie ret = -ENOMEM; 1495cb517eabSMiao Xie goto fail; 1496cb517eabSMiao Xie } 1497cb517eabSMiao Xie 14988257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14998257b2dcSMiao Xie if (IS_ERR(writers)) { 15008257b2dcSMiao Xie ret = PTR_ERR(writers); 15018257b2dcSMiao Xie goto fail; 15028257b2dcSMiao Xie } 15038257b2dcSMiao Xie root->subv_writers = writers; 15048257b2dcSMiao Xie 1505cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 150657cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 150757cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1508cb517eabSMiao Xie 1509cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1510cb517eabSMiao Xie if (ret) 1511876d2cf1SLiu Bo goto fail; 1512f32e48e9SChandan Rajendra 1513f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1514f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1515f32e48e9SChandan Rajendra &root->highest_objectid); 1516f32e48e9SChandan Rajendra if (ret) { 1517f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1518876d2cf1SLiu Bo goto fail; 1519f32e48e9SChandan Rajendra } 1520f32e48e9SChandan Rajendra 1521f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1522f32e48e9SChandan Rajendra 1523f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1524f32e48e9SChandan Rajendra 1525cb517eabSMiao Xie return 0; 1526cb517eabSMiao Xie fail: 152784db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1528cb517eabSMiao Xie return ret; 1529cb517eabSMiao Xie } 1530cb517eabSMiao Xie 153135bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1532cb517eabSMiao Xie u64 root_id) 1533cb517eabSMiao Xie { 1534cb517eabSMiao Xie struct btrfs_root *root; 1535cb517eabSMiao Xie 1536cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1537cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1538cb517eabSMiao Xie (unsigned long)root_id); 1539cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1540cb517eabSMiao Xie return root; 1541cb517eabSMiao Xie } 1542cb517eabSMiao Xie 1543cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1544cb517eabSMiao Xie struct btrfs_root *root) 1545cb517eabSMiao Xie { 1546cb517eabSMiao Xie int ret; 1547cb517eabSMiao Xie 1548e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1549cb517eabSMiao Xie if (ret) 1550cb517eabSMiao Xie return ret; 1551cb517eabSMiao Xie 1552cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1553cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1554cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1555cb517eabSMiao Xie root); 1556cb517eabSMiao Xie if (ret == 0) 155727cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1558cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1559cb517eabSMiao Xie radix_tree_preload_end(); 1560cb517eabSMiao Xie 1561cb517eabSMiao Xie return ret; 1562cb517eabSMiao Xie } 1563cb517eabSMiao Xie 1564c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1565c00869f1SMiao Xie struct btrfs_key *location, 1566c00869f1SMiao Xie bool check_ref) 15675eda7b5eSChris Mason { 15685eda7b5eSChris Mason struct btrfs_root *root; 1569381cf658SDavid Sterba struct btrfs_path *path; 15701d4c08e0SDavid Sterba struct btrfs_key key; 15715eda7b5eSChris Mason int ret; 15725eda7b5eSChris Mason 1573edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1574edbd8d4eSChris Mason return fs_info->tree_root; 1575edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1576edbd8d4eSChris Mason return fs_info->extent_root; 15778f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15788f18cf13SChris Mason return fs_info->chunk_root; 15798f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15808f18cf13SChris Mason return fs_info->dev_root; 15810403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15820403e47eSYan Zheng return fs_info->csum_root; 1583bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1584bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1585bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1586f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1587f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1588f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 158970f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 159070f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 159170f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 15924df27c4dSYan, Zheng again: 1593cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 159448475471SStefan Behrens if (root) { 1595c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 159648475471SStefan Behrens return ERR_PTR(-ENOENT); 15975eda7b5eSChris Mason return root; 159848475471SStefan Behrens } 15995eda7b5eSChris Mason 1600cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 16015eda7b5eSChris Mason if (IS_ERR(root)) 16025eda7b5eSChris Mason return root; 16033394e160SChris Mason 1604c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1605d68fc57bSYan, Zheng ret = -ENOENT; 1606d68fc57bSYan, Zheng goto fail; 1607d68fc57bSYan, Zheng } 1608d68fc57bSYan, Zheng 1609cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1610cb517eabSMiao Xie if (ret) 1611cb517eabSMiao Xie goto fail; 1612cb517eabSMiao Xie 1613381cf658SDavid Sterba path = btrfs_alloc_path(); 1614381cf658SDavid Sterba if (!path) { 1615381cf658SDavid Sterba ret = -ENOMEM; 1616381cf658SDavid Sterba goto fail; 1617381cf658SDavid Sterba } 16181d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16191d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16201d4c08e0SDavid Sterba key.offset = location->objectid; 16211d4c08e0SDavid Sterba 16221d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1623381cf658SDavid Sterba btrfs_free_path(path); 1624d68fc57bSYan, Zheng if (ret < 0) 1625d68fc57bSYan, Zheng goto fail; 1626d68fc57bSYan, Zheng if (ret == 0) 162727cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1628d68fc57bSYan, Zheng 1629cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16300f7d52f4SChris Mason if (ret) { 16314df27c4dSYan, Zheng if (ret == -EEXIST) { 163284db5ccfSDavid Sterba btrfs_free_fs_root(root); 16334df27c4dSYan, Zheng goto again; 16340f7d52f4SChris Mason } 16354df27c4dSYan, Zheng goto fail; 16364df27c4dSYan, Zheng } 1637edbd8d4eSChris Mason return root; 16384df27c4dSYan, Zheng fail: 163984db5ccfSDavid Sterba btrfs_free_fs_root(root); 16404df27c4dSYan, Zheng return ERR_PTR(ret); 1641edbd8d4eSChris Mason } 1642edbd8d4eSChris Mason 164304160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 164404160088SChris Mason { 164504160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 164604160088SChris Mason int ret = 0; 164704160088SChris Mason struct btrfs_device *device; 164804160088SChris Mason struct backing_dev_info *bdi; 1649b7967db7SChris Mason 16501f78160cSXiao Guangrong rcu_read_lock(); 16511f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1652dfe25020SChris Mason if (!device->bdev) 1653dfe25020SChris Mason continue; 1654efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1655ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 165604160088SChris Mason ret = 1; 165704160088SChris Mason break; 165804160088SChris Mason } 165904160088SChris Mason } 16601f78160cSXiao Guangrong rcu_read_unlock(); 166104160088SChris Mason return ret; 166204160088SChris Mason } 166304160088SChris Mason 16648b712842SChris Mason /* 16658b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16668b712842SChris Mason * functions. This is where read checksum verification actually happens 16678b712842SChris Mason */ 16688b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1669ce9adaa5SChris Mason { 1670ce9adaa5SChris Mason struct bio *bio; 167197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1672ce9adaa5SChris Mason 167397eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1674ce9adaa5SChris Mason bio = end_io_wq->bio; 16758b712842SChris Mason 16764e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1677ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1678ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 167997eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 16804246a0b6SChristoph Hellwig bio_endio(bio); 1681ce9adaa5SChris Mason } 168244b8bd7eSChris Mason 1683a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1684a74a4b97SChris Mason { 1685a74a4b97SChris Mason struct btrfs_root *root = arg; 16860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1687d0278245SMiao Xie int again; 1688a74a4b97SChris Mason 1689d6fd0ae2SOmar Sandoval while (1) { 1690d0278245SMiao Xie again = 0; 16919d1a2a3aSDavid Sterba 1692fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1693fd340d0fSJosef Bacik 1694d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 16952ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1696d0278245SMiao Xie goto sleep; 1697d0278245SMiao Xie 169890c711abSZygo Blaxell /* 169990c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 170090c711abSZygo Blaxell * before we have finished mounting the filesystem. 170190c711abSZygo Blaxell */ 17020b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 170390c711abSZygo Blaxell goto sleep; 170490c711abSZygo Blaxell 17050b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1706d0278245SMiao Xie goto sleep; 1707d0278245SMiao Xie 1708dc7f370cSMiao Xie /* 1709dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1710dc7f370cSMiao Xie * during the above check and trylock. 1711dc7f370cSMiao Xie */ 17122ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17130b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1714dc7f370cSMiao Xie goto sleep; 1715dc7f370cSMiao Xie } 1716dc7f370cSMiao Xie 17172ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1718c2d6cb16SFilipe Manana 17199d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17200b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1721a74a4b97SChris Mason 1722d0278245SMiao Xie /* 172305323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 172405323cd1SMiao Xie * needn't do anything special here. 1725d0278245SMiao Xie */ 17260b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 172767c5e7d4SFilipe Manana 172867c5e7d4SFilipe Manana /* 172967c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 173067c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 173167c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 173267c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 173367c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 173467c5e7d4SFilipe Manana * unused block groups. 173567c5e7d4SFilipe Manana */ 17360b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1737d0278245SMiao Xie sleep: 1738fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1739d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1740d6fd0ae2SOmar Sandoval kthread_parkme(); 1741d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1742d6fd0ae2SOmar Sandoval return 0; 1743838fe188SJiri Kosina if (!again) { 1744a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1745a74a4b97SChris Mason schedule(); 1746a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1747a74a4b97SChris Mason } 1748da288d28SFilipe Manana } 1749a74a4b97SChris Mason } 1750a74a4b97SChris Mason 1751a74a4b97SChris Mason static int transaction_kthread(void *arg) 1752a74a4b97SChris Mason { 1753a74a4b97SChris Mason struct btrfs_root *root = arg; 17540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1755a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1756a74a4b97SChris Mason struct btrfs_transaction *cur; 17578929ecfaSYan, Zheng u64 transid; 1758a944442cSAllen Pais time64_t now; 1759a74a4b97SChris Mason unsigned long delay; 1760914b2007SJan Kara bool cannot_commit; 1761a74a4b97SChris Mason 1762a74a4b97SChris Mason do { 1763914b2007SJan Kara cannot_commit = false; 17640b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17650b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1766a74a4b97SChris Mason 17670b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17680b246afaSJeff Mahoney cur = fs_info->running_transaction; 1769a74a4b97SChris Mason if (!cur) { 17700b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1771a74a4b97SChris Mason goto sleep; 1772a74a4b97SChris Mason } 177331153d81SYan Zheng 1774afd48513SArnd Bergmann now = ktime_get_seconds(); 17754a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 1776a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 17778b87dc17SDavid Sterba (now < cur->start_time || 17780b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1780a74a4b97SChris Mason delay = HZ * 5; 1781a74a4b97SChris Mason goto sleep; 1782a74a4b97SChris Mason } 17838929ecfaSYan, Zheng transid = cur->transid; 17840b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 178556bec294SChris Mason 178679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1787354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1788914b2007SJan Kara if (IS_ERR(trans)) { 1789354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1790914b2007SJan Kara cannot_commit = true; 179179787eaaSJeff Mahoney goto sleep; 1792914b2007SJan Kara } 17938929ecfaSYan, Zheng if (transid == trans->transid) { 17943a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 17958929ecfaSYan, Zheng } else { 17963a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17978929ecfaSYan, Zheng } 1798a74a4b97SChris Mason sleep: 17990b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18000b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1801a74a4b97SChris Mason 18024e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18030b246afaSJeff Mahoney &fs_info->fs_state))) 18042ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18058929ecfaSYan, Zheng if (!kthread_should_stop() && 18060b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1807914b2007SJan Kara cannot_commit)) 1808bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1809a74a4b97SChris Mason } while (!kthread_should_stop()); 1810a74a4b97SChris Mason return 0; 1811a74a4b97SChris Mason } 1812a74a4b97SChris Mason 1813af31f5e5SChris Mason /* 1814af31f5e5SChris Mason * this will find the highest generation in the array of 1815af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1816af31f5e5SChris Mason * or -1 if we can't find anything. 1817af31f5e5SChris Mason * 1818af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1819af31f5e5SChris Mason * generation of the latest root in the array with the generation 1820af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1821af31f5e5SChris Mason */ 1822af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1823af31f5e5SChris Mason { 1824af31f5e5SChris Mason u64 cur; 1825af31f5e5SChris Mason int newest_index = -1; 1826af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1827af31f5e5SChris Mason int i; 1828af31f5e5SChris Mason 1829af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1830af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1831af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1832af31f5e5SChris Mason if (cur == newest_gen) 1833af31f5e5SChris Mason newest_index = i; 1834af31f5e5SChris Mason } 1835af31f5e5SChris Mason 1836af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1837af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1838af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1839af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1840af31f5e5SChris Mason if (cur == newest_gen) 1841af31f5e5SChris Mason newest_index = 0; 1842af31f5e5SChris Mason } 1843af31f5e5SChris Mason return newest_index; 1844af31f5e5SChris Mason } 1845af31f5e5SChris Mason 1846af31f5e5SChris Mason 1847af31f5e5SChris Mason /* 1848af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1849af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1850af31f5e5SChris Mason * field in the fs_info struct 1851af31f5e5SChris Mason */ 1852af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1853af31f5e5SChris Mason u64 newest_gen) 1854af31f5e5SChris Mason { 1855af31f5e5SChris Mason int newest_index = -1; 1856af31f5e5SChris Mason 1857af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1858af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1859af31f5e5SChris Mason if (newest_index == -1) { 1860af31f5e5SChris Mason info->backup_root_index = 0; 1861af31f5e5SChris Mason } else { 1862af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1863af31f5e5SChris Mason } 1864af31f5e5SChris Mason } 1865af31f5e5SChris Mason 1866af31f5e5SChris Mason /* 1867af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1868af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1869af31f5e5SChris Mason * done 1870af31f5e5SChris Mason */ 1871af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1872af31f5e5SChris Mason { 1873af31f5e5SChris Mason int next_backup; 1874af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1875af31f5e5SChris Mason int last_backup; 1876af31f5e5SChris Mason 1877af31f5e5SChris Mason next_backup = info->backup_root_index; 1878af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1879af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason /* 1882af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1883af31f5e5SChris Mason * this happens only at umount 1884af31f5e5SChris Mason */ 1885af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1886af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1887af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1888af31f5e5SChris Mason next_backup = last_backup; 1889af31f5e5SChris Mason 1890af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1891af31f5e5SChris Mason 1892af31f5e5SChris Mason /* 1893af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1894af31f5e5SChris Mason * regardless of which ones we use today 1895af31f5e5SChris Mason */ 1896af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1899af31f5e5SChris Mason 1900af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1901af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1902af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1903af31f5e5SChris Mason 1904af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1905af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1908af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1909af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1910af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1911af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1912af31f5e5SChris Mason 1913af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1914af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1915af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1916af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1917af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1918af31f5e5SChris Mason 19197c7e82a7SChris Mason /* 19207c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19217c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19227c7e82a7SChris Mason */ 19237c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19247c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19257c7e82a7SChris Mason info->fs_root->node->start); 1926af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1927af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1928af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1929af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19307c7e82a7SChris Mason } 1931af31f5e5SChris Mason 1932af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1933af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1934af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1935af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1936af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1937af31f5e5SChris Mason 1938af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1939af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1940af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1941af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1942af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1943af31f5e5SChris Mason 1944af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1945af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1946af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1947af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1948af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1949af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1950af31f5e5SChris Mason 1951af31f5e5SChris Mason /* 1952af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1953af31f5e5SChris Mason * for the next commit 1954af31f5e5SChris Mason */ 1955af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1956af31f5e5SChris Mason &info->super_for_commit->super_roots, 1957af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1958af31f5e5SChris Mason } 1959af31f5e5SChris Mason 1960af31f5e5SChris Mason /* 1961af31f5e5SChris Mason * this copies info out of the root backup array and back into 1962af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1963af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1964af31f5e5SChris Mason * tried and the last backup index you used. 1965af31f5e5SChris Mason * 1966af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1967af31f5e5SChris Mason */ 1968af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1969af31f5e5SChris Mason struct btrfs_super_block *super, 1970af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1971af31f5e5SChris Mason { 1972af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1973af31f5e5SChris Mason int newest = *backup_index; 1974af31f5e5SChris Mason 1975af31f5e5SChris Mason if (*num_backups_tried == 0) { 1976af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1977af31f5e5SChris Mason 1978af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1979af31f5e5SChris Mason if (newest == -1) 1980af31f5e5SChris Mason return -1; 1981af31f5e5SChris Mason 1982af31f5e5SChris Mason *backup_index = newest; 1983af31f5e5SChris Mason *num_backups_tried = 1; 1984af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1985af31f5e5SChris Mason /* we've tried all the backups, all done */ 1986af31f5e5SChris Mason return -1; 1987af31f5e5SChris Mason } else { 1988af31f5e5SChris Mason /* jump to the next oldest backup */ 1989af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1990af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1991af31f5e5SChris Mason *backup_index = newest; 1992af31f5e5SChris Mason *num_backups_tried += 1; 1993af31f5e5SChris Mason } 1994af31f5e5SChris Mason root_backup = super->super_roots + newest; 1995af31f5e5SChris Mason 1996af31f5e5SChris Mason btrfs_set_super_generation(super, 1997af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1998af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1999af31f5e5SChris Mason btrfs_set_super_root_level(super, 2000af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2001af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2002af31f5e5SChris Mason 2003af31f5e5SChris Mason /* 2004af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2005af31f5e5SChris Mason * need a fsck 2006af31f5e5SChris Mason */ 2007af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2008af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2009af31f5e5SChris Mason return 0; 2010af31f5e5SChris Mason } 2011af31f5e5SChris Mason 20127abadb64SLiu Bo /* helper to cleanup workers */ 20137abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20147abadb64SLiu Bo { 2015dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2016afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20175cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2018fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2019fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20208b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2021d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2022fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2023fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2024a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20255b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2026e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2027736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2028a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2029fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2030a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2031a9b9477dSFilipe Manana /* 2032a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2033a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2034a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2035a9b9477dSFilipe Manana */ 2036a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2037a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20387abadb64SLiu Bo } 20397abadb64SLiu Bo 20402e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20412e9f5954SRashika { 20422e9f5954SRashika if (root) { 20432e9f5954SRashika free_extent_buffer(root->node); 20442e9f5954SRashika free_extent_buffer(root->commit_root); 20452e9f5954SRashika root->node = NULL; 20462e9f5954SRashika root->commit_root = NULL; 20472e9f5954SRashika } 20482e9f5954SRashika } 20492e9f5954SRashika 2050af31f5e5SChris Mason /* helper to cleanup tree roots */ 2051af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2052af31f5e5SChris Mason { 20532e9f5954SRashika free_root_extent_buffers(info->tree_root); 2054655b09feSJosef Bacik 20552e9f5954SRashika free_root_extent_buffers(info->dev_root); 20562e9f5954SRashika free_root_extent_buffers(info->extent_root); 20572e9f5954SRashika free_root_extent_buffers(info->csum_root); 20582e9f5954SRashika free_root_extent_buffers(info->quota_root); 20592e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20602e9f5954SRashika if (chunk_root) 20612e9f5954SRashika free_root_extent_buffers(info->chunk_root); 206270f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2063af31f5e5SChris Mason } 2064af31f5e5SChris Mason 2065faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2066171f6537SJosef Bacik { 2067171f6537SJosef Bacik int ret; 2068171f6537SJosef Bacik struct btrfs_root *gang[8]; 2069171f6537SJosef Bacik int i; 2070171f6537SJosef Bacik 2071171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2072171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2073171f6537SJosef Bacik struct btrfs_root, root_list); 2074171f6537SJosef Bacik list_del(&gang[0]->root_list); 2075171f6537SJosef Bacik 207627cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2077cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2078171f6537SJosef Bacik } else { 2079171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2080171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2081b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2082171f6537SJosef Bacik } 2083171f6537SJosef Bacik } 2084171f6537SJosef Bacik 2085171f6537SJosef Bacik while (1) { 2086171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2087171f6537SJosef Bacik (void **)gang, 0, 2088171f6537SJosef Bacik ARRAY_SIZE(gang)); 2089171f6537SJosef Bacik if (!ret) 2090171f6537SJosef Bacik break; 2091171f6537SJosef Bacik for (i = 0; i < ret; i++) 2092cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2093171f6537SJosef Bacik } 20941a4319ccSLiu Bo 20951a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20961a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 20972ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 20981a4319ccSLiu Bo } 2099171f6537SJosef Bacik } 2100af31f5e5SChris Mason 2101638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2102638aa7edSEric Sandeen { 2103638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2104638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2105638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2106638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2107638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2108638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2109ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2110638aa7edSEric Sandeen } 2111638aa7edSEric Sandeen 2112779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2113779a65a4SEric Sandeen { 2114779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2115779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2116779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2117779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2118779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2119779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2120779a65a4SEric Sandeen } 2121779a65a4SEric Sandeen 21226bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2123f37938e0SEric Sandeen { 21242ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21252ff7e61eSJeff Mahoney 21262ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21272ff7e61eSJeff Mahoney set_nlink(inode, 1); 2128f37938e0SEric Sandeen /* 2129f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2130f37938e0SEric Sandeen * the real end of the address space is determined by all of 2131f37938e0SEric Sandeen * the devices in the system 2132f37938e0SEric Sandeen */ 21332ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21342ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2135f37938e0SEric Sandeen 21362ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 213743eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 213843eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 21397b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21402ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2141f37938e0SEric Sandeen 21422ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2143f37938e0SEric Sandeen 21442ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21452ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21462ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21472ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2148f37938e0SEric Sandeen } 2149f37938e0SEric Sandeen 2150ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2151ad618368SEric Sandeen { 2152ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2153129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21547f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2155ad618368SEric Sandeen } 2156ad618368SEric Sandeen 2157f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2158f9e92e40SEric Sandeen { 2159f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2160f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2161f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2162f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2163f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2164f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2165d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2166f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2167f9e92e40SEric Sandeen } 2168f9e92e40SEric Sandeen 21692a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21702a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21712a458198SEric Sandeen { 2172f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21736f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21742a458198SEric Sandeen 21752a458198SEric Sandeen fs_info->workers = 2176cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2177cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21782a458198SEric Sandeen 21792a458198SEric Sandeen fs_info->delalloc_workers = 2180cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2181cb001095SJeff Mahoney flags, max_active, 2); 21822a458198SEric Sandeen 21832a458198SEric Sandeen fs_info->flush_workers = 2184cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2185cb001095SJeff Mahoney flags, max_active, 0); 21862a458198SEric Sandeen 21872a458198SEric Sandeen fs_info->caching_workers = 2188cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21892a458198SEric Sandeen 21902a458198SEric Sandeen /* 21912a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 21922a458198SEric Sandeen * likely that bios will be send down in a sane order to the 21932a458198SEric Sandeen * devices 21942a458198SEric Sandeen */ 21952a458198SEric Sandeen fs_info->submit_workers = 2196cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 21972a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 21982a458198SEric Sandeen max_active), 64); 21992a458198SEric Sandeen 22002a458198SEric Sandeen fs_info->fixup_workers = 2201cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22022a458198SEric Sandeen 22032a458198SEric Sandeen /* 22042a458198SEric Sandeen * endios are largely parallel and should have a very 22052a458198SEric Sandeen * low idle thresh 22062a458198SEric Sandeen */ 22072a458198SEric Sandeen fs_info->endio_workers = 2208cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22092a458198SEric Sandeen fs_info->endio_meta_workers = 2210cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2211cb001095SJeff Mahoney max_active, 4); 22122a458198SEric Sandeen fs_info->endio_meta_write_workers = 2213cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2214cb001095SJeff Mahoney max_active, 2); 22152a458198SEric Sandeen fs_info->endio_raid56_workers = 2216cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2217cb001095SJeff Mahoney max_active, 4); 22182a458198SEric Sandeen fs_info->endio_repair_workers = 2219cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 22202a458198SEric Sandeen fs_info->rmw_workers = 2221cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22222a458198SEric Sandeen fs_info->endio_write_workers = 2223cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2224cb001095SJeff Mahoney max_active, 2); 22252a458198SEric Sandeen fs_info->endio_freespace_worker = 2226cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2227cb001095SJeff Mahoney max_active, 0); 22282a458198SEric Sandeen fs_info->delayed_workers = 2229cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2230cb001095SJeff Mahoney max_active, 0); 22312a458198SEric Sandeen fs_info->readahead_workers = 2232cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2233cb001095SJeff Mahoney max_active, 2); 22342a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2235cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 22362a458198SEric Sandeen fs_info->extent_workers = 2237cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 22382a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22392a458198SEric Sandeen max_active), 8); 22402a458198SEric Sandeen 22412a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 22422a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 22432a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22442a458198SEric Sandeen fs_info->endio_meta_write_workers && 22452a458198SEric Sandeen fs_info->endio_repair_workers && 22462a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22472a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22482a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22492a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 22502a458198SEric Sandeen fs_info->extent_workers && 22512a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 22522a458198SEric Sandeen return -ENOMEM; 22532a458198SEric Sandeen } 22542a458198SEric Sandeen 22552a458198SEric Sandeen return 0; 22562a458198SEric Sandeen } 22572a458198SEric Sandeen 22586d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22596d97c6e3SJohannes Thumshirn { 22606d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 22616d97c6e3SJohannes Thumshirn const char *csum_name = btrfs_super_csum_name(csum_type); 22626d97c6e3SJohannes Thumshirn 22636d97c6e3SJohannes Thumshirn csum_shash = crypto_alloc_shash(csum_name, 0, 0); 22646d97c6e3SJohannes Thumshirn 22656d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22666d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 22676d97c6e3SJohannes Thumshirn csum_name); 22686d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22696d97c6e3SJohannes Thumshirn } 22706d97c6e3SJohannes Thumshirn 22716d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22726d97c6e3SJohannes Thumshirn 22736d97c6e3SJohannes Thumshirn return 0; 22746d97c6e3SJohannes Thumshirn } 22756d97c6e3SJohannes Thumshirn 22766d97c6e3SJohannes Thumshirn static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 22776d97c6e3SJohannes Thumshirn { 22786d97c6e3SJohannes Thumshirn crypto_free_shash(fs_info->csum_shash); 22796d97c6e3SJohannes Thumshirn } 22806d97c6e3SJohannes Thumshirn 228163443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 228263443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 228363443bf5SEric Sandeen { 228463443bf5SEric Sandeen int ret; 228563443bf5SEric Sandeen struct btrfs_root *log_tree_root; 228663443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 228763443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2288581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 228963443bf5SEric Sandeen 229063443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2291f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 229263443bf5SEric Sandeen return -EIO; 229363443bf5SEric Sandeen } 229463443bf5SEric Sandeen 229574e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 229663443bf5SEric Sandeen if (!log_tree_root) 229763443bf5SEric Sandeen return -ENOMEM; 229863443bf5SEric Sandeen 2299da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 230063443bf5SEric Sandeen 23012ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2302581c1760SQu Wenruo fs_info->generation + 1, 2303581c1760SQu Wenruo level, NULL); 230464c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2305f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 23060eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 230764c043deSLiu Bo kfree(log_tree_root); 23080eeff236SLiu Bo return ret; 230964c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2310f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 231163443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 231263443bf5SEric Sandeen kfree(log_tree_root); 231363443bf5SEric Sandeen return -EIO; 231463443bf5SEric Sandeen } 231563443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 231663443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 231763443bf5SEric Sandeen if (ret) { 23180b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 231963443bf5SEric Sandeen "Failed to recover log tree"); 232063443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 232163443bf5SEric Sandeen kfree(log_tree_root); 232263443bf5SEric Sandeen return ret; 232363443bf5SEric Sandeen } 232463443bf5SEric Sandeen 2325bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23266bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 232763443bf5SEric Sandeen if (ret) 232863443bf5SEric Sandeen return ret; 232963443bf5SEric Sandeen } 233063443bf5SEric Sandeen 233163443bf5SEric Sandeen return 0; 233263443bf5SEric Sandeen } 233363443bf5SEric Sandeen 23346bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23354bbcaa64SEric Sandeen { 23366bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2337a4f3d2c4SDavid Sterba struct btrfs_root *root; 23384bbcaa64SEric Sandeen struct btrfs_key location; 23394bbcaa64SEric Sandeen int ret; 23404bbcaa64SEric Sandeen 23416bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23426bccf3abSJeff Mahoney 23434bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23444bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23454bbcaa64SEric Sandeen location.offset = 0; 23464bbcaa64SEric Sandeen 2347a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2348f50f4353SLiu Bo if (IS_ERR(root)) { 2349f50f4353SLiu Bo ret = PTR_ERR(root); 2350f50f4353SLiu Bo goto out; 2351f50f4353SLiu Bo } 2352a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2353a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23544bbcaa64SEric Sandeen 23554bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2356a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2357f50f4353SLiu Bo if (IS_ERR(root)) { 2358f50f4353SLiu Bo ret = PTR_ERR(root); 2359f50f4353SLiu Bo goto out; 2360f50f4353SLiu Bo } 2361a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2362a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23634bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23644bbcaa64SEric Sandeen 23654bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2366a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2367f50f4353SLiu Bo if (IS_ERR(root)) { 2368f50f4353SLiu Bo ret = PTR_ERR(root); 2369f50f4353SLiu Bo goto out; 2370f50f4353SLiu Bo } 2371a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2372a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23734bbcaa64SEric Sandeen 23744bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2375a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2376a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2377a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2378afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2379a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23804bbcaa64SEric Sandeen } 23814bbcaa64SEric Sandeen 23824bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2383a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2384a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2385a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23864bbcaa64SEric Sandeen if (ret != -ENOENT) 2387f50f4353SLiu Bo goto out; 23884bbcaa64SEric Sandeen } else { 2389a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2390a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23914bbcaa64SEric Sandeen } 23924bbcaa64SEric Sandeen 239370f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 239470f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 239570f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2396f50f4353SLiu Bo if (IS_ERR(root)) { 2397f50f4353SLiu Bo ret = PTR_ERR(root); 2398f50f4353SLiu Bo goto out; 2399f50f4353SLiu Bo } 240070f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 240170f6d82eSOmar Sandoval fs_info->free_space_root = root; 240270f6d82eSOmar Sandoval } 240370f6d82eSOmar Sandoval 24044bbcaa64SEric Sandeen return 0; 2405f50f4353SLiu Bo out: 2406f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2407f50f4353SLiu Bo location.objectid, ret); 2408f50f4353SLiu Bo return ret; 24094bbcaa64SEric Sandeen } 24104bbcaa64SEric Sandeen 2411069ec957SQu Wenruo /* 2412069ec957SQu Wenruo * Real super block validation 2413069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2414069ec957SQu Wenruo * 2415069ec957SQu Wenruo * @sb: super block to check 2416069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2417069ec957SQu Wenruo * 0 the primary (1st) sb 2418069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2419069ec957SQu Wenruo * -1 skip bytenr check 2420069ec957SQu Wenruo */ 2421069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2422069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 242321a852b0SQu Wenruo { 242421a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 242521a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 242621a852b0SQu Wenruo int ret = 0; 242721a852b0SQu Wenruo 242821a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 242921a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 243021a852b0SQu Wenruo ret = -EINVAL; 243121a852b0SQu Wenruo } 243221a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 243321a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 243421a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 243521a852b0SQu Wenruo ret = -EINVAL; 243621a852b0SQu Wenruo } 243721a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 243821a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 243921a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 244021a852b0SQu Wenruo ret = -EINVAL; 244121a852b0SQu Wenruo } 244221a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 244321a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 244421a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 244521a852b0SQu Wenruo ret = -EINVAL; 244621a852b0SQu Wenruo } 244721a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 244821a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 244921a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 245021a852b0SQu Wenruo ret = -EINVAL; 245121a852b0SQu Wenruo } 245221a852b0SQu Wenruo 245321a852b0SQu Wenruo /* 245421a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 245521a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 245621a852b0SQu Wenruo */ 245721a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 245821a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 245921a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 246021a852b0SQu Wenruo ret = -EINVAL; 246121a852b0SQu Wenruo } 246221a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 246321a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 246421a852b0SQu Wenruo btrfs_err(fs_info, 246521a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 246621a852b0SQu Wenruo sectorsize, PAGE_SIZE); 246721a852b0SQu Wenruo ret = -EINVAL; 246821a852b0SQu Wenruo } 246921a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 247021a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 247121a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 247221a852b0SQu Wenruo ret = -EINVAL; 247321a852b0SQu Wenruo } 247421a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 247521a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 247621a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 247721a852b0SQu Wenruo ret = -EINVAL; 247821a852b0SQu Wenruo } 247921a852b0SQu Wenruo 248021a852b0SQu Wenruo /* Root alignment check */ 248121a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 248221a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 248321a852b0SQu Wenruo btrfs_super_root(sb)); 248421a852b0SQu Wenruo ret = -EINVAL; 248521a852b0SQu Wenruo } 248621a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 248721a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 248821a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 248921a852b0SQu Wenruo ret = -EINVAL; 249021a852b0SQu Wenruo } 249121a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 249221a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 249321a852b0SQu Wenruo btrfs_super_log_root(sb)); 249421a852b0SQu Wenruo ret = -EINVAL; 249521a852b0SQu Wenruo } 249621a852b0SQu Wenruo 2497de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24987239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 249921a852b0SQu Wenruo btrfs_err(fs_info, 25007239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2501de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 250221a852b0SQu Wenruo ret = -EINVAL; 250321a852b0SQu Wenruo } 250421a852b0SQu Wenruo 250521a852b0SQu Wenruo /* 250621a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 250721a852b0SQu Wenruo * done later 250821a852b0SQu Wenruo */ 250921a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 251021a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 251121a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 251221a852b0SQu Wenruo ret = -EINVAL; 251321a852b0SQu Wenruo } 251421a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 251521a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 251621a852b0SQu Wenruo btrfs_super_stripesize(sb)); 251721a852b0SQu Wenruo ret = -EINVAL; 251821a852b0SQu Wenruo } 251921a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 252021a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 252121a852b0SQu Wenruo btrfs_super_num_devices(sb)); 252221a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 252321a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 252421a852b0SQu Wenruo ret = -EINVAL; 252521a852b0SQu Wenruo } 252621a852b0SQu Wenruo 2527069ec957SQu Wenruo if (mirror_num >= 0 && 2528069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 252921a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 253021a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 253121a852b0SQu Wenruo ret = -EINVAL; 253221a852b0SQu Wenruo } 253321a852b0SQu Wenruo 253421a852b0SQu Wenruo /* 253521a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 253621a852b0SQu Wenruo * and one chunk 253721a852b0SQu Wenruo */ 253821a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 253921a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 254021a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 254121a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 254221a852b0SQu Wenruo ret = -EINVAL; 254321a852b0SQu Wenruo } 254421a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 254521a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 254621a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 254721a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 254821a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 254921a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 255021a852b0SQu Wenruo ret = -EINVAL; 255121a852b0SQu Wenruo } 255221a852b0SQu Wenruo 255321a852b0SQu Wenruo /* 255421a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 255521a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 255621a852b0SQu Wenruo */ 255721a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 255821a852b0SQu Wenruo btrfs_warn(fs_info, 255921a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 256021a852b0SQu Wenruo btrfs_super_generation(sb), 256121a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 256221a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 256321a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 256421a852b0SQu Wenruo btrfs_warn(fs_info, 256521a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 256621a852b0SQu Wenruo btrfs_super_generation(sb), 256721a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 256821a852b0SQu Wenruo 256921a852b0SQu Wenruo return ret; 257021a852b0SQu Wenruo } 257121a852b0SQu Wenruo 2572069ec957SQu Wenruo /* 2573069ec957SQu Wenruo * Validation of super block at mount time. 2574069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2575069ec957SQu Wenruo * flags will be skipped. 2576069ec957SQu Wenruo */ 2577069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2578069ec957SQu Wenruo { 2579069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2580069ec957SQu Wenruo } 2581069ec957SQu Wenruo 258275cb857dSQu Wenruo /* 258375cb857dSQu Wenruo * Validation of super block at write time. 258475cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 258575cb857dSQu Wenruo * overwritten soon. 258675cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 258775cb857dSQu Wenruo */ 258875cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 258975cb857dSQu Wenruo struct btrfs_super_block *sb) 259075cb857dSQu Wenruo { 259175cb857dSQu Wenruo int ret; 259275cb857dSQu Wenruo 259375cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 259475cb857dSQu Wenruo if (ret < 0) 259575cb857dSQu Wenruo goto out; 2596e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 259775cb857dSQu Wenruo ret = -EUCLEAN; 259875cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 259975cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 260075cb857dSQu Wenruo goto out; 260175cb857dSQu Wenruo } 260275cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 260375cb857dSQu Wenruo ret = -EUCLEAN; 260475cb857dSQu Wenruo btrfs_err(fs_info, 260575cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 260675cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 260775cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 260875cb857dSQu Wenruo goto out; 260975cb857dSQu Wenruo } 261075cb857dSQu Wenruo out: 261175cb857dSQu Wenruo if (ret < 0) 261275cb857dSQu Wenruo btrfs_err(fs_info, 261375cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 261475cb857dSQu Wenruo return ret; 261575cb857dSQu Wenruo } 261675cb857dSQu Wenruo 2617ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2618dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2619dfe25020SChris Mason char *options) 2620eb60ceacSChris Mason { 2621db94535dSChris Mason u32 sectorsize; 2622db94535dSChris Mason u32 nodesize; 262387ee04ebSChris Mason u32 stripesize; 262484234f3aSYan Zheng u64 generation; 2625f2b636e8SJosef Bacik u64 features; 262651bce6c9SJohannes Thumshirn u16 csum_type; 26273de4586cSChris Mason struct btrfs_key location; 2628a061fc8dSChris Mason struct buffer_head *bh; 26294d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2630815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2631f84a8bd6SAl Viro struct btrfs_root *tree_root; 26324d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2633eb60ceacSChris Mason int ret; 2634e58ca020SYan int err = -EINVAL; 2635af31f5e5SChris Mason int num_backups_tried = 0; 2636af31f5e5SChris Mason int backup_index = 0; 26376675df31SOmar Sandoval int clear_free_space_tree = 0; 2638581c1760SQu Wenruo int level; 26394543df7eSChris Mason 264074e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 264174e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2642cb517eabSMiao Xie if (!tree_root || !chunk_root) { 264339279cc3SChris Mason err = -ENOMEM; 264439279cc3SChris Mason goto fail; 264539279cc3SChris Mason } 264676dda93cSYan, Zheng 264776dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 264876dda93cSYan, Zheng if (ret) { 264976dda93cSYan, Zheng err = ret; 265076dda93cSYan, Zheng goto fail; 265176dda93cSYan, Zheng } 265276dda93cSYan, Zheng 26534297ff84SJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2654e2d84521SMiao Xie if (ret) { 2655e2d84521SMiao Xie err = ret; 26569e11ceeeSJan Kara goto fail_srcu; 2657e2d84521SMiao Xie } 26584297ff84SJosef Bacik 26594297ff84SJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 26604297ff84SJosef Bacik if (ret) { 26614297ff84SJosef Bacik err = ret; 26624297ff84SJosef Bacik goto fail_dio_bytes; 26634297ff84SJosef Bacik } 266409cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2665e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2666e2d84521SMiao Xie 2667908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2668963d678bSMiao Xie if (ret) { 2669963d678bSMiao Xie err = ret; 2670963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2671963d678bSMiao Xie } 2672963d678bSMiao Xie 26737f8d236aSDavid Sterba ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 26747f8d236aSDavid Sterba GFP_KERNEL); 2675c404e0dcSMiao Xie if (ret) { 2676c404e0dcSMiao Xie err = ret; 2677c404e0dcSMiao Xie goto fail_delalloc_bytes; 2678c404e0dcSMiao Xie } 2679c404e0dcSMiao Xie 268076dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2681f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26828fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2683facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 268424bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2685eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 268611833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2687eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2688a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 268976dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 269024bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26914cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2692f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2693ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2694f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 269547ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2696f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2697d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 269867c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26997585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2700573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2701de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 270219c00ddcSChris Mason 27030b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27046324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2705f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 270647ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 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); 2723e20d96d6SChris Mason fs_info->sb = sb; 272495ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27259ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27264cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2727a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2728f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27298b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2730f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 273190519d66SArne Jansen /* readahead state */ 2732d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 273390519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2734fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2735c8b97818SChris Mason 2736b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2737b34b086cSChris Mason num_online_cpus() + 2, 8); 27380afbaf8cSChris Mason 2739199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2740199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 274169fe2d75SJosef Bacik 274269fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 274369fe2d75SJosef Bacik if (!fs_info->btree_inode) { 274469fe2d75SJosef Bacik err = -ENOMEM; 274569fe2d75SJosef Bacik goto fail_bio_counter; 274669fe2d75SJosef Bacik } 274769fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 274869fe2d75SJosef Bacik 274916cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 275074e4d827SDavid Sterba GFP_KERNEL); 275116cdcec7SMiao Xie if (!fs_info->delayed_root) { 275216cdcec7SMiao Xie err = -ENOMEM; 275316cdcec7SMiao Xie goto fail_iput; 275416cdcec7SMiao Xie } 275516cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 27563eaa2885SChris Mason 2757638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 275821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 275921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 276021adbd5cSStefan Behrens #endif 2761779a65a4SEric Sandeen btrfs_init_balance(fs_info); 276221c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2763a2de733cSArne Jansen 27649f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 27659f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2766a061fc8dSChris Mason 27676bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 276839279cc3SChris Mason 2769e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 27706bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2771a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2772925baeddSChris Mason 277343eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[0], 277443eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS0, NULL); 277543eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[1], 277643eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS1, NULL); 277711833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2778afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2779509659cdSChris Mason 27805a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 278179154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2782925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2783a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2784a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27851bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27869e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2787c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 278876dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2789803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2790fa9c0d79SChris Mason 2791ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2792f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2793416ac51dSArne Jansen 2794fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2795fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2796fa9c0d79SChris Mason 2797e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2798f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2799bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 28004854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2801034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28023768f368SChris Mason 2803da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2804da17066cSJeff Mahoney fs_info->nodesize = 4096; 2805da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2806da17066cSJeff Mahoney fs_info->stripesize = 4096; 2807da17066cSJeff Mahoney 2808eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2809eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2810eede2bf3SOmar Sandoval 28119e967495SFilipe Manana fs_info->send_in_progress = 0; 28129e967495SFilipe Manana 281353b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 281453b381b3SDavid Woodhouse if (ret) { 281583c8266aSDavid Sterba err = ret; 281653b381b3SDavid Woodhouse goto fail_alloc; 281753b381b3SDavid Woodhouse } 281853b381b3SDavid Woodhouse 2819da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 28207eccb903SChris Mason 28213c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 28221104a885SDavid Sterba 28231104a885SDavid Sterba /* 28241104a885SDavid Sterba * Read super block and check the signature bytes only 28251104a885SDavid Sterba */ 2826a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 282792fc03fbSAnand Jain if (IS_ERR(bh)) { 282892fc03fbSAnand Jain err = PTR_ERR(bh); 282916cdcec7SMiao Xie goto fail_alloc; 283020b45077SDave Young } 283139279cc3SChris Mason 28321104a885SDavid Sterba /* 28338dc3f22cSJohannes Thumshirn * Verify the type first, if that or the the checksum value are 28348dc3f22cSJohannes Thumshirn * corrupted, we'll find out 28358dc3f22cSJohannes Thumshirn */ 283651bce6c9SJohannes Thumshirn csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data); 283751bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 28388dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 283951bce6c9SJohannes Thumshirn csum_type); 28408dc3f22cSJohannes Thumshirn err = -EINVAL; 28418dc3f22cSJohannes Thumshirn brelse(bh); 28428dc3f22cSJohannes Thumshirn goto fail_alloc; 28438dc3f22cSJohannes Thumshirn } 28448dc3f22cSJohannes Thumshirn 28456d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 28466d97c6e3SJohannes Thumshirn if (ret) { 28476d97c6e3SJohannes Thumshirn err = ret; 28486d97c6e3SJohannes Thumshirn goto fail_alloc; 28496d97c6e3SJohannes Thumshirn } 28506d97c6e3SJohannes Thumshirn 28518dc3f22cSJohannes Thumshirn /* 28521104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 28531104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 28541104a885SDavid Sterba */ 2855ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 285605135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 28571104a885SDavid Sterba err = -EINVAL; 2858b2acdddfSAnand Jain brelse(bh); 28596d97c6e3SJohannes Thumshirn goto fail_csum; 28601104a885SDavid Sterba } 28611104a885SDavid Sterba 28621104a885SDavid Sterba /* 28631104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 28641104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 28651104a885SDavid Sterba * the whole block of INFO_SIZE 28661104a885SDavid Sterba */ 28676c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 2868a061fc8dSChris Mason brelse(bh); 28695f39d397SChris Mason 2870fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2871fbc6feaeSNikolay Borisov 2872de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2873de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2874de37aa51SNikolay Borisov 28757239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2876de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2877de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2878de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 28797239ff4bSNikolay Borisov } 28800b86a832SChris Mason 2881fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2882fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2883fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2884fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2885fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2886fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2887fbc6feaeSNikolay Borisov } 2888fbc6feaeSNikolay Borisov 2889fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2890fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2891de37aa51SNikolay Borisov 2892069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 28931104a885SDavid Sterba if (ret) { 289405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 28951104a885SDavid Sterba err = -EINVAL; 28966d97c6e3SJohannes Thumshirn goto fail_csum; 28971104a885SDavid Sterba } 28981104a885SDavid Sterba 28990f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 29006d97c6e3SJohannes Thumshirn goto fail_csum; 29010f7d52f4SChris Mason 2902acce952bSliubo /* check FS state, whether FS is broken. */ 290387533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 290487533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2905acce952bSliubo 290675e7cb7fSLiu Bo /* 2907af31f5e5SChris Mason * run through our array of backup supers and setup 2908af31f5e5SChris Mason * our ring pointer to the oldest one 2909af31f5e5SChris Mason */ 2910af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2911af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2912af31f5e5SChris Mason 2913af31f5e5SChris Mason /* 291475e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 291575e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 291675e7cb7fSLiu Bo */ 291775e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 291875e7cb7fSLiu Bo 29192ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 29202b82032cSYan Zheng if (ret) { 29212b82032cSYan Zheng err = ret; 29226d97c6e3SJohannes Thumshirn goto fail_csum; 29232b82032cSYan Zheng } 2924dfe25020SChris Mason 2925f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2926f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2927f2b636e8SJosef Bacik if (features) { 292805135f59SDavid Sterba btrfs_err(fs_info, 292905135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2930c1c9ff7cSGeert Uytterhoeven features); 2931f2b636e8SJosef Bacik err = -EINVAL; 29326d97c6e3SJohannes Thumshirn goto fail_csum; 2933f2b636e8SJosef Bacik } 2934f2b636e8SJosef Bacik 29355d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 29365d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 29370b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2938a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 29395c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 29405c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2941727011e0SChris Mason 29423173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 294305135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 29443173a18fSJosef Bacik 2945727011e0SChris Mason /* 2946727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2947727011e0SChris Mason * they are bigger than the page size 2948727011e0SChris Mason */ 294909cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2950727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 295105135f59SDavid Sterba btrfs_info(fs_info, 295205135f59SDavid Sterba "flagging fs with big metadata feature"); 2953727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2954727011e0SChris Mason } 2955727011e0SChris Mason 2956bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2957bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2958b7f67055SChandan Rajendra stripesize = sectorsize; 2959707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2960963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2961bc3f116fSChris Mason 2962da17066cSJeff Mahoney /* Cache block sizes */ 2963da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2964da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2965da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2966da17066cSJeff Mahoney 2967bc3f116fSChris Mason /* 2968bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2969bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2970bc3f116fSChris Mason */ 2971bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2972707e8a07SDavid Sterba (sectorsize != nodesize)) { 297305135f59SDavid Sterba btrfs_err(fs_info, 297405135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 297505135f59SDavid Sterba nodesize, sectorsize); 29766d97c6e3SJohannes Thumshirn goto fail_csum; 2977bc3f116fSChris Mason } 2978bc3f116fSChris Mason 2979ceda0864SMiao Xie /* 2980ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2981ceda0864SMiao Xie * update the flag. 2982ceda0864SMiao Xie */ 29835d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 29845d4f98a2SYan Zheng 2985f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2986f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2987bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 298805135f59SDavid Sterba btrfs_err(fs_info, 298905135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2990c1c9ff7cSGeert Uytterhoeven features); 2991f2b636e8SJosef Bacik err = -EINVAL; 29926d97c6e3SJohannes Thumshirn goto fail_csum; 2993f2b636e8SJosef Bacik } 299461d92c32SChris Mason 29952a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 29962a458198SEric Sandeen if (ret) { 29972a458198SEric Sandeen err = ret; 29980dc3b84aSJosef Bacik goto fail_sb_buffer; 29990dc3b84aSJosef Bacik } 30004543df7eSChris Mason 30019e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 30029e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 30039e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 3004b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 30059e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 30069e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 30074575c9ccSChris Mason 3008a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3009a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3010de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3011db94535dSChris Mason 3012925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 30136bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3014925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 301584eed90fSChris Mason if (ret) { 301605135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 30175d4f98a2SYan Zheng goto fail_sb_buffer; 301884eed90fSChris Mason } 30190b86a832SChris Mason 302084234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3021581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 30220b86a832SChris Mason 3023da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 30240b86a832SChris Mason 30252ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 30260b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3027581c1760SQu Wenruo generation, level, NULL); 302864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 302964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 303005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3031e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3032e5fffbacSchandan free_extent_buffer(chunk_root->node); 303395ab1f64SZhao Lei chunk_root->node = NULL; 3034af31f5e5SChris Mason goto fail_tree_roots; 303583121942SDavid Woodhouse } 30365d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 30375d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 30380b86a832SChris Mason 3039e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3040b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 3041e17cade2SChris Mason 30425b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 30432b82032cSYan Zheng if (ret) { 304405135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3045af31f5e5SChris Mason goto fail_tree_roots; 30462b82032cSYan Zheng } 30470b86a832SChris Mason 30488dabb742SStefan Behrens /* 30499b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 30509b99b115SAnand Jain * device replace procedure 30518dabb742SStefan Behrens */ 30529b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3053dfe25020SChris Mason 3054a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 305505135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3056a6b0d5c8SChris Mason goto fail_tree_roots; 3057a6b0d5c8SChris Mason } 3058a6b0d5c8SChris Mason 3059af31f5e5SChris Mason retry_root_backup: 306084234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 3061581c1760SQu Wenruo level = btrfs_super_root_level(disk_super); 30620b86a832SChris Mason 30632ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 3064db94535dSChris Mason btrfs_super_root(disk_super), 3065581c1760SQu Wenruo generation, level, NULL); 306664c043deSLiu Bo if (IS_ERR(tree_root->node) || 306764c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 306805135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 3069e5fffbacSchandan if (!IS_ERR(tree_root->node)) 3070e5fffbacSchandan free_extent_buffer(tree_root->node); 307195ab1f64SZhao Lei tree_root->node = NULL; 3072af31f5e5SChris Mason goto recovery_tree_root; 307383121942SDavid Woodhouse } 3074af31f5e5SChris Mason 30755d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 30765d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 307769e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 3078db94535dSChris Mason 3079f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 3080f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 3081f32e48e9SChandan Rajendra &tree_root->highest_objectid); 3082f32e48e9SChandan Rajendra if (ret) { 3083f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 3084f32e48e9SChandan Rajendra goto recovery_tree_root; 3085f32e48e9SChandan Rajendra } 3086f32e48e9SChandan Rajendra 3087f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 3088f32e48e9SChandan Rajendra 3089f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 3090f32e48e9SChandan Rajendra 30916bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 30924bbcaa64SEric Sandeen if (ret) 3093af31f5e5SChris Mason goto recovery_tree_root; 3094f7a81ea4SStefan Behrens 30958929ecfaSYan, Zheng fs_info->generation = generation; 30968929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 30978929ecfaSYan, Zheng 3098cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3099cf90d884SQu Wenruo if (ret) { 3100cf90d884SQu Wenruo btrfs_err(fs_info, 3101cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3102cf90d884SQu Wenruo ret); 3103cf90d884SQu Wenruo goto fail_block_groups; 3104cf90d884SQu Wenruo } 310568310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 310668310a5eSIlya Dryomov if (ret) { 310705135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 310868310a5eSIlya Dryomov goto fail_block_groups; 310968310a5eSIlya Dryomov } 311068310a5eSIlya Dryomov 3111733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3112733f4fbbSStefan Behrens if (ret) { 311305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3114733f4fbbSStefan Behrens goto fail_block_groups; 3115733f4fbbSStefan Behrens } 3116733f4fbbSStefan Behrens 31178dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 31188dabb742SStefan Behrens if (ret) { 311905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 31208dabb742SStefan Behrens goto fail_block_groups; 31218dabb742SStefan Behrens } 31228dabb742SStefan Behrens 31239b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 31248dabb742SStefan Behrens 3125b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 3126b7c35e81SAnand Jain if (ret) { 312705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 312805135f59SDavid Sterba ret); 3129b7c35e81SAnand Jain goto fail_block_groups; 3130b7c35e81SAnand Jain } 3131b7c35e81SAnand Jain 3132b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 3133b7c35e81SAnand Jain if (ret) { 313405135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 313505135f59SDavid Sterba ret); 3136b7c35e81SAnand Jain goto fail_fsdev_sysfs; 3137b7c35e81SAnand Jain } 3138b7c35e81SAnand Jain 313996f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31405ac1d209SJeff Mahoney if (ret) { 314105135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3142b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31435ac1d209SJeff Mahoney } 31445ac1d209SJeff Mahoney 3145c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3146c59021f8Sliubo if (ret) { 314705135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31482365dd3cSAnand Jain goto fail_sysfs; 3149c59021f8Sliubo } 3150c59021f8Sliubo 31515b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31521b1d1f66SJosef Bacik if (ret) { 315305135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 31542365dd3cSAnand Jain goto fail_sysfs; 31551b1d1f66SJosef Bacik } 31564330e183SQu Wenruo 31576528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 315805135f59SDavid Sterba btrfs_warn(fs_info, 315952042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 31602365dd3cSAnand Jain goto fail_sysfs; 3161292fd7fcSStefan Behrens } 31629078a3e1SChris Mason 3163a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3164a74a4b97SChris Mason "btrfs-cleaner"); 316557506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 31662365dd3cSAnand Jain goto fail_sysfs; 3167a74a4b97SChris Mason 3168a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3169a74a4b97SChris Mason tree_root, 3170a74a4b97SChris Mason "btrfs-transaction"); 317157506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 31723f157a2fSChris Mason goto fail_cleaner; 3173a74a4b97SChris Mason 3174583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3175c289811cSChris Mason !fs_info->fs_devices->rotating) { 3176583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3177c289811cSChris Mason } 3178c289811cSChris Mason 3179572d9ab7SDavid Sterba /* 318001327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3181572d9ab7SDavid Sterba * not have to wait for transaction commit 3182572d9ab7SDavid Sterba */ 3183572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 31843818aea2SQu Wenruo 318521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 31860b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 31872ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 31880b246afaSJeff Mahoney btrfs_test_opt(fs_info, 318921adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 319021adbd5cSStefan Behrens 1 : 0, 319121adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 319221adbd5cSStefan Behrens if (ret) 319305135f59SDavid Sterba btrfs_warn(fs_info, 319405135f59SDavid Sterba "failed to initialize integrity check module: %d", 319505135f59SDavid Sterba ret); 319621adbd5cSStefan Behrens } 319721adbd5cSStefan Behrens #endif 3198bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3199bcef60f2SArne Jansen if (ret) 3200bcef60f2SArne Jansen goto fail_trans_kthread; 320121adbd5cSStefan Behrens 3202fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3203fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3204fd708b81SJosef Bacik 320596da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 320696da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32070b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 320863443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 320979787eaaSJeff Mahoney if (ret) { 321063443bf5SEric Sandeen err = ret; 321128c16cbbSWang Shilong goto fail_qgroup; 3212e556ce2cSYan Zheng } 3213e02119d5SChris Mason } 32141a40e23bSZheng Yan 32156bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 321679787eaaSJeff Mahoney if (ret) 321728c16cbbSWang Shilong goto fail_qgroup; 321876dda93cSYan, Zheng 3219bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3220d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 322144c44af2SIlya Dryomov if (ret) 322228c16cbbSWang Shilong goto fail_qgroup; 322390c711abSZygo Blaxell 322490c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 32255d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 322690c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3227d7ce5843SMiao Xie if (ret < 0) { 322805135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 322905135f59SDavid Sterba ret); 3230d7ce5843SMiao Xie err = -EINVAL; 3231bcef60f2SArne Jansen goto fail_qgroup; 3232d7ce5843SMiao Xie } 32337c2ca468SChris Mason } 32341a40e23bSZheng Yan 32353de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32363de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3237cb517eabSMiao Xie location.offset = 0; 32383de4586cSChris Mason 32393de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 32403140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32413140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3242f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3243bcef60f2SArne Jansen goto fail_qgroup; 32443140c9a3SDan Carpenter } 3245c289811cSChris Mason 3246bc98a42cSDavid Howells if (sb_rdonly(sb)) 32472b6ba629SIlya Dryomov return 0; 32482b6ba629SIlya Dryomov 3249f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3250f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32516675df31SOmar Sandoval clear_free_space_tree = 1; 32526675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32536675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32546675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 32556675df31SOmar Sandoval clear_free_space_tree = 1; 32566675df31SOmar Sandoval } 32576675df31SOmar Sandoval 32586675df31SOmar Sandoval if (clear_free_space_tree) { 3259f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3260f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3261f8d468a1SOmar Sandoval if (ret) { 3262f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3263f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 32646bccf3abSJeff Mahoney close_ctree(fs_info); 3265f8d468a1SOmar Sandoval return ret; 3266f8d468a1SOmar Sandoval } 3267f8d468a1SOmar Sandoval } 3268f8d468a1SOmar Sandoval 32690b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3270511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 327105135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3272511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3273511711afSChris Mason if (ret) { 327405135f59SDavid Sterba btrfs_warn(fs_info, 327505135f59SDavid Sterba "failed to create free space tree: %d", ret); 32766bccf3abSJeff Mahoney close_ctree(fs_info); 3277511711afSChris Mason return ret; 3278511711afSChris Mason } 3279511711afSChris Mason } 3280511711afSChris Mason 3281e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 32822b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 32832b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 32842b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 32856bccf3abSJeff Mahoney close_ctree(fs_info); 32862b6ba629SIlya Dryomov return ret; 32872b6ba629SIlya Dryomov } 3288e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 328959641015SIlya Dryomov 32902b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 32912b6ba629SIlya Dryomov if (ret) { 329205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 32936bccf3abSJeff Mahoney close_ctree(fs_info); 32942b6ba629SIlya Dryomov return ret; 3295e3acc2a6SJosef Bacik } 3296e3acc2a6SJosef Bacik 32978dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 32988dabb742SStefan Behrens if (ret) { 329905135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33006bccf3abSJeff Mahoney close_ctree(fs_info); 33018dabb742SStefan Behrens return ret; 33028dabb742SStefan Behrens } 33038dabb742SStefan Behrens 3304b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3305b382a324SJan Schmidt 33064bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 330705135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3308f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3309f7a81ea4SStefan Behrens if (ret) { 331005135f59SDavid Sterba btrfs_warn(fs_info, 331105135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33126bccf3abSJeff Mahoney close_ctree(fs_info); 3313f7a81ea4SStefan Behrens return ret; 3314f7a81ea4SStefan Behrens } 33150b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33164bbcaa64SEric Sandeen fs_info->generation != 33174bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 331805135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 331970f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 332070f80175SStefan Behrens if (ret) { 332105135f59SDavid Sterba btrfs_warn(fs_info, 332205135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 33236bccf3abSJeff Mahoney close_ctree(fs_info); 332470f80175SStefan Behrens return ret; 332570f80175SStefan Behrens } 332670f80175SStefan Behrens } else { 3327afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3328f7a81ea4SStefan Behrens } 3329afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 333047ab2a6cSJosef Bacik 33318dcddfa0SQu Wenruo /* 33328dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33338dcddfa0SQu Wenruo * no need to keep the flag 33348dcddfa0SQu Wenruo */ 33358dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33368dcddfa0SQu Wenruo 3337ad2b2c80SAl Viro return 0; 333839279cc3SChris Mason 3339bcef60f2SArne Jansen fail_qgroup: 3340bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33417c2ca468SChris Mason fail_trans_kthread: 33427c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33432ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3344faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33453f157a2fSChris Mason fail_cleaner: 3346a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33477c2ca468SChris Mason 33487c2ca468SChris Mason /* 33497c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 33507c2ca468SChris Mason * kthreads 33517c2ca468SChris Mason */ 33527c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 33537c2ca468SChris Mason 33542365dd3cSAnand Jain fail_sysfs: 33556618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 33562365dd3cSAnand Jain 3357b7c35e81SAnand Jain fail_fsdev_sysfs: 3358b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3359b7c35e81SAnand Jain 33601b1d1f66SJosef Bacik fail_block_groups: 336154067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3362af31f5e5SChris Mason 3363af31f5e5SChris Mason fail_tree_roots: 3364af31f5e5SChris Mason free_root_pointers(fs_info, 1); 33652b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3366af31f5e5SChris Mason 336739279cc3SChris Mason fail_sb_buffer: 33687abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 33695cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 33706d97c6e3SJohannes Thumshirn fail_csum: 33716d97c6e3SJohannes Thumshirn btrfs_free_csum_hash(fs_info); 337216cdcec7SMiao Xie fail_alloc: 33734543df7eSChris Mason fail_iput: 3374586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3375586e46e2SIlya Dryomov 33764543df7eSChris Mason iput(fs_info->btree_inode); 3377c404e0dcSMiao Xie fail_bio_counter: 33787f8d236aSDavid Sterba percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 3379963d678bSMiao Xie fail_delalloc_bytes: 3380963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3381e2d84521SMiao Xie fail_dirty_metadata_bytes: 3382e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 33834297ff84SJosef Bacik fail_dio_bytes: 33844297ff84SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 338576dda93cSYan, Zheng fail_srcu: 338676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 33877e662854SQinghuang Feng fail: 338853b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3389586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3390ad2b2c80SAl Viro return err; 3391af31f5e5SChris Mason 3392af31f5e5SChris Mason recovery_tree_root: 33930b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3394af31f5e5SChris Mason goto fail_tree_roots; 3395af31f5e5SChris Mason 3396af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3397af31f5e5SChris Mason 3398af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3399af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3400af31f5e5SChris Mason 3401af31f5e5SChris Mason /* we can't trust the free space cache either */ 3402af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3403af31f5e5SChris Mason 3404af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3405af31f5e5SChris Mason &num_backups_tried, &backup_index); 3406af31f5e5SChris Mason if (ret == -1) 3407af31f5e5SChris Mason goto fail_block_groups; 3408af31f5e5SChris Mason goto retry_root_backup; 3409eb60ceacSChris Mason } 3410663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3411eb60ceacSChris Mason 3412f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3413f2984462SChris Mason { 3414f2984462SChris Mason if (uptodate) { 3415f2984462SChris Mason set_buffer_uptodate(bh); 3416f2984462SChris Mason } else { 3417442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3418442a4f63SStefan Behrens bh->b_private; 3419442a4f63SStefan Behrens 3420fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3421b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3422606686eeSJosef Bacik rcu_str_deref(device->name)); 342301327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 34241259ab75SChris Mason * our own ways of dealing with the IO errors 34251259ab75SChris Mason */ 3426f2984462SChris Mason clear_buffer_uptodate(bh); 3427442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3428f2984462SChris Mason } 3429f2984462SChris Mason unlock_buffer(bh); 3430f2984462SChris Mason put_bh(bh); 3431f2984462SChris Mason } 3432f2984462SChris Mason 343329c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 343429c36d72SAnand Jain struct buffer_head **bh_ret) 343529c36d72SAnand Jain { 343629c36d72SAnand Jain struct buffer_head *bh; 343729c36d72SAnand Jain struct btrfs_super_block *super; 343829c36d72SAnand Jain u64 bytenr; 343929c36d72SAnand Jain 344029c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 344129c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 344229c36d72SAnand Jain return -EINVAL; 344329c36d72SAnand Jain 34449f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 344529c36d72SAnand Jain /* 344629c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 344729c36d72SAnand Jain * the best option we have is to mark it EIO. 344829c36d72SAnand Jain */ 344929c36d72SAnand Jain if (!bh) 345029c36d72SAnand Jain return -EIO; 345129c36d72SAnand Jain 345229c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 345329c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 345429c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 345529c36d72SAnand Jain brelse(bh); 345629c36d72SAnand Jain return -EINVAL; 345729c36d72SAnand Jain } 345829c36d72SAnand Jain 345929c36d72SAnand Jain *bh_ret = bh; 346029c36d72SAnand Jain return 0; 346129c36d72SAnand Jain } 346229c36d72SAnand Jain 346329c36d72SAnand Jain 3464a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3465a512bbf8SYan Zheng { 3466a512bbf8SYan Zheng struct buffer_head *bh; 3467a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3468a512bbf8SYan Zheng struct btrfs_super_block *super; 3469a512bbf8SYan Zheng int i; 3470a512bbf8SYan Zheng u64 transid = 0; 347192fc03fbSAnand Jain int ret = -EINVAL; 3472a512bbf8SYan Zheng 3473a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3474a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3475a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3476a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3477a512bbf8SYan Zheng */ 3478a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 347929c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 348029c36d72SAnand Jain if (ret) 3481a512bbf8SYan Zheng continue; 3482a512bbf8SYan Zheng 3483a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3484a512bbf8SYan Zheng 3485a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3486a512bbf8SYan Zheng brelse(latest); 3487a512bbf8SYan Zheng latest = bh; 3488a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3489a512bbf8SYan Zheng } else { 3490a512bbf8SYan Zheng brelse(bh); 3491a512bbf8SYan Zheng } 3492a512bbf8SYan Zheng } 349392fc03fbSAnand Jain 349492fc03fbSAnand Jain if (!latest) 349592fc03fbSAnand Jain return ERR_PTR(ret); 349692fc03fbSAnand Jain 3497a512bbf8SYan Zheng return latest; 3498a512bbf8SYan Zheng } 3499a512bbf8SYan Zheng 35004eedeb75SHisashi Hifumi /* 3501abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3502abbb3b8eSDavid Sterba * buffer heads we write are pinned. 35034eedeb75SHisashi Hifumi * 3504abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3505abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3506abbb3b8eSDavid Sterba * same for write and wait phases. 35074eedeb75SHisashi Hifumi * 3508abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 35094eedeb75SHisashi Hifumi */ 3510a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3511abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3512a512bbf8SYan Zheng { 3513d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3514d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3515a512bbf8SYan Zheng struct buffer_head *bh; 3516a512bbf8SYan Zheng int i; 3517a512bbf8SYan Zheng int ret; 3518a512bbf8SYan Zheng int errors = 0; 3519a512bbf8SYan Zheng u64 bytenr; 35201b9e619cSOmar Sandoval int op_flags; 3521a512bbf8SYan Zheng 3522a512bbf8SYan Zheng if (max_mirrors == 0) 3523a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3524a512bbf8SYan Zheng 3525d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3526d5178578SJohannes Thumshirn 3527a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3528a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3529935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3530935e5cc9SMiao Xie device->commit_total_bytes) 3531a512bbf8SYan Zheng break; 3532a512bbf8SYan Zheng 3533a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3534a512bbf8SYan Zheng 3535d5178578SJohannes Thumshirn crypto_shash_init(shash); 3536d5178578SJohannes Thumshirn crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3537abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3538d5178578SJohannes Thumshirn crypto_shash_final(shash, sb->csum); 3539a512bbf8SYan Zheng 3540abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 35419f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3542a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3543634554dcSJosef Bacik if (!bh) { 3544fb456252SJeff Mahoney btrfs_err(device->fs_info, 3545f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3546f14d104dSDavid Sterba bytenr); 3547634554dcSJosef Bacik errors++; 3548634554dcSJosef Bacik continue; 3549634554dcSJosef Bacik } 3550634554dcSJosef Bacik 3551a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3552a512bbf8SYan Zheng 35534eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3554a512bbf8SYan Zheng get_bh(bh); 35554eedeb75SHisashi Hifumi 35564eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3557a512bbf8SYan Zheng lock_buffer(bh); 3558a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3559442a4f63SStefan Behrens bh->b_private = device; 3560a512bbf8SYan Zheng 3561387125fcSChris Mason /* 3562387125fcSChris Mason * we fua the first super. The others we allow 3563387125fcSChris Mason * to go down lazy. 3564387125fcSChris Mason */ 35651b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 35661b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 35671b9e619cSOmar Sandoval op_flags |= REQ_FUA; 35681b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 35694eedeb75SHisashi Hifumi if (ret) 3570a512bbf8SYan Zheng errors++; 3571a512bbf8SYan Zheng } 3572a512bbf8SYan Zheng return errors < i ? 0 : -1; 3573a512bbf8SYan Zheng } 3574a512bbf8SYan Zheng 3575387125fcSChris Mason /* 3576abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3577abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3578abbb3b8eSDavid Sterba * 3579abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3580abbb3b8eSDavid Sterba * date. 3581abbb3b8eSDavid Sterba */ 3582abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3583abbb3b8eSDavid Sterba { 3584abbb3b8eSDavid Sterba struct buffer_head *bh; 3585abbb3b8eSDavid Sterba int i; 3586abbb3b8eSDavid Sterba int errors = 0; 3587b6a535faSHoward McLauchlan bool primary_failed = false; 3588abbb3b8eSDavid Sterba u64 bytenr; 3589abbb3b8eSDavid Sterba 3590abbb3b8eSDavid Sterba if (max_mirrors == 0) 3591abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3592abbb3b8eSDavid Sterba 3593abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3594abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3595abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3596abbb3b8eSDavid Sterba device->commit_total_bytes) 3597abbb3b8eSDavid Sterba break; 3598abbb3b8eSDavid Sterba 35999f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 36009f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3601abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3602abbb3b8eSDavid Sterba if (!bh) { 3603abbb3b8eSDavid Sterba errors++; 3604b6a535faSHoward McLauchlan if (i == 0) 3605b6a535faSHoward McLauchlan primary_failed = true; 3606abbb3b8eSDavid Sterba continue; 3607abbb3b8eSDavid Sterba } 3608abbb3b8eSDavid Sterba wait_on_buffer(bh); 3609b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3610abbb3b8eSDavid Sterba errors++; 3611b6a535faSHoward McLauchlan if (i == 0) 3612b6a535faSHoward McLauchlan primary_failed = true; 3613b6a535faSHoward McLauchlan } 3614abbb3b8eSDavid Sterba 3615abbb3b8eSDavid Sterba /* drop our reference */ 3616abbb3b8eSDavid Sterba brelse(bh); 3617abbb3b8eSDavid Sterba 3618abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3619abbb3b8eSDavid Sterba brelse(bh); 3620abbb3b8eSDavid Sterba } 3621abbb3b8eSDavid Sterba 3622b6a535faSHoward McLauchlan /* log error, force error return */ 3623b6a535faSHoward McLauchlan if (primary_failed) { 3624b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3625b6a535faSHoward McLauchlan device->devid); 3626b6a535faSHoward McLauchlan return -1; 3627b6a535faSHoward McLauchlan } 3628b6a535faSHoward McLauchlan 3629abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3630abbb3b8eSDavid Sterba } 3631abbb3b8eSDavid Sterba 3632abbb3b8eSDavid Sterba /* 3633387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3634387125fcSChris Mason * for the barrier when it is done 3635387125fcSChris Mason */ 36364246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3637387125fcSChris Mason { 3638387125fcSChris Mason complete(bio->bi_private); 3639387125fcSChris Mason } 3640387125fcSChris Mason 3641387125fcSChris Mason /* 36424fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36434fc6441aSAnand Jain * done in the waiting counterpart. 3644387125fcSChris Mason */ 36454fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3646387125fcSChris Mason { 3647c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3648e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3649387125fcSChris Mason 3650c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36514fc6441aSAnand Jain return; 3652387125fcSChris Mason 3653e0ae9994SDavid Sterba bio_reset(bio); 3654387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 365574d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36568d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3657387125fcSChris Mason init_completion(&device->flush_wait); 3658387125fcSChris Mason bio->bi_private = &device->flush_wait; 3659387125fcSChris Mason 366043a01111SLu Fengqi btrfsic_submit_bio(bio); 36611c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36624fc6441aSAnand Jain } 3663387125fcSChris Mason 36644fc6441aSAnand Jain /* 36654fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36664fc6441aSAnand Jain */ 36678c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36684fc6441aSAnand Jain { 36694fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36704fc6441aSAnand Jain 36711c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 367258efbc9fSOmar Sandoval return BLK_STS_OK; 36734fc6441aSAnand Jain 36741c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36752980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 36764fc6441aSAnand Jain 36778c27cb35SLinus Torvalds return bio->bi_status; 3678387125fcSChris Mason } 3679387125fcSChris Mason 3680d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3681401b41e5SAnand Jain { 36826528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3683401b41e5SAnand Jain return -EIO; 3684387125fcSChris Mason return 0; 3685387125fcSChris Mason } 3686387125fcSChris Mason 3687387125fcSChris Mason /* 3688387125fcSChris Mason * send an empty flush down to each device in parallel, 3689387125fcSChris Mason * then wait for them 3690387125fcSChris Mason */ 3691387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3692387125fcSChris Mason { 3693387125fcSChris Mason struct list_head *head; 3694387125fcSChris Mason struct btrfs_device *dev; 36955af3e8ccSStefan Behrens int errors_wait = 0; 36964e4cbee9SChristoph Hellwig blk_status_t ret; 3697387125fcSChris Mason 36981538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3699387125fcSChris Mason /* send down all the barriers */ 3700387125fcSChris Mason head = &info->fs_devices->devices; 37011538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3702e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3703f88ba6a2SHidetoshi Seto continue; 3704cea7c8bfSAnand Jain if (!dev->bdev) 3705387125fcSChris Mason continue; 3706e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3707ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3708387125fcSChris Mason continue; 3709387125fcSChris Mason 37104fc6441aSAnand Jain write_dev_flush(dev); 371158efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3712387125fcSChris Mason } 3713387125fcSChris Mason 3714387125fcSChris Mason /* wait for all the barriers */ 37151538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3716e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3717f88ba6a2SHidetoshi Seto continue; 3718387125fcSChris Mason if (!dev->bdev) { 37195af3e8ccSStefan Behrens errors_wait++; 3720387125fcSChris Mason continue; 3721387125fcSChris Mason } 3722e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3723ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3724387125fcSChris Mason continue; 3725387125fcSChris Mason 37264fc6441aSAnand Jain ret = wait_dev_flush(dev); 3727401b41e5SAnand Jain if (ret) { 3728401b41e5SAnand Jain dev->last_flush_error = ret; 372966b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 373066b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 37315af3e8ccSStefan Behrens errors_wait++; 3732387125fcSChris Mason } 3733401b41e5SAnand Jain } 3734401b41e5SAnand Jain 3735cea7c8bfSAnand Jain if (errors_wait) { 3736401b41e5SAnand Jain /* 3737401b41e5SAnand Jain * At some point we need the status of all disks 3738401b41e5SAnand Jain * to arrive at the volume status. So error checking 3739401b41e5SAnand Jain * is being pushed to a separate loop. 3740401b41e5SAnand Jain */ 3741d10b82feSQu Wenruo return check_barrier_error(info); 3742401b41e5SAnand Jain } 3743387125fcSChris Mason return 0; 3744387125fcSChris Mason } 3745387125fcSChris Mason 3746943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3747943c6e99SZhao Lei { 37488789f4feSZhao Lei int raid_type; 37498789f4feSZhao Lei int min_tolerated = INT_MAX; 3750943c6e99SZhao Lei 37518789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37528789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37538c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37548789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37558789f4feSZhao Lei tolerated_failures); 3756943c6e99SZhao Lei 37578789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37588789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37598789f4feSZhao Lei continue; 376041a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37618789f4feSZhao Lei continue; 37628c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 37638789f4feSZhao Lei btrfs_raid_array[raid_type]. 37648789f4feSZhao Lei tolerated_failures); 37658789f4feSZhao Lei } 3766943c6e99SZhao Lei 37678789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3768ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37698789f4feSZhao Lei min_tolerated = 0; 37708789f4feSZhao Lei } 37718789f4feSZhao Lei 37728789f4feSZhao Lei return min_tolerated; 3773943c6e99SZhao Lei } 3774943c6e99SZhao Lei 3775eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3776f2984462SChris Mason { 3777e5e9a520SChris Mason struct list_head *head; 3778f2984462SChris Mason struct btrfs_device *dev; 3779a061fc8dSChris Mason struct btrfs_super_block *sb; 3780f2984462SChris Mason struct btrfs_dev_item *dev_item; 3781f2984462SChris Mason int ret; 3782f2984462SChris Mason int do_barriers; 3783a236aed1SChris Mason int max_errors; 3784a236aed1SChris Mason int total_errors = 0; 3785a061fc8dSChris Mason u64 flags; 3786f2984462SChris Mason 37870b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3788fed3b381SLiu Bo 3789fed3b381SLiu Bo /* 3790fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3791fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3792fed3b381SLiu Bo * been consistent on disk. 3793fed3b381SLiu Bo */ 3794fed3b381SLiu Bo if (max_mirrors == 0) 37950b246afaSJeff Mahoney backup_super_roots(fs_info); 3796f2984462SChris Mason 37970b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3798a061fc8dSChris Mason dev_item = &sb->dev_item; 3799e5e9a520SChris Mason 38000b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 38010b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 38020b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3803387125fcSChris Mason 38045af3e8ccSStefan Behrens if (do_barriers) { 38050b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 38065af3e8ccSStefan Behrens if (ret) { 38075af3e8ccSStefan Behrens mutex_unlock( 38080b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38090b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38105af3e8ccSStefan Behrens "errors while submitting device barriers."); 38115af3e8ccSStefan Behrens return ret; 38125af3e8ccSStefan Behrens } 38135af3e8ccSStefan Behrens } 3814387125fcSChris Mason 38151538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3816dfe25020SChris Mason if (!dev->bdev) { 3817dfe25020SChris Mason total_errors++; 3818dfe25020SChris Mason continue; 3819dfe25020SChris Mason } 3820e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3821ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3822dfe25020SChris Mason continue; 3823dfe25020SChris Mason 38242b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3825a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3826a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 38277df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3828935e5cc9SMiao Xie dev->commit_total_bytes); 3829ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3830ce7213c7SMiao Xie dev->commit_bytes_used); 3831a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3832a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3833a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3834a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 38357239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38367239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3837a512bbf8SYan Zheng 3838a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3839a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3840f2984462SChris Mason 384175cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 384275cb857dSQu Wenruo if (ret < 0) { 384375cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 384475cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 384575cb857dSQu Wenruo "unexpected superblock corruption detected"); 384675cb857dSQu Wenruo return -EUCLEAN; 384775cb857dSQu Wenruo } 384875cb857dSQu Wenruo 3849abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3850a236aed1SChris Mason if (ret) 3851a236aed1SChris Mason total_errors++; 3852f2984462SChris Mason } 3853a236aed1SChris Mason if (total_errors > max_errors) { 38540b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3855d397712bSChris Mason total_errors); 38560b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 385779787eaaSJeff Mahoney 38589d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38590b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38600b246afaSJeff Mahoney "%d errors while writing supers", 38610b246afaSJeff Mahoney total_errors); 38629d565ba4SStefan Behrens return -EIO; 3863a236aed1SChris Mason } 3864f2984462SChris Mason 3865a512bbf8SYan Zheng total_errors = 0; 38661538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3867dfe25020SChris Mason if (!dev->bdev) 3868dfe25020SChris Mason continue; 3869e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3870ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3871dfe25020SChris Mason continue; 3872dfe25020SChris Mason 3873abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3874a512bbf8SYan Zheng if (ret) 38751259ab75SChris Mason total_errors++; 3876f2984462SChris Mason } 38770b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3878a236aed1SChris Mason if (total_errors > max_errors) { 38790b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38800b246afaSJeff Mahoney "%d errors while writing supers", 38810b246afaSJeff Mahoney total_errors); 388279787eaaSJeff Mahoney return -EIO; 3883a236aed1SChris Mason } 3884f2984462SChris Mason return 0; 3885f2984462SChris Mason } 3886f2984462SChris Mason 3887cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3888cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3889cb517eabSMiao Xie struct btrfs_root *root) 38902619ba1fSChris Mason { 38914df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 38922619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 38932619ba1fSChris Mason (unsigned long)root->root_key.objectid); 38944df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 389576dda93cSYan, Zheng 389676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 389776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 389876dda93cSYan, Zheng 38991c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 39003321719eSLiu Bo btrfs_free_log(NULL, root); 39011c1ea4f7SLiu Bo if (root->reloc_root) { 39021c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 39031c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 39041c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 39051c1ea4f7SLiu Bo root->reloc_root = NULL; 39061c1ea4f7SLiu Bo } 39071c1ea4f7SLiu Bo } 39083321719eSLiu Bo 3909faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3910581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3911faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3912581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 391384db5ccfSDavid Sterba btrfs_free_fs_root(root); 39144df27c4dSYan, Zheng } 39154df27c4dSYan, Zheng 391684db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 39174df27c4dSYan, Zheng { 391857cdc8dbSDavid Sterba iput(root->ino_cache_inode); 39194df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 39200ee5dc67SAl Viro if (root->anon_dev) 39210ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 39228257b2dcSMiao Xie if (root->subv_writers) 39238257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 39245f39d397SChris Mason free_extent_buffer(root->node); 39255f39d397SChris Mason free_extent_buffer(root->commit_root); 3926581bb050SLi Zefan kfree(root->free_ino_ctl); 3927581bb050SLi Zefan kfree(root->free_ino_pinned); 3928b0feb9d9SMiao Xie btrfs_put_fs_root(root); 39292619ba1fSChris Mason } 39302619ba1fSChris Mason 3931c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3932c146afadSYan Zheng { 3933c146afadSYan Zheng u64 root_objectid = 0; 3934c146afadSYan Zheng struct btrfs_root *gang[8]; 393565d33fd7SQu Wenruo int i = 0; 393665d33fd7SQu Wenruo int err = 0; 393765d33fd7SQu Wenruo unsigned int ret = 0; 393865d33fd7SQu Wenruo int index; 3939c146afadSYan Zheng 3940c146afadSYan Zheng while (1) { 394165d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3942c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3943c146afadSYan Zheng (void **)gang, root_objectid, 3944c146afadSYan Zheng ARRAY_SIZE(gang)); 394565d33fd7SQu Wenruo if (!ret) { 394665d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3947c146afadSYan Zheng break; 394865d33fd7SQu Wenruo } 39495d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 395066b4ffd1SJosef Bacik 395165d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 395265d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 395365d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 395465d33fd7SQu Wenruo gang[i] = NULL; 395565d33fd7SQu Wenruo continue; 395665d33fd7SQu Wenruo } 395765d33fd7SQu Wenruo /* grab all the search result for later use */ 395865d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 395965d33fd7SQu Wenruo } 396065d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 396165d33fd7SQu Wenruo 396265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 396365d33fd7SQu Wenruo if (!gang[i]) 396465d33fd7SQu Wenruo continue; 3965c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 396666b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 396766b4ffd1SJosef Bacik if (err) 396865d33fd7SQu Wenruo break; 396965d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3970c146afadSYan Zheng } 3971c146afadSYan Zheng root_objectid++; 3972c146afadSYan Zheng } 397365d33fd7SQu Wenruo 397465d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 397565d33fd7SQu Wenruo for (; i < ret; i++) { 397665d33fd7SQu Wenruo if (gang[i]) 397765d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 397865d33fd7SQu Wenruo } 397965d33fd7SQu Wenruo return err; 3980c146afadSYan Zheng } 3981c146afadSYan Zheng 39826bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3983c146afadSYan Zheng { 39846bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3985c146afadSYan Zheng struct btrfs_trans_handle *trans; 3986c146afadSYan Zheng 39870b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39882ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39890b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39900b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3991c71bf099SYan, Zheng 3992c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39930b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39940b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3995c71bf099SYan, Zheng 39967a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39973612b495STsutomu Itoh if (IS_ERR(trans)) 39983612b495STsutomu Itoh return PTR_ERR(trans); 39993a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4000c146afadSYan Zheng } 4001c146afadSYan Zheng 40026bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 4003eb60ceacSChris Mason { 4004c146afadSYan Zheng int ret; 4005e089f05cSChris Mason 4006afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4007d6fd0ae2SOmar Sandoval /* 4008d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4009d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4010d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4011d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4012d6fd0ae2SOmar Sandoval */ 4013d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4014a2135011SChris Mason 40157343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4016d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40177343dd61SJustin Maggard 4018803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4019803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4020803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4021803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4022803b2f54SStefan Behrens 4023837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4024aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4025837d5b6eSIlya Dryomov 40268dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40278dabb742SStefan Behrens 4028aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 40294cb5300bSChris Mason 40304cb5300bSChris Mason /* wait for any defraggers to finish */ 40314cb5300bSChris Mason wait_event(fs_info->transaction_wait, 40324cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 40334cb5300bSChris Mason 40344cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 403526176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40364cb5300bSChris Mason 403721c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 403821c7e756SMiao Xie 4039bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4040e44163e1SJeff Mahoney /* 4041d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4042d6fd0ae2SOmar Sandoval * unused block groups. 4043e44163e1SJeff Mahoney */ 40440b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4045e44163e1SJeff Mahoney 40466bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4047d397712bSChris Mason if (ret) 404804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4049c146afadSYan Zheng } 4050ed2ff2cbSChris Mason 4051af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4052af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40532ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4054acce952bSliubo 4055e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4056e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40578929ecfaSYan, Zheng 4058e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4059afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4060f25784b3SYan Zheng 406104892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4062fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4063bcef60f2SArne Jansen 4064963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 406504892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4066963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4067b0c68f8bSChris Mason } 406831153d81SYan Zheng 40694297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 40704297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 40714297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 40724297ff84SJosef Bacik 40736618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4074b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 40755ac1d209SJeff Mahoney 4076faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4077d10c5f31SChris Mason 40781a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 40791a4319ccSLiu Bo 4080de348ee0SWang Shilong /* 4081de348ee0SWang Shilong * we must make sure there is not any read request to 4082de348ee0SWang Shilong * submit after we stopping all workers. 4083de348ee0SWang Shilong */ 4084de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 408596192499SJosef Bacik btrfs_stop_all_workers(fs_info); 408696192499SJosef Bacik 40875cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 40885cdd7db6SFilipe Manana 4089afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 409013e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 40919ad6b7bcSChris Mason 409213e6c37bSJosef Bacik iput(fs_info->btree_inode); 4093d6bfde87SChris Mason 409421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40950b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 40962ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 409721adbd5cSStefan Behrens #endif 409821adbd5cSStefan Behrens 40990b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 410068c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4101b248a415SChris Mason 4102e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 4103963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 41044297ff84SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 41057f8d236aSDavid Sterba percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 410676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 41070b86a832SChris Mason 4108bfcea1c6SJohannes Thumshirn btrfs_free_csum_hash(fs_info); 410953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 4110fd708b81SJosef Bacik btrfs_free_ref_cache(fs_info); 4111eb60ceacSChris Mason } 4112eb60ceacSChris Mason 4113b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4114b9fab919SChris Mason int atomic) 4115ccd467d6SChris Mason { 41161259ab75SChris Mason int ret; 4117727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 41181259ab75SChris Mason 41190b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 41201259ab75SChris Mason if (!ret) 41211259ab75SChris Mason return ret; 41221259ab75SChris Mason 41231259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4124b9fab919SChris Mason parent_transid, atomic); 4125b9fab919SChris Mason if (ret == -EAGAIN) 4126b9fab919SChris Mason return ret; 41271259ab75SChris Mason return !ret; 41285f39d397SChris Mason } 41296702ed49SChris Mason 41305f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 41315f39d397SChris Mason { 41320b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 413306ea65a3SJosef Bacik struct btrfs_root *root; 41345f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4135b9473439SChris Mason int was_dirty; 4136b4ce94deSChris Mason 413706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 413806ea65a3SJosef Bacik /* 413906ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 414052042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 414106ea65a3SJosef Bacik * outside of the sanity tests. 414206ea65a3SJosef Bacik */ 4143b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 414406ea65a3SJosef Bacik return; 414506ea65a3SJosef Bacik #endif 414606ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41470b246afaSJeff Mahoney fs_info = root->fs_info; 4148b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41490b246afaSJeff Mahoney if (transid != fs_info->generation) 41505d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41510b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41520b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4153e2d84521SMiao Xie if (!was_dirty) 4154104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4155e2d84521SMiao Xie buf->len, 41560b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41571f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 415869fc6cbbSQu Wenruo /* 415969fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 416069fc6cbbSQu Wenruo * but item data not updated. 416169fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 416269fc6cbbSQu Wenruo */ 416369fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4164cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4165a4f78750SDavid Sterba btrfs_print_leaf(buf); 41661f21ef0aSFilipe Manana ASSERT(0); 41671f21ef0aSFilipe Manana } 41681f21ef0aSFilipe Manana #endif 4169eb60ceacSChris Mason } 4170eb60ceacSChris Mason 41712ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4172b53d3f5dSLiu Bo int flush_delayed) 417335b7e476SChris Mason { 4174188de649SChris Mason /* 4175188de649SChris Mason * looks as though older kernels can get into trouble with 4176188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4177188de649SChris Mason */ 4178e2d84521SMiao Xie int ret; 4179d6bfde87SChris Mason 41806933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4181d6bfde87SChris Mason return; 4182d6bfde87SChris Mason 4183b53d3f5dSLiu Bo if (flush_delayed) 41842ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 418516cdcec7SMiao Xie 4186d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4187d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4188d814a491SEthan Lien fs_info->dirty_metadata_batch); 4189e2d84521SMiao Xie if (ret > 0) { 41900b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 419116cdcec7SMiao Xie } 419216cdcec7SMiao Xie } 419316cdcec7SMiao Xie 41942ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 419516cdcec7SMiao Xie { 41962ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 419735b7e476SChris Mason } 4198b53d3f5dSLiu Bo 41992ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4200b53d3f5dSLiu Bo { 42012ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4202d6bfde87SChris Mason } 42036b80053dSChris Mason 4204581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4205581c1760SQu Wenruo struct btrfs_key *first_key) 42066b80053dSChris Mason { 42075ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4208581c1760SQu Wenruo level, first_key); 42096b80053dSChris Mason } 42100da5468fSChris Mason 42112ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4212acce952bSliubo { 4213fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4214fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4215fe816d0fSNikolay Borisov 42160b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42172ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42180b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4219acce952bSliubo 42200b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42210b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4222acce952bSliubo } 4223acce952bSliubo 4224143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4225acce952bSliubo { 4226acce952bSliubo struct btrfs_ordered_extent *ordered; 4227acce952bSliubo 4228199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4229779880efSJosef Bacik /* 4230779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4231779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4232779880efSJosef Bacik */ 4233199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4234779880efSJosef Bacik root_extent_list) 4235779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4236199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4237199c2a9cSMiao Xie } 4238199c2a9cSMiao Xie 4239199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4240199c2a9cSMiao Xie { 4241199c2a9cSMiao Xie struct btrfs_root *root; 4242199c2a9cSMiao Xie struct list_head splice; 4243199c2a9cSMiao Xie 4244199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4245199c2a9cSMiao Xie 4246199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4247199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4248199c2a9cSMiao Xie while (!list_empty(&splice)) { 4249199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4250199c2a9cSMiao Xie ordered_root); 42511de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42521de2cfdeSJosef Bacik &fs_info->ordered_roots); 4253199c2a9cSMiao Xie 42542a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4255199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4256199c2a9cSMiao Xie 42572a85d9caSLiu Bo cond_resched(); 42582a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4259199c2a9cSMiao Xie } 4260199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 426174d5d229SJosef Bacik 426274d5d229SJosef Bacik /* 426374d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 426474d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 426574d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 426674d5d229SJosef Bacik * actually get run and error out properly. 426774d5d229SJosef Bacik */ 426874d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4269acce952bSliubo } 4270acce952bSliubo 427135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 42722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4273acce952bSliubo { 4274acce952bSliubo struct rb_node *node; 4275acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4276acce952bSliubo struct btrfs_delayed_ref_node *ref; 4277acce952bSliubo int ret = 0; 4278acce952bSliubo 4279acce952bSliubo delayed_refs = &trans->delayed_refs; 4280acce952bSliubo 4281acce952bSliubo spin_lock(&delayed_refs->lock); 4282d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4283cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 42840b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4285acce952bSliubo return ret; 4286acce952bSliubo } 4287acce952bSliubo 42885c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4289d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 42900e0adbcfSJosef Bacik struct rb_node *n; 4291e78417d1SJosef Bacik bool pin_bytes = false; 4292acce952bSliubo 4293d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4294d7df2c79SJosef Bacik href_node); 42953069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4296b939d1abSJosef Bacik continue; 42973069bd26SJosef Bacik 4298d7df2c79SJosef Bacik spin_lock(&head->lock); 4299e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 43000e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 43010e0adbcfSJosef Bacik ref_node); 4302d7df2c79SJosef Bacik ref->in_tree = 0; 4303e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 43040e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 43051d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43061d57ee94SWang Xiaoguang list_del(&ref->add_list); 4307d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4308d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4309d7df2c79SJosef Bacik } 431054067ae9SJosef Bacik if (head->must_insert_reserved) 4311e78417d1SJosef Bacik pin_bytes = true; 431278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4313fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4314d7df2c79SJosef Bacik spin_unlock(&head->lock); 4315acce952bSliubo spin_unlock(&delayed_refs->lock); 4316e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4317acce952bSliubo 4318d7df2c79SJosef Bacik if (pin_bytes) 4319d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4320d278850eSJosef Bacik head->num_bytes, 1); 432131890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4322d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4323acce952bSliubo cond_resched(); 4324acce952bSliubo spin_lock(&delayed_refs->lock); 4325acce952bSliubo } 4326acce952bSliubo 4327acce952bSliubo spin_unlock(&delayed_refs->lock); 4328acce952bSliubo 4329acce952bSliubo return ret; 4330acce952bSliubo } 4331acce952bSliubo 4332143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4333acce952bSliubo { 4334acce952bSliubo struct btrfs_inode *btrfs_inode; 4335acce952bSliubo struct list_head splice; 4336acce952bSliubo 4337acce952bSliubo INIT_LIST_HEAD(&splice); 4338acce952bSliubo 4339eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4340eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4341acce952bSliubo 4342acce952bSliubo while (!list_empty(&splice)) { 4343fe816d0fSNikolay Borisov struct inode *inode = NULL; 4344eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4345acce952bSliubo delalloc_inodes); 4346fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4347eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4348acce952bSliubo 4349fe816d0fSNikolay Borisov /* 4350fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4351fe816d0fSNikolay Borisov * meanwhile. 4352fe816d0fSNikolay Borisov */ 4353fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4354fe816d0fSNikolay Borisov if (inode) { 4355fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4356fe816d0fSNikolay Borisov iput(inode); 4357fe816d0fSNikolay Borisov } 4358eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4359acce952bSliubo } 4360eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4361eb73c1b7SMiao Xie } 4362eb73c1b7SMiao Xie 4363eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4364eb73c1b7SMiao Xie { 4365eb73c1b7SMiao Xie struct btrfs_root *root; 4366eb73c1b7SMiao Xie struct list_head splice; 4367eb73c1b7SMiao Xie 4368eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4369eb73c1b7SMiao Xie 4370eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4371eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4372eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4373eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4374eb73c1b7SMiao Xie delalloc_root); 4375eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4376eb73c1b7SMiao Xie BUG_ON(!root); 4377eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4378eb73c1b7SMiao Xie 4379eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4380eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4381eb73c1b7SMiao Xie 4382eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4383eb73c1b7SMiao Xie } 4384eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4385acce952bSliubo } 4386acce952bSliubo 43872ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4388acce952bSliubo struct extent_io_tree *dirty_pages, 4389acce952bSliubo int mark) 4390acce952bSliubo { 4391acce952bSliubo int ret; 4392acce952bSliubo struct extent_buffer *eb; 4393acce952bSliubo u64 start = 0; 4394acce952bSliubo u64 end; 4395acce952bSliubo 4396acce952bSliubo while (1) { 4397acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4398e6138876SJosef Bacik mark, NULL); 4399acce952bSliubo if (ret) 4400acce952bSliubo break; 4401acce952bSliubo 440291166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4403acce952bSliubo while (start <= end) { 44040b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44050b246afaSJeff Mahoney start += fs_info->nodesize; 4406fd8b2b61SJosef Bacik if (!eb) 4407acce952bSliubo continue; 4408fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4409acce952bSliubo 4410fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4411fd8b2b61SJosef Bacik &eb->bflags)) 4412fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4413fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4414acce952bSliubo } 4415acce952bSliubo } 4416acce952bSliubo 4417acce952bSliubo return ret; 4418acce952bSliubo } 4419acce952bSliubo 44202ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4421acce952bSliubo struct extent_io_tree *pinned_extents) 4422acce952bSliubo { 4423acce952bSliubo struct extent_io_tree *unpin; 4424acce952bSliubo u64 start; 4425acce952bSliubo u64 end; 4426acce952bSliubo int ret; 4427ed0eaa14SLiu Bo bool loop = true; 4428acce952bSliubo 4429acce952bSliubo unpin = pinned_extents; 4430ed0eaa14SLiu Bo again: 4431acce952bSliubo while (1) { 44320e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 44330e6ec385SFilipe Manana 4434fcd5e742SLu Fengqi /* 4435fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4436fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4437fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4438fcd5e742SLu Fengqi * the same extent range. 4439fcd5e742SLu Fengqi */ 4440fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4441acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44420e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4443fcd5e742SLu Fengqi if (ret) { 4444fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4445acce952bSliubo break; 4446fcd5e742SLu Fengqi } 4447acce952bSliubo 44480e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44490e6ec385SFilipe Manana free_extent_state(cached_state); 44502ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4451fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4452acce952bSliubo cond_resched(); 4453acce952bSliubo } 4454acce952bSliubo 4455ed0eaa14SLiu Bo if (loop) { 44560b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44570b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4458ed0eaa14SLiu Bo else 44590b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4460ed0eaa14SLiu Bo loop = false; 4461ed0eaa14SLiu Bo goto again; 4462ed0eaa14SLiu Bo } 4463ed0eaa14SLiu Bo 4464acce952bSliubo return 0; 4465acce952bSliubo } 4466acce952bSliubo 4467c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4468c79a1751SLiu Bo { 4469c79a1751SLiu Bo struct inode *inode; 4470c79a1751SLiu Bo 4471c79a1751SLiu Bo inode = cache->io_ctl.inode; 4472c79a1751SLiu Bo if (inode) { 4473c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4474c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4475c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4476c79a1751SLiu Bo iput(inode); 4477c79a1751SLiu Bo } 4478c79a1751SLiu Bo btrfs_put_block_group(cache); 4479c79a1751SLiu Bo } 4480c79a1751SLiu Bo 4481c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 44822ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4483c79a1751SLiu Bo { 4484c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4485c79a1751SLiu Bo 4486c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4487c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4488c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4489c79a1751SLiu Bo struct btrfs_block_group_cache, 4490c79a1751SLiu Bo dirty_list); 4491c79a1751SLiu Bo 4492c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4493c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4494c79a1751SLiu Bo list_del_init(&cache->io_list); 4495c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4496c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4497c79a1751SLiu Bo } 4498c79a1751SLiu Bo 4499c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4500c79a1751SLiu Bo spin_lock(&cache->lock); 4501c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4502c79a1751SLiu Bo spin_unlock(&cache->lock); 4503c79a1751SLiu Bo 4504c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4505c79a1751SLiu Bo btrfs_put_block_group(cache); 4506ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4507c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4508c79a1751SLiu Bo } 4509c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4510c79a1751SLiu Bo 451145ae2c18SNikolay Borisov /* 451245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 451345ae2c18SNikolay Borisov * to use it without any locking 451445ae2c18SNikolay Borisov */ 4515c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4516c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4517c79a1751SLiu Bo struct btrfs_block_group_cache, 4518c79a1751SLiu Bo io_list); 4519c79a1751SLiu Bo 4520c79a1751SLiu Bo list_del_init(&cache->io_list); 4521c79a1751SLiu Bo spin_lock(&cache->lock); 4522c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4523c79a1751SLiu Bo spin_unlock(&cache->lock); 4524c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4525c79a1751SLiu Bo } 4526c79a1751SLiu Bo } 4527c79a1751SLiu Bo 452849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45292ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 453049b25e05SJeff Mahoney { 4531bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4532bbbf7243SNikolay Borisov 45332ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4534c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4535c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4536c79a1751SLiu Bo 4537bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4538bbbf7243SNikolay Borisov post_commit_list) { 4539bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4540bbbf7243SNikolay Borisov } 4541bbbf7243SNikolay Borisov 45422ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 454349b25e05SJeff Mahoney 45444a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45450b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 454649b25e05SJeff Mahoney 45474a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45480b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 454949b25e05SJeff Mahoney 4550ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4551ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 455267cde344SMiao Xie 45532ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 455449b25e05SJeff Mahoney EXTENT_DIRTY); 45552ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45560b246afaSJeff Mahoney fs_info->pinned_extents); 455749b25e05SJeff Mahoney 45584a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45594a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 456049b25e05SJeff Mahoney } 456149b25e05SJeff Mahoney 45622ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4563acce952bSliubo { 4564acce952bSliubo struct btrfs_transaction *t; 4565acce952bSliubo 45660b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4567acce952bSliubo 45680b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45690b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 45700b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4571724e2315SJosef Bacik struct btrfs_transaction, list); 4572724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 45739b64f57dSElena Reshetova refcount_inc(&t->use_count); 45740b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45752ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4576724e2315SJosef Bacik btrfs_put_transaction(t); 45770b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4578724e2315SJosef Bacik continue; 4579724e2315SJosef Bacik } 45800b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4581724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 45820b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4583724e2315SJosef Bacik /* 4584724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4585724e2315SJosef Bacik * handle open currently for this transaction. 4586724e2315SJosef Bacik */ 4587724e2315SJosef Bacik wait_event(t->writer_wait, 4588724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4589724e2315SJosef Bacik } else { 45900b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4591724e2315SJosef Bacik } 45922ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4593724e2315SJosef Bacik 45940b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45950b246afaSJeff Mahoney if (t == fs_info->running_transaction) 45960b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4597724e2315SJosef Bacik list_del_init(&t->list); 45980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4599a4abeea4SJosef Bacik 4600724e2315SJosef Bacik btrfs_put_transaction(t); 46012ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46020b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4603724e2315SJosef Bacik } 46040b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46050b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4606ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4607ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46082ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 46090b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46100b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4611acce952bSliubo 4612acce952bSliubo return 0; 4613acce952bSliubo } 4614acce952bSliubo 4615e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 46164d53dddbSDavid Sterba /* mandatory callbacks */ 46170b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 46184d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 46190da5468fSChris Mason }; 4620