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> 8fc7cbcd4SDavid Sterba #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" 26103c1972SChristoph Hellwig #include "bio.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" 46c7f13d42SJosef Bacik #include "fs.h" 4707e81dc9SJosef Bacik #include "accessors.h" 48a0231804SJosef Bacik #include "extent-tree.h" 4945c40c8fSJosef Bacik #include "root-tree.h" 5059b818e0SJosef Bacik #include "defrag.h" 51c7a03b52SJosef Bacik #include "uuid-tree.h" 5267707479SJosef Bacik #include "relocation.h" 532fc6822cSJosef Bacik #include "scrub.h" 54c03b2207SJosef Bacik #include "super.h" 55*deb6216fSChristoph Hellwig #include "file-item.h" 56eb60ceacSChris Mason 57319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 58319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 59319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 60319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 61e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 62e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 63319e4d06SQu Wenruo 64143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 65acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 662ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 67143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 682ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 69acce952bSliubo struct extent_io_tree *dirty_pages, 70acce952bSliubo int mark); 712ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 72acce952bSliubo struct extent_io_tree *pinned_extents); 732ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 742ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 75ce9adaa5SChris Mason 76141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 77141386e1SJosef Bacik { 78141386e1SJosef Bacik if (fs_info->csum_shash) 79141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 80141386e1SJosef Bacik } 81141386e1SJosef Bacik 82d352ac68SChris Mason /* 83d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 84d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 85d352ac68SChris Mason * just before they are sent down the IO stack. 86d352ac68SChris Mason */ 8744b8bd7eSChris Mason struct async_submit_bio { 88da67daabSDavid Sterba struct btrfs_inode *inode; 8944b8bd7eSChris Mason struct bio *bio; 90ab2072b2SDavid Sterba enum btrfs_wq_submit_cmd submit_cmd; 9144b8bd7eSChris Mason int mirror_num; 921941b64bSQu Wenruo 93ab2072b2SDavid Sterba /* Optional parameter for used by direct io */ 941941b64bSQu Wenruo u64 dio_file_offset; 958b712842SChris Mason struct btrfs_work work; 964e4cbee9SChristoph Hellwig blk_status_t status; 9744b8bd7eSChris Mason }; 9844b8bd7eSChris Mason 9985d4e461SChris Mason /* 1002996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 101d352ac68SChris Mason */ 102c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 10319c00ddcSChris Mason { 104d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 1057280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 106a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 107d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 10819c00ddcSChris Mason char *kaddr; 109e9be5a30SDavid Sterba int i; 110d5178578SJohannes Thumshirn 111d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 112d5178578SJohannes Thumshirn crypto_shash_init(shash); 113a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 114e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 115a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 11619c00ddcSChris Mason 117e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 118e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 119e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 12019c00ddcSChris Mason } 12171a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 122d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 12319c00ddcSChris Mason } 12419c00ddcSChris Mason 125d352ac68SChris Mason /* 126d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 127d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 128d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 129d352ac68SChris Mason * in the wrong place. 130d352ac68SChris Mason */ 1311259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 132b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 133b9fab919SChris Mason int atomic) 1341259ab75SChris Mason { 1352ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 1361259ab75SChris Mason int ret; 1371259ab75SChris Mason 1381259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 1391259ab75SChris Mason return 0; 1401259ab75SChris Mason 141b9fab919SChris Mason if (atomic) 142b9fab919SChris Mason return -EAGAIN; 143b9fab919SChris Mason 144570eb97bSJosef Bacik lock_extent(io_tree, eb->start, eb->start + eb->len - 1, &cached_state); 1450b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 1461259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 1471259ab75SChris Mason ret = 0; 1481259ab75SChris Mason goto out; 1491259ab75SChris Mason } 15094647322SDavid Sterba btrfs_err_rl(eb->fs_info, 1518f0ed7d4SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu", 1528f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 15329549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 1541259ab75SChris Mason ret = 1; 1550b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 15633958dc6SChris Mason out: 157570eb97bSJosef Bacik unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, 158e43bbe5eSDavid Sterba &cached_state); 1591259ab75SChris Mason return ret; 1601259ab75SChris Mason } 1611259ab75SChris Mason 162e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 163e7e16f48SJohannes Thumshirn { 164e7e16f48SJohannes Thumshirn switch (csum_type) { 165e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 1663951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 1673831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 168352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 169e7e16f48SJohannes Thumshirn return true; 170e7e16f48SJohannes Thumshirn default: 171e7e16f48SJohannes Thumshirn return false; 172e7e16f48SJohannes Thumshirn } 173e7e16f48SJohannes Thumshirn } 174e7e16f48SJohannes Thumshirn 175d352ac68SChris Mason /* 1761104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 1771104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 1781104a885SDavid Sterba */ 1793d17adeaSQu Wenruo int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 1803d17adeaSQu Wenruo const struct btrfs_super_block *disk_sb) 1811104a885SDavid Sterba { 18251bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 183d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 184d5178578SJohannes Thumshirn 185d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 1861104a885SDavid Sterba 1871104a885SDavid Sterba /* 1881104a885SDavid Sterba * The super_block structure does not span the whole 18951bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 19051bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 1911104a885SDavid Sterba */ 1923d17adeaSQu Wenruo crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE, 193fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 1941104a885SDavid Sterba 19555fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 196e7e16f48SJohannes Thumshirn return 1; 1971104a885SDavid Sterba 198e7e16f48SJohannes Thumshirn return 0; 1991104a885SDavid Sterba } 2001104a885SDavid Sterba 201e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 202ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 203581c1760SQu Wenruo { 204e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 205581c1760SQu Wenruo int found_level; 206581c1760SQu Wenruo struct btrfs_key found_key; 207581c1760SQu Wenruo int ret; 208581c1760SQu Wenruo 209581c1760SQu Wenruo found_level = btrfs_header_level(eb); 210581c1760SQu Wenruo if (found_level != level) { 21163489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 21263489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 213581c1760SQu Wenruo btrfs_err(fs_info, 214581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 215581c1760SQu Wenruo eb->start, level, found_level); 216581c1760SQu Wenruo return -EIO; 217581c1760SQu Wenruo } 218581c1760SQu Wenruo 219581c1760SQu Wenruo if (!first_key) 220581c1760SQu Wenruo return 0; 221581c1760SQu Wenruo 2225d41be6fSQu Wenruo /* 2235d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 2245d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 2255d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 2265d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 2275d41be6fSQu Wenruo */ 2285d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 2295d41be6fSQu Wenruo return 0; 23062fdaa52SQu Wenruo 23162fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 23262fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 23362fdaa52SQu Wenruo btrfs_err(fs_info, 23462fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 23562fdaa52SQu Wenruo eb->start); 23662fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 23762fdaa52SQu Wenruo return -EUCLEAN; 23862fdaa52SQu Wenruo } 23962fdaa52SQu Wenruo 240581c1760SQu Wenruo if (found_level) 241581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 242581c1760SQu Wenruo else 243581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 244581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 245581c1760SQu Wenruo 246581c1760SQu Wenruo if (ret) { 24763489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 24863489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 249581c1760SQu Wenruo btrfs_err(fs_info, 250ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 251ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 252ff76a864SLiu Bo first_key->type, first_key->offset, 253ff76a864SLiu Bo found_key.objectid, found_key.type, 254ff76a864SLiu Bo found_key.offset); 255581c1760SQu Wenruo } 256581c1760SQu Wenruo return ret; 257581c1760SQu Wenruo } 258581c1760SQu Wenruo 259bacf60e5SChristoph Hellwig static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, 260bacf60e5SChristoph Hellwig int mirror_num) 261bacf60e5SChristoph Hellwig { 262bacf60e5SChristoph Hellwig struct btrfs_fs_info *fs_info = eb->fs_info; 263bacf60e5SChristoph Hellwig u64 start = eb->start; 264bacf60e5SChristoph Hellwig int i, num_pages = num_extent_pages(eb); 265bacf60e5SChristoph Hellwig int ret = 0; 266bacf60e5SChristoph Hellwig 267bacf60e5SChristoph Hellwig if (sb_rdonly(fs_info->sb)) 268bacf60e5SChristoph Hellwig return -EROFS; 269bacf60e5SChristoph Hellwig 270bacf60e5SChristoph Hellwig for (i = 0; i < num_pages; i++) { 271bacf60e5SChristoph Hellwig struct page *p = eb->pages[i]; 272bacf60e5SChristoph Hellwig 273bacf60e5SChristoph Hellwig ret = btrfs_repair_io_failure(fs_info, 0, start, PAGE_SIZE, 274bacf60e5SChristoph Hellwig start, p, start - page_offset(p), mirror_num); 275bacf60e5SChristoph Hellwig if (ret) 276bacf60e5SChristoph Hellwig break; 277bacf60e5SChristoph Hellwig start += PAGE_SIZE; 278bacf60e5SChristoph Hellwig } 279bacf60e5SChristoph Hellwig 280bacf60e5SChristoph Hellwig return ret; 281bacf60e5SChristoph Hellwig } 282bacf60e5SChristoph Hellwig 2831104a885SDavid Sterba /* 284d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 285d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 286581c1760SQu Wenruo * 287789d6a3aSQu Wenruo * @check: expected tree parentness check, see the comments of the 288789d6a3aSQu Wenruo * structure for details. 289d352ac68SChris Mason */ 2906a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb, 291789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check) 292f188591eSChris Mason { 2935ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 294ea466794SJosef Bacik int failed = 0; 295f188591eSChris Mason int ret; 296f188591eSChris Mason int num_copies = 0; 297f188591eSChris Mason int mirror_num = 0; 298ea466794SJosef Bacik int failed_mirror = 0; 299f188591eSChris Mason 300789d6a3aSQu Wenruo ASSERT(check); 301789d6a3aSQu Wenruo 302f188591eSChris Mason while (1) { 303f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 304947a6299SQu Wenruo ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check); 305947a6299SQu Wenruo if (!ret) 306581c1760SQu Wenruo break; 307d397712bSChris Mason 3080b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 309f188591eSChris Mason eb->start, eb->len); 3104235298eSChris Mason if (num_copies == 1) 311ea466794SJosef Bacik break; 3124235298eSChris Mason 3135cf1ab56SJosef Bacik if (!failed_mirror) { 3145cf1ab56SJosef Bacik failed = 1; 3155cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 3165cf1ab56SJosef Bacik } 3175cf1ab56SJosef Bacik 318f188591eSChris Mason mirror_num++; 319ea466794SJosef Bacik if (mirror_num == failed_mirror) 320ea466794SJosef Bacik mirror_num++; 321ea466794SJosef Bacik 3224235298eSChris Mason if (mirror_num > num_copies) 323ea466794SJosef Bacik break; 324f188591eSChris Mason } 325ea466794SJosef Bacik 326c0901581SStefan Behrens if (failed && !ret && failed_mirror) 32720a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 328ea466794SJosef Bacik 329ea466794SJosef Bacik return ret; 330f188591eSChris Mason } 33119c00ddcSChris Mason 332eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 333eca0f6f6SQu Wenruo { 334eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 335eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 336eca0f6f6SQu Wenruo int ret; 337eca0f6f6SQu Wenruo 338eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 339eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 340eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 341eca0f6f6SQu Wenruo csum_tree_block(eb, result); 342eca0f6f6SQu Wenruo 343eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 344eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 345eca0f6f6SQu Wenruo else 346eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 347eca0f6f6SQu Wenruo 3483777369fSQu Wenruo if (ret < 0) 3493777369fSQu Wenruo goto error; 3503777369fSQu Wenruo 3513777369fSQu Wenruo /* 3523777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than 3533777369fSQu Wenruo * last committed. Or something seriously wrong happened. 3543777369fSQu Wenruo */ 3553777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { 3563777369fSQu Wenruo ret = -EUCLEAN; 357eca0f6f6SQu Wenruo btrfs_err(fs_info, 3583777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu", 3593777369fSQu Wenruo eb->start, btrfs_header_generation(eb), 3603777369fSQu Wenruo fs_info->last_trans_committed); 3613777369fSQu Wenruo goto error; 362eca0f6f6SQu Wenruo } 363eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 364eca0f6f6SQu Wenruo 365eca0f6f6SQu Wenruo return 0; 3663777369fSQu Wenruo 3673777369fSQu Wenruo error: 3683777369fSQu Wenruo btrfs_print_tree(eb, 0); 3693777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected", 3703777369fSQu Wenruo eb->start); 37116199ad9SFilipe Manana /* 37216199ad9SFilipe Manana * Be noisy if this is an extent buffer from a log tree. We don't abort 37316199ad9SFilipe Manana * a transaction in case there's a bad log tree extent buffer, we just 37416199ad9SFilipe Manana * fallback to a transaction commit. Still we want to know when there is 37516199ad9SFilipe Manana * a bad log tree extent buffer, as that may signal a bug somewhere. 37616199ad9SFilipe Manana */ 37716199ad9SFilipe Manana WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) || 37816199ad9SFilipe Manana btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID); 3793777369fSQu Wenruo return ret; 380eca0f6f6SQu Wenruo } 381eca0f6f6SQu Wenruo 382eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 383eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 384eca0f6f6SQu Wenruo struct bio_vec *bvec) 385eca0f6f6SQu Wenruo { 386eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 387eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 388eca0f6f6SQu Wenruo u64 cur; 389eca0f6f6SQu Wenruo int ret = 0; 390eca0f6f6SQu Wenruo 391eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 392eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 393eca0f6f6SQu Wenruo struct extent_buffer *eb; 394eca0f6f6SQu Wenruo bool uptodate; 395eca0f6f6SQu Wenruo 396eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 397eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 398eca0f6f6SQu Wenruo fs_info->nodesize); 399eca0f6f6SQu Wenruo 40001cd3909SDavid Sterba /* A dirty eb shouldn't disappear from buffer_radix */ 401eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 402eca0f6f6SQu Wenruo return -EUCLEAN; 403eca0f6f6SQu Wenruo 404eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 405eca0f6f6SQu Wenruo free_extent_buffer(eb); 406eca0f6f6SQu Wenruo return -EUCLEAN; 407eca0f6f6SQu Wenruo } 408eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 409eca0f6f6SQu Wenruo free_extent_buffer(eb); 410eca0f6f6SQu Wenruo return -EUCLEAN; 411eca0f6f6SQu Wenruo } 412eca0f6f6SQu Wenruo 413eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 414eca0f6f6SQu Wenruo free_extent_buffer(eb); 415eca0f6f6SQu Wenruo if (ret < 0) 416eca0f6f6SQu Wenruo return ret; 417eca0f6f6SQu Wenruo } 418eca0f6f6SQu Wenruo return ret; 419eca0f6f6SQu Wenruo } 420eca0f6f6SQu Wenruo 421d352ac68SChris Mason /* 422ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 423ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 424ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 425d352ac68SChris Mason */ 426ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 42719c00ddcSChris Mason { 428ac303b69SQu Wenruo struct page *page = bvec->bv_page; 4294eee4fa4SMiao Xie u64 start = page_offset(page); 43019c00ddcSChris Mason u64 found_start; 43119c00ddcSChris Mason struct extent_buffer *eb; 432eca0f6f6SQu Wenruo 433fbca46ebSQu Wenruo if (fs_info->nodesize < PAGE_SIZE) 434eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 435f188591eSChris Mason 4364f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4374f2de97aSJosef Bacik if (page != eb->pages[0]) 4384f2de97aSJosef Bacik return 0; 4390f805531SAlex Lyakas 44019c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 441d3575156SNaohiro Aota 442d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 443d3575156SNaohiro Aota WARN_ON(found_start != 0); 444d3575156SNaohiro Aota return 0; 445d3575156SNaohiro Aota } 446d3575156SNaohiro Aota 4470f805531SAlex Lyakas /* 4480f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4490f805531SAlex Lyakas * It is useful to know what went wrong. 4500f805531SAlex Lyakas */ 4510f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4520f805531SAlex Lyakas return -EUCLEAN; 4530f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4540f805531SAlex Lyakas return -EUCLEAN; 4550f805531SAlex Lyakas 456eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 45719c00ddcSChris Mason } 45819c00ddcSChris Mason 459*deb6216fSChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 460*deb6216fSChristoph Hellwig { 461*deb6216fSChristoph Hellwig struct bio_vec *bvec; 462*deb6216fSChristoph Hellwig struct btrfs_root *root; 463*deb6216fSChristoph Hellwig struct bvec_iter_all iter_all; 464*deb6216fSChristoph Hellwig int ret = 0; 465*deb6216fSChristoph Hellwig 466*deb6216fSChristoph Hellwig ASSERT(!bio_flagged(bio, BIO_CLONED)); 467*deb6216fSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 468*deb6216fSChristoph Hellwig root = BTRFS_I(bvec->bv_page->mapping->host)->root; 469*deb6216fSChristoph Hellwig ret = csum_dirty_buffer(root->fs_info, bvec); 470*deb6216fSChristoph Hellwig if (ret) 471*deb6216fSChristoph Hellwig break; 472*deb6216fSChristoph Hellwig } 473*deb6216fSChristoph Hellwig 474*deb6216fSChristoph Hellwig return errno_to_blk_status(ret); 475*deb6216fSChristoph Hellwig } 476*deb6216fSChristoph Hellwig 477b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 4782b82032cSYan Zheng { 479b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 480944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 48144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 482944d3f9fSNikolay Borisov u8 *metadata_uuid; 4832b82032cSYan Zheng 4849a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 4859a8658e3SDavid Sterba BTRFS_FSID_SIZE); 4867239ff4bSNikolay Borisov /* 487944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 488944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 489944d3f9fSNikolay Borisov * ->fsid in this case is fine 4907239ff4bSNikolay Borisov */ 491944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 4927239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 4937239ff4bSNikolay Borisov else 4947239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 4957239ff4bSNikolay Borisov 496944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 497944d3f9fSNikolay Borisov return 0; 498944d3f9fSNikolay Borisov 499944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 500944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 501944d3f9fSNikolay Borisov return 0; 502944d3f9fSNikolay Borisov 503944d3f9fSNikolay Borisov return 1; 5042b82032cSYan Zheng } 5052b82032cSYan Zheng 50677bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 507947a6299SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb, 508947a6299SQu Wenruo struct btrfs_tree_parent_check *check) 509ce9adaa5SChris Mason { 51077bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 511ce9adaa5SChris Mason u64 found_start; 51277bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 51377bf40a2SQu Wenruo u8 found_level; 5142996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 515dfd29eedSDavid Sterba const u8 *header_csum; 51677bf40a2SQu Wenruo int ret = 0; 517ea466794SJosef Bacik 518947a6299SQu Wenruo ASSERT(check); 519947a6299SQu Wenruo 520ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 521727011e0SChris Mason if (found_start != eb->start) { 5228f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, 5238f0ed7d4SQu Wenruo "bad tree block start, mirror %u want %llu have %llu", 5248f0ed7d4SQu Wenruo eb->read_mirror, eb->start, found_start); 525f188591eSChris Mason ret = -EIO; 52677bf40a2SQu Wenruo goto out; 527ce9adaa5SChris Mason } 528b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 5298f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u", 5308f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 5311259ab75SChris Mason ret = -EIO; 53277bf40a2SQu Wenruo goto out; 5331259ab75SChris Mason } 534ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5351c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 5368f0ed7d4SQu Wenruo btrfs_err(fs_info, 5378f0ed7d4SQu Wenruo "bad tree block level, mirror %u level %d on logical %llu", 5388f0ed7d4SQu Wenruo eb->read_mirror, btrfs_header_level(eb), eb->start); 5391c24c3ceSJosef Bacik ret = -EIO; 54077bf40a2SQu Wenruo goto out; 5411c24c3ceSJosef Bacik } 542ce9adaa5SChris Mason 543c67b3892SDavid Sterba csum_tree_block(eb, result); 544dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 545dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 546a826d6dcSJosef Bacik 547dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 5482996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 5498f0ed7d4SQu Wenruo "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5508f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 551dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 55235be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 55335be8851SJohannes Thumshirn btrfs_header_level(eb)); 5542996e1f8SJohannes Thumshirn ret = -EUCLEAN; 55577bf40a2SQu Wenruo goto out; 5562996e1f8SJohannes Thumshirn } 5572996e1f8SJohannes Thumshirn 558947a6299SQu Wenruo if (found_level != check->level) { 55977177ed1SQu Wenruo btrfs_err(fs_info, 56077177ed1SQu Wenruo "level verify failed on logical %llu mirror %u wanted %u found %u", 56177177ed1SQu Wenruo eb->start, eb->read_mirror, check->level, found_level); 562947a6299SQu Wenruo ret = -EIO; 563947a6299SQu Wenruo goto out; 564947a6299SQu Wenruo } 565947a6299SQu Wenruo if (unlikely(check->transid && 566947a6299SQu Wenruo btrfs_header_generation(eb) != check->transid)) { 567947a6299SQu Wenruo btrfs_err_rl(eb->fs_info, 568947a6299SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu", 569947a6299SQu Wenruo eb->start, eb->read_mirror, check->transid, 570947a6299SQu Wenruo btrfs_header_generation(eb)); 571947a6299SQu Wenruo ret = -EIO; 572947a6299SQu Wenruo goto out; 573947a6299SQu Wenruo } 574947a6299SQu Wenruo if (check->has_first_key) { 575947a6299SQu Wenruo struct btrfs_key *expect_key = &check->first_key; 576947a6299SQu Wenruo struct btrfs_key found_key; 577947a6299SQu Wenruo 578947a6299SQu Wenruo if (found_level) 579947a6299SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 580947a6299SQu Wenruo else 581947a6299SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 582947a6299SQu Wenruo if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) { 583947a6299SQu Wenruo btrfs_err(fs_info, 584947a6299SQu Wenruo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 585947a6299SQu Wenruo eb->start, check->transid, 586947a6299SQu Wenruo expect_key->objectid, 587947a6299SQu Wenruo expect_key->type, expect_key->offset, 588947a6299SQu Wenruo found_key.objectid, found_key.type, 589947a6299SQu Wenruo found_key.offset); 590947a6299SQu Wenruo ret = -EUCLEAN; 591947a6299SQu Wenruo goto out; 592947a6299SQu Wenruo } 593947a6299SQu Wenruo } 594947a6299SQu Wenruo if (check->owner_root) { 595947a6299SQu Wenruo ret = btrfs_check_eb_owner(eb, check->owner_root); 596947a6299SQu Wenruo if (ret < 0) 597947a6299SQu Wenruo goto out; 598947a6299SQu Wenruo } 599947a6299SQu Wenruo 600a826d6dcSJosef Bacik /* 601a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 602a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 603a826d6dcSJosef Bacik * return -EIO. 604a826d6dcSJosef Bacik */ 6051c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 606a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 607a826d6dcSJosef Bacik ret = -EIO; 608a826d6dcSJosef Bacik } 609ce9adaa5SChris Mason 610813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 611053ab70fSLiu Bo ret = -EIO; 612053ab70fSLiu Bo 6130b32f4bbSJosef Bacik if (!ret) 6140b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 61575391f0dSQu Wenruo else 61675391f0dSQu Wenruo btrfs_err(fs_info, 6178f0ed7d4SQu Wenruo "read time tree block corruption detected on logical %llu mirror %u", 6188f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 61977bf40a2SQu Wenruo out: 62077bf40a2SQu Wenruo return ret; 62177bf40a2SQu Wenruo } 62277bf40a2SQu Wenruo 623371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 624947a6299SQu Wenruo int mirror, struct btrfs_tree_parent_check *check) 625371cdc07SQu Wenruo { 626371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 627371cdc07SQu Wenruo struct extent_buffer *eb; 628371cdc07SQu Wenruo bool reads_done; 629371cdc07SQu Wenruo int ret = 0; 630371cdc07SQu Wenruo 631947a6299SQu Wenruo ASSERT(check); 632947a6299SQu Wenruo 633371cdc07SQu Wenruo /* 634371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 635371cdc07SQu Wenruo * only get one eb for each endio hook. 636371cdc07SQu Wenruo */ 637371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 638371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 639371cdc07SQu Wenruo 640371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 641371cdc07SQu Wenruo /* 642371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 643371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 644371cdc07SQu Wenruo */ 645371cdc07SQu Wenruo ASSERT(eb); 646371cdc07SQu Wenruo 647371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 648371cdc07SQu Wenruo /* Subpage read must finish in page read */ 649371cdc07SQu Wenruo ASSERT(reads_done); 650371cdc07SQu Wenruo 651371cdc07SQu Wenruo eb->read_mirror = mirror; 652371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 653371cdc07SQu Wenruo ret = -EIO; 654371cdc07SQu Wenruo goto err; 655371cdc07SQu Wenruo } 656947a6299SQu Wenruo ret = validate_extent_buffer(eb, check); 657371cdc07SQu Wenruo if (ret < 0) 658371cdc07SQu Wenruo goto err; 659371cdc07SQu Wenruo 660371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 661371cdc07SQu Wenruo 662371cdc07SQu Wenruo free_extent_buffer(eb); 663371cdc07SQu Wenruo return ret; 664371cdc07SQu Wenruo err: 665371cdc07SQu Wenruo /* 666371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 667371cdc07SQu Wenruo * make sure it has something to decrement. 668371cdc07SQu Wenruo */ 669371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 670371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 671371cdc07SQu Wenruo free_extent_buffer(eb); 672371cdc07SQu Wenruo return ret; 673371cdc07SQu Wenruo } 674371cdc07SQu Wenruo 675c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 67677bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 67777bf40a2SQu Wenruo int mirror) 67877bf40a2SQu Wenruo { 67977bf40a2SQu Wenruo struct extent_buffer *eb; 68077bf40a2SQu Wenruo int ret = 0; 68177bf40a2SQu Wenruo int reads_done; 68277bf40a2SQu Wenruo 68377bf40a2SQu Wenruo ASSERT(page->private); 684371cdc07SQu Wenruo 685fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 686947a6299SQu Wenruo return validate_subpage_buffer(page, start, end, mirror, 687947a6299SQu Wenruo &bbio->parent_check); 688371cdc07SQu Wenruo 68977bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 69077bf40a2SQu Wenruo 69177bf40a2SQu Wenruo /* 69277bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 69377bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 69477bf40a2SQu Wenruo */ 69577bf40a2SQu Wenruo atomic_inc(&eb->refs); 69677bf40a2SQu Wenruo 69777bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 69877bf40a2SQu Wenruo if (!reads_done) 69977bf40a2SQu Wenruo goto err; 70077bf40a2SQu Wenruo 70177bf40a2SQu Wenruo eb->read_mirror = mirror; 70277bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 70377bf40a2SQu Wenruo ret = -EIO; 70477bf40a2SQu Wenruo goto err; 70577bf40a2SQu Wenruo } 706947a6299SQu Wenruo ret = validate_extent_buffer(eb, &bbio->parent_check); 707ce9adaa5SChris Mason err: 70853b381b3SDavid Woodhouse if (ret) { 70953b381b3SDavid Woodhouse /* 71053b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 71153b381b3SDavid Woodhouse * again, we have to make sure it has something 71253b381b3SDavid Woodhouse * to decrement 71353b381b3SDavid Woodhouse */ 71453b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7150b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 71653b381b3SDavid Woodhouse } 7170b32f4bbSJosef Bacik free_extent_buffer(eb); 71877bf40a2SQu Wenruo 719f188591eSChris Mason return ret; 720ce9adaa5SChris Mason } 721ce9adaa5SChris Mason 7224a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7234a69a410SChris Mason { 7244a69a410SChris Mason struct async_submit_bio *async; 7254e4cbee9SChristoph Hellwig blk_status_t ret; 7264a69a410SChris Mason 7274a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 728ab2072b2SDavid Sterba switch (async->submit_cmd) { 729ab2072b2SDavid Sterba case WQ_SUBMIT_METADATA: 730*deb6216fSChristoph Hellwig ret = btree_csum_one_bio(async->bio); 731ab2072b2SDavid Sterba break; 732ab2072b2SDavid Sterba case WQ_SUBMIT_DATA: 733*deb6216fSChristoph Hellwig ret = btrfs_csum_one_bio(async->inode, async->bio, (u64)-1, false); 734ab2072b2SDavid Sterba break; 735ab2072b2SDavid Sterba case WQ_SUBMIT_DATA_DIO: 736*deb6216fSChristoph Hellwig ret = btrfs_csum_one_bio(async->inode, async->bio, 737*deb6216fSChristoph Hellwig async->dio_file_offset, false); 738ab2072b2SDavid Sterba break; 739a6ca692eSJosef Bacik default: 740a6ca692eSJosef Bacik /* Can't happen so return something that would prevent the IO. */ 741a6ca692eSJosef Bacik ret = BLK_STS_IOERR; 742a6ca692eSJosef Bacik ASSERT(0); 743ab2072b2SDavid Sterba } 74479787eaaSJeff Mahoney if (ret) 7454e4cbee9SChristoph Hellwig async->status = ret; 7464a69a410SChris Mason } 7474a69a410SChris Mason 74806ea01b1SDavid Sterba /* 74906ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 75006ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 75106ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 75206ea01b1SDavid Sterba * 75306ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 75406ea01b1SDavid Sterba * inserted into the tree. 75506ea01b1SDavid Sterba */ 7564a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7578b712842SChris Mason { 758917f32a2SChristoph Hellwig struct async_submit_bio *async = 759917f32a2SChristoph Hellwig container_of(work, struct async_submit_bio, work); 760da67daabSDavid Sterba struct btrfs_inode *inode = async->inode; 761917f32a2SChristoph Hellwig struct btrfs_bio *bbio = btrfs_bio(async->bio); 7624854ddd0SChris Mason 763bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7644e4cbee9SChristoph Hellwig if (async->status) { 765917f32a2SChristoph Hellwig btrfs_bio_end_io(bbio, async->status); 76679787eaaSJeff Mahoney return; 76779787eaaSJeff Mahoney } 76879787eaaSJeff Mahoney 769ec39f769SChris Mason /* 770ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 771ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 772ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 773ec39f769SChris Mason */ 774ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 775da67daabSDavid Sterba btrfs_submit_bio(inode->root->fs_info, async->bio, async->mirror_num); 7764a69a410SChris Mason } 7774a69a410SChris Mason 7784a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7794a69a410SChris Mason { 7804a69a410SChris Mason struct async_submit_bio *async; 7814a69a410SChris Mason 7824a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7838b712842SChris Mason kfree(async); 7848b712842SChris Mason } 7858b712842SChris Mason 786ea1f0cedSChristoph Hellwig /* 787ea1f0cedSChristoph Hellwig * Submit bio to an async queue. 788ea1f0cedSChristoph Hellwig * 78967da05b3SColin Ian King * Return: 79067da05b3SColin Ian King * - true if the work has been successfully submitted 791ea1f0cedSChristoph Hellwig * - false in case of error 792ea1f0cedSChristoph Hellwig */ 7935fcdadc2SDavid Sterba bool btrfs_wq_submit_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num, 794ab2072b2SDavid Sterba u64 dio_file_offset, enum btrfs_wq_submit_cmd cmd) 79544b8bd7eSChris Mason { 7965fcdadc2SDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 79744b8bd7eSChris Mason struct async_submit_bio *async; 79844b8bd7eSChris Mason 79944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 80044b8bd7eSChris Mason if (!async) 801ea1f0cedSChristoph Hellwig return false; 80244b8bd7eSChris Mason 8035fcdadc2SDavid Sterba async->inode = inode; 80444b8bd7eSChris Mason async->bio = bio; 80544b8bd7eSChris Mason async->mirror_num = mirror_num; 806ab2072b2SDavid Sterba async->submit_cmd = cmd; 8074a69a410SChris Mason 808a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 809a0cac0ecSOmar Sandoval run_one_async_free); 8104a69a410SChris Mason 8111941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 8128c8bee1dSChris Mason 8134e4cbee9SChristoph Hellwig async->status = 0; 81479787eaaSJeff Mahoney 81567f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 816a31b4a43SChristoph Hellwig btrfs_queue_work(fs_info->hipri_workers, &async->work); 817a31b4a43SChristoph Hellwig else 8185cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 819ea1f0cedSChristoph Hellwig return true; 82044b8bd7eSChris Mason } 82144b8bd7eSChris Mason 822f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 8239b4e675aSDavid Sterba struct btrfs_inode *bi) 824de0022b9SJosef Bacik { 8254eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 826f4dcfb30SJohannes Thumshirn return false; 8276300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 828f4dcfb30SJohannes Thumshirn return false; 8299b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 830f4dcfb30SJohannes Thumshirn return false; 831f4dcfb30SJohannes Thumshirn return true; 832de0022b9SJosef Bacik } 833de0022b9SJosef Bacik 834644094fdSDavid Sterba void btrfs_submit_metadata_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num) 83544b8bd7eSChris Mason { 836644094fdSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 837917f32a2SChristoph Hellwig struct btrfs_bio *bbio = btrfs_bio(bio); 8384e4cbee9SChristoph Hellwig blk_status_t ret; 839cad321adSChris Mason 84008a6f464SChristoph Hellwig bio->bi_opf |= REQ_META; 84108a6f464SChristoph Hellwig 842cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 8431a722d8fSChristoph Hellwig btrfs_submit_bio(fs_info, bio, mirror_num); 8441a722d8fSChristoph Hellwig return; 84544b8bd7eSChris Mason } 84644b8bd7eSChris Mason 847ea1f0cedSChristoph Hellwig /* 848ea1f0cedSChristoph Hellwig * Kthread helpers are used to submit writes so that checksumming can 849ea1f0cedSChristoph Hellwig * happen in parallel across all CPUs. 850ea1f0cedSChristoph Hellwig */ 851644094fdSDavid Sterba if (should_async_write(fs_info, inode) && 852644094fdSDavid Sterba btrfs_wq_submit_bio(inode, bio, mirror_num, 0, WQ_SUBMIT_METADATA)) 853ea1f0cedSChristoph Hellwig return; 854ea1f0cedSChristoph Hellwig 855ea1f0cedSChristoph Hellwig ret = btree_csum_one_bio(bio); 85694d9e11bSChristoph Hellwig if (ret) { 857917f32a2SChristoph Hellwig btrfs_bio_end_io(bbio, ret); 858ea1f0cedSChristoph Hellwig return; 85994d9e11bSChristoph Hellwig } 860ea1f0cedSChristoph Hellwig 861ea1f0cedSChristoph Hellwig btrfs_submit_bio(fs_info, bio, mirror_num); 86261891923SStefan Behrens } 86361891923SStefan Behrens 8643dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 8658958b551SMatthew Wilcox (Oracle) static int btree_migrate_folio(struct address_space *mapping, 8668958b551SMatthew Wilcox (Oracle) struct folio *dst, struct folio *src, enum migrate_mode mode) 867784b4e29SChris Mason { 868784b4e29SChris Mason /* 869784b4e29SChris Mason * we can't safely write a btree page from here, 870784b4e29SChris Mason * we haven't done the locking hook 871784b4e29SChris Mason */ 8728958b551SMatthew Wilcox (Oracle) if (folio_test_dirty(src)) 873784b4e29SChris Mason return -EAGAIN; 874784b4e29SChris Mason /* 875784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 876784b4e29SChris Mason * We must have no buffers or drop them. 877784b4e29SChris Mason */ 8788958b551SMatthew Wilcox (Oracle) if (folio_get_private(src) && 8798958b551SMatthew Wilcox (Oracle) !filemap_release_folio(src, GFP_KERNEL)) 880784b4e29SChris Mason return -EAGAIN; 88154184650SMatthew Wilcox (Oracle) return migrate_folio(mapping, dst, src, mode); 882784b4e29SChris Mason } 8838958b551SMatthew Wilcox (Oracle) #else 8848958b551SMatthew Wilcox (Oracle) #define btree_migrate_folio NULL 8853dd1462eSJan Beulich #endif 886784b4e29SChris Mason 8870da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8880da5468fSChris Mason struct writeback_control *wbc) 8890da5468fSChris Mason { 890e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 891e2d84521SMiao Xie int ret; 892e2d84521SMiao Xie 893d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 894448d640bSChris Mason 895448d640bSChris Mason if (wbc->for_kupdate) 896448d640bSChris Mason return 0; 897448d640bSChris Mason 898e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 899b9473439SChris Mason /* this is a bit racy, but that's ok */ 900d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 901d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 902d814a491SEthan Lien fs_info->dirty_metadata_batch); 903e2d84521SMiao Xie if (ret < 0) 904793955bcSChris Mason return 0; 905793955bcSChris Mason } 9060b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9070da5468fSChris Mason } 9080da5468fSChris Mason 909f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags) 9105f39d397SChris Mason { 911f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio)) 912f913cff3SMatthew Wilcox (Oracle) return false; 9130c4e538bSDavid Sterba 914f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page); 915d98237b3SChris Mason } 916d98237b3SChris Mason 917895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 918895586ebSMatthew Wilcox (Oracle) size_t length) 919d98237b3SChris Mason { 920d1310b2eSChris Mason struct extent_io_tree *tree; 921895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 922895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 923f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS); 924895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 925895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 926895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 927895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 928895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 9299ad6b7bcSChris Mason } 930d98237b3SChris Mason } 931d98237b3SChris Mason 932bb146eb2SJosef Bacik #ifdef DEBUG 9330079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 9340079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 9350079c3b1SMatthew Wilcox (Oracle) { 9360079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 937139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 9380b32f4bbSJosef Bacik struct extent_buffer *eb; 939139e8cd3SQu Wenruo int cur_bit = 0; 9400079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 9410b32f4bbSJosef Bacik 942139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 9430079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 9440b32f4bbSJosef Bacik BUG_ON(!eb); 9450b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9460b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 94749d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 9480079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 949139e8cd3SQu Wenruo } 9500079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 951139e8cd3SQu Wenruo 952139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 953139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 954139e8cd3SQu Wenruo unsigned long flags; 955139e8cd3SQu Wenruo u64 cur; 956139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 957139e8cd3SQu Wenruo 958139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 959139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 960139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 961139e8cd3SQu Wenruo cur_bit++; 962139e8cd3SQu Wenruo continue; 963139e8cd3SQu Wenruo } 964139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 965139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 966139e8cd3SQu Wenruo 967139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 968139e8cd3SQu Wenruo ASSERT(eb); 969139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 970139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 97149d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 972139e8cd3SQu Wenruo free_extent_buffer(eb); 973139e8cd3SQu Wenruo 974139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 975139e8cd3SQu Wenruo } 9760079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 9770b32f4bbSJosef Bacik } 9780079c3b1SMatthew Wilcox (Oracle) #else 9790079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 9800079c3b1SMatthew Wilcox (Oracle) #endif 9810b32f4bbSJosef Bacik 9827f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9830da5468fSChris Mason .writepages = btree_writepages, 984f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio, 985895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 9868958b551SMatthew Wilcox (Oracle) .migrate_folio = btree_migrate_folio, 9870079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 988d98237b3SChris Mason }; 989123abc88SChris Mason 9902ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9912ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9923fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 9933fbaf258SJosef Bacik int level) 9940999df54SChris Mason { 9950b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9960b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 9973fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 9980999df54SChris Mason } 9990999df54SChris Mason 1000581c1760SQu Wenruo /* 1001581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1002581c1760SQu Wenruo * verification. 1003581c1760SQu Wenruo * 1004789d6a3aSQu Wenruo * @check: expected tree parentness check, see comments of the 1005789d6a3aSQu Wenruo * structure for details. 1006581c1760SQu Wenruo */ 10072ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1008789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check) 1009e20d96d6SChris Mason { 10105f39d397SChris Mason struct extent_buffer *buf = NULL; 101119c00ddcSChris Mason int ret; 101219c00ddcSChris Mason 1013789d6a3aSQu Wenruo ASSERT(check); 1014789d6a3aSQu Wenruo 1015789d6a3aSQu Wenruo buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root, 1016789d6a3aSQu Wenruo check->level); 1017c871b0f2SLiu Bo if (IS_ERR(buf)) 1018c871b0f2SLiu Bo return buf; 1019e4204dedSChris Mason 1020789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(buf, check); 10210f0fe8f7SFilipe David Borba Manana if (ret) { 1022537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 102364c043deSLiu Bo return ERR_PTR(ret); 10240f0fe8f7SFilipe David Borba Manana } 1025789d6a3aSQu Wenruo if (btrfs_check_eb_owner(buf, check->owner_root)) { 102688c602abSQu Wenruo free_extent_buffer_stale(buf); 102788c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 102888c602abSQu Wenruo } 10295f39d397SChris Mason return buf; 1030ce9adaa5SChris Mason 1031eb60ceacSChris Mason } 1032eb60ceacSChris Mason 10336a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1034ed2ff2cbSChris Mason { 10356a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 103655c69072SChris Mason if (btrfs_header_generation(buf) == 1037e2d84521SMiao Xie fs_info->running_transaction->transid) { 103849d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1039b4ce94deSChris Mason 1040b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1041104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1042e2d84521SMiao Xie -buf->len, 1043e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10440b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1045925baeddSChris Mason } 10465f39d397SChris Mason } 1047ed7b63ebSJosef Bacik } 10485f39d397SChris Mason 1049da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1050e20d96d6SChris Mason u64 objectid) 1051d97e63b6SChris Mason { 10527c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 10532e608bd1SJosef Bacik 10542e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 10552e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 10562e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 105796dfcb46SJosef Bacik root->fs_info = fs_info; 10582e608bd1SJosef Bacik root->root_key.objectid = objectid; 1059cfaa7295SChris Mason root->node = NULL; 1060a28ec197SChris Mason root->commit_root = NULL; 106127cdeb70SMiao Xie root->state = 0; 1062abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 10630b86a832SChris Mason 10640f7d52f4SChris Mason root->last_trans = 0; 10656b8fad57SNikolay Borisov root->free_objectid = 0; 1066eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1067199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10686bef4d31SEric Paris root->inode_tree = RB_ROOT; 1069088aea3bSDavid Sterba INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 10702e608bd1SJosef Bacik 10712e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 10720b86a832SChris Mason 10730b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10745d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1075eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1076eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1077199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1078199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1079d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10802ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10812ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10825d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1083eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1084199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1085f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10862ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10872ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10888287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1089a2135011SChris Mason mutex_init(&root->objectid_mutex); 1090e02119d5SChris Mason mutex_init(&root->log_mutex); 109131f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1092573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1093c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10947237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10957237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10967237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 10978b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 10988b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 10997237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11007237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11017237f183SYan Zheng atomic_set(&root->log_writers, 0); 11022ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11030700cea7SElena Reshetova refcount_set(&root->refs, 1); 11048ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1105eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 11067237f183SYan Zheng root->log_transid = 0; 1107d1433debSMiao Xie root->log_transid_committed = -1; 1108257c62e1SChris Mason root->last_log_commit = 0; 11092e608bd1SJosef Bacik root->anon_dev = 0; 1110e289f03eSFilipe Manana if (!dummy) { 111143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 111235da5a7eSDavid Sterba IO_TREE_ROOT_DIRTY_LOG_PAGES); 1113e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 111435da5a7eSDavid Sterba IO_TREE_LOG_CSUM_RANGE); 1115e289f03eSFilipe Manana } 1116017e5369SChris Mason 11175f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1118370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1119bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1120bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1121fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1122bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1123fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1124bd647ce3SJosef Bacik #endif 11253768f368SChris Mason } 11263768f368SChris Mason 112774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 112896dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11296f07e42eSAl Viro { 113074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11316f07e42eSAl Viro if (root) 113296dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11336f07e42eSAl Viro return root; 11346f07e42eSAl Viro } 11356f07e42eSAl Viro 113606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 113706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1138da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 113906ea65a3SJosef Bacik { 114006ea65a3SJosef Bacik struct btrfs_root *root; 114106ea65a3SJosef Bacik 11427c0260eeSJeff Mahoney if (!fs_info) 11437c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11447c0260eeSJeff Mahoney 114596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 114606ea65a3SJosef Bacik if (!root) 114706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1148da17066cSJeff Mahoney 1149b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1150faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 115106ea65a3SJosef Bacik 115206ea65a3SJosef Bacik return root; 115306ea65a3SJosef Bacik } 115406ea65a3SJosef Bacik #endif 115506ea65a3SJosef Bacik 1156abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1157abed4aaaSJosef Bacik { 1158abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1159abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1160abed4aaaSJosef Bacik 1161abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1162abed4aaaSJosef Bacik } 1163abed4aaaSJosef Bacik 1164abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1165abed4aaaSJosef Bacik { 1166abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1167abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1168abed4aaaSJosef Bacik 1169abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1170abed4aaaSJosef Bacik } 1171abed4aaaSJosef Bacik 1172abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1173abed4aaaSJosef Bacik { 1174abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1175abed4aaaSJosef Bacik struct rb_node *tmp; 1176abed4aaaSJosef Bacik 1177abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1178abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1179abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1180abed4aaaSJosef Bacik ASSERT(!tmp); 1181abed4aaaSJosef Bacik 1182abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1183abed4aaaSJosef Bacik } 1184abed4aaaSJosef Bacik 1185abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1186abed4aaaSJosef Bacik { 1187abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1188abed4aaaSJosef Bacik 1189abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1190abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1191abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1192abed4aaaSJosef Bacik } 1193abed4aaaSJosef Bacik 1194abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1195abed4aaaSJosef Bacik struct btrfs_key *key) 1196abed4aaaSJosef Bacik { 1197abed4aaaSJosef Bacik struct rb_node *node; 1198abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1199abed4aaaSJosef Bacik 1200abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1201abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1202abed4aaaSJosef Bacik if (node) 1203abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1204abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1205abed4aaaSJosef Bacik 1206abed4aaaSJosef Bacik return root; 1207abed4aaaSJosef Bacik } 1208abed4aaaSJosef Bacik 1209f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1210f7238e50SJosef Bacik { 1211f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1212f7238e50SJosef Bacik u64 ret; 1213f7238e50SJosef Bacik 1214f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1215f7238e50SJosef Bacik return 0; 1216f7238e50SJosef Bacik 1217f7238e50SJosef Bacik if (bytenr) 1218f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1219f7238e50SJosef Bacik else 1220f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1221f7238e50SJosef Bacik ASSERT(block_group); 1222f7238e50SJosef Bacik if (!block_group) 1223f7238e50SJosef Bacik return 0; 1224f7238e50SJosef Bacik ret = block_group->global_root_id; 1225f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1226f7238e50SJosef Bacik 1227f7238e50SJosef Bacik return ret; 1228f7238e50SJosef Bacik } 1229f7238e50SJosef Bacik 1230abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1231abed4aaaSJosef Bacik { 1232abed4aaaSJosef Bacik struct btrfs_key key = { 1233abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1234abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1235f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1236abed4aaaSJosef Bacik }; 1237abed4aaaSJosef Bacik 1238abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1239abed4aaaSJosef Bacik } 1240abed4aaaSJosef Bacik 1241abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1242abed4aaaSJosef Bacik { 1243abed4aaaSJosef Bacik struct btrfs_key key = { 1244abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1245abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1246f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1247abed4aaaSJosef Bacik }; 1248abed4aaaSJosef Bacik 1249abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1250abed4aaaSJosef Bacik } 1251abed4aaaSJosef Bacik 125251129b33SJosef Bacik struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info) 125351129b33SJosef Bacik { 125451129b33SJosef Bacik if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) 125551129b33SJosef Bacik return fs_info->block_group_root; 125651129b33SJosef Bacik return btrfs_extent_root(fs_info, 0); 125751129b33SJosef Bacik } 125851129b33SJosef Bacik 125920897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 126020897f5cSArne Jansen u64 objectid) 126120897f5cSArne Jansen { 12629b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 126320897f5cSArne Jansen struct extent_buffer *leaf; 126420897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 126520897f5cSArne Jansen struct btrfs_root *root; 126620897f5cSArne Jansen struct btrfs_key key; 1267b89f6d1fSFilipe Manana unsigned int nofs_flag; 126820897f5cSArne Jansen int ret = 0; 126920897f5cSArne Jansen 1270b89f6d1fSFilipe Manana /* 1271b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1272b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1273b89f6d1fSFilipe Manana */ 1274b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 127596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1276b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 127720897f5cSArne Jansen if (!root) 127820897f5cSArne Jansen return ERR_PTR(-ENOMEM); 127920897f5cSArne Jansen 128020897f5cSArne Jansen root->root_key.objectid = objectid; 128120897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 128220897f5cSArne Jansen root->root_key.offset = 0; 128320897f5cSArne Jansen 12849631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12859631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 128620897f5cSArne Jansen if (IS_ERR(leaf)) { 128720897f5cSArne Jansen ret = PTR_ERR(leaf); 12881dd05682STsutomu Itoh leaf = NULL; 1289c1b07854SPeng Hao goto fail; 129020897f5cSArne Jansen } 129120897f5cSArne Jansen 129220897f5cSArne Jansen root->node = leaf; 129320897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 129420897f5cSArne Jansen 129520897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129627cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 129720897f5cSArne Jansen 1298f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1299f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 130020897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 130120897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 130220897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 130320897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 130420897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 130520897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130620897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130733d85fdaSQu Wenruo if (is_fstree(objectid)) 1308807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1309807fc790SAndy Shevchenko else 1310807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1311c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 131220897f5cSArne Jansen 13138a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 13148a6a87cdSBoris Burkov 131520897f5cSArne Jansen key.objectid = objectid; 131620897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131720897f5cSArne Jansen key.offset = 0; 131820897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 131920897f5cSArne Jansen if (ret) 132020897f5cSArne Jansen goto fail; 132120897f5cSArne Jansen 132220897f5cSArne Jansen return root; 13231dd05682STsutomu Itoh 13248a6a87cdSBoris Burkov fail: 132500246528SJosef Bacik btrfs_put_root(root); 13261dd05682STsutomu Itoh 13271dd05682STsutomu Itoh return ERR_PTR(ret); 132820897f5cSArne Jansen } 132920897f5cSArne Jansen 13307237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1331e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13320f7d52f4SChris Mason { 13330f7d52f4SChris Mason struct btrfs_root *root; 1334e02119d5SChris Mason 133596dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1336e02119d5SChris Mason if (!root) 13377237f183SYan Zheng return ERR_PTR(-ENOMEM); 1338e02119d5SChris Mason 1339e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1340e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1341e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 134227cdeb70SMiao Xie 13436ab6ebb7SNaohiro Aota return root; 13446ab6ebb7SNaohiro Aota } 13456ab6ebb7SNaohiro Aota 13466ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 13476ab6ebb7SNaohiro Aota struct btrfs_root *root) 13486ab6ebb7SNaohiro Aota { 13496ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 13506ab6ebb7SNaohiro Aota 13517237f183SYan Zheng /* 135292a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 135327cdeb70SMiao Xie * 135492a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 135592a7cc42SQu Wenruo * and they go away before a real commit is actually done. 135692a7cc42SQu Wenruo * 135792a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 135892a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 13597237f183SYan Zheng */ 1360e02119d5SChris Mason 13614d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13629631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 13636ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 13646ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1365e02119d5SChris Mason 13667237f183SYan Zheng root->node = leaf; 1367e02119d5SChris Mason 1368e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1369e02119d5SChris Mason btrfs_tree_unlock(root->node); 13706ab6ebb7SNaohiro Aota 13716ab6ebb7SNaohiro Aota return 0; 13727237f183SYan Zheng } 13737237f183SYan Zheng 13747237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13757237f183SYan Zheng struct btrfs_fs_info *fs_info) 13767237f183SYan Zheng { 13777237f183SYan Zheng struct btrfs_root *log_root; 13787237f183SYan Zheng 13797237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13807237f183SYan Zheng if (IS_ERR(log_root)) 13817237f183SYan Zheng return PTR_ERR(log_root); 13826ab6ebb7SNaohiro Aota 13833ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 13843ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 13853ddebf27SNaohiro Aota 13866ab6ebb7SNaohiro Aota if (ret) { 13876ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13886ab6ebb7SNaohiro Aota return ret; 13896ab6ebb7SNaohiro Aota } 13903ddebf27SNaohiro Aota } 13916ab6ebb7SNaohiro Aota 13927237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13937237f183SYan Zheng fs_info->log_root_tree = log_root; 13947237f183SYan Zheng return 0; 13957237f183SYan Zheng } 13967237f183SYan Zheng 13977237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13987237f183SYan Zheng struct btrfs_root *root) 13997237f183SYan Zheng { 14000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14017237f183SYan Zheng struct btrfs_root *log_root; 14027237f183SYan Zheng struct btrfs_inode_item *inode_item; 14036ab6ebb7SNaohiro Aota int ret; 14047237f183SYan Zheng 14050b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14067237f183SYan Zheng if (IS_ERR(log_root)) 14077237f183SYan Zheng return PTR_ERR(log_root); 14087237f183SYan Zheng 14096ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 14106ab6ebb7SNaohiro Aota if (ret) { 14116ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 14126ab6ebb7SNaohiro Aota return ret; 14136ab6ebb7SNaohiro Aota } 14146ab6ebb7SNaohiro Aota 14157237f183SYan Zheng log_root->last_trans = trans->transid; 14167237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14177237f183SYan Zheng 14187237f183SYan Zheng inode_item = &log_root->root_item.inode; 14193cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14203cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14213cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1422da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14230b246afaSJeff Mahoney fs_info->nodesize); 14243cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14257237f183SYan Zheng 14265d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14277237f183SYan Zheng 14287237f183SYan Zheng WARN_ON(root->log_root); 14297237f183SYan Zheng root->log_root = log_root; 14307237f183SYan Zheng root->log_transid = 0; 1431d1433debSMiao Xie root->log_transid_committed = -1; 1432257c62e1SChris Mason root->last_log_commit = 0; 1433e02119d5SChris Mason return 0; 1434e02119d5SChris Mason } 1435e02119d5SChris Mason 143649d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 143749d11beaSJosef Bacik struct btrfs_path *path, 1438cb517eabSMiao Xie struct btrfs_key *key) 1439e02119d5SChris Mason { 1440e02119d5SChris Mason struct btrfs_root *root; 1441789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 1442e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 144384234f3aSYan Zheng u64 generation; 1444cb517eabSMiao Xie int ret; 1445581c1760SQu Wenruo int level; 1446cb517eabSMiao Xie 144796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 144849d11beaSJosef Bacik if (!root) 144949d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 14500f7d52f4SChris Mason 1451cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1452cb517eabSMiao Xie &root->root_item, &root->root_key); 14530f7d52f4SChris Mason if (ret) { 145413a8a7c8SYan, Zheng if (ret > 0) 145513a8a7c8SYan, Zheng ret = -ENOENT; 145649d11beaSJosef Bacik goto fail; 14570f7d52f4SChris Mason } 145813a8a7c8SYan, Zheng 145984234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1460581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 1461789d6a3aSQu Wenruo check.level = level; 1462789d6a3aSQu Wenruo check.transid = generation; 1463789d6a3aSQu Wenruo check.owner_root = key->objectid; 1464789d6a3aSQu Wenruo root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item), 1465789d6a3aSQu Wenruo &check); 146664c043deSLiu Bo if (IS_ERR(root->node)) { 146764c043deSLiu Bo ret = PTR_ERR(root->node); 14688c38938cSJosef Bacik root->node = NULL; 146949d11beaSJosef Bacik goto fail; 14704eb150d6SQu Wenruo } 14714eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1472cb517eabSMiao Xie ret = -EIO; 147349d11beaSJosef Bacik goto fail; 1474416bc658SJosef Bacik } 147588c602abSQu Wenruo 147688c602abSQu Wenruo /* 147788c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 147888c602abSQu Wenruo * match its root node owner 147988c602abSQu Wenruo */ 148088c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 148188c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 148288c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 148388c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 148488c602abSQu Wenruo btrfs_crit(fs_info, 148588c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 148688c602abSQu Wenruo root->root_key.objectid, root->node->start, 148788c602abSQu Wenruo btrfs_header_owner(root->node), 148888c602abSQu Wenruo root->root_key.objectid); 148988c602abSQu Wenruo ret = -EUCLEAN; 149088c602abSQu Wenruo goto fail; 149188c602abSQu Wenruo } 14925d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1493cb517eabSMiao Xie return root; 149449d11beaSJosef Bacik fail: 149500246528SJosef Bacik btrfs_put_root(root); 149649d11beaSJosef Bacik return ERR_PTR(ret); 149749d11beaSJosef Bacik } 149849d11beaSJosef Bacik 149949d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 150049d11beaSJosef Bacik struct btrfs_key *key) 150149d11beaSJosef Bacik { 150249d11beaSJosef Bacik struct btrfs_root *root; 150349d11beaSJosef Bacik struct btrfs_path *path; 150449d11beaSJosef Bacik 150549d11beaSJosef Bacik path = btrfs_alloc_path(); 150649d11beaSJosef Bacik if (!path) 150749d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 150849d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 150949d11beaSJosef Bacik btrfs_free_path(path); 151049d11beaSJosef Bacik 151149d11beaSJosef Bacik return root; 1512cb517eabSMiao Xie } 1513cb517eabSMiao Xie 15142dfb1e43SQu Wenruo /* 15152dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 15162dfb1e43SQu Wenruo * 15172dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 15182dfb1e43SQu Wenruo */ 15192dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1520cb517eabSMiao Xie { 1521cb517eabSMiao Xie int ret; 1522dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1523cb517eabSMiao Xie 1524dcc3eb96SNikolay Borisov /* 1525dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1526dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1527dcc3eb96SNikolay Borisov */ 1528dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1529dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1530dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1531dcc3eb96SNikolay Borisov if (ret) 15328257b2dcSMiao Xie goto fail; 15338257b2dcSMiao Xie 1534aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 153537f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 153692a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1537f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1538f39e4571SJosef Bacik } 1539f39e4571SJosef Bacik 1540851fd730SQu Wenruo /* 1541851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1542851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1543851fd730SQu Wenruo */ 1544851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1545851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 15462dfb1e43SQu Wenruo if (!anon_dev) { 1547cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1548cb517eabSMiao Xie if (ret) 1549876d2cf1SLiu Bo goto fail; 15502dfb1e43SQu Wenruo } else { 15512dfb1e43SQu Wenruo root->anon_dev = anon_dev; 15522dfb1e43SQu Wenruo } 1553851fd730SQu Wenruo } 1554f32e48e9SChandan Rajendra 1555f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1556453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1557f32e48e9SChandan Rajendra if (ret) { 1558f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1559876d2cf1SLiu Bo goto fail; 1560f32e48e9SChandan Rajendra } 1561f32e48e9SChandan Rajendra 15626b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1563f32e48e9SChandan Rajendra 1564f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1565f32e48e9SChandan Rajendra 1566cb517eabSMiao Xie return 0; 1567cb517eabSMiao Xie fail: 156884db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1569cb517eabSMiao Xie return ret; 1570cb517eabSMiao Xie } 1571cb517eabSMiao Xie 1572a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1573cb517eabSMiao Xie u64 root_id) 1574cb517eabSMiao Xie { 1575cb517eabSMiao Xie struct btrfs_root *root; 1576cb517eabSMiao Xie 1577fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1578fc7cbcd4SDavid Sterba root = radix_tree_lookup(&fs_info->fs_roots_radix, 1579fc7cbcd4SDavid Sterba (unsigned long)root_id); 1580bc44d7c4SJosef Bacik if (root) 158100246528SJosef Bacik root = btrfs_grab_root(root); 1582fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1583cb517eabSMiao Xie return root; 1584cb517eabSMiao Xie } 1585cb517eabSMiao Xie 158649d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 158749d11beaSJosef Bacik u64 objectid) 158849d11beaSJosef Bacik { 1589abed4aaaSJosef Bacik struct btrfs_key key = { 1590abed4aaaSJosef Bacik .objectid = objectid, 1591abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1592abed4aaaSJosef Bacik .offset = 0, 1593abed4aaaSJosef Bacik }; 1594abed4aaaSJosef Bacik 159549d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 159649d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 159749d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1598abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 159949d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 160049d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 160149d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 160249d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 160349d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1604abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 160549d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 160649d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 160749d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 160849d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 160949d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 161049d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 161114033b08SQu Wenruo if (objectid == BTRFS_BLOCK_GROUP_TREE_OBJECTID) 161214033b08SQu Wenruo return btrfs_grab_root(fs_info->block_group_root) ? 161314033b08SQu Wenruo fs_info->block_group_root : ERR_PTR(-ENOENT); 1614abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1615abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1616abed4aaaSJosef Bacik 1617abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1618abed4aaaSJosef Bacik } 161949d11beaSJosef Bacik return NULL; 162049d11beaSJosef Bacik } 162149d11beaSJosef Bacik 1622cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1623cb517eabSMiao Xie struct btrfs_root *root) 1624cb517eabSMiao Xie { 1625cb517eabSMiao Xie int ret; 1626cb517eabSMiao Xie 1627fc7cbcd4SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1628fc7cbcd4SDavid Sterba if (ret) 1629fc7cbcd4SDavid Sterba return ret; 1630fc7cbcd4SDavid Sterba 1631fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1632fc7cbcd4SDavid Sterba ret = radix_tree_insert(&fs_info->fs_roots_radix, 1633fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid, 1634fc7cbcd4SDavid Sterba root); 1635af01d2e5SJosef Bacik if (ret == 0) { 163600246528SJosef Bacik btrfs_grab_root(root); 1637fc7cbcd4SDavid Sterba set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1638af01d2e5SJosef Bacik } 1639fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1640fc7cbcd4SDavid Sterba radix_tree_preload_end(); 1641cb517eabSMiao Xie 1642cb517eabSMiao Xie return ret; 1643cb517eabSMiao Xie } 1644cb517eabSMiao Xie 1645bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1646bd647ce3SJosef Bacik { 1647bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1648bd647ce3SJosef Bacik struct btrfs_root *root; 1649bd647ce3SJosef Bacik 1650bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1651457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1652457f1864SJosef Bacik 1653bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1654bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1655457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 165671008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1657bd647ce3SJosef Bacik refcount_read(&root->refs)); 1658bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 165900246528SJosef Bacik btrfs_put_root(root); 166000246528SJosef Bacik btrfs_put_root(root); 1661bd647ce3SJosef Bacik } 1662bd647ce3SJosef Bacik #endif 1663bd647ce3SJosef Bacik } 1664bd647ce3SJosef Bacik 1665abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1666abed4aaaSJosef Bacik { 1667abed4aaaSJosef Bacik struct btrfs_root *root; 1668abed4aaaSJosef Bacik struct rb_node *node; 1669abed4aaaSJosef Bacik 1670abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1671abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1672abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1673abed4aaaSJosef Bacik btrfs_put_root(root); 1674abed4aaaSJosef Bacik } 1675abed4aaaSJosef Bacik } 1676abed4aaaSJosef Bacik 16770d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 16780d4b0463SJosef Bacik { 1679141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1680141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 16815deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1682141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1683141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1684141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1685141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 16860d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 16870d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1688abed4aaaSJosef Bacik free_global_roots(fs_info); 168900246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 169000246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 169100246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 169200246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 169300246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 169400246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1695aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 16969c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1697bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 16983fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 16990d4b0463SJosef Bacik kfree(fs_info->super_copy); 17000d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 17018481dd80SQu Wenruo kfree(fs_info->subpage_info); 17020d4b0463SJosef Bacik kvfree(fs_info); 17030d4b0463SJosef Bacik } 17040d4b0463SJosef Bacik 17050d4b0463SJosef Bacik 17062dfb1e43SQu Wenruo /* 17072dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 17082dfb1e43SQu Wenruo * 17092dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 17102dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 17112dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 17122dfb1e43SQu Wenruo * 17132dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 17142dfb1e43SQu Wenruo * 17152dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 17162dfb1e43SQu Wenruo * same root objectid. 17172dfb1e43SQu Wenruo * 17182dfb1e43SQu Wenruo * @objectid: root id 17192dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 17202dfb1e43SQu Wenruo * pass 0 for new allocation. 17212dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 17222dfb1e43SQu Wenruo * for orphan roots 17232dfb1e43SQu Wenruo */ 17242dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 17252dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 17262dfb1e43SQu Wenruo bool check_ref) 17275eda7b5eSChris Mason { 17285eda7b5eSChris Mason struct btrfs_root *root; 1729381cf658SDavid Sterba struct btrfs_path *path; 17301d4c08e0SDavid Sterba struct btrfs_key key; 17315eda7b5eSChris Mason int ret; 17325eda7b5eSChris Mason 173349d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 173449d11beaSJosef Bacik if (root) 173549d11beaSJosef Bacik return root; 17364df27c4dSYan, Zheng again: 173756e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 173848475471SStefan Behrens if (root) { 17392dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17402dfb1e43SQu Wenruo ASSERT(!anon_dev); 1741bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 174200246528SJosef Bacik btrfs_put_root(root); 174348475471SStefan Behrens return ERR_PTR(-ENOENT); 1744bc44d7c4SJosef Bacik } 17455eda7b5eSChris Mason return root; 174648475471SStefan Behrens } 17475eda7b5eSChris Mason 174856e9357aSDavid Sterba key.objectid = objectid; 174956e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 175056e9357aSDavid Sterba key.offset = (u64)-1; 175156e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 17525eda7b5eSChris Mason if (IS_ERR(root)) 17535eda7b5eSChris Mason return root; 17543394e160SChris Mason 1755c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1756d68fc57bSYan, Zheng ret = -ENOENT; 1757d68fc57bSYan, Zheng goto fail; 1758d68fc57bSYan, Zheng } 1759d68fc57bSYan, Zheng 17602dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1761cb517eabSMiao Xie if (ret) 1762cb517eabSMiao Xie goto fail; 1763cb517eabSMiao Xie 1764381cf658SDavid Sterba path = btrfs_alloc_path(); 1765381cf658SDavid Sterba if (!path) { 1766381cf658SDavid Sterba ret = -ENOMEM; 1767381cf658SDavid Sterba goto fail; 1768381cf658SDavid Sterba } 17691d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17701d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 177156e9357aSDavid Sterba key.offset = objectid; 17721d4c08e0SDavid Sterba 17731d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1774381cf658SDavid Sterba btrfs_free_path(path); 1775d68fc57bSYan, Zheng if (ret < 0) 1776d68fc57bSYan, Zheng goto fail; 1777d68fc57bSYan, Zheng if (ret == 0) 177827cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1779d68fc57bSYan, Zheng 1780cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17810f7d52f4SChris Mason if (ret) { 1782168a2f77SJia-Ju Bai if (ret == -EEXIST) { 178300246528SJosef Bacik btrfs_put_root(root); 17844df27c4dSYan, Zheng goto again; 1785168a2f77SJia-Ju Bai } 17864df27c4dSYan, Zheng goto fail; 17874df27c4dSYan, Zheng } 1788edbd8d4eSChris Mason return root; 17894df27c4dSYan, Zheng fail: 179033fab972SFilipe Manana /* 179133fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 1792143823cfSDavid Sterba * responsibility to free it in case we fail. So we have to set our 179333fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 179433fab972SFilipe Manana * and once again by our caller. 179533fab972SFilipe Manana */ 179633fab972SFilipe Manana if (anon_dev) 179733fab972SFilipe Manana root->anon_dev = 0; 17988c38938cSJosef Bacik btrfs_put_root(root); 17994df27c4dSYan, Zheng return ERR_PTR(ret); 1800edbd8d4eSChris Mason } 1801edbd8d4eSChris Mason 18022dfb1e43SQu Wenruo /* 18032dfb1e43SQu Wenruo * Get in-memory reference of a root structure 18042dfb1e43SQu Wenruo * 18052dfb1e43SQu Wenruo * @objectid: tree objectid 18062dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 18072dfb1e43SQu Wenruo * one reference 18082dfb1e43SQu Wenruo */ 18092dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 18102dfb1e43SQu Wenruo u64 objectid, bool check_ref) 18112dfb1e43SQu Wenruo { 18122dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 18132dfb1e43SQu Wenruo } 18142dfb1e43SQu Wenruo 18152dfb1e43SQu Wenruo /* 18162dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 18172dfb1e43SQu Wenruo * the anonymous block device id 18182dfb1e43SQu Wenruo * 18192dfb1e43SQu Wenruo * @objectid: tree objectid 18202dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 18212dfb1e43SQu Wenruo * parameter value 18222dfb1e43SQu Wenruo */ 18232dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 18242dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 18252dfb1e43SQu Wenruo { 18262dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 18272dfb1e43SQu Wenruo } 18282dfb1e43SQu Wenruo 18298b712842SChris Mason /* 183049d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 183149d11beaSJosef Bacik * @fs_info: the fs_info 183249d11beaSJosef Bacik * @objectid: the objectid we need to lookup 183349d11beaSJosef Bacik * 183449d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 183549d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 183649d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 183749d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 183849d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 183949d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 184049d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 184149d11beaSJosef Bacik * backref code is finished using the root. 184249d11beaSJosef Bacik */ 184349d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 184449d11beaSJosef Bacik struct btrfs_path *path, 184549d11beaSJosef Bacik u64 objectid) 184649d11beaSJosef Bacik { 184749d11beaSJosef Bacik struct btrfs_root *root; 184849d11beaSJosef Bacik struct btrfs_key key; 184949d11beaSJosef Bacik 185049d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 185149d11beaSJosef Bacik 185249d11beaSJosef Bacik /* 185349d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 185449d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 185549d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 185649d11beaSJosef Bacik * != NULL just return it. 185749d11beaSJosef Bacik */ 185849d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 185949d11beaSJosef Bacik if (root) 186049d11beaSJosef Bacik return root; 186149d11beaSJosef Bacik 186249d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 186349d11beaSJosef Bacik if (root) 186449d11beaSJosef Bacik return root; 186549d11beaSJosef Bacik 186649d11beaSJosef Bacik key.objectid = objectid; 186749d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 186849d11beaSJosef Bacik key.offset = (u64)-1; 186949d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 187049d11beaSJosef Bacik btrfs_release_path(path); 187149d11beaSJosef Bacik 187249d11beaSJosef Bacik return root; 187349d11beaSJosef Bacik } 187449d11beaSJosef Bacik 1875a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1876a74a4b97SChris Mason { 18770d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1878d0278245SMiao Xie int again; 1879a74a4b97SChris Mason 1880d6fd0ae2SOmar Sandoval while (1) { 1881d0278245SMiao Xie again = 0; 18829d1a2a3aSDavid Sterba 1883fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1884fd340d0fSJosef Bacik 1885d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18862ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1887d0278245SMiao Xie goto sleep; 1888d0278245SMiao Xie 188990c711abSZygo Blaxell /* 189090c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 189190c711abSZygo Blaxell * before we have finished mounting the filesystem. 189290c711abSZygo Blaxell */ 18930b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 189490c711abSZygo Blaxell goto sleep; 189590c711abSZygo Blaxell 18960b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1897d0278245SMiao Xie goto sleep; 1898d0278245SMiao Xie 1899dc7f370cSMiao Xie /* 1900dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1901dc7f370cSMiao Xie * during the above check and trylock. 1902dc7f370cSMiao Xie */ 19032ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 19040b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1905dc7f370cSMiao Xie goto sleep; 1906dc7f370cSMiao Xie } 1907dc7f370cSMiao Xie 1908b7625f46SQu Wenruo if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags)) 1909b7625f46SQu Wenruo btrfs_sysfs_feature_update(fs_info); 1910b7625f46SQu Wenruo 19112ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1912c2d6cb16SFilipe Manana 191333c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 19140b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1915a74a4b97SChris Mason 1916d0278245SMiao Xie /* 191705323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 191805323cd1SMiao Xie * needn't do anything special here. 1919d0278245SMiao Xie */ 19200b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 192167c5e7d4SFilipe Manana 192267c5e7d4SFilipe Manana /* 1923f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 192467c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 192567c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1926f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 192767c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 192867c5e7d4SFilipe Manana * unused block groups. 192967c5e7d4SFilipe Manana */ 19300b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 193118bb8bbfSJohannes Thumshirn 193218bb8bbfSJohannes Thumshirn /* 193318bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 193418bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 193518bb8bbfSJohannes Thumshirn * space. 193618bb8bbfSJohannes Thumshirn */ 193718bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 1938d0278245SMiao Xie sleep: 1939a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1940d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1941d6fd0ae2SOmar Sandoval kthread_parkme(); 1942d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1943d6fd0ae2SOmar Sandoval return 0; 1944838fe188SJiri Kosina if (!again) { 1945a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1946a74a4b97SChris Mason schedule(); 1947a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1948a74a4b97SChris Mason } 1949da288d28SFilipe Manana } 1950a74a4b97SChris Mason } 1951a74a4b97SChris Mason 1952a74a4b97SChris Mason static int transaction_kthread(void *arg) 1953a74a4b97SChris Mason { 1954a74a4b97SChris Mason struct btrfs_root *root = arg; 19550b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1956a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1957a74a4b97SChris Mason struct btrfs_transaction *cur; 19588929ecfaSYan, Zheng u64 transid; 1959643900beSNikolay Borisov time64_t delta; 1960a74a4b97SChris Mason unsigned long delay; 1961914b2007SJan Kara bool cannot_commit; 1962a74a4b97SChris Mason 1963a74a4b97SChris Mason do { 1964914b2007SJan Kara cannot_commit = false; 1965ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 19660b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1967a74a4b97SChris Mason 19680b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19690b246afaSJeff Mahoney cur = fs_info->running_transaction; 1970a74a4b97SChris Mason if (!cur) { 19710b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1972a74a4b97SChris Mason goto sleep; 1973a74a4b97SChris Mason } 197431153d81SYan Zheng 1975643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 1976fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 1977fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 1978643900beSNikolay Borisov delta < fs_info->commit_interval) { 19790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1980fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1981fb8a7e94SNikolay Borisov delay = min(delay, 1982fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1983a74a4b97SChris Mason goto sleep; 1984a74a4b97SChris Mason } 19858929ecfaSYan, Zheng transid = cur->transid; 19860b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 198756bec294SChris Mason 198879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1989354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1990914b2007SJan Kara if (IS_ERR(trans)) { 1991354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1992914b2007SJan Kara cannot_commit = true; 199379787eaaSJeff Mahoney goto sleep; 1994914b2007SJan Kara } 19958929ecfaSYan, Zheng if (transid == trans->transid) { 19963a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19978929ecfaSYan, Zheng } else { 19983a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19998929ecfaSYan, Zheng } 2000a74a4b97SChris Mason sleep: 20010b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 20020b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 2003a74a4b97SChris Mason 200484961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 20052ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 20068929ecfaSYan, Zheng if (!kthread_should_stop() && 20070b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 2008914b2007SJan Kara cannot_commit)) 2009bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 2010a74a4b97SChris Mason } while (!kthread_should_stop()); 2011a74a4b97SChris Mason return 0; 2012a74a4b97SChris Mason } 2013a74a4b97SChris Mason 2014af31f5e5SChris Mason /* 201501f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 201601f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 201701f0f9daSNikolay Borisov * anything. 2018af31f5e5SChris Mason * 2019af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2020af31f5e5SChris Mason * generation of the latest root in the array with the generation 2021af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2022af31f5e5SChris Mason */ 202301f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 2024af31f5e5SChris Mason { 202501f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 2026af31f5e5SChris Mason u64 cur; 2027af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2028af31f5e5SChris Mason int i; 2029af31f5e5SChris Mason 2030af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2031af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2032af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2033af31f5e5SChris Mason if (cur == newest_gen) 203401f0f9daSNikolay Borisov return i; 2035af31f5e5SChris Mason } 2036af31f5e5SChris Mason 203701f0f9daSNikolay Borisov return -EINVAL; 2038af31f5e5SChris Mason } 2039af31f5e5SChris Mason 2040af31f5e5SChris Mason /* 2041af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2042af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2043af31f5e5SChris Mason * done 2044af31f5e5SChris Mason */ 2045af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2046af31f5e5SChris Mason { 20476ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2048af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2049af31f5e5SChris Mason 2050af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2051af31f5e5SChris Mason 2052af31f5e5SChris Mason /* 2053af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2054af31f5e5SChris Mason * regardless of which ones we use today 2055af31f5e5SChris Mason */ 2056af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2057af31f5e5SChris Mason 2058af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2059af31f5e5SChris Mason 2060af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2061af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2062af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2063af31f5e5SChris Mason 2064af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2065af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2066af31f5e5SChris Mason 2067af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2068af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2069af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2070af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2071af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2072af31f5e5SChris Mason 20731c56ab99SQu Wenruo if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) { 20749c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2075f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 20769c54e80dSJosef Bacik 20779c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 20789c54e80dSJosef Bacik extent_root->node->start); 2079af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 208029cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2081af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 208229cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2083af31f5e5SChris Mason 2084f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2085f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2086f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2087f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2088f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 20899c54e80dSJosef Bacik } 2090af31f5e5SChris Mason 20917c7e82a7SChris Mason /* 20927c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20937c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20947c7e82a7SChris Mason */ 20957c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20967c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20977c7e82a7SChris Mason info->fs_root->node->start); 2098af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2099af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2100af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2101af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21027c7e82a7SChris Mason } 2103af31f5e5SChris Mason 2104af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2105af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2106af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2107af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2108af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2109af31f5e5SChris Mason 2110af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2111af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2112af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2113af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2114af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2115af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2116af31f5e5SChris Mason 2117af31f5e5SChris Mason /* 2118af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2119af31f5e5SChris Mason * for the next commit 2120af31f5e5SChris Mason */ 2121af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2122af31f5e5SChris Mason &info->super_for_commit->super_roots, 2123af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2124af31f5e5SChris Mason } 2125af31f5e5SChris Mason 2126af31f5e5SChris Mason /* 2127bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2128bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2129bd2336b2SNikolay Borisov * 2130bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2131bd2336b2SNikolay Borisov * priority - priority of backup root required 2132bd2336b2SNikolay Borisov * 2133bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2134bd2336b2SNikolay Borisov */ 2135bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2136bd2336b2SNikolay Borisov { 2137bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2138bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2139bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2140bd2336b2SNikolay Borisov 2141bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2142bd2336b2SNikolay Borisov if (priority == 0) 2143bd2336b2SNikolay Borisov return backup_index; 2144bd2336b2SNikolay Borisov 2145bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2146bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2147bd2336b2SNikolay Borisov } else { 2148bd2336b2SNikolay Borisov return -EINVAL; 2149bd2336b2SNikolay Borisov } 2150bd2336b2SNikolay Borisov 2151bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2152bd2336b2SNikolay Borisov 2153bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2154bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2155bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2156bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2157bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2158bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2159bd2336b2SNikolay Borisov 2160bd2336b2SNikolay Borisov /* 2161bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2162bd2336b2SNikolay Borisov * need a fsck 2163bd2336b2SNikolay Borisov */ 2164bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2165bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2166bd2336b2SNikolay Borisov 2167bd2336b2SNikolay Borisov return backup_index; 2168bd2336b2SNikolay Borisov } 2169bd2336b2SNikolay Borisov 21707abadb64SLiu Bo /* helper to cleanup workers */ 21717abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21727abadb64SLiu Bo { 2173dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2174afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 2175a31b4a43SChristoph Hellwig btrfs_destroy_workqueue(fs_info->hipri_workers); 21765cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2177d7b9416fSChristoph Hellwig if (fs_info->endio_workers) 2178d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_workers); 2179385de0efSChristoph Hellwig if (fs_info->rmw_workers) 2180385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers); 2181fed8a72dSChristoph Hellwig if (fs_info->compressed_write_workers) 2182fed8a72dSChristoph Hellwig destroy_workqueue(fs_info->compressed_write_workers); 2183fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2184fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 21855b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2186e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2187a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2188fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2189b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2190b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2191a9b9477dSFilipe Manana /* 2192a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2193a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2194a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2195a9b9477dSFilipe Manana */ 2196d7b9416fSChristoph Hellwig if (fs_info->endio_meta_workers) 2197d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_meta_workers); 21987abadb64SLiu Bo } 21997abadb64SLiu Bo 22002e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22012e9f5954SRashika { 22022e9f5954SRashika if (root) { 22032e9f5954SRashika free_extent_buffer(root->node); 22042e9f5954SRashika free_extent_buffer(root->commit_root); 22052e9f5954SRashika root->node = NULL; 22062e9f5954SRashika root->commit_root = NULL; 22072e9f5954SRashika } 22082e9f5954SRashika } 22092e9f5954SRashika 2210abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2211abed4aaaSJosef Bacik { 2212abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2213abed4aaaSJosef Bacik 2214abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2215abed4aaaSJosef Bacik &fs_info->global_root_tree, 2216abed4aaaSJosef Bacik rb_node) 2217abed4aaaSJosef Bacik free_root_extent_buffers(root); 2218abed4aaaSJosef Bacik } 2219abed4aaaSJosef Bacik 2220af31f5e5SChris Mason /* helper to cleanup tree roots */ 22214273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2222af31f5e5SChris Mason { 22232e9f5954SRashika free_root_extent_buffers(info->tree_root); 2224655b09feSJosef Bacik 2225abed4aaaSJosef Bacik free_global_root_pointers(info); 22262e9f5954SRashika free_root_extent_buffers(info->dev_root); 22272e9f5954SRashika free_root_extent_buffers(info->quota_root); 22282e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22298c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2230aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 22319c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 22324273eaffSAnand Jain if (free_chunk_root) 22332e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2234af31f5e5SChris Mason } 2235af31f5e5SChris Mason 22368c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 22378c38938cSJosef Bacik { 22388c38938cSJosef Bacik if (!root) 22398c38938cSJosef Bacik return; 22408c38938cSJosef Bacik 22418c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 22428c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 22431dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 22448c38938cSJosef Bacik if (root->anon_dev) 22458c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 22468c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2247923eb523SJohannes Thumshirn free_root_extent_buffers(root); 22488c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2249fc7cbcd4SDavid Sterba spin_lock(&root->fs_info->fs_roots_radix_lock); 22508c38938cSJosef Bacik list_del_init(&root->leak_list); 2251fc7cbcd4SDavid Sterba spin_unlock(&root->fs_info->fs_roots_radix_lock); 22528c38938cSJosef Bacik #endif 22538c38938cSJosef Bacik kfree(root); 22548c38938cSJosef Bacik } 22558c38938cSJosef Bacik } 22568c38938cSJosef Bacik 2257faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2258171f6537SJosef Bacik { 2259fc7cbcd4SDavid Sterba int ret; 2260fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 2261fc7cbcd4SDavid Sterba int i; 2262171f6537SJosef Bacik 2263171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2264fc7cbcd4SDavid Sterba gang[0] = list_entry(fs_info->dead_roots.next, 2265171f6537SJosef Bacik struct btrfs_root, root_list); 2266fc7cbcd4SDavid Sterba list_del(&gang[0]->root_list); 2267171f6537SJosef Bacik 2268fc7cbcd4SDavid Sterba if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2269fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2270fc7cbcd4SDavid Sterba btrfs_put_root(gang[0]); 2271171f6537SJosef Bacik } 2272171f6537SJosef Bacik 2273fc7cbcd4SDavid Sterba while (1) { 2274fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2275fc7cbcd4SDavid Sterba (void **)gang, 0, 2276fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 2277fc7cbcd4SDavid Sterba if (!ret) 2278fc7cbcd4SDavid Sterba break; 2279fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 2280fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2281171f6537SJosef Bacik } 2282171f6537SJosef Bacik } 2283af31f5e5SChris Mason 2284638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2285638aa7edSEric Sandeen { 2286638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2287638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2288638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2289638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2290638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2291638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2292ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2293638aa7edSEric Sandeen } 2294638aa7edSEric Sandeen 2295779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2296779a65a4SEric Sandeen { 2297779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2298779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2299779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2300779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2301779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2302779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2303907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2304779a65a4SEric Sandeen } 2305779a65a4SEric Sandeen 23066bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2307f37938e0SEric Sandeen { 23082ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 2309e256927bSJosef Bacik unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID, 2310e256927bSJosef Bacik fs_info->tree_root); 23112ff7e61eSJeff Mahoney 23122ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23132ff7e61eSJeff Mahoney set_nlink(inode, 1); 2314f37938e0SEric Sandeen /* 2315f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2316f37938e0SEric Sandeen * the real end of the address space is determined by all of 2317f37938e0SEric Sandeen * the devices in the system 2318f37938e0SEric Sandeen */ 23192ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23202ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2321f37938e0SEric Sandeen 23222ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 232343eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 232435da5a7eSDavid Sterba IO_TREE_BTREE_INODE_IO); 23252ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2326f37938e0SEric Sandeen 23275c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 2328adac5584SFilipe Manana BTRFS_I(inode)->location.objectid = BTRFS_BTREE_INODE_OBJECTID; 2329adac5584SFilipe Manana BTRFS_I(inode)->location.type = 0; 2330adac5584SFilipe Manana BTRFS_I(inode)->location.offset = 0; 23312ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 2332e256927bSJosef Bacik __insert_inode_hash(inode, hash); 2333f37938e0SEric Sandeen } 2334f37938e0SEric Sandeen 2335ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2336ad618368SEric Sandeen { 2337ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2338129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 23397f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2340ad618368SEric Sandeen } 2341ad618368SEric Sandeen 2342f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2343f9e92e40SEric Sandeen { 2344f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2345f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2346f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2347f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2348f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2349f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2350d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2351011b46c3SQu Wenruo fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL; 2352f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2353f9e92e40SEric Sandeen } 2354f9e92e40SEric Sandeen 2355d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 23562a458198SEric Sandeen { 2357f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 23586f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23592a458198SEric Sandeen 23602a458198SEric Sandeen fs_info->workers = 2361a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16); 2362a31b4a43SChristoph Hellwig fs_info->hipri_workers = 2363a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker-high", 2364cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23652a458198SEric Sandeen 23662a458198SEric Sandeen fs_info->delalloc_workers = 2367cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2368cb001095SJeff Mahoney flags, max_active, 2); 23692a458198SEric Sandeen 23702a458198SEric Sandeen fs_info->flush_workers = 2371cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2372cb001095SJeff Mahoney flags, max_active, 0); 23732a458198SEric Sandeen 23742a458198SEric Sandeen fs_info->caching_workers = 2375cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23762a458198SEric Sandeen 23772a458198SEric Sandeen fs_info->fixup_workers = 2378cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23792a458198SEric Sandeen 23802a458198SEric Sandeen fs_info->endio_workers = 2381d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio", flags, max_active); 23822a458198SEric Sandeen fs_info->endio_meta_workers = 2383d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio-meta", flags, max_active); 2384385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active); 23852a458198SEric Sandeen fs_info->endio_write_workers = 2386cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2387cb001095SJeff Mahoney max_active, 2); 2388fed8a72dSChristoph Hellwig fs_info->compressed_write_workers = 2389fed8a72dSChristoph Hellwig alloc_workqueue("btrfs-compressed-write", flags, max_active); 23902a458198SEric Sandeen fs_info->endio_freespace_worker = 2391cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2392cb001095SJeff Mahoney max_active, 0); 23932a458198SEric Sandeen fs_info->delayed_workers = 2394cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2395cb001095SJeff Mahoney max_active, 0); 23962a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2397cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2398b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2399b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 24002a458198SEric Sandeen 2401a31b4a43SChristoph Hellwig if (!(fs_info->workers && fs_info->hipri_workers && 2402a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers && 24032a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 2404fed8a72dSChristoph Hellwig fs_info->compressed_write_workers && 24051a1a2851SQu Wenruo fs_info->endio_write_workers && 24062a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2407f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2408f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2409b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 24102a458198SEric Sandeen return -ENOMEM; 24112a458198SEric Sandeen } 24122a458198SEric Sandeen 24132a458198SEric Sandeen return 0; 24142a458198SEric Sandeen } 24152a458198SEric Sandeen 24166d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 24176d97c6e3SJohannes Thumshirn { 24186d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2419b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 24206d97c6e3SJohannes Thumshirn 2421b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 24226d97c6e3SJohannes Thumshirn 24236d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 24246d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2425b4e967beSDavid Sterba csum_driver); 24266d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 24276d97c6e3SJohannes Thumshirn } 24286d97c6e3SJohannes Thumshirn 24296d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 24306d97c6e3SJohannes Thumshirn 2431c8a5f8caSDavid Sterba btrfs_info(fs_info, "using %s (%s) checksum algorithm", 2432c8a5f8caSDavid Sterba btrfs_super_csum_name(csum_type), 2433c8a5f8caSDavid Sterba crypto_shash_driver_name(csum_shash)); 24346d97c6e3SJohannes Thumshirn return 0; 24356d97c6e3SJohannes Thumshirn } 24366d97c6e3SJohannes Thumshirn 243763443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 243863443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 243963443bf5SEric Sandeen { 244063443bf5SEric Sandeen int ret; 2441789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 244263443bf5SEric Sandeen struct btrfs_root *log_tree_root; 244363443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 244463443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2445581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 244663443bf5SEric Sandeen 244763443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2448f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 244963443bf5SEric Sandeen return -EIO; 245063443bf5SEric Sandeen } 245163443bf5SEric Sandeen 245296dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 245396dfcb46SJosef Bacik GFP_KERNEL); 245463443bf5SEric Sandeen if (!log_tree_root) 245563443bf5SEric Sandeen return -ENOMEM; 245663443bf5SEric Sandeen 2457789d6a3aSQu Wenruo check.level = level; 2458789d6a3aSQu Wenruo check.transid = fs_info->generation + 1; 2459789d6a3aSQu Wenruo check.owner_root = BTRFS_TREE_LOG_OBJECTID; 2460789d6a3aSQu Wenruo log_tree_root->node = read_tree_block(fs_info, bytenr, &check); 246164c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2462f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24630eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 24648c38938cSJosef Bacik log_tree_root->node = NULL; 246500246528SJosef Bacik btrfs_put_root(log_tree_root); 24660eeff236SLiu Bo return ret; 24674eb150d6SQu Wenruo } 24684eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2469f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 247000246528SJosef Bacik btrfs_put_root(log_tree_root); 247163443bf5SEric Sandeen return -EIO; 247263443bf5SEric Sandeen } 24734eb150d6SQu Wenruo 247463443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 247563443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 247663443bf5SEric Sandeen if (ret) { 24770b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 247863443bf5SEric Sandeen "Failed to recover log tree"); 247900246528SJosef Bacik btrfs_put_root(log_tree_root); 248063443bf5SEric Sandeen return ret; 248163443bf5SEric Sandeen } 248263443bf5SEric Sandeen 2483bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 24846bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 248563443bf5SEric Sandeen if (ret) 248663443bf5SEric Sandeen return ret; 248763443bf5SEric Sandeen } 248863443bf5SEric Sandeen 248963443bf5SEric Sandeen return 0; 249063443bf5SEric Sandeen } 249163443bf5SEric Sandeen 2492abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2493abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2494abed4aaaSJosef Bacik const char *name) 2495abed4aaaSJosef Bacik { 2496abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2497abed4aaaSJosef Bacik struct btrfs_root *root; 2498f7238e50SJosef Bacik u64 max_global_id = 0; 2499abed4aaaSJosef Bacik int ret; 2500abed4aaaSJosef Bacik struct btrfs_key key = { 2501abed4aaaSJosef Bacik .objectid = objectid, 2502abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2503abed4aaaSJosef Bacik .offset = 0, 2504abed4aaaSJosef Bacik }; 2505abed4aaaSJosef Bacik bool found = false; 2506abed4aaaSJosef Bacik 2507abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2508abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2509abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2510abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2511abed4aaaSJosef Bacik return 0; 2512abed4aaaSJosef Bacik } 2513abed4aaaSJosef Bacik 2514abed4aaaSJosef Bacik while (1) { 2515abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2516abed4aaaSJosef Bacik if (ret < 0) 2517abed4aaaSJosef Bacik break; 2518abed4aaaSJosef Bacik 2519abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2520abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2521abed4aaaSJosef Bacik if (ret) { 2522abed4aaaSJosef Bacik if (ret > 0) 2523abed4aaaSJosef Bacik ret = 0; 2524abed4aaaSJosef Bacik break; 2525abed4aaaSJosef Bacik } 2526abed4aaaSJosef Bacik } 2527abed4aaaSJosef Bacik ret = 0; 2528abed4aaaSJosef Bacik 2529abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2530abed4aaaSJosef Bacik if (key.objectid != objectid) 2531abed4aaaSJosef Bacik break; 2532abed4aaaSJosef Bacik btrfs_release_path(path); 2533abed4aaaSJosef Bacik 2534f7238e50SJosef Bacik /* 2535f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2536f7238e50SJosef Bacik * everybody. 2537f7238e50SJosef Bacik */ 2538f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2539f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2540f7238e50SJosef Bacik 2541abed4aaaSJosef Bacik found = true; 2542abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2543abed4aaaSJosef Bacik if (IS_ERR(root)) { 2544abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2545abed4aaaSJosef Bacik ret = PTR_ERR(root); 2546abed4aaaSJosef Bacik break; 2547abed4aaaSJosef Bacik } 2548abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2549abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2550abed4aaaSJosef Bacik if (ret) { 2551abed4aaaSJosef Bacik btrfs_put_root(root); 2552abed4aaaSJosef Bacik break; 2553abed4aaaSJosef Bacik } 2554abed4aaaSJosef Bacik key.offset++; 2555abed4aaaSJosef Bacik } 2556abed4aaaSJosef Bacik btrfs_release_path(path); 2557abed4aaaSJosef Bacik 2558f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2559f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2560f7238e50SJosef Bacik 2561abed4aaaSJosef Bacik if (!found || ret) { 2562abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2563abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2564abed4aaaSJosef Bacik 2565abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2566abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2567abed4aaaSJosef Bacik else 2568abed4aaaSJosef Bacik ret = 0; 2569abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2570abed4aaaSJosef Bacik } 2571abed4aaaSJosef Bacik return ret; 2572abed4aaaSJosef Bacik } 2573abed4aaaSJosef Bacik 2574abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2575abed4aaaSJosef Bacik { 2576abed4aaaSJosef Bacik struct btrfs_path *path; 2577abed4aaaSJosef Bacik int ret = 0; 2578abed4aaaSJosef Bacik 2579abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2580abed4aaaSJosef Bacik if (!path) 2581abed4aaaSJosef Bacik return -ENOMEM; 2582abed4aaaSJosef Bacik 2583abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2584abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2585abed4aaaSJosef Bacik if (ret) 2586abed4aaaSJosef Bacik goto out; 2587abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2588abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2589abed4aaaSJosef Bacik if (ret) 2590abed4aaaSJosef Bacik goto out; 2591abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2592abed4aaaSJosef Bacik goto out; 2593abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2594abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2595abed4aaaSJosef Bacik "free space"); 2596abed4aaaSJosef Bacik out: 2597abed4aaaSJosef Bacik btrfs_free_path(path); 2598abed4aaaSJosef Bacik return ret; 2599abed4aaaSJosef Bacik } 2600abed4aaaSJosef Bacik 26016bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 26024bbcaa64SEric Sandeen { 26036bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2604a4f3d2c4SDavid Sterba struct btrfs_root *root; 26054bbcaa64SEric Sandeen struct btrfs_key location; 26064bbcaa64SEric Sandeen int ret; 26074bbcaa64SEric Sandeen 26086bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 26096bccf3abSJeff Mahoney 2610abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2611abed4aaaSJosef Bacik if (ret) 2612abed4aaaSJosef Bacik return ret; 2613abed4aaaSJosef Bacik 26144bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 26154bbcaa64SEric Sandeen location.offset = 0; 26164bbcaa64SEric Sandeen 26171c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) { 261814033b08SQu Wenruo location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID; 261914033b08SQu Wenruo root = btrfs_read_tree_root(tree_root, &location); 262014033b08SQu Wenruo if (IS_ERR(root)) { 262114033b08SQu Wenruo if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 262214033b08SQu Wenruo ret = PTR_ERR(root); 262314033b08SQu Wenruo goto out; 262414033b08SQu Wenruo } 262514033b08SQu Wenruo } else { 262614033b08SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 262714033b08SQu Wenruo fs_info->block_group_root = root; 262814033b08SQu Wenruo } 262914033b08SQu Wenruo } 263014033b08SQu Wenruo 263114033b08SQu Wenruo location.objectid = BTRFS_DEV_TREE_OBJECTID; 2632a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2633f50f4353SLiu Bo if (IS_ERR(root)) { 263442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2635f50f4353SLiu Bo ret = PTR_ERR(root); 2636f50f4353SLiu Bo goto out; 2637f50f4353SLiu Bo } 263842437a63SJosef Bacik } else { 2639a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2640a4f3d2c4SDavid Sterba fs_info->dev_root = root; 264142437a63SJosef Bacik } 2642820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2643a8d1b164SJohannes Thumshirn ret = btrfs_init_devices_late(fs_info); 2644a8d1b164SJohannes Thumshirn if (ret) 2645a8d1b164SJohannes Thumshirn goto out; 26464bbcaa64SEric Sandeen 2647aeb935a4SQu Wenruo /* 2648aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2649aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2650aeb935a4SQu Wenruo */ 265156e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 265256e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2653aeb935a4SQu Wenruo if (IS_ERR(root)) { 265442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2655aeb935a4SQu Wenruo ret = PTR_ERR(root); 2656aeb935a4SQu Wenruo goto out; 2657aeb935a4SQu Wenruo } 265842437a63SJosef Bacik } else { 2659aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2660aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 266142437a63SJosef Bacik } 2662aeb935a4SQu Wenruo 26634bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2664a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2665a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2666a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2667afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2668a4f3d2c4SDavid Sterba fs_info->quota_root = root; 26694bbcaa64SEric Sandeen } 26704bbcaa64SEric Sandeen 26714bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2672a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2673a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 267442437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2675a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 26764bbcaa64SEric Sandeen if (ret != -ENOENT) 2677f50f4353SLiu Bo goto out; 267842437a63SJosef Bacik } 26794bbcaa64SEric Sandeen } else { 2680a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2681a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 26824bbcaa64SEric Sandeen } 26834bbcaa64SEric Sandeen 26844bbcaa64SEric Sandeen return 0; 2685f50f4353SLiu Bo out: 2686f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2687f50f4353SLiu Bo location.objectid, ret); 2688f50f4353SLiu Bo return ret; 26894bbcaa64SEric Sandeen } 26904bbcaa64SEric Sandeen 2691069ec957SQu Wenruo /* 2692069ec957SQu Wenruo * Real super block validation 2693069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2694069ec957SQu Wenruo * 2695069ec957SQu Wenruo * @sb: super block to check 2696069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2697069ec957SQu Wenruo * 0 the primary (1st) sb 2698069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2699069ec957SQu Wenruo * -1 skip bytenr check 2700069ec957SQu Wenruo */ 2701a05d3c91SQu Wenruo int btrfs_validate_super(struct btrfs_fs_info *fs_info, 2702069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 270321a852b0SQu Wenruo { 270421a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 270521a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 270621a852b0SQu Wenruo int ret = 0; 270721a852b0SQu Wenruo 270821a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 270921a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 271021a852b0SQu Wenruo ret = -EINVAL; 271121a852b0SQu Wenruo } 271221a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 271321a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 271421a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 271521a852b0SQu Wenruo ret = -EINVAL; 271621a852b0SQu Wenruo } 271721a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 271821a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 271921a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 272021a852b0SQu Wenruo ret = -EINVAL; 272121a852b0SQu Wenruo } 272221a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 272321a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 272421a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 272521a852b0SQu Wenruo ret = -EINVAL; 272621a852b0SQu Wenruo } 272721a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 272821a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 272921a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 273021a852b0SQu Wenruo ret = -EINVAL; 273121a852b0SQu Wenruo } 273221a852b0SQu Wenruo 273321a852b0SQu Wenruo /* 273421a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 273521a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 273621a852b0SQu Wenruo */ 273721a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 273821a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 273921a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 274021a852b0SQu Wenruo ret = -EINVAL; 274121a852b0SQu Wenruo } 27420bb3eb3eSQu Wenruo 27430bb3eb3eSQu Wenruo /* 27441a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 27451a42daabSQu Wenruo * 27461a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 27471a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 27481a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 27491a42daabSQu Wenruo * beginning. 27500bb3eb3eSQu Wenruo */ 27511a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 275221a852b0SQu Wenruo btrfs_err(fs_info, 27530bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 275421a852b0SQu Wenruo sectorsize, PAGE_SIZE); 275521a852b0SQu Wenruo ret = -EINVAL; 275621a852b0SQu Wenruo } 27570bb3eb3eSQu Wenruo 275821a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 275921a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 276021a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 276121a852b0SQu Wenruo ret = -EINVAL; 276221a852b0SQu Wenruo } 276321a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 276421a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 276521a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 276621a852b0SQu Wenruo ret = -EINVAL; 276721a852b0SQu Wenruo } 276821a852b0SQu Wenruo 276921a852b0SQu Wenruo /* Root alignment check */ 277021a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 277121a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 277221a852b0SQu Wenruo btrfs_super_root(sb)); 277321a852b0SQu Wenruo ret = -EINVAL; 277421a852b0SQu Wenruo } 277521a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 277621a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 277721a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 277821a852b0SQu Wenruo ret = -EINVAL; 277921a852b0SQu Wenruo } 278021a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 278121a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 278221a852b0SQu Wenruo btrfs_super_log_root(sb)); 278321a852b0SQu Wenruo ret = -EINVAL; 278421a852b0SQu Wenruo } 278521a852b0SQu Wenruo 2786aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2787aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2788aefd7f70SNikolay Borisov btrfs_err(fs_info, 2789aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2790aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2791aefd7f70SNikolay Borisov ret = -EINVAL; 2792aefd7f70SNikolay Borisov } 2793aefd7f70SNikolay Borisov 2794aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2795aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2796aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2797aefd7f70SNikolay Borisov btrfs_err(fs_info, 2798aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2799aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2800aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2801aefd7f70SNikolay Borisov ret = -EINVAL; 2802aefd7f70SNikolay Borisov } 2803aefd7f70SNikolay Borisov 28041c56ab99SQu Wenruo /* 28051c56ab99SQu Wenruo * Artificial requirement for block-group-tree to force newer features 28061c56ab99SQu Wenruo * (free-space-tree, no-holes) so the test matrix is smaller. 28071c56ab99SQu Wenruo */ 28081c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) && 28091c56ab99SQu Wenruo (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) || 28101c56ab99SQu Wenruo !btrfs_fs_incompat(fs_info, NO_HOLES))) { 28111c56ab99SQu Wenruo btrfs_err(fs_info, 28121c56ab99SQu Wenruo "block-group-tree feature requires fres-space-tree and no-holes"); 28131c56ab99SQu Wenruo ret = -EINVAL; 28141c56ab99SQu Wenruo } 28151c56ab99SQu Wenruo 2816de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 28177239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 281821a852b0SQu Wenruo btrfs_err(fs_info, 28197239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2820de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 282121a852b0SQu Wenruo ret = -EINVAL; 282221a852b0SQu Wenruo } 282321a852b0SQu Wenruo 282421a852b0SQu Wenruo /* 282521a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 282621a852b0SQu Wenruo * done later 282721a852b0SQu Wenruo */ 282821a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 282921a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 283021a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 283121a852b0SQu Wenruo ret = -EINVAL; 283221a852b0SQu Wenruo } 283321a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 283421a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 283521a852b0SQu Wenruo btrfs_super_stripesize(sb)); 283621a852b0SQu Wenruo ret = -EINVAL; 283721a852b0SQu Wenruo } 283821a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 283921a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 284021a852b0SQu Wenruo btrfs_super_num_devices(sb)); 284121a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 284221a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 284321a852b0SQu Wenruo ret = -EINVAL; 284421a852b0SQu Wenruo } 284521a852b0SQu Wenruo 2846069ec957SQu Wenruo if (mirror_num >= 0 && 2847069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 284821a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 284921a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 285021a852b0SQu Wenruo ret = -EINVAL; 285121a852b0SQu Wenruo } 285221a852b0SQu Wenruo 285321a852b0SQu Wenruo /* 285421a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 285521a852b0SQu Wenruo * and one chunk 285621a852b0SQu Wenruo */ 285721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 285821a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 285921a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 286021a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 286121a852b0SQu Wenruo ret = -EINVAL; 286221a852b0SQu Wenruo } 286321a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 286421a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 286521a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 286621a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 286721a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 286821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 286921a852b0SQu Wenruo ret = -EINVAL; 287021a852b0SQu Wenruo } 287121a852b0SQu Wenruo 287221a852b0SQu Wenruo /* 287321a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 287421a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 287521a852b0SQu Wenruo */ 287621a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 287721a852b0SQu Wenruo btrfs_warn(fs_info, 287821a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 287921a852b0SQu Wenruo btrfs_super_generation(sb), 288021a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 288121a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 288221a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 288321a852b0SQu Wenruo btrfs_warn(fs_info, 288421a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 288521a852b0SQu Wenruo btrfs_super_generation(sb), 288621a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 288721a852b0SQu Wenruo 288821a852b0SQu Wenruo return ret; 288921a852b0SQu Wenruo } 289021a852b0SQu Wenruo 2891069ec957SQu Wenruo /* 2892069ec957SQu Wenruo * Validation of super block at mount time. 2893069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2894069ec957SQu Wenruo * flags will be skipped. 2895069ec957SQu Wenruo */ 2896069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2897069ec957SQu Wenruo { 2898a05d3c91SQu Wenruo return btrfs_validate_super(fs_info, fs_info->super_copy, 0); 2899069ec957SQu Wenruo } 2900069ec957SQu Wenruo 290175cb857dSQu Wenruo /* 290275cb857dSQu Wenruo * Validation of super block at write time. 290375cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 290475cb857dSQu Wenruo * overwritten soon. 290575cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 290675cb857dSQu Wenruo */ 290775cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 290875cb857dSQu Wenruo struct btrfs_super_block *sb) 290975cb857dSQu Wenruo { 291075cb857dSQu Wenruo int ret; 291175cb857dSQu Wenruo 2912a05d3c91SQu Wenruo ret = btrfs_validate_super(fs_info, sb, -1); 291375cb857dSQu Wenruo if (ret < 0) 291475cb857dSQu Wenruo goto out; 2915e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 291675cb857dSQu Wenruo ret = -EUCLEAN; 291775cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 291875cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 291975cb857dSQu Wenruo goto out; 292075cb857dSQu Wenruo } 292175cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 292275cb857dSQu Wenruo ret = -EUCLEAN; 292375cb857dSQu Wenruo btrfs_err(fs_info, 292475cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 292575cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 292675cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 292775cb857dSQu Wenruo goto out; 292875cb857dSQu Wenruo } 292975cb857dSQu Wenruo out: 293075cb857dSQu Wenruo if (ret < 0) 293175cb857dSQu Wenruo btrfs_err(fs_info, 293275cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 293375cb857dSQu Wenruo return ret; 293475cb857dSQu Wenruo } 293575cb857dSQu Wenruo 2936bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2937bd676446SJosef Bacik { 2938789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 2939789d6a3aSQu Wenruo .level = level, 2940789d6a3aSQu Wenruo .transid = gen, 2941789d6a3aSQu Wenruo .owner_root = root->root_key.objectid 2942789d6a3aSQu Wenruo }; 2943bd676446SJosef Bacik int ret = 0; 2944bd676446SJosef Bacik 2945789d6a3aSQu Wenruo root->node = read_tree_block(root->fs_info, bytenr, &check); 2946bd676446SJosef Bacik if (IS_ERR(root->node)) { 2947bd676446SJosef Bacik ret = PTR_ERR(root->node); 2948bd676446SJosef Bacik root->node = NULL; 29494eb150d6SQu Wenruo return ret; 29504eb150d6SQu Wenruo } 29514eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 2952bd676446SJosef Bacik free_extent_buffer(root->node); 2953bd676446SJosef Bacik root->node = NULL; 29544eb150d6SQu Wenruo return -EIO; 2955bd676446SJosef Bacik } 2956bd676446SJosef Bacik 2957bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 2958bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 2959bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 2960bd676446SJosef Bacik return ret; 2961bd676446SJosef Bacik } 2962bd676446SJosef Bacik 2963bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 2964bd676446SJosef Bacik { 2965bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 2966bd676446SJosef Bacik u64 gen, bytenr; 2967bd676446SJosef Bacik int level, ret; 2968bd676446SJosef Bacik 2969bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 2970bd676446SJosef Bacik gen = btrfs_super_generation(sb); 2971bd676446SJosef Bacik level = btrfs_super_root_level(sb); 2972bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 29739c54e80dSJosef Bacik if (ret) { 2974bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 2975bd676446SJosef Bacik return ret; 2976bd676446SJosef Bacik } 29779c54e80dSJosef Bacik return 0; 29789c54e80dSJosef Bacik } 29799c54e80dSJosef Bacik 29806ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2981b8522a1eSNikolay Borisov { 29826ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2983b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2984b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2985b8522a1eSNikolay Borisov bool handle_error = false; 2986b8522a1eSNikolay Borisov int ret = 0; 2987b8522a1eSNikolay Borisov int i; 2988b8522a1eSNikolay Borisov 2989b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2990b8522a1eSNikolay Borisov if (handle_error) { 2991b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2992b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2993b8522a1eSNikolay Borisov tree_root->node = NULL; 2994b8522a1eSNikolay Borisov 2995b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2996b8522a1eSNikolay Borisov break; 2997b8522a1eSNikolay Borisov 2998b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2999b8522a1eSNikolay Borisov 3000b8522a1eSNikolay Borisov /* 3001b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 3002b8522a1eSNikolay Borisov * valid 3003b8522a1eSNikolay Borisov */ 3004b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 3005b8522a1eSNikolay Borisov 3006b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 3007b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3008b8522a1eSNikolay Borisov 3009b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 30106ef108ddSNikolay Borisov backup_index = ret; 3011b8522a1eSNikolay Borisov if (ret < 0) 3012b8522a1eSNikolay Borisov return ret; 3013b8522a1eSNikolay Borisov } 3014b8522a1eSNikolay Borisov 3015bd676446SJosef Bacik ret = load_important_roots(fs_info); 3016bd676446SJosef Bacik if (ret) { 3017217f5004SNikolay Borisov handle_error = true; 3018b8522a1eSNikolay Borisov continue; 3019b8522a1eSNikolay Borisov } 3020b8522a1eSNikolay Borisov 3021336a0d8dSNikolay Borisov /* 3022336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 3023336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 3024336a0d8dSNikolay Borisov */ 3025453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 3026b8522a1eSNikolay Borisov if (ret < 0) { 3027b8522a1eSNikolay Borisov handle_error = true; 3028b8522a1eSNikolay Borisov continue; 3029b8522a1eSNikolay Borisov } 3030b8522a1eSNikolay Borisov 30316b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3032b8522a1eSNikolay Borisov 3033b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3034b8522a1eSNikolay Borisov if (ret < 0) { 3035b8522a1eSNikolay Borisov handle_error = true; 3036b8522a1eSNikolay Borisov continue; 3037b8522a1eSNikolay Borisov } 3038b8522a1eSNikolay Borisov 3039b8522a1eSNikolay Borisov /* All successful */ 3040bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3041bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3042d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 30436ef108ddSNikolay Borisov 30446ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 30456ef108ddSNikolay Borisov if (backup_index < 0) { 30466ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 30476ef108ddSNikolay Borisov } else { 30486ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 30496ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 30506ef108ddSNikolay Borisov } 3051b8522a1eSNikolay Borisov break; 3052b8522a1eSNikolay Borisov } 3053b8522a1eSNikolay Borisov 3054b8522a1eSNikolay Borisov return ret; 3055b8522a1eSNikolay Borisov } 3056b8522a1eSNikolay Borisov 30578260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3058eb60ceacSChris Mason { 3059fc7cbcd4SDavid Sterba INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 306001cd3909SDavid Sterba INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 30618fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3062facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 306324bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3064eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 306511833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3066eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3067a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 3068fc7cbcd4SDavid Sterba spin_lock_init(&fs_info->fs_roots_radix_lock); 306924bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 30704cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3071ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3072f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 307347ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 307440ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3075afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3076c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3077f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3078abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3079d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3080f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 30817585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3082573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 30830bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 30845f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3085de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 308619c00ddcSChris Mason 3087e1489b4fSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers); 30885a9ba670SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters); 30898b53779eSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered); 30905f4403e1SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent); 30913e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_start, 30923e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_COMMIT_START); 30933e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked, 30943e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_UNBLOCKED); 30953e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed, 30963e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED); 30973e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed, 30983e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_COMPLETED); 3099e1489b4fSIoannis Angelakopoulos 31000b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 31016324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3102f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 310347ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 310418bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3105afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3106bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3107bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 31083fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 31093fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3110bd647ce3SJosef Bacik #endif 3111c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 311266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 311366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 311466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 311566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 311666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 311766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 311866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3119ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3120ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3121ba2c4d4eSJosef Bacik 3122771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 31234cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3124034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3125fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3126abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 312795ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 31289ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 31294cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3130a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3131f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 31328b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3133f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3134fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3135c8b97818SChris Mason 3136b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3137b34b086cSChris Mason num_online_cpus() + 2, 8); 31380afbaf8cSChris Mason 3139199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3140199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 314169fe2d75SJosef Bacik 3142638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 314321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 314421adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 314521adbd5cSStefan Behrens #endif 3146779a65a4SEric Sandeen btrfs_init_balance(fs_info); 314757056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3148a2de733cSArne Jansen 314916b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock); 315008dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED; 31510f9dd46cSJosef Bacik 3152fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 315335da5a7eSDavid Sterba IO_TREE_FS_EXCLUDED_EXTENTS); 315439279cc3SChris Mason 31555a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3156e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3157925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3158a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3159a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 31601bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 31619e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3162c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 316376dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3164803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3165fa9c0d79SChris Mason 3166ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3167f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3168b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3169416ac51dSArne Jansen 3170fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3171fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3172fa9c0d79SChris Mason 3173e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3174f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3175bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 31764854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3177034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 31783768f368SChris Mason 3179da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3180da17066cSJeff Mahoney fs_info->nodesize = 4096; 3181da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3182ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3183da17066cSJeff Mahoney fs_info->stripesize = 4096; 3184da17066cSJeff Mahoney 3185f7b12a62SNaohiro Aota fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE; 3186f7b12a62SNaohiro Aota 3187eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3188eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3189eede2bf3SOmar Sandoval 319018bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 319118bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 31928260edbaSJosef Bacik } 31938260edbaSJosef Bacik 31948260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 31958260edbaSJosef Bacik { 31968260edbaSJosef Bacik int ret; 31978260edbaSJosef Bacik 31988260edbaSJosef Bacik fs_info->sb = sb; 31998260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 32008260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 32019e967495SFilipe Manana 32025deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3203ae18c37aSJosef Bacik if (ret) 3204ae18c37aSJosef Bacik return ret; 3205ae18c37aSJosef Bacik 3206ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3207ae18c37aSJosef Bacik if (ret) 3208c75e8394SJosef Bacik return ret; 3209ae18c37aSJosef Bacik 3210ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3211ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3212ae18c37aSJosef Bacik 3213ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3214ae18c37aSJosef Bacik if (ret) 3215c75e8394SJosef Bacik return ret; 3216ae18c37aSJosef Bacik 3217ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3218ae18c37aSJosef Bacik GFP_KERNEL); 3219ae18c37aSJosef Bacik if (ret) 3220c75e8394SJosef Bacik return ret; 3221ae18c37aSJosef Bacik 3222ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3223ae18c37aSJosef Bacik GFP_KERNEL); 3224c75e8394SJosef Bacik if (!fs_info->delayed_root) 3225c75e8394SJosef Bacik return -ENOMEM; 3226ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3227ae18c37aSJosef Bacik 3228a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3229a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3230a0a1db70SFilipe Manana 3231c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3232ae18c37aSJosef Bacik } 3233ae18c37aSJosef Bacik 323497f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 323597f4dd09SNikolay Borisov { 32360d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 323797f4dd09SNikolay Borisov int ret; 323897f4dd09SNikolay Borisov 323997f4dd09SNikolay Borisov /* 324097f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 324197f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 324297f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 324397f4dd09SNikolay Borisov */ 324497f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 324597f4dd09SNikolay Borisov if (ret < 0) { 3246c94bec2cSJosef Bacik if (ret != -EINTR) 3247c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3248c94bec2cSJosef Bacik ret); 324997f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 325097f4dd09SNikolay Borisov return ret; 325197f4dd09SNikolay Borisov } 325297f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 325397f4dd09SNikolay Borisov } 325497f4dd09SNikolay Borisov 325597f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 325697f4dd09SNikolay Borisov { 325797f4dd09SNikolay Borisov struct task_struct *task; 325897f4dd09SNikolay Borisov 325997f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 326097f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 326197f4dd09SNikolay Borisov if (IS_ERR(task)) { 326297f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 326397f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 326497f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 326597f4dd09SNikolay Borisov return PTR_ERR(task); 326697f4dd09SNikolay Borisov } 326797f4dd09SNikolay Borisov 326897f4dd09SNikolay Borisov return 0; 326997f4dd09SNikolay Borisov } 327097f4dd09SNikolay Borisov 327144c0ca21SBoris Burkov /* 32728cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 32738cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 32748cd29088SBoris Burkov * code paths. 32758cd29088SBoris Burkov */ 32768cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 32778cd29088SBoris Burkov { 32788cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32798b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 32808cd29088SBoris Burkov } 32818cd29088SBoris Burkov 32828cd29088SBoris Burkov /* 328344c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 328444c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 328544c0ca21SBoris Burkov */ 328644c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 328744c0ca21SBoris Burkov { 328844c0ca21SBoris Burkov int ret; 328994846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 32908b228324SBoris Burkov bool clear_free_space_tree = false; 32918b228324SBoris Burkov 32928b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 32938b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32948b228324SBoris Burkov clear_free_space_tree = true; 32958b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32968b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32978b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 32988b228324SBoris Burkov clear_free_space_tree = true; 32998b228324SBoris Burkov } 33008b228324SBoris Burkov 33018b228324SBoris Burkov if (clear_free_space_tree) { 33028b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 33038b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 33048b228324SBoris Burkov if (ret) { 33058b228324SBoris Burkov btrfs_warn(fs_info, 33068b228324SBoris Burkov "failed to clear free space tree: %d", ret); 33078b228324SBoris Burkov goto out; 33088b228324SBoris Burkov } 33098b228324SBoris Burkov } 331044c0ca21SBoris Burkov 33118d488a8cSFilipe Manana /* 33128d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 33138d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 3314fc7cbcd4SDavid Sterba * them into the fs_info->fs_roots_radix tree. This must be done before 33158d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 33168d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 33178d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 33188d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 33198d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 33208d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 33218d488a8cSFilipe Manana */ 33228d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 33238d488a8cSFilipe Manana if (ret) 33248d488a8cSFilipe Manana goto out; 33258d488a8cSFilipe Manana 332644c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 332744c0ca21SBoris Burkov if (ret) 332844c0ca21SBoris Burkov goto out; 332944c0ca21SBoris Burkov 33308f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 33318f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33328f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33338f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33348f1c21d7SBoris Burkov goto out; 33358f1c21d7SBoris Burkov } 33368f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33378f1c21d7SBoris Burkov 333844c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 33397eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 334044c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 334144c0ca21SBoris Burkov if (ret < 0) { 334244c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 334344c0ca21SBoris Burkov goto out; 334444c0ca21SBoris Burkov } 334544c0ca21SBoris Burkov 33465011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 33475011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33485011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 33495011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 33505011139aSBoris Burkov if (ret) { 33515011139aSBoris Burkov btrfs_warn(fs_info, 33525011139aSBoris Burkov "failed to create free space tree: %d", ret); 33535011139aSBoris Burkov goto out; 33545011139aSBoris Burkov } 33555011139aSBoris Burkov } 33565011139aSBoris Burkov 335794846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 335894846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 335994846229SBoris Burkov if (ret) 336094846229SBoris Burkov goto out; 336194846229SBoris Burkov } 336294846229SBoris Burkov 336344c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 336444c0ca21SBoris Burkov if (ret) 336544c0ca21SBoris Burkov goto out; 336644c0ca21SBoris Burkov 336744c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 336844c0ca21SBoris Burkov if (ret) { 336944c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 337044c0ca21SBoris Burkov goto out; 337144c0ca21SBoris Burkov } 337244c0ca21SBoris Burkov 337344c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 337444c0ca21SBoris Burkov 337544c0ca21SBoris Burkov if (!fs_info->uuid_root) { 337644c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 337744c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 337844c0ca21SBoris Burkov if (ret) { 337944c0ca21SBoris Burkov btrfs_warn(fs_info, 338044c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 338144c0ca21SBoris Burkov goto out; 338244c0ca21SBoris Burkov } 338344c0ca21SBoris Burkov } 338444c0ca21SBoris Burkov 338544c0ca21SBoris Burkov out: 338644c0ca21SBoris Burkov return ret; 338744c0ca21SBoris Burkov } 338844c0ca21SBoris Burkov 3389d7f67ac9SQu Wenruo /* 3390d7f67ac9SQu Wenruo * Do various sanity and dependency checks of different features. 3391d7f67ac9SQu Wenruo * 33922ba48b20SQu Wenruo * @is_rw_mount: If the mount is read-write. 33932ba48b20SQu Wenruo * 3394d7f67ac9SQu Wenruo * This is the place for less strict checks (like for subpage or artificial 3395d7f67ac9SQu Wenruo * feature dependencies). 3396d7f67ac9SQu Wenruo * 3397d7f67ac9SQu Wenruo * For strict checks or possible corruption detection, see 3398d7f67ac9SQu Wenruo * btrfs_validate_super(). 3399d7f67ac9SQu Wenruo * 3400d7f67ac9SQu Wenruo * This should be called after btrfs_parse_options(), as some mount options 3401d7f67ac9SQu Wenruo * (space cache related) can modify on-disk format like free space tree and 3402d7f67ac9SQu Wenruo * screw up certain feature dependencies. 3403d7f67ac9SQu Wenruo */ 34042ba48b20SQu Wenruo int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount) 3405d7f67ac9SQu Wenruo { 3406d7f67ac9SQu Wenruo struct btrfs_super_block *disk_super = fs_info->super_copy; 3407d7f67ac9SQu Wenruo u64 incompat = btrfs_super_incompat_flags(disk_super); 3408d7f67ac9SQu Wenruo const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super); 3409d7f67ac9SQu Wenruo const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP); 3410d7f67ac9SQu Wenruo 3411d7f67ac9SQu Wenruo if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 3412d7f67ac9SQu Wenruo btrfs_err(fs_info, 3413d7f67ac9SQu Wenruo "cannot mount because of unknown incompat features (0x%llx)", 3414d7f67ac9SQu Wenruo incompat); 3415d7f67ac9SQu Wenruo return -EINVAL; 3416d7f67ac9SQu Wenruo } 3417d7f67ac9SQu Wenruo 3418d7f67ac9SQu Wenruo /* Runtime limitation for mixed block groups. */ 3419d7f67ac9SQu Wenruo if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3420d7f67ac9SQu Wenruo (fs_info->sectorsize != fs_info->nodesize)) { 3421d7f67ac9SQu Wenruo btrfs_err(fs_info, 3422d7f67ac9SQu Wenruo "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 3423d7f67ac9SQu Wenruo fs_info->nodesize, fs_info->sectorsize); 3424d7f67ac9SQu Wenruo return -EINVAL; 3425d7f67ac9SQu Wenruo } 3426d7f67ac9SQu Wenruo 3427d7f67ac9SQu Wenruo /* Mixed backref is an always-enabled feature. */ 3428d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 3429d7f67ac9SQu Wenruo 3430d7f67ac9SQu Wenruo /* Set compression related flags just in case. */ 3431d7f67ac9SQu Wenruo if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3432d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 3433d7f67ac9SQu Wenruo else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 3434d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3435d7f67ac9SQu Wenruo 3436d7f67ac9SQu Wenruo /* 3437d7f67ac9SQu Wenruo * An ancient flag, which should really be marked deprecated. 3438d7f67ac9SQu Wenruo * Such runtime limitation doesn't really need a incompat flag. 3439d7f67ac9SQu Wenruo */ 3440d7f67ac9SQu Wenruo if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) 3441d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3442d7f67ac9SQu Wenruo 34432ba48b20SQu Wenruo if (compat_ro_unsupp && is_rw_mount) { 3444d7f67ac9SQu Wenruo btrfs_err(fs_info, 3445d7f67ac9SQu Wenruo "cannot mount read-write because of unknown compat_ro features (0x%llx)", 3446d7f67ac9SQu Wenruo compat_ro); 3447d7f67ac9SQu Wenruo return -EINVAL; 3448d7f67ac9SQu Wenruo } 3449d7f67ac9SQu Wenruo 3450d7f67ac9SQu Wenruo /* 3451d7f67ac9SQu Wenruo * We have unsupported RO compat features, although RO mounted, we 3452d7f67ac9SQu Wenruo * should not cause any metadata writes, including log replay. 3453d7f67ac9SQu Wenruo * Or we could screw up whatever the new feature requires. 3454d7f67ac9SQu Wenruo */ 3455d7f67ac9SQu Wenruo if (compat_ro_unsupp && btrfs_super_log_root(disk_super) && 3456d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3457d7f67ac9SQu Wenruo btrfs_err(fs_info, 3458d7f67ac9SQu Wenruo "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay", 3459d7f67ac9SQu Wenruo compat_ro); 3460d7f67ac9SQu Wenruo return -EINVAL; 3461d7f67ac9SQu Wenruo } 3462d7f67ac9SQu Wenruo 3463d7f67ac9SQu Wenruo /* 3464d7f67ac9SQu Wenruo * Artificial limitations for block group tree, to force 3465d7f67ac9SQu Wenruo * block-group-tree to rely on no-holes and free-space-tree. 3466d7f67ac9SQu Wenruo */ 3467d7f67ac9SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) && 3468d7f67ac9SQu Wenruo (!btrfs_fs_incompat(fs_info, NO_HOLES) || 3469d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) { 3470d7f67ac9SQu Wenruo btrfs_err(fs_info, 3471d7f67ac9SQu Wenruo "block-group-tree feature requires no-holes and free-space-tree features"); 3472d7f67ac9SQu Wenruo return -EINVAL; 3473d7f67ac9SQu Wenruo } 3474d7f67ac9SQu Wenruo 3475d7f67ac9SQu Wenruo /* 3476d7f67ac9SQu Wenruo * Subpage runtime limitation on v1 cache. 3477d7f67ac9SQu Wenruo * 3478d7f67ac9SQu Wenruo * V1 space cache still has some hard codeed PAGE_SIZE usage, while 3479d7f67ac9SQu Wenruo * we're already defaulting to v2 cache, no need to bother v1 as it's 3480d7f67ac9SQu Wenruo * going to be deprecated anyway. 3481d7f67ac9SQu Wenruo */ 3482d7f67ac9SQu Wenruo if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) { 3483d7f67ac9SQu Wenruo btrfs_warn(fs_info, 3484d7f67ac9SQu Wenruo "v1 space cache is not supported for page size %lu with sectorsize %u", 3485d7f67ac9SQu Wenruo PAGE_SIZE, fs_info->sectorsize); 3486d7f67ac9SQu Wenruo return -EINVAL; 3487d7f67ac9SQu Wenruo } 3488d7f67ac9SQu Wenruo 3489d7f67ac9SQu Wenruo /* This can be called by remount, we need to protect the super block. */ 3490d7f67ac9SQu Wenruo spin_lock(&fs_info->super_lock); 3491d7f67ac9SQu Wenruo btrfs_set_super_incompat_flags(disk_super, incompat); 3492d7f67ac9SQu Wenruo spin_unlock(&fs_info->super_lock); 3493d7f67ac9SQu Wenruo 3494d7f67ac9SQu Wenruo return 0; 3495d7f67ac9SQu Wenruo } 3496d7f67ac9SQu Wenruo 3497ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3498ae18c37aSJosef Bacik char *options) 3499ae18c37aSJosef Bacik { 3500ae18c37aSJosef Bacik u32 sectorsize; 3501ae18c37aSJosef Bacik u32 nodesize; 3502ae18c37aSJosef Bacik u32 stripesize; 3503ae18c37aSJosef Bacik u64 generation; 3504ae18c37aSJosef Bacik u64 features; 3505ae18c37aSJosef Bacik u16 csum_type; 3506ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3507ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3508ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3509ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3510ae18c37aSJosef Bacik int ret; 3511ae18c37aSJosef Bacik int err = -EINVAL; 3512ae18c37aSJosef Bacik int level; 3513ae18c37aSJosef Bacik 35148260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 351553b381b3SDavid Woodhouse if (ret) { 351683c8266aSDavid Sterba err = ret; 3517ae18c37aSJosef Bacik goto fail; 351853b381b3SDavid Woodhouse } 351953b381b3SDavid Woodhouse 3520ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3521ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3522ae18c37aSJosef Bacik GFP_KERNEL); 3523ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3524ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3525ae18c37aSJosef Bacik GFP_KERNEL); 3526ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3527ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3528ae18c37aSJosef Bacik err = -ENOMEM; 3529c75e8394SJosef Bacik goto fail; 3530ae18c37aSJosef Bacik } 3531ae18c37aSJosef Bacik 3532ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3533ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3534ae18c37aSJosef Bacik err = -ENOMEM; 3535c75e8394SJosef Bacik goto fail; 3536ae18c37aSJosef Bacik } 3537ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3538ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3539ae18c37aSJosef Bacik 3540d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 35411104a885SDavid Sterba 35421104a885SDavid Sterba /* 35431104a885SDavid Sterba * Read super block and check the signature bytes only 35441104a885SDavid Sterba */ 3545d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 35468f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 35478f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 354816cdcec7SMiao Xie goto fail_alloc; 354920b45077SDave Young } 355039279cc3SChris Mason 35511104a885SDavid Sterba /* 3552260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 35538dc3f22cSJohannes Thumshirn * corrupted, we'll find out 35548dc3f22cSJohannes Thumshirn */ 35558f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 355651bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 35578dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 355851bce6c9SJohannes Thumshirn csum_type); 35598dc3f22cSJohannes Thumshirn err = -EINVAL; 35608f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35618dc3f22cSJohannes Thumshirn goto fail_alloc; 35628dc3f22cSJohannes Thumshirn } 35638dc3f22cSJohannes Thumshirn 356483c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 356583c68bbcSSu Yue 35666d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 35676d97c6e3SJohannes Thumshirn if (ret) { 35686d97c6e3SJohannes Thumshirn err = ret; 35698f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35706d97c6e3SJohannes Thumshirn goto fail_alloc; 35716d97c6e3SJohannes Thumshirn } 35726d97c6e3SJohannes Thumshirn 35738dc3f22cSJohannes Thumshirn /* 35741104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 35751104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 35761104a885SDavid Sterba */ 35773d17adeaSQu Wenruo if (btrfs_check_super_csum(fs_info, disk_super)) { 357805135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 35791104a885SDavid Sterba err = -EINVAL; 35808f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3581141386e1SJosef Bacik goto fail_alloc; 35821104a885SDavid Sterba } 35831104a885SDavid Sterba 35841104a885SDavid Sterba /* 35851104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 35861104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 35871104a885SDavid Sterba * the whole block of INFO_SIZE 35881104a885SDavid Sterba */ 35898f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 35908f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35915f39d397SChris Mason 3592fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3593fbc6feaeSNikolay Borisov 35940b86a832SChris Mason 3595fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3596fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3597fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3598fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3599fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3600fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3601fbc6feaeSNikolay Borisov } 3602fbc6feaeSNikolay Borisov 3603fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3604fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3605de37aa51SNikolay Borisov 3606069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 36071104a885SDavid Sterba if (ret) { 360805135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 36091104a885SDavid Sterba err = -EINVAL; 3610141386e1SJosef Bacik goto fail_alloc; 36111104a885SDavid Sterba } 36121104a885SDavid Sterba 36130f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3614141386e1SJosef Bacik goto fail_alloc; 36150f7d52f4SChris Mason 3616acce952bSliubo /* check FS state, whether FS is broken. */ 361787533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 361887533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3619acce952bSliubo 362075e7cb7fSLiu Bo /* 362175e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 362275e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 362375e7cb7fSLiu Bo */ 362475e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 362575e7cb7fSLiu Bo 36266f93e834SAnand Jain 36276f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 36286f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 36296f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 36306f93e834SAnand Jain stripesize = sectorsize; 36316f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 36326f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 36336f93e834SAnand Jain 36346f93e834SAnand Jain fs_info->nodesize = nodesize; 36356f93e834SAnand Jain fs_info->sectorsize = sectorsize; 36366f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 36376f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 36386f93e834SAnand Jain fs_info->stripesize = stripesize; 36396f93e834SAnand Jain 36402ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 36412b82032cSYan Zheng if (ret) { 36422b82032cSYan Zheng err = ret; 3643141386e1SJosef Bacik goto fail_alloc; 36442b82032cSYan Zheng } 3645dfe25020SChris Mason 36462ba48b20SQu Wenruo ret = btrfs_check_features(fs_info, !sb_rdonly(sb)); 3647d7f67ac9SQu Wenruo if (ret < 0) { 3648d7f67ac9SQu Wenruo err = ret; 3649141386e1SJosef Bacik goto fail_alloc; 3650f2b636e8SJosef Bacik } 3651f2b636e8SJosef Bacik 36528481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 36538481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 36548481dd80SQu Wenruo 36559f73f1aeSQu Wenruo /* 36569f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 36579f73f1aeSQu Wenruo * going to be deprecated. 36589f73f1aeSQu Wenruo * 36599f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 36609f73f1aeSQu Wenruo */ 36619f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 36629f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 36639f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 36649f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 36659f73f1aeSQu Wenruo 366695ea0486SQu Wenruo btrfs_warn(fs_info, 366795ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 36680bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 36698481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 36708481dd80SQu Wenruo if (!subpage_info) 36718481dd80SQu Wenruo goto fail_alloc; 36728481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 36738481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3674c8050b3bSQu Wenruo } 36750bb3eb3eSQu Wenruo 3676d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 36772a458198SEric Sandeen if (ret) { 36782a458198SEric Sandeen err = ret; 36790dc3b84aSJosef Bacik goto fail_sb_buffer; 36800dc3b84aSJosef Bacik } 36814543df7eSChris Mason 36829e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 36839e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 36844575c9ccSChris Mason 3685a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3686a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3687de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3688db94535dSChris Mason 3689925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 36906bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3691925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 369284eed90fSChris Mason if (ret) { 369305135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36945d4f98a2SYan Zheng goto fail_sb_buffer; 369584eed90fSChris Mason } 36960b86a832SChris Mason 369784234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3698581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3699bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3700bd676446SJosef Bacik generation, level); 3701bd676446SJosef Bacik if (ret) { 370205135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3703af31f5e5SChris Mason goto fail_tree_roots; 370483121942SDavid Woodhouse } 37050b86a832SChris Mason 3706e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3707c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3708c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3709e17cade2SChris Mason 37105b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 37112b82032cSYan Zheng if (ret) { 371205135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3713af31f5e5SChris Mason goto fail_tree_roots; 37142b82032cSYan Zheng } 37150b86a832SChris Mason 37168dabb742SStefan Behrens /* 3717bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3718bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3719bacce86aSAnand Jain * target is required. So free devices that are not part of this 37201a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3721bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 37228dabb742SStefan Behrens */ 3723bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3724d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 372505135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3726a6b0d5c8SChris Mason goto fail_tree_roots; 3727a6b0d5c8SChris Mason } 3728a6b0d5c8SChris Mason 3729b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 37304bbcaa64SEric Sandeen if (ret) 3731b8522a1eSNikolay Borisov goto fail_tree_roots; 37328929ecfaSYan, Zheng 373375ec1db8SJosef Bacik /* 373473651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 373573651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 373673651042SNaohiro Aota * zoned incompat feature flag set. 373773651042SNaohiro Aota */ 373873651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 373973651042SNaohiro Aota if (ret) { 374073651042SNaohiro Aota btrfs_err(fs_info, 374173651042SNaohiro Aota "zoned: failed to read device zone info: %d", 374273651042SNaohiro Aota ret); 374373651042SNaohiro Aota goto fail_block_groups; 374473651042SNaohiro Aota } 374573651042SNaohiro Aota 374673651042SNaohiro Aota /* 374775ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 374875ec1db8SJosef Bacik * check the generation here so we can set the 374975ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 375075ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 375175ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 375275ec1db8SJosef Bacik * even though it was perfectly fine. 375375ec1db8SJosef Bacik */ 375475ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 375575ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 375675ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 375775ec1db8SJosef Bacik 3758cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3759cf90d884SQu Wenruo if (ret) { 3760cf90d884SQu Wenruo btrfs_err(fs_info, 3761cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3762cf90d884SQu Wenruo ret); 3763cf90d884SQu Wenruo goto fail_block_groups; 3764cf90d884SQu Wenruo } 376568310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 376668310a5eSIlya Dryomov if (ret) { 376705135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 376868310a5eSIlya Dryomov goto fail_block_groups; 376968310a5eSIlya Dryomov } 377068310a5eSIlya Dryomov 3771733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3772733f4fbbSStefan Behrens if (ret) { 377305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3774733f4fbbSStefan Behrens goto fail_block_groups; 3775733f4fbbSStefan Behrens } 3776733f4fbbSStefan Behrens 37778dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 37788dabb742SStefan Behrens if (ret) { 377905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 37808dabb742SStefan Behrens goto fail_block_groups; 37818dabb742SStefan Behrens } 37828dabb742SStefan Behrens 3783b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3784b70f5097SNaohiro Aota if (ret) { 3785b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3786b70f5097SNaohiro Aota ret); 3787b70f5097SNaohiro Aota goto fail_block_groups; 3788b70f5097SNaohiro Aota } 3789b70f5097SNaohiro Aota 3790c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3791b7c35e81SAnand Jain if (ret) { 379205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 379305135f59SDavid Sterba ret); 3794b7c35e81SAnand Jain goto fail_block_groups; 3795b7c35e81SAnand Jain } 3796b7c35e81SAnand Jain 379796f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 37985ac1d209SJeff Mahoney if (ret) { 379905135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3800b7c35e81SAnand Jain goto fail_fsdev_sysfs; 38015ac1d209SJeff Mahoney } 38025ac1d209SJeff Mahoney 3803c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3804c59021f8Sliubo if (ret) { 380505135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 38062365dd3cSAnand Jain goto fail_sysfs; 3807c59021f8Sliubo } 3808c59021f8Sliubo 38095b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 38101b1d1f66SJosef Bacik if (ret) { 381105135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 38122365dd3cSAnand Jain goto fail_sysfs; 38131b1d1f66SJosef Bacik } 38144330e183SQu Wenruo 381516beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 381616beac87SNaohiro Aota 38175c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 38185c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 381905135f59SDavid Sterba btrfs_warn(fs_info, 382052042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 38212365dd3cSAnand Jain goto fail_sysfs; 3822292fd7fcSStefan Behrens } 38239078a3e1SChris Mason 382433c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3825a74a4b97SChris Mason "btrfs-cleaner"); 382657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 38272365dd3cSAnand Jain goto fail_sysfs; 3828a74a4b97SChris Mason 3829a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3830a74a4b97SChris Mason tree_root, 3831a74a4b97SChris Mason "btrfs-transaction"); 383257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 38333f157a2fSChris Mason goto fail_cleaner; 3834a74a4b97SChris Mason 3835583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3836c289811cSChris Mason !fs_info->fs_devices->rotating) { 3837583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3838c289811cSChris Mason } 3839c289811cSChris Mason 3840572d9ab7SDavid Sterba /* 384163a7cb13SDavid Sterba * For devices supporting discard turn on discard=async automatically, 384263a7cb13SDavid Sterba * unless it's already set or disabled. This could be turned off by 384363a7cb13SDavid Sterba * nodiscard for the same mount. 384463a7cb13SDavid Sterba */ 384563a7cb13SDavid Sterba if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) || 384663a7cb13SDavid Sterba btrfs_test_opt(fs_info, DISCARD_ASYNC) || 384763a7cb13SDavid Sterba btrfs_test_opt(fs_info, NODISCARD)) && 384863a7cb13SDavid Sterba fs_info->fs_devices->discardable) { 384963a7cb13SDavid Sterba btrfs_set_and_info(fs_info, DISCARD_ASYNC, 385063a7cb13SDavid Sterba "auto enabling async discard"); 385163a7cb13SDavid Sterba btrfs_clear_opt(fs_info->mount_opt, NODISCARD); 385263a7cb13SDavid Sterba } 385363a7cb13SDavid Sterba 385421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38550b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 38562ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 38570b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3858cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 385921adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 386021adbd5cSStefan Behrens if (ret) 386105135f59SDavid Sterba btrfs_warn(fs_info, 386205135f59SDavid Sterba "failed to initialize integrity check module: %d", 386305135f59SDavid Sterba ret); 386421adbd5cSStefan Behrens } 386521adbd5cSStefan Behrens #endif 3866bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3867bcef60f2SArne Jansen if (ret) 3868bcef60f2SArne Jansen goto fail_trans_kthread; 386921adbd5cSStefan Behrens 3870fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3871fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3872fd708b81SJosef Bacik 387396da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 387496da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 38750b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3876e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 387763443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 387879787eaaSJeff Mahoney if (ret) { 387963443bf5SEric Sandeen err = ret; 388028c16cbbSWang Shilong goto fail_qgroup; 3881e556ce2cSYan Zheng } 3882e02119d5SChris Mason } 38831a40e23bSZheng Yan 388456e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 38853140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 38863140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3887f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3888315bf8efSJosef Bacik fs_info->fs_root = NULL; 3889bcef60f2SArne Jansen goto fail_qgroup; 38903140c9a3SDan Carpenter } 3891c289811cSChris Mason 3892bc98a42cSDavid Howells if (sb_rdonly(sb)) 38938cd29088SBoris Burkov goto clear_oneshot; 38942b6ba629SIlya Dryomov 389544c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 38962b6ba629SIlya Dryomov if (ret) { 38976bccf3abSJeff Mahoney close_ctree(fs_info); 38982b6ba629SIlya Dryomov return ret; 3899e3acc2a6SJosef Bacik } 3900b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3901b382a324SJan Schmidt 390244c0ca21SBoris Burkov if (fs_info->uuid_root && 390344c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 390444c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 390505135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 390670f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 390770f80175SStefan Behrens if (ret) { 390805135f59SDavid Sterba btrfs_warn(fs_info, 390905135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 39106bccf3abSJeff Mahoney close_ctree(fs_info); 391170f80175SStefan Behrens return ret; 391270f80175SStefan Behrens } 3913f7a81ea4SStefan Behrens } 391494846229SBoris Burkov 3915afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 391647ab2a6cSJosef Bacik 3917b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3918b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3919b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3920b4be6aefSJosef Bacik 39218cd29088SBoris Burkov clear_oneshot: 39228cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3923ad2b2c80SAl Viro return 0; 392439279cc3SChris Mason 3925bcef60f2SArne Jansen fail_qgroup: 3926bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 39277c2ca468SChris Mason fail_trans_kthread: 39287c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 39292ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3930faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 39313f157a2fSChris Mason fail_cleaner: 3932a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 39337c2ca468SChris Mason 39347c2ca468SChris Mason /* 39357c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 39367c2ca468SChris Mason * kthreads 39377c2ca468SChris Mason */ 39387c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 39397c2ca468SChris Mason 39402365dd3cSAnand Jain fail_sysfs: 39416618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 39422365dd3cSAnand Jain 3943b7c35e81SAnand Jain fail_fsdev_sysfs: 3944b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3945b7c35e81SAnand Jain 39461b1d1f66SJosef Bacik fail_block_groups: 394754067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3948af31f5e5SChris Mason 3949af31f5e5SChris Mason fail_tree_roots: 39509e3aa805SJosef Bacik if (fs_info->data_reloc_root) 39519e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 39524273eaffSAnand Jain free_root_pointers(fs_info, true); 39532b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3954af31f5e5SChris Mason 395539279cc3SChris Mason fail_sb_buffer: 39567abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 39575cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 395816cdcec7SMiao Xie fail_alloc: 3959586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3960586e46e2SIlya Dryomov 39614543df7eSChris Mason iput(fs_info->btree_inode); 39627e662854SQinghuang Feng fail: 3963586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3964ad2b2c80SAl Viro return err; 3965eb60ceacSChris Mason } 3966663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3967eb60ceacSChris Mason 3968314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3969f2984462SChris Mason { 3970314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3971314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3972314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3973314b6dd0SJohannes Thumshirn struct page *page; 3974442a4f63SStefan Behrens 3975314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3976314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3977314b6dd0SJohannes Thumshirn 3978314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3979fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3980314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3981cb3e217bSQu Wenruo btrfs_dev_name(device), 3982314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3983314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3984314b6dd0SJohannes Thumshirn SetPageError(page); 3985314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3986314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3987314b6dd0SJohannes Thumshirn } else { 3988314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3989f2984462SChris Mason } 3990314b6dd0SJohannes Thumshirn 3991314b6dd0SJohannes Thumshirn put_page(page); 3992314b6dd0SJohannes Thumshirn unlock_page(page); 3993314b6dd0SJohannes Thumshirn } 3994314b6dd0SJohannes Thumshirn 3995314b6dd0SJohannes Thumshirn bio_put(bio); 3996f2984462SChris Mason } 3997f2984462SChris Mason 39988f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 3999a05d3c91SQu Wenruo int copy_num, bool drop_cache) 400029c36d72SAnand Jain { 400129c36d72SAnand Jain struct btrfs_super_block *super; 40028f32380dSJohannes Thumshirn struct page *page; 400312659251SNaohiro Aota u64 bytenr, bytenr_orig; 40048f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 400512659251SNaohiro Aota int ret; 400629c36d72SAnand Jain 400712659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 400812659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 400912659251SNaohiro Aota if (ret == -ENOENT) 401012659251SNaohiro Aota return ERR_PTR(-EINVAL); 401112659251SNaohiro Aota else if (ret) 401212659251SNaohiro Aota return ERR_PTR(ret); 401312659251SNaohiro Aota 4014cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 40158f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 401629c36d72SAnand Jain 4017a05d3c91SQu Wenruo if (drop_cache) { 4018a05d3c91SQu Wenruo /* This should only be called with the primary sb. */ 4019a05d3c91SQu Wenruo ASSERT(copy_num == 0); 4020a05d3c91SQu Wenruo 4021a05d3c91SQu Wenruo /* 4022a05d3c91SQu Wenruo * Drop the page of the primary superblock, so later read will 4023a05d3c91SQu Wenruo * always read from the device. 4024a05d3c91SQu Wenruo */ 4025a05d3c91SQu Wenruo invalidate_inode_pages2_range(mapping, 4026a05d3c91SQu Wenruo bytenr >> PAGE_SHIFT, 4027a05d3c91SQu Wenruo (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT); 4028a05d3c91SQu Wenruo } 4029a05d3c91SQu Wenruo 40308f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 40318f32380dSJohannes Thumshirn if (IS_ERR(page)) 40328f32380dSJohannes Thumshirn return ERR_CAST(page); 403329c36d72SAnand Jain 40348f32380dSJohannes Thumshirn super = page_address(page); 403596c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 403696c2e067SAnand Jain btrfs_release_disk_super(super); 403796c2e067SAnand Jain return ERR_PTR(-ENODATA); 403896c2e067SAnand Jain } 403996c2e067SAnand Jain 404012659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 40418f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40428f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 404329c36d72SAnand Jain } 404429c36d72SAnand Jain 40458f32380dSJohannes Thumshirn return super; 404629c36d72SAnand Jain } 404729c36d72SAnand Jain 404829c36d72SAnand Jain 40498f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 4050a512bbf8SYan Zheng { 40518f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 4052a512bbf8SYan Zheng int i; 4053a512bbf8SYan Zheng u64 transid = 0; 4054a512bbf8SYan Zheng 4055a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 4056a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 4057a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 4058a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 4059a512bbf8SYan Zheng */ 4060a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 4061a05d3c91SQu Wenruo super = btrfs_read_dev_one_super(bdev, i, false); 40628f32380dSJohannes Thumshirn if (IS_ERR(super)) 4063a512bbf8SYan Zheng continue; 4064a512bbf8SYan Zheng 4065a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 40668f32380dSJohannes Thumshirn if (latest) 40678f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40688f32380dSJohannes Thumshirn 40698f32380dSJohannes Thumshirn latest = super; 4070a512bbf8SYan Zheng transid = btrfs_super_generation(super); 4071a512bbf8SYan Zheng } 4072a512bbf8SYan Zheng } 407392fc03fbSAnand Jain 40748f32380dSJohannes Thumshirn return super; 4075a512bbf8SYan Zheng } 4076a512bbf8SYan Zheng 40774eedeb75SHisashi Hifumi /* 4078abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 4079314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 40804eedeb75SHisashi Hifumi * 4081abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 4082abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 4083abbb3b8eSDavid Sterba * same for write and wait phases. 40844eedeb75SHisashi Hifumi * 4085314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 40864eedeb75SHisashi Hifumi */ 4087a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 4088abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 4089a512bbf8SYan Zheng { 4090d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 4091314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 4092d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 4093a512bbf8SYan Zheng int i; 4094a512bbf8SYan Zheng int errors = 0; 409512659251SNaohiro Aota int ret; 409612659251SNaohiro Aota u64 bytenr, bytenr_orig; 4097a512bbf8SYan Zheng 4098a512bbf8SYan Zheng if (max_mirrors == 0) 4099a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4100a512bbf8SYan Zheng 4101d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4102d5178578SJohannes Thumshirn 4103a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4104314b6dd0SJohannes Thumshirn struct page *page; 4105314b6dd0SJohannes Thumshirn struct bio *bio; 4106314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4107314b6dd0SJohannes Thumshirn 410812659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 410912659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 411012659251SNaohiro Aota if (ret == -ENOENT) { 411112659251SNaohiro Aota continue; 411212659251SNaohiro Aota } else if (ret < 0) { 411312659251SNaohiro Aota btrfs_err(device->fs_info, 411412659251SNaohiro Aota "couldn't get super block location for mirror %d", 411512659251SNaohiro Aota i); 411612659251SNaohiro Aota errors++; 411712659251SNaohiro Aota continue; 411812659251SNaohiro Aota } 4119935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4120935e5cc9SMiao Xie device->commit_total_bytes) 4121a512bbf8SYan Zheng break; 4122a512bbf8SYan Zheng 412312659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4124a512bbf8SYan Zheng 4125fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4126fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4127fd08001fSEric Biggers sb->csum); 4128a512bbf8SYan Zheng 4129314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4130314b6dd0SJohannes Thumshirn GFP_NOFS); 4131314b6dd0SJohannes Thumshirn if (!page) { 4132fb456252SJeff Mahoney btrfs_err(device->fs_info, 4133314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4134f14d104dSDavid Sterba bytenr); 4135634554dcSJosef Bacik errors++; 4136634554dcSJosef Bacik continue; 4137634554dcSJosef Bacik } 4138634554dcSJosef Bacik 4139314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4140314b6dd0SJohannes Thumshirn get_page(page); 4141a512bbf8SYan Zheng 4142314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4143314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4144a512bbf8SYan Zheng 4145387125fcSChris Mason /* 4146314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4147314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4148314b6dd0SJohannes Thumshirn * checking. 4149387125fcSChris Mason */ 415007888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 415107888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 415207888c66SChristoph Hellwig GFP_NOFS); 4153314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4154314b6dd0SJohannes Thumshirn bio->bi_private = device; 4155314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4156314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4157314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4158314b6dd0SJohannes Thumshirn 4159314b6dd0SJohannes Thumshirn /* 4160314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4161314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4162314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4163314b6dd0SJohannes Thumshirn */ 41641b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4165314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4166314b6dd0SJohannes Thumshirn 416758ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 416858ff51f1SChristoph Hellwig submit_bio(bio); 41698376d9e1SNaohiro Aota 41708376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 41718376d9e1SNaohiro Aota errors++; 4172a512bbf8SYan Zheng } 4173a512bbf8SYan Zheng return errors < i ? 0 : -1; 4174a512bbf8SYan Zheng } 4175a512bbf8SYan Zheng 4176387125fcSChris Mason /* 4177abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4178abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4179abbb3b8eSDavid Sterba * 4180314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4181abbb3b8eSDavid Sterba * date. 4182abbb3b8eSDavid Sterba */ 4183abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4184abbb3b8eSDavid Sterba { 4185abbb3b8eSDavid Sterba int i; 4186abbb3b8eSDavid Sterba int errors = 0; 4187b6a535faSHoward McLauchlan bool primary_failed = false; 418812659251SNaohiro Aota int ret; 4189abbb3b8eSDavid Sterba u64 bytenr; 4190abbb3b8eSDavid Sterba 4191abbb3b8eSDavid Sterba if (max_mirrors == 0) 4192abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4193abbb3b8eSDavid Sterba 4194abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4195314b6dd0SJohannes Thumshirn struct page *page; 4196314b6dd0SJohannes Thumshirn 419712659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 419812659251SNaohiro Aota if (ret == -ENOENT) { 419912659251SNaohiro Aota break; 420012659251SNaohiro Aota } else if (ret < 0) { 420112659251SNaohiro Aota errors++; 420212659251SNaohiro Aota if (i == 0) 420312659251SNaohiro Aota primary_failed = true; 420412659251SNaohiro Aota continue; 420512659251SNaohiro Aota } 4206abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4207abbb3b8eSDavid Sterba device->commit_total_bytes) 4208abbb3b8eSDavid Sterba break; 4209abbb3b8eSDavid Sterba 4210314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4211314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4212314b6dd0SJohannes Thumshirn if (!page) { 4213abbb3b8eSDavid Sterba errors++; 4214b6a535faSHoward McLauchlan if (i == 0) 4215b6a535faSHoward McLauchlan primary_failed = true; 4216abbb3b8eSDavid Sterba continue; 4217abbb3b8eSDavid Sterba } 4218314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4219314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4220314b6dd0SJohannes Thumshirn if (PageError(page)) { 4221abbb3b8eSDavid Sterba errors++; 4222b6a535faSHoward McLauchlan if (i == 0) 4223b6a535faSHoward McLauchlan primary_failed = true; 4224b6a535faSHoward McLauchlan } 4225abbb3b8eSDavid Sterba 4226314b6dd0SJohannes Thumshirn /* Drop our reference */ 4227314b6dd0SJohannes Thumshirn put_page(page); 4228abbb3b8eSDavid Sterba 4229314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4230314b6dd0SJohannes Thumshirn put_page(page); 4231abbb3b8eSDavid Sterba } 4232abbb3b8eSDavid Sterba 4233b6a535faSHoward McLauchlan /* log error, force error return */ 4234b6a535faSHoward McLauchlan if (primary_failed) { 4235b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4236b6a535faSHoward McLauchlan device->devid); 4237b6a535faSHoward McLauchlan return -1; 4238b6a535faSHoward McLauchlan } 4239b6a535faSHoward McLauchlan 4240abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4241abbb3b8eSDavid Sterba } 4242abbb3b8eSDavid Sterba 4243abbb3b8eSDavid Sterba /* 4244387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4245387125fcSChris Mason * for the barrier when it is done 4246387125fcSChris Mason */ 42474246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4248387125fcSChris Mason { 4249f9e69aa9SChristoph Hellwig bio_uninit(bio); 4250387125fcSChris Mason complete(bio->bi_private); 4251387125fcSChris Mason } 4252387125fcSChris Mason 4253387125fcSChris Mason /* 42544fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 42554fc6441aSAnand Jain * done in the waiting counterpart. 4256387125fcSChris Mason */ 42574fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4258387125fcSChris Mason { 4259f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 4260387125fcSChris Mason 4261a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4262a91cf0ffSWang Yugui /* 4263a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4264a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4265a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4266a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4267a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4268a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4269a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4270a91cf0ffSWang Yugui * non-debug builds. 4271a91cf0ffSWang Yugui */ 427208e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev)) 42734fc6441aSAnand Jain return; 4274a91cf0ffSWang Yugui #endif 4275387125fcSChris Mason 4276f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0, 4277f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4278387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4279387125fcSChris Mason init_completion(&device->flush_wait); 4280387125fcSChris Mason bio->bi_private = &device->flush_wait; 4281387125fcSChris Mason 428258ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 428358ff51f1SChristoph Hellwig submit_bio(bio); 42841c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42854fc6441aSAnand Jain } 4286387125fcSChris Mason 42874fc6441aSAnand Jain /* 42884fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 42894fc6441aSAnand Jain */ 42908c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 42914fc6441aSAnand Jain { 4292f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 42934fc6441aSAnand Jain 42941c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 429558efbc9fSOmar Sandoval return BLK_STS_OK; 42964fc6441aSAnand Jain 42971c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42982980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 42994fc6441aSAnand Jain 43008c27cb35SLinus Torvalds return bio->bi_status; 4301387125fcSChris Mason } 4302387125fcSChris Mason 4303d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4304401b41e5SAnand Jain { 43056528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4306401b41e5SAnand Jain return -EIO; 4307387125fcSChris Mason return 0; 4308387125fcSChris Mason } 4309387125fcSChris Mason 4310387125fcSChris Mason /* 4311387125fcSChris Mason * send an empty flush down to each device in parallel, 4312387125fcSChris Mason * then wait for them 4313387125fcSChris Mason */ 4314387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4315387125fcSChris Mason { 4316387125fcSChris Mason struct list_head *head; 4317387125fcSChris Mason struct btrfs_device *dev; 43185af3e8ccSStefan Behrens int errors_wait = 0; 43194e4cbee9SChristoph Hellwig blk_status_t ret; 4320387125fcSChris Mason 43211538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4322387125fcSChris Mason /* send down all the barriers */ 4323387125fcSChris Mason head = &info->fs_devices->devices; 43241538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4325e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4326f88ba6a2SHidetoshi Seto continue; 4327cea7c8bfSAnand Jain if (!dev->bdev) 4328387125fcSChris Mason continue; 4329e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4330ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4331387125fcSChris Mason continue; 4332387125fcSChris Mason 43334fc6441aSAnand Jain write_dev_flush(dev); 433458efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4335387125fcSChris Mason } 4336387125fcSChris Mason 4337387125fcSChris Mason /* wait for all the barriers */ 43381538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4339e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4340f88ba6a2SHidetoshi Seto continue; 4341387125fcSChris Mason if (!dev->bdev) { 43425af3e8ccSStefan Behrens errors_wait++; 4343387125fcSChris Mason continue; 4344387125fcSChris Mason } 4345e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4346ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4347387125fcSChris Mason continue; 4348387125fcSChris Mason 43494fc6441aSAnand Jain ret = wait_dev_flush(dev); 4350401b41e5SAnand Jain if (ret) { 4351401b41e5SAnand Jain dev->last_flush_error = ret; 435266b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 435366b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 43545af3e8ccSStefan Behrens errors_wait++; 4355387125fcSChris Mason } 4356401b41e5SAnand Jain } 4357401b41e5SAnand Jain 4358cea7c8bfSAnand Jain if (errors_wait) { 4359401b41e5SAnand Jain /* 4360401b41e5SAnand Jain * At some point we need the status of all disks 4361401b41e5SAnand Jain * to arrive at the volume status. So error checking 4362401b41e5SAnand Jain * is being pushed to a separate loop. 4363401b41e5SAnand Jain */ 4364d10b82feSQu Wenruo return check_barrier_error(info); 4365401b41e5SAnand Jain } 4366387125fcSChris Mason return 0; 4367387125fcSChris Mason } 4368387125fcSChris Mason 4369943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4370943c6e99SZhao Lei { 43718789f4feSZhao Lei int raid_type; 43728789f4feSZhao Lei int min_tolerated = INT_MAX; 4373943c6e99SZhao Lei 43748789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 43758789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 43768c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43778789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 43788789f4feSZhao Lei tolerated_failures); 4379943c6e99SZhao Lei 43808789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 43818789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 43828789f4feSZhao Lei continue; 438341a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 43848789f4feSZhao Lei continue; 43858c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43868789f4feSZhao Lei btrfs_raid_array[raid_type]. 43878789f4feSZhao Lei tolerated_failures); 43888789f4feSZhao Lei } 4389943c6e99SZhao Lei 43908789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4391ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 43928789f4feSZhao Lei min_tolerated = 0; 43938789f4feSZhao Lei } 43948789f4feSZhao Lei 43958789f4feSZhao Lei return min_tolerated; 4396943c6e99SZhao Lei } 4397943c6e99SZhao Lei 4398eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4399f2984462SChris Mason { 4400e5e9a520SChris Mason struct list_head *head; 4401f2984462SChris Mason struct btrfs_device *dev; 4402a061fc8dSChris Mason struct btrfs_super_block *sb; 4403f2984462SChris Mason struct btrfs_dev_item *dev_item; 4404f2984462SChris Mason int ret; 4405f2984462SChris Mason int do_barriers; 4406a236aed1SChris Mason int max_errors; 4407a236aed1SChris Mason int total_errors = 0; 4408a061fc8dSChris Mason u64 flags; 4409f2984462SChris Mason 44100b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4411fed3b381SLiu Bo 4412fed3b381SLiu Bo /* 4413fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4414fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4415fed3b381SLiu Bo * been consistent on disk. 4416fed3b381SLiu Bo */ 4417fed3b381SLiu Bo if (max_mirrors == 0) 44180b246afaSJeff Mahoney backup_super_roots(fs_info); 4419f2984462SChris Mason 44200b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4421a061fc8dSChris Mason dev_item = &sb->dev_item; 4422e5e9a520SChris Mason 44230b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 44240b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 44250b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4426387125fcSChris Mason 44275af3e8ccSStefan Behrens if (do_barriers) { 44280b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 44295af3e8ccSStefan Behrens if (ret) { 44305af3e8ccSStefan Behrens mutex_unlock( 44310b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 44320b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 44335af3e8ccSStefan Behrens "errors while submitting device barriers."); 44345af3e8ccSStefan Behrens return ret; 44355af3e8ccSStefan Behrens } 44365af3e8ccSStefan Behrens } 4437387125fcSChris Mason 44381538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4439dfe25020SChris Mason if (!dev->bdev) { 4440dfe25020SChris Mason total_errors++; 4441dfe25020SChris Mason continue; 4442dfe25020SChris Mason } 4443e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4444ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4445dfe25020SChris Mason continue; 4446dfe25020SChris Mason 44472b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4448a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4449a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 44507df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4451935e5cc9SMiao Xie dev->commit_total_bytes); 4452ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4453ce7213c7SMiao Xie dev->commit_bytes_used); 4454a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4455a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4456a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4457a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 44587239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 44597239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4460a512bbf8SYan Zheng 4461a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4462a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4463f2984462SChris Mason 446475cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 446575cb857dSQu Wenruo if (ret < 0) { 446675cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 446775cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 446875cb857dSQu Wenruo "unexpected superblock corruption detected"); 446975cb857dSQu Wenruo return -EUCLEAN; 447075cb857dSQu Wenruo } 447175cb857dSQu Wenruo 4472abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4473a236aed1SChris Mason if (ret) 4474a236aed1SChris Mason total_errors++; 4475f2984462SChris Mason } 4476a236aed1SChris Mason if (total_errors > max_errors) { 44770b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4478d397712bSChris Mason total_errors); 44790b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 448079787eaaSJeff Mahoney 44819d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 44820b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44830b246afaSJeff Mahoney "%d errors while writing supers", 44840b246afaSJeff Mahoney total_errors); 44859d565ba4SStefan Behrens return -EIO; 4486a236aed1SChris Mason } 4487f2984462SChris Mason 4488a512bbf8SYan Zheng total_errors = 0; 44891538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4490dfe25020SChris Mason if (!dev->bdev) 4491dfe25020SChris Mason continue; 4492e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4493ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4494dfe25020SChris Mason continue; 4495dfe25020SChris Mason 4496abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4497a512bbf8SYan Zheng if (ret) 44981259ab75SChris Mason total_errors++; 4499f2984462SChris Mason } 45000b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4501a236aed1SChris Mason if (total_errors > max_errors) { 45020b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 45030b246afaSJeff Mahoney "%d errors while writing supers", 45040b246afaSJeff Mahoney total_errors); 450579787eaaSJeff Mahoney return -EIO; 4506a236aed1SChris Mason } 4507f2984462SChris Mason return 0; 4508f2984462SChris Mason } 4509f2984462SChris Mason 4510cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4511cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4512cb517eabSMiao Xie struct btrfs_root *root) 45132619ba1fSChris Mason { 45144785e24fSJosef Bacik bool drop_ref = false; 45154785e24fSJosef Bacik 4516fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4517fc7cbcd4SDavid Sterba radix_tree_delete(&fs_info->fs_roots_radix, 4518fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid); 4519fc7cbcd4SDavid Sterba if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 45204785e24fSJosef Bacik drop_ref = true; 4521fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 452276dda93cSYan, Zheng 452384961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4524ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 45251c1ea4f7SLiu Bo if (root->reloc_root) { 452600246528SJosef Bacik btrfs_put_root(root->reloc_root); 45271c1ea4f7SLiu Bo root->reloc_root = NULL; 45281c1ea4f7SLiu Bo } 45291c1ea4f7SLiu Bo } 45303321719eSLiu Bo 45314785e24fSJosef Bacik if (drop_ref) 453200246528SJosef Bacik btrfs_put_root(root); 45332619ba1fSChris Mason } 45342619ba1fSChris Mason 4535c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4536c146afadSYan Zheng { 4537fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4538fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4539fc7cbcd4SDavid Sterba int i = 0; 454065d33fd7SQu Wenruo int err = 0; 4541fc7cbcd4SDavid Sterba unsigned int ret = 0; 4542c146afadSYan Zheng 4543c146afadSYan Zheng while (1) { 4544fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4545fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4546fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4547fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 4548fc7cbcd4SDavid Sterba if (!ret) { 4549fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4550c146afadSYan Zheng break; 455165d33fd7SQu Wenruo } 4552fc7cbcd4SDavid Sterba root_objectid = gang[ret - 1]->root_key.objectid + 1; 455366b4ffd1SJosef Bacik 4554fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4555fc7cbcd4SDavid Sterba /* Avoid to grab roots in dead_roots */ 4556fc7cbcd4SDavid Sterba if (btrfs_root_refs(&gang[i]->root_item) == 0) { 4557fc7cbcd4SDavid Sterba gang[i] = NULL; 455865d33fd7SQu Wenruo continue; 4559c146afadSYan Zheng } 4560fc7cbcd4SDavid Sterba /* grab all the search result for later use */ 4561fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4562fc7cbcd4SDavid Sterba } 4563fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4564fc7cbcd4SDavid Sterba 4565fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4566fc7cbcd4SDavid Sterba if (!gang[i]) 4567fc7cbcd4SDavid Sterba continue; 4568fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4569fc7cbcd4SDavid Sterba err = btrfs_orphan_cleanup(gang[i]); 4570fc7cbcd4SDavid Sterba if (err) 4571fc7cbcd4SDavid Sterba break; 4572fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4573fc7cbcd4SDavid Sterba } 4574fc7cbcd4SDavid Sterba root_objectid++; 4575c146afadSYan Zheng } 457665d33fd7SQu Wenruo 4577fc7cbcd4SDavid Sterba /* release the uncleaned roots due to error */ 4578fc7cbcd4SDavid Sterba for (; i < ret; i++) { 4579fc7cbcd4SDavid Sterba if (gang[i]) 4580fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 458165d33fd7SQu Wenruo } 458265d33fd7SQu Wenruo return err; 4583c146afadSYan Zheng } 4584c146afadSYan Zheng 45856bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4586c146afadSYan Zheng { 45876bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4588c146afadSYan Zheng struct btrfs_trans_handle *trans; 4589c146afadSYan Zheng 45900b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 45912ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 45920b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 45930b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4594c71bf099SYan, Zheng 4595c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 45960b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45970b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4598c71bf099SYan, Zheng 45997a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 46003612b495STsutomu Itoh if (IS_ERR(trans)) 46013612b495STsutomu Itoh return PTR_ERR(trans); 46023a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4603c146afadSYan Zheng } 4604c146afadSYan Zheng 460536c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 460636c86a9eSQu Wenruo { 460736c86a9eSQu Wenruo struct btrfs_transaction *trans; 460836c86a9eSQu Wenruo struct btrfs_transaction *tmp; 460936c86a9eSQu Wenruo bool found = false; 461036c86a9eSQu Wenruo 461136c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 461236c86a9eSQu Wenruo return; 461336c86a9eSQu Wenruo 461436c86a9eSQu Wenruo /* 461536c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 461636c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 461736c86a9eSQu Wenruo */ 461836c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 461936c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 462036c86a9eSQu Wenruo struct extent_state *cached = NULL; 462136c86a9eSQu Wenruo u64 dirty_bytes = 0; 462236c86a9eSQu Wenruo u64 cur = 0; 462336c86a9eSQu Wenruo u64 found_start; 462436c86a9eSQu Wenruo u64 found_end; 462536c86a9eSQu Wenruo 462636c86a9eSQu Wenruo found = true; 462736c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 462836c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 462936c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 463036c86a9eSQu Wenruo cur = found_end + 1; 463136c86a9eSQu Wenruo } 463236c86a9eSQu Wenruo btrfs_warn(fs_info, 463336c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 463436c86a9eSQu Wenruo trans->transid, dirty_bytes); 463536c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 463636c86a9eSQu Wenruo 463736c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 463836c86a9eSQu Wenruo fs_info->running_transaction = NULL; 463936c86a9eSQu Wenruo list_del_init(&trans->list); 464036c86a9eSQu Wenruo 464136c86a9eSQu Wenruo btrfs_put_transaction(trans); 464236c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 464336c86a9eSQu Wenruo } 464436c86a9eSQu Wenruo ASSERT(!found); 464536c86a9eSQu Wenruo } 464636c86a9eSQu Wenruo 4647b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4648eb60ceacSChris Mason { 4649c146afadSYan Zheng int ret; 4650e089f05cSChris Mason 4651afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 465231e70e52SFilipe Manana 465331e70e52SFilipe Manana /* 46548a1f1e3dSFilipe Manana * If we had UNFINISHED_DROPS we could still be processing them, so 46558a1f1e3dSFilipe Manana * clear that bit and wake up relocation so it can stop. 46568a1f1e3dSFilipe Manana * We must do this before stopping the block group reclaim task, because 46578a1f1e3dSFilipe Manana * at btrfs_relocate_block_group() we wait for this bit, and after the 46588a1f1e3dSFilipe Manana * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we 46598a1f1e3dSFilipe Manana * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will 46608a1f1e3dSFilipe Manana * return 1. 46618a1f1e3dSFilipe Manana */ 46628a1f1e3dSFilipe Manana btrfs_wake_unfinished_drop(fs_info); 46638a1f1e3dSFilipe Manana 46648a1f1e3dSFilipe Manana /* 466531e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group, 466631e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park 466731e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after 466831e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang 466931e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is 467031e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when 467131e70e52SFilipe Manana * trying to stop the async reclaim task. 467231e70e52SFilipe Manana */ 467331e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work); 4674d6fd0ae2SOmar Sandoval /* 4675d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4676d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4677d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4678d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4679d6fd0ae2SOmar Sandoval */ 4680d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4681a2135011SChris Mason 46827343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4683d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 46847343dd61SJustin Maggard 4685803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4686803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4687803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4688803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4689803b2f54SStefan Behrens 4690837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4691aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4692837d5b6eSIlya Dryomov 46938dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 46948dabb742SStefan Behrens 4695aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 46964cb5300bSChris Mason 46974cb5300bSChris Mason /* wait for any defraggers to finish */ 46984cb5300bSChris Mason wait_event(fs_info->transaction_wait, 46994cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 47004cb5300bSChris Mason 47014cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 470226176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 47034cb5300bSChris Mason 4704a362bb86SFilipe Manana /* 4705a362bb86SFilipe Manana * After we parked the cleaner kthread, ordered extents may have 4706a362bb86SFilipe Manana * completed and created new delayed iputs. If one of the async reclaim 4707a362bb86SFilipe Manana * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we 4708a362bb86SFilipe Manana * can hang forever trying to stop it, because if a delayed iput is 4709a362bb86SFilipe Manana * added after it ran btrfs_run_delayed_iputs() and before it called 4710a362bb86SFilipe Manana * btrfs_wait_on_delayed_iputs(), it will hang forever since there is 4711a362bb86SFilipe Manana * no one else to run iputs. 4712a362bb86SFilipe Manana * 4713a362bb86SFilipe Manana * So wait for all ongoing ordered extents to complete and then run 4714a362bb86SFilipe Manana * delayed iputs. This works because once we reach this point no one 4715a362bb86SFilipe Manana * can either create new ordered extents nor create delayed iputs 4716a362bb86SFilipe Manana * through some other means. 4717a362bb86SFilipe Manana * 4718a362bb86SFilipe Manana * Also note that btrfs_wait_ordered_roots() is not safe here, because 4719a362bb86SFilipe Manana * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent, 4720a362bb86SFilipe Manana * but the delayed iput for the respective inode is made only when doing 4721a362bb86SFilipe Manana * the final btrfs_put_ordered_extent() (which must happen at 4722a362bb86SFilipe Manana * btrfs_finish_ordered_io() when we are unmounting). 4723a362bb86SFilipe Manana */ 4724a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_write_workers); 4725a362bb86SFilipe Manana /* Ordered extents for free space inodes. */ 4726a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_freespace_worker); 4727a362bb86SFilipe Manana btrfs_run_delayed_iputs(fs_info); 4728a362bb86SFilipe Manana 472921c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 473057056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4731576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 473221c7e756SMiao Xie 4733b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4734b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4735b0643e59SDennis Zhou 4736bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4737e44163e1SJeff Mahoney /* 4738d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4739d6fd0ae2SOmar Sandoval * unused block groups. 4740e44163e1SJeff Mahoney */ 47410b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4742e44163e1SJeff Mahoney 4743f0cc2cd7SFilipe Manana /* 4744f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4745f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4746f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4747f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4748f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4749f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4750f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4751f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4752f0cc2cd7SFilipe Manana * This is a very rare case. 4753f0cc2cd7SFilipe Manana */ 4754f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4755f0cc2cd7SFilipe Manana 47566bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4757d397712bSChris Mason if (ret) 475804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4759c146afadSYan Zheng } 4760ed2ff2cbSChris Mason 476184961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 47622ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4763acce952bSliubo 4764e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4765e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 47668929ecfaSYan, Zheng 4767e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4768afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4769f25784b3SYan Zheng 47705958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 47715958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 47725958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 47735958253cSQu Wenruo } 47745958253cSQu Wenruo 477504892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4776fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4777bcef60f2SArne Jansen 4778963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 477904892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4780963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4781b0c68f8bSChris Mason } 478231153d81SYan Zheng 47835deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 47844297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 47855deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 47864297ff84SJosef Bacik 47876618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4788b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 47895ac1d209SJeff Mahoney 47901a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 47911a4319ccSLiu Bo 4792de348ee0SWang Shilong /* 4793de348ee0SWang Shilong * we must make sure there is not any read request to 4794de348ee0SWang Shilong * submit after we stopping all workers. 4795de348ee0SWang Shilong */ 4796de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 479796192499SJosef Bacik btrfs_stop_all_workers(fs_info); 479896192499SJosef Bacik 47990a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 480036c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 48010a31daa4SFilipe Manana 4802afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 48034273eaffSAnand Jain free_root_pointers(fs_info, true); 48048c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 48059ad6b7bcSChris Mason 48064e19443dSJosef Bacik /* 48074e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 48084e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 48094e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 48104e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 48114e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 48124e19443dSJosef Bacik */ 48134e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 48144e19443dSJosef Bacik 481513e6c37bSJosef Bacik iput(fs_info->btree_inode); 4816d6bfde87SChris Mason 481721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 48180b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 48192ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 482021adbd5cSStefan Behrens #endif 482121adbd5cSStefan Behrens 48220b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 482368c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4824eb60ceacSChris Mason } 4825eb60ceacSChris Mason 4826b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4827b9fab919SChris Mason int atomic) 4828ccd467d6SChris Mason { 48291259ab75SChris Mason int ret; 4830727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 48311259ab75SChris Mason 48320b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 48331259ab75SChris Mason if (!ret) 48341259ab75SChris Mason return ret; 48351259ab75SChris Mason 48361259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4837b9fab919SChris Mason parent_transid, atomic); 4838b9fab919SChris Mason if (ret == -EAGAIN) 4839b9fab919SChris Mason return ret; 48401259ab75SChris Mason return !ret; 48415f39d397SChris Mason } 48426702ed49SChris Mason 48435f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 48445f39d397SChris Mason { 48452f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 48465f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4847b9473439SChris Mason int was_dirty; 4848b4ce94deSChris Mason 484906ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 485006ea65a3SJosef Bacik /* 485106ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 485252042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 485306ea65a3SJosef Bacik * outside of the sanity tests. 485406ea65a3SJosef Bacik */ 4855b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 485606ea65a3SJosef Bacik return; 485706ea65a3SJosef Bacik #endif 485849d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 48590b246afaSJeff Mahoney if (transid != fs_info->generation) 48605d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 48610b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 48620b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4863e2d84521SMiao Xie if (!was_dirty) 4864104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4865e2d84521SMiao Xie buf->len, 48660b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 48671f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 486869fc6cbbSQu Wenruo /* 486969fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 487069fc6cbbSQu Wenruo * but item data not updated. 487169fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 487269fc6cbbSQu Wenruo */ 487369fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4874cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4875a4f78750SDavid Sterba btrfs_print_leaf(buf); 48761f21ef0aSFilipe Manana ASSERT(0); 48771f21ef0aSFilipe Manana } 48781f21ef0aSFilipe Manana #endif 4879eb60ceacSChris Mason } 4880eb60ceacSChris Mason 48812ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4882b53d3f5dSLiu Bo int flush_delayed) 488335b7e476SChris Mason { 4884188de649SChris Mason /* 4885188de649SChris Mason * looks as though older kernels can get into trouble with 4886188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4887188de649SChris Mason */ 4888e2d84521SMiao Xie int ret; 4889d6bfde87SChris Mason 48906933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4891d6bfde87SChris Mason return; 4892d6bfde87SChris Mason 4893b53d3f5dSLiu Bo if (flush_delayed) 48942ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 489516cdcec7SMiao Xie 4896d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4897d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4898d814a491SEthan Lien fs_info->dirty_metadata_batch); 4899e2d84521SMiao Xie if (ret > 0) { 49000b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 490116cdcec7SMiao Xie } 490216cdcec7SMiao Xie } 490316cdcec7SMiao Xie 49042ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 490516cdcec7SMiao Xie { 49062ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 490735b7e476SChris Mason } 4908b53d3f5dSLiu Bo 49092ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4910b53d3f5dSLiu Bo { 49112ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4912d6bfde87SChris Mason } 49136b80053dSChris Mason 49142ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4915acce952bSliubo { 4916fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4917fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4918fe816d0fSNikolay Borisov 49190b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 49202ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 49210b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4922acce952bSliubo 49230b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 49240b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4925acce952bSliubo } 4926acce952bSliubo 4927ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4928ef67963dSJosef Bacik { 4929fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4930fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4931fc7cbcd4SDavid Sterba int ret; 4932ef67963dSJosef Bacik 4933fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4934fc7cbcd4SDavid Sterba while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4935fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4936fc7cbcd4SDavid Sterba ARRAY_SIZE(gang))) != 0) { 4937fc7cbcd4SDavid Sterba int i; 4938ef67963dSJosef Bacik 4939fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 4940fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4941fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4942fc7cbcd4SDavid Sterba 4943fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4944fc7cbcd4SDavid Sterba if (!gang[i]) 4945ef67963dSJosef Bacik continue; 4946fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4947fc7cbcd4SDavid Sterba btrfs_free_log(NULL, gang[i]); 4948fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4949ef67963dSJosef Bacik } 4950fc7cbcd4SDavid Sterba root_objectid++; 4951fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4952ef67963dSJosef Bacik } 4953fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4954ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4955ef67963dSJosef Bacik } 4956ef67963dSJosef Bacik 4957143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4958acce952bSliubo { 4959acce952bSliubo struct btrfs_ordered_extent *ordered; 4960acce952bSliubo 4961199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4962779880efSJosef Bacik /* 4963779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4964779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4965779880efSJosef Bacik */ 4966199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4967779880efSJosef Bacik root_extent_list) 4968779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4969199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4970199c2a9cSMiao Xie } 4971199c2a9cSMiao Xie 4972199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4973199c2a9cSMiao Xie { 4974199c2a9cSMiao Xie struct btrfs_root *root; 4975199c2a9cSMiao Xie struct list_head splice; 4976199c2a9cSMiao Xie 4977199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4978199c2a9cSMiao Xie 4979199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4980199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4981199c2a9cSMiao Xie while (!list_empty(&splice)) { 4982199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4983199c2a9cSMiao Xie ordered_root); 49841de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 49851de2cfdeSJosef Bacik &fs_info->ordered_roots); 4986199c2a9cSMiao Xie 49872a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4988199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4989199c2a9cSMiao Xie 49902a85d9caSLiu Bo cond_resched(); 49912a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4992199c2a9cSMiao Xie } 4993199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 499474d5d229SJosef Bacik 499574d5d229SJosef Bacik /* 499674d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 499774d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 499874d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 499974d5d229SJosef Bacik * actually get run and error out properly. 500074d5d229SJosef Bacik */ 500174d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 5002acce952bSliubo } 5003acce952bSliubo 500435a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 50052ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5006acce952bSliubo { 5007acce952bSliubo struct rb_node *node; 5008acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 5009acce952bSliubo struct btrfs_delayed_ref_node *ref; 5010acce952bSliubo int ret = 0; 5011acce952bSliubo 5012acce952bSliubo delayed_refs = &trans->delayed_refs; 5013acce952bSliubo 5014acce952bSliubo spin_lock(&delayed_refs->lock); 5015d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 5016cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 5017b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 5018acce952bSliubo return ret; 5019acce952bSliubo } 5020acce952bSliubo 50215c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 5022d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 50230e0adbcfSJosef Bacik struct rb_node *n; 5024e78417d1SJosef Bacik bool pin_bytes = false; 5025acce952bSliubo 5026d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 5027d7df2c79SJosef Bacik href_node); 50283069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 5029b939d1abSJosef Bacik continue; 50303069bd26SJosef Bacik 5031d7df2c79SJosef Bacik spin_lock(&head->lock); 5032e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 50330e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 50340e0adbcfSJosef Bacik ref_node); 5035d7df2c79SJosef Bacik ref->in_tree = 0; 5036e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 50370e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 50381d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 50391d57ee94SWang Xiaoguang list_del(&ref->add_list); 5040d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 5041d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 5042d7df2c79SJosef Bacik } 504354067ae9SJosef Bacik if (head->must_insert_reserved) 5044e78417d1SJosef Bacik pin_bytes = true; 504578a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 5046fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 5047d7df2c79SJosef Bacik spin_unlock(&head->lock); 5048acce952bSliubo spin_unlock(&delayed_refs->lock); 5049e78417d1SJosef Bacik mutex_unlock(&head->mutex); 5050acce952bSliubo 5051f603bb94SNikolay Borisov if (pin_bytes) { 5052f603bb94SNikolay Borisov struct btrfs_block_group *cache; 5053f603bb94SNikolay Borisov 5054f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 5055f603bb94SNikolay Borisov BUG_ON(!cache); 5056f603bb94SNikolay Borisov 5057f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 5058f603bb94SNikolay Borisov spin_lock(&cache->lock); 5059f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 5060f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 5061f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 5062f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 5063f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 5064f603bb94SNikolay Borisov spin_unlock(&cache->lock); 5065f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 5066f603bb94SNikolay Borisov 5067f603bb94SNikolay Borisov btrfs_put_block_group(cache); 5068f603bb94SNikolay Borisov 5069f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 5070f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 5071f603bb94SNikolay Borisov } 507231890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 5073d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 5074acce952bSliubo cond_resched(); 5075acce952bSliubo spin_lock(&delayed_refs->lock); 5076acce952bSliubo } 507781f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 5078acce952bSliubo 5079acce952bSliubo spin_unlock(&delayed_refs->lock); 5080acce952bSliubo 5081acce952bSliubo return ret; 5082acce952bSliubo } 5083acce952bSliubo 5084143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 5085acce952bSliubo { 5086acce952bSliubo struct btrfs_inode *btrfs_inode; 5087acce952bSliubo struct list_head splice; 5088acce952bSliubo 5089acce952bSliubo INIT_LIST_HEAD(&splice); 5090acce952bSliubo 5091eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5092eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 5093acce952bSliubo 5094acce952bSliubo while (!list_empty(&splice)) { 5095fe816d0fSNikolay Borisov struct inode *inode = NULL; 5096eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 5097acce952bSliubo delalloc_inodes); 5098fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 5099eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5100acce952bSliubo 5101fe816d0fSNikolay Borisov /* 5102fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 5103fe816d0fSNikolay Borisov * meanwhile. 5104fe816d0fSNikolay Borisov */ 5105fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 5106fe816d0fSNikolay Borisov if (inode) { 5107fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 5108fe816d0fSNikolay Borisov iput(inode); 5109fe816d0fSNikolay Borisov } 5110eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5111acce952bSliubo } 5112eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5113eb73c1b7SMiao Xie } 5114eb73c1b7SMiao Xie 5115eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 5116eb73c1b7SMiao Xie { 5117eb73c1b7SMiao Xie struct btrfs_root *root; 5118eb73c1b7SMiao Xie struct list_head splice; 5119eb73c1b7SMiao Xie 5120eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 5121eb73c1b7SMiao Xie 5122eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5123eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 5124eb73c1b7SMiao Xie while (!list_empty(&splice)) { 5125eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 5126eb73c1b7SMiao Xie delalloc_root); 512700246528SJosef Bacik root = btrfs_grab_root(root); 5128eb73c1b7SMiao Xie BUG_ON(!root); 5129eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5130eb73c1b7SMiao Xie 5131eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 513200246528SJosef Bacik btrfs_put_root(root); 5133eb73c1b7SMiao Xie 5134eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5135eb73c1b7SMiao Xie } 5136eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5137acce952bSliubo } 5138acce952bSliubo 51392ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5140acce952bSliubo struct extent_io_tree *dirty_pages, 5141acce952bSliubo int mark) 5142acce952bSliubo { 5143acce952bSliubo int ret; 5144acce952bSliubo struct extent_buffer *eb; 5145acce952bSliubo u64 start = 0; 5146acce952bSliubo u64 end; 5147acce952bSliubo 5148acce952bSliubo while (1) { 5149acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5150e6138876SJosef Bacik mark, NULL); 5151acce952bSliubo if (ret) 5152acce952bSliubo break; 5153acce952bSliubo 515491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5155acce952bSliubo while (start <= end) { 51560b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 51570b246afaSJeff Mahoney start += fs_info->nodesize; 5158fd8b2b61SJosef Bacik if (!eb) 5159acce952bSliubo continue; 5160fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5161acce952bSliubo 5162fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5163fd8b2b61SJosef Bacik &eb->bflags)) 5164fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5165fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5166acce952bSliubo } 5167acce952bSliubo } 5168acce952bSliubo 5169acce952bSliubo return ret; 5170acce952bSliubo } 5171acce952bSliubo 51722ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5173fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5174acce952bSliubo { 5175acce952bSliubo u64 start; 5176acce952bSliubo u64 end; 5177acce952bSliubo int ret; 5178acce952bSliubo 5179acce952bSliubo while (1) { 51800e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 51810e6ec385SFilipe Manana 5182fcd5e742SLu Fengqi /* 5183fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5184fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5185fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5186fcd5e742SLu Fengqi * the same extent range. 5187fcd5e742SLu Fengqi */ 5188fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5189acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 51900e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5191fcd5e742SLu Fengqi if (ret) { 5192fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5193acce952bSliubo break; 5194fcd5e742SLu Fengqi } 5195acce952bSliubo 51960e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 51970e6ec385SFilipe Manana free_extent_state(cached_state); 51982ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5199fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5200acce952bSliubo cond_resched(); 5201acce952bSliubo } 5202acce952bSliubo 5203acce952bSliubo return 0; 5204acce952bSliubo } 5205acce952bSliubo 520632da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5207c79a1751SLiu Bo { 5208c79a1751SLiu Bo struct inode *inode; 5209c79a1751SLiu Bo 5210c79a1751SLiu Bo inode = cache->io_ctl.inode; 5211c79a1751SLiu Bo if (inode) { 5212c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5213c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5214c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5215c79a1751SLiu Bo iput(inode); 5216c79a1751SLiu Bo } 5217bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5218c79a1751SLiu Bo btrfs_put_block_group(cache); 5219c79a1751SLiu Bo } 5220c79a1751SLiu Bo 5221c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 52222ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5223c79a1751SLiu Bo { 522432da5386SDavid Sterba struct btrfs_block_group *cache; 5225c79a1751SLiu Bo 5226c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5227c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5228c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 522932da5386SDavid Sterba struct btrfs_block_group, 5230c79a1751SLiu Bo dirty_list); 5231c79a1751SLiu Bo 5232c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5233c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5234c79a1751SLiu Bo list_del_init(&cache->io_list); 5235c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5236c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5237c79a1751SLiu Bo } 5238c79a1751SLiu Bo 5239c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5240c79a1751SLiu Bo spin_lock(&cache->lock); 5241c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5242c79a1751SLiu Bo spin_unlock(&cache->lock); 5243c79a1751SLiu Bo 5244c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5245c79a1751SLiu Bo btrfs_put_block_group(cache); 5246ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5247c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5248c79a1751SLiu Bo } 5249c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5250c79a1751SLiu Bo 525145ae2c18SNikolay Borisov /* 525245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 525345ae2c18SNikolay Borisov * to use it without any locking 525445ae2c18SNikolay Borisov */ 5255c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5256c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 525732da5386SDavid Sterba struct btrfs_block_group, 5258c79a1751SLiu Bo io_list); 5259c79a1751SLiu Bo 5260c79a1751SLiu Bo list_del_init(&cache->io_list); 5261c79a1751SLiu Bo spin_lock(&cache->lock); 5262c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5263c79a1751SLiu Bo spin_unlock(&cache->lock); 5264c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5265c79a1751SLiu Bo } 5266c79a1751SLiu Bo } 5267c79a1751SLiu Bo 526849b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 52692ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 527049b25e05SJeff Mahoney { 5271bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5272bbbf7243SNikolay Borisov 52732ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5274c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5275c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5276c79a1751SLiu Bo 5277bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5278bbbf7243SNikolay Borisov post_commit_list) { 5279bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5280bbbf7243SNikolay Borisov } 5281bbbf7243SNikolay Borisov 52822ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 528349b25e05SJeff Mahoney 52844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 52850b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 528649b25e05SJeff Mahoney 52874a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 52880b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 528949b25e05SJeff Mahoney 5290ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 529167cde344SMiao Xie 52922ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 529349b25e05SJeff Mahoney EXTENT_DIRTY); 5294fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 529549b25e05SJeff Mahoney 5296d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5297d3575156SNaohiro Aota 52984a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 52994a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 530049b25e05SJeff Mahoney } 530149b25e05SJeff Mahoney 53022ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5303acce952bSliubo { 5304acce952bSliubo struct btrfs_transaction *t; 5305acce952bSliubo 53060b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5307acce952bSliubo 53080b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 53090b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 53100b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5311724e2315SJosef Bacik struct btrfs_transaction, list); 5312724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 53139b64f57dSElena Reshetova refcount_inc(&t->use_count); 53140b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 53152ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5316724e2315SJosef Bacik btrfs_put_transaction(t); 53170b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5318724e2315SJosef Bacik continue; 5319724e2315SJosef Bacik } 53200b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5321724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 53220b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5323724e2315SJosef Bacik /* 5324724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5325724e2315SJosef Bacik * handle open currently for this transaction. 5326724e2315SJosef Bacik */ 5327724e2315SJosef Bacik wait_event(t->writer_wait, 5328724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5329724e2315SJosef Bacik } else { 53300b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5331724e2315SJosef Bacik } 53322ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5333724e2315SJosef Bacik 53340b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 53350b246afaSJeff Mahoney if (t == fs_info->running_transaction) 53360b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5337724e2315SJosef Bacik list_del_init(&t->list); 53380b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5339a4abeea4SJosef Bacik 5340724e2315SJosef Bacik btrfs_put_transaction(t); 53412e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 53420b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5343724e2315SJosef Bacik } 53440b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 53450b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5346ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5347ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 53480b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5349ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 53500b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5351acce952bSliubo 5352acce952bSliubo return 0; 5353acce952bSliubo } 5354ec7d6dfdSNikolay Borisov 5355453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5356ec7d6dfdSNikolay Borisov { 5357ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5358ec7d6dfdSNikolay Borisov int ret; 5359ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5360ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5361ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5362ec7d6dfdSNikolay Borisov int slot; 5363ec7d6dfdSNikolay Borisov 5364ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5365ec7d6dfdSNikolay Borisov if (!path) 5366ec7d6dfdSNikolay Borisov return -ENOMEM; 5367ec7d6dfdSNikolay Borisov 5368ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5369ec7d6dfdSNikolay Borisov search_key.type = -1; 5370ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5371ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5372ec7d6dfdSNikolay Borisov if (ret < 0) 5373ec7d6dfdSNikolay Borisov goto error; 5374ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5375ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5376ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5377ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5378ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 537923125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 538023125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5381ec7d6dfdSNikolay Borisov } else { 538223125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5383ec7d6dfdSNikolay Borisov } 5384ec7d6dfdSNikolay Borisov ret = 0; 5385ec7d6dfdSNikolay Borisov error: 5386ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5387ec7d6dfdSNikolay Borisov return ret; 5388ec7d6dfdSNikolay Borisov } 5389ec7d6dfdSNikolay Borisov 5390543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5391ec7d6dfdSNikolay Borisov { 5392ec7d6dfdSNikolay Borisov int ret; 5393ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5394ec7d6dfdSNikolay Borisov 53956b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5396ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5397ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5398ec7d6dfdSNikolay Borisov root->root_key.objectid); 5399ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5400ec7d6dfdSNikolay Borisov goto out; 5401ec7d6dfdSNikolay Borisov } 5402ec7d6dfdSNikolay Borisov 540323125104SNikolay Borisov *objectid = root->free_objectid++; 5404ec7d6dfdSNikolay Borisov ret = 0; 5405ec7d6dfdSNikolay Borisov out: 5406ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5407ec7d6dfdSNikolay Borisov return ret; 5408ec7d6dfdSNikolay Borisov } 5409