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; 212e9be5a30SDavid Sterba const int num_pages = fs_info->nodesize >> PAGE_SHIFT; 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; 2442755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 2451259ab75SChris Mason 2461259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 2471259ab75SChris Mason return 0; 2481259ab75SChris Mason 249b9fab919SChris Mason if (atomic) 250b9fab919SChris Mason return -EAGAIN; 251b9fab919SChris Mason 252ac5887c8SJosef Bacik if (need_lock) 253a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 254a26e8c9fSJosef Bacik 2552ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 256ff13db41SDavid Sterba &cached_state); 2570b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 2581259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 2591259ab75SChris Mason ret = 0; 2601259ab75SChris Mason goto out; 2611259ab75SChris Mason } 26294647322SDavid Sterba btrfs_err_rl(eb->fs_info, 26394647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 26494647322SDavid Sterba eb->start, 26529549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 2661259ab75SChris Mason ret = 1; 267a26e8c9fSJosef Bacik 268a26e8c9fSJosef Bacik /* 269a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 270a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 27101327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 272a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 273a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 274a26e8c9fSJosef Bacik * making everybody very sad. 275a26e8c9fSJosef Bacik */ 276a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 2770b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 27833958dc6SChris Mason out: 2792ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 280e43bbe5eSDavid Sterba &cached_state); 281472b909fSJosef Bacik if (need_lock) 282ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 2831259ab75SChris Mason return ret; 2841259ab75SChris Mason } 2851259ab75SChris Mason 286e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 287e7e16f48SJohannes Thumshirn { 288e7e16f48SJohannes Thumshirn switch (csum_type) { 289e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 2903951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 2913831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 292352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 293e7e16f48SJohannes Thumshirn return true; 294e7e16f48SJohannes Thumshirn default: 295e7e16f48SJohannes Thumshirn return false; 296e7e16f48SJohannes Thumshirn } 297e7e16f48SJohannes Thumshirn } 298e7e16f48SJohannes Thumshirn 299d352ac68SChris Mason /* 3001104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3011104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3021104a885SDavid Sterba */ 303ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 304ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3051104a885SDavid Sterba { 3061104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3071104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 30851bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 309d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 310d5178578SJohannes Thumshirn 311d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3121104a885SDavid Sterba 3131104a885SDavid Sterba /* 3141104a885SDavid Sterba * The super_block structure does not span the whole 31551bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 31651bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 3171104a885SDavid Sterba */ 318fd08001fSEric Biggers crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE, 319fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 3201104a885SDavid Sterba 32155fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 322e7e16f48SJohannes Thumshirn return 1; 3231104a885SDavid Sterba 324e7e16f48SJohannes Thumshirn return 0; 3251104a885SDavid Sterba } 3261104a885SDavid Sterba 327e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 328ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 329581c1760SQu Wenruo { 330e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 331581c1760SQu Wenruo int found_level; 332581c1760SQu Wenruo struct btrfs_key found_key; 333581c1760SQu Wenruo int ret; 334581c1760SQu Wenruo 335581c1760SQu Wenruo found_level = btrfs_header_level(eb); 336581c1760SQu Wenruo if (found_level != level) { 33763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 33863489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 339581c1760SQu Wenruo btrfs_err(fs_info, 340581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 341581c1760SQu Wenruo eb->start, level, found_level); 342581c1760SQu Wenruo return -EIO; 343581c1760SQu Wenruo } 344581c1760SQu Wenruo 345581c1760SQu Wenruo if (!first_key) 346581c1760SQu Wenruo return 0; 347581c1760SQu Wenruo 3485d41be6fSQu Wenruo /* 3495d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 3505d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 3515d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 3525d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 3535d41be6fSQu Wenruo */ 3545d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 3555d41be6fSQu Wenruo return 0; 35662fdaa52SQu Wenruo 35762fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 35862fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 35962fdaa52SQu Wenruo btrfs_err(fs_info, 36062fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 36162fdaa52SQu Wenruo eb->start); 36262fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 36362fdaa52SQu Wenruo return -EUCLEAN; 36462fdaa52SQu Wenruo } 36562fdaa52SQu Wenruo 366581c1760SQu Wenruo if (found_level) 367581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 368581c1760SQu Wenruo else 369581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 370581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 371581c1760SQu Wenruo 372581c1760SQu Wenruo if (ret) { 37363489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 37463489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 375581c1760SQu Wenruo btrfs_err(fs_info, 376ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 377ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 378ff76a864SLiu Bo first_key->type, first_key->offset, 379ff76a864SLiu Bo found_key.objectid, found_key.type, 380ff76a864SLiu Bo found_key.offset); 381581c1760SQu Wenruo } 382581c1760SQu Wenruo return ret; 383581c1760SQu Wenruo } 384581c1760SQu Wenruo 3851104a885SDavid Sterba /* 386d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 387d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 388581c1760SQu Wenruo * 389581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 390581c1760SQu Wenruo * @level: expected level, mandatory check 391581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 392d352ac68SChris Mason */ 3935ab12d1fSDavid Sterba static int btree_read_extent_buffer_pages(struct extent_buffer *eb, 394581c1760SQu Wenruo u64 parent_transid, int level, 395581c1760SQu Wenruo struct btrfs_key *first_key) 396f188591eSChris Mason { 3975ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 398f188591eSChris Mason struct extent_io_tree *io_tree; 399ea466794SJosef Bacik int failed = 0; 400f188591eSChris Mason int ret; 401f188591eSChris Mason int num_copies = 0; 402f188591eSChris Mason int mirror_num = 0; 403ea466794SJosef Bacik int failed_mirror = 0; 404f188591eSChris Mason 4050b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 406f188591eSChris Mason while (1) { 407f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 408c2ccfbc6SNikolay Borisov ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num); 409256dd1bbSStefan Behrens if (!ret) { 410581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 411b9fab919SChris Mason parent_transid, 0)) 412256dd1bbSStefan Behrens ret = -EIO; 413e064d5e9SDavid Sterba else if (btrfs_verify_level_key(eb, level, 414ff76a864SLiu Bo first_key, parent_transid)) 415581c1760SQu Wenruo ret = -EUCLEAN; 416581c1760SQu Wenruo else 417581c1760SQu Wenruo break; 418256dd1bbSStefan Behrens } 419d397712bSChris Mason 4200b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 421f188591eSChris Mason eb->start, eb->len); 4224235298eSChris Mason if (num_copies == 1) 423ea466794SJosef Bacik break; 4244235298eSChris Mason 4255cf1ab56SJosef Bacik if (!failed_mirror) { 4265cf1ab56SJosef Bacik failed = 1; 4275cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4285cf1ab56SJosef Bacik } 4295cf1ab56SJosef Bacik 430f188591eSChris Mason mirror_num++; 431ea466794SJosef Bacik if (mirror_num == failed_mirror) 432ea466794SJosef Bacik mirror_num++; 433ea466794SJosef Bacik 4344235298eSChris Mason if (mirror_num > num_copies) 435ea466794SJosef Bacik break; 436f188591eSChris Mason } 437ea466794SJosef Bacik 438c0901581SStefan Behrens if (failed && !ret && failed_mirror) 43920a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 440ea466794SJosef Bacik 441ea466794SJosef Bacik return ret; 442f188591eSChris Mason } 44319c00ddcSChris Mason 444eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 445eca0f6f6SQu Wenruo { 446eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 447eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 448eca0f6f6SQu Wenruo int ret; 449eca0f6f6SQu Wenruo 450eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 451eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 452eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 453eca0f6f6SQu Wenruo csum_tree_block(eb, result); 454eca0f6f6SQu Wenruo 455eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 456eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 457eca0f6f6SQu Wenruo else 458eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 459eca0f6f6SQu Wenruo 460eca0f6f6SQu Wenruo if (ret < 0) { 461eca0f6f6SQu Wenruo btrfs_print_tree(eb, 0); 462eca0f6f6SQu Wenruo btrfs_err(fs_info, 463eca0f6f6SQu Wenruo "block=%llu write time tree block corruption detected", 464eca0f6f6SQu Wenruo eb->start); 465eca0f6f6SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 466eca0f6f6SQu Wenruo return ret; 467eca0f6f6SQu Wenruo } 468eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 469eca0f6f6SQu Wenruo 470eca0f6f6SQu Wenruo return 0; 471eca0f6f6SQu Wenruo } 472eca0f6f6SQu Wenruo 473eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 474eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 475eca0f6f6SQu Wenruo struct bio_vec *bvec) 476eca0f6f6SQu Wenruo { 477eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 478eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 479eca0f6f6SQu Wenruo u64 cur; 480eca0f6f6SQu Wenruo int ret = 0; 481eca0f6f6SQu Wenruo 482eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 483eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 484eca0f6f6SQu Wenruo struct extent_buffer *eb; 485eca0f6f6SQu Wenruo bool uptodate; 486eca0f6f6SQu Wenruo 487eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 488eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 489eca0f6f6SQu Wenruo fs_info->nodesize); 490eca0f6f6SQu Wenruo 491eca0f6f6SQu Wenruo /* A dirty eb shouldn't disappear from buffer_radix */ 492eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 493eca0f6f6SQu Wenruo return -EUCLEAN; 494eca0f6f6SQu Wenruo 495eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 496eca0f6f6SQu Wenruo free_extent_buffer(eb); 497eca0f6f6SQu Wenruo return -EUCLEAN; 498eca0f6f6SQu Wenruo } 499eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 500eca0f6f6SQu Wenruo free_extent_buffer(eb); 501eca0f6f6SQu Wenruo return -EUCLEAN; 502eca0f6f6SQu Wenruo } 503eca0f6f6SQu Wenruo 504eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 505eca0f6f6SQu Wenruo free_extent_buffer(eb); 506eca0f6f6SQu Wenruo if (ret < 0) 507eca0f6f6SQu Wenruo return ret; 508eca0f6f6SQu Wenruo } 509eca0f6f6SQu Wenruo return ret; 510eca0f6f6SQu Wenruo } 511eca0f6f6SQu Wenruo 512d352ac68SChris Mason /* 513ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 514ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 515ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 516d352ac68SChris Mason */ 517ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 51819c00ddcSChris Mason { 519ac303b69SQu Wenruo struct page *page = bvec->bv_page; 5204eee4fa4SMiao Xie u64 start = page_offset(page); 52119c00ddcSChris Mason u64 found_start; 52219c00ddcSChris Mason struct extent_buffer *eb; 523eca0f6f6SQu Wenruo 524eca0f6f6SQu Wenruo if (fs_info->sectorsize < PAGE_SIZE) 525eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 526f188591eSChris Mason 5274f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5284f2de97aSJosef Bacik if (page != eb->pages[0]) 5294f2de97aSJosef Bacik return 0; 5300f805531SAlex Lyakas 53119c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 532d3575156SNaohiro Aota 533d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 534d3575156SNaohiro Aota WARN_ON(found_start != 0); 535d3575156SNaohiro Aota return 0; 536d3575156SNaohiro Aota } 537d3575156SNaohiro Aota 5380f805531SAlex Lyakas /* 5390f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5400f805531SAlex Lyakas * It is useful to know what went wrong. 5410f805531SAlex Lyakas */ 5420f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5430f805531SAlex Lyakas return -EUCLEAN; 5440f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5450f805531SAlex Lyakas return -EUCLEAN; 5460f805531SAlex Lyakas 547eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 54819c00ddcSChris Mason } 54919c00ddcSChris Mason 550b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 5512b82032cSYan Zheng { 552b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 553944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 55444880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 555944d3f9fSNikolay Borisov u8 *metadata_uuid; 5562b82032cSYan Zheng 5579a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 5589a8658e3SDavid Sterba BTRFS_FSID_SIZE); 5597239ff4bSNikolay Borisov /* 560944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 561944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 562944d3f9fSNikolay Borisov * ->fsid in this case is fine 5637239ff4bSNikolay Borisov */ 564944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 5657239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 5667239ff4bSNikolay Borisov else 5677239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 5687239ff4bSNikolay Borisov 569944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 570944d3f9fSNikolay Borisov return 0; 571944d3f9fSNikolay Borisov 572944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 573944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 574944d3f9fSNikolay Borisov return 0; 575944d3f9fSNikolay Borisov 576944d3f9fSNikolay Borisov return 1; 5772b82032cSYan Zheng } 5782b82032cSYan Zheng 57977bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 58077bf40a2SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb) 581ce9adaa5SChris Mason { 58277bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 583ce9adaa5SChris Mason u64 found_start; 58477bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 58577bf40a2SQu Wenruo u8 found_level; 5862996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 587*dfd29eedSDavid Sterba const u8 *header_csum; 58877bf40a2SQu Wenruo int ret = 0; 589ea466794SJosef Bacik 590ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 591727011e0SChris Mason if (found_start != eb->start) { 592893bf4b1SSu Yue btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu", 593893bf4b1SSu Yue eb->start, found_start); 594f188591eSChris Mason ret = -EIO; 59577bf40a2SQu Wenruo goto out; 596ce9adaa5SChris Mason } 597b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 59802873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 59994647322SDavid Sterba eb->start); 6001259ab75SChris Mason ret = -EIO; 60177bf40a2SQu Wenruo goto out; 6021259ab75SChris Mason } 603ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6041c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 605893bf4b1SSu Yue btrfs_err(fs_info, "bad tree block level %d on %llu", 606893bf4b1SSu Yue (int)btrfs_header_level(eb), eb->start); 6071c24c3ceSJosef Bacik ret = -EIO; 60877bf40a2SQu Wenruo goto out; 6091c24c3ceSJosef Bacik } 610ce9adaa5SChris Mason 611c67b3892SDavid Sterba csum_tree_block(eb, result); 612*dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 613*dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 614a826d6dcSJosef Bacik 615*dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 6162996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 617ff14aa79SDavid Sterba "checksum verify failed on %llu wanted " CSUM_FMT " found " CSUM_FMT " level %d", 618ff14aa79SDavid Sterba eb->start, 619*dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 62035be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 62135be8851SJohannes Thumshirn btrfs_header_level(eb)); 6222996e1f8SJohannes Thumshirn ret = -EUCLEAN; 62377bf40a2SQu Wenruo goto out; 6242996e1f8SJohannes Thumshirn } 6252996e1f8SJohannes Thumshirn 626a826d6dcSJosef Bacik /* 627a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 628a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 629a826d6dcSJosef Bacik * return -EIO. 630a826d6dcSJosef Bacik */ 6311c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 632a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 633a826d6dcSJosef Bacik ret = -EIO; 634a826d6dcSJosef Bacik } 635ce9adaa5SChris Mason 636813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 637053ab70fSLiu Bo ret = -EIO; 638053ab70fSLiu Bo 6390b32f4bbSJosef Bacik if (!ret) 6400b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 64175391f0dSQu Wenruo else 64275391f0dSQu Wenruo btrfs_err(fs_info, 64375391f0dSQu Wenruo "block=%llu read time tree block corruption detected", 64475391f0dSQu Wenruo eb->start); 64577bf40a2SQu Wenruo out: 64677bf40a2SQu Wenruo return ret; 64777bf40a2SQu Wenruo } 64877bf40a2SQu Wenruo 649371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 650371cdc07SQu Wenruo int mirror) 651371cdc07SQu Wenruo { 652371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 653371cdc07SQu Wenruo struct extent_buffer *eb; 654371cdc07SQu Wenruo bool reads_done; 655371cdc07SQu Wenruo int ret = 0; 656371cdc07SQu Wenruo 657371cdc07SQu Wenruo /* 658371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 659371cdc07SQu Wenruo * only get one eb for each endio hook. 660371cdc07SQu Wenruo */ 661371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 662371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 663371cdc07SQu Wenruo 664371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 665371cdc07SQu Wenruo /* 666371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 667371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 668371cdc07SQu Wenruo */ 669371cdc07SQu Wenruo ASSERT(eb); 670371cdc07SQu Wenruo 671371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 672371cdc07SQu Wenruo /* Subpage read must finish in page read */ 673371cdc07SQu Wenruo ASSERT(reads_done); 674371cdc07SQu Wenruo 675371cdc07SQu Wenruo eb->read_mirror = mirror; 676371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 677371cdc07SQu Wenruo ret = -EIO; 678371cdc07SQu Wenruo goto err; 679371cdc07SQu Wenruo } 680371cdc07SQu Wenruo ret = validate_extent_buffer(eb); 681371cdc07SQu Wenruo if (ret < 0) 682371cdc07SQu Wenruo goto err; 683371cdc07SQu Wenruo 684371cdc07SQu Wenruo if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 685371cdc07SQu Wenruo btree_readahead_hook(eb, ret); 686371cdc07SQu Wenruo 687371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 688371cdc07SQu Wenruo 689371cdc07SQu Wenruo free_extent_buffer(eb); 690371cdc07SQu Wenruo return ret; 691371cdc07SQu Wenruo err: 692371cdc07SQu Wenruo /* 693371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 694371cdc07SQu Wenruo * make sure it has something to decrement. 695371cdc07SQu Wenruo */ 696371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 697371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 698371cdc07SQu Wenruo free_extent_buffer(eb); 699371cdc07SQu Wenruo return ret; 700371cdc07SQu Wenruo } 701371cdc07SQu Wenruo 7028e1dc982SQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio, 70377bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 70477bf40a2SQu Wenruo int mirror) 70577bf40a2SQu Wenruo { 70677bf40a2SQu Wenruo struct extent_buffer *eb; 70777bf40a2SQu Wenruo int ret = 0; 70877bf40a2SQu Wenruo int reads_done; 70977bf40a2SQu Wenruo 71077bf40a2SQu Wenruo ASSERT(page->private); 711371cdc07SQu Wenruo 712371cdc07SQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->sectorsize < PAGE_SIZE) 713371cdc07SQu Wenruo return validate_subpage_buffer(page, start, end, mirror); 714371cdc07SQu Wenruo 71577bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 71677bf40a2SQu Wenruo 71777bf40a2SQu Wenruo /* 71877bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 71977bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 72077bf40a2SQu Wenruo */ 72177bf40a2SQu Wenruo atomic_inc(&eb->refs); 72277bf40a2SQu Wenruo 72377bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 72477bf40a2SQu Wenruo if (!reads_done) 72577bf40a2SQu Wenruo goto err; 72677bf40a2SQu Wenruo 72777bf40a2SQu Wenruo eb->read_mirror = mirror; 72877bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 72977bf40a2SQu Wenruo ret = -EIO; 73077bf40a2SQu Wenruo goto err; 73177bf40a2SQu Wenruo } 73277bf40a2SQu Wenruo ret = validate_extent_buffer(eb); 733ce9adaa5SChris Mason err: 73479fb65a1SJosef Bacik if (reads_done && 73579fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 736d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 7374bb31e92SArne Jansen 73853b381b3SDavid Woodhouse if (ret) { 73953b381b3SDavid Woodhouse /* 74053b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 74153b381b3SDavid Woodhouse * again, we have to make sure it has something 74253b381b3SDavid Woodhouse * to decrement 74353b381b3SDavid Woodhouse */ 74453b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7450b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 74653b381b3SDavid Woodhouse } 7470b32f4bbSJosef Bacik free_extent_buffer(eb); 74877bf40a2SQu Wenruo 749f188591eSChris Mason return ret; 750ce9adaa5SChris Mason } 751ce9adaa5SChris Mason 7524246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 753ce9adaa5SChris Mason { 75497eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 755ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7569e0af237SLiu Bo struct btrfs_workqueue *wq; 757ce9adaa5SChris Mason 758ce9adaa5SChris Mason fs_info = end_io_wq->info; 7594e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 760d20f7043SChris Mason 761cfe94440SNaohiro Aota if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 762a0cac0ecSOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 7639e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 764a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7659e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 766a0cac0ecSOmar Sandoval else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7679e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 768a0cac0ecSOmar Sandoval else 7699e0af237SLiu Bo wq = fs_info->endio_write_workers; 7709e0af237SLiu Bo } else { 7715c047a69SOmar Sandoval if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 7729e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 773a0cac0ecSOmar Sandoval else if (end_io_wq->metadata) 7749e0af237SLiu Bo wq = fs_info->endio_meta_workers; 775a0cac0ecSOmar Sandoval else 7769e0af237SLiu Bo wq = fs_info->endio_workers; 7779e0af237SLiu Bo } 7789e0af237SLiu Bo 779a0cac0ecSOmar Sandoval btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 7809e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 781ce9adaa5SChris Mason } 782ce9adaa5SChris Mason 7834e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 784bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7850b86a832SChris Mason { 78697eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7878b110e39SMiao Xie 78897eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 789ce9adaa5SChris Mason if (!end_io_wq) 7904e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 791ce9adaa5SChris Mason 792ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 793ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 79422c59948SChris Mason end_io_wq->info = info; 7954e4cbee9SChristoph Hellwig end_io_wq->status = 0; 796ce9adaa5SChris Mason end_io_wq->bio = bio; 79722c59948SChris Mason end_io_wq->metadata = metadata; 798ce9adaa5SChris Mason 799ce9adaa5SChris Mason bio->bi_private = end_io_wq; 800ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 80122c59948SChris Mason return 0; 80222c59948SChris Mason } 80322c59948SChris Mason 8044a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 8054a69a410SChris Mason { 8064a69a410SChris Mason struct async_submit_bio *async; 8074e4cbee9SChristoph Hellwig blk_status_t ret; 8084a69a410SChris Mason 8094a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8101941b64bSQu Wenruo ret = async->submit_bio_start(async->inode, async->bio, 8111941b64bSQu Wenruo async->dio_file_offset); 81279787eaaSJeff Mahoney if (ret) 8134e4cbee9SChristoph Hellwig async->status = ret; 8144a69a410SChris Mason } 8154a69a410SChris Mason 81606ea01b1SDavid Sterba /* 81706ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 81806ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 81906ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 82006ea01b1SDavid Sterba * 82106ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 82206ea01b1SDavid Sterba * inserted into the tree. 82306ea01b1SDavid Sterba */ 8244a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8258b712842SChris Mason { 8268b712842SChris Mason struct async_submit_bio *async; 82706ea01b1SDavid Sterba struct inode *inode; 82806ea01b1SDavid Sterba blk_status_t ret; 8298b712842SChris Mason 8308b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8318896a08dSQu Wenruo inode = async->inode; 8324854ddd0SChris Mason 833bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 8344e4cbee9SChristoph Hellwig if (async->status) { 8354e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 8364246a0b6SChristoph Hellwig bio_endio(async->bio); 83779787eaaSJeff Mahoney return; 83879787eaaSJeff Mahoney } 83979787eaaSJeff Mahoney 840ec39f769SChris Mason /* 841ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 842ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 843ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 844ec39f769SChris Mason */ 845ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 84608635baeSChris Mason ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio, async->mirror_num); 84706ea01b1SDavid Sterba if (ret) { 84806ea01b1SDavid Sterba async->bio->bi_status = ret; 84906ea01b1SDavid Sterba bio_endio(async->bio); 85006ea01b1SDavid Sterba } 8514a69a410SChris Mason } 8524a69a410SChris Mason 8534a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8544a69a410SChris Mason { 8554a69a410SChris Mason struct async_submit_bio *async; 8564a69a410SChris Mason 8574a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8588b712842SChris Mason kfree(async); 8598b712842SChris Mason } 8608b712842SChris Mason 8618896a08dSQu Wenruo blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio, 862c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 8631941b64bSQu Wenruo u64 dio_file_offset, 864e288c080SDavid Sterba extent_submit_bio_start_t *submit_bio_start) 86544b8bd7eSChris Mason { 8668896a08dSQu Wenruo struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 86744b8bd7eSChris Mason struct async_submit_bio *async; 86844b8bd7eSChris Mason 86944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 87044b8bd7eSChris Mason if (!async) 8714e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 87244b8bd7eSChris Mason 8738896a08dSQu Wenruo async->inode = inode; 87444b8bd7eSChris Mason async->bio = bio; 87544b8bd7eSChris Mason async->mirror_num = mirror_num; 8764a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8774a69a410SChris Mason 878a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 879a0cac0ecSOmar Sandoval run_one_async_free); 8804a69a410SChris Mason 8811941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8828c8bee1dSChris Mason 8834e4cbee9SChristoph Hellwig async->status = 0; 88479787eaaSJeff Mahoney 88567f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8865cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 887d313d7a3SChris Mason 8885cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 88944b8bd7eSChris Mason return 0; 89044b8bd7eSChris Mason } 89144b8bd7eSChris Mason 8924e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 893ce3ed71aSChris Mason { 8942c30c71bSKent Overstreet struct bio_vec *bvec; 895ce3ed71aSChris Mason struct btrfs_root *root; 8962b070cfeSChristoph Hellwig int ret = 0; 8976dc4f100SMing Lei struct bvec_iter_all iter_all; 898ce3ed71aSChris Mason 899c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 9002b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 901ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 902ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 90379787eaaSJeff Mahoney if (ret) 90479787eaaSJeff Mahoney break; 905ce3ed71aSChris Mason } 9062c30c71bSKent Overstreet 9074e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 908ce3ed71aSChris Mason } 909ce3ed71aSChris Mason 9108896a08dSQu Wenruo static blk_status_t btree_submit_bio_start(struct inode *inode, struct bio *bio, 9111941b64bSQu Wenruo u64 dio_file_offset) 91222c59948SChris Mason { 9138b712842SChris Mason /* 9148b712842SChris Mason * when we're called for a write, we're already in the async 9155443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9168b712842SChris Mason */ 91779787eaaSJeff Mahoney return btree_csum_one_bio(bio); 91822c59948SChris Mason } 91922c59948SChris Mason 920f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 9219b4e675aSDavid Sterba struct btrfs_inode *bi) 922de0022b9SJosef Bacik { 9234eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 924f4dcfb30SJohannes Thumshirn return false; 9256300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 926f4dcfb30SJohannes Thumshirn return false; 9279b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 928f4dcfb30SJohannes Thumshirn return false; 929f4dcfb30SJohannes Thumshirn return true; 930de0022b9SJosef Bacik } 931de0022b9SJosef Bacik 9321b36294aSNikolay Borisov blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio, 9331b36294aSNikolay Borisov int mirror_num, unsigned long bio_flags) 93444b8bd7eSChris Mason { 9350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 9364e4cbee9SChristoph Hellwig blk_status_t ret; 937cad321adSChris Mason 938cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 939cad321adSChris Mason /* 940cad321adSChris Mason * called for a read, do the setup so that checksum validation 941cad321adSChris Mason * can happen in the async kernel threads 942cad321adSChris Mason */ 9430b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9440b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9451d4284bdSChris Mason if (ret) 94661891923SStefan Behrens goto out_w_error; 94708635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 948f4dcfb30SJohannes Thumshirn } else if (!should_async_write(fs_info, BTRFS_I(inode))) { 949de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 950de0022b9SJosef Bacik if (ret) 95161891923SStefan Behrens goto out_w_error; 95208635baeSChris Mason ret = btrfs_map_bio(fs_info, bio, mirror_num); 95361891923SStefan Behrens } else { 954cad321adSChris Mason /* 95561891923SStefan Behrens * kthread helpers are used to submit writes so that 95661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 957cad321adSChris Mason */ 9588896a08dSQu Wenruo ret = btrfs_wq_submit_bio(inode, bio, mirror_num, 0, 9598896a08dSQu Wenruo 0, btree_submit_bio_start); 96044b8bd7eSChris Mason } 96144b8bd7eSChris Mason 9624246a0b6SChristoph Hellwig if (ret) 9634246a0b6SChristoph Hellwig goto out_w_error; 9644246a0b6SChristoph Hellwig return 0; 9654246a0b6SChristoph Hellwig 96661891923SStefan Behrens out_w_error: 9674e4cbee9SChristoph Hellwig bio->bi_status = ret; 9684246a0b6SChristoph Hellwig bio_endio(bio); 96961891923SStefan Behrens return ret; 97061891923SStefan Behrens } 97161891923SStefan Behrens 9723dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 973784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 974a6bc32b8SMel Gorman struct page *newpage, struct page *page, 975a6bc32b8SMel Gorman enum migrate_mode mode) 976784b4e29SChris Mason { 977784b4e29SChris Mason /* 978784b4e29SChris Mason * we can't safely write a btree page from here, 979784b4e29SChris Mason * we haven't done the locking hook 980784b4e29SChris Mason */ 981784b4e29SChris Mason if (PageDirty(page)) 982784b4e29SChris Mason return -EAGAIN; 983784b4e29SChris Mason /* 984784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 985784b4e29SChris Mason * We must have no buffers or drop them. 986784b4e29SChris Mason */ 987784b4e29SChris Mason if (page_has_private(page) && 988784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 989784b4e29SChris Mason return -EAGAIN; 990a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 991784b4e29SChris Mason } 9923dd1462eSJan Beulich #endif 993784b4e29SChris Mason 9940da5468fSChris Mason 9950da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9960da5468fSChris Mason struct writeback_control *wbc) 9970da5468fSChris Mason { 998e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 999e2d84521SMiao Xie int ret; 1000e2d84521SMiao Xie 1001d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 1002448d640bSChris Mason 1003448d640bSChris Mason if (wbc->for_kupdate) 1004448d640bSChris Mason return 0; 1005448d640bSChris Mason 1006e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 1007b9473439SChris Mason /* this is a bit racy, but that's ok */ 1008d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1009d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 1010d814a491SEthan Lien fs_info->dirty_metadata_batch); 1011e2d84521SMiao Xie if (ret < 0) 1012793955bcSChris Mason return 0; 1013793955bcSChris Mason } 10140b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 10150da5468fSChris Mason } 10160da5468fSChris Mason 101770dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10185f39d397SChris Mason { 101998509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 102098509cfcSChris Mason return 0; 10210c4e538bSDavid Sterba 1022f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1023d98237b3SChris Mason } 1024d98237b3SChris Mason 1025d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1026d47992f8SLukas Czerner unsigned int length) 1027d98237b3SChris Mason { 1028d1310b2eSChris Mason struct extent_io_tree *tree; 1029d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10305f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10315f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10329ad6b7bcSChris Mason if (PagePrivate(page)) { 1033efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1034efe120a0SFrank Holton "page private not zero on page %llu", 1035efe120a0SFrank Holton (unsigned long long)page_offset(page)); 1036d1b89bc0SGuoqing Jiang detach_page_private(page); 10379ad6b7bcSChris Mason } 1038d98237b3SChris Mason } 1039d98237b3SChris Mason 10400b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10410b32f4bbSJosef Bacik { 1042bb146eb2SJosef Bacik #ifdef DEBUG 1043139e8cd3SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 1044139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 10450b32f4bbSJosef Bacik struct extent_buffer *eb; 1046139e8cd3SQu Wenruo int cur_bit = 0; 1047139e8cd3SQu Wenruo u64 page_start = page_offset(page); 10480b32f4bbSJosef Bacik 1049139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 10500b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10510b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10520b32f4bbSJosef Bacik BUG_ON(!eb); 10530b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10540b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10550b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1056139e8cd3SQu Wenruo return __set_page_dirty_nobuffers(page); 1057139e8cd3SQu Wenruo } 1058139e8cd3SQu Wenruo ASSERT(PagePrivate(page) && page->private); 1059139e8cd3SQu Wenruo subpage = (struct btrfs_subpage *)page->private; 1060139e8cd3SQu Wenruo 1061139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 1062139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 1063139e8cd3SQu Wenruo unsigned long flags; 1064139e8cd3SQu Wenruo u64 cur; 1065139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 1066139e8cd3SQu Wenruo 1067139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 1068139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 1069139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1070139e8cd3SQu Wenruo cur_bit++; 1071139e8cd3SQu Wenruo continue; 1072139e8cd3SQu Wenruo } 1073139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 1074139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 1075139e8cd3SQu Wenruo 1076139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 1077139e8cd3SQu Wenruo ASSERT(eb); 1078139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 1079139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 1080139e8cd3SQu Wenruo btrfs_assert_tree_locked(eb); 1081139e8cd3SQu Wenruo free_extent_buffer(eb); 1082139e8cd3SQu Wenruo 1083139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 1084139e8cd3SQu Wenruo } 1085bb146eb2SJosef Bacik #endif 10860b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10870b32f4bbSJosef Bacik } 10880b32f4bbSJosef Bacik 10897f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 10900da5468fSChris Mason .writepages = btree_writepages, 10915f39d397SChris Mason .releasepage = btree_releasepage, 10925f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10935a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1094784b4e29SChris Mason .migratepage = btree_migratepage, 10955a92bc88SChris Mason #endif 10960b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1097d98237b3SChris Mason }; 1098123abc88SChris Mason 10992ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 11002ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 11013fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 11023fbaf258SJosef Bacik int level) 11030999df54SChris Mason { 11040b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 11050b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 11063fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 11070999df54SChris Mason } 11080999df54SChris Mason 1109581c1760SQu Wenruo /* 1110581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1111581c1760SQu Wenruo * verification. 1112581c1760SQu Wenruo * 11131b7ec85eSJosef Bacik * @owner_root: the objectid of the root owner for this block. 1114581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1115581c1760SQu Wenruo * @level: expected level, mandatory check 1116581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1117581c1760SQu Wenruo */ 11182ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 11191b7ec85eSJosef Bacik u64 owner_root, u64 parent_transid, 11201b7ec85eSJosef Bacik int level, struct btrfs_key *first_key) 1121e20d96d6SChris Mason { 11225f39d397SChris Mason struct extent_buffer *buf = NULL; 112319c00ddcSChris Mason int ret; 112419c00ddcSChris Mason 11253fbaf258SJosef Bacik buf = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); 1126c871b0f2SLiu Bo if (IS_ERR(buf)) 1127c871b0f2SLiu Bo return buf; 1128e4204dedSChris Mason 11295ab12d1fSDavid Sterba ret = btree_read_extent_buffer_pages(buf, parent_transid, 1130581c1760SQu Wenruo level, first_key); 11310f0fe8f7SFilipe David Borba Manana if (ret) { 1132537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 113364c043deSLiu Bo return ERR_PTR(ret); 11340f0fe8f7SFilipe David Borba Manana } 11355f39d397SChris Mason return buf; 1136ce9adaa5SChris Mason 1137eb60ceacSChris Mason } 1138eb60ceacSChris Mason 11396a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1140ed2ff2cbSChris Mason { 11416a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 114255c69072SChris Mason if (btrfs_header_generation(buf) == 1143e2d84521SMiao Xie fs_info->running_transaction->transid) { 1144b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1145b4ce94deSChris Mason 1146b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1147104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1148e2d84521SMiao Xie -buf->len, 1149e2d84521SMiao Xie fs_info->dirty_metadata_batch); 11500b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1151925baeddSChris Mason } 11525f39d397SChris Mason } 1153ed7b63ebSJosef Bacik } 11545f39d397SChris Mason 1155da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1156e20d96d6SChris Mason u64 objectid) 1157d97e63b6SChris Mason { 11587c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 115996dfcb46SJosef Bacik root->fs_info = fs_info; 1160cfaa7295SChris Mason root->node = NULL; 1161a28ec197SChris Mason root->commit_root = NULL; 116227cdeb70SMiao Xie root->state = 0; 1163d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 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); 1171f0486c68SYan, Zheng root->block_rsv = NULL; 11720b86a832SChris Mason 11730b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11745d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1175eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1176eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1177199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1178199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1179d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 11802ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11812ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 11825d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1183eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1184199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1185f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11862ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11872ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 11888287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1189a2135011SChris Mason mutex_init(&root->objectid_mutex); 1190e02119d5SChris Mason mutex_init(&root->log_mutex); 119131f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1192573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1193c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 11947237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11957237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11967237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11978b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11988b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11997237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12007237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12017237f183SYan Zheng atomic_set(&root->log_writers, 0); 12022ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12030700cea7SElena Reshetova refcount_set(&root->refs, 1); 12048ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1205eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 12067237f183SYan Zheng root->log_transid = 0; 1207d1433debSMiao Xie root->log_transid_committed = -1; 1208257c62e1SChris Mason root->last_log_commit = 0; 1209e289f03eSFilipe Manana if (!dummy) { 121043eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 121143eb5f29SQu Wenruo IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL); 1212e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 1213e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, NULL); 1214e289f03eSFilipe Manana } 1215017e5369SChris Mason 12163768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12173768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12186702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 12194d775673SChris Mason root->root_key.objectid = objectid; 12200ee5dc67SAl Viro root->anon_dev = 0; 12218ea05e3aSAlexander Block 12225f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1223370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1224bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1225bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1226bd647ce3SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 1227bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1228bd647ce3SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 1229bd647ce3SJosef Bacik #endif 12303768f368SChris Mason } 12313768f368SChris Mason 123274e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 123396dfcb46SJosef Bacik u64 objectid, gfp_t flags) 12346f07e42eSAl Viro { 123574e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12366f07e42eSAl Viro if (root) 123796dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 12386f07e42eSAl Viro return root; 12396f07e42eSAl Viro } 12406f07e42eSAl Viro 124106ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 124206ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1243da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 124406ea65a3SJosef Bacik { 124506ea65a3SJosef Bacik struct btrfs_root *root; 124606ea65a3SJosef Bacik 12477c0260eeSJeff Mahoney if (!fs_info) 12487c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12497c0260eeSJeff Mahoney 125096dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 125106ea65a3SJosef Bacik if (!root) 125206ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1253da17066cSJeff Mahoney 1254b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1255faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 125606ea65a3SJosef Bacik 125706ea65a3SJosef Bacik return root; 125806ea65a3SJosef Bacik } 125906ea65a3SJosef Bacik #endif 126006ea65a3SJosef Bacik 126120897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 126220897f5cSArne Jansen u64 objectid) 126320897f5cSArne Jansen { 12649b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 126520897f5cSArne Jansen struct extent_buffer *leaf; 126620897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 126720897f5cSArne Jansen struct btrfs_root *root; 126820897f5cSArne Jansen struct btrfs_key key; 1269b89f6d1fSFilipe Manana unsigned int nofs_flag; 127020897f5cSArne Jansen int ret = 0; 127120897f5cSArne Jansen 1272b89f6d1fSFilipe Manana /* 1273b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1274b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1275b89f6d1fSFilipe Manana */ 1276b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 127796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1278b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 127920897f5cSArne Jansen if (!root) 128020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 128120897f5cSArne Jansen 128220897f5cSArne Jansen root->root_key.objectid = objectid; 128320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 128420897f5cSArne Jansen root->root_key.offset = 0; 128520897f5cSArne Jansen 12869631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12879631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 128820897f5cSArne Jansen if (IS_ERR(leaf)) { 128920897f5cSArne Jansen ret = PTR_ERR(leaf); 12901dd05682STsutomu Itoh leaf = NULL; 12918a6a87cdSBoris Burkov goto fail_unlock; 129220897f5cSArne Jansen } 129320897f5cSArne Jansen 129420897f5cSArne Jansen root->node = leaf; 129520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 129620897f5cSArne Jansen 129720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129827cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 129920897f5cSArne Jansen 1300f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1301f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 130220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 130320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 130420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 130520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 130620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 130720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130933d85fdaSQu Wenruo if (is_fstree(objectid)) 1310807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1311807fc790SAndy Shevchenko else 1312807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1313c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 131420897f5cSArne Jansen 13158a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13168a6a87cdSBoris Burkov 131720897f5cSArne Jansen key.objectid = objectid; 131820897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131920897f5cSArne Jansen key.offset = 0; 132020897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 132120897f5cSArne Jansen if (ret) 132220897f5cSArne Jansen goto fail; 132320897f5cSArne Jansen 132420897f5cSArne Jansen return root; 13251dd05682STsutomu Itoh 13268a6a87cdSBoris Burkov fail_unlock: 13278c38938cSJosef Bacik if (leaf) 13281dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13298a6a87cdSBoris Burkov fail: 133000246528SJosef Bacik btrfs_put_root(root); 13311dd05682STsutomu Itoh 13321dd05682STsutomu Itoh return ERR_PTR(ret); 133320897f5cSArne Jansen } 133420897f5cSArne Jansen 13357237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1336e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13370f7d52f4SChris Mason { 13380f7d52f4SChris Mason struct btrfs_root *root; 1339e02119d5SChris Mason 134096dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1341e02119d5SChris Mason if (!root) 13427237f183SYan Zheng return ERR_PTR(-ENOMEM); 1343e02119d5SChris Mason 1344e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1345e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1346e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 134727cdeb70SMiao Xie 13486ab6ebb7SNaohiro Aota return root; 13496ab6ebb7SNaohiro Aota } 13506ab6ebb7SNaohiro Aota 13516ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 13526ab6ebb7SNaohiro Aota struct btrfs_root *root) 13536ab6ebb7SNaohiro Aota { 13546ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 13556ab6ebb7SNaohiro Aota 13567237f183SYan Zheng /* 135792a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 135827cdeb70SMiao Xie * 135992a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 136092a7cc42SQu Wenruo * and they go away before a real commit is actually done. 136192a7cc42SQu Wenruo * 136292a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 136392a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 13647237f183SYan Zheng */ 1365e02119d5SChris Mason 13664d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13679631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 13686ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 13696ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1370e02119d5SChris Mason 13717237f183SYan Zheng root->node = leaf; 1372e02119d5SChris Mason 1373e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1374e02119d5SChris Mason btrfs_tree_unlock(root->node); 13756ab6ebb7SNaohiro Aota 13766ab6ebb7SNaohiro Aota return 0; 13777237f183SYan Zheng } 13787237f183SYan Zheng 13797237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13807237f183SYan Zheng struct btrfs_fs_info *fs_info) 13817237f183SYan Zheng { 13827237f183SYan Zheng struct btrfs_root *log_root; 13837237f183SYan Zheng 13847237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13857237f183SYan Zheng if (IS_ERR(log_root)) 13867237f183SYan Zheng return PTR_ERR(log_root); 13876ab6ebb7SNaohiro Aota 13883ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 13893ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 13903ddebf27SNaohiro Aota 13916ab6ebb7SNaohiro Aota if (ret) { 13926ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13936ab6ebb7SNaohiro Aota return ret; 13946ab6ebb7SNaohiro Aota } 13953ddebf27SNaohiro Aota } 13966ab6ebb7SNaohiro Aota 13977237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13987237f183SYan Zheng fs_info->log_root_tree = log_root; 13997237f183SYan Zheng return 0; 14007237f183SYan Zheng } 14017237f183SYan Zheng 14027237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14037237f183SYan Zheng struct btrfs_root *root) 14047237f183SYan Zheng { 14050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14067237f183SYan Zheng struct btrfs_root *log_root; 14077237f183SYan Zheng struct btrfs_inode_item *inode_item; 14086ab6ebb7SNaohiro Aota int ret; 14097237f183SYan Zheng 14100b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14117237f183SYan Zheng if (IS_ERR(log_root)) 14127237f183SYan Zheng return PTR_ERR(log_root); 14137237f183SYan Zheng 14146ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14156ab6ebb7SNaohiro Aota if (ret) { 14166ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14176ab6ebb7SNaohiro Aota return ret; 14186ab6ebb7SNaohiro Aota } 14196ab6ebb7SNaohiro Aota 14207237f183SYan Zheng log_root->last_trans = trans->transid; 14217237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14227237f183SYan Zheng 14237237f183SYan Zheng inode_item = &log_root->root_item.inode; 14243cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14253cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14263cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1427da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14280b246afaSJeff Mahoney fs_info->nodesize); 14293cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14307237f183SYan Zheng 14315d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14327237f183SYan Zheng 14337237f183SYan Zheng WARN_ON(root->log_root); 14347237f183SYan Zheng root->log_root = log_root; 14357237f183SYan Zheng root->log_transid = 0; 1436d1433debSMiao Xie root->log_transid_committed = -1; 1437257c62e1SChris Mason root->last_log_commit = 0; 1438e02119d5SChris Mason return 0; 1439e02119d5SChris Mason } 1440e02119d5SChris Mason 144149d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 144249d11beaSJosef Bacik struct btrfs_path *path, 1443cb517eabSMiao Xie struct btrfs_key *key) 1444e02119d5SChris Mason { 1445e02119d5SChris Mason struct btrfs_root *root; 1446e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 144784234f3aSYan Zheng u64 generation; 1448cb517eabSMiao Xie int ret; 1449581c1760SQu Wenruo int level; 1450cb517eabSMiao Xie 145196dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 145249d11beaSJosef Bacik if (!root) 145349d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 14540f7d52f4SChris Mason 1455cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1456cb517eabSMiao Xie &root->root_item, &root->root_key); 14570f7d52f4SChris Mason if (ret) { 145813a8a7c8SYan, Zheng if (ret > 0) 145913a8a7c8SYan, Zheng ret = -ENOENT; 146049d11beaSJosef Bacik goto fail; 14610f7d52f4SChris Mason } 146213a8a7c8SYan, Zheng 146384234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1464581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14652ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14662ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 14671b7ec85eSJosef Bacik key->objectid, generation, level, NULL); 146864c043deSLiu Bo if (IS_ERR(root->node)) { 146964c043deSLiu Bo ret = PTR_ERR(root->node); 14708c38938cSJosef Bacik root->node = NULL; 147149d11beaSJosef Bacik goto fail; 1472cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1473cb517eabSMiao Xie ret = -EIO; 147449d11beaSJosef Bacik goto fail; 1475416bc658SJosef Bacik } 14765d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1477cb517eabSMiao Xie return root; 147849d11beaSJosef Bacik fail: 147900246528SJosef Bacik btrfs_put_root(root); 148049d11beaSJosef Bacik return ERR_PTR(ret); 148149d11beaSJosef Bacik } 148249d11beaSJosef Bacik 148349d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 148449d11beaSJosef Bacik struct btrfs_key *key) 148549d11beaSJosef Bacik { 148649d11beaSJosef Bacik struct btrfs_root *root; 148749d11beaSJosef Bacik struct btrfs_path *path; 148849d11beaSJosef Bacik 148949d11beaSJosef Bacik path = btrfs_alloc_path(); 149049d11beaSJosef Bacik if (!path) 149149d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 149249d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 149349d11beaSJosef Bacik btrfs_free_path(path); 149449d11beaSJosef Bacik 149549d11beaSJosef Bacik return root; 1496cb517eabSMiao Xie } 1497cb517eabSMiao Xie 14982dfb1e43SQu Wenruo /* 14992dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15002dfb1e43SQu Wenruo * 15012dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 15022dfb1e43SQu Wenruo */ 15032dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1504cb517eabSMiao Xie { 1505cb517eabSMiao Xie int ret; 1506dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1507cb517eabSMiao Xie 1508dcc3eb96SNikolay Borisov /* 1509dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1510dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1511dcc3eb96SNikolay Borisov */ 1512dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1513dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1514dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1515dcc3eb96SNikolay Borisov if (ret) 15168257b2dcSMiao Xie goto fail; 15178257b2dcSMiao Xie 1518aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 1519aeb935a4SQu Wenruo root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { 152092a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1521f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1522f39e4571SJosef Bacik } 1523f39e4571SJosef Bacik 1524851fd730SQu Wenruo /* 1525851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1526851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1527851fd730SQu Wenruo */ 1528851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1529851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 15302dfb1e43SQu Wenruo if (!anon_dev) { 1531cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1532cb517eabSMiao Xie if (ret) 1533876d2cf1SLiu Bo goto fail; 15342dfb1e43SQu Wenruo } else { 15352dfb1e43SQu Wenruo root->anon_dev = anon_dev; 15362dfb1e43SQu Wenruo } 1537851fd730SQu Wenruo } 1538f32e48e9SChandan Rajendra 1539f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1540453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1541f32e48e9SChandan Rajendra if (ret) { 1542f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1543876d2cf1SLiu Bo goto fail; 1544f32e48e9SChandan Rajendra } 1545f32e48e9SChandan Rajendra 15466b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1547f32e48e9SChandan Rajendra 1548f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1549f32e48e9SChandan Rajendra 1550cb517eabSMiao Xie return 0; 1551cb517eabSMiao Xie fail: 155284db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1553cb517eabSMiao Xie return ret; 1554cb517eabSMiao Xie } 1555cb517eabSMiao Xie 1556a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1557cb517eabSMiao Xie u64 root_id) 1558cb517eabSMiao Xie { 1559cb517eabSMiao Xie struct btrfs_root *root; 1560cb517eabSMiao Xie 1561cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1562cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1563cb517eabSMiao Xie (unsigned long)root_id); 1564bc44d7c4SJosef Bacik if (root) 156500246528SJosef Bacik root = btrfs_grab_root(root); 1566cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1567cb517eabSMiao Xie return root; 1568cb517eabSMiao Xie } 1569cb517eabSMiao Xie 157049d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 157149d11beaSJosef Bacik u64 objectid) 157249d11beaSJosef Bacik { 157349d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 157449d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 157549d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 157649d11beaSJosef Bacik return btrfs_grab_root(fs_info->extent_root); 157749d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 157849d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 157949d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 158049d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 158149d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 158249d11beaSJosef Bacik return btrfs_grab_root(fs_info->csum_root); 158349d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 158449d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 158549d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 158649d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 158749d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 158849d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 158949d11beaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 159049d11beaSJosef Bacik return btrfs_grab_root(fs_info->free_space_root) ? 159149d11beaSJosef Bacik fs_info->free_space_root : ERR_PTR(-ENOENT); 159249d11beaSJosef Bacik return NULL; 159349d11beaSJosef Bacik } 159449d11beaSJosef Bacik 1595cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1596cb517eabSMiao Xie struct btrfs_root *root) 1597cb517eabSMiao Xie { 1598cb517eabSMiao Xie int ret; 1599cb517eabSMiao Xie 1600e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1601cb517eabSMiao Xie if (ret) 1602cb517eabSMiao Xie return ret; 1603cb517eabSMiao Xie 1604cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1605cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1606cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1607cb517eabSMiao Xie root); 1608af01d2e5SJosef Bacik if (ret == 0) { 160900246528SJosef Bacik btrfs_grab_root(root); 161027cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1611af01d2e5SJosef Bacik } 1612cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1613cb517eabSMiao Xie radix_tree_preload_end(); 1614cb517eabSMiao Xie 1615cb517eabSMiao Xie return ret; 1616cb517eabSMiao Xie } 1617cb517eabSMiao Xie 1618bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1619bd647ce3SJosef Bacik { 1620bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1621bd647ce3SJosef Bacik struct btrfs_root *root; 1622bd647ce3SJosef Bacik 1623bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1624457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1625457f1864SJosef Bacik 1626bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1627bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1628457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 162971008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1630bd647ce3SJosef Bacik refcount_read(&root->refs)); 1631bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 163200246528SJosef Bacik btrfs_put_root(root); 163300246528SJosef Bacik btrfs_put_root(root); 1634bd647ce3SJosef Bacik } 1635bd647ce3SJosef Bacik #endif 1636bd647ce3SJosef Bacik } 1637bd647ce3SJosef Bacik 16380d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 16390d4b0463SJosef Bacik { 1640141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1641141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 16425deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1643141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1644141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1645141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1646141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 16470d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 16480d4b0463SJosef Bacik kfree(fs_info->delayed_root); 164900246528SJosef Bacik btrfs_put_root(fs_info->extent_root); 165000246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 165100246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 165200246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 165300246528SJosef Bacik btrfs_put_root(fs_info->csum_root); 165400246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 165500246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 165600246528SJosef Bacik btrfs_put_root(fs_info->free_space_root); 165700246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1658aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 1659bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 16603fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 16610d4b0463SJosef Bacik kfree(fs_info->super_copy); 16620d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 16630d4b0463SJosef Bacik kvfree(fs_info); 16640d4b0463SJosef Bacik } 16650d4b0463SJosef Bacik 16660d4b0463SJosef Bacik 16672dfb1e43SQu Wenruo /* 16682dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 16692dfb1e43SQu Wenruo * 16702dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 16712dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 16722dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 16732dfb1e43SQu Wenruo * 16742dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 16752dfb1e43SQu Wenruo * 16762dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 16772dfb1e43SQu Wenruo * same root objectid. 16782dfb1e43SQu Wenruo * 16792dfb1e43SQu Wenruo * @objectid: root id 16802dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 16812dfb1e43SQu Wenruo * pass 0 for new allocation. 16822dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 16832dfb1e43SQu Wenruo * for orphan roots 16842dfb1e43SQu Wenruo */ 16852dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 16862dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 16872dfb1e43SQu Wenruo bool check_ref) 16885eda7b5eSChris Mason { 16895eda7b5eSChris Mason struct btrfs_root *root; 1690381cf658SDavid Sterba struct btrfs_path *path; 16911d4c08e0SDavid Sterba struct btrfs_key key; 16925eda7b5eSChris Mason int ret; 16935eda7b5eSChris Mason 169449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 169549d11beaSJosef Bacik if (root) 169649d11beaSJosef Bacik return root; 16974df27c4dSYan, Zheng again: 169856e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 169948475471SStefan Behrens if (root) { 17002dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17012dfb1e43SQu Wenruo ASSERT(!anon_dev); 1702bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 170300246528SJosef Bacik btrfs_put_root(root); 170448475471SStefan Behrens return ERR_PTR(-ENOENT); 1705bc44d7c4SJosef Bacik } 17065eda7b5eSChris Mason return root; 170748475471SStefan Behrens } 17085eda7b5eSChris Mason 170956e9357aSDavid Sterba key.objectid = objectid; 171056e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 171156e9357aSDavid Sterba key.offset = (u64)-1; 171256e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 17135eda7b5eSChris Mason if (IS_ERR(root)) 17145eda7b5eSChris Mason return root; 17153394e160SChris Mason 1716c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1717d68fc57bSYan, Zheng ret = -ENOENT; 1718d68fc57bSYan, Zheng goto fail; 1719d68fc57bSYan, Zheng } 1720d68fc57bSYan, Zheng 17212dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1722cb517eabSMiao Xie if (ret) 1723cb517eabSMiao Xie goto fail; 1724cb517eabSMiao Xie 1725381cf658SDavid Sterba path = btrfs_alloc_path(); 1726381cf658SDavid Sterba if (!path) { 1727381cf658SDavid Sterba ret = -ENOMEM; 1728381cf658SDavid Sterba goto fail; 1729381cf658SDavid Sterba } 17301d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17311d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 173256e9357aSDavid Sterba key.offset = objectid; 17331d4c08e0SDavid Sterba 17341d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1735381cf658SDavid Sterba btrfs_free_path(path); 1736d68fc57bSYan, Zheng if (ret < 0) 1737d68fc57bSYan, Zheng goto fail; 1738d68fc57bSYan, Zheng if (ret == 0) 173927cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1740d68fc57bSYan, Zheng 1741cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17420f7d52f4SChris Mason if (ret) { 174300246528SJosef Bacik btrfs_put_root(root); 17444785e24fSJosef Bacik if (ret == -EEXIST) 17454df27c4dSYan, Zheng goto again; 17464df27c4dSYan, Zheng goto fail; 17474df27c4dSYan, Zheng } 1748edbd8d4eSChris Mason return root; 17494df27c4dSYan, Zheng fail: 17508c38938cSJosef Bacik btrfs_put_root(root); 17514df27c4dSYan, Zheng return ERR_PTR(ret); 1752edbd8d4eSChris Mason } 1753edbd8d4eSChris Mason 17542dfb1e43SQu Wenruo /* 17552dfb1e43SQu Wenruo * Get in-memory reference of a root structure 17562dfb1e43SQu Wenruo * 17572dfb1e43SQu Wenruo * @objectid: tree objectid 17582dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 17592dfb1e43SQu Wenruo * one reference 17602dfb1e43SQu Wenruo */ 17612dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 17622dfb1e43SQu Wenruo u64 objectid, bool check_ref) 17632dfb1e43SQu Wenruo { 17642dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 17652dfb1e43SQu Wenruo } 17662dfb1e43SQu Wenruo 17672dfb1e43SQu Wenruo /* 17682dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 17692dfb1e43SQu Wenruo * the anonymous block device id 17702dfb1e43SQu Wenruo * 17712dfb1e43SQu Wenruo * @objectid: tree objectid 17722dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 17732dfb1e43SQu Wenruo * parameter value 17742dfb1e43SQu Wenruo */ 17752dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 17762dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 17772dfb1e43SQu Wenruo { 17782dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 17792dfb1e43SQu Wenruo } 17802dfb1e43SQu Wenruo 17818b712842SChris Mason /* 178249d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 178349d11beaSJosef Bacik * @fs_info: the fs_info 178449d11beaSJosef Bacik * @objectid: the objectid we need to lookup 178549d11beaSJosef Bacik * 178649d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 178749d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 178849d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 178949d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 179049d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 179149d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 179249d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 179349d11beaSJosef Bacik * backref code is finished using the root. 179449d11beaSJosef Bacik */ 179549d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 179649d11beaSJosef Bacik struct btrfs_path *path, 179749d11beaSJosef Bacik u64 objectid) 179849d11beaSJosef Bacik { 179949d11beaSJosef Bacik struct btrfs_root *root; 180049d11beaSJosef Bacik struct btrfs_key key; 180149d11beaSJosef Bacik 180249d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 180349d11beaSJosef Bacik 180449d11beaSJosef Bacik /* 180549d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 180649d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 180749d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 180849d11beaSJosef Bacik * != NULL just return it. 180949d11beaSJosef Bacik */ 181049d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 181149d11beaSJosef Bacik if (root) 181249d11beaSJosef Bacik return root; 181349d11beaSJosef Bacik 181449d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 181549d11beaSJosef Bacik if (root) 181649d11beaSJosef Bacik return root; 181749d11beaSJosef Bacik 181849d11beaSJosef Bacik key.objectid = objectid; 181949d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 182049d11beaSJosef Bacik key.offset = (u64)-1; 182149d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 182249d11beaSJosef Bacik btrfs_release_path(path); 182349d11beaSJosef Bacik 182449d11beaSJosef Bacik return root; 182549d11beaSJosef Bacik } 182649d11beaSJosef Bacik 182749d11beaSJosef Bacik /* 18288b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 18298b712842SChris Mason * functions. This is where read checksum verification actually happens 18308b712842SChris Mason */ 18318b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1832ce9adaa5SChris Mason { 1833ce9adaa5SChris Mason struct bio *bio; 183497eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1835ce9adaa5SChris Mason 183697eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1837ce9adaa5SChris Mason bio = end_io_wq->bio; 18388b712842SChris Mason 18394e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1840ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1841ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 18424246a0b6SChristoph Hellwig bio_endio(bio); 18439be490f1SOmar Sandoval kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1844ce9adaa5SChris Mason } 184544b8bd7eSChris Mason 1846a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1847a74a4b97SChris Mason { 1848a74a4b97SChris Mason struct btrfs_root *root = arg; 18490b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1850d0278245SMiao Xie int again; 1851a74a4b97SChris Mason 1852d6fd0ae2SOmar Sandoval while (1) { 1853d0278245SMiao Xie again = 0; 18549d1a2a3aSDavid Sterba 1855fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1856fd340d0fSJosef Bacik 1857d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18582ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1859d0278245SMiao Xie goto sleep; 1860d0278245SMiao Xie 186190c711abSZygo Blaxell /* 186290c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 186390c711abSZygo Blaxell * before we have finished mounting the filesystem. 186490c711abSZygo Blaxell */ 18650b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 186690c711abSZygo Blaxell goto sleep; 186790c711abSZygo Blaxell 18680b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1869d0278245SMiao Xie goto sleep; 1870d0278245SMiao Xie 1871dc7f370cSMiao Xie /* 1872dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1873dc7f370cSMiao Xie * during the above check and trylock. 1874dc7f370cSMiao Xie */ 18752ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18760b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1877dc7f370cSMiao Xie goto sleep; 1878dc7f370cSMiao Xie } 1879dc7f370cSMiao Xie 18802ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1881c2d6cb16SFilipe Manana 18829d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 18830b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1884a74a4b97SChris Mason 1885d0278245SMiao Xie /* 188605323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 188705323cd1SMiao Xie * needn't do anything special here. 1888d0278245SMiao Xie */ 18890b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 189067c5e7d4SFilipe Manana 189167c5e7d4SFilipe Manana /* 1892f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 189367c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 189467c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1895f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 189667c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 189767c5e7d4SFilipe Manana * unused block groups. 189867c5e7d4SFilipe Manana */ 18990b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 190018bb8bbfSJohannes Thumshirn 190118bb8bbfSJohannes Thumshirn /* 190218bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 190318bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 190418bb8bbfSJohannes Thumshirn * space. 190518bb8bbfSJohannes Thumshirn */ 190618bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 1907d0278245SMiao Xie sleep: 1908a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1909d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1910d6fd0ae2SOmar Sandoval kthread_parkme(); 1911d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1912d6fd0ae2SOmar Sandoval return 0; 1913838fe188SJiri Kosina if (!again) { 1914a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1915a74a4b97SChris Mason schedule(); 1916a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1917a74a4b97SChris Mason } 1918da288d28SFilipe Manana } 1919a74a4b97SChris Mason } 1920a74a4b97SChris Mason 1921a74a4b97SChris Mason static int transaction_kthread(void *arg) 1922a74a4b97SChris Mason { 1923a74a4b97SChris Mason struct btrfs_root *root = arg; 19240b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1925a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1926a74a4b97SChris Mason struct btrfs_transaction *cur; 19278929ecfaSYan, Zheng u64 transid; 1928643900beSNikolay Borisov time64_t delta; 1929a74a4b97SChris Mason unsigned long delay; 1930914b2007SJan Kara bool cannot_commit; 1931a74a4b97SChris Mason 1932a74a4b97SChris Mason do { 1933914b2007SJan Kara cannot_commit = false; 1934ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 19350b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1936a74a4b97SChris Mason 19370b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19380b246afaSJeff Mahoney cur = fs_info->running_transaction; 1939a74a4b97SChris Mason if (!cur) { 19400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1941a74a4b97SChris Mason goto sleep; 1942a74a4b97SChris Mason } 194331153d81SYan Zheng 1944643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 19453296bf56SQu Wenruo if (cur->state < TRANS_STATE_COMMIT_START && 1946643900beSNikolay Borisov delta < fs_info->commit_interval) { 19470b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1948fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1949fb8a7e94SNikolay Borisov delay = min(delay, 1950fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1951a74a4b97SChris Mason goto sleep; 1952a74a4b97SChris Mason } 19538929ecfaSYan, Zheng transid = cur->transid; 19540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 195556bec294SChris Mason 195679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1957354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1958914b2007SJan Kara if (IS_ERR(trans)) { 1959354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1960914b2007SJan Kara cannot_commit = true; 196179787eaaSJeff Mahoney goto sleep; 1962914b2007SJan Kara } 19638929ecfaSYan, Zheng if (transid == trans->transid) { 19643a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19658929ecfaSYan, Zheng } else { 19663a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19678929ecfaSYan, Zheng } 1968a74a4b97SChris Mason sleep: 19690b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19700b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1971a74a4b97SChris Mason 19724e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 19730b246afaSJeff Mahoney &fs_info->fs_state))) 19742ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 19758929ecfaSYan, Zheng if (!kthread_should_stop() && 19760b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1977914b2007SJan Kara cannot_commit)) 1978bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1979a74a4b97SChris Mason } while (!kthread_should_stop()); 1980a74a4b97SChris Mason return 0; 1981a74a4b97SChris Mason } 1982a74a4b97SChris Mason 1983af31f5e5SChris Mason /* 198401f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 198501f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 198601f0f9daSNikolay Borisov * anything. 1987af31f5e5SChris Mason * 1988af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1989af31f5e5SChris Mason * generation of the latest root in the array with the generation 1990af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1991af31f5e5SChris Mason */ 199201f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1993af31f5e5SChris Mason { 199401f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1995af31f5e5SChris Mason u64 cur; 1996af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1997af31f5e5SChris Mason int i; 1998af31f5e5SChris Mason 1999af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2000af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2001af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2002af31f5e5SChris Mason if (cur == newest_gen) 200301f0f9daSNikolay Borisov return i; 2004af31f5e5SChris Mason } 2005af31f5e5SChris Mason 200601f0f9daSNikolay Borisov return -EINVAL; 2007af31f5e5SChris Mason } 2008af31f5e5SChris Mason 2009af31f5e5SChris Mason /* 2010af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2011af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2012af31f5e5SChris Mason * done 2013af31f5e5SChris Mason */ 2014af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2015af31f5e5SChris Mason { 20166ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2017af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2018af31f5e5SChris Mason 2019af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2020af31f5e5SChris Mason 2021af31f5e5SChris Mason /* 2022af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2023af31f5e5SChris Mason * regardless of which ones we use today 2024af31f5e5SChris Mason */ 2025af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2026af31f5e5SChris Mason 2027af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2028af31f5e5SChris Mason 2029af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2030af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2031af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2032af31f5e5SChris Mason 2033af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2034af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2035af31f5e5SChris Mason 2036af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2037af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2038af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2039af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2040af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2041af31f5e5SChris Mason 2042af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 2043af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 2044af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 2045af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 2046af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 2047af31f5e5SChris Mason 20487c7e82a7SChris Mason /* 20497c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20507c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20517c7e82a7SChris Mason */ 20527c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20537c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20547c7e82a7SChris Mason info->fs_root->node->start); 2055af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2056af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2057af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2058af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 20597c7e82a7SChris Mason } 2060af31f5e5SChris Mason 2061af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2062af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2063af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2064af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2065af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2066af31f5e5SChris Mason 2067af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 2068af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 2069af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 2070af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 2071af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 2072af31f5e5SChris Mason 2073af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2074af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2075af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2076af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2077af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2078af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2079af31f5e5SChris Mason 2080af31f5e5SChris Mason /* 2081af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2082af31f5e5SChris Mason * for the next commit 2083af31f5e5SChris Mason */ 2084af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2085af31f5e5SChris Mason &info->super_for_commit->super_roots, 2086af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2087af31f5e5SChris Mason } 2088af31f5e5SChris Mason 2089af31f5e5SChris Mason /* 2090bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2091bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2092bd2336b2SNikolay Borisov * 2093bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2094bd2336b2SNikolay Borisov * priority - priority of backup root required 2095bd2336b2SNikolay Borisov * 2096bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2097bd2336b2SNikolay Borisov */ 2098bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2099bd2336b2SNikolay Borisov { 2100bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2101bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2102bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2103bd2336b2SNikolay Borisov 2104bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2105bd2336b2SNikolay Borisov if (priority == 0) 2106bd2336b2SNikolay Borisov return backup_index; 2107bd2336b2SNikolay Borisov 2108bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2109bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2110bd2336b2SNikolay Borisov } else { 2111bd2336b2SNikolay Borisov return -EINVAL; 2112bd2336b2SNikolay Borisov } 2113bd2336b2SNikolay Borisov 2114bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2115bd2336b2SNikolay Borisov 2116bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2117bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2118bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2119bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2120bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2121bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2122bd2336b2SNikolay Borisov 2123bd2336b2SNikolay Borisov /* 2124bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2125bd2336b2SNikolay Borisov * need a fsck 2126bd2336b2SNikolay Borisov */ 2127bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2128bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2129bd2336b2SNikolay Borisov 2130bd2336b2SNikolay Borisov return backup_index; 2131bd2336b2SNikolay Borisov } 2132bd2336b2SNikolay Borisov 21337abadb64SLiu Bo /* helper to cleanup workers */ 21347abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21357abadb64SLiu Bo { 2136dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2137afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 21385cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2139fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2140fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2141d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2142fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2143fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 21445b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2145e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2146736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2147a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2148fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2149b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2150b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2151a9b9477dSFilipe Manana /* 2152a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2153a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2154a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2155a9b9477dSFilipe Manana */ 2156a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2157a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 21587abadb64SLiu Bo } 21597abadb64SLiu Bo 21602e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 21612e9f5954SRashika { 21622e9f5954SRashika if (root) { 21632e9f5954SRashika free_extent_buffer(root->node); 21642e9f5954SRashika free_extent_buffer(root->commit_root); 21652e9f5954SRashika root->node = NULL; 21662e9f5954SRashika root->commit_root = NULL; 21672e9f5954SRashika } 21682e9f5954SRashika } 21692e9f5954SRashika 2170af31f5e5SChris Mason /* helper to cleanup tree roots */ 21714273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2172af31f5e5SChris Mason { 21732e9f5954SRashika free_root_extent_buffers(info->tree_root); 2174655b09feSJosef Bacik 21752e9f5954SRashika free_root_extent_buffers(info->dev_root); 21762e9f5954SRashika free_root_extent_buffers(info->extent_root); 21772e9f5954SRashika free_root_extent_buffers(info->csum_root); 21782e9f5954SRashika free_root_extent_buffers(info->quota_root); 21792e9f5954SRashika free_root_extent_buffers(info->uuid_root); 21808c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2181aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 21824273eaffSAnand Jain if (free_chunk_root) 21832e9f5954SRashika free_root_extent_buffers(info->chunk_root); 218470f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2185af31f5e5SChris Mason } 2186af31f5e5SChris Mason 21878c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 21888c38938cSJosef Bacik { 21898c38938cSJosef Bacik if (!root) 21908c38938cSJosef Bacik return; 21918c38938cSJosef Bacik 21928c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 21938c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 21941dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 21958c38938cSJosef Bacik if (root->anon_dev) 21968c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 21978c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2198923eb523SJohannes Thumshirn free_root_extent_buffers(root); 21998c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 22008c38938cSJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 22018c38938cSJosef Bacik list_del_init(&root->leak_list); 22028c38938cSJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 22038c38938cSJosef Bacik #endif 22048c38938cSJosef Bacik kfree(root); 22058c38938cSJosef Bacik } 22068c38938cSJosef Bacik } 22078c38938cSJosef Bacik 2208faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2209171f6537SJosef Bacik { 2210171f6537SJosef Bacik int ret; 2211171f6537SJosef Bacik struct btrfs_root *gang[8]; 2212171f6537SJosef Bacik int i; 2213171f6537SJosef Bacik 2214171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2215171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2216171f6537SJosef Bacik struct btrfs_root, root_list); 2217171f6537SJosef Bacik list_del(&gang[0]->root_list); 2218171f6537SJosef Bacik 22198c38938cSJosef Bacik if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2220cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 222100246528SJosef Bacik btrfs_put_root(gang[0]); 2222171f6537SJosef Bacik } 2223171f6537SJosef Bacik 2224171f6537SJosef Bacik while (1) { 2225171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2226171f6537SJosef Bacik (void **)gang, 0, 2227171f6537SJosef Bacik ARRAY_SIZE(gang)); 2228171f6537SJosef Bacik if (!ret) 2229171f6537SJosef Bacik break; 2230171f6537SJosef Bacik for (i = 0; i < ret; i++) 2231cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2232171f6537SJosef Bacik } 2233171f6537SJosef Bacik } 2234af31f5e5SChris Mason 2235638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2236638aa7edSEric Sandeen { 2237638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2238638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2239638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2240638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2241638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2242638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2243ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2244638aa7edSEric Sandeen } 2245638aa7edSEric Sandeen 2246779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2247779a65a4SEric Sandeen { 2248779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2249779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2250779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2251779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2252779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2253779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2254779a65a4SEric Sandeen } 2255779a65a4SEric Sandeen 22566bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2257f37938e0SEric Sandeen { 22582ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 22592ff7e61eSJeff Mahoney 22602ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 22612ff7e61eSJeff Mahoney set_nlink(inode, 1); 2262f37938e0SEric Sandeen /* 2263f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2264f37938e0SEric Sandeen * the real end of the address space is determined by all of 2265f37938e0SEric Sandeen * the devices in the system 2266f37938e0SEric Sandeen */ 22672ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 22682ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2269f37938e0SEric Sandeen 22702ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 227143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 22722c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, inode); 22737b439738SDavid Sterba BTRFS_I(inode)->io_tree.track_uptodate = false; 22742ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2275f37938e0SEric Sandeen 22765c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 22772ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 22782ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 22792ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2280f37938e0SEric Sandeen } 2281f37938e0SEric Sandeen 2282ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2283ad618368SEric Sandeen { 2284ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2285129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 22867f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2287ad618368SEric Sandeen } 2288ad618368SEric Sandeen 2289f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2290f9e92e40SEric Sandeen { 2291f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2292f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2293f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2294f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2295f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2296f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2297d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2298f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2299f9e92e40SEric Sandeen } 2300f9e92e40SEric Sandeen 23012a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 23022a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 23032a458198SEric Sandeen { 2304f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 23056f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23062a458198SEric Sandeen 23072a458198SEric Sandeen fs_info->workers = 2308cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2309cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23102a458198SEric Sandeen 23112a458198SEric Sandeen fs_info->delalloc_workers = 2312cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2313cb001095SJeff Mahoney flags, max_active, 2); 23142a458198SEric Sandeen 23152a458198SEric Sandeen fs_info->flush_workers = 2316cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2317cb001095SJeff Mahoney flags, max_active, 0); 23182a458198SEric Sandeen 23192a458198SEric Sandeen fs_info->caching_workers = 2320cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23212a458198SEric Sandeen 23222a458198SEric Sandeen fs_info->fixup_workers = 2323cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23242a458198SEric Sandeen 23252a458198SEric Sandeen /* 23262a458198SEric Sandeen * endios are largely parallel and should have a very 23272a458198SEric Sandeen * low idle thresh 23282a458198SEric Sandeen */ 23292a458198SEric Sandeen fs_info->endio_workers = 2330cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 23312a458198SEric Sandeen fs_info->endio_meta_workers = 2332cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2333cb001095SJeff Mahoney max_active, 4); 23342a458198SEric Sandeen fs_info->endio_meta_write_workers = 2335cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2336cb001095SJeff Mahoney max_active, 2); 23372a458198SEric Sandeen fs_info->endio_raid56_workers = 2338cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2339cb001095SJeff Mahoney max_active, 4); 23402a458198SEric Sandeen fs_info->rmw_workers = 2341cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 23422a458198SEric Sandeen fs_info->endio_write_workers = 2343cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2344cb001095SJeff Mahoney max_active, 2); 23452a458198SEric Sandeen fs_info->endio_freespace_worker = 2346cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2347cb001095SJeff Mahoney max_active, 0); 23482a458198SEric Sandeen fs_info->delayed_workers = 2349cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2350cb001095SJeff Mahoney max_active, 0); 23512a458198SEric Sandeen fs_info->readahead_workers = 2352cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2353cb001095SJeff Mahoney max_active, 2); 23542a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2355cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2356b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2357b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 23582a458198SEric Sandeen 23592a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 2360ba8a9d07SChris Mason fs_info->flush_workers && 23612a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 23622a458198SEric Sandeen fs_info->endio_meta_write_workers && 23632a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 23642a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 23652a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 23662a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 2367b0643e59SDennis Zhou fs_info->qgroup_rescan_workers && 2368b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 23692a458198SEric Sandeen return -ENOMEM; 23702a458198SEric Sandeen } 23712a458198SEric Sandeen 23722a458198SEric Sandeen return 0; 23732a458198SEric Sandeen } 23742a458198SEric Sandeen 23756d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 23766d97c6e3SJohannes Thumshirn { 23776d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2378b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 23796d97c6e3SJohannes Thumshirn 2380b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 23816d97c6e3SJohannes Thumshirn 23826d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 23836d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2384b4e967beSDavid Sterba csum_driver); 23856d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 23866d97c6e3SJohannes Thumshirn } 23876d97c6e3SJohannes Thumshirn 23886d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 23896d97c6e3SJohannes Thumshirn 23906d97c6e3SJohannes Thumshirn return 0; 23916d97c6e3SJohannes Thumshirn } 23926d97c6e3SJohannes Thumshirn 239363443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 239463443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 239563443bf5SEric Sandeen { 239663443bf5SEric Sandeen int ret; 239763443bf5SEric Sandeen struct btrfs_root *log_tree_root; 239863443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 239963443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2400581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 240163443bf5SEric Sandeen 240263443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2403f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 240463443bf5SEric Sandeen return -EIO; 240563443bf5SEric Sandeen } 240663443bf5SEric Sandeen 240796dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 240896dfcb46SJosef Bacik GFP_KERNEL); 240963443bf5SEric Sandeen if (!log_tree_root) 241063443bf5SEric Sandeen return -ENOMEM; 241163443bf5SEric Sandeen 24122ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 24131b7ec85eSJosef Bacik BTRFS_TREE_LOG_OBJECTID, 24141b7ec85eSJosef Bacik fs_info->generation + 1, level, 24151b7ec85eSJosef Bacik NULL); 241664c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2417f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24180eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 24198c38938cSJosef Bacik log_tree_root->node = NULL; 242000246528SJosef Bacik btrfs_put_root(log_tree_root); 24210eeff236SLiu Bo return ret; 242264c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2423f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 242400246528SJosef Bacik btrfs_put_root(log_tree_root); 242563443bf5SEric Sandeen return -EIO; 242663443bf5SEric Sandeen } 242763443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 242863443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 242963443bf5SEric Sandeen if (ret) { 24300b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 243163443bf5SEric Sandeen "Failed to recover log tree"); 243200246528SJosef Bacik btrfs_put_root(log_tree_root); 243363443bf5SEric Sandeen return ret; 243463443bf5SEric Sandeen } 243563443bf5SEric Sandeen 2436bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 24376bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 243863443bf5SEric Sandeen if (ret) 243963443bf5SEric Sandeen return ret; 244063443bf5SEric Sandeen } 244163443bf5SEric Sandeen 244263443bf5SEric Sandeen return 0; 244363443bf5SEric Sandeen } 244463443bf5SEric Sandeen 24456bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 24464bbcaa64SEric Sandeen { 24476bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2448a4f3d2c4SDavid Sterba struct btrfs_root *root; 24494bbcaa64SEric Sandeen struct btrfs_key location; 24504bbcaa64SEric Sandeen int ret; 24514bbcaa64SEric Sandeen 24526bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 24536bccf3abSJeff Mahoney 24544bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 24554bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 24564bbcaa64SEric Sandeen location.offset = 0; 24574bbcaa64SEric Sandeen 2458a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2459f50f4353SLiu Bo if (IS_ERR(root)) { 246042437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2461f50f4353SLiu Bo ret = PTR_ERR(root); 2462f50f4353SLiu Bo goto out; 2463f50f4353SLiu Bo } 246442437a63SJosef Bacik } else { 2465a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2466a4f3d2c4SDavid Sterba fs_info->extent_root = root; 246742437a63SJosef Bacik } 24684bbcaa64SEric Sandeen 24694bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2470a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2471f50f4353SLiu Bo if (IS_ERR(root)) { 247242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2473f50f4353SLiu Bo ret = PTR_ERR(root); 2474f50f4353SLiu Bo goto out; 2475f50f4353SLiu Bo } 247642437a63SJosef Bacik } else { 2477a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2478a4f3d2c4SDavid Sterba fs_info->dev_root = root; 247942437a63SJosef Bacik } 2480820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2481820a49daSJosef Bacik btrfs_init_devices_late(fs_info); 24824bbcaa64SEric Sandeen 2483882dbe0cSJosef Bacik /* If IGNOREDATACSUMS is set don't bother reading the csum root. */ 2484882dbe0cSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 24854bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2486a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2487f50f4353SLiu Bo if (IS_ERR(root)) { 248842437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2489f50f4353SLiu Bo ret = PTR_ERR(root); 2490f50f4353SLiu Bo goto out; 2491f50f4353SLiu Bo } 249242437a63SJosef Bacik } else { 2493a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2494a4f3d2c4SDavid Sterba fs_info->csum_root = root; 249542437a63SJosef Bacik } 2496882dbe0cSJosef Bacik } 24974bbcaa64SEric Sandeen 2498aeb935a4SQu Wenruo /* 2499aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2500aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2501aeb935a4SQu Wenruo */ 250256e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 250356e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2504aeb935a4SQu Wenruo if (IS_ERR(root)) { 250542437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2506aeb935a4SQu Wenruo ret = PTR_ERR(root); 2507aeb935a4SQu Wenruo goto out; 2508aeb935a4SQu Wenruo } 250942437a63SJosef Bacik } else { 2510aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2511aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 251242437a63SJosef Bacik } 2513aeb935a4SQu Wenruo 25144bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2515a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2516a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2517a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2518afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2519a4f3d2c4SDavid Sterba fs_info->quota_root = root; 25204bbcaa64SEric Sandeen } 25214bbcaa64SEric Sandeen 25224bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2523a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2524a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 252542437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2526a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 25274bbcaa64SEric Sandeen if (ret != -ENOENT) 2528f50f4353SLiu Bo goto out; 252942437a63SJosef Bacik } 25304bbcaa64SEric Sandeen } else { 2531a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2532a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 25334bbcaa64SEric Sandeen } 25344bbcaa64SEric Sandeen 253570f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 253670f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 253770f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2538f50f4353SLiu Bo if (IS_ERR(root)) { 253942437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2540f50f4353SLiu Bo ret = PTR_ERR(root); 2541f50f4353SLiu Bo goto out; 2542f50f4353SLiu Bo } 254342437a63SJosef Bacik } else { 254470f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 254570f6d82eSOmar Sandoval fs_info->free_space_root = root; 254670f6d82eSOmar Sandoval } 254742437a63SJosef Bacik } 254870f6d82eSOmar Sandoval 25494bbcaa64SEric Sandeen return 0; 2550f50f4353SLiu Bo out: 2551f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2552f50f4353SLiu Bo location.objectid, ret); 2553f50f4353SLiu Bo return ret; 25544bbcaa64SEric Sandeen } 25554bbcaa64SEric Sandeen 2556069ec957SQu Wenruo /* 2557069ec957SQu Wenruo * Real super block validation 2558069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2559069ec957SQu Wenruo * 2560069ec957SQu Wenruo * @sb: super block to check 2561069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2562069ec957SQu Wenruo * 0 the primary (1st) sb 2563069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2564069ec957SQu Wenruo * -1 skip bytenr check 2565069ec957SQu Wenruo */ 2566069ec957SQu Wenruo static int validate_super(struct btrfs_fs_info *fs_info, 2567069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 256821a852b0SQu Wenruo { 256921a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 257021a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 257121a852b0SQu Wenruo int ret = 0; 257221a852b0SQu Wenruo 257321a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 257421a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 257521a852b0SQu Wenruo ret = -EINVAL; 257621a852b0SQu Wenruo } 257721a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 257821a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 257921a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 258021a852b0SQu Wenruo ret = -EINVAL; 258121a852b0SQu Wenruo } 258221a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 258321a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 258421a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 258521a852b0SQu Wenruo ret = -EINVAL; 258621a852b0SQu Wenruo } 258721a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 258821a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 258921a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 259021a852b0SQu Wenruo ret = -EINVAL; 259121a852b0SQu Wenruo } 259221a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 259321a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 259421a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 259521a852b0SQu Wenruo ret = -EINVAL; 259621a852b0SQu Wenruo } 259721a852b0SQu Wenruo 259821a852b0SQu Wenruo /* 259921a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 260021a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 260121a852b0SQu Wenruo */ 260221a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 260321a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 260421a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 260521a852b0SQu Wenruo ret = -EINVAL; 260621a852b0SQu Wenruo } 26070bb3eb3eSQu Wenruo 26080bb3eb3eSQu Wenruo /* 26090bb3eb3eSQu Wenruo * For 4K page size, we only support 4K sector size. 26100bb3eb3eSQu Wenruo * For 64K page size, we support read-write for 64K sector size, and 26110bb3eb3eSQu Wenruo * read-only for 4K sector size. 26120bb3eb3eSQu Wenruo */ 26130bb3eb3eSQu Wenruo if ((PAGE_SIZE == SZ_4K && sectorsize != PAGE_SIZE) || 26140bb3eb3eSQu Wenruo (PAGE_SIZE == SZ_64K && (sectorsize != SZ_4K && 26150bb3eb3eSQu Wenruo sectorsize != SZ_64K))) { 261621a852b0SQu Wenruo btrfs_err(fs_info, 26170bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 261821a852b0SQu Wenruo sectorsize, PAGE_SIZE); 261921a852b0SQu Wenruo ret = -EINVAL; 262021a852b0SQu Wenruo } 26210bb3eb3eSQu Wenruo 262221a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 262321a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 262421a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 262521a852b0SQu Wenruo ret = -EINVAL; 262621a852b0SQu Wenruo } 262721a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 262821a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 262921a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 263021a852b0SQu Wenruo ret = -EINVAL; 263121a852b0SQu Wenruo } 263221a852b0SQu Wenruo 263321a852b0SQu Wenruo /* Root alignment check */ 263421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 263521a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 263621a852b0SQu Wenruo btrfs_super_root(sb)); 263721a852b0SQu Wenruo ret = -EINVAL; 263821a852b0SQu Wenruo } 263921a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 264021a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 264121a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 264221a852b0SQu Wenruo ret = -EINVAL; 264321a852b0SQu Wenruo } 264421a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 264521a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 264621a852b0SQu Wenruo btrfs_super_log_root(sb)); 264721a852b0SQu Wenruo ret = -EINVAL; 264821a852b0SQu Wenruo } 264921a852b0SQu Wenruo 2650aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2651aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2652aefd7f70SNikolay Borisov btrfs_err(fs_info, 2653aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2654aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2655aefd7f70SNikolay Borisov ret = -EINVAL; 2656aefd7f70SNikolay Borisov } 2657aefd7f70SNikolay Borisov 2658aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2659aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2660aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2661aefd7f70SNikolay Borisov btrfs_err(fs_info, 2662aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2663aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2664aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2665aefd7f70SNikolay Borisov ret = -EINVAL; 2666aefd7f70SNikolay Borisov } 2667aefd7f70SNikolay Borisov 2668de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 26697239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 267021a852b0SQu Wenruo btrfs_err(fs_info, 26717239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2672de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 267321a852b0SQu Wenruo ret = -EINVAL; 267421a852b0SQu Wenruo } 267521a852b0SQu Wenruo 267621a852b0SQu Wenruo /* 267721a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 267821a852b0SQu Wenruo * done later 267921a852b0SQu Wenruo */ 268021a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 268121a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 268221a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 268321a852b0SQu Wenruo ret = -EINVAL; 268421a852b0SQu Wenruo } 268521a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 268621a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 268721a852b0SQu Wenruo btrfs_super_stripesize(sb)); 268821a852b0SQu Wenruo ret = -EINVAL; 268921a852b0SQu Wenruo } 269021a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 269121a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 269221a852b0SQu Wenruo btrfs_super_num_devices(sb)); 269321a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 269421a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 269521a852b0SQu Wenruo ret = -EINVAL; 269621a852b0SQu Wenruo } 269721a852b0SQu Wenruo 2698069ec957SQu Wenruo if (mirror_num >= 0 && 2699069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 270021a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 270121a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 270221a852b0SQu Wenruo ret = -EINVAL; 270321a852b0SQu Wenruo } 270421a852b0SQu Wenruo 270521a852b0SQu Wenruo /* 270621a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 270721a852b0SQu Wenruo * and one chunk 270821a852b0SQu Wenruo */ 270921a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 271021a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 271121a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 271221a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 271321a852b0SQu Wenruo ret = -EINVAL; 271421a852b0SQu Wenruo } 271521a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 271621a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 271721a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 271821a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 271921a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 272021a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 272121a852b0SQu Wenruo ret = -EINVAL; 272221a852b0SQu Wenruo } 272321a852b0SQu Wenruo 272421a852b0SQu Wenruo /* 272521a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 272621a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 272721a852b0SQu Wenruo */ 272821a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 272921a852b0SQu Wenruo btrfs_warn(fs_info, 273021a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 273121a852b0SQu Wenruo btrfs_super_generation(sb), 273221a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 273321a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 273421a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 273521a852b0SQu Wenruo btrfs_warn(fs_info, 273621a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 273721a852b0SQu Wenruo btrfs_super_generation(sb), 273821a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 273921a852b0SQu Wenruo 274021a852b0SQu Wenruo return ret; 274121a852b0SQu Wenruo } 274221a852b0SQu Wenruo 2743069ec957SQu Wenruo /* 2744069ec957SQu Wenruo * Validation of super block at mount time. 2745069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2746069ec957SQu Wenruo * flags will be skipped. 2747069ec957SQu Wenruo */ 2748069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2749069ec957SQu Wenruo { 2750069ec957SQu Wenruo return validate_super(fs_info, fs_info->super_copy, 0); 2751069ec957SQu Wenruo } 2752069ec957SQu Wenruo 275375cb857dSQu Wenruo /* 275475cb857dSQu Wenruo * Validation of super block at write time. 275575cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 275675cb857dSQu Wenruo * overwritten soon. 275775cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 275875cb857dSQu Wenruo */ 275975cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 276075cb857dSQu Wenruo struct btrfs_super_block *sb) 276175cb857dSQu Wenruo { 276275cb857dSQu Wenruo int ret; 276375cb857dSQu Wenruo 276475cb857dSQu Wenruo ret = validate_super(fs_info, sb, -1); 276575cb857dSQu Wenruo if (ret < 0) 276675cb857dSQu Wenruo goto out; 2767e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 276875cb857dSQu Wenruo ret = -EUCLEAN; 276975cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 277075cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 277175cb857dSQu Wenruo goto out; 277275cb857dSQu Wenruo } 277375cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 277475cb857dSQu Wenruo ret = -EUCLEAN; 277575cb857dSQu Wenruo btrfs_err(fs_info, 277675cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 277775cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 277875cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 277975cb857dSQu Wenruo goto out; 278075cb857dSQu Wenruo } 278175cb857dSQu Wenruo out: 278275cb857dSQu Wenruo if (ret < 0) 278375cb857dSQu Wenruo btrfs_err(fs_info, 278475cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 278575cb857dSQu Wenruo return ret; 278675cb857dSQu Wenruo } 278775cb857dSQu Wenruo 27886ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2789b8522a1eSNikolay Borisov { 27906ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2791b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2792b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2793b8522a1eSNikolay Borisov bool handle_error = false; 2794b8522a1eSNikolay Borisov int ret = 0; 2795b8522a1eSNikolay Borisov int i; 2796b8522a1eSNikolay Borisov 2797b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2798b8522a1eSNikolay Borisov u64 generation; 2799b8522a1eSNikolay Borisov int level; 2800b8522a1eSNikolay Borisov 2801b8522a1eSNikolay Borisov if (handle_error) { 2802b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2803b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2804b8522a1eSNikolay Borisov tree_root->node = NULL; 2805b8522a1eSNikolay Borisov 2806b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2807b8522a1eSNikolay Borisov break; 2808b8522a1eSNikolay Borisov 2809b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2810b8522a1eSNikolay Borisov 2811b8522a1eSNikolay Borisov /* 2812b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2813b8522a1eSNikolay Borisov * valid 2814b8522a1eSNikolay Borisov */ 2815b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2816b8522a1eSNikolay Borisov 2817b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2818b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2819b8522a1eSNikolay Borisov 2820b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 28216ef108ddSNikolay Borisov backup_index = ret; 2822b8522a1eSNikolay Borisov if (ret < 0) 2823b8522a1eSNikolay Borisov return ret; 2824b8522a1eSNikolay Borisov } 2825b8522a1eSNikolay Borisov generation = btrfs_super_generation(sb); 2826b8522a1eSNikolay Borisov level = btrfs_super_root_level(sb); 2827b8522a1eSNikolay Borisov tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb), 28281b7ec85eSJosef Bacik BTRFS_ROOT_TREE_OBJECTID, 2829b8522a1eSNikolay Borisov generation, level, NULL); 28300465337cSJosef Bacik if (IS_ERR(tree_root->node)) { 2831217f5004SNikolay Borisov handle_error = true; 2832b8522a1eSNikolay Borisov ret = PTR_ERR(tree_root->node); 28330465337cSJosef Bacik tree_root->node = NULL; 2834217f5004SNikolay Borisov btrfs_warn(fs_info, "couldn't read tree root"); 2835217f5004SNikolay Borisov continue; 2836b8522a1eSNikolay Borisov 2837217f5004SNikolay Borisov } else if (!extent_buffer_uptodate(tree_root->node)) { 2838217f5004SNikolay Borisov handle_error = true; 2839217f5004SNikolay Borisov ret = -EIO; 2840217f5004SNikolay Borisov btrfs_warn(fs_info, "error while reading tree root"); 2841b8522a1eSNikolay Borisov continue; 2842b8522a1eSNikolay Borisov } 2843b8522a1eSNikolay Borisov 2844b8522a1eSNikolay Borisov btrfs_set_root_node(&tree_root->root_item, tree_root->node); 2845b8522a1eSNikolay Borisov tree_root->commit_root = btrfs_root_node(tree_root); 2846b8522a1eSNikolay Borisov btrfs_set_root_refs(&tree_root->root_item, 1); 2847b8522a1eSNikolay Borisov 2848336a0d8dSNikolay Borisov /* 2849336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2850336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2851336a0d8dSNikolay Borisov */ 2852453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 2853b8522a1eSNikolay Borisov if (ret < 0) { 2854b8522a1eSNikolay Borisov handle_error = true; 2855b8522a1eSNikolay Borisov continue; 2856b8522a1eSNikolay Borisov } 2857b8522a1eSNikolay Borisov 28586b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 2859b8522a1eSNikolay Borisov 2860b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 2861b8522a1eSNikolay Borisov if (ret < 0) { 2862b8522a1eSNikolay Borisov handle_error = true; 2863b8522a1eSNikolay Borisov continue; 2864b8522a1eSNikolay Borisov } 2865b8522a1eSNikolay Borisov 2866b8522a1eSNikolay Borisov /* All successful */ 2867b8522a1eSNikolay Borisov fs_info->generation = generation; 2868b8522a1eSNikolay Borisov fs_info->last_trans_committed = generation; 28696ef108ddSNikolay Borisov 28706ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 28716ef108ddSNikolay Borisov if (backup_index < 0) { 28726ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 28736ef108ddSNikolay Borisov } else { 28746ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 28756ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 28766ef108ddSNikolay Borisov } 2877b8522a1eSNikolay Borisov break; 2878b8522a1eSNikolay Borisov } 2879b8522a1eSNikolay Borisov 2880b8522a1eSNikolay Borisov return ret; 2881b8522a1eSNikolay Borisov } 2882b8522a1eSNikolay Borisov 28838260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 2884eb60ceacSChris Mason { 288576dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2886f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 28878fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2888facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 288924bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2890eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 289111833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2892eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2893a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 289476dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 289524bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 28964cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2897ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2898f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 289947ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 290040ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 2901f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2902d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 2903f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 29047585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2905573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 29060bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 2907de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 290819c00ddcSChris Mason 29090b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 29106324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2911f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 291247ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 291318bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 2914bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2915bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 29163fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 29173fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 2918bd647ce3SJosef Bacik #endif 2919c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 292066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 292166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 292266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 292366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 292466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 292566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 292666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2927ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 2928ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 2929ba2c4d4eSJosef Bacik 2930771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 29314cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 29322fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2933034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 2934fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 293595ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 29369ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 29374cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2938a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2939f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 29408b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2941f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 294290519d66SArne Jansen /* readahead state */ 2943d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 294490519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2945fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2946c8b97818SChris Mason 2947b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2948b34b086cSChris Mason num_online_cpus() + 2, 8); 29490afbaf8cSChris Mason 2950199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2951199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 295269fe2d75SJosef Bacik 2953638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 295421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 295521adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 295621adbd5cSStefan Behrens #endif 2957779a65a4SEric Sandeen btrfs_init_balance(fs_info); 295857056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 2959a2de733cSArne Jansen 29600f9dd46cSJosef Bacik spin_lock_init(&fs_info->block_group_cache_lock); 29616bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2962a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 29630f9dd46cSJosef Bacik 2964fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 2965fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, NULL); 2966afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 296739279cc3SChris Mason 29685a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 2969e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 2970925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2971a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2972a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 29731bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 29749e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2975c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 297676dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2977803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2978fa9c0d79SChris Mason 2979ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2980f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2981b0643e59SDennis Zhou btrfs_discard_init(fs_info); 2982416ac51dSArne Jansen 2983fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2984fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2985fa9c0d79SChris Mason 2986e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2987f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2988bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 29894854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 2990034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 29913768f368SChris Mason 2992da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2993da17066cSJeff Mahoney fs_info->nodesize = 4096; 2994da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2995ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 2996da17066cSJeff Mahoney fs_info->stripesize = 4096; 2997da17066cSJeff Mahoney 2998eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 2999eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3000eede2bf3SOmar Sandoval 30019e967495SFilipe Manana fs_info->send_in_progress = 0; 300218bb8bbfSJohannes Thumshirn 300318bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 300418bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 30058260edbaSJosef Bacik } 30068260edbaSJosef Bacik 30078260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 30088260edbaSJosef Bacik { 30098260edbaSJosef Bacik int ret; 30108260edbaSJosef Bacik 30118260edbaSJosef Bacik fs_info->sb = sb; 30128260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 30138260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 30149e967495SFilipe Manana 30155deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3016ae18c37aSJosef Bacik if (ret) 3017ae18c37aSJosef Bacik return ret; 3018ae18c37aSJosef Bacik 3019ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3020ae18c37aSJosef Bacik if (ret) 3021c75e8394SJosef Bacik return ret; 3022ae18c37aSJosef Bacik 3023ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3024ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3025ae18c37aSJosef Bacik 3026ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3027ae18c37aSJosef Bacik if (ret) 3028c75e8394SJosef Bacik return ret; 3029ae18c37aSJosef Bacik 3030ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3031ae18c37aSJosef Bacik GFP_KERNEL); 3032ae18c37aSJosef Bacik if (ret) 3033c75e8394SJosef Bacik return ret; 3034ae18c37aSJosef Bacik 3035ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3036ae18c37aSJosef Bacik GFP_KERNEL); 3037c75e8394SJosef Bacik if (!fs_info->delayed_root) 3038c75e8394SJosef Bacik return -ENOMEM; 3039ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3040ae18c37aSJosef Bacik 3041a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3042a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3043a0a1db70SFilipe Manana 3044c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3045ae18c37aSJosef Bacik } 3046ae18c37aSJosef Bacik 304797f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 304897f4dd09SNikolay Borisov { 304997f4dd09SNikolay Borisov struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; 305097f4dd09SNikolay Borisov int ret; 305197f4dd09SNikolay Borisov 305297f4dd09SNikolay Borisov /* 305397f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 305497f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 305597f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 305697f4dd09SNikolay Borisov */ 305797f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 305897f4dd09SNikolay Borisov if (ret < 0) { 3059c94bec2cSJosef Bacik if (ret != -EINTR) 3060c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3061c94bec2cSJosef Bacik ret); 306297f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 306397f4dd09SNikolay Borisov return ret; 306497f4dd09SNikolay Borisov } 306597f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 306697f4dd09SNikolay Borisov } 306797f4dd09SNikolay Borisov 306897f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 306997f4dd09SNikolay Borisov { 307097f4dd09SNikolay Borisov struct task_struct *task; 307197f4dd09SNikolay Borisov 307297f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 307397f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 307497f4dd09SNikolay Borisov if (IS_ERR(task)) { 307597f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 307697f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 307797f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 307897f4dd09SNikolay Borisov return PTR_ERR(task); 307997f4dd09SNikolay Borisov } 308097f4dd09SNikolay Borisov 308197f4dd09SNikolay Borisov return 0; 308297f4dd09SNikolay Borisov } 308397f4dd09SNikolay Borisov 308444c0ca21SBoris Burkov /* 30858cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 30868cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 30878cd29088SBoris Burkov * code paths. 30888cd29088SBoris Burkov */ 30898cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 30908cd29088SBoris Burkov { 30918cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 30928b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 30938cd29088SBoris Burkov } 30948cd29088SBoris Burkov 30958cd29088SBoris Burkov /* 309644c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 309744c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 309844c0ca21SBoris Burkov */ 309944c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 310044c0ca21SBoris Burkov { 310144c0ca21SBoris Burkov int ret; 310294846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 31038b228324SBoris Burkov bool clear_free_space_tree = false; 31048b228324SBoris Burkov 31058b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 31068b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 31078b228324SBoris Burkov clear_free_space_tree = true; 31088b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 31098b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 31108b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 31118b228324SBoris Burkov clear_free_space_tree = true; 31128b228324SBoris Burkov } 31138b228324SBoris Burkov 31148b228324SBoris Burkov if (clear_free_space_tree) { 31158b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 31168b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 31178b228324SBoris Burkov if (ret) { 31188b228324SBoris Burkov btrfs_warn(fs_info, 31198b228324SBoris Burkov "failed to clear free space tree: %d", ret); 31208b228324SBoris Burkov goto out; 31218b228324SBoris Burkov } 31228b228324SBoris Burkov } 312344c0ca21SBoris Burkov 31248d488a8cSFilipe Manana /* 31258d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 31268d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 31278d488a8cSFilipe Manana * them into the fs_info->fs_roots_radix tree. This must be done before 31288d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 31298d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 31308d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 31318d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 31328d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 31338d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 31348d488a8cSFilipe Manana */ 31358d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 31368d488a8cSFilipe Manana if (ret) 31378d488a8cSFilipe Manana goto out; 31388d488a8cSFilipe Manana 313944c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 314044c0ca21SBoris Burkov if (ret) 314144c0ca21SBoris Burkov goto out; 314244c0ca21SBoris Burkov 31438f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 31448f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 31458f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 31468f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 31478f1c21d7SBoris Burkov goto out; 31488f1c21d7SBoris Burkov } 31498f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 31508f1c21d7SBoris Burkov 315144c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 315244c0ca21SBoris Burkov ret = btrfs_recover_relocation(fs_info->tree_root); 315344c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 315444c0ca21SBoris Burkov if (ret < 0) { 315544c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 315644c0ca21SBoris Burkov goto out; 315744c0ca21SBoris Burkov } 315844c0ca21SBoris Burkov 31595011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 31605011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 31615011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 31625011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 31635011139aSBoris Burkov if (ret) { 31645011139aSBoris Burkov btrfs_warn(fs_info, 31655011139aSBoris Burkov "failed to create free space tree: %d", ret); 31665011139aSBoris Burkov goto out; 31675011139aSBoris Burkov } 31685011139aSBoris Burkov } 31695011139aSBoris Burkov 317094846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 317194846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 317294846229SBoris Burkov if (ret) 317394846229SBoris Burkov goto out; 317494846229SBoris Burkov } 317594846229SBoris Burkov 317644c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 317744c0ca21SBoris Burkov if (ret) 317844c0ca21SBoris Burkov goto out; 317944c0ca21SBoris Burkov 318044c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 318144c0ca21SBoris Burkov if (ret) { 318244c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 318344c0ca21SBoris Burkov goto out; 318444c0ca21SBoris Burkov } 318544c0ca21SBoris Burkov 318644c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 318744c0ca21SBoris Burkov 318844c0ca21SBoris Burkov if (!fs_info->uuid_root) { 318944c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 319044c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 319144c0ca21SBoris Burkov if (ret) { 319244c0ca21SBoris Burkov btrfs_warn(fs_info, 319344c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 319444c0ca21SBoris Burkov goto out; 319544c0ca21SBoris Burkov } 319644c0ca21SBoris Burkov } 319744c0ca21SBoris Burkov 319844c0ca21SBoris Burkov out: 319944c0ca21SBoris Burkov return ret; 320044c0ca21SBoris Burkov } 320144c0ca21SBoris Burkov 3202ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3203ae18c37aSJosef Bacik char *options) 3204ae18c37aSJosef Bacik { 3205ae18c37aSJosef Bacik u32 sectorsize; 3206ae18c37aSJosef Bacik u32 nodesize; 3207ae18c37aSJosef Bacik u32 stripesize; 3208ae18c37aSJosef Bacik u64 generation; 3209ae18c37aSJosef Bacik u64 features; 3210ae18c37aSJosef Bacik u16 csum_type; 3211ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3212ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3213ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3214ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3215ae18c37aSJosef Bacik int ret; 3216ae18c37aSJosef Bacik int err = -EINVAL; 3217ae18c37aSJosef Bacik int level; 3218ae18c37aSJosef Bacik 32198260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 322053b381b3SDavid Woodhouse if (ret) { 322183c8266aSDavid Sterba err = ret; 3222ae18c37aSJosef Bacik goto fail; 322353b381b3SDavid Woodhouse } 322453b381b3SDavid Woodhouse 3225ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3226ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3227ae18c37aSJosef Bacik GFP_KERNEL); 3228ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3229ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3230ae18c37aSJosef Bacik GFP_KERNEL); 3231ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3232ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3233ae18c37aSJosef Bacik err = -ENOMEM; 3234c75e8394SJosef Bacik goto fail; 3235ae18c37aSJosef Bacik } 3236ae18c37aSJosef Bacik 3237ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3238ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3239ae18c37aSJosef Bacik err = -ENOMEM; 3240c75e8394SJosef Bacik goto fail; 3241ae18c37aSJosef Bacik } 3242ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3243ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3244ae18c37aSJosef Bacik 32453c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 32461104a885SDavid Sterba 32471104a885SDavid Sterba /* 32481104a885SDavid Sterba * Read super block and check the signature bytes only 32491104a885SDavid Sterba */ 32508f32380dSJohannes Thumshirn disk_super = btrfs_read_dev_super(fs_devices->latest_bdev); 32518f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 32528f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 325316cdcec7SMiao Xie goto fail_alloc; 325420b45077SDave Young } 325539279cc3SChris Mason 32561104a885SDavid Sterba /* 3257260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 32588dc3f22cSJohannes Thumshirn * corrupted, we'll find out 32598dc3f22cSJohannes Thumshirn */ 32608f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 326151bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 32628dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 326351bce6c9SJohannes Thumshirn csum_type); 32648dc3f22cSJohannes Thumshirn err = -EINVAL; 32658f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 32668dc3f22cSJohannes Thumshirn goto fail_alloc; 32678dc3f22cSJohannes Thumshirn } 32688dc3f22cSJohannes Thumshirn 326983c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 327083c68bbcSSu Yue 32716d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 32726d97c6e3SJohannes Thumshirn if (ret) { 32736d97c6e3SJohannes Thumshirn err = ret; 32748f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 32756d97c6e3SJohannes Thumshirn goto fail_alloc; 32766d97c6e3SJohannes Thumshirn } 32776d97c6e3SJohannes Thumshirn 32788dc3f22cSJohannes Thumshirn /* 32791104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 32801104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 32811104a885SDavid Sterba */ 32828f32380dSJohannes Thumshirn if (btrfs_check_super_csum(fs_info, (u8 *)disk_super)) { 328305135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 32841104a885SDavid Sterba err = -EINVAL; 32858f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3286141386e1SJosef Bacik goto fail_alloc; 32871104a885SDavid Sterba } 32881104a885SDavid Sterba 32891104a885SDavid Sterba /* 32901104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 32911104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 32921104a885SDavid Sterba * the whole block of INFO_SIZE 32931104a885SDavid Sterba */ 32948f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 32958f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 32965f39d397SChris Mason 3297fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3298fbc6feaeSNikolay Borisov 32990b86a832SChris Mason 3300fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3301fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3302fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3303fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3304fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3305fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3306fbc6feaeSNikolay Borisov } 3307fbc6feaeSNikolay Borisov 3308fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3309fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3310de37aa51SNikolay Borisov 3311069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 33121104a885SDavid Sterba if (ret) { 331305135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 33141104a885SDavid Sterba err = -EINVAL; 3315141386e1SJosef Bacik goto fail_alloc; 33161104a885SDavid Sterba } 33171104a885SDavid Sterba 33180f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3319141386e1SJosef Bacik goto fail_alloc; 33200f7d52f4SChris Mason 3321acce952bSliubo /* check FS state, whether FS is broken. */ 332287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 332387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3324acce952bSliubo 332575e7cb7fSLiu Bo /* 332675e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 332775e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 332875e7cb7fSLiu Bo */ 332975e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 333075e7cb7fSLiu Bo 33312ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 33322b82032cSYan Zheng if (ret) { 33332b82032cSYan Zheng err = ret; 3334141386e1SJosef Bacik goto fail_alloc; 33352b82032cSYan Zheng } 3336dfe25020SChris Mason 3337f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 3338f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 3339f2b636e8SJosef Bacik if (features) { 334005135f59SDavid Sterba btrfs_err(fs_info, 334105135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 3342c1c9ff7cSGeert Uytterhoeven features); 3343f2b636e8SJosef Bacik err = -EINVAL; 3344141386e1SJosef Bacik goto fail_alloc; 3345f2b636e8SJosef Bacik } 3346f2b636e8SJosef Bacik 33475d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 33485d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 33490b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3350a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 33515c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 33525c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3353727011e0SChris Mason 33543173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 335505135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 33563173a18fSJosef Bacik 3357727011e0SChris Mason /* 3358727011e0SChris Mason * flag our filesystem as having big metadata blocks if 3359727011e0SChris Mason * they are bigger than the page size 3360727011e0SChris Mason */ 336109cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 3362727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 336305135f59SDavid Sterba btrfs_info(fs_info, 336405135f59SDavid Sterba "flagging fs with big metadata feature"); 3365727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3366727011e0SChris Mason } 3367727011e0SChris Mason 3368bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 3369bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 3370b7f67055SChandan Rajendra stripesize = sectorsize; 3371707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 3372963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 3373bc3f116fSChris Mason 3374da17066cSJeff Mahoney /* Cache block sizes */ 3375da17066cSJeff Mahoney fs_info->nodesize = nodesize; 3376da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 3377ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 3378fe5ecbe8SDavid Sterba fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3379da17066cSJeff Mahoney fs_info->stripesize = stripesize; 3380da17066cSJeff Mahoney 3381bc3f116fSChris Mason /* 3382bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 3383bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 3384bc3f116fSChris Mason */ 3385bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3386707e8a07SDavid Sterba (sectorsize != nodesize)) { 338705135f59SDavid Sterba btrfs_err(fs_info, 338805135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 338905135f59SDavid Sterba nodesize, sectorsize); 3390141386e1SJosef Bacik goto fail_alloc; 3391bc3f116fSChris Mason } 3392bc3f116fSChris Mason 3393ceda0864SMiao Xie /* 3394ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 3395ceda0864SMiao Xie * update the flag. 3396ceda0864SMiao Xie */ 33975d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 33985d4f98a2SYan Zheng 3399f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 3400f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 3401bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 340205135f59SDavid Sterba btrfs_err(fs_info, 340305135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 3404c1c9ff7cSGeert Uytterhoeven features); 3405f2b636e8SJosef Bacik err = -EINVAL; 3406141386e1SJosef Bacik goto fail_alloc; 3407f2b636e8SJosef Bacik } 340861d92c32SChris Mason 34090bb3eb3eSQu Wenruo /* For 4K sector size support, it's only read-only */ 34100bb3eb3eSQu Wenruo if (PAGE_SIZE == SZ_64K && sectorsize == SZ_4K) { 34110bb3eb3eSQu Wenruo if (!sb_rdonly(sb) || btrfs_super_log_root(disk_super)) { 34120bb3eb3eSQu Wenruo btrfs_err(fs_info, 34130bb3eb3eSQu Wenruo "subpage sectorsize %u only supported read-only for page size %lu", 34140bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 34150bb3eb3eSQu Wenruo err = -EINVAL; 34160bb3eb3eSQu Wenruo goto fail_alloc; 34170bb3eb3eSQu Wenruo } 34180bb3eb3eSQu Wenruo } 34190bb3eb3eSQu Wenruo 34202a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 34212a458198SEric Sandeen if (ret) { 34222a458198SEric Sandeen err = ret; 34230dc3b84aSJosef Bacik goto fail_sb_buffer; 34240dc3b84aSJosef Bacik } 34254543df7eSChris Mason 34269e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 34279e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 34284575c9ccSChris Mason 3429a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3430a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3431de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3432db94535dSChris Mason 3433925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 34346bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3435925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 343684eed90fSChris Mason if (ret) { 343705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 34385d4f98a2SYan Zheng goto fail_sb_buffer; 343984eed90fSChris Mason } 34400b86a832SChris Mason 344184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3442581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 34430b86a832SChris Mason 34442ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 34450b86a832SChris Mason btrfs_super_chunk_root(disk_super), 34461b7ec85eSJosef Bacik BTRFS_CHUNK_TREE_OBJECTID, 3447581c1760SQu Wenruo generation, level, NULL); 344864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 344964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 345005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3451e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 3452e5fffbacSchandan free_extent_buffer(chunk_root->node); 345395ab1f64SZhao Lei chunk_root->node = NULL; 3454af31f5e5SChris Mason goto fail_tree_roots; 345583121942SDavid Woodhouse } 34565d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 34575d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 34580b86a832SChris Mason 3459e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3460c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3461c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3462e17cade2SChris Mason 34635b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 34642b82032cSYan Zheng if (ret) { 346505135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3466af31f5e5SChris Mason goto fail_tree_roots; 34672b82032cSYan Zheng } 34680b86a832SChris Mason 34698dabb742SStefan Behrens /* 3470bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3471bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3472bacce86aSAnand Jain * target is required. So free devices that are not part of this 3473bacce86aSAnand Jain * filesystem but skip the replace traget device which is checked 3474bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 34758dabb742SStefan Behrens */ 3476bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3477a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 347805135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3479a6b0d5c8SChris Mason goto fail_tree_roots; 3480a6b0d5c8SChris Mason } 3481a6b0d5c8SChris Mason 3482b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 34834bbcaa64SEric Sandeen if (ret) 3484b8522a1eSNikolay Borisov goto fail_tree_roots; 34858929ecfaSYan, Zheng 348675ec1db8SJosef Bacik /* 348773651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 348873651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 348973651042SNaohiro Aota * zoned incompat feature flag set. 349073651042SNaohiro Aota */ 349173651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 349273651042SNaohiro Aota if (ret) { 349373651042SNaohiro Aota btrfs_err(fs_info, 349473651042SNaohiro Aota "zoned: failed to read device zone info: %d", 349573651042SNaohiro Aota ret); 349673651042SNaohiro Aota goto fail_block_groups; 349773651042SNaohiro Aota } 349873651042SNaohiro Aota 349973651042SNaohiro Aota /* 350075ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 350175ec1db8SJosef Bacik * check the generation here so we can set the 350275ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 350375ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 350475ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 350575ec1db8SJosef Bacik * even though it was perfectly fine. 350675ec1db8SJosef Bacik */ 350775ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 350875ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 350975ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 351075ec1db8SJosef Bacik 3511cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3512cf90d884SQu Wenruo if (ret) { 3513cf90d884SQu Wenruo btrfs_err(fs_info, 3514cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3515cf90d884SQu Wenruo ret); 3516cf90d884SQu Wenruo goto fail_block_groups; 3517cf90d884SQu Wenruo } 351868310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 351968310a5eSIlya Dryomov if (ret) { 352005135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 352168310a5eSIlya Dryomov goto fail_block_groups; 352268310a5eSIlya Dryomov } 352368310a5eSIlya Dryomov 3524733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3525733f4fbbSStefan Behrens if (ret) { 352605135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3527733f4fbbSStefan Behrens goto fail_block_groups; 3528733f4fbbSStefan Behrens } 3529733f4fbbSStefan Behrens 35308dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 35318dabb742SStefan Behrens if (ret) { 353205135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 35338dabb742SStefan Behrens goto fail_block_groups; 35348dabb742SStefan Behrens } 35358dabb742SStefan Behrens 3536b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3537b70f5097SNaohiro Aota if (ret) { 3538b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3539b70f5097SNaohiro Aota ret); 3540b70f5097SNaohiro Aota goto fail_block_groups; 3541b70f5097SNaohiro Aota } 3542b70f5097SNaohiro Aota 3543c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3544b7c35e81SAnand Jain if (ret) { 354505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 354605135f59SDavid Sterba ret); 3547b7c35e81SAnand Jain goto fail_block_groups; 3548b7c35e81SAnand Jain } 3549b7c35e81SAnand Jain 355096f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 35515ac1d209SJeff Mahoney if (ret) { 355205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3553b7c35e81SAnand Jain goto fail_fsdev_sysfs; 35545ac1d209SJeff Mahoney } 35555ac1d209SJeff Mahoney 3556c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3557c59021f8Sliubo if (ret) { 355805135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 35592365dd3cSAnand Jain goto fail_sysfs; 3560c59021f8Sliubo } 3561c59021f8Sliubo 35625b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 35631b1d1f66SJosef Bacik if (ret) { 356405135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 35652365dd3cSAnand Jain goto fail_sysfs; 35661b1d1f66SJosef Bacik } 35674330e183SQu Wenruo 35686528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 356905135f59SDavid Sterba btrfs_warn(fs_info, 357052042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 35712365dd3cSAnand Jain goto fail_sysfs; 3572292fd7fcSStefan Behrens } 35739078a3e1SChris Mason 3574a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3575a74a4b97SChris Mason "btrfs-cleaner"); 357657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 35772365dd3cSAnand Jain goto fail_sysfs; 3578a74a4b97SChris Mason 3579a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3580a74a4b97SChris Mason tree_root, 3581a74a4b97SChris Mason "btrfs-transaction"); 358257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 35833f157a2fSChris Mason goto fail_cleaner; 3584a74a4b97SChris Mason 3585583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3586c289811cSChris Mason !fs_info->fs_devices->rotating) { 3587583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3588c289811cSChris Mason } 3589c289811cSChris Mason 3590572d9ab7SDavid Sterba /* 359101327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3592572d9ab7SDavid Sterba * not have to wait for transaction commit 3593572d9ab7SDavid Sterba */ 3594572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 35953818aea2SQu Wenruo 359621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 35970b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 35982ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 35990b246afaSJeff Mahoney btrfs_test_opt(fs_info, 360021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 360121adbd5cSStefan Behrens 1 : 0, 360221adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 360321adbd5cSStefan Behrens if (ret) 360405135f59SDavid Sterba btrfs_warn(fs_info, 360505135f59SDavid Sterba "failed to initialize integrity check module: %d", 360605135f59SDavid Sterba ret); 360721adbd5cSStefan Behrens } 360821adbd5cSStefan Behrens #endif 3609bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3610bcef60f2SArne Jansen if (ret) 3611bcef60f2SArne Jansen goto fail_trans_kthread; 361221adbd5cSStefan Behrens 3613fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3614fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3615fd708b81SJosef Bacik 361696da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 361796da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 36180b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3619e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 362063443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 362179787eaaSJeff Mahoney if (ret) { 362263443bf5SEric Sandeen err = ret; 362328c16cbbSWang Shilong goto fail_qgroup; 3624e556ce2cSYan Zheng } 3625e02119d5SChris Mason } 36261a40e23bSZheng Yan 362756e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 36283140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 36293140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3630f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3631315bf8efSJosef Bacik fs_info->fs_root = NULL; 3632bcef60f2SArne Jansen goto fail_qgroup; 36333140c9a3SDan Carpenter } 3634c289811cSChris Mason 3635bc98a42cSDavid Howells if (sb_rdonly(sb)) 36368cd29088SBoris Burkov goto clear_oneshot; 36372b6ba629SIlya Dryomov 363844c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 36392b6ba629SIlya Dryomov if (ret) { 36406bccf3abSJeff Mahoney close_ctree(fs_info); 36412b6ba629SIlya Dryomov return ret; 3642e3acc2a6SJosef Bacik } 3643b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3644b382a324SJan Schmidt 364544c0ca21SBoris Burkov if (fs_info->uuid_root && 364644c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 364744c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 364805135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 364970f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 365070f80175SStefan Behrens if (ret) { 365105135f59SDavid Sterba btrfs_warn(fs_info, 365205135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 36536bccf3abSJeff Mahoney close_ctree(fs_info); 365470f80175SStefan Behrens return ret; 365570f80175SStefan Behrens } 3656f7a81ea4SStefan Behrens } 365794846229SBoris Burkov 3658afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 365947ab2a6cSJosef Bacik 36608cd29088SBoris Burkov clear_oneshot: 36618cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3662ad2b2c80SAl Viro return 0; 366339279cc3SChris Mason 3664bcef60f2SArne Jansen fail_qgroup: 3665bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 36667c2ca468SChris Mason fail_trans_kthread: 36677c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 36682ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3669faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 36703f157a2fSChris Mason fail_cleaner: 3671a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 36727c2ca468SChris Mason 36737c2ca468SChris Mason /* 36747c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 36757c2ca468SChris Mason * kthreads 36767c2ca468SChris Mason */ 36777c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 36787c2ca468SChris Mason 36792365dd3cSAnand Jain fail_sysfs: 36806618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 36812365dd3cSAnand Jain 3682b7c35e81SAnand Jain fail_fsdev_sysfs: 3683b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3684b7c35e81SAnand Jain 36851b1d1f66SJosef Bacik fail_block_groups: 368654067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3687af31f5e5SChris Mason 3688af31f5e5SChris Mason fail_tree_roots: 36899e3aa805SJosef Bacik if (fs_info->data_reloc_root) 36909e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 36914273eaffSAnand Jain free_root_pointers(fs_info, true); 36922b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3693af31f5e5SChris Mason 369439279cc3SChris Mason fail_sb_buffer: 36957abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 36965cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 369716cdcec7SMiao Xie fail_alloc: 3698586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3699586e46e2SIlya Dryomov 37004543df7eSChris Mason iput(fs_info->btree_inode); 37017e662854SQinghuang Feng fail: 3702586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3703ad2b2c80SAl Viro return err; 3704eb60ceacSChris Mason } 3705663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3706eb60ceacSChris Mason 3707314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3708f2984462SChris Mason { 3709314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3710314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3711314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3712314b6dd0SJohannes Thumshirn struct page *page; 3713442a4f63SStefan Behrens 3714314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3715314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3716314b6dd0SJohannes Thumshirn 3717314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3718fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3719314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3720314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3721314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3722314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3723314b6dd0SJohannes Thumshirn SetPageError(page); 3724314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3725314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3726314b6dd0SJohannes Thumshirn } else { 3727314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3728f2984462SChris Mason } 3729314b6dd0SJohannes Thumshirn 3730314b6dd0SJohannes Thumshirn put_page(page); 3731314b6dd0SJohannes Thumshirn unlock_page(page); 3732314b6dd0SJohannes Thumshirn } 3733314b6dd0SJohannes Thumshirn 3734314b6dd0SJohannes Thumshirn bio_put(bio); 3735f2984462SChris Mason } 3736f2984462SChris Mason 37378f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 37388f32380dSJohannes Thumshirn int copy_num) 373929c36d72SAnand Jain { 374029c36d72SAnand Jain struct btrfs_super_block *super; 37418f32380dSJohannes Thumshirn struct page *page; 374212659251SNaohiro Aota u64 bytenr, bytenr_orig; 37438f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 374412659251SNaohiro Aota int ret; 374529c36d72SAnand Jain 374612659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 374712659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 374812659251SNaohiro Aota if (ret == -ENOENT) 374912659251SNaohiro Aota return ERR_PTR(-EINVAL); 375012659251SNaohiro Aota else if (ret) 375112659251SNaohiro Aota return ERR_PTR(ret); 375212659251SNaohiro Aota 375329c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 37548f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 375529c36d72SAnand Jain 37568f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 37578f32380dSJohannes Thumshirn if (IS_ERR(page)) 37588f32380dSJohannes Thumshirn return ERR_CAST(page); 375929c36d72SAnand Jain 37608f32380dSJohannes Thumshirn super = page_address(page); 376196c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 376296c2e067SAnand Jain btrfs_release_disk_super(super); 376396c2e067SAnand Jain return ERR_PTR(-ENODATA); 376496c2e067SAnand Jain } 376596c2e067SAnand Jain 376612659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 37678f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 37688f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 376929c36d72SAnand Jain } 377029c36d72SAnand Jain 37718f32380dSJohannes Thumshirn return super; 377229c36d72SAnand Jain } 377329c36d72SAnand Jain 377429c36d72SAnand Jain 37758f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 3776a512bbf8SYan Zheng { 37778f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 3778a512bbf8SYan Zheng int i; 3779a512bbf8SYan Zheng u64 transid = 0; 3780a512bbf8SYan Zheng 3781a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3782a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3783a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3784a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3785a512bbf8SYan Zheng */ 3786a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 37878f32380dSJohannes Thumshirn super = btrfs_read_dev_one_super(bdev, i); 37888f32380dSJohannes Thumshirn if (IS_ERR(super)) 3789a512bbf8SYan Zheng continue; 3790a512bbf8SYan Zheng 3791a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 37928f32380dSJohannes Thumshirn if (latest) 37938f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 37948f32380dSJohannes Thumshirn 37958f32380dSJohannes Thumshirn latest = super; 3796a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3797a512bbf8SYan Zheng } 3798a512bbf8SYan Zheng } 379992fc03fbSAnand Jain 38008f32380dSJohannes Thumshirn return super; 3801a512bbf8SYan Zheng } 3802a512bbf8SYan Zheng 38034eedeb75SHisashi Hifumi /* 3804abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3805314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 38064eedeb75SHisashi Hifumi * 3807abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3808abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3809abbb3b8eSDavid Sterba * same for write and wait phases. 38104eedeb75SHisashi Hifumi * 3811314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 38124eedeb75SHisashi Hifumi */ 3813a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3814abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3815a512bbf8SYan Zheng { 3816d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 3817314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 3818d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 3819a512bbf8SYan Zheng int i; 3820a512bbf8SYan Zheng int errors = 0; 382112659251SNaohiro Aota int ret; 382212659251SNaohiro Aota u64 bytenr, bytenr_orig; 3823a512bbf8SYan Zheng 3824a512bbf8SYan Zheng if (max_mirrors == 0) 3825a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3826a512bbf8SYan Zheng 3827d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 3828d5178578SJohannes Thumshirn 3829a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3830314b6dd0SJohannes Thumshirn struct page *page; 3831314b6dd0SJohannes Thumshirn struct bio *bio; 3832314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 3833314b6dd0SJohannes Thumshirn 383412659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 383512659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 383612659251SNaohiro Aota if (ret == -ENOENT) { 383712659251SNaohiro Aota continue; 383812659251SNaohiro Aota } else if (ret < 0) { 383912659251SNaohiro Aota btrfs_err(device->fs_info, 384012659251SNaohiro Aota "couldn't get super block location for mirror %d", 384112659251SNaohiro Aota i); 384212659251SNaohiro Aota errors++; 384312659251SNaohiro Aota continue; 384412659251SNaohiro Aota } 3845935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3846935e5cc9SMiao Xie device->commit_total_bytes) 3847a512bbf8SYan Zheng break; 3848a512bbf8SYan Zheng 384912659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 3850a512bbf8SYan Zheng 3851fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 3852fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 3853fd08001fSEric Biggers sb->csum); 3854a512bbf8SYan Zheng 3855314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 3856314b6dd0SJohannes Thumshirn GFP_NOFS); 3857314b6dd0SJohannes Thumshirn if (!page) { 3858fb456252SJeff Mahoney btrfs_err(device->fs_info, 3859314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 3860f14d104dSDavid Sterba bytenr); 3861634554dcSJosef Bacik errors++; 3862634554dcSJosef Bacik continue; 3863634554dcSJosef Bacik } 3864634554dcSJosef Bacik 3865314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 3866314b6dd0SJohannes Thumshirn get_page(page); 3867a512bbf8SYan Zheng 3868314b6dd0SJohannes Thumshirn disk_super = page_address(page); 3869314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 3870a512bbf8SYan Zheng 3871387125fcSChris Mason /* 3872314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 3873314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 3874314b6dd0SJohannes Thumshirn * checking. 3875387125fcSChris Mason */ 3876314b6dd0SJohannes Thumshirn bio = bio_alloc(GFP_NOFS, 1); 3877314b6dd0SJohannes Thumshirn bio_set_dev(bio, device->bdev); 3878314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 3879314b6dd0SJohannes Thumshirn bio->bi_private = device; 3880314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 3881314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 3882314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 3883314b6dd0SJohannes Thumshirn 3884314b6dd0SJohannes Thumshirn /* 3885314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 3886314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 3887314b6dd0SJohannes Thumshirn * copies might still contain the older version. 3888314b6dd0SJohannes Thumshirn */ 3889314b6dd0SJohannes Thumshirn bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; 38901b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 3891314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 3892314b6dd0SJohannes Thumshirn 3893314b6dd0SJohannes Thumshirn btrfsic_submit_bio(bio); 389412659251SNaohiro Aota btrfs_advance_sb_log(device, i); 3895a512bbf8SYan Zheng } 3896a512bbf8SYan Zheng return errors < i ? 0 : -1; 3897a512bbf8SYan Zheng } 3898a512bbf8SYan Zheng 3899387125fcSChris Mason /* 3900abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3901abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3902abbb3b8eSDavid Sterba * 3903314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 3904abbb3b8eSDavid Sterba * date. 3905abbb3b8eSDavid Sterba */ 3906abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3907abbb3b8eSDavid Sterba { 3908abbb3b8eSDavid Sterba int i; 3909abbb3b8eSDavid Sterba int errors = 0; 3910b6a535faSHoward McLauchlan bool primary_failed = false; 391112659251SNaohiro Aota int ret; 3912abbb3b8eSDavid Sterba u64 bytenr; 3913abbb3b8eSDavid Sterba 3914abbb3b8eSDavid Sterba if (max_mirrors == 0) 3915abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3916abbb3b8eSDavid Sterba 3917abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3918314b6dd0SJohannes Thumshirn struct page *page; 3919314b6dd0SJohannes Thumshirn 392012659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 392112659251SNaohiro Aota if (ret == -ENOENT) { 392212659251SNaohiro Aota break; 392312659251SNaohiro Aota } else if (ret < 0) { 392412659251SNaohiro Aota errors++; 392512659251SNaohiro Aota if (i == 0) 392612659251SNaohiro Aota primary_failed = true; 392712659251SNaohiro Aota continue; 392812659251SNaohiro Aota } 3929abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3930abbb3b8eSDavid Sterba device->commit_total_bytes) 3931abbb3b8eSDavid Sterba break; 3932abbb3b8eSDavid Sterba 3933314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 3934314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 3935314b6dd0SJohannes Thumshirn if (!page) { 3936abbb3b8eSDavid Sterba errors++; 3937b6a535faSHoward McLauchlan if (i == 0) 3938b6a535faSHoward McLauchlan primary_failed = true; 3939abbb3b8eSDavid Sterba continue; 3940abbb3b8eSDavid Sterba } 3941314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 3942314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 3943314b6dd0SJohannes Thumshirn if (PageError(page)) { 3944abbb3b8eSDavid Sterba errors++; 3945b6a535faSHoward McLauchlan if (i == 0) 3946b6a535faSHoward McLauchlan primary_failed = true; 3947b6a535faSHoward McLauchlan } 3948abbb3b8eSDavid Sterba 3949314b6dd0SJohannes Thumshirn /* Drop our reference */ 3950314b6dd0SJohannes Thumshirn put_page(page); 3951abbb3b8eSDavid Sterba 3952314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 3953314b6dd0SJohannes Thumshirn put_page(page); 3954abbb3b8eSDavid Sterba } 3955abbb3b8eSDavid Sterba 3956b6a535faSHoward McLauchlan /* log error, force error return */ 3957b6a535faSHoward McLauchlan if (primary_failed) { 3958b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3959b6a535faSHoward McLauchlan device->devid); 3960b6a535faSHoward McLauchlan return -1; 3961b6a535faSHoward McLauchlan } 3962b6a535faSHoward McLauchlan 3963abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3964abbb3b8eSDavid Sterba } 3965abbb3b8eSDavid Sterba 3966abbb3b8eSDavid Sterba /* 3967387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3968387125fcSChris Mason * for the barrier when it is done 3969387125fcSChris Mason */ 39704246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3971387125fcSChris Mason { 3972387125fcSChris Mason complete(bio->bi_private); 3973387125fcSChris Mason } 3974387125fcSChris Mason 3975387125fcSChris Mason /* 39764fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 39774fc6441aSAnand Jain * done in the waiting counterpart. 3978387125fcSChris Mason */ 39794fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3980387125fcSChris Mason { 3981c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3982e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3983387125fcSChris Mason 3984c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 39854fc6441aSAnand Jain return; 3986387125fcSChris Mason 3987e0ae9994SDavid Sterba bio_reset(bio); 3988387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 398974d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 39908d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3991387125fcSChris Mason init_completion(&device->flush_wait); 3992387125fcSChris Mason bio->bi_private = &device->flush_wait; 3993387125fcSChris Mason 399443a01111SLu Fengqi btrfsic_submit_bio(bio); 39951c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 39964fc6441aSAnand Jain } 3997387125fcSChris Mason 39984fc6441aSAnand Jain /* 39994fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 40004fc6441aSAnand Jain */ 40018c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 40024fc6441aSAnand Jain { 40034fc6441aSAnand Jain struct bio *bio = device->flush_bio; 40044fc6441aSAnand Jain 40051c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 400658efbc9fSOmar Sandoval return BLK_STS_OK; 40074fc6441aSAnand Jain 40081c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 40092980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 40104fc6441aSAnand Jain 40118c27cb35SLinus Torvalds return bio->bi_status; 4012387125fcSChris Mason } 4013387125fcSChris Mason 4014d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4015401b41e5SAnand Jain { 40166528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4017401b41e5SAnand Jain return -EIO; 4018387125fcSChris Mason return 0; 4019387125fcSChris Mason } 4020387125fcSChris Mason 4021387125fcSChris Mason /* 4022387125fcSChris Mason * send an empty flush down to each device in parallel, 4023387125fcSChris Mason * then wait for them 4024387125fcSChris Mason */ 4025387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4026387125fcSChris Mason { 4027387125fcSChris Mason struct list_head *head; 4028387125fcSChris Mason struct btrfs_device *dev; 40295af3e8ccSStefan Behrens int errors_wait = 0; 40304e4cbee9SChristoph Hellwig blk_status_t ret; 4031387125fcSChris Mason 40321538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4033387125fcSChris Mason /* send down all the barriers */ 4034387125fcSChris Mason head = &info->fs_devices->devices; 40351538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4036e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4037f88ba6a2SHidetoshi Seto continue; 4038cea7c8bfSAnand Jain if (!dev->bdev) 4039387125fcSChris Mason continue; 4040e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4041ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4042387125fcSChris Mason continue; 4043387125fcSChris Mason 40444fc6441aSAnand Jain write_dev_flush(dev); 404558efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4046387125fcSChris Mason } 4047387125fcSChris Mason 4048387125fcSChris Mason /* wait for all the barriers */ 40491538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4050e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4051f88ba6a2SHidetoshi Seto continue; 4052387125fcSChris Mason if (!dev->bdev) { 40535af3e8ccSStefan Behrens errors_wait++; 4054387125fcSChris Mason continue; 4055387125fcSChris Mason } 4056e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4057ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4058387125fcSChris Mason continue; 4059387125fcSChris Mason 40604fc6441aSAnand Jain ret = wait_dev_flush(dev); 4061401b41e5SAnand Jain if (ret) { 4062401b41e5SAnand Jain dev->last_flush_error = ret; 406366b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 406466b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 40655af3e8ccSStefan Behrens errors_wait++; 4066387125fcSChris Mason } 4067401b41e5SAnand Jain } 4068401b41e5SAnand Jain 4069cea7c8bfSAnand Jain if (errors_wait) { 4070401b41e5SAnand Jain /* 4071401b41e5SAnand Jain * At some point we need the status of all disks 4072401b41e5SAnand Jain * to arrive at the volume status. So error checking 4073401b41e5SAnand Jain * is being pushed to a separate loop. 4074401b41e5SAnand Jain */ 4075d10b82feSQu Wenruo return check_barrier_error(info); 4076401b41e5SAnand Jain } 4077387125fcSChris Mason return 0; 4078387125fcSChris Mason } 4079387125fcSChris Mason 4080943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4081943c6e99SZhao Lei { 40828789f4feSZhao Lei int raid_type; 40838789f4feSZhao Lei int min_tolerated = INT_MAX; 4084943c6e99SZhao Lei 40858789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 40868789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 40878c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 40888789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 40898789f4feSZhao Lei tolerated_failures); 4090943c6e99SZhao Lei 40918789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 40928789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 40938789f4feSZhao Lei continue; 409441a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 40958789f4feSZhao Lei continue; 40968c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 40978789f4feSZhao Lei btrfs_raid_array[raid_type]. 40988789f4feSZhao Lei tolerated_failures); 40998789f4feSZhao Lei } 4100943c6e99SZhao Lei 41018789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4102ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 41038789f4feSZhao Lei min_tolerated = 0; 41048789f4feSZhao Lei } 41058789f4feSZhao Lei 41068789f4feSZhao Lei return min_tolerated; 4107943c6e99SZhao Lei } 4108943c6e99SZhao Lei 4109eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4110f2984462SChris Mason { 4111e5e9a520SChris Mason struct list_head *head; 4112f2984462SChris Mason struct btrfs_device *dev; 4113a061fc8dSChris Mason struct btrfs_super_block *sb; 4114f2984462SChris Mason struct btrfs_dev_item *dev_item; 4115f2984462SChris Mason int ret; 4116f2984462SChris Mason int do_barriers; 4117a236aed1SChris Mason int max_errors; 4118a236aed1SChris Mason int total_errors = 0; 4119a061fc8dSChris Mason u64 flags; 4120f2984462SChris Mason 41210b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4122fed3b381SLiu Bo 4123fed3b381SLiu Bo /* 4124fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4125fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4126fed3b381SLiu Bo * been consistent on disk. 4127fed3b381SLiu Bo */ 4128fed3b381SLiu Bo if (max_mirrors == 0) 41290b246afaSJeff Mahoney backup_super_roots(fs_info); 4130f2984462SChris Mason 41310b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4132a061fc8dSChris Mason dev_item = &sb->dev_item; 4133e5e9a520SChris Mason 41340b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 41350b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 41360b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4137387125fcSChris Mason 41385af3e8ccSStefan Behrens if (do_barriers) { 41390b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 41405af3e8ccSStefan Behrens if (ret) { 41415af3e8ccSStefan Behrens mutex_unlock( 41420b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 41430b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 41445af3e8ccSStefan Behrens "errors while submitting device barriers."); 41455af3e8ccSStefan Behrens return ret; 41465af3e8ccSStefan Behrens } 41475af3e8ccSStefan Behrens } 4148387125fcSChris Mason 41491538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4150dfe25020SChris Mason if (!dev->bdev) { 4151dfe25020SChris Mason total_errors++; 4152dfe25020SChris Mason continue; 4153dfe25020SChris Mason } 4154e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4155ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4156dfe25020SChris Mason continue; 4157dfe25020SChris Mason 41582b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4159a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4160a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 41617df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4162935e5cc9SMiao Xie dev->commit_total_bytes); 4163ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4164ce7213c7SMiao Xie dev->commit_bytes_used); 4165a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4166a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4167a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4168a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 41697239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 41707239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4171a512bbf8SYan Zheng 4172a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4173a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4174f2984462SChris Mason 417575cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 417675cb857dSQu Wenruo if (ret < 0) { 417775cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 417875cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 417975cb857dSQu Wenruo "unexpected superblock corruption detected"); 418075cb857dSQu Wenruo return -EUCLEAN; 418175cb857dSQu Wenruo } 418275cb857dSQu Wenruo 4183abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4184a236aed1SChris Mason if (ret) 4185a236aed1SChris Mason total_errors++; 4186f2984462SChris Mason } 4187a236aed1SChris Mason if (total_errors > max_errors) { 41880b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4189d397712bSChris Mason total_errors); 41900b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 419179787eaaSJeff Mahoney 41929d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 41930b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 41940b246afaSJeff Mahoney "%d errors while writing supers", 41950b246afaSJeff Mahoney total_errors); 41969d565ba4SStefan Behrens return -EIO; 4197a236aed1SChris Mason } 4198f2984462SChris Mason 4199a512bbf8SYan Zheng total_errors = 0; 42001538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4201dfe25020SChris Mason if (!dev->bdev) 4202dfe25020SChris Mason continue; 4203e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4204ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4205dfe25020SChris Mason continue; 4206dfe25020SChris Mason 4207abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4208a512bbf8SYan Zheng if (ret) 42091259ab75SChris Mason total_errors++; 4210f2984462SChris Mason } 42110b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4212a236aed1SChris Mason if (total_errors > max_errors) { 42130b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 42140b246afaSJeff Mahoney "%d errors while writing supers", 42150b246afaSJeff Mahoney total_errors); 421679787eaaSJeff Mahoney return -EIO; 4217a236aed1SChris Mason } 4218f2984462SChris Mason return 0; 4219f2984462SChris Mason } 4220f2984462SChris Mason 4221cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4222cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4223cb517eabSMiao Xie struct btrfs_root *root) 42242619ba1fSChris Mason { 42254785e24fSJosef Bacik bool drop_ref = false; 42264785e24fSJosef Bacik 42274df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 42282619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 42292619ba1fSChris Mason (unsigned long)root->root_key.objectid); 4230af01d2e5SJosef Bacik if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 42314785e24fSJosef Bacik drop_ref = true; 42324df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 423376dda93cSYan, Zheng 42341c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 4235ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 42361c1ea4f7SLiu Bo if (root->reloc_root) { 423700246528SJosef Bacik btrfs_put_root(root->reloc_root); 42381c1ea4f7SLiu Bo root->reloc_root = NULL; 42391c1ea4f7SLiu Bo } 42401c1ea4f7SLiu Bo } 42413321719eSLiu Bo 42424785e24fSJosef Bacik if (drop_ref) 424300246528SJosef Bacik btrfs_put_root(root); 42442619ba1fSChris Mason } 42452619ba1fSChris Mason 4246c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4247c146afadSYan Zheng { 4248c146afadSYan Zheng u64 root_objectid = 0; 4249c146afadSYan Zheng struct btrfs_root *gang[8]; 425065d33fd7SQu Wenruo int i = 0; 425165d33fd7SQu Wenruo int err = 0; 425265d33fd7SQu Wenruo unsigned int ret = 0; 4253c146afadSYan Zheng 4254c146afadSYan Zheng while (1) { 4255efc34534SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4256c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4257c146afadSYan Zheng (void **)gang, root_objectid, 4258c146afadSYan Zheng ARRAY_SIZE(gang)); 425965d33fd7SQu Wenruo if (!ret) { 4260efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4261c146afadSYan Zheng break; 426265d33fd7SQu Wenruo } 42635d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 426466b4ffd1SJosef Bacik 426565d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 426665d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 426765d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 426865d33fd7SQu Wenruo gang[i] = NULL; 426965d33fd7SQu Wenruo continue; 427065d33fd7SQu Wenruo } 427165d33fd7SQu Wenruo /* grab all the search result for later use */ 427200246528SJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 427365d33fd7SQu Wenruo } 4274efc34534SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 427565d33fd7SQu Wenruo 427665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 427765d33fd7SQu Wenruo if (!gang[i]) 427865d33fd7SQu Wenruo continue; 4279c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 428066b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 428166b4ffd1SJosef Bacik if (err) 428265d33fd7SQu Wenruo break; 428300246528SJosef Bacik btrfs_put_root(gang[i]); 4284c146afadSYan Zheng } 4285c146afadSYan Zheng root_objectid++; 4286c146afadSYan Zheng } 428765d33fd7SQu Wenruo 428865d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 428965d33fd7SQu Wenruo for (; i < ret; i++) { 429065d33fd7SQu Wenruo if (gang[i]) 429100246528SJosef Bacik btrfs_put_root(gang[i]); 429265d33fd7SQu Wenruo } 429365d33fd7SQu Wenruo return err; 4294c146afadSYan Zheng } 4295c146afadSYan Zheng 42966bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4297c146afadSYan Zheng { 42986bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4299c146afadSYan Zheng struct btrfs_trans_handle *trans; 4300c146afadSYan Zheng 43010b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 43022ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 43030b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 43040b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4305c71bf099SYan, Zheng 4306c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 43070b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 43080b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4309c71bf099SYan, Zheng 43107a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 43113612b495STsutomu Itoh if (IS_ERR(trans)) 43123612b495STsutomu Itoh return PTR_ERR(trans); 43133a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4314c146afadSYan Zheng } 4315c146afadSYan Zheng 4316b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4317eb60ceacSChris Mason { 4318c146afadSYan Zheng int ret; 4319e089f05cSChris Mason 4320afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 4321d6fd0ae2SOmar Sandoval /* 4322d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4323d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4324d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4325d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4326d6fd0ae2SOmar Sandoval */ 4327d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4328a2135011SChris Mason 43297343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4330d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 43317343dd61SJustin Maggard 4332803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4333803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4334803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4335803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4336803b2f54SStefan Behrens 4337837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4338aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4339837d5b6eSIlya Dryomov 43408dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 43418dabb742SStefan Behrens 4342aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 43434cb5300bSChris Mason 43444cb5300bSChris Mason /* wait for any defraggers to finish */ 43454cb5300bSChris Mason wait_event(fs_info->transaction_wait, 43464cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 43474cb5300bSChris Mason 43484cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 434926176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 43504cb5300bSChris Mason 435121c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 435257056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4353576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 435421c7e756SMiao Xie 435518bb8bbfSJohannes Thumshirn cancel_work_sync(&fs_info->reclaim_bgs_work); 435618bb8bbfSJohannes Thumshirn 4357b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4358b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4359b0643e59SDennis Zhou 4360bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4361e44163e1SJeff Mahoney /* 4362d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4363d6fd0ae2SOmar Sandoval * unused block groups. 4364e44163e1SJeff Mahoney */ 43650b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4366e44163e1SJeff Mahoney 4367f0cc2cd7SFilipe Manana /* 4368f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4369f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4370f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4371f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4372f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4373f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4374f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4375f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4376f0cc2cd7SFilipe Manana * This is a very rare case. 4377f0cc2cd7SFilipe Manana */ 4378f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4379f0cc2cd7SFilipe Manana 43806bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4381d397712bSChris Mason if (ret) 438204892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4383c146afadSYan Zheng } 4384ed2ff2cbSChris Mason 4385af722733SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) || 4386af722733SLiu Bo test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) 43872ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4388acce952bSliubo 4389e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4390e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 43918929ecfaSYan, Zheng 4392e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4393afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4394f25784b3SYan Zheng 43955958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 43965958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 43975958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 43985958253cSQu Wenruo } 43995958253cSQu Wenruo 440004892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4401fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4402bcef60f2SArne Jansen 4403963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 440404892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4405963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4406b0c68f8bSChris Mason } 440731153d81SYan Zheng 44085deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 44094297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 44105deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 44114297ff84SJosef Bacik 44126618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4413b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 44145ac1d209SJeff Mahoney 44151a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 44161a4319ccSLiu Bo 4417de348ee0SWang Shilong /* 4418de348ee0SWang Shilong * we must make sure there is not any read request to 4419de348ee0SWang Shilong * submit after we stopping all workers. 4420de348ee0SWang Shilong */ 4421de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 442296192499SJosef Bacik btrfs_stop_all_workers(fs_info); 442396192499SJosef Bacik 44240a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 44250a31daa4SFilipe Manana ASSERT(list_empty(&fs_info->trans_list)); 44260a31daa4SFilipe Manana 4427afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 44284273eaffSAnand Jain free_root_pointers(fs_info, true); 44298c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 44309ad6b7bcSChris Mason 44314e19443dSJosef Bacik /* 44324e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 44334e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 44344e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 44354e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 44364e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 44374e19443dSJosef Bacik */ 44384e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 44394e19443dSJosef Bacik 444013e6c37bSJosef Bacik iput(fs_info->btree_inode); 4441d6bfde87SChris Mason 444221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 44430b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 44442ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 444521adbd5cSStefan Behrens #endif 444621adbd5cSStefan Behrens 44470b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 444868c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4449eb60ceacSChris Mason } 4450eb60ceacSChris Mason 4451b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4452b9fab919SChris Mason int atomic) 4453ccd467d6SChris Mason { 44541259ab75SChris Mason int ret; 4455727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 44561259ab75SChris Mason 44570b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 44581259ab75SChris Mason if (!ret) 44591259ab75SChris Mason return ret; 44601259ab75SChris Mason 44611259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4462b9fab919SChris Mason parent_transid, atomic); 4463b9fab919SChris Mason if (ret == -EAGAIN) 4464b9fab919SChris Mason return ret; 44651259ab75SChris Mason return !ret; 44665f39d397SChris Mason } 44676702ed49SChris Mason 44685f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 44695f39d397SChris Mason { 44702f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 44715f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4472b9473439SChris Mason int was_dirty; 4473b4ce94deSChris Mason 447406ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 447506ea65a3SJosef Bacik /* 447606ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 447752042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 447806ea65a3SJosef Bacik * outside of the sanity tests. 447906ea65a3SJosef Bacik */ 4480b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 448106ea65a3SJosef Bacik return; 448206ea65a3SJosef Bacik #endif 4483b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 44840b246afaSJeff Mahoney if (transid != fs_info->generation) 44855d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 44860b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 44870b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4488e2d84521SMiao Xie if (!was_dirty) 4489104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4490e2d84521SMiao Xie buf->len, 44910b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 44921f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 449369fc6cbbSQu Wenruo /* 449469fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 449569fc6cbbSQu Wenruo * but item data not updated. 449669fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 449769fc6cbbSQu Wenruo */ 449869fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4499cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4500a4f78750SDavid Sterba btrfs_print_leaf(buf); 45011f21ef0aSFilipe Manana ASSERT(0); 45021f21ef0aSFilipe Manana } 45031f21ef0aSFilipe Manana #endif 4504eb60ceacSChris Mason } 4505eb60ceacSChris Mason 45062ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4507b53d3f5dSLiu Bo int flush_delayed) 450835b7e476SChris Mason { 4509188de649SChris Mason /* 4510188de649SChris Mason * looks as though older kernels can get into trouble with 4511188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4512188de649SChris Mason */ 4513e2d84521SMiao Xie int ret; 4514d6bfde87SChris Mason 45156933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4516d6bfde87SChris Mason return; 4517d6bfde87SChris Mason 4518b53d3f5dSLiu Bo if (flush_delayed) 45192ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 452016cdcec7SMiao Xie 4521d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4522d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4523d814a491SEthan Lien fs_info->dirty_metadata_batch); 4524e2d84521SMiao Xie if (ret > 0) { 45250b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 452616cdcec7SMiao Xie } 452716cdcec7SMiao Xie } 452816cdcec7SMiao Xie 45292ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 453016cdcec7SMiao Xie { 45312ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 453235b7e476SChris Mason } 4533b53d3f5dSLiu Bo 45342ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4535b53d3f5dSLiu Bo { 45362ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4537d6bfde87SChris Mason } 45386b80053dSChris Mason 4539581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 4540581c1760SQu Wenruo struct btrfs_key *first_key) 45416b80053dSChris Mason { 45425ab12d1fSDavid Sterba return btree_read_extent_buffer_pages(buf, parent_transid, 4543581c1760SQu Wenruo level, first_key); 45446b80053dSChris Mason } 45450da5468fSChris Mason 45462ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4547acce952bSliubo { 4548fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4549fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4550fe816d0fSNikolay Borisov 45510b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 45522ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 45530b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4554acce952bSliubo 45550b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45560b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4557acce952bSliubo } 4558acce952bSliubo 4559ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4560ef67963dSJosef Bacik { 4561ef67963dSJosef Bacik struct btrfs_root *gang[8]; 4562ef67963dSJosef Bacik u64 root_objectid = 0; 4563ef67963dSJosef Bacik int ret; 4564ef67963dSJosef Bacik 4565ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4566ef67963dSJosef Bacik while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4567ef67963dSJosef Bacik (void **)gang, root_objectid, 4568ef67963dSJosef Bacik ARRAY_SIZE(gang))) != 0) { 4569ef67963dSJosef Bacik int i; 4570ef67963dSJosef Bacik 4571ef67963dSJosef Bacik for (i = 0; i < ret; i++) 4572ef67963dSJosef Bacik gang[i] = btrfs_grab_root(gang[i]); 4573ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4574ef67963dSJosef Bacik 4575ef67963dSJosef Bacik for (i = 0; i < ret; i++) { 4576ef67963dSJosef Bacik if (!gang[i]) 4577ef67963dSJosef Bacik continue; 4578ef67963dSJosef Bacik root_objectid = gang[i]->root_key.objectid; 4579ef67963dSJosef Bacik btrfs_free_log(NULL, gang[i]); 4580ef67963dSJosef Bacik btrfs_put_root(gang[i]); 4581ef67963dSJosef Bacik } 4582ef67963dSJosef Bacik root_objectid++; 4583ef67963dSJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 4584ef67963dSJosef Bacik } 4585ef67963dSJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 4586ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4587ef67963dSJosef Bacik } 4588ef67963dSJosef Bacik 4589143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4590acce952bSliubo { 4591acce952bSliubo struct btrfs_ordered_extent *ordered; 4592acce952bSliubo 4593199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4594779880efSJosef Bacik /* 4595779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4596779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4597779880efSJosef Bacik */ 4598199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4599779880efSJosef Bacik root_extent_list) 4600779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4601199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4602199c2a9cSMiao Xie } 4603199c2a9cSMiao Xie 4604199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4605199c2a9cSMiao Xie { 4606199c2a9cSMiao Xie struct btrfs_root *root; 4607199c2a9cSMiao Xie struct list_head splice; 4608199c2a9cSMiao Xie 4609199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4610199c2a9cSMiao Xie 4611199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4612199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4613199c2a9cSMiao Xie while (!list_empty(&splice)) { 4614199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4615199c2a9cSMiao Xie ordered_root); 46161de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 46171de2cfdeSJosef Bacik &fs_info->ordered_roots); 4618199c2a9cSMiao Xie 46192a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4620199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4621199c2a9cSMiao Xie 46222a85d9caSLiu Bo cond_resched(); 46232a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4624199c2a9cSMiao Xie } 4625199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 462674d5d229SJosef Bacik 462774d5d229SJosef Bacik /* 462874d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 462974d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 463074d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 463174d5d229SJosef Bacik * actually get run and error out properly. 463274d5d229SJosef Bacik */ 463374d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4634acce952bSliubo } 4635acce952bSliubo 463635a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 46372ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4638acce952bSliubo { 4639acce952bSliubo struct rb_node *node; 4640acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4641acce952bSliubo struct btrfs_delayed_ref_node *ref; 4642acce952bSliubo int ret = 0; 4643acce952bSliubo 4644acce952bSliubo delayed_refs = &trans->delayed_refs; 4645acce952bSliubo 4646acce952bSliubo spin_lock(&delayed_refs->lock); 4647d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4648cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4649b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4650acce952bSliubo return ret; 4651acce952bSliubo } 4652acce952bSliubo 46535c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4654d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 46550e0adbcfSJosef Bacik struct rb_node *n; 4656e78417d1SJosef Bacik bool pin_bytes = false; 4657acce952bSliubo 4658d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4659d7df2c79SJosef Bacik href_node); 46603069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4661b939d1abSJosef Bacik continue; 46623069bd26SJosef Bacik 4663d7df2c79SJosef Bacik spin_lock(&head->lock); 4664e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 46650e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 46660e0adbcfSJosef Bacik ref_node); 4667d7df2c79SJosef Bacik ref->in_tree = 0; 4668e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 46690e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 46701d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 46711d57ee94SWang Xiaoguang list_del(&ref->add_list); 4672d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4673d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4674d7df2c79SJosef Bacik } 467554067ae9SJosef Bacik if (head->must_insert_reserved) 4676e78417d1SJosef Bacik pin_bytes = true; 467778a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4678fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 4679d7df2c79SJosef Bacik spin_unlock(&head->lock); 4680acce952bSliubo spin_unlock(&delayed_refs->lock); 4681e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4682acce952bSliubo 4683f603bb94SNikolay Borisov if (pin_bytes) { 4684f603bb94SNikolay Borisov struct btrfs_block_group *cache; 4685f603bb94SNikolay Borisov 4686f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 4687f603bb94SNikolay Borisov BUG_ON(!cache); 4688f603bb94SNikolay Borisov 4689f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 4690f603bb94SNikolay Borisov spin_lock(&cache->lock); 4691f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 4692f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 4693f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 4694f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 4695f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 4696f603bb94SNikolay Borisov spin_unlock(&cache->lock); 4697f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 4698f603bb94SNikolay Borisov percpu_counter_add_batch( 4699f603bb94SNikolay Borisov &cache->space_info->total_bytes_pinned, 4700f603bb94SNikolay Borisov head->num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); 4701f603bb94SNikolay Borisov 4702f603bb94SNikolay Borisov btrfs_put_block_group(cache); 4703f603bb94SNikolay Borisov 4704f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 4705f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 4706f603bb94SNikolay Borisov } 470731890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 4708d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4709acce952bSliubo cond_resched(); 4710acce952bSliubo spin_lock(&delayed_refs->lock); 4711acce952bSliubo } 471281f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 4713acce952bSliubo 4714acce952bSliubo spin_unlock(&delayed_refs->lock); 4715acce952bSliubo 4716acce952bSliubo return ret; 4717acce952bSliubo } 4718acce952bSliubo 4719143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4720acce952bSliubo { 4721acce952bSliubo struct btrfs_inode *btrfs_inode; 4722acce952bSliubo struct list_head splice; 4723acce952bSliubo 4724acce952bSliubo INIT_LIST_HEAD(&splice); 4725acce952bSliubo 4726eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4727eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4728acce952bSliubo 4729acce952bSliubo while (!list_empty(&splice)) { 4730fe816d0fSNikolay Borisov struct inode *inode = NULL; 4731eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4732acce952bSliubo delalloc_inodes); 4733fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 4734eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4735acce952bSliubo 4736fe816d0fSNikolay Borisov /* 4737fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 4738fe816d0fSNikolay Borisov * meanwhile. 4739fe816d0fSNikolay Borisov */ 4740fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 4741fe816d0fSNikolay Borisov if (inode) { 4742fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 4743fe816d0fSNikolay Borisov iput(inode); 4744fe816d0fSNikolay Borisov } 4745eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4746acce952bSliubo } 4747eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4748eb73c1b7SMiao Xie } 4749eb73c1b7SMiao Xie 4750eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4751eb73c1b7SMiao Xie { 4752eb73c1b7SMiao Xie struct btrfs_root *root; 4753eb73c1b7SMiao Xie struct list_head splice; 4754eb73c1b7SMiao Xie 4755eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4756eb73c1b7SMiao Xie 4757eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4758eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4759eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4760eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4761eb73c1b7SMiao Xie delalloc_root); 476200246528SJosef Bacik root = btrfs_grab_root(root); 4763eb73c1b7SMiao Xie BUG_ON(!root); 4764eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4765eb73c1b7SMiao Xie 4766eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 476700246528SJosef Bacik btrfs_put_root(root); 4768eb73c1b7SMiao Xie 4769eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4770eb73c1b7SMiao Xie } 4771eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4772acce952bSliubo } 4773acce952bSliubo 47742ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4775acce952bSliubo struct extent_io_tree *dirty_pages, 4776acce952bSliubo int mark) 4777acce952bSliubo { 4778acce952bSliubo int ret; 4779acce952bSliubo struct extent_buffer *eb; 4780acce952bSliubo u64 start = 0; 4781acce952bSliubo u64 end; 4782acce952bSliubo 4783acce952bSliubo while (1) { 4784acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4785e6138876SJosef Bacik mark, NULL); 4786acce952bSliubo if (ret) 4787acce952bSliubo break; 4788acce952bSliubo 478991166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4790acce952bSliubo while (start <= end) { 47910b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 47920b246afaSJeff Mahoney start += fs_info->nodesize; 4793fd8b2b61SJosef Bacik if (!eb) 4794acce952bSliubo continue; 4795fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4796acce952bSliubo 4797fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4798fd8b2b61SJosef Bacik &eb->bflags)) 4799fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4800fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4801acce952bSliubo } 4802acce952bSliubo } 4803acce952bSliubo 4804acce952bSliubo return ret; 4805acce952bSliubo } 4806acce952bSliubo 48072ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4808fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 4809acce952bSliubo { 4810acce952bSliubo u64 start; 4811acce952bSliubo u64 end; 4812acce952bSliubo int ret; 4813acce952bSliubo 4814acce952bSliubo while (1) { 48150e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 48160e6ec385SFilipe Manana 4817fcd5e742SLu Fengqi /* 4818fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 4819fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 4820fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 4821fcd5e742SLu Fengqi * the same extent range. 4822fcd5e742SLu Fengqi */ 4823fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 4824acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 48250e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 4826fcd5e742SLu Fengqi if (ret) { 4827fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4828acce952bSliubo break; 4829fcd5e742SLu Fengqi } 4830acce952bSliubo 48310e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 48320e6ec385SFilipe Manana free_extent_state(cached_state); 48332ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4834fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 4835acce952bSliubo cond_resched(); 4836acce952bSliubo } 4837acce952bSliubo 4838acce952bSliubo return 0; 4839acce952bSliubo } 4840acce952bSliubo 484132da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 4842c79a1751SLiu Bo { 4843c79a1751SLiu Bo struct inode *inode; 4844c79a1751SLiu Bo 4845c79a1751SLiu Bo inode = cache->io_ctl.inode; 4846c79a1751SLiu Bo if (inode) { 4847c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4848c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4849c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4850c79a1751SLiu Bo iput(inode); 4851c79a1751SLiu Bo } 4852bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 4853c79a1751SLiu Bo btrfs_put_block_group(cache); 4854c79a1751SLiu Bo } 4855c79a1751SLiu Bo 4856c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 48572ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4858c79a1751SLiu Bo { 485932da5386SDavid Sterba struct btrfs_block_group *cache; 4860c79a1751SLiu Bo 4861c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4862c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4863c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 486432da5386SDavid Sterba struct btrfs_block_group, 4865c79a1751SLiu Bo dirty_list); 4866c79a1751SLiu Bo 4867c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4868c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4869c79a1751SLiu Bo list_del_init(&cache->io_list); 4870c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4871c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4872c79a1751SLiu Bo } 4873c79a1751SLiu Bo 4874c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4875c79a1751SLiu Bo spin_lock(&cache->lock); 4876c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4877c79a1751SLiu Bo spin_unlock(&cache->lock); 4878c79a1751SLiu Bo 4879c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4880c79a1751SLiu Bo btrfs_put_block_group(cache); 4881ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 4882c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4883c79a1751SLiu Bo } 4884c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4885c79a1751SLiu Bo 488645ae2c18SNikolay Borisov /* 488745ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 488845ae2c18SNikolay Borisov * to use it without any locking 488945ae2c18SNikolay Borisov */ 4890c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4891c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 489232da5386SDavid Sterba struct btrfs_block_group, 4893c79a1751SLiu Bo io_list); 4894c79a1751SLiu Bo 4895c79a1751SLiu Bo list_del_init(&cache->io_list); 4896c79a1751SLiu Bo spin_lock(&cache->lock); 4897c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4898c79a1751SLiu Bo spin_unlock(&cache->lock); 4899c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4900c79a1751SLiu Bo } 4901c79a1751SLiu Bo } 4902c79a1751SLiu Bo 490349b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 49042ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 490549b25e05SJeff Mahoney { 4906bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 4907bbbf7243SNikolay Borisov 49082ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4909c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4910c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4911c79a1751SLiu Bo 4912bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 4913bbbf7243SNikolay Borisov post_commit_list) { 4914bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 4915bbbf7243SNikolay Borisov } 4916bbbf7243SNikolay Borisov 49172ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 491849b25e05SJeff Mahoney 49194a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 49200b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 492149b25e05SJeff Mahoney 49224a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 49230b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 492449b25e05SJeff Mahoney 4925ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 492667cde344SMiao Xie 49272ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 492849b25e05SJeff Mahoney EXTENT_DIRTY); 4929fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 493049b25e05SJeff Mahoney 4931d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 4932d3575156SNaohiro Aota 49334a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 49344a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 493549b25e05SJeff Mahoney } 493649b25e05SJeff Mahoney 49372ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4938acce952bSliubo { 4939acce952bSliubo struct btrfs_transaction *t; 4940acce952bSliubo 49410b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4942acce952bSliubo 49430b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 49440b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 49450b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4946724e2315SJosef Bacik struct btrfs_transaction, list); 4947724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 49489b64f57dSElena Reshetova refcount_inc(&t->use_count); 49490b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 49502ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4951724e2315SJosef Bacik btrfs_put_transaction(t); 49520b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4953724e2315SJosef Bacik continue; 4954724e2315SJosef Bacik } 49550b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4956724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 49570b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4958724e2315SJosef Bacik /* 4959724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4960724e2315SJosef Bacik * handle open currently for this transaction. 4961724e2315SJosef Bacik */ 4962724e2315SJosef Bacik wait_event(t->writer_wait, 4963724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4964724e2315SJosef Bacik } else { 49650b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4966724e2315SJosef Bacik } 49672ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4968724e2315SJosef Bacik 49690b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 49700b246afaSJeff Mahoney if (t == fs_info->running_transaction) 49710b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4972724e2315SJosef Bacik list_del_init(&t->list); 49730b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4974a4abeea4SJosef Bacik 4975724e2315SJosef Bacik btrfs_put_transaction(t); 49762ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 49770b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4978724e2315SJosef Bacik } 49790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 49800b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4981ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4982ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 49830b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 4984ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 49850b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4986acce952bSliubo 4987acce952bSliubo return 0; 4988acce952bSliubo } 4989ec7d6dfdSNikolay Borisov 4990453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 4991ec7d6dfdSNikolay Borisov { 4992ec7d6dfdSNikolay Borisov struct btrfs_path *path; 4993ec7d6dfdSNikolay Borisov int ret; 4994ec7d6dfdSNikolay Borisov struct extent_buffer *l; 4995ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 4996ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 4997ec7d6dfdSNikolay Borisov int slot; 4998ec7d6dfdSNikolay Borisov 4999ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5000ec7d6dfdSNikolay Borisov if (!path) 5001ec7d6dfdSNikolay Borisov return -ENOMEM; 5002ec7d6dfdSNikolay Borisov 5003ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5004ec7d6dfdSNikolay Borisov search_key.type = -1; 5005ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5006ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5007ec7d6dfdSNikolay Borisov if (ret < 0) 5008ec7d6dfdSNikolay Borisov goto error; 5009ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5010ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5011ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5012ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5013ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 501423125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 501523125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5016ec7d6dfdSNikolay Borisov } else { 501723125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5018ec7d6dfdSNikolay Borisov } 5019ec7d6dfdSNikolay Borisov ret = 0; 5020ec7d6dfdSNikolay Borisov error: 5021ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5022ec7d6dfdSNikolay Borisov return ret; 5023ec7d6dfdSNikolay Borisov } 5024ec7d6dfdSNikolay Borisov 5025543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5026ec7d6dfdSNikolay Borisov { 5027ec7d6dfdSNikolay Borisov int ret; 5028ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5029ec7d6dfdSNikolay Borisov 50306b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5031ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5032ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5033ec7d6dfdSNikolay Borisov root->root_key.objectid); 5034ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5035ec7d6dfdSNikolay Borisov goto out; 5036ec7d6dfdSNikolay Borisov } 5037ec7d6dfdSNikolay Borisov 503823125104SNikolay Borisov *objectid = root->free_objectid++; 5039ec7d6dfdSNikolay Borisov ret = 0; 5040ec7d6dfdSNikolay Borisov out: 5041ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5042ec7d6dfdSNikolay Borisov return ret; 5043ec7d6dfdSNikolay Borisov } 5044