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> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 32581bb050SLi Zefan #include "inode-map.h" 3321adbd5cSStefan Behrens #include "check-integrity.h" 34606686eeSJosef Bacik #include "rcu-string.h" 358dabb742SStefan Behrens #include "dev-replace.h" 3653b381b3SDavid Woodhouse #include "raid56.h" 375ac1d209SJeff Mahoney #include "sysfs.h" 38fcebe456SJosef Bacik #include "qgroup.h" 39ebb8765bSAnand Jain #include "compression.h" 40557ea5ddSQu Wenruo #include "tree-checker.h" 41fd708b81SJosef Bacik #include "ref-verify.h" 42eb60ceacSChris Mason 43de0022b9SJosef Bacik #ifdef CONFIG_X86 44de0022b9SJosef Bacik #include <asm/cpufeature.h> 45de0022b9SJosef Bacik #endif 46de0022b9SJosef Bacik 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 54e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 558b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 56143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 57acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 582ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 59143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 602ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 61acce952bSliubo struct extent_io_tree *dirty_pages, 62acce952bSliubo int mark); 632ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 64acce952bSliubo struct extent_io_tree *pinned_extents); 652ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 662ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 67ce9adaa5SChris Mason 68d352ac68SChris Mason /* 6997eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 7097eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 71d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 72d352ac68SChris Mason */ 7397eb6b69SDavid Sterba struct btrfs_end_io_wq { 74ce9adaa5SChris Mason struct bio *bio; 75ce9adaa5SChris Mason bio_end_io_t *end_io; 76ce9adaa5SChris Mason void *private; 77ce9adaa5SChris Mason struct btrfs_fs_info *info; 784e4cbee9SChristoph Hellwig blk_status_t status; 79bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 808b712842SChris Mason struct btrfs_work work; 81ce9adaa5SChris Mason }; 820da5468fSChris Mason 8397eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8497eb6b69SDavid Sterba 8597eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8697eb6b69SDavid Sterba { 8797eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8897eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8997eb6b69SDavid Sterba 0, 90fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9197eb6b69SDavid Sterba NULL); 9297eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9397eb6b69SDavid Sterba return -ENOMEM; 9497eb6b69SDavid Sterba return 0; 9597eb6b69SDavid Sterba } 9697eb6b69SDavid Sterba 97e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9897eb6b69SDavid Sterba { 9997eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 10097eb6b69SDavid Sterba } 10197eb6b69SDavid Sterba 102d352ac68SChris Mason /* 103d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 104d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 105d352ac68SChris Mason * just before they are sent down the IO stack. 106d352ac68SChris Mason */ 10744b8bd7eSChris Mason struct async_submit_bio { 108c6100a4bSJosef Bacik void *private_data; 10944b8bd7eSChris Mason struct bio *bio; 110a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11144b8bd7eSChris Mason int mirror_num; 112eaf25d93SChris Mason /* 113eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 114eaf25d93SChris Mason * can't tell us where in the file the bio should go 115eaf25d93SChris Mason */ 116eaf25d93SChris Mason u64 bio_offset; 1178b712842SChris Mason struct btrfs_work work; 1184e4cbee9SChristoph Hellwig blk_status_t status; 11944b8bd7eSChris Mason }; 12044b8bd7eSChris Mason 12185d4e461SChris Mason /* 12285d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12385d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12485d4e461SChris Mason * the level the eb occupies in the tree. 1254008c04aSChris Mason * 12685d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12785d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 12885d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1294fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1304fd786e6SMisono Tomohiro * roots have separate keysets. 1314008c04aSChris Mason * 13285d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13385d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13485d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1354008c04aSChris Mason * 13685d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13785d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 13885d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 13985d4e461SChris Mason * 14085d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14185d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14285d4e461SChris Mason * needs update as well. 1434008c04aSChris Mason */ 1444008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1454008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1464008c04aSChris Mason # error 1474008c04aSChris Mason # endif 14885d4e461SChris Mason 14985d4e461SChris Mason static struct btrfs_lockdep_keyset { 15085d4e461SChris Mason u64 id; /* root objectid */ 15185d4e461SChris Mason const char *name_stem; /* lock name stem */ 15285d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 15385d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 15485d4e461SChris Mason } btrfs_lockdep_keysets[] = { 15585d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 15685d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 15785d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 15885d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 15985d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 16085d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 16160b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 16285d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 16385d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 16485d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 16513fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1666b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 16785d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1684008c04aSChris Mason }; 16985d4e461SChris Mason 17085d4e461SChris Mason void __init btrfs_init_lockdep(void) 17185d4e461SChris Mason { 17285d4e461SChris Mason int i, j; 17385d4e461SChris Mason 17485d4e461SChris Mason /* initialize lockdep class names */ 17585d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 17685d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 17785d4e461SChris Mason 17885d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 17985d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 18085d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 18185d4e461SChris Mason } 18285d4e461SChris Mason } 18385d4e461SChris Mason 18485d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18585d4e461SChris Mason int level) 18685d4e461SChris Mason { 18785d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 18885d4e461SChris Mason 18985d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19085d4e461SChris Mason 19185d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19285d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19385d4e461SChris Mason if (ks->id == objectid) 19485d4e461SChris Mason break; 19585d4e461SChris Mason 19685d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 19785d4e461SChris Mason &ks->keys[level], ks->names[level]); 19885d4e461SChris Mason } 19985d4e461SChris Mason 2004008c04aSChris Mason #endif 2014008c04aSChris Mason 202d352ac68SChris Mason /* 203d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 204d352ac68SChris Mason * that covers the entire device 205d352ac68SChris Mason */ 2066af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 207306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2085f39d397SChris Mason int create) 2095f39d397SChris Mason { 2103ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 211fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2125f39d397SChris Mason struct extent_map *em; 2135f39d397SChris Mason int ret; 2145f39d397SChris Mason 215890871beSChris Mason read_lock(&em_tree->lock); 216d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 217a061fc8dSChris Mason if (em) { 2180b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 219890871beSChris Mason read_unlock(&em_tree->lock); 2205f39d397SChris Mason goto out; 221a061fc8dSChris Mason } 222890871beSChris Mason read_unlock(&em_tree->lock); 2237b13b7b1SChris Mason 224172ddd60SDavid Sterba em = alloc_extent_map(); 2255f39d397SChris Mason if (!em) { 2265f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2275f39d397SChris Mason goto out; 2285f39d397SChris Mason } 2295f39d397SChris Mason em->start = 0; 2300afbaf8cSChris Mason em->len = (u64)-1; 231c8b97818SChris Mason em->block_len = (u64)-1; 2325f39d397SChris Mason em->block_start = 0; 2330b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 234d1310b2eSChris Mason 235890871beSChris Mason write_lock(&em_tree->lock); 23609a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2375f39d397SChris Mason if (ret == -EEXIST) { 2385f39d397SChris Mason free_extent_map(em); 2397b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 240b4f359abSTsutomu Itoh if (!em) 2410433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2425f39d397SChris Mason } else if (ret) { 2437b13b7b1SChris Mason free_extent_map(em); 2440433f20dSTsutomu Itoh em = ERR_PTR(ret); 2455f39d397SChris Mason } 246890871beSChris Mason write_unlock(&em_tree->lock); 2477b13b7b1SChris Mason 2485f39d397SChris Mason out: 2495f39d397SChris Mason return em; 2505f39d397SChris Mason } 2515f39d397SChris Mason 2529ed57367SDavid Sterba u32 btrfs_csum_data(const char *data, u32 seed, size_t len) 25319c00ddcSChris Mason { 2549678c543SNikolay Borisov return crc32c(seed, data, len); 25519c00ddcSChris Mason } 25619c00ddcSChris Mason 2570b5e3dafSDomagoj Tršan void btrfs_csum_final(u32 crc, u8 *result) 25819c00ddcSChris Mason { 2597e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 26019c00ddcSChris Mason } 26119c00ddcSChris Mason 262d352ac68SChris Mason /* 2632996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 2642996e1f8SJohannes Thumshirn * 2652996e1f8SJohannes Thumshirn * Returns error if the extent buffer cannot be mapped. 266d352ac68SChris Mason */ 2672996e1f8SJohannes Thumshirn static int csum_tree_block(struct extent_buffer *buf, u8 *result) 26819c00ddcSChris Mason { 26919c00ddcSChris Mason unsigned long len; 27019c00ddcSChris Mason unsigned long cur_len; 27119c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 27219c00ddcSChris Mason char *kaddr; 27319c00ddcSChris Mason unsigned long map_start; 27419c00ddcSChris Mason unsigned long map_len; 27519c00ddcSChris Mason int err; 27619c00ddcSChris Mason u32 crc = ~(u32)0; 27719c00ddcSChris Mason 27819c00ddcSChris Mason len = buf->len - offset; 27919c00ddcSChris Mason while (len > 0) { 280d2e174d5SJohannes Thumshirn /* 281d2e174d5SJohannes Thumshirn * Note: we don't need to check for the err == 1 case here, as 282d2e174d5SJohannes Thumshirn * with the given combination of 'start = BTRFS_CSUM_SIZE (32)' 283d2e174d5SJohannes Thumshirn * and 'min_len = 32' and the currently implemented mapping 284d2e174d5SJohannes Thumshirn * algorithm we cannot cross a page boundary. 285d2e174d5SJohannes Thumshirn */ 28619c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 287a6591715SChris Mason &kaddr, &map_start, &map_len); 288c53839fcSJohannes Thumshirn if (WARN_ON(err)) 2898bd98f0eSAlex Lyakas return err; 29019c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 291b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 29219c00ddcSChris Mason crc, cur_len); 29319c00ddcSChris Mason len -= cur_len; 29419c00ddcSChris Mason offset += cur_len; 29519c00ddcSChris Mason } 29671a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 297607d432dSJosef Bacik 29819c00ddcSChris Mason btrfs_csum_final(crc, result); 29919c00ddcSChris Mason 30019c00ddcSChris Mason return 0; 30119c00ddcSChris Mason } 30219c00ddcSChris Mason 303d352ac68SChris Mason /* 304d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 305d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 306d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 307d352ac68SChris Mason * in the wrong place. 308d352ac68SChris Mason */ 3091259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 310b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 311b9fab919SChris Mason int atomic) 3121259ab75SChris Mason { 3132ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3141259ab75SChris Mason int ret; 3152755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3161259ab75SChris Mason 3171259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3181259ab75SChris Mason return 0; 3191259ab75SChris Mason 320b9fab919SChris Mason if (atomic) 321b9fab919SChris Mason return -EAGAIN; 322b9fab919SChris Mason 323a26e8c9fSJosef Bacik if (need_lock) { 324a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 325300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 326a26e8c9fSJosef Bacik } 327a26e8c9fSJosef Bacik 3282ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 329ff13db41SDavid Sterba &cached_state); 3300b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3311259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3321259ab75SChris Mason ret = 0; 3331259ab75SChris Mason goto out; 3341259ab75SChris Mason } 33594647322SDavid Sterba btrfs_err_rl(eb->fs_info, 33694647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 33794647322SDavid Sterba eb->start, 33829549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3391259ab75SChris Mason ret = 1; 340a26e8c9fSJosef Bacik 341a26e8c9fSJosef Bacik /* 342a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 343a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 34401327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 345a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 346a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 347a26e8c9fSJosef Bacik * making everybody very sad. 348a26e8c9fSJosef Bacik */ 349a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3500b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 35133958dc6SChris Mason out: 3522ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 353e43bbe5eSDavid Sterba &cached_state); 354472b909fSJosef Bacik if (need_lock) 355a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3561259ab75SChris Mason return ret; 3571259ab75SChris Mason } 3581259ab75SChris Mason 359d352ac68SChris Mason /* 3601104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3611104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3621104a885SDavid Sterba */ 363ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 364ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3651104a885SDavid Sterba { 3661104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3671104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 3681104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 3691104a885SDavid Sterba int ret = 0; 3701104a885SDavid Sterba 3711104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 3721104a885SDavid Sterba u32 crc = ~(u32)0; 373776c4a7cSNikolay Borisov char result[sizeof(crc)]; 3741104a885SDavid Sterba 3751104a885SDavid Sterba /* 3761104a885SDavid Sterba * The super_block structure does not span the whole 3771104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 37801327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 3791104a885SDavid Sterba */ 3801104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 3811104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3821104a885SDavid Sterba btrfs_csum_final(crc, result); 3831104a885SDavid Sterba 384776c4a7cSNikolay Borisov if (memcmp(raw_disk_sb, result, sizeof(result))) 3851104a885SDavid Sterba ret = 1; 3861104a885SDavid Sterba } 3871104a885SDavid Sterba 3881104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 389ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 3901104a885SDavid Sterba csum_type); 3911104a885SDavid Sterba ret = 1; 3921104a885SDavid Sterba } 3931104a885SDavid Sterba 3941104a885SDavid Sterba return ret; 3951104a885SDavid Sterba } 3961104a885SDavid Sterba 397448de471SQu Wenruo int btrfs_verify_level_key(struct btrfs_fs_info *fs_info, 398581c1760SQu Wenruo struct extent_buffer *eb, int level, 399ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 400581c1760SQu Wenruo { 401581c1760SQu Wenruo int found_level; 402581c1760SQu Wenruo struct btrfs_key found_key; 403581c1760SQu Wenruo int ret; 404581c1760SQu Wenruo 405581c1760SQu Wenruo found_level = btrfs_header_level(eb); 406581c1760SQu Wenruo if (found_level != level) { 40763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 40863489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 409581c1760SQu Wenruo btrfs_err(fs_info, 410581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 411581c1760SQu Wenruo eb->start, level, found_level); 412581c1760SQu Wenruo return -EIO; 413581c1760SQu Wenruo } 414581c1760SQu Wenruo 415581c1760SQu Wenruo if (!first_key) 416581c1760SQu Wenruo return 0; 417581c1760SQu Wenruo 4185d41be6fSQu Wenruo /* 4195d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 4205d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 4215d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 4225d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 4235d41be6fSQu Wenruo */ 4245d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 4255d41be6fSQu Wenruo return 0; 426581c1760SQu Wenruo if (found_level) 427581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 428581c1760SQu Wenruo else 429581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 430581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 431581c1760SQu Wenruo 432581c1760SQu Wenruo if (ret) { 43363489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 43463489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 435581c1760SQu Wenruo btrfs_err(fs_info, 436ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 437ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 438ff76a864SLiu Bo first_key->type, first_key->offset, 439ff76a864SLiu Bo found_key.objectid, found_key.type, 440ff76a864SLiu Bo found_key.offset); 441581c1760SQu Wenruo } 442581c1760SQu Wenruo return ret; 443581c1760SQu Wenruo } 444581c1760SQu Wenruo 4451104a885SDavid Sterba /* 446d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 447d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 448581c1760SQu Wenruo * 449581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 450581c1760SQu Wenruo * @level: expected level, mandatory check 451581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 452d352ac68SChris Mason */ 4532ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 454f188591eSChris Mason struct extent_buffer *eb, 455581c1760SQu Wenruo u64 parent_transid, int level, 456581c1760SQu Wenruo struct btrfs_key *first_key) 457f188591eSChris Mason { 458f188591eSChris Mason struct extent_io_tree *io_tree; 459ea466794SJosef Bacik int failed = 0; 460f188591eSChris Mason int ret; 461f188591eSChris Mason int num_copies = 0; 462f188591eSChris Mason int mirror_num = 0; 463ea466794SJosef Bacik int failed_mirror = 0; 464f188591eSChris Mason 4650b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 466f188591eSChris Mason while (1) { 467f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4688436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 4696af49dbdSDavid Sterba mirror_num); 470256dd1bbSStefan Behrens if (!ret) { 471581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 472b9fab919SChris Mason parent_transid, 0)) 473256dd1bbSStefan Behrens ret = -EIO; 474448de471SQu Wenruo else if (btrfs_verify_level_key(fs_info, eb, level, 475ff76a864SLiu Bo first_key, parent_transid)) 476581c1760SQu Wenruo ret = -EUCLEAN; 477581c1760SQu Wenruo else 478581c1760SQu Wenruo break; 479256dd1bbSStefan Behrens } 480d397712bSChris Mason 4810b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 482f188591eSChris Mason eb->start, eb->len); 4834235298eSChris Mason if (num_copies == 1) 484ea466794SJosef Bacik break; 4854235298eSChris Mason 4865cf1ab56SJosef Bacik if (!failed_mirror) { 4875cf1ab56SJosef Bacik failed = 1; 4885cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4895cf1ab56SJosef Bacik } 4905cf1ab56SJosef Bacik 491f188591eSChris Mason mirror_num++; 492ea466794SJosef Bacik if (mirror_num == failed_mirror) 493ea466794SJosef Bacik mirror_num++; 494ea466794SJosef Bacik 4954235298eSChris Mason if (mirror_num > num_copies) 496ea466794SJosef Bacik break; 497f188591eSChris Mason } 498ea466794SJosef Bacik 499c0901581SStefan Behrens if (failed && !ret && failed_mirror) 5002ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 501ea466794SJosef Bacik 502ea466794SJosef Bacik return ret; 503f188591eSChris Mason } 50419c00ddcSChris Mason 505d352ac68SChris Mason /* 506d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 507d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 508d352ac68SChris Mason */ 509d397712bSChris Mason 51001d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 51119c00ddcSChris Mason { 5124eee4fa4SMiao Xie u64 start = page_offset(page); 51319c00ddcSChris Mason u64 found_start; 5142996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 5152996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 51619c00ddcSChris Mason struct extent_buffer *eb; 517f188591eSChris Mason 5184f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5194f2de97aSJosef Bacik if (page != eb->pages[0]) 5204f2de97aSJosef Bacik return 0; 5210f805531SAlex Lyakas 52219c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5230f805531SAlex Lyakas /* 5240f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5250f805531SAlex Lyakas * It is useful to know what went wrong. 5260f805531SAlex Lyakas */ 5270f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5280f805531SAlex Lyakas return -EUCLEAN; 5290f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5300f805531SAlex Lyakas return -EUCLEAN; 5310f805531SAlex Lyakas 532de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 5330f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5340f805531SAlex Lyakas 5352996e1f8SJohannes Thumshirn if (csum_tree_block(eb, result)) 5362996e1f8SJohannes Thumshirn return -EINVAL; 5372996e1f8SJohannes Thumshirn 5382996e1f8SJohannes Thumshirn write_extent_buffer(eb, result, 0, csum_size); 5392996e1f8SJohannes Thumshirn return 0; 54019c00ddcSChris Mason } 54119c00ddcSChris Mason 54201d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5432b82032cSYan Zheng struct extent_buffer *eb) 5442b82032cSYan Zheng { 54501d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 54644880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5472b82032cSYan Zheng int ret = 1; 5482b82032cSYan Zheng 5490a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5502b82032cSYan Zheng while (fs_devices) { 5517239ff4bSNikolay Borisov u8 *metadata_uuid; 5527239ff4bSNikolay Borisov 5537239ff4bSNikolay Borisov /* 5547239ff4bSNikolay Borisov * Checking the incompat flag is only valid for the current 5557239ff4bSNikolay Borisov * fs. For seed devices it's forbidden to have their uuid 5567239ff4bSNikolay Borisov * changed so reading ->fsid in this case is fine 5577239ff4bSNikolay Borisov */ 5587239ff4bSNikolay Borisov if (fs_devices == fs_info->fs_devices && 5597239ff4bSNikolay Borisov btrfs_fs_incompat(fs_info, METADATA_UUID)) 5607239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5617239ff4bSNikolay Borisov else 5627239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5637239ff4bSNikolay Borisov 5647239ff4bSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) { 5652b82032cSYan Zheng ret = 0; 5662b82032cSYan Zheng break; 5672b82032cSYan Zheng } 5682b82032cSYan Zheng fs_devices = fs_devices->seed; 5692b82032cSYan Zheng } 5702b82032cSYan Zheng return ret; 5712b82032cSYan Zheng } 5722b82032cSYan Zheng 573facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 574facc8a22SMiao Xie u64 phy_offset, struct page *page, 575facc8a22SMiao Xie u64 start, u64 end, int mirror) 576ce9adaa5SChris Mason { 577ce9adaa5SChris Mason u64 found_start; 578ce9adaa5SChris Mason int found_level; 579ce9adaa5SChris Mason struct extent_buffer *eb; 580ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 58102873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 5822996e1f8SJohannes Thumshirn u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 583f188591eSChris Mason int ret = 0; 5842996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 585727011e0SChris Mason int reads_done; 586ce9adaa5SChris Mason 587ce9adaa5SChris Mason if (!page->private) 588ce9adaa5SChris Mason goto out; 589d397712bSChris Mason 5904f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 591d397712bSChris Mason 5920b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5930b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5940b32f4bbSJosef Bacik */ 5950b32f4bbSJosef Bacik extent_buffer_get(eb); 5960b32f4bbSJosef Bacik 5970b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 598727011e0SChris Mason if (!reads_done) 599727011e0SChris Mason goto err; 600f188591eSChris Mason 6015cf1ab56SJosef Bacik eb->read_mirror = mirror; 602656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 603ea466794SJosef Bacik ret = -EIO; 604ea466794SJosef Bacik goto err; 605ea466794SJosef Bacik } 606ea466794SJosef Bacik 607ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 608727011e0SChris Mason if (found_start != eb->start) { 609893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 610893bf4b1SSu Yue eb->start, found_start); 611f188591eSChris Mason ret = -EIO; 612ce9adaa5SChris Mason goto err; 613ce9adaa5SChris Mason } 61402873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 61502873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 61694647322SDavid Sterba eb->start); 6171259ab75SChris Mason ret = -EIO; 6181259ab75SChris Mason goto err; 6191259ab75SChris Mason } 620ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6211c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 622893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 623893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6241c24c3ceSJosef Bacik ret = -EIO; 6251c24c3ceSJosef Bacik goto err; 6261c24c3ceSJosef Bacik } 627ce9adaa5SChris Mason 62885d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 62985d4e461SChris Mason eb, found_level); 6304008c04aSChris Mason 6312996e1f8SJohannes Thumshirn ret = csum_tree_block(eb, result); 6328bd98f0eSAlex Lyakas if (ret) 633a826d6dcSJosef Bacik goto err; 634a826d6dcSJosef Bacik 6352996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 6362996e1f8SJohannes Thumshirn u32 val; 6372996e1f8SJohannes Thumshirn u32 found = 0; 6382996e1f8SJohannes Thumshirn 6392996e1f8SJohannes Thumshirn memcpy(&found, result, csum_size); 6402996e1f8SJohannes Thumshirn 6412996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 6422996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 6432996e1f8SJohannes Thumshirn "%s checksum verify failed on %llu wanted %x found %x level %d", 6442996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 6452996e1f8SJohannes Thumshirn val, found, btrfs_header_level(eb)); 6462996e1f8SJohannes Thumshirn ret = -EUCLEAN; 6472996e1f8SJohannes Thumshirn goto err; 6482996e1f8SJohannes Thumshirn } 6492996e1f8SJohannes Thumshirn 650a826d6dcSJosef Bacik /* 651a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 652a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 653a826d6dcSJosef Bacik * return -EIO. 654a826d6dcSJosef Bacik */ 6552f659546SQu Wenruo if (found_level == 0 && btrfs_check_leaf_full(fs_info, eb)) { 656a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 657a826d6dcSJosef Bacik ret = -EIO; 658a826d6dcSJosef Bacik } 659ce9adaa5SChris Mason 6602f659546SQu Wenruo if (found_level > 0 && btrfs_check_node(fs_info, eb)) 661053ab70fSLiu Bo ret = -EIO; 662053ab70fSLiu Bo 6630b32f4bbSJosef Bacik if (!ret) 6640b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 66575391f0dSQu Wenruo else 66675391f0dSQu Wenruo btrfs_err(fs_info, 66775391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 66875391f0dSQu Wenruo eb->start); 669ce9adaa5SChris Mason err: 67079fb65a1SJosef Bacik if (reads_done && 67179fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 672d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6734bb31e92SArne Jansen 67453b381b3SDavid Woodhouse if (ret) { 67553b381b3SDavid Woodhouse /* 67653b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 67753b381b3SDavid Woodhouse * again, we have to make sure it has something 67853b381b3SDavid Woodhouse * to decrement 67953b381b3SDavid Woodhouse */ 68053b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6810b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 68253b381b3SDavid Woodhouse } 6830b32f4bbSJosef Bacik free_extent_buffer(eb); 684ce9adaa5SChris Mason out: 685f188591eSChris Mason return ret; 686ce9adaa5SChris Mason } 687ce9adaa5SChris Mason 6884246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 689ce9adaa5SChris Mason { 69097eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 691ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6929e0af237SLiu Bo struct btrfs_workqueue *wq; 6939e0af237SLiu Bo btrfs_work_func_t func; 694ce9adaa5SChris Mason 695ce9adaa5SChris Mason fs_info = end_io_wq->info; 6964e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 697d20f7043SChris Mason 69837226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 6999e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 7009e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 7019e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 7029e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 7039e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 7049e0af237SLiu Bo func = btrfs_freespace_write_helper; 7059e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7069e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7079e0af237SLiu Bo func = btrfs_endio_raid56_helper; 708d20f7043SChris Mason } else { 7099e0af237SLiu Bo wq = fs_info->endio_write_workers; 7109e0af237SLiu Bo func = btrfs_endio_write_helper; 711d20f7043SChris Mason } 7129e0af237SLiu Bo } else { 7138b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 7148b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 7158b110e39SMiao Xie wq = fs_info->endio_repair_workers; 7168b110e39SMiao Xie func = btrfs_endio_repair_helper; 7178b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7189e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7199e0af237SLiu Bo func = btrfs_endio_raid56_helper; 7209e0af237SLiu Bo } else if (end_io_wq->metadata) { 7219e0af237SLiu Bo wq = fs_info->endio_meta_workers; 7229e0af237SLiu Bo func = btrfs_endio_meta_helper; 7239e0af237SLiu Bo } else { 7249e0af237SLiu Bo wq = fs_info->endio_workers; 7259e0af237SLiu Bo func = btrfs_endio_helper; 7269e0af237SLiu Bo } 7279e0af237SLiu Bo } 7289e0af237SLiu Bo 7299e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 7309e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 731ce9adaa5SChris Mason } 732ce9adaa5SChris Mason 7334e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 734bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7350b86a832SChris Mason { 73697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7378b110e39SMiao Xie 73897eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 739ce9adaa5SChris Mason if (!end_io_wq) 7404e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 741ce9adaa5SChris Mason 742ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 743ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 74422c59948SChris Mason end_io_wq->info = info; 7454e4cbee9SChristoph Hellwig end_io_wq->status = 0; 746ce9adaa5SChris Mason end_io_wq->bio = bio; 74722c59948SChris Mason end_io_wq->metadata = metadata; 748ce9adaa5SChris Mason 749ce9adaa5SChris Mason bio->bi_private = end_io_wq; 750ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 75122c59948SChris Mason return 0; 75222c59948SChris Mason } 75322c59948SChris Mason 7544a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7554a69a410SChris Mason { 7564a69a410SChris Mason struct async_submit_bio *async; 7574e4cbee9SChristoph Hellwig blk_status_t ret; 7584a69a410SChris Mason 7594a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 760c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 761eaf25d93SChris Mason async->bio_offset); 76279787eaaSJeff Mahoney if (ret) 7634e4cbee9SChristoph Hellwig async->status = ret; 7644a69a410SChris Mason } 7654a69a410SChris Mason 76606ea01b1SDavid Sterba /* 76706ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 76806ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 76906ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 77006ea01b1SDavid Sterba * 77106ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 77206ea01b1SDavid Sterba * inserted into the tree. 77306ea01b1SDavid Sterba */ 7744a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7758b712842SChris Mason { 7768b712842SChris Mason struct async_submit_bio *async; 77706ea01b1SDavid Sterba struct inode *inode; 77806ea01b1SDavid Sterba blk_status_t ret; 7798b712842SChris Mason 7808b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 78106ea01b1SDavid Sterba inode = async->private_data; 7824854ddd0SChris Mason 783bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7844e4cbee9SChristoph Hellwig if (async->status) { 7854e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7864246a0b6SChristoph Hellwig bio_endio(async->bio); 78779787eaaSJeff Mahoney return; 78879787eaaSJeff Mahoney } 78979787eaaSJeff Mahoney 79006ea01b1SDavid Sterba ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, 79106ea01b1SDavid Sterba async->mirror_num, 1); 79206ea01b1SDavid Sterba if (ret) { 79306ea01b1SDavid Sterba async->bio->bi_status = ret; 79406ea01b1SDavid Sterba bio_endio(async->bio); 79506ea01b1SDavid Sterba } 7964a69a410SChris Mason } 7974a69a410SChris Mason 7984a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7994a69a410SChris Mason { 8004a69a410SChris Mason struct async_submit_bio *async; 8014a69a410SChris Mason 8024a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8038b712842SChris Mason kfree(async); 8048b712842SChris Mason } 8058b712842SChris Mason 8068c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 807c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 808c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 809e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 81044b8bd7eSChris Mason { 81144b8bd7eSChris Mason struct async_submit_bio *async; 81244b8bd7eSChris Mason 81344b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81444b8bd7eSChris Mason if (!async) 8154e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 81644b8bd7eSChris Mason 817c6100a4bSJosef Bacik async->private_data = private_data; 81844b8bd7eSChris Mason async->bio = bio; 81944b8bd7eSChris Mason async->mirror_num = mirror_num; 8204a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8214a69a410SChris Mason 8229e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 8235cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 8244a69a410SChris Mason 825eaf25d93SChris Mason async->bio_offset = bio_offset; 8268c8bee1dSChris Mason 8274e4cbee9SChristoph Hellwig async->status = 0; 82879787eaaSJeff Mahoney 82967f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8305cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 831d313d7a3SChris Mason 8325cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 83344b8bd7eSChris Mason return 0; 83444b8bd7eSChris Mason } 83544b8bd7eSChris Mason 8364e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 837ce3ed71aSChris Mason { 8382c30c71bSKent Overstreet struct bio_vec *bvec; 839ce3ed71aSChris Mason struct btrfs_root *root; 8402c30c71bSKent Overstreet int i, ret = 0; 8416dc4f100SMing Lei struct bvec_iter_all iter_all; 842ce3ed71aSChris Mason 843c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8446dc4f100SMing Lei bio_for_each_segment_all(bvec, bio, i, iter_all) { 845ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 84601d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 84779787eaaSJeff Mahoney if (ret) 84879787eaaSJeff Mahoney break; 849ce3ed71aSChris Mason } 8502c30c71bSKent Overstreet 8514e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 852ce3ed71aSChris Mason } 853ce3ed71aSChris Mason 854d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 855eaf25d93SChris Mason u64 bio_offset) 85622c59948SChris Mason { 8578b712842SChris Mason /* 8588b712842SChris Mason * when we're called for a write, we're already in the async 8595443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8608b712842SChris Mason */ 86179787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86222c59948SChris Mason } 86322c59948SChris Mason 86418fdc679SLiu Bo static int check_async_write(struct btrfs_inode *bi) 865de0022b9SJosef Bacik { 8666300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8676300463bSLiu Bo return 0; 868de0022b9SJosef Bacik #ifdef CONFIG_X86 869bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 870de0022b9SJosef Bacik return 0; 871de0022b9SJosef Bacik #endif 872de0022b9SJosef Bacik return 1; 873de0022b9SJosef Bacik } 874de0022b9SJosef Bacik 8758c27cb35SLinus Torvalds static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio, 876eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 877eaf25d93SChris Mason u64 bio_offset) 87844b8bd7eSChris Mason { 879c6100a4bSJosef Bacik struct inode *inode = private_data; 8800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 88118fdc679SLiu Bo int async = check_async_write(BTRFS_I(inode)); 8824e4cbee9SChristoph Hellwig blk_status_t ret; 883cad321adSChris Mason 88437226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 885cad321adSChris Mason /* 886cad321adSChris Mason * called for a read, do the setup so that checksum validation 887cad321adSChris Mason * can happen in the async kernel threads 888cad321adSChris Mason */ 8890b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8900b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8911d4284bdSChris Mason if (ret) 89261891923SStefan Behrens goto out_w_error; 8932ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 894de0022b9SJosef Bacik } else if (!async) { 895de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 896de0022b9SJosef Bacik if (ret) 89761891923SStefan Behrens goto out_w_error; 8982ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 89961891923SStefan Behrens } else { 900cad321adSChris Mason /* 90161891923SStefan Behrens * kthread helpers are used to submit writes so that 90261891923SStefan Behrens * checksumming can happen in parallel across all CPUs 903cad321adSChris Mason */ 904c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 905c6100a4bSJosef Bacik bio_offset, private_data, 906e288c080SDavid Sterba btree_submit_bio_start); 90744b8bd7eSChris Mason } 90844b8bd7eSChris Mason 9094246a0b6SChristoph Hellwig if (ret) 9104246a0b6SChristoph Hellwig goto out_w_error; 9114246a0b6SChristoph Hellwig return 0; 9124246a0b6SChristoph Hellwig 91361891923SStefan Behrens out_w_error: 9144e4cbee9SChristoph Hellwig bio->bi_status = ret; 9154246a0b6SChristoph Hellwig bio_endio(bio); 91661891923SStefan Behrens return ret; 91761891923SStefan Behrens } 91861891923SStefan Behrens 9193dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 920784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 921a6bc32b8SMel Gorman struct page *newpage, struct page *page, 922a6bc32b8SMel Gorman enum migrate_mode mode) 923784b4e29SChris Mason { 924784b4e29SChris Mason /* 925784b4e29SChris Mason * we can't safely write a btree page from here, 926784b4e29SChris Mason * we haven't done the locking hook 927784b4e29SChris Mason */ 928784b4e29SChris Mason if (PageDirty(page)) 929784b4e29SChris Mason return -EAGAIN; 930784b4e29SChris Mason /* 931784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 932784b4e29SChris Mason * We must have no buffers or drop them. 933784b4e29SChris Mason */ 934784b4e29SChris Mason if (page_has_private(page) && 935784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 936784b4e29SChris Mason return -EAGAIN; 937a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 938784b4e29SChris Mason } 9393dd1462eSJan Beulich #endif 940784b4e29SChris Mason 9410da5468fSChris Mason 9420da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9430da5468fSChris Mason struct writeback_control *wbc) 9440da5468fSChris Mason { 945e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 946e2d84521SMiao Xie int ret; 947e2d84521SMiao Xie 948d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 949448d640bSChris Mason 950448d640bSChris Mason if (wbc->for_kupdate) 951448d640bSChris Mason return 0; 952448d640bSChris Mason 953e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 954b9473439SChris Mason /* this is a bit racy, but that's ok */ 955d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 956d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 957d814a491SEthan Lien fs_info->dirty_metadata_batch); 958e2d84521SMiao Xie if (ret < 0) 959793955bcSChris Mason return 0; 960793955bcSChris Mason } 9610b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9620da5468fSChris Mason } 9630da5468fSChris Mason 964b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9655f39d397SChris Mason { 966d1310b2eSChris Mason struct extent_io_tree *tree; 967d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9688ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9695f39d397SChris Mason } 9705f39d397SChris Mason 97170dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9725f39d397SChris Mason { 97398509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 97498509cfcSChris Mason return 0; 9750c4e538bSDavid Sterba 976f7a52a40SDavid Sterba return try_release_extent_buffer(page); 977d98237b3SChris Mason } 978d98237b3SChris Mason 979d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 980d47992f8SLukas Czerner unsigned int length) 981d98237b3SChris Mason { 982d1310b2eSChris Mason struct extent_io_tree *tree; 983d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9845f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9855f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9869ad6b7bcSChris Mason if (PagePrivate(page)) { 987efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 988efe120a0SFrank Holton "page private not zero on page %llu", 989efe120a0SFrank Holton (unsigned long long)page_offset(page)); 9909ad6b7bcSChris Mason ClearPagePrivate(page); 9919ad6b7bcSChris Mason set_page_private(page, 0); 99209cbfeafSKirill A. Shutemov put_page(page); 9939ad6b7bcSChris Mason } 994d98237b3SChris Mason } 995d98237b3SChris Mason 9960b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9970b32f4bbSJosef Bacik { 998bb146eb2SJosef Bacik #ifdef DEBUG 9990b32f4bbSJosef Bacik struct extent_buffer *eb; 10000b32f4bbSJosef Bacik 10010b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10020b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10030b32f4bbSJosef Bacik BUG_ON(!eb); 10040b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10050b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10060b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1007bb146eb2SJosef Bacik #endif 10080b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10090b32f4bbSJosef Bacik } 10100b32f4bbSJosef Bacik 10117f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1012d98237b3SChris Mason .readpage = btree_readpage, 10130da5468fSChris Mason .writepages = btree_writepages, 10145f39d397SChris Mason .releasepage = btree_releasepage, 10155f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10165a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1017784b4e29SChris Mason .migratepage = btree_migratepage, 10185a92bc88SChris Mason #endif 10190b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1020d98237b3SChris Mason }; 1021123abc88SChris Mason 10222ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1023090d1875SChris Mason { 10245f39d397SChris Mason struct extent_buffer *buf = NULL; 10252ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1026537f38f0SNikolay Borisov int ret; 1027090d1875SChris Mason 10282ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1029c871b0f2SLiu Bo if (IS_ERR(buf)) 10306197d86eSDavid Sterba return; 1031537f38f0SNikolay Borisov 1032537f38f0SNikolay Borisov ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 1033537f38f0SNikolay Borisov WAIT_NONE, 0); 1034537f38f0SNikolay Borisov if (ret < 0) 1035537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1036537f38f0SNikolay Borisov else 10375f39d397SChris Mason free_extent_buffer(buf); 1038090d1875SChris Mason } 1039090d1875SChris Mason 10402ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1041ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1042ab0fff03SArne Jansen { 1043ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 10442ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1045ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1046ab0fff03SArne Jansen int ret; 1047ab0fff03SArne Jansen 10482ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1049c871b0f2SLiu Bo if (IS_ERR(buf)) 1050ab0fff03SArne Jansen return 0; 1051ab0fff03SArne Jansen 1052ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1053ab0fff03SArne Jansen 10548436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 10556af49dbdSDavid Sterba mirror_num); 1056ab0fff03SArne Jansen if (ret) { 1057537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1058ab0fff03SArne Jansen return ret; 1059ab0fff03SArne Jansen } 1060ab0fff03SArne Jansen 1061ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1062537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 1063ab0fff03SArne Jansen return -EIO; 10640b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1065ab0fff03SArne Jansen *eb = buf; 1066ab0fff03SArne Jansen } else { 1067ab0fff03SArne Jansen free_extent_buffer(buf); 1068ab0fff03SArne Jansen } 1069ab0fff03SArne Jansen return 0; 1070ab0fff03SArne Jansen } 1071ab0fff03SArne Jansen 10722ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10732ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10740308af44SDavid Sterba u64 bytenr) 10750999df54SChris Mason { 10760b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10770b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10780b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10790999df54SChris Mason } 10800999df54SChris Mason 10810999df54SChris Mason 1082e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1083e02119d5SChris Mason { 1084727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 10858aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1086e02119d5SChris Mason } 1087e02119d5SChris Mason 10883189ff77SJeff Layton void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1089e02119d5SChris Mason { 10903189ff77SJeff Layton filemap_fdatawait_range(buf->pages[0]->mapping, 10918aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1092e02119d5SChris Mason } 1093e02119d5SChris Mason 1094581c1760SQu Wenruo /* 1095581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1096581c1760SQu Wenruo * verification. 1097581c1760SQu Wenruo * 1098581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1099581c1760SQu Wenruo * @level: expected level, mandatory check 1100581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1101581c1760SQu Wenruo */ 11022ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1103581c1760SQu Wenruo u64 parent_transid, int level, 1104581c1760SQu Wenruo struct btrfs_key *first_key) 1105e20d96d6SChris Mason { 11065f39d397SChris Mason struct extent_buffer *buf = NULL; 110719c00ddcSChris Mason int ret; 110819c00ddcSChris Mason 11092ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1110c871b0f2SLiu Bo if (IS_ERR(buf)) 1111c871b0f2SLiu Bo return buf; 1112e4204dedSChris Mason 1113581c1760SQu Wenruo ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid, 1114581c1760SQu Wenruo level, first_key); 11150f0fe8f7SFilipe David Borba Manana if (ret) { 1116537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 111764c043deSLiu Bo return ERR_PTR(ret); 11180f0fe8f7SFilipe David Borba Manana } 11195f39d397SChris Mason return buf; 1120ce9adaa5SChris Mason 1121eb60ceacSChris Mason } 1122eb60ceacSChris Mason 11237c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 11245f39d397SChris Mason struct extent_buffer *buf) 1125ed2ff2cbSChris Mason { 112655c69072SChris Mason if (btrfs_header_generation(buf) == 1127e2d84521SMiao Xie fs_info->running_transaction->transid) { 1128b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1129b4ce94deSChris Mason 1130b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1131104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1132e2d84521SMiao Xie -buf->len, 1133e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1134b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 11358bead258SDavid Sterba btrfs_set_lock_blocking_write(buf); 11360b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1137925baeddSChris Mason } 11385f39d397SChris Mason } 1139ed7b63ebSJosef Bacik } 11405f39d397SChris Mason 11418257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11428257b2dcSMiao Xie { 11438257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11448257b2dcSMiao Xie int ret; 11458257b2dcSMiao Xie 11468257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11478257b2dcSMiao Xie if (!writers) 11488257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11498257b2dcSMiao Xie 11508a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11518257b2dcSMiao Xie if (ret < 0) { 11528257b2dcSMiao Xie kfree(writers); 11538257b2dcSMiao Xie return ERR_PTR(ret); 11548257b2dcSMiao Xie } 11558257b2dcSMiao Xie 11568257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11578257b2dcSMiao Xie return writers; 11588257b2dcSMiao Xie } 11598257b2dcSMiao Xie 11608257b2dcSMiao Xie static void 11618257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11628257b2dcSMiao Xie { 11638257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11648257b2dcSMiao Xie kfree(writers); 11658257b2dcSMiao Xie } 11668257b2dcSMiao Xie 1167da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1168e20d96d6SChris Mason u64 objectid) 1169d97e63b6SChris Mason { 11707c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1171cfaa7295SChris Mason root->node = NULL; 1172a28ec197SChris Mason root->commit_root = NULL; 117327cdeb70SMiao Xie root->state = 0; 1174d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11750b86a832SChris Mason 11760f7d52f4SChris Mason root->last_trans = 0; 117713a8a7c8SYan, Zheng root->highest_objectid = 0; 1178eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1179199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11806bef4d31SEric Paris root->inode_tree = RB_ROOT; 118116cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1182f0486c68SYan, Zheng root->block_rsv = NULL; 11830b86a832SChris Mason 11840b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11855d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1186eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1187eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1188199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1189199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1190d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11912ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11922ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11935d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1194eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1195199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1196f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11972ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11982ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11998287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1200a2135011SChris Mason mutex_init(&root->objectid_mutex); 1201e02119d5SChris Mason mutex_init(&root->log_mutex); 120231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1203573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 12047237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12057237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12067237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12078b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 12088b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 12097237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12107237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12117237f183SYan Zheng atomic_set(&root->log_writers, 0); 12122ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12130700cea7SElena Reshetova refcount_set(&root->refs, 1); 1214ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 12158ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1216eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 12177237f183SYan Zheng root->log_transid = 0; 1218d1433debSMiao Xie root->log_transid_committed = -1; 1219257c62e1SChris Mason root->last_log_commit = 0; 12207c0260eeSJeff Mahoney if (!dummy) 122143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 122243eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1223017e5369SChris Mason 12243768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12253768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12266702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 12277c0260eeSJeff Mahoney if (!dummy) 12283f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 122906ea65a3SJosef Bacik else 123006ea65a3SJosef Bacik root->defrag_trans_start = 0; 12314d775673SChris Mason root->root_key.objectid = objectid; 12320ee5dc67SAl Viro root->anon_dev = 0; 12338ea05e3aSAlexander Block 12345f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1235370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 12363768f368SChris Mason } 12373768f368SChris Mason 123874e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 123974e4d827SDavid Sterba gfp_t flags) 12406f07e42eSAl Viro { 124174e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12426f07e42eSAl Viro if (root) 12436f07e42eSAl Viro root->fs_info = fs_info; 12446f07e42eSAl Viro return root; 12456f07e42eSAl Viro } 12466f07e42eSAl Viro 124706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 124806ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1249da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 125006ea65a3SJosef Bacik { 125106ea65a3SJosef Bacik struct btrfs_root *root; 125206ea65a3SJosef Bacik 12537c0260eeSJeff Mahoney if (!fs_info) 12547c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12557c0260eeSJeff Mahoney 12567c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 125706ea65a3SJosef Bacik if (!root) 125806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1259da17066cSJeff Mahoney 1260b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1261da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1262faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 126306ea65a3SJosef Bacik 126406ea65a3SJosef Bacik return root; 126506ea65a3SJosef Bacik } 126606ea65a3SJosef Bacik #endif 126706ea65a3SJosef Bacik 126820897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 126920897f5cSArne Jansen struct btrfs_fs_info *fs_info, 127020897f5cSArne Jansen u64 objectid) 127120897f5cSArne Jansen { 127220897f5cSArne Jansen struct extent_buffer *leaf; 127320897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 127420897f5cSArne Jansen struct btrfs_root *root; 127520897f5cSArne Jansen struct btrfs_key key; 1276b89f6d1fSFilipe Manana unsigned int nofs_flag; 127720897f5cSArne Jansen int ret = 0; 127833d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 127920897f5cSArne Jansen 1280b89f6d1fSFilipe Manana /* 1281b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1282b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1283b89f6d1fSFilipe Manana */ 1284b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 128574e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 1286b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 128720897f5cSArne Jansen if (!root) 128820897f5cSArne Jansen return ERR_PTR(-ENOMEM); 128920897f5cSArne Jansen 1290da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 129120897f5cSArne Jansen root->root_key.objectid = objectid; 129220897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 129320897f5cSArne Jansen root->root_key.offset = 0; 129420897f5cSArne Jansen 12954d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 129620897f5cSArne Jansen if (IS_ERR(leaf)) { 129720897f5cSArne Jansen ret = PTR_ERR(leaf); 12981dd05682STsutomu Itoh leaf = NULL; 129920897f5cSArne Jansen goto fail; 130020897f5cSArne Jansen } 130120897f5cSArne Jansen 130220897f5cSArne Jansen root->node = leaf; 130320897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 130420897f5cSArne Jansen 130520897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 130627cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 130720897f5cSArne Jansen 130820897f5cSArne Jansen root->root_item.flags = 0; 130920897f5cSArne Jansen root->root_item.byte_limit = 0; 131020897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 131120897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 131220897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 131320897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 131420897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 131520897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 131620897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 131733d85fdaSQu Wenruo if (is_fstree(objectid)) 13186463fe58SStefan Behrens uuid_le_gen(&uuid); 13196463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 132020897f5cSArne Jansen root->root_item.drop_level = 0; 132120897f5cSArne Jansen 132220897f5cSArne Jansen key.objectid = objectid; 132320897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 132420897f5cSArne Jansen key.offset = 0; 132520897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 132620897f5cSArne Jansen if (ret) 132720897f5cSArne Jansen goto fail; 132820897f5cSArne Jansen 132920897f5cSArne Jansen btrfs_tree_unlock(leaf); 133020897f5cSArne Jansen 133120897f5cSArne Jansen return root; 13321dd05682STsutomu Itoh 13331dd05682STsutomu Itoh fail: 13341dd05682STsutomu Itoh if (leaf) { 13351dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 133659885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13371dd05682STsutomu Itoh free_extent_buffer(leaf); 13381dd05682STsutomu Itoh } 13391dd05682STsutomu Itoh kfree(root); 13401dd05682STsutomu Itoh 13411dd05682STsutomu Itoh return ERR_PTR(ret); 134220897f5cSArne Jansen } 134320897f5cSArne Jansen 13447237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1345e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13460f7d52f4SChris Mason { 13470f7d52f4SChris Mason struct btrfs_root *root; 13487237f183SYan Zheng struct extent_buffer *leaf; 1349e02119d5SChris Mason 135074e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1351e02119d5SChris Mason if (!root) 13527237f183SYan Zheng return ERR_PTR(-ENOMEM); 1353e02119d5SChris Mason 1354da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1355e02119d5SChris Mason 1356e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1357e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1358e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 135927cdeb70SMiao Xie 13607237f183SYan Zheng /* 136127cdeb70SMiao Xie * DON'T set REF_COWS for log trees 136227cdeb70SMiao Xie * 13637237f183SYan Zheng * log trees do not get reference counted because they go away 13647237f183SYan Zheng * before a real commit is actually done. They do store pointers 13657237f183SYan Zheng * to file data extents, and those reference counts still get 13667237f183SYan Zheng * updated (along with back refs to the log tree). 13677237f183SYan Zheng */ 1368e02119d5SChris Mason 13694d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13704d75f8a9SDavid Sterba NULL, 0, 0, 0); 13717237f183SYan Zheng if (IS_ERR(leaf)) { 13727237f183SYan Zheng kfree(root); 13737237f183SYan Zheng return ERR_CAST(leaf); 13747237f183SYan Zheng } 1375e02119d5SChris Mason 13767237f183SYan Zheng root->node = leaf; 1377e02119d5SChris Mason 1378e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1379e02119d5SChris Mason btrfs_tree_unlock(root->node); 13807237f183SYan Zheng return root; 13817237f183SYan Zheng } 13827237f183SYan Zheng 13837237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13847237f183SYan Zheng struct btrfs_fs_info *fs_info) 13857237f183SYan Zheng { 13867237f183SYan Zheng struct btrfs_root *log_root; 13877237f183SYan Zheng 13887237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13897237f183SYan Zheng if (IS_ERR(log_root)) 13907237f183SYan Zheng return PTR_ERR(log_root); 13917237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13927237f183SYan Zheng fs_info->log_root_tree = log_root; 13937237f183SYan Zheng return 0; 13947237f183SYan Zheng } 13957237f183SYan Zheng 13967237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13977237f183SYan Zheng struct btrfs_root *root) 13987237f183SYan Zheng { 13990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14007237f183SYan Zheng struct btrfs_root *log_root; 14017237f183SYan Zheng struct btrfs_inode_item *inode_item; 14027237f183SYan Zheng 14030b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14047237f183SYan Zheng if (IS_ERR(log_root)) 14057237f183SYan Zheng return PTR_ERR(log_root); 14067237f183SYan Zheng 14077237f183SYan Zheng log_root->last_trans = trans->transid; 14087237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14097237f183SYan Zheng 14107237f183SYan Zheng inode_item = &log_root->root_item.inode; 14113cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14123cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14133cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1414da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14150b246afaSJeff Mahoney fs_info->nodesize); 14163cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14177237f183SYan Zheng 14185d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14197237f183SYan Zheng 14207237f183SYan Zheng WARN_ON(root->log_root); 14217237f183SYan Zheng root->log_root = log_root; 14227237f183SYan Zheng root->log_transid = 0; 1423d1433debSMiao Xie root->log_transid_committed = -1; 1424257c62e1SChris Mason root->last_log_commit = 0; 1425e02119d5SChris Mason return 0; 1426e02119d5SChris Mason } 1427e02119d5SChris Mason 142835a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1429cb517eabSMiao Xie struct btrfs_key *key) 1430e02119d5SChris Mason { 1431e02119d5SChris Mason struct btrfs_root *root; 1432e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14330f7d52f4SChris Mason struct btrfs_path *path; 143484234f3aSYan Zheng u64 generation; 1435cb517eabSMiao Xie int ret; 1436581c1760SQu Wenruo int level; 1437cb517eabSMiao Xie 1438cb517eabSMiao Xie path = btrfs_alloc_path(); 1439cb517eabSMiao Xie if (!path) 1440cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14410f7d52f4SChris Mason 144274e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1443cb517eabSMiao Xie if (!root) { 1444cb517eabSMiao Xie ret = -ENOMEM; 1445cb517eabSMiao Xie goto alloc_fail; 14460f7d52f4SChris Mason } 14470f7d52f4SChris Mason 1448da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 14490f7d52f4SChris Mason 1450cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1451cb517eabSMiao Xie &root->root_item, &root->root_key); 14520f7d52f4SChris Mason if (ret) { 145313a8a7c8SYan, Zheng if (ret > 0) 145413a8a7c8SYan, Zheng ret = -ENOENT; 1455cb517eabSMiao Xie goto find_fail; 14560f7d52f4SChris Mason } 145713a8a7c8SYan, Zheng 145884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1459581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14602ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14612ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1462581c1760SQu Wenruo generation, level, NULL); 146364c043deSLiu Bo if (IS_ERR(root->node)) { 146464c043deSLiu Bo ret = PTR_ERR(root->node); 1465cb517eabSMiao Xie goto find_fail; 1466cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1467cb517eabSMiao Xie ret = -EIO; 146864c043deSLiu Bo free_extent_buffer(root->node); 146964c043deSLiu Bo goto find_fail; 1470416bc658SJosef Bacik } 14715d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 147213a8a7c8SYan, Zheng out: 1473cb517eabSMiao Xie btrfs_free_path(path); 1474cb517eabSMiao Xie return root; 1475cb517eabSMiao Xie 1476cb517eabSMiao Xie find_fail: 1477cb517eabSMiao Xie kfree(root); 1478cb517eabSMiao Xie alloc_fail: 1479cb517eabSMiao Xie root = ERR_PTR(ret); 1480cb517eabSMiao Xie goto out; 1481cb517eabSMiao Xie } 1482cb517eabSMiao Xie 1483cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1484cb517eabSMiao Xie struct btrfs_key *location) 1485cb517eabSMiao Xie { 1486cb517eabSMiao Xie struct btrfs_root *root; 1487cb517eabSMiao Xie 1488cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1489cb517eabSMiao Xie if (IS_ERR(root)) 1490cb517eabSMiao Xie return root; 1491cb517eabSMiao Xie 1492cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 149327cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 149408fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 149508fe4db1SLi Zefan } 149613a8a7c8SYan, Zheng 14975eda7b5eSChris Mason return root; 14985eda7b5eSChris Mason } 14995eda7b5eSChris Mason 1500cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1501cb517eabSMiao Xie { 1502cb517eabSMiao Xie int ret; 15038257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1504cb517eabSMiao Xie 1505cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1506cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1507cb517eabSMiao Xie GFP_NOFS); 1508cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1509cb517eabSMiao Xie ret = -ENOMEM; 1510cb517eabSMiao Xie goto fail; 1511cb517eabSMiao Xie } 1512cb517eabSMiao Xie 15138257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 15148257b2dcSMiao Xie if (IS_ERR(writers)) { 15158257b2dcSMiao Xie ret = PTR_ERR(writers); 15168257b2dcSMiao Xie goto fail; 15178257b2dcSMiao Xie } 15188257b2dcSMiao Xie root->subv_writers = writers; 15198257b2dcSMiao Xie 1520cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 152157cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 152257cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1523cb517eabSMiao Xie 1524cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1525cb517eabSMiao Xie if (ret) 1526876d2cf1SLiu Bo goto fail; 1527f32e48e9SChandan Rajendra 1528f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1529f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1530f32e48e9SChandan Rajendra &root->highest_objectid); 1531f32e48e9SChandan Rajendra if (ret) { 1532f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1533876d2cf1SLiu Bo goto fail; 1534f32e48e9SChandan Rajendra } 1535f32e48e9SChandan Rajendra 1536f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1537f32e48e9SChandan Rajendra 1538f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1539f32e48e9SChandan Rajendra 1540cb517eabSMiao Xie return 0; 1541cb517eabSMiao Xie fail: 154284db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1543cb517eabSMiao Xie return ret; 1544cb517eabSMiao Xie } 1545cb517eabSMiao Xie 154635bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1547cb517eabSMiao Xie u64 root_id) 1548cb517eabSMiao Xie { 1549cb517eabSMiao Xie struct btrfs_root *root; 1550cb517eabSMiao Xie 1551cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1552cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1553cb517eabSMiao Xie (unsigned long)root_id); 1554cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1555cb517eabSMiao Xie return root; 1556cb517eabSMiao Xie } 1557cb517eabSMiao Xie 1558cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1559cb517eabSMiao Xie struct btrfs_root *root) 1560cb517eabSMiao Xie { 1561cb517eabSMiao Xie int ret; 1562cb517eabSMiao Xie 1563e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1564cb517eabSMiao Xie if (ret) 1565cb517eabSMiao Xie return ret; 1566cb517eabSMiao Xie 1567cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1568cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1569cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1570cb517eabSMiao Xie root); 1571cb517eabSMiao Xie if (ret == 0) 157227cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1573cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1574cb517eabSMiao Xie radix_tree_preload_end(); 1575cb517eabSMiao Xie 1576cb517eabSMiao Xie return ret; 1577cb517eabSMiao Xie } 1578cb517eabSMiao Xie 1579c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1580c00869f1SMiao Xie struct btrfs_key *location, 1581c00869f1SMiao Xie bool check_ref) 15825eda7b5eSChris Mason { 15835eda7b5eSChris Mason struct btrfs_root *root; 1584381cf658SDavid Sterba struct btrfs_path *path; 15851d4c08e0SDavid Sterba struct btrfs_key key; 15865eda7b5eSChris Mason int ret; 15875eda7b5eSChris Mason 1588edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1589edbd8d4eSChris Mason return fs_info->tree_root; 1590edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1591edbd8d4eSChris Mason return fs_info->extent_root; 15928f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15938f18cf13SChris Mason return fs_info->chunk_root; 15948f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15958f18cf13SChris Mason return fs_info->dev_root; 15960403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15970403e47eSYan Zheng return fs_info->csum_root; 1598bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1599bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1600bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1601f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1602f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1603f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 160470f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 160570f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 160670f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 16074df27c4dSYan, Zheng again: 1608cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 160948475471SStefan Behrens if (root) { 1610c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 161148475471SStefan Behrens return ERR_PTR(-ENOENT); 16125eda7b5eSChris Mason return root; 161348475471SStefan Behrens } 16145eda7b5eSChris Mason 1615cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 16165eda7b5eSChris Mason if (IS_ERR(root)) 16175eda7b5eSChris Mason return root; 16183394e160SChris Mason 1619c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1620d68fc57bSYan, Zheng ret = -ENOENT; 1621d68fc57bSYan, Zheng goto fail; 1622d68fc57bSYan, Zheng } 1623d68fc57bSYan, Zheng 1624cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1625cb517eabSMiao Xie if (ret) 1626cb517eabSMiao Xie goto fail; 1627cb517eabSMiao Xie 1628381cf658SDavid Sterba path = btrfs_alloc_path(); 1629381cf658SDavid Sterba if (!path) { 1630381cf658SDavid Sterba ret = -ENOMEM; 1631381cf658SDavid Sterba goto fail; 1632381cf658SDavid Sterba } 16331d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16341d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16351d4c08e0SDavid Sterba key.offset = location->objectid; 16361d4c08e0SDavid Sterba 16371d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1638381cf658SDavid Sterba btrfs_free_path(path); 1639d68fc57bSYan, Zheng if (ret < 0) 1640d68fc57bSYan, Zheng goto fail; 1641d68fc57bSYan, Zheng if (ret == 0) 164227cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1643d68fc57bSYan, Zheng 1644cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16450f7d52f4SChris Mason if (ret) { 16464df27c4dSYan, Zheng if (ret == -EEXIST) { 164784db5ccfSDavid Sterba btrfs_free_fs_root(root); 16484df27c4dSYan, Zheng goto again; 16490f7d52f4SChris Mason } 16504df27c4dSYan, Zheng goto fail; 16514df27c4dSYan, Zheng } 1652edbd8d4eSChris Mason return root; 16534df27c4dSYan, Zheng fail: 165484db5ccfSDavid Sterba btrfs_free_fs_root(root); 16554df27c4dSYan, Zheng return ERR_PTR(ret); 1656edbd8d4eSChris Mason } 1657edbd8d4eSChris Mason 165804160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 165904160088SChris Mason { 166004160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 166104160088SChris Mason int ret = 0; 166204160088SChris Mason struct btrfs_device *device; 166304160088SChris Mason struct backing_dev_info *bdi; 1664b7967db7SChris Mason 16651f78160cSXiao Guangrong rcu_read_lock(); 16661f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1667dfe25020SChris Mason if (!device->bdev) 1668dfe25020SChris Mason continue; 1669efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1670ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 167104160088SChris Mason ret = 1; 167204160088SChris Mason break; 167304160088SChris Mason } 167404160088SChris Mason } 16751f78160cSXiao Guangrong rcu_read_unlock(); 167604160088SChris Mason return ret; 167704160088SChris Mason } 167804160088SChris Mason 16798b712842SChris Mason /* 16808b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16818b712842SChris Mason * functions. This is where read checksum verification actually happens 16828b712842SChris Mason */ 16838b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1684ce9adaa5SChris Mason { 1685ce9adaa5SChris Mason struct bio *bio; 168697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1687ce9adaa5SChris Mason 168897eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1689ce9adaa5SChris Mason bio = end_io_wq->bio; 16908b712842SChris Mason 16914e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1692ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1693ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 169497eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 16954246a0b6SChristoph Hellwig bio_endio(bio); 1696ce9adaa5SChris Mason } 169744b8bd7eSChris Mason 1698a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1699a74a4b97SChris Mason { 1700a74a4b97SChris Mason struct btrfs_root *root = arg; 17010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1702d0278245SMiao Xie int again; 1703a74a4b97SChris Mason 1704d6fd0ae2SOmar Sandoval while (1) { 1705d0278245SMiao Xie again = 0; 17069d1a2a3aSDavid Sterba 1707fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1708fd340d0fSJosef Bacik 1709d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17102ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1711d0278245SMiao Xie goto sleep; 1712d0278245SMiao Xie 171390c711abSZygo Blaxell /* 171490c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 171590c711abSZygo Blaxell * before we have finished mounting the filesystem. 171690c711abSZygo Blaxell */ 17170b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 171890c711abSZygo Blaxell goto sleep; 171990c711abSZygo Blaxell 17200b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1721d0278245SMiao Xie goto sleep; 1722d0278245SMiao Xie 1723dc7f370cSMiao Xie /* 1724dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1725dc7f370cSMiao Xie * during the above check and trylock. 1726dc7f370cSMiao Xie */ 17272ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17280b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1729dc7f370cSMiao Xie goto sleep; 1730dc7f370cSMiao Xie } 1731dc7f370cSMiao Xie 17322ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1733c2d6cb16SFilipe Manana 17349d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17350b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1736a74a4b97SChris Mason 1737d0278245SMiao Xie /* 173805323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 173905323cd1SMiao Xie * needn't do anything special here. 1740d0278245SMiao Xie */ 17410b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 174267c5e7d4SFilipe Manana 174367c5e7d4SFilipe Manana /* 174467c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 174567c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 174667c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 174767c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 174867c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 174967c5e7d4SFilipe Manana * unused block groups. 175067c5e7d4SFilipe Manana */ 17510b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1752d0278245SMiao Xie sleep: 1753fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1754d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1755d6fd0ae2SOmar Sandoval kthread_parkme(); 1756d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1757d6fd0ae2SOmar Sandoval return 0; 1758838fe188SJiri Kosina if (!again) { 1759a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1760a74a4b97SChris Mason schedule(); 1761a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1762a74a4b97SChris Mason } 1763da288d28SFilipe Manana } 1764a74a4b97SChris Mason } 1765a74a4b97SChris Mason 1766a74a4b97SChris Mason static int transaction_kthread(void *arg) 1767a74a4b97SChris Mason { 1768a74a4b97SChris Mason struct btrfs_root *root = arg; 17690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1770a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1771a74a4b97SChris Mason struct btrfs_transaction *cur; 17728929ecfaSYan, Zheng u64 transid; 1773a944442cSAllen Pais time64_t now; 1774a74a4b97SChris Mason unsigned long delay; 1775914b2007SJan Kara bool cannot_commit; 1776a74a4b97SChris Mason 1777a74a4b97SChris Mason do { 1778914b2007SJan Kara cannot_commit = false; 17790b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17800b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1781a74a4b97SChris Mason 17820b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17830b246afaSJeff Mahoney cur = fs_info->running_transaction; 1784a74a4b97SChris Mason if (!cur) { 17850b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1786a74a4b97SChris Mason goto sleep; 1787a74a4b97SChris Mason } 178831153d81SYan Zheng 1789afd48513SArnd Bergmann now = ktime_get_seconds(); 17904a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 1791a514d638SQu Wenruo !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) && 17928b87dc17SDavid Sterba (now < cur->start_time || 17930b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17940b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1795a74a4b97SChris Mason delay = HZ * 5; 1796a74a4b97SChris Mason goto sleep; 1797a74a4b97SChris Mason } 17988929ecfaSYan, Zheng transid = cur->transid; 17990b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 180056bec294SChris Mason 180179787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1802354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1803914b2007SJan Kara if (IS_ERR(trans)) { 1804354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1805914b2007SJan Kara cannot_commit = true; 180679787eaaSJeff Mahoney goto sleep; 1807914b2007SJan Kara } 18088929ecfaSYan, Zheng if (transid == trans->transid) { 18093a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18108929ecfaSYan, Zheng } else { 18113a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18128929ecfaSYan, Zheng } 1813a74a4b97SChris Mason sleep: 18140b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18150b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1816a74a4b97SChris Mason 18174e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18180b246afaSJeff Mahoney &fs_info->fs_state))) 18192ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18208929ecfaSYan, Zheng if (!kthread_should_stop() && 18210b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1822914b2007SJan Kara cannot_commit)) 1823bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1824a74a4b97SChris Mason } while (!kthread_should_stop()); 1825a74a4b97SChris Mason return 0; 1826a74a4b97SChris Mason } 1827a74a4b97SChris Mason 1828af31f5e5SChris Mason /* 1829af31f5e5SChris Mason * this will find the highest generation in the array of 1830af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1831af31f5e5SChris Mason * or -1 if we can't find anything. 1832af31f5e5SChris Mason * 1833af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1834af31f5e5SChris Mason * generation of the latest root in the array with the generation 1835af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1836af31f5e5SChris Mason */ 1837af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1838af31f5e5SChris Mason { 1839af31f5e5SChris Mason u64 cur; 1840af31f5e5SChris Mason int newest_index = -1; 1841af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1842af31f5e5SChris Mason int i; 1843af31f5e5SChris Mason 1844af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1845af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1846af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1847af31f5e5SChris Mason if (cur == newest_gen) 1848af31f5e5SChris Mason newest_index = i; 1849af31f5e5SChris Mason } 1850af31f5e5SChris Mason 1851af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1852af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1853af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1854af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1855af31f5e5SChris Mason if (cur == newest_gen) 1856af31f5e5SChris Mason newest_index = 0; 1857af31f5e5SChris Mason } 1858af31f5e5SChris Mason return newest_index; 1859af31f5e5SChris Mason } 1860af31f5e5SChris Mason 1861af31f5e5SChris Mason 1862af31f5e5SChris Mason /* 1863af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1864af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1865af31f5e5SChris Mason * field in the fs_info struct 1866af31f5e5SChris Mason */ 1867af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1868af31f5e5SChris Mason u64 newest_gen) 1869af31f5e5SChris Mason { 1870af31f5e5SChris Mason int newest_index = -1; 1871af31f5e5SChris Mason 1872af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1873af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1874af31f5e5SChris Mason if (newest_index == -1) { 1875af31f5e5SChris Mason info->backup_root_index = 0; 1876af31f5e5SChris Mason } else { 1877af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1878af31f5e5SChris Mason } 1879af31f5e5SChris Mason } 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason /* 1882af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1883af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1884af31f5e5SChris Mason * done 1885af31f5e5SChris Mason */ 1886af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1887af31f5e5SChris Mason { 1888af31f5e5SChris Mason int next_backup; 1889af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1890af31f5e5SChris Mason int last_backup; 1891af31f5e5SChris Mason 1892af31f5e5SChris Mason next_backup = info->backup_root_index; 1893af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1894af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1895af31f5e5SChris Mason 1896af31f5e5SChris Mason /* 1897af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1898af31f5e5SChris Mason * this happens only at umount 1899af31f5e5SChris Mason */ 1900af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1901af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1902af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1903af31f5e5SChris Mason next_backup = last_backup; 1904af31f5e5SChris Mason 1905af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason /* 1908af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1909af31f5e5SChris Mason * regardless of which ones we use today 1910af31f5e5SChris Mason */ 1911af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1912af31f5e5SChris Mason 1913af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1914af31f5e5SChris Mason 1915af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1916af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1917af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1918af31f5e5SChris Mason 1919af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1927af31f5e5SChris Mason 1928af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1929af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1930af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1931af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1932af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1933af31f5e5SChris Mason 19347c7e82a7SChris Mason /* 19357c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19367c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19377c7e82a7SChris Mason */ 19387c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19397c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19407c7e82a7SChris Mason info->fs_root->node->start); 1941af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1942af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1943af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1944af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19457c7e82a7SChris Mason } 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1948af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1949af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1950af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1951af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1952af31f5e5SChris Mason 1953af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1954af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1955af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1956af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1957af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1960af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1961af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1962af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1963af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1964af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1965af31f5e5SChris Mason 1966af31f5e5SChris Mason /* 1967af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1968af31f5e5SChris Mason * for the next commit 1969af31f5e5SChris Mason */ 1970af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1971af31f5e5SChris Mason &info->super_for_commit->super_roots, 1972af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1973af31f5e5SChris Mason } 1974af31f5e5SChris Mason 1975af31f5e5SChris Mason /* 1976af31f5e5SChris Mason * this copies info out of the root backup array and back into 1977af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1978af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1979af31f5e5SChris Mason * tried and the last backup index you used. 1980af31f5e5SChris Mason * 1981af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1982af31f5e5SChris Mason */ 1983af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1984af31f5e5SChris Mason struct btrfs_super_block *super, 1985af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1986af31f5e5SChris Mason { 1987af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1988af31f5e5SChris Mason int newest = *backup_index; 1989af31f5e5SChris Mason 1990af31f5e5SChris Mason if (*num_backups_tried == 0) { 1991af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1992af31f5e5SChris Mason 1993af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1994af31f5e5SChris Mason if (newest == -1) 1995af31f5e5SChris Mason return -1; 1996af31f5e5SChris Mason 1997af31f5e5SChris Mason *backup_index = newest; 1998af31f5e5SChris Mason *num_backups_tried = 1; 1999af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2000af31f5e5SChris Mason /* we've tried all the backups, all done */ 2001af31f5e5SChris Mason return -1; 2002af31f5e5SChris Mason } else { 2003af31f5e5SChris Mason /* jump to the next oldest backup */ 2004af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2005af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2006af31f5e5SChris Mason *backup_index = newest; 2007af31f5e5SChris Mason *num_backups_tried += 1; 2008af31f5e5SChris Mason } 2009af31f5e5SChris Mason root_backup = super->super_roots + newest; 2010af31f5e5SChris Mason 2011af31f5e5SChris Mason btrfs_set_super_generation(super, 2012af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2013af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2014af31f5e5SChris Mason btrfs_set_super_root_level(super, 2015af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2016af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2017af31f5e5SChris Mason 2018af31f5e5SChris Mason /* 2019af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2020af31f5e5SChris Mason * need a fsck 2021af31f5e5SChris Mason */ 2022af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2023af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2024af31f5e5SChris Mason return 0; 2025af31f5e5SChris Mason } 2026af31f5e5SChris Mason 20277abadb64SLiu Bo /* helper to cleanup workers */ 20287abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20297abadb64SLiu Bo { 2030dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2031afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20325cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2033fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2034fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20358b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2036d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2037fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2038fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2039a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20405b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2041e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2042736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2043a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2044fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2045a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2046a9b9477dSFilipe Manana /* 2047a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2048a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2049a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2050a9b9477dSFilipe Manana */ 2051a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2052a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20537abadb64SLiu Bo } 20547abadb64SLiu Bo 20552e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20562e9f5954SRashika { 20572e9f5954SRashika if (root) { 20582e9f5954SRashika free_extent_buffer(root->node); 20592e9f5954SRashika free_extent_buffer(root->commit_root); 20602e9f5954SRashika root->node = NULL; 20612e9f5954SRashika root->commit_root = NULL; 20622e9f5954SRashika } 20632e9f5954SRashika } 20642e9f5954SRashika 2065af31f5e5SChris Mason /* helper to cleanup tree roots */ 2066af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2067af31f5e5SChris Mason { 20682e9f5954SRashika free_root_extent_buffers(info->tree_root); 2069655b09feSJosef Bacik 20702e9f5954SRashika free_root_extent_buffers(info->dev_root); 20712e9f5954SRashika free_root_extent_buffers(info->extent_root); 20722e9f5954SRashika free_root_extent_buffers(info->csum_root); 20732e9f5954SRashika free_root_extent_buffers(info->quota_root); 20742e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20752e9f5954SRashika if (chunk_root) 20762e9f5954SRashika free_root_extent_buffers(info->chunk_root); 207770f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2078af31f5e5SChris Mason } 2079af31f5e5SChris Mason 2080faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2081171f6537SJosef Bacik { 2082171f6537SJosef Bacik int ret; 2083171f6537SJosef Bacik struct btrfs_root *gang[8]; 2084171f6537SJosef Bacik int i; 2085171f6537SJosef Bacik 2086171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2087171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2088171f6537SJosef Bacik struct btrfs_root, root_list); 2089171f6537SJosef Bacik list_del(&gang[0]->root_list); 2090171f6537SJosef Bacik 209127cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2092cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2093171f6537SJosef Bacik } else { 2094171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2095171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2096b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2097171f6537SJosef Bacik } 2098171f6537SJosef Bacik } 2099171f6537SJosef Bacik 2100171f6537SJosef Bacik while (1) { 2101171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2102171f6537SJosef Bacik (void **)gang, 0, 2103171f6537SJosef Bacik ARRAY_SIZE(gang)); 2104171f6537SJosef Bacik if (!ret) 2105171f6537SJosef Bacik break; 2106171f6537SJosef Bacik for (i = 0; i < ret; i++) 2107cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2108171f6537SJosef Bacik } 21091a4319ccSLiu Bo 21101a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 21111a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 21122ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 21131a4319ccSLiu Bo } 2114171f6537SJosef Bacik } 2115af31f5e5SChris Mason 2116638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2117638aa7edSEric Sandeen { 2118638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2119638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2120638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2121638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2122638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2123638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2124ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2125638aa7edSEric Sandeen } 2126638aa7edSEric Sandeen 2127779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2128779a65a4SEric Sandeen { 2129779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2130779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2131779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2132779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2133779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2134779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2135779a65a4SEric Sandeen } 2136779a65a4SEric Sandeen 21376bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2138f37938e0SEric Sandeen { 21392ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21402ff7e61eSJeff Mahoney 21412ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21422ff7e61eSJeff Mahoney set_nlink(inode, 1); 2143f37938e0SEric Sandeen /* 2144f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2145f37938e0SEric Sandeen * the real end of the address space is determined by all of 2146f37938e0SEric Sandeen * the devices in the system 2147f37938e0SEric Sandeen */ 21482ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21492ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2150f37938e0SEric Sandeen 21512ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 215243eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 215343eb5f29SQu Wenruo IO_TREE_INODE_IO, inode); 21547b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21552ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2156f37938e0SEric Sandeen 21572ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2158f37938e0SEric Sandeen 21592ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21602ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21612ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21622ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2163f37938e0SEric Sandeen } 2164f37938e0SEric Sandeen 2165ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2166ad618368SEric Sandeen { 2167ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2168129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21697f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2170ad618368SEric Sandeen } 2171ad618368SEric Sandeen 2172f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2173f9e92e40SEric Sandeen { 2174f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2175f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2176f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2177f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2178f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2179f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2180f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2181d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2182f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2183f9e92e40SEric Sandeen } 2184f9e92e40SEric Sandeen 21852a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21862a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21872a458198SEric Sandeen { 2188f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21896f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21902a458198SEric Sandeen 21912a458198SEric Sandeen fs_info->workers = 2192cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2193cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21942a458198SEric Sandeen 21952a458198SEric Sandeen fs_info->delalloc_workers = 2196cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2197cb001095SJeff Mahoney flags, max_active, 2); 21982a458198SEric Sandeen 21992a458198SEric Sandeen fs_info->flush_workers = 2200cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2201cb001095SJeff Mahoney flags, max_active, 0); 22022a458198SEric Sandeen 22032a458198SEric Sandeen fs_info->caching_workers = 2204cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22052a458198SEric Sandeen 22062a458198SEric Sandeen /* 22072a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 22082a458198SEric Sandeen * likely that bios will be send down in a sane order to the 22092a458198SEric Sandeen * devices 22102a458198SEric Sandeen */ 22112a458198SEric Sandeen fs_info->submit_workers = 2212cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 22132a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22142a458198SEric Sandeen max_active), 64); 22152a458198SEric Sandeen 22162a458198SEric Sandeen fs_info->fixup_workers = 2217cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22182a458198SEric Sandeen 22192a458198SEric Sandeen /* 22202a458198SEric Sandeen * endios are largely parallel and should have a very 22212a458198SEric Sandeen * low idle thresh 22222a458198SEric Sandeen */ 22232a458198SEric Sandeen fs_info->endio_workers = 2224cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22252a458198SEric Sandeen fs_info->endio_meta_workers = 2226cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2227cb001095SJeff Mahoney max_active, 4); 22282a458198SEric Sandeen fs_info->endio_meta_write_workers = 2229cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2230cb001095SJeff Mahoney max_active, 2); 22312a458198SEric Sandeen fs_info->endio_raid56_workers = 2232cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2233cb001095SJeff Mahoney max_active, 4); 22342a458198SEric Sandeen fs_info->endio_repair_workers = 2235cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 22362a458198SEric Sandeen fs_info->rmw_workers = 2237cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22382a458198SEric Sandeen fs_info->endio_write_workers = 2239cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2240cb001095SJeff Mahoney max_active, 2); 22412a458198SEric Sandeen fs_info->endio_freespace_worker = 2242cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2243cb001095SJeff Mahoney max_active, 0); 22442a458198SEric Sandeen fs_info->delayed_workers = 2245cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2246cb001095SJeff Mahoney max_active, 0); 22472a458198SEric Sandeen fs_info->readahead_workers = 2248cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2249cb001095SJeff Mahoney max_active, 2); 22502a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2251cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 22522a458198SEric Sandeen fs_info->extent_workers = 2253cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 22542a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22552a458198SEric Sandeen max_active), 8); 22562a458198SEric Sandeen 22572a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 22582a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 22592a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22602a458198SEric Sandeen fs_info->endio_meta_write_workers && 22612a458198SEric Sandeen fs_info->endio_repair_workers && 22622a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22632a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22642a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22652a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 22662a458198SEric Sandeen fs_info->extent_workers && 22672a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 22682a458198SEric Sandeen return -ENOMEM; 22692a458198SEric Sandeen } 22702a458198SEric Sandeen 22712a458198SEric Sandeen return 0; 22722a458198SEric Sandeen } 22732a458198SEric Sandeen 227463443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 227563443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 227663443bf5SEric Sandeen { 227763443bf5SEric Sandeen int ret; 227863443bf5SEric Sandeen struct btrfs_root *log_tree_root; 227963443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 228063443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2281581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 228263443bf5SEric Sandeen 228363443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2284f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 228563443bf5SEric Sandeen return -EIO; 228663443bf5SEric Sandeen } 228763443bf5SEric Sandeen 228874e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 228963443bf5SEric Sandeen if (!log_tree_root) 229063443bf5SEric Sandeen return -ENOMEM; 229163443bf5SEric Sandeen 2292da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 229363443bf5SEric Sandeen 22942ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2295581c1760SQu Wenruo fs_info->generation + 1, 2296581c1760SQu Wenruo level, NULL); 229764c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2298f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22990eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 230064c043deSLiu Bo kfree(log_tree_root); 23010eeff236SLiu Bo return ret; 230264c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2303f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 230463443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 230563443bf5SEric Sandeen kfree(log_tree_root); 230663443bf5SEric Sandeen return -EIO; 230763443bf5SEric Sandeen } 230863443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 230963443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 231063443bf5SEric Sandeen if (ret) { 23110b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 231263443bf5SEric Sandeen "Failed to recover log tree"); 231363443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 231463443bf5SEric Sandeen kfree(log_tree_root); 231563443bf5SEric Sandeen return ret; 231663443bf5SEric Sandeen } 231763443bf5SEric Sandeen 2318bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23196bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 232063443bf5SEric Sandeen if (ret) 232163443bf5SEric Sandeen return ret; 232263443bf5SEric Sandeen } 232363443bf5SEric Sandeen 232463443bf5SEric Sandeen return 0; 232563443bf5SEric Sandeen } 232663443bf5SEric Sandeen 23276bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23284bbcaa64SEric Sandeen { 23296bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2330a4f3d2c4SDavid Sterba struct btrfs_root *root; 23314bbcaa64SEric Sandeen struct btrfs_key location; 23324bbcaa64SEric Sandeen int ret; 23334bbcaa64SEric Sandeen 23346bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23356bccf3abSJeff Mahoney 23364bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23374bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23384bbcaa64SEric Sandeen location.offset = 0; 23394bbcaa64SEric Sandeen 2340a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2341f50f4353SLiu Bo if (IS_ERR(root)) { 2342f50f4353SLiu Bo ret = PTR_ERR(root); 2343f50f4353SLiu Bo goto out; 2344f50f4353SLiu Bo } 2345a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2346a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23474bbcaa64SEric Sandeen 23484bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2349a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2350f50f4353SLiu Bo if (IS_ERR(root)) { 2351f50f4353SLiu Bo ret = PTR_ERR(root); 2352f50f4353SLiu Bo goto out; 2353f50f4353SLiu Bo } 2354a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2355a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23564bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23574bbcaa64SEric Sandeen 23584bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2359a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2360f50f4353SLiu Bo if (IS_ERR(root)) { 2361f50f4353SLiu Bo ret = PTR_ERR(root); 2362f50f4353SLiu Bo goto out; 2363f50f4353SLiu Bo } 2364a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2365a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23664bbcaa64SEric Sandeen 23674bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2368a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2369a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2370a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2371afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2372a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23734bbcaa64SEric Sandeen } 23744bbcaa64SEric Sandeen 23754bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2376a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2377a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2378a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23794bbcaa64SEric Sandeen if (ret != -ENOENT) 2380f50f4353SLiu Bo goto out; 23814bbcaa64SEric Sandeen } else { 2382a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2383a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23844bbcaa64SEric Sandeen } 23854bbcaa64SEric Sandeen 238670f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 238770f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 238870f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2389f50f4353SLiu Bo if (IS_ERR(root)) { 2390f50f4353SLiu Bo ret = PTR_ERR(root); 2391f50f4353SLiu Bo goto out; 2392f50f4353SLiu Bo } 239370f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 239470f6d82eSOmar Sandoval fs_info->free_space_root = root; 239570f6d82eSOmar Sandoval } 239670f6d82eSOmar Sandoval 23974bbcaa64SEric Sandeen return 0; 2398f50f4353SLiu Bo out: 2399f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2400f50f4353SLiu Bo location.objectid, ret); 2401f50f4353SLiu Bo return ret; 24024bbcaa64SEric Sandeen } 24034bbcaa64SEric Sandeen 2404069ec957SQu Wenruo /* 2405069ec957SQu Wenruo * Real super block validation 2406069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2407069ec957SQu Wenruo * 2408069ec957SQu Wenruo * @sb: super block to check 2409069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2410069ec957SQu Wenruo * 0 the primary (1st) sb 2411069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2412069ec957SQu Wenruo * -1 skip bytenr check 2413069ec957SQu Wenruo */ 2414069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2415069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 241621a852b0SQu Wenruo { 241721a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 241821a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 241921a852b0SQu Wenruo int ret = 0; 242021a852b0SQu Wenruo 242121a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 242221a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 242321a852b0SQu Wenruo ret = -EINVAL; 242421a852b0SQu Wenruo } 242521a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 242621a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 242721a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 242821a852b0SQu Wenruo ret = -EINVAL; 242921a852b0SQu Wenruo } 243021a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 243121a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 243221a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 243321a852b0SQu Wenruo ret = -EINVAL; 243421a852b0SQu Wenruo } 243521a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 243621a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 243721a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 243821a852b0SQu Wenruo ret = -EINVAL; 243921a852b0SQu Wenruo } 244021a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 244121a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 244221a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 244321a852b0SQu Wenruo ret = -EINVAL; 244421a852b0SQu Wenruo } 244521a852b0SQu Wenruo 244621a852b0SQu Wenruo /* 244721a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 244821a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 244921a852b0SQu Wenruo */ 245021a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 245121a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 245221a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 245321a852b0SQu Wenruo ret = -EINVAL; 245421a852b0SQu Wenruo } 245521a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 245621a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 245721a852b0SQu Wenruo btrfs_err(fs_info, 245821a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 245921a852b0SQu Wenruo sectorsize, PAGE_SIZE); 246021a852b0SQu Wenruo ret = -EINVAL; 246121a852b0SQu Wenruo } 246221a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 246321a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 246421a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 246521a852b0SQu Wenruo ret = -EINVAL; 246621a852b0SQu Wenruo } 246721a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 246821a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 246921a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 247021a852b0SQu Wenruo ret = -EINVAL; 247121a852b0SQu Wenruo } 247221a852b0SQu Wenruo 247321a852b0SQu Wenruo /* Root alignment check */ 247421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 247521a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 247621a852b0SQu Wenruo btrfs_super_root(sb)); 247721a852b0SQu Wenruo ret = -EINVAL; 247821a852b0SQu Wenruo } 247921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 248021a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 248121a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 248221a852b0SQu Wenruo ret = -EINVAL; 248321a852b0SQu Wenruo } 248421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 248521a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 248621a852b0SQu Wenruo btrfs_super_log_root(sb)); 248721a852b0SQu Wenruo ret = -EINVAL; 248821a852b0SQu Wenruo } 248921a852b0SQu Wenruo 2490de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24917239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 249221a852b0SQu Wenruo btrfs_err(fs_info, 24937239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2494de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 249521a852b0SQu Wenruo ret = -EINVAL; 249621a852b0SQu Wenruo } 249721a852b0SQu Wenruo 249821a852b0SQu Wenruo /* 249921a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 250021a852b0SQu Wenruo * done later 250121a852b0SQu Wenruo */ 250221a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 250321a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 250421a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 250521a852b0SQu Wenruo ret = -EINVAL; 250621a852b0SQu Wenruo } 250721a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 250821a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 250921a852b0SQu Wenruo btrfs_super_stripesize(sb)); 251021a852b0SQu Wenruo ret = -EINVAL; 251121a852b0SQu Wenruo } 251221a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 251321a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 251421a852b0SQu Wenruo btrfs_super_num_devices(sb)); 251521a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 251621a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 251721a852b0SQu Wenruo ret = -EINVAL; 251821a852b0SQu Wenruo } 251921a852b0SQu Wenruo 2520069ec957SQu Wenruo if (mirror_num >= 0 && 2521069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 252221a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 252321a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 252421a852b0SQu Wenruo ret = -EINVAL; 252521a852b0SQu Wenruo } 252621a852b0SQu Wenruo 252721a852b0SQu Wenruo /* 252821a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 252921a852b0SQu Wenruo * and one chunk 253021a852b0SQu Wenruo */ 253121a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 253221a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 253321a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 253421a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 253521a852b0SQu Wenruo ret = -EINVAL; 253621a852b0SQu Wenruo } 253721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 253821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 253921a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 254021a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 254121a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 254221a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 254321a852b0SQu Wenruo ret = -EINVAL; 254421a852b0SQu Wenruo } 254521a852b0SQu Wenruo 254621a852b0SQu Wenruo /* 254721a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 254821a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 254921a852b0SQu Wenruo */ 255021a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 255121a852b0SQu Wenruo btrfs_warn(fs_info, 255221a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 255321a852b0SQu Wenruo btrfs_super_generation(sb), 255421a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 255521a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 255621a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 255721a852b0SQu Wenruo btrfs_warn(fs_info, 255821a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 255921a852b0SQu Wenruo btrfs_super_generation(sb), 256021a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 256121a852b0SQu Wenruo 256221a852b0SQu Wenruo return ret; 256321a852b0SQu Wenruo } 256421a852b0SQu Wenruo 2565069ec957SQu Wenruo /* 2566069ec957SQu Wenruo * Validation of super block at mount time. 2567069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2568069ec957SQu Wenruo * flags will be skipped. 2569069ec957SQu Wenruo */ 2570069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2571069ec957SQu Wenruo { 2572069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2573069ec957SQu Wenruo } 2574069ec957SQu Wenruo 257575cb857dSQu Wenruo /* 257675cb857dSQu Wenruo * Validation of super block at write time. 257775cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 257875cb857dSQu Wenruo * overwritten soon. 257975cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 258075cb857dSQu Wenruo */ 258175cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 258275cb857dSQu Wenruo struct btrfs_super_block *sb) 258375cb857dSQu Wenruo { 258475cb857dSQu Wenruo int ret; 258575cb857dSQu Wenruo 258675cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 258775cb857dSQu Wenruo if (ret < 0) 258875cb857dSQu Wenruo goto out; 258975cb857dSQu Wenruo if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) { 259075cb857dSQu Wenruo ret = -EUCLEAN; 259175cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 259275cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 259375cb857dSQu Wenruo goto out; 259475cb857dSQu Wenruo } 259575cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 259675cb857dSQu Wenruo ret = -EUCLEAN; 259775cb857dSQu Wenruo btrfs_err(fs_info, 259875cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 259975cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 260075cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 260175cb857dSQu Wenruo goto out; 260275cb857dSQu Wenruo } 260375cb857dSQu Wenruo out: 260475cb857dSQu Wenruo if (ret < 0) 260575cb857dSQu Wenruo btrfs_err(fs_info, 260675cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 260775cb857dSQu Wenruo return ret; 260875cb857dSQu Wenruo } 260975cb857dSQu Wenruo 2610ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2611dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2612dfe25020SChris Mason char *options) 2613eb60ceacSChris Mason { 2614db94535dSChris Mason u32 sectorsize; 2615db94535dSChris Mason u32 nodesize; 261687ee04ebSChris Mason u32 stripesize; 261784234f3aSYan Zheng u64 generation; 2618f2b636e8SJosef Bacik u64 features; 26193de4586cSChris Mason struct btrfs_key location; 2620a061fc8dSChris Mason struct buffer_head *bh; 26214d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2622815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2623f84a8bd6SAl Viro struct btrfs_root *tree_root; 26244d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2625eb60ceacSChris Mason int ret; 2626e58ca020SYan int err = -EINVAL; 2627af31f5e5SChris Mason int num_backups_tried = 0; 2628af31f5e5SChris Mason int backup_index = 0; 26296675df31SOmar Sandoval int clear_free_space_tree = 0; 2630581c1760SQu Wenruo int level; 26314543df7eSChris Mason 263274e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 263374e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2634cb517eabSMiao Xie if (!tree_root || !chunk_root) { 263539279cc3SChris Mason err = -ENOMEM; 263639279cc3SChris Mason goto fail; 263739279cc3SChris Mason } 263876dda93cSYan, Zheng 263976dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 264076dda93cSYan, Zheng if (ret) { 264176dda93cSYan, Zheng err = ret; 264276dda93cSYan, Zheng goto fail; 264376dda93cSYan, Zheng } 264476dda93cSYan, Zheng 2645908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2646e2d84521SMiao Xie if (ret) { 2647e2d84521SMiao Xie err = ret; 26489e11ceeeSJan Kara goto fail_srcu; 2649e2d84521SMiao Xie } 265009cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2651e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2652e2d84521SMiao Xie 2653908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2654963d678bSMiao Xie if (ret) { 2655963d678bSMiao Xie err = ret; 2656963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2657963d678bSMiao Xie } 2658963d678bSMiao Xie 26597f8d236aSDavid Sterba ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 26607f8d236aSDavid Sterba GFP_KERNEL); 2661c404e0dcSMiao Xie if (ret) { 2662c404e0dcSMiao Xie err = ret; 2663c404e0dcSMiao Xie goto fail_delalloc_bytes; 2664c404e0dcSMiao Xie } 2665c404e0dcSMiao Xie 266676dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2667f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26688fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2669facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 267024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2671eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 267211833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 267375cb379dSJeff Mahoney INIT_LIST_HEAD(&fs_info->pending_raid_kobjs); 267475cb379dSJeff Mahoney spin_lock_init(&fs_info->pending_raid_kobjs_lock); 2675eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2676a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 267776dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 267824bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26794cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2680f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2681ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2682fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2683f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 268447ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2685f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2686d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 268767c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26887585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2689573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2690de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 269119c00ddcSChris Mason 26920b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26936324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2694f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 269547ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 26960b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 269766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 269866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 269966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 270066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 270166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 270266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 270366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2704ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2705ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2706ba2c4d4eSJosef Bacik 2707771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27084cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2709fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 27102fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2711034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2712fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2713e20d96d6SChris Mason fs_info->sb = sb; 271495ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27159ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27164cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2717a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2718f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27198b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2720f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 272190519d66SArne Jansen /* readahead state */ 2722d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 272390519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2724fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2725c8b97818SChris Mason 2726b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2727b34b086cSChris Mason num_online_cpus() + 2, 8); 27280afbaf8cSChris Mason 2729199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2730199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 273169fe2d75SJosef Bacik 273269fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 273369fe2d75SJosef Bacik if (!fs_info->btree_inode) { 273469fe2d75SJosef Bacik err = -ENOMEM; 273569fe2d75SJosef Bacik goto fail_bio_counter; 273669fe2d75SJosef Bacik } 273769fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 273869fe2d75SJosef Bacik 273916cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 274074e4d827SDavid Sterba GFP_KERNEL); 274116cdcec7SMiao Xie if (!fs_info->delayed_root) { 274216cdcec7SMiao Xie err = -ENOMEM; 274316cdcec7SMiao Xie goto fail_iput; 274416cdcec7SMiao Xie } 274516cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 27463eaa2885SChris Mason 2747638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 274821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 274921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 275021adbd5cSStefan Behrens #endif 2751779a65a4SEric Sandeen btrfs_init_balance(fs_info); 275221c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2753a2de733cSArne Jansen 27549f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 27559f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2756a061fc8dSChris Mason 27576bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 275839279cc3SChris Mason 2759e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 27606bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2761a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2762925baeddSChris Mason 276343eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[0], 276443eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS0, NULL); 276543eb5f29SQu Wenruo extent_io_tree_init(fs_info, &fs_info->freed_extents[1], 276643eb5f29SQu Wenruo IO_TREE_FS_INFO_FREED_EXTENTS1, NULL); 276711833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2768afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2769509659cdSChris Mason 27705a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 277179154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2772925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2773a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2774a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27751bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27769e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2777c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 277876dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2779803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2780fa9c0d79SChris Mason 2781ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2782f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2783416ac51dSArne Jansen 2784fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2785fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2786fa9c0d79SChris Mason 2787e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2788f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2789bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27904854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2791034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 27923768f368SChris Mason 279304216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 279404216820SFilipe Manana 2795da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2796da17066cSJeff Mahoney fs_info->nodesize = 4096; 2797da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2798da17066cSJeff Mahoney fs_info->stripesize = 4096; 2799da17066cSJeff Mahoney 2800eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2801eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2802eede2bf3SOmar Sandoval 280353b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 280453b381b3SDavid Woodhouse if (ret) { 280583c8266aSDavid Sterba err = ret; 280653b381b3SDavid Woodhouse goto fail_alloc; 280753b381b3SDavid Woodhouse } 280853b381b3SDavid Woodhouse 2809da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 28107eccb903SChris Mason 28113c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 28121104a885SDavid Sterba 28131104a885SDavid Sterba /* 28141104a885SDavid Sterba * Read super block and check the signature bytes only 28151104a885SDavid Sterba */ 2816a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 281792fc03fbSAnand Jain if (IS_ERR(bh)) { 281892fc03fbSAnand Jain err = PTR_ERR(bh); 281916cdcec7SMiao Xie goto fail_alloc; 282020b45077SDave Young } 282139279cc3SChris Mason 28221104a885SDavid Sterba /* 28231104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 28241104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 28251104a885SDavid Sterba */ 2826ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 282705135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 28281104a885SDavid Sterba err = -EINVAL; 2829b2acdddfSAnand Jain brelse(bh); 28301104a885SDavid Sterba goto fail_alloc; 28311104a885SDavid Sterba } 28321104a885SDavid Sterba 28331104a885SDavid Sterba /* 28341104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 28351104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 28361104a885SDavid Sterba * the whole block of INFO_SIZE 28371104a885SDavid Sterba */ 28386c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 2839a061fc8dSChris Mason brelse(bh); 28405f39d397SChris Mason 2841fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2842fbc6feaeSNikolay Borisov 2843de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2844de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2845de37aa51SNikolay Borisov 28467239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2847de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2848de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2849de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 28507239ff4bSNikolay Borisov } 28510b86a832SChris Mason 2852fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2853fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2854fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2855fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2856fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2857fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2858fbc6feaeSNikolay Borisov } 2859fbc6feaeSNikolay Borisov 2860fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 2861fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 2862de37aa51SNikolay Borisov 2863069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 28641104a885SDavid Sterba if (ret) { 286505135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 28661104a885SDavid Sterba err = -EINVAL; 28671104a885SDavid Sterba goto fail_alloc; 28681104a885SDavid Sterba } 28691104a885SDavid Sterba 28700f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 287116cdcec7SMiao Xie goto fail_alloc; 28720f7d52f4SChris Mason 2873acce952bSliubo /* check FS state, whether FS is broken. */ 287487533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 287587533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2876acce952bSliubo 287775e7cb7fSLiu Bo /* 2878af31f5e5SChris Mason * run through our array of backup supers and setup 2879af31f5e5SChris Mason * our ring pointer to the oldest one 2880af31f5e5SChris Mason */ 2881af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2882af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2883af31f5e5SChris Mason 2884af31f5e5SChris Mason /* 288575e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 288675e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 288775e7cb7fSLiu Bo */ 288875e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 288975e7cb7fSLiu Bo 28902ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 28912b82032cSYan Zheng if (ret) { 28922b82032cSYan Zheng err = ret; 289316cdcec7SMiao Xie goto fail_alloc; 28942b82032cSYan Zheng } 2895dfe25020SChris Mason 2896f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2897f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2898f2b636e8SJosef Bacik if (features) { 289905135f59SDavid Sterba btrfs_err(fs_info, 290005135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2901c1c9ff7cSGeert Uytterhoeven features); 2902f2b636e8SJosef Bacik err = -EINVAL; 290316cdcec7SMiao Xie goto fail_alloc; 2904f2b636e8SJosef Bacik } 2905f2b636e8SJosef Bacik 29065d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 29075d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 29080b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2909a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 29105c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 29115c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2912727011e0SChris Mason 29133173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 291405135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 29153173a18fSJosef Bacik 2916727011e0SChris Mason /* 2917727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2918727011e0SChris Mason * they are bigger than the page size 2919727011e0SChris Mason */ 292009cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2921727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 292205135f59SDavid Sterba btrfs_info(fs_info, 292305135f59SDavid Sterba "flagging fs with big metadata feature"); 2924727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2925727011e0SChris Mason } 2926727011e0SChris Mason 2927bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2928bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2929b7f67055SChandan Rajendra stripesize = sectorsize; 2930707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2931963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2932bc3f116fSChris Mason 2933da17066cSJeff Mahoney /* Cache block sizes */ 2934da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2935da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2936da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2937da17066cSJeff Mahoney 2938bc3f116fSChris Mason /* 2939bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2940bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2941bc3f116fSChris Mason */ 2942bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2943707e8a07SDavid Sterba (sectorsize != nodesize)) { 294405135f59SDavid Sterba btrfs_err(fs_info, 294505135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 294605135f59SDavid Sterba nodesize, sectorsize); 2947bc3f116fSChris Mason goto fail_alloc; 2948bc3f116fSChris Mason } 2949bc3f116fSChris Mason 2950ceda0864SMiao Xie /* 2951ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2952ceda0864SMiao Xie * update the flag. 2953ceda0864SMiao Xie */ 29545d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 29555d4f98a2SYan Zheng 2956f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2957f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2958bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 295905135f59SDavid Sterba btrfs_err(fs_info, 296005135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2961c1c9ff7cSGeert Uytterhoeven features); 2962f2b636e8SJosef Bacik err = -EINVAL; 296316cdcec7SMiao Xie goto fail_alloc; 2964f2b636e8SJosef Bacik } 296561d92c32SChris Mason 29662a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 29672a458198SEric Sandeen if (ret) { 29682a458198SEric Sandeen err = ret; 29690dc3b84aSJosef Bacik goto fail_sb_buffer; 29700dc3b84aSJosef Bacik } 29714543df7eSChris Mason 29729e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 29739e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 29749e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 2975b5420237SNikolay Borisov sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 29769e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 29779e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 29784575c9ccSChris Mason 2979a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2980a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2981de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 2982db94535dSChris Mason 2983925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 29846bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2985925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 298684eed90fSChris Mason if (ret) { 298705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 29885d4f98a2SYan Zheng goto fail_sb_buffer; 298984eed90fSChris Mason } 29900b86a832SChris Mason 299184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 2992581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 29930b86a832SChris Mason 2994da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 29950b86a832SChris Mason 29962ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 29970b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2998581c1760SQu Wenruo generation, level, NULL); 299964c043deSLiu Bo if (IS_ERR(chunk_root->node) || 300064c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 300105135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3002e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3003e5fffbacSchandan free_extent_buffer(chunk_root->node); 300495ab1f64SZhao Lei chunk_root->node = NULL; 3005af31f5e5SChris Mason goto fail_tree_roots; 300683121942SDavid Woodhouse } 30075d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 30085d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 30090b86a832SChris Mason 3010e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3011b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 3012e17cade2SChris Mason 30135b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 30142b82032cSYan Zheng if (ret) { 301505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3016af31f5e5SChris Mason goto fail_tree_roots; 30172b82032cSYan Zheng } 30180b86a832SChris Mason 30198dabb742SStefan Behrens /* 30209b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 30219b99b115SAnand Jain * device replace procedure 30228dabb742SStefan Behrens */ 30239b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3024dfe25020SChris Mason 3025a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 302605135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3027a6b0d5c8SChris Mason goto fail_tree_roots; 3028a6b0d5c8SChris Mason } 3029a6b0d5c8SChris Mason 3030af31f5e5SChris Mason retry_root_backup: 303184234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 3032581c1760SQu Wenruo level = btrfs_super_root_level(disk_super); 30330b86a832SChris Mason 30342ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 3035db94535dSChris Mason btrfs_super_root(disk_super), 3036581c1760SQu Wenruo generation, level, NULL); 303764c043deSLiu Bo if (IS_ERR(tree_root->node) || 303864c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 303905135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 3040e5fffbacSchandan if (!IS_ERR(tree_root->node)) 3041e5fffbacSchandan free_extent_buffer(tree_root->node); 304295ab1f64SZhao Lei tree_root->node = NULL; 3043af31f5e5SChris Mason goto recovery_tree_root; 304483121942SDavid Woodhouse } 3045af31f5e5SChris Mason 30465d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 30475d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 304869e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 3049db94535dSChris Mason 3050f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 3051f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 3052f32e48e9SChandan Rajendra &tree_root->highest_objectid); 3053f32e48e9SChandan Rajendra if (ret) { 3054f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 3055f32e48e9SChandan Rajendra goto recovery_tree_root; 3056f32e48e9SChandan Rajendra } 3057f32e48e9SChandan Rajendra 3058f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 3059f32e48e9SChandan Rajendra 3060f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 3061f32e48e9SChandan Rajendra 30626bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 30634bbcaa64SEric Sandeen if (ret) 3064af31f5e5SChris Mason goto recovery_tree_root; 3065f7a81ea4SStefan Behrens 30668929ecfaSYan, Zheng fs_info->generation = generation; 30678929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 30688929ecfaSYan, Zheng 3069cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3070cf90d884SQu Wenruo if (ret) { 3071cf90d884SQu Wenruo btrfs_err(fs_info, 3072cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3073cf90d884SQu Wenruo ret); 3074cf90d884SQu Wenruo goto fail_block_groups; 3075cf90d884SQu Wenruo } 307668310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 307768310a5eSIlya Dryomov if (ret) { 307805135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 307968310a5eSIlya Dryomov goto fail_block_groups; 308068310a5eSIlya Dryomov } 308168310a5eSIlya Dryomov 3082733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3083733f4fbbSStefan Behrens if (ret) { 308405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3085733f4fbbSStefan Behrens goto fail_block_groups; 3086733f4fbbSStefan Behrens } 3087733f4fbbSStefan Behrens 30888dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 30898dabb742SStefan Behrens if (ret) { 309005135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 30918dabb742SStefan Behrens goto fail_block_groups; 30928dabb742SStefan Behrens } 30938dabb742SStefan Behrens 30949b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 30958dabb742SStefan Behrens 3096b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 3097b7c35e81SAnand Jain if (ret) { 309805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 309905135f59SDavid Sterba ret); 3100b7c35e81SAnand Jain goto fail_block_groups; 3101b7c35e81SAnand Jain } 3102b7c35e81SAnand Jain 3103b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 3104b7c35e81SAnand Jain if (ret) { 310505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 310605135f59SDavid Sterba ret); 3107b7c35e81SAnand Jain goto fail_fsdev_sysfs; 3108b7c35e81SAnand Jain } 3109b7c35e81SAnand Jain 311096f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 31115ac1d209SJeff Mahoney if (ret) { 311205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3113b7c35e81SAnand Jain goto fail_fsdev_sysfs; 31145ac1d209SJeff Mahoney } 31155ac1d209SJeff Mahoney 3116c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3117c59021f8Sliubo if (ret) { 311805135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 31192365dd3cSAnand Jain goto fail_sysfs; 3120c59021f8Sliubo } 3121c59021f8Sliubo 31225b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 31231b1d1f66SJosef Bacik if (ret) { 312405135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 31252365dd3cSAnand Jain goto fail_sysfs; 31261b1d1f66SJosef Bacik } 31274330e183SQu Wenruo 31286528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 312905135f59SDavid Sterba btrfs_warn(fs_info, 313052042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 31312365dd3cSAnand Jain goto fail_sysfs; 3132292fd7fcSStefan Behrens } 31339078a3e1SChris Mason 3134a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3135a74a4b97SChris Mason "btrfs-cleaner"); 313657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 31372365dd3cSAnand Jain goto fail_sysfs; 3138a74a4b97SChris Mason 3139a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3140a74a4b97SChris Mason tree_root, 3141a74a4b97SChris Mason "btrfs-transaction"); 314257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 31433f157a2fSChris Mason goto fail_cleaner; 3144a74a4b97SChris Mason 3145583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3146c289811cSChris Mason !fs_info->fs_devices->rotating) { 3147583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3148c289811cSChris Mason } 3149c289811cSChris Mason 3150572d9ab7SDavid Sterba /* 315101327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3152572d9ab7SDavid Sterba * not have to wait for transaction commit 3153572d9ab7SDavid Sterba */ 3154572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 31553818aea2SQu Wenruo 315621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 31570b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 31582ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 31590b246afaSJeff Mahoney btrfs_test_opt(fs_info, 316021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 316121adbd5cSStefan Behrens 1 : 0, 316221adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 316321adbd5cSStefan Behrens if (ret) 316405135f59SDavid Sterba btrfs_warn(fs_info, 316505135f59SDavid Sterba "failed to initialize integrity check module: %d", 316605135f59SDavid Sterba ret); 316721adbd5cSStefan Behrens } 316821adbd5cSStefan Behrens #endif 3169bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3170bcef60f2SArne Jansen if (ret) 3171bcef60f2SArne Jansen goto fail_trans_kthread; 317221adbd5cSStefan Behrens 3173fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3174fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3175fd708b81SJosef Bacik 317696da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 317796da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 31780b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 317963443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 318079787eaaSJeff Mahoney if (ret) { 318163443bf5SEric Sandeen err = ret; 318228c16cbbSWang Shilong goto fail_qgroup; 3183e556ce2cSYan Zheng } 3184e02119d5SChris Mason } 31851a40e23bSZheng Yan 31866bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 318779787eaaSJeff Mahoney if (ret) 318828c16cbbSWang Shilong goto fail_qgroup; 318976dda93cSYan, Zheng 3190bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3191d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 319244c44af2SIlya Dryomov if (ret) 319328c16cbbSWang Shilong goto fail_qgroup; 319490c711abSZygo Blaxell 319590c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 31965d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 319790c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3198d7ce5843SMiao Xie if (ret < 0) { 319905135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 320005135f59SDavid Sterba ret); 3201d7ce5843SMiao Xie err = -EINVAL; 3202bcef60f2SArne Jansen goto fail_qgroup; 3203d7ce5843SMiao Xie } 32047c2ca468SChris Mason } 32051a40e23bSZheng Yan 32063de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 32073de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3208cb517eabSMiao Xie location.offset = 0; 32093de4586cSChris Mason 32103de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 32113140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 32123140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3213f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3214bcef60f2SArne Jansen goto fail_qgroup; 32153140c9a3SDan Carpenter } 3216c289811cSChris Mason 3217bc98a42cSDavid Howells if (sb_rdonly(sb)) 32182b6ba629SIlya Dryomov return 0; 32192b6ba629SIlya Dryomov 3220f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3221f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32226675df31SOmar Sandoval clear_free_space_tree = 1; 32236675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32246675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32256675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 32266675df31SOmar Sandoval clear_free_space_tree = 1; 32276675df31SOmar Sandoval } 32286675df31SOmar Sandoval 32296675df31SOmar Sandoval if (clear_free_space_tree) { 3230f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3231f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3232f8d468a1SOmar Sandoval if (ret) { 3233f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3234f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 32356bccf3abSJeff Mahoney close_ctree(fs_info); 3236f8d468a1SOmar Sandoval return ret; 3237f8d468a1SOmar Sandoval } 3238f8d468a1SOmar Sandoval } 3239f8d468a1SOmar Sandoval 32400b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3241511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 324205135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3243511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3244511711afSChris Mason if (ret) { 324505135f59SDavid Sterba btrfs_warn(fs_info, 324605135f59SDavid Sterba "failed to create free space tree: %d", ret); 32476bccf3abSJeff Mahoney close_ctree(fs_info); 3248511711afSChris Mason return ret; 3249511711afSChris Mason } 3250511711afSChris Mason } 3251511711afSChris Mason 3252e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 32532b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 32542b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 32552b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 32566bccf3abSJeff Mahoney close_ctree(fs_info); 32572b6ba629SIlya Dryomov return ret; 32582b6ba629SIlya Dryomov } 3259e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 326059641015SIlya Dryomov 32612b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 32622b6ba629SIlya Dryomov if (ret) { 326305135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 32646bccf3abSJeff Mahoney close_ctree(fs_info); 32652b6ba629SIlya Dryomov return ret; 3266e3acc2a6SJosef Bacik } 3267e3acc2a6SJosef Bacik 32688dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 32698dabb742SStefan Behrens if (ret) { 327005135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 32716bccf3abSJeff Mahoney close_ctree(fs_info); 32728dabb742SStefan Behrens return ret; 32738dabb742SStefan Behrens } 32748dabb742SStefan Behrens 3275b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3276b382a324SJan Schmidt 32774bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 327805135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3279f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3280f7a81ea4SStefan Behrens if (ret) { 328105135f59SDavid Sterba btrfs_warn(fs_info, 328205135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 32836bccf3abSJeff Mahoney close_ctree(fs_info); 3284f7a81ea4SStefan Behrens return ret; 3285f7a81ea4SStefan Behrens } 32860b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 32874bbcaa64SEric Sandeen fs_info->generation != 32884bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 328905135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 329070f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 329170f80175SStefan Behrens if (ret) { 329205135f59SDavid Sterba btrfs_warn(fs_info, 329305135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 32946bccf3abSJeff Mahoney close_ctree(fs_info); 329570f80175SStefan Behrens return ret; 329670f80175SStefan Behrens } 329770f80175SStefan Behrens } else { 3298afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3299f7a81ea4SStefan Behrens } 3300afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 330147ab2a6cSJosef Bacik 33028dcddfa0SQu Wenruo /* 33038dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 33048dcddfa0SQu Wenruo * no need to keep the flag 33058dcddfa0SQu Wenruo */ 33068dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33078dcddfa0SQu Wenruo 3308ad2b2c80SAl Viro return 0; 330939279cc3SChris Mason 3310bcef60f2SArne Jansen fail_qgroup: 3311bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 33127c2ca468SChris Mason fail_trans_kthread: 33137c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 33142ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3315faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 33163f157a2fSChris Mason fail_cleaner: 3317a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 33187c2ca468SChris Mason 33197c2ca468SChris Mason /* 33207c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 33217c2ca468SChris Mason * kthreads 33227c2ca468SChris Mason */ 33237c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 33247c2ca468SChris Mason 33252365dd3cSAnand Jain fail_sysfs: 33266618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 33272365dd3cSAnand Jain 3328b7c35e81SAnand Jain fail_fsdev_sysfs: 3329b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3330b7c35e81SAnand Jain 33311b1d1f66SJosef Bacik fail_block_groups: 333254067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3333af31f5e5SChris Mason 3334af31f5e5SChris Mason fail_tree_roots: 3335af31f5e5SChris Mason free_root_pointers(fs_info, 1); 33362b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3337af31f5e5SChris Mason 333839279cc3SChris Mason fail_sb_buffer: 33397abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 33405cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 334116cdcec7SMiao Xie fail_alloc: 33424543df7eSChris Mason fail_iput: 3343586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3344586e46e2SIlya Dryomov 33454543df7eSChris Mason iput(fs_info->btree_inode); 3346c404e0dcSMiao Xie fail_bio_counter: 33477f8d236aSDavid Sterba percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 3348963d678bSMiao Xie fail_delalloc_bytes: 3349963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3350e2d84521SMiao Xie fail_dirty_metadata_bytes: 3351e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 335276dda93cSYan, Zheng fail_srcu: 335376dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 33547e662854SQinghuang Feng fail: 335553b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3356586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3357ad2b2c80SAl Viro return err; 3358af31f5e5SChris Mason 3359af31f5e5SChris Mason recovery_tree_root: 33600b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3361af31f5e5SChris Mason goto fail_tree_roots; 3362af31f5e5SChris Mason 3363af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3364af31f5e5SChris Mason 3365af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3366af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3367af31f5e5SChris Mason 3368af31f5e5SChris Mason /* we can't trust the free space cache either */ 3369af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3370af31f5e5SChris Mason 3371af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3372af31f5e5SChris Mason &num_backups_tried, &backup_index); 3373af31f5e5SChris Mason if (ret == -1) 3374af31f5e5SChris Mason goto fail_block_groups; 3375af31f5e5SChris Mason goto retry_root_backup; 3376eb60ceacSChris Mason } 3377663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3378eb60ceacSChris Mason 3379f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3380f2984462SChris Mason { 3381f2984462SChris Mason if (uptodate) { 3382f2984462SChris Mason set_buffer_uptodate(bh); 3383f2984462SChris Mason } else { 3384442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3385442a4f63SStefan Behrens bh->b_private; 3386442a4f63SStefan Behrens 3387fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3388b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3389606686eeSJosef Bacik rcu_str_deref(device->name)); 339001327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 33911259ab75SChris Mason * our own ways of dealing with the IO errors 33921259ab75SChris Mason */ 3393f2984462SChris Mason clear_buffer_uptodate(bh); 3394442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3395f2984462SChris Mason } 3396f2984462SChris Mason unlock_buffer(bh); 3397f2984462SChris Mason put_bh(bh); 3398f2984462SChris Mason } 3399f2984462SChris Mason 340029c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 340129c36d72SAnand Jain struct buffer_head **bh_ret) 340229c36d72SAnand Jain { 340329c36d72SAnand Jain struct buffer_head *bh; 340429c36d72SAnand Jain struct btrfs_super_block *super; 340529c36d72SAnand Jain u64 bytenr; 340629c36d72SAnand Jain 340729c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 340829c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 340929c36d72SAnand Jain return -EINVAL; 341029c36d72SAnand Jain 34119f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 341229c36d72SAnand Jain /* 341329c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 341429c36d72SAnand Jain * the best option we have is to mark it EIO. 341529c36d72SAnand Jain */ 341629c36d72SAnand Jain if (!bh) 341729c36d72SAnand Jain return -EIO; 341829c36d72SAnand Jain 341929c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 342029c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 342129c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 342229c36d72SAnand Jain brelse(bh); 342329c36d72SAnand Jain return -EINVAL; 342429c36d72SAnand Jain } 342529c36d72SAnand Jain 342629c36d72SAnand Jain *bh_ret = bh; 342729c36d72SAnand Jain return 0; 342829c36d72SAnand Jain } 342929c36d72SAnand Jain 343029c36d72SAnand Jain 3431a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3432a512bbf8SYan Zheng { 3433a512bbf8SYan Zheng struct buffer_head *bh; 3434a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3435a512bbf8SYan Zheng struct btrfs_super_block *super; 3436a512bbf8SYan Zheng int i; 3437a512bbf8SYan Zheng u64 transid = 0; 343892fc03fbSAnand Jain int ret = -EINVAL; 3439a512bbf8SYan Zheng 3440a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3441a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3442a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3443a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3444a512bbf8SYan Zheng */ 3445a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 344629c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 344729c36d72SAnand Jain if (ret) 3448a512bbf8SYan Zheng continue; 3449a512bbf8SYan Zheng 3450a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3451a512bbf8SYan Zheng 3452a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3453a512bbf8SYan Zheng brelse(latest); 3454a512bbf8SYan Zheng latest = bh; 3455a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3456a512bbf8SYan Zheng } else { 3457a512bbf8SYan Zheng brelse(bh); 3458a512bbf8SYan Zheng } 3459a512bbf8SYan Zheng } 346092fc03fbSAnand Jain 346192fc03fbSAnand Jain if (!latest) 346292fc03fbSAnand Jain return ERR_PTR(ret); 346392fc03fbSAnand Jain 3464a512bbf8SYan Zheng return latest; 3465a512bbf8SYan Zheng } 3466a512bbf8SYan Zheng 34674eedeb75SHisashi Hifumi /* 3468abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3469abbb3b8eSDavid Sterba * buffer heads we write are pinned. 34704eedeb75SHisashi Hifumi * 3471abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3472abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3473abbb3b8eSDavid Sterba * same for write and wait phases. 34744eedeb75SHisashi Hifumi * 3475abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 34764eedeb75SHisashi Hifumi */ 3477a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3478abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3479a512bbf8SYan Zheng { 3480a512bbf8SYan Zheng struct buffer_head *bh; 3481a512bbf8SYan Zheng int i; 3482a512bbf8SYan Zheng int ret; 3483a512bbf8SYan Zheng int errors = 0; 3484a512bbf8SYan Zheng u32 crc; 3485a512bbf8SYan Zheng u64 bytenr; 34861b9e619cSOmar Sandoval int op_flags; 3487a512bbf8SYan Zheng 3488a512bbf8SYan Zheng if (max_mirrors == 0) 3489a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3490a512bbf8SYan Zheng 3491a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3492a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3493935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3494935e5cc9SMiao Xie device->commit_total_bytes) 3495a512bbf8SYan Zheng break; 3496a512bbf8SYan Zheng 3497a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3498a512bbf8SYan Zheng 3499a512bbf8SYan Zheng crc = ~(u32)0; 3500abbb3b8eSDavid Sterba crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc, 3501abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3502a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3503a512bbf8SYan Zheng 3504abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 35059f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3506a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3507634554dcSJosef Bacik if (!bh) { 3508fb456252SJeff Mahoney btrfs_err(device->fs_info, 3509f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3510f14d104dSDavid Sterba bytenr); 3511634554dcSJosef Bacik errors++; 3512634554dcSJosef Bacik continue; 3513634554dcSJosef Bacik } 3514634554dcSJosef Bacik 3515a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3516a512bbf8SYan Zheng 35174eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3518a512bbf8SYan Zheng get_bh(bh); 35194eedeb75SHisashi Hifumi 35204eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3521a512bbf8SYan Zheng lock_buffer(bh); 3522a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3523442a4f63SStefan Behrens bh->b_private = device; 3524a512bbf8SYan Zheng 3525387125fcSChris Mason /* 3526387125fcSChris Mason * we fua the first super. The others we allow 3527387125fcSChris Mason * to go down lazy. 3528387125fcSChris Mason */ 35291b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 35301b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 35311b9e619cSOmar Sandoval op_flags |= REQ_FUA; 35321b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 35334eedeb75SHisashi Hifumi if (ret) 3534a512bbf8SYan Zheng errors++; 3535a512bbf8SYan Zheng } 3536a512bbf8SYan Zheng return errors < i ? 0 : -1; 3537a512bbf8SYan Zheng } 3538a512bbf8SYan Zheng 3539387125fcSChris Mason /* 3540abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3541abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3542abbb3b8eSDavid Sterba * 3543abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3544abbb3b8eSDavid Sterba * date. 3545abbb3b8eSDavid Sterba */ 3546abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3547abbb3b8eSDavid Sterba { 3548abbb3b8eSDavid Sterba struct buffer_head *bh; 3549abbb3b8eSDavid Sterba int i; 3550abbb3b8eSDavid Sterba int errors = 0; 3551b6a535faSHoward McLauchlan bool primary_failed = false; 3552abbb3b8eSDavid Sterba u64 bytenr; 3553abbb3b8eSDavid Sterba 3554abbb3b8eSDavid Sterba if (max_mirrors == 0) 3555abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3556abbb3b8eSDavid Sterba 3557abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3558abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3559abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3560abbb3b8eSDavid Sterba device->commit_total_bytes) 3561abbb3b8eSDavid Sterba break; 3562abbb3b8eSDavid Sterba 35639f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 35649f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3565abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3566abbb3b8eSDavid Sterba if (!bh) { 3567abbb3b8eSDavid Sterba errors++; 3568b6a535faSHoward McLauchlan if (i == 0) 3569b6a535faSHoward McLauchlan primary_failed = true; 3570abbb3b8eSDavid Sterba continue; 3571abbb3b8eSDavid Sterba } 3572abbb3b8eSDavid Sterba wait_on_buffer(bh); 3573b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3574abbb3b8eSDavid Sterba errors++; 3575b6a535faSHoward McLauchlan if (i == 0) 3576b6a535faSHoward McLauchlan primary_failed = true; 3577b6a535faSHoward McLauchlan } 3578abbb3b8eSDavid Sterba 3579abbb3b8eSDavid Sterba /* drop our reference */ 3580abbb3b8eSDavid Sterba brelse(bh); 3581abbb3b8eSDavid Sterba 3582abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3583abbb3b8eSDavid Sterba brelse(bh); 3584abbb3b8eSDavid Sterba } 3585abbb3b8eSDavid Sterba 3586b6a535faSHoward McLauchlan /* log error, force error return */ 3587b6a535faSHoward McLauchlan if (primary_failed) { 3588b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3589b6a535faSHoward McLauchlan device->devid); 3590b6a535faSHoward McLauchlan return -1; 3591b6a535faSHoward McLauchlan } 3592b6a535faSHoward McLauchlan 3593abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3594abbb3b8eSDavid Sterba } 3595abbb3b8eSDavid Sterba 3596abbb3b8eSDavid Sterba /* 3597387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3598387125fcSChris Mason * for the barrier when it is done 3599387125fcSChris Mason */ 36004246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3601387125fcSChris Mason { 3602387125fcSChris Mason complete(bio->bi_private); 3603387125fcSChris Mason } 3604387125fcSChris Mason 3605387125fcSChris Mason /* 36064fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36074fc6441aSAnand Jain * done in the waiting counterpart. 3608387125fcSChris Mason */ 36094fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3610387125fcSChris Mason { 3611c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3612e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3613387125fcSChris Mason 3614c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 36154fc6441aSAnand Jain return; 3616387125fcSChris Mason 3617e0ae9994SDavid Sterba bio_reset(bio); 3618387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 361974d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 36208d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3621387125fcSChris Mason init_completion(&device->flush_wait); 3622387125fcSChris Mason bio->bi_private = &device->flush_wait; 3623387125fcSChris Mason 362443a01111SLu Fengqi btrfsic_submit_bio(bio); 36251c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36264fc6441aSAnand Jain } 3627387125fcSChris Mason 36284fc6441aSAnand Jain /* 36294fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 36304fc6441aSAnand Jain */ 36318c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 36324fc6441aSAnand Jain { 36334fc6441aSAnand Jain struct bio *bio = device->flush_bio; 36344fc6441aSAnand Jain 36351c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 363658efbc9fSOmar Sandoval return BLK_STS_OK; 36374fc6441aSAnand Jain 36381c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 36392980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 36404fc6441aSAnand Jain 36418c27cb35SLinus Torvalds return bio->bi_status; 3642387125fcSChris Mason } 3643387125fcSChris Mason 3644d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3645401b41e5SAnand Jain { 36466528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3647401b41e5SAnand Jain return -EIO; 3648387125fcSChris Mason return 0; 3649387125fcSChris Mason } 3650387125fcSChris Mason 3651387125fcSChris Mason /* 3652387125fcSChris Mason * send an empty flush down to each device in parallel, 3653387125fcSChris Mason * then wait for them 3654387125fcSChris Mason */ 3655387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3656387125fcSChris Mason { 3657387125fcSChris Mason struct list_head *head; 3658387125fcSChris Mason struct btrfs_device *dev; 36595af3e8ccSStefan Behrens int errors_wait = 0; 36604e4cbee9SChristoph Hellwig blk_status_t ret; 3661387125fcSChris Mason 36621538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3663387125fcSChris Mason /* send down all the barriers */ 3664387125fcSChris Mason head = &info->fs_devices->devices; 36651538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3666e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3667f88ba6a2SHidetoshi Seto continue; 3668cea7c8bfSAnand Jain if (!dev->bdev) 3669387125fcSChris Mason continue; 3670e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3671ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3672387125fcSChris Mason continue; 3673387125fcSChris Mason 36744fc6441aSAnand Jain write_dev_flush(dev); 367558efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3676387125fcSChris Mason } 3677387125fcSChris Mason 3678387125fcSChris Mason /* wait for all the barriers */ 36791538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3680e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3681f88ba6a2SHidetoshi Seto continue; 3682387125fcSChris Mason if (!dev->bdev) { 36835af3e8ccSStefan Behrens errors_wait++; 3684387125fcSChris Mason continue; 3685387125fcSChris Mason } 3686e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3687ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3688387125fcSChris Mason continue; 3689387125fcSChris Mason 36904fc6441aSAnand Jain ret = wait_dev_flush(dev); 3691401b41e5SAnand Jain if (ret) { 3692401b41e5SAnand Jain dev->last_flush_error = ret; 369366b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 369466b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 36955af3e8ccSStefan Behrens errors_wait++; 3696387125fcSChris Mason } 3697401b41e5SAnand Jain } 3698401b41e5SAnand Jain 3699cea7c8bfSAnand Jain if (errors_wait) { 3700401b41e5SAnand Jain /* 3701401b41e5SAnand Jain * At some point we need the status of all disks 3702401b41e5SAnand Jain * to arrive at the volume status. So error checking 3703401b41e5SAnand Jain * is being pushed to a separate loop. 3704401b41e5SAnand Jain */ 3705d10b82feSQu Wenruo return check_barrier_error(info); 3706401b41e5SAnand Jain } 3707387125fcSChris Mason return 0; 3708387125fcSChris Mason } 3709387125fcSChris Mason 3710943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3711943c6e99SZhao Lei { 37128789f4feSZhao Lei int raid_type; 37138789f4feSZhao Lei int min_tolerated = INT_MAX; 3714943c6e99SZhao Lei 37158789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 37168789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 37178789f4feSZhao Lei min_tolerated = min(min_tolerated, 37188789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 37198789f4feSZhao Lei tolerated_failures); 3720943c6e99SZhao Lei 37218789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 37228789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 37238789f4feSZhao Lei continue; 372441a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 37258789f4feSZhao Lei continue; 37268789f4feSZhao Lei min_tolerated = min(min_tolerated, 37278789f4feSZhao Lei btrfs_raid_array[raid_type]. 37288789f4feSZhao Lei tolerated_failures); 37298789f4feSZhao Lei } 3730943c6e99SZhao Lei 37318789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3732ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 37338789f4feSZhao Lei min_tolerated = 0; 37348789f4feSZhao Lei } 37358789f4feSZhao Lei 37368789f4feSZhao Lei return min_tolerated; 3737943c6e99SZhao Lei } 3738943c6e99SZhao Lei 3739eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3740f2984462SChris Mason { 3741e5e9a520SChris Mason struct list_head *head; 3742f2984462SChris Mason struct btrfs_device *dev; 3743a061fc8dSChris Mason struct btrfs_super_block *sb; 3744f2984462SChris Mason struct btrfs_dev_item *dev_item; 3745f2984462SChris Mason int ret; 3746f2984462SChris Mason int do_barriers; 3747a236aed1SChris Mason int max_errors; 3748a236aed1SChris Mason int total_errors = 0; 3749a061fc8dSChris Mason u64 flags; 3750f2984462SChris Mason 37510b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3752fed3b381SLiu Bo 3753fed3b381SLiu Bo /* 3754fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3755fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3756fed3b381SLiu Bo * been consistent on disk. 3757fed3b381SLiu Bo */ 3758fed3b381SLiu Bo if (max_mirrors == 0) 37590b246afaSJeff Mahoney backup_super_roots(fs_info); 3760f2984462SChris Mason 37610b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3762a061fc8dSChris Mason dev_item = &sb->dev_item; 3763e5e9a520SChris Mason 37640b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37650b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37660b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3767387125fcSChris Mason 37685af3e8ccSStefan Behrens if (do_barriers) { 37690b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37705af3e8ccSStefan Behrens if (ret) { 37715af3e8ccSStefan Behrens mutex_unlock( 37720b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 37730b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 37745af3e8ccSStefan Behrens "errors while submitting device barriers."); 37755af3e8ccSStefan Behrens return ret; 37765af3e8ccSStefan Behrens } 37775af3e8ccSStefan Behrens } 3778387125fcSChris Mason 37791538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3780dfe25020SChris Mason if (!dev->bdev) { 3781dfe25020SChris Mason total_errors++; 3782dfe25020SChris Mason continue; 3783dfe25020SChris Mason } 3784e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3785ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3786dfe25020SChris Mason continue; 3787dfe25020SChris Mason 37882b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3789a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3790a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 37917df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3792935e5cc9SMiao Xie dev->commit_total_bytes); 3793ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3794ce7213c7SMiao Xie dev->commit_bytes_used); 3795a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3796a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3797a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3798a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 37997239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38007239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3801a512bbf8SYan Zheng 3802a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3803a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3804f2984462SChris Mason 380575cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 380675cb857dSQu Wenruo if (ret < 0) { 380775cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 380875cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 380975cb857dSQu Wenruo "unexpected superblock corruption detected"); 381075cb857dSQu Wenruo return -EUCLEAN; 381175cb857dSQu Wenruo } 381275cb857dSQu Wenruo 3813abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3814a236aed1SChris Mason if (ret) 3815a236aed1SChris Mason total_errors++; 3816f2984462SChris Mason } 3817a236aed1SChris Mason if (total_errors > max_errors) { 38180b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3819d397712bSChris Mason total_errors); 38200b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 382179787eaaSJeff Mahoney 38229d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 38230b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38240b246afaSJeff Mahoney "%d errors while writing supers", 38250b246afaSJeff Mahoney total_errors); 38269d565ba4SStefan Behrens return -EIO; 3827a236aed1SChris Mason } 3828f2984462SChris Mason 3829a512bbf8SYan Zheng total_errors = 0; 38301538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3831dfe25020SChris Mason if (!dev->bdev) 3832dfe25020SChris Mason continue; 3833e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3834ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3835dfe25020SChris Mason continue; 3836dfe25020SChris Mason 3837abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3838a512bbf8SYan Zheng if (ret) 38391259ab75SChris Mason total_errors++; 3840f2984462SChris Mason } 38410b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3842a236aed1SChris Mason if (total_errors > max_errors) { 38430b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 38440b246afaSJeff Mahoney "%d errors while writing supers", 38450b246afaSJeff Mahoney total_errors); 384679787eaaSJeff Mahoney return -EIO; 3847a236aed1SChris Mason } 3848f2984462SChris Mason return 0; 3849f2984462SChris Mason } 3850f2984462SChris Mason 3851cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3852cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3853cb517eabSMiao Xie struct btrfs_root *root) 38542619ba1fSChris Mason { 38554df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 38562619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 38572619ba1fSChris Mason (unsigned long)root->root_key.objectid); 38584df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 385976dda93cSYan, Zheng 386076dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 386176dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 386276dda93cSYan, Zheng 38631c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 38643321719eSLiu Bo btrfs_free_log(NULL, root); 38651c1ea4f7SLiu Bo if (root->reloc_root) { 38661c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38671c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 38681c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 38691c1ea4f7SLiu Bo root->reloc_root = NULL; 38701c1ea4f7SLiu Bo } 38711c1ea4f7SLiu Bo } 38723321719eSLiu Bo 3873faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3874581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3875faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3876581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 387784db5ccfSDavid Sterba btrfs_free_fs_root(root); 38784df27c4dSYan, Zheng } 38794df27c4dSYan, Zheng 388084db5ccfSDavid Sterba void btrfs_free_fs_root(struct btrfs_root *root) 38814df27c4dSYan, Zheng { 388257cdc8dbSDavid Sterba iput(root->ino_cache_inode); 38834df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 38840ee5dc67SAl Viro if (root->anon_dev) 38850ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 38868257b2dcSMiao Xie if (root->subv_writers) 38878257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 38885f39d397SChris Mason free_extent_buffer(root->node); 38895f39d397SChris Mason free_extent_buffer(root->commit_root); 3890581bb050SLi Zefan kfree(root->free_ino_ctl); 3891581bb050SLi Zefan kfree(root->free_ino_pinned); 3892b0feb9d9SMiao Xie btrfs_put_fs_root(root); 38932619ba1fSChris Mason } 38942619ba1fSChris Mason 3895c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3896c146afadSYan Zheng { 3897c146afadSYan Zheng u64 root_objectid = 0; 3898c146afadSYan Zheng struct btrfs_root *gang[8]; 389965d33fd7SQu Wenruo int i = 0; 390065d33fd7SQu Wenruo int err = 0; 390165d33fd7SQu Wenruo unsigned int ret = 0; 390265d33fd7SQu Wenruo int index; 3903c146afadSYan Zheng 3904c146afadSYan Zheng while (1) { 390565d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3906c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3907c146afadSYan Zheng (void **)gang, root_objectid, 3908c146afadSYan Zheng ARRAY_SIZE(gang)); 390965d33fd7SQu Wenruo if (!ret) { 391065d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3911c146afadSYan Zheng break; 391265d33fd7SQu Wenruo } 39135d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 391466b4ffd1SJosef Bacik 391565d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 391665d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 391765d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 391865d33fd7SQu Wenruo gang[i] = NULL; 391965d33fd7SQu Wenruo continue; 392065d33fd7SQu Wenruo } 392165d33fd7SQu Wenruo /* grab all the search result for later use */ 392265d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 392365d33fd7SQu Wenruo } 392465d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 392565d33fd7SQu Wenruo 392665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 392765d33fd7SQu Wenruo if (!gang[i]) 392865d33fd7SQu Wenruo continue; 3929c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 393066b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 393166b4ffd1SJosef Bacik if (err) 393265d33fd7SQu Wenruo break; 393365d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3934c146afadSYan Zheng } 3935c146afadSYan Zheng root_objectid++; 3936c146afadSYan Zheng } 393765d33fd7SQu Wenruo 393865d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 393965d33fd7SQu Wenruo for (; i < ret; i++) { 394065d33fd7SQu Wenruo if (gang[i]) 394165d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 394265d33fd7SQu Wenruo } 394365d33fd7SQu Wenruo return err; 3944c146afadSYan Zheng } 3945c146afadSYan Zheng 39466bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3947c146afadSYan Zheng { 39486bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3949c146afadSYan Zheng struct btrfs_trans_handle *trans; 3950c146afadSYan Zheng 39510b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39522ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39530b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39540b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3955c71bf099SYan, Zheng 3956c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39570b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39580b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3959c71bf099SYan, Zheng 39607a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39613612b495STsutomu Itoh if (IS_ERR(trans)) 39623612b495STsutomu Itoh return PTR_ERR(trans); 39633a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3964c146afadSYan Zheng } 3965c146afadSYan Zheng 39666bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3967eb60ceacSChris Mason { 3968c146afadSYan Zheng int ret; 3969e089f05cSChris Mason 3970afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3971d6fd0ae2SOmar Sandoval /* 3972d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 3973d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 3974d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 3975d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 3976d6fd0ae2SOmar Sandoval */ 3977d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 3978a2135011SChris Mason 39797343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3980d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 39817343dd61SJustin Maggard 3982803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3983803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3984803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3985803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3986803b2f54SStefan Behrens 3987837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3988aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3989837d5b6eSIlya Dryomov 39908dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 39918dabb742SStefan Behrens 3992aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 39934cb5300bSChris Mason 39944cb5300bSChris Mason /* wait for any defraggers to finish */ 39954cb5300bSChris Mason wait_event(fs_info->transaction_wait, 39964cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 39974cb5300bSChris Mason 39984cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 399926176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40004cb5300bSChris Mason 400121c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 400221c7e756SMiao Xie 4003bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4004e44163e1SJeff Mahoney /* 4005d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4006d6fd0ae2SOmar Sandoval * unused block groups. 4007e44163e1SJeff Mahoney */ 40080b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4009e44163e1SJeff Mahoney 40106bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4011d397712bSChris Mason if (ret) 401204892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4013c146afadSYan Zheng } 4014ed2ff2cbSChris Mason 4015af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4016af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 40172ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4018acce952bSliubo 4019e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4020e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 40218929ecfaSYan, Zheng 4022e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4023afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4024f25784b3SYan Zheng 402504892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4026fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4027bcef60f2SArne Jansen 4028963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 402904892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4030963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4031b0c68f8bSChris Mason } 403231153d81SYan Zheng 40336618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4034b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 40355ac1d209SJeff Mahoney 4036faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 4037d10c5f31SChris Mason 40381a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 40391a4319ccSLiu Bo 4040de348ee0SWang Shilong /* 4041de348ee0SWang Shilong * we must make sure there is not any read request to 4042de348ee0SWang Shilong * submit after we stopping all workers. 4043de348ee0SWang Shilong */ 4044de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 404596192499SJosef Bacik btrfs_stop_all_workers(fs_info); 404696192499SJosef Bacik 40475cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 40485cdd7db6SFilipe Manana 4049afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 405013e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 40519ad6b7bcSChris Mason 405213e6c37bSJosef Bacik iput(fs_info->btree_inode); 4053d6bfde87SChris Mason 405421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40550b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 40562ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 405721adbd5cSStefan Behrens #endif 405821adbd5cSStefan Behrens 4059dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 40600b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 4061b248a415SChris Mason 4062e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 4063963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 40647f8d236aSDavid Sterba percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 406576dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 40660b86a832SChris Mason 406753b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 4068fd708b81SJosef Bacik btrfs_free_ref_cache(fs_info); 406953b381b3SDavid Woodhouse 407004216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 407104216820SFilipe Manana struct extent_map *em; 407204216820SFilipe Manana 407304216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 407404216820SFilipe Manana struct extent_map, list); 407504216820SFilipe Manana list_del_init(&em->list); 407604216820SFilipe Manana free_extent_map(em); 407704216820SFilipe Manana } 4078eb60ceacSChris Mason } 4079eb60ceacSChris Mason 4080b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4081b9fab919SChris Mason int atomic) 4082ccd467d6SChris Mason { 40831259ab75SChris Mason int ret; 4084727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 40851259ab75SChris Mason 40860b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 40871259ab75SChris Mason if (!ret) 40881259ab75SChris Mason return ret; 40891259ab75SChris Mason 40901259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4091b9fab919SChris Mason parent_transid, atomic); 4092b9fab919SChris Mason if (ret == -EAGAIN) 4093b9fab919SChris Mason return ret; 40941259ab75SChris Mason return !ret; 40955f39d397SChris Mason } 40966702ed49SChris Mason 40975f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 40985f39d397SChris Mason { 40990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 410006ea65a3SJosef Bacik struct btrfs_root *root; 41015f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4102b9473439SChris Mason int was_dirty; 4103b4ce94deSChris Mason 410406ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 410506ea65a3SJosef Bacik /* 410606ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 410752042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 410806ea65a3SJosef Bacik * outside of the sanity tests. 410906ea65a3SJosef Bacik */ 4110b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 411106ea65a3SJosef Bacik return; 411206ea65a3SJosef Bacik #endif 411306ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41140b246afaSJeff Mahoney fs_info = root->fs_info; 4115b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 41160b246afaSJeff Mahoney if (transid != fs_info->generation) 41175d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 41180b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 41190b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4120e2d84521SMiao Xie if (!was_dirty) 4121104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4122e2d84521SMiao Xie buf->len, 41230b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 41241f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 412569fc6cbbSQu Wenruo /* 412669fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 412769fc6cbbSQu Wenruo * but item data not updated. 412869fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 412969fc6cbbSQu Wenruo */ 413069fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 41312f659546SQu Wenruo btrfs_check_leaf_relaxed(fs_info, buf)) { 4132a4f78750SDavid Sterba btrfs_print_leaf(buf); 41331f21ef0aSFilipe Manana ASSERT(0); 41341f21ef0aSFilipe Manana } 41351f21ef0aSFilipe Manana #endif 4136eb60ceacSChris Mason } 4137eb60ceacSChris Mason 41382ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4139b53d3f5dSLiu Bo int flush_delayed) 414035b7e476SChris Mason { 4141188de649SChris Mason /* 4142188de649SChris Mason * looks as though older kernels can get into trouble with 4143188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4144188de649SChris Mason */ 4145e2d84521SMiao Xie int ret; 4146d6bfde87SChris Mason 41476933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4148d6bfde87SChris Mason return; 4149d6bfde87SChris Mason 4150b53d3f5dSLiu Bo if (flush_delayed) 41512ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 415216cdcec7SMiao Xie 4153d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4154d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4155d814a491SEthan Lien fs_info->dirty_metadata_batch); 4156e2d84521SMiao Xie if (ret > 0) { 41570b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 415816cdcec7SMiao Xie } 415916cdcec7SMiao Xie } 416016cdcec7SMiao Xie 41612ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 416216cdcec7SMiao Xie { 41632ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 416435b7e476SChris Mason } 4165b53d3f5dSLiu Bo 41662ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4167b53d3f5dSLiu Bo { 41682ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4169d6bfde87SChris Mason } 41706b80053dSChris Mason 4171581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4172581c1760SQu Wenruo struct btrfs_key *first_key) 41736b80053dSChris Mason { 4174727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41752ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41762ff7e61eSJeff Mahoney 4177581c1760SQu Wenruo return btree_read_extent_buffer_pages(fs_info, buf, parent_transid, 4178581c1760SQu Wenruo level, first_key); 41796b80053dSChris Mason } 41800da5468fSChris Mason 41812ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4182acce952bSliubo { 4183fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4184fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4185fe816d0fSNikolay Borisov 41860b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 41872ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 41880b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4189acce952bSliubo 41900b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 41910b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4192acce952bSliubo } 4193acce952bSliubo 4194143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4195acce952bSliubo { 4196acce952bSliubo struct btrfs_ordered_extent *ordered; 4197acce952bSliubo 4198199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4199779880efSJosef Bacik /* 4200779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4201779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4202779880efSJosef Bacik */ 4203199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4204779880efSJosef Bacik root_extent_list) 4205779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4206199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4207199c2a9cSMiao Xie } 4208199c2a9cSMiao Xie 4209199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4210199c2a9cSMiao Xie { 4211199c2a9cSMiao Xie struct btrfs_root *root; 4212199c2a9cSMiao Xie struct list_head splice; 4213199c2a9cSMiao Xie 4214199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4215199c2a9cSMiao Xie 4216199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4217199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4218199c2a9cSMiao Xie while (!list_empty(&splice)) { 4219199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4220199c2a9cSMiao Xie ordered_root); 42211de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42221de2cfdeSJosef Bacik &fs_info->ordered_roots); 4223199c2a9cSMiao Xie 42242a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4225199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4226199c2a9cSMiao Xie 42272a85d9caSLiu Bo cond_resched(); 42282a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4229199c2a9cSMiao Xie } 4230199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 423174d5d229SJosef Bacik 423274d5d229SJosef Bacik /* 423374d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 423474d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 423574d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 423674d5d229SJosef Bacik * actually get run and error out properly. 423774d5d229SJosef Bacik */ 423874d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4239acce952bSliubo } 4240acce952bSliubo 424135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 42422ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4243acce952bSliubo { 4244acce952bSliubo struct rb_node *node; 4245acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4246acce952bSliubo struct btrfs_delayed_ref_node *ref; 4247acce952bSliubo int ret = 0; 4248acce952bSliubo 4249acce952bSliubo delayed_refs = &trans->delayed_refs; 4250acce952bSliubo 4251acce952bSliubo spin_lock(&delayed_refs->lock); 4252d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4253cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 42540b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4255acce952bSliubo return ret; 4256acce952bSliubo } 4257acce952bSliubo 42585c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4259d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 42600e0adbcfSJosef Bacik struct rb_node *n; 4261e78417d1SJosef Bacik bool pin_bytes = false; 4262acce952bSliubo 4263d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4264d7df2c79SJosef Bacik href_node); 42653069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4266b939d1abSJosef Bacik continue; 42673069bd26SJosef Bacik 4268d7df2c79SJosef Bacik spin_lock(&head->lock); 4269e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 42700e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 42710e0adbcfSJosef Bacik ref_node); 4272d7df2c79SJosef Bacik ref->in_tree = 0; 4273e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 42740e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 42751d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 42761d57ee94SWang Xiaoguang list_del(&ref->add_list); 4277d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4278d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4279d7df2c79SJosef Bacik } 428054067ae9SJosef Bacik if (head->must_insert_reserved) 4281e78417d1SJosef Bacik pin_bytes = true; 428278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4283fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4284d7df2c79SJosef Bacik spin_unlock(&head->lock); 4285acce952bSliubo spin_unlock(&delayed_refs->lock); 4286e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4287acce952bSliubo 4288d7df2c79SJosef Bacik if (pin_bytes) 4289d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4290d278850eSJosef Bacik head->num_bytes, 1); 429131890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4292d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4293acce952bSliubo cond_resched(); 4294acce952bSliubo spin_lock(&delayed_refs->lock); 4295acce952bSliubo } 4296acce952bSliubo 4297acce952bSliubo spin_unlock(&delayed_refs->lock); 4298acce952bSliubo 4299acce952bSliubo return ret; 4300acce952bSliubo } 4301acce952bSliubo 4302143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4303acce952bSliubo { 4304acce952bSliubo struct btrfs_inode *btrfs_inode; 4305acce952bSliubo struct list_head splice; 4306acce952bSliubo 4307acce952bSliubo INIT_LIST_HEAD(&splice); 4308acce952bSliubo 4309eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4310eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4311acce952bSliubo 4312acce952bSliubo while (!list_empty(&splice)) { 4313fe816d0fSNikolay Borisov struct inode *inode = NULL; 4314eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4315acce952bSliubo delalloc_inodes); 4316fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4317eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4318acce952bSliubo 4319fe816d0fSNikolay Borisov /* 4320fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4321fe816d0fSNikolay Borisov * meanwhile. 4322fe816d0fSNikolay Borisov */ 4323fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4324fe816d0fSNikolay Borisov if (inode) { 4325fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4326fe816d0fSNikolay Borisov iput(inode); 4327fe816d0fSNikolay Borisov } 4328eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4329acce952bSliubo } 4330eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4331eb73c1b7SMiao Xie } 4332eb73c1b7SMiao Xie 4333eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4334eb73c1b7SMiao Xie { 4335eb73c1b7SMiao Xie struct btrfs_root *root; 4336eb73c1b7SMiao Xie struct list_head splice; 4337eb73c1b7SMiao Xie 4338eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4339eb73c1b7SMiao Xie 4340eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4341eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4342eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4343eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4344eb73c1b7SMiao Xie delalloc_root); 4345eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4346eb73c1b7SMiao Xie BUG_ON(!root); 4347eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4348eb73c1b7SMiao Xie 4349eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4350eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4351eb73c1b7SMiao Xie 4352eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4353eb73c1b7SMiao Xie } 4354eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4355acce952bSliubo } 4356acce952bSliubo 43572ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4358acce952bSliubo struct extent_io_tree *dirty_pages, 4359acce952bSliubo int mark) 4360acce952bSliubo { 4361acce952bSliubo int ret; 4362acce952bSliubo struct extent_buffer *eb; 4363acce952bSliubo u64 start = 0; 4364acce952bSliubo u64 end; 4365acce952bSliubo 4366acce952bSliubo while (1) { 4367acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4368e6138876SJosef Bacik mark, NULL); 4369acce952bSliubo if (ret) 4370acce952bSliubo break; 4371acce952bSliubo 437291166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4373acce952bSliubo while (start <= end) { 43740b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 43750b246afaSJeff Mahoney start += fs_info->nodesize; 4376fd8b2b61SJosef Bacik if (!eb) 4377acce952bSliubo continue; 4378fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4379acce952bSliubo 4380fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4381fd8b2b61SJosef Bacik &eb->bflags)) 4382fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4383fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4384acce952bSliubo } 4385acce952bSliubo } 4386acce952bSliubo 4387acce952bSliubo return ret; 4388acce952bSliubo } 4389acce952bSliubo 43902ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4391acce952bSliubo struct extent_io_tree *pinned_extents) 4392acce952bSliubo { 4393acce952bSliubo struct extent_io_tree *unpin; 4394acce952bSliubo u64 start; 4395acce952bSliubo u64 end; 4396acce952bSliubo int ret; 4397ed0eaa14SLiu Bo bool loop = true; 4398acce952bSliubo 4399acce952bSliubo unpin = pinned_extents; 4400ed0eaa14SLiu Bo again: 4401acce952bSliubo while (1) { 44020e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 44030e6ec385SFilipe Manana 4404fcd5e742SLu Fengqi /* 4405fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4406fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4407fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4408fcd5e742SLu Fengqi * the same extent range. 4409fcd5e742SLu Fengqi */ 4410fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4411acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 44120e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4413fcd5e742SLu Fengqi if (ret) { 4414fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4415acce952bSliubo break; 4416fcd5e742SLu Fengqi } 4417acce952bSliubo 44180e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 44190e6ec385SFilipe Manana free_extent_state(cached_state); 44202ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4421fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4422acce952bSliubo cond_resched(); 4423acce952bSliubo } 4424acce952bSliubo 4425ed0eaa14SLiu Bo if (loop) { 44260b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44270b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4428ed0eaa14SLiu Bo else 44290b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4430ed0eaa14SLiu Bo loop = false; 4431ed0eaa14SLiu Bo goto again; 4432ed0eaa14SLiu Bo } 4433ed0eaa14SLiu Bo 4434acce952bSliubo return 0; 4435acce952bSliubo } 4436acce952bSliubo 4437c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4438c79a1751SLiu Bo { 4439c79a1751SLiu Bo struct inode *inode; 4440c79a1751SLiu Bo 4441c79a1751SLiu Bo inode = cache->io_ctl.inode; 4442c79a1751SLiu Bo if (inode) { 4443c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4444c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4445c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4446c79a1751SLiu Bo iput(inode); 4447c79a1751SLiu Bo } 4448c79a1751SLiu Bo btrfs_put_block_group(cache); 4449c79a1751SLiu Bo } 4450c79a1751SLiu Bo 4451c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 44522ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4453c79a1751SLiu Bo { 4454c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4455c79a1751SLiu Bo 4456c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4457c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4458c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4459c79a1751SLiu Bo struct btrfs_block_group_cache, 4460c79a1751SLiu Bo dirty_list); 4461c79a1751SLiu Bo 4462c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4463c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4464c79a1751SLiu Bo list_del_init(&cache->io_list); 4465c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4466c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4467c79a1751SLiu Bo } 4468c79a1751SLiu Bo 4469c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4470c79a1751SLiu Bo spin_lock(&cache->lock); 4471c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4472c79a1751SLiu Bo spin_unlock(&cache->lock); 4473c79a1751SLiu Bo 4474c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4475c79a1751SLiu Bo btrfs_put_block_group(cache); 4476ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4477c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4478c79a1751SLiu Bo } 4479c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4480c79a1751SLiu Bo 448145ae2c18SNikolay Borisov /* 448245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 448345ae2c18SNikolay Borisov * to use it without any locking 448445ae2c18SNikolay Borisov */ 4485c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4486c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4487c79a1751SLiu Bo struct btrfs_block_group_cache, 4488c79a1751SLiu Bo io_list); 4489c79a1751SLiu Bo 4490c79a1751SLiu Bo list_del_init(&cache->io_list); 4491c79a1751SLiu Bo spin_lock(&cache->lock); 4492c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4493c79a1751SLiu Bo spin_unlock(&cache->lock); 4494c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4495c79a1751SLiu Bo } 4496c79a1751SLiu Bo } 4497c79a1751SLiu Bo 449849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 44992ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 450049b25e05SJeff Mahoney { 45012ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4502c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4503c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4504c79a1751SLiu Bo 45052ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 450649b25e05SJeff Mahoney 45074a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45080b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 450949b25e05SJeff Mahoney 45104a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45110b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 451249b25e05SJeff Mahoney 4513ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4514ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 451567cde344SMiao Xie 45162ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 451749b25e05SJeff Mahoney EXTENT_DIRTY); 45182ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45190b246afaSJeff Mahoney fs_info->pinned_extents); 452049b25e05SJeff Mahoney 45214a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45224a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 452349b25e05SJeff Mahoney } 452449b25e05SJeff Mahoney 45252ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4526acce952bSliubo { 4527acce952bSliubo struct btrfs_transaction *t; 4528acce952bSliubo 45290b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4530acce952bSliubo 45310b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45320b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 45330b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4534724e2315SJosef Bacik struct btrfs_transaction, list); 4535724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 45369b64f57dSElena Reshetova refcount_inc(&t->use_count); 45370b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45382ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4539724e2315SJosef Bacik btrfs_put_transaction(t); 45400b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4541724e2315SJosef Bacik continue; 4542724e2315SJosef Bacik } 45430b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4544724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 45450b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4546724e2315SJosef Bacik /* 4547724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4548724e2315SJosef Bacik * handle open currently for this transaction. 4549724e2315SJosef Bacik */ 4550724e2315SJosef Bacik wait_event(t->writer_wait, 4551724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4552724e2315SJosef Bacik } else { 45530b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4554724e2315SJosef Bacik } 45552ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4556724e2315SJosef Bacik 45570b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 45580b246afaSJeff Mahoney if (t == fs_info->running_transaction) 45590b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4560724e2315SJosef Bacik list_del_init(&t->list); 45610b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4562a4abeea4SJosef Bacik 4563724e2315SJosef Bacik btrfs_put_transaction(t); 45642ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 45650b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4566724e2315SJosef Bacik } 45670b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45680b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4569ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4570ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 45712ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 45720b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 45730b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4574acce952bSliubo 4575acce952bSliubo return 0; 4576acce952bSliubo } 4577acce952bSliubo 4578e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 45794d53dddbSDavid Sterba /* mandatory callbacks */ 45800b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 45814d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 45820da5468fSChris Mason }; 4583