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" 45139e8cd3SQu Wenruo #include "subpage.h" 46eb60ceacSChris Mason 47319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 48319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 49319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 50319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 51e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 52e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 53319e4d06SQu Wenruo 548b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 55143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 56acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 58143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 592ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 60acce952bSliubo struct extent_io_tree *dirty_pages, 61acce952bSliubo int mark); 622ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 63acce952bSliubo struct extent_io_tree *pinned_extents); 642ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 66ce9adaa5SChris Mason 67d352ac68SChris Mason /* 6897eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 6997eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 70d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 71d352ac68SChris Mason */ 7297eb6b69SDavid Sterba struct btrfs_end_io_wq { 73ce9adaa5SChris Mason struct bio *bio; 74ce9adaa5SChris Mason bio_end_io_t *end_io; 75ce9adaa5SChris Mason void *private; 76ce9adaa5SChris Mason struct btrfs_fs_info *info; 774e4cbee9SChristoph Hellwig blk_status_t status; 78bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 798b712842SChris Mason struct btrfs_work work; 80ce9adaa5SChris Mason }; 810da5468fSChris Mason 8297eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 8397eb6b69SDavid Sterba 8497eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 8597eb6b69SDavid Sterba { 8697eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 8797eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 8897eb6b69SDavid Sterba 0, 89fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 9097eb6b69SDavid Sterba NULL); 9197eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 9297eb6b69SDavid Sterba return -ENOMEM; 9397eb6b69SDavid Sterba return 0; 9497eb6b69SDavid Sterba } 9597eb6b69SDavid Sterba 96e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 9797eb6b69SDavid Sterba { 9897eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 9997eb6b69SDavid Sterba } 10097eb6b69SDavid Sterba 101141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 102141386e1SJosef Bacik { 103141386e1SJosef Bacik if (fs_info->csum_shash) 104141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 105141386e1SJosef Bacik } 106141386e1SJosef Bacik 107d352ac68SChris Mason /* 108d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 109d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 110d352ac68SChris Mason * just before they are sent down the IO stack. 111d352ac68SChris Mason */ 11244b8bd7eSChris Mason struct async_submit_bio { 1138896a08dSQu Wenruo struct inode *inode; 11444b8bd7eSChris Mason struct bio *bio; 115a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 11644b8bd7eSChris Mason int mirror_num; 1171941b64bSQu Wenruo 1181941b64bSQu Wenruo /* Optional parameter for submit_bio_start used by direct io */ 1191941b64bSQu Wenruo u64 dio_file_offset; 1208b712842SChris Mason struct btrfs_work work; 1214e4cbee9SChristoph Hellwig blk_status_t status; 12244b8bd7eSChris Mason }; 12344b8bd7eSChris Mason 12485d4e461SChris Mason /* 12585d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 12685d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 12785d4e461SChris Mason * the level the eb occupies in the tree. 1284008c04aSChris Mason * 12985d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13085d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 13185d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 1324fd786e6SMisono Tomohiro * by btrfs_root->root_key.objectid. This ensures that all special purpose 1334fd786e6SMisono Tomohiro * roots have separate keysets. 1344008c04aSChris Mason * 13585d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 13685d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 13785d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1384008c04aSChris Mason * 13985d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14085d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 14185d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 14285d4e461SChris Mason * 14385d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 14485d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 14585d4e461SChris Mason * needs update as well. 1464008c04aSChris Mason */ 1474008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1484008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1494008c04aSChris Mason # error 1504008c04aSChris Mason # endif 15185d4e461SChris Mason 152ab1405aaSDavid Sterba #define DEFINE_LEVEL(stem, level) \ 153ab1405aaSDavid Sterba .names[level] = "btrfs-" stem "-0" #level, 154ab1405aaSDavid Sterba 155ab1405aaSDavid Sterba #define DEFINE_NAME(stem) \ 156ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 0) \ 157ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 1) \ 158ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 2) \ 159ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 3) \ 160ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 4) \ 161ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 5) \ 162ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 6) \ 163ab1405aaSDavid Sterba DEFINE_LEVEL(stem, 7) 164ab1405aaSDavid Sterba 16585d4e461SChris Mason static struct btrfs_lockdep_keyset { 16685d4e461SChris Mason u64 id; /* root objectid */ 167ab1405aaSDavid Sterba /* Longest entry: btrfs-free-space-00 */ 168387824afSDavid Sterba char names[BTRFS_MAX_LEVEL][20]; 169387824afSDavid Sterba struct lock_class_key keys[BTRFS_MAX_LEVEL]; 17085d4e461SChris Mason } btrfs_lockdep_keysets[] = { 171ab1405aaSDavid Sterba { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") }, 172ab1405aaSDavid Sterba { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") }, 173ab1405aaSDavid Sterba { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") }, 174ab1405aaSDavid Sterba { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") }, 175ab1405aaSDavid Sterba { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") }, 176ab1405aaSDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") }, 177ab1405aaSDavid Sterba { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") }, 178ab1405aaSDavid Sterba { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") }, 179ab1405aaSDavid Sterba { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") }, 180ab1405aaSDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") }, 181ab1405aaSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") }, 182ab1405aaSDavid Sterba { .id = 0, DEFINE_NAME("tree") }, 1834008c04aSChris Mason }; 18485d4e461SChris Mason 185ab1405aaSDavid Sterba #undef DEFINE_LEVEL 186ab1405aaSDavid Sterba #undef DEFINE_NAME 18785d4e461SChris Mason 18885d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 18985d4e461SChris Mason int level) 19085d4e461SChris Mason { 19185d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19285d4e461SChris Mason 19385d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 19485d4e461SChris Mason 19585d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 19685d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 19785d4e461SChris Mason if (ks->id == objectid) 19885d4e461SChris Mason break; 19985d4e461SChris Mason 20085d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20185d4e461SChris Mason &ks->keys[level], ks->names[level]); 20285d4e461SChris Mason } 20385d4e461SChris Mason 2044008c04aSChris Mason #endif 2054008c04aSChris Mason 206d352ac68SChris Mason /* 2072996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 208d352ac68SChris Mason */ 209c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 21019c00ddcSChris Mason { 211d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 2127280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 213a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 214d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 21519c00ddcSChris Mason char *kaddr; 216e9be5a30SDavid Sterba int i; 217d5178578SJohannes Thumshirn 218d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 219d5178578SJohannes Thumshirn crypto_shash_init(shash); 220a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 221e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 222a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 22319c00ddcSChris Mason 224e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 225e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 226e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 22719c00ddcSChris Mason } 22871a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 229d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 23019c00ddcSChris Mason } 23119c00ddcSChris Mason 232d352ac68SChris Mason /* 233d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 234d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 235d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 236d352ac68SChris Mason * in the wrong place. 237d352ac68SChris Mason */ 2381259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 239b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 240b9fab919SChris Mason int atomic) 2411259ab75SChris Mason { 2422ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 2431259ab75SChris Mason int ret; 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 2512ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 252ff13db41SDavid Sterba &cached_state); 2530b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2541259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2551259ab75SChris Mason ret = 0; 2561259ab75SChris Mason goto out; 2571259ab75SChris Mason } 25894647322SDavid Sterba btrfs_err_rl(eb->fs_info, 25994647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 26094647322SDavid Sterba eb->start, 26129549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2621259ab75SChris Mason ret = 1; 2630b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 26433958dc6SChris Mason out: 2652ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 266e43bbe5eSDavid Sterba &cached_state); 2671259ab75SChris Mason return ret; 2681259ab75SChris Mason } 2691259ab75SChris Mason 270e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 271e7e16f48SJohannes Thumshirn { 272e7e16f48SJohannes Thumshirn switch (csum_type) { 273e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2743951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2753831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 276352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 277e7e16f48SJohannes Thumshirn return true; 278e7e16f48SJohannes Thumshirn default: 279e7e16f48SJohannes Thumshirn return false; 280e7e16f48SJohannes Thumshirn } 281e7e16f48SJohannes Thumshirn } 282e7e16f48SJohannes Thumshirn 283d352ac68SChris Mason /* 2841104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 2851104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 2861104a885SDavid Sterba */ 287ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 288ab8d0fc4SJeff Mahoney char *raw_disk_sb) 2891104a885SDavid Sterba { 2901104a885SDavid Sterba struct btrfs_super_block *disk_sb = 2911104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 29251bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 293d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 294d5178578SJohannes Thumshirn 295d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 2961104a885SDavid Sterba 2971104a885SDavid Sterba /* 2981104a885SDavid Sterba * The super_block structure does not span the whole 29951bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 30051bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3011104a885SDavid Sterba */ 302fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 303fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3041104a885SDavid Sterba 30555fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 306e7e16f48SJohannes Thumshirn return 1; 3071104a885SDavid Sterba 308e7e16f48SJohannes Thumshirn return 0; 3091104a885SDavid Sterba } 3101104a885SDavid Sterba 311e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 312ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 313581c1760SQu Wenruo { 314e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 315581c1760SQu Wenruo int found_level; 316581c1760SQu Wenruo struct btrfs_key found_key; 317581c1760SQu Wenruo int ret; 318581c1760SQu Wenruo 319581c1760SQu Wenruo found_level = btrfs_header_level(eb); 320581c1760SQu Wenruo if (found_level != level) { 32163489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 32263489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 323581c1760SQu Wenruo btrfs_err(fs_info, 324581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 325581c1760SQu Wenruo eb->start, level, found_level); 326581c1760SQu Wenruo return -EIO; 327581c1760SQu Wenruo } 328581c1760SQu Wenruo 329581c1760SQu Wenruo if (!first_key) 330581c1760SQu Wenruo return 0; 331581c1760SQu Wenruo 3325d41be6fSQu Wenruo /* 3335d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3345d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3355d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3365d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3375d41be6fSQu Wenruo */ 3385d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3395d41be6fSQu Wenruo return 0; 34062fdaa52SQu Wenruo 34162fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 34262fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 34362fdaa52SQu Wenruo btrfs_err(fs_info, 34462fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 34562fdaa52SQu Wenruo eb->start); 34662fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 34762fdaa52SQu Wenruo return -EUCLEAN; 34862fdaa52SQu Wenruo } 34962fdaa52SQu Wenruo 350581c1760SQu Wenruo if (found_level) 351581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 352581c1760SQu Wenruo else 353581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 354581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 355581c1760SQu Wenruo 356581c1760SQu Wenruo if (ret) { 35763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 35863489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 359581c1760SQu Wenruo btrfs_err(fs_info, 360ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 361ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 362ff76a864SLiu Bo first_key->type, first_key->offset, 363ff76a864SLiu Bo found_key.objectid, found_key.type, 364ff76a864SLiu Bo found_key.offset); 365581c1760SQu Wenruo } 366581c1760SQu Wenruo return ret; 367581c1760SQu Wenruo } 368581c1760SQu Wenruo 3691104a885SDavid Sterba /* 370d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 371d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 372581c1760SQu Wenruo * 373581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 374581c1760SQu Wenruo * @level: expected level, mandatory check 375581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 376d352ac68SChris Mason */ 3776a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb, 378581c1760SQu Wenruo u64 parent_transid, int level, 379581c1760SQu Wenruo struct btrfs_key *first_key) 380f188591eSChris Mason { 3815ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 382f188591eSChris Mason struct extent_io_tree *io_tree; 383ea466794SJosef Bacik int failed = 0; 384f188591eSChris Mason int ret; 385f188591eSChris Mason int num_copies = 0; 386f188591eSChris Mason int mirror_num = 0; 387ea466794SJosef Bacik int failed_mirror = 0; 388f188591eSChris Mason 3890b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 390f188591eSChris Mason while (1) { 391f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 392c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 393256dd1bbSStefan Behrens if (!ret) { 394581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 395b9fab919SChris Mason parent_transid, 0)) 396256dd1bbSStefan Behrens ret = -EIO; 397e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 398ff76a864SLiu Bo first_key, parent_transid)) 399581c1760SQu Wenruo ret = -EUCLEAN; 400581c1760SQu Wenruo else 401581c1760SQu Wenruo break; 402256dd1bbSStefan Behrens } 403d397712bSChris Mason 4040b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 405f188591eSChris Mason eb->start, eb->len); 4064235298eSChris Mason if (num_copies == 1) 407ea466794SJosef Bacik break; 4084235298eSChris Mason 4095cf1ab56SJosef Bacik if (!failed_mirror) { 4105cf1ab56SJosef Bacik failed = 1; 4115cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4125cf1ab56SJosef Bacik } 4135cf1ab56SJosef Bacik 414f188591eSChris Mason mirror_num++; 415ea466794SJosef Bacik if (mirror_num == failed_mirror) 416ea466794SJosef Bacik mirror_num++; 417ea466794SJosef Bacik 4184235298eSChris Mason if (mirror_num > num_copies) 419ea466794SJosef Bacik break; 420f188591eSChris Mason } 421ea466794SJosef Bacik 422c0901581SStefan Behrens if (failed && !ret && failed_mirror) 42320a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 424ea466794SJosef Bacik 425ea466794SJosef Bacik return ret; 426f188591eSChris Mason } 42719c00ddcSChris Mason 428eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 429eca0f6f6SQu Wenruo { 430eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 431eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 432eca0f6f6SQu Wenruo int ret; 433eca0f6f6SQu Wenruo 434eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 435eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 436eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 437eca0f6f6SQu Wenruo csum_tree_block(eb, result); 438eca0f6f6SQu Wenruo 439eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 440eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 441eca0f6f6SQu Wenruo else 442eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 443eca0f6f6SQu Wenruo 4443777369fSQu Wenruo if (ret < 0) 4453777369fSQu Wenruo goto error; 4463777369fSQu Wenruo 4473777369fSQu Wenruo /* 4483777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than 4493777369fSQu Wenruo * last committed. Or something seriously wrong happened. 4503777369fSQu Wenruo */ 4513777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { 4523777369fSQu Wenruo ret = -EUCLEAN; 453eca0f6f6SQu Wenruo btrfs_err(fs_info, 4543777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu", 4553777369fSQu Wenruo eb->start, btrfs_header_generation(eb), 4563777369fSQu Wenruo fs_info->last_trans_committed); 4573777369fSQu Wenruo goto error; 458eca0f6f6SQu Wenruo } 459eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 460eca0f6f6SQu Wenruo 461eca0f6f6SQu Wenruo return 0; 4623777369fSQu Wenruo 4633777369fSQu Wenruo error: 4643777369fSQu Wenruo btrfs_print_tree(eb, 0); 4653777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected", 4663777369fSQu Wenruo eb->start); 4673777369fSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 4683777369fSQu Wenruo return ret; 469eca0f6f6SQu Wenruo } 470eca0f6f6SQu Wenruo 471eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 472eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 473eca0f6f6SQu Wenruo struct bio_vec *bvec) 474eca0f6f6SQu Wenruo { 475eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 476eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 477eca0f6f6SQu Wenruo u64 cur; 478eca0f6f6SQu Wenruo int ret = 0; 479eca0f6f6SQu Wenruo 480eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 481eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 482eca0f6f6SQu Wenruo struct extent_buffer *eb; 483eca0f6f6SQu Wenruo bool uptodate; 484eca0f6f6SQu Wenruo 485eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 486eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 487eca0f6f6SQu Wenruo fs_info->nodesize); 488eca0f6f6SQu Wenruo 489eca0f6f6SQu Wenruo /* A dirty eb shouldn't disappear from buffer_radix */ 490eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 491eca0f6f6SQu Wenruo return -EUCLEAN; 492eca0f6f6SQu Wenruo 493eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 494eca0f6f6SQu Wenruo free_extent_buffer(eb); 495eca0f6f6SQu Wenruo return -EUCLEAN; 496eca0f6f6SQu Wenruo } 497eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 498eca0f6f6SQu Wenruo free_extent_buffer(eb); 499eca0f6f6SQu Wenruo return -EUCLEAN; 500eca0f6f6SQu Wenruo } 501eca0f6f6SQu Wenruo 502eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 503eca0f6f6SQu Wenruo free_extent_buffer(eb); 504eca0f6f6SQu Wenruo if (ret < 0) 505eca0f6f6SQu Wenruo return ret; 506eca0f6f6SQu Wenruo } 507eca0f6f6SQu Wenruo return ret; 508eca0f6f6SQu Wenruo } 509eca0f6f6SQu Wenruo 510d352ac68SChris Mason /* 511ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 512ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 513ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 514d352ac68SChris Mason */ 515ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 51619c00ddcSChris Mason { 517ac303b69SQu Wenruo struct page *page = bvec->bv_page; 5184eee4fa4SMiao Xie u64 start = page_offset(page); 51919c00ddcSChris Mason u64 found_start; 52019c00ddcSChris Mason struct extent_buffer *eb; 521eca0f6f6SQu Wenruo 522fbca46ebSQu Wenruo if (fs_info->nodesize < PAGE_SIZE) 523eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 524f188591eSChris Mason 5254f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5264f2de97aSJosef Bacik if (page != eb->pages[0]) 5274f2de97aSJosef Bacik return 0; 5280f805531SAlex Lyakas 52919c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 530d3575156SNaohiro Aota 531d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 532d3575156SNaohiro Aota WARN_ON(found_start != 0); 533d3575156SNaohiro Aota return 0; 534d3575156SNaohiro Aota } 535d3575156SNaohiro Aota 5360f805531SAlex Lyakas /* 5370f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5380f805531SAlex Lyakas * It is useful to know what went wrong. 5390f805531SAlex Lyakas */ 5400f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5410f805531SAlex Lyakas return -EUCLEAN; 5420f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5430f805531SAlex Lyakas return -EUCLEAN; 5440f805531SAlex Lyakas 545eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 54619c00ddcSChris Mason } 54719c00ddcSChris Mason 548b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5492b82032cSYan Zheng { 550b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 551944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 55244880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 553944d3f9fSNikolay Borisov u8 *metadata_uuid; 5542b82032cSYan Zheng 5559a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5569a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5577239ff4bSNikolay Borisov /* 558944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 559944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 560944d3f9fSNikolay Borisov * ->fsid in this case is fine 5617239ff4bSNikolay Borisov */ 562944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5637239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5647239ff4bSNikolay Borisov else 5657239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5667239ff4bSNikolay Borisov 567944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 568944d3f9fSNikolay Borisov return 0; 569944d3f9fSNikolay Borisov 570944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 571944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 572944d3f9fSNikolay Borisov return 0; 573944d3f9fSNikolay Borisov 574944d3f9fSNikolay Borisov return 1; 5752b82032cSYan Zheng } 5762b82032cSYan Zheng 57777bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 57877bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 579ce9adaa5SChris Mason { 58077bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 581ce9adaa5SChris Mason u64 found_start; 58277bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 58377bf40a2SQu Wenruo u8 found_level; 5842996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 585dfd29eedSDavid Sterba const u8 *header_csum; 58677bf40a2SQu Wenruo int ret = 0; 587ea466794SJosef Bacik 588ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 589727011e0SChris Mason if (found_start != eb->start) { 590893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 591893bf4b1SSu Yue eb->start, found_start); 592f188591eSChris Mason ret = -EIO; 59377bf40a2SQu Wenruo goto out; 594ce9adaa5SChris Mason } 595b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 59602873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 59794647322SDavid Sterba eb->start); 5981259ab75SChris Mason ret = -EIO; 59977bf40a2SQu Wenruo goto out; 6001259ab75SChris Mason } 601ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6021c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 603893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 604893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6051c24c3ceSJosef Bacik ret = -EIO; 60677bf40a2SQu Wenruo goto out; 6071c24c3ceSJosef Bacik } 608ce9adaa5SChris Mason 609c67b3892SDavid Sterba csum_tree_block(eb, result); 610dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 611dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 612a826d6dcSJosef Bacik 613dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 6142996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 615ff14aa79SDavid Sterba "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 616ff14aa79SDavid Sterba eb->start, 617dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 61835be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 61935be8851SJohannes Thumshirn btrfs_header_level(eb)); 6202996e1f8SJohannes Thumshirn ret = -EUCLEAN; 62177bf40a2SQu Wenruo goto out; 6222996e1f8SJohannes Thumshirn } 6232996e1f8SJohannes Thumshirn 624a826d6dcSJosef Bacik /* 625a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 626a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 627a826d6dcSJosef Bacik * return -EIO. 628a826d6dcSJosef Bacik */ 6291c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 630a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 631a826d6dcSJosef Bacik ret = -EIO; 632a826d6dcSJosef Bacik } 633ce9adaa5SChris Mason 634813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 635053ab70fSLiu Bo ret = -EIO; 636053ab70fSLiu Bo 6370b32f4bbSJosef Bacik if (!ret) 6380b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 63975391f0dSQu Wenruo else 64075391f0dSQu Wenruo btrfs_err(fs_info, 64175391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 64275391f0dSQu Wenruo eb->start); 64377bf40a2SQu Wenruo out: 64477bf40a2SQu Wenruo return ret; 64577bf40a2SQu Wenruo } 64677bf40a2SQu Wenruo 647371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 648371cdc07SQu Wenruo int mirror) 649371cdc07SQu Wenruo { 650371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 651371cdc07SQu Wenruo struct extent_buffer *eb; 652371cdc07SQu Wenruo bool reads_done; 653371cdc07SQu Wenruo int ret = 0; 654371cdc07SQu Wenruo 655371cdc07SQu Wenruo /* 656371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 657371cdc07SQu Wenruo * only get one eb for each endio hook. 658371cdc07SQu Wenruo */ 659371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 660371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 661371cdc07SQu Wenruo 662371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 663371cdc07SQu Wenruo /* 664371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 665371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 666371cdc07SQu Wenruo */ 667371cdc07SQu Wenruo ASSERT(eb); 668371cdc07SQu Wenruo 669371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 670371cdc07SQu Wenruo /* Subpage read must finish in page read */ 671371cdc07SQu Wenruo ASSERT(reads_done); 672371cdc07SQu Wenruo 673371cdc07SQu Wenruo eb->read_mirror = mirror; 674371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 675371cdc07SQu Wenruo ret = -EIO; 676371cdc07SQu Wenruo goto err; 677371cdc07SQu Wenruo } 678371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 679371cdc07SQu Wenruo if (ret < 0) 680371cdc07SQu Wenruo goto err; 681371cdc07SQu Wenruo 682371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 683371cdc07SQu Wenruo 684371cdc07SQu Wenruo free_extent_buffer(eb); 685371cdc07SQu Wenruo return ret; 686371cdc07SQu Wenruo err: 687371cdc07SQu Wenruo /* 688371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 689371cdc07SQu Wenruo * make sure it has something to decrement. 690371cdc07SQu Wenruo */ 691371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 692371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 693371cdc07SQu Wenruo free_extent_buffer(eb); 694371cdc07SQu Wenruo return ret; 695371cdc07SQu Wenruo } 696371cdc07SQu Wenruo 697c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 69877bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 69977bf40a2SQu Wenruo int mirror) 70077bf40a2SQu Wenruo { 70177bf40a2SQu Wenruo struct extent_buffer *eb; 70277bf40a2SQu Wenruo int ret = 0; 70377bf40a2SQu Wenruo int reads_done; 70477bf40a2SQu Wenruo 70577bf40a2SQu Wenruo ASSERT(page->private); 706371cdc07SQu Wenruo 707fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 708371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 709371cdc07SQu Wenruo 71077bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 71177bf40a2SQu Wenruo 71277bf40a2SQu Wenruo /* 71377bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 71477bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 71577bf40a2SQu Wenruo */ 71677bf40a2SQu Wenruo atomic_inc(&eb->refs); 71777bf40a2SQu Wenruo 71877bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 71977bf40a2SQu Wenruo if (!reads_done) 72077bf40a2SQu Wenruo goto err; 72177bf40a2SQu Wenruo 72277bf40a2SQu Wenruo eb->read_mirror = mirror; 72377bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 72477bf40a2SQu Wenruo ret = -EIO; 72577bf40a2SQu Wenruo goto err; 72677bf40a2SQu Wenruo } 72777bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 728ce9adaa5SChris Mason err: 72953b381b3SDavid Woodhouse if (ret) { 73053b381b3SDavid Woodhouse /* 73153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 73253b381b3SDavid Woodhouse * again, we have to make sure it has something 73353b381b3SDavid Woodhouse * to decrement 73453b381b3SDavid Woodhouse */ 73553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7360b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 73753b381b3SDavid Woodhouse } 7380b32f4bbSJosef Bacik free_extent_buffer(eb); 73977bf40a2SQu Wenruo 740f188591eSChris Mason return ret; 741ce9adaa5SChris Mason } 742ce9adaa5SChris Mason 7434246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 744ce9adaa5SChris Mason { 74597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 746ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7479e0af237SLiu Bo struct btrfs_workqueue *wq; 748ce9adaa5SChris Mason 749ce9adaa5SChris Mason fs_info = end_io_wq->info; 7504e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 751d20f7043SChris Mason 752cfe94440SNaohiro Aota if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 753a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7549e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 755a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7569e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 757a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7589e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 759a0cac0ecSOmar Sandoval else 7609e0af237SLiu Bo wq = fs_info->endio_write_workers; 7619e0af237SLiu Bo } else { 7625c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7639e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 764a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7659e0af237SLiu Bo wq = fs_info->endio_meta_workers; 766a0cac0ecSOmar Sandoval else 7679e0af237SLiu Bo wq = fs_info->endio_workers; 7689e0af237SLiu Bo } 7699e0af237SLiu Bo 770a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7719e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 772ce9adaa5SChris Mason } 773ce9adaa5SChris Mason 7744e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 775bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7760b86a832SChris Mason { 77797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7788b110e39SMiao Xie 77997eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 780ce9adaa5SChris Mason if (!end_io_wq) 7814e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 782ce9adaa5SChris Mason 783ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 784ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 78522c59948SChris Mason end_io_wq->info = info; 7864e4cbee9SChristoph Hellwig end_io_wq->status = 0; 787ce9adaa5SChris Mason end_io_wq->bio = bio; 78822c59948SChris Mason end_io_wq->metadata = metadata; 789ce9adaa5SChris Mason 790ce9adaa5SChris Mason bio->bi_private = end_io_wq; 791ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 79222c59948SChris Mason return 0; 79322c59948SChris Mason } 79422c59948SChris Mason 7954a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7964a69a410SChris Mason { 7974a69a410SChris Mason struct async_submit_bio *async; 7984e4cbee9SChristoph Hellwig blk_status_t ret; 7994a69a410SChris Mason 8004a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8011941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 8021941b64bSQu Wenruo async->dio_file_offset); 80379787eaaSJeff Mahoney if (ret) 8044e4cbee9SChristoph Hellwig async->status = ret; 8054a69a410SChris Mason } 8064a69a410SChris Mason 80706ea01b1SDavid Sterba /* 80806ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 80906ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 81006ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 81106ea01b1SDavid Sterba * 81206ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 81306ea01b1SDavid Sterba * inserted into the tree. 81406ea01b1SDavid Sterba */ 8154a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8168b712842SChris Mason { 8178b712842SChris Mason struct async_submit_bio *async; 81806ea01b1SDavid Sterba struct inode *inode; 81906ea01b1SDavid Sterba blk_status_t ret; 8208b712842SChris Mason 8218b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8228896a08dSQu Wenruo inode = async->inode; 8234854ddd0SChris Mason 824bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 8254e4cbee9SChristoph Hellwig if (async->status) { 8264e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 8274246a0b6SChristoph Hellwig bio_endio(async->bio); 82879787eaaSJeff Mahoney return; 82979787eaaSJeff Mahoney } 83079787eaaSJeff Mahoney 831ec39f769SChris Mason /* 832ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 833ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 834ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 835ec39f769SChris Mason */ 836ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 83708635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 83806ea01b1SDavid Sterba if (ret) { 83906ea01b1SDavid Sterba async->bio->bi_status = ret; 84006ea01b1SDavid Sterba bio_endio(async->bio); 84106ea01b1SDavid Sterba } 8424a69a410SChris Mason } 8434a69a410SChris Mason 8444a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8454a69a410SChris Mason { 8464a69a410SChris Mason struct async_submit_bio *async; 8474a69a410SChris Mason 8484a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8498b712842SChris Mason kfree(async); 8508b712842SChris Mason } 8518b712842SChris Mason 8528896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 853c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 8541941b64bSQu Wenruo u64 dio_file_offset, 855e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 85644b8bd7eSChris Mason { 8578896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 85844b8bd7eSChris Mason struct async_submit_bio *async; 85944b8bd7eSChris Mason 86044b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 86144b8bd7eSChris Mason if (!async) 8624e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 86344b8bd7eSChris Mason 8648896a08dSQu Wenruo async->inode = inode; 86544b8bd7eSChris Mason async->bio = bio; 86644b8bd7eSChris Mason async->mirror_num = mirror_num; 8674a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8684a69a410SChris Mason 869a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 870a0cac0ecSOmar Sandoval run_one_async_free); 8714a69a410SChris Mason 8721941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8738c8bee1dSChris Mason 8744e4cbee9SChristoph Hellwig async->status = 0; 87579787eaaSJeff Mahoney 87667f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8775cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 878d313d7a3SChris Mason 8795cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 88044b8bd7eSChris Mason return 0; 88144b8bd7eSChris Mason } 88244b8bd7eSChris Mason 8834e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 884ce3ed71aSChris Mason { 8852c30c71bSKent Overstreet struct bio_vec *bvec; 886ce3ed71aSChris Mason struct btrfs_root *root; 8872b070cfeSChristoph Hellwig int ret = 0; 8886dc4f100SMing Lei struct bvec_iter_all iter_all; 889ce3ed71aSChris Mason 890c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8912b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 892ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 893ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 89479787eaaSJeff Mahoney if (ret) 89579787eaaSJeff Mahoney break; 896ce3ed71aSChris Mason } 8972c30c71bSKent Overstreet 8984e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 899ce3ed71aSChris Mason } 900ce3ed71aSChris Mason 9018896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 9021941b64bSQu Wenruo u64 dio_file_offset) 90322c59948SChris Mason { 9048b712842SChris Mason /* 9058b712842SChris Mason * when we're called for a write, we're already in the async 9065443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9078b712842SChris Mason */ 90879787eaaSJeff Mahoney return btree_csum_one_bio(bio); 90922c59948SChris Mason } 91022c59948SChris Mason 911f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 9129b4e675aSDavid Sterba struct btrfs_inode *bi) 913de0022b9SJosef Bacik { 9144eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 915f4dcfb30SJohannes Thumshirn return false; 9166300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 917f4dcfb30SJohannes Thumshirn return false; 9189b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 919f4dcfb30SJohannes Thumshirn return false; 920f4dcfb30SJohannes Thumshirn return true; 921de0022b9SJosef Bacik } 922de0022b9SJosef Bacik 9231b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 9241b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 92544b8bd7eSChris Mason { 9260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 9274e4cbee9SChristoph Hellwig blk_status_t ret; 928cad321adSChris Mason 929cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 930cad321adSChris Mason /* 931cad321adSChris Mason * called for a read, do the setup so that checksum validation 932cad321adSChris Mason * can happen in the async kernel threads 933cad321adSChris Mason */ 9340b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9350b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9361d4284bdSChris Mason if (ret) 93761891923SStefan Behrens goto out_w_error; 93808635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 939f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 940de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 941de0022b9SJosef Bacik if (ret) 94261891923SStefan Behrens goto out_w_error; 94308635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 94461891923SStefan Behrens } else { 945cad321adSChris Mason /* 94661891923SStefan Behrens * kthread helpers are used to submit writes so that 94761891923SStefan Behrens * checksumming can happen in parallel across all CPUs 948cad321adSChris Mason */ 9498896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 9508896a08dSQu Wenruo 0, btree_submit_bio_start); 95144b8bd7eSChris Mason } 95244b8bd7eSChris Mason 9534246a0b6SChristoph Hellwig if (ret) 9544246a0b6SChristoph Hellwig goto out_w_error; 9554246a0b6SChristoph Hellwig return 0; 9564246a0b6SChristoph Hellwig 95761891923SStefan Behrens out_w_error: 9584e4cbee9SChristoph Hellwig bio->bi_status = ret; 9594246a0b6SChristoph Hellwig bio_endio(bio); 96061891923SStefan Behrens return ret; 96161891923SStefan Behrens } 96261891923SStefan Behrens 9633dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 964784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 965a6bc32b8SMel Gorman struct page *newpage, struct page *page, 966a6bc32b8SMel Gorman enum migrate_mode mode) 967784b4e29SChris Mason { 968784b4e29SChris Mason /* 969784b4e29SChris Mason * we can't safely write a btree page from here, 970784b4e29SChris Mason * we haven't done the locking hook 971784b4e29SChris Mason */ 972784b4e29SChris Mason if (PageDirty(page)) 973784b4e29SChris Mason return -EAGAIN; 974784b4e29SChris Mason /* 975784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 976784b4e29SChris Mason * We must have no buffers or drop them. 977784b4e29SChris Mason */ 978784b4e29SChris Mason if (page_has_private(page) && 979784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 980784b4e29SChris Mason return -EAGAIN; 981a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 982784b4e29SChris Mason } 9833dd1462eSJan Beulich #endif 984784b4e29SChris Mason 9850da5468fSChris Mason 9860da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9870da5468fSChris Mason struct writeback_control *wbc) 9880da5468fSChris Mason { 989e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 990e2d84521SMiao Xie int ret; 991e2d84521SMiao Xie 992d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 993448d640bSChris Mason 994448d640bSChris Mason if (wbc->for_kupdate) 995448d640bSChris Mason return 0; 996448d640bSChris Mason 997e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 998b9473439SChris Mason /* this is a bit racy, but that's ok */ 999d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1000d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 1001d814a491SEthan Lien fs_info->dirty_metadata_batch); 1002e2d84521SMiao Xie if (ret < 0) 1003793955bcSChris Mason return 0; 1004793955bcSChris Mason } 10050b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 10060da5468fSChris Mason } 10070da5468fSChris Mason 100870dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10095f39d397SChris Mason { 101098509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 101198509cfcSChris Mason return 0; 10120c4e538bSDavid Sterba 1013f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1014d98237b3SChris Mason } 1015d98237b3SChris Mason 1016895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 1017895586ebSMatthew Wilcox (Oracle) size_t length) 1018d98237b3SChris Mason { 1019d1310b2eSChris Mason struct extent_io_tree *tree; 1020895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 1021895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 1022895586ebSMatthew Wilcox (Oracle) btree_releasepage(&folio->page, GFP_NOFS); 1023895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 1024895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 1025895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 1026895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 1027895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 10289ad6b7bcSChris Mason } 1029d98237b3SChris Mason } 1030d98237b3SChris Mason 1031bb146eb2SJosef Bacik #ifdef DEBUG 10320079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 10330079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 10340079c3b1SMatthew Wilcox (Oracle) { 10350079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 1036139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 10370b32f4bbSJosef Bacik struct extent_buffer *eb; 1038139e8cd3SQu Wenruo int cur_bit = 0; 10390079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 10400b32f4bbSJosef Bacik 1041139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 10420079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 10430b32f4bbSJosef Bacik BUG_ON(!eb); 10440b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10450b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 104649d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 10470079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 1048139e8cd3SQu Wenruo } 10490079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 1050139e8cd3SQu Wenruo 1051139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 1052139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 1053139e8cd3SQu Wenruo unsigned long flags; 1054139e8cd3SQu Wenruo u64 cur; 1055139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 1056139e8cd3SQu Wenruo 1057139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 1058139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 1059139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1060139e8cd3SQu Wenruo cur_bit++; 1061139e8cd3SQu Wenruo continue; 1062139e8cd3SQu Wenruo } 1063139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1064139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 1065139e8cd3SQu Wenruo 1066139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 1067139e8cd3SQu Wenruo ASSERT(eb); 1068139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 1069139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 107049d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 1071139e8cd3SQu Wenruo free_extent_buffer(eb); 1072139e8cd3SQu Wenruo 1073139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 1074139e8cd3SQu Wenruo } 10750079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 10760b32f4bbSJosef Bacik } 10770079c3b1SMatthew Wilcox (Oracle) #else 10780079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 10790079c3b1SMatthew Wilcox (Oracle) #endif 10800b32f4bbSJosef Bacik 10817f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10820da5468fSChris Mason .writepages = btree_writepages, 10835f39d397SChris Mason .releasepage = btree_releasepage, 1084895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 10855a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1086784b4e29SChris Mason .migratepage = btree_migratepage, 10875a92bc88SChris Mason #endif 10880079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 1089d98237b3SChris Mason }; 1090123abc88SChris Mason 10912ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10922ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10933fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 10943fbaf258SJosef Bacik int level) 10950999df54SChris Mason { 10960b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10970b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10983fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 10990999df54SChris Mason } 11000999df54SChris Mason 1101581c1760SQu Wenruo /* 1102581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1103581c1760SQu Wenruo * verification. 1104581c1760SQu Wenruo * 11051b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1106581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1107581c1760SQu Wenruo * @level: expected level, mandatory check 1108581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1109581c1760SQu Wenruo */ 11102ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 11111b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 11121b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1113e20d96d6SChris Mason { 11145f39d397SChris Mason struct extent_buffer *buf = NULL; 111519c00ddcSChris Mason int ret; 111619c00ddcSChris Mason 11173fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1118c871b0f2SLiu Bo if (IS_ERR(buf)) 1119c871b0f2SLiu Bo return buf; 1120e4204dedSChris Mason 11216a2e9dc4SFilipe Manana ret = btrfs_read_extent_buffer(buf, parent_transid, level, first_key); 11220f0fe8f7SFilipe David Borba Manana if (ret) { 1123537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 112464c043deSLiu Bo return ERR_PTR(ret); 11250f0fe8f7SFilipe David Borba Manana } 112688c602abSQu Wenruo if (btrfs_check_eb_owner(buf, owner_root)) { 112788c602abSQu Wenruo free_extent_buffer_stale(buf); 112888c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 112988c602abSQu Wenruo } 11305f39d397SChris Mason return buf; 1131ce9adaa5SChris Mason 1132eb60ceacSChris Mason } 1133eb60ceacSChris Mason 11346a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1135ed2ff2cbSChris Mason { 11366a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 113755c69072SChris Mason if (btrfs_header_generation(buf) == 1138e2d84521SMiao Xie fs_info->running_transaction->transid) { 113949d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1140b4ce94deSChris Mason 1141b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1142104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1143e2d84521SMiao Xie -buf->len, 1144e2d84521SMiao Xie fs_info->dirty_metadata_batch); 11450b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1146925baeddSChris Mason } 11475f39d397SChris Mason } 1148ed7b63ebSJosef Bacik } 11495f39d397SChris Mason 1150da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1151e20d96d6SChris Mason u64 objectid) 1152d97e63b6SChris Mason { 11537c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 11542e608bd1SJosef Bacik 11552e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 11562e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 11572e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 115896dfcb46SJosef Bacik root->fs_info = fs_info; 11592e608bd1SJosef Bacik root->root_key.objectid = objectid; 1160cfaa7295SChris Mason root->node = NULL; 1161a28ec197SChris Mason root->commit_root = NULL; 116227cdeb70SMiao Xie root->state = 0; 1163abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 11640b86a832SChris Mason 11650f7d52f4SChris Mason root->last_trans = 0; 11666b8fad57SNikolay Borisov root->free_objectid = 0; 1167eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1168199c2a9cSMiao Xie root->nr_ordered_extents = 0; 11696bef4d31SEric Paris root->inode_tree = RB_ROOT; 117016cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 11712e608bd1SJosef Bacik 11722e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 11730b86a832SChris Mason 11740b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11755d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1176eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1177eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1178199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1179199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1180d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11812ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11822ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11835d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1184eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1185199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1186f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11872ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11882ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11898287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1190a2135011SChris Mason mutex_init(&root->objectid_mutex); 1191e02119d5SChris Mason mutex_init(&root->log_mutex); 119231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1193573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1194c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11957237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11967237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11977237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11988b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11998b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 12007237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12017237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12027237f183SYan Zheng atomic_set(&root->log_writers, 0); 12032ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12040700cea7SElena Reshetova refcount_set(&root->refs, 1); 12058ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1206eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 12077237f183SYan Zheng root->log_transid = 0; 1208d1433debSMiao Xie root->log_transid_committed = -1; 1209257c62e1SChris Mason root->last_log_commit = 0; 12102e608bd1SJosef Bacik root->anon_dev = 0; 1211e289f03eSFilipe Manana if (!dummy) { 121243eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 121343eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1214e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1215e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1216e289f03eSFilipe Manana } 1217017e5369SChris Mason 12185f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1219370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1220bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1221bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1222bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1223bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1224bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1225bd647ce3SJosef Bacik #endif 12263768f368SChris Mason } 12273768f368SChris Mason 122874e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 122996dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12306f07e42eSAl Viro { 123174e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12326f07e42eSAl Viro if (root) 123396dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12346f07e42eSAl Viro return root; 12356f07e42eSAl Viro } 12366f07e42eSAl Viro 123706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 123806ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1239da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 124006ea65a3SJosef Bacik { 124106ea65a3SJosef Bacik struct btrfs_root *root; 124206ea65a3SJosef Bacik 12437c0260eeSJeff Mahoney if (!fs_info) 12447c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12457c0260eeSJeff Mahoney 124696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 124706ea65a3SJosef Bacik if (!root) 124806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1249da17066cSJeff Mahoney 1250b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1251faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 125206ea65a3SJosef Bacik 125306ea65a3SJosef Bacik return root; 125406ea65a3SJosef Bacik } 125506ea65a3SJosef Bacik #endif 125606ea65a3SJosef Bacik 1257abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1258abed4aaaSJosef Bacik { 1259abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1260abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1261abed4aaaSJosef Bacik 1262abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1263abed4aaaSJosef Bacik } 1264abed4aaaSJosef Bacik 1265abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1266abed4aaaSJosef Bacik { 1267abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1268abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1269abed4aaaSJosef Bacik 1270abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1271abed4aaaSJosef Bacik } 1272abed4aaaSJosef Bacik 1273abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1274abed4aaaSJosef Bacik { 1275abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1276abed4aaaSJosef Bacik struct rb_node *tmp; 1277abed4aaaSJosef Bacik 1278abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1279abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1280abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1281abed4aaaSJosef Bacik ASSERT(!tmp); 1282abed4aaaSJosef Bacik 1283abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1284abed4aaaSJosef Bacik } 1285abed4aaaSJosef Bacik 1286abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1287abed4aaaSJosef Bacik { 1288abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1289abed4aaaSJosef Bacik 1290abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1291abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1292abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1293abed4aaaSJosef Bacik } 1294abed4aaaSJosef Bacik 1295abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1296abed4aaaSJosef Bacik struct btrfs_key *key) 1297abed4aaaSJosef Bacik { 1298abed4aaaSJosef Bacik struct rb_node *node; 1299abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1300abed4aaaSJosef Bacik 1301abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1302abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1303abed4aaaSJosef Bacik if (node) 1304abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1305abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1306abed4aaaSJosef Bacik 1307abed4aaaSJosef Bacik return root; 1308abed4aaaSJosef Bacik } 1309abed4aaaSJosef Bacik 1310f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1311f7238e50SJosef Bacik { 1312f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1313f7238e50SJosef Bacik u64 ret; 1314f7238e50SJosef Bacik 1315f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1316f7238e50SJosef Bacik return 0; 1317f7238e50SJosef Bacik 1318f7238e50SJosef Bacik if (bytenr) 1319f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1320f7238e50SJosef Bacik else 1321f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1322f7238e50SJosef Bacik ASSERT(block_group); 1323f7238e50SJosef Bacik if (!block_group) 1324f7238e50SJosef Bacik return 0; 1325f7238e50SJosef Bacik ret = block_group->global_root_id; 1326f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1327f7238e50SJosef Bacik 1328f7238e50SJosef Bacik return ret; 1329f7238e50SJosef Bacik } 1330f7238e50SJosef Bacik 1331abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1332abed4aaaSJosef Bacik { 1333abed4aaaSJosef Bacik struct btrfs_key key = { 1334abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1335abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1336f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1337abed4aaaSJosef Bacik }; 1338abed4aaaSJosef Bacik 1339abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1340abed4aaaSJosef Bacik } 1341abed4aaaSJosef Bacik 1342abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1343abed4aaaSJosef Bacik { 1344abed4aaaSJosef Bacik struct btrfs_key key = { 1345abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1346abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1347f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1348abed4aaaSJosef Bacik }; 1349abed4aaaSJosef Bacik 1350abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1351abed4aaaSJosef Bacik } 1352abed4aaaSJosef Bacik 135320897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 135420897f5cSArne Jansen u64 objectid) 135520897f5cSArne Jansen { 13569b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 135720897f5cSArne Jansen struct extent_buffer *leaf; 135820897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 135920897f5cSArne Jansen struct btrfs_root *root; 136020897f5cSArne Jansen struct btrfs_key key; 1361b89f6d1fSFilipe Manana unsigned int nofs_flag; 136220897f5cSArne Jansen int ret = 0; 136320897f5cSArne Jansen 1364b89f6d1fSFilipe Manana /* 1365b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1366b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1367b89f6d1fSFilipe Manana */ 1368b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 136996dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1370b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 137120897f5cSArne Jansen if (!root) 137220897f5cSArne Jansen return ERR_PTR(-ENOMEM); 137320897f5cSArne Jansen 137420897f5cSArne Jansen root->root_key.objectid = objectid; 137520897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 137620897f5cSArne Jansen root->root_key.offset = 0; 137720897f5cSArne Jansen 13789631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 13799631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 138020897f5cSArne Jansen if (IS_ERR(leaf)) { 138120897f5cSArne Jansen ret = PTR_ERR(leaf); 13821dd05682STsutomu Itoh leaf = NULL; 13838a6a87cdSBoris Burkov goto fail_unlock; 138420897f5cSArne Jansen } 138520897f5cSArne Jansen 138620897f5cSArne Jansen root->node = leaf; 138720897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 138820897f5cSArne Jansen 138920897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 139027cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 139120897f5cSArne Jansen 1392f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1393f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 139420897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 139520897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 139620897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 139720897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 139820897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 139920897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 140020897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 140133d85fdaSQu Wenruo if (is_fstree(objectid)) 1402807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1403807fc790SAndy Shevchenko else 1404807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1405c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 140620897f5cSArne Jansen 14078a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 14088a6a87cdSBoris Burkov 140920897f5cSArne Jansen key.objectid = objectid; 141020897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 141120897f5cSArne Jansen key.offset = 0; 141220897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 141320897f5cSArne Jansen if (ret) 141420897f5cSArne Jansen goto fail; 141520897f5cSArne Jansen 141620897f5cSArne Jansen return root; 14171dd05682STsutomu Itoh 14188a6a87cdSBoris Burkov fail_unlock: 14198c38938cSJosef Bacik if (leaf) 14201dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 14218a6a87cdSBoris Burkov fail: 142200246528SJosef Bacik btrfs_put_root(root); 14231dd05682STsutomu Itoh 14241dd05682STsutomu Itoh return ERR_PTR(ret); 142520897f5cSArne Jansen } 142620897f5cSArne Jansen 14277237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1428e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14290f7d52f4SChris Mason { 14300f7d52f4SChris Mason struct btrfs_root *root; 1431e02119d5SChris Mason 143296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1433e02119d5SChris Mason if (!root) 14347237f183SYan Zheng return ERR_PTR(-ENOMEM); 1435e02119d5SChris Mason 1436e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1437e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1438e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 143927cdeb70SMiao Xie 14406ab6ebb7SNaohiro Aota return root; 14416ab6ebb7SNaohiro Aota } 14426ab6ebb7SNaohiro Aota 14436ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 14446ab6ebb7SNaohiro Aota struct btrfs_root *root) 14456ab6ebb7SNaohiro Aota { 14466ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 14476ab6ebb7SNaohiro Aota 14487237f183SYan Zheng /* 144992a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 145027cdeb70SMiao Xie * 145192a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 145292a7cc42SQu Wenruo * and they go away before a real commit is actually done. 145392a7cc42SQu Wenruo * 145492a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 145592a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 14567237f183SYan Zheng */ 1457e02119d5SChris Mason 14584d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14599631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 14606ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 14616ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1462e02119d5SChris Mason 14637237f183SYan Zheng root->node = leaf; 1464e02119d5SChris Mason 1465e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1466e02119d5SChris Mason btrfs_tree_unlock(root->node); 14676ab6ebb7SNaohiro Aota 14686ab6ebb7SNaohiro Aota return 0; 14697237f183SYan Zheng } 14707237f183SYan Zheng 14717237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14727237f183SYan Zheng struct btrfs_fs_info *fs_info) 14737237f183SYan Zheng { 14747237f183SYan Zheng struct btrfs_root *log_root; 14757237f183SYan Zheng 14767237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14777237f183SYan Zheng if (IS_ERR(log_root)) 14787237f183SYan Zheng return PTR_ERR(log_root); 14796ab6ebb7SNaohiro Aota 14803ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 14813ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 14823ddebf27SNaohiro Aota 14836ab6ebb7SNaohiro Aota if (ret) { 14846ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14856ab6ebb7SNaohiro Aota return ret; 14866ab6ebb7SNaohiro Aota } 14873ddebf27SNaohiro Aota } 14886ab6ebb7SNaohiro Aota 14897237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14907237f183SYan Zheng fs_info->log_root_tree = log_root; 14917237f183SYan Zheng return 0; 14927237f183SYan Zheng } 14937237f183SYan Zheng 14947237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14957237f183SYan Zheng struct btrfs_root *root) 14967237f183SYan Zheng { 14970b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14987237f183SYan Zheng struct btrfs_root *log_root; 14997237f183SYan Zheng struct btrfs_inode_item *inode_item; 15006ab6ebb7SNaohiro Aota int ret; 15017237f183SYan Zheng 15020b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 15037237f183SYan Zheng if (IS_ERR(log_root)) 15047237f183SYan Zheng return PTR_ERR(log_root); 15057237f183SYan Zheng 15066ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 15076ab6ebb7SNaohiro Aota if (ret) { 15086ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 15096ab6ebb7SNaohiro Aota return ret; 15106ab6ebb7SNaohiro Aota } 15116ab6ebb7SNaohiro Aota 15127237f183SYan Zheng log_root->last_trans = trans->transid; 15137237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 15147237f183SYan Zheng 15157237f183SYan Zheng inode_item = &log_root->root_item.inode; 15163cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 15173cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15183cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1519da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15200b246afaSJeff Mahoney fs_info->nodesize); 15213cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15227237f183SYan Zheng 15235d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15247237f183SYan Zheng 15257237f183SYan Zheng WARN_ON(root->log_root); 15267237f183SYan Zheng root->log_root = log_root; 15277237f183SYan Zheng root->log_transid = 0; 1528d1433debSMiao Xie root->log_transid_committed = -1; 1529257c62e1SChris Mason root->last_log_commit = 0; 1530e02119d5SChris Mason return 0; 1531e02119d5SChris Mason } 1532e02119d5SChris Mason 153349d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 153449d11beaSJosef Bacik struct btrfs_path *path, 1535cb517eabSMiao Xie struct btrfs_key *key) 1536e02119d5SChris Mason { 1537e02119d5SChris Mason struct btrfs_root *root; 1538e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 153984234f3aSYan Zheng u64 generation; 1540cb517eabSMiao Xie int ret; 1541581c1760SQu Wenruo int level; 1542cb517eabSMiao Xie 154396dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 154449d11beaSJosef Bacik if (!root) 154549d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 15460f7d52f4SChris Mason 1547cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1548cb517eabSMiao Xie &root->root_item, &root->root_key); 15490f7d52f4SChris Mason if (ret) { 155013a8a7c8SYan, Zheng if (ret > 0) 155113a8a7c8SYan, Zheng ret = -ENOENT; 155249d11beaSJosef Bacik goto fail; 15530f7d52f4SChris Mason } 155413a8a7c8SYan, Zheng 155584234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1556581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 15572ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15582ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 15591b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 156064c043deSLiu Bo if (IS_ERR(root->node)) { 156164c043deSLiu Bo ret = PTR_ERR(root->node); 15628c38938cSJosef Bacik root->node = NULL; 156349d11beaSJosef Bacik goto fail; 15644eb150d6SQu Wenruo } 15654eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1566cb517eabSMiao Xie ret = -EIO; 156749d11beaSJosef Bacik goto fail; 1568416bc658SJosef Bacik } 156988c602abSQu Wenruo 157088c602abSQu Wenruo /* 157188c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 157288c602abSQu Wenruo * match its root node owner 157388c602abSQu Wenruo */ 157488c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 157588c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 157688c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 157788c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 157888c602abSQu Wenruo btrfs_crit(fs_info, 157988c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 158088c602abSQu Wenruo root->root_key.objectid, root->node->start, 158188c602abSQu Wenruo btrfs_header_owner(root->node), 158288c602abSQu Wenruo root->root_key.objectid); 158388c602abSQu Wenruo ret = -EUCLEAN; 158488c602abSQu Wenruo goto fail; 158588c602abSQu Wenruo } 15865d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1587cb517eabSMiao Xie return root; 158849d11beaSJosef Bacik fail: 158900246528SJosef Bacik btrfs_put_root(root); 159049d11beaSJosef Bacik return ERR_PTR(ret); 159149d11beaSJosef Bacik } 159249d11beaSJosef Bacik 159349d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 159449d11beaSJosef Bacik struct btrfs_key *key) 159549d11beaSJosef Bacik { 159649d11beaSJosef Bacik struct btrfs_root *root; 159749d11beaSJosef Bacik struct btrfs_path *path; 159849d11beaSJosef Bacik 159949d11beaSJosef Bacik path = btrfs_alloc_path(); 160049d11beaSJosef Bacik if (!path) 160149d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 160249d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 160349d11beaSJosef Bacik btrfs_free_path(path); 160449d11beaSJosef Bacik 160549d11beaSJosef Bacik return root; 1606cb517eabSMiao Xie } 1607cb517eabSMiao Xie 16082dfb1e43SQu Wenruo /* 16092dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 16102dfb1e43SQu Wenruo * 16112dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 16122dfb1e43SQu Wenruo */ 16132dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1614cb517eabSMiao Xie { 1615cb517eabSMiao Xie int ret; 1616dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1617cb517eabSMiao Xie 1618dcc3eb96SNikolay Borisov /* 1619dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1620dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1621dcc3eb96SNikolay Borisov */ 1622dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1623dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1624dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1625dcc3eb96SNikolay Borisov if (ret) 16268257b2dcSMiao Xie goto fail; 16278257b2dcSMiao Xie 1628aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 162937f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 163092a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1631f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1632f39e4571SJosef Bacik } 1633f39e4571SJosef Bacik 1634851fd730SQu Wenruo /* 1635851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1636851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1637851fd730SQu Wenruo */ 1638851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1639851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 16402dfb1e43SQu Wenruo if (!anon_dev) { 1641cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1642cb517eabSMiao Xie if (ret) 1643876d2cf1SLiu Bo goto fail; 16442dfb1e43SQu Wenruo } else { 16452dfb1e43SQu Wenruo root->anon_dev = anon_dev; 16462dfb1e43SQu Wenruo } 1647851fd730SQu Wenruo } 1648f32e48e9SChandan Rajendra 1649f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1650453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1651f32e48e9SChandan Rajendra if (ret) { 1652f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1653876d2cf1SLiu Bo goto fail; 1654f32e48e9SChandan Rajendra } 1655f32e48e9SChandan Rajendra 16566b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1657f32e48e9SChandan Rajendra 1658f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1659f32e48e9SChandan Rajendra 1660cb517eabSMiao Xie return 0; 1661cb517eabSMiao Xie fail: 166284db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1663cb517eabSMiao Xie return ret; 1664cb517eabSMiao Xie } 1665cb517eabSMiao Xie 1666a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1667cb517eabSMiao Xie u64 root_id) 1668cb517eabSMiao Xie { 1669cb517eabSMiao Xie struct btrfs_root *root; 1670cb517eabSMiao Xie 1671cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1672cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1673cb517eabSMiao Xie (unsigned long)root_id); 1674bc44d7c4SJosef Bacik if (root) 167500246528SJosef Bacik root = btrfs_grab_root(root); 1676cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1677cb517eabSMiao Xie return root; 1678cb517eabSMiao Xie } 1679cb517eabSMiao Xie 168049d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 168149d11beaSJosef Bacik u64 objectid) 168249d11beaSJosef Bacik { 1683abed4aaaSJosef Bacik struct btrfs_key key = { 1684abed4aaaSJosef Bacik .objectid = objectid, 1685abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1686abed4aaaSJosef Bacik .offset = 0, 1687abed4aaaSJosef Bacik }; 1688abed4aaaSJosef Bacik 168949d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 169049d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 169149d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1692abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 169349d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 169449d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 169549d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 169649d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 169749d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1698abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 169949d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 170049d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 170149d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 170249d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 170349d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 170449d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 1705abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1706abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1707abed4aaaSJosef Bacik 1708abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1709abed4aaaSJosef Bacik } 171049d11beaSJosef Bacik return NULL; 171149d11beaSJosef Bacik } 171249d11beaSJosef Bacik 1713cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1714cb517eabSMiao Xie struct btrfs_root *root) 1715cb517eabSMiao Xie { 1716cb517eabSMiao Xie int ret; 1717cb517eabSMiao Xie 1718e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1719cb517eabSMiao Xie if (ret) 1720cb517eabSMiao Xie return ret; 1721cb517eabSMiao Xie 1722cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1723cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1724cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1725cb517eabSMiao Xie root); 1726af01d2e5SJosef Bacik if (ret == 0) { 172700246528SJosef Bacik btrfs_grab_root(root); 172827cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1729af01d2e5SJosef Bacik } 1730cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1731cb517eabSMiao Xie radix_tree_preload_end(); 1732cb517eabSMiao Xie 1733cb517eabSMiao Xie return ret; 1734cb517eabSMiao Xie } 1735cb517eabSMiao Xie 1736bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1737bd647ce3SJosef Bacik { 1738bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1739bd647ce3SJosef Bacik struct btrfs_root *root; 1740bd647ce3SJosef Bacik 1741bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1742457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1743457f1864SJosef Bacik 1744bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1745bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1746457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 174771008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1748bd647ce3SJosef Bacik refcount_read(&root->refs)); 1749bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 175000246528SJosef Bacik btrfs_put_root(root); 175100246528SJosef Bacik btrfs_put_root(root); 1752bd647ce3SJosef Bacik } 1753bd647ce3SJosef Bacik #endif 1754bd647ce3SJosef Bacik } 1755bd647ce3SJosef Bacik 1756abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1757abed4aaaSJosef Bacik { 1758abed4aaaSJosef Bacik struct btrfs_root *root; 1759abed4aaaSJosef Bacik struct rb_node *node; 1760abed4aaaSJosef Bacik 1761abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1762abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1763abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1764abed4aaaSJosef Bacik btrfs_put_root(root); 1765abed4aaaSJosef Bacik } 1766abed4aaaSJosef Bacik } 1767abed4aaaSJosef Bacik 17680d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 17690d4b0463SJosef Bacik { 1770141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1771141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 17725deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1773141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1774141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1775141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1776141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 17770d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 17780d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1779abed4aaaSJosef Bacik free_global_roots(fs_info); 178000246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 178100246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 178200246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 178300246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 178400246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 178500246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1786aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 17879c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1788bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 17893fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 17900d4b0463SJosef Bacik kfree(fs_info->super_copy); 17910d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 17928481dd80SQu Wenruo kfree(fs_info->subpage_info); 17930d4b0463SJosef Bacik kvfree(fs_info); 17940d4b0463SJosef Bacik } 17950d4b0463SJosef Bacik 17960d4b0463SJosef Bacik 17972dfb1e43SQu Wenruo /* 17982dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 17992dfb1e43SQu Wenruo * 18002dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 18012dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 18022dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 18032dfb1e43SQu Wenruo * 18042dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 18052dfb1e43SQu Wenruo * 18062dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 18072dfb1e43SQu Wenruo * same root objectid. 18082dfb1e43SQu Wenruo * 18092dfb1e43SQu Wenruo * @objectid: root id 18102dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 18112dfb1e43SQu Wenruo * pass 0 for new allocation. 18122dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 18132dfb1e43SQu Wenruo * for orphan roots 18142dfb1e43SQu Wenruo */ 18152dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 18162dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 18172dfb1e43SQu Wenruo bool check_ref) 18185eda7b5eSChris Mason { 18195eda7b5eSChris Mason struct btrfs_root *root; 1820381cf658SDavid Sterba struct btrfs_path *path; 18211d4c08e0SDavid Sterba struct btrfs_key key; 18225eda7b5eSChris Mason int ret; 18235eda7b5eSChris Mason 182449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 182549d11beaSJosef Bacik if (root) 182649d11beaSJosef Bacik return root; 18274df27c4dSYan, Zheng again: 182856e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 182948475471SStefan Behrens if (root) { 18302dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 18312dfb1e43SQu Wenruo ASSERT(!anon_dev); 1832bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 183300246528SJosef Bacik btrfs_put_root(root); 183448475471SStefan Behrens return ERR_PTR(-ENOENT); 1835bc44d7c4SJosef Bacik } 18365eda7b5eSChris Mason return root; 183748475471SStefan Behrens } 18385eda7b5eSChris Mason 183956e9357aSDavid Sterba key.objectid = objectid; 184056e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 184156e9357aSDavid Sterba key.offset = (u64)-1; 184256e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 18435eda7b5eSChris Mason if (IS_ERR(root)) 18445eda7b5eSChris Mason return root; 18453394e160SChris Mason 1846c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1847d68fc57bSYan, Zheng ret = -ENOENT; 1848d68fc57bSYan, Zheng goto fail; 1849d68fc57bSYan, Zheng } 1850d68fc57bSYan, Zheng 18512dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1852cb517eabSMiao Xie if (ret) 1853cb517eabSMiao Xie goto fail; 1854cb517eabSMiao Xie 1855381cf658SDavid Sterba path = btrfs_alloc_path(); 1856381cf658SDavid Sterba if (!path) { 1857381cf658SDavid Sterba ret = -ENOMEM; 1858381cf658SDavid Sterba goto fail; 1859381cf658SDavid Sterba } 18601d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 18611d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 186256e9357aSDavid Sterba key.offset = objectid; 18631d4c08e0SDavid Sterba 18641d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1865381cf658SDavid Sterba btrfs_free_path(path); 1866d68fc57bSYan, Zheng if (ret < 0) 1867d68fc57bSYan, Zheng goto fail; 1868d68fc57bSYan, Zheng if (ret == 0) 186927cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1870d68fc57bSYan, Zheng 1871cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 18720f7d52f4SChris Mason if (ret) { 1873168a2f77SJia-Ju Bai if (ret == -EEXIST) { 187400246528SJosef Bacik btrfs_put_root(root); 18754df27c4dSYan, Zheng goto again; 1876168a2f77SJia-Ju Bai } 18774df27c4dSYan, Zheng goto fail; 18784df27c4dSYan, Zheng } 1879edbd8d4eSChris Mason return root; 18804df27c4dSYan, Zheng fail: 188133fab972SFilipe Manana /* 188233fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 188333fab972SFilipe Manana * responsability to free it in case we fail. So we have to set our 188433fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 188533fab972SFilipe Manana * and once again by our caller. 188633fab972SFilipe Manana */ 188733fab972SFilipe Manana if (anon_dev) 188833fab972SFilipe Manana root->anon_dev = 0; 18898c38938cSJosef Bacik btrfs_put_root(root); 18904df27c4dSYan, Zheng return ERR_PTR(ret); 1891edbd8d4eSChris Mason } 1892edbd8d4eSChris Mason 18932dfb1e43SQu Wenruo /* 18942dfb1e43SQu Wenruo * Get in-memory reference of a root structure 18952dfb1e43SQu Wenruo * 18962dfb1e43SQu Wenruo * @objectid: tree objectid 18972dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 18982dfb1e43SQu Wenruo * one reference 18992dfb1e43SQu Wenruo */ 19002dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 19012dfb1e43SQu Wenruo u64 objectid, bool check_ref) 19022dfb1e43SQu Wenruo { 19032dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 19042dfb1e43SQu Wenruo } 19052dfb1e43SQu Wenruo 19062dfb1e43SQu Wenruo /* 19072dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 19082dfb1e43SQu Wenruo * the anonymous block device id 19092dfb1e43SQu Wenruo * 19102dfb1e43SQu Wenruo * @objectid: tree objectid 19112dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 19122dfb1e43SQu Wenruo * parameter value 19132dfb1e43SQu Wenruo */ 19142dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 19152dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 19162dfb1e43SQu Wenruo { 19172dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 19182dfb1e43SQu Wenruo } 19192dfb1e43SQu Wenruo 19208b712842SChris Mason /* 192149d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 192249d11beaSJosef Bacik * @fs_info: the fs_info 192349d11beaSJosef Bacik * @objectid: the objectid we need to lookup 192449d11beaSJosef Bacik * 192549d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 192649d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 192749d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 192849d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 192949d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 193049d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 193149d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 193249d11beaSJosef Bacik * backref code is finished using the root. 193349d11beaSJosef Bacik */ 193449d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 193549d11beaSJosef Bacik struct btrfs_path *path, 193649d11beaSJosef Bacik u64 objectid) 193749d11beaSJosef Bacik { 193849d11beaSJosef Bacik struct btrfs_root *root; 193949d11beaSJosef Bacik struct btrfs_key key; 194049d11beaSJosef Bacik 194149d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 194249d11beaSJosef Bacik 194349d11beaSJosef Bacik /* 194449d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 194549d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 194649d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 194749d11beaSJosef Bacik * != NULL just return it. 194849d11beaSJosef Bacik */ 194949d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 195049d11beaSJosef Bacik if (root) 195149d11beaSJosef Bacik return root; 195249d11beaSJosef Bacik 195349d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 195449d11beaSJosef Bacik if (root) 195549d11beaSJosef Bacik return root; 195649d11beaSJosef Bacik 195749d11beaSJosef Bacik key.objectid = objectid; 195849d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 195949d11beaSJosef Bacik key.offset = (u64)-1; 196049d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 196149d11beaSJosef Bacik btrfs_release_path(path); 196249d11beaSJosef Bacik 196349d11beaSJosef Bacik return root; 196449d11beaSJosef Bacik } 196549d11beaSJosef Bacik 196649d11beaSJosef Bacik /* 19678b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 19688b712842SChris Mason * functions. This is where read checksum verification actually happens 19698b712842SChris Mason */ 19708b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1971ce9adaa5SChris Mason { 1972ce9adaa5SChris Mason struct bio *bio; 197397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1974ce9adaa5SChris Mason 197597eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1976ce9adaa5SChris Mason bio = end_io_wq->bio; 19778b712842SChris Mason 19784e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1979ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1980ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 19814246a0b6SChristoph Hellwig bio_endio(bio); 19829be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1983ce9adaa5SChris Mason } 198444b8bd7eSChris Mason 1985a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1986a74a4b97SChris Mason { 1987*0d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1988d0278245SMiao Xie int again; 1989a74a4b97SChris Mason 1990d6fd0ae2SOmar Sandoval while (1) { 1991d0278245SMiao Xie again = 0; 19929d1a2a3aSDavid Sterba 1993fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1994fd340d0fSJosef Bacik 1995d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 19962ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1997d0278245SMiao Xie goto sleep; 1998d0278245SMiao Xie 199990c711abSZygo Blaxell /* 200090c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 200190c711abSZygo Blaxell * before we have finished mounting the filesystem. 200290c711abSZygo Blaxell */ 20030b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 200490c711abSZygo Blaxell goto sleep; 200590c711abSZygo Blaxell 20060b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 2007d0278245SMiao Xie goto sleep; 2008d0278245SMiao Xie 2009dc7f370cSMiao Xie /* 2010dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 2011dc7f370cSMiao Xie * during the above check and trylock. 2012dc7f370cSMiao Xie */ 20132ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 20140b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 2015dc7f370cSMiao Xie goto sleep; 2016dc7f370cSMiao Xie } 2017dc7f370cSMiao Xie 20182ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 2019c2d6cb16SFilipe Manana 202033c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 20210b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 2022a74a4b97SChris Mason 2023d0278245SMiao Xie /* 202405323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 202505323cd1SMiao Xie * needn't do anything special here. 2026d0278245SMiao Xie */ 20270b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 202867c5e7d4SFilipe Manana 202967c5e7d4SFilipe Manana /* 2030f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 203167c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 203267c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 2033f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 203467c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 203567c5e7d4SFilipe Manana * unused block groups. 203667c5e7d4SFilipe Manana */ 20370b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 203818bb8bbfSJohannes Thumshirn 203918bb8bbfSJohannes Thumshirn /* 204018bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 204118bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 204218bb8bbfSJohannes Thumshirn * space. 204318bb8bbfSJohannes Thumshirn */ 204418bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 2045d0278245SMiao Xie sleep: 2046a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 2047d6fd0ae2SOmar Sandoval if (kthread_should_park()) 2048d6fd0ae2SOmar Sandoval kthread_parkme(); 2049d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 2050d6fd0ae2SOmar Sandoval return 0; 2051838fe188SJiri Kosina if (!again) { 2052a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 2053a74a4b97SChris Mason schedule(); 2054a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 2055a74a4b97SChris Mason } 2056da288d28SFilipe Manana } 2057a74a4b97SChris Mason } 2058a74a4b97SChris Mason 2059a74a4b97SChris Mason static int transaction_kthread(void *arg) 2060a74a4b97SChris Mason { 2061a74a4b97SChris Mason struct btrfs_root *root = arg; 20620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2063a74a4b97SChris Mason struct btrfs_trans_handle *trans; 2064a74a4b97SChris Mason struct btrfs_transaction *cur; 20658929ecfaSYan, Zheng u64 transid; 2066643900beSNikolay Borisov time64_t delta; 2067a74a4b97SChris Mason unsigned long delay; 2068914b2007SJan Kara bool cannot_commit; 2069a74a4b97SChris Mason 2070a74a4b97SChris Mason do { 2071914b2007SJan Kara cannot_commit = false; 2072ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 20730b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 2074a74a4b97SChris Mason 20750b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20760b246afaSJeff Mahoney cur = fs_info->running_transaction; 2077a74a4b97SChris Mason if (!cur) { 20780b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2079a74a4b97SChris Mason goto sleep; 2080a74a4b97SChris Mason } 208131153d81SYan Zheng 2082643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 2083fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 2084fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 2085643900beSNikolay Borisov delta < fs_info->commit_interval) { 20860b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2087fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 2088fb8a7e94SNikolay Borisov delay = min(delay, 2089fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 2090a74a4b97SChris Mason goto sleep; 2091a74a4b97SChris Mason } 20928929ecfaSYan, Zheng transid = cur->transid; 20930b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 209456bec294SChris Mason 209579787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 2096354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 2097914b2007SJan Kara if (IS_ERR(trans)) { 2098354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 2099914b2007SJan Kara cannot_commit = true; 210079787eaaSJeff Mahoney goto sleep; 2101914b2007SJan Kara } 21028929ecfaSYan, Zheng if (transid == trans->transid) { 21033a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 21048929ecfaSYan, Zheng } else { 21053a45bb20SJeff Mahoney btrfs_end_transaction(trans); 21068929ecfaSYan, Zheng } 2107a74a4b97SChris Mason sleep: 21080b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 21090b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 2110a74a4b97SChris Mason 211184961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 21122ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 21138929ecfaSYan, Zheng if (!kthread_should_stop() && 21140b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 2115914b2007SJan Kara cannot_commit)) 2116bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 2117a74a4b97SChris Mason } while (!kthread_should_stop()); 2118a74a4b97SChris Mason return 0; 2119a74a4b97SChris Mason } 2120a74a4b97SChris Mason 2121af31f5e5SChris Mason /* 212201f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 212301f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 212401f0f9daSNikolay Borisov * anything. 2125af31f5e5SChris Mason * 2126af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2127af31f5e5SChris Mason * generation of the latest root in the array with the generation 2128af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2129af31f5e5SChris Mason */ 213001f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2131af31f5e5SChris Mason { 213201f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2133af31f5e5SChris Mason u64 cur; 2134af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2135af31f5e5SChris Mason int i; 2136af31f5e5SChris Mason 2137af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2138af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2139af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2140af31f5e5SChris Mason if (cur == newest_gen) 214101f0f9daSNikolay Borisov return i; 2142af31f5e5SChris Mason } 2143af31f5e5SChris Mason 214401f0f9daSNikolay Borisov return -EINVAL; 2145af31f5e5SChris Mason } 2146af31f5e5SChris Mason 2147af31f5e5SChris Mason /* 2148af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2149af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2150af31f5e5SChris Mason * done 2151af31f5e5SChris Mason */ 2152af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2153af31f5e5SChris Mason { 21546ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2155af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2156af31f5e5SChris Mason 2157af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2158af31f5e5SChris Mason 2159af31f5e5SChris Mason /* 2160af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2161af31f5e5SChris Mason * regardless of which ones we use today 2162af31f5e5SChris Mason */ 2163af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2164af31f5e5SChris Mason 2165af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2166af31f5e5SChris Mason 2167af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2168af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2169af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2170af31f5e5SChris Mason 2171af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2172af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2173af31f5e5SChris Mason 2174af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2175af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2176af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2177af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2178af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2179af31f5e5SChris Mason 21809c54e80dSJosef Bacik if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) { 21819c54e80dSJosef Bacik btrfs_set_backup_block_group_root(root_backup, 21829c54e80dSJosef Bacik info->block_group_root->node->start); 21839c54e80dSJosef Bacik btrfs_set_backup_block_group_root_gen(root_backup, 21849c54e80dSJosef Bacik btrfs_header_generation(info->block_group_root->node)); 21859c54e80dSJosef Bacik btrfs_set_backup_block_group_root_level(root_backup, 21869c54e80dSJosef Bacik btrfs_header_level(info->block_group_root->node)); 21879c54e80dSJosef Bacik } else { 21889c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2189f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 21909c54e80dSJosef Bacik 21919c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 21929c54e80dSJosef Bacik extent_root->node->start); 2193af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 219429cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2195af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 219629cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2197af31f5e5SChris Mason 2198f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2199f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2200f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2201f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2202f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 22039c54e80dSJosef Bacik } 2204af31f5e5SChris Mason 22057c7e82a7SChris Mason /* 22067c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 22077c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 22087c7e82a7SChris Mason */ 22097c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 22107c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 22117c7e82a7SChris Mason info->fs_root->node->start); 2212af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2213af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2214af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2215af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 22167c7e82a7SChris Mason } 2217af31f5e5SChris Mason 2218af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2219af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2220af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2221af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2222af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2223af31f5e5SChris Mason 2224af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2225af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2226af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2227af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2228af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2229af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2230af31f5e5SChris Mason 2231af31f5e5SChris Mason /* 2232af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2233af31f5e5SChris Mason * for the next commit 2234af31f5e5SChris Mason */ 2235af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2236af31f5e5SChris Mason &info->super_for_commit->super_roots, 2237af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2238af31f5e5SChris Mason } 2239af31f5e5SChris Mason 2240af31f5e5SChris Mason /* 2241bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2242bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2243bd2336b2SNikolay Borisov * 2244bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2245bd2336b2SNikolay Borisov * priority - priority of backup root required 2246bd2336b2SNikolay Borisov * 2247bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2248bd2336b2SNikolay Borisov */ 2249bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2250bd2336b2SNikolay Borisov { 2251bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2252bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2253bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2254bd2336b2SNikolay Borisov 2255bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2256bd2336b2SNikolay Borisov if (priority == 0) 2257bd2336b2SNikolay Borisov return backup_index; 2258bd2336b2SNikolay Borisov 2259bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2260bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2261bd2336b2SNikolay Borisov } else { 2262bd2336b2SNikolay Borisov return -EINVAL; 2263bd2336b2SNikolay Borisov } 2264bd2336b2SNikolay Borisov 2265bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2266bd2336b2SNikolay Borisov 2267bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2268bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2269bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2270bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2271bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2272bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2273bd2336b2SNikolay Borisov 2274bd2336b2SNikolay Borisov /* 2275bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2276bd2336b2SNikolay Borisov * need a fsck 2277bd2336b2SNikolay Borisov */ 2278bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2279bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2280bd2336b2SNikolay Borisov 2281bd2336b2SNikolay Borisov return backup_index; 2282bd2336b2SNikolay Borisov } 2283bd2336b2SNikolay Borisov 22847abadb64SLiu Bo /* helper to cleanup workers */ 22857abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 22867abadb64SLiu Bo { 2287dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2288afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 22895cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2290fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2291fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2292d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2293fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2294fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 22955b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2296e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2297a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2298fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2299b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2300b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2301a9b9477dSFilipe Manana /* 2302a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2303a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2304a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2305a9b9477dSFilipe Manana */ 2306a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2307a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 23087abadb64SLiu Bo } 23097abadb64SLiu Bo 23102e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 23112e9f5954SRashika { 23122e9f5954SRashika if (root) { 23132e9f5954SRashika free_extent_buffer(root->node); 23142e9f5954SRashika free_extent_buffer(root->commit_root); 23152e9f5954SRashika root->node = NULL; 23162e9f5954SRashika root->commit_root = NULL; 23172e9f5954SRashika } 23182e9f5954SRashika } 23192e9f5954SRashika 2320abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2321abed4aaaSJosef Bacik { 2322abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2323abed4aaaSJosef Bacik 2324abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2325abed4aaaSJosef Bacik &fs_info->global_root_tree, 2326abed4aaaSJosef Bacik rb_node) 2327abed4aaaSJosef Bacik free_root_extent_buffers(root); 2328abed4aaaSJosef Bacik } 2329abed4aaaSJosef Bacik 2330af31f5e5SChris Mason /* helper to cleanup tree roots */ 23314273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2332af31f5e5SChris Mason { 23332e9f5954SRashika free_root_extent_buffers(info->tree_root); 2334655b09feSJosef Bacik 2335abed4aaaSJosef Bacik free_global_root_pointers(info); 23362e9f5954SRashika free_root_extent_buffers(info->dev_root); 23372e9f5954SRashika free_root_extent_buffers(info->quota_root); 23382e9f5954SRashika free_root_extent_buffers(info->uuid_root); 23398c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2340aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 23419c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 23424273eaffSAnand Jain if (free_chunk_root) 23432e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2344af31f5e5SChris Mason } 2345af31f5e5SChris Mason 23468c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 23478c38938cSJosef Bacik { 23488c38938cSJosef Bacik if (!root) 23498c38938cSJosef Bacik return; 23508c38938cSJosef Bacik 23518c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 23528c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 23531dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 23548c38938cSJosef Bacik if (root->anon_dev) 23558c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 23568c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2357923eb523SJohannes Thumshirn free_root_extent_buffers(root); 23588c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 23598c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 23608c38938cSJosef Bacik list_del_init(&root->leak_list); 23618c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 23628c38938cSJosef Bacik #endif 23638c38938cSJosef Bacik kfree(root); 23648c38938cSJosef Bacik } 23658c38938cSJosef Bacik } 23668c38938cSJosef Bacik 2367faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2368171f6537SJosef Bacik { 2369171f6537SJosef Bacik int ret; 2370171f6537SJosef Bacik struct btrfs_root *gang[8]; 2371171f6537SJosef Bacik int i; 2372171f6537SJosef Bacik 2373171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2374171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2375171f6537SJosef Bacik struct btrfs_root, root_list); 2376171f6537SJosef Bacik list_del(&gang[0]->root_list); 2377171f6537SJosef Bacik 23788c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2379cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 238000246528SJosef Bacik btrfs_put_root(gang[0]); 2381171f6537SJosef Bacik } 2382171f6537SJosef Bacik 2383171f6537SJosef Bacik while (1) { 2384171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2385171f6537SJosef Bacik (void **)gang, 0, 2386171f6537SJosef Bacik ARRAY_SIZE(gang)); 2387171f6537SJosef Bacik if (!ret) 2388171f6537SJosef Bacik break; 2389171f6537SJosef Bacik for (i = 0; i < ret; i++) 2390cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2391171f6537SJosef Bacik } 2392171f6537SJosef Bacik } 2393af31f5e5SChris Mason 2394638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2395638aa7edSEric Sandeen { 2396638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2397638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2398638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2399638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2400638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2401638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2402ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2403638aa7edSEric Sandeen } 2404638aa7edSEric Sandeen 2405779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2406779a65a4SEric Sandeen { 2407779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2408779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2409779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2410779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2411779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2412779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2413907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2414779a65a4SEric Sandeen } 2415779a65a4SEric Sandeen 24166bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2417f37938e0SEric Sandeen { 24182ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 24192ff7e61eSJeff Mahoney 24202ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 24212ff7e61eSJeff Mahoney set_nlink(inode, 1); 2422f37938e0SEric Sandeen /* 2423f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2424f37938e0SEric Sandeen * the real end of the address space is determined by all of 2425f37938e0SEric Sandeen * the devices in the system 2426f37938e0SEric Sandeen */ 24272ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 24282ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2429f37938e0SEric Sandeen 24302ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 243143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 24322c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 24337b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 24342ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2435f37938e0SEric Sandeen 24365c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 24372ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 24382ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 24392ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2440f37938e0SEric Sandeen } 2441f37938e0SEric Sandeen 2442ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2443ad618368SEric Sandeen { 2444ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2445129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 24467f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2447ad618368SEric Sandeen } 2448ad618368SEric Sandeen 2449f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2450f9e92e40SEric Sandeen { 2451f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2452f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2453f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2454f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2455f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2456f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2457d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2458f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2459f9e92e40SEric Sandeen } 2460f9e92e40SEric Sandeen 2461d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 24622a458198SEric Sandeen { 2463f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 24646f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 24652a458198SEric Sandeen 24662a458198SEric Sandeen fs_info->workers = 2467cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2468cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 24692a458198SEric Sandeen 24702a458198SEric Sandeen fs_info->delalloc_workers = 2471cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2472cb001095SJeff Mahoney flags, max_active, 2); 24732a458198SEric Sandeen 24742a458198SEric Sandeen fs_info->flush_workers = 2475cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2476cb001095SJeff Mahoney flags, max_active, 0); 24772a458198SEric Sandeen 24782a458198SEric Sandeen fs_info->caching_workers = 2479cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 24802a458198SEric Sandeen 24812a458198SEric Sandeen fs_info->fixup_workers = 2482cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 24832a458198SEric Sandeen 24842a458198SEric Sandeen /* 24852a458198SEric Sandeen * endios are largely parallel and should have a very 24862a458198SEric Sandeen * low idle thresh 24872a458198SEric Sandeen */ 24882a458198SEric Sandeen fs_info->endio_workers = 2489cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 24902a458198SEric Sandeen fs_info->endio_meta_workers = 2491cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2492cb001095SJeff Mahoney max_active, 4); 24932a458198SEric Sandeen fs_info->endio_meta_write_workers = 2494cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2495cb001095SJeff Mahoney max_active, 2); 24962a458198SEric Sandeen fs_info->endio_raid56_workers = 2497cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2498cb001095SJeff Mahoney max_active, 4); 24992a458198SEric Sandeen fs_info->rmw_workers = 2500cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 25012a458198SEric Sandeen fs_info->endio_write_workers = 2502cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2503cb001095SJeff Mahoney max_active, 2); 25042a458198SEric Sandeen fs_info->endio_freespace_worker = 2505cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2506cb001095SJeff Mahoney max_active, 0); 25072a458198SEric Sandeen fs_info->delayed_workers = 2508cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2509cb001095SJeff Mahoney max_active, 0); 25102a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2511cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2512b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2513b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 25142a458198SEric Sandeen 25152a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2516ba8a9d07SChris Mason fs_info->flush_workers && 25172a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 25182a458198SEric Sandeen fs_info->endio_meta_write_workers && 25192a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 25202a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2521f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2522f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2523b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 25242a458198SEric Sandeen return -ENOMEM; 25252a458198SEric Sandeen } 25262a458198SEric Sandeen 25272a458198SEric Sandeen return 0; 25282a458198SEric Sandeen } 25292a458198SEric Sandeen 25306d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 25316d97c6e3SJohannes Thumshirn { 25326d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2533b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 25346d97c6e3SJohannes Thumshirn 2535b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 25366d97c6e3SJohannes Thumshirn 25376d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 25386d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2539b4e967beSDavid Sterba csum_driver); 25406d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 25416d97c6e3SJohannes Thumshirn } 25426d97c6e3SJohannes Thumshirn 25436d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 25446d97c6e3SJohannes Thumshirn 25456d97c6e3SJohannes Thumshirn return 0; 25466d97c6e3SJohannes Thumshirn } 25476d97c6e3SJohannes Thumshirn 254863443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 254963443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 255063443bf5SEric Sandeen { 255163443bf5SEric Sandeen int ret; 255263443bf5SEric Sandeen struct btrfs_root *log_tree_root; 255363443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 255463443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2555581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 255663443bf5SEric Sandeen 255763443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2558f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 255963443bf5SEric Sandeen return -EIO; 256063443bf5SEric Sandeen } 256163443bf5SEric Sandeen 256296dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 256396dfcb46SJosef Bacik GFP_KERNEL); 256463443bf5SEric Sandeen if (!log_tree_root) 256563443bf5SEric Sandeen return -ENOMEM; 256663443bf5SEric Sandeen 25672ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 25681b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 25691b7ec85eSJosef Bacik fs_info->generation + 1, level, 25701b7ec85eSJosef Bacik NULL); 257164c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2572f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 25730eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 25748c38938cSJosef Bacik log_tree_root->node = NULL; 257500246528SJosef Bacik btrfs_put_root(log_tree_root); 25760eeff236SLiu Bo return ret; 25774eb150d6SQu Wenruo } 25784eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2579f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 258000246528SJosef Bacik btrfs_put_root(log_tree_root); 258163443bf5SEric Sandeen return -EIO; 258263443bf5SEric Sandeen } 25834eb150d6SQu Wenruo 258463443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 258563443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 258663443bf5SEric Sandeen if (ret) { 25870b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 258863443bf5SEric Sandeen "Failed to recover log tree"); 258900246528SJosef Bacik btrfs_put_root(log_tree_root); 259063443bf5SEric Sandeen return ret; 259163443bf5SEric Sandeen } 259263443bf5SEric Sandeen 2593bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 25946bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 259563443bf5SEric Sandeen if (ret) 259663443bf5SEric Sandeen return ret; 259763443bf5SEric Sandeen } 259863443bf5SEric Sandeen 259963443bf5SEric Sandeen return 0; 260063443bf5SEric Sandeen } 260163443bf5SEric Sandeen 2602abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2603abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2604abed4aaaSJosef Bacik const char *name) 2605abed4aaaSJosef Bacik { 2606abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2607abed4aaaSJosef Bacik struct btrfs_root *root; 2608f7238e50SJosef Bacik u64 max_global_id = 0; 2609abed4aaaSJosef Bacik int ret; 2610abed4aaaSJosef Bacik struct btrfs_key key = { 2611abed4aaaSJosef Bacik .objectid = objectid, 2612abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2613abed4aaaSJosef Bacik .offset = 0, 2614abed4aaaSJosef Bacik }; 2615abed4aaaSJosef Bacik bool found = false; 2616abed4aaaSJosef Bacik 2617abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2618abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2619abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2620abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2621abed4aaaSJosef Bacik return 0; 2622abed4aaaSJosef Bacik } 2623abed4aaaSJosef Bacik 2624abed4aaaSJosef Bacik while (1) { 2625abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2626abed4aaaSJosef Bacik if (ret < 0) 2627abed4aaaSJosef Bacik break; 2628abed4aaaSJosef Bacik 2629abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2630abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2631abed4aaaSJosef Bacik if (ret) { 2632abed4aaaSJosef Bacik if (ret > 0) 2633abed4aaaSJosef Bacik ret = 0; 2634abed4aaaSJosef Bacik break; 2635abed4aaaSJosef Bacik } 2636abed4aaaSJosef Bacik } 2637abed4aaaSJosef Bacik ret = 0; 2638abed4aaaSJosef Bacik 2639abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2640abed4aaaSJosef Bacik if (key.objectid != objectid) 2641abed4aaaSJosef Bacik break; 2642abed4aaaSJosef Bacik btrfs_release_path(path); 2643abed4aaaSJosef Bacik 2644f7238e50SJosef Bacik /* 2645f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2646f7238e50SJosef Bacik * everybody. 2647f7238e50SJosef Bacik */ 2648f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2649f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2650f7238e50SJosef Bacik 2651abed4aaaSJosef Bacik found = true; 2652abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2653abed4aaaSJosef Bacik if (IS_ERR(root)) { 2654abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2655abed4aaaSJosef Bacik ret = PTR_ERR(root); 2656abed4aaaSJosef Bacik break; 2657abed4aaaSJosef Bacik } 2658abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2659abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2660abed4aaaSJosef Bacik if (ret) { 2661abed4aaaSJosef Bacik btrfs_put_root(root); 2662abed4aaaSJosef Bacik break; 2663abed4aaaSJosef Bacik } 2664abed4aaaSJosef Bacik key.offset++; 2665abed4aaaSJosef Bacik } 2666abed4aaaSJosef Bacik btrfs_release_path(path); 2667abed4aaaSJosef Bacik 2668f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2669f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2670f7238e50SJosef Bacik 2671abed4aaaSJosef Bacik if (!found || ret) { 2672abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2673abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2674abed4aaaSJosef Bacik 2675abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2676abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2677abed4aaaSJosef Bacik else 2678abed4aaaSJosef Bacik ret = 0; 2679abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2680abed4aaaSJosef Bacik } 2681abed4aaaSJosef Bacik return ret; 2682abed4aaaSJosef Bacik } 2683abed4aaaSJosef Bacik 2684abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2685abed4aaaSJosef Bacik { 2686abed4aaaSJosef Bacik struct btrfs_path *path; 2687abed4aaaSJosef Bacik int ret = 0; 2688abed4aaaSJosef Bacik 2689abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2690abed4aaaSJosef Bacik if (!path) 2691abed4aaaSJosef Bacik return -ENOMEM; 2692abed4aaaSJosef Bacik 2693abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2694abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2695abed4aaaSJosef Bacik if (ret) 2696abed4aaaSJosef Bacik goto out; 2697abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2698abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2699abed4aaaSJosef Bacik if (ret) 2700abed4aaaSJosef Bacik goto out; 2701abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2702abed4aaaSJosef Bacik goto out; 2703abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2704abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2705abed4aaaSJosef Bacik "free space"); 2706abed4aaaSJosef Bacik out: 2707abed4aaaSJosef Bacik btrfs_free_path(path); 2708abed4aaaSJosef Bacik return ret; 2709abed4aaaSJosef Bacik } 2710abed4aaaSJosef Bacik 27116bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 27124bbcaa64SEric Sandeen { 27136bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2714a4f3d2c4SDavid Sterba struct btrfs_root *root; 27154bbcaa64SEric Sandeen struct btrfs_key location; 27164bbcaa64SEric Sandeen int ret; 27174bbcaa64SEric Sandeen 27186bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 27196bccf3abSJeff Mahoney 2720abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2721abed4aaaSJosef Bacik if (ret) 2722abed4aaaSJosef Bacik return ret; 2723abed4aaaSJosef Bacik 2724abed4aaaSJosef Bacik location.objectid = BTRFS_DEV_TREE_OBJECTID; 27254bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 27264bbcaa64SEric Sandeen location.offset = 0; 27274bbcaa64SEric Sandeen 2728a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2729f50f4353SLiu Bo if (IS_ERR(root)) { 273042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2731f50f4353SLiu Bo ret = PTR_ERR(root); 2732f50f4353SLiu Bo goto out; 2733f50f4353SLiu Bo } 273442437a63SJosef Bacik } else { 2735a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2736a4f3d2c4SDavid Sterba fs_info->dev_root = root; 273742437a63SJosef Bacik } 2738820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2739820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 27404bbcaa64SEric Sandeen 2741aeb935a4SQu Wenruo /* 2742aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2743aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2744aeb935a4SQu Wenruo */ 274556e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 274656e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2747aeb935a4SQu Wenruo if (IS_ERR(root)) { 274842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2749aeb935a4SQu Wenruo ret = PTR_ERR(root); 2750aeb935a4SQu Wenruo goto out; 2751aeb935a4SQu Wenruo } 275242437a63SJosef Bacik } else { 2753aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2754aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 275542437a63SJosef Bacik } 2756aeb935a4SQu Wenruo 27574bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2758a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2759a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2760a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2761afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2762a4f3d2c4SDavid Sterba fs_info->quota_root = root; 27634bbcaa64SEric Sandeen } 27644bbcaa64SEric Sandeen 27654bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2766a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2767a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 276842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2769a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 27704bbcaa64SEric Sandeen if (ret != -ENOENT) 2771f50f4353SLiu Bo goto out; 277242437a63SJosef Bacik } 27734bbcaa64SEric Sandeen } else { 2774a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2775a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 27764bbcaa64SEric Sandeen } 27774bbcaa64SEric Sandeen 27784bbcaa64SEric Sandeen return 0; 2779f50f4353SLiu Bo out: 2780f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2781f50f4353SLiu Bo location.objectid, ret); 2782f50f4353SLiu Bo return ret; 27834bbcaa64SEric Sandeen } 27844bbcaa64SEric Sandeen 2785069ec957SQu Wenruo /* 2786069ec957SQu Wenruo * Real super block validation 2787069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2788069ec957SQu Wenruo * 2789069ec957SQu Wenruo * @sb: super block to check 2790069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2791069ec957SQu Wenruo * 0 the primary (1st) sb 2792069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2793069ec957SQu Wenruo * -1 skip bytenr check 2794069ec957SQu Wenruo */ 2795069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2796069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 279721a852b0SQu Wenruo { 279821a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 279921a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 280021a852b0SQu Wenruo int ret = 0; 280121a852b0SQu Wenruo 280221a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 280321a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 280421a852b0SQu Wenruo ret = -EINVAL; 280521a852b0SQu Wenruo } 280621a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 280721a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 280821a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 280921a852b0SQu Wenruo ret = -EINVAL; 281021a852b0SQu Wenruo } 281121a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 281221a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 281321a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 281421a852b0SQu Wenruo ret = -EINVAL; 281521a852b0SQu Wenruo } 281621a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 281721a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 281821a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 281921a852b0SQu Wenruo ret = -EINVAL; 282021a852b0SQu Wenruo } 282121a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 282221a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 282321a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 282421a852b0SQu Wenruo ret = -EINVAL; 282521a852b0SQu Wenruo } 282621a852b0SQu Wenruo 282721a852b0SQu Wenruo /* 282821a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 282921a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 283021a852b0SQu Wenruo */ 283121a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 283221a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 283321a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 283421a852b0SQu Wenruo ret = -EINVAL; 283521a852b0SQu Wenruo } 28360bb3eb3eSQu Wenruo 28370bb3eb3eSQu Wenruo /* 28381a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 28391a42daabSQu Wenruo * 28401a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 28411a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 28421a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 28431a42daabSQu Wenruo * beginning. 28440bb3eb3eSQu Wenruo */ 28451a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 284621a852b0SQu Wenruo btrfs_err(fs_info, 28470bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 284821a852b0SQu Wenruo sectorsize, PAGE_SIZE); 284921a852b0SQu Wenruo ret = -EINVAL; 285021a852b0SQu Wenruo } 28510bb3eb3eSQu Wenruo 285221a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 285321a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 285421a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 285521a852b0SQu Wenruo ret = -EINVAL; 285621a852b0SQu Wenruo } 285721a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 285821a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 285921a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 286021a852b0SQu Wenruo ret = -EINVAL; 286121a852b0SQu Wenruo } 286221a852b0SQu Wenruo 286321a852b0SQu Wenruo /* Root alignment check */ 286421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 286521a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 286621a852b0SQu Wenruo btrfs_super_root(sb)); 286721a852b0SQu Wenruo ret = -EINVAL; 286821a852b0SQu Wenruo } 286921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 287021a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 287121a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 287221a852b0SQu Wenruo ret = -EINVAL; 287321a852b0SQu Wenruo } 287421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 287521a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 287621a852b0SQu Wenruo btrfs_super_log_root(sb)); 287721a852b0SQu Wenruo ret = -EINVAL; 287821a852b0SQu Wenruo } 287921a852b0SQu Wenruo 2880aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2881aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2882aefd7f70SNikolay Borisov btrfs_err(fs_info, 2883aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2884aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2885aefd7f70SNikolay Borisov ret = -EINVAL; 2886aefd7f70SNikolay Borisov } 2887aefd7f70SNikolay Borisov 2888aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2889aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2890aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2891aefd7f70SNikolay Borisov btrfs_err(fs_info, 2892aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2893aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2894aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2895aefd7f70SNikolay Borisov ret = -EINVAL; 2896aefd7f70SNikolay Borisov } 2897aefd7f70SNikolay Borisov 2898de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 28997239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 290021a852b0SQu Wenruo btrfs_err(fs_info, 29017239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2902de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 290321a852b0SQu Wenruo ret = -EINVAL; 290421a852b0SQu Wenruo } 290521a852b0SQu Wenruo 290621a852b0SQu Wenruo /* 290721a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 290821a852b0SQu Wenruo * done later 290921a852b0SQu Wenruo */ 291021a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 291121a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 291221a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 291321a852b0SQu Wenruo ret = -EINVAL; 291421a852b0SQu Wenruo } 291521a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 291621a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 291721a852b0SQu Wenruo btrfs_super_stripesize(sb)); 291821a852b0SQu Wenruo ret = -EINVAL; 291921a852b0SQu Wenruo } 292021a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 292121a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 292221a852b0SQu Wenruo btrfs_super_num_devices(sb)); 292321a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 292421a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 292521a852b0SQu Wenruo ret = -EINVAL; 292621a852b0SQu Wenruo } 292721a852b0SQu Wenruo 2928069ec957SQu Wenruo if (mirror_num >= 0 && 2929069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 293021a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 293121a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 293221a852b0SQu Wenruo ret = -EINVAL; 293321a852b0SQu Wenruo } 293421a852b0SQu Wenruo 293521a852b0SQu Wenruo /* 293621a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 293721a852b0SQu Wenruo * and one chunk 293821a852b0SQu Wenruo */ 293921a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 294021a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 294121a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 294221a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 294321a852b0SQu Wenruo ret = -EINVAL; 294421a852b0SQu Wenruo } 294521a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 294621a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 294721a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 294821a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 294921a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 295021a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 295121a852b0SQu Wenruo ret = -EINVAL; 295221a852b0SQu Wenruo } 295321a852b0SQu Wenruo 295421a852b0SQu Wenruo /* 295521a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 295621a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 295721a852b0SQu Wenruo */ 295821a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 295921a852b0SQu Wenruo btrfs_warn(fs_info, 296021a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 296121a852b0SQu Wenruo btrfs_super_generation(sb), 296221a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 296321a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 296421a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 296521a852b0SQu Wenruo btrfs_warn(fs_info, 296621a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 296721a852b0SQu Wenruo btrfs_super_generation(sb), 296821a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 296921a852b0SQu Wenruo 297021a852b0SQu Wenruo return ret; 297121a852b0SQu Wenruo } 297221a852b0SQu Wenruo 2973069ec957SQu Wenruo /* 2974069ec957SQu Wenruo * Validation of super block at mount time. 2975069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2976069ec957SQu Wenruo * flags will be skipped. 2977069ec957SQu Wenruo */ 2978069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2979069ec957SQu Wenruo { 2980069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2981069ec957SQu Wenruo } 2982069ec957SQu Wenruo 298375cb857dSQu Wenruo /* 298475cb857dSQu Wenruo * Validation of super block at write time. 298575cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 298675cb857dSQu Wenruo * overwritten soon. 298775cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 298875cb857dSQu Wenruo */ 298975cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 299075cb857dSQu Wenruo struct btrfs_super_block *sb) 299175cb857dSQu Wenruo { 299275cb857dSQu Wenruo int ret; 299375cb857dSQu Wenruo 299475cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 299575cb857dSQu Wenruo if (ret < 0) 299675cb857dSQu Wenruo goto out; 2997e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 299875cb857dSQu Wenruo ret = -EUCLEAN; 299975cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 300075cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 300175cb857dSQu Wenruo goto out; 300275cb857dSQu Wenruo } 300375cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 300475cb857dSQu Wenruo ret = -EUCLEAN; 300575cb857dSQu Wenruo btrfs_err(fs_info, 300675cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 300775cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 300875cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 300975cb857dSQu Wenruo goto out; 301075cb857dSQu Wenruo } 301175cb857dSQu Wenruo out: 301275cb857dSQu Wenruo if (ret < 0) 301375cb857dSQu Wenruo btrfs_err(fs_info, 301475cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 301575cb857dSQu Wenruo return ret; 301675cb857dSQu Wenruo } 301775cb857dSQu Wenruo 3018bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 3019bd676446SJosef Bacik { 3020bd676446SJosef Bacik int ret = 0; 3021bd676446SJosef Bacik 3022bd676446SJosef Bacik root->node = read_tree_block(root->fs_info, bytenr, 3023bd676446SJosef Bacik root->root_key.objectid, gen, level, NULL); 3024bd676446SJosef Bacik if (IS_ERR(root->node)) { 3025bd676446SJosef Bacik ret = PTR_ERR(root->node); 3026bd676446SJosef Bacik root->node = NULL; 30274eb150d6SQu Wenruo return ret; 30284eb150d6SQu Wenruo } 30294eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 3030bd676446SJosef Bacik free_extent_buffer(root->node); 3031bd676446SJosef Bacik root->node = NULL; 30324eb150d6SQu Wenruo return -EIO; 3033bd676446SJosef Bacik } 3034bd676446SJosef Bacik 3035bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 3036bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 3037bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 3038bd676446SJosef Bacik return ret; 3039bd676446SJosef Bacik } 3040bd676446SJosef Bacik 3041bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 3042bd676446SJosef Bacik { 3043bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 3044bd676446SJosef Bacik u64 gen, bytenr; 3045bd676446SJosef Bacik int level, ret; 3046bd676446SJosef Bacik 3047bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 3048bd676446SJosef Bacik gen = btrfs_super_generation(sb); 3049bd676446SJosef Bacik level = btrfs_super_root_level(sb); 3050bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 30519c54e80dSJosef Bacik if (ret) { 3052bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 3053bd676446SJosef Bacik return ret; 3054bd676446SJosef Bacik } 3055bd676446SJosef Bacik 30569c54e80dSJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 30579c54e80dSJosef Bacik return 0; 30589c54e80dSJosef Bacik 30599c54e80dSJosef Bacik bytenr = btrfs_super_block_group_root(sb); 30609c54e80dSJosef Bacik gen = btrfs_super_block_group_root_generation(sb); 30619c54e80dSJosef Bacik level = btrfs_super_block_group_root_level(sb); 30629c54e80dSJosef Bacik ret = load_super_root(fs_info->block_group_root, bytenr, gen, level); 30639c54e80dSJosef Bacik if (ret) 30649c54e80dSJosef Bacik btrfs_warn(fs_info, "couldn't read block group root"); 30659c54e80dSJosef Bacik return ret; 30669c54e80dSJosef Bacik } 30679c54e80dSJosef Bacik 30686ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 3069b8522a1eSNikolay Borisov { 30706ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 3071b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 3072b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 3073b8522a1eSNikolay Borisov bool handle_error = false; 3074b8522a1eSNikolay Borisov int ret = 0; 3075b8522a1eSNikolay Borisov int i; 3076b8522a1eSNikolay Borisov 30779c54e80dSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 30789c54e80dSJosef Bacik struct btrfs_root *root; 3079b8522a1eSNikolay Borisov 30809c54e80dSJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID, 30819c54e80dSJosef Bacik GFP_KERNEL); 30829c54e80dSJosef Bacik if (!root) 30839c54e80dSJosef Bacik return -ENOMEM; 30849c54e80dSJosef Bacik fs_info->block_group_root = root; 30859c54e80dSJosef Bacik } 30869c54e80dSJosef Bacik 3087b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 3088b8522a1eSNikolay Borisov if (handle_error) { 3089b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 3090b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 3091b8522a1eSNikolay Borisov tree_root->node = NULL; 3092b8522a1eSNikolay Borisov 3093b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3094b8522a1eSNikolay Borisov break; 3095b8522a1eSNikolay Borisov 3096b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 3097b8522a1eSNikolay Borisov 3098b8522a1eSNikolay Borisov /* 3099b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 3100b8522a1eSNikolay Borisov * valid 3101b8522a1eSNikolay Borisov */ 3102b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 3103b8522a1eSNikolay Borisov 3104b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 3105b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3106b8522a1eSNikolay Borisov 3107b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 31086ef108ddSNikolay Borisov backup_index = ret; 3109b8522a1eSNikolay Borisov if (ret < 0) 3110b8522a1eSNikolay Borisov return ret; 3111b8522a1eSNikolay Borisov } 3112b8522a1eSNikolay Borisov 3113bd676446SJosef Bacik ret = load_important_roots(fs_info); 3114bd676446SJosef Bacik if (ret) { 3115217f5004SNikolay Borisov handle_error = true; 3116b8522a1eSNikolay Borisov continue; 3117b8522a1eSNikolay Borisov } 3118b8522a1eSNikolay Borisov 3119336a0d8dSNikolay Borisov /* 3120336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3121336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3122336a0d8dSNikolay Borisov */ 3123453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3124b8522a1eSNikolay Borisov if (ret < 0) { 3125b8522a1eSNikolay Borisov handle_error = true; 3126b8522a1eSNikolay Borisov continue; 3127b8522a1eSNikolay Borisov } 3128b8522a1eSNikolay Borisov 31296b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3130b8522a1eSNikolay Borisov 3131b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3132b8522a1eSNikolay Borisov if (ret < 0) { 3133b8522a1eSNikolay Borisov handle_error = true; 3134b8522a1eSNikolay Borisov continue; 3135b8522a1eSNikolay Borisov } 3136b8522a1eSNikolay Borisov 3137b8522a1eSNikolay Borisov /* All successful */ 3138bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3139bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3140d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 31416ef108ddSNikolay Borisov 31426ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 31436ef108ddSNikolay Borisov if (backup_index < 0) { 31446ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 31456ef108ddSNikolay Borisov } else { 31466ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 31476ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 31486ef108ddSNikolay Borisov } 3149b8522a1eSNikolay Borisov break; 3150b8522a1eSNikolay Borisov } 3151b8522a1eSNikolay Borisov 3152b8522a1eSNikolay Borisov return ret; 3153b8522a1eSNikolay Borisov } 3154b8522a1eSNikolay Borisov 31558260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3156eb60ceacSChris Mason { 315776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 3158f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 31598fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3160facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 316124bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3162eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 316311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3164eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3165a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 316676dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 316724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 31684cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3169ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3170f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 317147ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 317240ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3173afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3174c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3175f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3176abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3177d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3178f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 31797585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3180573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 31810bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 31825f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3183de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 318419c00ddcSChris Mason 31850b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 31866324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3187f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 318847ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 318918bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3190afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3191bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3192bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 31933fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 31943fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3195bd647ce3SJosef Bacik #endif 3196c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 319766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 319866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 319966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 320066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 320166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 320266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 320366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3204ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3205ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3206ba2c4d4eSJosef Bacik 3207771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 32084cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3209034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3210fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3211abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 321295ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 32139ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 32144cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3215a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3216f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 32178b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3218f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3219fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3220c8b97818SChris Mason 3221b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3222b34b086cSChris Mason num_online_cpus() + 2, 8); 32230afbaf8cSChris Mason 3224199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3225199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 322669fe2d75SJosef Bacik 3227638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 322821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 322921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 323021adbd5cSStefan Behrens #endif 3231779a65a4SEric Sandeen btrfs_init_balance(fs_info); 323257056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3233a2de733cSArne Jansen 32340f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 32356bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 3236a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 32370f9dd46cSJosef Bacik 3238fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 3239fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 324039279cc3SChris Mason 32415a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3242e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3243925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3244a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3245a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 32461bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 32479e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3248c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 324976dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3250803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3251fa9c0d79SChris Mason 3252ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3253f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3254b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3255416ac51dSArne Jansen 3256fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3257fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3258fa9c0d79SChris Mason 3259e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3260f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3261bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 32624854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3263034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 32643768f368SChris Mason 3265da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3266da17066cSJeff Mahoney fs_info->nodesize = 4096; 3267da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3268ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3269da17066cSJeff Mahoney fs_info->stripesize = 4096; 3270da17066cSJeff Mahoney 3271eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3272eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3273eede2bf3SOmar Sandoval 327418bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 327518bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 32768260edbaSJosef Bacik } 32778260edbaSJosef Bacik 32788260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 32798260edbaSJosef Bacik { 32808260edbaSJosef Bacik int ret; 32818260edbaSJosef Bacik 32828260edbaSJosef Bacik fs_info->sb = sb; 32838260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 32848260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 32859e967495SFilipe Manana 32865deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3287ae18c37aSJosef Bacik if (ret) 3288ae18c37aSJosef Bacik return ret; 3289ae18c37aSJosef Bacik 3290ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3291ae18c37aSJosef Bacik if (ret) 3292c75e8394SJosef Bacik return ret; 3293ae18c37aSJosef Bacik 3294ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3295ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3296ae18c37aSJosef Bacik 3297ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3298ae18c37aSJosef Bacik if (ret) 3299c75e8394SJosef Bacik return ret; 3300ae18c37aSJosef Bacik 3301ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3302ae18c37aSJosef Bacik GFP_KERNEL); 3303ae18c37aSJosef Bacik if (ret) 3304c75e8394SJosef Bacik return ret; 3305ae18c37aSJosef Bacik 3306ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3307ae18c37aSJosef Bacik GFP_KERNEL); 3308c75e8394SJosef Bacik if (!fs_info->delayed_root) 3309c75e8394SJosef Bacik return -ENOMEM; 3310ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3311ae18c37aSJosef Bacik 3312a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3313a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3314a0a1db70SFilipe Manana 3315c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3316ae18c37aSJosef Bacik } 3317ae18c37aSJosef Bacik 331897f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 331997f4dd09SNikolay Borisov { 3320*0d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 332197f4dd09SNikolay Borisov int ret; 332297f4dd09SNikolay Borisov 332397f4dd09SNikolay Borisov /* 332497f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 332597f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 332697f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 332797f4dd09SNikolay Borisov */ 332897f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 332997f4dd09SNikolay Borisov if (ret < 0) { 3330c94bec2cSJosef Bacik if (ret != -EINTR) 3331c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3332c94bec2cSJosef Bacik ret); 333397f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 333497f4dd09SNikolay Borisov return ret; 333597f4dd09SNikolay Borisov } 333697f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 333797f4dd09SNikolay Borisov } 333897f4dd09SNikolay Borisov 333997f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 334097f4dd09SNikolay Borisov { 334197f4dd09SNikolay Borisov struct task_struct *task; 334297f4dd09SNikolay Borisov 334397f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 334497f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 334597f4dd09SNikolay Borisov if (IS_ERR(task)) { 334697f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 334797f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 334897f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 334997f4dd09SNikolay Borisov return PTR_ERR(task); 335097f4dd09SNikolay Borisov } 335197f4dd09SNikolay Borisov 335297f4dd09SNikolay Borisov return 0; 335397f4dd09SNikolay Borisov } 335497f4dd09SNikolay Borisov 335544c0ca21SBoris Burkov /* 33568cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 33578cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 33588cd29088SBoris Burkov * code paths. 33598cd29088SBoris Burkov */ 33608cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 33618cd29088SBoris Burkov { 33628cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 33638b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 33648cd29088SBoris Burkov } 33658cd29088SBoris Burkov 33668cd29088SBoris Burkov /* 336744c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 336844c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 336944c0ca21SBoris Burkov */ 337044c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 337144c0ca21SBoris Burkov { 337244c0ca21SBoris Burkov int ret; 337394846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 33748b228324SBoris Burkov bool clear_free_space_tree = false; 33758b228324SBoris Burkov 33768b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 33778b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33788b228324SBoris Burkov clear_free_space_tree = true; 33798b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 33808b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 33818b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 33828b228324SBoris Burkov clear_free_space_tree = true; 33838b228324SBoris Burkov } 33848b228324SBoris Burkov 33858b228324SBoris Burkov if (clear_free_space_tree) { 33868b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 33878b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 33888b228324SBoris Burkov if (ret) { 33898b228324SBoris Burkov btrfs_warn(fs_info, 33908b228324SBoris Burkov "failed to clear free space tree: %d", ret); 33918b228324SBoris Burkov goto out; 33928b228324SBoris Burkov } 33938b228324SBoris Burkov } 339444c0ca21SBoris Burkov 33958d488a8cSFilipe Manana /* 33968d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 33978d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 33988d488a8cSFilipe Manana * them into the fs_info->fs_roots_radix tree. This must be done before 33998d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 34008d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 34018d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 34028d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 34038d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 34048d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 34058d488a8cSFilipe Manana */ 34068d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 34078d488a8cSFilipe Manana if (ret) 34088d488a8cSFilipe Manana goto out; 34098d488a8cSFilipe Manana 341044c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 341144c0ca21SBoris Burkov if (ret) 341244c0ca21SBoris Burkov goto out; 341344c0ca21SBoris Burkov 34148f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 34158f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 34168f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 34178f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 34188f1c21d7SBoris Burkov goto out; 34198f1c21d7SBoris Burkov } 34208f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 34218f1c21d7SBoris Burkov 342244c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 34237eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 342444c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 342544c0ca21SBoris Burkov if (ret < 0) { 342644c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 342744c0ca21SBoris Burkov goto out; 342844c0ca21SBoris Burkov } 342944c0ca21SBoris Burkov 34305011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 34315011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 34325011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 34335011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 34345011139aSBoris Burkov if (ret) { 34355011139aSBoris Burkov btrfs_warn(fs_info, 34365011139aSBoris Burkov "failed to create free space tree: %d", ret); 34375011139aSBoris Burkov goto out; 34385011139aSBoris Burkov } 34395011139aSBoris Burkov } 34405011139aSBoris Burkov 344194846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 344294846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 344394846229SBoris Burkov if (ret) 344494846229SBoris Burkov goto out; 344594846229SBoris Burkov } 344694846229SBoris Burkov 344744c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 344844c0ca21SBoris Burkov if (ret) 344944c0ca21SBoris Burkov goto out; 345044c0ca21SBoris Burkov 345144c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 345244c0ca21SBoris Burkov if (ret) { 345344c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 345444c0ca21SBoris Burkov goto out; 345544c0ca21SBoris Burkov } 345644c0ca21SBoris Burkov 345744c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 345844c0ca21SBoris Burkov 345944c0ca21SBoris Burkov if (!fs_info->uuid_root) { 346044c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 346144c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 346244c0ca21SBoris Burkov if (ret) { 346344c0ca21SBoris Burkov btrfs_warn(fs_info, 346444c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 346544c0ca21SBoris Burkov goto out; 346644c0ca21SBoris Burkov } 346744c0ca21SBoris Burkov } 346844c0ca21SBoris Burkov 346944c0ca21SBoris Burkov out: 347044c0ca21SBoris Burkov return ret; 347144c0ca21SBoris Burkov } 347244c0ca21SBoris Burkov 3473ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3474ae18c37aSJosef Bacik char *options) 3475ae18c37aSJosef Bacik { 3476ae18c37aSJosef Bacik u32 sectorsize; 3477ae18c37aSJosef Bacik u32 nodesize; 3478ae18c37aSJosef Bacik u32 stripesize; 3479ae18c37aSJosef Bacik u64 generation; 3480ae18c37aSJosef Bacik u64 features; 3481ae18c37aSJosef Bacik u16 csum_type; 3482ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3483ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3484ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3485ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3486ae18c37aSJosef Bacik int ret; 3487ae18c37aSJosef Bacik int err = -EINVAL; 3488ae18c37aSJosef Bacik int level; 3489ae18c37aSJosef Bacik 34908260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 349153b381b3SDavid Woodhouse if (ret) { 349283c8266aSDavid Sterba err = ret; 3493ae18c37aSJosef Bacik goto fail; 349453b381b3SDavid Woodhouse } 349553b381b3SDavid Woodhouse 3496ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3497ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3498ae18c37aSJosef Bacik GFP_KERNEL); 3499ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3500ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3501ae18c37aSJosef Bacik GFP_KERNEL); 3502ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3503ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3504ae18c37aSJosef Bacik err = -ENOMEM; 3505c75e8394SJosef Bacik goto fail; 3506ae18c37aSJosef Bacik } 3507ae18c37aSJosef Bacik 3508ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3509ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3510ae18c37aSJosef Bacik err = -ENOMEM; 3511c75e8394SJosef Bacik goto fail; 3512ae18c37aSJosef Bacik } 3513ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3514ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3515ae18c37aSJosef Bacik 3516d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 35171104a885SDavid Sterba 35181104a885SDavid Sterba /* 35191104a885SDavid Sterba * Read super block and check the signature bytes only 35201104a885SDavid Sterba */ 3521d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 35228f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 35238f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 352416cdcec7SMiao Xie goto fail_alloc; 352520b45077SDave Young } 352639279cc3SChris Mason 35271104a885SDavid Sterba /* 3528260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 35298dc3f22cSJohannes Thumshirn * corrupted, we'll find out 35308dc3f22cSJohannes Thumshirn */ 35318f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 353251bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 35338dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 353451bce6c9SJohannes Thumshirn csum_type); 35358dc3f22cSJohannes Thumshirn err = -EINVAL; 35368f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35378dc3f22cSJohannes Thumshirn goto fail_alloc; 35388dc3f22cSJohannes Thumshirn } 35398dc3f22cSJohannes Thumshirn 354083c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 354183c68bbcSSu Yue 35426d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 35436d97c6e3SJohannes Thumshirn if (ret) { 35446d97c6e3SJohannes Thumshirn err = ret; 35458f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35466d97c6e3SJohannes Thumshirn goto fail_alloc; 35476d97c6e3SJohannes Thumshirn } 35486d97c6e3SJohannes Thumshirn 35498dc3f22cSJohannes Thumshirn /* 35501104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 35511104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 35521104a885SDavid Sterba */ 35538f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 355405135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 35551104a885SDavid Sterba err = -EINVAL; 35568f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3557141386e1SJosef Bacik goto fail_alloc; 35581104a885SDavid Sterba } 35591104a885SDavid Sterba 35601104a885SDavid Sterba /* 35611104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 35621104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 35631104a885SDavid Sterba * the whole block of INFO_SIZE 35641104a885SDavid Sterba */ 35658f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 35668f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35675f39d397SChris Mason 3568fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3569fbc6feaeSNikolay Borisov 35700b86a832SChris Mason 3571fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3572fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3573fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3574fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3575fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3576fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3577fbc6feaeSNikolay Borisov } 3578fbc6feaeSNikolay Borisov 3579fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3580fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3581de37aa51SNikolay Borisov 3582069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 35831104a885SDavid Sterba if (ret) { 358405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 35851104a885SDavid Sterba err = -EINVAL; 3586141386e1SJosef Bacik goto fail_alloc; 35871104a885SDavid Sterba } 35881104a885SDavid Sterba 35890f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3590141386e1SJosef Bacik goto fail_alloc; 35910f7d52f4SChris Mason 3592acce952bSliubo /* check FS state, whether FS is broken. */ 359387533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 359487533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3595acce952bSliubo 359675e7cb7fSLiu Bo /* 359775e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 359875e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 359975e7cb7fSLiu Bo */ 360075e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 360175e7cb7fSLiu Bo 36026f93e834SAnand Jain /* 36036f93e834SAnand Jain * Flag our filesystem as having big metadata blocks if they are bigger 36046f93e834SAnand Jain * than the page size. 36056f93e834SAnand Jain */ 36066f93e834SAnand Jain if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 36076f93e834SAnand Jain if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 36086f93e834SAnand Jain btrfs_info(fs_info, 36096f93e834SAnand Jain "flagging fs with big metadata feature"); 36106f93e834SAnand Jain features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 36116f93e834SAnand Jain } 36126f93e834SAnand Jain 36136f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 36146f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 36156f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 36166f93e834SAnand Jain stripesize = sectorsize; 36176f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 36186f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 36196f93e834SAnand Jain 36206f93e834SAnand Jain fs_info->nodesize = nodesize; 36216f93e834SAnand Jain fs_info->sectorsize = sectorsize; 36226f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 36236f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 36246f93e834SAnand Jain fs_info->stripesize = stripesize; 36256f93e834SAnand Jain 36262ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 36272b82032cSYan Zheng if (ret) { 36282b82032cSYan Zheng err = ret; 3629141386e1SJosef Bacik goto fail_alloc; 36302b82032cSYan Zheng } 3631dfe25020SChris Mason 3632f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3633f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3634f2b636e8SJosef Bacik if (features) { 363505135f59SDavid Sterba btrfs_err(fs_info, 363605135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3637c1c9ff7cSGeert Uytterhoeven features); 3638f2b636e8SJosef Bacik err = -EINVAL; 3639141386e1SJosef Bacik goto fail_alloc; 3640f2b636e8SJosef Bacik } 3641f2b636e8SJosef Bacik 36425d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 36435d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 36440b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3645a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 36465c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 36475c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3648727011e0SChris Mason 36493173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 365005135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 36513173a18fSJosef Bacik 3652727011e0SChris Mason /* 3653bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3654bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3655bc3f116fSChris Mason */ 3656bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3657707e8a07SDavid Sterba (sectorsize != nodesize)) { 365805135f59SDavid Sterba btrfs_err(fs_info, 365905135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 366005135f59SDavid Sterba nodesize, sectorsize); 3661141386e1SJosef Bacik goto fail_alloc; 3662bc3f116fSChris Mason } 3663bc3f116fSChris Mason 3664ceda0864SMiao Xie /* 3665ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3666ceda0864SMiao Xie * update the flag. 3667ceda0864SMiao Xie */ 36685d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 36695d4f98a2SYan Zheng 3670f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3671f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3672bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 367305135f59SDavid Sterba btrfs_err(fs_info, 367405135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3675c1c9ff7cSGeert Uytterhoeven features); 3676f2b636e8SJosef Bacik err = -EINVAL; 3677141386e1SJosef Bacik goto fail_alloc; 3678f2b636e8SJosef Bacik } 367961d92c32SChris Mason 36808481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 36818481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 36828481dd80SQu Wenruo 36839f73f1aeSQu Wenruo /* 36849f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 36859f73f1aeSQu Wenruo * going to be deprecated. 36869f73f1aeSQu Wenruo * 36879f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 36889f73f1aeSQu Wenruo */ 36899f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 36909f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 36919f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 36929f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 36939f73f1aeSQu Wenruo 369495ea0486SQu Wenruo btrfs_warn(fs_info, 369595ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 36960bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 3697c8050b3bSQu Wenruo if (btrfs_super_incompat_flags(fs_info->super_copy) & 3698c8050b3bSQu Wenruo BTRFS_FEATURE_INCOMPAT_RAID56) { 3699c8050b3bSQu Wenruo btrfs_err(fs_info, 3700c8050b3bSQu Wenruo "RAID56 is not yet supported for sector size %u with page size %lu", 3701c8050b3bSQu Wenruo sectorsize, PAGE_SIZE); 3702c8050b3bSQu Wenruo err = -EINVAL; 3703c8050b3bSQu Wenruo goto fail_alloc; 3704c8050b3bSQu Wenruo } 37058481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 37068481dd80SQu Wenruo if (!subpage_info) 37078481dd80SQu Wenruo goto fail_alloc; 37088481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 37098481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3710c8050b3bSQu Wenruo } 37110bb3eb3eSQu Wenruo 3712d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 37132a458198SEric Sandeen if (ret) { 37142a458198SEric Sandeen err = ret; 37150dc3b84aSJosef Bacik goto fail_sb_buffer; 37160dc3b84aSJosef Bacik } 37174543df7eSChris Mason 37189e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 37199e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 37204575c9ccSChris Mason 3721a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3722a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3723de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3724db94535dSChris Mason 3725925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 37266bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3727925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 372884eed90fSChris Mason if (ret) { 372905135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 37305d4f98a2SYan Zheng goto fail_sb_buffer; 373184eed90fSChris Mason } 37320b86a832SChris Mason 373384234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3734581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3735bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3736bd676446SJosef Bacik generation, level); 3737bd676446SJosef Bacik if (ret) { 373805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3739af31f5e5SChris Mason goto fail_tree_roots; 374083121942SDavid Woodhouse } 37410b86a832SChris Mason 3742e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3743c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3744c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3745e17cade2SChris Mason 37465b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 37472b82032cSYan Zheng if (ret) { 374805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3749af31f5e5SChris Mason goto fail_tree_roots; 37502b82032cSYan Zheng } 37510b86a832SChris Mason 37528dabb742SStefan Behrens /* 3753bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3754bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3755bacce86aSAnand Jain * target is required. So free devices that are not part of this 37561a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3757bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 37588dabb742SStefan Behrens */ 3759bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3760d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 376105135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3762a6b0d5c8SChris Mason goto fail_tree_roots; 3763a6b0d5c8SChris Mason } 3764a6b0d5c8SChris Mason 3765b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 37664bbcaa64SEric Sandeen if (ret) 3767b8522a1eSNikolay Borisov goto fail_tree_roots; 37688929ecfaSYan, Zheng 376975ec1db8SJosef Bacik /* 377073651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 377173651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 377273651042SNaohiro Aota * zoned incompat feature flag set. 377373651042SNaohiro Aota */ 377473651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 377573651042SNaohiro Aota if (ret) { 377673651042SNaohiro Aota btrfs_err(fs_info, 377773651042SNaohiro Aota "zoned: failed to read device zone info: %d", 377873651042SNaohiro Aota ret); 377973651042SNaohiro Aota goto fail_block_groups; 378073651042SNaohiro Aota } 378173651042SNaohiro Aota 378273651042SNaohiro Aota /* 378375ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 378475ec1db8SJosef Bacik * check the generation here so we can set the 378575ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 378675ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 378775ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 378875ec1db8SJosef Bacik * even though it was perfectly fine. 378975ec1db8SJosef Bacik */ 379075ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 379175ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 379275ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 379375ec1db8SJosef Bacik 3794cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3795cf90d884SQu Wenruo if (ret) { 3796cf90d884SQu Wenruo btrfs_err(fs_info, 3797cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3798cf90d884SQu Wenruo ret); 3799cf90d884SQu Wenruo goto fail_block_groups; 3800cf90d884SQu Wenruo } 380168310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 380268310a5eSIlya Dryomov if (ret) { 380305135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 380468310a5eSIlya Dryomov goto fail_block_groups; 380568310a5eSIlya Dryomov } 380668310a5eSIlya Dryomov 3807733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3808733f4fbbSStefan Behrens if (ret) { 380905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3810733f4fbbSStefan Behrens goto fail_block_groups; 3811733f4fbbSStefan Behrens } 3812733f4fbbSStefan Behrens 38138dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 38148dabb742SStefan Behrens if (ret) { 381505135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 38168dabb742SStefan Behrens goto fail_block_groups; 38178dabb742SStefan Behrens } 38188dabb742SStefan Behrens 3819b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3820b70f5097SNaohiro Aota if (ret) { 3821b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3822b70f5097SNaohiro Aota ret); 3823b70f5097SNaohiro Aota goto fail_block_groups; 3824b70f5097SNaohiro Aota } 3825b70f5097SNaohiro Aota 3826c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3827b7c35e81SAnand Jain if (ret) { 382805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 382905135f59SDavid Sterba ret); 3830b7c35e81SAnand Jain goto fail_block_groups; 3831b7c35e81SAnand Jain } 3832b7c35e81SAnand Jain 383396f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 38345ac1d209SJeff Mahoney if (ret) { 383505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3836b7c35e81SAnand Jain goto fail_fsdev_sysfs; 38375ac1d209SJeff Mahoney } 38385ac1d209SJeff Mahoney 3839c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3840c59021f8Sliubo if (ret) { 384105135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 38422365dd3cSAnand Jain goto fail_sysfs; 3843c59021f8Sliubo } 3844c59021f8Sliubo 38455b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 38461b1d1f66SJosef Bacik if (ret) { 384705135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 38482365dd3cSAnand Jain goto fail_sysfs; 38491b1d1f66SJosef Bacik } 38504330e183SQu Wenruo 385116beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 385216beac87SNaohiro Aota 38535c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 38545c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 385505135f59SDavid Sterba btrfs_warn(fs_info, 385652042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 38572365dd3cSAnand Jain goto fail_sysfs; 3858292fd7fcSStefan Behrens } 38599078a3e1SChris Mason 386033c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3861a74a4b97SChris Mason "btrfs-cleaner"); 386257506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 38632365dd3cSAnand Jain goto fail_sysfs; 3864a74a4b97SChris Mason 3865a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3866a74a4b97SChris Mason tree_root, 3867a74a4b97SChris Mason "btrfs-transaction"); 386857506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 38693f157a2fSChris Mason goto fail_cleaner; 3870a74a4b97SChris Mason 3871583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3872c289811cSChris Mason !fs_info->fs_devices->rotating) { 3873583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3874c289811cSChris Mason } 3875c289811cSChris Mason 3876572d9ab7SDavid Sterba /* 387701327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3878572d9ab7SDavid Sterba * not have to wait for transaction commit 3879572d9ab7SDavid Sterba */ 3880572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 38813818aea2SQu Wenruo 388221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38830b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 38842ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 38850b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3886cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 388721adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 388821adbd5cSStefan Behrens if (ret) 388905135f59SDavid Sterba btrfs_warn(fs_info, 389005135f59SDavid Sterba "failed to initialize integrity check module: %d", 389105135f59SDavid Sterba ret); 389221adbd5cSStefan Behrens } 389321adbd5cSStefan Behrens #endif 3894bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3895bcef60f2SArne Jansen if (ret) 3896bcef60f2SArne Jansen goto fail_trans_kthread; 389721adbd5cSStefan Behrens 3898fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3899fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3900fd708b81SJosef Bacik 390196da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 390296da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 39030b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3904e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 390563443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 390679787eaaSJeff Mahoney if (ret) { 390763443bf5SEric Sandeen err = ret; 390828c16cbbSWang Shilong goto fail_qgroup; 3909e556ce2cSYan Zheng } 3910e02119d5SChris Mason } 39111a40e23bSZheng Yan 391256e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 39133140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 39143140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3915f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3916315bf8efSJosef Bacik fs_info->fs_root = NULL; 3917bcef60f2SArne Jansen goto fail_qgroup; 39183140c9a3SDan Carpenter } 3919c289811cSChris Mason 3920bc98a42cSDavid Howells if (sb_rdonly(sb)) 39218cd29088SBoris Burkov goto clear_oneshot; 39222b6ba629SIlya Dryomov 392344c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 39242b6ba629SIlya Dryomov if (ret) { 39256bccf3abSJeff Mahoney close_ctree(fs_info); 39262b6ba629SIlya Dryomov return ret; 3927e3acc2a6SJosef Bacik } 3928b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3929b382a324SJan Schmidt 393044c0ca21SBoris Burkov if (fs_info->uuid_root && 393144c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 393244c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 393305135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 393470f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 393570f80175SStefan Behrens if (ret) { 393605135f59SDavid Sterba btrfs_warn(fs_info, 393705135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 39386bccf3abSJeff Mahoney close_ctree(fs_info); 393970f80175SStefan Behrens return ret; 394070f80175SStefan Behrens } 3941f7a81ea4SStefan Behrens } 394294846229SBoris Burkov 3943afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 394447ab2a6cSJosef Bacik 3945b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3946b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3947b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3948b4be6aefSJosef Bacik 39498cd29088SBoris Burkov clear_oneshot: 39508cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3951ad2b2c80SAl Viro return 0; 395239279cc3SChris Mason 3953bcef60f2SArne Jansen fail_qgroup: 3954bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 39557c2ca468SChris Mason fail_trans_kthread: 39567c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 39572ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3958faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 39593f157a2fSChris Mason fail_cleaner: 3960a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 39617c2ca468SChris Mason 39627c2ca468SChris Mason /* 39637c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 39647c2ca468SChris Mason * kthreads 39657c2ca468SChris Mason */ 39667c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 39677c2ca468SChris Mason 39682365dd3cSAnand Jain fail_sysfs: 39696618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 39702365dd3cSAnand Jain 3971b7c35e81SAnand Jain fail_fsdev_sysfs: 3972b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3973b7c35e81SAnand Jain 39741b1d1f66SJosef Bacik fail_block_groups: 397554067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3976af31f5e5SChris Mason 3977af31f5e5SChris Mason fail_tree_roots: 39789e3aa805SJosef Bacik if (fs_info->data_reloc_root) 39799e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 39804273eaffSAnand Jain free_root_pointers(fs_info, true); 39812b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3982af31f5e5SChris Mason 398339279cc3SChris Mason fail_sb_buffer: 39847abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 39855cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 398616cdcec7SMiao Xie fail_alloc: 3987586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3988586e46e2SIlya Dryomov 39894543df7eSChris Mason iput(fs_info->btree_inode); 39907e662854SQinghuang Feng fail: 3991586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3992ad2b2c80SAl Viro return err; 3993eb60ceacSChris Mason } 3994663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3995eb60ceacSChris Mason 3996314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3997f2984462SChris Mason { 3998314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3999314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 4000314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 4001314b6dd0SJohannes Thumshirn struct page *page; 4002442a4f63SStefan Behrens 4003314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 4004314b6dd0SJohannes Thumshirn page = bvec->bv_page; 4005314b6dd0SJohannes Thumshirn 4006314b6dd0SJohannes Thumshirn if (bio->bi_status) { 4007fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 4008314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 4009314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 4010314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 4011314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 4012314b6dd0SJohannes Thumshirn SetPageError(page); 4013314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 4014314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 4015314b6dd0SJohannes Thumshirn } else { 4016314b6dd0SJohannes Thumshirn SetPageUptodate(page); 4017f2984462SChris Mason } 4018314b6dd0SJohannes Thumshirn 4019314b6dd0SJohannes Thumshirn put_page(page); 4020314b6dd0SJohannes Thumshirn unlock_page(page); 4021314b6dd0SJohannes Thumshirn } 4022314b6dd0SJohannes Thumshirn 4023314b6dd0SJohannes Thumshirn bio_put(bio); 4024f2984462SChris Mason } 4025f2984462SChris Mason 40268f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 40278f32380dSJohannes Thumshirn int copy_num) 402829c36d72SAnand Jain { 402929c36d72SAnand Jain struct btrfs_super_block *super; 40308f32380dSJohannes Thumshirn struct page *page; 403112659251SNaohiro Aota u64 bytenr, bytenr_orig; 40328f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 403312659251SNaohiro Aota int ret; 403429c36d72SAnand Jain 403512659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 403612659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 403712659251SNaohiro Aota if (ret == -ENOENT) 403812659251SNaohiro Aota return ERR_PTR(-EINVAL); 403912659251SNaohiro Aota else if (ret) 404012659251SNaohiro Aota return ERR_PTR(ret); 404112659251SNaohiro Aota 4042cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 40438f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 404429c36d72SAnand Jain 40458f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 40468f32380dSJohannes Thumshirn if (IS_ERR(page)) 40478f32380dSJohannes Thumshirn return ERR_CAST(page); 404829c36d72SAnand Jain 40498f32380dSJohannes Thumshirn super = page_address(page); 405096c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 405196c2e067SAnand Jain btrfs_release_disk_super(super); 405296c2e067SAnand Jain return ERR_PTR(-ENODATA); 405396c2e067SAnand Jain } 405496c2e067SAnand Jain 405512659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 40568f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40578f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 405829c36d72SAnand Jain } 405929c36d72SAnand Jain 40608f32380dSJohannes Thumshirn return super; 406129c36d72SAnand Jain } 406229c36d72SAnand Jain 406329c36d72SAnand Jain 40648f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 4065a512bbf8SYan Zheng { 40668f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 4067a512bbf8SYan Zheng int i; 4068a512bbf8SYan Zheng u64 transid = 0; 4069a512bbf8SYan Zheng 4070a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 4071a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 4072a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 4073a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 4074a512bbf8SYan Zheng */ 4075a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 40768f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 40778f32380dSJohannes Thumshirn if (IS_ERR(super)) 4078a512bbf8SYan Zheng continue; 4079a512bbf8SYan Zheng 4080a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 40818f32380dSJohannes Thumshirn if (latest) 40828f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40838f32380dSJohannes Thumshirn 40848f32380dSJohannes Thumshirn latest = super; 4085a512bbf8SYan Zheng transid = btrfs_super_generation(super); 4086a512bbf8SYan Zheng } 4087a512bbf8SYan Zheng } 408892fc03fbSAnand Jain 40898f32380dSJohannes Thumshirn return super; 4090a512bbf8SYan Zheng } 4091a512bbf8SYan Zheng 40924eedeb75SHisashi Hifumi /* 4093abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 4094314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 40954eedeb75SHisashi Hifumi * 4096abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 4097abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 4098abbb3b8eSDavid Sterba * same for write and wait phases. 40994eedeb75SHisashi Hifumi * 4100314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 41014eedeb75SHisashi Hifumi */ 4102a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 4103abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 4104a512bbf8SYan Zheng { 4105d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 4106314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 4107d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 4108a512bbf8SYan Zheng int i; 4109a512bbf8SYan Zheng int errors = 0; 411012659251SNaohiro Aota int ret; 411112659251SNaohiro Aota u64 bytenr, bytenr_orig; 4112a512bbf8SYan Zheng 4113a512bbf8SYan Zheng if (max_mirrors == 0) 4114a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4115a512bbf8SYan Zheng 4116d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4117d5178578SJohannes Thumshirn 4118a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4119314b6dd0SJohannes Thumshirn struct page *page; 4120314b6dd0SJohannes Thumshirn struct bio *bio; 4121314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4122314b6dd0SJohannes Thumshirn 412312659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 412412659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 412512659251SNaohiro Aota if (ret == -ENOENT) { 412612659251SNaohiro Aota continue; 412712659251SNaohiro Aota } else if (ret < 0) { 412812659251SNaohiro Aota btrfs_err(device->fs_info, 412912659251SNaohiro Aota "couldn't get super block location for mirror %d", 413012659251SNaohiro Aota i); 413112659251SNaohiro Aota errors++; 413212659251SNaohiro Aota continue; 413312659251SNaohiro Aota } 4134935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4135935e5cc9SMiao Xie device->commit_total_bytes) 4136a512bbf8SYan Zheng break; 4137a512bbf8SYan Zheng 413812659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4139a512bbf8SYan Zheng 4140fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4141fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4142fd08001fSEric Biggers sb->csum); 4143a512bbf8SYan Zheng 4144314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4145314b6dd0SJohannes Thumshirn GFP_NOFS); 4146314b6dd0SJohannes Thumshirn if (!page) { 4147fb456252SJeff Mahoney btrfs_err(device->fs_info, 4148314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4149f14d104dSDavid Sterba bytenr); 4150634554dcSJosef Bacik errors++; 4151634554dcSJosef Bacik continue; 4152634554dcSJosef Bacik } 4153634554dcSJosef Bacik 4154314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4155314b6dd0SJohannes Thumshirn get_page(page); 4156a512bbf8SYan Zheng 4157314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4158314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4159a512bbf8SYan Zheng 4160387125fcSChris Mason /* 4161314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4162314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4163314b6dd0SJohannes Thumshirn * checking. 4164387125fcSChris Mason */ 416507888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 416607888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 416707888c66SChristoph Hellwig GFP_NOFS); 4168314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4169314b6dd0SJohannes Thumshirn bio->bi_private = device; 4170314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4171314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4172314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4173314b6dd0SJohannes Thumshirn 4174314b6dd0SJohannes Thumshirn /* 4175314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4176314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4177314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4178314b6dd0SJohannes Thumshirn */ 41791b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4180314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4181314b6dd0SJohannes Thumshirn 4182314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 41838376d9e1SNaohiro Aota 41848376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 41858376d9e1SNaohiro Aota errors++; 4186a512bbf8SYan Zheng } 4187a512bbf8SYan Zheng return errors < i ? 0 : -1; 4188a512bbf8SYan Zheng } 4189a512bbf8SYan Zheng 4190387125fcSChris Mason /* 4191abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4192abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4193abbb3b8eSDavid Sterba * 4194314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4195abbb3b8eSDavid Sterba * date. 4196abbb3b8eSDavid Sterba */ 4197abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4198abbb3b8eSDavid Sterba { 4199abbb3b8eSDavid Sterba int i; 4200abbb3b8eSDavid Sterba int errors = 0; 4201b6a535faSHoward McLauchlan bool primary_failed = false; 420212659251SNaohiro Aota int ret; 4203abbb3b8eSDavid Sterba u64 bytenr; 4204abbb3b8eSDavid Sterba 4205abbb3b8eSDavid Sterba if (max_mirrors == 0) 4206abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4207abbb3b8eSDavid Sterba 4208abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4209314b6dd0SJohannes Thumshirn struct page *page; 4210314b6dd0SJohannes Thumshirn 421112659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 421212659251SNaohiro Aota if (ret == -ENOENT) { 421312659251SNaohiro Aota break; 421412659251SNaohiro Aota } else if (ret < 0) { 421512659251SNaohiro Aota errors++; 421612659251SNaohiro Aota if (i == 0) 421712659251SNaohiro Aota primary_failed = true; 421812659251SNaohiro Aota continue; 421912659251SNaohiro Aota } 4220abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4221abbb3b8eSDavid Sterba device->commit_total_bytes) 4222abbb3b8eSDavid Sterba break; 4223abbb3b8eSDavid Sterba 4224314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4225314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4226314b6dd0SJohannes Thumshirn if (!page) { 4227abbb3b8eSDavid Sterba errors++; 4228b6a535faSHoward McLauchlan if (i == 0) 4229b6a535faSHoward McLauchlan primary_failed = true; 4230abbb3b8eSDavid Sterba continue; 4231abbb3b8eSDavid Sterba } 4232314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4233314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4234314b6dd0SJohannes Thumshirn if (PageError(page)) { 4235abbb3b8eSDavid Sterba errors++; 4236b6a535faSHoward McLauchlan if (i == 0) 4237b6a535faSHoward McLauchlan primary_failed = true; 4238b6a535faSHoward McLauchlan } 4239abbb3b8eSDavid Sterba 4240314b6dd0SJohannes Thumshirn /* Drop our reference */ 4241314b6dd0SJohannes Thumshirn put_page(page); 4242abbb3b8eSDavid Sterba 4243314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4244314b6dd0SJohannes Thumshirn put_page(page); 4245abbb3b8eSDavid Sterba } 4246abbb3b8eSDavid Sterba 4247b6a535faSHoward McLauchlan /* log error, force error return */ 4248b6a535faSHoward McLauchlan if (primary_failed) { 4249b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4250b6a535faSHoward McLauchlan device->devid); 4251b6a535faSHoward McLauchlan return -1; 4252b6a535faSHoward McLauchlan } 4253b6a535faSHoward McLauchlan 4254abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4255abbb3b8eSDavid Sterba } 4256abbb3b8eSDavid Sterba 4257abbb3b8eSDavid Sterba /* 4258387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4259387125fcSChris Mason * for the barrier when it is done 4260387125fcSChris Mason */ 42614246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4262387125fcSChris Mason { 4263387125fcSChris Mason complete(bio->bi_private); 4264387125fcSChris Mason } 4265387125fcSChris Mason 4266387125fcSChris Mason /* 42674fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 42684fc6441aSAnand Jain * done in the waiting counterpart. 4269387125fcSChris Mason */ 42704fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4271387125fcSChris Mason { 4272e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 4273387125fcSChris Mason 4274a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4275a91cf0ffSWang Yugui /* 4276a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4277a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4278a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4279a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4280a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4281a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4282a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4283a91cf0ffSWang Yugui * non-debug builds. 4284a91cf0ffSWang Yugui */ 4285a91cf0ffSWang Yugui struct request_queue *q = bdev_get_queue(device->bdev); 4286c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 42874fc6441aSAnand Jain return; 4288a91cf0ffSWang Yugui #endif 4289387125fcSChris Mason 4290a7c50c94SChristoph Hellwig bio_reset(bio, device->bdev, REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4291387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4292387125fcSChris Mason init_completion(&device->flush_wait); 4293387125fcSChris Mason bio->bi_private = &device->flush_wait; 4294387125fcSChris Mason 429543a01111SLu Fengqi btrfsic_submit_bio(bio); 42961c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42974fc6441aSAnand Jain } 4298387125fcSChris Mason 42994fc6441aSAnand Jain /* 43004fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 43014fc6441aSAnand Jain */ 43028c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 43034fc6441aSAnand Jain { 43044fc6441aSAnand Jain struct bio *bio = device->flush_bio; 43054fc6441aSAnand Jain 43061c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 430758efbc9fSOmar Sandoval return BLK_STS_OK; 43084fc6441aSAnand Jain 43091c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 43102980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 43114fc6441aSAnand Jain 43128c27cb35SLinus Torvalds return bio->bi_status; 4313387125fcSChris Mason } 4314387125fcSChris Mason 4315d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4316401b41e5SAnand Jain { 43176528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4318401b41e5SAnand Jain return -EIO; 4319387125fcSChris Mason return 0; 4320387125fcSChris Mason } 4321387125fcSChris Mason 4322387125fcSChris Mason /* 4323387125fcSChris Mason * send an empty flush down to each device in parallel, 4324387125fcSChris Mason * then wait for them 4325387125fcSChris Mason */ 4326387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4327387125fcSChris Mason { 4328387125fcSChris Mason struct list_head *head; 4329387125fcSChris Mason struct btrfs_device *dev; 43305af3e8ccSStefan Behrens int errors_wait = 0; 43314e4cbee9SChristoph Hellwig blk_status_t ret; 4332387125fcSChris Mason 43331538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4334387125fcSChris Mason /* send down all the barriers */ 4335387125fcSChris Mason head = &info->fs_devices->devices; 43361538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4337e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4338f88ba6a2SHidetoshi Seto continue; 4339cea7c8bfSAnand Jain if (!dev->bdev) 4340387125fcSChris Mason continue; 4341e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4342ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4343387125fcSChris Mason continue; 4344387125fcSChris Mason 43454fc6441aSAnand Jain write_dev_flush(dev); 434658efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4347387125fcSChris Mason } 4348387125fcSChris Mason 4349387125fcSChris Mason /* wait for all the barriers */ 43501538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4351e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4352f88ba6a2SHidetoshi Seto continue; 4353387125fcSChris Mason if (!dev->bdev) { 43545af3e8ccSStefan Behrens errors_wait++; 4355387125fcSChris Mason continue; 4356387125fcSChris Mason } 4357e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4358ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4359387125fcSChris Mason continue; 4360387125fcSChris Mason 43614fc6441aSAnand Jain ret = wait_dev_flush(dev); 4362401b41e5SAnand Jain if (ret) { 4363401b41e5SAnand Jain dev->last_flush_error = ret; 436466b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 436566b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 43665af3e8ccSStefan Behrens errors_wait++; 4367387125fcSChris Mason } 4368401b41e5SAnand Jain } 4369401b41e5SAnand Jain 4370cea7c8bfSAnand Jain if (errors_wait) { 4371401b41e5SAnand Jain /* 4372401b41e5SAnand Jain * At some point we need the status of all disks 4373401b41e5SAnand Jain * to arrive at the volume status. So error checking 4374401b41e5SAnand Jain * is being pushed to a separate loop. 4375401b41e5SAnand Jain */ 4376d10b82feSQu Wenruo return check_barrier_error(info); 4377401b41e5SAnand Jain } 4378387125fcSChris Mason return 0; 4379387125fcSChris Mason } 4380387125fcSChris Mason 4381943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4382943c6e99SZhao Lei { 43838789f4feSZhao Lei int raid_type; 43848789f4feSZhao Lei int min_tolerated = INT_MAX; 4385943c6e99SZhao Lei 43868789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 43878789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 43888c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43898789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 43908789f4feSZhao Lei tolerated_failures); 4391943c6e99SZhao Lei 43928789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 43938789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 43948789f4feSZhao Lei continue; 439541a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 43968789f4feSZhao Lei continue; 43978c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43988789f4feSZhao Lei btrfs_raid_array[raid_type]. 43998789f4feSZhao Lei tolerated_failures); 44008789f4feSZhao Lei } 4401943c6e99SZhao Lei 44028789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4403ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 44048789f4feSZhao Lei min_tolerated = 0; 44058789f4feSZhao Lei } 44068789f4feSZhao Lei 44078789f4feSZhao Lei return min_tolerated; 4408943c6e99SZhao Lei } 4409943c6e99SZhao Lei 4410eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4411f2984462SChris Mason { 4412e5e9a520SChris Mason struct list_head *head; 4413f2984462SChris Mason struct btrfs_device *dev; 4414a061fc8dSChris Mason struct btrfs_super_block *sb; 4415f2984462SChris Mason struct btrfs_dev_item *dev_item; 4416f2984462SChris Mason int ret; 4417f2984462SChris Mason int do_barriers; 4418a236aed1SChris Mason int max_errors; 4419a236aed1SChris Mason int total_errors = 0; 4420a061fc8dSChris Mason u64 flags; 4421f2984462SChris Mason 44220b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4423fed3b381SLiu Bo 4424fed3b381SLiu Bo /* 4425fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4426fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4427fed3b381SLiu Bo * been consistent on disk. 4428fed3b381SLiu Bo */ 4429fed3b381SLiu Bo if (max_mirrors == 0) 44300b246afaSJeff Mahoney backup_super_roots(fs_info); 4431f2984462SChris Mason 44320b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4433a061fc8dSChris Mason dev_item = &sb->dev_item; 4434e5e9a520SChris Mason 44350b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 44360b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 44370b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4438387125fcSChris Mason 44395af3e8ccSStefan Behrens if (do_barriers) { 44400b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 44415af3e8ccSStefan Behrens if (ret) { 44425af3e8ccSStefan Behrens mutex_unlock( 44430b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 44440b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 44455af3e8ccSStefan Behrens "errors while submitting device barriers."); 44465af3e8ccSStefan Behrens return ret; 44475af3e8ccSStefan Behrens } 44485af3e8ccSStefan Behrens } 4449387125fcSChris Mason 44501538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4451dfe25020SChris Mason if (!dev->bdev) { 4452dfe25020SChris Mason total_errors++; 4453dfe25020SChris Mason continue; 4454dfe25020SChris Mason } 4455e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4456ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4457dfe25020SChris Mason continue; 4458dfe25020SChris Mason 44592b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4460a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4461a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 44627df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4463935e5cc9SMiao Xie dev->commit_total_bytes); 4464ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4465ce7213c7SMiao Xie dev->commit_bytes_used); 4466a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4467a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4468a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4469a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 44707239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 44717239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4472a512bbf8SYan Zheng 4473a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4474a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4475f2984462SChris Mason 447675cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 447775cb857dSQu Wenruo if (ret < 0) { 447875cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 447975cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 448075cb857dSQu Wenruo "unexpected superblock corruption detected"); 448175cb857dSQu Wenruo return -EUCLEAN; 448275cb857dSQu Wenruo } 448375cb857dSQu Wenruo 4484abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4485a236aed1SChris Mason if (ret) 4486a236aed1SChris Mason total_errors++; 4487f2984462SChris Mason } 4488a236aed1SChris Mason if (total_errors > max_errors) { 44890b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4490d397712bSChris Mason total_errors); 44910b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 449279787eaaSJeff Mahoney 44939d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 44940b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44950b246afaSJeff Mahoney "%d errors while writing supers", 44960b246afaSJeff Mahoney total_errors); 44979d565ba4SStefan Behrens return -EIO; 4498a236aed1SChris Mason } 4499f2984462SChris Mason 4500a512bbf8SYan Zheng total_errors = 0; 45011538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4502dfe25020SChris Mason if (!dev->bdev) 4503dfe25020SChris Mason continue; 4504e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4505ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4506dfe25020SChris Mason continue; 4507dfe25020SChris Mason 4508abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4509a512bbf8SYan Zheng if (ret) 45101259ab75SChris Mason total_errors++; 4511f2984462SChris Mason } 45120b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4513a236aed1SChris Mason if (total_errors > max_errors) { 45140b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 45150b246afaSJeff Mahoney "%d errors while writing supers", 45160b246afaSJeff Mahoney total_errors); 451779787eaaSJeff Mahoney return -EIO; 4518a236aed1SChris Mason } 4519f2984462SChris Mason return 0; 4520f2984462SChris Mason } 4521f2984462SChris Mason 4522cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4523cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4524cb517eabSMiao Xie struct btrfs_root *root) 45252619ba1fSChris Mason { 45264785e24fSJosef Bacik bool drop_ref = false; 45274785e24fSJosef Bacik 45284df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 45292619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 45302619ba1fSChris Mason (unsigned long)root->root_key.objectid); 4531af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 45324785e24fSJosef Bacik drop_ref = true; 45334df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 453476dda93cSYan, Zheng 453584961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4536ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 45371c1ea4f7SLiu Bo if (root->reloc_root) { 453800246528SJosef Bacik btrfs_put_root(root->reloc_root); 45391c1ea4f7SLiu Bo root->reloc_root = NULL; 45401c1ea4f7SLiu Bo } 45411c1ea4f7SLiu Bo } 45423321719eSLiu Bo 45434785e24fSJosef Bacik if (drop_ref) 454400246528SJosef Bacik btrfs_put_root(root); 45452619ba1fSChris Mason } 45462619ba1fSChris Mason 4547c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4548c146afadSYan Zheng { 4549c146afadSYan Zheng u64 root_objectid = 0; 4550c146afadSYan Zheng struct btrfs_root *gang[8]; 455165d33fd7SQu Wenruo int i = 0; 455265d33fd7SQu Wenruo int err = 0; 455365d33fd7SQu Wenruo unsigned int ret = 0; 4554c146afadSYan Zheng 4555c146afadSYan Zheng while (1) { 4556efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4557c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4558c146afadSYan Zheng (void **)gang, root_objectid, 4559c146afadSYan Zheng ARRAY_SIZE(gang)); 456065d33fd7SQu Wenruo if (!ret) { 4561efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4562c146afadSYan Zheng break; 456365d33fd7SQu Wenruo } 45645d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 456566b4ffd1SJosef Bacik 456665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 456765d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 456865d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 456965d33fd7SQu Wenruo gang[i] = NULL; 457065d33fd7SQu Wenruo continue; 457165d33fd7SQu Wenruo } 457265d33fd7SQu Wenruo /* grab all the search result for later use */ 457300246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 457465d33fd7SQu Wenruo } 4575efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 457665d33fd7SQu Wenruo 457765d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 457865d33fd7SQu Wenruo if (!gang[i]) 457965d33fd7SQu Wenruo continue; 4580c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 458166b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 458266b4ffd1SJosef Bacik if (err) 458365d33fd7SQu Wenruo break; 458400246528SJosef Bacik btrfs_put_root(gang[i]); 4585c146afadSYan Zheng } 4586c146afadSYan Zheng root_objectid++; 4587c146afadSYan Zheng } 458865d33fd7SQu Wenruo 458965d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 459065d33fd7SQu Wenruo for (; i < ret; i++) { 459165d33fd7SQu Wenruo if (gang[i]) 459200246528SJosef Bacik btrfs_put_root(gang[i]); 459365d33fd7SQu Wenruo } 459465d33fd7SQu Wenruo return err; 4595c146afadSYan Zheng } 4596c146afadSYan Zheng 45976bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4598c146afadSYan Zheng { 45996bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4600c146afadSYan Zheng struct btrfs_trans_handle *trans; 4601c146afadSYan Zheng 46020b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 46032ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 46040b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 46050b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4606c71bf099SYan, Zheng 4607c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 46080b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 46090b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4610c71bf099SYan, Zheng 46117a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 46123612b495STsutomu Itoh if (IS_ERR(trans)) 46133612b495STsutomu Itoh return PTR_ERR(trans); 46143a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4615c146afadSYan Zheng } 4616c146afadSYan Zheng 461736c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 461836c86a9eSQu Wenruo { 461936c86a9eSQu Wenruo struct btrfs_transaction *trans; 462036c86a9eSQu Wenruo struct btrfs_transaction *tmp; 462136c86a9eSQu Wenruo bool found = false; 462236c86a9eSQu Wenruo 462336c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 462436c86a9eSQu Wenruo return; 462536c86a9eSQu Wenruo 462636c86a9eSQu Wenruo /* 462736c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 462836c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 462936c86a9eSQu Wenruo */ 463036c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 463136c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 463236c86a9eSQu Wenruo struct extent_state *cached = NULL; 463336c86a9eSQu Wenruo u64 dirty_bytes = 0; 463436c86a9eSQu Wenruo u64 cur = 0; 463536c86a9eSQu Wenruo u64 found_start; 463636c86a9eSQu Wenruo u64 found_end; 463736c86a9eSQu Wenruo 463836c86a9eSQu Wenruo found = true; 463936c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 464036c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 464136c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 464236c86a9eSQu Wenruo cur = found_end + 1; 464336c86a9eSQu Wenruo } 464436c86a9eSQu Wenruo btrfs_warn(fs_info, 464536c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 464636c86a9eSQu Wenruo trans->transid, dirty_bytes); 464736c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 464836c86a9eSQu Wenruo 464936c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 465036c86a9eSQu Wenruo fs_info->running_transaction = NULL; 465136c86a9eSQu Wenruo list_del_init(&trans->list); 465236c86a9eSQu Wenruo 465336c86a9eSQu Wenruo btrfs_put_transaction(trans); 465436c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 465536c86a9eSQu Wenruo } 465636c86a9eSQu Wenruo ASSERT(!found); 465736c86a9eSQu Wenruo } 465836c86a9eSQu Wenruo 4659b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4660eb60ceacSChris Mason { 4661c146afadSYan Zheng int ret; 4662e089f05cSChris Mason 4663afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4664d6fd0ae2SOmar Sandoval /* 4665d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4666d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4667d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4668d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4669d6fd0ae2SOmar Sandoval */ 4670d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4671a2135011SChris Mason 4672b4be6aefSJosef Bacik /* 4673b4be6aefSJosef Bacik * If we had UNFINISHED_DROPS we could still be processing them, so 4674b4be6aefSJosef Bacik * clear that bit and wake up relocation so it can stop. 4675b4be6aefSJosef Bacik */ 4676b4be6aefSJosef Bacik btrfs_wake_unfinished_drop(fs_info); 4677b4be6aefSJosef Bacik 46787343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4679d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 46807343dd61SJustin Maggard 4681803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4682803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4683803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4684803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4685803b2f54SStefan Behrens 4686837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4687aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4688837d5b6eSIlya Dryomov 46898dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 46908dabb742SStefan Behrens 4691aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 46924cb5300bSChris Mason 46934cb5300bSChris Mason /* wait for any defraggers to finish */ 46944cb5300bSChris Mason wait_event(fs_info->transaction_wait, 46954cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 46964cb5300bSChris Mason 46974cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 469826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 46994cb5300bSChris Mason 470021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 470157056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4702576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 470321c7e756SMiao Xie 470418bb8bbfSJohannes Thumshirn cancel_work_sync(&fs_info->reclaim_bgs_work); 470518bb8bbfSJohannes Thumshirn 4706b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4707b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4708b0643e59SDennis Zhou 4709bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4710e44163e1SJeff Mahoney /* 4711d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4712d6fd0ae2SOmar Sandoval * unused block groups. 4713e44163e1SJeff Mahoney */ 47140b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4715e44163e1SJeff Mahoney 4716f0cc2cd7SFilipe Manana /* 4717f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4718f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4719f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4720f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4721f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4722f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4723f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4724f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4725f0cc2cd7SFilipe Manana * This is a very rare case. 4726f0cc2cd7SFilipe Manana */ 4727f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4728f0cc2cd7SFilipe Manana 47296bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4730d397712bSChris Mason if (ret) 473104892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4732c146afadSYan Zheng } 4733ed2ff2cbSChris Mason 473484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 47352ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4736acce952bSliubo 4737e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4738e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 47398929ecfaSYan, Zheng 4740e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4741afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4742f25784b3SYan Zheng 47435958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 47445958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 47455958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 47465958253cSQu Wenruo } 47475958253cSQu Wenruo 474804892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4749fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4750bcef60f2SArne Jansen 4751963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 475204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4753963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4754b0c68f8bSChris Mason } 475531153d81SYan Zheng 47565deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 47574297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 47585deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 47594297ff84SJosef Bacik 47606618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4761b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 47625ac1d209SJeff Mahoney 47631a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 47641a4319ccSLiu Bo 4765de348ee0SWang Shilong /* 4766de348ee0SWang Shilong * we must make sure there is not any read request to 4767de348ee0SWang Shilong * submit after we stopping all workers. 4768de348ee0SWang Shilong */ 4769de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 477096192499SJosef Bacik btrfs_stop_all_workers(fs_info); 477196192499SJosef Bacik 47720a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 477336c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 47740a31daa4SFilipe Manana 4775afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 47764273eaffSAnand Jain free_root_pointers(fs_info, true); 47778c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 47789ad6b7bcSChris Mason 47794e19443dSJosef Bacik /* 47804e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 47814e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 47824e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 47834e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 47844e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 47854e19443dSJosef Bacik */ 47864e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 47874e19443dSJosef Bacik 478813e6c37bSJosef Bacik iput(fs_info->btree_inode); 4789d6bfde87SChris Mason 479021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 47910b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 47922ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 479321adbd5cSStefan Behrens #endif 479421adbd5cSStefan Behrens 47950b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 479668c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4797eb60ceacSChris Mason } 4798eb60ceacSChris Mason 4799b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4800b9fab919SChris Mason int atomic) 4801ccd467d6SChris Mason { 48021259ab75SChris Mason int ret; 4803727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 48041259ab75SChris Mason 48050b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 48061259ab75SChris Mason if (!ret) 48071259ab75SChris Mason return ret; 48081259ab75SChris Mason 48091259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4810b9fab919SChris Mason parent_transid, atomic); 4811b9fab919SChris Mason if (ret == -EAGAIN) 4812b9fab919SChris Mason return ret; 48131259ab75SChris Mason return !ret; 48145f39d397SChris Mason } 48156702ed49SChris Mason 48165f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 48175f39d397SChris Mason { 48182f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 48195f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4820b9473439SChris Mason int was_dirty; 4821b4ce94deSChris Mason 482206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 482306ea65a3SJosef Bacik /* 482406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 482552042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 482606ea65a3SJosef Bacik * outside of the sanity tests. 482706ea65a3SJosef Bacik */ 4828b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 482906ea65a3SJosef Bacik return; 483006ea65a3SJosef Bacik #endif 483149d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 48320b246afaSJeff Mahoney if (transid != fs_info->generation) 48335d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 48340b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 48350b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4836e2d84521SMiao Xie if (!was_dirty) 4837104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4838e2d84521SMiao Xie buf->len, 48390b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 48401f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 484169fc6cbbSQu Wenruo /* 484269fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 484369fc6cbbSQu Wenruo * but item data not updated. 484469fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 484569fc6cbbSQu Wenruo */ 484669fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4847cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4848a4f78750SDavid Sterba btrfs_print_leaf(buf); 48491f21ef0aSFilipe Manana ASSERT(0); 48501f21ef0aSFilipe Manana } 48511f21ef0aSFilipe Manana #endif 4852eb60ceacSChris Mason } 4853eb60ceacSChris Mason 48542ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4855b53d3f5dSLiu Bo int flush_delayed) 485635b7e476SChris Mason { 4857188de649SChris Mason /* 4858188de649SChris Mason * looks as though older kernels can get into trouble with 4859188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4860188de649SChris Mason */ 4861e2d84521SMiao Xie int ret; 4862d6bfde87SChris Mason 48636933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4864d6bfde87SChris Mason return; 4865d6bfde87SChris Mason 4866b53d3f5dSLiu Bo if (flush_delayed) 48672ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 486816cdcec7SMiao Xie 4869d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4870d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4871d814a491SEthan Lien fs_info->dirty_metadata_batch); 4872e2d84521SMiao Xie if (ret > 0) { 48730b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 487416cdcec7SMiao Xie } 487516cdcec7SMiao Xie } 487616cdcec7SMiao Xie 48772ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 487816cdcec7SMiao Xie { 48792ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 488035b7e476SChris Mason } 4881b53d3f5dSLiu Bo 48822ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4883b53d3f5dSLiu Bo { 48842ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4885d6bfde87SChris Mason } 48866b80053dSChris Mason 48872ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4888acce952bSliubo { 4889fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4890fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4891fe816d0fSNikolay Borisov 48920b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 48932ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 48940b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4895acce952bSliubo 48960b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 48970b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4898acce952bSliubo } 4899acce952bSliubo 4900ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4901ef67963dSJosef Bacik { 4902ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4903ef67963dSJosef Bacik u64 root_objectid = 0; 4904ef67963dSJosef Bacik int ret; 4905ef67963dSJosef Bacik 4906ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4907ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4908ef67963dSJosef Bacik (void **)gang, root_objectid, 4909ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4910ef67963dSJosef Bacik int i; 4911ef67963dSJosef Bacik 4912ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4913ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4914ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4915ef67963dSJosef Bacik 4916ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4917ef67963dSJosef Bacik if (!gang[i]) 4918ef67963dSJosef Bacik continue; 4919ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4920ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4921ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4922ef67963dSJosef Bacik } 4923ef67963dSJosef Bacik root_objectid++; 4924ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4925ef67963dSJosef Bacik } 4926ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4927ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4928ef67963dSJosef Bacik } 4929ef67963dSJosef Bacik 4930143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4931acce952bSliubo { 4932acce952bSliubo struct btrfs_ordered_extent *ordered; 4933acce952bSliubo 4934199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4935779880efSJosef Bacik /* 4936779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4937779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4938779880efSJosef Bacik */ 4939199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4940779880efSJosef Bacik root_extent_list) 4941779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4942199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4943199c2a9cSMiao Xie } 4944199c2a9cSMiao Xie 4945199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4946199c2a9cSMiao Xie { 4947199c2a9cSMiao Xie struct btrfs_root *root; 4948199c2a9cSMiao Xie struct list_head splice; 4949199c2a9cSMiao Xie 4950199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4951199c2a9cSMiao Xie 4952199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4953199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4954199c2a9cSMiao Xie while (!list_empty(&splice)) { 4955199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4956199c2a9cSMiao Xie ordered_root); 49571de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 49581de2cfdeSJosef Bacik &fs_info->ordered_roots); 4959199c2a9cSMiao Xie 49602a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4961199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4962199c2a9cSMiao Xie 49632a85d9caSLiu Bo cond_resched(); 49642a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4965199c2a9cSMiao Xie } 4966199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 496774d5d229SJosef Bacik 496874d5d229SJosef Bacik /* 496974d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 497074d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 497174d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 497274d5d229SJosef Bacik * actually get run and error out properly. 497374d5d229SJosef Bacik */ 497474d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4975acce952bSliubo } 4976acce952bSliubo 497735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 49782ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4979acce952bSliubo { 4980acce952bSliubo struct rb_node *node; 4981acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4982acce952bSliubo struct btrfs_delayed_ref_node *ref; 4983acce952bSliubo int ret = 0; 4984acce952bSliubo 4985acce952bSliubo delayed_refs = &trans->delayed_refs; 4986acce952bSliubo 4987acce952bSliubo spin_lock(&delayed_refs->lock); 4988d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4989cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4990b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4991acce952bSliubo return ret; 4992acce952bSliubo } 4993acce952bSliubo 49945c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4995d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 49960e0adbcfSJosef Bacik struct rb_node *n; 4997e78417d1SJosef Bacik bool pin_bytes = false; 4998acce952bSliubo 4999d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 5000d7df2c79SJosef Bacik href_node); 50013069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 5002b939d1abSJosef Bacik continue; 50033069bd26SJosef Bacik 5004d7df2c79SJosef Bacik spin_lock(&head->lock); 5005e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 50060e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 50070e0adbcfSJosef Bacik ref_node); 5008d7df2c79SJosef Bacik ref->in_tree = 0; 5009e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 50100e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 50111d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 50121d57ee94SWang Xiaoguang list_del(&ref->add_list); 5013d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 5014d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 5015d7df2c79SJosef Bacik } 501654067ae9SJosef Bacik if (head->must_insert_reserved) 5017e78417d1SJosef Bacik pin_bytes = true; 501878a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 5019fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 5020d7df2c79SJosef Bacik spin_unlock(&head->lock); 5021acce952bSliubo spin_unlock(&delayed_refs->lock); 5022e78417d1SJosef Bacik mutex_unlock(&head->mutex); 5023acce952bSliubo 5024f603bb94SNikolay Borisov if (pin_bytes) { 5025f603bb94SNikolay Borisov struct btrfs_block_group *cache; 5026f603bb94SNikolay Borisov 5027f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 5028f603bb94SNikolay Borisov BUG_ON(!cache); 5029f603bb94SNikolay Borisov 5030f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 5031f603bb94SNikolay Borisov spin_lock(&cache->lock); 5032f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 5033f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 5034f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 5035f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 5036f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 5037f603bb94SNikolay Borisov spin_unlock(&cache->lock); 5038f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 5039f603bb94SNikolay Borisov 5040f603bb94SNikolay Borisov btrfs_put_block_group(cache); 5041f603bb94SNikolay Borisov 5042f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 5043f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 5044f603bb94SNikolay Borisov } 504531890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 5046d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 5047acce952bSliubo cond_resched(); 5048acce952bSliubo spin_lock(&delayed_refs->lock); 5049acce952bSliubo } 505081f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 5051acce952bSliubo 5052acce952bSliubo spin_unlock(&delayed_refs->lock); 5053acce952bSliubo 5054acce952bSliubo return ret; 5055acce952bSliubo } 5056acce952bSliubo 5057143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 5058acce952bSliubo { 5059acce952bSliubo struct btrfs_inode *btrfs_inode; 5060acce952bSliubo struct list_head splice; 5061acce952bSliubo 5062acce952bSliubo INIT_LIST_HEAD(&splice); 5063acce952bSliubo 5064eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5065eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 5066acce952bSliubo 5067acce952bSliubo while (!list_empty(&splice)) { 5068fe816d0fSNikolay Borisov struct inode *inode = NULL; 5069eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 5070acce952bSliubo delalloc_inodes); 5071fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 5072eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5073acce952bSliubo 5074fe816d0fSNikolay Borisov /* 5075fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 5076fe816d0fSNikolay Borisov * meanwhile. 5077fe816d0fSNikolay Borisov */ 5078fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 5079fe816d0fSNikolay Borisov if (inode) { 5080fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 5081fe816d0fSNikolay Borisov iput(inode); 5082fe816d0fSNikolay Borisov } 5083eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5084acce952bSliubo } 5085eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5086eb73c1b7SMiao Xie } 5087eb73c1b7SMiao Xie 5088eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 5089eb73c1b7SMiao Xie { 5090eb73c1b7SMiao Xie struct btrfs_root *root; 5091eb73c1b7SMiao Xie struct list_head splice; 5092eb73c1b7SMiao Xie 5093eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 5094eb73c1b7SMiao Xie 5095eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5096eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 5097eb73c1b7SMiao Xie while (!list_empty(&splice)) { 5098eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 5099eb73c1b7SMiao Xie delalloc_root); 510000246528SJosef Bacik root = btrfs_grab_root(root); 5101eb73c1b7SMiao Xie BUG_ON(!root); 5102eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5103eb73c1b7SMiao Xie 5104eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 510500246528SJosef Bacik btrfs_put_root(root); 5106eb73c1b7SMiao Xie 5107eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5108eb73c1b7SMiao Xie } 5109eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5110acce952bSliubo } 5111acce952bSliubo 51122ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5113acce952bSliubo struct extent_io_tree *dirty_pages, 5114acce952bSliubo int mark) 5115acce952bSliubo { 5116acce952bSliubo int ret; 5117acce952bSliubo struct extent_buffer *eb; 5118acce952bSliubo u64 start = 0; 5119acce952bSliubo u64 end; 5120acce952bSliubo 5121acce952bSliubo while (1) { 5122acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5123e6138876SJosef Bacik mark, NULL); 5124acce952bSliubo if (ret) 5125acce952bSliubo break; 5126acce952bSliubo 512791166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5128acce952bSliubo while (start <= end) { 51290b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 51300b246afaSJeff Mahoney start += fs_info->nodesize; 5131fd8b2b61SJosef Bacik if (!eb) 5132acce952bSliubo continue; 5133fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5134acce952bSliubo 5135fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5136fd8b2b61SJosef Bacik &eb->bflags)) 5137fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5138fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5139acce952bSliubo } 5140acce952bSliubo } 5141acce952bSliubo 5142acce952bSliubo return ret; 5143acce952bSliubo } 5144acce952bSliubo 51452ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5146fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5147acce952bSliubo { 5148acce952bSliubo u64 start; 5149acce952bSliubo u64 end; 5150acce952bSliubo int ret; 5151acce952bSliubo 5152acce952bSliubo while (1) { 51530e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 51540e6ec385SFilipe Manana 5155fcd5e742SLu Fengqi /* 5156fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5157fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5158fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5159fcd5e742SLu Fengqi * the same extent range. 5160fcd5e742SLu Fengqi */ 5161fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5162acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 51630e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5164fcd5e742SLu Fengqi if (ret) { 5165fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5166acce952bSliubo break; 5167fcd5e742SLu Fengqi } 5168acce952bSliubo 51690e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 51700e6ec385SFilipe Manana free_extent_state(cached_state); 51712ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5172fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5173acce952bSliubo cond_resched(); 5174acce952bSliubo } 5175acce952bSliubo 5176acce952bSliubo return 0; 5177acce952bSliubo } 5178acce952bSliubo 517932da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5180c79a1751SLiu Bo { 5181c79a1751SLiu Bo struct inode *inode; 5182c79a1751SLiu Bo 5183c79a1751SLiu Bo inode = cache->io_ctl.inode; 5184c79a1751SLiu Bo if (inode) { 5185c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5186c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5187c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5188c79a1751SLiu Bo iput(inode); 5189c79a1751SLiu Bo } 5190bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5191c79a1751SLiu Bo btrfs_put_block_group(cache); 5192c79a1751SLiu Bo } 5193c79a1751SLiu Bo 5194c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 51952ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5196c79a1751SLiu Bo { 519732da5386SDavid Sterba struct btrfs_block_group *cache; 5198c79a1751SLiu Bo 5199c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5200c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5201c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 520232da5386SDavid Sterba struct btrfs_block_group, 5203c79a1751SLiu Bo dirty_list); 5204c79a1751SLiu Bo 5205c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5206c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5207c79a1751SLiu Bo list_del_init(&cache->io_list); 5208c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5209c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5210c79a1751SLiu Bo } 5211c79a1751SLiu Bo 5212c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5213c79a1751SLiu Bo spin_lock(&cache->lock); 5214c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5215c79a1751SLiu Bo spin_unlock(&cache->lock); 5216c79a1751SLiu Bo 5217c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5218c79a1751SLiu Bo btrfs_put_block_group(cache); 5219ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5220c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5221c79a1751SLiu Bo } 5222c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5223c79a1751SLiu Bo 522445ae2c18SNikolay Borisov /* 522545ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 522645ae2c18SNikolay Borisov * to use it without any locking 522745ae2c18SNikolay Borisov */ 5228c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5229c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 523032da5386SDavid Sterba struct btrfs_block_group, 5231c79a1751SLiu Bo io_list); 5232c79a1751SLiu Bo 5233c79a1751SLiu Bo list_del_init(&cache->io_list); 5234c79a1751SLiu Bo spin_lock(&cache->lock); 5235c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5236c79a1751SLiu Bo spin_unlock(&cache->lock); 5237c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5238c79a1751SLiu Bo } 5239c79a1751SLiu Bo } 5240c79a1751SLiu Bo 524149b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 52422ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 524349b25e05SJeff Mahoney { 5244bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5245bbbf7243SNikolay Borisov 52462ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5247c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5248c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5249c79a1751SLiu Bo 5250bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5251bbbf7243SNikolay Borisov post_commit_list) { 5252bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5253bbbf7243SNikolay Borisov } 5254bbbf7243SNikolay Borisov 52552ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 525649b25e05SJeff Mahoney 52574a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 52580b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 525949b25e05SJeff Mahoney 52604a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 52610b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 526249b25e05SJeff Mahoney 5263ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 526467cde344SMiao Xie 52652ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 526649b25e05SJeff Mahoney EXTENT_DIRTY); 5267fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 526849b25e05SJeff Mahoney 5269d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5270d3575156SNaohiro Aota 52714a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 52724a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 527349b25e05SJeff Mahoney } 527449b25e05SJeff Mahoney 52752ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5276acce952bSliubo { 5277acce952bSliubo struct btrfs_transaction *t; 5278acce952bSliubo 52790b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5280acce952bSliubo 52810b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52820b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 52830b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5284724e2315SJosef Bacik struct btrfs_transaction, list); 5285724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 52869b64f57dSElena Reshetova refcount_inc(&t->use_count); 52870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52882ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5289724e2315SJosef Bacik btrfs_put_transaction(t); 52900b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5291724e2315SJosef Bacik continue; 5292724e2315SJosef Bacik } 52930b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5294724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 52950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5296724e2315SJosef Bacik /* 5297724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5298724e2315SJosef Bacik * handle open currently for this transaction. 5299724e2315SJosef Bacik */ 5300724e2315SJosef Bacik wait_event(t->writer_wait, 5301724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5302724e2315SJosef Bacik } else { 53030b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5304724e2315SJosef Bacik } 53052ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5306724e2315SJosef Bacik 53070b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 53080b246afaSJeff Mahoney if (t == fs_info->running_transaction) 53090b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5310724e2315SJosef Bacik list_del_init(&t->list); 53110b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5312a4abeea4SJosef Bacik 5313724e2315SJosef Bacik btrfs_put_transaction(t); 53142e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 53150b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5316724e2315SJosef Bacik } 53170b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 53180b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5319ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5320ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 53210b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5322ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 53230b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5324acce952bSliubo 5325acce952bSliubo return 0; 5326acce952bSliubo } 5327ec7d6dfdSNikolay Borisov 5328453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5329ec7d6dfdSNikolay Borisov { 5330ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5331ec7d6dfdSNikolay Borisov int ret; 5332ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5333ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5334ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5335ec7d6dfdSNikolay Borisov int slot; 5336ec7d6dfdSNikolay Borisov 5337ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5338ec7d6dfdSNikolay Borisov if (!path) 5339ec7d6dfdSNikolay Borisov return -ENOMEM; 5340ec7d6dfdSNikolay Borisov 5341ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5342ec7d6dfdSNikolay Borisov search_key.type = -1; 5343ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5344ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5345ec7d6dfdSNikolay Borisov if (ret < 0) 5346ec7d6dfdSNikolay Borisov goto error; 5347ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5348ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5349ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5350ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5351ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 535223125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 535323125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5354ec7d6dfdSNikolay Borisov } else { 535523125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5356ec7d6dfdSNikolay Borisov } 5357ec7d6dfdSNikolay Borisov ret = 0; 5358ec7d6dfdSNikolay Borisov error: 5359ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5360ec7d6dfdSNikolay Borisov return ret; 5361ec7d6dfdSNikolay Borisov } 5362ec7d6dfdSNikolay Borisov 5363543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5364ec7d6dfdSNikolay Borisov { 5365ec7d6dfdSNikolay Borisov int ret; 5366ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5367ec7d6dfdSNikolay Borisov 53686b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5369ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5370ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5371ec7d6dfdSNikolay Borisov root->root_key.objectid); 5372ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5373ec7d6dfdSNikolay Borisov goto out; 5374ec7d6dfdSNikolay Borisov } 5375ec7d6dfdSNikolay Borisov 537623125104SNikolay Borisov *objectid = root->free_objectid++; 5377ec7d6dfdSNikolay Borisov ret = 0; 5378ec7d6dfdSNikolay Borisov out: 5379ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5380ec7d6dfdSNikolay Borisov return ret; 5381ec7d6dfdSNikolay Borisov } 5382