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" 3221adbd5cSStefan Behrens #include "check-integrity.h" 33606686eeSJosef Bacik #include "rcu-string.h" 348dabb742SStefan Behrens #include "dev-replace.h" 3553b381b3SDavid Woodhouse #include "raid56.h" 365ac1d209SJeff Mahoney #include "sysfs.h" 37fcebe456SJosef Bacik #include "qgroup.h" 38ebb8765bSAnand Jain #include "compression.h" 39557ea5ddSQu Wenruo #include "tree-checker.h" 40fd708b81SJosef Bacik #include "ref-verify.h" 41aac0023cSJosef Bacik #include "block-group.h" 42b0643e59SDennis Zhou #include "discard.h" 43f603bb94SNikolay Borisov #include "space-info.h" 44b70f5097SNaohiro Aota #include "zoned.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; 1161941b64bSQu Wenruo 1171941b64bSQu Wenruo /* Optional parameter for submit_bio_start used by direct io */ 1181941b64bSQu Wenruo u64 dio_file_offset; 1198b712842SChris Mason struct btrfs_work work; 1204e4cbee9SChristoph Hellwig blk_status_t status; 12144b8bd7eSChris Mason }; 12244b8bd7eSChris Mason 12385d4e461SChris Mason /* 12485d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12585d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12685d4e461SChris Mason * the level the eb occupies in the tree. 1274008c04aSChris Mason * 12885d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12985d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13085d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1314fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1324fd786e6SMisono Tomohiro * roots have separate keysets. 1334008c04aSChris Mason * 13485d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13585d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13685d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1374008c04aSChris Mason * 13885d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13985d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14085d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14185d4e461SChris Mason * 14285d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14385d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14485d4e461SChris Mason * needs update as well. 1454008c04aSChris Mason */ 1464008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1474008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1484008c04aSChris Mason # error 1494008c04aSChris Mason # endif 15085d4e461SChris Mason 151ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 152ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 153ab1405aaSDavid Sterba 154ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 155ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 156ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 157ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 163ab1405aaSDavid Sterba 16485d4e461SChris Mason static struct btrfs_lockdep_keyset { 16585d4e461SChris Mason u64 id; /* root objectid */ 166ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 167387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 168387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 16985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 170ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 171ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 172ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 173ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 174ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 175ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 176ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 177ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 178ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 179ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 180ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 181ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1824008c04aSChris Mason }; 18385d4e461SChris Mason 184ab1405aaSDavid Sterba #undef DEFINE_LEVEL 185ab1405aaSDavid Sterba #undef DEFINE_NAME 18685d4e461SChris Mason 18785d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18885d4e461SChris Mason int level) 18985d4e461SChris Mason { 19085d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19185d4e461SChris Mason 19285d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19385d4e461SChris Mason 19485d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19585d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19685d4e461SChris Mason if (ks->id == objectid) 19785d4e461SChris Mason break; 19885d4e461SChris Mason 19985d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20085d4e461SChris Mason &ks->keys[level], ks->names[level]); 20185d4e461SChris Mason } 20285d4e461SChris Mason 2034008c04aSChris Mason #endif 2044008c04aSChris Mason 205d352ac68SChris Mason /* 2062996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 207d352ac68SChris Mason */ 208c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 20919c00ddcSChris Mason { 210d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 211e9be5a30SDavid Sterba const int num_pages = fs_info->nodesize >> PAGE_SHIFT; 212a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 213d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 21419c00ddcSChris Mason char *kaddr; 215e9be5a30SDavid Sterba int i; 216d5178578SJohannes Thumshirn 217d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 218d5178578SJohannes Thumshirn crypto_shash_init(shash); 219a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 220e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 221a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 22219c00ddcSChris Mason 223e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 224e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 225e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 22619c00ddcSChris Mason } 22771a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 228d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 22919c00ddcSChris Mason } 23019c00ddcSChris Mason 231d352ac68SChris Mason /* 232d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 233d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 234d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 235d352ac68SChris Mason * in the wrong place. 236d352ac68SChris Mason */ 2371259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 238b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 239b9fab919SChris Mason int atomic) 2401259ab75SChris Mason { 2412ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2421259ab75SChris Mason int ret; 2432755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 2441259ab75SChris Mason 2451259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2461259ab75SChris Mason return 0; 2471259ab75SChris Mason 248b9fab919SChris Mason if (atomic) 249b9fab919SChris Mason return -EAGAIN; 250b9fab919SChris Mason 251ac5887c8SJosef Bacik if (need_lock) 252a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 253a26e8c9fSJosef Bacik 2542ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 255ff13db41SDavid Sterba &cached_state); 2560b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2571259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2581259ab75SChris Mason ret = 0; 2591259ab75SChris Mason goto out; 2601259ab75SChris Mason } 26194647322SDavid Sterba btrfs_err_rl(eb->fs_info, 26294647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 26394647322SDavid Sterba eb->start, 26429549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2651259ab75SChris Mason ret = 1; 266a26e8c9fSJosef Bacik 267a26e8c9fSJosef Bacik /* 268a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 269a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 27001327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 271a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 272a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 273a26e8c9fSJosef Bacik * making everybody very sad. 274a26e8c9fSJosef Bacik */ 275a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 2760b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 27733958dc6SChris Mason out: 2782ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 279e43bbe5eSDavid Sterba &cached_state); 280472b909fSJosef Bacik if (need_lock) 281ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 2821259ab75SChris Mason return ret; 2831259ab75SChris Mason } 2841259ab75SChris Mason 285e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 286e7e16f48SJohannes Thumshirn { 287e7e16f48SJohannes Thumshirn switch (csum_type) { 288e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2893951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2903831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 291352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 292e7e16f48SJohannes Thumshirn return true; 293e7e16f48SJohannes Thumshirn default: 294e7e16f48SJohannes Thumshirn return false; 295e7e16f48SJohannes Thumshirn } 296e7e16f48SJohannes Thumshirn } 297e7e16f48SJohannes Thumshirn 298d352ac68SChris Mason /* 2991104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3001104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3011104a885SDavid Sterba */ 302ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 303ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3041104a885SDavid Sterba { 3051104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3061104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 30751bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 308d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 309d5178578SJohannes Thumshirn 310d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3111104a885SDavid Sterba 3121104a885SDavid Sterba /* 3131104a885SDavid Sterba * The super_block structure does not span the whole 31451bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 31551bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3161104a885SDavid Sterba */ 317fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 318fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3191104a885SDavid Sterba 32055fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 321e7e16f48SJohannes Thumshirn return 1; 3221104a885SDavid Sterba 323e7e16f48SJohannes Thumshirn return 0; 3241104a885SDavid Sterba } 3251104a885SDavid Sterba 326e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 327ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 328581c1760SQu Wenruo { 329e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 330581c1760SQu Wenruo int found_level; 331581c1760SQu Wenruo struct btrfs_key found_key; 332581c1760SQu Wenruo int ret; 333581c1760SQu Wenruo 334581c1760SQu Wenruo found_level = btrfs_header_level(eb); 335581c1760SQu Wenruo if (found_level != level) { 33663489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 33763489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 338581c1760SQu Wenruo btrfs_err(fs_info, 339581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 340581c1760SQu Wenruo eb->start, level, found_level); 341581c1760SQu Wenruo return -EIO; 342581c1760SQu Wenruo } 343581c1760SQu Wenruo 344581c1760SQu Wenruo if (!first_key) 345581c1760SQu Wenruo return 0; 346581c1760SQu Wenruo 3475d41be6fSQu Wenruo /* 3485d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3495d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3505d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3515d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3525d41be6fSQu Wenruo */ 3535d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3545d41be6fSQu Wenruo return 0; 35562fdaa52SQu Wenruo 35662fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 35762fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 35862fdaa52SQu Wenruo btrfs_err(fs_info, 35962fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 36062fdaa52SQu Wenruo eb->start); 36162fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 36262fdaa52SQu Wenruo return -EUCLEAN; 36362fdaa52SQu Wenruo } 36462fdaa52SQu Wenruo 365581c1760SQu Wenruo if (found_level) 366581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 367581c1760SQu Wenruo else 368581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 369581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 370581c1760SQu Wenruo 371581c1760SQu Wenruo if (ret) { 37263489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 37363489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 374581c1760SQu Wenruo btrfs_err(fs_info, 375ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 376ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 377ff76a864SLiu Bo first_key->type, first_key->offset, 378ff76a864SLiu Bo found_key.objectid, found_key.type, 379ff76a864SLiu Bo found_key.offset); 380581c1760SQu Wenruo } 381581c1760SQu Wenruo return ret; 382581c1760SQu Wenruo } 383581c1760SQu Wenruo 3841104a885SDavid Sterba /* 385d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 386d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 387581c1760SQu Wenruo * 388581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 389581c1760SQu Wenruo * @level: expected level, mandatory check 390581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 391d352ac68SChris Mason */ 3925ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 393581c1760SQu Wenruo u64 parent_transid, int level, 394581c1760SQu Wenruo struct btrfs_key *first_key) 395f188591eSChris Mason { 3965ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 397f188591eSChris Mason struct extent_io_tree *io_tree; 398ea466794SJosef Bacik int failed = 0; 399f188591eSChris Mason int ret; 400f188591eSChris Mason int num_copies = 0; 401f188591eSChris Mason int mirror_num = 0; 402ea466794SJosef Bacik int failed_mirror = 0; 403f188591eSChris Mason 4040b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 405f188591eSChris Mason while (1) { 406f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 407c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 408256dd1bbSStefan Behrens if (!ret) { 409581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 410b9fab919SChris Mason parent_transid, 0)) 411256dd1bbSStefan Behrens ret = -EIO; 412e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 413ff76a864SLiu Bo first_key, parent_transid)) 414581c1760SQu Wenruo ret = -EUCLEAN; 415581c1760SQu Wenruo else 416581c1760SQu Wenruo break; 417256dd1bbSStefan Behrens } 418d397712bSChris Mason 4190b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 420f188591eSChris Mason eb->start, eb->len); 4214235298eSChris Mason if (num_copies == 1) 422ea466794SJosef Bacik break; 4234235298eSChris Mason 4245cf1ab56SJosef Bacik if (!failed_mirror) { 4255cf1ab56SJosef Bacik failed = 1; 4265cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4275cf1ab56SJosef Bacik } 4285cf1ab56SJosef Bacik 429f188591eSChris Mason mirror_num++; 430ea466794SJosef Bacik if (mirror_num == failed_mirror) 431ea466794SJosef Bacik mirror_num++; 432ea466794SJosef Bacik 4334235298eSChris Mason if (mirror_num > num_copies) 434ea466794SJosef Bacik break; 435f188591eSChris Mason } 436ea466794SJosef Bacik 437c0901581SStefan Behrens if (failed && !ret && failed_mirror) 43820a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 439ea466794SJosef Bacik 440ea466794SJosef Bacik return ret; 441f188591eSChris Mason } 44219c00ddcSChris Mason 443d352ac68SChris Mason /* 444ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 445ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 446ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 447d352ac68SChris Mason */ 448ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 44919c00ddcSChris Mason { 450ac303b69SQu Wenruo struct page *page = bvec->bv_page; 4514eee4fa4SMiao Xie u64 start = page_offset(page); 45219c00ddcSChris Mason u64 found_start; 4532996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 45419c00ddcSChris Mason struct extent_buffer *eb; 4558d47a0d8SQu Wenruo int ret; 456f188591eSChris Mason 4574f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4584f2de97aSJosef Bacik if (page != eb->pages[0]) 4594f2de97aSJosef Bacik return 0; 4600f805531SAlex Lyakas 46119c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 4620f805531SAlex Lyakas /* 4630f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4640f805531SAlex Lyakas * It is useful to know what went wrong. 4650f805531SAlex Lyakas */ 4660f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4670f805531SAlex Lyakas return -EUCLEAN; 4680f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4690f805531SAlex Lyakas return -EUCLEAN; 4700f805531SAlex Lyakas 471de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 4729a8658e3SDavid Sterba offsetof(struct btrfs_header, fsid), 4739a8658e3SDavid Sterba BTRFS_FSID_SIZE) == 0); 4740f805531SAlex Lyakas 475c67b3892SDavid Sterba csum_tree_block(eb, result); 4762996e1f8SJohannes Thumshirn 4778d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 4788d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 4798d47a0d8SQu Wenruo else 4808d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 4818d47a0d8SQu Wenruo 4828d47a0d8SQu Wenruo if (ret < 0) { 483c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 4848d47a0d8SQu Wenruo btrfs_err(fs_info, 4858d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 4868d47a0d8SQu Wenruo eb->start); 487c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4888d47a0d8SQu Wenruo return ret; 4898d47a0d8SQu Wenruo } 490713cebfbSDavid Sterba write_extent_buffer(eb, result, 0, fs_info->csum_size); 4918d47a0d8SQu Wenruo 4922996e1f8SJohannes Thumshirn return 0; 49319c00ddcSChris Mason } 49419c00ddcSChris Mason 495b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 4962b82032cSYan Zheng { 497b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 498944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 49944880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 500944d3f9fSNikolay Borisov u8 *metadata_uuid; 5012b82032cSYan Zheng 5029a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5039a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5047239ff4bSNikolay Borisov /* 505944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 506944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 507944d3f9fSNikolay Borisov * ->fsid in this case is fine 5087239ff4bSNikolay Borisov */ 509944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5107239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5117239ff4bSNikolay Borisov else 5127239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5137239ff4bSNikolay Borisov 514944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 515944d3f9fSNikolay Borisov return 0; 516944d3f9fSNikolay Borisov 517944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 518944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 519944d3f9fSNikolay Borisov return 0; 520944d3f9fSNikolay Borisov 521944d3f9fSNikolay Borisov return 1; 5222b82032cSYan Zheng } 5232b82032cSYan Zheng 52477bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 52577bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 526ce9adaa5SChris Mason { 52777bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 528ce9adaa5SChris Mason u64 found_start; 52977bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 53077bf40a2SQu Wenruo u8 found_level; 5312996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 53277bf40a2SQu Wenruo int ret = 0; 533ea466794SJosef Bacik 534ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 535727011e0SChris Mason if (found_start != eb->start) { 536893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 537893bf4b1SSu Yue eb->start, found_start); 538f188591eSChris Mason ret = -EIO; 53977bf40a2SQu Wenruo goto out; 540ce9adaa5SChris Mason } 541b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 54202873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 54394647322SDavid Sterba eb->start); 5441259ab75SChris Mason ret = -EIO; 54577bf40a2SQu Wenruo goto out; 5461259ab75SChris Mason } 547ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5481c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 549893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 550893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5511c24c3ceSJosef Bacik ret = -EIO; 55277bf40a2SQu Wenruo goto out; 5531c24c3ceSJosef Bacik } 554ce9adaa5SChris Mason 555c67b3892SDavid Sterba csum_tree_block(eb, result); 556a826d6dcSJosef Bacik 5572996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 55835be8851SJohannes Thumshirn u8 val[BTRFS_CSUM_SIZE] = { 0 }; 5592996e1f8SJohannes Thumshirn 5602996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 5612996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 56235be8851SJohannes Thumshirn "%s checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5632996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 56435be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, val), 56535be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 56635be8851SJohannes Thumshirn btrfs_header_level(eb)); 5672996e1f8SJohannes Thumshirn ret = -EUCLEAN; 56877bf40a2SQu Wenruo goto out; 5692996e1f8SJohannes Thumshirn } 5702996e1f8SJohannes Thumshirn 571a826d6dcSJosef Bacik /* 572a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 573a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 574a826d6dcSJosef Bacik * return -EIO. 575a826d6dcSJosef Bacik */ 5761c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 577a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 578a826d6dcSJosef Bacik ret = -EIO; 579a826d6dcSJosef Bacik } 580ce9adaa5SChris Mason 581813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 582053ab70fSLiu Bo ret = -EIO; 583053ab70fSLiu Bo 5840b32f4bbSJosef Bacik if (!ret) 5850b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 58675391f0dSQu Wenruo else 58775391f0dSQu Wenruo btrfs_err(fs_info, 58875391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 58975391f0dSQu Wenruo eb->start); 59077bf40a2SQu Wenruo out: 59177bf40a2SQu Wenruo return ret; 59277bf40a2SQu Wenruo } 59377bf40a2SQu Wenruo 594371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 595371cdc07SQu Wenruo int mirror) 596371cdc07SQu Wenruo { 597371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 598371cdc07SQu Wenruo struct extent_buffer *eb; 599371cdc07SQu Wenruo bool reads_done; 600371cdc07SQu Wenruo int ret = 0; 601371cdc07SQu Wenruo 602371cdc07SQu Wenruo /* 603371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 604371cdc07SQu Wenruo * only get one eb for each endio hook. 605371cdc07SQu Wenruo */ 606371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 607371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 608371cdc07SQu Wenruo 609371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 610371cdc07SQu Wenruo /* 611371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 612371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 613371cdc07SQu Wenruo */ 614371cdc07SQu Wenruo ASSERT(eb); 615371cdc07SQu Wenruo 616371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 617371cdc07SQu Wenruo /* Subpage read must finish in page read */ 618371cdc07SQu Wenruo ASSERT(reads_done); 619371cdc07SQu Wenruo 620371cdc07SQu Wenruo eb->read_mirror = mirror; 621371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 622371cdc07SQu Wenruo ret = -EIO; 623371cdc07SQu Wenruo goto err; 624371cdc07SQu Wenruo } 625371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 626371cdc07SQu Wenruo if (ret < 0) 627371cdc07SQu Wenruo goto err; 628371cdc07SQu Wenruo 629371cdc07SQu Wenruo if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 630371cdc07SQu Wenruo btree_readahead_hook(eb, ret); 631371cdc07SQu Wenruo 632371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 633371cdc07SQu Wenruo 634371cdc07SQu Wenruo free_extent_buffer(eb); 635371cdc07SQu Wenruo return ret; 636371cdc07SQu Wenruo err: 637371cdc07SQu Wenruo /* 638371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 639371cdc07SQu Wenruo * make sure it has something to decrement. 640371cdc07SQu Wenruo */ 641371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 642371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 643371cdc07SQu Wenruo free_extent_buffer(eb); 644371cdc07SQu Wenruo return ret; 645371cdc07SQu Wenruo } 646371cdc07SQu Wenruo 6478e1dc982SQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, 64877bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 64977bf40a2SQu Wenruo int mirror) 65077bf40a2SQu Wenruo { 65177bf40a2SQu Wenruo struct extent_buffer *eb; 65277bf40a2SQu Wenruo int ret = 0; 65377bf40a2SQu Wenruo int reads_done; 65477bf40a2SQu Wenruo 65577bf40a2SQu Wenruo ASSERT(page->private); 656371cdc07SQu Wenruo 657371cdc07SQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE) 658371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 659371cdc07SQu Wenruo 66077bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 66177bf40a2SQu Wenruo 66277bf40a2SQu Wenruo /* 66377bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 66477bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 66577bf40a2SQu Wenruo */ 66677bf40a2SQu Wenruo atomic_inc(&eb->refs); 66777bf40a2SQu Wenruo 66877bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 66977bf40a2SQu Wenruo if (!reads_done) 67077bf40a2SQu Wenruo goto err; 67177bf40a2SQu Wenruo 67277bf40a2SQu Wenruo eb->read_mirror = mirror; 67377bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 67477bf40a2SQu Wenruo ret = -EIO; 67577bf40a2SQu Wenruo goto err; 67677bf40a2SQu Wenruo } 67777bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 678ce9adaa5SChris Mason err: 67979fb65a1SJosef Bacik if (reads_done && 68079fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 681d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6824bb31e92SArne Jansen 68353b381b3SDavid Woodhouse if (ret) { 68453b381b3SDavid Woodhouse /* 68553b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 68653b381b3SDavid Woodhouse * again, we have to make sure it has something 68753b381b3SDavid Woodhouse * to decrement 68853b381b3SDavid Woodhouse */ 68953b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6900b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 69153b381b3SDavid Woodhouse } 6920b32f4bbSJosef Bacik free_extent_buffer(eb); 69377bf40a2SQu Wenruo 694f188591eSChris Mason return ret; 695ce9adaa5SChris Mason } 696ce9adaa5SChris Mason 6974246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 698ce9adaa5SChris Mason { 69997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 700ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7019e0af237SLiu Bo struct btrfs_workqueue *wq; 702ce9adaa5SChris Mason 703ce9adaa5SChris Mason fs_info = end_io_wq->info; 7044e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 705d20f7043SChris Mason 70637226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 707a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7089e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 709a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7109e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 711a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7129e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 713a0cac0ecSOmar Sandoval else 7149e0af237SLiu Bo wq = fs_info->endio_write_workers; 7159e0af237SLiu Bo } else { 7165c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7179e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 718a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7199e0af237SLiu Bo wq = fs_info->endio_meta_workers; 720a0cac0ecSOmar Sandoval else 7219e0af237SLiu Bo wq = fs_info->endio_workers; 7229e0af237SLiu Bo } 7239e0af237SLiu Bo 724a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7259e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 726ce9adaa5SChris Mason } 727ce9adaa5SChris Mason 7284e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 729bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7300b86a832SChris Mason { 73197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7328b110e39SMiao Xie 73397eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 734ce9adaa5SChris Mason if (!end_io_wq) 7354e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 736ce9adaa5SChris Mason 737ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 738ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 73922c59948SChris Mason end_io_wq->info = info; 7404e4cbee9SChristoph Hellwig end_io_wq->status = 0; 741ce9adaa5SChris Mason end_io_wq->bio = bio; 74222c59948SChris Mason end_io_wq->metadata = metadata; 743ce9adaa5SChris Mason 744ce9adaa5SChris Mason bio->bi_private = end_io_wq; 745ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 74622c59948SChris Mason return 0; 74722c59948SChris Mason } 74822c59948SChris Mason 7494a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7504a69a410SChris Mason { 7514a69a410SChris Mason struct async_submit_bio *async; 7524e4cbee9SChristoph Hellwig blk_status_t ret; 7534a69a410SChris Mason 7544a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7551941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 7561941b64bSQu Wenruo async->dio_file_offset); 75779787eaaSJeff Mahoney if (ret) 7584e4cbee9SChristoph Hellwig async->status = ret; 7594a69a410SChris Mason } 7604a69a410SChris Mason 76106ea01b1SDavid Sterba /* 76206ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 76306ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 76406ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 76506ea01b1SDavid Sterba * 76606ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 76706ea01b1SDavid Sterba * inserted into the tree. 76806ea01b1SDavid Sterba */ 7694a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7708b712842SChris Mason { 7718b712842SChris Mason struct async_submit_bio *async; 77206ea01b1SDavid Sterba struct inode *inode; 77306ea01b1SDavid Sterba blk_status_t ret; 7748b712842SChris Mason 7758b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7768896a08dSQu Wenruo inode = async->inode; 7774854ddd0SChris Mason 778bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7794e4cbee9SChristoph Hellwig if (async->status) { 7804e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7814246a0b6SChristoph Hellwig bio_endio(async->bio); 78279787eaaSJeff Mahoney return; 78379787eaaSJeff Mahoney } 78479787eaaSJeff Mahoney 785ec39f769SChris Mason /* 786ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 787ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 788ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 789ec39f769SChris Mason */ 790ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 79108635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 79206ea01b1SDavid Sterba if (ret) { 79306ea01b1SDavid Sterba async->bio->bi_status = ret; 79406ea01b1SDavid Sterba bio_endio(async->bio); 79506ea01b1SDavid Sterba } 7964a69a410SChris Mason } 7974a69a410SChris Mason 7984a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7994a69a410SChris Mason { 8004a69a410SChris Mason struct async_submit_bio *async; 8014a69a410SChris Mason 8024a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8038b712842SChris Mason kfree(async); 8048b712842SChris Mason } 8058b712842SChris Mason 8068896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 807c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 8081941b64bSQu Wenruo u64 dio_file_offset, 809e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 81044b8bd7eSChris Mason { 8118896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 81244b8bd7eSChris Mason struct async_submit_bio *async; 81344b8bd7eSChris Mason 81444b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81544b8bd7eSChris Mason if (!async) 8164e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 81744b8bd7eSChris Mason 8188896a08dSQu Wenruo async->inode = inode; 81944b8bd7eSChris Mason async->bio = bio; 82044b8bd7eSChris Mason async->mirror_num = mirror_num; 8214a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8224a69a410SChris Mason 823a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 824a0cac0ecSOmar Sandoval run_one_async_free); 8254a69a410SChris Mason 8261941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8278c8bee1dSChris Mason 8284e4cbee9SChristoph Hellwig async->status = 0; 82979787eaaSJeff Mahoney 83067f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8315cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 832d313d7a3SChris Mason 8335cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 83444b8bd7eSChris Mason return 0; 83544b8bd7eSChris Mason } 83644b8bd7eSChris Mason 8374e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 838ce3ed71aSChris Mason { 8392c30c71bSKent Overstreet struct bio_vec *bvec; 840ce3ed71aSChris Mason struct btrfs_root *root; 8412b070cfeSChristoph Hellwig int ret = 0; 8426dc4f100SMing Lei struct bvec_iter_all iter_all; 843ce3ed71aSChris Mason 844c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8452b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 846ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 847ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 84879787eaaSJeff Mahoney if (ret) 84979787eaaSJeff Mahoney break; 850ce3ed71aSChris Mason } 8512c30c71bSKent Overstreet 8524e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 853ce3ed71aSChris Mason } 854ce3ed71aSChris Mason 8558896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 8561941b64bSQu Wenruo u64 dio_file_offset) 85722c59948SChris Mason { 8588b712842SChris Mason /* 8598b712842SChris Mason * when we're called for a write, we're already in the async 8605443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8618b712842SChris Mason */ 86279787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86322c59948SChris Mason } 86422c59948SChris Mason 8659b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8669b4e675aSDavid Sterba struct btrfs_inode *bi) 867de0022b9SJosef Bacik { 8686300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8696300463bSLiu Bo return 0; 8709b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 871de0022b9SJosef Bacik return 0; 872de0022b9SJosef Bacik return 1; 873de0022b9SJosef Bacik } 874de0022b9SJosef Bacik 8751b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 8761b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 87744b8bd7eSChris Mason { 8780b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8799b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8804e4cbee9SChristoph Hellwig blk_status_t ret; 881cad321adSChris Mason 88237226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 883cad321adSChris Mason /* 884cad321adSChris Mason * called for a read, do the setup so that checksum validation 885cad321adSChris Mason * can happen in the async kernel threads 886cad321adSChris Mason */ 8870b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8880b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8891d4284bdSChris Mason if (ret) 89061891923SStefan Behrens goto out_w_error; 89108635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 892de0022b9SJosef Bacik } else if (!async) { 893de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 894de0022b9SJosef Bacik if (ret) 89561891923SStefan Behrens goto out_w_error; 89608635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 89761891923SStefan Behrens } else { 898cad321adSChris Mason /* 89961891923SStefan Behrens * kthread helpers are used to submit writes so that 90061891923SStefan Behrens * checksumming can happen in parallel across all CPUs 901cad321adSChris Mason */ 9028896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 9038896a08dSQu Wenruo 0, btree_submit_bio_start); 90444b8bd7eSChris Mason } 90544b8bd7eSChris Mason 9064246a0b6SChristoph Hellwig if (ret) 9074246a0b6SChristoph Hellwig goto out_w_error; 9084246a0b6SChristoph Hellwig return 0; 9094246a0b6SChristoph Hellwig 91061891923SStefan Behrens out_w_error: 9114e4cbee9SChristoph Hellwig bio->bi_status = ret; 9124246a0b6SChristoph Hellwig bio_endio(bio); 91361891923SStefan Behrens return ret; 91461891923SStefan Behrens } 91561891923SStefan Behrens 9163dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 917784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 918a6bc32b8SMel Gorman struct page *newpage, struct page *page, 919a6bc32b8SMel Gorman enum migrate_mode mode) 920784b4e29SChris Mason { 921784b4e29SChris Mason /* 922784b4e29SChris Mason * we can't safely write a btree page from here, 923784b4e29SChris Mason * we haven't done the locking hook 924784b4e29SChris Mason */ 925784b4e29SChris Mason if (PageDirty(page)) 926784b4e29SChris Mason return -EAGAIN; 927784b4e29SChris Mason /* 928784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 929784b4e29SChris Mason * We must have no buffers or drop them. 930784b4e29SChris Mason */ 931784b4e29SChris Mason if (page_has_private(page) && 932784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 933784b4e29SChris Mason return -EAGAIN; 934a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 935784b4e29SChris Mason } 9363dd1462eSJan Beulich #endif 937784b4e29SChris Mason 9380da5468fSChris Mason 9390da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9400da5468fSChris Mason struct writeback_control *wbc) 9410da5468fSChris Mason { 942e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 943e2d84521SMiao Xie int ret; 944e2d84521SMiao Xie 945d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 946448d640bSChris Mason 947448d640bSChris Mason if (wbc->for_kupdate) 948448d640bSChris Mason return 0; 949448d640bSChris Mason 950e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 951b9473439SChris Mason /* this is a bit racy, but that's ok */ 952d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 953d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 954d814a491SEthan Lien fs_info->dirty_metadata_batch); 955e2d84521SMiao Xie if (ret < 0) 956793955bcSChris Mason return 0; 957793955bcSChris Mason } 9580b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9590da5468fSChris Mason } 9600da5468fSChris Mason 96170dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9625f39d397SChris Mason { 96398509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 96498509cfcSChris Mason return 0; 9650c4e538bSDavid Sterba 966f7a52a40SDavid Sterba return try_release_extent_buffer(page); 967d98237b3SChris Mason } 968d98237b3SChris Mason 969d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 970d47992f8SLukas Czerner unsigned int length) 971d98237b3SChris Mason { 972d1310b2eSChris Mason struct extent_io_tree *tree; 973d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9745f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9755f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9769ad6b7bcSChris Mason if (PagePrivate(page)) { 977efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 978efe120a0SFrank Holton "page private not zero on page %llu", 979efe120a0SFrank Holton (unsigned long long)page_offset(page)); 980d1b89bc0SGuoqing Jiang detach_page_private(page); 9819ad6b7bcSChris Mason } 982d98237b3SChris Mason } 983d98237b3SChris Mason 9840b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9850b32f4bbSJosef Bacik { 986bb146eb2SJosef Bacik #ifdef DEBUG 9870b32f4bbSJosef Bacik struct extent_buffer *eb; 9880b32f4bbSJosef Bacik 9890b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9900b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9910b32f4bbSJosef Bacik BUG_ON(!eb); 9920b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9930b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9940b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 995bb146eb2SJosef Bacik #endif 9960b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9970b32f4bbSJosef Bacik } 9980b32f4bbSJosef Bacik 9997f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10000da5468fSChris Mason .writepages = btree_writepages, 10015f39d397SChris Mason .releasepage = btree_releasepage, 10025f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10035a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1004784b4e29SChris Mason .migratepage = btree_migratepage, 10055a92bc88SChris Mason #endif 10060b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1007d98237b3SChris Mason }; 1008123abc88SChris Mason 10092ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10102ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10113fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 10123fbaf258SJosef Bacik int level) 10130999df54SChris Mason { 10140b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10150b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10163fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10170999df54SChris Mason } 10180999df54SChris Mason 1019581c1760SQu Wenruo /* 1020581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1021581c1760SQu Wenruo * verification. 1022581c1760SQu Wenruo * 10231b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1024581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1025581c1760SQu Wenruo * @level: expected level, mandatory check 1026581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1027581c1760SQu Wenruo */ 10282ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 10291b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 10301b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1031e20d96d6SChris Mason { 10325f39d397SChris Mason struct extent_buffer *buf = NULL; 103319c00ddcSChris Mason int ret; 103419c00ddcSChris Mason 10353fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1036c871b0f2SLiu Bo if (IS_ERR(buf)) 1037c871b0f2SLiu Bo return buf; 1038e4204dedSChris Mason 10395ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1040581c1760SQu Wenruo level, first_key); 10410f0fe8f7SFilipe David Borba Manana if (ret) { 1042537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 104364c043deSLiu Bo return ERR_PTR(ret); 10440f0fe8f7SFilipe David Borba Manana } 10455f39d397SChris Mason return buf; 1046ce9adaa5SChris Mason 1047eb60ceacSChris Mason } 1048eb60ceacSChris Mason 10496a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1050ed2ff2cbSChris Mason { 10516a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 105255c69072SChris Mason if (btrfs_header_generation(buf) == 1053e2d84521SMiao Xie fs_info->running_transaction->transid) { 1054b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1055b4ce94deSChris Mason 1056b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1057104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1058e2d84521SMiao Xie -buf->len, 1059e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10600b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1061925baeddSChris Mason } 10625f39d397SChris Mason } 1063ed7b63ebSJosef Bacik } 10645f39d397SChris Mason 1065da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1066e20d96d6SChris Mason u64 objectid) 1067d97e63b6SChris Mason { 10687c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 106996dfcb46SJosef Bacik root->fs_info = fs_info; 1070cfaa7295SChris Mason root->node = NULL; 1071a28ec197SChris Mason root->commit_root = NULL; 107227cdeb70SMiao Xie root->state = 0; 1073d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 10740b86a832SChris Mason 10750f7d52f4SChris Mason root->last_trans = 0; 10766b8fad57SNikolay Borisov root->free_objectid = 0; 1077eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1078199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10796bef4d31SEric Paris root->inode_tree = RB_ROOT; 108016cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1081f0486c68SYan, Zheng root->block_rsv = NULL; 10820b86a832SChris Mason 10830b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10845d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1085eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1086eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1087199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1088199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1089d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10902ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10912ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10925d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1093eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1094199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1095f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10962ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10972ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10988287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1099a2135011SChris Mason mutex_init(&root->objectid_mutex); 1100e02119d5SChris Mason mutex_init(&root->log_mutex); 110131f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1102573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1103c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11047237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11057237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11067237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11078b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11088b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11097237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11107237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11117237f183SYan Zheng atomic_set(&root->log_writers, 0); 11122ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11130700cea7SElena Reshetova refcount_set(&root->refs, 1); 11148ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1115eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11167237f183SYan Zheng root->log_transid = 0; 1117d1433debSMiao Xie root->log_transid_committed = -1; 1118257c62e1SChris Mason root->last_log_commit = 0; 1119e289f03eSFilipe Manana if (!dummy) { 112043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 112143eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1122e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1123e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1124e289f03eSFilipe Manana } 1125017e5369SChris Mason 11263768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11273768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11286702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11294d775673SChris Mason root->root_key.objectid = objectid; 11300ee5dc67SAl Viro root->anon_dev = 0; 11318ea05e3aSAlexander Block 11325f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1133370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1134bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1135bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1136bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1137bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1138bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1139bd647ce3SJosef Bacik #endif 11403768f368SChris Mason } 11413768f368SChris Mason 114274e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 114396dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11446f07e42eSAl Viro { 114574e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11466f07e42eSAl Viro if (root) 114796dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11486f07e42eSAl Viro return root; 11496f07e42eSAl Viro } 11506f07e42eSAl Viro 115106ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 115206ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1153da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 115406ea65a3SJosef Bacik { 115506ea65a3SJosef Bacik struct btrfs_root *root; 115606ea65a3SJosef Bacik 11577c0260eeSJeff Mahoney if (!fs_info) 11587c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11597c0260eeSJeff Mahoney 116096dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 116106ea65a3SJosef Bacik if (!root) 116206ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1163da17066cSJeff Mahoney 1164b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1165faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 116606ea65a3SJosef Bacik 116706ea65a3SJosef Bacik return root; 116806ea65a3SJosef Bacik } 116906ea65a3SJosef Bacik #endif 117006ea65a3SJosef Bacik 117120897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 117220897f5cSArne Jansen u64 objectid) 117320897f5cSArne Jansen { 11749b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 117520897f5cSArne Jansen struct extent_buffer *leaf; 117620897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 117720897f5cSArne Jansen struct btrfs_root *root; 117820897f5cSArne Jansen struct btrfs_key key; 1179b89f6d1fSFilipe Manana unsigned int nofs_flag; 118020897f5cSArne Jansen int ret = 0; 118120897f5cSArne Jansen 1182b89f6d1fSFilipe Manana /* 1183b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1184b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1185b89f6d1fSFilipe Manana */ 1186b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 118796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1188b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 118920897f5cSArne Jansen if (!root) 119020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 119120897f5cSArne Jansen 119220897f5cSArne Jansen root->root_key.objectid = objectid; 119320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 119420897f5cSArne Jansen root->root_key.offset = 0; 119520897f5cSArne Jansen 11969631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 11979631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 119820897f5cSArne Jansen if (IS_ERR(leaf)) { 119920897f5cSArne Jansen ret = PTR_ERR(leaf); 12001dd05682STsutomu Itoh leaf = NULL; 12018a6a87cdSBoris Burkov goto fail_unlock; 120220897f5cSArne Jansen } 120320897f5cSArne Jansen 120420897f5cSArne Jansen root->node = leaf; 120520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 120620897f5cSArne Jansen 120720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 120827cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 120920897f5cSArne Jansen 1210f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1211f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 121220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 121320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 121420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 121520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 121620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 121720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 121820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 121933d85fdaSQu Wenruo if (is_fstree(objectid)) 1220807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1221807fc790SAndy Shevchenko else 1222807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1223c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 122420897f5cSArne Jansen 12258a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 12268a6a87cdSBoris Burkov 122720897f5cSArne Jansen key.objectid = objectid; 122820897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 122920897f5cSArne Jansen key.offset = 0; 123020897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 123120897f5cSArne Jansen if (ret) 123220897f5cSArne Jansen goto fail; 123320897f5cSArne Jansen 123420897f5cSArne Jansen return root; 12351dd05682STsutomu Itoh 12368a6a87cdSBoris Burkov fail_unlock: 12378c38938cSJosef Bacik if (leaf) 12381dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 12398a6a87cdSBoris Burkov fail: 124000246528SJosef Bacik btrfs_put_root(root); 12411dd05682STsutomu Itoh 12421dd05682STsutomu Itoh return ERR_PTR(ret); 124320897f5cSArne Jansen } 124420897f5cSArne Jansen 12457237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1246e02119d5SChris Mason struct btrfs_fs_info *fs_info) 12470f7d52f4SChris Mason { 12480f7d52f4SChris Mason struct btrfs_root *root; 12497237f183SYan Zheng struct extent_buffer *leaf; 1250e02119d5SChris Mason 125196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1252e02119d5SChris Mason if (!root) 12537237f183SYan Zheng return ERR_PTR(-ENOMEM); 1254e02119d5SChris Mason 1255e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1256e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1257e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 125827cdeb70SMiao Xie 12597237f183SYan Zheng /* 126092a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 126127cdeb70SMiao Xie * 126292a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 126392a7cc42SQu Wenruo * and they go away before a real commit is actually done. 126492a7cc42SQu Wenruo * 126592a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 126692a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 12677237f183SYan Zheng */ 1268e02119d5SChris Mason 12694d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 12709631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 12717237f183SYan Zheng if (IS_ERR(leaf)) { 127200246528SJosef Bacik btrfs_put_root(root); 12737237f183SYan Zheng return ERR_CAST(leaf); 12747237f183SYan Zheng } 1275e02119d5SChris Mason 12767237f183SYan Zheng root->node = leaf; 1277e02119d5SChris Mason 1278e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1279e02119d5SChris Mason btrfs_tree_unlock(root->node); 12807237f183SYan Zheng return root; 12817237f183SYan Zheng } 12827237f183SYan Zheng 12837237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 12847237f183SYan Zheng struct btrfs_fs_info *fs_info) 12857237f183SYan Zheng { 12867237f183SYan Zheng struct btrfs_root *log_root; 12877237f183SYan Zheng 12887237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 12897237f183SYan Zheng if (IS_ERR(log_root)) 12907237f183SYan Zheng return PTR_ERR(log_root); 12917237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 12927237f183SYan Zheng fs_info->log_root_tree = log_root; 12937237f183SYan Zheng return 0; 12947237f183SYan Zheng } 12957237f183SYan Zheng 12967237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 12977237f183SYan Zheng struct btrfs_root *root) 12987237f183SYan Zheng { 12990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13007237f183SYan Zheng struct btrfs_root *log_root; 13017237f183SYan Zheng struct btrfs_inode_item *inode_item; 13027237f183SYan Zheng 13030b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13047237f183SYan Zheng if (IS_ERR(log_root)) 13057237f183SYan Zheng return PTR_ERR(log_root); 13067237f183SYan Zheng 13077237f183SYan Zheng log_root->last_trans = trans->transid; 13087237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13097237f183SYan Zheng 13107237f183SYan Zheng inode_item = &log_root->root_item.inode; 13113cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13123cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13133cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1314da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13150b246afaSJeff Mahoney fs_info->nodesize); 13163cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13177237f183SYan Zheng 13185d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13197237f183SYan Zheng 13207237f183SYan Zheng WARN_ON(root->log_root); 13217237f183SYan Zheng root->log_root = log_root; 13227237f183SYan Zheng root->log_transid = 0; 1323d1433debSMiao Xie root->log_transid_committed = -1; 1324257c62e1SChris Mason root->last_log_commit = 0; 1325e02119d5SChris Mason return 0; 1326e02119d5SChris Mason } 1327e02119d5SChris Mason 132849d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 132949d11beaSJosef Bacik struct btrfs_path *path, 1330cb517eabSMiao Xie struct btrfs_key *key) 1331e02119d5SChris Mason { 1332e02119d5SChris Mason struct btrfs_root *root; 1333e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 133484234f3aSYan Zheng u64 generation; 1335cb517eabSMiao Xie int ret; 1336581c1760SQu Wenruo int level; 1337cb517eabSMiao Xie 133896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 133949d11beaSJosef Bacik if (!root) 134049d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 13410f7d52f4SChris Mason 1342cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1343cb517eabSMiao Xie &root->root_item, &root->root_key); 13440f7d52f4SChris Mason if (ret) { 134513a8a7c8SYan, Zheng if (ret > 0) 134613a8a7c8SYan, Zheng ret = -ENOENT; 134749d11beaSJosef Bacik goto fail; 13480f7d52f4SChris Mason } 134913a8a7c8SYan, Zheng 135084234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1351581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 13522ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 13532ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 13541b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 135564c043deSLiu Bo if (IS_ERR(root->node)) { 135664c043deSLiu Bo ret = PTR_ERR(root->node); 13578c38938cSJosef Bacik root->node = NULL; 135849d11beaSJosef Bacik goto fail; 1359cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1360cb517eabSMiao Xie ret = -EIO; 136149d11beaSJosef Bacik goto fail; 1362416bc658SJosef Bacik } 13635d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1364cb517eabSMiao Xie return root; 136549d11beaSJosef Bacik fail: 136600246528SJosef Bacik btrfs_put_root(root); 136749d11beaSJosef Bacik return ERR_PTR(ret); 136849d11beaSJosef Bacik } 136949d11beaSJosef Bacik 137049d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 137149d11beaSJosef Bacik struct btrfs_key *key) 137249d11beaSJosef Bacik { 137349d11beaSJosef Bacik struct btrfs_root *root; 137449d11beaSJosef Bacik struct btrfs_path *path; 137549d11beaSJosef Bacik 137649d11beaSJosef Bacik path = btrfs_alloc_path(); 137749d11beaSJosef Bacik if (!path) 137849d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 137949d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 138049d11beaSJosef Bacik btrfs_free_path(path); 138149d11beaSJosef Bacik 138249d11beaSJosef Bacik return root; 1383cb517eabSMiao Xie } 1384cb517eabSMiao Xie 13852dfb1e43SQu Wenruo /* 13862dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 13872dfb1e43SQu Wenruo * 13882dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 13892dfb1e43SQu Wenruo */ 13902dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1391cb517eabSMiao Xie { 1392cb517eabSMiao Xie int ret; 1393dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1394cb517eabSMiao Xie 1395dcc3eb96SNikolay Borisov /* 1396dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1397dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1398dcc3eb96SNikolay Borisov */ 1399dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1400dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1401dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1402dcc3eb96SNikolay Borisov if (ret) 14038257b2dcSMiao Xie goto fail; 14048257b2dcSMiao Xie 1405aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 1406aeb935a4SQu Wenruo root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { 140792a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1408f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1409f39e4571SJosef Bacik } 1410f39e4571SJosef Bacik 1411851fd730SQu Wenruo /* 1412851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1413851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1414851fd730SQu Wenruo */ 1415851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1416851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 14172dfb1e43SQu Wenruo if (!anon_dev) { 1418cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1419cb517eabSMiao Xie if (ret) 1420876d2cf1SLiu Bo goto fail; 14212dfb1e43SQu Wenruo } else { 14222dfb1e43SQu Wenruo root->anon_dev = anon_dev; 14232dfb1e43SQu Wenruo } 1424851fd730SQu Wenruo } 1425f32e48e9SChandan Rajendra 1426f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1427453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1428f32e48e9SChandan Rajendra if (ret) { 1429f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1430876d2cf1SLiu Bo goto fail; 1431f32e48e9SChandan Rajendra } 1432f32e48e9SChandan Rajendra 14336b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1434f32e48e9SChandan Rajendra 1435f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1436f32e48e9SChandan Rajendra 1437cb517eabSMiao Xie return 0; 1438cb517eabSMiao Xie fail: 143984db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1440cb517eabSMiao Xie return ret; 1441cb517eabSMiao Xie } 1442cb517eabSMiao Xie 1443a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1444cb517eabSMiao Xie u64 root_id) 1445cb517eabSMiao Xie { 1446cb517eabSMiao Xie struct btrfs_root *root; 1447cb517eabSMiao Xie 1448cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1449cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1450cb517eabSMiao Xie (unsigned long)root_id); 1451bc44d7c4SJosef Bacik if (root) 145200246528SJosef Bacik root = btrfs_grab_root(root); 1453cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1454cb517eabSMiao Xie return root; 1455cb517eabSMiao Xie } 1456cb517eabSMiao Xie 145749d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 145849d11beaSJosef Bacik u64 objectid) 145949d11beaSJosef Bacik { 146049d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 146149d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 146249d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 146349d11beaSJosef Bacik return btrfs_grab_root(fs_info->extent_root); 146449d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 146549d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 146649d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 146749d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 146849d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 146949d11beaSJosef Bacik return btrfs_grab_root(fs_info->csum_root); 147049d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 147149d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 147249d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 147349d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 147449d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 147549d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 147649d11beaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 147749d11beaSJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 147849d11beaSJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 147949d11beaSJosef Bacik return NULL; 148049d11beaSJosef Bacik } 148149d11beaSJosef Bacik 1482cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1483cb517eabSMiao Xie struct btrfs_root *root) 1484cb517eabSMiao Xie { 1485cb517eabSMiao Xie int ret; 1486cb517eabSMiao Xie 1487e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1488cb517eabSMiao Xie if (ret) 1489cb517eabSMiao Xie return ret; 1490cb517eabSMiao Xie 1491cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1492cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1493cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1494cb517eabSMiao Xie root); 1495af01d2e5SJosef Bacik if (ret == 0) { 149600246528SJosef Bacik btrfs_grab_root(root); 149727cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1498af01d2e5SJosef Bacik } 1499cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1500cb517eabSMiao Xie radix_tree_preload_end(); 1501cb517eabSMiao Xie 1502cb517eabSMiao Xie return ret; 1503cb517eabSMiao Xie } 1504cb517eabSMiao Xie 1505bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1506bd647ce3SJosef Bacik { 1507bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1508bd647ce3SJosef Bacik struct btrfs_root *root; 1509bd647ce3SJosef Bacik 1510bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1511457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1512457f1864SJosef Bacik 1513bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1514bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1515457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 151671008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1517bd647ce3SJosef Bacik refcount_read(&root->refs)); 1518bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 151900246528SJosef Bacik btrfs_put_root(root); 152000246528SJosef Bacik btrfs_put_root(root); 1521bd647ce3SJosef Bacik } 1522bd647ce3SJosef Bacik #endif 1523bd647ce3SJosef Bacik } 1524bd647ce3SJosef Bacik 15250d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15260d4b0463SJosef Bacik { 1527141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1528141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 15295deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1530141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1531141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1532141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1533141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15340d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15350d4b0463SJosef Bacik kfree(fs_info->delayed_root); 153600246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 153700246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 153800246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 153900246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 154000246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 154100246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 154200246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 154300246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 154400246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1545aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 1546bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15473fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 15480d4b0463SJosef Bacik kfree(fs_info->super_copy); 15490d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15500d4b0463SJosef Bacik kvfree(fs_info); 15510d4b0463SJosef Bacik } 15520d4b0463SJosef Bacik 15530d4b0463SJosef Bacik 15542dfb1e43SQu Wenruo /* 15552dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 15562dfb1e43SQu Wenruo * 15572dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 15582dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 15592dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 15602dfb1e43SQu Wenruo * 15612dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 15622dfb1e43SQu Wenruo * 15632dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 15642dfb1e43SQu Wenruo * same root objectid. 15652dfb1e43SQu Wenruo * 15662dfb1e43SQu Wenruo * @objectid: root id 15672dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 15682dfb1e43SQu Wenruo * pass 0 for new allocation. 15692dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 15702dfb1e43SQu Wenruo * for orphan roots 15712dfb1e43SQu Wenruo */ 15722dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 15732dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 15742dfb1e43SQu Wenruo bool check_ref) 15755eda7b5eSChris Mason { 15765eda7b5eSChris Mason struct btrfs_root *root; 1577381cf658SDavid Sterba struct btrfs_path *path; 15781d4c08e0SDavid Sterba struct btrfs_key key; 15795eda7b5eSChris Mason int ret; 15805eda7b5eSChris Mason 158149d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 158249d11beaSJosef Bacik if (root) 158349d11beaSJosef Bacik return root; 15844df27c4dSYan, Zheng again: 158556e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 158648475471SStefan Behrens if (root) { 15872dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 15882dfb1e43SQu Wenruo ASSERT(!anon_dev); 1589bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 159000246528SJosef Bacik btrfs_put_root(root); 159148475471SStefan Behrens return ERR_PTR(-ENOENT); 1592bc44d7c4SJosef Bacik } 15935eda7b5eSChris Mason return root; 159448475471SStefan Behrens } 15955eda7b5eSChris Mason 159656e9357aSDavid Sterba key.objectid = objectid; 159756e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 159856e9357aSDavid Sterba key.offset = (u64)-1; 159956e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 16005eda7b5eSChris Mason if (IS_ERR(root)) 16015eda7b5eSChris Mason return root; 16023394e160SChris Mason 1603c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1604d68fc57bSYan, Zheng ret = -ENOENT; 1605d68fc57bSYan, Zheng goto fail; 1606d68fc57bSYan, Zheng } 1607d68fc57bSYan, Zheng 16082dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1609cb517eabSMiao Xie if (ret) 1610cb517eabSMiao Xie goto fail; 1611cb517eabSMiao Xie 1612381cf658SDavid Sterba path = btrfs_alloc_path(); 1613381cf658SDavid Sterba if (!path) { 1614381cf658SDavid Sterba ret = -ENOMEM; 1615381cf658SDavid Sterba goto fail; 1616381cf658SDavid Sterba } 16171d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16181d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 161956e9357aSDavid Sterba key.offset = objectid; 16201d4c08e0SDavid Sterba 16211d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1622381cf658SDavid Sterba btrfs_free_path(path); 1623d68fc57bSYan, Zheng if (ret < 0) 1624d68fc57bSYan, Zheng goto fail; 1625d68fc57bSYan, Zheng if (ret == 0) 162627cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1627d68fc57bSYan, Zheng 1628cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16290f7d52f4SChris Mason if (ret) { 163000246528SJosef Bacik btrfs_put_root(root); 16314785e24fSJosef Bacik if (ret == -EEXIST) 16324df27c4dSYan, Zheng goto again; 16334df27c4dSYan, Zheng goto fail; 16344df27c4dSYan, Zheng } 1635edbd8d4eSChris Mason return root; 16364df27c4dSYan, Zheng fail: 16378c38938cSJosef Bacik btrfs_put_root(root); 16384df27c4dSYan, Zheng return ERR_PTR(ret); 1639edbd8d4eSChris Mason } 1640edbd8d4eSChris Mason 16412dfb1e43SQu Wenruo /* 16422dfb1e43SQu Wenruo * Get in-memory reference of a root structure 16432dfb1e43SQu Wenruo * 16442dfb1e43SQu Wenruo * @objectid: tree objectid 16452dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 16462dfb1e43SQu Wenruo * one reference 16472dfb1e43SQu Wenruo */ 16482dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 16492dfb1e43SQu Wenruo u64 objectid, bool check_ref) 16502dfb1e43SQu Wenruo { 16512dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 16522dfb1e43SQu Wenruo } 16532dfb1e43SQu Wenruo 16542dfb1e43SQu Wenruo /* 16552dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 16562dfb1e43SQu Wenruo * the anonymous block device id 16572dfb1e43SQu Wenruo * 16582dfb1e43SQu Wenruo * @objectid: tree objectid 16592dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 16602dfb1e43SQu Wenruo * parameter value 16612dfb1e43SQu Wenruo */ 16622dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 16632dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 16642dfb1e43SQu Wenruo { 16652dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 16662dfb1e43SQu Wenruo } 16672dfb1e43SQu Wenruo 16688b712842SChris Mason /* 166949d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 167049d11beaSJosef Bacik * @fs_info: the fs_info 167149d11beaSJosef Bacik * @objectid: the objectid we need to lookup 167249d11beaSJosef Bacik * 167349d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 167449d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 167549d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 167649d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 167749d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 167849d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 167949d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 168049d11beaSJosef Bacik * backref code is finished using the root. 168149d11beaSJosef Bacik */ 168249d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 168349d11beaSJosef Bacik struct btrfs_path *path, 168449d11beaSJosef Bacik u64 objectid) 168549d11beaSJosef Bacik { 168649d11beaSJosef Bacik struct btrfs_root *root; 168749d11beaSJosef Bacik struct btrfs_key key; 168849d11beaSJosef Bacik 168949d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 169049d11beaSJosef Bacik 169149d11beaSJosef Bacik /* 169249d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 169349d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 169449d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 169549d11beaSJosef Bacik * != NULL just return it. 169649d11beaSJosef Bacik */ 169749d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 169849d11beaSJosef Bacik if (root) 169949d11beaSJosef Bacik return root; 170049d11beaSJosef Bacik 170149d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 170249d11beaSJosef Bacik if (root) 170349d11beaSJosef Bacik return root; 170449d11beaSJosef Bacik 170549d11beaSJosef Bacik key.objectid = objectid; 170649d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 170749d11beaSJosef Bacik key.offset = (u64)-1; 170849d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 170949d11beaSJosef Bacik btrfs_release_path(path); 171049d11beaSJosef Bacik 171149d11beaSJosef Bacik return root; 171249d11beaSJosef Bacik } 171349d11beaSJosef Bacik 171449d11beaSJosef Bacik /* 17158b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 17168b712842SChris Mason * functions. This is where read checksum verification actually happens 17178b712842SChris Mason */ 17188b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1719ce9adaa5SChris Mason { 1720ce9adaa5SChris Mason struct bio *bio; 172197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1722ce9adaa5SChris Mason 172397eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1724ce9adaa5SChris Mason bio = end_io_wq->bio; 17258b712842SChris Mason 17264e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1727ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1728ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 17294246a0b6SChristoph Hellwig bio_endio(bio); 17309be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1731ce9adaa5SChris Mason } 173244b8bd7eSChris Mason 1733a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1734a74a4b97SChris Mason { 1735a74a4b97SChris Mason struct btrfs_root *root = arg; 17360b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1737d0278245SMiao Xie int again; 1738a74a4b97SChris Mason 1739d6fd0ae2SOmar Sandoval while (1) { 1740d0278245SMiao Xie again = 0; 17419d1a2a3aSDavid Sterba 1742fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1743fd340d0fSJosef Bacik 1744d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17452ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1746d0278245SMiao Xie goto sleep; 1747d0278245SMiao Xie 174890c711abSZygo Blaxell /* 174990c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 175090c711abSZygo Blaxell * before we have finished mounting the filesystem. 175190c711abSZygo Blaxell */ 17520b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 175390c711abSZygo Blaxell goto sleep; 175490c711abSZygo Blaxell 17550b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1756d0278245SMiao Xie goto sleep; 1757d0278245SMiao Xie 1758dc7f370cSMiao Xie /* 1759dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1760dc7f370cSMiao Xie * during the above check and trylock. 1761dc7f370cSMiao Xie */ 17622ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17630b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1764dc7f370cSMiao Xie goto sleep; 1765dc7f370cSMiao Xie } 1766dc7f370cSMiao Xie 17672ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1768c2d6cb16SFilipe Manana 17699d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17700b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1771a74a4b97SChris Mason 1772d0278245SMiao Xie /* 177305323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 177405323cd1SMiao Xie * needn't do anything special here. 1775d0278245SMiao Xie */ 17760b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 177767c5e7d4SFilipe Manana 177867c5e7d4SFilipe Manana /* 177967c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 178067c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 178167c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 178267c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 178367c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 178467c5e7d4SFilipe Manana * unused block groups. 178567c5e7d4SFilipe Manana */ 17860b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1787d0278245SMiao Xie sleep: 1788a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1789d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1790d6fd0ae2SOmar Sandoval kthread_parkme(); 1791d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1792d6fd0ae2SOmar Sandoval return 0; 1793838fe188SJiri Kosina if (!again) { 1794a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1795a74a4b97SChris Mason schedule(); 1796a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1797a74a4b97SChris Mason } 1798da288d28SFilipe Manana } 1799a74a4b97SChris Mason } 1800a74a4b97SChris Mason 1801a74a4b97SChris Mason static int transaction_kthread(void *arg) 1802a74a4b97SChris Mason { 1803a74a4b97SChris Mason struct btrfs_root *root = arg; 18040b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1805a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1806a74a4b97SChris Mason struct btrfs_transaction *cur; 18078929ecfaSYan, Zheng u64 transid; 1808643900beSNikolay Borisov time64_t delta; 1809a74a4b97SChris Mason unsigned long delay; 1810914b2007SJan Kara bool cannot_commit; 1811a74a4b97SChris Mason 1812a74a4b97SChris Mason do { 1813914b2007SJan Kara cannot_commit = false; 1814ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 18150b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1816a74a4b97SChris Mason 18170b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18180b246afaSJeff Mahoney cur = fs_info->running_transaction; 1819a74a4b97SChris Mason if (!cur) { 18200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1821a74a4b97SChris Mason goto sleep; 1822a74a4b97SChris Mason } 182331153d81SYan Zheng 1824643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 18253296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1826643900beSNikolay Borisov delta < fs_info->commit_interval) { 18270b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1828fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1829fb8a7e94SNikolay Borisov delay = min(delay, 1830fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1831a74a4b97SChris Mason goto sleep; 1832a74a4b97SChris Mason } 18338929ecfaSYan, Zheng transid = cur->transid; 18340b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 183556bec294SChris Mason 183679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1837354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1838914b2007SJan Kara if (IS_ERR(trans)) { 1839354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1840914b2007SJan Kara cannot_commit = true; 184179787eaaSJeff Mahoney goto sleep; 1842914b2007SJan Kara } 18438929ecfaSYan, Zheng if (transid == trans->transid) { 18443a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18458929ecfaSYan, Zheng } else { 18463a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18478929ecfaSYan, Zheng } 1848a74a4b97SChris Mason sleep: 18490b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18500b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1851a74a4b97SChris Mason 18524e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18530b246afaSJeff Mahoney &fs_info->fs_state))) 18542ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18558929ecfaSYan, Zheng if (!kthread_should_stop() && 18560b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1857914b2007SJan Kara cannot_commit)) 1858bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1859a74a4b97SChris Mason } while (!kthread_should_stop()); 1860a74a4b97SChris Mason return 0; 1861a74a4b97SChris Mason } 1862a74a4b97SChris Mason 1863af31f5e5SChris Mason /* 186401f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 186501f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 186601f0f9daSNikolay Borisov * anything. 1867af31f5e5SChris Mason * 1868af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1869af31f5e5SChris Mason * generation of the latest root in the array with the generation 1870af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1871af31f5e5SChris Mason */ 187201f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1873af31f5e5SChris Mason { 187401f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1875af31f5e5SChris Mason u64 cur; 1876af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1877af31f5e5SChris Mason int i; 1878af31f5e5SChris Mason 1879af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1880af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1881af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1882af31f5e5SChris Mason if (cur == newest_gen) 188301f0f9daSNikolay Borisov return i; 1884af31f5e5SChris Mason } 1885af31f5e5SChris Mason 188601f0f9daSNikolay Borisov return -EINVAL; 1887af31f5e5SChris Mason } 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason /* 1890af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1891af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1892af31f5e5SChris Mason * done 1893af31f5e5SChris Mason */ 1894af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1895af31f5e5SChris Mason { 18966ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1897af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1898af31f5e5SChris Mason 1899af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1900af31f5e5SChris Mason 1901af31f5e5SChris Mason /* 1902af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1903af31f5e5SChris Mason * regardless of which ones we use today 1904af31f5e5SChris Mason */ 1905af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1908af31f5e5SChris Mason 1909af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1910af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1911af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1912af31f5e5SChris Mason 1913af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1914af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1915af31f5e5SChris Mason 1916af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1917af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1918af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1919af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1927af31f5e5SChris Mason 19287c7e82a7SChris Mason /* 19297c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19307c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19317c7e82a7SChris Mason */ 19327c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19337c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19347c7e82a7SChris Mason info->fs_root->node->start); 1935af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1936af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1937af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1938af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19397c7e82a7SChris Mason } 1940af31f5e5SChris Mason 1941af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1942af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1943af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1944af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1945af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1948af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1949af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1950af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1951af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1952af31f5e5SChris Mason 1953af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1954af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1955af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1956af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1957af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1958af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1959af31f5e5SChris Mason 1960af31f5e5SChris Mason /* 1961af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1962af31f5e5SChris Mason * for the next commit 1963af31f5e5SChris Mason */ 1964af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1965af31f5e5SChris Mason &info->super_for_commit->super_roots, 1966af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1967af31f5e5SChris Mason } 1968af31f5e5SChris Mason 1969af31f5e5SChris Mason /* 1970bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1971bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1972bd2336b2SNikolay Borisov * 1973bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1974bd2336b2SNikolay Borisov * priority - priority of backup root required 1975bd2336b2SNikolay Borisov * 1976bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1977bd2336b2SNikolay Borisov */ 1978bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1979bd2336b2SNikolay Borisov { 1980bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1981bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1982bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1983bd2336b2SNikolay Borisov 1984bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1985bd2336b2SNikolay Borisov if (priority == 0) 1986bd2336b2SNikolay Borisov return backup_index; 1987bd2336b2SNikolay Borisov 1988bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1989bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1990bd2336b2SNikolay Borisov } else { 1991bd2336b2SNikolay Borisov return -EINVAL; 1992bd2336b2SNikolay Borisov } 1993bd2336b2SNikolay Borisov 1994bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 1995bd2336b2SNikolay Borisov 1996bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 1997bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 1998bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1999bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2000bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2001bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2002bd2336b2SNikolay Borisov 2003bd2336b2SNikolay Borisov /* 2004bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2005bd2336b2SNikolay Borisov * need a fsck 2006bd2336b2SNikolay Borisov */ 2007bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2008bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2009bd2336b2SNikolay Borisov 2010bd2336b2SNikolay Borisov return backup_index; 2011bd2336b2SNikolay Borisov } 2012bd2336b2SNikolay Borisov 20137abadb64SLiu Bo /* helper to cleanup workers */ 20147abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20157abadb64SLiu Bo { 2016dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2017afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20185cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2019fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2020fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2021d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2022fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2023fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 20245b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2025e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2026736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2027a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2028fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2029b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2030b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2031a9b9477dSFilipe Manana /* 2032a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2033a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2034a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2035a9b9477dSFilipe Manana */ 2036a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2037a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20387abadb64SLiu Bo } 20397abadb64SLiu Bo 20402e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20412e9f5954SRashika { 20422e9f5954SRashika if (root) { 20432e9f5954SRashika free_extent_buffer(root->node); 20442e9f5954SRashika free_extent_buffer(root->commit_root); 20452e9f5954SRashika root->node = NULL; 20462e9f5954SRashika root->commit_root = NULL; 20472e9f5954SRashika } 20482e9f5954SRashika } 20492e9f5954SRashika 2050af31f5e5SChris Mason /* helper to cleanup tree roots */ 20514273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2052af31f5e5SChris Mason { 20532e9f5954SRashika free_root_extent_buffers(info->tree_root); 2054655b09feSJosef Bacik 20552e9f5954SRashika free_root_extent_buffers(info->dev_root); 20562e9f5954SRashika free_root_extent_buffers(info->extent_root); 20572e9f5954SRashika free_root_extent_buffers(info->csum_root); 20582e9f5954SRashika free_root_extent_buffers(info->quota_root); 20592e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20608c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2061aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 20624273eaffSAnand Jain if (free_chunk_root) 20632e9f5954SRashika free_root_extent_buffers(info->chunk_root); 206470f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2065af31f5e5SChris Mason } 2066af31f5e5SChris Mason 20678c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 20688c38938cSJosef Bacik { 20698c38938cSJosef Bacik if (!root) 20708c38938cSJosef Bacik return; 20718c38938cSJosef Bacik 20728c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 20738c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 20741dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 20758c38938cSJosef Bacik if (root->anon_dev) 20768c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 20778c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2078923eb523SJohannes Thumshirn free_root_extent_buffers(root); 20798c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 20808c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 20818c38938cSJosef Bacik list_del_init(&root->leak_list); 20828c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 20838c38938cSJosef Bacik #endif 20848c38938cSJosef Bacik kfree(root); 20858c38938cSJosef Bacik } 20868c38938cSJosef Bacik } 20878c38938cSJosef Bacik 2088faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2089171f6537SJosef Bacik { 2090171f6537SJosef Bacik int ret; 2091171f6537SJosef Bacik struct btrfs_root *gang[8]; 2092171f6537SJosef Bacik int i; 2093171f6537SJosef Bacik 2094171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2095171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2096171f6537SJosef Bacik struct btrfs_root, root_list); 2097171f6537SJosef Bacik list_del(&gang[0]->root_list); 2098171f6537SJosef Bacik 20998c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2100cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 210100246528SJosef Bacik btrfs_put_root(gang[0]); 2102171f6537SJosef Bacik } 2103171f6537SJosef Bacik 2104171f6537SJosef Bacik while (1) { 2105171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2106171f6537SJosef Bacik (void **)gang, 0, 2107171f6537SJosef Bacik ARRAY_SIZE(gang)); 2108171f6537SJosef Bacik if (!ret) 2109171f6537SJosef Bacik break; 2110171f6537SJosef Bacik for (i = 0; i < ret; i++) 2111cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2112171f6537SJosef Bacik } 2113171f6537SJosef Bacik } 2114af31f5e5SChris Mason 2115638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2116638aa7edSEric Sandeen { 2117638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2118638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2119638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2120638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2121638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2122638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2123ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2124638aa7edSEric Sandeen } 2125638aa7edSEric Sandeen 2126779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2127779a65a4SEric Sandeen { 2128779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2129779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2130779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2131779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2132779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2133779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2134779a65a4SEric Sandeen } 2135779a65a4SEric Sandeen 21366bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2137f37938e0SEric Sandeen { 21382ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21392ff7e61eSJeff Mahoney 21402ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21412ff7e61eSJeff Mahoney set_nlink(inode, 1); 2142f37938e0SEric Sandeen /* 2143f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2144f37938e0SEric Sandeen * the real end of the address space is determined by all of 2145f37938e0SEric Sandeen * the devices in the system 2146f37938e0SEric Sandeen */ 21472ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21482ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2149f37938e0SEric Sandeen 21502ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 215143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 21522c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 21537b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21542ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2155f37938e0SEric Sandeen 21565c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 21572ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21582ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21592ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2160f37938e0SEric Sandeen } 2161f37938e0SEric Sandeen 2162ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2163ad618368SEric Sandeen { 2164ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2165129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21667f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2167ad618368SEric Sandeen } 2168ad618368SEric Sandeen 2169f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2170f9e92e40SEric Sandeen { 2171f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2172f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2173f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2174f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2175f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2176f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2177d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2178f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2179f9e92e40SEric Sandeen } 2180f9e92e40SEric Sandeen 21812a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21822a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21832a458198SEric Sandeen { 2184f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21856f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21862a458198SEric Sandeen 21872a458198SEric Sandeen fs_info->workers = 2188cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2189cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21902a458198SEric Sandeen 21912a458198SEric Sandeen fs_info->delalloc_workers = 2192cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2193cb001095SJeff Mahoney flags, max_active, 2); 21942a458198SEric Sandeen 21952a458198SEric Sandeen fs_info->flush_workers = 2196cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2197cb001095SJeff Mahoney flags, max_active, 0); 21982a458198SEric Sandeen 21992a458198SEric Sandeen fs_info->caching_workers = 2200cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22012a458198SEric Sandeen 22022a458198SEric Sandeen fs_info->fixup_workers = 2203cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22042a458198SEric Sandeen 22052a458198SEric Sandeen /* 22062a458198SEric Sandeen * endios are largely parallel and should have a very 22072a458198SEric Sandeen * low idle thresh 22082a458198SEric Sandeen */ 22092a458198SEric Sandeen fs_info->endio_workers = 2210cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22112a458198SEric Sandeen fs_info->endio_meta_workers = 2212cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2213cb001095SJeff Mahoney max_active, 4); 22142a458198SEric Sandeen fs_info->endio_meta_write_workers = 2215cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2216cb001095SJeff Mahoney max_active, 2); 22172a458198SEric Sandeen fs_info->endio_raid56_workers = 2218cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2219cb001095SJeff Mahoney max_active, 4); 22202a458198SEric Sandeen fs_info->rmw_workers = 2221cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22222a458198SEric Sandeen fs_info->endio_write_workers = 2223cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2224cb001095SJeff Mahoney max_active, 2); 22252a458198SEric Sandeen fs_info->endio_freespace_worker = 2226cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2227cb001095SJeff Mahoney max_active, 0); 22282a458198SEric Sandeen fs_info->delayed_workers = 2229cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2230cb001095SJeff Mahoney max_active, 0); 22312a458198SEric Sandeen fs_info->readahead_workers = 2232cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2233cb001095SJeff Mahoney max_active, 2); 22342a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2235cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2236b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2237b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 22382a458198SEric Sandeen 22392a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2240ba8a9d07SChris Mason fs_info->flush_workers && 22412a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22422a458198SEric Sandeen fs_info->endio_meta_write_workers && 22432a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22442a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22452a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22462a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2247b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2248b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22492a458198SEric Sandeen return -ENOMEM; 22502a458198SEric Sandeen } 22512a458198SEric Sandeen 22522a458198SEric Sandeen return 0; 22532a458198SEric Sandeen } 22542a458198SEric Sandeen 22556d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22566d97c6e3SJohannes Thumshirn { 22576d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2258b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22596d97c6e3SJohannes Thumshirn 2260b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22616d97c6e3SJohannes Thumshirn 22626d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22636d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2264b4e967beSDavid Sterba csum_driver); 22656d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22666d97c6e3SJohannes Thumshirn } 22676d97c6e3SJohannes Thumshirn 22686d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22696d97c6e3SJohannes Thumshirn 22706d97c6e3SJohannes Thumshirn return 0; 22716d97c6e3SJohannes Thumshirn } 22726d97c6e3SJohannes Thumshirn 227363443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 227463443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 227563443bf5SEric Sandeen { 227663443bf5SEric Sandeen int ret; 227763443bf5SEric Sandeen struct btrfs_root *log_tree_root; 227863443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 227963443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2280581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 228163443bf5SEric Sandeen 228263443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2283f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 228463443bf5SEric Sandeen return -EIO; 228563443bf5SEric Sandeen } 228663443bf5SEric Sandeen 228796dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 228896dfcb46SJosef Bacik GFP_KERNEL); 228963443bf5SEric Sandeen if (!log_tree_root) 229063443bf5SEric Sandeen return -ENOMEM; 229163443bf5SEric Sandeen 22922ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 22931b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 22941b7ec85eSJosef Bacik fs_info->generation + 1, level, 22951b7ec85eSJosef Bacik NULL); 229664c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2297f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22980eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 22998c38938cSJosef Bacik log_tree_root->node = NULL; 230000246528SJosef Bacik btrfs_put_root(log_tree_root); 23010eeff236SLiu Bo return ret; 230264c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2303f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 230400246528SJosef Bacik btrfs_put_root(log_tree_root); 230563443bf5SEric Sandeen return -EIO; 230663443bf5SEric Sandeen } 230763443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 230863443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 230963443bf5SEric Sandeen if (ret) { 23100b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 231163443bf5SEric Sandeen "Failed to recover log tree"); 231200246528SJosef Bacik btrfs_put_root(log_tree_root); 231363443bf5SEric Sandeen return ret; 231463443bf5SEric Sandeen } 231563443bf5SEric Sandeen 2316bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23176bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 231863443bf5SEric Sandeen if (ret) 231963443bf5SEric Sandeen return ret; 232063443bf5SEric Sandeen } 232163443bf5SEric Sandeen 232263443bf5SEric Sandeen return 0; 232363443bf5SEric Sandeen } 232463443bf5SEric Sandeen 23256bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23264bbcaa64SEric Sandeen { 23276bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2328a4f3d2c4SDavid Sterba struct btrfs_root *root; 23294bbcaa64SEric Sandeen struct btrfs_key location; 23304bbcaa64SEric Sandeen int ret; 23314bbcaa64SEric Sandeen 23326bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23336bccf3abSJeff Mahoney 23344bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23354bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23364bbcaa64SEric Sandeen location.offset = 0; 23374bbcaa64SEric Sandeen 2338a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2339f50f4353SLiu Bo if (IS_ERR(root)) { 234042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2341f50f4353SLiu Bo ret = PTR_ERR(root); 2342f50f4353SLiu Bo goto out; 2343f50f4353SLiu Bo } 234442437a63SJosef Bacik } else { 2345a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2346a4f3d2c4SDavid Sterba fs_info->extent_root = root; 234742437a63SJosef Bacik } 23484bbcaa64SEric Sandeen 23494bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2350a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2351f50f4353SLiu Bo if (IS_ERR(root)) { 235242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2353f50f4353SLiu Bo ret = PTR_ERR(root); 2354f50f4353SLiu Bo goto out; 2355f50f4353SLiu Bo } 235642437a63SJosef Bacik } else { 2357a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2358a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23594bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 236042437a63SJosef Bacik } 23614bbcaa64SEric Sandeen 2362882dbe0cSJosef Bacik /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ 2363882dbe0cSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 23644bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2365a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2366f50f4353SLiu Bo if (IS_ERR(root)) { 236742437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2368f50f4353SLiu Bo ret = PTR_ERR(root); 2369f50f4353SLiu Bo goto out; 2370f50f4353SLiu Bo } 237142437a63SJosef Bacik } else { 2372a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2373a4f3d2c4SDavid Sterba fs_info->csum_root = root; 237442437a63SJosef Bacik } 2375882dbe0cSJosef Bacik } 23764bbcaa64SEric Sandeen 2377aeb935a4SQu Wenruo /* 2378aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2379aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2380aeb935a4SQu Wenruo */ 238156e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 238256e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2383aeb935a4SQu Wenruo if (IS_ERR(root)) { 238442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2385aeb935a4SQu Wenruo ret = PTR_ERR(root); 2386aeb935a4SQu Wenruo goto out; 2387aeb935a4SQu Wenruo } 238842437a63SJosef Bacik } else { 2389aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2390aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 239142437a63SJosef Bacik } 2392aeb935a4SQu Wenruo 23934bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2394a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2395a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2396a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2397afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2398a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23994bbcaa64SEric Sandeen } 24004bbcaa64SEric Sandeen 24014bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2402a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2403a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 240442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2405a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 24064bbcaa64SEric Sandeen if (ret != -ENOENT) 2407f50f4353SLiu Bo goto out; 240842437a63SJosef Bacik } 24094bbcaa64SEric Sandeen } else { 2410a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2411a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 24124bbcaa64SEric Sandeen } 24134bbcaa64SEric Sandeen 241470f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 241570f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 241670f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2417f50f4353SLiu Bo if (IS_ERR(root)) { 241842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2419f50f4353SLiu Bo ret = PTR_ERR(root); 2420f50f4353SLiu Bo goto out; 2421f50f4353SLiu Bo } 242242437a63SJosef Bacik } else { 242370f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 242470f6d82eSOmar Sandoval fs_info->free_space_root = root; 242570f6d82eSOmar Sandoval } 242642437a63SJosef Bacik } 242770f6d82eSOmar Sandoval 24284bbcaa64SEric Sandeen return 0; 2429f50f4353SLiu Bo out: 2430f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2431f50f4353SLiu Bo location.objectid, ret); 2432f50f4353SLiu Bo return ret; 24334bbcaa64SEric Sandeen } 24344bbcaa64SEric Sandeen 2435069ec957SQu Wenruo /* 2436069ec957SQu Wenruo * Real super block validation 2437069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2438069ec957SQu Wenruo * 2439069ec957SQu Wenruo * @sb: super block to check 2440069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2441069ec957SQu Wenruo * 0 the primary (1st) sb 2442069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2443069ec957SQu Wenruo * -1 skip bytenr check 2444069ec957SQu Wenruo */ 2445069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2446069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 244721a852b0SQu Wenruo { 244821a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 244921a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 245021a852b0SQu Wenruo int ret = 0; 245121a852b0SQu Wenruo 245221a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 245321a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 245421a852b0SQu Wenruo ret = -EINVAL; 245521a852b0SQu Wenruo } 245621a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 245721a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 245821a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 245921a852b0SQu Wenruo ret = -EINVAL; 246021a852b0SQu Wenruo } 246121a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 246221a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 246321a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 246421a852b0SQu Wenruo ret = -EINVAL; 246521a852b0SQu Wenruo } 246621a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 246721a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 246821a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 246921a852b0SQu Wenruo ret = -EINVAL; 247021a852b0SQu Wenruo } 247121a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 247221a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 247321a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 247421a852b0SQu Wenruo ret = -EINVAL; 247521a852b0SQu Wenruo } 247621a852b0SQu Wenruo 247721a852b0SQu Wenruo /* 247821a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 247921a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 248021a852b0SQu Wenruo */ 248121a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 248221a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 248321a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 248421a852b0SQu Wenruo ret = -EINVAL; 248521a852b0SQu Wenruo } 2486*0bb3eb3eSQu Wenruo 2487*0bb3eb3eSQu Wenruo /* 2488*0bb3eb3eSQu Wenruo * For 4K page size, we only support 4K sector size. 2489*0bb3eb3eSQu Wenruo * For 64K page size, we support read-write for 64K sector size, and 2490*0bb3eb3eSQu Wenruo * read-only for 4K sector size. 2491*0bb3eb3eSQu Wenruo */ 2492*0bb3eb3eSQu Wenruo if ((PAGE_SIZE == SZ_4K && sectorsize != PAGE_SIZE) || 2493*0bb3eb3eSQu Wenruo (PAGE_SIZE == SZ_64K && (sectorsize != SZ_4K && 2494*0bb3eb3eSQu Wenruo sectorsize != SZ_64K))) { 249521a852b0SQu Wenruo btrfs_err(fs_info, 2496*0bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 249721a852b0SQu Wenruo sectorsize, PAGE_SIZE); 249821a852b0SQu Wenruo ret = -EINVAL; 249921a852b0SQu Wenruo } 2500*0bb3eb3eSQu Wenruo 250121a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 250221a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 250321a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 250421a852b0SQu Wenruo ret = -EINVAL; 250521a852b0SQu Wenruo } 250621a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 250721a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 250821a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 250921a852b0SQu Wenruo ret = -EINVAL; 251021a852b0SQu Wenruo } 251121a852b0SQu Wenruo 251221a852b0SQu Wenruo /* Root alignment check */ 251321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 251421a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 251521a852b0SQu Wenruo btrfs_super_root(sb)); 251621a852b0SQu Wenruo ret = -EINVAL; 251721a852b0SQu Wenruo } 251821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 251921a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 252021a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 252121a852b0SQu Wenruo ret = -EINVAL; 252221a852b0SQu Wenruo } 252321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 252421a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 252521a852b0SQu Wenruo btrfs_super_log_root(sb)); 252621a852b0SQu Wenruo ret = -EINVAL; 252721a852b0SQu Wenruo } 252821a852b0SQu Wenruo 2529de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 25307239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 253121a852b0SQu Wenruo btrfs_err(fs_info, 25327239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2533de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 253421a852b0SQu Wenruo ret = -EINVAL; 253521a852b0SQu Wenruo } 253621a852b0SQu Wenruo 253721a852b0SQu Wenruo /* 253821a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 253921a852b0SQu Wenruo * done later 254021a852b0SQu Wenruo */ 254121a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 254221a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 254321a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 254421a852b0SQu Wenruo ret = -EINVAL; 254521a852b0SQu Wenruo } 254621a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 254721a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 254821a852b0SQu Wenruo btrfs_super_stripesize(sb)); 254921a852b0SQu Wenruo ret = -EINVAL; 255021a852b0SQu Wenruo } 255121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 255221a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 255321a852b0SQu Wenruo btrfs_super_num_devices(sb)); 255421a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 255521a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 255621a852b0SQu Wenruo ret = -EINVAL; 255721a852b0SQu Wenruo } 255821a852b0SQu Wenruo 2559069ec957SQu Wenruo if (mirror_num >= 0 && 2560069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 256121a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 256221a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 256321a852b0SQu Wenruo ret = -EINVAL; 256421a852b0SQu Wenruo } 256521a852b0SQu Wenruo 256621a852b0SQu Wenruo /* 256721a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 256821a852b0SQu Wenruo * and one chunk 256921a852b0SQu Wenruo */ 257021a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 257121a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 257221a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 257321a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 257421a852b0SQu Wenruo ret = -EINVAL; 257521a852b0SQu Wenruo } 257621a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 257721a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 257821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 257921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 258021a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 258121a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 258221a852b0SQu Wenruo ret = -EINVAL; 258321a852b0SQu Wenruo } 258421a852b0SQu Wenruo 258521a852b0SQu Wenruo /* 258621a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 258721a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 258821a852b0SQu Wenruo */ 258921a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 259021a852b0SQu Wenruo btrfs_warn(fs_info, 259121a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 259221a852b0SQu Wenruo btrfs_super_generation(sb), 259321a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 259421a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 259521a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 259621a852b0SQu Wenruo btrfs_warn(fs_info, 259721a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 259821a852b0SQu Wenruo btrfs_super_generation(sb), 259921a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 260021a852b0SQu Wenruo 260121a852b0SQu Wenruo return ret; 260221a852b0SQu Wenruo } 260321a852b0SQu Wenruo 2604069ec957SQu Wenruo /* 2605069ec957SQu Wenruo * Validation of super block at mount time. 2606069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2607069ec957SQu Wenruo * flags will be skipped. 2608069ec957SQu Wenruo */ 2609069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2610069ec957SQu Wenruo { 2611069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2612069ec957SQu Wenruo } 2613069ec957SQu Wenruo 261475cb857dSQu Wenruo /* 261575cb857dSQu Wenruo * Validation of super block at write time. 261675cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 261775cb857dSQu Wenruo * overwritten soon. 261875cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 261975cb857dSQu Wenruo */ 262075cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 262175cb857dSQu Wenruo struct btrfs_super_block *sb) 262275cb857dSQu Wenruo { 262375cb857dSQu Wenruo int ret; 262475cb857dSQu Wenruo 262575cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 262675cb857dSQu Wenruo if (ret < 0) 262775cb857dSQu Wenruo goto out; 2628e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 262975cb857dSQu Wenruo ret = -EUCLEAN; 263075cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 263175cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 263275cb857dSQu Wenruo goto out; 263375cb857dSQu Wenruo } 263475cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 263575cb857dSQu Wenruo ret = -EUCLEAN; 263675cb857dSQu Wenruo btrfs_err(fs_info, 263775cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 263875cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 263975cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 264075cb857dSQu Wenruo goto out; 264175cb857dSQu Wenruo } 264275cb857dSQu Wenruo out: 264375cb857dSQu Wenruo if (ret < 0) 264475cb857dSQu Wenruo btrfs_err(fs_info, 264575cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 264675cb857dSQu Wenruo return ret; 264775cb857dSQu Wenruo } 264875cb857dSQu Wenruo 26496ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2650b8522a1eSNikolay Borisov { 26516ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2652b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2653b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2654b8522a1eSNikolay Borisov bool handle_error = false; 2655b8522a1eSNikolay Borisov int ret = 0; 2656b8522a1eSNikolay Borisov int i; 2657b8522a1eSNikolay Borisov 2658b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2659b8522a1eSNikolay Borisov u64 generation; 2660b8522a1eSNikolay Borisov int level; 2661b8522a1eSNikolay Borisov 2662b8522a1eSNikolay Borisov if (handle_error) { 2663b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2664b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2665b8522a1eSNikolay Borisov tree_root->node = NULL; 2666b8522a1eSNikolay Borisov 2667b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2668b8522a1eSNikolay Borisov break; 2669b8522a1eSNikolay Borisov 2670b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2671b8522a1eSNikolay Borisov 2672b8522a1eSNikolay Borisov /* 2673b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2674b8522a1eSNikolay Borisov * valid 2675b8522a1eSNikolay Borisov */ 2676b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2677b8522a1eSNikolay Borisov 2678b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2679b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2680b8522a1eSNikolay Borisov 2681b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26826ef108ddSNikolay Borisov backup_index = ret; 2683b8522a1eSNikolay Borisov if (ret < 0) 2684b8522a1eSNikolay Borisov return ret; 2685b8522a1eSNikolay Borisov } 2686b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2687b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2688b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 26891b7ec85eSJosef Bacik BTRFS_ROOT_TREE_OBJECTID, 2690b8522a1eSNikolay Borisov generation, level, NULL); 26910465337cSJosef Bacik if (IS_ERR(tree_root->node)) { 2692217f5004SNikolay Borisov handle_error = true; 2693b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 26940465337cSJosef Bacik tree_root->node = NULL; 2695217f5004SNikolay Borisov btrfs_warn(fs_info, "couldn't read tree root"); 2696217f5004SNikolay Borisov continue; 2697b8522a1eSNikolay Borisov 2698217f5004SNikolay Borisov } else if (!extent_buffer_uptodate(tree_root->node)) { 2699217f5004SNikolay Borisov handle_error = true; 2700217f5004SNikolay Borisov ret = -EIO; 2701217f5004SNikolay Borisov btrfs_warn(fs_info, "error while reading tree root"); 2702b8522a1eSNikolay Borisov continue; 2703b8522a1eSNikolay Borisov } 2704b8522a1eSNikolay Borisov 2705b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2706b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2707b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2708b8522a1eSNikolay Borisov 2709336a0d8dSNikolay Borisov /* 2710336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2711336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2712336a0d8dSNikolay Borisov */ 2713453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 2714b8522a1eSNikolay Borisov if (ret < 0) { 2715b8522a1eSNikolay Borisov handle_error = true; 2716b8522a1eSNikolay Borisov continue; 2717b8522a1eSNikolay Borisov } 2718b8522a1eSNikolay Borisov 27196b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 2720b8522a1eSNikolay Borisov 2721b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2722b8522a1eSNikolay Borisov if (ret < 0) { 2723b8522a1eSNikolay Borisov handle_error = true; 2724b8522a1eSNikolay Borisov continue; 2725b8522a1eSNikolay Borisov } 2726b8522a1eSNikolay Borisov 2727b8522a1eSNikolay Borisov /* All successful */ 2728b8522a1eSNikolay Borisov fs_info->generation = generation; 2729b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 27306ef108ddSNikolay Borisov 27316ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 27326ef108ddSNikolay Borisov if (backup_index < 0) { 27336ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 27346ef108ddSNikolay Borisov } else { 27356ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 27366ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 27376ef108ddSNikolay Borisov } 2738b8522a1eSNikolay Borisov break; 2739b8522a1eSNikolay Borisov } 2740b8522a1eSNikolay Borisov 2741b8522a1eSNikolay Borisov return ret; 2742b8522a1eSNikolay Borisov } 2743b8522a1eSNikolay Borisov 27448260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2745eb60ceacSChris Mason { 274676dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2747f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 27488fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2749facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 275024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2751eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 275211833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2753eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2754a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 275576dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 275624bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 27574cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2758ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2759f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 276047ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2761f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2762d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 276367c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 27647585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2765573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2766de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 276719c00ddcSChris Mason 27680b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27696324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2770f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 277147ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2772bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2773bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 27743fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 27753fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2776bd647ce3SJosef Bacik #endif 2777c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 277866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 277966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 278066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 278166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 278266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 278366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 278466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2785ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2786ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2787ba2c4d4eSJosef Bacik 2788771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27894cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27902fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2791034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2792fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 279395ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 27949ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 27954cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2796a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2797f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 27988b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2799f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 280090519d66SArne Jansen /* readahead state */ 2801d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 280290519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2803fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2804c8b97818SChris Mason 2805b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2806b34b086cSChris Mason num_online_cpus() + 2, 8); 28070afbaf8cSChris Mason 2808199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2809199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 281069fe2d75SJosef Bacik 2811638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 281221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 281321adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 281421adbd5cSStefan Behrens #endif 2815779a65a4SEric Sandeen btrfs_init_balance(fs_info); 281657056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 2817a2de733cSArne Jansen 28180f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 28196bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2820a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 28210f9dd46cSJosef Bacik 2822fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2823fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2824afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 282539279cc3SChris Mason 28265a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2827e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2828925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2829a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2830a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 28311bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 28329e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2833c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 283476dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2835803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2836fa9c0d79SChris Mason 2837ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2838f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2839b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2840416ac51dSArne Jansen 2841fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2842fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2843fa9c0d79SChris Mason 2844e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2845f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2846bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 28474854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2848034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28493768f368SChris Mason 2850da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2851da17066cSJeff Mahoney fs_info->nodesize = 4096; 2852da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2853ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 2854da17066cSJeff Mahoney fs_info->stripesize = 4096; 2855da17066cSJeff Mahoney 2856eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2857eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2858eede2bf3SOmar Sandoval 28599e967495SFilipe Manana fs_info->send_in_progress = 0; 28608260edbaSJosef Bacik } 28618260edbaSJosef Bacik 28628260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 28638260edbaSJosef Bacik { 28648260edbaSJosef Bacik int ret; 28658260edbaSJosef Bacik 28668260edbaSJosef Bacik fs_info->sb = sb; 28678260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28688260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 28699e967495SFilipe Manana 28705deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 2871ae18c37aSJosef Bacik if (ret) 2872ae18c37aSJosef Bacik return ret; 2873ae18c37aSJosef Bacik 2874ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2875ae18c37aSJosef Bacik if (ret) 2876c75e8394SJosef Bacik return ret; 2877ae18c37aSJosef Bacik 2878ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2879ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2880ae18c37aSJosef Bacik 2881ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2882ae18c37aSJosef Bacik if (ret) 2883c75e8394SJosef Bacik return ret; 2884ae18c37aSJosef Bacik 2885ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2886ae18c37aSJosef Bacik GFP_KERNEL); 2887ae18c37aSJosef Bacik if (ret) 2888c75e8394SJosef Bacik return ret; 2889ae18c37aSJosef Bacik 2890ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2891ae18c37aSJosef Bacik GFP_KERNEL); 2892c75e8394SJosef Bacik if (!fs_info->delayed_root) 2893c75e8394SJosef Bacik return -ENOMEM; 2894ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2895ae18c37aSJosef Bacik 2896a0a1db70SFilipe Manana if (sb_rdonly(sb)) 2897a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 2898a0a1db70SFilipe Manana 2899c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 2900ae18c37aSJosef Bacik } 2901ae18c37aSJosef Bacik 290297f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 290397f4dd09SNikolay Borisov { 290497f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 290597f4dd09SNikolay Borisov int ret; 290697f4dd09SNikolay Borisov 290797f4dd09SNikolay Borisov /* 290897f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 290997f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 291097f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 291197f4dd09SNikolay Borisov */ 291297f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 291397f4dd09SNikolay Borisov if (ret < 0) { 2914c94bec2cSJosef Bacik if (ret != -EINTR) 2915c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 2916c94bec2cSJosef Bacik ret); 291797f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 291897f4dd09SNikolay Borisov return ret; 291997f4dd09SNikolay Borisov } 292097f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 292197f4dd09SNikolay Borisov } 292297f4dd09SNikolay Borisov 292397f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 292497f4dd09SNikolay Borisov { 292597f4dd09SNikolay Borisov struct task_struct *task; 292697f4dd09SNikolay Borisov 292797f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 292897f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 292997f4dd09SNikolay Borisov if (IS_ERR(task)) { 293097f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 293197f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 293297f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 293397f4dd09SNikolay Borisov return PTR_ERR(task); 293497f4dd09SNikolay Borisov } 293597f4dd09SNikolay Borisov 293697f4dd09SNikolay Borisov return 0; 293797f4dd09SNikolay Borisov } 293897f4dd09SNikolay Borisov 293944c0ca21SBoris Burkov /* 29408cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 29418cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 29428cd29088SBoris Burkov * code paths. 29438cd29088SBoris Burkov */ 29448cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 29458cd29088SBoris Burkov { 29468cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 29478b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 29488cd29088SBoris Burkov } 29498cd29088SBoris Burkov 29508cd29088SBoris Burkov /* 295144c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 295244c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 295344c0ca21SBoris Burkov */ 295444c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 295544c0ca21SBoris Burkov { 295644c0ca21SBoris Burkov int ret; 295794846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 29588b228324SBoris Burkov bool clear_free_space_tree = false; 29598b228324SBoris Burkov 29608b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 29618b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 29628b228324SBoris Burkov clear_free_space_tree = true; 29638b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 29648b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 29658b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 29668b228324SBoris Burkov clear_free_space_tree = true; 29678b228324SBoris Burkov } 29688b228324SBoris Burkov 29698b228324SBoris Burkov if (clear_free_space_tree) { 29708b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 29718b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 29728b228324SBoris Burkov if (ret) { 29738b228324SBoris Burkov btrfs_warn(fs_info, 29748b228324SBoris Burkov "failed to clear free space tree: %d", ret); 29758b228324SBoris Burkov goto out; 29768b228324SBoris Burkov } 29778b228324SBoris Burkov } 297844c0ca21SBoris Burkov 297944c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 298044c0ca21SBoris Burkov if (ret) 298144c0ca21SBoris Burkov goto out; 298244c0ca21SBoris Burkov 29838f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 29848f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 29858f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 29868f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 29878f1c21d7SBoris Burkov goto out; 29888f1c21d7SBoris Burkov } 29898f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 29908f1c21d7SBoris Burkov 299144c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 299244c0ca21SBoris Burkov ret = btrfs_recover_relocation(fs_info->tree_root); 299344c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 299444c0ca21SBoris Burkov if (ret < 0) { 299544c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 299644c0ca21SBoris Burkov goto out; 299744c0ca21SBoris Burkov } 299844c0ca21SBoris Burkov 29995011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 30005011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 30015011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 30025011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 30035011139aSBoris Burkov if (ret) { 30045011139aSBoris Burkov btrfs_warn(fs_info, 30055011139aSBoris Burkov "failed to create free space tree: %d", ret); 30065011139aSBoris Burkov goto out; 30075011139aSBoris Burkov } 30085011139aSBoris Burkov } 30095011139aSBoris Burkov 301094846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 301194846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 301294846229SBoris Burkov if (ret) 301394846229SBoris Burkov goto out; 301494846229SBoris Burkov } 301594846229SBoris Burkov 301644c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 301744c0ca21SBoris Burkov if (ret) 301844c0ca21SBoris Burkov goto out; 301944c0ca21SBoris Burkov 302044c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 302144c0ca21SBoris Burkov if (ret) { 302244c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 302344c0ca21SBoris Burkov goto out; 302444c0ca21SBoris Burkov } 302544c0ca21SBoris Burkov 302644c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 302744c0ca21SBoris Burkov 302844c0ca21SBoris Burkov if (!fs_info->uuid_root) { 302944c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 303044c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 303144c0ca21SBoris Burkov if (ret) { 303244c0ca21SBoris Burkov btrfs_warn(fs_info, 303344c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 303444c0ca21SBoris Burkov goto out; 303544c0ca21SBoris Burkov } 303644c0ca21SBoris Burkov } 303744c0ca21SBoris Burkov 3038638331faSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 303944c0ca21SBoris Burkov out: 304044c0ca21SBoris Burkov return ret; 304144c0ca21SBoris Burkov } 304244c0ca21SBoris Burkov 3043ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3044ae18c37aSJosef Bacik char *options) 3045ae18c37aSJosef Bacik { 3046ae18c37aSJosef Bacik u32 sectorsize; 3047ae18c37aSJosef Bacik u32 nodesize; 3048ae18c37aSJosef Bacik u32 stripesize; 3049ae18c37aSJosef Bacik u64 generation; 3050ae18c37aSJosef Bacik u64 features; 3051ae18c37aSJosef Bacik u16 csum_type; 3052ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3053ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3054ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3055ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3056ae18c37aSJosef Bacik int ret; 3057ae18c37aSJosef Bacik int err = -EINVAL; 3058ae18c37aSJosef Bacik int level; 3059ae18c37aSJosef Bacik 30608260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 306153b381b3SDavid Woodhouse if (ret) { 306283c8266aSDavid Sterba err = ret; 3063ae18c37aSJosef Bacik goto fail; 306453b381b3SDavid Woodhouse } 306553b381b3SDavid Woodhouse 3066ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3067ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3068ae18c37aSJosef Bacik GFP_KERNEL); 3069ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3070ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3071ae18c37aSJosef Bacik GFP_KERNEL); 3072ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3073ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3074ae18c37aSJosef Bacik err = -ENOMEM; 3075c75e8394SJosef Bacik goto fail; 3076ae18c37aSJosef Bacik } 3077ae18c37aSJosef Bacik 3078ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3079ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3080ae18c37aSJosef Bacik err = -ENOMEM; 3081c75e8394SJosef Bacik goto fail; 3082ae18c37aSJosef Bacik } 3083ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3084ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3085ae18c37aSJosef Bacik 30863c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 30871104a885SDavid Sterba 30881104a885SDavid Sterba /* 30891104a885SDavid Sterba * Read super block and check the signature bytes only 30901104a885SDavid Sterba */ 30918f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 30928f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 30938f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 309416cdcec7SMiao Xie goto fail_alloc; 309520b45077SDave Young } 309639279cc3SChris Mason 30971104a885SDavid Sterba /* 3098260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 30998dc3f22cSJohannes Thumshirn * corrupted, we'll find out 31008dc3f22cSJohannes Thumshirn */ 31018f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 310251bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 31038dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 310451bce6c9SJohannes Thumshirn csum_type); 31058dc3f22cSJohannes Thumshirn err = -EINVAL; 31068f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31078dc3f22cSJohannes Thumshirn goto fail_alloc; 31088dc3f22cSJohannes Thumshirn } 31098dc3f22cSJohannes Thumshirn 31106d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 31116d97c6e3SJohannes Thumshirn if (ret) { 31126d97c6e3SJohannes Thumshirn err = ret; 31138f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31146d97c6e3SJohannes Thumshirn goto fail_alloc; 31156d97c6e3SJohannes Thumshirn } 31166d97c6e3SJohannes Thumshirn 31178dc3f22cSJohannes Thumshirn /* 31181104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 31191104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 31201104a885SDavid Sterba */ 31218f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 312205135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 31231104a885SDavid Sterba err = -EINVAL; 31248f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3125141386e1SJosef Bacik goto fail_alloc; 31261104a885SDavid Sterba } 31271104a885SDavid Sterba 31281104a885SDavid Sterba /* 31291104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 31301104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 31311104a885SDavid Sterba * the whole block of INFO_SIZE 31321104a885SDavid Sterba */ 31338f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 31348f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31355f39d397SChris Mason 3136fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3137fbc6feaeSNikolay Borisov 3138de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 3139de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 3140de37aa51SNikolay Borisov 31417239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 3142de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 3143de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 3144de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 31457239ff4bSNikolay Borisov } 31460b86a832SChris Mason 3147fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3148fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3149fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3150fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3151fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3152fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3153fbc6feaeSNikolay Borisov } 3154fbc6feaeSNikolay Borisov 3155fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3156fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3157de37aa51SNikolay Borisov 3158069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 31591104a885SDavid Sterba if (ret) { 316005135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 31611104a885SDavid Sterba err = -EINVAL; 3162141386e1SJosef Bacik goto fail_alloc; 31631104a885SDavid Sterba } 31641104a885SDavid Sterba 31650f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3166141386e1SJosef Bacik goto fail_alloc; 31670f7d52f4SChris Mason 3168acce952bSliubo /* check FS state, whether FS is broken. */ 316987533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 317087533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3171acce952bSliubo 317275e7cb7fSLiu Bo /* 317375e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 317475e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 317575e7cb7fSLiu Bo */ 317675e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 317775e7cb7fSLiu Bo 31782ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 31792b82032cSYan Zheng if (ret) { 31802b82032cSYan Zheng err = ret; 3181141386e1SJosef Bacik goto fail_alloc; 31822b82032cSYan Zheng } 3183dfe25020SChris Mason 3184f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3185f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3186f2b636e8SJosef Bacik if (features) { 318705135f59SDavid Sterba btrfs_err(fs_info, 318805135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3189c1c9ff7cSGeert Uytterhoeven features); 3190f2b636e8SJosef Bacik err = -EINVAL; 3191141386e1SJosef Bacik goto fail_alloc; 3192f2b636e8SJosef Bacik } 3193f2b636e8SJosef Bacik 31945d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 31955d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 31960b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3197a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 31985c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 31995c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3200727011e0SChris Mason 32013173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 320205135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 32033173a18fSJosef Bacik 3204b70f5097SNaohiro Aota fs_info->zoned = (features & BTRFS_FEATURE_INCOMPAT_ZONED); 3205b70f5097SNaohiro Aota 3206727011e0SChris Mason /* 3207727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3208727011e0SChris Mason * they are bigger than the page size 3209727011e0SChris Mason */ 321009cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3211727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 321205135f59SDavid Sterba btrfs_info(fs_info, 321305135f59SDavid Sterba "flagging fs with big metadata feature"); 3214727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3215727011e0SChris Mason } 3216727011e0SChris Mason 3217bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3218bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3219b7f67055SChandan Rajendra stripesize = sectorsize; 3220707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3221963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3222bc3f116fSChris Mason 3223da17066cSJeff Mahoney /* Cache block sizes */ 3224da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3225da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3226ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 322722b6331dSDavid Sterba fs_info->csum_size = btrfs_super_csum_size(disk_super); 3228fe5ecbe8SDavid Sterba fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3229da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3230da17066cSJeff Mahoney 3231bc3f116fSChris Mason /* 3232bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3233bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3234bc3f116fSChris Mason */ 3235bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3236707e8a07SDavid Sterba (sectorsize != nodesize)) { 323705135f59SDavid Sterba btrfs_err(fs_info, 323805135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 323905135f59SDavid Sterba nodesize, sectorsize); 3240141386e1SJosef Bacik goto fail_alloc; 3241bc3f116fSChris Mason } 3242bc3f116fSChris Mason 3243ceda0864SMiao Xie /* 3244ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3245ceda0864SMiao Xie * update the flag. 3246ceda0864SMiao Xie */ 32475d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 32485d4f98a2SYan Zheng 3249f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3250f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3251bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 325205135f59SDavid Sterba btrfs_err(fs_info, 325305135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3254c1c9ff7cSGeert Uytterhoeven features); 3255f2b636e8SJosef Bacik err = -EINVAL; 3256141386e1SJosef Bacik goto fail_alloc; 3257f2b636e8SJosef Bacik } 325861d92c32SChris Mason 3259*0bb3eb3eSQu Wenruo /* For 4K sector size support, it's only read-only */ 3260*0bb3eb3eSQu Wenruo if (PAGE_SIZE == SZ_64K && sectorsize == SZ_4K) { 3261*0bb3eb3eSQu Wenruo if (!sb_rdonly(sb) || btrfs_super_log_root(disk_super)) { 3262*0bb3eb3eSQu Wenruo btrfs_err(fs_info, 3263*0bb3eb3eSQu Wenruo "subpage sectorsize %u only supported read-only for page size %lu", 3264*0bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 3265*0bb3eb3eSQu Wenruo err = -EINVAL; 3266*0bb3eb3eSQu Wenruo goto fail_alloc; 3267*0bb3eb3eSQu Wenruo } 3268*0bb3eb3eSQu Wenruo } 3269*0bb3eb3eSQu Wenruo 32702a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 32712a458198SEric Sandeen if (ret) { 32722a458198SEric Sandeen err = ret; 32730dc3b84aSJosef Bacik goto fail_sb_buffer; 32740dc3b84aSJosef Bacik } 32754543df7eSChris Mason 32769e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 32779e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 32784575c9ccSChris Mason 3279a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3280a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3281de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3282db94535dSChris Mason 3283925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 32846bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3285925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 328684eed90fSChris Mason if (ret) { 328705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 32885d4f98a2SYan Zheng goto fail_sb_buffer; 328984eed90fSChris Mason } 32900b86a832SChris Mason 329184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3292581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 32930b86a832SChris Mason 32942ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 32950b86a832SChris Mason btrfs_super_chunk_root(disk_super), 32961b7ec85eSJosef Bacik BTRFS_CHUNK_TREE_OBJECTID, 3297581c1760SQu Wenruo generation, level, NULL); 329864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 329964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 330005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3301e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3302e5fffbacSchandan free_extent_buffer(chunk_root->node); 330395ab1f64SZhao Lei chunk_root->node = NULL; 3304af31f5e5SChris Mason goto fail_tree_roots; 330583121942SDavid Woodhouse } 33065d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 33075d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 33080b86a832SChris Mason 3309e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3310c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3311c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3312e17cade2SChris Mason 33135b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 33142b82032cSYan Zheng if (ret) { 331505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3316af31f5e5SChris Mason goto fail_tree_roots; 33172b82032cSYan Zheng } 33180b86a832SChris Mason 33198dabb742SStefan Behrens /* 3320bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3321bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3322bacce86aSAnand Jain * target is required. So free devices that are not part of this 3323bacce86aSAnand Jain * filesystem but skip the replace traget device which is checked 3324bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 33258dabb742SStefan Behrens */ 3326bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3327a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 332805135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3329a6b0d5c8SChris Mason goto fail_tree_roots; 3330a6b0d5c8SChris Mason } 3331a6b0d5c8SChris Mason 3332b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 33334bbcaa64SEric Sandeen if (ret) 3334b8522a1eSNikolay Borisov goto fail_tree_roots; 33358929ecfaSYan, Zheng 333675ec1db8SJosef Bacik /* 333775ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 333875ec1db8SJosef Bacik * check the generation here so we can set the 333975ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 334075ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 334175ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 334275ec1db8SJosef Bacik * even though it was perfectly fine. 334375ec1db8SJosef Bacik */ 334475ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 334575ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 334675ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 334775ec1db8SJosef Bacik 3348cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3349cf90d884SQu Wenruo if (ret) { 3350cf90d884SQu Wenruo btrfs_err(fs_info, 3351cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3352cf90d884SQu Wenruo ret); 3353cf90d884SQu Wenruo goto fail_block_groups; 3354cf90d884SQu Wenruo } 335568310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 335668310a5eSIlya Dryomov if (ret) { 335705135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 335868310a5eSIlya Dryomov goto fail_block_groups; 335968310a5eSIlya Dryomov } 336068310a5eSIlya Dryomov 3361733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3362733f4fbbSStefan Behrens if (ret) { 336305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3364733f4fbbSStefan Behrens goto fail_block_groups; 3365733f4fbbSStefan Behrens } 3366733f4fbbSStefan Behrens 33678dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 33688dabb742SStefan Behrens if (ret) { 336905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 33708dabb742SStefan Behrens goto fail_block_groups; 33718dabb742SStefan Behrens } 33728dabb742SStefan Behrens 3373b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3374b70f5097SNaohiro Aota if (ret) { 3375b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3376b70f5097SNaohiro Aota ret); 3377b70f5097SNaohiro Aota goto fail_block_groups; 3378b70f5097SNaohiro Aota } 3379b70f5097SNaohiro Aota 3380c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3381b7c35e81SAnand Jain if (ret) { 338205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 338305135f59SDavid Sterba ret); 3384b7c35e81SAnand Jain goto fail_block_groups; 3385b7c35e81SAnand Jain } 3386b7c35e81SAnand Jain 338796f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 33885ac1d209SJeff Mahoney if (ret) { 338905135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3390b7c35e81SAnand Jain goto fail_fsdev_sysfs; 33915ac1d209SJeff Mahoney } 33925ac1d209SJeff Mahoney 3393c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3394c59021f8Sliubo if (ret) { 339505135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 33962365dd3cSAnand Jain goto fail_sysfs; 3397c59021f8Sliubo } 3398c59021f8Sliubo 33995b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 34001b1d1f66SJosef Bacik if (ret) { 340105135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 34022365dd3cSAnand Jain goto fail_sysfs; 34031b1d1f66SJosef Bacik } 34044330e183SQu Wenruo 34056528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 340605135f59SDavid Sterba btrfs_warn(fs_info, 340752042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 34082365dd3cSAnand Jain goto fail_sysfs; 3409292fd7fcSStefan Behrens } 34109078a3e1SChris Mason 3411a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3412a74a4b97SChris Mason "btrfs-cleaner"); 341357506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 34142365dd3cSAnand Jain goto fail_sysfs; 3415a74a4b97SChris Mason 3416a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3417a74a4b97SChris Mason tree_root, 3418a74a4b97SChris Mason "btrfs-transaction"); 341957506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 34203f157a2fSChris Mason goto fail_cleaner; 3421a74a4b97SChris Mason 3422583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3423c289811cSChris Mason !fs_info->fs_devices->rotating) { 3424583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3425c289811cSChris Mason } 3426c289811cSChris Mason 3427572d9ab7SDavid Sterba /* 342801327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3429572d9ab7SDavid Sterba * not have to wait for transaction commit 3430572d9ab7SDavid Sterba */ 3431572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 34323818aea2SQu Wenruo 343321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 34340b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 34352ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 34360b246afaSJeff Mahoney btrfs_test_opt(fs_info, 343721adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 343821adbd5cSStefan Behrens 1 : 0, 343921adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 344021adbd5cSStefan Behrens if (ret) 344105135f59SDavid Sterba btrfs_warn(fs_info, 344205135f59SDavid Sterba "failed to initialize integrity check module: %d", 344305135f59SDavid Sterba ret); 344421adbd5cSStefan Behrens } 344521adbd5cSStefan Behrens #endif 3446bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3447bcef60f2SArne Jansen if (ret) 3448bcef60f2SArne Jansen goto fail_trans_kthread; 344921adbd5cSStefan Behrens 3450fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3451fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3452fd708b81SJosef Bacik 345396da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 345496da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 34550b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3456e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 345763443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 345879787eaaSJeff Mahoney if (ret) { 345963443bf5SEric Sandeen err = ret; 346028c16cbbSWang Shilong goto fail_qgroup; 3461e556ce2cSYan Zheng } 3462e02119d5SChris Mason } 34631a40e23bSZheng Yan 346456e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 34653140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 34663140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3467f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3468315bf8efSJosef Bacik fs_info->fs_root = NULL; 3469bcef60f2SArne Jansen goto fail_qgroup; 34703140c9a3SDan Carpenter } 3471c289811cSChris Mason 3472bc98a42cSDavid Howells if (sb_rdonly(sb)) 34738cd29088SBoris Burkov goto clear_oneshot; 34742b6ba629SIlya Dryomov 347544c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 34762b6ba629SIlya Dryomov if (ret) { 34776bccf3abSJeff Mahoney close_ctree(fs_info); 34782b6ba629SIlya Dryomov return ret; 3479e3acc2a6SJosef Bacik } 3480b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3481b382a324SJan Schmidt 348244c0ca21SBoris Burkov if (fs_info->uuid_root && 348344c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 348444c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 348505135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 348670f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 348770f80175SStefan Behrens if (ret) { 348805135f59SDavid Sterba btrfs_warn(fs_info, 348905135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 34906bccf3abSJeff Mahoney close_ctree(fs_info); 349170f80175SStefan Behrens return ret; 349270f80175SStefan Behrens } 3493f7a81ea4SStefan Behrens } 349494846229SBoris Burkov 3495afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 349647ab2a6cSJosef Bacik 34978cd29088SBoris Burkov clear_oneshot: 34988cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3499ad2b2c80SAl Viro return 0; 350039279cc3SChris Mason 3501bcef60f2SArne Jansen fail_qgroup: 3502bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 35037c2ca468SChris Mason fail_trans_kthread: 35047c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 35052ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3506faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 35073f157a2fSChris Mason fail_cleaner: 3508a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 35097c2ca468SChris Mason 35107c2ca468SChris Mason /* 35117c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 35127c2ca468SChris Mason * kthreads 35137c2ca468SChris Mason */ 35147c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 35157c2ca468SChris Mason 35162365dd3cSAnand Jain fail_sysfs: 35176618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 35182365dd3cSAnand Jain 3519b7c35e81SAnand Jain fail_fsdev_sysfs: 3520b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3521b7c35e81SAnand Jain 35221b1d1f66SJosef Bacik fail_block_groups: 352354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3524af31f5e5SChris Mason 3525af31f5e5SChris Mason fail_tree_roots: 35269e3aa805SJosef Bacik if (fs_info->data_reloc_root) 35279e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 35284273eaffSAnand Jain free_root_pointers(fs_info, true); 35292b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3530af31f5e5SChris Mason 353139279cc3SChris Mason fail_sb_buffer: 35327abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 35335cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 353416cdcec7SMiao Xie fail_alloc: 3535586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3536586e46e2SIlya Dryomov 35374543df7eSChris Mason iput(fs_info->btree_inode); 35387e662854SQinghuang Feng fail: 3539586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3540ad2b2c80SAl Viro return err; 3541eb60ceacSChris Mason } 3542663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3543eb60ceacSChris Mason 3544314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3545f2984462SChris Mason { 3546314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3547314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3548314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3549314b6dd0SJohannes Thumshirn struct page *page; 3550442a4f63SStefan Behrens 3551314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3552314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3553314b6dd0SJohannes Thumshirn 3554314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3555fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3556314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3557314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3558314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3559314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3560314b6dd0SJohannes Thumshirn SetPageError(page); 3561314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3562314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3563314b6dd0SJohannes Thumshirn } else { 3564314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3565f2984462SChris Mason } 3566314b6dd0SJohannes Thumshirn 3567314b6dd0SJohannes Thumshirn put_page(page); 3568314b6dd0SJohannes Thumshirn unlock_page(page); 3569314b6dd0SJohannes Thumshirn } 3570314b6dd0SJohannes Thumshirn 3571314b6dd0SJohannes Thumshirn bio_put(bio); 3572f2984462SChris Mason } 3573f2984462SChris Mason 35748f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 35758f32380dSJohannes Thumshirn int copy_num) 357629c36d72SAnand Jain { 357729c36d72SAnand Jain struct btrfs_super_block *super; 35788f32380dSJohannes Thumshirn struct page *page; 357912659251SNaohiro Aota u64 bytenr, bytenr_orig; 35808f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 358112659251SNaohiro Aota int ret; 358229c36d72SAnand Jain 358312659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 358412659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 358512659251SNaohiro Aota if (ret == -ENOENT) 358612659251SNaohiro Aota return ERR_PTR(-EINVAL); 358712659251SNaohiro Aota else if (ret) 358812659251SNaohiro Aota return ERR_PTR(ret); 358912659251SNaohiro Aota 359029c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 35918f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 359229c36d72SAnand Jain 35938f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 35948f32380dSJohannes Thumshirn if (IS_ERR(page)) 35958f32380dSJohannes Thumshirn return ERR_CAST(page); 359629c36d72SAnand Jain 35978f32380dSJohannes Thumshirn super = page_address(page); 359896c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 359996c2e067SAnand Jain btrfs_release_disk_super(super); 360096c2e067SAnand Jain return ERR_PTR(-ENODATA); 360196c2e067SAnand Jain } 360296c2e067SAnand Jain 360312659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 36048f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 36058f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 360629c36d72SAnand Jain } 360729c36d72SAnand Jain 36088f32380dSJohannes Thumshirn return super; 360929c36d72SAnand Jain } 361029c36d72SAnand Jain 361129c36d72SAnand Jain 36128f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3613a512bbf8SYan Zheng { 36148f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3615a512bbf8SYan Zheng int i; 3616a512bbf8SYan Zheng u64 transid = 0; 3617a512bbf8SYan Zheng 3618a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3619a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3620a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3621a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3622a512bbf8SYan Zheng */ 3623a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 36248f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 36258f32380dSJohannes Thumshirn if (IS_ERR(super)) 3626a512bbf8SYan Zheng continue; 3627a512bbf8SYan Zheng 3628a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 36298f32380dSJohannes Thumshirn if (latest) 36308f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 36318f32380dSJohannes Thumshirn 36328f32380dSJohannes Thumshirn latest = super; 3633a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3634a512bbf8SYan Zheng } 3635a512bbf8SYan Zheng } 363692fc03fbSAnand Jain 36378f32380dSJohannes Thumshirn return super; 3638a512bbf8SYan Zheng } 3639a512bbf8SYan Zheng 36404eedeb75SHisashi Hifumi /* 3641abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3642314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 36434eedeb75SHisashi Hifumi * 3644abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3645abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3646abbb3b8eSDavid Sterba * same for write and wait phases. 36474eedeb75SHisashi Hifumi * 3648314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 36494eedeb75SHisashi Hifumi */ 3650a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3651abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3652a512bbf8SYan Zheng { 3653d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3654314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3655d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3656a512bbf8SYan Zheng int i; 3657a512bbf8SYan Zheng int errors = 0; 365812659251SNaohiro Aota int ret; 365912659251SNaohiro Aota u64 bytenr, bytenr_orig; 3660a512bbf8SYan Zheng 3661a512bbf8SYan Zheng if (max_mirrors == 0) 3662a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3663a512bbf8SYan Zheng 3664d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3665d5178578SJohannes Thumshirn 3666a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3667314b6dd0SJohannes Thumshirn struct page *page; 3668314b6dd0SJohannes Thumshirn struct bio *bio; 3669314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3670314b6dd0SJohannes Thumshirn 367112659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 367212659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 367312659251SNaohiro Aota if (ret == -ENOENT) { 367412659251SNaohiro Aota continue; 367512659251SNaohiro Aota } else if (ret < 0) { 367612659251SNaohiro Aota btrfs_err(device->fs_info, 367712659251SNaohiro Aota "couldn't get super block location for mirror %d", 367812659251SNaohiro Aota i); 367912659251SNaohiro Aota errors++; 368012659251SNaohiro Aota continue; 368112659251SNaohiro Aota } 3682935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3683935e5cc9SMiao Xie device->commit_total_bytes) 3684a512bbf8SYan Zheng break; 3685a512bbf8SYan Zheng 368612659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 3687a512bbf8SYan Zheng 3688fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3689fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 3690fd08001fSEric Biggers sb->csum); 3691a512bbf8SYan Zheng 3692314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3693314b6dd0SJohannes Thumshirn GFP_NOFS); 3694314b6dd0SJohannes Thumshirn if (!page) { 3695fb456252SJeff Mahoney btrfs_err(device->fs_info, 3696314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3697f14d104dSDavid Sterba bytenr); 3698634554dcSJosef Bacik errors++; 3699634554dcSJosef Bacik continue; 3700634554dcSJosef Bacik } 3701634554dcSJosef Bacik 3702314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3703314b6dd0SJohannes Thumshirn get_page(page); 3704a512bbf8SYan Zheng 3705314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3706314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3707a512bbf8SYan Zheng 3708387125fcSChris Mason /* 3709314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3710314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3711314b6dd0SJohannes Thumshirn * checking. 3712387125fcSChris Mason */ 3713314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3714314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3715314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3716314b6dd0SJohannes Thumshirn bio->bi_private = device; 3717314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3718314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3719314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3720314b6dd0SJohannes Thumshirn 3721314b6dd0SJohannes Thumshirn /* 3722314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3723314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3724314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3725314b6dd0SJohannes Thumshirn */ 3726314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 37271b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3728314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3729314b6dd0SJohannes Thumshirn 3730314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 373112659251SNaohiro Aota btrfs_advance_sb_log(device, i); 3732a512bbf8SYan Zheng } 3733a512bbf8SYan Zheng return errors < i ? 0 : -1; 3734a512bbf8SYan Zheng } 3735a512bbf8SYan Zheng 3736387125fcSChris Mason /* 3737abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3738abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3739abbb3b8eSDavid Sterba * 3740314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3741abbb3b8eSDavid Sterba * date. 3742abbb3b8eSDavid Sterba */ 3743abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3744abbb3b8eSDavid Sterba { 3745abbb3b8eSDavid Sterba int i; 3746abbb3b8eSDavid Sterba int errors = 0; 3747b6a535faSHoward McLauchlan bool primary_failed = false; 374812659251SNaohiro Aota int ret; 3749abbb3b8eSDavid Sterba u64 bytenr; 3750abbb3b8eSDavid Sterba 3751abbb3b8eSDavid Sterba if (max_mirrors == 0) 3752abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3753abbb3b8eSDavid Sterba 3754abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3755314b6dd0SJohannes Thumshirn struct page *page; 3756314b6dd0SJohannes Thumshirn 375712659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 375812659251SNaohiro Aota if (ret == -ENOENT) { 375912659251SNaohiro Aota break; 376012659251SNaohiro Aota } else if (ret < 0) { 376112659251SNaohiro Aota errors++; 376212659251SNaohiro Aota if (i == 0) 376312659251SNaohiro Aota primary_failed = true; 376412659251SNaohiro Aota continue; 376512659251SNaohiro Aota } 3766abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3767abbb3b8eSDavid Sterba device->commit_total_bytes) 3768abbb3b8eSDavid Sterba break; 3769abbb3b8eSDavid Sterba 3770314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3771314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3772314b6dd0SJohannes Thumshirn if (!page) { 3773abbb3b8eSDavid Sterba errors++; 3774b6a535faSHoward McLauchlan if (i == 0) 3775b6a535faSHoward McLauchlan primary_failed = true; 3776abbb3b8eSDavid Sterba continue; 3777abbb3b8eSDavid Sterba } 3778314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3779314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3780314b6dd0SJohannes Thumshirn if (PageError(page)) { 3781abbb3b8eSDavid Sterba errors++; 3782b6a535faSHoward McLauchlan if (i == 0) 3783b6a535faSHoward McLauchlan primary_failed = true; 3784b6a535faSHoward McLauchlan } 3785abbb3b8eSDavid Sterba 3786314b6dd0SJohannes Thumshirn /* Drop our reference */ 3787314b6dd0SJohannes Thumshirn put_page(page); 3788abbb3b8eSDavid Sterba 3789314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3790314b6dd0SJohannes Thumshirn put_page(page); 3791abbb3b8eSDavid Sterba } 3792abbb3b8eSDavid Sterba 3793b6a535faSHoward McLauchlan /* log error, force error return */ 3794b6a535faSHoward McLauchlan if (primary_failed) { 3795b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3796b6a535faSHoward McLauchlan device->devid); 3797b6a535faSHoward McLauchlan return -1; 3798b6a535faSHoward McLauchlan } 3799b6a535faSHoward McLauchlan 3800abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3801abbb3b8eSDavid Sterba } 3802abbb3b8eSDavid Sterba 3803abbb3b8eSDavid Sterba /* 3804387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3805387125fcSChris Mason * for the barrier when it is done 3806387125fcSChris Mason */ 38074246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3808387125fcSChris Mason { 3809387125fcSChris Mason complete(bio->bi_private); 3810387125fcSChris Mason } 3811387125fcSChris Mason 3812387125fcSChris Mason /* 38134fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 38144fc6441aSAnand Jain * done in the waiting counterpart. 3815387125fcSChris Mason */ 38164fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3817387125fcSChris Mason { 3818c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3819e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3820387125fcSChris Mason 3821c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 38224fc6441aSAnand Jain return; 3823387125fcSChris Mason 3824e0ae9994SDavid Sterba bio_reset(bio); 3825387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 382674d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 38278d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3828387125fcSChris Mason init_completion(&device->flush_wait); 3829387125fcSChris Mason bio->bi_private = &device->flush_wait; 3830387125fcSChris Mason 383143a01111SLu Fengqi btrfsic_submit_bio(bio); 38321c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 38334fc6441aSAnand Jain } 3834387125fcSChris Mason 38354fc6441aSAnand Jain /* 38364fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 38374fc6441aSAnand Jain */ 38388c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 38394fc6441aSAnand Jain { 38404fc6441aSAnand Jain struct bio *bio = device->flush_bio; 38414fc6441aSAnand Jain 38421c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 384358efbc9fSOmar Sandoval return BLK_STS_OK; 38444fc6441aSAnand Jain 38451c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 38462980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 38474fc6441aSAnand Jain 38488c27cb35SLinus Torvalds return bio->bi_status; 3849387125fcSChris Mason } 3850387125fcSChris Mason 3851d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3852401b41e5SAnand Jain { 38536528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3854401b41e5SAnand Jain return -EIO; 3855387125fcSChris Mason return 0; 3856387125fcSChris Mason } 3857387125fcSChris Mason 3858387125fcSChris Mason /* 3859387125fcSChris Mason * send an empty flush down to each device in parallel, 3860387125fcSChris Mason * then wait for them 3861387125fcSChris Mason */ 3862387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3863387125fcSChris Mason { 3864387125fcSChris Mason struct list_head *head; 3865387125fcSChris Mason struct btrfs_device *dev; 38665af3e8ccSStefan Behrens int errors_wait = 0; 38674e4cbee9SChristoph Hellwig blk_status_t ret; 3868387125fcSChris Mason 38691538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3870387125fcSChris Mason /* send down all the barriers */ 3871387125fcSChris Mason head = &info->fs_devices->devices; 38721538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3873e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3874f88ba6a2SHidetoshi Seto continue; 3875cea7c8bfSAnand Jain if (!dev->bdev) 3876387125fcSChris Mason continue; 3877e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3878ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3879387125fcSChris Mason continue; 3880387125fcSChris Mason 38814fc6441aSAnand Jain write_dev_flush(dev); 388258efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3883387125fcSChris Mason } 3884387125fcSChris Mason 3885387125fcSChris Mason /* wait for all the barriers */ 38861538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3887e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3888f88ba6a2SHidetoshi Seto continue; 3889387125fcSChris Mason if (!dev->bdev) { 38905af3e8ccSStefan Behrens errors_wait++; 3891387125fcSChris Mason continue; 3892387125fcSChris Mason } 3893e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3894ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3895387125fcSChris Mason continue; 3896387125fcSChris Mason 38974fc6441aSAnand Jain ret = wait_dev_flush(dev); 3898401b41e5SAnand Jain if (ret) { 3899401b41e5SAnand Jain dev->last_flush_error = ret; 390066b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 390166b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 39025af3e8ccSStefan Behrens errors_wait++; 3903387125fcSChris Mason } 3904401b41e5SAnand Jain } 3905401b41e5SAnand Jain 3906cea7c8bfSAnand Jain if (errors_wait) { 3907401b41e5SAnand Jain /* 3908401b41e5SAnand Jain * At some point we need the status of all disks 3909401b41e5SAnand Jain * to arrive at the volume status. So error checking 3910401b41e5SAnand Jain * is being pushed to a separate loop. 3911401b41e5SAnand Jain */ 3912d10b82feSQu Wenruo return check_barrier_error(info); 3913401b41e5SAnand Jain } 3914387125fcSChris Mason return 0; 3915387125fcSChris Mason } 3916387125fcSChris Mason 3917943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3918943c6e99SZhao Lei { 39198789f4feSZhao Lei int raid_type; 39208789f4feSZhao Lei int min_tolerated = INT_MAX; 3921943c6e99SZhao Lei 39228789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 39238789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 39248c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 39258789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 39268789f4feSZhao Lei tolerated_failures); 3927943c6e99SZhao Lei 39288789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 39298789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 39308789f4feSZhao Lei continue; 393141a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 39328789f4feSZhao Lei continue; 39338c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 39348789f4feSZhao Lei btrfs_raid_array[raid_type]. 39358789f4feSZhao Lei tolerated_failures); 39368789f4feSZhao Lei } 3937943c6e99SZhao Lei 39388789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3939ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 39408789f4feSZhao Lei min_tolerated = 0; 39418789f4feSZhao Lei } 39428789f4feSZhao Lei 39438789f4feSZhao Lei return min_tolerated; 3944943c6e99SZhao Lei } 3945943c6e99SZhao Lei 3946eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3947f2984462SChris Mason { 3948e5e9a520SChris Mason struct list_head *head; 3949f2984462SChris Mason struct btrfs_device *dev; 3950a061fc8dSChris Mason struct btrfs_super_block *sb; 3951f2984462SChris Mason struct btrfs_dev_item *dev_item; 3952f2984462SChris Mason int ret; 3953f2984462SChris Mason int do_barriers; 3954a236aed1SChris Mason int max_errors; 3955a236aed1SChris Mason int total_errors = 0; 3956a061fc8dSChris Mason u64 flags; 3957f2984462SChris Mason 39580b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3959fed3b381SLiu Bo 3960fed3b381SLiu Bo /* 3961fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3962fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3963fed3b381SLiu Bo * been consistent on disk. 3964fed3b381SLiu Bo */ 3965fed3b381SLiu Bo if (max_mirrors == 0) 39660b246afaSJeff Mahoney backup_super_roots(fs_info); 3967f2984462SChris Mason 39680b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3969a061fc8dSChris Mason dev_item = &sb->dev_item; 3970e5e9a520SChris Mason 39710b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 39720b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 39730b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3974387125fcSChris Mason 39755af3e8ccSStefan Behrens if (do_barriers) { 39760b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 39775af3e8ccSStefan Behrens if (ret) { 39785af3e8ccSStefan Behrens mutex_unlock( 39790b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 39800b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 39815af3e8ccSStefan Behrens "errors while submitting device barriers."); 39825af3e8ccSStefan Behrens return ret; 39835af3e8ccSStefan Behrens } 39845af3e8ccSStefan Behrens } 3985387125fcSChris Mason 39861538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3987dfe25020SChris Mason if (!dev->bdev) { 3988dfe25020SChris Mason total_errors++; 3989dfe25020SChris Mason continue; 3990dfe25020SChris Mason } 3991e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3992ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3993dfe25020SChris Mason continue; 3994dfe25020SChris Mason 39952b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3996a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3997a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 39987df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3999935e5cc9SMiao Xie dev->commit_total_bytes); 4000ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4001ce7213c7SMiao Xie dev->commit_bytes_used); 4002a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4003a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4004a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4005a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 40067239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 40077239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4008a512bbf8SYan Zheng 4009a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4010a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4011f2984462SChris Mason 401275cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 401375cb857dSQu Wenruo if (ret < 0) { 401475cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 401575cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 401675cb857dSQu Wenruo "unexpected superblock corruption detected"); 401775cb857dSQu Wenruo return -EUCLEAN; 401875cb857dSQu Wenruo } 401975cb857dSQu Wenruo 4020abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4021a236aed1SChris Mason if (ret) 4022a236aed1SChris Mason total_errors++; 4023f2984462SChris Mason } 4024a236aed1SChris Mason if (total_errors > max_errors) { 40250b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4026d397712bSChris Mason total_errors); 40270b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 402879787eaaSJeff Mahoney 40299d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 40300b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 40310b246afaSJeff Mahoney "%d errors while writing supers", 40320b246afaSJeff Mahoney total_errors); 40339d565ba4SStefan Behrens return -EIO; 4034a236aed1SChris Mason } 4035f2984462SChris Mason 4036a512bbf8SYan Zheng total_errors = 0; 40371538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4038dfe25020SChris Mason if (!dev->bdev) 4039dfe25020SChris Mason continue; 4040e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4041ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4042dfe25020SChris Mason continue; 4043dfe25020SChris Mason 4044abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4045a512bbf8SYan Zheng if (ret) 40461259ab75SChris Mason total_errors++; 4047f2984462SChris Mason } 40480b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4049a236aed1SChris Mason if (total_errors > max_errors) { 40500b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 40510b246afaSJeff Mahoney "%d errors while writing supers", 40520b246afaSJeff Mahoney total_errors); 405379787eaaSJeff Mahoney return -EIO; 4054a236aed1SChris Mason } 4055f2984462SChris Mason return 0; 4056f2984462SChris Mason } 4057f2984462SChris Mason 4058cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4059cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4060cb517eabSMiao Xie struct btrfs_root *root) 40612619ba1fSChris Mason { 40624785e24fSJosef Bacik bool drop_ref = false; 40634785e24fSJosef Bacik 40644df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 40652619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 40662619ba1fSChris Mason (unsigned long)root->root_key.objectid); 4067af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 40684785e24fSJosef Bacik drop_ref = true; 40694df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 407076dda93cSYan, Zheng 40711c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 4072ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 40731c1ea4f7SLiu Bo if (root->reloc_root) { 407400246528SJosef Bacik btrfs_put_root(root->reloc_root); 40751c1ea4f7SLiu Bo root->reloc_root = NULL; 40761c1ea4f7SLiu Bo } 40771c1ea4f7SLiu Bo } 40783321719eSLiu Bo 40794785e24fSJosef Bacik if (drop_ref) 408000246528SJosef Bacik btrfs_put_root(root); 40812619ba1fSChris Mason } 40822619ba1fSChris Mason 4083c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4084c146afadSYan Zheng { 4085c146afadSYan Zheng u64 root_objectid = 0; 4086c146afadSYan Zheng struct btrfs_root *gang[8]; 408765d33fd7SQu Wenruo int i = 0; 408865d33fd7SQu Wenruo int err = 0; 408965d33fd7SQu Wenruo unsigned int ret = 0; 4090c146afadSYan Zheng 4091c146afadSYan Zheng while (1) { 4092efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4093c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4094c146afadSYan Zheng (void **)gang, root_objectid, 4095c146afadSYan Zheng ARRAY_SIZE(gang)); 409665d33fd7SQu Wenruo if (!ret) { 4097efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4098c146afadSYan Zheng break; 409965d33fd7SQu Wenruo } 41005d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 410166b4ffd1SJosef Bacik 410265d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 410365d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 410465d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 410565d33fd7SQu Wenruo gang[i] = NULL; 410665d33fd7SQu Wenruo continue; 410765d33fd7SQu Wenruo } 410865d33fd7SQu Wenruo /* grab all the search result for later use */ 410900246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 411065d33fd7SQu Wenruo } 4111efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 411265d33fd7SQu Wenruo 411365d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 411465d33fd7SQu Wenruo if (!gang[i]) 411565d33fd7SQu Wenruo continue; 4116c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 411766b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 411866b4ffd1SJosef Bacik if (err) 411965d33fd7SQu Wenruo break; 412000246528SJosef Bacik btrfs_put_root(gang[i]); 4121c146afadSYan Zheng } 4122c146afadSYan Zheng root_objectid++; 4123c146afadSYan Zheng } 412465d33fd7SQu Wenruo 412565d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 412665d33fd7SQu Wenruo for (; i < ret; i++) { 412765d33fd7SQu Wenruo if (gang[i]) 412800246528SJosef Bacik btrfs_put_root(gang[i]); 412965d33fd7SQu Wenruo } 413065d33fd7SQu Wenruo return err; 4131c146afadSYan Zheng } 4132c146afadSYan Zheng 41336bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4134c146afadSYan Zheng { 41356bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4136c146afadSYan Zheng struct btrfs_trans_handle *trans; 4137c146afadSYan Zheng 41380b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 41392ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 41400b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 41410b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4142c71bf099SYan, Zheng 4143c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 41440b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 41450b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4146c71bf099SYan, Zheng 41477a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 41483612b495STsutomu Itoh if (IS_ERR(trans)) 41493612b495STsutomu Itoh return PTR_ERR(trans); 41503a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4151c146afadSYan Zheng } 4152c146afadSYan Zheng 4153b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4154eb60ceacSChris Mason { 4155c146afadSYan Zheng int ret; 4156e089f05cSChris Mason 4157afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4158d6fd0ae2SOmar Sandoval /* 4159d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4160d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4161d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4162d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4163d6fd0ae2SOmar Sandoval */ 4164d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4165a2135011SChris Mason 41667343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4167d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 41687343dd61SJustin Maggard 4169803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4170803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4171803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4172803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4173803b2f54SStefan Behrens 4174837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4175aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4176837d5b6eSIlya Dryomov 41778dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 41788dabb742SStefan Behrens 4179aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 41804cb5300bSChris Mason 41814cb5300bSChris Mason /* wait for any defraggers to finish */ 41824cb5300bSChris Mason wait_event(fs_info->transaction_wait, 41834cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 41844cb5300bSChris Mason 41854cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 418626176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 41874cb5300bSChris Mason 418821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 418957056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4190576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 419121c7e756SMiao Xie 4192b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4193b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4194b0643e59SDennis Zhou 4195bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4196e44163e1SJeff Mahoney /* 4197d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4198d6fd0ae2SOmar Sandoval * unused block groups. 4199e44163e1SJeff Mahoney */ 42000b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4201e44163e1SJeff Mahoney 4202f0cc2cd7SFilipe Manana /* 4203f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4204f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4205f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4206f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4207f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4208f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4209f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4210f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4211f0cc2cd7SFilipe Manana * This is a very rare case. 4212f0cc2cd7SFilipe Manana */ 4213f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4214f0cc2cd7SFilipe Manana 42156bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4216d397712bSChris Mason if (ret) 421704892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4218c146afadSYan Zheng } 4219ed2ff2cbSChris Mason 4220af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4221af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 42222ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4223acce952bSliubo 4224e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4225e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 42268929ecfaSYan, Zheng 4227e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4228afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4229f25784b3SYan Zheng 42305958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 42315958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 42325958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 42335958253cSQu Wenruo } 42345958253cSQu Wenruo 423504892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4236fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4237bcef60f2SArne Jansen 4238963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 423904892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4240963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4241b0c68f8bSChris Mason } 424231153d81SYan Zheng 42435deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 42444297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 42455deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 42464297ff84SJosef Bacik 42476618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4248b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 42495ac1d209SJeff Mahoney 42501a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 42511a4319ccSLiu Bo 4252de348ee0SWang Shilong /* 4253de348ee0SWang Shilong * we must make sure there is not any read request to 4254de348ee0SWang Shilong * submit after we stopping all workers. 4255de348ee0SWang Shilong */ 4256de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 425796192499SJosef Bacik btrfs_stop_all_workers(fs_info); 425896192499SJosef Bacik 42590a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 42600a31daa4SFilipe Manana ASSERT(list_empty(&fs_info->trans_list)); 42610a31daa4SFilipe Manana 4262afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 42634273eaffSAnand Jain free_root_pointers(fs_info, true); 42648c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 42659ad6b7bcSChris Mason 42664e19443dSJosef Bacik /* 42674e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 42684e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 42694e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 42704e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 42714e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 42724e19443dSJosef Bacik */ 42734e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 42744e19443dSJosef Bacik 427513e6c37bSJosef Bacik iput(fs_info->btree_inode); 4276d6bfde87SChris Mason 427721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 42780b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 42792ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 428021adbd5cSStefan Behrens #endif 428121adbd5cSStefan Behrens 42820b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 428368c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4284eb60ceacSChris Mason } 4285eb60ceacSChris Mason 4286b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4287b9fab919SChris Mason int atomic) 4288ccd467d6SChris Mason { 42891259ab75SChris Mason int ret; 4290727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 42911259ab75SChris Mason 42920b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 42931259ab75SChris Mason if (!ret) 42941259ab75SChris Mason return ret; 42951259ab75SChris Mason 42961259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4297b9fab919SChris Mason parent_transid, atomic); 4298b9fab919SChris Mason if (ret == -EAGAIN) 4299b9fab919SChris Mason return ret; 43001259ab75SChris Mason return !ret; 43015f39d397SChris Mason } 43026702ed49SChris Mason 43035f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 43045f39d397SChris Mason { 43052f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 43065f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4307b9473439SChris Mason int was_dirty; 4308b4ce94deSChris Mason 430906ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 431006ea65a3SJosef Bacik /* 431106ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 431252042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 431306ea65a3SJosef Bacik * outside of the sanity tests. 431406ea65a3SJosef Bacik */ 4315b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 431606ea65a3SJosef Bacik return; 431706ea65a3SJosef Bacik #endif 4318b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 43190b246afaSJeff Mahoney if (transid != fs_info->generation) 43205d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 43210b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 43220b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4323e2d84521SMiao Xie if (!was_dirty) 4324104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4325e2d84521SMiao Xie buf->len, 43260b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 43271f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 432869fc6cbbSQu Wenruo /* 432969fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 433069fc6cbbSQu Wenruo * but item data not updated. 433169fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 433269fc6cbbSQu Wenruo */ 433369fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4334cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4335a4f78750SDavid Sterba btrfs_print_leaf(buf); 43361f21ef0aSFilipe Manana ASSERT(0); 43371f21ef0aSFilipe Manana } 43381f21ef0aSFilipe Manana #endif 4339eb60ceacSChris Mason } 4340eb60ceacSChris Mason 43412ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4342b53d3f5dSLiu Bo int flush_delayed) 434335b7e476SChris Mason { 4344188de649SChris Mason /* 4345188de649SChris Mason * looks as though older kernels can get into trouble with 4346188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4347188de649SChris Mason */ 4348e2d84521SMiao Xie int ret; 4349d6bfde87SChris Mason 43506933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4351d6bfde87SChris Mason return; 4352d6bfde87SChris Mason 4353b53d3f5dSLiu Bo if (flush_delayed) 43542ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 435516cdcec7SMiao Xie 4356d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4357d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4358d814a491SEthan Lien fs_info->dirty_metadata_batch); 4359e2d84521SMiao Xie if (ret > 0) { 43600b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 436116cdcec7SMiao Xie } 436216cdcec7SMiao Xie } 436316cdcec7SMiao Xie 43642ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 436516cdcec7SMiao Xie { 43662ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 436735b7e476SChris Mason } 4368b53d3f5dSLiu Bo 43692ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4370b53d3f5dSLiu Bo { 43712ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4372d6bfde87SChris Mason } 43736b80053dSChris Mason 4374581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4375581c1760SQu Wenruo struct btrfs_key *first_key) 43766b80053dSChris Mason { 43775ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4378581c1760SQu Wenruo level, first_key); 43796b80053dSChris Mason } 43800da5468fSChris Mason 43812ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4382acce952bSliubo { 4383fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4384fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4385fe816d0fSNikolay Borisov 43860b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 43872ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 43880b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4389acce952bSliubo 43900b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 43910b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4392acce952bSliubo } 4393acce952bSliubo 4394ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4395ef67963dSJosef Bacik { 4396ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4397ef67963dSJosef Bacik u64 root_objectid = 0; 4398ef67963dSJosef Bacik int ret; 4399ef67963dSJosef Bacik 4400ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4401ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4402ef67963dSJosef Bacik (void **)gang, root_objectid, 4403ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4404ef67963dSJosef Bacik int i; 4405ef67963dSJosef Bacik 4406ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4407ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4408ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4409ef67963dSJosef Bacik 4410ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4411ef67963dSJosef Bacik if (!gang[i]) 4412ef67963dSJosef Bacik continue; 4413ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4414ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4415ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4416ef67963dSJosef Bacik } 4417ef67963dSJosef Bacik root_objectid++; 4418ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4419ef67963dSJosef Bacik } 4420ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4421ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4422ef67963dSJosef Bacik } 4423ef67963dSJosef Bacik 4424143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4425acce952bSliubo { 4426acce952bSliubo struct btrfs_ordered_extent *ordered; 4427acce952bSliubo 4428199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4429779880efSJosef Bacik /* 4430779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4431779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4432779880efSJosef Bacik */ 4433199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4434779880efSJosef Bacik root_extent_list) 4435779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4436199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4437199c2a9cSMiao Xie } 4438199c2a9cSMiao Xie 4439199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4440199c2a9cSMiao Xie { 4441199c2a9cSMiao Xie struct btrfs_root *root; 4442199c2a9cSMiao Xie struct list_head splice; 4443199c2a9cSMiao Xie 4444199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4445199c2a9cSMiao Xie 4446199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4447199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4448199c2a9cSMiao Xie while (!list_empty(&splice)) { 4449199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4450199c2a9cSMiao Xie ordered_root); 44511de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 44521de2cfdeSJosef Bacik &fs_info->ordered_roots); 4453199c2a9cSMiao Xie 44542a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4455199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4456199c2a9cSMiao Xie 44572a85d9caSLiu Bo cond_resched(); 44582a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4459199c2a9cSMiao Xie } 4460199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 446174d5d229SJosef Bacik 446274d5d229SJosef Bacik /* 446374d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 446474d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 446574d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 446674d5d229SJosef Bacik * actually get run and error out properly. 446774d5d229SJosef Bacik */ 446874d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4469acce952bSliubo } 4470acce952bSliubo 447135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 44722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4473acce952bSliubo { 4474acce952bSliubo struct rb_node *node; 4475acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4476acce952bSliubo struct btrfs_delayed_ref_node *ref; 4477acce952bSliubo int ret = 0; 4478acce952bSliubo 4479acce952bSliubo delayed_refs = &trans->delayed_refs; 4480acce952bSliubo 4481acce952bSliubo spin_lock(&delayed_refs->lock); 4482d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4483cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4484b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4485acce952bSliubo return ret; 4486acce952bSliubo } 4487acce952bSliubo 44885c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4489d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 44900e0adbcfSJosef Bacik struct rb_node *n; 4491e78417d1SJosef Bacik bool pin_bytes = false; 4492acce952bSliubo 4493d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4494d7df2c79SJosef Bacik href_node); 44953069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4496b939d1abSJosef Bacik continue; 44973069bd26SJosef Bacik 4498d7df2c79SJosef Bacik spin_lock(&head->lock); 4499e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 45000e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 45010e0adbcfSJosef Bacik ref_node); 4502d7df2c79SJosef Bacik ref->in_tree = 0; 4503e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 45040e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 45051d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 45061d57ee94SWang Xiaoguang list_del(&ref->add_list); 4507d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4508d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4509d7df2c79SJosef Bacik } 451054067ae9SJosef Bacik if (head->must_insert_reserved) 4511e78417d1SJosef Bacik pin_bytes = true; 451278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4513fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4514d7df2c79SJosef Bacik spin_unlock(&head->lock); 4515acce952bSliubo spin_unlock(&delayed_refs->lock); 4516e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4517acce952bSliubo 4518f603bb94SNikolay Borisov if (pin_bytes) { 4519f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4520f603bb94SNikolay Borisov 4521f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4522f603bb94SNikolay Borisov BUG_ON(!cache); 4523f603bb94SNikolay Borisov 4524f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4525f603bb94SNikolay Borisov spin_lock(&cache->lock); 4526f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4527f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4528f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4529f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4530f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4531f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4532f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4533f603bb94SNikolay Borisov percpu_counter_add_batch( 4534f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4535f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4536f603bb94SNikolay Borisov 4537f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4538f603bb94SNikolay Borisov 4539f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4540f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4541f603bb94SNikolay Borisov } 454231890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4543d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4544acce952bSliubo cond_resched(); 4545acce952bSliubo spin_lock(&delayed_refs->lock); 4546acce952bSliubo } 454781f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4548acce952bSliubo 4549acce952bSliubo spin_unlock(&delayed_refs->lock); 4550acce952bSliubo 4551acce952bSliubo return ret; 4552acce952bSliubo } 4553acce952bSliubo 4554143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4555acce952bSliubo { 4556acce952bSliubo struct btrfs_inode *btrfs_inode; 4557acce952bSliubo struct list_head splice; 4558acce952bSliubo 4559acce952bSliubo INIT_LIST_HEAD(&splice); 4560acce952bSliubo 4561eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4562eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4563acce952bSliubo 4564acce952bSliubo while (!list_empty(&splice)) { 4565fe816d0fSNikolay Borisov struct inode *inode = NULL; 4566eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4567acce952bSliubo delalloc_inodes); 4568fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4569eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4570acce952bSliubo 4571fe816d0fSNikolay Borisov /* 4572fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4573fe816d0fSNikolay Borisov * meanwhile. 4574fe816d0fSNikolay Borisov */ 4575fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4576fe816d0fSNikolay Borisov if (inode) { 4577fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4578fe816d0fSNikolay Borisov iput(inode); 4579fe816d0fSNikolay Borisov } 4580eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4581acce952bSliubo } 4582eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4583eb73c1b7SMiao Xie } 4584eb73c1b7SMiao Xie 4585eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4586eb73c1b7SMiao Xie { 4587eb73c1b7SMiao Xie struct btrfs_root *root; 4588eb73c1b7SMiao Xie struct list_head splice; 4589eb73c1b7SMiao Xie 4590eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4591eb73c1b7SMiao Xie 4592eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4593eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4594eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4595eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4596eb73c1b7SMiao Xie delalloc_root); 459700246528SJosef Bacik root = btrfs_grab_root(root); 4598eb73c1b7SMiao Xie BUG_ON(!root); 4599eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4600eb73c1b7SMiao Xie 4601eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 460200246528SJosef Bacik btrfs_put_root(root); 4603eb73c1b7SMiao Xie 4604eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4605eb73c1b7SMiao Xie } 4606eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4607acce952bSliubo } 4608acce952bSliubo 46092ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4610acce952bSliubo struct extent_io_tree *dirty_pages, 4611acce952bSliubo int mark) 4612acce952bSliubo { 4613acce952bSliubo int ret; 4614acce952bSliubo struct extent_buffer *eb; 4615acce952bSliubo u64 start = 0; 4616acce952bSliubo u64 end; 4617acce952bSliubo 4618acce952bSliubo while (1) { 4619acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4620e6138876SJosef Bacik mark, NULL); 4621acce952bSliubo if (ret) 4622acce952bSliubo break; 4623acce952bSliubo 462491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4625acce952bSliubo while (start <= end) { 46260b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 46270b246afaSJeff Mahoney start += fs_info->nodesize; 4628fd8b2b61SJosef Bacik if (!eb) 4629acce952bSliubo continue; 4630fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4631acce952bSliubo 4632fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4633fd8b2b61SJosef Bacik &eb->bflags)) 4634fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4635fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4636acce952bSliubo } 4637acce952bSliubo } 4638acce952bSliubo 4639acce952bSliubo return ret; 4640acce952bSliubo } 4641acce952bSliubo 46422ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4643fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4644acce952bSliubo { 4645acce952bSliubo u64 start; 4646acce952bSliubo u64 end; 4647acce952bSliubo int ret; 4648acce952bSliubo 4649acce952bSliubo while (1) { 46500e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 46510e6ec385SFilipe Manana 4652fcd5e742SLu Fengqi /* 4653fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4654fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4655fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4656fcd5e742SLu Fengqi * the same extent range. 4657fcd5e742SLu Fengqi */ 4658fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4659acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 46600e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4661fcd5e742SLu Fengqi if (ret) { 4662fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4663acce952bSliubo break; 4664fcd5e742SLu Fengqi } 4665acce952bSliubo 46660e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 46670e6ec385SFilipe Manana free_extent_state(cached_state); 46682ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4669fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4670acce952bSliubo cond_resched(); 4671acce952bSliubo } 4672acce952bSliubo 4673acce952bSliubo return 0; 4674acce952bSliubo } 4675acce952bSliubo 467632da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4677c79a1751SLiu Bo { 4678c79a1751SLiu Bo struct inode *inode; 4679c79a1751SLiu Bo 4680c79a1751SLiu Bo inode = cache->io_ctl.inode; 4681c79a1751SLiu Bo if (inode) { 4682c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4683c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4684c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4685c79a1751SLiu Bo iput(inode); 4686c79a1751SLiu Bo } 4687bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 4688c79a1751SLiu Bo btrfs_put_block_group(cache); 4689c79a1751SLiu Bo } 4690c79a1751SLiu Bo 4691c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 46922ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4693c79a1751SLiu Bo { 469432da5386SDavid Sterba struct btrfs_block_group *cache; 4695c79a1751SLiu Bo 4696c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4697c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4698c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 469932da5386SDavid Sterba struct btrfs_block_group, 4700c79a1751SLiu Bo dirty_list); 4701c79a1751SLiu Bo 4702c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4703c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4704c79a1751SLiu Bo list_del_init(&cache->io_list); 4705c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4706c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4707c79a1751SLiu Bo } 4708c79a1751SLiu Bo 4709c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4710c79a1751SLiu Bo spin_lock(&cache->lock); 4711c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4712c79a1751SLiu Bo spin_unlock(&cache->lock); 4713c79a1751SLiu Bo 4714c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4715c79a1751SLiu Bo btrfs_put_block_group(cache); 4716ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4717c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4718c79a1751SLiu Bo } 4719c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4720c79a1751SLiu Bo 472145ae2c18SNikolay Borisov /* 472245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 472345ae2c18SNikolay Borisov * to use it without any locking 472445ae2c18SNikolay Borisov */ 4725c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4726c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 472732da5386SDavid Sterba struct btrfs_block_group, 4728c79a1751SLiu Bo io_list); 4729c79a1751SLiu Bo 4730c79a1751SLiu Bo list_del_init(&cache->io_list); 4731c79a1751SLiu Bo spin_lock(&cache->lock); 4732c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4733c79a1751SLiu Bo spin_unlock(&cache->lock); 4734c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4735c79a1751SLiu Bo } 4736c79a1751SLiu Bo } 4737c79a1751SLiu Bo 473849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 47392ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 474049b25e05SJeff Mahoney { 4741bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4742bbbf7243SNikolay Borisov 47432ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4744c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4745c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4746c79a1751SLiu Bo 4747bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4748bbbf7243SNikolay Borisov post_commit_list) { 4749bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4750bbbf7243SNikolay Borisov } 4751bbbf7243SNikolay Borisov 47522ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 475349b25e05SJeff Mahoney 47544a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 47550b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 475649b25e05SJeff Mahoney 47574a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 47580b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 475949b25e05SJeff Mahoney 4760ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 476167cde344SMiao Xie 47622ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 476349b25e05SJeff Mahoney EXTENT_DIRTY); 4764fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 476549b25e05SJeff Mahoney 47664a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 47674a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 476849b25e05SJeff Mahoney } 476949b25e05SJeff Mahoney 47702ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4771acce952bSliubo { 4772acce952bSliubo struct btrfs_transaction *t; 4773acce952bSliubo 47740b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4775acce952bSliubo 47760b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 47770b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 47780b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4779724e2315SJosef Bacik struct btrfs_transaction, list); 4780724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 47819b64f57dSElena Reshetova refcount_inc(&t->use_count); 47820b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 47832ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4784724e2315SJosef Bacik btrfs_put_transaction(t); 47850b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4786724e2315SJosef Bacik continue; 4787724e2315SJosef Bacik } 47880b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4789724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 47900b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4791724e2315SJosef Bacik /* 4792724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4793724e2315SJosef Bacik * handle open currently for this transaction. 4794724e2315SJosef Bacik */ 4795724e2315SJosef Bacik wait_event(t->writer_wait, 4796724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4797724e2315SJosef Bacik } else { 47980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4799724e2315SJosef Bacik } 48002ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4801724e2315SJosef Bacik 48020b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 48030b246afaSJeff Mahoney if (t == fs_info->running_transaction) 48040b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4805724e2315SJosef Bacik list_del_init(&t->list); 48060b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4807a4abeea4SJosef Bacik 4808724e2315SJosef Bacik btrfs_put_transaction(t); 48092ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 48100b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4811724e2315SJosef Bacik } 48120b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 48130b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4814ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4815ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 48160b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 4817ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 48180b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4819acce952bSliubo 4820acce952bSliubo return 0; 4821acce952bSliubo } 4822ec7d6dfdSNikolay Borisov 4823453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 4824ec7d6dfdSNikolay Borisov { 4825ec7d6dfdSNikolay Borisov struct btrfs_path *path; 4826ec7d6dfdSNikolay Borisov int ret; 4827ec7d6dfdSNikolay Borisov struct extent_buffer *l; 4828ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 4829ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 4830ec7d6dfdSNikolay Borisov int slot; 4831ec7d6dfdSNikolay Borisov 4832ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 4833ec7d6dfdSNikolay Borisov if (!path) 4834ec7d6dfdSNikolay Borisov return -ENOMEM; 4835ec7d6dfdSNikolay Borisov 4836ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 4837ec7d6dfdSNikolay Borisov search_key.type = -1; 4838ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 4839ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 4840ec7d6dfdSNikolay Borisov if (ret < 0) 4841ec7d6dfdSNikolay Borisov goto error; 4842ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 4843ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 4844ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 4845ec7d6dfdSNikolay Borisov l = path->nodes[0]; 4846ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 484723125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 484823125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 4849ec7d6dfdSNikolay Borisov } else { 485023125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 4851ec7d6dfdSNikolay Borisov } 4852ec7d6dfdSNikolay Borisov ret = 0; 4853ec7d6dfdSNikolay Borisov error: 4854ec7d6dfdSNikolay Borisov btrfs_free_path(path); 4855ec7d6dfdSNikolay Borisov return ret; 4856ec7d6dfdSNikolay Borisov } 4857ec7d6dfdSNikolay Borisov 4858543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 4859ec7d6dfdSNikolay Borisov { 4860ec7d6dfdSNikolay Borisov int ret; 4861ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 4862ec7d6dfdSNikolay Borisov 48636b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 4864ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 4865ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 4866ec7d6dfdSNikolay Borisov root->root_key.objectid); 4867ec7d6dfdSNikolay Borisov ret = -ENOSPC; 4868ec7d6dfdSNikolay Borisov goto out; 4869ec7d6dfdSNikolay Borisov } 4870ec7d6dfdSNikolay Borisov 487123125104SNikolay Borisov *objectid = root->free_objectid++; 4872ec7d6dfdSNikolay Borisov ret = 0; 4873ec7d6dfdSNikolay Borisov out: 4874ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 4875ec7d6dfdSNikolay Borisov return ret; 4876ec7d6dfdSNikolay Borisov } 4877