1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6e20d96d6SChris Mason #include <linux/fs.h> 7d98237b3SChris Mason #include <linux/blkdev.h> 80f7d52f4SChris Mason #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10ce9adaa5SChris Mason #include <linux/workqueue.h> 11a74a4b97SChris Mason #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13784b4e29SChris Mason #include <linux/migrate.h> 147a36ddecSDavid Sterba #include <linux/ratelimit.h> 156463fe58SStefan Behrens #include <linux/uuid.h> 16803b2f54SStefan Behrens #include <linux/semaphore.h> 17540adea3SMasami Hiramatsu #include <linux/error-injection.h> 189678c543SNikolay Borisov #include <linux/crc32c.h> 19b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 207e75bf3fSDavid Sterba #include <asm/unaligned.h> 216d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 32581bb050SLi Zefan #include "inode-map.h" 3321adbd5cSStefan Behrens #include "check-integrity.h" 34606686eeSJosef Bacik #include "rcu-string.h" 358dabb742SStefan Behrens #include "dev-replace.h" 3653b381b3SDavid Woodhouse #include "raid56.h" 375ac1d209SJeff Mahoney #include "sysfs.h" 38fcebe456SJosef Bacik #include "qgroup.h" 39ebb8765bSAnand Jain #include "compression.h" 40557ea5ddSQu Wenruo #include "tree-checker.h" 41fd708b81SJosef Bacik #include "ref-verify.h" 42aac0023cSJosef Bacik #include "block-group.h" 43b0643e59SDennis Zhou #include "discard.h" 44f603bb94SNikolay Borisov #include "space-info.h" 45b70f5097SNaohiro Aota #include "zoned.h" 46eb60ceacSChris Mason 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 548b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 55143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 56acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 58143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 592ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 60acce952bSliubo struct extent_io_tree *dirty_pages, 61acce952bSliubo int mark); 622ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 63acce952bSliubo struct extent_io_tree *pinned_extents); 642ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 66ce9adaa5SChris Mason 67d352ac68SChris Mason /* 6897eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6997eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 70d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 71d352ac68SChris Mason */ 7297eb6b69SDavid Sterba struct btrfs_end_io_wq { 73ce9adaa5SChris Mason struct bio *bio; 74ce9adaa5SChris Mason bio_end_io_t *end_io; 75ce9adaa5SChris Mason void *private; 76ce9adaa5SChris Mason struct btrfs_fs_info *info; 774e4cbee9SChristoph Hellwig blk_status_t status; 78bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 798b712842SChris Mason struct btrfs_work work; 80ce9adaa5SChris Mason }; 810da5468fSChris Mason 8297eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8397eb6b69SDavid Sterba 8497eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8597eb6b69SDavid Sterba { 8697eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8797eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8897eb6b69SDavid Sterba 0, 89fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9097eb6b69SDavid Sterba NULL); 9197eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9297eb6b69SDavid Sterba return -ENOMEM; 9397eb6b69SDavid Sterba return 0; 9497eb6b69SDavid Sterba } 9597eb6b69SDavid Sterba 96e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9797eb6b69SDavid Sterba { 9897eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9997eb6b69SDavid Sterba } 10097eb6b69SDavid Sterba 101141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 102141386e1SJosef Bacik { 103141386e1SJosef Bacik if (fs_info->csum_shash) 104141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 105141386e1SJosef Bacik } 106141386e1SJosef Bacik 107d352ac68SChris Mason /* 108d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 109d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 110d352ac68SChris Mason * just before they are sent down the IO stack. 111d352ac68SChris Mason */ 11244b8bd7eSChris Mason struct async_submit_bio { 1138896a08dSQu Wenruo struct inode *inode; 11444b8bd7eSChris Mason struct bio *bio; 115a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11644b8bd7eSChris Mason int mirror_num; 117eaf25d93SChris Mason /* 118eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 119eaf25d93SChris Mason * can't tell us where in the file the bio should go 120eaf25d93SChris Mason */ 121eaf25d93SChris Mason u64 bio_offset; 1228b712842SChris Mason struct btrfs_work work; 1234e4cbee9SChristoph Hellwig blk_status_t status; 12444b8bd7eSChris Mason }; 12544b8bd7eSChris Mason 12685d4e461SChris Mason /* 12785d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12885d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12985d4e461SChris Mason * the level the eb occupies in the tree. 1304008c04aSChris Mason * 13185d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13285d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13385d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1344fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1354fd786e6SMisono Tomohiro * roots have separate keysets. 1364008c04aSChris Mason * 13785d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13885d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13985d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1404008c04aSChris Mason * 14185d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14285d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14385d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14485d4e461SChris Mason * 14585d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14685d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14785d4e461SChris Mason * needs update as well. 1484008c04aSChris Mason */ 1494008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1504008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1514008c04aSChris Mason # error 1524008c04aSChris Mason # endif 15385d4e461SChris Mason 154ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 155ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 156ab1405aaSDavid Sterba 157ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 163ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 164ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 165ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 166ab1405aaSDavid Sterba 16785d4e461SChris Mason static struct btrfs_lockdep_keyset { 16885d4e461SChris Mason u64 id; /* root objectid */ 169ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 170387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 171387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 17285d4e461SChris Mason } btrfs_lockdep_keysets[] = { 173ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 174ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 175ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 176ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 177ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 178ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 179ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 180ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 181ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 182ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 183ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 184ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1854008c04aSChris Mason }; 18685d4e461SChris Mason 187ab1405aaSDavid Sterba #undef DEFINE_LEVEL 188ab1405aaSDavid Sterba #undef DEFINE_NAME 18985d4e461SChris Mason 19085d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 19185d4e461SChris Mason int level) 19285d4e461SChris Mason { 19385d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19485d4e461SChris Mason 19585d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19685d4e461SChris Mason 19785d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19885d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19985d4e461SChris Mason if (ks->id == objectid) 20085d4e461SChris Mason break; 20185d4e461SChris Mason 20285d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20385d4e461SChris Mason &ks->keys[level], ks->names[level]); 20485d4e461SChris Mason } 20585d4e461SChris Mason 2064008c04aSChris Mason #endif 2074008c04aSChris Mason 208d352ac68SChris Mason /* 2092996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 210d352ac68SChris Mason */ 211c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 21219c00ddcSChris Mason { 213d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 214e9be5a30SDavid Sterba const int num_pages = fs_info->nodesize >> PAGE_SHIFT; 215a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 216d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 21719c00ddcSChris Mason char *kaddr; 218e9be5a30SDavid Sterba int i; 219d5178578SJohannes Thumshirn 220d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 221d5178578SJohannes Thumshirn crypto_shash_init(shash); 222a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 223e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 224a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 22519c00ddcSChris Mason 226e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 227e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 228e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 22919c00ddcSChris Mason } 23071a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 231d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 23219c00ddcSChris Mason } 23319c00ddcSChris Mason 234d352ac68SChris Mason /* 235d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 236d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 237d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 238d352ac68SChris Mason * in the wrong place. 239d352ac68SChris Mason */ 2401259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 241b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 242b9fab919SChris Mason int atomic) 2431259ab75SChris Mason { 2442ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2451259ab75SChris Mason int ret; 2462755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 2471259ab75SChris Mason 2481259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2491259ab75SChris Mason return 0; 2501259ab75SChris Mason 251b9fab919SChris Mason if (atomic) 252b9fab919SChris Mason return -EAGAIN; 253b9fab919SChris Mason 254ac5887c8SJosef Bacik if (need_lock) 255a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 256a26e8c9fSJosef Bacik 2572ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 258ff13db41SDavid Sterba &cached_state); 2590b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2601259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2611259ab75SChris Mason ret = 0; 2621259ab75SChris Mason goto out; 2631259ab75SChris Mason } 26494647322SDavid Sterba btrfs_err_rl(eb->fs_info, 26594647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 26694647322SDavid Sterba eb->start, 26729549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2681259ab75SChris Mason ret = 1; 269a26e8c9fSJosef Bacik 270a26e8c9fSJosef Bacik /* 271a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 272a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 27301327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 274a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 275a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 276a26e8c9fSJosef Bacik * making everybody very sad. 277a26e8c9fSJosef Bacik */ 278a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 2790b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 28033958dc6SChris Mason out: 2812ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 282e43bbe5eSDavid Sterba &cached_state); 283472b909fSJosef Bacik if (need_lock) 284ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 2851259ab75SChris Mason return ret; 2861259ab75SChris Mason } 2871259ab75SChris Mason 288e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 289e7e16f48SJohannes Thumshirn { 290e7e16f48SJohannes Thumshirn switch (csum_type) { 291e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2923951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2933831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 294352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 295e7e16f48SJohannes Thumshirn return true; 296e7e16f48SJohannes Thumshirn default: 297e7e16f48SJohannes Thumshirn return false; 298e7e16f48SJohannes Thumshirn } 299e7e16f48SJohannes Thumshirn } 300e7e16f48SJohannes Thumshirn 301d352ac68SChris Mason /* 3021104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3031104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3041104a885SDavid Sterba */ 305ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 306ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3071104a885SDavid Sterba { 3081104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3091104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 31051bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 311d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 312d5178578SJohannes Thumshirn 313d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3141104a885SDavid Sterba 3151104a885SDavid Sterba /* 3161104a885SDavid Sterba * The super_block structure does not span the whole 31751bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 31851bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3191104a885SDavid Sterba */ 320fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 321fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3221104a885SDavid Sterba 32355fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 324e7e16f48SJohannes Thumshirn return 1; 3251104a885SDavid Sterba 326e7e16f48SJohannes Thumshirn return 0; 3271104a885SDavid Sterba } 3281104a885SDavid Sterba 329e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 330ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 331581c1760SQu Wenruo { 332e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 333581c1760SQu Wenruo int found_level; 334581c1760SQu Wenruo struct btrfs_key found_key; 335581c1760SQu Wenruo int ret; 336581c1760SQu Wenruo 337581c1760SQu Wenruo found_level = btrfs_header_level(eb); 338581c1760SQu Wenruo if (found_level != level) { 33963489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 34063489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 341581c1760SQu Wenruo btrfs_err(fs_info, 342581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 343581c1760SQu Wenruo eb->start, level, found_level); 344581c1760SQu Wenruo return -EIO; 345581c1760SQu Wenruo } 346581c1760SQu Wenruo 347581c1760SQu Wenruo if (!first_key) 348581c1760SQu Wenruo return 0; 349581c1760SQu Wenruo 3505d41be6fSQu Wenruo /* 3515d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3525d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3535d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3545d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3555d41be6fSQu Wenruo */ 3565d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3575d41be6fSQu Wenruo return 0; 35862fdaa52SQu Wenruo 35962fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 36062fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 36162fdaa52SQu Wenruo btrfs_err(fs_info, 36262fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 36362fdaa52SQu Wenruo eb->start); 36462fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 36562fdaa52SQu Wenruo return -EUCLEAN; 36662fdaa52SQu Wenruo } 36762fdaa52SQu Wenruo 368581c1760SQu Wenruo if (found_level) 369581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 370581c1760SQu Wenruo else 371581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 372581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 373581c1760SQu Wenruo 374581c1760SQu Wenruo if (ret) { 37563489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 37663489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 377581c1760SQu Wenruo btrfs_err(fs_info, 378ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 379ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 380ff76a864SLiu Bo first_key->type, first_key->offset, 381ff76a864SLiu Bo found_key.objectid, found_key.type, 382ff76a864SLiu Bo found_key.offset); 383581c1760SQu Wenruo } 384581c1760SQu Wenruo return ret; 385581c1760SQu Wenruo } 386581c1760SQu Wenruo 3871104a885SDavid Sterba /* 388d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 389d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 390581c1760SQu Wenruo * 391581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 392581c1760SQu Wenruo * @level: expected level, mandatory check 393581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 394d352ac68SChris Mason */ 3955ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 396581c1760SQu Wenruo u64 parent_transid, int level, 397581c1760SQu Wenruo struct btrfs_key *first_key) 398f188591eSChris Mason { 3995ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 400f188591eSChris Mason struct extent_io_tree *io_tree; 401ea466794SJosef Bacik int failed = 0; 402f188591eSChris Mason int ret; 403f188591eSChris Mason int num_copies = 0; 404f188591eSChris Mason int mirror_num = 0; 405ea466794SJosef Bacik int failed_mirror = 0; 406f188591eSChris Mason 4070b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 408f188591eSChris Mason while (1) { 409f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 410c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 411256dd1bbSStefan Behrens if (!ret) { 412581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 413b9fab919SChris Mason parent_transid, 0)) 414256dd1bbSStefan Behrens ret = -EIO; 415e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 416ff76a864SLiu Bo first_key, parent_transid)) 417581c1760SQu Wenruo ret = -EUCLEAN; 418581c1760SQu Wenruo else 419581c1760SQu Wenruo break; 420256dd1bbSStefan Behrens } 421d397712bSChris Mason 4220b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 423f188591eSChris Mason eb->start, eb->len); 4244235298eSChris Mason if (num_copies == 1) 425ea466794SJosef Bacik break; 4264235298eSChris Mason 4275cf1ab56SJosef Bacik if (!failed_mirror) { 4285cf1ab56SJosef Bacik failed = 1; 4295cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4305cf1ab56SJosef Bacik } 4315cf1ab56SJosef Bacik 432f188591eSChris Mason mirror_num++; 433ea466794SJosef Bacik if (mirror_num == failed_mirror) 434ea466794SJosef Bacik mirror_num++; 435ea466794SJosef Bacik 4364235298eSChris Mason if (mirror_num > num_copies) 437ea466794SJosef Bacik break; 438f188591eSChris Mason } 439ea466794SJosef Bacik 440c0901581SStefan Behrens if (failed && !ret && failed_mirror) 44120a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 442ea466794SJosef Bacik 443ea466794SJosef Bacik return ret; 444f188591eSChris Mason } 44519c00ddcSChris Mason 446d352ac68SChris Mason /* 447ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 448ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 449ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 450d352ac68SChris Mason */ 451ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 45219c00ddcSChris Mason { 453ac303b69SQu Wenruo struct page *page = bvec->bv_page; 4544eee4fa4SMiao Xie u64 start = page_offset(page); 45519c00ddcSChris Mason u64 found_start; 4562996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 45719c00ddcSChris Mason struct extent_buffer *eb; 4588d47a0d8SQu Wenruo int ret; 459f188591eSChris Mason 4604f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4614f2de97aSJosef Bacik if (page != eb->pages[0]) 4624f2de97aSJosef Bacik return 0; 4630f805531SAlex Lyakas 46419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 4650f805531SAlex Lyakas /* 4660f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4670f805531SAlex Lyakas * It is useful to know what went wrong. 4680f805531SAlex Lyakas */ 4690f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4700f805531SAlex Lyakas return -EUCLEAN; 4710f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4720f805531SAlex Lyakas return -EUCLEAN; 4730f805531SAlex Lyakas 474de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 4759a8658e3SDavid Sterba offsetof(struct btrfs_header, fsid), 4769a8658e3SDavid Sterba BTRFS_FSID_SIZE) == 0); 4770f805531SAlex Lyakas 478c67b3892SDavid Sterba csum_tree_block(eb, result); 4792996e1f8SJohannes Thumshirn 4808d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 4818d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 4828d47a0d8SQu Wenruo else 4838d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 4848d47a0d8SQu Wenruo 4858d47a0d8SQu Wenruo if (ret < 0) { 486c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 4878d47a0d8SQu Wenruo btrfs_err(fs_info, 4888d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 4898d47a0d8SQu Wenruo eb->start); 490c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4918d47a0d8SQu Wenruo return ret; 4928d47a0d8SQu Wenruo } 493713cebfbSDavid Sterba write_extent_buffer(eb, result, 0, fs_info->csum_size); 4948d47a0d8SQu Wenruo 4952996e1f8SJohannes Thumshirn return 0; 49619c00ddcSChris Mason } 49719c00ddcSChris Mason 498b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 4992b82032cSYan Zheng { 500b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 501944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 50244880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 503944d3f9fSNikolay Borisov u8 *metadata_uuid; 5042b82032cSYan Zheng 5059a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5069a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5077239ff4bSNikolay Borisov /* 508944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 509944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 510944d3f9fSNikolay Borisov * ->fsid in this case is fine 5117239ff4bSNikolay Borisov */ 512944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5137239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5147239ff4bSNikolay Borisov else 5157239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5167239ff4bSNikolay Borisov 517944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 518944d3f9fSNikolay Borisov return 0; 519944d3f9fSNikolay Borisov 520944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 521944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 522944d3f9fSNikolay Borisov return 0; 523944d3f9fSNikolay Borisov 524944d3f9fSNikolay Borisov return 1; 5252b82032cSYan Zheng } 5262b82032cSYan Zheng 52777bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 52877bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 529ce9adaa5SChris Mason { 53077bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 531ce9adaa5SChris Mason u64 found_start; 53277bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 53377bf40a2SQu Wenruo u8 found_level; 5342996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 53577bf40a2SQu Wenruo int ret = 0; 536ea466794SJosef Bacik 537ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 538727011e0SChris Mason if (found_start != eb->start) { 539893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 540893bf4b1SSu Yue eb->start, found_start); 541f188591eSChris Mason ret = -EIO; 54277bf40a2SQu Wenruo goto out; 543ce9adaa5SChris Mason } 544b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 54502873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 54694647322SDavid Sterba eb->start); 5471259ab75SChris Mason ret = -EIO; 54877bf40a2SQu Wenruo goto out; 5491259ab75SChris Mason } 550ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5511c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 552893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 553893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5541c24c3ceSJosef Bacik ret = -EIO; 55577bf40a2SQu Wenruo goto out; 5561c24c3ceSJosef Bacik } 557ce9adaa5SChris Mason 558c67b3892SDavid Sterba csum_tree_block(eb, result); 559a826d6dcSJosef Bacik 5602996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 56135be8851SJohannes Thumshirn u8 val[BTRFS_CSUM_SIZE] = { 0 }; 5622996e1f8SJohannes Thumshirn 5632996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 5642996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 56535be8851SJohannes Thumshirn "%s checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5662996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 56735be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, val), 56835be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 56935be8851SJohannes Thumshirn btrfs_header_level(eb)); 5702996e1f8SJohannes Thumshirn ret = -EUCLEAN; 57177bf40a2SQu Wenruo goto out; 5722996e1f8SJohannes Thumshirn } 5732996e1f8SJohannes Thumshirn 574a826d6dcSJosef Bacik /* 575a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 576a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 577a826d6dcSJosef Bacik * return -EIO. 578a826d6dcSJosef Bacik */ 5791c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 580a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 581a826d6dcSJosef Bacik ret = -EIO; 582a826d6dcSJosef Bacik } 583ce9adaa5SChris Mason 584813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 585053ab70fSLiu Bo ret = -EIO; 586053ab70fSLiu Bo 5870b32f4bbSJosef Bacik if (!ret) 5880b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 58975391f0dSQu Wenruo else 59075391f0dSQu Wenruo btrfs_err(fs_info, 59175391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 59275391f0dSQu Wenruo eb->start); 59377bf40a2SQu Wenruo out: 59477bf40a2SQu Wenruo return ret; 59577bf40a2SQu Wenruo } 59677bf40a2SQu Wenruo 5978e1dc982SQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, 59877bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 59977bf40a2SQu Wenruo int mirror) 60077bf40a2SQu Wenruo { 60177bf40a2SQu Wenruo struct extent_buffer *eb; 60277bf40a2SQu Wenruo int ret = 0; 60377bf40a2SQu Wenruo int reads_done; 60477bf40a2SQu Wenruo 60577bf40a2SQu Wenruo ASSERT(page->private); 60677bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 60777bf40a2SQu Wenruo 60877bf40a2SQu Wenruo /* 60977bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 61077bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 61177bf40a2SQu Wenruo */ 61277bf40a2SQu Wenruo atomic_inc(&eb->refs); 61377bf40a2SQu Wenruo 61477bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 61577bf40a2SQu Wenruo if (!reads_done) 61677bf40a2SQu Wenruo goto err; 61777bf40a2SQu Wenruo 61877bf40a2SQu Wenruo eb->read_mirror = mirror; 61977bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 62077bf40a2SQu Wenruo ret = -EIO; 62177bf40a2SQu Wenruo goto err; 62277bf40a2SQu Wenruo } 62377bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 624ce9adaa5SChris Mason err: 62579fb65a1SJosef Bacik if (reads_done && 62679fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 627d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6284bb31e92SArne Jansen 62953b381b3SDavid Woodhouse if (ret) { 63053b381b3SDavid Woodhouse /* 63153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 63253b381b3SDavid Woodhouse * again, we have to make sure it has something 63353b381b3SDavid Woodhouse * to decrement 63453b381b3SDavid Woodhouse */ 63553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6360b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 63753b381b3SDavid Woodhouse } 6380b32f4bbSJosef Bacik free_extent_buffer(eb); 63977bf40a2SQu Wenruo 640f188591eSChris Mason return ret; 641ce9adaa5SChris Mason } 642ce9adaa5SChris Mason 6434246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 644ce9adaa5SChris Mason { 64597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 646ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6479e0af237SLiu Bo struct btrfs_workqueue *wq; 648ce9adaa5SChris Mason 649ce9adaa5SChris Mason fs_info = end_io_wq->info; 6504e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 651d20f7043SChris Mason 65237226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 653a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 6549e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 655a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 6569e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 657a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 6589e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 659a0cac0ecSOmar Sandoval else 6609e0af237SLiu Bo wq = fs_info->endio_write_workers; 6619e0af237SLiu Bo } else { 6625c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 6639e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 664a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 6659e0af237SLiu Bo wq = fs_info->endio_meta_workers; 666a0cac0ecSOmar Sandoval else 6679e0af237SLiu Bo wq = fs_info->endio_workers; 6689e0af237SLiu Bo } 6699e0af237SLiu Bo 670a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 6719e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 672ce9adaa5SChris Mason } 673ce9adaa5SChris Mason 6744e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 675bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 6760b86a832SChris Mason { 67797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 6788b110e39SMiao Xie 67997eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 680ce9adaa5SChris Mason if (!end_io_wq) 6814e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 682ce9adaa5SChris Mason 683ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 684ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 68522c59948SChris Mason end_io_wq->info = info; 6864e4cbee9SChristoph Hellwig end_io_wq->status = 0; 687ce9adaa5SChris Mason end_io_wq->bio = bio; 68822c59948SChris Mason end_io_wq->metadata = metadata; 689ce9adaa5SChris Mason 690ce9adaa5SChris Mason bio->bi_private = end_io_wq; 691ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 69222c59948SChris Mason return 0; 69322c59948SChris Mason } 69422c59948SChris Mason 6954a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 6964a69a410SChris Mason { 6974a69a410SChris Mason struct async_submit_bio *async; 6984e4cbee9SChristoph Hellwig blk_status_t ret; 6994a69a410SChris Mason 7004a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7018896a08dSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, async->bio_offset); 70279787eaaSJeff Mahoney if (ret) 7034e4cbee9SChristoph Hellwig async->status = ret; 7044a69a410SChris Mason } 7054a69a410SChris Mason 70606ea01b1SDavid Sterba /* 70706ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 70806ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 70906ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 71006ea01b1SDavid Sterba * 71106ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 71206ea01b1SDavid Sterba * inserted into the tree. 71306ea01b1SDavid Sterba */ 7144a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7158b712842SChris Mason { 7168b712842SChris Mason struct async_submit_bio *async; 71706ea01b1SDavid Sterba struct inode *inode; 71806ea01b1SDavid Sterba blk_status_t ret; 7198b712842SChris Mason 7208b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7218896a08dSQu Wenruo inode = async->inode; 7224854ddd0SChris Mason 723bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7244e4cbee9SChristoph Hellwig if (async->status) { 7254e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7264246a0b6SChristoph Hellwig bio_endio(async->bio); 72779787eaaSJeff Mahoney return; 72879787eaaSJeff Mahoney } 72979787eaaSJeff Mahoney 730ec39f769SChris Mason /* 731ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 732ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 733ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 734ec39f769SChris Mason */ 735ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 73608635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 73706ea01b1SDavid Sterba if (ret) { 73806ea01b1SDavid Sterba async->bio->bi_status = ret; 73906ea01b1SDavid Sterba bio_endio(async->bio); 74006ea01b1SDavid Sterba } 7414a69a410SChris Mason } 7424a69a410SChris Mason 7434a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7444a69a410SChris Mason { 7454a69a410SChris Mason struct async_submit_bio *async; 7464a69a410SChris Mason 7474a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7488b712842SChris Mason kfree(async); 7498b712842SChris Mason } 7508b712842SChris Mason 7518896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 752c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 7538896a08dSQu Wenruo u64 bio_offset, 754e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 75544b8bd7eSChris Mason { 7568896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 75744b8bd7eSChris Mason struct async_submit_bio *async; 75844b8bd7eSChris Mason 75944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 76044b8bd7eSChris Mason if (!async) 7614e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 76244b8bd7eSChris Mason 7638896a08dSQu Wenruo async->inode = inode; 76444b8bd7eSChris Mason async->bio = bio; 76544b8bd7eSChris Mason async->mirror_num = mirror_num; 7664a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7674a69a410SChris Mason 768a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 769a0cac0ecSOmar Sandoval run_one_async_free); 7704a69a410SChris Mason 771eaf25d93SChris Mason async->bio_offset = bio_offset; 7728c8bee1dSChris Mason 7734e4cbee9SChristoph Hellwig async->status = 0; 77479787eaaSJeff Mahoney 77567f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 7765cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 777d313d7a3SChris Mason 7785cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 77944b8bd7eSChris Mason return 0; 78044b8bd7eSChris Mason } 78144b8bd7eSChris Mason 7824e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 783ce3ed71aSChris Mason { 7842c30c71bSKent Overstreet struct bio_vec *bvec; 785ce3ed71aSChris Mason struct btrfs_root *root; 7862b070cfeSChristoph Hellwig int ret = 0; 7876dc4f100SMing Lei struct bvec_iter_all iter_all; 788ce3ed71aSChris Mason 789c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 7902b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 791ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 792ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 79379787eaaSJeff Mahoney if (ret) 79479787eaaSJeff Mahoney break; 795ce3ed71aSChris Mason } 7962c30c71bSKent Overstreet 7974e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 798ce3ed71aSChris Mason } 799ce3ed71aSChris Mason 8008896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 801eaf25d93SChris Mason u64 bio_offset) 80222c59948SChris Mason { 8038b712842SChris Mason /* 8048b712842SChris Mason * when we're called for a write, we're already in the async 8055443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8068b712842SChris Mason */ 80779787eaaSJeff Mahoney return btree_csum_one_bio(bio); 80822c59948SChris Mason } 80922c59948SChris Mason 8109b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8119b4e675aSDavid Sterba struct btrfs_inode *bi) 812de0022b9SJosef Bacik { 8136300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8146300463bSLiu Bo return 0; 8159b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 816de0022b9SJosef Bacik return 0; 817de0022b9SJosef Bacik return 1; 818de0022b9SJosef Bacik } 819de0022b9SJosef Bacik 8201b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 8211b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 82244b8bd7eSChris Mason { 8230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8249b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8254e4cbee9SChristoph Hellwig blk_status_t ret; 826cad321adSChris Mason 82737226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 828cad321adSChris Mason /* 829cad321adSChris Mason * called for a read, do the setup so that checksum validation 830cad321adSChris Mason * can happen in the async kernel threads 831cad321adSChris Mason */ 8320b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8330b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8341d4284bdSChris Mason if (ret) 83561891923SStefan Behrens goto out_w_error; 83608635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 837de0022b9SJosef Bacik } else if (!async) { 838de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 839de0022b9SJosef Bacik if (ret) 84061891923SStefan Behrens goto out_w_error; 84108635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 84261891923SStefan Behrens } else { 843cad321adSChris Mason /* 84461891923SStefan Behrens * kthread helpers are used to submit writes so that 84561891923SStefan Behrens * checksumming can happen in parallel across all CPUs 846cad321adSChris Mason */ 8478896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 8488896a08dSQu Wenruo 0, btree_submit_bio_start); 84944b8bd7eSChris Mason } 85044b8bd7eSChris Mason 8514246a0b6SChristoph Hellwig if (ret) 8524246a0b6SChristoph Hellwig goto out_w_error; 8534246a0b6SChristoph Hellwig return 0; 8544246a0b6SChristoph Hellwig 85561891923SStefan Behrens out_w_error: 8564e4cbee9SChristoph Hellwig bio->bi_status = ret; 8574246a0b6SChristoph Hellwig bio_endio(bio); 85861891923SStefan Behrens return ret; 85961891923SStefan Behrens } 86061891923SStefan Behrens 8613dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 862784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 863a6bc32b8SMel Gorman struct page *newpage, struct page *page, 864a6bc32b8SMel Gorman enum migrate_mode mode) 865784b4e29SChris Mason { 866784b4e29SChris Mason /* 867784b4e29SChris Mason * we can't safely write a btree page from here, 868784b4e29SChris Mason * we haven't done the locking hook 869784b4e29SChris Mason */ 870784b4e29SChris Mason if (PageDirty(page)) 871784b4e29SChris Mason return -EAGAIN; 872784b4e29SChris Mason /* 873784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 874784b4e29SChris Mason * We must have no buffers or drop them. 875784b4e29SChris Mason */ 876784b4e29SChris Mason if (page_has_private(page) && 877784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 878784b4e29SChris Mason return -EAGAIN; 879a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 880784b4e29SChris Mason } 8813dd1462eSJan Beulich #endif 882784b4e29SChris Mason 8830da5468fSChris Mason 8840da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8850da5468fSChris Mason struct writeback_control *wbc) 8860da5468fSChris Mason { 887e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 888e2d84521SMiao Xie int ret; 889e2d84521SMiao Xie 890d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 891448d640bSChris Mason 892448d640bSChris Mason if (wbc->for_kupdate) 893448d640bSChris Mason return 0; 894448d640bSChris Mason 895e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 896b9473439SChris Mason /* this is a bit racy, but that's ok */ 897d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 898d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 899d814a491SEthan Lien fs_info->dirty_metadata_batch); 900e2d84521SMiao Xie if (ret < 0) 901793955bcSChris Mason return 0; 902793955bcSChris Mason } 9030b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9040da5468fSChris Mason } 9050da5468fSChris Mason 90670dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9075f39d397SChris Mason { 90898509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 90998509cfcSChris Mason return 0; 9100c4e538bSDavid Sterba 911f7a52a40SDavid Sterba return try_release_extent_buffer(page); 912d98237b3SChris Mason } 913d98237b3SChris Mason 914d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 915d47992f8SLukas Czerner unsigned int length) 916d98237b3SChris Mason { 917d1310b2eSChris Mason struct extent_io_tree *tree; 918d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9195f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9205f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9219ad6b7bcSChris Mason if (PagePrivate(page)) { 922efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 923efe120a0SFrank Holton "page private not zero on page %llu", 924efe120a0SFrank Holton (unsigned long long)page_offset(page)); 925d1b89bc0SGuoqing Jiang detach_page_private(page); 9269ad6b7bcSChris Mason } 927d98237b3SChris Mason } 928d98237b3SChris Mason 9290b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9300b32f4bbSJosef Bacik { 931bb146eb2SJosef Bacik #ifdef DEBUG 9320b32f4bbSJosef Bacik struct extent_buffer *eb; 9330b32f4bbSJosef Bacik 9340b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9350b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9360b32f4bbSJosef Bacik BUG_ON(!eb); 9370b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9380b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9390b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 940bb146eb2SJosef Bacik #endif 9410b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9420b32f4bbSJosef Bacik } 9430b32f4bbSJosef Bacik 9447f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9450da5468fSChris Mason .writepages = btree_writepages, 9465f39d397SChris Mason .releasepage = btree_releasepage, 9475f39d397SChris Mason .invalidatepage = btree_invalidatepage, 9485a92bc88SChris Mason #ifdef CONFIG_MIGRATION 949784b4e29SChris Mason .migratepage = btree_migratepage, 9505a92bc88SChris Mason #endif 9510b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 952d98237b3SChris Mason }; 953123abc88SChris Mason 9542ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9552ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9563fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 9573fbaf258SJosef Bacik int level) 9580999df54SChris Mason { 9590b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9600b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 9613fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 9620999df54SChris Mason } 9630999df54SChris Mason 964581c1760SQu Wenruo /* 965581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 966581c1760SQu Wenruo * verification. 967581c1760SQu Wenruo * 9681b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 969581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 970581c1760SQu Wenruo * @level: expected level, mandatory check 971581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 972581c1760SQu Wenruo */ 9732ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 9741b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 9751b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 976e20d96d6SChris Mason { 9775f39d397SChris Mason struct extent_buffer *buf = NULL; 97819c00ddcSChris Mason int ret; 97919c00ddcSChris Mason 9803fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 981c871b0f2SLiu Bo if (IS_ERR(buf)) 982c871b0f2SLiu Bo return buf; 983e4204dedSChris Mason 9845ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 985581c1760SQu Wenruo level, first_key); 9860f0fe8f7SFilipe David Borba Manana if (ret) { 987537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 98864c043deSLiu Bo return ERR_PTR(ret); 9890f0fe8f7SFilipe David Borba Manana } 9905f39d397SChris Mason return buf; 991ce9adaa5SChris Mason 992eb60ceacSChris Mason } 993eb60ceacSChris Mason 9946a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 995ed2ff2cbSChris Mason { 9966a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 99755c69072SChris Mason if (btrfs_header_generation(buf) == 998e2d84521SMiao Xie fs_info->running_transaction->transid) { 999b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1000b4ce94deSChris Mason 1001b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1002104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1003e2d84521SMiao Xie -buf->len, 1004e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10050b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1006925baeddSChris Mason } 10075f39d397SChris Mason } 1008ed7b63ebSJosef Bacik } 10095f39d397SChris Mason 1010da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1011e20d96d6SChris Mason u64 objectid) 1012d97e63b6SChris Mason { 10137c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 101496dfcb46SJosef Bacik root->fs_info = fs_info; 1015cfaa7295SChris Mason root->node = NULL; 1016a28ec197SChris Mason root->commit_root = NULL; 101727cdeb70SMiao Xie root->state = 0; 1018d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 10190b86a832SChris Mason 10200f7d52f4SChris Mason root->last_trans = 0; 102113a8a7c8SYan, Zheng root->highest_objectid = 0; 1022eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1023199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10246bef4d31SEric Paris root->inode_tree = RB_ROOT; 102516cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1026f0486c68SYan, Zheng root->block_rsv = NULL; 10270b86a832SChris Mason 10280b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10295d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1030eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1031eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1032199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1033199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1034d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10352ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10362ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10375d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1038eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1039199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1040f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10412ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10422ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10438287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1044a2135011SChris Mason mutex_init(&root->objectid_mutex); 1045e02119d5SChris Mason mutex_init(&root->log_mutex); 104631f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1047573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1048c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10497237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10507237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10517237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 10528b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 10538b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 10547237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 10557237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 10567237f183SYan Zheng atomic_set(&root->log_writers, 0); 10572ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 10580700cea7SElena Reshetova refcount_set(&root->refs, 1); 10598ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1060eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 10617237f183SYan Zheng root->log_transid = 0; 1062d1433debSMiao Xie root->log_transid_committed = -1; 1063257c62e1SChris Mason root->last_log_commit = 0; 1064e289f03eSFilipe Manana if (!dummy) { 106543eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 106643eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1067e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1068e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1069e289f03eSFilipe Manana } 1070017e5369SChris Mason 10713768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 10723768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 10736702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 10744d775673SChris Mason root->root_key.objectid = objectid; 10750ee5dc67SAl Viro root->anon_dev = 0; 10768ea05e3aSAlexander Block 10775f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1078370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1079bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1080bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1081bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1082bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1083bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1084bd647ce3SJosef Bacik #endif 10853768f368SChris Mason } 10863768f368SChris Mason 108774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 108896dfcb46SJosef Bacik u64 objectid, gfp_t flags) 10896f07e42eSAl Viro { 109074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 10916f07e42eSAl Viro if (root) 109296dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 10936f07e42eSAl Viro return root; 10946f07e42eSAl Viro } 10956f07e42eSAl Viro 109606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 109706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1098da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 109906ea65a3SJosef Bacik { 110006ea65a3SJosef Bacik struct btrfs_root *root; 110106ea65a3SJosef Bacik 11027c0260eeSJeff Mahoney if (!fs_info) 11037c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11047c0260eeSJeff Mahoney 110596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 110606ea65a3SJosef Bacik if (!root) 110706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1108da17066cSJeff Mahoney 1109b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1110faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 111106ea65a3SJosef Bacik 111206ea65a3SJosef Bacik return root; 111306ea65a3SJosef Bacik } 111406ea65a3SJosef Bacik #endif 111506ea65a3SJosef Bacik 111620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 111720897f5cSArne Jansen u64 objectid) 111820897f5cSArne Jansen { 11199b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 112020897f5cSArne Jansen struct extent_buffer *leaf; 112120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 112220897f5cSArne Jansen struct btrfs_root *root; 112320897f5cSArne Jansen struct btrfs_key key; 1124b89f6d1fSFilipe Manana unsigned int nofs_flag; 112520897f5cSArne Jansen int ret = 0; 112620897f5cSArne Jansen 1127b89f6d1fSFilipe Manana /* 1128b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1129b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1130b89f6d1fSFilipe Manana */ 1131b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 113296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1133b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 113420897f5cSArne Jansen if (!root) 113520897f5cSArne Jansen return ERR_PTR(-ENOMEM); 113620897f5cSArne Jansen 113720897f5cSArne Jansen root->root_key.objectid = objectid; 113820897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 113920897f5cSArne Jansen root->root_key.offset = 0; 114020897f5cSArne Jansen 11419631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 11429631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 114320897f5cSArne Jansen if (IS_ERR(leaf)) { 114420897f5cSArne Jansen ret = PTR_ERR(leaf); 11451dd05682STsutomu Itoh leaf = NULL; 114620897f5cSArne Jansen goto fail; 114720897f5cSArne Jansen } 114820897f5cSArne Jansen 114920897f5cSArne Jansen root->node = leaf; 115020897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 115120897f5cSArne Jansen 115220897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 115327cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 115420897f5cSArne Jansen 1155f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1156f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 115720897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 115820897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 115920897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 116020897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 116120897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 116220897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 116320897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 116433d85fdaSQu Wenruo if (is_fstree(objectid)) 1165807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1166807fc790SAndy Shevchenko else 1167807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1168c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 116920897f5cSArne Jansen 117020897f5cSArne Jansen key.objectid = objectid; 117120897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 117220897f5cSArne Jansen key.offset = 0; 117320897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 117420897f5cSArne Jansen if (ret) 117520897f5cSArne Jansen goto fail; 117620897f5cSArne Jansen 117720897f5cSArne Jansen btrfs_tree_unlock(leaf); 117820897f5cSArne Jansen 117920897f5cSArne Jansen return root; 11801dd05682STsutomu Itoh 11811dd05682STsutomu Itoh fail: 11828c38938cSJosef Bacik if (leaf) 11831dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 118400246528SJosef Bacik btrfs_put_root(root); 11851dd05682STsutomu Itoh 11861dd05682STsutomu Itoh return ERR_PTR(ret); 118720897f5cSArne Jansen } 118820897f5cSArne Jansen 11897237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1190e02119d5SChris Mason struct btrfs_fs_info *fs_info) 11910f7d52f4SChris Mason { 11920f7d52f4SChris Mason struct btrfs_root *root; 11937237f183SYan Zheng struct extent_buffer *leaf; 1194e02119d5SChris Mason 119596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1196e02119d5SChris Mason if (!root) 11977237f183SYan Zheng return ERR_PTR(-ENOMEM); 1198e02119d5SChris Mason 1199e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1200e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1201e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 120227cdeb70SMiao Xie 12037237f183SYan Zheng /* 120492a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 120527cdeb70SMiao Xie * 120692a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 120792a7cc42SQu Wenruo * and they go away before a real commit is actually done. 120892a7cc42SQu Wenruo * 120992a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 121092a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 12117237f183SYan Zheng */ 1212e02119d5SChris Mason 12134d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 12149631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 12157237f183SYan Zheng if (IS_ERR(leaf)) { 121600246528SJosef Bacik btrfs_put_root(root); 12177237f183SYan Zheng return ERR_CAST(leaf); 12187237f183SYan Zheng } 1219e02119d5SChris Mason 12207237f183SYan Zheng root->node = leaf; 1221e02119d5SChris Mason 1222e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1223e02119d5SChris Mason btrfs_tree_unlock(root->node); 12247237f183SYan Zheng return root; 12257237f183SYan Zheng } 12267237f183SYan Zheng 12277237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 12287237f183SYan Zheng struct btrfs_fs_info *fs_info) 12297237f183SYan Zheng { 12307237f183SYan Zheng struct btrfs_root *log_root; 12317237f183SYan Zheng 12327237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 12337237f183SYan Zheng if (IS_ERR(log_root)) 12347237f183SYan Zheng return PTR_ERR(log_root); 12357237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 12367237f183SYan Zheng fs_info->log_root_tree = log_root; 12377237f183SYan Zheng return 0; 12387237f183SYan Zheng } 12397237f183SYan Zheng 12407237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 12417237f183SYan Zheng struct btrfs_root *root) 12427237f183SYan Zheng { 12430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12447237f183SYan Zheng struct btrfs_root *log_root; 12457237f183SYan Zheng struct btrfs_inode_item *inode_item; 12467237f183SYan Zheng 12470b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 12487237f183SYan Zheng if (IS_ERR(log_root)) 12497237f183SYan Zheng return PTR_ERR(log_root); 12507237f183SYan Zheng 12517237f183SYan Zheng log_root->last_trans = trans->transid; 12527237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 12537237f183SYan Zheng 12547237f183SYan Zheng inode_item = &log_root->root_item.inode; 12553cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 12563cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 12573cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1258da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 12590b246afaSJeff Mahoney fs_info->nodesize); 12603cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 12617237f183SYan Zheng 12625d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 12637237f183SYan Zheng 12647237f183SYan Zheng WARN_ON(root->log_root); 12657237f183SYan Zheng root->log_root = log_root; 12667237f183SYan Zheng root->log_transid = 0; 1267d1433debSMiao Xie root->log_transid_committed = -1; 1268257c62e1SChris Mason root->last_log_commit = 0; 1269e02119d5SChris Mason return 0; 1270e02119d5SChris Mason } 1271e02119d5SChris Mason 127249d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 127349d11beaSJosef Bacik struct btrfs_path *path, 1274cb517eabSMiao Xie struct btrfs_key *key) 1275e02119d5SChris Mason { 1276e02119d5SChris Mason struct btrfs_root *root; 1277e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 127884234f3aSYan Zheng u64 generation; 1279cb517eabSMiao Xie int ret; 1280581c1760SQu Wenruo int level; 1281cb517eabSMiao Xie 128296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 128349d11beaSJosef Bacik if (!root) 128449d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 12850f7d52f4SChris Mason 1286cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1287cb517eabSMiao Xie &root->root_item, &root->root_key); 12880f7d52f4SChris Mason if (ret) { 128913a8a7c8SYan, Zheng if (ret > 0) 129013a8a7c8SYan, Zheng ret = -ENOENT; 129149d11beaSJosef Bacik goto fail; 12920f7d52f4SChris Mason } 129313a8a7c8SYan, Zheng 129484234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1295581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 12962ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 12972ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 12981b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 129964c043deSLiu Bo if (IS_ERR(root->node)) { 130064c043deSLiu Bo ret = PTR_ERR(root->node); 13018c38938cSJosef Bacik root->node = NULL; 130249d11beaSJosef Bacik goto fail; 1303cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1304cb517eabSMiao Xie ret = -EIO; 130549d11beaSJosef Bacik goto fail; 1306416bc658SJosef Bacik } 13075d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1308cb517eabSMiao Xie return root; 130949d11beaSJosef Bacik fail: 131000246528SJosef Bacik btrfs_put_root(root); 131149d11beaSJosef Bacik return ERR_PTR(ret); 131249d11beaSJosef Bacik } 131349d11beaSJosef Bacik 131449d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 131549d11beaSJosef Bacik struct btrfs_key *key) 131649d11beaSJosef Bacik { 131749d11beaSJosef Bacik struct btrfs_root *root; 131849d11beaSJosef Bacik struct btrfs_path *path; 131949d11beaSJosef Bacik 132049d11beaSJosef Bacik path = btrfs_alloc_path(); 132149d11beaSJosef Bacik if (!path) 132249d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 132349d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 132449d11beaSJosef Bacik btrfs_free_path(path); 132549d11beaSJosef Bacik 132649d11beaSJosef Bacik return root; 1327cb517eabSMiao Xie } 1328cb517eabSMiao Xie 13292dfb1e43SQu Wenruo /* 13302dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 13312dfb1e43SQu Wenruo * 13322dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 13332dfb1e43SQu Wenruo */ 13342dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1335cb517eabSMiao Xie { 1336cb517eabSMiao Xie int ret; 1337dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1338cb517eabSMiao Xie 1339cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1340cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1341cb517eabSMiao Xie GFP_NOFS); 1342cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1343cb517eabSMiao Xie ret = -ENOMEM; 1344cb517eabSMiao Xie goto fail; 1345cb517eabSMiao Xie } 1346cb517eabSMiao Xie 1347dcc3eb96SNikolay Borisov /* 1348dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1349dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1350dcc3eb96SNikolay Borisov */ 1351dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1352dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1353dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1354dcc3eb96SNikolay Borisov if (ret) 13558257b2dcSMiao Xie goto fail; 13568257b2dcSMiao Xie 1357aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 1358aeb935a4SQu Wenruo root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { 135992a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1360f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1361f39e4571SJosef Bacik } 1362f39e4571SJosef Bacik 1363cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 136457cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 136557cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1366cb517eabSMiao Xie 1367851fd730SQu Wenruo /* 1368851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1369851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1370851fd730SQu Wenruo */ 1371851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1372851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 13732dfb1e43SQu Wenruo if (!anon_dev) { 1374cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1375cb517eabSMiao Xie if (ret) 1376876d2cf1SLiu Bo goto fail; 13772dfb1e43SQu Wenruo } else { 13782dfb1e43SQu Wenruo root->anon_dev = anon_dev; 13792dfb1e43SQu Wenruo } 1380851fd730SQu Wenruo } 1381f32e48e9SChandan Rajendra 1382f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1383f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1384f32e48e9SChandan Rajendra &root->highest_objectid); 1385f32e48e9SChandan Rajendra if (ret) { 1386f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1387876d2cf1SLiu Bo goto fail; 1388f32e48e9SChandan Rajendra } 1389f32e48e9SChandan Rajendra 1390f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1391f32e48e9SChandan Rajendra 1392f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1393f32e48e9SChandan Rajendra 1394cb517eabSMiao Xie return 0; 1395cb517eabSMiao Xie fail: 139684db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1397cb517eabSMiao Xie return ret; 1398cb517eabSMiao Xie } 1399cb517eabSMiao Xie 1400a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1401cb517eabSMiao Xie u64 root_id) 1402cb517eabSMiao Xie { 1403cb517eabSMiao Xie struct btrfs_root *root; 1404cb517eabSMiao Xie 1405cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1406cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1407cb517eabSMiao Xie (unsigned long)root_id); 1408bc44d7c4SJosef Bacik if (root) 140900246528SJosef Bacik root = btrfs_grab_root(root); 1410cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1411cb517eabSMiao Xie return root; 1412cb517eabSMiao Xie } 1413cb517eabSMiao Xie 141449d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 141549d11beaSJosef Bacik u64 objectid) 141649d11beaSJosef Bacik { 141749d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 141849d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 141949d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 142049d11beaSJosef Bacik return btrfs_grab_root(fs_info->extent_root); 142149d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 142249d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 142349d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 142449d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 142549d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 142649d11beaSJosef Bacik return btrfs_grab_root(fs_info->csum_root); 142749d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 142849d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 142949d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 143049d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 143149d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 143249d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 143349d11beaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 143449d11beaSJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 143549d11beaSJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 143649d11beaSJosef Bacik return NULL; 143749d11beaSJosef Bacik } 143849d11beaSJosef Bacik 1439cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1440cb517eabSMiao Xie struct btrfs_root *root) 1441cb517eabSMiao Xie { 1442cb517eabSMiao Xie int ret; 1443cb517eabSMiao Xie 1444e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1445cb517eabSMiao Xie if (ret) 1446cb517eabSMiao Xie return ret; 1447cb517eabSMiao Xie 1448cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1449cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1450cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1451cb517eabSMiao Xie root); 1452af01d2e5SJosef Bacik if (ret == 0) { 145300246528SJosef Bacik btrfs_grab_root(root); 145427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1455af01d2e5SJosef Bacik } 1456cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1457cb517eabSMiao Xie radix_tree_preload_end(); 1458cb517eabSMiao Xie 1459cb517eabSMiao Xie return ret; 1460cb517eabSMiao Xie } 1461cb517eabSMiao Xie 1462bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1463bd647ce3SJosef Bacik { 1464bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1465bd647ce3SJosef Bacik struct btrfs_root *root; 1466bd647ce3SJosef Bacik 1467bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1468457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1469457f1864SJosef Bacik 1470bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1471bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1472457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 1473457f1864SJosef Bacik btrfs_root_name(root->root_key.objectid, buf), 1474bd647ce3SJosef Bacik refcount_read(&root->refs)); 1475bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 147600246528SJosef Bacik btrfs_put_root(root); 147700246528SJosef Bacik btrfs_put_root(root); 1478bd647ce3SJosef Bacik } 1479bd647ce3SJosef Bacik #endif 1480bd647ce3SJosef Bacik } 1481bd647ce3SJosef Bacik 14820d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 14830d4b0463SJosef Bacik { 1484141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1485141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1486141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1487141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1488141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1489141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1490141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 14910d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 14920d4b0463SJosef Bacik kfree(fs_info->delayed_root); 149300246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 149400246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 149500246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 149600246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 149700246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 149800246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 149900246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 150000246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 150100246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1502aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 1503bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15043fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 15050d4b0463SJosef Bacik kfree(fs_info->super_copy); 15060d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15070d4b0463SJosef Bacik kvfree(fs_info); 15080d4b0463SJosef Bacik } 15090d4b0463SJosef Bacik 15100d4b0463SJosef Bacik 15112dfb1e43SQu Wenruo /* 15122dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 15132dfb1e43SQu Wenruo * 15142dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 15152dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 15162dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 15172dfb1e43SQu Wenruo * 15182dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 15192dfb1e43SQu Wenruo * 15202dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 15212dfb1e43SQu Wenruo * same root objectid. 15222dfb1e43SQu Wenruo * 15232dfb1e43SQu Wenruo * @objectid: root id 15242dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 15252dfb1e43SQu Wenruo * pass 0 for new allocation. 15262dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 15272dfb1e43SQu Wenruo * for orphan roots 15282dfb1e43SQu Wenruo */ 15292dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 15302dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 15312dfb1e43SQu Wenruo bool check_ref) 15325eda7b5eSChris Mason { 15335eda7b5eSChris Mason struct btrfs_root *root; 1534381cf658SDavid Sterba struct btrfs_path *path; 15351d4c08e0SDavid Sterba struct btrfs_key key; 15365eda7b5eSChris Mason int ret; 15375eda7b5eSChris Mason 153849d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 153949d11beaSJosef Bacik if (root) 154049d11beaSJosef Bacik return root; 15414df27c4dSYan, Zheng again: 154256e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 154348475471SStefan Behrens if (root) { 15442dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 15452dfb1e43SQu Wenruo ASSERT(!anon_dev); 1546bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 154700246528SJosef Bacik btrfs_put_root(root); 154848475471SStefan Behrens return ERR_PTR(-ENOENT); 1549bc44d7c4SJosef Bacik } 15505eda7b5eSChris Mason return root; 155148475471SStefan Behrens } 15525eda7b5eSChris Mason 155356e9357aSDavid Sterba key.objectid = objectid; 155456e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 155556e9357aSDavid Sterba key.offset = (u64)-1; 155656e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 15575eda7b5eSChris Mason if (IS_ERR(root)) 15585eda7b5eSChris Mason return root; 15593394e160SChris Mason 1560c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1561d68fc57bSYan, Zheng ret = -ENOENT; 1562d68fc57bSYan, Zheng goto fail; 1563d68fc57bSYan, Zheng } 1564d68fc57bSYan, Zheng 15652dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1566cb517eabSMiao Xie if (ret) 1567cb517eabSMiao Xie goto fail; 1568cb517eabSMiao Xie 1569381cf658SDavid Sterba path = btrfs_alloc_path(); 1570381cf658SDavid Sterba if (!path) { 1571381cf658SDavid Sterba ret = -ENOMEM; 1572381cf658SDavid Sterba goto fail; 1573381cf658SDavid Sterba } 15741d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 15751d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 157656e9357aSDavid Sterba key.offset = objectid; 15771d4c08e0SDavid Sterba 15781d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1579381cf658SDavid Sterba btrfs_free_path(path); 1580d68fc57bSYan, Zheng if (ret < 0) 1581d68fc57bSYan, Zheng goto fail; 1582d68fc57bSYan, Zheng if (ret == 0) 158327cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1584d68fc57bSYan, Zheng 1585cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 15860f7d52f4SChris Mason if (ret) { 158700246528SJosef Bacik btrfs_put_root(root); 15884785e24fSJosef Bacik if (ret == -EEXIST) 15894df27c4dSYan, Zheng goto again; 15904df27c4dSYan, Zheng goto fail; 15914df27c4dSYan, Zheng } 1592edbd8d4eSChris Mason return root; 15934df27c4dSYan, Zheng fail: 15948c38938cSJosef Bacik btrfs_put_root(root); 15954df27c4dSYan, Zheng return ERR_PTR(ret); 1596edbd8d4eSChris Mason } 1597edbd8d4eSChris Mason 15982dfb1e43SQu Wenruo /* 15992dfb1e43SQu Wenruo * Get in-memory reference of a root structure 16002dfb1e43SQu Wenruo * 16012dfb1e43SQu Wenruo * @objectid: tree objectid 16022dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 16032dfb1e43SQu Wenruo * one reference 16042dfb1e43SQu Wenruo */ 16052dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 16062dfb1e43SQu Wenruo u64 objectid, bool check_ref) 16072dfb1e43SQu Wenruo { 16082dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 16092dfb1e43SQu Wenruo } 16102dfb1e43SQu Wenruo 16112dfb1e43SQu Wenruo /* 16122dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 16132dfb1e43SQu Wenruo * the anonymous block device id 16142dfb1e43SQu Wenruo * 16152dfb1e43SQu Wenruo * @objectid: tree objectid 16162dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 16172dfb1e43SQu Wenruo * parameter value 16182dfb1e43SQu Wenruo */ 16192dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 16202dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 16212dfb1e43SQu Wenruo { 16222dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 16232dfb1e43SQu Wenruo } 16242dfb1e43SQu Wenruo 16258b712842SChris Mason /* 162649d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 162749d11beaSJosef Bacik * @fs_info: the fs_info 162849d11beaSJosef Bacik * @objectid: the objectid we need to lookup 162949d11beaSJosef Bacik * 163049d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 163149d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 163249d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 163349d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 163449d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 163549d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 163649d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 163749d11beaSJosef Bacik * backref code is finished using the root. 163849d11beaSJosef Bacik */ 163949d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 164049d11beaSJosef Bacik struct btrfs_path *path, 164149d11beaSJosef Bacik u64 objectid) 164249d11beaSJosef Bacik { 164349d11beaSJosef Bacik struct btrfs_root *root; 164449d11beaSJosef Bacik struct btrfs_key key; 164549d11beaSJosef Bacik 164649d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 164749d11beaSJosef Bacik 164849d11beaSJosef Bacik /* 164949d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 165049d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 165149d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 165249d11beaSJosef Bacik * != NULL just return it. 165349d11beaSJosef Bacik */ 165449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 165549d11beaSJosef Bacik if (root) 165649d11beaSJosef Bacik return root; 165749d11beaSJosef Bacik 165849d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 165949d11beaSJosef Bacik if (root) 166049d11beaSJosef Bacik return root; 166149d11beaSJosef Bacik 166249d11beaSJosef Bacik key.objectid = objectid; 166349d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 166449d11beaSJosef Bacik key.offset = (u64)-1; 166549d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 166649d11beaSJosef Bacik btrfs_release_path(path); 166749d11beaSJosef Bacik 166849d11beaSJosef Bacik return root; 166949d11beaSJosef Bacik } 167049d11beaSJosef Bacik 167149d11beaSJosef Bacik /* 16728b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16738b712842SChris Mason * functions. This is where read checksum verification actually happens 16748b712842SChris Mason */ 16758b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1676ce9adaa5SChris Mason { 1677ce9adaa5SChris Mason struct bio *bio; 167897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1679ce9adaa5SChris Mason 168097eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1681ce9adaa5SChris Mason bio = end_io_wq->bio; 16828b712842SChris Mason 16834e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1684ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1685ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 16864246a0b6SChristoph Hellwig bio_endio(bio); 16879be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1688ce9adaa5SChris Mason } 168944b8bd7eSChris Mason 1690a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1691a74a4b97SChris Mason { 1692a74a4b97SChris Mason struct btrfs_root *root = arg; 16930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1694d0278245SMiao Xie int again; 1695a74a4b97SChris Mason 1696d6fd0ae2SOmar Sandoval while (1) { 1697d0278245SMiao Xie again = 0; 16989d1a2a3aSDavid Sterba 1699fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1700fd340d0fSJosef Bacik 1701d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17022ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1703d0278245SMiao Xie goto sleep; 1704d0278245SMiao Xie 170590c711abSZygo Blaxell /* 170690c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 170790c711abSZygo Blaxell * before we have finished mounting the filesystem. 170890c711abSZygo Blaxell */ 17090b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 171090c711abSZygo Blaxell goto sleep; 171190c711abSZygo Blaxell 17120b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1713d0278245SMiao Xie goto sleep; 1714d0278245SMiao Xie 1715dc7f370cSMiao Xie /* 1716dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1717dc7f370cSMiao Xie * during the above check and trylock. 1718dc7f370cSMiao Xie */ 17192ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17200b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1721dc7f370cSMiao Xie goto sleep; 1722dc7f370cSMiao Xie } 1723dc7f370cSMiao Xie 17242ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1725c2d6cb16SFilipe Manana 17269d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17270b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1728a74a4b97SChris Mason 1729d0278245SMiao Xie /* 173005323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 173105323cd1SMiao Xie * needn't do anything special here. 1732d0278245SMiao Xie */ 17330b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 173467c5e7d4SFilipe Manana 173567c5e7d4SFilipe Manana /* 173667c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 173767c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 173867c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 173967c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 174067c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 174167c5e7d4SFilipe Manana * unused block groups. 174267c5e7d4SFilipe Manana */ 17430b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1744d0278245SMiao Xie sleep: 1745fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1746d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1747d6fd0ae2SOmar Sandoval kthread_parkme(); 1748d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1749d6fd0ae2SOmar Sandoval return 0; 1750838fe188SJiri Kosina if (!again) { 1751a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1752a74a4b97SChris Mason schedule(); 1753a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1754a74a4b97SChris Mason } 1755da288d28SFilipe Manana } 1756a74a4b97SChris Mason } 1757a74a4b97SChris Mason 1758a74a4b97SChris Mason static int transaction_kthread(void *arg) 1759a74a4b97SChris Mason { 1760a74a4b97SChris Mason struct btrfs_root *root = arg; 17610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1762a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1763a74a4b97SChris Mason struct btrfs_transaction *cur; 17648929ecfaSYan, Zheng u64 transid; 1765643900beSNikolay Borisov time64_t delta; 1766a74a4b97SChris Mason unsigned long delay; 1767914b2007SJan Kara bool cannot_commit; 1768a74a4b97SChris Mason 1769a74a4b97SChris Mason do { 1770914b2007SJan Kara cannot_commit = false; 1771ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 17720b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1773a74a4b97SChris Mason 17740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17750b246afaSJeff Mahoney cur = fs_info->running_transaction; 1776a74a4b97SChris Mason if (!cur) { 17770b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1778a74a4b97SChris Mason goto sleep; 1779a74a4b97SChris Mason } 178031153d81SYan Zheng 1781643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 17823296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1783643900beSNikolay Borisov delta < fs_info->commit_interval) { 17840b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1785fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1786fb8a7e94SNikolay Borisov delay = min(delay, 1787fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1788a74a4b97SChris Mason goto sleep; 1789a74a4b97SChris Mason } 17908929ecfaSYan, Zheng transid = cur->transid; 17910b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 179256bec294SChris Mason 179379787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1794354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1795914b2007SJan Kara if (IS_ERR(trans)) { 1796354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1797914b2007SJan Kara cannot_commit = true; 179879787eaaSJeff Mahoney goto sleep; 1799914b2007SJan Kara } 18008929ecfaSYan, Zheng if (transid == trans->transid) { 18013a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18028929ecfaSYan, Zheng } else { 18033a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18048929ecfaSYan, Zheng } 1805a74a4b97SChris Mason sleep: 18060b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18070b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1808a74a4b97SChris Mason 18094e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18100b246afaSJeff Mahoney &fs_info->fs_state))) 18112ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18128929ecfaSYan, Zheng if (!kthread_should_stop() && 18130b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1814914b2007SJan Kara cannot_commit)) 1815bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1816a74a4b97SChris Mason } while (!kthread_should_stop()); 1817a74a4b97SChris Mason return 0; 1818a74a4b97SChris Mason } 1819a74a4b97SChris Mason 1820af31f5e5SChris Mason /* 182101f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 182201f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 182301f0f9daSNikolay Borisov * anything. 1824af31f5e5SChris Mason * 1825af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1826af31f5e5SChris Mason * generation of the latest root in the array with the generation 1827af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1828af31f5e5SChris Mason */ 182901f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1830af31f5e5SChris Mason { 183101f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1832af31f5e5SChris Mason u64 cur; 1833af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1834af31f5e5SChris Mason int i; 1835af31f5e5SChris Mason 1836af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1837af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1838af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1839af31f5e5SChris Mason if (cur == newest_gen) 184001f0f9daSNikolay Borisov return i; 1841af31f5e5SChris Mason } 1842af31f5e5SChris Mason 184301f0f9daSNikolay Borisov return -EINVAL; 1844af31f5e5SChris Mason } 1845af31f5e5SChris Mason 1846af31f5e5SChris Mason /* 1847af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1848af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1849af31f5e5SChris Mason * done 1850af31f5e5SChris Mason */ 1851af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1852af31f5e5SChris Mason { 18536ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1854af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1855af31f5e5SChris Mason 1856af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1857af31f5e5SChris Mason 1858af31f5e5SChris Mason /* 1859af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1860af31f5e5SChris Mason * regardless of which ones we use today 1861af31f5e5SChris Mason */ 1862af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1863af31f5e5SChris Mason 1864af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1865af31f5e5SChris Mason 1866af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1867af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1868af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1869af31f5e5SChris Mason 1870af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1871af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1872af31f5e5SChris Mason 1873af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1874af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1875af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1876af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1877af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1878af31f5e5SChris Mason 1879af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1880af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1881af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1882af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1883af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1884af31f5e5SChris Mason 18857c7e82a7SChris Mason /* 18867c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18877c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18887c7e82a7SChris Mason */ 18897c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18907c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18917c7e82a7SChris Mason info->fs_root->node->start); 1892af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1893af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1894af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1895af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18967c7e82a7SChris Mason } 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1899af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1900af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1901af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1902af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1903af31f5e5SChris Mason 1904af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1905af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1906af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1907af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1908af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1909af31f5e5SChris Mason 1910af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1911af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1912af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1913af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1914af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1915af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1916af31f5e5SChris Mason 1917af31f5e5SChris Mason /* 1918af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1919af31f5e5SChris Mason * for the next commit 1920af31f5e5SChris Mason */ 1921af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1922af31f5e5SChris Mason &info->super_for_commit->super_roots, 1923af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1924af31f5e5SChris Mason } 1925af31f5e5SChris Mason 1926af31f5e5SChris Mason /* 1927bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1928bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1929bd2336b2SNikolay Borisov * 1930bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1931bd2336b2SNikolay Borisov * priority - priority of backup root required 1932bd2336b2SNikolay Borisov * 1933bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1934bd2336b2SNikolay Borisov */ 1935bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1936bd2336b2SNikolay Borisov { 1937bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1938bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1939bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1940bd2336b2SNikolay Borisov 1941bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1942bd2336b2SNikolay Borisov if (priority == 0) 1943bd2336b2SNikolay Borisov return backup_index; 1944bd2336b2SNikolay Borisov 1945bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1946bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1947bd2336b2SNikolay Borisov } else { 1948bd2336b2SNikolay Borisov return -EINVAL; 1949bd2336b2SNikolay Borisov } 1950bd2336b2SNikolay Borisov 1951bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1952bd2336b2SNikolay Borisov 1953bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1954bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1955bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1956bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1957bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1958bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1959bd2336b2SNikolay Borisov 1960bd2336b2SNikolay Borisov /* 1961bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1962bd2336b2SNikolay Borisov * need a fsck 1963bd2336b2SNikolay Borisov */ 1964bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1965bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1966bd2336b2SNikolay Borisov 1967bd2336b2SNikolay Borisov return backup_index; 1968bd2336b2SNikolay Borisov } 1969bd2336b2SNikolay Borisov 19707abadb64SLiu Bo /* helper to cleanup workers */ 19717abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19727abadb64SLiu Bo { 1973dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1974afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 19755cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 1976fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 1977fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 1978d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 1979fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 1980fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 19815b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 1982e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 1983736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 1984a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 1985fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 1986b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 1987b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 1988a9b9477dSFilipe Manana /* 1989a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 1990a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 1991a9b9477dSFilipe Manana * queues can do metadata I/O operations. 1992a9b9477dSFilipe Manana */ 1993a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 1994a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 19957abadb64SLiu Bo } 19967abadb64SLiu Bo 19972e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 19982e9f5954SRashika { 19992e9f5954SRashika if (root) { 20002e9f5954SRashika free_extent_buffer(root->node); 20012e9f5954SRashika free_extent_buffer(root->commit_root); 20022e9f5954SRashika root->node = NULL; 20032e9f5954SRashika root->commit_root = NULL; 20042e9f5954SRashika } 20052e9f5954SRashika } 20062e9f5954SRashika 2007af31f5e5SChris Mason /* helper to cleanup tree roots */ 20084273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2009af31f5e5SChris Mason { 20102e9f5954SRashika free_root_extent_buffers(info->tree_root); 2011655b09feSJosef Bacik 20122e9f5954SRashika free_root_extent_buffers(info->dev_root); 20132e9f5954SRashika free_root_extent_buffers(info->extent_root); 20142e9f5954SRashika free_root_extent_buffers(info->csum_root); 20152e9f5954SRashika free_root_extent_buffers(info->quota_root); 20162e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20178c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2018aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 20194273eaffSAnand Jain if (free_chunk_root) 20202e9f5954SRashika free_root_extent_buffers(info->chunk_root); 202170f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2022af31f5e5SChris Mason } 2023af31f5e5SChris Mason 20248c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 20258c38938cSJosef Bacik { 20268c38938cSJosef Bacik if (!root) 20278c38938cSJosef Bacik return; 20288c38938cSJosef Bacik 20298c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 20308c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 20311dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 20328c38938cSJosef Bacik if (root->anon_dev) 20338c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 20348c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2035923eb523SJohannes Thumshirn free_root_extent_buffers(root); 20368c38938cSJosef Bacik kfree(root->free_ino_ctl); 20378c38938cSJosef Bacik kfree(root->free_ino_pinned); 20388c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 20398c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 20408c38938cSJosef Bacik list_del_init(&root->leak_list); 20418c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 20428c38938cSJosef Bacik #endif 20438c38938cSJosef Bacik kfree(root); 20448c38938cSJosef Bacik } 20458c38938cSJosef Bacik } 20468c38938cSJosef Bacik 2047faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2048171f6537SJosef Bacik { 2049171f6537SJosef Bacik int ret; 2050171f6537SJosef Bacik struct btrfs_root *gang[8]; 2051171f6537SJosef Bacik int i; 2052171f6537SJosef Bacik 2053171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2054171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2055171f6537SJosef Bacik struct btrfs_root, root_list); 2056171f6537SJosef Bacik list_del(&gang[0]->root_list); 2057171f6537SJosef Bacik 20588c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2059cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 206000246528SJosef Bacik btrfs_put_root(gang[0]); 2061171f6537SJosef Bacik } 2062171f6537SJosef Bacik 2063171f6537SJosef Bacik while (1) { 2064171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2065171f6537SJosef Bacik (void **)gang, 0, 2066171f6537SJosef Bacik ARRAY_SIZE(gang)); 2067171f6537SJosef Bacik if (!ret) 2068171f6537SJosef Bacik break; 2069171f6537SJosef Bacik for (i = 0; i < ret; i++) 2070cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2071171f6537SJosef Bacik } 2072171f6537SJosef Bacik } 2073af31f5e5SChris Mason 2074638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2075638aa7edSEric Sandeen { 2076638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2077638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2078638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2079638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2080638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2081638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2082ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2083638aa7edSEric Sandeen } 2084638aa7edSEric Sandeen 2085779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2086779a65a4SEric Sandeen { 2087779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2088779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2089779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2090779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2091779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2092779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2093779a65a4SEric Sandeen } 2094779a65a4SEric Sandeen 20956bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2096f37938e0SEric Sandeen { 20972ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 20982ff7e61eSJeff Mahoney 20992ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21002ff7e61eSJeff Mahoney set_nlink(inode, 1); 2101f37938e0SEric Sandeen /* 2102f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2103f37938e0SEric Sandeen * the real end of the address space is determined by all of 2104f37938e0SEric Sandeen * the devices in the system 2105f37938e0SEric Sandeen */ 21062ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21072ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2108f37938e0SEric Sandeen 21092ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 211043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 21112c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 21127b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21132ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2114f37938e0SEric Sandeen 21155c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 21162ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21172ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21182ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2119f37938e0SEric Sandeen } 2120f37938e0SEric Sandeen 2121ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2122ad618368SEric Sandeen { 2123ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2124129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21257f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2126ad618368SEric Sandeen } 2127ad618368SEric Sandeen 2128f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2129f9e92e40SEric Sandeen { 2130f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2131f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2132f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2133f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2134f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2135f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2136d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2137f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2138f9e92e40SEric Sandeen } 2139f9e92e40SEric Sandeen 21402a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21412a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21422a458198SEric Sandeen { 2143f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21446f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21452a458198SEric Sandeen 21462a458198SEric Sandeen fs_info->workers = 2147cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2148cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21492a458198SEric Sandeen 21502a458198SEric Sandeen fs_info->delalloc_workers = 2151cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2152cb001095SJeff Mahoney flags, max_active, 2); 21532a458198SEric Sandeen 21542a458198SEric Sandeen fs_info->flush_workers = 2155cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2156cb001095SJeff Mahoney flags, max_active, 0); 21572a458198SEric Sandeen 21582a458198SEric Sandeen fs_info->caching_workers = 2159cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21602a458198SEric Sandeen 21612a458198SEric Sandeen fs_info->fixup_workers = 2162cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21632a458198SEric Sandeen 21642a458198SEric Sandeen /* 21652a458198SEric Sandeen * endios are largely parallel and should have a very 21662a458198SEric Sandeen * low idle thresh 21672a458198SEric Sandeen */ 21682a458198SEric Sandeen fs_info->endio_workers = 2169cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21702a458198SEric Sandeen fs_info->endio_meta_workers = 2171cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2172cb001095SJeff Mahoney max_active, 4); 21732a458198SEric Sandeen fs_info->endio_meta_write_workers = 2174cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2175cb001095SJeff Mahoney max_active, 2); 21762a458198SEric Sandeen fs_info->endio_raid56_workers = 2177cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2178cb001095SJeff Mahoney max_active, 4); 21792a458198SEric Sandeen fs_info->rmw_workers = 2180cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21812a458198SEric Sandeen fs_info->endio_write_workers = 2182cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2183cb001095SJeff Mahoney max_active, 2); 21842a458198SEric Sandeen fs_info->endio_freespace_worker = 2185cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2186cb001095SJeff Mahoney max_active, 0); 21872a458198SEric Sandeen fs_info->delayed_workers = 2188cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2189cb001095SJeff Mahoney max_active, 0); 21902a458198SEric Sandeen fs_info->readahead_workers = 2191cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2192cb001095SJeff Mahoney max_active, 2); 21932a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2194cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2195b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2196b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 21972a458198SEric Sandeen 21982a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2199ba8a9d07SChris Mason fs_info->flush_workers && 22002a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22012a458198SEric Sandeen fs_info->endio_meta_write_workers && 22022a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22032a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22042a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22052a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2206b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2207b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22082a458198SEric Sandeen return -ENOMEM; 22092a458198SEric Sandeen } 22102a458198SEric Sandeen 22112a458198SEric Sandeen return 0; 22122a458198SEric Sandeen } 22132a458198SEric Sandeen 22146d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22156d97c6e3SJohannes Thumshirn { 22166d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2217b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22186d97c6e3SJohannes Thumshirn 2219b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22206d97c6e3SJohannes Thumshirn 22216d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22226d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2223b4e967beSDavid Sterba csum_driver); 22246d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22256d97c6e3SJohannes Thumshirn } 22266d97c6e3SJohannes Thumshirn 22276d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22286d97c6e3SJohannes Thumshirn 22296d97c6e3SJohannes Thumshirn return 0; 22306d97c6e3SJohannes Thumshirn } 22316d97c6e3SJohannes Thumshirn 223263443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 223363443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 223463443bf5SEric Sandeen { 223563443bf5SEric Sandeen int ret; 223663443bf5SEric Sandeen struct btrfs_root *log_tree_root; 223763443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 223863443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2239581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 224063443bf5SEric Sandeen 224163443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2242f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 224363443bf5SEric Sandeen return -EIO; 224463443bf5SEric Sandeen } 224563443bf5SEric Sandeen 224696dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 224796dfcb46SJosef Bacik GFP_KERNEL); 224863443bf5SEric Sandeen if (!log_tree_root) 224963443bf5SEric Sandeen return -ENOMEM; 225063443bf5SEric Sandeen 22512ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 22521b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 22531b7ec85eSJosef Bacik fs_info->generation + 1, level, 22541b7ec85eSJosef Bacik NULL); 225564c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2256f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22570eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 22588c38938cSJosef Bacik log_tree_root->node = NULL; 225900246528SJosef Bacik btrfs_put_root(log_tree_root); 22600eeff236SLiu Bo return ret; 226164c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2262f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 226300246528SJosef Bacik btrfs_put_root(log_tree_root); 226463443bf5SEric Sandeen return -EIO; 226563443bf5SEric Sandeen } 226663443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 226763443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 226863443bf5SEric Sandeen if (ret) { 22690b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 227063443bf5SEric Sandeen "Failed to recover log tree"); 227100246528SJosef Bacik btrfs_put_root(log_tree_root); 227263443bf5SEric Sandeen return ret; 227363443bf5SEric Sandeen } 227463443bf5SEric Sandeen 2275bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22766bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 227763443bf5SEric Sandeen if (ret) 227863443bf5SEric Sandeen return ret; 227963443bf5SEric Sandeen } 228063443bf5SEric Sandeen 228163443bf5SEric Sandeen return 0; 228263443bf5SEric Sandeen } 228363443bf5SEric Sandeen 22846bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 22854bbcaa64SEric Sandeen { 22866bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2287a4f3d2c4SDavid Sterba struct btrfs_root *root; 22884bbcaa64SEric Sandeen struct btrfs_key location; 22894bbcaa64SEric Sandeen int ret; 22904bbcaa64SEric Sandeen 22916bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 22926bccf3abSJeff Mahoney 22934bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 22944bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 22954bbcaa64SEric Sandeen location.offset = 0; 22964bbcaa64SEric Sandeen 2297a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2298f50f4353SLiu Bo if (IS_ERR(root)) { 229942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2300f50f4353SLiu Bo ret = PTR_ERR(root); 2301f50f4353SLiu Bo goto out; 2302f50f4353SLiu Bo } 230342437a63SJosef Bacik } else { 2304a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2305a4f3d2c4SDavid Sterba fs_info->extent_root = root; 230642437a63SJosef Bacik } 23074bbcaa64SEric Sandeen 23084bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2309a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2310f50f4353SLiu Bo if (IS_ERR(root)) { 231142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2312f50f4353SLiu Bo ret = PTR_ERR(root); 2313f50f4353SLiu Bo goto out; 2314f50f4353SLiu Bo } 231542437a63SJosef Bacik } else { 2316a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2317a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23184bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 231942437a63SJosef Bacik } 23204bbcaa64SEric Sandeen 2321882dbe0cSJosef Bacik /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ 2322882dbe0cSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 23234bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2324a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2325f50f4353SLiu Bo if (IS_ERR(root)) { 232642437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2327f50f4353SLiu Bo ret = PTR_ERR(root); 2328f50f4353SLiu Bo goto out; 2329f50f4353SLiu Bo } 233042437a63SJosef Bacik } else { 2331a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2332a4f3d2c4SDavid Sterba fs_info->csum_root = root; 233342437a63SJosef Bacik } 2334882dbe0cSJosef Bacik } 23354bbcaa64SEric Sandeen 2336aeb935a4SQu Wenruo /* 2337aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2338aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2339aeb935a4SQu Wenruo */ 234056e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 234156e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2342aeb935a4SQu Wenruo if (IS_ERR(root)) { 234342437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2344aeb935a4SQu Wenruo ret = PTR_ERR(root); 2345aeb935a4SQu Wenruo goto out; 2346aeb935a4SQu Wenruo } 234742437a63SJosef Bacik } else { 2348aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2349aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 235042437a63SJosef Bacik } 2351aeb935a4SQu Wenruo 23524bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2353a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2354a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2355a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2356afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2357a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23584bbcaa64SEric Sandeen } 23594bbcaa64SEric Sandeen 23604bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2361a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2362a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 236342437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2364a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23654bbcaa64SEric Sandeen if (ret != -ENOENT) 2366f50f4353SLiu Bo goto out; 236742437a63SJosef Bacik } 23684bbcaa64SEric Sandeen } else { 2369a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2370a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23714bbcaa64SEric Sandeen } 23724bbcaa64SEric Sandeen 237370f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 237470f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 237570f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2376f50f4353SLiu Bo if (IS_ERR(root)) { 237742437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2378f50f4353SLiu Bo ret = PTR_ERR(root); 2379f50f4353SLiu Bo goto out; 2380f50f4353SLiu Bo } 238142437a63SJosef Bacik } else { 238270f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 238370f6d82eSOmar Sandoval fs_info->free_space_root = root; 238470f6d82eSOmar Sandoval } 238542437a63SJosef Bacik } 238670f6d82eSOmar Sandoval 23874bbcaa64SEric Sandeen return 0; 2388f50f4353SLiu Bo out: 2389f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2390f50f4353SLiu Bo location.objectid, ret); 2391f50f4353SLiu Bo return ret; 23924bbcaa64SEric Sandeen } 23934bbcaa64SEric Sandeen 2394069ec957SQu Wenruo /* 2395069ec957SQu Wenruo * Real super block validation 2396069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2397069ec957SQu Wenruo * 2398069ec957SQu Wenruo * @sb: super block to check 2399069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2400069ec957SQu Wenruo * 0 the primary (1st) sb 2401069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2402069ec957SQu Wenruo * -1 skip bytenr check 2403069ec957SQu Wenruo */ 2404069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2405069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 240621a852b0SQu Wenruo { 240721a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 240821a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 240921a852b0SQu Wenruo int ret = 0; 241021a852b0SQu Wenruo 241121a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 241221a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 241321a852b0SQu Wenruo ret = -EINVAL; 241421a852b0SQu Wenruo } 241521a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 241621a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 241721a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 241821a852b0SQu Wenruo ret = -EINVAL; 241921a852b0SQu Wenruo } 242021a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 242121a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 242221a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 242321a852b0SQu Wenruo ret = -EINVAL; 242421a852b0SQu Wenruo } 242521a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 242621a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 242721a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 242821a852b0SQu Wenruo ret = -EINVAL; 242921a852b0SQu Wenruo } 243021a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 243121a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 243221a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 243321a852b0SQu Wenruo ret = -EINVAL; 243421a852b0SQu Wenruo } 243521a852b0SQu Wenruo 243621a852b0SQu Wenruo /* 243721a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 243821a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 243921a852b0SQu Wenruo */ 244021a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 244121a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 244221a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 244321a852b0SQu Wenruo ret = -EINVAL; 244421a852b0SQu Wenruo } 244521a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 244621a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 244721a852b0SQu Wenruo btrfs_err(fs_info, 244821a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 244921a852b0SQu Wenruo sectorsize, PAGE_SIZE); 245021a852b0SQu Wenruo ret = -EINVAL; 245121a852b0SQu Wenruo } 245221a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 245321a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 245421a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 245521a852b0SQu Wenruo ret = -EINVAL; 245621a852b0SQu Wenruo } 245721a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 245821a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 245921a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 246021a852b0SQu Wenruo ret = -EINVAL; 246121a852b0SQu Wenruo } 246221a852b0SQu Wenruo 246321a852b0SQu Wenruo /* Root alignment check */ 246421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 246521a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 246621a852b0SQu Wenruo btrfs_super_root(sb)); 246721a852b0SQu Wenruo ret = -EINVAL; 246821a852b0SQu Wenruo } 246921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 247021a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 247121a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 247221a852b0SQu Wenruo ret = -EINVAL; 247321a852b0SQu Wenruo } 247421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 247521a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 247621a852b0SQu Wenruo btrfs_super_log_root(sb)); 247721a852b0SQu Wenruo ret = -EINVAL; 247821a852b0SQu Wenruo } 247921a852b0SQu Wenruo 2480de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24817239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 248221a852b0SQu Wenruo btrfs_err(fs_info, 24837239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2484de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 248521a852b0SQu Wenruo ret = -EINVAL; 248621a852b0SQu Wenruo } 248721a852b0SQu Wenruo 248821a852b0SQu Wenruo /* 248921a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 249021a852b0SQu Wenruo * done later 249121a852b0SQu Wenruo */ 249221a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 249321a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 249421a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 249521a852b0SQu Wenruo ret = -EINVAL; 249621a852b0SQu Wenruo } 249721a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 249821a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 249921a852b0SQu Wenruo btrfs_super_stripesize(sb)); 250021a852b0SQu Wenruo ret = -EINVAL; 250121a852b0SQu Wenruo } 250221a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 250321a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 250421a852b0SQu Wenruo btrfs_super_num_devices(sb)); 250521a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 250621a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 250721a852b0SQu Wenruo ret = -EINVAL; 250821a852b0SQu Wenruo } 250921a852b0SQu Wenruo 2510069ec957SQu Wenruo if (mirror_num >= 0 && 2511069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 251221a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 251321a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 251421a852b0SQu Wenruo ret = -EINVAL; 251521a852b0SQu Wenruo } 251621a852b0SQu Wenruo 251721a852b0SQu Wenruo /* 251821a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 251921a852b0SQu Wenruo * and one chunk 252021a852b0SQu Wenruo */ 252121a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 252221a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 252321a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 252421a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 252521a852b0SQu Wenruo ret = -EINVAL; 252621a852b0SQu Wenruo } 252721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 252821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 252921a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 253021a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 253121a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 253221a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 253321a852b0SQu Wenruo ret = -EINVAL; 253421a852b0SQu Wenruo } 253521a852b0SQu Wenruo 253621a852b0SQu Wenruo /* 253721a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 253821a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 253921a852b0SQu Wenruo */ 254021a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 254121a852b0SQu Wenruo btrfs_warn(fs_info, 254221a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 254321a852b0SQu Wenruo btrfs_super_generation(sb), 254421a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 254521a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 254621a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 254721a852b0SQu Wenruo btrfs_warn(fs_info, 254821a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 254921a852b0SQu Wenruo btrfs_super_generation(sb), 255021a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 255121a852b0SQu Wenruo 255221a852b0SQu Wenruo return ret; 255321a852b0SQu Wenruo } 255421a852b0SQu Wenruo 2555069ec957SQu Wenruo /* 2556069ec957SQu Wenruo * Validation of super block at mount time. 2557069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2558069ec957SQu Wenruo * flags will be skipped. 2559069ec957SQu Wenruo */ 2560069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2561069ec957SQu Wenruo { 2562069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2563069ec957SQu Wenruo } 2564069ec957SQu Wenruo 256575cb857dSQu Wenruo /* 256675cb857dSQu Wenruo * Validation of super block at write time. 256775cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 256875cb857dSQu Wenruo * overwritten soon. 256975cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 257075cb857dSQu Wenruo */ 257175cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 257275cb857dSQu Wenruo struct btrfs_super_block *sb) 257375cb857dSQu Wenruo { 257475cb857dSQu Wenruo int ret; 257575cb857dSQu Wenruo 257675cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 257775cb857dSQu Wenruo if (ret < 0) 257875cb857dSQu Wenruo goto out; 2579e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 258075cb857dSQu Wenruo ret = -EUCLEAN; 258175cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 258275cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 258375cb857dSQu Wenruo goto out; 258475cb857dSQu Wenruo } 258575cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 258675cb857dSQu Wenruo ret = -EUCLEAN; 258775cb857dSQu Wenruo btrfs_err(fs_info, 258875cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 258975cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 259075cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 259175cb857dSQu Wenruo goto out; 259275cb857dSQu Wenruo } 259375cb857dSQu Wenruo out: 259475cb857dSQu Wenruo if (ret < 0) 259575cb857dSQu Wenruo btrfs_err(fs_info, 259675cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 259775cb857dSQu Wenruo return ret; 259875cb857dSQu Wenruo } 259975cb857dSQu Wenruo 26006ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2601b8522a1eSNikolay Borisov { 26026ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2603b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2604b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2605b8522a1eSNikolay Borisov bool handle_error = false; 2606b8522a1eSNikolay Borisov int ret = 0; 2607b8522a1eSNikolay Borisov int i; 2608b8522a1eSNikolay Borisov 2609b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2610b8522a1eSNikolay Borisov u64 generation; 2611b8522a1eSNikolay Borisov int level; 2612b8522a1eSNikolay Borisov 2613b8522a1eSNikolay Borisov if (handle_error) { 2614b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2615b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2616b8522a1eSNikolay Borisov tree_root->node = NULL; 2617b8522a1eSNikolay Borisov 2618b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2619b8522a1eSNikolay Borisov break; 2620b8522a1eSNikolay Borisov 2621b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2622b8522a1eSNikolay Borisov 2623b8522a1eSNikolay Borisov /* 2624b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2625b8522a1eSNikolay Borisov * valid 2626b8522a1eSNikolay Borisov */ 2627b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2628b8522a1eSNikolay Borisov 2629b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2630b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2631b8522a1eSNikolay Borisov 2632b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26336ef108ddSNikolay Borisov backup_index = ret; 2634b8522a1eSNikolay Borisov if (ret < 0) 2635b8522a1eSNikolay Borisov return ret; 2636b8522a1eSNikolay Borisov } 2637b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2638b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2639b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 26401b7ec85eSJosef Bacik BTRFS_ROOT_TREE_OBJECTID, 2641b8522a1eSNikolay Borisov generation, level, NULL); 26420465337cSJosef Bacik if (IS_ERR(tree_root->node)) { 2643217f5004SNikolay Borisov handle_error = true; 2644b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 26450465337cSJosef Bacik tree_root->node = NULL; 2646217f5004SNikolay Borisov btrfs_warn(fs_info, "couldn't read tree root"); 2647217f5004SNikolay Borisov continue; 2648b8522a1eSNikolay Borisov 2649217f5004SNikolay Borisov } else if (!extent_buffer_uptodate(tree_root->node)) { 2650217f5004SNikolay Borisov handle_error = true; 2651217f5004SNikolay Borisov ret = -EIO; 2652217f5004SNikolay Borisov btrfs_warn(fs_info, "error while reading tree root"); 2653b8522a1eSNikolay Borisov continue; 2654b8522a1eSNikolay Borisov } 2655b8522a1eSNikolay Borisov 2656b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2657b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2658b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2659b8522a1eSNikolay Borisov 2660336a0d8dSNikolay Borisov /* 2661336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2662336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2663336a0d8dSNikolay Borisov */ 2664b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2665b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2666b8522a1eSNikolay Borisov if (ret < 0) { 2667b8522a1eSNikolay Borisov handle_error = true; 2668b8522a1eSNikolay Borisov continue; 2669b8522a1eSNikolay Borisov } 2670b8522a1eSNikolay Borisov 2671b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2672b8522a1eSNikolay Borisov 2673b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2674b8522a1eSNikolay Borisov if (ret < 0) { 2675b8522a1eSNikolay Borisov handle_error = true; 2676b8522a1eSNikolay Borisov continue; 2677b8522a1eSNikolay Borisov } 2678b8522a1eSNikolay Borisov 2679b8522a1eSNikolay Borisov /* All successful */ 2680b8522a1eSNikolay Borisov fs_info->generation = generation; 2681b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26826ef108ddSNikolay Borisov 26836ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26846ef108ddSNikolay Borisov if (backup_index < 0) { 26856ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26866ef108ddSNikolay Borisov } else { 26876ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26886ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26896ef108ddSNikolay Borisov } 2690b8522a1eSNikolay Borisov break; 2691b8522a1eSNikolay Borisov } 2692b8522a1eSNikolay Borisov 2693b8522a1eSNikolay Borisov return ret; 2694b8522a1eSNikolay Borisov } 2695b8522a1eSNikolay Borisov 26968260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2697eb60ceacSChris Mason { 269876dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2699f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 27008fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2701facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 270224bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2703eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 270411833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2705eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2706a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 270776dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 270824bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 27094cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2710ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2711f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 271247ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2713f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2714d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 271567c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 27167585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2717573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2718de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 271919c00ddcSChris Mason 27200b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27216324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2722f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 272347ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2724bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2725bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 27263fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 27273fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2728bd647ce3SJosef Bacik #endif 2729c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 273066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 273166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 273266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 273366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 273466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 273566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 273666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2737ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2738ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2739ba2c4d4eSJosef Bacik 2740771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27414cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27422fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2743034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2744fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 274595ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27469ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27474cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2748a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2749f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27508b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2751f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 275290519d66SArne Jansen /* readahead state */ 2753d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 275490519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2755fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2756c8b97818SChris Mason 2757b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2758b34b086cSChris Mason num_online_cpus() + 2, 8); 27590afbaf8cSChris Mason 2760199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2761199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 276269fe2d75SJosef Bacik 2763638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 276421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 276521adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 276621adbd5cSStefan Behrens #endif 2767779a65a4SEric Sandeen btrfs_init_balance(fs_info); 276857056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 2769a2de733cSArne Jansen 27700f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 27716bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2772a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 27730f9dd46cSJosef Bacik 2774fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2775fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2776afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 277739279cc3SChris Mason 27785a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2779e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2780925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2781a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2782a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27831bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27849e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2785c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 278676dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2787803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2788fa9c0d79SChris Mason 2789ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2790f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2791b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2792416ac51dSArne Jansen 2793fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2794fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2795fa9c0d79SChris Mason 2796e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2797f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2798bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27994854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2800034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28013768f368SChris Mason 2802da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2803da17066cSJeff Mahoney fs_info->nodesize = 4096; 2804da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2805ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 2806da17066cSJeff Mahoney fs_info->stripesize = 4096; 2807da17066cSJeff Mahoney 2808eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2809eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2810eede2bf3SOmar Sandoval 28119e967495SFilipe Manana fs_info->send_in_progress = 0; 28128260edbaSJosef Bacik } 28138260edbaSJosef Bacik 28148260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 28158260edbaSJosef Bacik { 28168260edbaSJosef Bacik int ret; 28178260edbaSJosef Bacik 28188260edbaSJosef Bacik fs_info->sb = sb; 28198260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28208260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 28219e967495SFilipe Manana 2822c75e8394SJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2823ae18c37aSJosef Bacik if (ret) 2824ae18c37aSJosef Bacik return ret; 2825ae18c37aSJosef Bacik 2826ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2827ae18c37aSJosef Bacik if (ret) 2828c75e8394SJosef Bacik return ret; 2829ae18c37aSJosef Bacik 2830ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2831ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2832ae18c37aSJosef Bacik 2833ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2834ae18c37aSJosef Bacik if (ret) 2835c75e8394SJosef Bacik return ret; 2836ae18c37aSJosef Bacik 2837ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2838ae18c37aSJosef Bacik GFP_KERNEL); 2839ae18c37aSJosef Bacik if (ret) 2840c75e8394SJosef Bacik return ret; 2841ae18c37aSJosef Bacik 2842ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2843ae18c37aSJosef Bacik GFP_KERNEL); 2844c75e8394SJosef Bacik if (!fs_info->delayed_root) 2845c75e8394SJosef Bacik return -ENOMEM; 2846ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2847ae18c37aSJosef Bacik 2848c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 2849ae18c37aSJosef Bacik } 2850ae18c37aSJosef Bacik 285197f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 285297f4dd09SNikolay Borisov { 285397f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 285497f4dd09SNikolay Borisov int ret; 285597f4dd09SNikolay Borisov 285697f4dd09SNikolay Borisov /* 285797f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 285897f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 285997f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 286097f4dd09SNikolay Borisov */ 286197f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 286297f4dd09SNikolay Borisov if (ret < 0) { 2863c94bec2cSJosef Bacik if (ret != -EINTR) 2864c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 2865c94bec2cSJosef Bacik ret); 286697f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 286797f4dd09SNikolay Borisov return ret; 286897f4dd09SNikolay Borisov } 286997f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 287097f4dd09SNikolay Borisov } 287197f4dd09SNikolay Borisov 287297f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 287397f4dd09SNikolay Borisov { 287497f4dd09SNikolay Borisov struct task_struct *task; 287597f4dd09SNikolay Borisov 287697f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 287797f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 287897f4dd09SNikolay Borisov if (IS_ERR(task)) { 287997f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 288097f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 288197f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 288297f4dd09SNikolay Borisov return PTR_ERR(task); 288397f4dd09SNikolay Borisov } 288497f4dd09SNikolay Borisov 288597f4dd09SNikolay Borisov return 0; 288697f4dd09SNikolay Borisov } 288797f4dd09SNikolay Borisov 2888ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 2889ae18c37aSJosef Bacik char *options) 2890ae18c37aSJosef Bacik { 2891ae18c37aSJosef Bacik u32 sectorsize; 2892ae18c37aSJosef Bacik u32 nodesize; 2893ae18c37aSJosef Bacik u32 stripesize; 2894ae18c37aSJosef Bacik u64 generation; 2895ae18c37aSJosef Bacik u64 features; 2896ae18c37aSJosef Bacik u16 csum_type; 2897ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 2898ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2899ae18c37aSJosef Bacik struct btrfs_root *tree_root; 2900ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 2901ae18c37aSJosef Bacik int ret; 2902ae18c37aSJosef Bacik int err = -EINVAL; 2903ae18c37aSJosef Bacik int clear_free_space_tree = 0; 2904ae18c37aSJosef Bacik int level; 2905ae18c37aSJosef Bacik 29068260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 290753b381b3SDavid Woodhouse if (ret) { 290883c8266aSDavid Sterba err = ret; 2909ae18c37aSJosef Bacik goto fail; 291053b381b3SDavid Woodhouse } 291153b381b3SDavid Woodhouse 2912ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 2913ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 2914ae18c37aSJosef Bacik GFP_KERNEL); 2915ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 2916ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 2917ae18c37aSJosef Bacik GFP_KERNEL); 2918ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 2919ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 2920ae18c37aSJosef Bacik err = -ENOMEM; 2921c75e8394SJosef Bacik goto fail; 2922ae18c37aSJosef Bacik } 2923ae18c37aSJosef Bacik 2924ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 2925ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 2926ae18c37aSJosef Bacik err = -ENOMEM; 2927c75e8394SJosef Bacik goto fail; 2928ae18c37aSJosef Bacik } 2929ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 2930ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 2931ae18c37aSJosef Bacik 29323c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 29331104a885SDavid Sterba 29341104a885SDavid Sterba /* 29351104a885SDavid Sterba * Read super block and check the signature bytes only 29361104a885SDavid Sterba */ 29378f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 29388f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 29398f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 294016cdcec7SMiao Xie goto fail_alloc; 294120b45077SDave Young } 294239279cc3SChris Mason 29431104a885SDavid Sterba /* 2944260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 29458dc3f22cSJohannes Thumshirn * corrupted, we'll find out 29468dc3f22cSJohannes Thumshirn */ 29478f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 294851bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 29498dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 295051bce6c9SJohannes Thumshirn csum_type); 29518dc3f22cSJohannes Thumshirn err = -EINVAL; 29528f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29538dc3f22cSJohannes Thumshirn goto fail_alloc; 29548dc3f22cSJohannes Thumshirn } 29558dc3f22cSJohannes Thumshirn 29566d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 29576d97c6e3SJohannes Thumshirn if (ret) { 29586d97c6e3SJohannes Thumshirn err = ret; 29598f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29606d97c6e3SJohannes Thumshirn goto fail_alloc; 29616d97c6e3SJohannes Thumshirn } 29626d97c6e3SJohannes Thumshirn 29638dc3f22cSJohannes Thumshirn /* 29641104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 29651104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 29661104a885SDavid Sterba */ 29678f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 296805135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 29691104a885SDavid Sterba err = -EINVAL; 29708f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 2971141386e1SJosef Bacik goto fail_alloc; 29721104a885SDavid Sterba } 29731104a885SDavid Sterba 29741104a885SDavid Sterba /* 29751104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 29761104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29771104a885SDavid Sterba * the whole block of INFO_SIZE 29781104a885SDavid Sterba */ 29798f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 29808f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29815f39d397SChris Mason 2982fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2983fbc6feaeSNikolay Borisov 2984de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2985de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2986de37aa51SNikolay Borisov 29877239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2988de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2989de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2990de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29917239ff4bSNikolay Borisov } 29920b86a832SChris Mason 2993fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 2994fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 2995fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 2996fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 2997fbc6feaeSNikolay Borisov btrfs_info(fs_info, 2998fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 2999fbc6feaeSNikolay Borisov } 3000fbc6feaeSNikolay Borisov 3001fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3002fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3003de37aa51SNikolay Borisov 3004069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 30051104a885SDavid Sterba if (ret) { 300605135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 30071104a885SDavid Sterba err = -EINVAL; 3008141386e1SJosef Bacik goto fail_alloc; 30091104a885SDavid Sterba } 30101104a885SDavid Sterba 30110f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3012141386e1SJosef Bacik goto fail_alloc; 30130f7d52f4SChris Mason 3014acce952bSliubo /* check FS state, whether FS is broken. */ 301587533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 301687533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3017acce952bSliubo 301875e7cb7fSLiu Bo /* 301975e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 302075e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 302175e7cb7fSLiu Bo */ 302275e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 302375e7cb7fSLiu Bo 30242ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 30252b82032cSYan Zheng if (ret) { 30262b82032cSYan Zheng err = ret; 3027141386e1SJosef Bacik goto fail_alloc; 30282b82032cSYan Zheng } 3029dfe25020SChris Mason 3030f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3031f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3032f2b636e8SJosef Bacik if (features) { 303305135f59SDavid Sterba btrfs_err(fs_info, 303405135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3035c1c9ff7cSGeert Uytterhoeven features); 3036f2b636e8SJosef Bacik err = -EINVAL; 3037141386e1SJosef Bacik goto fail_alloc; 3038f2b636e8SJosef Bacik } 3039f2b636e8SJosef Bacik 30405d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 30415d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 30420b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3043a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 30445c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 30455c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3046727011e0SChris Mason 30473173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 304805135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 30493173a18fSJosef Bacik 3050b70f5097SNaohiro Aota fs_info->zoned = (features & BTRFS_FEATURE_INCOMPAT_ZONED); 3051b70f5097SNaohiro Aota 3052727011e0SChris Mason /* 3053727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3054727011e0SChris Mason * they are bigger than the page size 3055727011e0SChris Mason */ 305609cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3057727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 305805135f59SDavid Sterba btrfs_info(fs_info, 305905135f59SDavid Sterba "flagging fs with big metadata feature"); 3060727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3061727011e0SChris Mason } 3062727011e0SChris Mason 3063bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3064bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3065b7f67055SChandan Rajendra stripesize = sectorsize; 3066707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3067963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3068bc3f116fSChris Mason 3069da17066cSJeff Mahoney /* Cache block sizes */ 3070da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3071da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3072ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 307322b6331dSDavid Sterba fs_info->csum_size = btrfs_super_csum_size(disk_super); 3074fe5ecbe8SDavid Sterba fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3075da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3076da17066cSJeff Mahoney 3077bc3f116fSChris Mason /* 3078bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3079bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3080bc3f116fSChris Mason */ 3081bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3082707e8a07SDavid Sterba (sectorsize != nodesize)) { 308305135f59SDavid Sterba btrfs_err(fs_info, 308405135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 308505135f59SDavid Sterba nodesize, sectorsize); 3086141386e1SJosef Bacik goto fail_alloc; 3087bc3f116fSChris Mason } 3088bc3f116fSChris Mason 3089ceda0864SMiao Xie /* 3090ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3091ceda0864SMiao Xie * update the flag. 3092ceda0864SMiao Xie */ 30935d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 30945d4f98a2SYan Zheng 3095f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3096f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3097bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 309805135f59SDavid Sterba btrfs_err(fs_info, 309905135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3100c1c9ff7cSGeert Uytterhoeven features); 3101f2b636e8SJosef Bacik err = -EINVAL; 3102141386e1SJosef Bacik goto fail_alloc; 3103f2b636e8SJosef Bacik } 310461d92c32SChris Mason 31052a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 31062a458198SEric Sandeen if (ret) { 31072a458198SEric Sandeen err = ret; 31080dc3b84aSJosef Bacik goto fail_sb_buffer; 31090dc3b84aSJosef Bacik } 31104543df7eSChris Mason 31119e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 31129e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 31134575c9ccSChris Mason 3114a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3115a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3116de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3117db94535dSChris Mason 3118925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 31196bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3120925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 312184eed90fSChris Mason if (ret) { 312205135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 31235d4f98a2SYan Zheng goto fail_sb_buffer; 312484eed90fSChris Mason } 31250b86a832SChris Mason 312684234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3127581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 31280b86a832SChris Mason 31292ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 31300b86a832SChris Mason btrfs_super_chunk_root(disk_super), 31311b7ec85eSJosef Bacik BTRFS_CHUNK_TREE_OBJECTID, 3132581c1760SQu Wenruo generation, level, NULL); 313364c043deSLiu Bo if (IS_ERR(chunk_root->node) || 313464c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 313505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3136e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3137e5fffbacSchandan free_extent_buffer(chunk_root->node); 313895ab1f64SZhao Lei chunk_root->node = NULL; 3139af31f5e5SChris Mason goto fail_tree_roots; 314083121942SDavid Woodhouse } 31415d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 31425d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 31430b86a832SChris Mason 3144e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3145c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3146c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3147e17cade2SChris Mason 31485b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 31492b82032cSYan Zheng if (ret) { 315005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3151af31f5e5SChris Mason goto fail_tree_roots; 31522b82032cSYan Zheng } 31530b86a832SChris Mason 31548dabb742SStefan Behrens /* 3155bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3156bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3157bacce86aSAnand Jain * target is required. So free devices that are not part of this 3158bacce86aSAnand Jain * filesystem but skip the replace traget device which is checked 3159bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 31608dabb742SStefan Behrens */ 3161bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3162a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 316305135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3164a6b0d5c8SChris Mason goto fail_tree_roots; 3165a6b0d5c8SChris Mason } 3166a6b0d5c8SChris Mason 3167b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 31684bbcaa64SEric Sandeen if (ret) 3169b8522a1eSNikolay Borisov goto fail_tree_roots; 31708929ecfaSYan, Zheng 317175ec1db8SJosef Bacik /* 317275ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 317375ec1db8SJosef Bacik * check the generation here so we can set the 317475ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 317575ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 317675ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 317775ec1db8SJosef Bacik * even though it was perfectly fine. 317875ec1db8SJosef Bacik */ 317975ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 318075ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 318175ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 318275ec1db8SJosef Bacik 3183cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3184cf90d884SQu Wenruo if (ret) { 3185cf90d884SQu Wenruo btrfs_err(fs_info, 3186cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3187cf90d884SQu Wenruo ret); 3188cf90d884SQu Wenruo goto fail_block_groups; 3189cf90d884SQu Wenruo } 319068310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 319168310a5eSIlya Dryomov if (ret) { 319205135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 319368310a5eSIlya Dryomov goto fail_block_groups; 319468310a5eSIlya Dryomov } 319568310a5eSIlya Dryomov 3196733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3197733f4fbbSStefan Behrens if (ret) { 319805135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3199733f4fbbSStefan Behrens goto fail_block_groups; 3200733f4fbbSStefan Behrens } 3201733f4fbbSStefan Behrens 32028dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 32038dabb742SStefan Behrens if (ret) { 320405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 32058dabb742SStefan Behrens goto fail_block_groups; 32068dabb742SStefan Behrens } 32078dabb742SStefan Behrens 3208b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3209b70f5097SNaohiro Aota if (ret) { 3210b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3211b70f5097SNaohiro Aota ret); 3212b70f5097SNaohiro Aota goto fail_block_groups; 3213b70f5097SNaohiro Aota } 3214b70f5097SNaohiro Aota 3215c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3216b7c35e81SAnand Jain if (ret) { 321705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 321805135f59SDavid Sterba ret); 3219b7c35e81SAnand Jain goto fail_block_groups; 3220b7c35e81SAnand Jain } 3221b7c35e81SAnand Jain 322296f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 32235ac1d209SJeff Mahoney if (ret) { 322405135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3225b7c35e81SAnand Jain goto fail_fsdev_sysfs; 32265ac1d209SJeff Mahoney } 32275ac1d209SJeff Mahoney 3228c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3229c59021f8Sliubo if (ret) { 323005135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 32312365dd3cSAnand Jain goto fail_sysfs; 3232c59021f8Sliubo } 3233c59021f8Sliubo 32345b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 32351b1d1f66SJosef Bacik if (ret) { 323605135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 32372365dd3cSAnand Jain goto fail_sysfs; 32381b1d1f66SJosef Bacik } 32394330e183SQu Wenruo 32406528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 324105135f59SDavid Sterba btrfs_warn(fs_info, 324252042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 32432365dd3cSAnand Jain goto fail_sysfs; 3244292fd7fcSStefan Behrens } 32459078a3e1SChris Mason 3246a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3247a74a4b97SChris Mason "btrfs-cleaner"); 324857506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 32492365dd3cSAnand Jain goto fail_sysfs; 3250a74a4b97SChris Mason 3251a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3252a74a4b97SChris Mason tree_root, 3253a74a4b97SChris Mason "btrfs-transaction"); 325457506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 32553f157a2fSChris Mason goto fail_cleaner; 3256a74a4b97SChris Mason 3257583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3258c289811cSChris Mason !fs_info->fs_devices->rotating) { 3259583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3260c289811cSChris Mason } 3261c289811cSChris Mason 3262572d9ab7SDavid Sterba /* 326301327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3264572d9ab7SDavid Sterba * not have to wait for transaction commit 3265572d9ab7SDavid Sterba */ 3266572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 32673818aea2SQu Wenruo 326821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 32690b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 32702ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 32710b246afaSJeff Mahoney btrfs_test_opt(fs_info, 327221adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 327321adbd5cSStefan Behrens 1 : 0, 327421adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 327521adbd5cSStefan Behrens if (ret) 327605135f59SDavid Sterba btrfs_warn(fs_info, 327705135f59SDavid Sterba "failed to initialize integrity check module: %d", 327805135f59SDavid Sterba ret); 327921adbd5cSStefan Behrens } 328021adbd5cSStefan Behrens #endif 3281bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3282bcef60f2SArne Jansen if (ret) 3283bcef60f2SArne Jansen goto fail_trans_kthread; 328421adbd5cSStefan Behrens 3285fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3286fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3287fd708b81SJosef Bacik 328896da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 328996da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32900b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3291e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 329263443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 329379787eaaSJeff Mahoney if (ret) { 329463443bf5SEric Sandeen err = ret; 329528c16cbbSWang Shilong goto fail_qgroup; 3296e556ce2cSYan Zheng } 3297e02119d5SChris Mason } 32981a40e23bSZheng Yan 32996bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 330079787eaaSJeff Mahoney if (ret) 330128c16cbbSWang Shilong goto fail_qgroup; 330276dda93cSYan, Zheng 3303bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3304d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 330544c44af2SIlya Dryomov if (ret) 330628c16cbbSWang Shilong goto fail_qgroup; 330790c711abSZygo Blaxell 330890c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 33095d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 331090c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3311d7ce5843SMiao Xie if (ret < 0) { 331205135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 331305135f59SDavid Sterba ret); 3314d7ce5843SMiao Xie err = -EINVAL; 3315bcef60f2SArne Jansen goto fail_qgroup; 3316d7ce5843SMiao Xie } 33177c2ca468SChris Mason } 33181a40e23bSZheng Yan 331956e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 33203140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 33213140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3322f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3323315bf8efSJosef Bacik fs_info->fs_root = NULL; 3324bcef60f2SArne Jansen goto fail_qgroup; 33253140c9a3SDan Carpenter } 3326c289811cSChris Mason 3327bc98a42cSDavid Howells if (sb_rdonly(sb)) 33282b6ba629SIlya Dryomov return 0; 33292b6ba629SIlya Dryomov 3330f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3331f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33326675df31SOmar Sandoval clear_free_space_tree = 1; 33336675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33346675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33356675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 33366675df31SOmar Sandoval clear_free_space_tree = 1; 33376675df31SOmar Sandoval } 33386675df31SOmar Sandoval 33396675df31SOmar Sandoval if (clear_free_space_tree) { 3340f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3341f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3342f8d468a1SOmar Sandoval if (ret) { 3343f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3344f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 33456bccf3abSJeff Mahoney close_ctree(fs_info); 3346f8d468a1SOmar Sandoval return ret; 3347f8d468a1SOmar Sandoval } 3348f8d468a1SOmar Sandoval } 3349f8d468a1SOmar Sandoval 33500b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3351511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 335205135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3353511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3354511711afSChris Mason if (ret) { 335505135f59SDavid Sterba btrfs_warn(fs_info, 335605135f59SDavid Sterba "failed to create free space tree: %d", ret); 33576bccf3abSJeff Mahoney close_ctree(fs_info); 3358511711afSChris Mason return ret; 3359511711afSChris Mason } 3360511711afSChris Mason } 3361511711afSChris Mason 3362e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 33632b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33642b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33652b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 33666bccf3abSJeff Mahoney close_ctree(fs_info); 33672b6ba629SIlya Dryomov return ret; 33682b6ba629SIlya Dryomov } 3369e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 337059641015SIlya Dryomov 33712b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 33722b6ba629SIlya Dryomov if (ret) { 337305135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 33746bccf3abSJeff Mahoney close_ctree(fs_info); 33752b6ba629SIlya Dryomov return ret; 3376e3acc2a6SJosef Bacik } 3377e3acc2a6SJosef Bacik 33788dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 33798dabb742SStefan Behrens if (ret) { 338005135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33816bccf3abSJeff Mahoney close_ctree(fs_info); 33828dabb742SStefan Behrens return ret; 33838dabb742SStefan Behrens } 33848dabb742SStefan Behrens 3385b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3386b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3387b382a324SJan Schmidt 33884bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 338905135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3390f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3391f7a81ea4SStefan Behrens if (ret) { 339205135f59SDavid Sterba btrfs_warn(fs_info, 339305135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33946bccf3abSJeff Mahoney close_ctree(fs_info); 3395f7a81ea4SStefan Behrens return ret; 3396f7a81ea4SStefan Behrens } 33970b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33984bbcaa64SEric Sandeen fs_info->generation != 33994bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 340005135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 340170f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 340270f80175SStefan Behrens if (ret) { 340305135f59SDavid Sterba btrfs_warn(fs_info, 340405135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 34056bccf3abSJeff Mahoney close_ctree(fs_info); 340670f80175SStefan Behrens return ret; 340770f80175SStefan Behrens } 3408f7a81ea4SStefan Behrens } 3409afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 341047ab2a6cSJosef Bacik 3411ad2b2c80SAl Viro return 0; 341239279cc3SChris Mason 3413bcef60f2SArne Jansen fail_qgroup: 3414bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 34157c2ca468SChris Mason fail_trans_kthread: 34167c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 34172ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3418faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 34193f157a2fSChris Mason fail_cleaner: 3420a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 34217c2ca468SChris Mason 34227c2ca468SChris Mason /* 34237c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 34247c2ca468SChris Mason * kthreads 34257c2ca468SChris Mason */ 34267c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 34277c2ca468SChris Mason 34282365dd3cSAnand Jain fail_sysfs: 34296618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 34302365dd3cSAnand Jain 3431b7c35e81SAnand Jain fail_fsdev_sysfs: 3432b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3433b7c35e81SAnand Jain 34341b1d1f66SJosef Bacik fail_block_groups: 343554067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3436af31f5e5SChris Mason 3437af31f5e5SChris Mason fail_tree_roots: 34389e3aa805SJosef Bacik if (fs_info->data_reloc_root) 34399e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 34404273eaffSAnand Jain free_root_pointers(fs_info, true); 34412b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3442af31f5e5SChris Mason 344339279cc3SChris Mason fail_sb_buffer: 34447abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 34455cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 344616cdcec7SMiao Xie fail_alloc: 3447586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3448586e46e2SIlya Dryomov 34494543df7eSChris Mason iput(fs_info->btree_inode); 34507e662854SQinghuang Feng fail: 3451586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3452ad2b2c80SAl Viro return err; 3453eb60ceacSChris Mason } 3454663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3455eb60ceacSChris Mason 3456314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3457f2984462SChris Mason { 3458314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3459314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3460314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3461314b6dd0SJohannes Thumshirn struct page *page; 3462442a4f63SStefan Behrens 3463314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3464314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3465314b6dd0SJohannes Thumshirn 3466314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3467fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3468314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3469314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3470314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3471314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3472314b6dd0SJohannes Thumshirn SetPageError(page); 3473314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3474314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3475314b6dd0SJohannes Thumshirn } else { 3476314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3477f2984462SChris Mason } 3478314b6dd0SJohannes Thumshirn 3479314b6dd0SJohannes Thumshirn put_page(page); 3480314b6dd0SJohannes Thumshirn unlock_page(page); 3481314b6dd0SJohannes Thumshirn } 3482314b6dd0SJohannes Thumshirn 3483314b6dd0SJohannes Thumshirn bio_put(bio); 3484f2984462SChris Mason } 3485f2984462SChris Mason 34868f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 34878f32380dSJohannes Thumshirn int copy_num) 348829c36d72SAnand Jain { 348929c36d72SAnand Jain struct btrfs_super_block *super; 34908f32380dSJohannes Thumshirn struct page *page; 3491*12659251SNaohiro Aota u64 bytenr, bytenr_orig; 34928f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 3493*12659251SNaohiro Aota int ret; 349429c36d72SAnand Jain 3495*12659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 3496*12659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 3497*12659251SNaohiro Aota if (ret == -ENOENT) 3498*12659251SNaohiro Aota return ERR_PTR(-EINVAL); 3499*12659251SNaohiro Aota else if (ret) 3500*12659251SNaohiro Aota return ERR_PTR(ret); 3501*12659251SNaohiro Aota 350229c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 35038f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 350429c36d72SAnand Jain 35058f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 35068f32380dSJohannes Thumshirn if (IS_ERR(page)) 35078f32380dSJohannes Thumshirn return ERR_CAST(page); 350829c36d72SAnand Jain 35098f32380dSJohannes Thumshirn super = page_address(page); 351096c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 351196c2e067SAnand Jain btrfs_release_disk_super(super); 351296c2e067SAnand Jain return ERR_PTR(-ENODATA); 351396c2e067SAnand Jain } 351496c2e067SAnand Jain 3515*12659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 35168f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 35178f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 351829c36d72SAnand Jain } 351929c36d72SAnand Jain 35208f32380dSJohannes Thumshirn return super; 352129c36d72SAnand Jain } 352229c36d72SAnand Jain 352329c36d72SAnand Jain 35248f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3525a512bbf8SYan Zheng { 35268f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3527a512bbf8SYan Zheng int i; 3528a512bbf8SYan Zheng u64 transid = 0; 3529a512bbf8SYan Zheng 3530a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3531a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3532a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3533a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3534a512bbf8SYan Zheng */ 3535a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 35368f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 35378f32380dSJohannes Thumshirn if (IS_ERR(super)) 3538a512bbf8SYan Zheng continue; 3539a512bbf8SYan Zheng 3540a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 35418f32380dSJohannes Thumshirn if (latest) 35428f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 35438f32380dSJohannes Thumshirn 35448f32380dSJohannes Thumshirn latest = super; 3545a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3546a512bbf8SYan Zheng } 3547a512bbf8SYan Zheng } 354892fc03fbSAnand Jain 35498f32380dSJohannes Thumshirn return super; 3550a512bbf8SYan Zheng } 3551a512bbf8SYan Zheng 35524eedeb75SHisashi Hifumi /* 3553abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3554314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 35554eedeb75SHisashi Hifumi * 3556abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3557abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3558abbb3b8eSDavid Sterba * same for write and wait phases. 35594eedeb75SHisashi Hifumi * 3560314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 35614eedeb75SHisashi Hifumi */ 3562a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3563abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3564a512bbf8SYan Zheng { 3565d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3566314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3567d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3568a512bbf8SYan Zheng int i; 3569a512bbf8SYan Zheng int errors = 0; 3570*12659251SNaohiro Aota int ret; 3571*12659251SNaohiro Aota u64 bytenr, bytenr_orig; 3572a512bbf8SYan Zheng 3573a512bbf8SYan Zheng if (max_mirrors == 0) 3574a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3575a512bbf8SYan Zheng 3576d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3577d5178578SJohannes Thumshirn 3578a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3579314b6dd0SJohannes Thumshirn struct page *page; 3580314b6dd0SJohannes Thumshirn struct bio *bio; 3581314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3582314b6dd0SJohannes Thumshirn 3583*12659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 3584*12659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 3585*12659251SNaohiro Aota if (ret == -ENOENT) { 3586*12659251SNaohiro Aota continue; 3587*12659251SNaohiro Aota } else if (ret < 0) { 3588*12659251SNaohiro Aota btrfs_err(device->fs_info, 3589*12659251SNaohiro Aota "couldn't get super block location for mirror %d", 3590*12659251SNaohiro Aota i); 3591*12659251SNaohiro Aota errors++; 3592*12659251SNaohiro Aota continue; 3593*12659251SNaohiro Aota } 3594935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3595935e5cc9SMiao Xie device->commit_total_bytes) 3596a512bbf8SYan Zheng break; 3597a512bbf8SYan Zheng 3598*12659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 3599a512bbf8SYan Zheng 3600fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3601fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 3602fd08001fSEric Biggers sb->csum); 3603a512bbf8SYan Zheng 3604314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3605314b6dd0SJohannes Thumshirn GFP_NOFS); 3606314b6dd0SJohannes Thumshirn if (!page) { 3607fb456252SJeff Mahoney btrfs_err(device->fs_info, 3608314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3609f14d104dSDavid Sterba bytenr); 3610634554dcSJosef Bacik errors++; 3611634554dcSJosef Bacik continue; 3612634554dcSJosef Bacik } 3613634554dcSJosef Bacik 3614314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3615314b6dd0SJohannes Thumshirn get_page(page); 3616a512bbf8SYan Zheng 3617314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3618314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3619a512bbf8SYan Zheng 3620387125fcSChris Mason /* 3621314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3622314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3623314b6dd0SJohannes Thumshirn * checking. 3624387125fcSChris Mason */ 3625314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3626314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3627314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3628314b6dd0SJohannes Thumshirn bio->bi_private = device; 3629314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3630314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3631314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3632314b6dd0SJohannes Thumshirn 3633314b6dd0SJohannes Thumshirn /* 3634314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3635314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3636314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3637314b6dd0SJohannes Thumshirn */ 3638314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 36391b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3640314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3641314b6dd0SJohannes Thumshirn 3642314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 3643*12659251SNaohiro Aota btrfs_advance_sb_log(device, i); 3644a512bbf8SYan Zheng } 3645a512bbf8SYan Zheng return errors < i ? 0 : -1; 3646a512bbf8SYan Zheng } 3647a512bbf8SYan Zheng 3648387125fcSChris Mason /* 3649abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3650abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3651abbb3b8eSDavid Sterba * 3652314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3653abbb3b8eSDavid Sterba * date. 3654abbb3b8eSDavid Sterba */ 3655abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3656abbb3b8eSDavid Sterba { 3657abbb3b8eSDavid Sterba int i; 3658abbb3b8eSDavid Sterba int errors = 0; 3659b6a535faSHoward McLauchlan bool primary_failed = false; 3660*12659251SNaohiro Aota int ret; 3661abbb3b8eSDavid Sterba u64 bytenr; 3662abbb3b8eSDavid Sterba 3663abbb3b8eSDavid Sterba if (max_mirrors == 0) 3664abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3665abbb3b8eSDavid Sterba 3666abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3667314b6dd0SJohannes Thumshirn struct page *page; 3668314b6dd0SJohannes Thumshirn 3669*12659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 3670*12659251SNaohiro Aota if (ret == -ENOENT) { 3671*12659251SNaohiro Aota break; 3672*12659251SNaohiro Aota } else if (ret < 0) { 3673*12659251SNaohiro Aota errors++; 3674*12659251SNaohiro Aota if (i == 0) 3675*12659251SNaohiro Aota primary_failed = true; 3676*12659251SNaohiro Aota continue; 3677*12659251SNaohiro Aota } 3678abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3679abbb3b8eSDavid Sterba device->commit_total_bytes) 3680abbb3b8eSDavid Sterba break; 3681abbb3b8eSDavid Sterba 3682314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3683314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3684314b6dd0SJohannes Thumshirn if (!page) { 3685abbb3b8eSDavid Sterba errors++; 3686b6a535faSHoward McLauchlan if (i == 0) 3687b6a535faSHoward McLauchlan primary_failed = true; 3688abbb3b8eSDavid Sterba continue; 3689abbb3b8eSDavid Sterba } 3690314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3691314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3692314b6dd0SJohannes Thumshirn if (PageError(page)) { 3693abbb3b8eSDavid Sterba errors++; 3694b6a535faSHoward McLauchlan if (i == 0) 3695b6a535faSHoward McLauchlan primary_failed = true; 3696b6a535faSHoward McLauchlan } 3697abbb3b8eSDavid Sterba 3698314b6dd0SJohannes Thumshirn /* Drop our reference */ 3699314b6dd0SJohannes Thumshirn put_page(page); 3700abbb3b8eSDavid Sterba 3701314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3702314b6dd0SJohannes Thumshirn put_page(page); 3703abbb3b8eSDavid Sterba } 3704abbb3b8eSDavid Sterba 3705b6a535faSHoward McLauchlan /* log error, force error return */ 3706b6a535faSHoward McLauchlan if (primary_failed) { 3707b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3708b6a535faSHoward McLauchlan device->devid); 3709b6a535faSHoward McLauchlan return -1; 3710b6a535faSHoward McLauchlan } 3711b6a535faSHoward McLauchlan 3712abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3713abbb3b8eSDavid Sterba } 3714abbb3b8eSDavid Sterba 3715abbb3b8eSDavid Sterba /* 3716387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3717387125fcSChris Mason * for the barrier when it is done 3718387125fcSChris Mason */ 37194246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3720387125fcSChris Mason { 3721387125fcSChris Mason complete(bio->bi_private); 3722387125fcSChris Mason } 3723387125fcSChris Mason 3724387125fcSChris Mason /* 37254fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 37264fc6441aSAnand Jain * done in the waiting counterpart. 3727387125fcSChris Mason */ 37284fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3729387125fcSChris Mason { 3730c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3731e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3732387125fcSChris Mason 3733c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 37344fc6441aSAnand Jain return; 3735387125fcSChris Mason 3736e0ae9994SDavid Sterba bio_reset(bio); 3737387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 373874d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 37398d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3740387125fcSChris Mason init_completion(&device->flush_wait); 3741387125fcSChris Mason bio->bi_private = &device->flush_wait; 3742387125fcSChris Mason 374343a01111SLu Fengqi btrfsic_submit_bio(bio); 37441c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 37454fc6441aSAnand Jain } 3746387125fcSChris Mason 37474fc6441aSAnand Jain /* 37484fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 37494fc6441aSAnand Jain */ 37508c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 37514fc6441aSAnand Jain { 37524fc6441aSAnand Jain struct bio *bio = device->flush_bio; 37534fc6441aSAnand Jain 37541c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 375558efbc9fSOmar Sandoval return BLK_STS_OK; 37564fc6441aSAnand Jain 37571c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 37582980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 37594fc6441aSAnand Jain 37608c27cb35SLinus Torvalds return bio->bi_status; 3761387125fcSChris Mason } 3762387125fcSChris Mason 3763d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3764401b41e5SAnand Jain { 37656528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3766401b41e5SAnand Jain return -EIO; 3767387125fcSChris Mason return 0; 3768387125fcSChris Mason } 3769387125fcSChris Mason 3770387125fcSChris Mason /* 3771387125fcSChris Mason * send an empty flush down to each device in parallel, 3772387125fcSChris Mason * then wait for them 3773387125fcSChris Mason */ 3774387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3775387125fcSChris Mason { 3776387125fcSChris Mason struct list_head *head; 3777387125fcSChris Mason struct btrfs_device *dev; 37785af3e8ccSStefan Behrens int errors_wait = 0; 37794e4cbee9SChristoph Hellwig blk_status_t ret; 3780387125fcSChris Mason 37811538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3782387125fcSChris Mason /* send down all the barriers */ 3783387125fcSChris Mason head = &info->fs_devices->devices; 37841538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3785e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3786f88ba6a2SHidetoshi Seto continue; 3787cea7c8bfSAnand Jain if (!dev->bdev) 3788387125fcSChris Mason continue; 3789e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3790ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3791387125fcSChris Mason continue; 3792387125fcSChris Mason 37934fc6441aSAnand Jain write_dev_flush(dev); 379458efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3795387125fcSChris Mason } 3796387125fcSChris Mason 3797387125fcSChris Mason /* wait for all the barriers */ 37981538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3799e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3800f88ba6a2SHidetoshi Seto continue; 3801387125fcSChris Mason if (!dev->bdev) { 38025af3e8ccSStefan Behrens errors_wait++; 3803387125fcSChris Mason continue; 3804387125fcSChris Mason } 3805e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3806ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3807387125fcSChris Mason continue; 3808387125fcSChris Mason 38094fc6441aSAnand Jain ret = wait_dev_flush(dev); 3810401b41e5SAnand Jain if (ret) { 3811401b41e5SAnand Jain dev->last_flush_error = ret; 381266b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 381366b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 38145af3e8ccSStefan Behrens errors_wait++; 3815387125fcSChris Mason } 3816401b41e5SAnand Jain } 3817401b41e5SAnand Jain 3818cea7c8bfSAnand Jain if (errors_wait) { 3819401b41e5SAnand Jain /* 3820401b41e5SAnand Jain * At some point we need the status of all disks 3821401b41e5SAnand Jain * to arrive at the volume status. So error checking 3822401b41e5SAnand Jain * is being pushed to a separate loop. 3823401b41e5SAnand Jain */ 3824d10b82feSQu Wenruo return check_barrier_error(info); 3825401b41e5SAnand Jain } 3826387125fcSChris Mason return 0; 3827387125fcSChris Mason } 3828387125fcSChris Mason 3829943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3830943c6e99SZhao Lei { 38318789f4feSZhao Lei int raid_type; 38328789f4feSZhao Lei int min_tolerated = INT_MAX; 3833943c6e99SZhao Lei 38348789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 38358789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 38368c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 38378789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 38388789f4feSZhao Lei tolerated_failures); 3839943c6e99SZhao Lei 38408789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 38418789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 38428789f4feSZhao Lei continue; 384341a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 38448789f4feSZhao Lei continue; 38458c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 38468789f4feSZhao Lei btrfs_raid_array[raid_type]. 38478789f4feSZhao Lei tolerated_failures); 38488789f4feSZhao Lei } 3849943c6e99SZhao Lei 38508789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3851ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 38528789f4feSZhao Lei min_tolerated = 0; 38538789f4feSZhao Lei } 38548789f4feSZhao Lei 38558789f4feSZhao Lei return min_tolerated; 3856943c6e99SZhao Lei } 3857943c6e99SZhao Lei 3858eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3859f2984462SChris Mason { 3860e5e9a520SChris Mason struct list_head *head; 3861f2984462SChris Mason struct btrfs_device *dev; 3862a061fc8dSChris Mason struct btrfs_super_block *sb; 3863f2984462SChris Mason struct btrfs_dev_item *dev_item; 3864f2984462SChris Mason int ret; 3865f2984462SChris Mason int do_barriers; 3866a236aed1SChris Mason int max_errors; 3867a236aed1SChris Mason int total_errors = 0; 3868a061fc8dSChris Mason u64 flags; 3869f2984462SChris Mason 38700b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3871fed3b381SLiu Bo 3872fed3b381SLiu Bo /* 3873fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3874fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3875fed3b381SLiu Bo * been consistent on disk. 3876fed3b381SLiu Bo */ 3877fed3b381SLiu Bo if (max_mirrors == 0) 38780b246afaSJeff Mahoney backup_super_roots(fs_info); 3879f2984462SChris Mason 38800b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3881a061fc8dSChris Mason dev_item = &sb->dev_item; 3882e5e9a520SChris Mason 38830b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 38840b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 38850b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3886387125fcSChris Mason 38875af3e8ccSStefan Behrens if (do_barriers) { 38880b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 38895af3e8ccSStefan Behrens if (ret) { 38905af3e8ccSStefan Behrens mutex_unlock( 38910b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38920b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38935af3e8ccSStefan Behrens "errors while submitting device barriers."); 38945af3e8ccSStefan Behrens return ret; 38955af3e8ccSStefan Behrens } 38965af3e8ccSStefan Behrens } 3897387125fcSChris Mason 38981538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3899dfe25020SChris Mason if (!dev->bdev) { 3900dfe25020SChris Mason total_errors++; 3901dfe25020SChris Mason continue; 3902dfe25020SChris Mason } 3903e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3904ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3905dfe25020SChris Mason continue; 3906dfe25020SChris Mason 39072b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3908a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3909a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 39107df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3911935e5cc9SMiao Xie dev->commit_total_bytes); 3912ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3913ce7213c7SMiao Xie dev->commit_bytes_used); 3914a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3915a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3916a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3917a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 39187239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 39197239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3920a512bbf8SYan Zheng 3921a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3922a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3923f2984462SChris Mason 392475cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 392575cb857dSQu Wenruo if (ret < 0) { 392675cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 392775cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 392875cb857dSQu Wenruo "unexpected superblock corruption detected"); 392975cb857dSQu Wenruo return -EUCLEAN; 393075cb857dSQu Wenruo } 393175cb857dSQu Wenruo 3932abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3933a236aed1SChris Mason if (ret) 3934a236aed1SChris Mason total_errors++; 3935f2984462SChris Mason } 3936a236aed1SChris Mason if (total_errors > max_errors) { 39370b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3938d397712bSChris Mason total_errors); 39390b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 394079787eaaSJeff Mahoney 39419d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 39420b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 39430b246afaSJeff Mahoney "%d errors while writing supers", 39440b246afaSJeff Mahoney total_errors); 39459d565ba4SStefan Behrens return -EIO; 3946a236aed1SChris Mason } 3947f2984462SChris Mason 3948a512bbf8SYan Zheng total_errors = 0; 39491538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3950dfe25020SChris Mason if (!dev->bdev) 3951dfe25020SChris Mason continue; 3952e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3953ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3954dfe25020SChris Mason continue; 3955dfe25020SChris Mason 3956abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3957a512bbf8SYan Zheng if (ret) 39581259ab75SChris Mason total_errors++; 3959f2984462SChris Mason } 39600b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3961a236aed1SChris Mason if (total_errors > max_errors) { 39620b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 39630b246afaSJeff Mahoney "%d errors while writing supers", 39640b246afaSJeff Mahoney total_errors); 396579787eaaSJeff Mahoney return -EIO; 3966a236aed1SChris Mason } 3967f2984462SChris Mason return 0; 3968f2984462SChris Mason } 3969f2984462SChris Mason 3970cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3971cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3972cb517eabSMiao Xie struct btrfs_root *root) 39732619ba1fSChris Mason { 39744785e24fSJosef Bacik bool drop_ref = false; 39754785e24fSJosef Bacik 39764df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 39772619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 39782619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3979af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 39804785e24fSJosef Bacik drop_ref = true; 39814df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 398276dda93cSYan, Zheng 39831c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 3984ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 39851c1ea4f7SLiu Bo if (root->reloc_root) { 398600246528SJosef Bacik btrfs_put_root(root->reloc_root); 39871c1ea4f7SLiu Bo root->reloc_root = NULL; 39881c1ea4f7SLiu Bo } 39891c1ea4f7SLiu Bo } 39903321719eSLiu Bo 3991faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3992581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3993faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3994581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 39950e996e7fSJosef Bacik if (root->ino_cache_inode) { 39960e996e7fSJosef Bacik iput(root->ino_cache_inode); 39970e996e7fSJosef Bacik root->ino_cache_inode = NULL; 39980e996e7fSJosef Bacik } 39994785e24fSJosef Bacik if (drop_ref) 400000246528SJosef Bacik btrfs_put_root(root); 40012619ba1fSChris Mason } 40022619ba1fSChris Mason 4003c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4004c146afadSYan Zheng { 4005c146afadSYan Zheng u64 root_objectid = 0; 4006c146afadSYan Zheng struct btrfs_root *gang[8]; 400765d33fd7SQu Wenruo int i = 0; 400865d33fd7SQu Wenruo int err = 0; 400965d33fd7SQu Wenruo unsigned int ret = 0; 4010c146afadSYan Zheng 4011c146afadSYan Zheng while (1) { 4012efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4013c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4014c146afadSYan Zheng (void **)gang, root_objectid, 4015c146afadSYan Zheng ARRAY_SIZE(gang)); 401665d33fd7SQu Wenruo if (!ret) { 4017efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4018c146afadSYan Zheng break; 401965d33fd7SQu Wenruo } 40205d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 402166b4ffd1SJosef Bacik 402265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 402365d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 402465d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 402565d33fd7SQu Wenruo gang[i] = NULL; 402665d33fd7SQu Wenruo continue; 402765d33fd7SQu Wenruo } 402865d33fd7SQu Wenruo /* grab all the search result for later use */ 402900246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 403065d33fd7SQu Wenruo } 4031efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 403265d33fd7SQu Wenruo 403365d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 403465d33fd7SQu Wenruo if (!gang[i]) 403565d33fd7SQu Wenruo continue; 4036c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 403766b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 403866b4ffd1SJosef Bacik if (err) 403965d33fd7SQu Wenruo break; 404000246528SJosef Bacik btrfs_put_root(gang[i]); 4041c146afadSYan Zheng } 4042c146afadSYan Zheng root_objectid++; 4043c146afadSYan Zheng } 404465d33fd7SQu Wenruo 404565d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 404665d33fd7SQu Wenruo for (; i < ret; i++) { 404765d33fd7SQu Wenruo if (gang[i]) 404800246528SJosef Bacik btrfs_put_root(gang[i]); 404965d33fd7SQu Wenruo } 405065d33fd7SQu Wenruo return err; 4051c146afadSYan Zheng } 4052c146afadSYan Zheng 40536bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4054c146afadSYan Zheng { 40556bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4056c146afadSYan Zheng struct btrfs_trans_handle *trans; 4057c146afadSYan Zheng 40580b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 40592ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 40600b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 40610b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4062c71bf099SYan, Zheng 4063c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 40640b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 40650b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4066c71bf099SYan, Zheng 40677a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 40683612b495STsutomu Itoh if (IS_ERR(trans)) 40693612b495STsutomu Itoh return PTR_ERR(trans); 40703a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4071c146afadSYan Zheng } 4072c146afadSYan Zheng 4073b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4074eb60ceacSChris Mason { 4075c146afadSYan Zheng int ret; 4076e089f05cSChris Mason 4077afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4078d6fd0ae2SOmar Sandoval /* 4079d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4080d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4081d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4082d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4083d6fd0ae2SOmar Sandoval */ 4084d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4085a2135011SChris Mason 40867343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4087d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40887343dd61SJustin Maggard 4089803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4090803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4091803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4092803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4093803b2f54SStefan Behrens 4094837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4095aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4096837d5b6eSIlya Dryomov 40978dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40988dabb742SStefan Behrens 4099aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 41004cb5300bSChris Mason 41014cb5300bSChris Mason /* wait for any defraggers to finish */ 41024cb5300bSChris Mason wait_event(fs_info->transaction_wait, 41034cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 41044cb5300bSChris Mason 41054cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 410626176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 41074cb5300bSChris Mason 410821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 410957056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 411021c7e756SMiao Xie 4111b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4112b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4113b0643e59SDennis Zhou 4114bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4115e44163e1SJeff Mahoney /* 4116d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4117d6fd0ae2SOmar Sandoval * unused block groups. 4118e44163e1SJeff Mahoney */ 41190b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4120e44163e1SJeff Mahoney 4121f0cc2cd7SFilipe Manana /* 4122f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4123f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4124f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4125f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4126f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4127f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4128f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4129f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4130f0cc2cd7SFilipe Manana * This is a very rare case. 4131f0cc2cd7SFilipe Manana */ 4132f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4133f0cc2cd7SFilipe Manana 41346bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4135d397712bSChris Mason if (ret) 413604892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4137c146afadSYan Zheng } 4138ed2ff2cbSChris Mason 4139af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4140af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 41412ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4142acce952bSliubo 4143e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4144e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 41458929ecfaSYan, Zheng 4146e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4147afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4148f25784b3SYan Zheng 41495958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 41505958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 41515958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 41525958253cSQu Wenruo } 41535958253cSQu Wenruo 415404892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4155fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4156bcef60f2SArne Jansen 4157963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 415804892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4159963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4160b0c68f8bSChris Mason } 416131153d81SYan Zheng 41624297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 41634297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 41644297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 41654297ff84SJosef Bacik 41666618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4167b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 41685ac1d209SJeff Mahoney 41691a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 41701a4319ccSLiu Bo 4171de348ee0SWang Shilong /* 4172de348ee0SWang Shilong * we must make sure there is not any read request to 4173de348ee0SWang Shilong * submit after we stopping all workers. 4174de348ee0SWang Shilong */ 4175de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 417696192499SJosef Bacik btrfs_stop_all_workers(fs_info); 417796192499SJosef Bacik 4178afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 41794273eaffSAnand Jain free_root_pointers(fs_info, true); 41808c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 41819ad6b7bcSChris Mason 41824e19443dSJosef Bacik /* 41834e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 41844e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 41854e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 41864e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 41874e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 41884e19443dSJosef Bacik */ 41894e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 41904e19443dSJosef Bacik 419113e6c37bSJosef Bacik iput(fs_info->btree_inode); 4192d6bfde87SChris Mason 419321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 41940b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 41952ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 419621adbd5cSStefan Behrens #endif 419721adbd5cSStefan Behrens 41980b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 419968c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4200eb60ceacSChris Mason } 4201eb60ceacSChris Mason 4202b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4203b9fab919SChris Mason int atomic) 4204ccd467d6SChris Mason { 42051259ab75SChris Mason int ret; 4206727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 42071259ab75SChris Mason 42080b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 42091259ab75SChris Mason if (!ret) 42101259ab75SChris Mason return ret; 42111259ab75SChris Mason 42121259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4213b9fab919SChris Mason parent_transid, atomic); 4214b9fab919SChris Mason if (ret == -EAGAIN) 4215b9fab919SChris Mason return ret; 42161259ab75SChris Mason return !ret; 42175f39d397SChris Mason } 42186702ed49SChris Mason 42195f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 42205f39d397SChris Mason { 42212f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 42225f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4223b9473439SChris Mason int was_dirty; 4224b4ce94deSChris Mason 422506ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 422606ea65a3SJosef Bacik /* 422706ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 422852042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 422906ea65a3SJosef Bacik * outside of the sanity tests. 423006ea65a3SJosef Bacik */ 4231b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 423206ea65a3SJosef Bacik return; 423306ea65a3SJosef Bacik #endif 4234b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 42350b246afaSJeff Mahoney if (transid != fs_info->generation) 42365d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 42370b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 42380b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4239e2d84521SMiao Xie if (!was_dirty) 4240104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4241e2d84521SMiao Xie buf->len, 42420b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 42431f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 424469fc6cbbSQu Wenruo /* 424569fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 424669fc6cbbSQu Wenruo * but item data not updated. 424769fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 424869fc6cbbSQu Wenruo */ 424969fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4250cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4251a4f78750SDavid Sterba btrfs_print_leaf(buf); 42521f21ef0aSFilipe Manana ASSERT(0); 42531f21ef0aSFilipe Manana } 42541f21ef0aSFilipe Manana #endif 4255eb60ceacSChris Mason } 4256eb60ceacSChris Mason 42572ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4258b53d3f5dSLiu Bo int flush_delayed) 425935b7e476SChris Mason { 4260188de649SChris Mason /* 4261188de649SChris Mason * looks as though older kernels can get into trouble with 4262188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4263188de649SChris Mason */ 4264e2d84521SMiao Xie int ret; 4265d6bfde87SChris Mason 42666933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4267d6bfde87SChris Mason return; 4268d6bfde87SChris Mason 4269b53d3f5dSLiu Bo if (flush_delayed) 42702ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 427116cdcec7SMiao Xie 4272d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4273d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4274d814a491SEthan Lien fs_info->dirty_metadata_batch); 4275e2d84521SMiao Xie if (ret > 0) { 42760b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 427716cdcec7SMiao Xie } 427816cdcec7SMiao Xie } 427916cdcec7SMiao Xie 42802ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 428116cdcec7SMiao Xie { 42822ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 428335b7e476SChris Mason } 4284b53d3f5dSLiu Bo 42852ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4286b53d3f5dSLiu Bo { 42872ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4288d6bfde87SChris Mason } 42896b80053dSChris Mason 4290581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4291581c1760SQu Wenruo struct btrfs_key *first_key) 42926b80053dSChris Mason { 42935ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4294581c1760SQu Wenruo level, first_key); 42956b80053dSChris Mason } 42960da5468fSChris Mason 42972ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4298acce952bSliubo { 4299fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4300fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4301fe816d0fSNikolay Borisov 43020b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 43032ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 43040b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4305acce952bSliubo 43060b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 43070b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4308acce952bSliubo } 4309acce952bSliubo 4310ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4311ef67963dSJosef Bacik { 4312ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4313ef67963dSJosef Bacik u64 root_objectid = 0; 4314ef67963dSJosef Bacik int ret; 4315ef67963dSJosef Bacik 4316ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4317ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4318ef67963dSJosef Bacik (void **)gang, root_objectid, 4319ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4320ef67963dSJosef Bacik int i; 4321ef67963dSJosef Bacik 4322ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4323ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4324ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4325ef67963dSJosef Bacik 4326ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4327ef67963dSJosef Bacik if (!gang[i]) 4328ef67963dSJosef Bacik continue; 4329ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4330ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4331ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4332ef67963dSJosef Bacik } 4333ef67963dSJosef Bacik root_objectid++; 4334ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4335ef67963dSJosef Bacik } 4336ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4337ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4338ef67963dSJosef Bacik } 4339ef67963dSJosef Bacik 4340143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4341acce952bSliubo { 4342acce952bSliubo struct btrfs_ordered_extent *ordered; 4343acce952bSliubo 4344199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4345779880efSJosef Bacik /* 4346779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4347779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4348779880efSJosef Bacik */ 4349199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4350779880efSJosef Bacik root_extent_list) 4351779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4352199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4353199c2a9cSMiao Xie } 4354199c2a9cSMiao Xie 4355199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4356199c2a9cSMiao Xie { 4357199c2a9cSMiao Xie struct btrfs_root *root; 4358199c2a9cSMiao Xie struct list_head splice; 4359199c2a9cSMiao Xie 4360199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4361199c2a9cSMiao Xie 4362199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4363199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4364199c2a9cSMiao Xie while (!list_empty(&splice)) { 4365199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4366199c2a9cSMiao Xie ordered_root); 43671de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 43681de2cfdeSJosef Bacik &fs_info->ordered_roots); 4369199c2a9cSMiao Xie 43702a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4371199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4372199c2a9cSMiao Xie 43732a85d9caSLiu Bo cond_resched(); 43742a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4375199c2a9cSMiao Xie } 4376199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 437774d5d229SJosef Bacik 437874d5d229SJosef Bacik /* 437974d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 438074d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 438174d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 438274d5d229SJosef Bacik * actually get run and error out properly. 438374d5d229SJosef Bacik */ 438474d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4385acce952bSliubo } 4386acce952bSliubo 438735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43882ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4389acce952bSliubo { 4390acce952bSliubo struct rb_node *node; 4391acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4392acce952bSliubo struct btrfs_delayed_ref_node *ref; 4393acce952bSliubo int ret = 0; 4394acce952bSliubo 4395acce952bSliubo delayed_refs = &trans->delayed_refs; 4396acce952bSliubo 4397acce952bSliubo spin_lock(&delayed_refs->lock); 4398d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4399cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4400b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4401acce952bSliubo return ret; 4402acce952bSliubo } 4403acce952bSliubo 44045c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4405d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 44060e0adbcfSJosef Bacik struct rb_node *n; 4407e78417d1SJosef Bacik bool pin_bytes = false; 4408acce952bSliubo 4409d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4410d7df2c79SJosef Bacik href_node); 44113069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4412b939d1abSJosef Bacik continue; 44133069bd26SJosef Bacik 4414d7df2c79SJosef Bacik spin_lock(&head->lock); 4415e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 44160e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 44170e0adbcfSJosef Bacik ref_node); 4418d7df2c79SJosef Bacik ref->in_tree = 0; 4419e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 44200e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 44211d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 44221d57ee94SWang Xiaoguang list_del(&ref->add_list); 4423d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4424d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4425d7df2c79SJosef Bacik } 442654067ae9SJosef Bacik if (head->must_insert_reserved) 4427e78417d1SJosef Bacik pin_bytes = true; 442878a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4429fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4430d7df2c79SJosef Bacik spin_unlock(&head->lock); 4431acce952bSliubo spin_unlock(&delayed_refs->lock); 4432e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4433acce952bSliubo 4434f603bb94SNikolay Borisov if (pin_bytes) { 4435f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4436f603bb94SNikolay Borisov 4437f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4438f603bb94SNikolay Borisov BUG_ON(!cache); 4439f603bb94SNikolay Borisov 4440f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4441f603bb94SNikolay Borisov spin_lock(&cache->lock); 4442f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4443f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4444f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4445f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4446f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4447f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4448f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4449f603bb94SNikolay Borisov percpu_counter_add_batch( 4450f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4451f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4452f603bb94SNikolay Borisov 4453f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4454f603bb94SNikolay Borisov 4455f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4456f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4457f603bb94SNikolay Borisov } 445831890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4459d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4460acce952bSliubo cond_resched(); 4461acce952bSliubo spin_lock(&delayed_refs->lock); 4462acce952bSliubo } 446381f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4464acce952bSliubo 4465acce952bSliubo spin_unlock(&delayed_refs->lock); 4466acce952bSliubo 4467acce952bSliubo return ret; 4468acce952bSliubo } 4469acce952bSliubo 4470143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4471acce952bSliubo { 4472acce952bSliubo struct btrfs_inode *btrfs_inode; 4473acce952bSliubo struct list_head splice; 4474acce952bSliubo 4475acce952bSliubo INIT_LIST_HEAD(&splice); 4476acce952bSliubo 4477eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4478eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4479acce952bSliubo 4480acce952bSliubo while (!list_empty(&splice)) { 4481fe816d0fSNikolay Borisov struct inode *inode = NULL; 4482eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4483acce952bSliubo delalloc_inodes); 4484fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4485eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4486acce952bSliubo 4487fe816d0fSNikolay Borisov /* 4488fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4489fe816d0fSNikolay Borisov * meanwhile. 4490fe816d0fSNikolay Borisov */ 4491fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4492fe816d0fSNikolay Borisov if (inode) { 4493fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4494fe816d0fSNikolay Borisov iput(inode); 4495fe816d0fSNikolay Borisov } 4496eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4497acce952bSliubo } 4498eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4499eb73c1b7SMiao Xie } 4500eb73c1b7SMiao Xie 4501eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4502eb73c1b7SMiao Xie { 4503eb73c1b7SMiao Xie struct btrfs_root *root; 4504eb73c1b7SMiao Xie struct list_head splice; 4505eb73c1b7SMiao Xie 4506eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4507eb73c1b7SMiao Xie 4508eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4509eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4510eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4511eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4512eb73c1b7SMiao Xie delalloc_root); 451300246528SJosef Bacik root = btrfs_grab_root(root); 4514eb73c1b7SMiao Xie BUG_ON(!root); 4515eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4516eb73c1b7SMiao Xie 4517eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 451800246528SJosef Bacik btrfs_put_root(root); 4519eb73c1b7SMiao Xie 4520eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4521eb73c1b7SMiao Xie } 4522eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4523acce952bSliubo } 4524acce952bSliubo 45252ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4526acce952bSliubo struct extent_io_tree *dirty_pages, 4527acce952bSliubo int mark) 4528acce952bSliubo { 4529acce952bSliubo int ret; 4530acce952bSliubo struct extent_buffer *eb; 4531acce952bSliubo u64 start = 0; 4532acce952bSliubo u64 end; 4533acce952bSliubo 4534acce952bSliubo while (1) { 4535acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4536e6138876SJosef Bacik mark, NULL); 4537acce952bSliubo if (ret) 4538acce952bSliubo break; 4539acce952bSliubo 454091166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4541acce952bSliubo while (start <= end) { 45420b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 45430b246afaSJeff Mahoney start += fs_info->nodesize; 4544fd8b2b61SJosef Bacik if (!eb) 4545acce952bSliubo continue; 4546fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4547acce952bSliubo 4548fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4549fd8b2b61SJosef Bacik &eb->bflags)) 4550fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4551fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4552acce952bSliubo } 4553acce952bSliubo } 4554acce952bSliubo 4555acce952bSliubo return ret; 4556acce952bSliubo } 4557acce952bSliubo 45582ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4559fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4560acce952bSliubo { 4561acce952bSliubo u64 start; 4562acce952bSliubo u64 end; 4563acce952bSliubo int ret; 4564acce952bSliubo 4565acce952bSliubo while (1) { 45660e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 45670e6ec385SFilipe Manana 4568fcd5e742SLu Fengqi /* 4569fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4570fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4571fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4572fcd5e742SLu Fengqi * the same extent range. 4573fcd5e742SLu Fengqi */ 4574fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4575acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 45760e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4577fcd5e742SLu Fengqi if (ret) { 4578fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4579acce952bSliubo break; 4580fcd5e742SLu Fengqi } 4581acce952bSliubo 45820e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 45830e6ec385SFilipe Manana free_extent_state(cached_state); 45842ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4585fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4586acce952bSliubo cond_resched(); 4587acce952bSliubo } 4588acce952bSliubo 4589acce952bSliubo return 0; 4590acce952bSliubo } 4591acce952bSliubo 459232da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4593c79a1751SLiu Bo { 4594c79a1751SLiu Bo struct inode *inode; 4595c79a1751SLiu Bo 4596c79a1751SLiu Bo inode = cache->io_ctl.inode; 4597c79a1751SLiu Bo if (inode) { 4598c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4599c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4600c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4601c79a1751SLiu Bo iput(inode); 4602c79a1751SLiu Bo } 4603bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 4604c79a1751SLiu Bo btrfs_put_block_group(cache); 4605c79a1751SLiu Bo } 4606c79a1751SLiu Bo 4607c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 46082ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4609c79a1751SLiu Bo { 461032da5386SDavid Sterba struct btrfs_block_group *cache; 4611c79a1751SLiu Bo 4612c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4613c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4614c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 461532da5386SDavid Sterba struct btrfs_block_group, 4616c79a1751SLiu Bo dirty_list); 4617c79a1751SLiu Bo 4618c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4619c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4620c79a1751SLiu Bo list_del_init(&cache->io_list); 4621c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4622c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4623c79a1751SLiu Bo } 4624c79a1751SLiu Bo 4625c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4626c79a1751SLiu Bo spin_lock(&cache->lock); 4627c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4628c79a1751SLiu Bo spin_unlock(&cache->lock); 4629c79a1751SLiu Bo 4630c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4631c79a1751SLiu Bo btrfs_put_block_group(cache); 4632ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4633c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4634c79a1751SLiu Bo } 4635c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4636c79a1751SLiu Bo 463745ae2c18SNikolay Borisov /* 463845ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 463945ae2c18SNikolay Borisov * to use it without any locking 464045ae2c18SNikolay Borisov */ 4641c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4642c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 464332da5386SDavid Sterba struct btrfs_block_group, 4644c79a1751SLiu Bo io_list); 4645c79a1751SLiu Bo 4646c79a1751SLiu Bo list_del_init(&cache->io_list); 4647c79a1751SLiu Bo spin_lock(&cache->lock); 4648c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4649c79a1751SLiu Bo spin_unlock(&cache->lock); 4650c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4651c79a1751SLiu Bo } 4652c79a1751SLiu Bo } 4653c79a1751SLiu Bo 465449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 46552ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 465649b25e05SJeff Mahoney { 4657bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4658bbbf7243SNikolay Borisov 46592ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4660c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4661c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4662c79a1751SLiu Bo 4663bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4664bbbf7243SNikolay Borisov post_commit_list) { 4665bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4666bbbf7243SNikolay Borisov } 4667bbbf7243SNikolay Borisov 46682ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 466949b25e05SJeff Mahoney 46704a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 46710b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 467249b25e05SJeff Mahoney 46734a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 46740b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 467549b25e05SJeff Mahoney 4676ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 467767cde344SMiao Xie 46782ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 467949b25e05SJeff Mahoney EXTENT_DIRTY); 4680fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 468149b25e05SJeff Mahoney 46824a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 46834a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 468449b25e05SJeff Mahoney } 468549b25e05SJeff Mahoney 46862ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4687acce952bSliubo { 4688acce952bSliubo struct btrfs_transaction *t; 4689acce952bSliubo 46900b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4691acce952bSliubo 46920b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46930b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46940b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4695724e2315SJosef Bacik struct btrfs_transaction, list); 4696724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 46979b64f57dSElena Reshetova refcount_inc(&t->use_count); 46980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46992ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4700724e2315SJosef Bacik btrfs_put_transaction(t); 47010b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4702724e2315SJosef Bacik continue; 4703724e2315SJosef Bacik } 47040b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4705724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 47060b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4707724e2315SJosef Bacik /* 4708724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4709724e2315SJosef Bacik * handle open currently for this transaction. 4710724e2315SJosef Bacik */ 4711724e2315SJosef Bacik wait_event(t->writer_wait, 4712724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4713724e2315SJosef Bacik } else { 47140b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4715724e2315SJosef Bacik } 47162ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4717724e2315SJosef Bacik 47180b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 47190b246afaSJeff Mahoney if (t == fs_info->running_transaction) 47200b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4721724e2315SJosef Bacik list_del_init(&t->list); 47220b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4723a4abeea4SJosef Bacik 4724724e2315SJosef Bacik btrfs_put_transaction(t); 47252ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 47260b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4727724e2315SJosef Bacik } 47280b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 47290b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4730ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4731ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 47320b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 4733ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 47340b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4735acce952bSliubo 4736acce952bSliubo return 0; 4737acce952bSliubo } 4738