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); 462d3575156SNaohiro Aota 463d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 464d3575156SNaohiro Aota WARN_ON(found_start != 0); 465d3575156SNaohiro Aota return 0; 466d3575156SNaohiro Aota } 467d3575156SNaohiro Aota 4680f805531SAlex Lyakas /* 4690f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4700f805531SAlex Lyakas * It is useful to know what went wrong. 4710f805531SAlex Lyakas */ 4720f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4730f805531SAlex Lyakas return -EUCLEAN; 4740f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4750f805531SAlex Lyakas return -EUCLEAN; 4760f805531SAlex Lyakas 477de37aa51SNikolay Borisov ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 4789a8658e3SDavid Sterba offsetof(struct btrfs_header, fsid), 4799a8658e3SDavid Sterba BTRFS_FSID_SIZE) == 0); 4800f805531SAlex Lyakas 481c67b3892SDavid Sterba csum_tree_block(eb, result); 4822996e1f8SJohannes Thumshirn 4838d47a0d8SQu Wenruo if (btrfs_header_level(eb)) 4848d47a0d8SQu Wenruo ret = btrfs_check_node(eb); 4858d47a0d8SQu Wenruo else 4868d47a0d8SQu Wenruo ret = btrfs_check_leaf_full(eb); 4878d47a0d8SQu Wenruo 4888d47a0d8SQu Wenruo if (ret < 0) { 489c06631b0SQu Wenruo btrfs_print_tree(eb, 0); 4908d47a0d8SQu Wenruo btrfs_err(fs_info, 4918d47a0d8SQu Wenruo "block=%llu write time tree block corruption detected", 4928d47a0d8SQu Wenruo eb->start); 493c06631b0SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4948d47a0d8SQu Wenruo return ret; 4958d47a0d8SQu Wenruo } 496713cebfbSDavid Sterba write_extent_buffer(eb, result, 0, fs_info->csum_size); 4978d47a0d8SQu Wenruo 4982996e1f8SJohannes Thumshirn return 0; 49919c00ddcSChris Mason } 50019c00ddcSChris Mason 501b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5022b82032cSYan Zheng { 503b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 504944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 50544880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 506944d3f9fSNikolay Borisov u8 *metadata_uuid; 5072b82032cSYan Zheng 5089a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5099a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5107239ff4bSNikolay Borisov /* 511944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 512944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 513944d3f9fSNikolay Borisov * ->fsid in this case is fine 5147239ff4bSNikolay Borisov */ 515944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5167239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5177239ff4bSNikolay Borisov else 5187239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5197239ff4bSNikolay Borisov 520944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 521944d3f9fSNikolay Borisov return 0; 522944d3f9fSNikolay Borisov 523944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 524944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 525944d3f9fSNikolay Borisov return 0; 526944d3f9fSNikolay Borisov 527944d3f9fSNikolay Borisov return 1; 5282b82032cSYan Zheng } 5292b82032cSYan Zheng 53077bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 53177bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 532ce9adaa5SChris Mason { 53377bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 534ce9adaa5SChris Mason u64 found_start; 53577bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 53677bf40a2SQu Wenruo u8 found_level; 5372996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 53877bf40a2SQu Wenruo int ret = 0; 539ea466794SJosef Bacik 540ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 541727011e0SChris Mason if (found_start != eb->start) { 542893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 543893bf4b1SSu Yue eb->start, found_start); 544f188591eSChris Mason ret = -EIO; 54577bf40a2SQu Wenruo goto out; 546ce9adaa5SChris Mason } 547b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 54802873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 54994647322SDavid Sterba eb->start); 5501259ab75SChris Mason ret = -EIO; 55177bf40a2SQu Wenruo goto out; 5521259ab75SChris Mason } 553ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5541c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 555893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 556893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 5571c24c3ceSJosef Bacik ret = -EIO; 55877bf40a2SQu Wenruo goto out; 5591c24c3ceSJosef Bacik } 560ce9adaa5SChris Mason 561c67b3892SDavid Sterba csum_tree_block(eb, result); 562a826d6dcSJosef Bacik 5632996e1f8SJohannes Thumshirn if (memcmp_extent_buffer(eb, result, 0, csum_size)) { 56435be8851SJohannes Thumshirn u8 val[BTRFS_CSUM_SIZE] = { 0 }; 5652996e1f8SJohannes Thumshirn 5662996e1f8SJohannes Thumshirn read_extent_buffer(eb, &val, 0, csum_size); 5672996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 56835be8851SJohannes Thumshirn "%s checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5692996e1f8SJohannes Thumshirn fs_info->sb->s_id, eb->start, 57035be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, val), 57135be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 57235be8851SJohannes Thumshirn btrfs_header_level(eb)); 5732996e1f8SJohannes Thumshirn ret = -EUCLEAN; 57477bf40a2SQu Wenruo goto out; 5752996e1f8SJohannes Thumshirn } 5762996e1f8SJohannes Thumshirn 577a826d6dcSJosef Bacik /* 578a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 579a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 580a826d6dcSJosef Bacik * return -EIO. 581a826d6dcSJosef Bacik */ 5821c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 583a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 584a826d6dcSJosef Bacik ret = -EIO; 585a826d6dcSJosef Bacik } 586ce9adaa5SChris Mason 587813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 588053ab70fSLiu Bo ret = -EIO; 589053ab70fSLiu Bo 5900b32f4bbSJosef Bacik if (!ret) 5910b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 59275391f0dSQu Wenruo else 59375391f0dSQu Wenruo btrfs_err(fs_info, 59475391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 59575391f0dSQu Wenruo eb->start); 59677bf40a2SQu Wenruo out: 59777bf40a2SQu Wenruo return ret; 59877bf40a2SQu Wenruo } 59977bf40a2SQu Wenruo 600371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 601371cdc07SQu Wenruo int mirror) 602371cdc07SQu Wenruo { 603371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 604371cdc07SQu Wenruo struct extent_buffer *eb; 605371cdc07SQu Wenruo bool reads_done; 606371cdc07SQu Wenruo int ret = 0; 607371cdc07SQu Wenruo 608371cdc07SQu Wenruo /* 609371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 610371cdc07SQu Wenruo * only get one eb for each endio hook. 611371cdc07SQu Wenruo */ 612371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 613371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 614371cdc07SQu Wenruo 615371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 616371cdc07SQu Wenruo /* 617371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 618371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 619371cdc07SQu Wenruo */ 620371cdc07SQu Wenruo ASSERT(eb); 621371cdc07SQu Wenruo 622371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 623371cdc07SQu Wenruo /* Subpage read must finish in page read */ 624371cdc07SQu Wenruo ASSERT(reads_done); 625371cdc07SQu Wenruo 626371cdc07SQu Wenruo eb->read_mirror = mirror; 627371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 628371cdc07SQu Wenruo ret = -EIO; 629371cdc07SQu Wenruo goto err; 630371cdc07SQu Wenruo } 631371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 632371cdc07SQu Wenruo if (ret < 0) 633371cdc07SQu Wenruo goto err; 634371cdc07SQu Wenruo 635371cdc07SQu Wenruo if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 636371cdc07SQu Wenruo btree_readahead_hook(eb, ret); 637371cdc07SQu Wenruo 638371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 639371cdc07SQu Wenruo 640371cdc07SQu Wenruo free_extent_buffer(eb); 641371cdc07SQu Wenruo return ret; 642371cdc07SQu Wenruo err: 643371cdc07SQu Wenruo /* 644371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 645371cdc07SQu Wenruo * make sure it has something to decrement. 646371cdc07SQu Wenruo */ 647371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 648371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 649371cdc07SQu Wenruo free_extent_buffer(eb); 650371cdc07SQu Wenruo return ret; 651371cdc07SQu Wenruo } 652371cdc07SQu Wenruo 6538e1dc982SQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, 65477bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 65577bf40a2SQu Wenruo int mirror) 65677bf40a2SQu Wenruo { 65777bf40a2SQu Wenruo struct extent_buffer *eb; 65877bf40a2SQu Wenruo int ret = 0; 65977bf40a2SQu Wenruo int reads_done; 66077bf40a2SQu Wenruo 66177bf40a2SQu Wenruo ASSERT(page->private); 662371cdc07SQu Wenruo 663371cdc07SQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE) 664371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 665371cdc07SQu Wenruo 66677bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 66777bf40a2SQu Wenruo 66877bf40a2SQu Wenruo /* 66977bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 67077bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 67177bf40a2SQu Wenruo */ 67277bf40a2SQu Wenruo atomic_inc(&eb->refs); 67377bf40a2SQu Wenruo 67477bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 67577bf40a2SQu Wenruo if (!reads_done) 67677bf40a2SQu Wenruo goto err; 67777bf40a2SQu Wenruo 67877bf40a2SQu Wenruo eb->read_mirror = mirror; 67977bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 68077bf40a2SQu Wenruo ret = -EIO; 68177bf40a2SQu Wenruo goto err; 68277bf40a2SQu Wenruo } 68377bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 684ce9adaa5SChris Mason err: 68579fb65a1SJosef Bacik if (reads_done && 68679fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 687d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6884bb31e92SArne Jansen 68953b381b3SDavid Woodhouse if (ret) { 69053b381b3SDavid Woodhouse /* 69153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 69253b381b3SDavid Woodhouse * again, we have to make sure it has something 69353b381b3SDavid Woodhouse * to decrement 69453b381b3SDavid Woodhouse */ 69553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6960b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 69753b381b3SDavid Woodhouse } 6980b32f4bbSJosef Bacik free_extent_buffer(eb); 69977bf40a2SQu Wenruo 700f188591eSChris Mason return ret; 701ce9adaa5SChris Mason } 702ce9adaa5SChris Mason 7034246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 704ce9adaa5SChris Mason { 70597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 706ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7079e0af237SLiu Bo struct btrfs_workqueue *wq; 708ce9adaa5SChris Mason 709ce9adaa5SChris Mason fs_info = end_io_wq->info; 7104e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 711d20f7043SChris Mason 712cfe94440SNaohiro Aota if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 713a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7149e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 715a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7169e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 717a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7189e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 719a0cac0ecSOmar Sandoval else 7209e0af237SLiu Bo wq = fs_info->endio_write_workers; 7219e0af237SLiu Bo } else { 7225c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7239e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 724a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7259e0af237SLiu Bo wq = fs_info->endio_meta_workers; 726a0cac0ecSOmar Sandoval else 7279e0af237SLiu Bo wq = fs_info->endio_workers; 7289e0af237SLiu Bo } 7299e0af237SLiu Bo 730a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7319e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 732ce9adaa5SChris Mason } 733ce9adaa5SChris Mason 7344e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 735bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7360b86a832SChris Mason { 73797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7388b110e39SMiao Xie 73997eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 740ce9adaa5SChris Mason if (!end_io_wq) 7414e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 742ce9adaa5SChris Mason 743ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 744ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 74522c59948SChris Mason end_io_wq->info = info; 7464e4cbee9SChristoph Hellwig end_io_wq->status = 0; 747ce9adaa5SChris Mason end_io_wq->bio = bio; 74822c59948SChris Mason end_io_wq->metadata = metadata; 749ce9adaa5SChris Mason 750ce9adaa5SChris Mason bio->bi_private = end_io_wq; 751ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 75222c59948SChris Mason return 0; 75322c59948SChris Mason } 75422c59948SChris Mason 7554a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7564a69a410SChris Mason { 7574a69a410SChris Mason struct async_submit_bio *async; 7584e4cbee9SChristoph Hellwig blk_status_t ret; 7594a69a410SChris Mason 7604a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7611941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 7621941b64bSQu Wenruo async->dio_file_offset); 76379787eaaSJeff Mahoney if (ret) 7644e4cbee9SChristoph Hellwig async->status = ret; 7654a69a410SChris Mason } 7664a69a410SChris Mason 76706ea01b1SDavid Sterba /* 76806ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 76906ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 77006ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 77106ea01b1SDavid Sterba * 77206ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 77306ea01b1SDavid Sterba * inserted into the tree. 77406ea01b1SDavid Sterba */ 7754a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7768b712842SChris Mason { 7778b712842SChris Mason struct async_submit_bio *async; 77806ea01b1SDavid Sterba struct inode *inode; 77906ea01b1SDavid Sterba blk_status_t ret; 7808b712842SChris Mason 7818b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7828896a08dSQu Wenruo inode = async->inode; 7834854ddd0SChris Mason 784bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7854e4cbee9SChristoph Hellwig if (async->status) { 7864e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7874246a0b6SChristoph Hellwig bio_endio(async->bio); 78879787eaaSJeff Mahoney return; 78979787eaaSJeff Mahoney } 79079787eaaSJeff Mahoney 791ec39f769SChris Mason /* 792ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 793ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 794ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 795ec39f769SChris Mason */ 796ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 79708635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 79806ea01b1SDavid Sterba if (ret) { 79906ea01b1SDavid Sterba async->bio->bi_status = ret; 80006ea01b1SDavid Sterba bio_endio(async->bio); 80106ea01b1SDavid Sterba } 8024a69a410SChris Mason } 8034a69a410SChris Mason 8044a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8054a69a410SChris Mason { 8064a69a410SChris Mason struct async_submit_bio *async; 8074a69a410SChris Mason 8084a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8098b712842SChris Mason kfree(async); 8108b712842SChris Mason } 8118b712842SChris Mason 8128896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 813c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 8141941b64bSQu Wenruo u64 dio_file_offset, 815e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 81644b8bd7eSChris Mason { 8178896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 81844b8bd7eSChris Mason struct async_submit_bio *async; 81944b8bd7eSChris Mason 82044b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 82144b8bd7eSChris Mason if (!async) 8224e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 82344b8bd7eSChris Mason 8248896a08dSQu Wenruo async->inode = inode; 82544b8bd7eSChris Mason async->bio = bio; 82644b8bd7eSChris Mason async->mirror_num = mirror_num; 8274a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8284a69a410SChris Mason 829a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 830a0cac0ecSOmar Sandoval run_one_async_free); 8314a69a410SChris Mason 8321941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8338c8bee1dSChris Mason 8344e4cbee9SChristoph Hellwig async->status = 0; 83579787eaaSJeff Mahoney 83667f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8375cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 838d313d7a3SChris Mason 8395cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 84044b8bd7eSChris Mason return 0; 84144b8bd7eSChris Mason } 84244b8bd7eSChris Mason 8434e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 844ce3ed71aSChris Mason { 8452c30c71bSKent Overstreet struct bio_vec *bvec; 846ce3ed71aSChris Mason struct btrfs_root *root; 8472b070cfeSChristoph Hellwig int ret = 0; 8486dc4f100SMing Lei struct bvec_iter_all iter_all; 849ce3ed71aSChris Mason 850c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8512b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 852ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 853ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 85479787eaaSJeff Mahoney if (ret) 85579787eaaSJeff Mahoney break; 856ce3ed71aSChris Mason } 8572c30c71bSKent Overstreet 8584e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 859ce3ed71aSChris Mason } 860ce3ed71aSChris Mason 8618896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 8621941b64bSQu Wenruo u64 dio_file_offset) 86322c59948SChris Mason { 8648b712842SChris Mason /* 8658b712842SChris Mason * when we're called for a write, we're already in the async 8665443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8678b712842SChris Mason */ 86879787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86922c59948SChris Mason } 87022c59948SChris Mason 8719b4e675aSDavid Sterba static int check_async_write(struct btrfs_fs_info *fs_info, 8729b4e675aSDavid Sterba struct btrfs_inode *bi) 873de0022b9SJosef Bacik { 8746300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8756300463bSLiu Bo return 0; 8769b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 877de0022b9SJosef Bacik return 0; 878de0022b9SJosef Bacik return 1; 879de0022b9SJosef Bacik } 880de0022b9SJosef Bacik 8811b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 8821b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 88344b8bd7eSChris Mason { 8840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8859b4e675aSDavid Sterba int async = check_async_write(fs_info, BTRFS_I(inode)); 8864e4cbee9SChristoph Hellwig blk_status_t ret; 887cad321adSChris Mason 888cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 889cad321adSChris Mason /* 890cad321adSChris Mason * called for a read, do the setup so that checksum validation 891cad321adSChris Mason * can happen in the async kernel threads 892cad321adSChris Mason */ 8930b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8940b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8951d4284bdSChris Mason if (ret) 89661891923SStefan Behrens goto out_w_error; 89708635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 898de0022b9SJosef Bacik } else if (!async) { 899de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 900de0022b9SJosef Bacik if (ret) 90161891923SStefan Behrens goto out_w_error; 90208635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 90361891923SStefan Behrens } else { 904cad321adSChris Mason /* 90561891923SStefan Behrens * kthread helpers are used to submit writes so that 90661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 907cad321adSChris Mason */ 9088896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 9098896a08dSQu Wenruo 0, btree_submit_bio_start); 91044b8bd7eSChris Mason } 91144b8bd7eSChris Mason 9124246a0b6SChristoph Hellwig if (ret) 9134246a0b6SChristoph Hellwig goto out_w_error; 9144246a0b6SChristoph Hellwig return 0; 9154246a0b6SChristoph Hellwig 91661891923SStefan Behrens out_w_error: 9174e4cbee9SChristoph Hellwig bio->bi_status = ret; 9184246a0b6SChristoph Hellwig bio_endio(bio); 91961891923SStefan Behrens return ret; 92061891923SStefan Behrens } 92161891923SStefan Behrens 9223dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 923784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 924a6bc32b8SMel Gorman struct page *newpage, struct page *page, 925a6bc32b8SMel Gorman enum migrate_mode mode) 926784b4e29SChris Mason { 927784b4e29SChris Mason /* 928784b4e29SChris Mason * we can't safely write a btree page from here, 929784b4e29SChris Mason * we haven't done the locking hook 930784b4e29SChris Mason */ 931784b4e29SChris Mason if (PageDirty(page)) 932784b4e29SChris Mason return -EAGAIN; 933784b4e29SChris Mason /* 934784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 935784b4e29SChris Mason * We must have no buffers or drop them. 936784b4e29SChris Mason */ 937784b4e29SChris Mason if (page_has_private(page) && 938784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 939784b4e29SChris Mason return -EAGAIN; 940a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 941784b4e29SChris Mason } 9423dd1462eSJan Beulich #endif 943784b4e29SChris Mason 9440da5468fSChris Mason 9450da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9460da5468fSChris Mason struct writeback_control *wbc) 9470da5468fSChris Mason { 948e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 949e2d84521SMiao Xie int ret; 950e2d84521SMiao Xie 951d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 952448d640bSChris Mason 953448d640bSChris Mason if (wbc->for_kupdate) 954448d640bSChris Mason return 0; 955448d640bSChris Mason 956e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 957b9473439SChris Mason /* this is a bit racy, but that's ok */ 958d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 959d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 960d814a491SEthan Lien fs_info->dirty_metadata_batch); 961e2d84521SMiao Xie if (ret < 0) 962793955bcSChris Mason return 0; 963793955bcSChris Mason } 9640b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9650da5468fSChris Mason } 9660da5468fSChris Mason 96770dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9685f39d397SChris Mason { 96998509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 97098509cfcSChris Mason return 0; 9710c4e538bSDavid Sterba 972f7a52a40SDavid Sterba return try_release_extent_buffer(page); 973d98237b3SChris Mason } 974d98237b3SChris Mason 975d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 976d47992f8SLukas Czerner unsigned int length) 977d98237b3SChris Mason { 978d1310b2eSChris Mason struct extent_io_tree *tree; 979d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9805f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9815f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9829ad6b7bcSChris Mason if (PagePrivate(page)) { 983efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 984efe120a0SFrank Holton "page private not zero on page %llu", 985efe120a0SFrank Holton (unsigned long long)page_offset(page)); 986d1b89bc0SGuoqing Jiang detach_page_private(page); 9879ad6b7bcSChris Mason } 988d98237b3SChris Mason } 989d98237b3SChris Mason 9900b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9910b32f4bbSJosef Bacik { 992bb146eb2SJosef Bacik #ifdef DEBUG 9930b32f4bbSJosef Bacik struct extent_buffer *eb; 9940b32f4bbSJosef Bacik 9950b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9960b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9970b32f4bbSJosef Bacik BUG_ON(!eb); 9980b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9990b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10000b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1001bb146eb2SJosef Bacik #endif 10020b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10030b32f4bbSJosef Bacik } 10040b32f4bbSJosef Bacik 10057f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10060da5468fSChris Mason .writepages = btree_writepages, 10075f39d397SChris Mason .releasepage = btree_releasepage, 10085f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10095a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1010784b4e29SChris Mason .migratepage = btree_migratepage, 10115a92bc88SChris Mason #endif 10120b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1013d98237b3SChris Mason }; 1014123abc88SChris Mason 10152ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10162ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10173fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 10183fbaf258SJosef Bacik int level) 10190999df54SChris Mason { 10200b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10210b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10223fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10230999df54SChris Mason } 10240999df54SChris Mason 1025581c1760SQu Wenruo /* 1026581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1027581c1760SQu Wenruo * verification. 1028581c1760SQu Wenruo * 10291b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1030581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1031581c1760SQu Wenruo * @level: expected level, mandatory check 1032581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1033581c1760SQu Wenruo */ 10342ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 10351b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 10361b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1037e20d96d6SChris Mason { 10385f39d397SChris Mason struct extent_buffer *buf = NULL; 103919c00ddcSChris Mason int ret; 104019c00ddcSChris Mason 10413fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1042c871b0f2SLiu Bo if (IS_ERR(buf)) 1043c871b0f2SLiu Bo return buf; 1044e4204dedSChris Mason 10455ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1046581c1760SQu Wenruo level, first_key); 10470f0fe8f7SFilipe David Borba Manana if (ret) { 1048537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 104964c043deSLiu Bo return ERR_PTR(ret); 10500f0fe8f7SFilipe David Borba Manana } 10515f39d397SChris Mason return buf; 1052ce9adaa5SChris Mason 1053eb60ceacSChris Mason } 1054eb60ceacSChris Mason 10556a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1056ed2ff2cbSChris Mason { 10576a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 105855c69072SChris Mason if (btrfs_header_generation(buf) == 1059e2d84521SMiao Xie fs_info->running_transaction->transid) { 1060b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1061b4ce94deSChris Mason 1062b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1063104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1064e2d84521SMiao Xie -buf->len, 1065e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10660b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1067925baeddSChris Mason } 10685f39d397SChris Mason } 1069ed7b63ebSJosef Bacik } 10705f39d397SChris Mason 1071da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1072e20d96d6SChris Mason u64 objectid) 1073d97e63b6SChris Mason { 10747c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 107596dfcb46SJosef Bacik root->fs_info = fs_info; 1076cfaa7295SChris Mason root->node = NULL; 1077a28ec197SChris Mason root->commit_root = NULL; 107827cdeb70SMiao Xie root->state = 0; 1079d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 10800b86a832SChris Mason 10810f7d52f4SChris Mason root->last_trans = 0; 10826b8fad57SNikolay Borisov root->free_objectid = 0; 1083eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1084199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10856bef4d31SEric Paris root->inode_tree = RB_ROOT; 108616cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1087f0486c68SYan, Zheng root->block_rsv = NULL; 10880b86a832SChris Mason 10890b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10905d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1091eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1092eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1093199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1094199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1095d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10962ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10972ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10985d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1099eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1100199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1101f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11022ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11032ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11048287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1105a2135011SChris Mason mutex_init(&root->objectid_mutex); 1106e02119d5SChris Mason mutex_init(&root->log_mutex); 110731f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1108573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1109c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11107237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11117237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11127237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11138b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11148b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11157237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11167237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11177237f183SYan Zheng atomic_set(&root->log_writers, 0); 11182ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11190700cea7SElena Reshetova refcount_set(&root->refs, 1); 11208ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1121eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11227237f183SYan Zheng root->log_transid = 0; 1123d1433debSMiao Xie root->log_transid_committed = -1; 1124257c62e1SChris Mason root->last_log_commit = 0; 1125e289f03eSFilipe Manana if (!dummy) { 112643eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 112743eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1128e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1129e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1130e289f03eSFilipe Manana } 1131017e5369SChris Mason 11323768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11333768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11346702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11354d775673SChris Mason root->root_key.objectid = objectid; 11360ee5dc67SAl Viro root->anon_dev = 0; 11378ea05e3aSAlexander Block 11385f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1139370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1140bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1141bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1142bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1143bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1144bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1145bd647ce3SJosef Bacik #endif 11463768f368SChris Mason } 11473768f368SChris Mason 114874e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 114996dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11506f07e42eSAl Viro { 115174e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11526f07e42eSAl Viro if (root) 115396dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11546f07e42eSAl Viro return root; 11556f07e42eSAl Viro } 11566f07e42eSAl Viro 115706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 115806ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1159da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 116006ea65a3SJosef Bacik { 116106ea65a3SJosef Bacik struct btrfs_root *root; 116206ea65a3SJosef Bacik 11637c0260eeSJeff Mahoney if (!fs_info) 11647c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11657c0260eeSJeff Mahoney 116696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 116706ea65a3SJosef Bacik if (!root) 116806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1169da17066cSJeff Mahoney 1170b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1171faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 117206ea65a3SJosef Bacik 117306ea65a3SJosef Bacik return root; 117406ea65a3SJosef Bacik } 117506ea65a3SJosef Bacik #endif 117606ea65a3SJosef Bacik 117720897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 117820897f5cSArne Jansen u64 objectid) 117920897f5cSArne Jansen { 11809b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 118120897f5cSArne Jansen struct extent_buffer *leaf; 118220897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 118320897f5cSArne Jansen struct btrfs_root *root; 118420897f5cSArne Jansen struct btrfs_key key; 1185b89f6d1fSFilipe Manana unsigned int nofs_flag; 118620897f5cSArne Jansen int ret = 0; 118720897f5cSArne Jansen 1188b89f6d1fSFilipe Manana /* 1189b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1190b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1191b89f6d1fSFilipe Manana */ 1192b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 119396dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1194b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 119520897f5cSArne Jansen if (!root) 119620897f5cSArne Jansen return ERR_PTR(-ENOMEM); 119720897f5cSArne Jansen 119820897f5cSArne Jansen root->root_key.objectid = objectid; 119920897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 120020897f5cSArne Jansen root->root_key.offset = 0; 120120897f5cSArne Jansen 12029631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12039631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 120420897f5cSArne Jansen if (IS_ERR(leaf)) { 120520897f5cSArne Jansen ret = PTR_ERR(leaf); 12061dd05682STsutomu Itoh leaf = NULL; 12078a6a87cdSBoris Burkov goto fail_unlock; 120820897f5cSArne Jansen } 120920897f5cSArne Jansen 121020897f5cSArne Jansen root->node = leaf; 121120897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 121220897f5cSArne Jansen 121320897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 121427cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 121520897f5cSArne Jansen 1216f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1217f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 121820897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 121920897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 122020897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 122120897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 122220897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 122320897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 122420897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 122533d85fdaSQu Wenruo if (is_fstree(objectid)) 1226807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1227807fc790SAndy Shevchenko else 1228807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1229c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 123020897f5cSArne Jansen 12318a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 12328a6a87cdSBoris Burkov 123320897f5cSArne Jansen key.objectid = objectid; 123420897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 123520897f5cSArne Jansen key.offset = 0; 123620897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 123720897f5cSArne Jansen if (ret) 123820897f5cSArne Jansen goto fail; 123920897f5cSArne Jansen 124020897f5cSArne Jansen return root; 12411dd05682STsutomu Itoh 12428a6a87cdSBoris Burkov fail_unlock: 12438c38938cSJosef Bacik if (leaf) 12441dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 12458a6a87cdSBoris Burkov fail: 124600246528SJosef Bacik btrfs_put_root(root); 12471dd05682STsutomu Itoh 12481dd05682STsutomu Itoh return ERR_PTR(ret); 124920897f5cSArne Jansen } 125020897f5cSArne Jansen 12517237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1252e02119d5SChris Mason struct btrfs_fs_info *fs_info) 12530f7d52f4SChris Mason { 12540f7d52f4SChris Mason struct btrfs_root *root; 12557237f183SYan Zheng struct extent_buffer *leaf; 1256e02119d5SChris Mason 125796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1258e02119d5SChris Mason if (!root) 12597237f183SYan Zheng return ERR_PTR(-ENOMEM); 1260e02119d5SChris Mason 1261e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1262e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1263e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 126427cdeb70SMiao Xie 12657237f183SYan Zheng /* 126692a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 126727cdeb70SMiao Xie * 126892a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 126992a7cc42SQu Wenruo * and they go away before a real commit is actually done. 127092a7cc42SQu Wenruo * 127192a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 127292a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 12737237f183SYan Zheng */ 1274e02119d5SChris Mason 12754d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 12769631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 12777237f183SYan Zheng if (IS_ERR(leaf)) { 127800246528SJosef Bacik btrfs_put_root(root); 12797237f183SYan Zheng return ERR_CAST(leaf); 12807237f183SYan Zheng } 1281e02119d5SChris Mason 12827237f183SYan Zheng root->node = leaf; 1283e02119d5SChris Mason 1284e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1285e02119d5SChris Mason btrfs_tree_unlock(root->node); 12867237f183SYan Zheng return root; 12877237f183SYan Zheng } 12887237f183SYan Zheng 12897237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 12907237f183SYan Zheng struct btrfs_fs_info *fs_info) 12917237f183SYan Zheng { 12927237f183SYan Zheng struct btrfs_root *log_root; 12937237f183SYan Zheng 12947237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 12957237f183SYan Zheng if (IS_ERR(log_root)) 12967237f183SYan Zheng return PTR_ERR(log_root); 12977237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 12987237f183SYan Zheng fs_info->log_root_tree = log_root; 12997237f183SYan Zheng return 0; 13007237f183SYan Zheng } 13017237f183SYan Zheng 13027237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13037237f183SYan Zheng struct btrfs_root *root) 13047237f183SYan Zheng { 13050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13067237f183SYan Zheng struct btrfs_root *log_root; 13077237f183SYan Zheng struct btrfs_inode_item *inode_item; 13087237f183SYan Zheng 13090b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13107237f183SYan Zheng if (IS_ERR(log_root)) 13117237f183SYan Zheng return PTR_ERR(log_root); 13127237f183SYan Zheng 13137237f183SYan Zheng log_root->last_trans = trans->transid; 13147237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13157237f183SYan Zheng 13167237f183SYan Zheng inode_item = &log_root->root_item.inode; 13173cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13183cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13193cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1320da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13210b246afaSJeff Mahoney fs_info->nodesize); 13223cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13237237f183SYan Zheng 13245d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13257237f183SYan Zheng 13267237f183SYan Zheng WARN_ON(root->log_root); 13277237f183SYan Zheng root->log_root = log_root; 13287237f183SYan Zheng root->log_transid = 0; 1329d1433debSMiao Xie root->log_transid_committed = -1; 1330257c62e1SChris Mason root->last_log_commit = 0; 1331e02119d5SChris Mason return 0; 1332e02119d5SChris Mason } 1333e02119d5SChris Mason 133449d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 133549d11beaSJosef Bacik struct btrfs_path *path, 1336cb517eabSMiao Xie struct btrfs_key *key) 1337e02119d5SChris Mason { 1338e02119d5SChris Mason struct btrfs_root *root; 1339e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 134084234f3aSYan Zheng u64 generation; 1341cb517eabSMiao Xie int ret; 1342581c1760SQu Wenruo int level; 1343cb517eabSMiao Xie 134496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 134549d11beaSJosef Bacik if (!root) 134649d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 13470f7d52f4SChris Mason 1348cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1349cb517eabSMiao Xie &root->root_item, &root->root_key); 13500f7d52f4SChris Mason if (ret) { 135113a8a7c8SYan, Zheng if (ret > 0) 135213a8a7c8SYan, Zheng ret = -ENOENT; 135349d11beaSJosef Bacik goto fail; 13540f7d52f4SChris Mason } 135513a8a7c8SYan, Zheng 135684234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1357581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 13582ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 13592ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 13601b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 136164c043deSLiu Bo if (IS_ERR(root->node)) { 136264c043deSLiu Bo ret = PTR_ERR(root->node); 13638c38938cSJosef Bacik root->node = NULL; 136449d11beaSJosef Bacik goto fail; 1365cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1366cb517eabSMiao Xie ret = -EIO; 136749d11beaSJosef Bacik goto fail; 1368416bc658SJosef Bacik } 13695d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1370cb517eabSMiao Xie return root; 137149d11beaSJosef Bacik fail: 137200246528SJosef Bacik btrfs_put_root(root); 137349d11beaSJosef Bacik return ERR_PTR(ret); 137449d11beaSJosef Bacik } 137549d11beaSJosef Bacik 137649d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 137749d11beaSJosef Bacik struct btrfs_key *key) 137849d11beaSJosef Bacik { 137949d11beaSJosef Bacik struct btrfs_root *root; 138049d11beaSJosef Bacik struct btrfs_path *path; 138149d11beaSJosef Bacik 138249d11beaSJosef Bacik path = btrfs_alloc_path(); 138349d11beaSJosef Bacik if (!path) 138449d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 138549d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 138649d11beaSJosef Bacik btrfs_free_path(path); 138749d11beaSJosef Bacik 138849d11beaSJosef Bacik return root; 1389cb517eabSMiao Xie } 1390cb517eabSMiao Xie 13912dfb1e43SQu Wenruo /* 13922dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 13932dfb1e43SQu Wenruo * 13942dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 13952dfb1e43SQu Wenruo */ 13962dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1397cb517eabSMiao Xie { 1398cb517eabSMiao Xie int ret; 1399dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1400cb517eabSMiao Xie 1401dcc3eb96SNikolay Borisov /* 1402dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1403dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1404dcc3eb96SNikolay Borisov */ 1405dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1406dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1407dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1408dcc3eb96SNikolay Borisov if (ret) 14098257b2dcSMiao Xie goto fail; 14108257b2dcSMiao Xie 1411aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 1412aeb935a4SQu Wenruo root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { 141392a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1414f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1415f39e4571SJosef Bacik } 1416f39e4571SJosef Bacik 1417851fd730SQu Wenruo /* 1418851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1419851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1420851fd730SQu Wenruo */ 1421851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1422851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 14232dfb1e43SQu Wenruo if (!anon_dev) { 1424cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1425cb517eabSMiao Xie if (ret) 1426876d2cf1SLiu Bo goto fail; 14272dfb1e43SQu Wenruo } else { 14282dfb1e43SQu Wenruo root->anon_dev = anon_dev; 14292dfb1e43SQu Wenruo } 1430851fd730SQu Wenruo } 1431f32e48e9SChandan Rajendra 1432f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1433453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1434f32e48e9SChandan Rajendra if (ret) { 1435f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1436876d2cf1SLiu Bo goto fail; 1437f32e48e9SChandan Rajendra } 1438f32e48e9SChandan Rajendra 14396b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1440f32e48e9SChandan Rajendra 1441f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1442f32e48e9SChandan Rajendra 1443cb517eabSMiao Xie return 0; 1444cb517eabSMiao Xie fail: 144584db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1446cb517eabSMiao Xie return ret; 1447cb517eabSMiao Xie } 1448cb517eabSMiao Xie 1449a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1450cb517eabSMiao Xie u64 root_id) 1451cb517eabSMiao Xie { 1452cb517eabSMiao Xie struct btrfs_root *root; 1453cb517eabSMiao Xie 1454cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1455cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1456cb517eabSMiao Xie (unsigned long)root_id); 1457bc44d7c4SJosef Bacik if (root) 145800246528SJosef Bacik root = btrfs_grab_root(root); 1459cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1460cb517eabSMiao Xie return root; 1461cb517eabSMiao Xie } 1462cb517eabSMiao Xie 146349d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 146449d11beaSJosef Bacik u64 objectid) 146549d11beaSJosef Bacik { 146649d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 146749d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 146849d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 146949d11beaSJosef Bacik return btrfs_grab_root(fs_info->extent_root); 147049d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 147149d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 147249d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 147349d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 147449d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 147549d11beaSJosef Bacik return btrfs_grab_root(fs_info->csum_root); 147649d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 147749d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 147849d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 147949d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 148049d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 148149d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 148249d11beaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 148349d11beaSJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 148449d11beaSJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 148549d11beaSJosef Bacik return NULL; 148649d11beaSJosef Bacik } 148749d11beaSJosef Bacik 1488cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1489cb517eabSMiao Xie struct btrfs_root *root) 1490cb517eabSMiao Xie { 1491cb517eabSMiao Xie int ret; 1492cb517eabSMiao Xie 1493e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1494cb517eabSMiao Xie if (ret) 1495cb517eabSMiao Xie return ret; 1496cb517eabSMiao Xie 1497cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1498cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1499cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1500cb517eabSMiao Xie root); 1501af01d2e5SJosef Bacik if (ret == 0) { 150200246528SJosef Bacik btrfs_grab_root(root); 150327cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1504af01d2e5SJosef Bacik } 1505cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1506cb517eabSMiao Xie radix_tree_preload_end(); 1507cb517eabSMiao Xie 1508cb517eabSMiao Xie return ret; 1509cb517eabSMiao Xie } 1510cb517eabSMiao Xie 1511bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1512bd647ce3SJosef Bacik { 1513bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1514bd647ce3SJosef Bacik struct btrfs_root *root; 1515bd647ce3SJosef Bacik 1516bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1517457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1518457f1864SJosef Bacik 1519bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1520bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1521457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 152271008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1523bd647ce3SJosef Bacik refcount_read(&root->refs)); 1524bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 152500246528SJosef Bacik btrfs_put_root(root); 152600246528SJosef Bacik btrfs_put_root(root); 1527bd647ce3SJosef Bacik } 1528bd647ce3SJosef Bacik #endif 1529bd647ce3SJosef Bacik } 1530bd647ce3SJosef Bacik 15310d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 15320d4b0463SJosef Bacik { 1533141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1534141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 15355deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1536141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1537141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1538141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1539141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 15400d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 15410d4b0463SJosef Bacik kfree(fs_info->delayed_root); 154200246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 154300246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 154400246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 154500246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 154600246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 154700246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 154800246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 154900246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 155000246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1551aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 1552bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 15533fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 15540d4b0463SJosef Bacik kfree(fs_info->super_copy); 15550d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 15560d4b0463SJosef Bacik kvfree(fs_info); 15570d4b0463SJosef Bacik } 15580d4b0463SJosef Bacik 15590d4b0463SJosef Bacik 15602dfb1e43SQu Wenruo /* 15612dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 15622dfb1e43SQu Wenruo * 15632dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 15642dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 15652dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 15662dfb1e43SQu Wenruo * 15672dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 15682dfb1e43SQu Wenruo * 15692dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 15702dfb1e43SQu Wenruo * same root objectid. 15712dfb1e43SQu Wenruo * 15722dfb1e43SQu Wenruo * @objectid: root id 15732dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 15742dfb1e43SQu Wenruo * pass 0 for new allocation. 15752dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 15762dfb1e43SQu Wenruo * for orphan roots 15772dfb1e43SQu Wenruo */ 15782dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 15792dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 15802dfb1e43SQu Wenruo bool check_ref) 15815eda7b5eSChris Mason { 15825eda7b5eSChris Mason struct btrfs_root *root; 1583381cf658SDavid Sterba struct btrfs_path *path; 15841d4c08e0SDavid Sterba struct btrfs_key key; 15855eda7b5eSChris Mason int ret; 15865eda7b5eSChris Mason 158749d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 158849d11beaSJosef Bacik if (root) 158949d11beaSJosef Bacik return root; 15904df27c4dSYan, Zheng again: 159156e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 159248475471SStefan Behrens if (root) { 15932dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 15942dfb1e43SQu Wenruo ASSERT(!anon_dev); 1595bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 159600246528SJosef Bacik btrfs_put_root(root); 159748475471SStefan Behrens return ERR_PTR(-ENOENT); 1598bc44d7c4SJosef Bacik } 15995eda7b5eSChris Mason return root; 160048475471SStefan Behrens } 16015eda7b5eSChris Mason 160256e9357aSDavid Sterba key.objectid = objectid; 160356e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 160456e9357aSDavid Sterba key.offset = (u64)-1; 160556e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 16065eda7b5eSChris Mason if (IS_ERR(root)) 16075eda7b5eSChris Mason return root; 16083394e160SChris Mason 1609c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1610d68fc57bSYan, Zheng ret = -ENOENT; 1611d68fc57bSYan, Zheng goto fail; 1612d68fc57bSYan, Zheng } 1613d68fc57bSYan, Zheng 16142dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1615cb517eabSMiao Xie if (ret) 1616cb517eabSMiao Xie goto fail; 1617cb517eabSMiao Xie 1618381cf658SDavid Sterba path = btrfs_alloc_path(); 1619381cf658SDavid Sterba if (!path) { 1620381cf658SDavid Sterba ret = -ENOMEM; 1621381cf658SDavid Sterba goto fail; 1622381cf658SDavid Sterba } 16231d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16241d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 162556e9357aSDavid Sterba key.offset = objectid; 16261d4c08e0SDavid Sterba 16271d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1628381cf658SDavid Sterba btrfs_free_path(path); 1629d68fc57bSYan, Zheng if (ret < 0) 1630d68fc57bSYan, Zheng goto fail; 1631d68fc57bSYan, Zheng if (ret == 0) 163227cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1633d68fc57bSYan, Zheng 1634cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16350f7d52f4SChris Mason if (ret) { 163600246528SJosef Bacik btrfs_put_root(root); 16374785e24fSJosef Bacik if (ret == -EEXIST) 16384df27c4dSYan, Zheng goto again; 16394df27c4dSYan, Zheng goto fail; 16404df27c4dSYan, Zheng } 1641edbd8d4eSChris Mason return root; 16424df27c4dSYan, Zheng fail: 16438c38938cSJosef Bacik btrfs_put_root(root); 16444df27c4dSYan, Zheng return ERR_PTR(ret); 1645edbd8d4eSChris Mason } 1646edbd8d4eSChris Mason 16472dfb1e43SQu Wenruo /* 16482dfb1e43SQu Wenruo * Get in-memory reference of a root structure 16492dfb1e43SQu Wenruo * 16502dfb1e43SQu Wenruo * @objectid: tree objectid 16512dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 16522dfb1e43SQu Wenruo * one reference 16532dfb1e43SQu Wenruo */ 16542dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 16552dfb1e43SQu Wenruo u64 objectid, bool check_ref) 16562dfb1e43SQu Wenruo { 16572dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 16582dfb1e43SQu Wenruo } 16592dfb1e43SQu Wenruo 16602dfb1e43SQu Wenruo /* 16612dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 16622dfb1e43SQu Wenruo * the anonymous block device id 16632dfb1e43SQu Wenruo * 16642dfb1e43SQu Wenruo * @objectid: tree objectid 16652dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 16662dfb1e43SQu Wenruo * parameter value 16672dfb1e43SQu Wenruo */ 16682dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 16692dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 16702dfb1e43SQu Wenruo { 16712dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 16722dfb1e43SQu Wenruo } 16732dfb1e43SQu Wenruo 16748b712842SChris Mason /* 167549d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 167649d11beaSJosef Bacik * @fs_info: the fs_info 167749d11beaSJosef Bacik * @objectid: the objectid we need to lookup 167849d11beaSJosef Bacik * 167949d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 168049d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 168149d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 168249d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 168349d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 168449d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 168549d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 168649d11beaSJosef Bacik * backref code is finished using the root. 168749d11beaSJosef Bacik */ 168849d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 168949d11beaSJosef Bacik struct btrfs_path *path, 169049d11beaSJosef Bacik u64 objectid) 169149d11beaSJosef Bacik { 169249d11beaSJosef Bacik struct btrfs_root *root; 169349d11beaSJosef Bacik struct btrfs_key key; 169449d11beaSJosef Bacik 169549d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 169649d11beaSJosef Bacik 169749d11beaSJosef Bacik /* 169849d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 169949d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 170049d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 170149d11beaSJosef Bacik * != NULL just return it. 170249d11beaSJosef Bacik */ 170349d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 170449d11beaSJosef Bacik if (root) 170549d11beaSJosef Bacik return root; 170649d11beaSJosef Bacik 170749d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 170849d11beaSJosef Bacik if (root) 170949d11beaSJosef Bacik return root; 171049d11beaSJosef Bacik 171149d11beaSJosef Bacik key.objectid = objectid; 171249d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 171349d11beaSJosef Bacik key.offset = (u64)-1; 171449d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 171549d11beaSJosef Bacik btrfs_release_path(path); 171649d11beaSJosef Bacik 171749d11beaSJosef Bacik return root; 171849d11beaSJosef Bacik } 171949d11beaSJosef Bacik 172049d11beaSJosef Bacik /* 17218b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 17228b712842SChris Mason * functions. This is where read checksum verification actually happens 17238b712842SChris Mason */ 17248b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1725ce9adaa5SChris Mason { 1726ce9adaa5SChris Mason struct bio *bio; 172797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1728ce9adaa5SChris Mason 172997eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1730ce9adaa5SChris Mason bio = end_io_wq->bio; 17318b712842SChris Mason 17324e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1733ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1734ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 17354246a0b6SChristoph Hellwig bio_endio(bio); 17369be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1737ce9adaa5SChris Mason } 173844b8bd7eSChris Mason 1739a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1740a74a4b97SChris Mason { 1741a74a4b97SChris Mason struct btrfs_root *root = arg; 17420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1743d0278245SMiao Xie int again; 1744a74a4b97SChris Mason 1745d6fd0ae2SOmar Sandoval while (1) { 1746d0278245SMiao Xie again = 0; 17479d1a2a3aSDavid Sterba 1748fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1749fd340d0fSJosef Bacik 1750d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17512ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1752d0278245SMiao Xie goto sleep; 1753d0278245SMiao Xie 175490c711abSZygo Blaxell /* 175590c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 175690c711abSZygo Blaxell * before we have finished mounting the filesystem. 175790c711abSZygo Blaxell */ 17580b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 175990c711abSZygo Blaxell goto sleep; 176090c711abSZygo Blaxell 17610b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1762d0278245SMiao Xie goto sleep; 1763d0278245SMiao Xie 1764dc7f370cSMiao Xie /* 1765dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1766dc7f370cSMiao Xie * during the above check and trylock. 1767dc7f370cSMiao Xie */ 17682ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17690b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1770dc7f370cSMiao Xie goto sleep; 1771dc7f370cSMiao Xie } 1772dc7f370cSMiao Xie 17732ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1774c2d6cb16SFilipe Manana 17759d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17760b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1777a74a4b97SChris Mason 1778d0278245SMiao Xie /* 177905323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 178005323cd1SMiao Xie * needn't do anything special here. 1781d0278245SMiao Xie */ 17820b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 178367c5e7d4SFilipe Manana 178467c5e7d4SFilipe Manana /* 178567c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 178667c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 178767c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 178867c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 178967c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 179067c5e7d4SFilipe Manana * unused block groups. 179167c5e7d4SFilipe Manana */ 17920b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1793d0278245SMiao Xie sleep: 1794a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1795d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1796d6fd0ae2SOmar Sandoval kthread_parkme(); 1797d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1798d6fd0ae2SOmar Sandoval return 0; 1799838fe188SJiri Kosina if (!again) { 1800a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1801a74a4b97SChris Mason schedule(); 1802a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1803a74a4b97SChris Mason } 1804da288d28SFilipe Manana } 1805a74a4b97SChris Mason } 1806a74a4b97SChris Mason 1807a74a4b97SChris Mason static int transaction_kthread(void *arg) 1808a74a4b97SChris Mason { 1809a74a4b97SChris Mason struct btrfs_root *root = arg; 18100b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1811a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1812a74a4b97SChris Mason struct btrfs_transaction *cur; 18138929ecfaSYan, Zheng u64 transid; 1814643900beSNikolay Borisov time64_t delta; 1815a74a4b97SChris Mason unsigned long delay; 1816914b2007SJan Kara bool cannot_commit; 1817a74a4b97SChris Mason 1818a74a4b97SChris Mason do { 1819914b2007SJan Kara cannot_commit = false; 1820ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 18210b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1822a74a4b97SChris Mason 18230b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18240b246afaSJeff Mahoney cur = fs_info->running_transaction; 1825a74a4b97SChris Mason if (!cur) { 18260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1827a74a4b97SChris Mason goto sleep; 1828a74a4b97SChris Mason } 182931153d81SYan Zheng 1830643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 18313296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1832643900beSNikolay Borisov delta < fs_info->commit_interval) { 18330b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1834fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1835fb8a7e94SNikolay Borisov delay = min(delay, 1836fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1837a74a4b97SChris Mason goto sleep; 1838a74a4b97SChris Mason } 18398929ecfaSYan, Zheng transid = cur->transid; 18400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 184156bec294SChris Mason 184279787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1843354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1844914b2007SJan Kara if (IS_ERR(trans)) { 1845354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1846914b2007SJan Kara cannot_commit = true; 184779787eaaSJeff Mahoney goto sleep; 1848914b2007SJan Kara } 18498929ecfaSYan, Zheng if (transid == trans->transid) { 18503a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18518929ecfaSYan, Zheng } else { 18523a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18538929ecfaSYan, Zheng } 1854a74a4b97SChris Mason sleep: 18550b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18560b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1857a74a4b97SChris Mason 18584e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18590b246afaSJeff Mahoney &fs_info->fs_state))) 18602ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18618929ecfaSYan, Zheng if (!kthread_should_stop() && 18620b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1863914b2007SJan Kara cannot_commit)) 1864bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1865a74a4b97SChris Mason } while (!kthread_should_stop()); 1866a74a4b97SChris Mason return 0; 1867a74a4b97SChris Mason } 1868a74a4b97SChris Mason 1869af31f5e5SChris Mason /* 187001f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 187101f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 187201f0f9daSNikolay Borisov * anything. 1873af31f5e5SChris Mason * 1874af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1875af31f5e5SChris Mason * generation of the latest root in the array with the generation 1876af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1877af31f5e5SChris Mason */ 187801f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1879af31f5e5SChris Mason { 188001f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1881af31f5e5SChris Mason u64 cur; 1882af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1883af31f5e5SChris Mason int i; 1884af31f5e5SChris Mason 1885af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1886af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1887af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1888af31f5e5SChris Mason if (cur == newest_gen) 188901f0f9daSNikolay Borisov return i; 1890af31f5e5SChris Mason } 1891af31f5e5SChris Mason 189201f0f9daSNikolay Borisov return -EINVAL; 1893af31f5e5SChris Mason } 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason /* 1896af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1897af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1898af31f5e5SChris Mason * done 1899af31f5e5SChris Mason */ 1900af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1901af31f5e5SChris Mason { 19026ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 1903af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1904af31f5e5SChris Mason 1905af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason /* 1908af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1909af31f5e5SChris Mason * regardless of which ones we use today 1910af31f5e5SChris Mason */ 1911af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1912af31f5e5SChris Mason 1913af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1914af31f5e5SChris Mason 1915af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1916af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1917af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1918af31f5e5SChris Mason 1919af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1927af31f5e5SChris Mason 1928af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1929af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1930af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1931af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1932af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1933af31f5e5SChris Mason 19347c7e82a7SChris Mason /* 19357c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19367c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19377c7e82a7SChris Mason */ 19387c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19397c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19407c7e82a7SChris Mason info->fs_root->node->start); 1941af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1942af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1943af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1944af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19457c7e82a7SChris Mason } 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1948af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1949af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1950af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1951af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1952af31f5e5SChris Mason 1953af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1954af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1955af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1956af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1957af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1960af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1961af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1962af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1963af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1964af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1965af31f5e5SChris Mason 1966af31f5e5SChris Mason /* 1967af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1968af31f5e5SChris Mason * for the next commit 1969af31f5e5SChris Mason */ 1970af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1971af31f5e5SChris Mason &info->super_for_commit->super_roots, 1972af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1973af31f5e5SChris Mason } 1974af31f5e5SChris Mason 1975af31f5e5SChris Mason /* 1976bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 1977bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 1978bd2336b2SNikolay Borisov * 1979bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 1980bd2336b2SNikolay Borisov * priority - priority of backup root required 1981bd2336b2SNikolay Borisov * 1982bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 1983bd2336b2SNikolay Borisov */ 1984bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 1985bd2336b2SNikolay Borisov { 1986bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 1987bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 1988bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 1989bd2336b2SNikolay Borisov 1990bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 1991bd2336b2SNikolay Borisov if (priority == 0) 1992bd2336b2SNikolay Borisov return backup_index; 1993bd2336b2SNikolay Borisov 1994bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 1995bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 1996bd2336b2SNikolay Borisov } else { 1997bd2336b2SNikolay Borisov return -EINVAL; 1998bd2336b2SNikolay Borisov } 1999bd2336b2SNikolay Borisov 2000bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2001bd2336b2SNikolay Borisov 2002bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2003bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2004bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2005bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2006bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2007bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2008bd2336b2SNikolay Borisov 2009bd2336b2SNikolay Borisov /* 2010bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2011bd2336b2SNikolay Borisov * need a fsck 2012bd2336b2SNikolay Borisov */ 2013bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2014bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2015bd2336b2SNikolay Borisov 2016bd2336b2SNikolay Borisov return backup_index; 2017bd2336b2SNikolay Borisov } 2018bd2336b2SNikolay Borisov 20197abadb64SLiu Bo /* helper to cleanup workers */ 20207abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20217abadb64SLiu Bo { 2022dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2023afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20245cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2025fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2026fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2027d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2028fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2029fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 20305b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2031e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2032736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2033a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2034fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2035b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2036b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2037a9b9477dSFilipe Manana /* 2038a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2039a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2040a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2041a9b9477dSFilipe Manana */ 2042a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2043a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20447abadb64SLiu Bo } 20457abadb64SLiu Bo 20462e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20472e9f5954SRashika { 20482e9f5954SRashika if (root) { 20492e9f5954SRashika free_extent_buffer(root->node); 20502e9f5954SRashika free_extent_buffer(root->commit_root); 20512e9f5954SRashika root->node = NULL; 20522e9f5954SRashika root->commit_root = NULL; 20532e9f5954SRashika } 20542e9f5954SRashika } 20552e9f5954SRashika 2056af31f5e5SChris Mason /* helper to cleanup tree roots */ 20574273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2058af31f5e5SChris Mason { 20592e9f5954SRashika free_root_extent_buffers(info->tree_root); 2060655b09feSJosef Bacik 20612e9f5954SRashika free_root_extent_buffers(info->dev_root); 20622e9f5954SRashika free_root_extent_buffers(info->extent_root); 20632e9f5954SRashika free_root_extent_buffers(info->csum_root); 20642e9f5954SRashika free_root_extent_buffers(info->quota_root); 20652e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20668c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2067aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 20684273eaffSAnand Jain if (free_chunk_root) 20692e9f5954SRashika free_root_extent_buffers(info->chunk_root); 207070f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2071af31f5e5SChris Mason } 2072af31f5e5SChris Mason 20738c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 20748c38938cSJosef Bacik { 20758c38938cSJosef Bacik if (!root) 20768c38938cSJosef Bacik return; 20778c38938cSJosef Bacik 20788c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 20798c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 20801dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 20818c38938cSJosef Bacik if (root->anon_dev) 20828c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 20838c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2084923eb523SJohannes Thumshirn free_root_extent_buffers(root); 20858c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 20868c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 20878c38938cSJosef Bacik list_del_init(&root->leak_list); 20888c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 20898c38938cSJosef Bacik #endif 20908c38938cSJosef Bacik kfree(root); 20918c38938cSJosef Bacik } 20928c38938cSJosef Bacik } 20938c38938cSJosef Bacik 2094faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2095171f6537SJosef Bacik { 2096171f6537SJosef Bacik int ret; 2097171f6537SJosef Bacik struct btrfs_root *gang[8]; 2098171f6537SJosef Bacik int i; 2099171f6537SJosef Bacik 2100171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2101171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2102171f6537SJosef Bacik struct btrfs_root, root_list); 2103171f6537SJosef Bacik list_del(&gang[0]->root_list); 2104171f6537SJosef Bacik 21058c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2106cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 210700246528SJosef Bacik btrfs_put_root(gang[0]); 2108171f6537SJosef Bacik } 2109171f6537SJosef Bacik 2110171f6537SJosef Bacik while (1) { 2111171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2112171f6537SJosef Bacik (void **)gang, 0, 2113171f6537SJosef Bacik ARRAY_SIZE(gang)); 2114171f6537SJosef Bacik if (!ret) 2115171f6537SJosef Bacik break; 2116171f6537SJosef Bacik for (i = 0; i < ret; i++) 2117cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2118171f6537SJosef Bacik } 2119171f6537SJosef Bacik } 2120af31f5e5SChris Mason 2121638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2122638aa7edSEric Sandeen { 2123638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2124638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2125638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2126638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2127638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2128638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2129ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2130638aa7edSEric Sandeen } 2131638aa7edSEric Sandeen 2132779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2133779a65a4SEric Sandeen { 2134779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2135779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2136779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2137779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2138779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2139779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2140779a65a4SEric Sandeen } 2141779a65a4SEric Sandeen 21426bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2143f37938e0SEric Sandeen { 21442ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21452ff7e61eSJeff Mahoney 21462ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21472ff7e61eSJeff Mahoney set_nlink(inode, 1); 2148f37938e0SEric Sandeen /* 2149f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2150f37938e0SEric Sandeen * the real end of the address space is determined by all of 2151f37938e0SEric Sandeen * the devices in the system 2152f37938e0SEric Sandeen */ 21532ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21542ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2155f37938e0SEric Sandeen 21562ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 215743eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 21582c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 21597b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 21602ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2161f37938e0SEric Sandeen 21625c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 21632ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21642ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21652ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2166f37938e0SEric Sandeen } 2167f37938e0SEric Sandeen 2168ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2169ad618368SEric Sandeen { 2170ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2171129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 21727f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2173ad618368SEric Sandeen } 2174ad618368SEric Sandeen 2175f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2176f9e92e40SEric Sandeen { 2177f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2178f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2179f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2180f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2181f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2182f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2183d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2184f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2185f9e92e40SEric Sandeen } 2186f9e92e40SEric Sandeen 21872a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21882a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21892a458198SEric Sandeen { 2190f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 21916f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21922a458198SEric Sandeen 21932a458198SEric Sandeen fs_info->workers = 2194cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2195cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21962a458198SEric Sandeen 21972a458198SEric Sandeen fs_info->delalloc_workers = 2198cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2199cb001095SJeff Mahoney flags, max_active, 2); 22002a458198SEric Sandeen 22012a458198SEric Sandeen fs_info->flush_workers = 2202cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2203cb001095SJeff Mahoney flags, max_active, 0); 22042a458198SEric Sandeen 22052a458198SEric Sandeen fs_info->caching_workers = 2206cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22072a458198SEric Sandeen 22082a458198SEric Sandeen fs_info->fixup_workers = 2209cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22102a458198SEric Sandeen 22112a458198SEric Sandeen /* 22122a458198SEric Sandeen * endios are largely parallel and should have a very 22132a458198SEric Sandeen * low idle thresh 22142a458198SEric Sandeen */ 22152a458198SEric Sandeen fs_info->endio_workers = 2216cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22172a458198SEric Sandeen fs_info->endio_meta_workers = 2218cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2219cb001095SJeff Mahoney max_active, 4); 22202a458198SEric Sandeen fs_info->endio_meta_write_workers = 2221cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2222cb001095SJeff Mahoney max_active, 2); 22232a458198SEric Sandeen fs_info->endio_raid56_workers = 2224cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2225cb001095SJeff Mahoney max_active, 4); 22262a458198SEric Sandeen fs_info->rmw_workers = 2227cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22282a458198SEric Sandeen fs_info->endio_write_workers = 2229cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2230cb001095SJeff Mahoney max_active, 2); 22312a458198SEric Sandeen fs_info->endio_freespace_worker = 2232cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2233cb001095SJeff Mahoney max_active, 0); 22342a458198SEric Sandeen fs_info->delayed_workers = 2235cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2236cb001095SJeff Mahoney max_active, 0); 22372a458198SEric Sandeen fs_info->readahead_workers = 2238cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2239cb001095SJeff Mahoney max_active, 2); 22402a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2241cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2242b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2243b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 22442a458198SEric Sandeen 22452a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2246ba8a9d07SChris Mason fs_info->flush_workers && 22472a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22482a458198SEric Sandeen fs_info->endio_meta_write_workers && 22492a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22502a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22512a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22522a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2253b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2254b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 22552a458198SEric Sandeen return -ENOMEM; 22562a458198SEric Sandeen } 22572a458198SEric Sandeen 22582a458198SEric Sandeen return 0; 22592a458198SEric Sandeen } 22602a458198SEric Sandeen 22616d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 22626d97c6e3SJohannes Thumshirn { 22636d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2264b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 22656d97c6e3SJohannes Thumshirn 2266b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 22676d97c6e3SJohannes Thumshirn 22686d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 22696d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2270b4e967beSDavid Sterba csum_driver); 22716d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 22726d97c6e3SJohannes Thumshirn } 22736d97c6e3SJohannes Thumshirn 22746d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 22756d97c6e3SJohannes Thumshirn 22766d97c6e3SJohannes Thumshirn return 0; 22776d97c6e3SJohannes Thumshirn } 22786d97c6e3SJohannes Thumshirn 227963443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 228063443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 228163443bf5SEric Sandeen { 228263443bf5SEric Sandeen int ret; 228363443bf5SEric Sandeen struct btrfs_root *log_tree_root; 228463443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 228563443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2286581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 228763443bf5SEric Sandeen 228863443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2289f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 229063443bf5SEric Sandeen return -EIO; 229163443bf5SEric Sandeen } 229263443bf5SEric Sandeen 229396dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 229496dfcb46SJosef Bacik GFP_KERNEL); 229563443bf5SEric Sandeen if (!log_tree_root) 229663443bf5SEric Sandeen return -ENOMEM; 229763443bf5SEric Sandeen 22982ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 22991b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 23001b7ec85eSJosef Bacik fs_info->generation + 1, level, 23011b7ec85eSJosef Bacik NULL); 230264c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2303f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 23040eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 23058c38938cSJosef Bacik log_tree_root->node = NULL; 230600246528SJosef Bacik btrfs_put_root(log_tree_root); 23070eeff236SLiu Bo return ret; 230864c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2309f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 231000246528SJosef Bacik btrfs_put_root(log_tree_root); 231163443bf5SEric Sandeen return -EIO; 231263443bf5SEric Sandeen } 231363443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 231463443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 231563443bf5SEric Sandeen if (ret) { 23160b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 231763443bf5SEric Sandeen "Failed to recover log tree"); 231800246528SJosef Bacik btrfs_put_root(log_tree_root); 231963443bf5SEric Sandeen return ret; 232063443bf5SEric Sandeen } 232163443bf5SEric Sandeen 2322bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23236bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 232463443bf5SEric Sandeen if (ret) 232563443bf5SEric Sandeen return ret; 232663443bf5SEric Sandeen } 232763443bf5SEric Sandeen 232863443bf5SEric Sandeen return 0; 232963443bf5SEric Sandeen } 233063443bf5SEric Sandeen 23316bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23324bbcaa64SEric Sandeen { 23336bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2334a4f3d2c4SDavid Sterba struct btrfs_root *root; 23354bbcaa64SEric Sandeen struct btrfs_key location; 23364bbcaa64SEric Sandeen int ret; 23374bbcaa64SEric Sandeen 23386bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23396bccf3abSJeff Mahoney 23404bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23414bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23424bbcaa64SEric Sandeen location.offset = 0; 23434bbcaa64SEric Sandeen 2344a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2345f50f4353SLiu Bo if (IS_ERR(root)) { 234642437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2347f50f4353SLiu Bo ret = PTR_ERR(root); 2348f50f4353SLiu Bo goto out; 2349f50f4353SLiu Bo } 235042437a63SJosef Bacik } else { 2351a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2352a4f3d2c4SDavid Sterba fs_info->extent_root = root; 235342437a63SJosef Bacik } 23544bbcaa64SEric Sandeen 23554bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2356a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2357f50f4353SLiu Bo if (IS_ERR(root)) { 235842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2359f50f4353SLiu Bo ret = PTR_ERR(root); 2360f50f4353SLiu Bo goto out; 2361f50f4353SLiu Bo } 236242437a63SJosef Bacik } else { 2363a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2364a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23654bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 236642437a63SJosef Bacik } 23674bbcaa64SEric Sandeen 2368882dbe0cSJosef Bacik /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ 2369882dbe0cSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 23704bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2371a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2372f50f4353SLiu Bo if (IS_ERR(root)) { 237342437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2374f50f4353SLiu Bo ret = PTR_ERR(root); 2375f50f4353SLiu Bo goto out; 2376f50f4353SLiu Bo } 237742437a63SJosef Bacik } else { 2378a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2379a4f3d2c4SDavid Sterba fs_info->csum_root = root; 238042437a63SJosef Bacik } 2381882dbe0cSJosef Bacik } 23824bbcaa64SEric Sandeen 2383aeb935a4SQu Wenruo /* 2384aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2385aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2386aeb935a4SQu Wenruo */ 238756e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 238856e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2389aeb935a4SQu Wenruo if (IS_ERR(root)) { 239042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2391aeb935a4SQu Wenruo ret = PTR_ERR(root); 2392aeb935a4SQu Wenruo goto out; 2393aeb935a4SQu Wenruo } 239442437a63SJosef Bacik } else { 2395aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2396aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 239742437a63SJosef Bacik } 2398aeb935a4SQu Wenruo 23994bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2400a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2401a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2402a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2403afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2404a4f3d2c4SDavid Sterba fs_info->quota_root = root; 24054bbcaa64SEric Sandeen } 24064bbcaa64SEric Sandeen 24074bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2408a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2409a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 241042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2411a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 24124bbcaa64SEric Sandeen if (ret != -ENOENT) 2413f50f4353SLiu Bo goto out; 241442437a63SJosef Bacik } 24154bbcaa64SEric Sandeen } else { 2416a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2417a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 24184bbcaa64SEric Sandeen } 24194bbcaa64SEric Sandeen 242070f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 242170f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 242270f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2423f50f4353SLiu Bo if (IS_ERR(root)) { 242442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2425f50f4353SLiu Bo ret = PTR_ERR(root); 2426f50f4353SLiu Bo goto out; 2427f50f4353SLiu Bo } 242842437a63SJosef Bacik } else { 242970f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 243070f6d82eSOmar Sandoval fs_info->free_space_root = root; 243170f6d82eSOmar Sandoval } 243242437a63SJosef Bacik } 243370f6d82eSOmar Sandoval 24344bbcaa64SEric Sandeen return 0; 2435f50f4353SLiu Bo out: 2436f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2437f50f4353SLiu Bo location.objectid, ret); 2438f50f4353SLiu Bo return ret; 24394bbcaa64SEric Sandeen } 24404bbcaa64SEric Sandeen 2441069ec957SQu Wenruo /* 2442069ec957SQu Wenruo * Real super block validation 2443069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2444069ec957SQu Wenruo * 2445069ec957SQu Wenruo * @sb: super block to check 2446069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2447069ec957SQu Wenruo * 0 the primary (1st) sb 2448069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2449069ec957SQu Wenruo * -1 skip bytenr check 2450069ec957SQu Wenruo */ 2451069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2452069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 245321a852b0SQu Wenruo { 245421a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 245521a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 245621a852b0SQu Wenruo int ret = 0; 245721a852b0SQu Wenruo 245821a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 245921a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 246021a852b0SQu Wenruo ret = -EINVAL; 246121a852b0SQu Wenruo } 246221a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 246321a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 246421a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 246521a852b0SQu Wenruo ret = -EINVAL; 246621a852b0SQu Wenruo } 246721a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 246821a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 246921a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 247021a852b0SQu Wenruo ret = -EINVAL; 247121a852b0SQu Wenruo } 247221a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 247321a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 247421a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 247521a852b0SQu Wenruo ret = -EINVAL; 247621a852b0SQu Wenruo } 247721a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 247821a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 247921a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 248021a852b0SQu Wenruo ret = -EINVAL; 248121a852b0SQu Wenruo } 248221a852b0SQu Wenruo 248321a852b0SQu Wenruo /* 248421a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 248521a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 248621a852b0SQu Wenruo */ 248721a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 248821a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 248921a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 249021a852b0SQu Wenruo ret = -EINVAL; 249121a852b0SQu Wenruo } 24920bb3eb3eSQu Wenruo 24930bb3eb3eSQu Wenruo /* 24940bb3eb3eSQu Wenruo * For 4K page size, we only support 4K sector size. 24950bb3eb3eSQu Wenruo * For 64K page size, we support read-write for 64K sector size, and 24960bb3eb3eSQu Wenruo * read-only for 4K sector size. 24970bb3eb3eSQu Wenruo */ 24980bb3eb3eSQu Wenruo if ((PAGE_SIZE == SZ_4K && sectorsize != PAGE_SIZE) || 24990bb3eb3eSQu Wenruo (PAGE_SIZE == SZ_64K && (sectorsize != SZ_4K && 25000bb3eb3eSQu Wenruo sectorsize != SZ_64K))) { 250121a852b0SQu Wenruo btrfs_err(fs_info, 25020bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 250321a852b0SQu Wenruo sectorsize, PAGE_SIZE); 250421a852b0SQu Wenruo ret = -EINVAL; 250521a852b0SQu Wenruo } 25060bb3eb3eSQu Wenruo 250721a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 250821a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 250921a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 251021a852b0SQu Wenruo ret = -EINVAL; 251121a852b0SQu Wenruo } 251221a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 251321a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 251421a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 251521a852b0SQu Wenruo ret = -EINVAL; 251621a852b0SQu Wenruo } 251721a852b0SQu Wenruo 251821a852b0SQu Wenruo /* Root alignment check */ 251921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 252021a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 252121a852b0SQu Wenruo btrfs_super_root(sb)); 252221a852b0SQu Wenruo ret = -EINVAL; 252321a852b0SQu Wenruo } 252421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 252521a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 252621a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 252721a852b0SQu Wenruo ret = -EINVAL; 252821a852b0SQu Wenruo } 252921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 253021a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 253121a852b0SQu Wenruo btrfs_super_log_root(sb)); 253221a852b0SQu Wenruo ret = -EINVAL; 253321a852b0SQu Wenruo } 253421a852b0SQu Wenruo 2535de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 25367239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 253721a852b0SQu Wenruo btrfs_err(fs_info, 25387239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2539de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 254021a852b0SQu Wenruo ret = -EINVAL; 254121a852b0SQu Wenruo } 254221a852b0SQu Wenruo 254321a852b0SQu Wenruo /* 254421a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 254521a852b0SQu Wenruo * done later 254621a852b0SQu Wenruo */ 254721a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 254821a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 254921a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 255021a852b0SQu Wenruo ret = -EINVAL; 255121a852b0SQu Wenruo } 255221a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 255321a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 255421a852b0SQu Wenruo btrfs_super_stripesize(sb)); 255521a852b0SQu Wenruo ret = -EINVAL; 255621a852b0SQu Wenruo } 255721a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 255821a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 255921a852b0SQu Wenruo btrfs_super_num_devices(sb)); 256021a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 256121a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 256221a852b0SQu Wenruo ret = -EINVAL; 256321a852b0SQu Wenruo } 256421a852b0SQu Wenruo 2565069ec957SQu Wenruo if (mirror_num >= 0 && 2566069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 256721a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 256821a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 256921a852b0SQu Wenruo ret = -EINVAL; 257021a852b0SQu Wenruo } 257121a852b0SQu Wenruo 257221a852b0SQu Wenruo /* 257321a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 257421a852b0SQu Wenruo * and one chunk 257521a852b0SQu Wenruo */ 257621a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 257721a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 257821a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 257921a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 258021a852b0SQu Wenruo ret = -EINVAL; 258121a852b0SQu Wenruo } 258221a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 258321a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 258421a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 258521a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 258621a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 258721a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 258821a852b0SQu Wenruo ret = -EINVAL; 258921a852b0SQu Wenruo } 259021a852b0SQu Wenruo 259121a852b0SQu Wenruo /* 259221a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 259321a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 259421a852b0SQu Wenruo */ 259521a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 259621a852b0SQu Wenruo btrfs_warn(fs_info, 259721a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 259821a852b0SQu Wenruo btrfs_super_generation(sb), 259921a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 260021a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 260121a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 260221a852b0SQu Wenruo btrfs_warn(fs_info, 260321a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 260421a852b0SQu Wenruo btrfs_super_generation(sb), 260521a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 260621a852b0SQu Wenruo 260721a852b0SQu Wenruo return ret; 260821a852b0SQu Wenruo } 260921a852b0SQu Wenruo 2610069ec957SQu Wenruo /* 2611069ec957SQu Wenruo * Validation of super block at mount time. 2612069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2613069ec957SQu Wenruo * flags will be skipped. 2614069ec957SQu Wenruo */ 2615069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2616069ec957SQu Wenruo { 2617069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2618069ec957SQu Wenruo } 2619069ec957SQu Wenruo 262075cb857dSQu Wenruo /* 262175cb857dSQu Wenruo * Validation of super block at write time. 262275cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 262375cb857dSQu Wenruo * overwritten soon. 262475cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 262575cb857dSQu Wenruo */ 262675cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 262775cb857dSQu Wenruo struct btrfs_super_block *sb) 262875cb857dSQu Wenruo { 262975cb857dSQu Wenruo int ret; 263075cb857dSQu Wenruo 263175cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 263275cb857dSQu Wenruo if (ret < 0) 263375cb857dSQu Wenruo goto out; 2634e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 263575cb857dSQu Wenruo ret = -EUCLEAN; 263675cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 263775cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 263875cb857dSQu Wenruo goto out; 263975cb857dSQu Wenruo } 264075cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 264175cb857dSQu Wenruo ret = -EUCLEAN; 264275cb857dSQu Wenruo btrfs_err(fs_info, 264375cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 264475cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 264575cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 264675cb857dSQu Wenruo goto out; 264775cb857dSQu Wenruo } 264875cb857dSQu Wenruo out: 264975cb857dSQu Wenruo if (ret < 0) 265075cb857dSQu Wenruo btrfs_err(fs_info, 265175cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 265275cb857dSQu Wenruo return ret; 265375cb857dSQu Wenruo } 265475cb857dSQu Wenruo 26556ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2656b8522a1eSNikolay Borisov { 26576ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2658b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2659b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2660b8522a1eSNikolay Borisov bool handle_error = false; 2661b8522a1eSNikolay Borisov int ret = 0; 2662b8522a1eSNikolay Borisov int i; 2663b8522a1eSNikolay Borisov 2664b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2665b8522a1eSNikolay Borisov u64 generation; 2666b8522a1eSNikolay Borisov int level; 2667b8522a1eSNikolay Borisov 2668b8522a1eSNikolay Borisov if (handle_error) { 2669b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2670b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2671b8522a1eSNikolay Borisov tree_root->node = NULL; 2672b8522a1eSNikolay Borisov 2673b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2674b8522a1eSNikolay Borisov break; 2675b8522a1eSNikolay Borisov 2676b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2677b8522a1eSNikolay Borisov 2678b8522a1eSNikolay Borisov /* 2679b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2680b8522a1eSNikolay Borisov * valid 2681b8522a1eSNikolay Borisov */ 2682b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2683b8522a1eSNikolay Borisov 2684b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2685b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2686b8522a1eSNikolay Borisov 2687b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 26886ef108ddSNikolay Borisov backup_index = ret; 2689b8522a1eSNikolay Borisov if (ret < 0) 2690b8522a1eSNikolay Borisov return ret; 2691b8522a1eSNikolay Borisov } 2692b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2693b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2694b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 26951b7ec85eSJosef Bacik BTRFS_ROOT_TREE_OBJECTID, 2696b8522a1eSNikolay Borisov generation, level, NULL); 26970465337cSJosef Bacik if (IS_ERR(tree_root->node)) { 2698217f5004SNikolay Borisov handle_error = true; 2699b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 27000465337cSJosef Bacik tree_root->node = NULL; 2701217f5004SNikolay Borisov btrfs_warn(fs_info, "couldn't read tree root"); 2702217f5004SNikolay Borisov continue; 2703b8522a1eSNikolay Borisov 2704217f5004SNikolay Borisov } else if (!extent_buffer_uptodate(tree_root->node)) { 2705217f5004SNikolay Borisov handle_error = true; 2706217f5004SNikolay Borisov ret = -EIO; 2707217f5004SNikolay Borisov btrfs_warn(fs_info, "error while reading tree root"); 2708b8522a1eSNikolay Borisov continue; 2709b8522a1eSNikolay Borisov } 2710b8522a1eSNikolay Borisov 2711b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2712b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2713b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2714b8522a1eSNikolay Borisov 2715336a0d8dSNikolay Borisov /* 2716336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2717336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2718336a0d8dSNikolay Borisov */ 2719453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 2720b8522a1eSNikolay Borisov if (ret < 0) { 2721b8522a1eSNikolay Borisov handle_error = true; 2722b8522a1eSNikolay Borisov continue; 2723b8522a1eSNikolay Borisov } 2724b8522a1eSNikolay Borisov 27256b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 2726b8522a1eSNikolay Borisov 2727b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2728b8522a1eSNikolay Borisov if (ret < 0) { 2729b8522a1eSNikolay Borisov handle_error = true; 2730b8522a1eSNikolay Borisov continue; 2731b8522a1eSNikolay Borisov } 2732b8522a1eSNikolay Borisov 2733b8522a1eSNikolay Borisov /* All successful */ 2734b8522a1eSNikolay Borisov fs_info->generation = generation; 2735b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 27366ef108ddSNikolay Borisov 27376ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 27386ef108ddSNikolay Borisov if (backup_index < 0) { 27396ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 27406ef108ddSNikolay Borisov } else { 27416ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 27426ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 27436ef108ddSNikolay Borisov } 2744b8522a1eSNikolay Borisov break; 2745b8522a1eSNikolay Borisov } 2746b8522a1eSNikolay Borisov 2747b8522a1eSNikolay Borisov return ret; 2748b8522a1eSNikolay Borisov } 2749b8522a1eSNikolay Borisov 27508260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2751eb60ceacSChris Mason { 275276dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2753f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 27548fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2755facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 275624bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2757eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 275811833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2759eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2760a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 276176dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 276224bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 27634cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2764ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2765f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 276647ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2767f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2768d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 276967c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 27707585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2771573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2772*0bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 2773de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 277419c00ddcSChris Mason 27750b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 27766324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2777f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 277847ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 2779bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2780bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 27813fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 27823fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2783bd647ce3SJosef Bacik #endif 2784c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 278566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 278666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 278766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 278866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 278966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 279066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 279166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2792ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2793ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2794ba2c4d4eSJosef Bacik 2795771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 27964cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 27972fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2798034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2799fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 280095ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 28019ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 28024cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2803a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2804f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 28058b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2806f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 280790519d66SArne Jansen /* readahead state */ 2808d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 280990519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2810fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2811c8b97818SChris Mason 2812b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2813b34b086cSChris Mason num_online_cpus() + 2, 8); 28140afbaf8cSChris Mason 2815199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2816199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 281769fe2d75SJosef Bacik 2818638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 281921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 282021adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 282121adbd5cSStefan Behrens #endif 2822779a65a4SEric Sandeen btrfs_init_balance(fs_info); 282357056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 2824a2de733cSArne Jansen 28250f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 28266bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2827a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 28280f9dd46cSJosef Bacik 2829fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2830fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2831afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 283239279cc3SChris Mason 28335a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2834e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2835925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2836a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2837a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 28381bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 28399e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2840c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 284176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2842803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2843fa9c0d79SChris Mason 2844ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2845f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2846b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2847416ac51dSArne Jansen 2848fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2849fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2850fa9c0d79SChris Mason 2851e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2852f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2853bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 28544854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2855034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 28563768f368SChris Mason 2857da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2858da17066cSJeff Mahoney fs_info->nodesize = 4096; 2859da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2860ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 2861da17066cSJeff Mahoney fs_info->stripesize = 4096; 2862da17066cSJeff Mahoney 2863eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2864eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 2865eede2bf3SOmar Sandoval 28669e967495SFilipe Manana fs_info->send_in_progress = 0; 28678260edbaSJosef Bacik } 28688260edbaSJosef Bacik 28698260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 28708260edbaSJosef Bacik { 28718260edbaSJosef Bacik int ret; 28728260edbaSJosef Bacik 28738260edbaSJosef Bacik fs_info->sb = sb; 28748260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 28758260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 28769e967495SFilipe Manana 28775deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 2878ae18c37aSJosef Bacik if (ret) 2879ae18c37aSJosef Bacik return ret; 2880ae18c37aSJosef Bacik 2881ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2882ae18c37aSJosef Bacik if (ret) 2883c75e8394SJosef Bacik return ret; 2884ae18c37aSJosef Bacik 2885ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 2886ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 2887ae18c37aSJosef Bacik 2888ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2889ae18c37aSJosef Bacik if (ret) 2890c75e8394SJosef Bacik return ret; 2891ae18c37aSJosef Bacik 2892ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 2893ae18c37aSJosef Bacik GFP_KERNEL); 2894ae18c37aSJosef Bacik if (ret) 2895c75e8394SJosef Bacik return ret; 2896ae18c37aSJosef Bacik 2897ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 2898ae18c37aSJosef Bacik GFP_KERNEL); 2899c75e8394SJosef Bacik if (!fs_info->delayed_root) 2900c75e8394SJosef Bacik return -ENOMEM; 2901ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 2902ae18c37aSJosef Bacik 2903a0a1db70SFilipe Manana if (sb_rdonly(sb)) 2904a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 2905a0a1db70SFilipe Manana 2906c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 2907ae18c37aSJosef Bacik } 2908ae18c37aSJosef Bacik 290997f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 291097f4dd09SNikolay Borisov { 291197f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 291297f4dd09SNikolay Borisov int ret; 291397f4dd09SNikolay Borisov 291497f4dd09SNikolay Borisov /* 291597f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 291697f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 291797f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 291897f4dd09SNikolay Borisov */ 291997f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 292097f4dd09SNikolay Borisov if (ret < 0) { 2921c94bec2cSJosef Bacik if (ret != -EINTR) 2922c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 2923c94bec2cSJosef Bacik ret); 292497f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 292597f4dd09SNikolay Borisov return ret; 292697f4dd09SNikolay Borisov } 292797f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 292897f4dd09SNikolay Borisov } 292997f4dd09SNikolay Borisov 293097f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 293197f4dd09SNikolay Borisov { 293297f4dd09SNikolay Borisov struct task_struct *task; 293397f4dd09SNikolay Borisov 293497f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 293597f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 293697f4dd09SNikolay Borisov if (IS_ERR(task)) { 293797f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 293897f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 293997f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 294097f4dd09SNikolay Borisov return PTR_ERR(task); 294197f4dd09SNikolay Borisov } 294297f4dd09SNikolay Borisov 294397f4dd09SNikolay Borisov return 0; 294497f4dd09SNikolay Borisov } 294597f4dd09SNikolay Borisov 294644c0ca21SBoris Burkov /* 29478cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 29488cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 29498cd29088SBoris Burkov * code paths. 29508cd29088SBoris Burkov */ 29518cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 29528cd29088SBoris Burkov { 29538cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 29548b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 29558cd29088SBoris Burkov } 29568cd29088SBoris Burkov 29578cd29088SBoris Burkov /* 295844c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 295944c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 296044c0ca21SBoris Burkov */ 296144c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 296244c0ca21SBoris Burkov { 296344c0ca21SBoris Burkov int ret; 296494846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 29658b228324SBoris Burkov bool clear_free_space_tree = false; 29668b228324SBoris Burkov 29678b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 29688b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 29698b228324SBoris Burkov clear_free_space_tree = true; 29708b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 29718b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 29728b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 29738b228324SBoris Burkov clear_free_space_tree = true; 29748b228324SBoris Burkov } 29758b228324SBoris Burkov 29768b228324SBoris Burkov if (clear_free_space_tree) { 29778b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 29788b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 29798b228324SBoris Burkov if (ret) { 29808b228324SBoris Burkov btrfs_warn(fs_info, 29818b228324SBoris Burkov "failed to clear free space tree: %d", ret); 29828b228324SBoris Burkov goto out; 29838b228324SBoris Burkov } 29848b228324SBoris Burkov } 298544c0ca21SBoris Burkov 298644c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 298744c0ca21SBoris Burkov if (ret) 298844c0ca21SBoris Burkov goto out; 298944c0ca21SBoris Burkov 29908f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 29918f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 29928f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 29938f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 29948f1c21d7SBoris Burkov goto out; 29958f1c21d7SBoris Burkov } 29968f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 29978f1c21d7SBoris Burkov 299844c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 299944c0ca21SBoris Burkov ret = btrfs_recover_relocation(fs_info->tree_root); 300044c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 300144c0ca21SBoris Burkov if (ret < 0) { 300244c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 300344c0ca21SBoris Burkov goto out; 300444c0ca21SBoris Burkov } 300544c0ca21SBoris Burkov 30065011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 30075011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 30085011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 30095011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 30105011139aSBoris Burkov if (ret) { 30115011139aSBoris Burkov btrfs_warn(fs_info, 30125011139aSBoris Burkov "failed to create free space tree: %d", ret); 30135011139aSBoris Burkov goto out; 30145011139aSBoris Burkov } 30155011139aSBoris Burkov } 30165011139aSBoris Burkov 301794846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 301894846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 301994846229SBoris Burkov if (ret) 302094846229SBoris Burkov goto out; 302194846229SBoris Burkov } 302294846229SBoris Burkov 302344c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 302444c0ca21SBoris Burkov if (ret) 302544c0ca21SBoris Burkov goto out; 302644c0ca21SBoris Burkov 302744c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 302844c0ca21SBoris Burkov if (ret) { 302944c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 303044c0ca21SBoris Burkov goto out; 303144c0ca21SBoris Burkov } 303244c0ca21SBoris Burkov 303344c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 303444c0ca21SBoris Burkov 303544c0ca21SBoris Burkov if (!fs_info->uuid_root) { 303644c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 303744c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 303844c0ca21SBoris Burkov if (ret) { 303944c0ca21SBoris Burkov btrfs_warn(fs_info, 304044c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 304144c0ca21SBoris Burkov goto out; 304244c0ca21SBoris Burkov } 304344c0ca21SBoris Burkov } 304444c0ca21SBoris Burkov 3045638331faSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 304644c0ca21SBoris Burkov out: 304744c0ca21SBoris Burkov return ret; 304844c0ca21SBoris Burkov } 304944c0ca21SBoris Burkov 3050ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3051ae18c37aSJosef Bacik char *options) 3052ae18c37aSJosef Bacik { 3053ae18c37aSJosef Bacik u32 sectorsize; 3054ae18c37aSJosef Bacik u32 nodesize; 3055ae18c37aSJosef Bacik u32 stripesize; 3056ae18c37aSJosef Bacik u64 generation; 3057ae18c37aSJosef Bacik u64 features; 3058ae18c37aSJosef Bacik u16 csum_type; 3059ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3060ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3061ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3062ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3063ae18c37aSJosef Bacik int ret; 3064ae18c37aSJosef Bacik int err = -EINVAL; 3065ae18c37aSJosef Bacik int level; 3066ae18c37aSJosef Bacik 30678260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 306853b381b3SDavid Woodhouse if (ret) { 306983c8266aSDavid Sterba err = ret; 3070ae18c37aSJosef Bacik goto fail; 307153b381b3SDavid Woodhouse } 307253b381b3SDavid Woodhouse 3073ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3074ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3075ae18c37aSJosef Bacik GFP_KERNEL); 3076ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3077ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3078ae18c37aSJosef Bacik GFP_KERNEL); 3079ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3080ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3081ae18c37aSJosef Bacik err = -ENOMEM; 3082c75e8394SJosef Bacik goto fail; 3083ae18c37aSJosef Bacik } 3084ae18c37aSJosef Bacik 3085ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3086ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3087ae18c37aSJosef Bacik err = -ENOMEM; 3088c75e8394SJosef Bacik goto fail; 3089ae18c37aSJosef Bacik } 3090ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3091ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3092ae18c37aSJosef Bacik 30933c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 30941104a885SDavid Sterba 30951104a885SDavid Sterba /* 30961104a885SDavid Sterba * Read super block and check the signature bytes only 30971104a885SDavid Sterba */ 30988f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 30998f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 31008f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 310116cdcec7SMiao Xie goto fail_alloc; 310220b45077SDave Young } 310339279cc3SChris Mason 31041104a885SDavid Sterba /* 3105260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 31068dc3f22cSJohannes Thumshirn * corrupted, we'll find out 31078dc3f22cSJohannes Thumshirn */ 31088f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 310951bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 31108dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 311151bce6c9SJohannes Thumshirn csum_type); 31128dc3f22cSJohannes Thumshirn err = -EINVAL; 31138f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31148dc3f22cSJohannes Thumshirn goto fail_alloc; 31158dc3f22cSJohannes Thumshirn } 31168dc3f22cSJohannes Thumshirn 31176d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 31186d97c6e3SJohannes Thumshirn if (ret) { 31196d97c6e3SJohannes Thumshirn err = ret; 31208f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31216d97c6e3SJohannes Thumshirn goto fail_alloc; 31226d97c6e3SJohannes Thumshirn } 31236d97c6e3SJohannes Thumshirn 31248dc3f22cSJohannes Thumshirn /* 31251104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 31261104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 31271104a885SDavid Sterba */ 31288f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 312905135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 31301104a885SDavid Sterba err = -EINVAL; 31318f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3132141386e1SJosef Bacik goto fail_alloc; 31331104a885SDavid Sterba } 31341104a885SDavid Sterba 31351104a885SDavid Sterba /* 31361104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 31371104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 31381104a885SDavid Sterba * the whole block of INFO_SIZE 31391104a885SDavid Sterba */ 31408f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 31418f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 31425f39d397SChris Mason 3143fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3144fbc6feaeSNikolay Borisov 3145de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 3146de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 3147de37aa51SNikolay Borisov 31487239ff4bSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) { 3149de37aa51SNikolay Borisov ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid, 3150de37aa51SNikolay Borisov fs_info->super_copy->metadata_uuid, 3151de37aa51SNikolay Borisov BTRFS_FSID_SIZE)); 31527239ff4bSNikolay Borisov } 31530b86a832SChris Mason 3154fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3155fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3156fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3157fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3158fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3159fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3160fbc6feaeSNikolay Borisov } 3161fbc6feaeSNikolay Borisov 3162fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3163fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3164de37aa51SNikolay Borisov 3165069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 31661104a885SDavid Sterba if (ret) { 316705135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 31681104a885SDavid Sterba err = -EINVAL; 3169141386e1SJosef Bacik goto fail_alloc; 31701104a885SDavid Sterba } 31711104a885SDavid Sterba 31720f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3173141386e1SJosef Bacik goto fail_alloc; 31740f7d52f4SChris Mason 3175acce952bSliubo /* check FS state, whether FS is broken. */ 317687533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 317787533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3178acce952bSliubo 317975e7cb7fSLiu Bo /* 318075e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 318175e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 318275e7cb7fSLiu Bo */ 318375e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 318475e7cb7fSLiu Bo 31852ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 31862b82032cSYan Zheng if (ret) { 31872b82032cSYan Zheng err = ret; 3188141386e1SJosef Bacik goto fail_alloc; 31892b82032cSYan Zheng } 3190dfe25020SChris Mason 3191f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3192f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3193f2b636e8SJosef Bacik if (features) { 319405135f59SDavid Sterba btrfs_err(fs_info, 319505135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3196c1c9ff7cSGeert Uytterhoeven features); 3197f2b636e8SJosef Bacik err = -EINVAL; 3198141386e1SJosef Bacik goto fail_alloc; 3199f2b636e8SJosef Bacik } 3200f2b636e8SJosef Bacik 32015d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 32025d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 32030b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3204a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 32055c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 32065c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3207727011e0SChris Mason 32083173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 320905135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 32103173a18fSJosef Bacik 3211727011e0SChris Mason /* 3212727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3213727011e0SChris Mason * they are bigger than the page size 3214727011e0SChris Mason */ 321509cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3216727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 321705135f59SDavid Sterba btrfs_info(fs_info, 321805135f59SDavid Sterba "flagging fs with big metadata feature"); 3219727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3220727011e0SChris Mason } 3221727011e0SChris Mason 3222bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3223bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3224b7f67055SChandan Rajendra stripesize = sectorsize; 3225707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3226963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3227bc3f116fSChris Mason 3228da17066cSJeff Mahoney /* Cache block sizes */ 3229da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3230da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3231ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 323222b6331dSDavid Sterba fs_info->csum_size = btrfs_super_csum_size(disk_super); 3233fe5ecbe8SDavid Sterba fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3234da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3235da17066cSJeff Mahoney 3236bc3f116fSChris Mason /* 3237bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3238bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3239bc3f116fSChris Mason */ 3240bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3241707e8a07SDavid Sterba (sectorsize != nodesize)) { 324205135f59SDavid Sterba btrfs_err(fs_info, 324305135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 324405135f59SDavid Sterba nodesize, sectorsize); 3245141386e1SJosef Bacik goto fail_alloc; 3246bc3f116fSChris Mason } 3247bc3f116fSChris Mason 3248ceda0864SMiao Xie /* 3249ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3250ceda0864SMiao Xie * update the flag. 3251ceda0864SMiao Xie */ 32525d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 32535d4f98a2SYan Zheng 3254f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3255f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3256bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 325705135f59SDavid Sterba btrfs_err(fs_info, 325805135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3259c1c9ff7cSGeert Uytterhoeven features); 3260f2b636e8SJosef Bacik err = -EINVAL; 3261141386e1SJosef Bacik goto fail_alloc; 3262f2b636e8SJosef Bacik } 326361d92c32SChris Mason 32640bb3eb3eSQu Wenruo /* For 4K sector size support, it's only read-only */ 32650bb3eb3eSQu Wenruo if (PAGE_SIZE == SZ_64K && sectorsize == SZ_4K) { 32660bb3eb3eSQu Wenruo if (!sb_rdonly(sb) || btrfs_super_log_root(disk_super)) { 32670bb3eb3eSQu Wenruo btrfs_err(fs_info, 32680bb3eb3eSQu Wenruo "subpage sectorsize %u only supported read-only for page size %lu", 32690bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 32700bb3eb3eSQu Wenruo err = -EINVAL; 32710bb3eb3eSQu Wenruo goto fail_alloc; 32720bb3eb3eSQu Wenruo } 32730bb3eb3eSQu Wenruo } 32740bb3eb3eSQu Wenruo 32752a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 32762a458198SEric Sandeen if (ret) { 32772a458198SEric Sandeen err = ret; 32780dc3b84aSJosef Bacik goto fail_sb_buffer; 32790dc3b84aSJosef Bacik } 32804543df7eSChris Mason 32819e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 32829e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 32834575c9ccSChris Mason 3284a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3285a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3286de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3287db94535dSChris Mason 3288925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 32896bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3290925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 329184eed90fSChris Mason if (ret) { 329205135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 32935d4f98a2SYan Zheng goto fail_sb_buffer; 329484eed90fSChris Mason } 32950b86a832SChris Mason 329684234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3297581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 32980b86a832SChris Mason 32992ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 33000b86a832SChris Mason btrfs_super_chunk_root(disk_super), 33011b7ec85eSJosef Bacik BTRFS_CHUNK_TREE_OBJECTID, 3302581c1760SQu Wenruo generation, level, NULL); 330364c043deSLiu Bo if (IS_ERR(chunk_root->node) || 330464c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 330505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3306e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3307e5fffbacSchandan free_extent_buffer(chunk_root->node); 330895ab1f64SZhao Lei chunk_root->node = NULL; 3309af31f5e5SChris Mason goto fail_tree_roots; 331083121942SDavid Woodhouse } 33115d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 33125d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 33130b86a832SChris Mason 3314e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3315c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3316c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3317e17cade2SChris Mason 33185b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 33192b82032cSYan Zheng if (ret) { 332005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3321af31f5e5SChris Mason goto fail_tree_roots; 33222b82032cSYan Zheng } 33230b86a832SChris Mason 33248dabb742SStefan Behrens /* 3325bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3326bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3327bacce86aSAnand Jain * target is required. So free devices that are not part of this 3328bacce86aSAnand Jain * filesystem but skip the replace traget device which is checked 3329bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 33308dabb742SStefan Behrens */ 3331bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3332a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 333305135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3334a6b0d5c8SChris Mason goto fail_tree_roots; 3335a6b0d5c8SChris Mason } 3336a6b0d5c8SChris Mason 3337b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 33384bbcaa64SEric Sandeen if (ret) 3339b8522a1eSNikolay Borisov goto fail_tree_roots; 33408929ecfaSYan, Zheng 334175ec1db8SJosef Bacik /* 334273651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 334373651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 334473651042SNaohiro Aota * zoned incompat feature flag set. 334573651042SNaohiro Aota */ 334673651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 334773651042SNaohiro Aota if (ret) { 334873651042SNaohiro Aota btrfs_err(fs_info, 334973651042SNaohiro Aota "zoned: failed to read device zone info: %d", 335073651042SNaohiro Aota ret); 335173651042SNaohiro Aota goto fail_block_groups; 335273651042SNaohiro Aota } 335373651042SNaohiro Aota 335473651042SNaohiro Aota /* 335575ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 335675ec1db8SJosef Bacik * check the generation here so we can set the 335775ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 335875ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 335975ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 336075ec1db8SJosef Bacik * even though it was perfectly fine. 336175ec1db8SJosef Bacik */ 336275ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 336375ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 336475ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 336575ec1db8SJosef Bacik 3366cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3367cf90d884SQu Wenruo if (ret) { 3368cf90d884SQu Wenruo btrfs_err(fs_info, 3369cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3370cf90d884SQu Wenruo ret); 3371cf90d884SQu Wenruo goto fail_block_groups; 3372cf90d884SQu Wenruo } 337368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 337468310a5eSIlya Dryomov if (ret) { 337505135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 337668310a5eSIlya Dryomov goto fail_block_groups; 337768310a5eSIlya Dryomov } 337868310a5eSIlya Dryomov 3379733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3380733f4fbbSStefan Behrens if (ret) { 338105135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3382733f4fbbSStefan Behrens goto fail_block_groups; 3383733f4fbbSStefan Behrens } 3384733f4fbbSStefan Behrens 33858dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 33868dabb742SStefan Behrens if (ret) { 338705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 33888dabb742SStefan Behrens goto fail_block_groups; 33898dabb742SStefan Behrens } 33908dabb742SStefan Behrens 3391b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3392b70f5097SNaohiro Aota if (ret) { 3393b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3394b70f5097SNaohiro Aota ret); 3395b70f5097SNaohiro Aota goto fail_block_groups; 3396b70f5097SNaohiro Aota } 3397b70f5097SNaohiro Aota 3398c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3399b7c35e81SAnand Jain if (ret) { 340005135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 340105135f59SDavid Sterba ret); 3402b7c35e81SAnand Jain goto fail_block_groups; 3403b7c35e81SAnand Jain } 3404b7c35e81SAnand Jain 340596f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 34065ac1d209SJeff Mahoney if (ret) { 340705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3408b7c35e81SAnand Jain goto fail_fsdev_sysfs; 34095ac1d209SJeff Mahoney } 34105ac1d209SJeff Mahoney 3411c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3412c59021f8Sliubo if (ret) { 341305135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 34142365dd3cSAnand Jain goto fail_sysfs; 3415c59021f8Sliubo } 3416c59021f8Sliubo 34175b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 34181b1d1f66SJosef Bacik if (ret) { 341905135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 34202365dd3cSAnand Jain goto fail_sysfs; 34211b1d1f66SJosef Bacik } 34224330e183SQu Wenruo 34236528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 342405135f59SDavid Sterba btrfs_warn(fs_info, 342552042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 34262365dd3cSAnand Jain goto fail_sysfs; 3427292fd7fcSStefan Behrens } 34289078a3e1SChris Mason 3429a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3430a74a4b97SChris Mason "btrfs-cleaner"); 343157506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 34322365dd3cSAnand Jain goto fail_sysfs; 3433a74a4b97SChris Mason 3434a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3435a74a4b97SChris Mason tree_root, 3436a74a4b97SChris Mason "btrfs-transaction"); 343757506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 34383f157a2fSChris Mason goto fail_cleaner; 3439a74a4b97SChris Mason 3440583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3441c289811cSChris Mason !fs_info->fs_devices->rotating) { 3442583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3443c289811cSChris Mason } 3444c289811cSChris Mason 3445572d9ab7SDavid Sterba /* 344601327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3447572d9ab7SDavid Sterba * not have to wait for transaction commit 3448572d9ab7SDavid Sterba */ 3449572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 34503818aea2SQu Wenruo 345121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 34520b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 34532ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 34540b246afaSJeff Mahoney btrfs_test_opt(fs_info, 345521adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 345621adbd5cSStefan Behrens 1 : 0, 345721adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 345821adbd5cSStefan Behrens if (ret) 345905135f59SDavid Sterba btrfs_warn(fs_info, 346005135f59SDavid Sterba "failed to initialize integrity check module: %d", 346105135f59SDavid Sterba ret); 346221adbd5cSStefan Behrens } 346321adbd5cSStefan Behrens #endif 3464bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3465bcef60f2SArne Jansen if (ret) 3466bcef60f2SArne Jansen goto fail_trans_kthread; 346721adbd5cSStefan Behrens 3468fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3469fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3470fd708b81SJosef Bacik 347196da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 347296da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 34730b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3474e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 347563443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 347679787eaaSJeff Mahoney if (ret) { 347763443bf5SEric Sandeen err = ret; 347828c16cbbSWang Shilong goto fail_qgroup; 3479e556ce2cSYan Zheng } 3480e02119d5SChris Mason } 34811a40e23bSZheng Yan 348256e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 34833140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 34843140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3485f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3486315bf8efSJosef Bacik fs_info->fs_root = NULL; 3487bcef60f2SArne Jansen goto fail_qgroup; 34883140c9a3SDan Carpenter } 3489c289811cSChris Mason 3490bc98a42cSDavid Howells if (sb_rdonly(sb)) 34918cd29088SBoris Burkov goto clear_oneshot; 34922b6ba629SIlya Dryomov 349344c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 34942b6ba629SIlya Dryomov if (ret) { 34956bccf3abSJeff Mahoney close_ctree(fs_info); 34962b6ba629SIlya Dryomov return ret; 3497e3acc2a6SJosef Bacik } 3498b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3499b382a324SJan Schmidt 350044c0ca21SBoris Burkov if (fs_info->uuid_root && 350144c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 350244c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 350305135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 350470f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 350570f80175SStefan Behrens if (ret) { 350605135f59SDavid Sterba btrfs_warn(fs_info, 350705135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 35086bccf3abSJeff Mahoney close_ctree(fs_info); 350970f80175SStefan Behrens return ret; 351070f80175SStefan Behrens } 3511f7a81ea4SStefan Behrens } 351294846229SBoris Burkov 3513afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 351447ab2a6cSJosef Bacik 35158cd29088SBoris Burkov clear_oneshot: 35168cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3517ad2b2c80SAl Viro return 0; 351839279cc3SChris Mason 3519bcef60f2SArne Jansen fail_qgroup: 3520bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 35217c2ca468SChris Mason fail_trans_kthread: 35227c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 35232ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3524faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 35253f157a2fSChris Mason fail_cleaner: 3526a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 35277c2ca468SChris Mason 35287c2ca468SChris Mason /* 35297c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 35307c2ca468SChris Mason * kthreads 35317c2ca468SChris Mason */ 35327c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 35337c2ca468SChris Mason 35342365dd3cSAnand Jain fail_sysfs: 35356618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 35362365dd3cSAnand Jain 3537b7c35e81SAnand Jain fail_fsdev_sysfs: 3538b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3539b7c35e81SAnand Jain 35401b1d1f66SJosef Bacik fail_block_groups: 354154067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3542af31f5e5SChris Mason 3543af31f5e5SChris Mason fail_tree_roots: 35449e3aa805SJosef Bacik if (fs_info->data_reloc_root) 35459e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 35464273eaffSAnand Jain free_root_pointers(fs_info, true); 35472b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3548af31f5e5SChris Mason 354939279cc3SChris Mason fail_sb_buffer: 35507abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 35515cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 355216cdcec7SMiao Xie fail_alloc: 3553586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3554586e46e2SIlya Dryomov 35554543df7eSChris Mason iput(fs_info->btree_inode); 35567e662854SQinghuang Feng fail: 3557586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3558ad2b2c80SAl Viro return err; 3559eb60ceacSChris Mason } 3560663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3561eb60ceacSChris Mason 3562314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3563f2984462SChris Mason { 3564314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3565314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3566314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3567314b6dd0SJohannes Thumshirn struct page *page; 3568442a4f63SStefan Behrens 3569314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3570314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3571314b6dd0SJohannes Thumshirn 3572314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3573fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3574314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3575314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3576314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3577314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3578314b6dd0SJohannes Thumshirn SetPageError(page); 3579314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3580314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3581314b6dd0SJohannes Thumshirn } else { 3582314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3583f2984462SChris Mason } 3584314b6dd0SJohannes Thumshirn 3585314b6dd0SJohannes Thumshirn put_page(page); 3586314b6dd0SJohannes Thumshirn unlock_page(page); 3587314b6dd0SJohannes Thumshirn } 3588314b6dd0SJohannes Thumshirn 3589314b6dd0SJohannes Thumshirn bio_put(bio); 3590f2984462SChris Mason } 3591f2984462SChris Mason 35928f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 35938f32380dSJohannes Thumshirn int copy_num) 359429c36d72SAnand Jain { 359529c36d72SAnand Jain struct btrfs_super_block *super; 35968f32380dSJohannes Thumshirn struct page *page; 359712659251SNaohiro Aota u64 bytenr, bytenr_orig; 35988f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 359912659251SNaohiro Aota int ret; 360029c36d72SAnand Jain 360112659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 360212659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 360312659251SNaohiro Aota if (ret == -ENOENT) 360412659251SNaohiro Aota return ERR_PTR(-EINVAL); 360512659251SNaohiro Aota else if (ret) 360612659251SNaohiro Aota return ERR_PTR(ret); 360712659251SNaohiro Aota 360829c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 36098f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 361029c36d72SAnand Jain 36118f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 36128f32380dSJohannes Thumshirn if (IS_ERR(page)) 36138f32380dSJohannes Thumshirn return ERR_CAST(page); 361429c36d72SAnand Jain 36158f32380dSJohannes Thumshirn super = page_address(page); 361696c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 361796c2e067SAnand Jain btrfs_release_disk_super(super); 361896c2e067SAnand Jain return ERR_PTR(-ENODATA); 361996c2e067SAnand Jain } 362096c2e067SAnand Jain 362112659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 36228f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 36238f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 362429c36d72SAnand Jain } 362529c36d72SAnand Jain 36268f32380dSJohannes Thumshirn return super; 362729c36d72SAnand Jain } 362829c36d72SAnand Jain 362929c36d72SAnand Jain 36308f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3631a512bbf8SYan Zheng { 36328f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3633a512bbf8SYan Zheng int i; 3634a512bbf8SYan Zheng u64 transid = 0; 3635a512bbf8SYan Zheng 3636a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3637a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3638a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3639a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3640a512bbf8SYan Zheng */ 3641a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 36428f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 36438f32380dSJohannes Thumshirn if (IS_ERR(super)) 3644a512bbf8SYan Zheng continue; 3645a512bbf8SYan Zheng 3646a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 36478f32380dSJohannes Thumshirn if (latest) 36488f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 36498f32380dSJohannes Thumshirn 36508f32380dSJohannes Thumshirn latest = super; 3651a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3652a512bbf8SYan Zheng } 3653a512bbf8SYan Zheng } 365492fc03fbSAnand Jain 36558f32380dSJohannes Thumshirn return super; 3656a512bbf8SYan Zheng } 3657a512bbf8SYan Zheng 36584eedeb75SHisashi Hifumi /* 3659abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3660314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 36614eedeb75SHisashi Hifumi * 3662abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3663abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3664abbb3b8eSDavid Sterba * same for write and wait phases. 36654eedeb75SHisashi Hifumi * 3666314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 36674eedeb75SHisashi Hifumi */ 3668a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3669abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3670a512bbf8SYan Zheng { 3671d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3672314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3673d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3674a512bbf8SYan Zheng int i; 3675a512bbf8SYan Zheng int errors = 0; 367612659251SNaohiro Aota int ret; 367712659251SNaohiro Aota u64 bytenr, bytenr_orig; 3678a512bbf8SYan Zheng 3679a512bbf8SYan Zheng if (max_mirrors == 0) 3680a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3681a512bbf8SYan Zheng 3682d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3683d5178578SJohannes Thumshirn 3684a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3685314b6dd0SJohannes Thumshirn struct page *page; 3686314b6dd0SJohannes Thumshirn struct bio *bio; 3687314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3688314b6dd0SJohannes Thumshirn 368912659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 369012659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 369112659251SNaohiro Aota if (ret == -ENOENT) { 369212659251SNaohiro Aota continue; 369312659251SNaohiro Aota } else if (ret < 0) { 369412659251SNaohiro Aota btrfs_err(device->fs_info, 369512659251SNaohiro Aota "couldn't get super block location for mirror %d", 369612659251SNaohiro Aota i); 369712659251SNaohiro Aota errors++; 369812659251SNaohiro Aota continue; 369912659251SNaohiro Aota } 3700935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3701935e5cc9SMiao Xie device->commit_total_bytes) 3702a512bbf8SYan Zheng break; 3703a512bbf8SYan Zheng 370412659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 3705a512bbf8SYan Zheng 3706fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3707fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 3708fd08001fSEric Biggers sb->csum); 3709a512bbf8SYan Zheng 3710314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3711314b6dd0SJohannes Thumshirn GFP_NOFS); 3712314b6dd0SJohannes Thumshirn if (!page) { 3713fb456252SJeff Mahoney btrfs_err(device->fs_info, 3714314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3715f14d104dSDavid Sterba bytenr); 3716634554dcSJosef Bacik errors++; 3717634554dcSJosef Bacik continue; 3718634554dcSJosef Bacik } 3719634554dcSJosef Bacik 3720314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3721314b6dd0SJohannes Thumshirn get_page(page); 3722a512bbf8SYan Zheng 3723314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3724314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3725a512bbf8SYan Zheng 3726387125fcSChris Mason /* 3727314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3728314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3729314b6dd0SJohannes Thumshirn * checking. 3730387125fcSChris Mason */ 3731314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3732314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3733314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3734314b6dd0SJohannes Thumshirn bio->bi_private = device; 3735314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3736314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3737314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3738314b6dd0SJohannes Thumshirn 3739314b6dd0SJohannes Thumshirn /* 3740314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3741314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3742314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3743314b6dd0SJohannes Thumshirn */ 3744314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 37451b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3746314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3747314b6dd0SJohannes Thumshirn 3748314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 374912659251SNaohiro Aota btrfs_advance_sb_log(device, i); 3750a512bbf8SYan Zheng } 3751a512bbf8SYan Zheng return errors < i ? 0 : -1; 3752a512bbf8SYan Zheng } 3753a512bbf8SYan Zheng 3754387125fcSChris Mason /* 3755abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3756abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3757abbb3b8eSDavid Sterba * 3758314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3759abbb3b8eSDavid Sterba * date. 3760abbb3b8eSDavid Sterba */ 3761abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3762abbb3b8eSDavid Sterba { 3763abbb3b8eSDavid Sterba int i; 3764abbb3b8eSDavid Sterba int errors = 0; 3765b6a535faSHoward McLauchlan bool primary_failed = false; 376612659251SNaohiro Aota int ret; 3767abbb3b8eSDavid Sterba u64 bytenr; 3768abbb3b8eSDavid Sterba 3769abbb3b8eSDavid Sterba if (max_mirrors == 0) 3770abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3771abbb3b8eSDavid Sterba 3772abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3773314b6dd0SJohannes Thumshirn struct page *page; 3774314b6dd0SJohannes Thumshirn 377512659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 377612659251SNaohiro Aota if (ret == -ENOENT) { 377712659251SNaohiro Aota break; 377812659251SNaohiro Aota } else if (ret < 0) { 377912659251SNaohiro Aota errors++; 378012659251SNaohiro Aota if (i == 0) 378112659251SNaohiro Aota primary_failed = true; 378212659251SNaohiro Aota continue; 378312659251SNaohiro Aota } 3784abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3785abbb3b8eSDavid Sterba device->commit_total_bytes) 3786abbb3b8eSDavid Sterba break; 3787abbb3b8eSDavid Sterba 3788314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3789314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3790314b6dd0SJohannes Thumshirn if (!page) { 3791abbb3b8eSDavid Sterba errors++; 3792b6a535faSHoward McLauchlan if (i == 0) 3793b6a535faSHoward McLauchlan primary_failed = true; 3794abbb3b8eSDavid Sterba continue; 3795abbb3b8eSDavid Sterba } 3796314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3797314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3798314b6dd0SJohannes Thumshirn if (PageError(page)) { 3799abbb3b8eSDavid Sterba errors++; 3800b6a535faSHoward McLauchlan if (i == 0) 3801b6a535faSHoward McLauchlan primary_failed = true; 3802b6a535faSHoward McLauchlan } 3803abbb3b8eSDavid Sterba 3804314b6dd0SJohannes Thumshirn /* Drop our reference */ 3805314b6dd0SJohannes Thumshirn put_page(page); 3806abbb3b8eSDavid Sterba 3807314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3808314b6dd0SJohannes Thumshirn put_page(page); 3809abbb3b8eSDavid Sterba } 3810abbb3b8eSDavid Sterba 3811b6a535faSHoward McLauchlan /* log error, force error return */ 3812b6a535faSHoward McLauchlan if (primary_failed) { 3813b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3814b6a535faSHoward McLauchlan device->devid); 3815b6a535faSHoward McLauchlan return -1; 3816b6a535faSHoward McLauchlan } 3817b6a535faSHoward McLauchlan 3818abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3819abbb3b8eSDavid Sterba } 3820abbb3b8eSDavid Sterba 3821abbb3b8eSDavid Sterba /* 3822387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3823387125fcSChris Mason * for the barrier when it is done 3824387125fcSChris Mason */ 38254246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3826387125fcSChris Mason { 3827387125fcSChris Mason complete(bio->bi_private); 3828387125fcSChris Mason } 3829387125fcSChris Mason 3830387125fcSChris Mason /* 38314fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 38324fc6441aSAnand Jain * done in the waiting counterpart. 3833387125fcSChris Mason */ 38344fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3835387125fcSChris Mason { 3836c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3837e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3838387125fcSChris Mason 3839c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 38404fc6441aSAnand Jain return; 3841387125fcSChris Mason 3842e0ae9994SDavid Sterba bio_reset(bio); 3843387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 384474d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 38458d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3846387125fcSChris Mason init_completion(&device->flush_wait); 3847387125fcSChris Mason bio->bi_private = &device->flush_wait; 3848387125fcSChris Mason 384943a01111SLu Fengqi btrfsic_submit_bio(bio); 38501c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 38514fc6441aSAnand Jain } 3852387125fcSChris Mason 38534fc6441aSAnand Jain /* 38544fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 38554fc6441aSAnand Jain */ 38568c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 38574fc6441aSAnand Jain { 38584fc6441aSAnand Jain struct bio *bio = device->flush_bio; 38594fc6441aSAnand Jain 38601c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 386158efbc9fSOmar Sandoval return BLK_STS_OK; 38624fc6441aSAnand Jain 38631c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 38642980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 38654fc6441aSAnand Jain 38668c27cb35SLinus Torvalds return bio->bi_status; 3867387125fcSChris Mason } 3868387125fcSChris Mason 3869d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3870401b41e5SAnand Jain { 38716528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3872401b41e5SAnand Jain return -EIO; 3873387125fcSChris Mason return 0; 3874387125fcSChris Mason } 3875387125fcSChris Mason 3876387125fcSChris Mason /* 3877387125fcSChris Mason * send an empty flush down to each device in parallel, 3878387125fcSChris Mason * then wait for them 3879387125fcSChris Mason */ 3880387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3881387125fcSChris Mason { 3882387125fcSChris Mason struct list_head *head; 3883387125fcSChris Mason struct btrfs_device *dev; 38845af3e8ccSStefan Behrens int errors_wait = 0; 38854e4cbee9SChristoph Hellwig blk_status_t ret; 3886387125fcSChris Mason 38871538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3888387125fcSChris Mason /* send down all the barriers */ 3889387125fcSChris Mason head = &info->fs_devices->devices; 38901538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3891e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3892f88ba6a2SHidetoshi Seto continue; 3893cea7c8bfSAnand Jain if (!dev->bdev) 3894387125fcSChris Mason continue; 3895e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3896ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3897387125fcSChris Mason continue; 3898387125fcSChris Mason 38994fc6441aSAnand Jain write_dev_flush(dev); 390058efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3901387125fcSChris Mason } 3902387125fcSChris Mason 3903387125fcSChris Mason /* wait for all the barriers */ 39041538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3905e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3906f88ba6a2SHidetoshi Seto continue; 3907387125fcSChris Mason if (!dev->bdev) { 39085af3e8ccSStefan Behrens errors_wait++; 3909387125fcSChris Mason continue; 3910387125fcSChris Mason } 3911e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3912ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3913387125fcSChris Mason continue; 3914387125fcSChris Mason 39154fc6441aSAnand Jain ret = wait_dev_flush(dev); 3916401b41e5SAnand Jain if (ret) { 3917401b41e5SAnand Jain dev->last_flush_error = ret; 391866b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 391966b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 39205af3e8ccSStefan Behrens errors_wait++; 3921387125fcSChris Mason } 3922401b41e5SAnand Jain } 3923401b41e5SAnand Jain 3924cea7c8bfSAnand Jain if (errors_wait) { 3925401b41e5SAnand Jain /* 3926401b41e5SAnand Jain * At some point we need the status of all disks 3927401b41e5SAnand Jain * to arrive at the volume status. So error checking 3928401b41e5SAnand Jain * is being pushed to a separate loop. 3929401b41e5SAnand Jain */ 3930d10b82feSQu Wenruo return check_barrier_error(info); 3931401b41e5SAnand Jain } 3932387125fcSChris Mason return 0; 3933387125fcSChris Mason } 3934387125fcSChris Mason 3935943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3936943c6e99SZhao Lei { 39378789f4feSZhao Lei int raid_type; 39388789f4feSZhao Lei int min_tolerated = INT_MAX; 3939943c6e99SZhao Lei 39408789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 39418789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 39428c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 39438789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 39448789f4feSZhao Lei tolerated_failures); 3945943c6e99SZhao Lei 39468789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 39478789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 39488789f4feSZhao Lei continue; 394941a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 39508789f4feSZhao Lei continue; 39518c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 39528789f4feSZhao Lei btrfs_raid_array[raid_type]. 39538789f4feSZhao Lei tolerated_failures); 39548789f4feSZhao Lei } 3955943c6e99SZhao Lei 39568789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3957ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 39588789f4feSZhao Lei min_tolerated = 0; 39598789f4feSZhao Lei } 39608789f4feSZhao Lei 39618789f4feSZhao Lei return min_tolerated; 3962943c6e99SZhao Lei } 3963943c6e99SZhao Lei 3964eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3965f2984462SChris Mason { 3966e5e9a520SChris Mason struct list_head *head; 3967f2984462SChris Mason struct btrfs_device *dev; 3968a061fc8dSChris Mason struct btrfs_super_block *sb; 3969f2984462SChris Mason struct btrfs_dev_item *dev_item; 3970f2984462SChris Mason int ret; 3971f2984462SChris Mason int do_barriers; 3972a236aed1SChris Mason int max_errors; 3973a236aed1SChris Mason int total_errors = 0; 3974a061fc8dSChris Mason u64 flags; 3975f2984462SChris Mason 39760b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3977fed3b381SLiu Bo 3978fed3b381SLiu Bo /* 3979fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3980fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3981fed3b381SLiu Bo * been consistent on disk. 3982fed3b381SLiu Bo */ 3983fed3b381SLiu Bo if (max_mirrors == 0) 39840b246afaSJeff Mahoney backup_super_roots(fs_info); 3985f2984462SChris Mason 39860b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3987a061fc8dSChris Mason dev_item = &sb->dev_item; 3988e5e9a520SChris Mason 39890b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 39900b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 39910b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3992387125fcSChris Mason 39935af3e8ccSStefan Behrens if (do_barriers) { 39940b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 39955af3e8ccSStefan Behrens if (ret) { 39965af3e8ccSStefan Behrens mutex_unlock( 39970b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 39980b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 39995af3e8ccSStefan Behrens "errors while submitting device barriers."); 40005af3e8ccSStefan Behrens return ret; 40015af3e8ccSStefan Behrens } 40025af3e8ccSStefan Behrens } 4003387125fcSChris Mason 40041538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4005dfe25020SChris Mason if (!dev->bdev) { 4006dfe25020SChris Mason total_errors++; 4007dfe25020SChris Mason continue; 4008dfe25020SChris Mason } 4009e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4010ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4011dfe25020SChris Mason continue; 4012dfe25020SChris Mason 40132b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4014a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4015a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 40167df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4017935e5cc9SMiao Xie dev->commit_total_bytes); 4018ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4019ce7213c7SMiao Xie dev->commit_bytes_used); 4020a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4021a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4022a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4023a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 40247239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 40257239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4026a512bbf8SYan Zheng 4027a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4028a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4029f2984462SChris Mason 403075cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 403175cb857dSQu Wenruo if (ret < 0) { 403275cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 403375cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 403475cb857dSQu Wenruo "unexpected superblock corruption detected"); 403575cb857dSQu Wenruo return -EUCLEAN; 403675cb857dSQu Wenruo } 403775cb857dSQu Wenruo 4038abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4039a236aed1SChris Mason if (ret) 4040a236aed1SChris Mason total_errors++; 4041f2984462SChris Mason } 4042a236aed1SChris Mason if (total_errors > max_errors) { 40430b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4044d397712bSChris Mason total_errors); 40450b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 404679787eaaSJeff Mahoney 40479d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 40480b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 40490b246afaSJeff Mahoney "%d errors while writing supers", 40500b246afaSJeff Mahoney total_errors); 40519d565ba4SStefan Behrens return -EIO; 4052a236aed1SChris Mason } 4053f2984462SChris Mason 4054a512bbf8SYan Zheng total_errors = 0; 40551538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4056dfe25020SChris Mason if (!dev->bdev) 4057dfe25020SChris Mason continue; 4058e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4059ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4060dfe25020SChris Mason continue; 4061dfe25020SChris Mason 4062abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4063a512bbf8SYan Zheng if (ret) 40641259ab75SChris Mason total_errors++; 4065f2984462SChris Mason } 40660b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4067a236aed1SChris Mason if (total_errors > max_errors) { 40680b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 40690b246afaSJeff Mahoney "%d errors while writing supers", 40700b246afaSJeff Mahoney total_errors); 407179787eaaSJeff Mahoney return -EIO; 4072a236aed1SChris Mason } 4073f2984462SChris Mason return 0; 4074f2984462SChris Mason } 4075f2984462SChris Mason 4076cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4077cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4078cb517eabSMiao Xie struct btrfs_root *root) 40792619ba1fSChris Mason { 40804785e24fSJosef Bacik bool drop_ref = false; 40814785e24fSJosef Bacik 40824df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 40832619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 40842619ba1fSChris Mason (unsigned long)root->root_key.objectid); 4085af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 40864785e24fSJosef Bacik drop_ref = true; 40874df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 408876dda93cSYan, Zheng 40891c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 4090ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 40911c1ea4f7SLiu Bo if (root->reloc_root) { 409200246528SJosef Bacik btrfs_put_root(root->reloc_root); 40931c1ea4f7SLiu Bo root->reloc_root = NULL; 40941c1ea4f7SLiu Bo } 40951c1ea4f7SLiu Bo } 40963321719eSLiu Bo 40974785e24fSJosef Bacik if (drop_ref) 409800246528SJosef Bacik btrfs_put_root(root); 40992619ba1fSChris Mason } 41002619ba1fSChris Mason 4101c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4102c146afadSYan Zheng { 4103c146afadSYan Zheng u64 root_objectid = 0; 4104c146afadSYan Zheng struct btrfs_root *gang[8]; 410565d33fd7SQu Wenruo int i = 0; 410665d33fd7SQu Wenruo int err = 0; 410765d33fd7SQu Wenruo unsigned int ret = 0; 4108c146afadSYan Zheng 4109c146afadSYan Zheng while (1) { 4110efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4111c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4112c146afadSYan Zheng (void **)gang, root_objectid, 4113c146afadSYan Zheng ARRAY_SIZE(gang)); 411465d33fd7SQu Wenruo if (!ret) { 4115efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4116c146afadSYan Zheng break; 411765d33fd7SQu Wenruo } 41185d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 411966b4ffd1SJosef Bacik 412065d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 412165d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 412265d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 412365d33fd7SQu Wenruo gang[i] = NULL; 412465d33fd7SQu Wenruo continue; 412565d33fd7SQu Wenruo } 412665d33fd7SQu Wenruo /* grab all the search result for later use */ 412700246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 412865d33fd7SQu Wenruo } 4129efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 413065d33fd7SQu Wenruo 413165d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 413265d33fd7SQu Wenruo if (!gang[i]) 413365d33fd7SQu Wenruo continue; 4134c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 413566b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 413666b4ffd1SJosef Bacik if (err) 413765d33fd7SQu Wenruo break; 413800246528SJosef Bacik btrfs_put_root(gang[i]); 4139c146afadSYan Zheng } 4140c146afadSYan Zheng root_objectid++; 4141c146afadSYan Zheng } 414265d33fd7SQu Wenruo 414365d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 414465d33fd7SQu Wenruo for (; i < ret; i++) { 414565d33fd7SQu Wenruo if (gang[i]) 414600246528SJosef Bacik btrfs_put_root(gang[i]); 414765d33fd7SQu Wenruo } 414865d33fd7SQu Wenruo return err; 4149c146afadSYan Zheng } 4150c146afadSYan Zheng 41516bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4152c146afadSYan Zheng { 41536bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4154c146afadSYan Zheng struct btrfs_trans_handle *trans; 4155c146afadSYan Zheng 41560b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 41572ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 41580b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 41590b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4160c71bf099SYan, Zheng 4161c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 41620b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 41630b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4164c71bf099SYan, Zheng 41657a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 41663612b495STsutomu Itoh if (IS_ERR(trans)) 41673612b495STsutomu Itoh return PTR_ERR(trans); 41683a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4169c146afadSYan Zheng } 4170c146afadSYan Zheng 4171b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4172eb60ceacSChris Mason { 4173c146afadSYan Zheng int ret; 4174e089f05cSChris Mason 4175afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4176d6fd0ae2SOmar Sandoval /* 4177d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4178d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4179d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4180d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4181d6fd0ae2SOmar Sandoval */ 4182d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4183a2135011SChris Mason 41847343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4185d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 41867343dd61SJustin Maggard 4187803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4188803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4189803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4190803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4191803b2f54SStefan Behrens 4192837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4193aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4194837d5b6eSIlya Dryomov 41958dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 41968dabb742SStefan Behrens 4197aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 41984cb5300bSChris Mason 41994cb5300bSChris Mason /* wait for any defraggers to finish */ 42004cb5300bSChris Mason wait_event(fs_info->transaction_wait, 42014cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 42024cb5300bSChris Mason 42034cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 420426176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 42054cb5300bSChris Mason 420621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 420757056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4208576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 420921c7e756SMiao Xie 4210b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4211b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4212b0643e59SDennis Zhou 4213bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4214e44163e1SJeff Mahoney /* 4215d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4216d6fd0ae2SOmar Sandoval * unused block groups. 4217e44163e1SJeff Mahoney */ 42180b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4219e44163e1SJeff Mahoney 4220f0cc2cd7SFilipe Manana /* 4221f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4222f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4223f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4224f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4225f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4226f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4227f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4228f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4229f0cc2cd7SFilipe Manana * This is a very rare case. 4230f0cc2cd7SFilipe Manana */ 4231f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4232f0cc2cd7SFilipe Manana 42336bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4234d397712bSChris Mason if (ret) 423504892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4236c146afadSYan Zheng } 4237ed2ff2cbSChris Mason 4238af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4239af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 42402ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4241acce952bSliubo 4242e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4243e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 42448929ecfaSYan, Zheng 4245e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4246afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4247f25784b3SYan Zheng 42485958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 42495958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 42505958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 42515958253cSQu Wenruo } 42525958253cSQu Wenruo 425304892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4254fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4255bcef60f2SArne Jansen 4256963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 425704892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4258963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4259b0c68f8bSChris Mason } 426031153d81SYan Zheng 42615deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 42624297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 42635deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 42644297ff84SJosef Bacik 42656618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4266b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 42675ac1d209SJeff Mahoney 42681a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 42691a4319ccSLiu Bo 4270de348ee0SWang Shilong /* 4271de348ee0SWang Shilong * we must make sure there is not any read request to 4272de348ee0SWang Shilong * submit after we stopping all workers. 4273de348ee0SWang Shilong */ 4274de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 427596192499SJosef Bacik btrfs_stop_all_workers(fs_info); 427696192499SJosef Bacik 42770a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 42780a31daa4SFilipe Manana ASSERT(list_empty(&fs_info->trans_list)); 42790a31daa4SFilipe Manana 4280afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 42814273eaffSAnand Jain free_root_pointers(fs_info, true); 42828c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 42839ad6b7bcSChris Mason 42844e19443dSJosef Bacik /* 42854e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 42864e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 42874e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 42884e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 42894e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 42904e19443dSJosef Bacik */ 42914e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 42924e19443dSJosef Bacik 429313e6c37bSJosef Bacik iput(fs_info->btree_inode); 4294d6bfde87SChris Mason 429521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 42960b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 42972ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 429821adbd5cSStefan Behrens #endif 429921adbd5cSStefan Behrens 43000b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 430168c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4302eb60ceacSChris Mason } 4303eb60ceacSChris Mason 4304b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4305b9fab919SChris Mason int atomic) 4306ccd467d6SChris Mason { 43071259ab75SChris Mason int ret; 4308727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 43091259ab75SChris Mason 43100b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 43111259ab75SChris Mason if (!ret) 43121259ab75SChris Mason return ret; 43131259ab75SChris Mason 43141259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4315b9fab919SChris Mason parent_transid, atomic); 4316b9fab919SChris Mason if (ret == -EAGAIN) 4317b9fab919SChris Mason return ret; 43181259ab75SChris Mason return !ret; 43195f39d397SChris Mason } 43206702ed49SChris Mason 43215f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 43225f39d397SChris Mason { 43232f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 43245f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4325b9473439SChris Mason int was_dirty; 4326b4ce94deSChris Mason 432706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 432806ea65a3SJosef Bacik /* 432906ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 433052042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 433106ea65a3SJosef Bacik * outside of the sanity tests. 433206ea65a3SJosef Bacik */ 4333b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 433406ea65a3SJosef Bacik return; 433506ea65a3SJosef Bacik #endif 4336b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 43370b246afaSJeff Mahoney if (transid != fs_info->generation) 43385d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 43390b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 43400b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4341e2d84521SMiao Xie if (!was_dirty) 4342104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4343e2d84521SMiao Xie buf->len, 43440b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 43451f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 434669fc6cbbSQu Wenruo /* 434769fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 434869fc6cbbSQu Wenruo * but item data not updated. 434969fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 435069fc6cbbSQu Wenruo */ 435169fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4352cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4353a4f78750SDavid Sterba btrfs_print_leaf(buf); 43541f21ef0aSFilipe Manana ASSERT(0); 43551f21ef0aSFilipe Manana } 43561f21ef0aSFilipe Manana #endif 4357eb60ceacSChris Mason } 4358eb60ceacSChris Mason 43592ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4360b53d3f5dSLiu Bo int flush_delayed) 436135b7e476SChris Mason { 4362188de649SChris Mason /* 4363188de649SChris Mason * looks as though older kernels can get into trouble with 4364188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4365188de649SChris Mason */ 4366e2d84521SMiao Xie int ret; 4367d6bfde87SChris Mason 43686933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4369d6bfde87SChris Mason return; 4370d6bfde87SChris Mason 4371b53d3f5dSLiu Bo if (flush_delayed) 43722ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 437316cdcec7SMiao Xie 4374d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4375d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4376d814a491SEthan Lien fs_info->dirty_metadata_batch); 4377e2d84521SMiao Xie if (ret > 0) { 43780b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 437916cdcec7SMiao Xie } 438016cdcec7SMiao Xie } 438116cdcec7SMiao Xie 43822ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 438316cdcec7SMiao Xie { 43842ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 438535b7e476SChris Mason } 4386b53d3f5dSLiu Bo 43872ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4388b53d3f5dSLiu Bo { 43892ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4390d6bfde87SChris Mason } 43916b80053dSChris Mason 4392581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4393581c1760SQu Wenruo struct btrfs_key *first_key) 43946b80053dSChris Mason { 43955ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4396581c1760SQu Wenruo level, first_key); 43976b80053dSChris Mason } 43980da5468fSChris Mason 43992ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4400acce952bSliubo { 4401fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4402fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4403fe816d0fSNikolay Borisov 44040b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 44052ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 44060b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4407acce952bSliubo 44080b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 44090b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4410acce952bSliubo } 4411acce952bSliubo 4412ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4413ef67963dSJosef Bacik { 4414ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4415ef67963dSJosef Bacik u64 root_objectid = 0; 4416ef67963dSJosef Bacik int ret; 4417ef67963dSJosef Bacik 4418ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4419ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4420ef67963dSJosef Bacik (void **)gang, root_objectid, 4421ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4422ef67963dSJosef Bacik int i; 4423ef67963dSJosef Bacik 4424ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4425ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4426ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4427ef67963dSJosef Bacik 4428ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4429ef67963dSJosef Bacik if (!gang[i]) 4430ef67963dSJosef Bacik continue; 4431ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4432ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4433ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4434ef67963dSJosef Bacik } 4435ef67963dSJosef Bacik root_objectid++; 4436ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4437ef67963dSJosef Bacik } 4438ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4439ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4440ef67963dSJosef Bacik } 4441ef67963dSJosef Bacik 4442143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4443acce952bSliubo { 4444acce952bSliubo struct btrfs_ordered_extent *ordered; 4445acce952bSliubo 4446199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4447779880efSJosef Bacik /* 4448779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4449779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4450779880efSJosef Bacik */ 4451199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4452779880efSJosef Bacik root_extent_list) 4453779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4454199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4455199c2a9cSMiao Xie } 4456199c2a9cSMiao Xie 4457199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4458199c2a9cSMiao Xie { 4459199c2a9cSMiao Xie struct btrfs_root *root; 4460199c2a9cSMiao Xie struct list_head splice; 4461199c2a9cSMiao Xie 4462199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4463199c2a9cSMiao Xie 4464199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4465199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4466199c2a9cSMiao Xie while (!list_empty(&splice)) { 4467199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4468199c2a9cSMiao Xie ordered_root); 44691de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 44701de2cfdeSJosef Bacik &fs_info->ordered_roots); 4471199c2a9cSMiao Xie 44722a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4473199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4474199c2a9cSMiao Xie 44752a85d9caSLiu Bo cond_resched(); 44762a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4477199c2a9cSMiao Xie } 4478199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 447974d5d229SJosef Bacik 448074d5d229SJosef Bacik /* 448174d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 448274d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 448374d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 448474d5d229SJosef Bacik * actually get run and error out properly. 448574d5d229SJosef Bacik */ 448674d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4487acce952bSliubo } 4488acce952bSliubo 448935a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 44902ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4491acce952bSliubo { 4492acce952bSliubo struct rb_node *node; 4493acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4494acce952bSliubo struct btrfs_delayed_ref_node *ref; 4495acce952bSliubo int ret = 0; 4496acce952bSliubo 4497acce952bSliubo delayed_refs = &trans->delayed_refs; 4498acce952bSliubo 4499acce952bSliubo spin_lock(&delayed_refs->lock); 4500d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4501cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4502b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4503acce952bSliubo return ret; 4504acce952bSliubo } 4505acce952bSliubo 45065c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4507d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 45080e0adbcfSJosef Bacik struct rb_node *n; 4509e78417d1SJosef Bacik bool pin_bytes = false; 4510acce952bSliubo 4511d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4512d7df2c79SJosef Bacik href_node); 45133069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4514b939d1abSJosef Bacik continue; 45153069bd26SJosef Bacik 4516d7df2c79SJosef Bacik spin_lock(&head->lock); 4517e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 45180e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 45190e0adbcfSJosef Bacik ref_node); 4520d7df2c79SJosef Bacik ref->in_tree = 0; 4521e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 45220e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 45231d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 45241d57ee94SWang Xiaoguang list_del(&ref->add_list); 4525d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4526d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4527d7df2c79SJosef Bacik } 452854067ae9SJosef Bacik if (head->must_insert_reserved) 4529e78417d1SJosef Bacik pin_bytes = true; 453078a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4531fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4532d7df2c79SJosef Bacik spin_unlock(&head->lock); 4533acce952bSliubo spin_unlock(&delayed_refs->lock); 4534e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4535acce952bSliubo 4536f603bb94SNikolay Borisov if (pin_bytes) { 4537f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4538f603bb94SNikolay Borisov 4539f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4540f603bb94SNikolay Borisov BUG_ON(!cache); 4541f603bb94SNikolay Borisov 4542f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4543f603bb94SNikolay Borisov spin_lock(&cache->lock); 4544f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4545f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4546f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4547f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4548f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4549f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4550f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4551f603bb94SNikolay Borisov percpu_counter_add_batch( 4552f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4553f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4554f603bb94SNikolay Borisov 4555f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4556f603bb94SNikolay Borisov 4557f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4558f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4559f603bb94SNikolay Borisov } 456031890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4561d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4562acce952bSliubo cond_resched(); 4563acce952bSliubo spin_lock(&delayed_refs->lock); 4564acce952bSliubo } 456581f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4566acce952bSliubo 4567acce952bSliubo spin_unlock(&delayed_refs->lock); 4568acce952bSliubo 4569acce952bSliubo return ret; 4570acce952bSliubo } 4571acce952bSliubo 4572143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4573acce952bSliubo { 4574acce952bSliubo struct btrfs_inode *btrfs_inode; 4575acce952bSliubo struct list_head splice; 4576acce952bSliubo 4577acce952bSliubo INIT_LIST_HEAD(&splice); 4578acce952bSliubo 4579eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4580eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4581acce952bSliubo 4582acce952bSliubo while (!list_empty(&splice)) { 4583fe816d0fSNikolay Borisov struct inode *inode = NULL; 4584eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4585acce952bSliubo delalloc_inodes); 4586fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4587eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4588acce952bSliubo 4589fe816d0fSNikolay Borisov /* 4590fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4591fe816d0fSNikolay Borisov * meanwhile. 4592fe816d0fSNikolay Borisov */ 4593fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4594fe816d0fSNikolay Borisov if (inode) { 4595fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4596fe816d0fSNikolay Borisov iput(inode); 4597fe816d0fSNikolay Borisov } 4598eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4599acce952bSliubo } 4600eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4601eb73c1b7SMiao Xie } 4602eb73c1b7SMiao Xie 4603eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4604eb73c1b7SMiao Xie { 4605eb73c1b7SMiao Xie struct btrfs_root *root; 4606eb73c1b7SMiao Xie struct list_head splice; 4607eb73c1b7SMiao Xie 4608eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4609eb73c1b7SMiao Xie 4610eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4611eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4612eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4613eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4614eb73c1b7SMiao Xie delalloc_root); 461500246528SJosef Bacik root = btrfs_grab_root(root); 4616eb73c1b7SMiao Xie BUG_ON(!root); 4617eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4618eb73c1b7SMiao Xie 4619eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 462000246528SJosef Bacik btrfs_put_root(root); 4621eb73c1b7SMiao Xie 4622eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4623eb73c1b7SMiao Xie } 4624eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4625acce952bSliubo } 4626acce952bSliubo 46272ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4628acce952bSliubo struct extent_io_tree *dirty_pages, 4629acce952bSliubo int mark) 4630acce952bSliubo { 4631acce952bSliubo int ret; 4632acce952bSliubo struct extent_buffer *eb; 4633acce952bSliubo u64 start = 0; 4634acce952bSliubo u64 end; 4635acce952bSliubo 4636acce952bSliubo while (1) { 4637acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4638e6138876SJosef Bacik mark, NULL); 4639acce952bSliubo if (ret) 4640acce952bSliubo break; 4641acce952bSliubo 464291166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4643acce952bSliubo while (start <= end) { 46440b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 46450b246afaSJeff Mahoney start += fs_info->nodesize; 4646fd8b2b61SJosef Bacik if (!eb) 4647acce952bSliubo continue; 4648fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4649acce952bSliubo 4650fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4651fd8b2b61SJosef Bacik &eb->bflags)) 4652fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4653fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4654acce952bSliubo } 4655acce952bSliubo } 4656acce952bSliubo 4657acce952bSliubo return ret; 4658acce952bSliubo } 4659acce952bSliubo 46602ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4661fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4662acce952bSliubo { 4663acce952bSliubo u64 start; 4664acce952bSliubo u64 end; 4665acce952bSliubo int ret; 4666acce952bSliubo 4667acce952bSliubo while (1) { 46680e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 46690e6ec385SFilipe Manana 4670fcd5e742SLu Fengqi /* 4671fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4672fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4673fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4674fcd5e742SLu Fengqi * the same extent range. 4675fcd5e742SLu Fengqi */ 4676fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4677acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 46780e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4679fcd5e742SLu Fengqi if (ret) { 4680fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4681acce952bSliubo break; 4682fcd5e742SLu Fengqi } 4683acce952bSliubo 46840e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 46850e6ec385SFilipe Manana free_extent_state(cached_state); 46862ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4687fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4688acce952bSliubo cond_resched(); 4689acce952bSliubo } 4690acce952bSliubo 4691acce952bSliubo return 0; 4692acce952bSliubo } 4693acce952bSliubo 469432da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4695c79a1751SLiu Bo { 4696c79a1751SLiu Bo struct inode *inode; 4697c79a1751SLiu Bo 4698c79a1751SLiu Bo inode = cache->io_ctl.inode; 4699c79a1751SLiu Bo if (inode) { 4700c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4701c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4702c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4703c79a1751SLiu Bo iput(inode); 4704c79a1751SLiu Bo } 4705bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 4706c79a1751SLiu Bo btrfs_put_block_group(cache); 4707c79a1751SLiu Bo } 4708c79a1751SLiu Bo 4709c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 47102ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4711c79a1751SLiu Bo { 471232da5386SDavid Sterba struct btrfs_block_group *cache; 4713c79a1751SLiu Bo 4714c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4715c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4716c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 471732da5386SDavid Sterba struct btrfs_block_group, 4718c79a1751SLiu Bo dirty_list); 4719c79a1751SLiu Bo 4720c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4721c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4722c79a1751SLiu Bo list_del_init(&cache->io_list); 4723c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4724c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4725c79a1751SLiu Bo } 4726c79a1751SLiu Bo 4727c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4728c79a1751SLiu Bo spin_lock(&cache->lock); 4729c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4730c79a1751SLiu Bo spin_unlock(&cache->lock); 4731c79a1751SLiu Bo 4732c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4733c79a1751SLiu Bo btrfs_put_block_group(cache); 4734ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4735c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4736c79a1751SLiu Bo } 4737c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4738c79a1751SLiu Bo 473945ae2c18SNikolay Borisov /* 474045ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 474145ae2c18SNikolay Borisov * to use it without any locking 474245ae2c18SNikolay Borisov */ 4743c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4744c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 474532da5386SDavid Sterba struct btrfs_block_group, 4746c79a1751SLiu Bo io_list); 4747c79a1751SLiu Bo 4748c79a1751SLiu Bo list_del_init(&cache->io_list); 4749c79a1751SLiu Bo spin_lock(&cache->lock); 4750c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4751c79a1751SLiu Bo spin_unlock(&cache->lock); 4752c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4753c79a1751SLiu Bo } 4754c79a1751SLiu Bo } 4755c79a1751SLiu Bo 475649b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 47572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 475849b25e05SJeff Mahoney { 4759bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4760bbbf7243SNikolay Borisov 47612ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4762c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4763c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4764c79a1751SLiu Bo 4765bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4766bbbf7243SNikolay Borisov post_commit_list) { 4767bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4768bbbf7243SNikolay Borisov } 4769bbbf7243SNikolay Borisov 47702ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 477149b25e05SJeff Mahoney 47724a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 47730b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 477449b25e05SJeff Mahoney 47754a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 47760b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 477749b25e05SJeff Mahoney 4778ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 477967cde344SMiao Xie 47802ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 478149b25e05SJeff Mahoney EXTENT_DIRTY); 4782fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 478349b25e05SJeff Mahoney 4784d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 4785d3575156SNaohiro Aota 47864a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 47874a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 478849b25e05SJeff Mahoney } 478949b25e05SJeff Mahoney 47902ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4791acce952bSliubo { 4792acce952bSliubo struct btrfs_transaction *t; 4793acce952bSliubo 47940b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4795acce952bSliubo 47960b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 47970b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 47980b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4799724e2315SJosef Bacik struct btrfs_transaction, list); 4800724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 48019b64f57dSElena Reshetova refcount_inc(&t->use_count); 48020b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 48032ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4804724e2315SJosef Bacik btrfs_put_transaction(t); 48050b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4806724e2315SJosef Bacik continue; 4807724e2315SJosef Bacik } 48080b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4809724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 48100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4811724e2315SJosef Bacik /* 4812724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4813724e2315SJosef Bacik * handle open currently for this transaction. 4814724e2315SJosef Bacik */ 4815724e2315SJosef Bacik wait_event(t->writer_wait, 4816724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4817724e2315SJosef Bacik } else { 48180b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4819724e2315SJosef Bacik } 48202ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4821724e2315SJosef Bacik 48220b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 48230b246afaSJeff Mahoney if (t == fs_info->running_transaction) 48240b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4825724e2315SJosef Bacik list_del_init(&t->list); 48260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4827a4abeea4SJosef Bacik 4828724e2315SJosef Bacik btrfs_put_transaction(t); 48292ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 48300b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4831724e2315SJosef Bacik } 48320b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 48330b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4834ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4835ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 48360b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 4837ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 48380b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4839acce952bSliubo 4840acce952bSliubo return 0; 4841acce952bSliubo } 4842ec7d6dfdSNikolay Borisov 4843453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 4844ec7d6dfdSNikolay Borisov { 4845ec7d6dfdSNikolay Borisov struct btrfs_path *path; 4846ec7d6dfdSNikolay Borisov int ret; 4847ec7d6dfdSNikolay Borisov struct extent_buffer *l; 4848ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 4849ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 4850ec7d6dfdSNikolay Borisov int slot; 4851ec7d6dfdSNikolay Borisov 4852ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 4853ec7d6dfdSNikolay Borisov if (!path) 4854ec7d6dfdSNikolay Borisov return -ENOMEM; 4855ec7d6dfdSNikolay Borisov 4856ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 4857ec7d6dfdSNikolay Borisov search_key.type = -1; 4858ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 4859ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 4860ec7d6dfdSNikolay Borisov if (ret < 0) 4861ec7d6dfdSNikolay Borisov goto error; 4862ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 4863ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 4864ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 4865ec7d6dfdSNikolay Borisov l = path->nodes[0]; 4866ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 486723125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 486823125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 4869ec7d6dfdSNikolay Borisov } else { 487023125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 4871ec7d6dfdSNikolay Borisov } 4872ec7d6dfdSNikolay Borisov ret = 0; 4873ec7d6dfdSNikolay Borisov error: 4874ec7d6dfdSNikolay Borisov btrfs_free_path(path); 4875ec7d6dfdSNikolay Borisov return ret; 4876ec7d6dfdSNikolay Borisov } 4877ec7d6dfdSNikolay Borisov 4878543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 4879ec7d6dfdSNikolay Borisov { 4880ec7d6dfdSNikolay Borisov int ret; 4881ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 4882ec7d6dfdSNikolay Borisov 48836b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 4884ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 4885ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 4886ec7d6dfdSNikolay Borisov root->root_key.objectid); 4887ec7d6dfdSNikolay Borisov ret = -ENOSPC; 4888ec7d6dfdSNikolay Borisov goto out; 4889ec7d6dfdSNikolay Borisov } 4890ec7d6dfdSNikolay Borisov 489123125104SNikolay Borisov *objectid = root->free_objectid++; 4892ec7d6dfdSNikolay Borisov ret = 0; 4893ec7d6dfdSNikolay Borisov out: 4894ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 4895ec7d6dfdSNikolay Borisov return ret; 4896ec7d6dfdSNikolay Borisov } 4897