1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6e20d96d6SChris Mason #include <linux/fs.h> 7d98237b3SChris Mason #include <linux/blkdev.h> 80f7d52f4SChris Mason #include <linux/radix-tree.h> 935b7e476SChris Mason #include <linux/writeback.h> 10ce9adaa5SChris Mason #include <linux/workqueue.h> 11a74a4b97SChris Mason #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13784b4e29SChris Mason #include <linux/migrate.h> 147a36ddecSDavid Sterba #include <linux/ratelimit.h> 156463fe58SStefan Behrens #include <linux/uuid.h> 16803b2f54SStefan Behrens #include <linux/semaphore.h> 17540adea3SMasami Hiramatsu #include <linux/error-injection.h> 189678c543SNikolay Borisov #include <linux/crc32c.h> 19b89f6d1fSFilipe Manana #include <linux/sched/mm.h> 207e75bf3fSDavid Sterba #include <asm/unaligned.h> 216d97c6e3SJohannes Thumshirn #include <crypto/hash.h> 22eb60ceacSChris Mason #include "ctree.h" 23eb60ceacSChris Mason #include "disk-io.h" 24e089f05cSChris Mason #include "transaction.h" 250f7d52f4SChris Mason #include "btrfs_inode.h" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 32581bb050SLi Zefan #include "inode-map.h" 3321adbd5cSStefan Behrens #include "check-integrity.h" 34606686eeSJosef Bacik #include "rcu-string.h" 358dabb742SStefan Behrens #include "dev-replace.h" 3653b381b3SDavid Woodhouse #include "raid56.h" 375ac1d209SJeff Mahoney #include "sysfs.h" 38fcebe456SJosef Bacik #include "qgroup.h" 39ebb8765bSAnand Jain #include "compression.h" 40557ea5ddSQu Wenruo #include "tree-checker.h" 41fd708b81SJosef Bacik #include "ref-verify.h" 42aac0023cSJosef Bacik #include "block-group.h" 43b0643e59SDennis Zhou #include "discard.h" 44f603bb94SNikolay Borisov #include "space-info.h" 45eb60ceacSChris Mason 46319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 47319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 48319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 50e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 52319e4d06SQu Wenruo 538b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 54143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 55acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 562ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 57143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 582ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 59acce952bSliubo struct extent_io_tree *dirty_pages, 60acce952bSliubo int mark); 612ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 62acce952bSliubo struct extent_io_tree *pinned_extents); 632ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 642ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 65ce9adaa5SChris Mason 66d352ac68SChris Mason /* 6797eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6897eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 69d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 70d352ac68SChris Mason */ 7197eb6b69SDavid Sterba struct btrfs_end_io_wq { 72ce9adaa5SChris Mason struct bio *bio; 73ce9adaa5SChris Mason bio_end_io_t *end_io; 74ce9adaa5SChris Mason void *private; 75ce9adaa5SChris Mason struct btrfs_fs_info *info; 764e4cbee9SChristoph Hellwig blk_status_t status; 77bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 788b712842SChris Mason struct btrfs_work work; 79ce9adaa5SChris Mason }; 800da5468fSChris Mason 8197eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8297eb6b69SDavid Sterba 8397eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8497eb6b69SDavid Sterba { 8597eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8697eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8797eb6b69SDavid Sterba 0, 88fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 8997eb6b69SDavid Sterba NULL); 9097eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9197eb6b69SDavid Sterba return -ENOMEM; 9297eb6b69SDavid Sterba return 0; 9397eb6b69SDavid Sterba } 9497eb6b69SDavid Sterba 95e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9697eb6b69SDavid Sterba { 9797eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9897eb6b69SDavid Sterba } 9997eb6b69SDavid Sterba 100141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 101141386e1SJosef Bacik { 102141386e1SJosef Bacik if (fs_info->csum_shash) 103141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 104141386e1SJosef Bacik } 105141386e1SJosef Bacik 106d352ac68SChris Mason /* 107d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 108d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 109d352ac68SChris Mason * just before they are sent down the IO stack. 110d352ac68SChris Mason */ 11144b8bd7eSChris Mason struct async_submit_bio { 1128896a08dSQu Wenruo struct inode *inode; 11344b8bd7eSChris Mason struct bio *bio; 114a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11544b8bd7eSChris Mason int mirror_num; 116eaf25d93SChris Mason /* 117eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 118eaf25d93SChris Mason * can't tell us where in the file the bio should go 119eaf25d93SChris Mason */ 120eaf25d93SChris Mason u64 bio_offset; 1218b712842SChris Mason struct btrfs_work work; 1224e4cbee9SChristoph Hellwig blk_status_t status; 12344b8bd7eSChris Mason }; 12444b8bd7eSChris Mason 12585d4e461SChris Mason /* 12685d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12785d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12885d4e461SChris Mason * the level the eb occupies in the tree. 1294008c04aSChris Mason * 13085d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13185d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13285d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1334fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1344fd786e6SMisono Tomohiro * roots have separate keysets. 1354008c04aSChris Mason * 13685d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13785d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13885d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1394008c04aSChris Mason * 14085d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14185d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14285d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14385d4e461SChris Mason * 14485d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14585d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14685d4e461SChris Mason * needs update as well. 1474008c04aSChris Mason */ 1484008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1494008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1504008c04aSChris Mason # error 1514008c04aSChris Mason # endif 15285d4e461SChris Mason 153ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 154ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 155ab1405aaSDavid Sterba 156ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 157ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 163ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 164ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 165ab1405aaSDavid Sterba 16685d4e461SChris Mason static struct btrfs_lockdep_keyset { 16785d4e461SChris Mason u64 id; /* root objectid */ 168ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 169387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 170387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 17185d4e461SChris Mason } btrfs_lockdep_keysets[] = { 172ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 173ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 174ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 175ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 176ab1405aaSDavid Sterba { .id = BTRFS_FS_TREE_OBJECTID, DEFINE_NAME("fs") }, 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; 215*a26663e7SQu 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); 222*a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 223e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 224*a26663e7SQu 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 /* 447d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 448d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 449d352ac68SChris Mason */ 450d397712bSChris Mason 45101d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 45219c00ddcSChris Mason { 4534eee4fa4SMiao Xie u64 start = page_offset(page); 45419c00ddcSChris Mason u64 found_start; 4552996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 45619c00ddcSChris Mason struct extent_buffer *eb; 4578d47a0d8SQu Wenruo int ret; 458f188591eSChris Mason 4594f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4604f2de97aSJosef Bacik if (page != eb->pages[0]) 4614f2de97aSJosef Bacik return 0; 4620f805531SAlex Lyakas 46319c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 4640f805531SAlex Lyakas /* 4650f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4660f805531SAlex Lyakas * It is useful to know what went wrong. 4670f805531SAlex Lyakas */ 4680f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4690f805531SAlex Lyakas return -EUCLEAN; 4700f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4710f805531SAlex Lyakas return -EUCLEAN; 4720f805531SAlex Lyakas 473de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 4749a8658e3SDavid Sterba offsetof(struct btrfs_header, fsid), 4759a8658e3SDavid Sterba BTRFS_FSID_SIZE) == 0); 4760f805531SAlex Lyakas 477c67b3892SDavid Sterba csum_tree_block(eb, result); 4782996e1f8SJohannes Thumshirn 4798d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 4808d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 4818d47a0d8SQu Wenruo else 4828d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 4838d47a0d8SQu Wenruo 4848d47a0d8SQu Wenruo if (ret < 0) { 485c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 4868d47a0d8SQu Wenruo btrfs_err(fs_info, 4878d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 4888d47a0d8SQu Wenruo eb->start); 489c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4908d47a0d8SQu Wenruo return ret; 4918d47a0d8SQu Wenruo } 492713cebfbSDavid Sterba write_extent_buffer(eb, result, 0, fs_info->csum_size); 4938d47a0d8SQu Wenruo 4942996e1f8SJohannes Thumshirn return 0; 49519c00ddcSChris Mason } 49619c00ddcSChris Mason 497b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 4982b82032cSYan Zheng { 499b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 500944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 50144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 502944d3f9fSNikolay Borisov u8 *metadata_uuid; 5032b82032cSYan Zheng 5049a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5059a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5067239ff4bSNikolay Borisov /* 507944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 508944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 509944d3f9fSNikolay Borisov * ->fsid in this case is fine 5107239ff4bSNikolay Borisov */ 511944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5127239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5137239ff4bSNikolay Borisov else 5147239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5157239ff4bSNikolay Borisov 516944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 517944d3f9fSNikolay Borisov return 0; 518944d3f9fSNikolay Borisov 519944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 520944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 521944d3f9fSNikolay Borisov return 0; 522944d3f9fSNikolay Borisov 523944d3f9fSNikolay Borisov return 1; 5242b82032cSYan Zheng } 5252b82032cSYan Zheng 5269a446d6aSNikolay Borisov int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, u64 phy_offset, 5279a446d6aSNikolay Borisov struct page *page, u64 start, u64 end, 5289a446d6aSNikolay Borisov int mirror) 529ce9adaa5SChris Mason { 530ce9adaa5SChris Mason u64 found_start; 531ce9adaa5SChris Mason int found_level; 532ce9adaa5SChris Mason struct extent_buffer *eb; 53315b6e8a8SDavid Sterba struct btrfs_fs_info *fs_info; 53415b6e8a8SDavid Sterba u16 csum_size; 535f188591eSChris Mason int ret = 0; 5362996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 537727011e0SChris Mason int reads_done; 538ce9adaa5SChris Mason 539ce9adaa5SChris Mason if (!page->private) 540ce9adaa5SChris Mason goto out; 541d397712bSChris Mason 5424f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 54315b6e8a8SDavid Sterba fs_info = eb->fs_info; 54455fc29beSDavid Sterba csum_size = fs_info->csum_size; 545d397712bSChris Mason 5460b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5470b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5480b32f4bbSJosef Bacik */ 54967439dadSDavid Sterba atomic_inc(&eb->refs); 5500b32f4bbSJosef Bacik 5510b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 552727011e0SChris Mason if (!reads_done) 553727011e0SChris Mason goto err; 554f188591eSChris Mason 5555cf1ab56SJosef Bacik eb->read_mirror = mirror; 556656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 557ea466794SJosef Bacik ret = -EIO; 558ea466794SJosef Bacik goto err; 559ea466794SJosef Bacik } 560ea466794SJosef Bacik 561ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 562727011e0SChris Mason if (found_start != eb->start) { 563893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 564893bf4b1SSu Yue eb->start, found_start); 565f188591eSChris Mason ret = -EIO; 566ce9adaa5SChris Mason goto err; 567ce9adaa5SChris Mason } 568b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 56902873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 57094647322SDavid Sterba eb->start); 5711259ab75SChris Mason ret = -EIO; 5721259ab75SChris Mason goto err; 5731259ab75SChris Mason } 574ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5751c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 576893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 577893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5781c24c3ceSJosef Bacik ret = -EIO; 5791c24c3ceSJosef Bacik goto err; 5801c24c3ceSJosef Bacik } 581ce9adaa5SChris Mason 58285d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 58385d4e461SChris Mason eb, found_level); 5844008c04aSChris Mason 585c67b3892SDavid Sterba csum_tree_block(eb, result); 586a826d6dcSJosef Bacik 5872996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 58835be8851SJohannes Thumshirn u8 val[BTRFS_CSUM_SIZE] = { 0 }; 5892996e1f8SJohannes Thumshirn 5902996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 5912996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 59235be8851SJohannes Thumshirn "%s checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5932996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 59435be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, val), 59535be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 59635be8851SJohannes Thumshirn btrfs_header_level(eb)); 5972996e1f8SJohannes Thumshirn ret = -EUCLEAN; 5982996e1f8SJohannes Thumshirn goto err; 5992996e1f8SJohannes Thumshirn } 6002996e1f8SJohannes Thumshirn 601a826d6dcSJosef Bacik /* 602a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 603a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 604a826d6dcSJosef Bacik * return -EIO. 605a826d6dcSJosef Bacik */ 6061c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 607a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 608a826d6dcSJosef Bacik ret = -EIO; 609a826d6dcSJosef Bacik } 610ce9adaa5SChris Mason 611813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 612053ab70fSLiu Bo ret = -EIO; 613053ab70fSLiu Bo 6140b32f4bbSJosef Bacik if (!ret) 6150b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 61675391f0dSQu Wenruo else 61775391f0dSQu Wenruo btrfs_err(fs_info, 61875391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 61975391f0dSQu Wenruo eb->start); 620ce9adaa5SChris Mason err: 62179fb65a1SJosef Bacik if (reads_done && 62279fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 623d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6244bb31e92SArne Jansen 62553b381b3SDavid Woodhouse if (ret) { 62653b381b3SDavid Woodhouse /* 62753b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 62853b381b3SDavid Woodhouse * again, we have to make sure it has something 62953b381b3SDavid Woodhouse * to decrement 63053b381b3SDavid Woodhouse */ 63153b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6320b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 63353b381b3SDavid Woodhouse } 6340b32f4bbSJosef Bacik free_extent_buffer(eb); 635ce9adaa5SChris Mason out: 636f188591eSChris Mason return ret; 637ce9adaa5SChris Mason } 638ce9adaa5SChris Mason 6394246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 640ce9adaa5SChris Mason { 64197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 642ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6439e0af237SLiu Bo struct btrfs_workqueue *wq; 644ce9adaa5SChris Mason 645ce9adaa5SChris Mason fs_info = end_io_wq->info; 6464e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 647d20f7043SChris Mason 64837226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 649a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 6509e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 651a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 6529e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 653a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 6549e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 655a0cac0ecSOmar Sandoval else 6569e0af237SLiu Bo wq = fs_info->endio_write_workers; 6579e0af237SLiu Bo } else { 6585c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 6599e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 660a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 6619e0af237SLiu Bo wq = fs_info->endio_meta_workers; 662a0cac0ecSOmar Sandoval else 6639e0af237SLiu Bo wq = fs_info->endio_workers; 6649e0af237SLiu Bo } 6659e0af237SLiu Bo 666a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 6679e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 668ce9adaa5SChris Mason } 669ce9adaa5SChris Mason 6704e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 671bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 6720b86a832SChris Mason { 67397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 6748b110e39SMiao Xie 67597eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 676ce9adaa5SChris Mason if (!end_io_wq) 6774e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 678ce9adaa5SChris Mason 679ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 680ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 68122c59948SChris Mason end_io_wq->info = info; 6824e4cbee9SChristoph Hellwig end_io_wq->status = 0; 683ce9adaa5SChris Mason end_io_wq->bio = bio; 68422c59948SChris Mason end_io_wq->metadata = metadata; 685ce9adaa5SChris Mason 686ce9adaa5SChris Mason bio->bi_private = end_io_wq; 687ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 68822c59948SChris Mason return 0; 68922c59948SChris Mason } 69022c59948SChris Mason 6914a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 6924a69a410SChris Mason { 6934a69a410SChris Mason struct async_submit_bio *async; 6944e4cbee9SChristoph Hellwig blk_status_t ret; 6954a69a410SChris Mason 6964a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 6978896a08dSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, async->bio_offset); 69879787eaaSJeff Mahoney if (ret) 6994e4cbee9SChristoph Hellwig async->status = ret; 7004a69a410SChris Mason } 7014a69a410SChris Mason 70206ea01b1SDavid Sterba /* 70306ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 70406ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 70506ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 70606ea01b1SDavid Sterba * 70706ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 70806ea01b1SDavid Sterba * inserted into the tree. 70906ea01b1SDavid Sterba */ 7104a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7118b712842SChris Mason { 7128b712842SChris Mason struct async_submit_bio *async; 71306ea01b1SDavid Sterba struct inode *inode; 71406ea01b1SDavid Sterba blk_status_t ret; 7158b712842SChris Mason 7168b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7178896a08dSQu Wenruo inode = async->inode; 7184854ddd0SChris Mason 719bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7204e4cbee9SChristoph Hellwig if (async->status) { 7214e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7224246a0b6SChristoph Hellwig bio_endio(async->bio); 72379787eaaSJeff Mahoney return; 72479787eaaSJeff Mahoney } 72579787eaaSJeff Mahoney 726ec39f769SChris Mason /* 727ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 728ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 729ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 730ec39f769SChris Mason */ 731ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 73208635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 73306ea01b1SDavid Sterba if (ret) { 73406ea01b1SDavid Sterba async->bio->bi_status = ret; 73506ea01b1SDavid Sterba bio_endio(async->bio); 73606ea01b1SDavid Sterba } 7374a69a410SChris Mason } 7384a69a410SChris Mason 7394a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7404a69a410SChris Mason { 7414a69a410SChris Mason struct async_submit_bio *async; 7424a69a410SChris Mason 7434a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7448b712842SChris Mason kfree(async); 7458b712842SChris Mason } 7468b712842SChris Mason 7478896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 748c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 7498896a08dSQu Wenruo u64 bio_offset, 750e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 75144b8bd7eSChris Mason { 7528896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 75344b8bd7eSChris Mason struct async_submit_bio *async; 75444b8bd7eSChris Mason 75544b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 75644b8bd7eSChris Mason if (!async) 7574e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 75844b8bd7eSChris Mason 7598896a08dSQu Wenruo async->inode = inode; 76044b8bd7eSChris Mason async->bio = bio; 76144b8bd7eSChris Mason async->mirror_num = mirror_num; 7624a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7634a69a410SChris Mason 764a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 765a0cac0ecSOmar Sandoval run_one_async_free); 7664a69a410SChris Mason 767eaf25d93SChris Mason async->bio_offset = bio_offset; 7688c8bee1dSChris Mason 7694e4cbee9SChristoph Hellwig async->status = 0; 77079787eaaSJeff Mahoney 77167f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 7725cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 773d313d7a3SChris Mason 7745cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 77544b8bd7eSChris Mason return 0; 77644b8bd7eSChris Mason } 77744b8bd7eSChris Mason 7784e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 779ce3ed71aSChris Mason { 7802c30c71bSKent Overstreet struct bio_vec *bvec; 781ce3ed71aSChris Mason struct btrfs_root *root; 7822b070cfeSChristoph Hellwig int ret = 0; 7836dc4f100SMing Lei struct bvec_iter_all iter_all; 784ce3ed71aSChris Mason 785c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 7862b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 787ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 78801d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 78979787eaaSJeff Mahoney if (ret) 79079787eaaSJeff Mahoney break; 791ce3ed71aSChris Mason } 7922c30c71bSKent Overstreet 7934e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 794ce3ed71aSChris Mason } 795ce3ed71aSChris Mason 7968896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 797eaf25d93SChris Mason u64 bio_offset) 79822c59948SChris Mason { 7998b712842SChris Mason /* 8008b712842SChris Mason * when we're called for a write, we're already in the async 8015443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8028b712842SChris Mason */ 80379787eaaSJeff Mahoney return btree_csum_one_bio(bio); 80422c59948SChris Mason } 80522c59948SChris Mason 8069b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8079b4e675aSDavid Sterba struct btrfs_inode *bi) 808de0022b9SJosef Bacik { 8096300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8106300463bSLiu Bo return 0; 8119b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 812de0022b9SJosef Bacik return 0; 813de0022b9SJosef Bacik return 1; 814de0022b9SJosef Bacik } 815de0022b9SJosef Bacik 8161b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 8171b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 81844b8bd7eSChris Mason { 8190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8209b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8214e4cbee9SChristoph Hellwig blk_status_t ret; 822cad321adSChris Mason 82337226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 824cad321adSChris Mason /* 825cad321adSChris Mason * called for a read, do the setup so that checksum validation 826cad321adSChris Mason * can happen in the async kernel threads 827cad321adSChris Mason */ 8280b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8290b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8301d4284bdSChris Mason if (ret) 83161891923SStefan Behrens goto out_w_error; 83208635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 833de0022b9SJosef Bacik } else if (!async) { 834de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 835de0022b9SJosef Bacik if (ret) 83661891923SStefan Behrens goto out_w_error; 83708635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 83861891923SStefan Behrens } else { 839cad321adSChris Mason /* 84061891923SStefan Behrens * kthread helpers are used to submit writes so that 84161891923SStefan Behrens * checksumming can happen in parallel across all CPUs 842cad321adSChris Mason */ 8438896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 8448896a08dSQu Wenruo 0, btree_submit_bio_start); 84544b8bd7eSChris Mason } 84644b8bd7eSChris Mason 8474246a0b6SChristoph Hellwig if (ret) 8484246a0b6SChristoph Hellwig goto out_w_error; 8494246a0b6SChristoph Hellwig return 0; 8504246a0b6SChristoph Hellwig 85161891923SStefan Behrens out_w_error: 8524e4cbee9SChristoph Hellwig bio->bi_status = ret; 8534246a0b6SChristoph Hellwig bio_endio(bio); 85461891923SStefan Behrens return ret; 85561891923SStefan Behrens } 85661891923SStefan Behrens 8573dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 858784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 859a6bc32b8SMel Gorman struct page *newpage, struct page *page, 860a6bc32b8SMel Gorman enum migrate_mode mode) 861784b4e29SChris Mason { 862784b4e29SChris Mason /* 863784b4e29SChris Mason * we can't safely write a btree page from here, 864784b4e29SChris Mason * we haven't done the locking hook 865784b4e29SChris Mason */ 866784b4e29SChris Mason if (PageDirty(page)) 867784b4e29SChris Mason return -EAGAIN; 868784b4e29SChris Mason /* 869784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 870784b4e29SChris Mason * We must have no buffers or drop them. 871784b4e29SChris Mason */ 872784b4e29SChris Mason if (page_has_private(page) && 873784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 874784b4e29SChris Mason return -EAGAIN; 875a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 876784b4e29SChris Mason } 8773dd1462eSJan Beulich #endif 878784b4e29SChris Mason 8790da5468fSChris Mason 8800da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8810da5468fSChris Mason struct writeback_control *wbc) 8820da5468fSChris Mason { 883e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 884e2d84521SMiao Xie int ret; 885e2d84521SMiao Xie 886d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 887448d640bSChris Mason 888448d640bSChris Mason if (wbc->for_kupdate) 889448d640bSChris Mason return 0; 890448d640bSChris Mason 891e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 892b9473439SChris Mason /* this is a bit racy, but that's ok */ 893d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 894d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 895d814a491SEthan Lien fs_info->dirty_metadata_batch); 896e2d84521SMiao Xie if (ret < 0) 897793955bcSChris Mason return 0; 898793955bcSChris Mason } 8990b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9000da5468fSChris Mason } 9010da5468fSChris Mason 90270dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9035f39d397SChris Mason { 90498509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 90598509cfcSChris Mason return 0; 9060c4e538bSDavid Sterba 907f7a52a40SDavid Sterba return try_release_extent_buffer(page); 908d98237b3SChris Mason } 909d98237b3SChris Mason 910d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 911d47992f8SLukas Czerner unsigned int length) 912d98237b3SChris Mason { 913d1310b2eSChris Mason struct extent_io_tree *tree; 914d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9155f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9165f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9179ad6b7bcSChris Mason if (PagePrivate(page)) { 918efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 919efe120a0SFrank Holton "page private not zero on page %llu", 920efe120a0SFrank Holton (unsigned long long)page_offset(page)); 921d1b89bc0SGuoqing Jiang detach_page_private(page); 9229ad6b7bcSChris Mason } 923d98237b3SChris Mason } 924d98237b3SChris Mason 9250b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9260b32f4bbSJosef Bacik { 927bb146eb2SJosef Bacik #ifdef DEBUG 9280b32f4bbSJosef Bacik struct extent_buffer *eb; 9290b32f4bbSJosef Bacik 9300b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9310b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9320b32f4bbSJosef Bacik BUG_ON(!eb); 9330b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9340b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9350b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 936bb146eb2SJosef Bacik #endif 9370b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9380b32f4bbSJosef Bacik } 9390b32f4bbSJosef Bacik 9407f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9410da5468fSChris Mason .writepages = btree_writepages, 9425f39d397SChris Mason .releasepage = btree_releasepage, 9435f39d397SChris Mason .invalidatepage = btree_invalidatepage, 9445a92bc88SChris Mason #ifdef CONFIG_MIGRATION 945784b4e29SChris Mason .migratepage = btree_migratepage, 9465a92bc88SChris Mason #endif 9470b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 948d98237b3SChris Mason }; 949123abc88SChris Mason 9502ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 951090d1875SChris Mason { 9525f39d397SChris Mason struct extent_buffer *buf = NULL; 953537f38f0SNikolay Borisov int ret; 954090d1875SChris Mason 9552ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 956c871b0f2SLiu Bo if (IS_ERR(buf)) 9576197d86eSDavid Sterba return; 958537f38f0SNikolay Borisov 959c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(buf, WAIT_NONE, 0); 960537f38f0SNikolay Borisov if (ret < 0) 961537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 962537f38f0SNikolay Borisov else 9635f39d397SChris Mason free_extent_buffer(buf); 964090d1875SChris Mason } 965090d1875SChris Mason 9662ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9672ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9680308af44SDavid Sterba u64 bytenr) 9690999df54SChris Mason { 9700b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9710b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 9720b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 9730999df54SChris Mason } 9740999df54SChris Mason 975581c1760SQu Wenruo /* 976581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 977581c1760SQu Wenruo * verification. 978581c1760SQu Wenruo * 979581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 980581c1760SQu Wenruo * @level: expected level, mandatory check 981581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 982581c1760SQu Wenruo */ 9832ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 984581c1760SQu Wenruo u64 parent_transid, int level, 985581c1760SQu Wenruo struct btrfs_key *first_key) 986e20d96d6SChris Mason { 9875f39d397SChris Mason struct extent_buffer *buf = NULL; 98819c00ddcSChris Mason int ret; 98919c00ddcSChris Mason 9902ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 991c871b0f2SLiu Bo if (IS_ERR(buf)) 992c871b0f2SLiu Bo return buf; 993e4204dedSChris Mason 9945ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 995581c1760SQu Wenruo level, first_key); 9960f0fe8f7SFilipe David Borba Manana if (ret) { 997537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 99864c043deSLiu Bo return ERR_PTR(ret); 9990f0fe8f7SFilipe David Borba Manana } 10005f39d397SChris Mason return buf; 1001ce9adaa5SChris Mason 1002eb60ceacSChris Mason } 1003eb60ceacSChris Mason 10046a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1005ed2ff2cbSChris Mason { 10066a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 100755c69072SChris Mason if (btrfs_header_generation(buf) == 1008e2d84521SMiao Xie fs_info->running_transaction->transid) { 1009b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1010b4ce94deSChris Mason 1011b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1012104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1013e2d84521SMiao Xie -buf->len, 1014e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10150b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1016925baeddSChris Mason } 10175f39d397SChris Mason } 1018ed7b63ebSJosef Bacik } 10195f39d397SChris Mason 1020da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1021e20d96d6SChris Mason u64 objectid) 1022d97e63b6SChris Mason { 10237c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 102496dfcb46SJosef Bacik root->fs_info = fs_info; 1025cfaa7295SChris Mason root->node = NULL; 1026a28ec197SChris Mason root->commit_root = NULL; 102727cdeb70SMiao Xie root->state = 0; 1028d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 10290b86a832SChris Mason 10300f7d52f4SChris Mason root->last_trans = 0; 103113a8a7c8SYan, Zheng root->highest_objectid = 0; 1032eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1033199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10346bef4d31SEric Paris root->inode_tree = RB_ROOT; 103516cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1036f0486c68SYan, Zheng root->block_rsv = NULL; 10370b86a832SChris Mason 10380b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10395d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1040eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1041eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1042199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1043199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1044d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10452ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10462ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10475d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1048eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1049199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1050f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10512ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10522ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10538287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1054a2135011SChris Mason mutex_init(&root->objectid_mutex); 1055e02119d5SChris Mason mutex_init(&root->log_mutex); 105631f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1057573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1058c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10597237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10607237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10617237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 10628b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 10638b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 10647237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 10657237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 10667237f183SYan Zheng atomic_set(&root->log_writers, 0); 10672ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 10680700cea7SElena Reshetova refcount_set(&root->refs, 1); 10698ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1070eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 10717237f183SYan Zheng root->log_transid = 0; 1072d1433debSMiao Xie root->log_transid_committed = -1; 1073257c62e1SChris Mason root->last_log_commit = 0; 1074e289f03eSFilipe Manana if (!dummy) { 107543eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 107643eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1077e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1078e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1079e289f03eSFilipe Manana } 1080017e5369SChris Mason 10813768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 10823768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 10836702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 10844d775673SChris Mason root->root_key.objectid = objectid; 10850ee5dc67SAl Viro root->anon_dev = 0; 10868ea05e3aSAlexander Block 10875f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1088370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1089bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1090bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1091bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1092bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1093bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1094bd647ce3SJosef Bacik #endif 10953768f368SChris Mason } 10963768f368SChris Mason 109774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 109896dfcb46SJosef Bacik u64 objectid, gfp_t flags) 10996f07e42eSAl Viro { 110074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11016f07e42eSAl Viro if (root) 110296dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11036f07e42eSAl Viro return root; 11046f07e42eSAl Viro } 11056f07e42eSAl Viro 110606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 110706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1108da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 110906ea65a3SJosef Bacik { 111006ea65a3SJosef Bacik struct btrfs_root *root; 111106ea65a3SJosef Bacik 11127c0260eeSJeff Mahoney if (!fs_info) 11137c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11147c0260eeSJeff Mahoney 111596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 111606ea65a3SJosef Bacik if (!root) 111706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1118da17066cSJeff Mahoney 1119b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1120faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 112106ea65a3SJosef Bacik 112206ea65a3SJosef Bacik return root; 112306ea65a3SJosef Bacik } 112406ea65a3SJosef Bacik #endif 112506ea65a3SJosef Bacik 112620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 112720897f5cSArne Jansen u64 objectid) 112820897f5cSArne Jansen { 11299b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 113020897f5cSArne Jansen struct extent_buffer *leaf; 113120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 113220897f5cSArne Jansen struct btrfs_root *root; 113320897f5cSArne Jansen struct btrfs_key key; 1134b89f6d1fSFilipe Manana unsigned int nofs_flag; 113520897f5cSArne Jansen int ret = 0; 113620897f5cSArne Jansen 1137b89f6d1fSFilipe Manana /* 1138b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1139b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1140b89f6d1fSFilipe Manana */ 1141b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 114296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1143b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 114420897f5cSArne Jansen if (!root) 114520897f5cSArne Jansen return ERR_PTR(-ENOMEM); 114620897f5cSArne Jansen 114720897f5cSArne Jansen root->root_key.objectid = objectid; 114820897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 114920897f5cSArne Jansen root->root_key.offset = 0; 115020897f5cSArne Jansen 11519631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 11529631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 115320897f5cSArne Jansen if (IS_ERR(leaf)) { 115420897f5cSArne Jansen ret = PTR_ERR(leaf); 11551dd05682STsutomu Itoh leaf = NULL; 115620897f5cSArne Jansen goto fail; 115720897f5cSArne Jansen } 115820897f5cSArne Jansen 115920897f5cSArne Jansen root->node = leaf; 116020897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 116120897f5cSArne Jansen 116220897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 116327cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 116420897f5cSArne Jansen 1165f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1166f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 116720897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 116820897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 116920897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 117020897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 117120897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 117220897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 117320897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 117433d85fdaSQu Wenruo if (is_fstree(objectid)) 1175807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1176807fc790SAndy Shevchenko else 1177807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1178c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 117920897f5cSArne Jansen 118020897f5cSArne Jansen key.objectid = objectid; 118120897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 118220897f5cSArne Jansen key.offset = 0; 118320897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 118420897f5cSArne Jansen if (ret) 118520897f5cSArne Jansen goto fail; 118620897f5cSArne Jansen 118720897f5cSArne Jansen btrfs_tree_unlock(leaf); 118820897f5cSArne Jansen 118920897f5cSArne Jansen return root; 11901dd05682STsutomu Itoh 11911dd05682STsutomu Itoh fail: 11928c38938cSJosef Bacik if (leaf) 11931dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 119400246528SJosef Bacik btrfs_put_root(root); 11951dd05682STsutomu Itoh 11961dd05682STsutomu Itoh return ERR_PTR(ret); 119720897f5cSArne Jansen } 119820897f5cSArne Jansen 11997237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1200e02119d5SChris Mason struct btrfs_fs_info *fs_info) 12010f7d52f4SChris Mason { 12020f7d52f4SChris Mason struct btrfs_root *root; 12037237f183SYan Zheng struct extent_buffer *leaf; 1204e02119d5SChris Mason 120596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1206e02119d5SChris Mason if (!root) 12077237f183SYan Zheng return ERR_PTR(-ENOMEM); 1208e02119d5SChris Mason 1209e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1210e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1211e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 121227cdeb70SMiao Xie 12137237f183SYan Zheng /* 121492a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 121527cdeb70SMiao Xie * 121692a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 121792a7cc42SQu Wenruo * and they go away before a real commit is actually done. 121892a7cc42SQu Wenruo * 121992a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 122092a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 12217237f183SYan Zheng */ 1222e02119d5SChris Mason 12234d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 12249631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 12257237f183SYan Zheng if (IS_ERR(leaf)) { 122600246528SJosef Bacik btrfs_put_root(root); 12277237f183SYan Zheng return ERR_CAST(leaf); 12287237f183SYan Zheng } 1229e02119d5SChris Mason 12307237f183SYan Zheng root->node = leaf; 1231e02119d5SChris Mason 1232e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1233e02119d5SChris Mason btrfs_tree_unlock(root->node); 12347237f183SYan Zheng return root; 12357237f183SYan Zheng } 12367237f183SYan Zheng 12377237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 12387237f183SYan Zheng struct btrfs_fs_info *fs_info) 12397237f183SYan Zheng { 12407237f183SYan Zheng struct btrfs_root *log_root; 12417237f183SYan Zheng 12427237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 12437237f183SYan Zheng if (IS_ERR(log_root)) 12447237f183SYan Zheng return PTR_ERR(log_root); 12457237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 12467237f183SYan Zheng fs_info->log_root_tree = log_root; 12477237f183SYan Zheng return 0; 12487237f183SYan Zheng } 12497237f183SYan Zheng 12507237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 12517237f183SYan Zheng struct btrfs_root *root) 12527237f183SYan Zheng { 12530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12547237f183SYan Zheng struct btrfs_root *log_root; 12557237f183SYan Zheng struct btrfs_inode_item *inode_item; 12567237f183SYan Zheng 12570b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 12587237f183SYan Zheng if (IS_ERR(log_root)) 12597237f183SYan Zheng return PTR_ERR(log_root); 12607237f183SYan Zheng 12617237f183SYan Zheng log_root->last_trans = trans->transid; 12627237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 12637237f183SYan Zheng 12647237f183SYan Zheng inode_item = &log_root->root_item.inode; 12653cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 12663cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 12673cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1268da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 12690b246afaSJeff Mahoney fs_info->nodesize); 12703cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 12717237f183SYan Zheng 12725d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 12737237f183SYan Zheng 12747237f183SYan Zheng WARN_ON(root->log_root); 12757237f183SYan Zheng root->log_root = log_root; 12767237f183SYan Zheng root->log_transid = 0; 1277d1433debSMiao Xie root->log_transid_committed = -1; 1278257c62e1SChris Mason root->last_log_commit = 0; 1279e02119d5SChris Mason return 0; 1280e02119d5SChris Mason } 1281e02119d5SChris Mason 128249d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 128349d11beaSJosef Bacik struct btrfs_path *path, 1284cb517eabSMiao Xie struct btrfs_key *key) 1285e02119d5SChris Mason { 1286e02119d5SChris Mason struct btrfs_root *root; 1287e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 128884234f3aSYan Zheng u64 generation; 1289cb517eabSMiao Xie int ret; 1290581c1760SQu Wenruo int level; 1291cb517eabSMiao Xie 129296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 129349d11beaSJosef Bacik if (!root) 129449d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 12950f7d52f4SChris Mason 1296cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1297cb517eabSMiao Xie &root->root_item, &root->root_key); 12980f7d52f4SChris Mason if (ret) { 129913a8a7c8SYan, Zheng if (ret > 0) 130013a8a7c8SYan, Zheng ret = -ENOENT; 130149d11beaSJosef Bacik goto fail; 13020f7d52f4SChris Mason } 130313a8a7c8SYan, Zheng 130484234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1305581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 13062ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 13072ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1308581c1760SQu Wenruo generation, level, NULL); 130964c043deSLiu Bo if (IS_ERR(root->node)) { 131064c043deSLiu Bo ret = PTR_ERR(root->node); 13118c38938cSJosef Bacik root->node = NULL; 131249d11beaSJosef Bacik goto fail; 1313cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1314cb517eabSMiao Xie ret = -EIO; 131549d11beaSJosef Bacik goto fail; 1316416bc658SJosef Bacik } 13175d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1318cb517eabSMiao Xie return root; 131949d11beaSJosef Bacik fail: 132000246528SJosef Bacik btrfs_put_root(root); 132149d11beaSJosef Bacik return ERR_PTR(ret); 132249d11beaSJosef Bacik } 132349d11beaSJosef Bacik 132449d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 132549d11beaSJosef Bacik struct btrfs_key *key) 132649d11beaSJosef Bacik { 132749d11beaSJosef Bacik struct btrfs_root *root; 132849d11beaSJosef Bacik struct btrfs_path *path; 132949d11beaSJosef Bacik 133049d11beaSJosef Bacik path = btrfs_alloc_path(); 133149d11beaSJosef Bacik if (!path) 133249d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 133349d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 133449d11beaSJosef Bacik btrfs_free_path(path); 133549d11beaSJosef Bacik 133649d11beaSJosef Bacik return root; 1337cb517eabSMiao Xie } 1338cb517eabSMiao Xie 13392dfb1e43SQu Wenruo /* 13402dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 13412dfb1e43SQu Wenruo * 13422dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 13432dfb1e43SQu Wenruo */ 13442dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1345cb517eabSMiao Xie { 1346cb517eabSMiao Xie int ret; 1347dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1348cb517eabSMiao Xie 1349cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1350cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1351cb517eabSMiao Xie GFP_NOFS); 1352cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1353cb517eabSMiao Xie ret = -ENOMEM; 1354cb517eabSMiao Xie goto fail; 1355cb517eabSMiao Xie } 1356cb517eabSMiao Xie 1357dcc3eb96SNikolay Borisov /* 1358dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1359dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1360dcc3eb96SNikolay Borisov */ 1361dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1362dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1363dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1364dcc3eb96SNikolay Borisov if (ret) 13658257b2dcSMiao Xie goto fail; 13668257b2dcSMiao Xie 1367aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 1368aeb935a4SQu Wenruo root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { 136992a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1370f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1371f39e4571SJosef Bacik } 1372f39e4571SJosef Bacik 1373cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 137457cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 137557cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1376cb517eabSMiao Xie 1377851fd730SQu Wenruo /* 1378851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1379851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1380851fd730SQu Wenruo */ 1381851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1382851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 13832dfb1e43SQu Wenruo if (!anon_dev) { 1384cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1385cb517eabSMiao Xie if (ret) 1386876d2cf1SLiu Bo goto fail; 13872dfb1e43SQu Wenruo } else { 13882dfb1e43SQu Wenruo root->anon_dev = anon_dev; 13892dfb1e43SQu Wenruo } 1390851fd730SQu Wenruo } 1391f32e48e9SChandan Rajendra 1392f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1393f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1394f32e48e9SChandan Rajendra &root->highest_objectid); 1395f32e48e9SChandan Rajendra if (ret) { 1396f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1397876d2cf1SLiu Bo goto fail; 1398f32e48e9SChandan Rajendra } 1399f32e48e9SChandan Rajendra 1400f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1401f32e48e9SChandan Rajendra 1402f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1403f32e48e9SChandan Rajendra 1404cb517eabSMiao Xie return 0; 1405cb517eabSMiao Xie fail: 140684db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1407cb517eabSMiao Xie return ret; 1408cb517eabSMiao Xie } 1409cb517eabSMiao Xie 1410a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1411cb517eabSMiao Xie u64 root_id) 1412cb517eabSMiao Xie { 1413cb517eabSMiao Xie struct btrfs_root *root; 1414cb517eabSMiao Xie 1415cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1416cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1417cb517eabSMiao Xie (unsigned long)root_id); 1418bc44d7c4SJosef Bacik if (root) 141900246528SJosef Bacik root = btrfs_grab_root(root); 1420cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1421cb517eabSMiao Xie return root; 1422cb517eabSMiao Xie } 1423cb517eabSMiao Xie 142449d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 142549d11beaSJosef Bacik u64 objectid) 142649d11beaSJosef Bacik { 142749d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 142849d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 142949d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 143049d11beaSJosef Bacik return btrfs_grab_root(fs_info->extent_root); 143149d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 143249d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 143349d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 143449d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 143549d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 143649d11beaSJosef Bacik return btrfs_grab_root(fs_info->csum_root); 143749d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 143849d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 143949d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 144049d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 144149d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 144249d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 144349d11beaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 144449d11beaSJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 144549d11beaSJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 144649d11beaSJosef Bacik return NULL; 144749d11beaSJosef Bacik } 144849d11beaSJosef Bacik 1449cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1450cb517eabSMiao Xie struct btrfs_root *root) 1451cb517eabSMiao Xie { 1452cb517eabSMiao Xie int ret; 1453cb517eabSMiao Xie 1454e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1455cb517eabSMiao Xie if (ret) 1456cb517eabSMiao Xie return ret; 1457cb517eabSMiao Xie 1458cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1459cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1460cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1461cb517eabSMiao Xie root); 1462af01d2e5SJosef Bacik if (ret == 0) { 146300246528SJosef Bacik btrfs_grab_root(root); 146427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1465af01d2e5SJosef Bacik } 1466cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1467cb517eabSMiao Xie radix_tree_preload_end(); 1468cb517eabSMiao Xie 1469cb517eabSMiao Xie return ret; 1470cb517eabSMiao Xie } 1471cb517eabSMiao Xie 1472bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1473bd647ce3SJosef Bacik { 1474bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1475bd647ce3SJosef Bacik struct btrfs_root *root; 1476bd647ce3SJosef Bacik 1477bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1478457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1479457f1864SJosef Bacik 1480bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1481bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1482457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 1483457f1864SJosef Bacik btrfs_root_name(root->root_key.objectid, buf), 1484bd647ce3SJosef Bacik refcount_read(&root->refs)); 1485bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 148600246528SJosef Bacik btrfs_put_root(root); 148700246528SJosef Bacik btrfs_put_root(root); 1488bd647ce3SJosef Bacik } 1489bd647ce3SJosef Bacik #endif 1490bd647ce3SJosef Bacik } 1491bd647ce3SJosef Bacik 14920d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 14930d4b0463SJosef Bacik { 1494141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1495141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 1496141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dio_bytes); 1497141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1498141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1499141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1500141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15010d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15020d4b0463SJosef Bacik kfree(fs_info->delayed_root); 150300246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 150400246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 150500246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 150600246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 150700246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 150800246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 150900246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 151000246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 151100246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1512aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 1513bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15143fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 15150d4b0463SJosef Bacik kfree(fs_info->super_copy); 15160d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15170d4b0463SJosef Bacik kvfree(fs_info); 15180d4b0463SJosef Bacik } 15190d4b0463SJosef Bacik 15200d4b0463SJosef Bacik 15212dfb1e43SQu Wenruo /* 15222dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 15232dfb1e43SQu Wenruo * 15242dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 15252dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 15262dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 15272dfb1e43SQu Wenruo * 15282dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 15292dfb1e43SQu Wenruo * 15302dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 15312dfb1e43SQu Wenruo * same root objectid. 15322dfb1e43SQu Wenruo * 15332dfb1e43SQu Wenruo * @objectid: root id 15342dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 15352dfb1e43SQu Wenruo * pass 0 for new allocation. 15362dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 15372dfb1e43SQu Wenruo * for orphan roots 15382dfb1e43SQu Wenruo */ 15392dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 15402dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 15412dfb1e43SQu Wenruo bool check_ref) 15425eda7b5eSChris Mason { 15435eda7b5eSChris Mason struct btrfs_root *root; 1544381cf658SDavid Sterba struct btrfs_path *path; 15451d4c08e0SDavid Sterba struct btrfs_key key; 15465eda7b5eSChris Mason int ret; 15475eda7b5eSChris Mason 154849d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 154949d11beaSJosef Bacik if (root) 155049d11beaSJosef Bacik return root; 15514df27c4dSYan, Zheng again: 155256e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 155348475471SStefan Behrens if (root) { 15542dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 15552dfb1e43SQu Wenruo ASSERT(!anon_dev); 1556bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 155700246528SJosef Bacik btrfs_put_root(root); 155848475471SStefan Behrens return ERR_PTR(-ENOENT); 1559bc44d7c4SJosef Bacik } 15605eda7b5eSChris Mason return root; 156148475471SStefan Behrens } 15625eda7b5eSChris Mason 156356e9357aSDavid Sterba key.objectid = objectid; 156456e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 156556e9357aSDavid Sterba key.offset = (u64)-1; 156656e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 15675eda7b5eSChris Mason if (IS_ERR(root)) 15685eda7b5eSChris Mason return root; 15693394e160SChris Mason 1570c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1571d68fc57bSYan, Zheng ret = -ENOENT; 1572d68fc57bSYan, Zheng goto fail; 1573d68fc57bSYan, Zheng } 1574d68fc57bSYan, Zheng 15752dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1576cb517eabSMiao Xie if (ret) 1577cb517eabSMiao Xie goto fail; 1578cb517eabSMiao Xie 1579381cf658SDavid Sterba path = btrfs_alloc_path(); 1580381cf658SDavid Sterba if (!path) { 1581381cf658SDavid Sterba ret = -ENOMEM; 1582381cf658SDavid Sterba goto fail; 1583381cf658SDavid Sterba } 15841d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 15851d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 158656e9357aSDavid Sterba key.offset = objectid; 15871d4c08e0SDavid Sterba 15881d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1589381cf658SDavid Sterba btrfs_free_path(path); 1590d68fc57bSYan, Zheng if (ret < 0) 1591d68fc57bSYan, Zheng goto fail; 1592d68fc57bSYan, Zheng if (ret == 0) 159327cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1594d68fc57bSYan, Zheng 1595cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 15960f7d52f4SChris Mason if (ret) { 159700246528SJosef Bacik btrfs_put_root(root); 15984785e24fSJosef Bacik if (ret == -EEXIST) 15994df27c4dSYan, Zheng goto again; 16004df27c4dSYan, Zheng goto fail; 16014df27c4dSYan, Zheng } 1602edbd8d4eSChris Mason return root; 16034df27c4dSYan, Zheng fail: 16048c38938cSJosef Bacik btrfs_put_root(root); 16054df27c4dSYan, Zheng return ERR_PTR(ret); 1606edbd8d4eSChris Mason } 1607edbd8d4eSChris Mason 16082dfb1e43SQu Wenruo /* 16092dfb1e43SQu Wenruo * Get in-memory reference of a root structure 16102dfb1e43SQu Wenruo * 16112dfb1e43SQu Wenruo * @objectid: tree objectid 16122dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 16132dfb1e43SQu Wenruo * one reference 16142dfb1e43SQu Wenruo */ 16152dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 16162dfb1e43SQu Wenruo u64 objectid, bool check_ref) 16172dfb1e43SQu Wenruo { 16182dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 16192dfb1e43SQu Wenruo } 16202dfb1e43SQu Wenruo 16212dfb1e43SQu Wenruo /* 16222dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 16232dfb1e43SQu Wenruo * the anonymous block device id 16242dfb1e43SQu Wenruo * 16252dfb1e43SQu Wenruo * @objectid: tree objectid 16262dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 16272dfb1e43SQu Wenruo * parameter value 16282dfb1e43SQu Wenruo */ 16292dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 16302dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 16312dfb1e43SQu Wenruo { 16322dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 16332dfb1e43SQu Wenruo } 16342dfb1e43SQu Wenruo 16358b712842SChris Mason /* 163649d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 163749d11beaSJosef Bacik * @fs_info: the fs_info 163849d11beaSJosef Bacik * @objectid: the objectid we need to lookup 163949d11beaSJosef Bacik * 164049d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 164149d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 164249d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 164349d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 164449d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 164549d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 164649d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 164749d11beaSJosef Bacik * backref code is finished using the root. 164849d11beaSJosef Bacik */ 164949d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 165049d11beaSJosef Bacik struct btrfs_path *path, 165149d11beaSJosef Bacik u64 objectid) 165249d11beaSJosef Bacik { 165349d11beaSJosef Bacik struct btrfs_root *root; 165449d11beaSJosef Bacik struct btrfs_key key; 165549d11beaSJosef Bacik 165649d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 165749d11beaSJosef Bacik 165849d11beaSJosef Bacik /* 165949d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 166049d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 166149d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 166249d11beaSJosef Bacik * != NULL just return it. 166349d11beaSJosef Bacik */ 166449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 166549d11beaSJosef Bacik if (root) 166649d11beaSJosef Bacik return root; 166749d11beaSJosef Bacik 166849d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 166949d11beaSJosef Bacik if (root) 167049d11beaSJosef Bacik return root; 167149d11beaSJosef Bacik 167249d11beaSJosef Bacik key.objectid = objectid; 167349d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 167449d11beaSJosef Bacik key.offset = (u64)-1; 167549d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 167649d11beaSJosef Bacik btrfs_release_path(path); 167749d11beaSJosef Bacik 167849d11beaSJosef Bacik return root; 167949d11beaSJosef Bacik } 168049d11beaSJosef Bacik 168149d11beaSJosef Bacik /* 16828b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16838b712842SChris Mason * functions. This is where read checksum verification actually happens 16848b712842SChris Mason */ 16858b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1686ce9adaa5SChris Mason { 1687ce9adaa5SChris Mason struct bio *bio; 168897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1689ce9adaa5SChris Mason 169097eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1691ce9adaa5SChris Mason bio = end_io_wq->bio; 16928b712842SChris Mason 16934e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1694ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1695ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 16964246a0b6SChristoph Hellwig bio_endio(bio); 16979be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1698ce9adaa5SChris Mason } 169944b8bd7eSChris Mason 1700a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1701a74a4b97SChris Mason { 1702a74a4b97SChris Mason struct btrfs_root *root = arg; 17030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1704d0278245SMiao Xie int again; 1705a74a4b97SChris Mason 1706d6fd0ae2SOmar Sandoval while (1) { 1707d0278245SMiao Xie again = 0; 17089d1a2a3aSDavid Sterba 1709fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1710fd340d0fSJosef Bacik 1711d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17122ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1713d0278245SMiao Xie goto sleep; 1714d0278245SMiao Xie 171590c711abSZygo Blaxell /* 171690c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 171790c711abSZygo Blaxell * before we have finished mounting the filesystem. 171890c711abSZygo Blaxell */ 17190b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 172090c711abSZygo Blaxell goto sleep; 172190c711abSZygo Blaxell 17220b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1723d0278245SMiao Xie goto sleep; 1724d0278245SMiao Xie 1725dc7f370cSMiao Xie /* 1726dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1727dc7f370cSMiao Xie * during the above check and trylock. 1728dc7f370cSMiao Xie */ 17292ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17300b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1731dc7f370cSMiao Xie goto sleep; 1732dc7f370cSMiao Xie } 1733dc7f370cSMiao Xie 17342ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1735c2d6cb16SFilipe Manana 17369d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17370b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1738a74a4b97SChris Mason 1739d0278245SMiao Xie /* 174005323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 174105323cd1SMiao Xie * needn't do anything special here. 1742d0278245SMiao Xie */ 17430b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 174467c5e7d4SFilipe Manana 174567c5e7d4SFilipe Manana /* 174667c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 174767c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 174867c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 174967c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 175067c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 175167c5e7d4SFilipe Manana * unused block groups. 175267c5e7d4SFilipe Manana */ 17530b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1754d0278245SMiao Xie sleep: 1755fd340d0fSJosef Bacik clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1756d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1757d6fd0ae2SOmar Sandoval kthread_parkme(); 1758d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1759d6fd0ae2SOmar Sandoval return 0; 1760838fe188SJiri Kosina if (!again) { 1761a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1762a74a4b97SChris Mason schedule(); 1763a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1764a74a4b97SChris Mason } 1765da288d28SFilipe Manana } 1766a74a4b97SChris Mason } 1767a74a4b97SChris Mason 1768a74a4b97SChris Mason static int transaction_kthread(void *arg) 1769a74a4b97SChris Mason { 1770a74a4b97SChris Mason struct btrfs_root *root = arg; 17710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1772a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1773a74a4b97SChris Mason struct btrfs_transaction *cur; 17748929ecfaSYan, Zheng u64 transid; 1775643900beSNikolay Borisov time64_t delta; 1776a74a4b97SChris Mason unsigned long delay; 1777914b2007SJan Kara bool cannot_commit; 1778a74a4b97SChris Mason 1779a74a4b97SChris Mason do { 1780914b2007SJan Kara cannot_commit = false; 1781ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 17820b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1783a74a4b97SChris Mason 17840b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17850b246afaSJeff Mahoney cur = fs_info->running_transaction; 1786a74a4b97SChris Mason if (!cur) { 17870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1788a74a4b97SChris Mason goto sleep; 1789a74a4b97SChris Mason } 179031153d81SYan Zheng 1791643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 17923296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1793643900beSNikolay Borisov delta < fs_info->commit_interval) { 17940b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1795fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1796fb8a7e94SNikolay Borisov delay = min(delay, 1797fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1798a74a4b97SChris Mason goto sleep; 1799a74a4b97SChris Mason } 18008929ecfaSYan, Zheng transid = cur->transid; 18010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 180256bec294SChris Mason 180379787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1804354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1805914b2007SJan Kara if (IS_ERR(trans)) { 1806354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1807914b2007SJan Kara cannot_commit = true; 180879787eaaSJeff Mahoney goto sleep; 1809914b2007SJan Kara } 18108929ecfaSYan, Zheng if (transid == trans->transid) { 18113a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18128929ecfaSYan, Zheng } else { 18133a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18148929ecfaSYan, Zheng } 1815a74a4b97SChris Mason sleep: 18160b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18170b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1818a74a4b97SChris Mason 18194e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18200b246afaSJeff Mahoney &fs_info->fs_state))) 18212ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18228929ecfaSYan, Zheng if (!kthread_should_stop() && 18230b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1824914b2007SJan Kara cannot_commit)) 1825bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1826a74a4b97SChris Mason } while (!kthread_should_stop()); 1827a74a4b97SChris Mason return 0; 1828a74a4b97SChris Mason } 1829a74a4b97SChris Mason 1830af31f5e5SChris Mason /* 183101f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 183201f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 183301f0f9daSNikolay Borisov * anything. 1834af31f5e5SChris Mason * 1835af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1836af31f5e5SChris Mason * generation of the latest root in the array with the generation 1837af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1838af31f5e5SChris Mason */ 183901f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1840af31f5e5SChris Mason { 184101f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1842af31f5e5SChris Mason u64 cur; 1843af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1844af31f5e5SChris Mason int i; 1845af31f5e5SChris Mason 1846af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1847af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1848af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1849af31f5e5SChris Mason if (cur == newest_gen) 185001f0f9daSNikolay Borisov return i; 1851af31f5e5SChris Mason } 1852af31f5e5SChris Mason 185301f0f9daSNikolay Borisov return -EINVAL; 1854af31f5e5SChris Mason } 1855af31f5e5SChris Mason 1856af31f5e5SChris Mason /* 1857af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1858af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1859af31f5e5SChris Mason * done 1860af31f5e5SChris Mason */ 1861af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1862af31f5e5SChris Mason { 18636ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1864af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1865af31f5e5SChris Mason 1866af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1867af31f5e5SChris Mason 1868af31f5e5SChris Mason /* 1869af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1870af31f5e5SChris Mason * regardless of which ones we use today 1871af31f5e5SChris Mason */ 1872af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1873af31f5e5SChris Mason 1874af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1875af31f5e5SChris Mason 1876af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1877af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1878af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1879af31f5e5SChris Mason 1880af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1881af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1882af31f5e5SChris Mason 1883af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1884af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1885af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1886af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1887af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1890af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1891af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1892af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1893af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1894af31f5e5SChris Mason 18957c7e82a7SChris Mason /* 18967c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18977c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18987c7e82a7SChris Mason */ 18997c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19007c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19017c7e82a7SChris Mason info->fs_root->node->start); 1902af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1903af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1904af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1905af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19067c7e82a7SChris Mason } 1907af31f5e5SChris Mason 1908af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1909af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1910af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1911af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1912af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1913af31f5e5SChris Mason 1914af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1915af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1916af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1917af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1918af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1919af31f5e5SChris Mason 1920af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1921af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1922af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1923af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1924af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1925af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1926af31f5e5SChris Mason 1927af31f5e5SChris Mason /* 1928af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1929af31f5e5SChris Mason * for the next commit 1930af31f5e5SChris Mason */ 1931af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1932af31f5e5SChris Mason &info->super_for_commit->super_roots, 1933af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1934af31f5e5SChris Mason } 1935af31f5e5SChris Mason 1936af31f5e5SChris Mason /* 1937bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1938bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1939bd2336b2SNikolay Borisov * 1940bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1941bd2336b2SNikolay Borisov * priority - priority of backup root required 1942bd2336b2SNikolay Borisov * 1943bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1944bd2336b2SNikolay Borisov */ 1945bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1946bd2336b2SNikolay Borisov { 1947bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1948bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1949bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1950bd2336b2SNikolay Borisov 1951bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1952bd2336b2SNikolay Borisov if (priority == 0) 1953bd2336b2SNikolay Borisov return backup_index; 1954bd2336b2SNikolay Borisov 1955bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1956bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1957bd2336b2SNikolay Borisov } else { 1958bd2336b2SNikolay Borisov return -EINVAL; 1959bd2336b2SNikolay Borisov } 1960bd2336b2SNikolay Borisov 1961bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1962bd2336b2SNikolay Borisov 1963bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1964bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1965bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1966bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 1967bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 1968bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1969bd2336b2SNikolay Borisov 1970bd2336b2SNikolay Borisov /* 1971bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 1972bd2336b2SNikolay Borisov * need a fsck 1973bd2336b2SNikolay Borisov */ 1974bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1975bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1976bd2336b2SNikolay Borisov 1977bd2336b2SNikolay Borisov return backup_index; 1978bd2336b2SNikolay Borisov } 1979bd2336b2SNikolay Borisov 19807abadb64SLiu Bo /* helper to cleanup workers */ 19817abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19827abadb64SLiu Bo { 1983dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1984afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 19855cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 1986fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 1987fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 1988d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 1989fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 1990fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 19915b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 1992e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 1993736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 1994a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 1995fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 1996b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 1997b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 1998a9b9477dSFilipe Manana /* 1999a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2000a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2001a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2002a9b9477dSFilipe Manana */ 2003a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2004a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20057abadb64SLiu Bo } 20067abadb64SLiu Bo 20072e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20082e9f5954SRashika { 20092e9f5954SRashika if (root) { 20102e9f5954SRashika free_extent_buffer(root->node); 20112e9f5954SRashika free_extent_buffer(root->commit_root); 20122e9f5954SRashika root->node = NULL; 20132e9f5954SRashika root->commit_root = NULL; 20142e9f5954SRashika } 20152e9f5954SRashika } 20162e9f5954SRashika 2017af31f5e5SChris Mason /* helper to cleanup tree roots */ 20184273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2019af31f5e5SChris Mason { 20202e9f5954SRashika free_root_extent_buffers(info->tree_root); 2021655b09feSJosef Bacik 20222e9f5954SRashika free_root_extent_buffers(info->dev_root); 20232e9f5954SRashika free_root_extent_buffers(info->extent_root); 20242e9f5954SRashika free_root_extent_buffers(info->csum_root); 20252e9f5954SRashika free_root_extent_buffers(info->quota_root); 20262e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20278c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2028aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 20294273eaffSAnand Jain if (free_chunk_root) 20302e9f5954SRashika free_root_extent_buffers(info->chunk_root); 203170f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2032af31f5e5SChris Mason } 2033af31f5e5SChris Mason 20348c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 20358c38938cSJosef Bacik { 20368c38938cSJosef Bacik if (!root) 20378c38938cSJosef Bacik return; 20388c38938cSJosef Bacik 20398c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 20408c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 20411dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 20428c38938cSJosef Bacik if (root->anon_dev) 20438c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 20448c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2045923eb523SJohannes Thumshirn free_root_extent_buffers(root); 20468c38938cSJosef Bacik kfree(root->free_ino_ctl); 20478c38938cSJosef Bacik kfree(root->free_ino_pinned); 20488c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 20498c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 20508c38938cSJosef Bacik list_del_init(&root->leak_list); 20518c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 20528c38938cSJosef Bacik #endif 20538c38938cSJosef Bacik kfree(root); 20548c38938cSJosef Bacik } 20558c38938cSJosef Bacik } 20568c38938cSJosef Bacik 2057faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2058171f6537SJosef Bacik { 2059171f6537SJosef Bacik int ret; 2060171f6537SJosef Bacik struct btrfs_root *gang[8]; 2061171f6537SJosef Bacik int i; 2062171f6537SJosef Bacik 2063171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2064171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2065171f6537SJosef Bacik struct btrfs_root, root_list); 2066171f6537SJosef Bacik list_del(&gang[0]->root_list); 2067171f6537SJosef Bacik 20688c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2069cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 207000246528SJosef Bacik btrfs_put_root(gang[0]); 2071171f6537SJosef Bacik } 2072171f6537SJosef Bacik 2073171f6537SJosef Bacik while (1) { 2074171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2075171f6537SJosef Bacik (void **)gang, 0, 2076171f6537SJosef Bacik ARRAY_SIZE(gang)); 2077171f6537SJosef Bacik if (!ret) 2078171f6537SJosef Bacik break; 2079171f6537SJosef Bacik for (i = 0; i < ret; i++) 2080cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2081171f6537SJosef Bacik } 2082171f6537SJosef Bacik } 2083af31f5e5SChris Mason 2084638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2085638aa7edSEric Sandeen { 2086638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2087638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2088638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2089638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2090638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2091638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2092ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2093638aa7edSEric Sandeen } 2094638aa7edSEric Sandeen 2095779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2096779a65a4SEric Sandeen { 2097779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2098779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2099779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2100779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2101779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2102779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2103779a65a4SEric Sandeen } 2104779a65a4SEric Sandeen 21056bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2106f37938e0SEric Sandeen { 21072ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21082ff7e61eSJeff Mahoney 21092ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21102ff7e61eSJeff Mahoney set_nlink(inode, 1); 2111f37938e0SEric Sandeen /* 2112f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2113f37938e0SEric Sandeen * the real end of the address space is determined by all of 2114f37938e0SEric Sandeen * the devices in the system 2115f37938e0SEric Sandeen */ 21162ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21172ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2118f37938e0SEric Sandeen 21192ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 212043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 21212c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 21227b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21232ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2124f37938e0SEric Sandeen 21255c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 21262ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21272ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21282ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2129f37938e0SEric Sandeen } 2130f37938e0SEric Sandeen 2131ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2132ad618368SEric Sandeen { 2133ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2134129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21357f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2136ad618368SEric Sandeen } 2137ad618368SEric Sandeen 2138f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2139f9e92e40SEric Sandeen { 2140f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2141f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2142f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2143f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2144f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2145f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2146d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2147f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2148f9e92e40SEric Sandeen } 2149f9e92e40SEric Sandeen 21502a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21512a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21522a458198SEric Sandeen { 2153f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21546f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21552a458198SEric Sandeen 21562a458198SEric Sandeen fs_info->workers = 2157cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2158cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21592a458198SEric Sandeen 21602a458198SEric Sandeen fs_info->delalloc_workers = 2161cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2162cb001095SJeff Mahoney flags, max_active, 2); 21632a458198SEric Sandeen 21642a458198SEric Sandeen fs_info->flush_workers = 2165cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2166cb001095SJeff Mahoney flags, max_active, 0); 21672a458198SEric Sandeen 21682a458198SEric Sandeen fs_info->caching_workers = 2169cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 21702a458198SEric Sandeen 21712a458198SEric Sandeen fs_info->fixup_workers = 2172cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 21732a458198SEric Sandeen 21742a458198SEric Sandeen /* 21752a458198SEric Sandeen * endios are largely parallel and should have a very 21762a458198SEric Sandeen * low idle thresh 21772a458198SEric Sandeen */ 21782a458198SEric Sandeen fs_info->endio_workers = 2179cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 21802a458198SEric Sandeen fs_info->endio_meta_workers = 2181cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2182cb001095SJeff Mahoney max_active, 4); 21832a458198SEric Sandeen fs_info->endio_meta_write_workers = 2184cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2185cb001095SJeff Mahoney max_active, 2); 21862a458198SEric Sandeen fs_info->endio_raid56_workers = 2187cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2188cb001095SJeff Mahoney max_active, 4); 21892a458198SEric Sandeen fs_info->rmw_workers = 2190cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 21912a458198SEric Sandeen fs_info->endio_write_workers = 2192cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2193cb001095SJeff Mahoney max_active, 2); 21942a458198SEric Sandeen fs_info->endio_freespace_worker = 2195cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2196cb001095SJeff Mahoney max_active, 0); 21972a458198SEric Sandeen fs_info->delayed_workers = 2198cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2199cb001095SJeff Mahoney max_active, 0); 22002a458198SEric Sandeen fs_info->readahead_workers = 2201cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2202cb001095SJeff Mahoney max_active, 2); 22032a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2204cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2205b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2206b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 22072a458198SEric Sandeen 22082a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2209ba8a9d07SChris Mason fs_info->flush_workers && 22102a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22112a458198SEric Sandeen fs_info->endio_meta_write_workers && 22122a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22132a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22142a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22152a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2216b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2217b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22182a458198SEric Sandeen return -ENOMEM; 22192a458198SEric Sandeen } 22202a458198SEric Sandeen 22212a458198SEric Sandeen return 0; 22222a458198SEric Sandeen } 22232a458198SEric Sandeen 22246d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22256d97c6e3SJohannes Thumshirn { 22266d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2227b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22286d97c6e3SJohannes Thumshirn 2229b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22306d97c6e3SJohannes Thumshirn 22316d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22326d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2233b4e967beSDavid Sterba csum_driver); 22346d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22356d97c6e3SJohannes Thumshirn } 22366d97c6e3SJohannes Thumshirn 22376d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22386d97c6e3SJohannes Thumshirn 22396d97c6e3SJohannes Thumshirn return 0; 22406d97c6e3SJohannes Thumshirn } 22416d97c6e3SJohannes Thumshirn 224263443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 224363443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 224463443bf5SEric Sandeen { 224563443bf5SEric Sandeen int ret; 224663443bf5SEric Sandeen struct btrfs_root *log_tree_root; 224763443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 224863443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2249581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 225063443bf5SEric Sandeen 225163443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2252f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 225363443bf5SEric Sandeen return -EIO; 225463443bf5SEric Sandeen } 225563443bf5SEric Sandeen 225696dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 225796dfcb46SJosef Bacik GFP_KERNEL); 225863443bf5SEric Sandeen if (!log_tree_root) 225963443bf5SEric Sandeen return -ENOMEM; 226063443bf5SEric Sandeen 22612ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2262581c1760SQu Wenruo fs_info->generation + 1, 2263581c1760SQu Wenruo level, NULL); 226464c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2265f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22660eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 22678c38938cSJosef Bacik log_tree_root->node = NULL; 226800246528SJosef Bacik btrfs_put_root(log_tree_root); 22690eeff236SLiu Bo return ret; 227064c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2271f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 227200246528SJosef Bacik btrfs_put_root(log_tree_root); 227363443bf5SEric Sandeen return -EIO; 227463443bf5SEric Sandeen } 227563443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 227663443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 227763443bf5SEric Sandeen if (ret) { 22780b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 227963443bf5SEric Sandeen "Failed to recover log tree"); 228000246528SJosef Bacik btrfs_put_root(log_tree_root); 228163443bf5SEric Sandeen return ret; 228263443bf5SEric Sandeen } 228363443bf5SEric Sandeen 2284bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 22856bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 228663443bf5SEric Sandeen if (ret) 228763443bf5SEric Sandeen return ret; 228863443bf5SEric Sandeen } 228963443bf5SEric Sandeen 229063443bf5SEric Sandeen return 0; 229163443bf5SEric Sandeen } 229263443bf5SEric Sandeen 22936bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 22944bbcaa64SEric Sandeen { 22956bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2296a4f3d2c4SDavid Sterba struct btrfs_root *root; 22974bbcaa64SEric Sandeen struct btrfs_key location; 22984bbcaa64SEric Sandeen int ret; 22994bbcaa64SEric Sandeen 23006bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23016bccf3abSJeff Mahoney 23024bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23034bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23044bbcaa64SEric Sandeen location.offset = 0; 23054bbcaa64SEric Sandeen 2306a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2307f50f4353SLiu Bo if (IS_ERR(root)) { 230842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2309f50f4353SLiu Bo ret = PTR_ERR(root); 2310f50f4353SLiu Bo goto out; 2311f50f4353SLiu Bo } 231242437a63SJosef Bacik } else { 2313a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2314a4f3d2c4SDavid Sterba fs_info->extent_root = root; 231542437a63SJosef Bacik } 23164bbcaa64SEric Sandeen 23174bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2318a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2319f50f4353SLiu Bo if (IS_ERR(root)) { 232042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2321f50f4353SLiu Bo ret = PTR_ERR(root); 2322f50f4353SLiu Bo goto out; 2323f50f4353SLiu Bo } 232442437a63SJosef Bacik } else { 2325a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2326a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23274bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 232842437a63SJosef Bacik } 23294bbcaa64SEric Sandeen 2330882dbe0cSJosef Bacik /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ 2331882dbe0cSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 23324bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2333a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2334f50f4353SLiu Bo if (IS_ERR(root)) { 233542437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2336f50f4353SLiu Bo ret = PTR_ERR(root); 2337f50f4353SLiu Bo goto out; 2338f50f4353SLiu Bo } 233942437a63SJosef Bacik } else { 2340a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2341a4f3d2c4SDavid Sterba fs_info->csum_root = root; 234242437a63SJosef Bacik } 2343882dbe0cSJosef Bacik } 23444bbcaa64SEric Sandeen 2345aeb935a4SQu Wenruo /* 2346aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2347aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2348aeb935a4SQu Wenruo */ 234956e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 235056e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2351aeb935a4SQu Wenruo if (IS_ERR(root)) { 235242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2353aeb935a4SQu Wenruo ret = PTR_ERR(root); 2354aeb935a4SQu Wenruo goto out; 2355aeb935a4SQu Wenruo } 235642437a63SJosef Bacik } else { 2357aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2358aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 235942437a63SJosef Bacik } 2360aeb935a4SQu Wenruo 23614bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2362a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2363a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2364a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2365afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2366a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23674bbcaa64SEric Sandeen } 23684bbcaa64SEric Sandeen 23694bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2370a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2371a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 237242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2373a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23744bbcaa64SEric Sandeen if (ret != -ENOENT) 2375f50f4353SLiu Bo goto out; 237642437a63SJosef Bacik } 23774bbcaa64SEric Sandeen } else { 2378a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2379a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23804bbcaa64SEric Sandeen } 23814bbcaa64SEric Sandeen 238270f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 238370f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 238470f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2385f50f4353SLiu Bo if (IS_ERR(root)) { 238642437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2387f50f4353SLiu Bo ret = PTR_ERR(root); 2388f50f4353SLiu Bo goto out; 2389f50f4353SLiu Bo } 239042437a63SJosef Bacik } else { 239170f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 239270f6d82eSOmar Sandoval fs_info->free_space_root = root; 239370f6d82eSOmar Sandoval } 239442437a63SJosef Bacik } 239570f6d82eSOmar Sandoval 23964bbcaa64SEric Sandeen return 0; 2397f50f4353SLiu Bo out: 2398f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2399f50f4353SLiu Bo location.objectid, ret); 2400f50f4353SLiu Bo return ret; 24014bbcaa64SEric Sandeen } 24024bbcaa64SEric Sandeen 2403069ec957SQu Wenruo /* 2404069ec957SQu Wenruo * Real super block validation 2405069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2406069ec957SQu Wenruo * 2407069ec957SQu Wenruo * @sb: super block to check 2408069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2409069ec957SQu Wenruo * 0 the primary (1st) sb 2410069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2411069ec957SQu Wenruo * -1 skip bytenr check 2412069ec957SQu Wenruo */ 2413069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2414069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 241521a852b0SQu Wenruo { 241621a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 241721a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 241821a852b0SQu Wenruo int ret = 0; 241921a852b0SQu Wenruo 242021a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 242121a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 242221a852b0SQu Wenruo ret = -EINVAL; 242321a852b0SQu Wenruo } 242421a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 242521a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 242621a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 242721a852b0SQu Wenruo ret = -EINVAL; 242821a852b0SQu Wenruo } 242921a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 243021a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 243121a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 243221a852b0SQu Wenruo ret = -EINVAL; 243321a852b0SQu Wenruo } 243421a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 243521a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 243621a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 243721a852b0SQu Wenruo ret = -EINVAL; 243821a852b0SQu Wenruo } 243921a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 244021a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 244121a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 244221a852b0SQu Wenruo ret = -EINVAL; 244321a852b0SQu Wenruo } 244421a852b0SQu Wenruo 244521a852b0SQu Wenruo /* 244621a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 244721a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 244821a852b0SQu Wenruo */ 244921a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 245021a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 245121a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 245221a852b0SQu Wenruo ret = -EINVAL; 245321a852b0SQu Wenruo } 245421a852b0SQu Wenruo /* Only PAGE SIZE is supported yet */ 245521a852b0SQu Wenruo if (sectorsize != PAGE_SIZE) { 245621a852b0SQu Wenruo btrfs_err(fs_info, 245721a852b0SQu Wenruo "sectorsize %llu not supported yet, only support %lu", 245821a852b0SQu Wenruo sectorsize, PAGE_SIZE); 245921a852b0SQu Wenruo ret = -EINVAL; 246021a852b0SQu Wenruo } 246121a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 246221a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 246321a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 246421a852b0SQu Wenruo ret = -EINVAL; 246521a852b0SQu Wenruo } 246621a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 246721a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 246821a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 246921a852b0SQu Wenruo ret = -EINVAL; 247021a852b0SQu Wenruo } 247121a852b0SQu Wenruo 247221a852b0SQu Wenruo /* Root alignment check */ 247321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 247421a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 247521a852b0SQu Wenruo btrfs_super_root(sb)); 247621a852b0SQu Wenruo ret = -EINVAL; 247721a852b0SQu Wenruo } 247821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 247921a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 248021a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 248121a852b0SQu Wenruo ret = -EINVAL; 248221a852b0SQu Wenruo } 248321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 248421a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 248521a852b0SQu Wenruo btrfs_super_log_root(sb)); 248621a852b0SQu Wenruo ret = -EINVAL; 248721a852b0SQu Wenruo } 248821a852b0SQu Wenruo 2489de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 24907239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 249121a852b0SQu Wenruo btrfs_err(fs_info, 24927239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2493de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 249421a852b0SQu Wenruo ret = -EINVAL; 249521a852b0SQu Wenruo } 249621a852b0SQu Wenruo 249721a852b0SQu Wenruo /* 249821a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 249921a852b0SQu Wenruo * done later 250021a852b0SQu Wenruo */ 250121a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 250221a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 250321a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 250421a852b0SQu Wenruo ret = -EINVAL; 250521a852b0SQu Wenruo } 250621a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 250721a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 250821a852b0SQu Wenruo btrfs_super_stripesize(sb)); 250921a852b0SQu Wenruo ret = -EINVAL; 251021a852b0SQu Wenruo } 251121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 251221a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 251321a852b0SQu Wenruo btrfs_super_num_devices(sb)); 251421a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 251521a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 251621a852b0SQu Wenruo ret = -EINVAL; 251721a852b0SQu Wenruo } 251821a852b0SQu Wenruo 2519069ec957SQu Wenruo if (mirror_num >= 0 && 2520069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 252121a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 252221a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 252321a852b0SQu Wenruo ret = -EINVAL; 252421a852b0SQu Wenruo } 252521a852b0SQu Wenruo 252621a852b0SQu Wenruo /* 252721a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 252821a852b0SQu Wenruo * and one chunk 252921a852b0SQu Wenruo */ 253021a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 253121a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 253221a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 253321a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 253421a852b0SQu Wenruo ret = -EINVAL; 253521a852b0SQu Wenruo } 253621a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 253721a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 253821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 253921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 254021a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 254121a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 254221a852b0SQu Wenruo ret = -EINVAL; 254321a852b0SQu Wenruo } 254421a852b0SQu Wenruo 254521a852b0SQu Wenruo /* 254621a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 254721a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 254821a852b0SQu Wenruo */ 254921a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 255021a852b0SQu Wenruo btrfs_warn(fs_info, 255121a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 255221a852b0SQu Wenruo btrfs_super_generation(sb), 255321a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 255421a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 255521a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 255621a852b0SQu Wenruo btrfs_warn(fs_info, 255721a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 255821a852b0SQu Wenruo btrfs_super_generation(sb), 255921a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 256021a852b0SQu Wenruo 256121a852b0SQu Wenruo return ret; 256221a852b0SQu Wenruo } 256321a852b0SQu Wenruo 2564069ec957SQu Wenruo /* 2565069ec957SQu Wenruo * Validation of super block at mount time. 2566069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2567069ec957SQu Wenruo * flags will be skipped. 2568069ec957SQu Wenruo */ 2569069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2570069ec957SQu Wenruo { 2571069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2572069ec957SQu Wenruo } 2573069ec957SQu Wenruo 257475cb857dSQu Wenruo /* 257575cb857dSQu Wenruo * Validation of super block at write time. 257675cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 257775cb857dSQu Wenruo * overwritten soon. 257875cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 257975cb857dSQu Wenruo */ 258075cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 258175cb857dSQu Wenruo struct btrfs_super_block *sb) 258275cb857dSQu Wenruo { 258375cb857dSQu Wenruo int ret; 258475cb857dSQu Wenruo 258575cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 258675cb857dSQu Wenruo if (ret < 0) 258775cb857dSQu Wenruo goto out; 2588e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 258975cb857dSQu Wenruo ret = -EUCLEAN; 259075cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 259175cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 259275cb857dSQu Wenruo goto out; 259375cb857dSQu Wenruo } 259475cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 259575cb857dSQu Wenruo ret = -EUCLEAN; 259675cb857dSQu Wenruo btrfs_err(fs_info, 259775cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 259875cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 259975cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 260075cb857dSQu Wenruo goto out; 260175cb857dSQu Wenruo } 260275cb857dSQu Wenruo out: 260375cb857dSQu Wenruo if (ret < 0) 260475cb857dSQu Wenruo btrfs_err(fs_info, 260575cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 260675cb857dSQu Wenruo return ret; 260775cb857dSQu Wenruo } 260875cb857dSQu Wenruo 26096ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2610b8522a1eSNikolay Borisov { 26116ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2612b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2613b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2614b8522a1eSNikolay Borisov bool handle_error = false; 2615b8522a1eSNikolay Borisov int ret = 0; 2616b8522a1eSNikolay Borisov int i; 2617b8522a1eSNikolay Borisov 2618b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2619b8522a1eSNikolay Borisov u64 generation; 2620b8522a1eSNikolay Borisov int level; 2621b8522a1eSNikolay Borisov 2622b8522a1eSNikolay Borisov if (handle_error) { 2623b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2624b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2625b8522a1eSNikolay Borisov tree_root->node = NULL; 2626b8522a1eSNikolay Borisov 2627b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2628b8522a1eSNikolay Borisov break; 2629b8522a1eSNikolay Borisov 2630b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2631b8522a1eSNikolay Borisov 2632b8522a1eSNikolay Borisov /* 2633b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2634b8522a1eSNikolay Borisov * valid 2635b8522a1eSNikolay Borisov */ 2636b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2637b8522a1eSNikolay Borisov 2638b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2639b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2640b8522a1eSNikolay Borisov 2641b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26426ef108ddSNikolay Borisov backup_index = ret; 2643b8522a1eSNikolay Borisov if (ret < 0) 2644b8522a1eSNikolay Borisov return ret; 2645b8522a1eSNikolay Borisov } 2646b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2647b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2648b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 2649b8522a1eSNikolay Borisov generation, level, NULL); 26500465337cSJosef Bacik if (IS_ERR(tree_root->node)) { 2651217f5004SNikolay Borisov handle_error = true; 2652b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 26530465337cSJosef Bacik tree_root->node = NULL; 2654217f5004SNikolay Borisov btrfs_warn(fs_info, "couldn't read tree root"); 2655217f5004SNikolay Borisov continue; 2656b8522a1eSNikolay Borisov 2657217f5004SNikolay Borisov } else if (!extent_buffer_uptodate(tree_root->node)) { 2658217f5004SNikolay Borisov handle_error = true; 2659217f5004SNikolay Borisov ret = -EIO; 2660217f5004SNikolay Borisov btrfs_warn(fs_info, "error while reading tree root"); 2661b8522a1eSNikolay Borisov continue; 2662b8522a1eSNikolay Borisov } 2663b8522a1eSNikolay Borisov 2664b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2665b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2666b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2667b8522a1eSNikolay Borisov 2668336a0d8dSNikolay Borisov /* 2669336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2670336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2671336a0d8dSNikolay Borisov */ 2672b8522a1eSNikolay Borisov ret = btrfs_find_highest_objectid(tree_root, 2673b8522a1eSNikolay Borisov &tree_root->highest_objectid); 2674b8522a1eSNikolay Borisov if (ret < 0) { 2675b8522a1eSNikolay Borisov handle_error = true; 2676b8522a1eSNikolay Borisov continue; 2677b8522a1eSNikolay Borisov } 2678b8522a1eSNikolay Borisov 2679b8522a1eSNikolay Borisov ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2680b8522a1eSNikolay Borisov 2681b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2682b8522a1eSNikolay Borisov if (ret < 0) { 2683b8522a1eSNikolay Borisov handle_error = true; 2684b8522a1eSNikolay Borisov continue; 2685b8522a1eSNikolay Borisov } 2686b8522a1eSNikolay Borisov 2687b8522a1eSNikolay Borisov /* All successful */ 2688b8522a1eSNikolay Borisov fs_info->generation = generation; 2689b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 26906ef108ddSNikolay Borisov 26916ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 26926ef108ddSNikolay Borisov if (backup_index < 0) { 26936ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 26946ef108ddSNikolay Borisov } else { 26956ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 26966ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 26976ef108ddSNikolay Borisov } 2698b8522a1eSNikolay Borisov break; 2699b8522a1eSNikolay Borisov } 2700b8522a1eSNikolay Borisov 2701b8522a1eSNikolay Borisov return ret; 2702b8522a1eSNikolay Borisov } 2703b8522a1eSNikolay Borisov 27048260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2705eb60ceacSChris Mason { 270676dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2707f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 27088fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2709facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 271024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2711eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 271211833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2713eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2714a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 271576dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 271624bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 27174cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2718ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2719f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 272047ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2721f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2722d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 272367c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 27247585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2725573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2726de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 272719c00ddcSChris Mason 27280b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27296324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2730f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 273147ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2732bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2733bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 27343fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 27353fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2736bd647ce3SJosef Bacik #endif 2737c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 273866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 273966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 274066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 274166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 274266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 274366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 274466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2745ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2746ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2747ba2c4d4eSJosef Bacik 2748771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27494cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27502fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2751034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2752fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 275395ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27549ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27554cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2756a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2757f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27588b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2759f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 276090519d66SArne Jansen /* readahead state */ 2761d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 276290519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2763fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2764c8b97818SChris Mason 2765b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2766b34b086cSChris Mason num_online_cpus() + 2, 8); 27670afbaf8cSChris Mason 2768199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2769199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 277069fe2d75SJosef Bacik 2771638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 277221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 277321adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 277421adbd5cSStefan Behrens #endif 2775779a65a4SEric Sandeen btrfs_init_balance(fs_info); 277657056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 2777a2de733cSArne Jansen 27780f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 27796bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2780a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 27810f9dd46cSJosef Bacik 2782fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2783fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2784afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 278539279cc3SChris Mason 27865a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2787e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2788925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2789a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2790a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27911bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27929e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2793c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 279476dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2795803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2796fa9c0d79SChris Mason 2797ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2798f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2799b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2800416ac51dSArne Jansen 2801fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2802fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2803fa9c0d79SChris Mason 2804e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2805f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2806bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 28074854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2808034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28093768f368SChris Mason 2810da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2811da17066cSJeff Mahoney fs_info->nodesize = 4096; 2812da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2813ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 2814da17066cSJeff Mahoney fs_info->stripesize = 4096; 2815da17066cSJeff Mahoney 2816eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2817eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2818eede2bf3SOmar Sandoval 28199e967495SFilipe Manana fs_info->send_in_progress = 0; 28208260edbaSJosef Bacik } 28218260edbaSJosef Bacik 28228260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 28238260edbaSJosef Bacik { 28248260edbaSJosef Bacik int ret; 28258260edbaSJosef Bacik 28268260edbaSJosef Bacik fs_info->sb = sb; 28278260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28288260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 28299e967495SFilipe Manana 2830c75e8394SJosef Bacik ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL); 2831ae18c37aSJosef Bacik if (ret) 2832ae18c37aSJosef Bacik return ret; 2833ae18c37aSJosef Bacik 2834ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2835ae18c37aSJosef Bacik if (ret) 2836c75e8394SJosef Bacik return ret; 2837ae18c37aSJosef Bacik 2838ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2839ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2840ae18c37aSJosef Bacik 2841ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2842ae18c37aSJosef Bacik if (ret) 2843c75e8394SJosef Bacik return ret; 2844ae18c37aSJosef Bacik 2845ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2846ae18c37aSJosef Bacik GFP_KERNEL); 2847ae18c37aSJosef Bacik if (ret) 2848c75e8394SJosef Bacik return ret; 2849ae18c37aSJosef Bacik 2850ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2851ae18c37aSJosef Bacik GFP_KERNEL); 2852c75e8394SJosef Bacik if (!fs_info->delayed_root) 2853c75e8394SJosef Bacik return -ENOMEM; 2854ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2855ae18c37aSJosef Bacik 2856c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 2857ae18c37aSJosef Bacik } 2858ae18c37aSJosef Bacik 285997f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 286097f4dd09SNikolay Borisov { 286197f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 286297f4dd09SNikolay Borisov int ret; 286397f4dd09SNikolay Borisov 286497f4dd09SNikolay Borisov /* 286597f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 286697f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 286797f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 286897f4dd09SNikolay Borisov */ 286997f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 287097f4dd09SNikolay Borisov if (ret < 0) { 2871c94bec2cSJosef Bacik if (ret != -EINTR) 2872c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 2873c94bec2cSJosef Bacik ret); 287497f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 287597f4dd09SNikolay Borisov return ret; 287697f4dd09SNikolay Borisov } 287797f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 287897f4dd09SNikolay Borisov } 287997f4dd09SNikolay Borisov 288097f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 288197f4dd09SNikolay Borisov { 288297f4dd09SNikolay Borisov struct task_struct *task; 288397f4dd09SNikolay Borisov 288497f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 288597f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 288697f4dd09SNikolay Borisov if (IS_ERR(task)) { 288797f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 288897f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 288997f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 289097f4dd09SNikolay Borisov return PTR_ERR(task); 289197f4dd09SNikolay Borisov } 289297f4dd09SNikolay Borisov 289397f4dd09SNikolay Borisov return 0; 289497f4dd09SNikolay Borisov } 289597f4dd09SNikolay Borisov 2896ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 2897ae18c37aSJosef Bacik char *options) 2898ae18c37aSJosef Bacik { 2899ae18c37aSJosef Bacik u32 sectorsize; 2900ae18c37aSJosef Bacik u32 nodesize; 2901ae18c37aSJosef Bacik u32 stripesize; 2902ae18c37aSJosef Bacik u64 generation; 2903ae18c37aSJosef Bacik u64 features; 2904ae18c37aSJosef Bacik u16 csum_type; 2905ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 2906ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2907ae18c37aSJosef Bacik struct btrfs_root *tree_root; 2908ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 2909ae18c37aSJosef Bacik int ret; 2910ae18c37aSJosef Bacik int err = -EINVAL; 2911ae18c37aSJosef Bacik int clear_free_space_tree = 0; 2912ae18c37aSJosef Bacik int level; 2913ae18c37aSJosef Bacik 29148260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 291553b381b3SDavid Woodhouse if (ret) { 291683c8266aSDavid Sterba err = ret; 2917ae18c37aSJosef Bacik goto fail; 291853b381b3SDavid Woodhouse } 291953b381b3SDavid Woodhouse 2920ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 2921ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 2922ae18c37aSJosef Bacik GFP_KERNEL); 2923ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 2924ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 2925ae18c37aSJosef Bacik GFP_KERNEL); 2926ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 2927ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 2928ae18c37aSJosef Bacik err = -ENOMEM; 2929c75e8394SJosef Bacik goto fail; 2930ae18c37aSJosef Bacik } 2931ae18c37aSJosef Bacik 2932ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 2933ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 2934ae18c37aSJosef Bacik err = -ENOMEM; 2935c75e8394SJosef Bacik goto fail; 2936ae18c37aSJosef Bacik } 2937ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 2938ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 2939ae18c37aSJosef Bacik 29403c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 29411104a885SDavid Sterba 29421104a885SDavid Sterba /* 29431104a885SDavid Sterba * Read super block and check the signature bytes only 29441104a885SDavid Sterba */ 29458f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 29468f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 29478f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 294816cdcec7SMiao Xie goto fail_alloc; 294920b45077SDave Young } 295039279cc3SChris Mason 29511104a885SDavid Sterba /* 2952260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 29538dc3f22cSJohannes Thumshirn * corrupted, we'll find out 29548dc3f22cSJohannes Thumshirn */ 29558f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 295651bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 29578dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 295851bce6c9SJohannes Thumshirn csum_type); 29598dc3f22cSJohannes Thumshirn err = -EINVAL; 29608f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29618dc3f22cSJohannes Thumshirn goto fail_alloc; 29628dc3f22cSJohannes Thumshirn } 29638dc3f22cSJohannes Thumshirn 29646d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 29656d97c6e3SJohannes Thumshirn if (ret) { 29666d97c6e3SJohannes Thumshirn err = ret; 29678f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29686d97c6e3SJohannes Thumshirn goto fail_alloc; 29696d97c6e3SJohannes Thumshirn } 29706d97c6e3SJohannes Thumshirn 29718dc3f22cSJohannes Thumshirn /* 29721104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 29731104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 29741104a885SDavid Sterba */ 29758f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 297605135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 29771104a885SDavid Sterba err = -EINVAL; 29788f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 2979141386e1SJosef Bacik goto fail_alloc; 29801104a885SDavid Sterba } 29811104a885SDavid Sterba 29821104a885SDavid Sterba /* 29831104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 29841104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 29851104a885SDavid Sterba * the whole block of INFO_SIZE 29861104a885SDavid Sterba */ 29878f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 29888f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 29895f39d397SChris Mason 2990fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 2991fbc6feaeSNikolay Borisov 2992de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2993de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 2994de37aa51SNikolay Borisov 29957239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 2996de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 2997de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 2998de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 29997239ff4bSNikolay Borisov } 30000b86a832SChris Mason 3001fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3002fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3003fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3004fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3005fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3006fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3007fbc6feaeSNikolay Borisov } 3008fbc6feaeSNikolay Borisov 3009fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3010fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3011de37aa51SNikolay Borisov 3012069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 30131104a885SDavid Sterba if (ret) { 301405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 30151104a885SDavid Sterba err = -EINVAL; 3016141386e1SJosef Bacik goto fail_alloc; 30171104a885SDavid Sterba } 30181104a885SDavid Sterba 30190f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3020141386e1SJosef Bacik goto fail_alloc; 30210f7d52f4SChris Mason 3022acce952bSliubo /* check FS state, whether FS is broken. */ 302387533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 302487533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3025acce952bSliubo 302675e7cb7fSLiu Bo /* 302775e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 302875e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 302975e7cb7fSLiu Bo */ 303075e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 303175e7cb7fSLiu Bo 30322ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 30332b82032cSYan Zheng if (ret) { 30342b82032cSYan Zheng err = ret; 3035141386e1SJosef Bacik goto fail_alloc; 30362b82032cSYan Zheng } 3037dfe25020SChris Mason 3038f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3039f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3040f2b636e8SJosef Bacik if (features) { 304105135f59SDavid Sterba btrfs_err(fs_info, 304205135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3043c1c9ff7cSGeert Uytterhoeven features); 3044f2b636e8SJosef Bacik err = -EINVAL; 3045141386e1SJosef Bacik goto fail_alloc; 3046f2b636e8SJosef Bacik } 3047f2b636e8SJosef Bacik 30485d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 30495d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 30500b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3051a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 30525c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 30535c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3054727011e0SChris Mason 30553173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 305605135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 30573173a18fSJosef Bacik 3058727011e0SChris Mason /* 3059727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3060727011e0SChris Mason * they are bigger than the page size 3061727011e0SChris Mason */ 306209cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3063727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 306405135f59SDavid Sterba btrfs_info(fs_info, 306505135f59SDavid Sterba "flagging fs with big metadata feature"); 3066727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3067727011e0SChris Mason } 3068727011e0SChris Mason 3069bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3070bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3071b7f67055SChandan Rajendra stripesize = sectorsize; 3072707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3073963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3074bc3f116fSChris Mason 3075da17066cSJeff Mahoney /* Cache block sizes */ 3076da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3077da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3078ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 307922b6331dSDavid Sterba fs_info->csum_size = btrfs_super_csum_size(disk_super); 3080fe5ecbe8SDavid Sterba fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3081da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3082da17066cSJeff Mahoney 3083bc3f116fSChris Mason /* 3084bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3085bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3086bc3f116fSChris Mason */ 3087bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3088707e8a07SDavid Sterba (sectorsize != nodesize)) { 308905135f59SDavid Sterba btrfs_err(fs_info, 309005135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 309105135f59SDavid Sterba nodesize, sectorsize); 3092141386e1SJosef Bacik goto fail_alloc; 3093bc3f116fSChris Mason } 3094bc3f116fSChris Mason 3095ceda0864SMiao Xie /* 3096ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3097ceda0864SMiao Xie * update the flag. 3098ceda0864SMiao Xie */ 30995d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 31005d4f98a2SYan Zheng 3101f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3102f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3103bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 310405135f59SDavid Sterba btrfs_err(fs_info, 310505135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3106c1c9ff7cSGeert Uytterhoeven features); 3107f2b636e8SJosef Bacik err = -EINVAL; 3108141386e1SJosef Bacik goto fail_alloc; 3109f2b636e8SJosef Bacik } 311061d92c32SChris Mason 31112a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 31122a458198SEric Sandeen if (ret) { 31132a458198SEric Sandeen err = ret; 31140dc3b84aSJosef Bacik goto fail_sb_buffer; 31150dc3b84aSJosef Bacik } 31164543df7eSChris Mason 31179e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 31189e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 31194575c9ccSChris Mason 3120a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3121a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3122de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3123db94535dSChris Mason 3124925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 31256bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3126925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 312784eed90fSChris Mason if (ret) { 312805135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 31295d4f98a2SYan Zheng goto fail_sb_buffer; 313084eed90fSChris Mason } 31310b86a832SChris Mason 313284234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3133581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 31340b86a832SChris Mason 31352ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 31360b86a832SChris Mason btrfs_super_chunk_root(disk_super), 3137581c1760SQu Wenruo generation, level, NULL); 313864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 313964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 314005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3141e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3142e5fffbacSchandan free_extent_buffer(chunk_root->node); 314395ab1f64SZhao Lei chunk_root->node = NULL; 3144af31f5e5SChris Mason goto fail_tree_roots; 314583121942SDavid Woodhouse } 31465d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 31475d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 31480b86a832SChris Mason 3149e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3150c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3151c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3152e17cade2SChris Mason 31535b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 31542b82032cSYan Zheng if (ret) { 315505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3156af31f5e5SChris Mason goto fail_tree_roots; 31572b82032cSYan Zheng } 31580b86a832SChris Mason 31598dabb742SStefan Behrens /* 31609b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 31619b99b115SAnand Jain * device replace procedure 31628dabb742SStefan Behrens */ 31639b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 3164dfe25020SChris Mason 3165a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 316605135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3167a6b0d5c8SChris Mason goto fail_tree_roots; 3168a6b0d5c8SChris Mason } 3169a6b0d5c8SChris Mason 3170b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 31714bbcaa64SEric Sandeen if (ret) 3172b8522a1eSNikolay Borisov goto fail_tree_roots; 31738929ecfaSYan, Zheng 317475ec1db8SJosef Bacik /* 317575ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 317675ec1db8SJosef Bacik * check the generation here so we can set the 317775ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 317875ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 317975ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 318075ec1db8SJosef Bacik * even though it was perfectly fine. 318175ec1db8SJosef Bacik */ 318275ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 318375ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 318475ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 318575ec1db8SJosef Bacik 3186cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3187cf90d884SQu Wenruo if (ret) { 3188cf90d884SQu Wenruo btrfs_err(fs_info, 3189cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3190cf90d884SQu Wenruo ret); 3191cf90d884SQu Wenruo goto fail_block_groups; 3192cf90d884SQu Wenruo } 319368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 319468310a5eSIlya Dryomov if (ret) { 319505135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 319668310a5eSIlya Dryomov goto fail_block_groups; 319768310a5eSIlya Dryomov } 319868310a5eSIlya Dryomov 3199733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3200733f4fbbSStefan Behrens if (ret) { 320105135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3202733f4fbbSStefan Behrens goto fail_block_groups; 3203733f4fbbSStefan Behrens } 3204733f4fbbSStefan Behrens 32058dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 32068dabb742SStefan Behrens if (ret) { 320705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 32088dabb742SStefan Behrens goto fail_block_groups; 32098dabb742SStefan Behrens } 32108dabb742SStefan Behrens 32119b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 32128dabb742SStefan Behrens 3213c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3214b7c35e81SAnand Jain if (ret) { 321505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 321605135f59SDavid Sterba ret); 3217b7c35e81SAnand Jain goto fail_block_groups; 3218b7c35e81SAnand Jain } 3219b7c35e81SAnand Jain 322096f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 32215ac1d209SJeff Mahoney if (ret) { 322205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3223b7c35e81SAnand Jain goto fail_fsdev_sysfs; 32245ac1d209SJeff Mahoney } 32255ac1d209SJeff Mahoney 3226c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3227c59021f8Sliubo if (ret) { 322805135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 32292365dd3cSAnand Jain goto fail_sysfs; 3230c59021f8Sliubo } 3231c59021f8Sliubo 32325b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 32331b1d1f66SJosef Bacik if (ret) { 323405135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 32352365dd3cSAnand Jain goto fail_sysfs; 32361b1d1f66SJosef Bacik } 32374330e183SQu Wenruo 32386528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 323905135f59SDavid Sterba btrfs_warn(fs_info, 324052042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 32412365dd3cSAnand Jain goto fail_sysfs; 3242292fd7fcSStefan Behrens } 32439078a3e1SChris Mason 3244a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3245a74a4b97SChris Mason "btrfs-cleaner"); 324657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 32472365dd3cSAnand Jain goto fail_sysfs; 3248a74a4b97SChris Mason 3249a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3250a74a4b97SChris Mason tree_root, 3251a74a4b97SChris Mason "btrfs-transaction"); 325257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 32533f157a2fSChris Mason goto fail_cleaner; 3254a74a4b97SChris Mason 3255583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3256c289811cSChris Mason !fs_info->fs_devices->rotating) { 3257583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3258c289811cSChris Mason } 3259c289811cSChris Mason 3260572d9ab7SDavid Sterba /* 326101327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3262572d9ab7SDavid Sterba * not have to wait for transaction commit 3263572d9ab7SDavid Sterba */ 3264572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 32653818aea2SQu Wenruo 326621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 32670b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 32682ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 32690b246afaSJeff Mahoney btrfs_test_opt(fs_info, 327021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 327121adbd5cSStefan Behrens 1 : 0, 327221adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 327321adbd5cSStefan Behrens if (ret) 327405135f59SDavid Sterba btrfs_warn(fs_info, 327505135f59SDavid Sterba "failed to initialize integrity check module: %d", 327605135f59SDavid Sterba ret); 327721adbd5cSStefan Behrens } 327821adbd5cSStefan Behrens #endif 3279bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3280bcef60f2SArne Jansen if (ret) 3281bcef60f2SArne Jansen goto fail_trans_kthread; 328221adbd5cSStefan Behrens 3283fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3284fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3285fd708b81SJosef Bacik 328696da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 328796da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 32880b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3289e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 329063443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 329179787eaaSJeff Mahoney if (ret) { 329263443bf5SEric Sandeen err = ret; 329328c16cbbSWang Shilong goto fail_qgroup; 3294e556ce2cSYan Zheng } 3295e02119d5SChris Mason } 32961a40e23bSZheng Yan 32976bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 329879787eaaSJeff Mahoney if (ret) 329928c16cbbSWang Shilong goto fail_qgroup; 330076dda93cSYan, Zheng 3301bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 3302d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 330344c44af2SIlya Dryomov if (ret) 330428c16cbbSWang Shilong goto fail_qgroup; 330590c711abSZygo Blaxell 330690c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 33075d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 330890c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3309d7ce5843SMiao Xie if (ret < 0) { 331005135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 331105135f59SDavid Sterba ret); 3312d7ce5843SMiao Xie err = -EINVAL; 3313bcef60f2SArne Jansen goto fail_qgroup; 3314d7ce5843SMiao Xie } 33157c2ca468SChris Mason } 33161a40e23bSZheng Yan 331756e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 33183140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 33193140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3320f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3321315bf8efSJosef Bacik fs_info->fs_root = NULL; 3322bcef60f2SArne Jansen goto fail_qgroup; 33233140c9a3SDan Carpenter } 3324c289811cSChris Mason 3325bc98a42cSDavid Howells if (sb_rdonly(sb)) 33262b6ba629SIlya Dryomov return 0; 33272b6ba629SIlya Dryomov 3328f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3329f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33306675df31SOmar Sandoval clear_free_space_tree = 1; 33316675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33326675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33336675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 33346675df31SOmar Sandoval clear_free_space_tree = 1; 33356675df31SOmar Sandoval } 33366675df31SOmar Sandoval 33376675df31SOmar Sandoval if (clear_free_space_tree) { 3338f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3339f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3340f8d468a1SOmar Sandoval if (ret) { 3341f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3342f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 33436bccf3abSJeff Mahoney close_ctree(fs_info); 3344f8d468a1SOmar Sandoval return ret; 3345f8d468a1SOmar Sandoval } 3346f8d468a1SOmar Sandoval } 3347f8d468a1SOmar Sandoval 33480b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3349511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 335005135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3351511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3352511711afSChris Mason if (ret) { 335305135f59SDavid Sterba btrfs_warn(fs_info, 335405135f59SDavid Sterba "failed to create free space tree: %d", ret); 33556bccf3abSJeff Mahoney close_ctree(fs_info); 3356511711afSChris Mason return ret; 3357511711afSChris Mason } 3358511711afSChris Mason } 3359511711afSChris Mason 3360e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 33612b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33622b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33632b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 33646bccf3abSJeff Mahoney close_ctree(fs_info); 33652b6ba629SIlya Dryomov return ret; 33662b6ba629SIlya Dryomov } 3367e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 336859641015SIlya Dryomov 33692b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 33702b6ba629SIlya Dryomov if (ret) { 337105135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 33726bccf3abSJeff Mahoney close_ctree(fs_info); 33732b6ba629SIlya Dryomov return ret; 3374e3acc2a6SJosef Bacik } 3375e3acc2a6SJosef Bacik 33768dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 33778dabb742SStefan Behrens if (ret) { 337805135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 33796bccf3abSJeff Mahoney close_ctree(fs_info); 33808dabb742SStefan Behrens return ret; 33818dabb742SStefan Behrens } 33828dabb742SStefan Behrens 3383b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3384b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3385b382a324SJan Schmidt 33864bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 338705135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3388f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3389f7a81ea4SStefan Behrens if (ret) { 339005135f59SDavid Sterba btrfs_warn(fs_info, 339105135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 33926bccf3abSJeff Mahoney close_ctree(fs_info); 3393f7a81ea4SStefan Behrens return ret; 3394f7a81ea4SStefan Behrens } 33950b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 33964bbcaa64SEric Sandeen fs_info->generation != 33974bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 339805135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 339970f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 340070f80175SStefan Behrens if (ret) { 340105135f59SDavid Sterba btrfs_warn(fs_info, 340205135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 34036bccf3abSJeff Mahoney close_ctree(fs_info); 340470f80175SStefan Behrens return ret; 340570f80175SStefan Behrens } 3406f7a81ea4SStefan Behrens } 3407afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 340847ab2a6cSJosef Bacik 3409ad2b2c80SAl Viro return 0; 341039279cc3SChris Mason 3411bcef60f2SArne Jansen fail_qgroup: 3412bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 34137c2ca468SChris Mason fail_trans_kthread: 34147c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 34152ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3416faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 34173f157a2fSChris Mason fail_cleaner: 3418a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 34197c2ca468SChris Mason 34207c2ca468SChris Mason /* 34217c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 34227c2ca468SChris Mason * kthreads 34237c2ca468SChris Mason */ 34247c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 34257c2ca468SChris Mason 34262365dd3cSAnand Jain fail_sysfs: 34276618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 34282365dd3cSAnand Jain 3429b7c35e81SAnand Jain fail_fsdev_sysfs: 3430b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3431b7c35e81SAnand Jain 34321b1d1f66SJosef Bacik fail_block_groups: 343354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3434af31f5e5SChris Mason 3435af31f5e5SChris Mason fail_tree_roots: 34369e3aa805SJosef Bacik if (fs_info->data_reloc_root) 34379e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 34384273eaffSAnand Jain free_root_pointers(fs_info, true); 34392b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3440af31f5e5SChris Mason 344139279cc3SChris Mason fail_sb_buffer: 34427abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 34435cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 344416cdcec7SMiao Xie fail_alloc: 3445586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3446586e46e2SIlya Dryomov 34474543df7eSChris Mason iput(fs_info->btree_inode); 34487e662854SQinghuang Feng fail: 3449586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3450ad2b2c80SAl Viro return err; 3451eb60ceacSChris Mason } 3452663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3453eb60ceacSChris Mason 3454314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3455f2984462SChris Mason { 3456314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3457314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3458314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3459314b6dd0SJohannes Thumshirn struct page *page; 3460442a4f63SStefan Behrens 3461314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3462314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3463314b6dd0SJohannes Thumshirn 3464314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3465fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3466314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3467314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3468314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3469314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3470314b6dd0SJohannes Thumshirn SetPageError(page); 3471314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3472314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3473314b6dd0SJohannes Thumshirn } else { 3474314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3475f2984462SChris Mason } 3476314b6dd0SJohannes Thumshirn 3477314b6dd0SJohannes Thumshirn put_page(page); 3478314b6dd0SJohannes Thumshirn unlock_page(page); 3479314b6dd0SJohannes Thumshirn } 3480314b6dd0SJohannes Thumshirn 3481314b6dd0SJohannes Thumshirn bio_put(bio); 3482f2984462SChris Mason } 3483f2984462SChris Mason 34848f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 34858f32380dSJohannes Thumshirn int copy_num) 348629c36d72SAnand Jain { 348729c36d72SAnand Jain struct btrfs_super_block *super; 34888f32380dSJohannes Thumshirn struct page *page; 348929c36d72SAnand Jain u64 bytenr; 34908f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 349129c36d72SAnand Jain 349229c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 349329c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 34948f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 349529c36d72SAnand Jain 34968f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 34978f32380dSJohannes Thumshirn if (IS_ERR(page)) 34988f32380dSJohannes Thumshirn return ERR_CAST(page); 349929c36d72SAnand Jain 35008f32380dSJohannes Thumshirn super = page_address(page); 350196c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 350296c2e067SAnand Jain btrfs_release_disk_super(super); 350396c2e067SAnand Jain return ERR_PTR(-ENODATA); 350496c2e067SAnand Jain } 350596c2e067SAnand Jain 350696c2e067SAnand Jain if (btrfs_super_bytenr(super) != bytenr) { 35078f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 35088f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 350929c36d72SAnand Jain } 351029c36d72SAnand Jain 35118f32380dSJohannes Thumshirn return super; 351229c36d72SAnand Jain } 351329c36d72SAnand Jain 351429c36d72SAnand Jain 35158f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3516a512bbf8SYan Zheng { 35178f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3518a512bbf8SYan Zheng int i; 3519a512bbf8SYan Zheng u64 transid = 0; 3520a512bbf8SYan Zheng 3521a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3522a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3523a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3524a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3525a512bbf8SYan Zheng */ 3526a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 35278f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 35288f32380dSJohannes Thumshirn if (IS_ERR(super)) 3529a512bbf8SYan Zheng continue; 3530a512bbf8SYan Zheng 3531a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 35328f32380dSJohannes Thumshirn if (latest) 35338f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 35348f32380dSJohannes Thumshirn 35358f32380dSJohannes Thumshirn latest = super; 3536a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3537a512bbf8SYan Zheng } 3538a512bbf8SYan Zheng } 353992fc03fbSAnand Jain 35408f32380dSJohannes Thumshirn return super; 3541a512bbf8SYan Zheng } 3542a512bbf8SYan Zheng 35434eedeb75SHisashi Hifumi /* 3544abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3545314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 35464eedeb75SHisashi Hifumi * 3547abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3548abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3549abbb3b8eSDavid Sterba * same for write and wait phases. 35504eedeb75SHisashi Hifumi * 3551314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 35524eedeb75SHisashi Hifumi */ 3553a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3554abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3555a512bbf8SYan Zheng { 3556d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3557314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3558d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3559a512bbf8SYan Zheng int i; 3560a512bbf8SYan Zheng int errors = 0; 3561a512bbf8SYan Zheng u64 bytenr; 3562a512bbf8SYan Zheng 3563a512bbf8SYan Zheng if (max_mirrors == 0) 3564a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3565a512bbf8SYan Zheng 3566d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3567d5178578SJohannes Thumshirn 3568a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3569314b6dd0SJohannes Thumshirn struct page *page; 3570314b6dd0SJohannes Thumshirn struct bio *bio; 3571314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3572314b6dd0SJohannes Thumshirn 3573a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3574935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3575935e5cc9SMiao Xie device->commit_total_bytes) 3576a512bbf8SYan Zheng break; 3577a512bbf8SYan Zheng 3578a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3579a512bbf8SYan Zheng 3580fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3581fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 3582fd08001fSEric Biggers sb->csum); 3583a512bbf8SYan Zheng 3584314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3585314b6dd0SJohannes Thumshirn GFP_NOFS); 3586314b6dd0SJohannes Thumshirn if (!page) { 3587fb456252SJeff Mahoney btrfs_err(device->fs_info, 3588314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3589f14d104dSDavid Sterba bytenr); 3590634554dcSJosef Bacik errors++; 3591634554dcSJosef Bacik continue; 3592634554dcSJosef Bacik } 3593634554dcSJosef Bacik 3594314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3595314b6dd0SJohannes Thumshirn get_page(page); 3596a512bbf8SYan Zheng 3597314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3598314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3599a512bbf8SYan Zheng 3600387125fcSChris Mason /* 3601314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3602314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3603314b6dd0SJohannes Thumshirn * checking. 3604387125fcSChris Mason */ 3605314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3606314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3607314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3608314b6dd0SJohannes Thumshirn bio->bi_private = device; 3609314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3610314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3611314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3612314b6dd0SJohannes Thumshirn 3613314b6dd0SJohannes Thumshirn /* 3614314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3615314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3616314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3617314b6dd0SJohannes Thumshirn */ 3618314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 36191b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3620314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3621314b6dd0SJohannes Thumshirn 3622314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 3623a512bbf8SYan Zheng } 3624a512bbf8SYan Zheng return errors < i ? 0 : -1; 3625a512bbf8SYan Zheng } 3626a512bbf8SYan Zheng 3627387125fcSChris Mason /* 3628abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3629abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3630abbb3b8eSDavid Sterba * 3631314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3632abbb3b8eSDavid Sterba * date. 3633abbb3b8eSDavid Sterba */ 3634abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3635abbb3b8eSDavid Sterba { 3636abbb3b8eSDavid Sterba int i; 3637abbb3b8eSDavid Sterba int errors = 0; 3638b6a535faSHoward McLauchlan bool primary_failed = false; 3639abbb3b8eSDavid Sterba u64 bytenr; 3640abbb3b8eSDavid Sterba 3641abbb3b8eSDavid Sterba if (max_mirrors == 0) 3642abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3643abbb3b8eSDavid Sterba 3644abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3645314b6dd0SJohannes Thumshirn struct page *page; 3646314b6dd0SJohannes Thumshirn 3647abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3648abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3649abbb3b8eSDavid Sterba device->commit_total_bytes) 3650abbb3b8eSDavid Sterba break; 3651abbb3b8eSDavid Sterba 3652314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3653314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3654314b6dd0SJohannes Thumshirn if (!page) { 3655abbb3b8eSDavid Sterba errors++; 3656b6a535faSHoward McLauchlan if (i == 0) 3657b6a535faSHoward McLauchlan primary_failed = true; 3658abbb3b8eSDavid Sterba continue; 3659abbb3b8eSDavid Sterba } 3660314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3661314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3662314b6dd0SJohannes Thumshirn if (PageError(page)) { 3663abbb3b8eSDavid Sterba errors++; 3664b6a535faSHoward McLauchlan if (i == 0) 3665b6a535faSHoward McLauchlan primary_failed = true; 3666b6a535faSHoward McLauchlan } 3667abbb3b8eSDavid Sterba 3668314b6dd0SJohannes Thumshirn /* Drop our reference */ 3669314b6dd0SJohannes Thumshirn put_page(page); 3670abbb3b8eSDavid Sterba 3671314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3672314b6dd0SJohannes Thumshirn put_page(page); 3673abbb3b8eSDavid Sterba } 3674abbb3b8eSDavid Sterba 3675b6a535faSHoward McLauchlan /* log error, force error return */ 3676b6a535faSHoward McLauchlan if (primary_failed) { 3677b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3678b6a535faSHoward McLauchlan device->devid); 3679b6a535faSHoward McLauchlan return -1; 3680b6a535faSHoward McLauchlan } 3681b6a535faSHoward McLauchlan 3682abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3683abbb3b8eSDavid Sterba } 3684abbb3b8eSDavid Sterba 3685abbb3b8eSDavid Sterba /* 3686387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3687387125fcSChris Mason * for the barrier when it is done 3688387125fcSChris Mason */ 36894246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3690387125fcSChris Mason { 3691387125fcSChris Mason complete(bio->bi_private); 3692387125fcSChris Mason } 3693387125fcSChris Mason 3694387125fcSChris Mason /* 36954fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 36964fc6441aSAnand Jain * done in the waiting counterpart. 3697387125fcSChris Mason */ 36984fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3699387125fcSChris Mason { 3700c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3701e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3702387125fcSChris Mason 3703c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 37044fc6441aSAnand Jain return; 3705387125fcSChris Mason 3706e0ae9994SDavid Sterba bio_reset(bio); 3707387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 370874d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 37098d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3710387125fcSChris Mason init_completion(&device->flush_wait); 3711387125fcSChris Mason bio->bi_private = &device->flush_wait; 3712387125fcSChris Mason 371343a01111SLu Fengqi btrfsic_submit_bio(bio); 37141c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 37154fc6441aSAnand Jain } 3716387125fcSChris Mason 37174fc6441aSAnand Jain /* 37184fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 37194fc6441aSAnand Jain */ 37208c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 37214fc6441aSAnand Jain { 37224fc6441aSAnand Jain struct bio *bio = device->flush_bio; 37234fc6441aSAnand Jain 37241c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 372558efbc9fSOmar Sandoval return BLK_STS_OK; 37264fc6441aSAnand Jain 37271c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 37282980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 37294fc6441aSAnand Jain 37308c27cb35SLinus Torvalds return bio->bi_status; 3731387125fcSChris Mason } 3732387125fcSChris Mason 3733d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3734401b41e5SAnand Jain { 37356528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3736401b41e5SAnand Jain return -EIO; 3737387125fcSChris Mason return 0; 3738387125fcSChris Mason } 3739387125fcSChris Mason 3740387125fcSChris Mason /* 3741387125fcSChris Mason * send an empty flush down to each device in parallel, 3742387125fcSChris Mason * then wait for them 3743387125fcSChris Mason */ 3744387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3745387125fcSChris Mason { 3746387125fcSChris Mason struct list_head *head; 3747387125fcSChris Mason struct btrfs_device *dev; 37485af3e8ccSStefan Behrens int errors_wait = 0; 37494e4cbee9SChristoph Hellwig blk_status_t ret; 3750387125fcSChris Mason 37511538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3752387125fcSChris Mason /* send down all the barriers */ 3753387125fcSChris Mason head = &info->fs_devices->devices; 37541538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3755e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3756f88ba6a2SHidetoshi Seto continue; 3757cea7c8bfSAnand Jain if (!dev->bdev) 3758387125fcSChris Mason continue; 3759e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3760ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3761387125fcSChris Mason continue; 3762387125fcSChris Mason 37634fc6441aSAnand Jain write_dev_flush(dev); 376458efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3765387125fcSChris Mason } 3766387125fcSChris Mason 3767387125fcSChris Mason /* wait for all the barriers */ 37681538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3769e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3770f88ba6a2SHidetoshi Seto continue; 3771387125fcSChris Mason if (!dev->bdev) { 37725af3e8ccSStefan Behrens errors_wait++; 3773387125fcSChris Mason continue; 3774387125fcSChris Mason } 3775e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3776ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3777387125fcSChris Mason continue; 3778387125fcSChris Mason 37794fc6441aSAnand Jain ret = wait_dev_flush(dev); 3780401b41e5SAnand Jain if (ret) { 3781401b41e5SAnand Jain dev->last_flush_error = ret; 378266b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 378366b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 37845af3e8ccSStefan Behrens errors_wait++; 3785387125fcSChris Mason } 3786401b41e5SAnand Jain } 3787401b41e5SAnand Jain 3788cea7c8bfSAnand Jain if (errors_wait) { 3789401b41e5SAnand Jain /* 3790401b41e5SAnand Jain * At some point we need the status of all disks 3791401b41e5SAnand Jain * to arrive at the volume status. So error checking 3792401b41e5SAnand Jain * is being pushed to a separate loop. 3793401b41e5SAnand Jain */ 3794d10b82feSQu Wenruo return check_barrier_error(info); 3795401b41e5SAnand Jain } 3796387125fcSChris Mason return 0; 3797387125fcSChris Mason } 3798387125fcSChris Mason 3799943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3800943c6e99SZhao Lei { 38018789f4feSZhao Lei int raid_type; 38028789f4feSZhao Lei int min_tolerated = INT_MAX; 3803943c6e99SZhao Lei 38048789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 38058789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 38068c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 38078789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 38088789f4feSZhao Lei tolerated_failures); 3809943c6e99SZhao Lei 38108789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 38118789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 38128789f4feSZhao Lei continue; 381341a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 38148789f4feSZhao Lei continue; 38158c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 38168789f4feSZhao Lei btrfs_raid_array[raid_type]. 38178789f4feSZhao Lei tolerated_failures); 38188789f4feSZhao Lei } 3819943c6e99SZhao Lei 38208789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3821ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 38228789f4feSZhao Lei min_tolerated = 0; 38238789f4feSZhao Lei } 38248789f4feSZhao Lei 38258789f4feSZhao Lei return min_tolerated; 3826943c6e99SZhao Lei } 3827943c6e99SZhao Lei 3828eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3829f2984462SChris Mason { 3830e5e9a520SChris Mason struct list_head *head; 3831f2984462SChris Mason struct btrfs_device *dev; 3832a061fc8dSChris Mason struct btrfs_super_block *sb; 3833f2984462SChris Mason struct btrfs_dev_item *dev_item; 3834f2984462SChris Mason int ret; 3835f2984462SChris Mason int do_barriers; 3836a236aed1SChris Mason int max_errors; 3837a236aed1SChris Mason int total_errors = 0; 3838a061fc8dSChris Mason u64 flags; 3839f2984462SChris Mason 38400b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3841fed3b381SLiu Bo 3842fed3b381SLiu Bo /* 3843fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3844fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3845fed3b381SLiu Bo * been consistent on disk. 3846fed3b381SLiu Bo */ 3847fed3b381SLiu Bo if (max_mirrors == 0) 38480b246afaSJeff Mahoney backup_super_roots(fs_info); 3849f2984462SChris Mason 38500b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3851a061fc8dSChris Mason dev_item = &sb->dev_item; 3852e5e9a520SChris Mason 38530b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 38540b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 38550b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3856387125fcSChris Mason 38575af3e8ccSStefan Behrens if (do_barriers) { 38580b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 38595af3e8ccSStefan Behrens if (ret) { 38605af3e8ccSStefan Behrens mutex_unlock( 38610b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 38620b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 38635af3e8ccSStefan Behrens "errors while submitting device barriers."); 38645af3e8ccSStefan Behrens return ret; 38655af3e8ccSStefan Behrens } 38665af3e8ccSStefan Behrens } 3867387125fcSChris Mason 38681538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3869dfe25020SChris Mason if (!dev->bdev) { 3870dfe25020SChris Mason total_errors++; 3871dfe25020SChris Mason continue; 3872dfe25020SChris Mason } 3873e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3874ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3875dfe25020SChris Mason continue; 3876dfe25020SChris Mason 38772b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3878a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3879a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 38807df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3881935e5cc9SMiao Xie dev->commit_total_bytes); 3882ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3883ce7213c7SMiao Xie dev->commit_bytes_used); 3884a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3885a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3886a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3887a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 38887239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 38897239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 3890a512bbf8SYan Zheng 3891a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3892a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3893f2984462SChris Mason 389475cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 389575cb857dSQu Wenruo if (ret < 0) { 389675cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 389775cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 389875cb857dSQu Wenruo "unexpected superblock corruption detected"); 389975cb857dSQu Wenruo return -EUCLEAN; 390075cb857dSQu Wenruo } 390175cb857dSQu Wenruo 3902abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3903a236aed1SChris Mason if (ret) 3904a236aed1SChris Mason total_errors++; 3905f2984462SChris Mason } 3906a236aed1SChris Mason if (total_errors > max_errors) { 39070b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3908d397712bSChris Mason total_errors); 39090b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 391079787eaaSJeff Mahoney 39119d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 39120b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 39130b246afaSJeff Mahoney "%d errors while writing supers", 39140b246afaSJeff Mahoney total_errors); 39159d565ba4SStefan Behrens return -EIO; 3916a236aed1SChris Mason } 3917f2984462SChris Mason 3918a512bbf8SYan Zheng total_errors = 0; 39191538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3920dfe25020SChris Mason if (!dev->bdev) 3921dfe25020SChris Mason continue; 3922e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3923ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3924dfe25020SChris Mason continue; 3925dfe25020SChris Mason 3926abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3927a512bbf8SYan Zheng if (ret) 39281259ab75SChris Mason total_errors++; 3929f2984462SChris Mason } 39300b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3931a236aed1SChris Mason if (total_errors > max_errors) { 39320b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 39330b246afaSJeff Mahoney "%d errors while writing supers", 39340b246afaSJeff Mahoney total_errors); 393579787eaaSJeff Mahoney return -EIO; 3936a236aed1SChris Mason } 3937f2984462SChris Mason return 0; 3938f2984462SChris Mason } 3939f2984462SChris Mason 3940cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3941cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3942cb517eabSMiao Xie struct btrfs_root *root) 39432619ba1fSChris Mason { 39444785e24fSJosef Bacik bool drop_ref = false; 39454785e24fSJosef Bacik 39464df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 39472619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 39482619ba1fSChris Mason (unsigned long)root->root_key.objectid); 3949af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 39504785e24fSJosef Bacik drop_ref = true; 39514df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 395276dda93cSYan, Zheng 39531c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 3954ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 39551c1ea4f7SLiu Bo if (root->reloc_root) { 395600246528SJosef Bacik btrfs_put_root(root->reloc_root); 39571c1ea4f7SLiu Bo root->reloc_root = NULL; 39581c1ea4f7SLiu Bo } 39591c1ea4f7SLiu Bo } 39603321719eSLiu Bo 3961faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3962581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3963faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3964581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 39650e996e7fSJosef Bacik if (root->ino_cache_inode) { 39660e996e7fSJosef Bacik iput(root->ino_cache_inode); 39670e996e7fSJosef Bacik root->ino_cache_inode = NULL; 39680e996e7fSJosef Bacik } 39694785e24fSJosef Bacik if (drop_ref) 397000246528SJosef Bacik btrfs_put_root(root); 39712619ba1fSChris Mason } 39722619ba1fSChris Mason 3973c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3974c146afadSYan Zheng { 3975c146afadSYan Zheng u64 root_objectid = 0; 3976c146afadSYan Zheng struct btrfs_root *gang[8]; 397765d33fd7SQu Wenruo int i = 0; 397865d33fd7SQu Wenruo int err = 0; 397965d33fd7SQu Wenruo unsigned int ret = 0; 3980c146afadSYan Zheng 3981c146afadSYan Zheng while (1) { 3982efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 3983c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3984c146afadSYan Zheng (void **)gang, root_objectid, 3985c146afadSYan Zheng ARRAY_SIZE(gang)); 398665d33fd7SQu Wenruo if (!ret) { 3987efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 3988c146afadSYan Zheng break; 398965d33fd7SQu Wenruo } 39905d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 399166b4ffd1SJosef Bacik 399265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 399365d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 399465d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 399565d33fd7SQu Wenruo gang[i] = NULL; 399665d33fd7SQu Wenruo continue; 399765d33fd7SQu Wenruo } 399865d33fd7SQu Wenruo /* grab all the search result for later use */ 399900246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 400065d33fd7SQu Wenruo } 4001efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 400265d33fd7SQu Wenruo 400365d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 400465d33fd7SQu Wenruo if (!gang[i]) 400565d33fd7SQu Wenruo continue; 4006c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 400766b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 400866b4ffd1SJosef Bacik if (err) 400965d33fd7SQu Wenruo break; 401000246528SJosef Bacik btrfs_put_root(gang[i]); 4011c146afadSYan Zheng } 4012c146afadSYan Zheng root_objectid++; 4013c146afadSYan Zheng } 401465d33fd7SQu Wenruo 401565d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 401665d33fd7SQu Wenruo for (; i < ret; i++) { 401765d33fd7SQu Wenruo if (gang[i]) 401800246528SJosef Bacik btrfs_put_root(gang[i]); 401965d33fd7SQu Wenruo } 402065d33fd7SQu Wenruo return err; 4021c146afadSYan Zheng } 4022c146afadSYan Zheng 40236bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4024c146afadSYan Zheng { 40256bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4026c146afadSYan Zheng struct btrfs_trans_handle *trans; 4027c146afadSYan Zheng 40280b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 40292ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 40300b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 40310b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4032c71bf099SYan, Zheng 4033c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 40340b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 40350b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4036c71bf099SYan, Zheng 40377a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 40383612b495STsutomu Itoh if (IS_ERR(trans)) 40393612b495STsutomu Itoh return PTR_ERR(trans); 40403a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4041c146afadSYan Zheng } 4042c146afadSYan Zheng 4043b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4044eb60ceacSChris Mason { 4045c146afadSYan Zheng int ret; 4046e089f05cSChris Mason 4047afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4048d6fd0ae2SOmar Sandoval /* 4049d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4050d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4051d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4052d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4053d6fd0ae2SOmar Sandoval */ 4054d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4055a2135011SChris Mason 40567343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4057d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 40587343dd61SJustin Maggard 4059803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4060803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4061803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4062803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4063803b2f54SStefan Behrens 4064837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4065aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4066837d5b6eSIlya Dryomov 40678dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 40688dabb742SStefan Behrens 4069aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 40704cb5300bSChris Mason 40714cb5300bSChris Mason /* wait for any defraggers to finish */ 40724cb5300bSChris Mason wait_event(fs_info->transaction_wait, 40734cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 40744cb5300bSChris Mason 40754cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 407626176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 40774cb5300bSChris Mason 407821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 407957056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 408021c7e756SMiao Xie 4081b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4082b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4083b0643e59SDennis Zhou 4084bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4085e44163e1SJeff Mahoney /* 4086d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4087d6fd0ae2SOmar Sandoval * unused block groups. 4088e44163e1SJeff Mahoney */ 40890b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4090e44163e1SJeff Mahoney 4091f0cc2cd7SFilipe Manana /* 4092f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4093f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4094f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4095f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4096f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4097f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4098f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4099f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4100f0cc2cd7SFilipe Manana * This is a very rare case. 4101f0cc2cd7SFilipe Manana */ 4102f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4103f0cc2cd7SFilipe Manana 41046bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4105d397712bSChris Mason if (ret) 410604892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4107c146afadSYan Zheng } 4108ed2ff2cbSChris Mason 4109af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4110af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 41112ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4112acce952bSliubo 4113e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4114e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 41158929ecfaSYan, Zheng 4116e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4117afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4118f25784b3SYan Zheng 41195958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 41205958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 41215958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 41225958253cSQu Wenruo } 41235958253cSQu Wenruo 412404892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4125fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4126bcef60f2SArne Jansen 4127963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 412804892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4129963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4130b0c68f8bSChris Mason } 413131153d81SYan Zheng 41324297ff84SJosef Bacik if (percpu_counter_sum(&fs_info->dio_bytes)) 41334297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 41344297ff84SJosef Bacik percpu_counter_sum(&fs_info->dio_bytes)); 41354297ff84SJosef Bacik 41366618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4137b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 41385ac1d209SJeff Mahoney 41391a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 41401a4319ccSLiu Bo 4141de348ee0SWang Shilong /* 4142de348ee0SWang Shilong * we must make sure there is not any read request to 4143de348ee0SWang Shilong * submit after we stopping all workers. 4144de348ee0SWang Shilong */ 4145de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 414696192499SJosef Bacik btrfs_stop_all_workers(fs_info); 414796192499SJosef Bacik 4148afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 41494273eaffSAnand Jain free_root_pointers(fs_info, true); 41508c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 41519ad6b7bcSChris Mason 41524e19443dSJosef Bacik /* 41534e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 41544e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 41554e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 41564e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 41574e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 41584e19443dSJosef Bacik */ 41594e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 41604e19443dSJosef Bacik 416113e6c37bSJosef Bacik iput(fs_info->btree_inode); 4162d6bfde87SChris Mason 416321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 41640b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 41652ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 416621adbd5cSStefan Behrens #endif 416721adbd5cSStefan Behrens 41680b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 416968c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4170eb60ceacSChris Mason } 4171eb60ceacSChris Mason 4172b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4173b9fab919SChris Mason int atomic) 4174ccd467d6SChris Mason { 41751259ab75SChris Mason int ret; 4176727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 41771259ab75SChris Mason 41780b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 41791259ab75SChris Mason if (!ret) 41801259ab75SChris Mason return ret; 41811259ab75SChris Mason 41821259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4183b9fab919SChris Mason parent_transid, atomic); 4184b9fab919SChris Mason if (ret == -EAGAIN) 4185b9fab919SChris Mason return ret; 41861259ab75SChris Mason return !ret; 41875f39d397SChris Mason } 41886702ed49SChris Mason 41895f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 41905f39d397SChris Mason { 41912f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 41925f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4193b9473439SChris Mason int was_dirty; 4194b4ce94deSChris Mason 419506ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 419606ea65a3SJosef Bacik /* 419706ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 419852042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 419906ea65a3SJosef Bacik * outside of the sanity tests. 420006ea65a3SJosef Bacik */ 4201b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 420206ea65a3SJosef Bacik return; 420306ea65a3SJosef Bacik #endif 4204b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 42050b246afaSJeff Mahoney if (transid != fs_info->generation) 42065d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 42070b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 42080b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4209e2d84521SMiao Xie if (!was_dirty) 4210104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4211e2d84521SMiao Xie buf->len, 42120b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 42131f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 421469fc6cbbSQu Wenruo /* 421569fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 421669fc6cbbSQu Wenruo * but item data not updated. 421769fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 421869fc6cbbSQu Wenruo */ 421969fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4220cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4221a4f78750SDavid Sterba btrfs_print_leaf(buf); 42221f21ef0aSFilipe Manana ASSERT(0); 42231f21ef0aSFilipe Manana } 42241f21ef0aSFilipe Manana #endif 4225eb60ceacSChris Mason } 4226eb60ceacSChris Mason 42272ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4228b53d3f5dSLiu Bo int flush_delayed) 422935b7e476SChris Mason { 4230188de649SChris Mason /* 4231188de649SChris Mason * looks as though older kernels can get into trouble with 4232188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4233188de649SChris Mason */ 4234e2d84521SMiao Xie int ret; 4235d6bfde87SChris Mason 42366933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4237d6bfde87SChris Mason return; 4238d6bfde87SChris Mason 4239b53d3f5dSLiu Bo if (flush_delayed) 42402ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 424116cdcec7SMiao Xie 4242d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4243d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4244d814a491SEthan Lien fs_info->dirty_metadata_batch); 4245e2d84521SMiao Xie if (ret > 0) { 42460b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 424716cdcec7SMiao Xie } 424816cdcec7SMiao Xie } 424916cdcec7SMiao Xie 42502ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 425116cdcec7SMiao Xie { 42522ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 425335b7e476SChris Mason } 4254b53d3f5dSLiu Bo 42552ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4256b53d3f5dSLiu Bo { 42572ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4258d6bfde87SChris Mason } 42596b80053dSChris Mason 4260581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4261581c1760SQu Wenruo struct btrfs_key *first_key) 42626b80053dSChris Mason { 42635ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4264581c1760SQu Wenruo level, first_key); 42656b80053dSChris Mason } 42660da5468fSChris Mason 42672ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4268acce952bSliubo { 4269fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4270fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4271fe816d0fSNikolay Borisov 42720b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42732ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42740b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4275acce952bSliubo 42760b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42770b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4278acce952bSliubo } 4279acce952bSliubo 4280ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4281ef67963dSJosef Bacik { 4282ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4283ef67963dSJosef Bacik u64 root_objectid = 0; 4284ef67963dSJosef Bacik int ret; 4285ef67963dSJosef Bacik 4286ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4287ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4288ef67963dSJosef Bacik (void **)gang, root_objectid, 4289ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4290ef67963dSJosef Bacik int i; 4291ef67963dSJosef Bacik 4292ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4293ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4294ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4295ef67963dSJosef Bacik 4296ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4297ef67963dSJosef Bacik if (!gang[i]) 4298ef67963dSJosef Bacik continue; 4299ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4300ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4301ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4302ef67963dSJosef Bacik } 4303ef67963dSJosef Bacik root_objectid++; 4304ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4305ef67963dSJosef Bacik } 4306ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4307ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4308ef67963dSJosef Bacik } 4309ef67963dSJosef Bacik 4310143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4311acce952bSliubo { 4312acce952bSliubo struct btrfs_ordered_extent *ordered; 4313acce952bSliubo 4314199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4315779880efSJosef Bacik /* 4316779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4317779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4318779880efSJosef Bacik */ 4319199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4320779880efSJosef Bacik root_extent_list) 4321779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4322199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4323199c2a9cSMiao Xie } 4324199c2a9cSMiao Xie 4325199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4326199c2a9cSMiao Xie { 4327199c2a9cSMiao Xie struct btrfs_root *root; 4328199c2a9cSMiao Xie struct list_head splice; 4329199c2a9cSMiao Xie 4330199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4331199c2a9cSMiao Xie 4332199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4333199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4334199c2a9cSMiao Xie while (!list_empty(&splice)) { 4335199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4336199c2a9cSMiao Xie ordered_root); 43371de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 43381de2cfdeSJosef Bacik &fs_info->ordered_roots); 4339199c2a9cSMiao Xie 43402a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4341199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4342199c2a9cSMiao Xie 43432a85d9caSLiu Bo cond_resched(); 43442a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4345199c2a9cSMiao Xie } 4346199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 434774d5d229SJosef Bacik 434874d5d229SJosef Bacik /* 434974d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 435074d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 435174d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 435274d5d229SJosef Bacik * actually get run and error out properly. 435374d5d229SJosef Bacik */ 435474d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4355acce952bSliubo } 4356acce952bSliubo 435735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43582ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4359acce952bSliubo { 4360acce952bSliubo struct rb_node *node; 4361acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4362acce952bSliubo struct btrfs_delayed_ref_node *ref; 4363acce952bSliubo int ret = 0; 4364acce952bSliubo 4365acce952bSliubo delayed_refs = &trans->delayed_refs; 4366acce952bSliubo 4367acce952bSliubo spin_lock(&delayed_refs->lock); 4368d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4369cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4370b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4371acce952bSliubo return ret; 4372acce952bSliubo } 4373acce952bSliubo 43745c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4375d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 43760e0adbcfSJosef Bacik struct rb_node *n; 4377e78417d1SJosef Bacik bool pin_bytes = false; 4378acce952bSliubo 4379d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4380d7df2c79SJosef Bacik href_node); 43813069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4382b939d1abSJosef Bacik continue; 43833069bd26SJosef Bacik 4384d7df2c79SJosef Bacik spin_lock(&head->lock); 4385e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 43860e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 43870e0adbcfSJosef Bacik ref_node); 4388d7df2c79SJosef Bacik ref->in_tree = 0; 4389e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 43900e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 43911d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43921d57ee94SWang Xiaoguang list_del(&ref->add_list); 4393d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4394d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4395d7df2c79SJosef Bacik } 439654067ae9SJosef Bacik if (head->must_insert_reserved) 4397e78417d1SJosef Bacik pin_bytes = true; 439878a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4399fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4400d7df2c79SJosef Bacik spin_unlock(&head->lock); 4401acce952bSliubo spin_unlock(&delayed_refs->lock); 4402e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4403acce952bSliubo 4404f603bb94SNikolay Borisov if (pin_bytes) { 4405f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4406f603bb94SNikolay Borisov 4407f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4408f603bb94SNikolay Borisov BUG_ON(!cache); 4409f603bb94SNikolay Borisov 4410f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4411f603bb94SNikolay Borisov spin_lock(&cache->lock); 4412f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4413f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4414f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4415f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4416f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4417f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4418f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4419f603bb94SNikolay Borisov percpu_counter_add_batch( 4420f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4421f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4422f603bb94SNikolay Borisov 4423f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4424f603bb94SNikolay Borisov 4425f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4426f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4427f603bb94SNikolay Borisov } 442831890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4429d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4430acce952bSliubo cond_resched(); 4431acce952bSliubo spin_lock(&delayed_refs->lock); 4432acce952bSliubo } 443381f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4434acce952bSliubo 4435acce952bSliubo spin_unlock(&delayed_refs->lock); 4436acce952bSliubo 4437acce952bSliubo return ret; 4438acce952bSliubo } 4439acce952bSliubo 4440143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4441acce952bSliubo { 4442acce952bSliubo struct btrfs_inode *btrfs_inode; 4443acce952bSliubo struct list_head splice; 4444acce952bSliubo 4445acce952bSliubo INIT_LIST_HEAD(&splice); 4446acce952bSliubo 4447eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4448eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4449acce952bSliubo 4450acce952bSliubo while (!list_empty(&splice)) { 4451fe816d0fSNikolay Borisov struct inode *inode = NULL; 4452eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4453acce952bSliubo delalloc_inodes); 4454fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4455eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4456acce952bSliubo 4457fe816d0fSNikolay Borisov /* 4458fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4459fe816d0fSNikolay Borisov * meanwhile. 4460fe816d0fSNikolay Borisov */ 4461fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4462fe816d0fSNikolay Borisov if (inode) { 4463fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4464fe816d0fSNikolay Borisov iput(inode); 4465fe816d0fSNikolay Borisov } 4466eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4467acce952bSliubo } 4468eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4469eb73c1b7SMiao Xie } 4470eb73c1b7SMiao Xie 4471eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4472eb73c1b7SMiao Xie { 4473eb73c1b7SMiao Xie struct btrfs_root *root; 4474eb73c1b7SMiao Xie struct list_head splice; 4475eb73c1b7SMiao Xie 4476eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4477eb73c1b7SMiao Xie 4478eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4479eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4480eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4481eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4482eb73c1b7SMiao Xie delalloc_root); 448300246528SJosef Bacik root = btrfs_grab_root(root); 4484eb73c1b7SMiao Xie BUG_ON(!root); 4485eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4486eb73c1b7SMiao Xie 4487eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 448800246528SJosef Bacik btrfs_put_root(root); 4489eb73c1b7SMiao Xie 4490eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4491eb73c1b7SMiao Xie } 4492eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4493acce952bSliubo } 4494acce952bSliubo 44952ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4496acce952bSliubo struct extent_io_tree *dirty_pages, 4497acce952bSliubo int mark) 4498acce952bSliubo { 4499acce952bSliubo int ret; 4500acce952bSliubo struct extent_buffer *eb; 4501acce952bSliubo u64 start = 0; 4502acce952bSliubo u64 end; 4503acce952bSliubo 4504acce952bSliubo while (1) { 4505acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4506e6138876SJosef Bacik mark, NULL); 4507acce952bSliubo if (ret) 4508acce952bSliubo break; 4509acce952bSliubo 451091166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4511acce952bSliubo while (start <= end) { 45120b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 45130b246afaSJeff Mahoney start += fs_info->nodesize; 4514fd8b2b61SJosef Bacik if (!eb) 4515acce952bSliubo continue; 4516fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4517acce952bSliubo 4518fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4519fd8b2b61SJosef Bacik &eb->bflags)) 4520fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4521fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4522acce952bSliubo } 4523acce952bSliubo } 4524acce952bSliubo 4525acce952bSliubo return ret; 4526acce952bSliubo } 4527acce952bSliubo 45282ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4529fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4530acce952bSliubo { 4531acce952bSliubo u64 start; 4532acce952bSliubo u64 end; 4533acce952bSliubo int ret; 4534acce952bSliubo 4535acce952bSliubo while (1) { 45360e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 45370e6ec385SFilipe Manana 4538fcd5e742SLu Fengqi /* 4539fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4540fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4541fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4542fcd5e742SLu Fengqi * the same extent range. 4543fcd5e742SLu Fengqi */ 4544fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4545acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 45460e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4547fcd5e742SLu Fengqi if (ret) { 4548fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4549acce952bSliubo break; 4550fcd5e742SLu Fengqi } 4551acce952bSliubo 45520e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 45530e6ec385SFilipe Manana free_extent_state(cached_state); 45542ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4555fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4556acce952bSliubo cond_resched(); 4557acce952bSliubo } 4558acce952bSliubo 4559acce952bSliubo return 0; 4560acce952bSliubo } 4561acce952bSliubo 456232da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4563c79a1751SLiu Bo { 4564c79a1751SLiu Bo struct inode *inode; 4565c79a1751SLiu Bo 4566c79a1751SLiu Bo inode = cache->io_ctl.inode; 4567c79a1751SLiu Bo if (inode) { 4568c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4569c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4570c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4571c79a1751SLiu Bo iput(inode); 4572c79a1751SLiu Bo } 4573bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 4574c79a1751SLiu Bo btrfs_put_block_group(cache); 4575c79a1751SLiu Bo } 4576c79a1751SLiu Bo 4577c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45782ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4579c79a1751SLiu Bo { 458032da5386SDavid Sterba struct btrfs_block_group *cache; 4581c79a1751SLiu Bo 4582c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4583c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4584c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 458532da5386SDavid Sterba struct btrfs_block_group, 4586c79a1751SLiu Bo dirty_list); 4587c79a1751SLiu Bo 4588c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4589c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4590c79a1751SLiu Bo list_del_init(&cache->io_list); 4591c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4592c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4593c79a1751SLiu Bo } 4594c79a1751SLiu Bo 4595c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4596c79a1751SLiu Bo spin_lock(&cache->lock); 4597c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4598c79a1751SLiu Bo spin_unlock(&cache->lock); 4599c79a1751SLiu Bo 4600c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4601c79a1751SLiu Bo btrfs_put_block_group(cache); 4602ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4603c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4604c79a1751SLiu Bo } 4605c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4606c79a1751SLiu Bo 460745ae2c18SNikolay Borisov /* 460845ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 460945ae2c18SNikolay Borisov * to use it without any locking 461045ae2c18SNikolay Borisov */ 4611c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4612c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 461332da5386SDavid Sterba struct btrfs_block_group, 4614c79a1751SLiu Bo io_list); 4615c79a1751SLiu Bo 4616c79a1751SLiu Bo list_del_init(&cache->io_list); 4617c79a1751SLiu Bo spin_lock(&cache->lock); 4618c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4619c79a1751SLiu Bo spin_unlock(&cache->lock); 4620c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4621c79a1751SLiu Bo } 4622c79a1751SLiu Bo } 4623c79a1751SLiu Bo 462449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 46252ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 462649b25e05SJeff Mahoney { 4627bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4628bbbf7243SNikolay Borisov 46292ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4630c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4631c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4632c79a1751SLiu Bo 4633bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4634bbbf7243SNikolay Borisov post_commit_list) { 4635bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4636bbbf7243SNikolay Borisov } 4637bbbf7243SNikolay Borisov 46382ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 463949b25e05SJeff Mahoney 46404a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 46410b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 464249b25e05SJeff Mahoney 46434a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 46440b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 464549b25e05SJeff Mahoney 4646ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 464767cde344SMiao Xie 46482ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 464949b25e05SJeff Mahoney EXTENT_DIRTY); 4650fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 465149b25e05SJeff Mahoney 46524a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 46534a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 465449b25e05SJeff Mahoney } 465549b25e05SJeff Mahoney 46562ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4657acce952bSliubo { 4658acce952bSliubo struct btrfs_transaction *t; 4659acce952bSliubo 46600b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4661acce952bSliubo 46620b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46630b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46640b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4665724e2315SJosef Bacik struct btrfs_transaction, list); 4666724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 46679b64f57dSElena Reshetova refcount_inc(&t->use_count); 46680b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46692ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4670724e2315SJosef Bacik btrfs_put_transaction(t); 46710b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4672724e2315SJosef Bacik continue; 4673724e2315SJosef Bacik } 46740b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4675724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46760b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4677724e2315SJosef Bacik /* 4678724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4679724e2315SJosef Bacik * handle open currently for this transaction. 4680724e2315SJosef Bacik */ 4681724e2315SJosef Bacik wait_event(t->writer_wait, 4682724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4683724e2315SJosef Bacik } else { 46840b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4685724e2315SJosef Bacik } 46862ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4687724e2315SJosef Bacik 46880b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46890b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46900b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4691724e2315SJosef Bacik list_del_init(&t->list); 46920b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4693a4abeea4SJosef Bacik 4694724e2315SJosef Bacik btrfs_put_transaction(t); 46952ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46960b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4697724e2315SJosef Bacik } 46980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46990b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4700ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4701ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 47020b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 4703ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 47040b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4705acce952bSliubo 4706acce952bSliubo return 0; 4707acce952bSliubo } 4708