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" 260b86a832SChris Mason #include "volumes.h" 27db94535dSChris Mason #include "print-tree.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30fa9c0d79SChris Mason #include "free-space-cache.h" 3170f6d82eSOmar Sandoval #include "free-space-tree.h" 3221adbd5cSStefan Behrens #include "check-integrity.h" 33606686eeSJosef Bacik #include "rcu-string.h" 348dabb742SStefan Behrens #include "dev-replace.h" 3553b381b3SDavid Woodhouse #include "raid56.h" 365ac1d209SJeff Mahoney #include "sysfs.h" 37fcebe456SJosef Bacik #include "qgroup.h" 38ebb8765bSAnand Jain #include "compression.h" 39557ea5ddSQu Wenruo #include "tree-checker.h" 40fd708b81SJosef Bacik #include "ref-verify.h" 41aac0023cSJosef Bacik #include "block-group.h" 42b0643e59SDennis Zhou #include "discard.h" 43f603bb94SNikolay Borisov #include "space-info.h" 44b70f5097SNaohiro Aota #include "zoned.h" 45139e8cd3SQu Wenruo #include "subpage.h" 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" 55eb60ceacSChris Mason 56319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 57319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 58319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 59319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 60e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 61e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 62319e4d06SQu Wenruo 63143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 64acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 652ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 66143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 672ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 68acce952bSliubo struct extent_io_tree *dirty_pages, 69acce952bSliubo int mark); 702ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 71acce952bSliubo struct extent_io_tree *pinned_extents); 722ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 732ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 74ce9adaa5SChris Mason 75141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) 76141386e1SJosef Bacik { 77141386e1SJosef Bacik if (fs_info->csum_shash) 78141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash); 79141386e1SJosef Bacik } 80141386e1SJosef Bacik 81d352ac68SChris Mason /* 82d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 83d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 84d352ac68SChris Mason * just before they are sent down the IO stack. 85d352ac68SChris Mason */ 8644b8bd7eSChris Mason struct async_submit_bio { 87da67daabSDavid Sterba struct btrfs_inode *inode; 8844b8bd7eSChris Mason struct bio *bio; 89ab2072b2SDavid Sterba enum btrfs_wq_submit_cmd submit_cmd; 9044b8bd7eSChris Mason int mirror_num; 911941b64bSQu Wenruo 92ab2072b2SDavid Sterba /* Optional parameter for used by direct io */ 931941b64bSQu Wenruo u64 dio_file_offset; 948b712842SChris Mason struct btrfs_work work; 954e4cbee9SChristoph Hellwig blk_status_t status; 9644b8bd7eSChris Mason }; 9744b8bd7eSChris Mason 9885d4e461SChris Mason /* 992996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer. 100d352ac68SChris Mason */ 101c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result) 10219c00ddcSChris Mason { 103d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info; 1047280305eSDavid Sterba const int num_pages = num_extent_pages(buf); 105a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); 106d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 10719c00ddcSChris Mason char *kaddr; 108e9be5a30SDavid Sterba int i; 109d5178578SJohannes Thumshirn 110d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 111d5178578SJohannes Thumshirn crypto_shash_init(shash); 112a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); 113e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, 114a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE); 11519c00ddcSChris Mason 116e9be5a30SDavid Sterba for (i = 1; i < num_pages; i++) { 117e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]); 118e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE); 11919c00ddcSChris Mason } 12071a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 121d5178578SJohannes Thumshirn crypto_shash_final(shash, result); 12219c00ddcSChris Mason } 12319c00ddcSChris Mason 124d352ac68SChris Mason /* 125d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 126d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 127d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 128d352ac68SChris Mason * in the wrong place. 129d352ac68SChris Mason */ 1301259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 131b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 132b9fab919SChris Mason int atomic) 1331259ab75SChris Mason { 1342ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 1351259ab75SChris Mason int ret; 1361259ab75SChris Mason 1371259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 1381259ab75SChris Mason return 0; 1391259ab75SChris Mason 140b9fab919SChris Mason if (atomic) 141b9fab919SChris Mason return -EAGAIN; 142b9fab919SChris Mason 143570eb97bSJosef Bacik lock_extent(io_tree, eb->start, eb->start + eb->len - 1, &cached_state); 1440b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 1451259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 1461259ab75SChris Mason ret = 0; 1471259ab75SChris Mason goto out; 1481259ab75SChris Mason } 14994647322SDavid Sterba btrfs_err_rl(eb->fs_info, 1508f0ed7d4SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu", 1518f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 15229549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 1531259ab75SChris Mason ret = 1; 1540b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 15533958dc6SChris Mason out: 156570eb97bSJosef Bacik unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, 157e43bbe5eSDavid Sterba &cached_state); 1581259ab75SChris Mason return ret; 1591259ab75SChris Mason } 1601259ab75SChris Mason 161e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type) 162e7e16f48SJohannes Thumshirn { 163e7e16f48SJohannes Thumshirn switch (csum_type) { 164e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32: 1653951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH: 1663831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256: 167352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2: 168e7e16f48SJohannes Thumshirn return true; 169e7e16f48SJohannes Thumshirn default: 170e7e16f48SJohannes Thumshirn return false; 171e7e16f48SJohannes Thumshirn } 172e7e16f48SJohannes Thumshirn } 173e7e16f48SJohannes Thumshirn 174d352ac68SChris Mason /* 1751104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 1761104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 1771104a885SDavid Sterba */ 1783d17adeaSQu Wenruo int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 1793d17adeaSQu Wenruo const struct btrfs_super_block *disk_sb) 1801104a885SDavid Sterba { 18151bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE]; 182d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 183d5178578SJohannes Thumshirn 184d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 1851104a885SDavid Sterba 1861104a885SDavid Sterba /* 1871104a885SDavid Sterba * The super_block structure does not span the whole 18851bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is 18951bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum. 1901104a885SDavid Sterba */ 1913d17adeaSQu Wenruo crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE, 192fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result); 1931104a885SDavid Sterba 19455fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size)) 195e7e16f48SJohannes Thumshirn return 1; 1961104a885SDavid Sterba 197e7e16f48SJohannes Thumshirn return 0; 1981104a885SDavid Sterba } 1991104a885SDavid Sterba 200e064d5e9SDavid Sterba int btrfs_verify_level_key(struct extent_buffer *eb, int level, 201ff76a864SLiu Bo struct btrfs_key *first_key, u64 parent_transid) 202581c1760SQu Wenruo { 203e064d5e9SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 204581c1760SQu Wenruo int found_level; 205581c1760SQu Wenruo struct btrfs_key found_key; 206581c1760SQu Wenruo int ret; 207581c1760SQu Wenruo 208581c1760SQu Wenruo found_level = btrfs_header_level(eb); 209581c1760SQu Wenruo if (found_level != level) { 21063489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 21163489055SQu Wenruo KERN_ERR "BTRFS: tree level check failed\n"); 212581c1760SQu Wenruo btrfs_err(fs_info, 213581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 214581c1760SQu Wenruo eb->start, level, found_level); 215581c1760SQu Wenruo return -EIO; 216581c1760SQu Wenruo } 217581c1760SQu Wenruo 218581c1760SQu Wenruo if (!first_key) 219581c1760SQu Wenruo return 0; 220581c1760SQu Wenruo 2215d41be6fSQu Wenruo /* 2225d41be6fSQu Wenruo * For live tree block (new tree blocks in current transaction), 2235d41be6fSQu Wenruo * we need proper lock context to avoid race, which is impossible here. 2245d41be6fSQu Wenruo * So we only checks tree blocks which is read from disk, whose 2255d41be6fSQu Wenruo * generation <= fs_info->last_trans_committed. 2265d41be6fSQu Wenruo */ 2275d41be6fSQu Wenruo if (btrfs_header_generation(eb) > fs_info->last_trans_committed) 2285d41be6fSQu Wenruo return 0; 22962fdaa52SQu Wenruo 23062fdaa52SQu Wenruo /* We have @first_key, so this @eb must have at least one item */ 23162fdaa52SQu Wenruo if (btrfs_header_nritems(eb) == 0) { 23262fdaa52SQu Wenruo btrfs_err(fs_info, 23362fdaa52SQu Wenruo "invalid tree nritems, bytenr=%llu nritems=0 expect >0", 23462fdaa52SQu Wenruo eb->start); 23562fdaa52SQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 23662fdaa52SQu Wenruo return -EUCLEAN; 23762fdaa52SQu Wenruo } 23862fdaa52SQu Wenruo 239581c1760SQu Wenruo if (found_level) 240581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 241581c1760SQu Wenruo else 242581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 243581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 244581c1760SQu Wenruo 245581c1760SQu Wenruo if (ret) { 24663489055SQu Wenruo WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), 24763489055SQu Wenruo KERN_ERR "BTRFS: tree first key check failed\n"); 248581c1760SQu Wenruo btrfs_err(fs_info, 249ff76a864SLiu Bo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 250ff76a864SLiu Bo eb->start, parent_transid, first_key->objectid, 251ff76a864SLiu Bo first_key->type, first_key->offset, 252ff76a864SLiu Bo found_key.objectid, found_key.type, 253ff76a864SLiu Bo found_key.offset); 254581c1760SQu Wenruo } 255581c1760SQu Wenruo return ret; 256581c1760SQu Wenruo } 257581c1760SQu Wenruo 2581104a885SDavid Sterba /* 259d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 260d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 261581c1760SQu Wenruo * 262789d6a3aSQu Wenruo * @check: expected tree parentness check, see the comments of the 263789d6a3aSQu Wenruo * structure for details. 264d352ac68SChris Mason */ 2656a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb, 266789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check) 267f188591eSChris Mason { 2685ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 269ea466794SJosef Bacik int failed = 0; 270f188591eSChris Mason int ret; 271f188591eSChris Mason int num_copies = 0; 272f188591eSChris Mason int mirror_num = 0; 273ea466794SJosef Bacik int failed_mirror = 0; 274f188591eSChris Mason 275789d6a3aSQu Wenruo ASSERT(check); 276789d6a3aSQu Wenruo 277f188591eSChris Mason while (1) { 278f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 279*947a6299SQu Wenruo ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check); 280*947a6299SQu Wenruo if (!ret) 281581c1760SQu Wenruo break; 282d397712bSChris Mason 2830b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 284f188591eSChris Mason eb->start, eb->len); 2854235298eSChris Mason if (num_copies == 1) 286ea466794SJosef Bacik break; 2874235298eSChris Mason 2885cf1ab56SJosef Bacik if (!failed_mirror) { 2895cf1ab56SJosef Bacik failed = 1; 2905cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 2915cf1ab56SJosef Bacik } 2925cf1ab56SJosef Bacik 293f188591eSChris Mason mirror_num++; 294ea466794SJosef Bacik if (mirror_num == failed_mirror) 295ea466794SJosef Bacik mirror_num++; 296ea466794SJosef Bacik 2974235298eSChris Mason if (mirror_num > num_copies) 298ea466794SJosef Bacik break; 299f188591eSChris Mason } 300ea466794SJosef Bacik 301c0901581SStefan Behrens if (failed && !ret && failed_mirror) 30220a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror); 303ea466794SJosef Bacik 304ea466794SJosef Bacik return ret; 305f188591eSChris Mason } 30619c00ddcSChris Mason 307eca0f6f6SQu Wenruo static int csum_one_extent_buffer(struct extent_buffer *eb) 308eca0f6f6SQu Wenruo { 309eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 310eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE]; 311eca0f6f6SQu Wenruo int ret; 312eca0f6f6SQu Wenruo 313eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid, 314eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid), 315eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0); 316eca0f6f6SQu Wenruo csum_tree_block(eb, result); 317eca0f6f6SQu Wenruo 318eca0f6f6SQu Wenruo if (btrfs_header_level(eb)) 319eca0f6f6SQu Wenruo ret = btrfs_check_node(eb); 320eca0f6f6SQu Wenruo else 321eca0f6f6SQu Wenruo ret = btrfs_check_leaf_full(eb); 322eca0f6f6SQu Wenruo 3233777369fSQu Wenruo if (ret < 0) 3243777369fSQu Wenruo goto error; 3253777369fSQu Wenruo 3263777369fSQu Wenruo /* 3273777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than 3283777369fSQu Wenruo * last committed. Or something seriously wrong happened. 3293777369fSQu Wenruo */ 3303777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) { 3313777369fSQu Wenruo ret = -EUCLEAN; 332eca0f6f6SQu Wenruo btrfs_err(fs_info, 3333777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu", 3343777369fSQu Wenruo eb->start, btrfs_header_generation(eb), 3353777369fSQu Wenruo fs_info->last_trans_committed); 3363777369fSQu Wenruo goto error; 337eca0f6f6SQu Wenruo } 338eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size); 339eca0f6f6SQu Wenruo 340eca0f6f6SQu Wenruo return 0; 3413777369fSQu Wenruo 3423777369fSQu Wenruo error: 3433777369fSQu Wenruo btrfs_print_tree(eb, 0); 3443777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected", 3453777369fSQu Wenruo eb->start); 3463777369fSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 3473777369fSQu Wenruo return ret; 348eca0f6f6SQu Wenruo } 349eca0f6f6SQu Wenruo 350eca0f6f6SQu Wenruo /* Checksum all dirty extent buffers in one bio_vec */ 351eca0f6f6SQu Wenruo static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, 352eca0f6f6SQu Wenruo struct bio_vec *bvec) 353eca0f6f6SQu Wenruo { 354eca0f6f6SQu Wenruo struct page *page = bvec->bv_page; 355eca0f6f6SQu Wenruo u64 bvec_start = page_offset(page) + bvec->bv_offset; 356eca0f6f6SQu Wenruo u64 cur; 357eca0f6f6SQu Wenruo int ret = 0; 358eca0f6f6SQu Wenruo 359eca0f6f6SQu Wenruo for (cur = bvec_start; cur < bvec_start + bvec->bv_len; 360eca0f6f6SQu Wenruo cur += fs_info->nodesize) { 361eca0f6f6SQu Wenruo struct extent_buffer *eb; 362eca0f6f6SQu Wenruo bool uptodate; 363eca0f6f6SQu Wenruo 364eca0f6f6SQu Wenruo eb = find_extent_buffer(fs_info, cur); 365eca0f6f6SQu Wenruo uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, 366eca0f6f6SQu Wenruo fs_info->nodesize); 367eca0f6f6SQu Wenruo 36801cd3909SDavid Sterba /* A dirty eb shouldn't disappear from buffer_radix */ 369eca0f6f6SQu Wenruo if (WARN_ON(!eb)) 370eca0f6f6SQu Wenruo return -EUCLEAN; 371eca0f6f6SQu Wenruo 372eca0f6f6SQu Wenruo if (WARN_ON(cur != btrfs_header_bytenr(eb))) { 373eca0f6f6SQu Wenruo free_extent_buffer(eb); 374eca0f6f6SQu Wenruo return -EUCLEAN; 375eca0f6f6SQu Wenruo } 376eca0f6f6SQu Wenruo if (WARN_ON(!uptodate)) { 377eca0f6f6SQu Wenruo free_extent_buffer(eb); 378eca0f6f6SQu Wenruo return -EUCLEAN; 379eca0f6f6SQu Wenruo } 380eca0f6f6SQu Wenruo 381eca0f6f6SQu Wenruo ret = csum_one_extent_buffer(eb); 382eca0f6f6SQu Wenruo free_extent_buffer(eb); 383eca0f6f6SQu Wenruo if (ret < 0) 384eca0f6f6SQu Wenruo return ret; 385eca0f6f6SQu Wenruo } 386eca0f6f6SQu Wenruo return ret; 387eca0f6f6SQu Wenruo } 388eca0f6f6SQu Wenruo 389d352ac68SChris Mason /* 390ac303b69SQu Wenruo * Checksum a dirty tree block before IO. This has extra checks to make sure 391ac303b69SQu Wenruo * we only fill in the checksum field in the first page of a multi-page block. 392ac303b69SQu Wenruo * For subpage extent buffers we need bvec to also read the offset in the page. 393d352ac68SChris Mason */ 394ac303b69SQu Wenruo static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct bio_vec *bvec) 39519c00ddcSChris Mason { 396ac303b69SQu Wenruo struct page *page = bvec->bv_page; 3974eee4fa4SMiao Xie u64 start = page_offset(page); 39819c00ddcSChris Mason u64 found_start; 39919c00ddcSChris Mason struct extent_buffer *eb; 400eca0f6f6SQu Wenruo 401fbca46ebSQu Wenruo if (fs_info->nodesize < PAGE_SIZE) 402eca0f6f6SQu Wenruo return csum_dirty_subpage_buffers(fs_info, bvec); 403f188591eSChris Mason 4044f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4054f2de97aSJosef Bacik if (page != eb->pages[0]) 4064f2de97aSJosef Bacik return 0; 4070f805531SAlex Lyakas 40819c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 409d3575156SNaohiro Aota 410d3575156SNaohiro Aota if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) { 411d3575156SNaohiro Aota WARN_ON(found_start != 0); 412d3575156SNaohiro Aota return 0; 413d3575156SNaohiro Aota } 414d3575156SNaohiro Aota 4150f805531SAlex Lyakas /* 4160f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 4170f805531SAlex Lyakas * It is useful to know what went wrong. 4180f805531SAlex Lyakas */ 4190f805531SAlex Lyakas if (WARN_ON(found_start != start)) 4200f805531SAlex Lyakas return -EUCLEAN; 4210f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 4220f805531SAlex Lyakas return -EUCLEAN; 4230f805531SAlex Lyakas 424eca0f6f6SQu Wenruo return csum_one_extent_buffer(eb); 42519c00ddcSChris Mason } 42619c00ddcSChris Mason 427b0c9b3b0SDavid Sterba static int check_tree_block_fsid(struct extent_buffer *eb) 4282b82032cSYan Zheng { 429b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info; 430944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; 43144880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 432944d3f9fSNikolay Borisov u8 *metadata_uuid; 4332b82032cSYan Zheng 4349a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid), 4359a8658e3SDavid Sterba BTRFS_FSID_SIZE); 4367239ff4bSNikolay Borisov /* 437944d3f9fSNikolay Borisov * Checking the incompat flag is only valid for the current fs. For 438944d3f9fSNikolay Borisov * seed devices it's forbidden to have their uuid changed so reading 439944d3f9fSNikolay Borisov * ->fsid in this case is fine 4407239ff4bSNikolay Borisov */ 441944d3f9fSNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID)) 4427239ff4bSNikolay Borisov metadata_uuid = fs_devices->metadata_uuid; 4437239ff4bSNikolay Borisov else 4447239ff4bSNikolay Borisov metadata_uuid = fs_devices->fsid; 4457239ff4bSNikolay Borisov 446944d3f9fSNikolay Borisov if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) 447944d3f9fSNikolay Borisov return 0; 448944d3f9fSNikolay Borisov 449944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) 450944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE)) 451944d3f9fSNikolay Borisov return 0; 452944d3f9fSNikolay Borisov 453944d3f9fSNikolay Borisov return 1; 4542b82032cSYan Zheng } 4552b82032cSYan Zheng 45677bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */ 457*947a6299SQu Wenruo static int validate_extent_buffer(struct extent_buffer *eb, 458*947a6299SQu Wenruo struct btrfs_tree_parent_check *check) 459ce9adaa5SChris Mason { 46077bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info; 461ce9adaa5SChris Mason u64 found_start; 46277bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size; 46377bf40a2SQu Wenruo u8 found_level; 4642996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE]; 465dfd29eedSDavid Sterba const u8 *header_csum; 46677bf40a2SQu Wenruo int ret = 0; 467ea466794SJosef Bacik 468*947a6299SQu Wenruo ASSERT(check); 469*947a6299SQu Wenruo 470ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 471727011e0SChris Mason if (found_start != eb->start) { 4728f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, 4738f0ed7d4SQu Wenruo "bad tree block start, mirror %u want %llu have %llu", 4748f0ed7d4SQu Wenruo eb->read_mirror, eb->start, found_start); 475f188591eSChris Mason ret = -EIO; 47677bf40a2SQu Wenruo goto out; 477ce9adaa5SChris Mason } 478b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) { 4798f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u", 4808f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 4811259ab75SChris Mason ret = -EIO; 48277bf40a2SQu Wenruo goto out; 4831259ab75SChris Mason } 484ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 4851c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 4868f0ed7d4SQu Wenruo btrfs_err(fs_info, 4878f0ed7d4SQu Wenruo "bad tree block level, mirror %u level %d on logical %llu", 4888f0ed7d4SQu Wenruo eb->read_mirror, btrfs_header_level(eb), eb->start); 4891c24c3ceSJosef Bacik ret = -EIO; 49077bf40a2SQu Wenruo goto out; 4911c24c3ceSJosef Bacik } 492ce9adaa5SChris Mason 493c67b3892SDavid Sterba csum_tree_block(eb, result); 494dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) + 495dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum)); 496a826d6dcSJosef Bacik 497dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) { 4982996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info, 4998f0ed7d4SQu Wenruo "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d", 5008f0ed7d4SQu Wenruo eb->start, eb->read_mirror, 501dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum), 50235be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result), 50335be8851SJohannes Thumshirn btrfs_header_level(eb)); 5042996e1f8SJohannes Thumshirn ret = -EUCLEAN; 50577bf40a2SQu Wenruo goto out; 5062996e1f8SJohannes Thumshirn } 5072996e1f8SJohannes Thumshirn 508*947a6299SQu Wenruo if (found_level != check->level) { 509*947a6299SQu Wenruo ret = -EIO; 510*947a6299SQu Wenruo goto out; 511*947a6299SQu Wenruo } 512*947a6299SQu Wenruo if (unlikely(check->transid && 513*947a6299SQu Wenruo btrfs_header_generation(eb) != check->transid)) { 514*947a6299SQu Wenruo btrfs_err_rl(eb->fs_info, 515*947a6299SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu", 516*947a6299SQu Wenruo eb->start, eb->read_mirror, check->transid, 517*947a6299SQu Wenruo btrfs_header_generation(eb)); 518*947a6299SQu Wenruo ret = -EIO; 519*947a6299SQu Wenruo goto out; 520*947a6299SQu Wenruo } 521*947a6299SQu Wenruo if (check->has_first_key) { 522*947a6299SQu Wenruo struct btrfs_key *expect_key = &check->first_key; 523*947a6299SQu Wenruo struct btrfs_key found_key; 524*947a6299SQu Wenruo 525*947a6299SQu Wenruo if (found_level) 526*947a6299SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 527*947a6299SQu Wenruo else 528*947a6299SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 529*947a6299SQu Wenruo if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) { 530*947a6299SQu Wenruo btrfs_err(fs_info, 531*947a6299SQu Wenruo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)", 532*947a6299SQu Wenruo eb->start, check->transid, 533*947a6299SQu Wenruo expect_key->objectid, 534*947a6299SQu Wenruo expect_key->type, expect_key->offset, 535*947a6299SQu Wenruo found_key.objectid, found_key.type, 536*947a6299SQu Wenruo found_key.offset); 537*947a6299SQu Wenruo ret = -EUCLEAN; 538*947a6299SQu Wenruo goto out; 539*947a6299SQu Wenruo } 540*947a6299SQu Wenruo } 541*947a6299SQu Wenruo if (check->owner_root) { 542*947a6299SQu Wenruo ret = btrfs_check_eb_owner(eb, check->owner_root); 543*947a6299SQu Wenruo if (ret < 0) 544*947a6299SQu Wenruo goto out; 545*947a6299SQu Wenruo } 546*947a6299SQu Wenruo 547a826d6dcSJosef Bacik /* 548a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 549a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 550a826d6dcSJosef Bacik * return -EIO. 551a826d6dcSJosef Bacik */ 5521c4360eeSDavid Sterba if (found_level == 0 && btrfs_check_leaf_full(eb)) { 553a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 554a826d6dcSJosef Bacik ret = -EIO; 555a826d6dcSJosef Bacik } 556ce9adaa5SChris Mason 557813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb)) 558053ab70fSLiu Bo ret = -EIO; 559053ab70fSLiu Bo 5600b32f4bbSJosef Bacik if (!ret) 5610b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 56275391f0dSQu Wenruo else 56375391f0dSQu Wenruo btrfs_err(fs_info, 5648f0ed7d4SQu Wenruo "read time tree block corruption detected on logical %llu mirror %u", 5658f0ed7d4SQu Wenruo eb->start, eb->read_mirror); 56677bf40a2SQu Wenruo out: 56777bf40a2SQu Wenruo return ret; 56877bf40a2SQu Wenruo } 56977bf40a2SQu Wenruo 570371cdc07SQu Wenruo static int validate_subpage_buffer(struct page *page, u64 start, u64 end, 571*947a6299SQu Wenruo int mirror, struct btrfs_tree_parent_check *check) 572371cdc07SQu Wenruo { 573371cdc07SQu Wenruo struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); 574371cdc07SQu Wenruo struct extent_buffer *eb; 575371cdc07SQu Wenruo bool reads_done; 576371cdc07SQu Wenruo int ret = 0; 577371cdc07SQu Wenruo 578*947a6299SQu Wenruo ASSERT(check); 579*947a6299SQu Wenruo 580371cdc07SQu Wenruo /* 581371cdc07SQu Wenruo * We don't allow bio merge for subpage metadata read, so we should 582371cdc07SQu Wenruo * only get one eb for each endio hook. 583371cdc07SQu Wenruo */ 584371cdc07SQu Wenruo ASSERT(end == start + fs_info->nodesize - 1); 585371cdc07SQu Wenruo ASSERT(PagePrivate(page)); 586371cdc07SQu Wenruo 587371cdc07SQu Wenruo eb = find_extent_buffer(fs_info, start); 588371cdc07SQu Wenruo /* 589371cdc07SQu Wenruo * When we are reading one tree block, eb must have been inserted into 590371cdc07SQu Wenruo * the radix tree. If not, something is wrong. 591371cdc07SQu Wenruo */ 592371cdc07SQu Wenruo ASSERT(eb); 593371cdc07SQu Wenruo 594371cdc07SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 595371cdc07SQu Wenruo /* Subpage read must finish in page read */ 596371cdc07SQu Wenruo ASSERT(reads_done); 597371cdc07SQu Wenruo 598371cdc07SQu Wenruo eb->read_mirror = mirror; 599371cdc07SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 600371cdc07SQu Wenruo ret = -EIO; 601371cdc07SQu Wenruo goto err; 602371cdc07SQu Wenruo } 603*947a6299SQu Wenruo ret = validate_extent_buffer(eb, check); 604371cdc07SQu Wenruo if (ret < 0) 605371cdc07SQu Wenruo goto err; 606371cdc07SQu Wenruo 607371cdc07SQu Wenruo set_extent_buffer_uptodate(eb); 608371cdc07SQu Wenruo 609371cdc07SQu Wenruo free_extent_buffer(eb); 610371cdc07SQu Wenruo return ret; 611371cdc07SQu Wenruo err: 612371cdc07SQu Wenruo /* 613371cdc07SQu Wenruo * end_bio_extent_readpage decrements io_pages in case of error, 614371cdc07SQu Wenruo * make sure it has something to decrement. 615371cdc07SQu Wenruo */ 616371cdc07SQu Wenruo atomic_inc(&eb->io_pages); 617371cdc07SQu Wenruo clear_extent_buffer_uptodate(eb); 618371cdc07SQu Wenruo free_extent_buffer(eb); 619371cdc07SQu Wenruo return ret; 620371cdc07SQu Wenruo } 621371cdc07SQu Wenruo 622c3a3b19bSQu Wenruo int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 62377bf40a2SQu Wenruo struct page *page, u64 start, u64 end, 62477bf40a2SQu Wenruo int mirror) 62577bf40a2SQu Wenruo { 62677bf40a2SQu Wenruo struct extent_buffer *eb; 62777bf40a2SQu Wenruo int ret = 0; 62877bf40a2SQu Wenruo int reads_done; 62977bf40a2SQu Wenruo 63077bf40a2SQu Wenruo ASSERT(page->private); 631371cdc07SQu Wenruo 632fbca46ebSQu Wenruo if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) 633*947a6299SQu Wenruo return validate_subpage_buffer(page, start, end, mirror, 634*947a6299SQu Wenruo &bbio->parent_check); 635371cdc07SQu Wenruo 63677bf40a2SQu Wenruo eb = (struct extent_buffer *)page->private; 63777bf40a2SQu Wenruo 63877bf40a2SQu Wenruo /* 63977bf40a2SQu Wenruo * The pending IO might have been the only thing that kept this buffer 64077bf40a2SQu Wenruo * in memory. Make sure we have a ref for all this other checks 64177bf40a2SQu Wenruo */ 64277bf40a2SQu Wenruo atomic_inc(&eb->refs); 64377bf40a2SQu Wenruo 64477bf40a2SQu Wenruo reads_done = atomic_dec_and_test(&eb->io_pages); 64577bf40a2SQu Wenruo if (!reads_done) 64677bf40a2SQu Wenruo goto err; 64777bf40a2SQu Wenruo 64877bf40a2SQu Wenruo eb->read_mirror = mirror; 64977bf40a2SQu Wenruo if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 65077bf40a2SQu Wenruo ret = -EIO; 65177bf40a2SQu Wenruo goto err; 65277bf40a2SQu Wenruo } 653*947a6299SQu Wenruo ret = validate_extent_buffer(eb, &bbio->parent_check); 654ce9adaa5SChris Mason err: 65553b381b3SDavid Woodhouse if (ret) { 65653b381b3SDavid Woodhouse /* 65753b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 65853b381b3SDavid Woodhouse * again, we have to make sure it has something 65953b381b3SDavid Woodhouse * to decrement 66053b381b3SDavid Woodhouse */ 66153b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6620b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 66353b381b3SDavid Woodhouse } 6640b32f4bbSJosef Bacik free_extent_buffer(eb); 66577bf40a2SQu Wenruo 666f188591eSChris Mason return ret; 667ce9adaa5SChris Mason } 668ce9adaa5SChris Mason 6694a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 6704a69a410SChris Mason { 6714a69a410SChris Mason struct async_submit_bio *async; 6724e4cbee9SChristoph Hellwig blk_status_t ret; 6734a69a410SChris Mason 6744a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 675ab2072b2SDavid Sterba switch (async->submit_cmd) { 676ab2072b2SDavid Sterba case WQ_SUBMIT_METADATA: 677ad65ecf3SDavid Sterba ret = btree_submit_bio_start(async->bio); 678ab2072b2SDavid Sterba break; 679ab2072b2SDavid Sterba case WQ_SUBMIT_DATA: 680882681acSDavid Sterba ret = btrfs_submit_bio_start(async->inode, async->bio); 681ab2072b2SDavid Sterba break; 682ab2072b2SDavid Sterba case WQ_SUBMIT_DATA_DIO: 683bfa17066SDavid Sterba ret = btrfs_submit_bio_start_direct_io(async->inode, 684da67daabSDavid Sterba async->bio, async->dio_file_offset); 685ab2072b2SDavid Sterba break; 686ab2072b2SDavid Sterba } 68779787eaaSJeff Mahoney if (ret) 6884e4cbee9SChristoph Hellwig async->status = ret; 6894a69a410SChris Mason } 6904a69a410SChris Mason 69106ea01b1SDavid Sterba /* 69206ea01b1SDavid Sterba * In order to insert checksums into the metadata in large chunks, we wait 69306ea01b1SDavid Sterba * until bio submission time. All the pages in the bio are checksummed and 69406ea01b1SDavid Sterba * sums are attached onto the ordered extent record. 69506ea01b1SDavid Sterba * 69606ea01b1SDavid Sterba * At IO completion time the csums attached on the ordered extent record are 69706ea01b1SDavid Sterba * inserted into the tree. 69806ea01b1SDavid Sterba */ 6994a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7008b712842SChris Mason { 701917f32a2SChristoph Hellwig struct async_submit_bio *async = 702917f32a2SChristoph Hellwig container_of(work, struct async_submit_bio, work); 703da67daabSDavid Sterba struct btrfs_inode *inode = async->inode; 704917f32a2SChristoph Hellwig struct btrfs_bio *bbio = btrfs_bio(async->bio); 7054854ddd0SChris Mason 706bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7074e4cbee9SChristoph Hellwig if (async->status) { 708917f32a2SChristoph Hellwig btrfs_bio_end_io(bbio, async->status); 70979787eaaSJeff Mahoney return; 71079787eaaSJeff Mahoney } 71179787eaaSJeff Mahoney 712ec39f769SChris Mason /* 713ec39f769SChris Mason * All of the bios that pass through here are from async helpers. 714ec39f769SChris Mason * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context. 715ec39f769SChris Mason * This changes nothing when cgroups aren't in use. 716ec39f769SChris Mason */ 717ec39f769SChris Mason async->bio->bi_opf |= REQ_CGROUP_PUNT; 718da67daabSDavid Sterba btrfs_submit_bio(inode->root->fs_info, async->bio, async->mirror_num); 7194a69a410SChris Mason } 7204a69a410SChris Mason 7214a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7224a69a410SChris Mason { 7234a69a410SChris Mason struct async_submit_bio *async; 7244a69a410SChris Mason 7254a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7268b712842SChris Mason kfree(async); 7278b712842SChris Mason } 7288b712842SChris Mason 729ea1f0cedSChristoph Hellwig /* 730ea1f0cedSChristoph Hellwig * Submit bio to an async queue. 731ea1f0cedSChristoph Hellwig * 732ea1f0cedSChristoph Hellwig * Retrun: 733ea1f0cedSChristoph Hellwig * - true if the work has been succesfuly submitted 734ea1f0cedSChristoph Hellwig * - false in case of error 735ea1f0cedSChristoph Hellwig */ 7365fcdadc2SDavid Sterba bool btrfs_wq_submit_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num, 737ab2072b2SDavid Sterba u64 dio_file_offset, enum btrfs_wq_submit_cmd cmd) 73844b8bd7eSChris Mason { 7395fcdadc2SDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 74044b8bd7eSChris Mason struct async_submit_bio *async; 74144b8bd7eSChris Mason 74244b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 74344b8bd7eSChris Mason if (!async) 744ea1f0cedSChristoph Hellwig return false; 74544b8bd7eSChris Mason 7465fcdadc2SDavid Sterba async->inode = inode; 74744b8bd7eSChris Mason async->bio = bio; 74844b8bd7eSChris Mason async->mirror_num = mirror_num; 749ab2072b2SDavid Sterba async->submit_cmd = cmd; 7504a69a410SChris Mason 751a0cac0ecSOmar Sandoval btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 752a0cac0ecSOmar Sandoval run_one_async_free); 7534a69a410SChris Mason 7541941b64bSQu Wenruo async->dio_file_offset = dio_file_offset; 7558c8bee1dSChris Mason 7564e4cbee9SChristoph Hellwig async->status = 0; 75779787eaaSJeff Mahoney 75867f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 759a31b4a43SChristoph Hellwig btrfs_queue_work(fs_info->hipri_workers, &async->work); 760a31b4a43SChristoph Hellwig else 7615cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 762ea1f0cedSChristoph Hellwig return true; 76344b8bd7eSChris Mason } 76444b8bd7eSChris Mason 7654e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 766ce3ed71aSChris Mason { 7672c30c71bSKent Overstreet struct bio_vec *bvec; 768ce3ed71aSChris Mason struct btrfs_root *root; 7692b070cfeSChristoph Hellwig int ret = 0; 7706dc4f100SMing Lei struct bvec_iter_all iter_all; 771ce3ed71aSChris Mason 772c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 7732b070cfeSChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 774ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 775ac303b69SQu Wenruo ret = csum_dirty_buffer(root->fs_info, bvec); 77679787eaaSJeff Mahoney if (ret) 77779787eaaSJeff Mahoney break; 778ce3ed71aSChris Mason } 7792c30c71bSKent Overstreet 7804e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 781ce3ed71aSChris Mason } 782ce3ed71aSChris Mason 783ad65ecf3SDavid Sterba blk_status_t btree_submit_bio_start(struct bio *bio) 78422c59948SChris Mason { 7858b712842SChris Mason /* 7868b712842SChris Mason * when we're called for a write, we're already in the async 7871a722d8fSChristoph Hellwig * submission context. Just jump into btrfs_submit_bio. 7888b712842SChris Mason */ 78979787eaaSJeff Mahoney return btree_csum_one_bio(bio); 79022c59948SChris Mason } 79122c59948SChris Mason 792f4dcfb30SJohannes Thumshirn static bool should_async_write(struct btrfs_fs_info *fs_info, 7939b4e675aSDavid Sterba struct btrfs_inode *bi) 794de0022b9SJosef Bacik { 7954eef29efSNaohiro Aota if (btrfs_is_zoned(fs_info)) 796f4dcfb30SJohannes Thumshirn return false; 7976300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 798f4dcfb30SJohannes Thumshirn return false; 7999b4e675aSDavid Sterba if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags)) 800f4dcfb30SJohannes Thumshirn return false; 801f4dcfb30SJohannes Thumshirn return true; 802de0022b9SJosef Bacik } 803de0022b9SJosef Bacik 804644094fdSDavid Sterba void btrfs_submit_metadata_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num) 80544b8bd7eSChris Mason { 806644094fdSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 807917f32a2SChristoph Hellwig struct btrfs_bio *bbio = btrfs_bio(bio); 8084e4cbee9SChristoph Hellwig blk_status_t ret; 809cad321adSChris Mason 81008a6f464SChristoph Hellwig bio->bi_opf |= REQ_META; 811*947a6299SQu Wenruo bbio->is_metadata = 1; 81208a6f464SChristoph Hellwig 813cfe94440SNaohiro Aota if (btrfs_op(bio) != BTRFS_MAP_WRITE) { 8141a722d8fSChristoph Hellwig btrfs_submit_bio(fs_info, bio, mirror_num); 8151a722d8fSChristoph Hellwig return; 81644b8bd7eSChris Mason } 81744b8bd7eSChris Mason 818ea1f0cedSChristoph Hellwig /* 819ea1f0cedSChristoph Hellwig * Kthread helpers are used to submit writes so that checksumming can 820ea1f0cedSChristoph Hellwig * happen in parallel across all CPUs. 821ea1f0cedSChristoph Hellwig */ 822644094fdSDavid Sterba if (should_async_write(fs_info, inode) && 823644094fdSDavid Sterba btrfs_wq_submit_bio(inode, bio, mirror_num, 0, WQ_SUBMIT_METADATA)) 824ea1f0cedSChristoph Hellwig return; 825ea1f0cedSChristoph Hellwig 826ea1f0cedSChristoph Hellwig ret = btree_csum_one_bio(bio); 82794d9e11bSChristoph Hellwig if (ret) { 828917f32a2SChristoph Hellwig btrfs_bio_end_io(bbio, ret); 829ea1f0cedSChristoph Hellwig return; 83094d9e11bSChristoph Hellwig } 831ea1f0cedSChristoph Hellwig 832ea1f0cedSChristoph Hellwig btrfs_submit_bio(fs_info, bio, mirror_num); 83361891923SStefan Behrens } 83461891923SStefan Behrens 8353dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 8368958b551SMatthew Wilcox (Oracle) static int btree_migrate_folio(struct address_space *mapping, 8378958b551SMatthew Wilcox (Oracle) struct folio *dst, struct folio *src, enum migrate_mode mode) 838784b4e29SChris Mason { 839784b4e29SChris Mason /* 840784b4e29SChris Mason * we can't safely write a btree page from here, 841784b4e29SChris Mason * we haven't done the locking hook 842784b4e29SChris Mason */ 8438958b551SMatthew Wilcox (Oracle) if (folio_test_dirty(src)) 844784b4e29SChris Mason return -EAGAIN; 845784b4e29SChris Mason /* 846784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 847784b4e29SChris Mason * We must have no buffers or drop them. 848784b4e29SChris Mason */ 8498958b551SMatthew Wilcox (Oracle) if (folio_get_private(src) && 8508958b551SMatthew Wilcox (Oracle) !filemap_release_folio(src, GFP_KERNEL)) 851784b4e29SChris Mason return -EAGAIN; 85254184650SMatthew Wilcox (Oracle) return migrate_folio(mapping, dst, src, mode); 853784b4e29SChris Mason } 8548958b551SMatthew Wilcox (Oracle) #else 8558958b551SMatthew Wilcox (Oracle) #define btree_migrate_folio NULL 8563dd1462eSJan Beulich #endif 857784b4e29SChris Mason 8580da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 8590da5468fSChris Mason struct writeback_control *wbc) 8600da5468fSChris Mason { 861e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 862e2d84521SMiao Xie int ret; 863e2d84521SMiao Xie 864d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 865448d640bSChris Mason 866448d640bSChris Mason if (wbc->for_kupdate) 867448d640bSChris Mason return 0; 868448d640bSChris Mason 869e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 870b9473439SChris Mason /* this is a bit racy, but that's ok */ 871d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 872d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 873d814a491SEthan Lien fs_info->dirty_metadata_batch); 874e2d84521SMiao Xie if (ret < 0) 875793955bcSChris Mason return 0; 876793955bcSChris Mason } 8770b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 8780da5468fSChris Mason } 8790da5468fSChris Mason 880f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags) 8815f39d397SChris Mason { 882f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio)) 883f913cff3SMatthew Wilcox (Oracle) return false; 8840c4e538bSDavid Sterba 885f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page); 886d98237b3SChris Mason } 887d98237b3SChris Mason 888895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset, 889895586ebSMatthew Wilcox (Oracle) size_t length) 890d98237b3SChris Mason { 891d1310b2eSChris Mason struct extent_io_tree *tree; 892895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree; 893895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset); 894f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS); 895895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) { 896895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info, 897895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu", 898895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio)); 899895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio); 9009ad6b7bcSChris Mason } 901d98237b3SChris Mason } 902d98237b3SChris Mason 903bb146eb2SJosef Bacik #ifdef DEBUG 9040079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping, 9050079c3b1SMatthew Wilcox (Oracle) struct folio *folio) 9060079c3b1SMatthew Wilcox (Oracle) { 9070079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); 908139e8cd3SQu Wenruo struct btrfs_subpage *subpage; 9090b32f4bbSJosef Bacik struct extent_buffer *eb; 910139e8cd3SQu Wenruo int cur_bit = 0; 9110079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio); 9120b32f4bbSJosef Bacik 913139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) { 9140079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio); 9150b32f4bbSJosef Bacik BUG_ON(!eb); 9160b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9170b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 91849d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 9190079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 920139e8cd3SQu Wenruo } 9210079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio); 922139e8cd3SQu Wenruo 923139e8cd3SQu Wenruo ASSERT(subpage->dirty_bitmap); 924139e8cd3SQu Wenruo while (cur_bit < BTRFS_SUBPAGE_BITMAP_SIZE) { 925139e8cd3SQu Wenruo unsigned long flags; 926139e8cd3SQu Wenruo u64 cur; 927139e8cd3SQu Wenruo u16 tmp = (1 << cur_bit); 928139e8cd3SQu Wenruo 929139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags); 930139e8cd3SQu Wenruo if (!(tmp & subpage->dirty_bitmap)) { 931139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 932139e8cd3SQu Wenruo cur_bit++; 933139e8cd3SQu Wenruo continue; 934139e8cd3SQu Wenruo } 935139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags); 936139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize; 937139e8cd3SQu Wenruo 938139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur); 939139e8cd3SQu Wenruo ASSERT(eb); 940139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 941139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs)); 94249d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb); 943139e8cd3SQu Wenruo free_extent_buffer(eb); 944139e8cd3SQu Wenruo 945139e8cd3SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits); 946139e8cd3SQu Wenruo } 9470079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio); 9480b32f4bbSJosef Bacik } 9490079c3b1SMatthew Wilcox (Oracle) #else 9500079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio 9510079c3b1SMatthew Wilcox (Oracle) #endif 9520b32f4bbSJosef Bacik 9537f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 9540da5468fSChris Mason .writepages = btree_writepages, 955f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio, 956895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio, 9578958b551SMatthew Wilcox (Oracle) .migrate_folio = btree_migrate_folio, 9580079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio, 959d98237b3SChris Mason }; 960123abc88SChris Mason 9612ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 9622ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 9633fbaf258SJosef Bacik u64 bytenr, u64 owner_root, 9643fbaf258SJosef Bacik int level) 9650999df54SChris Mason { 9660b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 9670b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 9683fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level); 9690999df54SChris Mason } 9700999df54SChris Mason 971581c1760SQu Wenruo /* 972581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 973581c1760SQu Wenruo * verification. 974581c1760SQu Wenruo * 975789d6a3aSQu Wenruo * @check: expected tree parentness check, see comments of the 976789d6a3aSQu Wenruo * structure for details. 977581c1760SQu Wenruo */ 9782ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 979789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check) 980e20d96d6SChris Mason { 9815f39d397SChris Mason struct extent_buffer *buf = NULL; 98219c00ddcSChris Mason int ret; 98319c00ddcSChris Mason 984789d6a3aSQu Wenruo ASSERT(check); 985789d6a3aSQu Wenruo 986789d6a3aSQu Wenruo buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root, 987789d6a3aSQu Wenruo check->level); 988c871b0f2SLiu Bo if (IS_ERR(buf)) 989c871b0f2SLiu Bo return buf; 990e4204dedSChris Mason 991789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(buf, check); 9920f0fe8f7SFilipe David Borba Manana if (ret) { 993537f38f0SNikolay Borisov free_extent_buffer_stale(buf); 99464c043deSLiu Bo return ERR_PTR(ret); 9950f0fe8f7SFilipe David Borba Manana } 996789d6a3aSQu Wenruo if (btrfs_check_eb_owner(buf, check->owner_root)) { 99788c602abSQu Wenruo free_extent_buffer_stale(buf); 99888c602abSQu Wenruo return ERR_PTR(-EUCLEAN); 99988c602abSQu Wenruo } 10005f39d397SChris Mason return buf; 1001ce9adaa5SChris Mason 1002eb60ceacSChris Mason } 1003eb60ceacSChris Mason 10046a884d7dSDavid Sterba void btrfs_clean_tree_block(struct extent_buffer *buf) 1005ed2ff2cbSChris Mason { 10066a884d7dSDavid Sterba struct btrfs_fs_info *fs_info = buf->fs_info; 100755c69072SChris Mason if (btrfs_header_generation(buf) == 1008e2d84521SMiao Xie fs_info->running_transaction->transid) { 100949d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 1010b4ce94deSChris Mason 1011b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1012104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1013e2d84521SMiao Xie -buf->len, 1014e2d84521SMiao Xie fs_info->dirty_metadata_batch); 10150b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1016925baeddSChris Mason } 10175f39d397SChris Mason } 1018ed7b63ebSJosef Bacik } 10195f39d397SChris Mason 1020da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1021e20d96d6SChris Mason u64 objectid) 1022d97e63b6SChris Mason { 10237c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 10242e608bd1SJosef Bacik 10252e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key)); 10262e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item)); 10272e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 102896dfcb46SJosef Bacik root->fs_info = fs_info; 10292e608bd1SJosef Bacik root->root_key.objectid = objectid; 1030cfaa7295SChris Mason root->node = NULL; 1031a28ec197SChris Mason root->commit_root = NULL; 103227cdeb70SMiao Xie root->state = 0; 1033abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node); 10340b86a832SChris Mason 10350f7d52f4SChris Mason root->last_trans = 0; 10366b8fad57SNikolay Borisov root->free_objectid = 0; 1037eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1038199c2a9cSMiao Xie root->nr_ordered_extents = 0; 10396bef4d31SEric Paris root->inode_tree = RB_ROOT; 1040088aea3bSDavid Sterba INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 10412e608bd1SJosef Bacik 10422e608bd1SJosef Bacik btrfs_init_root_block_rsv(root); 10430b86a832SChris Mason 10440b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 10455d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1046eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1047eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1048199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1049199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 1050d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list); 10512ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 10522ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 10535d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1054eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1055199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1056f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 10572ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 10582ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 10598287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1060a2135011SChris Mason mutex_init(&root->objectid_mutex); 1061e02119d5SChris Mason mutex_init(&root->log_mutex); 106231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1063573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 1064c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait); 10657237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 10667237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 10677237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 10688b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 10698b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 10707237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 10717237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 10727237f183SYan Zheng atomic_set(&root->log_writers, 0); 10732ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 10740700cea7SElena Reshetova refcount_set(&root->refs, 1); 10758ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0); 1076eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0); 10777237f183SYan Zheng root->log_transid = 0; 1078d1433debSMiao Xie root->log_transid_committed = -1; 1079257c62e1SChris Mason root->last_log_commit = 0; 10802e608bd1SJosef Bacik root->anon_dev = 0; 1081e289f03eSFilipe Manana if (!dummy) { 108243eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages, 108335da5a7eSDavid Sterba IO_TREE_ROOT_DIRTY_LOG_PAGES); 1084e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range, 108535da5a7eSDavid Sterba IO_TREE_LOG_CSUM_RANGE); 1086e289f03eSFilipe Manana } 1087017e5369SChris Mason 10885f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 1089370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks); 1090bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1091bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list); 1092fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1093bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots); 1094fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1095bd647ce3SJosef Bacik #endif 10963768f368SChris Mason } 10973768f368SChris Mason 109874e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 109996dfcb46SJosef Bacik u64 objectid, gfp_t flags) 11006f07e42eSAl Viro { 110174e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 11026f07e42eSAl Viro if (root) 110396dfcb46SJosef Bacik __setup_root(root, fs_info, objectid); 11046f07e42eSAl Viro return root; 11056f07e42eSAl Viro } 11066f07e42eSAl Viro 110706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 110806ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1109da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 111006ea65a3SJosef Bacik { 111106ea65a3SJosef Bacik struct btrfs_root *root; 111206ea65a3SJosef Bacik 11137c0260eeSJeff Mahoney if (!fs_info) 11147c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 11157c0260eeSJeff Mahoney 111696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL); 111706ea65a3SJosef Bacik if (!root) 111806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1119da17066cSJeff Mahoney 1120b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1121faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 112206ea65a3SJosef Bacik 112306ea65a3SJosef Bacik return root; 112406ea65a3SJosef Bacik } 112506ea65a3SJosef Bacik #endif 112606ea65a3SJosef Bacik 1127abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node) 1128abed4aaaSJosef Bacik { 1129abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node); 1130abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node); 1131abed4aaaSJosef Bacik 1132abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key); 1133abed4aaaSJosef Bacik } 1134abed4aaaSJosef Bacik 1135abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node) 1136abed4aaaSJosef Bacik { 1137abed4aaaSJosef Bacik const struct btrfs_key *key = k; 1138abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node); 1139abed4aaaSJosef Bacik 1140abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key); 1141abed4aaaSJosef Bacik } 1142abed4aaaSJosef Bacik 1143abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root) 1144abed4aaaSJosef Bacik { 1145abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1146abed4aaaSJosef Bacik struct rb_node *tmp; 1147abed4aaaSJosef Bacik 1148abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1149abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp); 1150abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1151abed4aaaSJosef Bacik ASSERT(!tmp); 1152abed4aaaSJosef Bacik 1153abed4aaaSJosef Bacik return tmp ? -EEXIST : 0; 1154abed4aaaSJosef Bacik } 1155abed4aaaSJosef Bacik 1156abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root) 1157abed4aaaSJosef Bacik { 1158abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info; 1159abed4aaaSJosef Bacik 1160abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock); 1161abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1162abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock); 1163abed4aaaSJosef Bacik } 1164abed4aaaSJosef Bacik 1165abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 1166abed4aaaSJosef Bacik struct btrfs_key *key) 1167abed4aaaSJosef Bacik { 1168abed4aaaSJosef Bacik struct rb_node *node; 1169abed4aaaSJosef Bacik struct btrfs_root *root = NULL; 1170abed4aaaSJosef Bacik 1171abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock); 1172abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp); 1173abed4aaaSJosef Bacik if (node) 1174abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node); 1175abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock); 1176abed4aaaSJosef Bacik 1177abed4aaaSJosef Bacik return root; 1178abed4aaaSJosef Bacik } 1179abed4aaaSJosef Bacik 1180f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr) 1181f7238e50SJosef Bacik { 1182f7238e50SJosef Bacik struct btrfs_block_group *block_group; 1183f7238e50SJosef Bacik u64 ret; 1184f7238e50SJosef Bacik 1185f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1186f7238e50SJosef Bacik return 0; 1187f7238e50SJosef Bacik 1188f7238e50SJosef Bacik if (bytenr) 1189f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr); 1190f7238e50SJosef Bacik else 1191f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr); 1192f7238e50SJosef Bacik ASSERT(block_group); 1193f7238e50SJosef Bacik if (!block_group) 1194f7238e50SJosef Bacik return 0; 1195f7238e50SJosef Bacik ret = block_group->global_root_id; 1196f7238e50SJosef Bacik btrfs_put_block_group(block_group); 1197f7238e50SJosef Bacik 1198f7238e50SJosef Bacik return ret; 1199f7238e50SJosef Bacik } 1200f7238e50SJosef Bacik 1201abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1202abed4aaaSJosef Bacik { 1203abed4aaaSJosef Bacik struct btrfs_key key = { 1204abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID, 1205abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1206f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1207abed4aaaSJosef Bacik }; 1208abed4aaaSJosef Bacik 1209abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1210abed4aaaSJosef Bacik } 1211abed4aaaSJosef Bacik 1212abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr) 1213abed4aaaSJosef Bacik { 1214abed4aaaSJosef Bacik struct btrfs_key key = { 1215abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID, 1216abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1217f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr), 1218abed4aaaSJosef Bacik }; 1219abed4aaaSJosef Bacik 1220abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key); 1221abed4aaaSJosef Bacik } 1222abed4aaaSJosef Bacik 122351129b33SJosef Bacik struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info) 122451129b33SJosef Bacik { 122551129b33SJosef Bacik if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) 122651129b33SJosef Bacik return fs_info->block_group_root; 122751129b33SJosef Bacik return btrfs_extent_root(fs_info, 0); 122851129b33SJosef Bacik } 122951129b33SJosef Bacik 123020897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 123120897f5cSArne Jansen u64 objectid) 123220897f5cSArne Jansen { 12339b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 123420897f5cSArne Jansen struct extent_buffer *leaf; 123520897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 123620897f5cSArne Jansen struct btrfs_root *root; 123720897f5cSArne Jansen struct btrfs_key key; 1238b89f6d1fSFilipe Manana unsigned int nofs_flag; 123920897f5cSArne Jansen int ret = 0; 124020897f5cSArne Jansen 1241b89f6d1fSFilipe Manana /* 1242b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 1243b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens. 1244b89f6d1fSFilipe Manana */ 1245b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save(); 124696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL); 1247b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag); 124820897f5cSArne Jansen if (!root) 124920897f5cSArne Jansen return ERR_PTR(-ENOMEM); 125020897f5cSArne Jansen 125120897f5cSArne Jansen root->root_key.objectid = objectid; 125220897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 125320897f5cSArne Jansen root->root_key.offset = 0; 125420897f5cSArne Jansen 12559631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 12569631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 125720897f5cSArne Jansen if (IS_ERR(leaf)) { 125820897f5cSArne Jansen ret = PTR_ERR(leaf); 12591dd05682STsutomu Itoh leaf = NULL; 1260c1b07854SPeng Hao goto fail; 126120897f5cSArne Jansen } 126220897f5cSArne Jansen 126320897f5cSArne Jansen root->node = leaf; 126420897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 126520897f5cSArne Jansen 126620897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 126727cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 126820897f5cSArne Jansen 1269f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0); 1270f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0); 127120897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 127220897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 127320897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 127420897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 127520897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 127620897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 127720897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 127833d85fdaSQu Wenruo if (is_fstree(objectid)) 1279807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid); 1280807fc790SAndy Shevchenko else 1281807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null); 1282c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 128320897f5cSArne Jansen 12848a6a87cdSBoris Burkov btrfs_tree_unlock(leaf); 12858a6a87cdSBoris Burkov 128620897f5cSArne Jansen key.objectid = objectid; 128720897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 128820897f5cSArne Jansen key.offset = 0; 128920897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129020897f5cSArne Jansen if (ret) 129120897f5cSArne Jansen goto fail; 129220897f5cSArne Jansen 129320897f5cSArne Jansen return root; 12941dd05682STsutomu Itoh 12958a6a87cdSBoris Burkov fail: 129600246528SJosef Bacik btrfs_put_root(root); 12971dd05682STsutomu Itoh 12981dd05682STsutomu Itoh return ERR_PTR(ret); 129920897f5cSArne Jansen } 130020897f5cSArne Jansen 13017237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1302e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13030f7d52f4SChris Mason { 13040f7d52f4SChris Mason struct btrfs_root *root; 1305e02119d5SChris Mason 130696dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS); 1307e02119d5SChris Mason if (!root) 13087237f183SYan Zheng return ERR_PTR(-ENOMEM); 1309e02119d5SChris Mason 1310e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1311e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1312e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 131327cdeb70SMiao Xie 13146ab6ebb7SNaohiro Aota return root; 13156ab6ebb7SNaohiro Aota } 13166ab6ebb7SNaohiro Aota 13176ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 13186ab6ebb7SNaohiro Aota struct btrfs_root *root) 13196ab6ebb7SNaohiro Aota { 13206ab6ebb7SNaohiro Aota struct extent_buffer *leaf; 13216ab6ebb7SNaohiro Aota 13227237f183SYan Zheng /* 132392a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees. 132427cdeb70SMiao Xie * 132592a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted, 132692a7cc42SQu Wenruo * and they go away before a real commit is actually done. 132792a7cc42SQu Wenruo * 132892a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference 132992a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree). 13307237f183SYan Zheng */ 1331e02119d5SChris Mason 13324d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13339631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL); 13346ab6ebb7SNaohiro Aota if (IS_ERR(leaf)) 13356ab6ebb7SNaohiro Aota return PTR_ERR(leaf); 1336e02119d5SChris Mason 13377237f183SYan Zheng root->node = leaf; 1338e02119d5SChris Mason 1339e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1340e02119d5SChris Mason btrfs_tree_unlock(root->node); 13416ab6ebb7SNaohiro Aota 13426ab6ebb7SNaohiro Aota return 0; 13437237f183SYan Zheng } 13447237f183SYan Zheng 13457237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13467237f183SYan Zheng struct btrfs_fs_info *fs_info) 13477237f183SYan Zheng { 13487237f183SYan Zheng struct btrfs_root *log_root; 13497237f183SYan Zheng 13507237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13517237f183SYan Zheng if (IS_ERR(log_root)) 13527237f183SYan Zheng return PTR_ERR(log_root); 13536ab6ebb7SNaohiro Aota 13543ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) { 13553ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root); 13563ddebf27SNaohiro Aota 13576ab6ebb7SNaohiro Aota if (ret) { 13586ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13596ab6ebb7SNaohiro Aota return ret; 13606ab6ebb7SNaohiro Aota } 13613ddebf27SNaohiro Aota } 13626ab6ebb7SNaohiro Aota 13637237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13647237f183SYan Zheng fs_info->log_root_tree = log_root; 13657237f183SYan Zheng return 0; 13667237f183SYan Zheng } 13677237f183SYan Zheng 13687237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13697237f183SYan Zheng struct btrfs_root *root) 13707237f183SYan Zheng { 13710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13727237f183SYan Zheng struct btrfs_root *log_root; 13737237f183SYan Zheng struct btrfs_inode_item *inode_item; 13746ab6ebb7SNaohiro Aota int ret; 13757237f183SYan Zheng 13760b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13777237f183SYan Zheng if (IS_ERR(log_root)) 13787237f183SYan Zheng return PTR_ERR(log_root); 13797237f183SYan Zheng 13806ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root); 13816ab6ebb7SNaohiro Aota if (ret) { 13826ab6ebb7SNaohiro Aota btrfs_put_root(log_root); 13836ab6ebb7SNaohiro Aota return ret; 13846ab6ebb7SNaohiro Aota } 13856ab6ebb7SNaohiro Aota 13867237f183SYan Zheng log_root->last_trans = trans->transid; 13877237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13887237f183SYan Zheng 13897237f183SYan Zheng inode_item = &log_root->root_item.inode; 13903cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13913cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13923cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1393da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13940b246afaSJeff Mahoney fs_info->nodesize); 13953cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13967237f183SYan Zheng 13975d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13987237f183SYan Zheng 13997237f183SYan Zheng WARN_ON(root->log_root); 14007237f183SYan Zheng root->log_root = log_root; 14017237f183SYan Zheng root->log_transid = 0; 1402d1433debSMiao Xie root->log_transid_committed = -1; 1403257c62e1SChris Mason root->last_log_commit = 0; 1404e02119d5SChris Mason return 0; 1405e02119d5SChris Mason } 1406e02119d5SChris Mason 140749d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, 140849d11beaSJosef Bacik struct btrfs_path *path, 1409cb517eabSMiao Xie struct btrfs_key *key) 1410e02119d5SChris Mason { 1411e02119d5SChris Mason struct btrfs_root *root; 1412789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 1413e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 141484234f3aSYan Zheng u64 generation; 1415cb517eabSMiao Xie int ret; 1416581c1760SQu Wenruo int level; 1417cb517eabSMiao Xie 141896dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS); 141949d11beaSJosef Bacik if (!root) 142049d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 14210f7d52f4SChris Mason 1422cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1423cb517eabSMiao Xie &root->root_item, &root->root_key); 14240f7d52f4SChris Mason if (ret) { 142513a8a7c8SYan, Zheng if (ret > 0) 142613a8a7c8SYan, Zheng ret = -ENOENT; 142749d11beaSJosef Bacik goto fail; 14280f7d52f4SChris Mason } 142913a8a7c8SYan, Zheng 143084234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1431581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 1432789d6a3aSQu Wenruo check.level = level; 1433789d6a3aSQu Wenruo check.transid = generation; 1434789d6a3aSQu Wenruo check.owner_root = key->objectid; 1435789d6a3aSQu Wenruo root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item), 1436789d6a3aSQu Wenruo &check); 143764c043deSLiu Bo if (IS_ERR(root->node)) { 143864c043deSLiu Bo ret = PTR_ERR(root->node); 14398c38938cSJosef Bacik root->node = NULL; 144049d11beaSJosef Bacik goto fail; 14414eb150d6SQu Wenruo } 14424eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1443cb517eabSMiao Xie ret = -EIO; 144449d11beaSJosef Bacik goto fail; 1445416bc658SJosef Bacik } 144688c602abSQu Wenruo 144788c602abSQu Wenruo /* 144888c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must 144988c602abSQu Wenruo * match its root node owner 145088c602abSQu Wenruo */ 145188c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) && 145288c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 145388c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && 145488c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) { 145588c602abSQu Wenruo btrfs_crit(fs_info, 145688c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu", 145788c602abSQu Wenruo root->root_key.objectid, root->node->start, 145888c602abSQu Wenruo btrfs_header_owner(root->node), 145988c602abSQu Wenruo root->root_key.objectid); 146088c602abSQu Wenruo ret = -EUCLEAN; 146188c602abSQu Wenruo goto fail; 146288c602abSQu Wenruo } 14635d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 1464cb517eabSMiao Xie return root; 146549d11beaSJosef Bacik fail: 146600246528SJosef Bacik btrfs_put_root(root); 146749d11beaSJosef Bacik return ERR_PTR(ret); 146849d11beaSJosef Bacik } 146949d11beaSJosef Bacik 147049d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 147149d11beaSJosef Bacik struct btrfs_key *key) 147249d11beaSJosef Bacik { 147349d11beaSJosef Bacik struct btrfs_root *root; 147449d11beaSJosef Bacik struct btrfs_path *path; 147549d11beaSJosef Bacik 147649d11beaSJosef Bacik path = btrfs_alloc_path(); 147749d11beaSJosef Bacik if (!path) 147849d11beaSJosef Bacik return ERR_PTR(-ENOMEM); 147949d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key); 148049d11beaSJosef Bacik btrfs_free_path(path); 148149d11beaSJosef Bacik 148249d11beaSJosef Bacik return root; 1483cb517eabSMiao Xie } 1484cb517eabSMiao Xie 14852dfb1e43SQu Wenruo /* 14862dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure 14872dfb1e43SQu Wenruo * 14882dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new 14892dfb1e43SQu Wenruo */ 14902dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev) 1491cb517eabSMiao Xie { 1492cb517eabSMiao Xie int ret; 1493dcc3eb96SNikolay Borisov unsigned int nofs_flag; 1494cb517eabSMiao Xie 1495dcc3eb96SNikolay Borisov /* 1496dcc3eb96SNikolay Borisov * We might be called under a transaction (e.g. indirect backref 1497dcc3eb96SNikolay Borisov * resolution) which could deadlock if it triggers memory reclaim 1498dcc3eb96SNikolay Borisov */ 1499dcc3eb96SNikolay Borisov nofs_flag = memalloc_nofs_save(); 1500dcc3eb96SNikolay Borisov ret = btrfs_drew_lock_init(&root->snapshot_lock); 1501dcc3eb96SNikolay Borisov memalloc_nofs_restore(nofs_flag); 1502dcc3eb96SNikolay Borisov if (ret) 15038257b2dcSMiao Xie goto fail; 15048257b2dcSMiao Xie 1505aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID && 150637f00a6dSJohannes Thumshirn !btrfs_is_data_reloc_root(root)) { 150792a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state); 1508f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item); 1509f39e4571SJosef Bacik } 1510f39e4571SJosef Bacik 1511851fd730SQu Wenruo /* 1512851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to 1513851fd730SQu Wenruo * userspace, the id pool is limited to 1M 1514851fd730SQu Wenruo */ 1515851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) && 1516851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) { 15172dfb1e43SQu Wenruo if (!anon_dev) { 1518cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1519cb517eabSMiao Xie if (ret) 1520876d2cf1SLiu Bo goto fail; 15212dfb1e43SQu Wenruo } else { 15222dfb1e43SQu Wenruo root->anon_dev = anon_dev; 15232dfb1e43SQu Wenruo } 1524851fd730SQu Wenruo } 1525f32e48e9SChandan Rajendra 1526f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1527453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root); 1528f32e48e9SChandan Rajendra if (ret) { 1529f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1530876d2cf1SLiu Bo goto fail; 1531f32e48e9SChandan Rajendra } 1532f32e48e9SChandan Rajendra 15336b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 1534f32e48e9SChandan Rajendra 1535f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1536f32e48e9SChandan Rajendra 1537cb517eabSMiao Xie return 0; 1538cb517eabSMiao Xie fail: 153984db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */ 1540cb517eabSMiao Xie return ret; 1541cb517eabSMiao Xie } 1542cb517eabSMiao Xie 1543a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1544cb517eabSMiao Xie u64 root_id) 1545cb517eabSMiao Xie { 1546cb517eabSMiao Xie struct btrfs_root *root; 1547cb517eabSMiao Xie 1548fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1549fc7cbcd4SDavid Sterba root = radix_tree_lookup(&fs_info->fs_roots_radix, 1550fc7cbcd4SDavid Sterba (unsigned long)root_id); 1551bc44d7c4SJosef Bacik if (root) 155200246528SJosef Bacik root = btrfs_grab_root(root); 1553fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1554cb517eabSMiao Xie return root; 1555cb517eabSMiao Xie } 1556cb517eabSMiao Xie 155749d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, 155849d11beaSJosef Bacik u64 objectid) 155949d11beaSJosef Bacik { 1560abed4aaaSJosef Bacik struct btrfs_key key = { 1561abed4aaaSJosef Bacik .objectid = objectid, 1562abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 1563abed4aaaSJosef Bacik .offset = 0, 1564abed4aaaSJosef Bacik }; 1565abed4aaaSJosef Bacik 156649d11beaSJosef Bacik if (objectid == BTRFS_ROOT_TREE_OBJECTID) 156749d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root); 156849d11beaSJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 1569abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 157049d11beaSJosef Bacik if (objectid == BTRFS_CHUNK_TREE_OBJECTID) 157149d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root); 157249d11beaSJosef Bacik if (objectid == BTRFS_DEV_TREE_OBJECTID) 157349d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root); 157449d11beaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 1575abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key)); 157649d11beaSJosef Bacik if (objectid == BTRFS_QUOTA_TREE_OBJECTID) 157749d11beaSJosef Bacik return btrfs_grab_root(fs_info->quota_root) ? 157849d11beaSJosef Bacik fs_info->quota_root : ERR_PTR(-ENOENT); 157949d11beaSJosef Bacik if (objectid == BTRFS_UUID_TREE_OBJECTID) 158049d11beaSJosef Bacik return btrfs_grab_root(fs_info->uuid_root) ? 158149d11beaSJosef Bacik fs_info->uuid_root : ERR_PTR(-ENOENT); 158214033b08SQu Wenruo if (objectid == BTRFS_BLOCK_GROUP_TREE_OBJECTID) 158314033b08SQu Wenruo return btrfs_grab_root(fs_info->block_group_root) ? 158414033b08SQu Wenruo fs_info->block_group_root : ERR_PTR(-ENOENT); 1585abed4aaaSJosef Bacik if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) { 1586abed4aaaSJosef Bacik struct btrfs_root *root = btrfs_global_root(fs_info, &key); 1587abed4aaaSJosef Bacik 1588abed4aaaSJosef Bacik return btrfs_grab_root(root) ? root : ERR_PTR(-ENOENT); 1589abed4aaaSJosef Bacik } 159049d11beaSJosef Bacik return NULL; 159149d11beaSJosef Bacik } 159249d11beaSJosef Bacik 1593cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1594cb517eabSMiao Xie struct btrfs_root *root) 1595cb517eabSMiao Xie { 1596cb517eabSMiao Xie int ret; 1597cb517eabSMiao Xie 1598fc7cbcd4SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1599fc7cbcd4SDavid Sterba if (ret) 1600fc7cbcd4SDavid Sterba return ret; 1601fc7cbcd4SDavid Sterba 1602fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 1603fc7cbcd4SDavid Sterba ret = radix_tree_insert(&fs_info->fs_roots_radix, 1604fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid, 1605fc7cbcd4SDavid Sterba root); 1606af01d2e5SJosef Bacik if (ret == 0) { 160700246528SJosef Bacik btrfs_grab_root(root); 1608fc7cbcd4SDavid Sterba set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1609af01d2e5SJosef Bacik } 1610fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 1611fc7cbcd4SDavid Sterba radix_tree_preload_end(); 1612cb517eabSMiao Xie 1613cb517eabSMiao Xie return ret; 1614cb517eabSMiao Xie } 1615cb517eabSMiao Xie 1616bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) 1617bd647ce3SJosef Bacik { 1618bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1619bd647ce3SJosef Bacik struct btrfs_root *root; 1620bd647ce3SJosef Bacik 1621bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) { 1622457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN]; 1623457f1864SJosef Bacik 1624bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots, 1625bd647ce3SJosef Bacik struct btrfs_root, leak_list); 1626457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d", 162771008734SJosef Bacik btrfs_root_name(&root->root_key, buf), 1628bd647ce3SJosef Bacik refcount_read(&root->refs)); 1629bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1) 163000246528SJosef Bacik btrfs_put_root(root); 163100246528SJosef Bacik btrfs_put_root(root); 1632bd647ce3SJosef Bacik } 1633bd647ce3SJosef Bacik #endif 1634bd647ce3SJosef Bacik } 1635bd647ce3SJosef Bacik 1636abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info) 1637abed4aaaSJosef Bacik { 1638abed4aaaSJosef Bacik struct btrfs_root *root; 1639abed4aaaSJosef Bacik struct rb_node *node; 1640abed4aaaSJosef Bacik 1641abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) { 1642abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node); 1643abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree); 1644abed4aaaSJosef Bacik btrfs_put_root(root); 1645abed4aaaSJosef Bacik } 1646abed4aaaSJosef Bacik } 1647abed4aaaSJosef Bacik 16480d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) 16490d4b0463SJosef Bacik { 1650141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1651141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes); 16525deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes); 1653141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter); 1654141386e1SJosef Bacik btrfs_free_csum_hash(fs_info); 1655141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info); 1656141386e1SJosef Bacik btrfs_free_ref_cache(fs_info); 16570d4b0463SJosef Bacik kfree(fs_info->balance_ctl); 16580d4b0463SJosef Bacik kfree(fs_info->delayed_root); 1659abed4aaaSJosef Bacik free_global_roots(fs_info); 166000246528SJosef Bacik btrfs_put_root(fs_info->tree_root); 166100246528SJosef Bacik btrfs_put_root(fs_info->chunk_root); 166200246528SJosef Bacik btrfs_put_root(fs_info->dev_root); 166300246528SJosef Bacik btrfs_put_root(fs_info->quota_root); 166400246528SJosef Bacik btrfs_put_root(fs_info->uuid_root); 166500246528SJosef Bacik btrfs_put_root(fs_info->fs_root); 1666aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root); 16679c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root); 1668bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 16693fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 16700d4b0463SJosef Bacik kfree(fs_info->super_copy); 16710d4b0463SJosef Bacik kfree(fs_info->super_for_commit); 16728481dd80SQu Wenruo kfree(fs_info->subpage_info); 16730d4b0463SJosef Bacik kvfree(fs_info); 16740d4b0463SJosef Bacik } 16750d4b0463SJosef Bacik 16760d4b0463SJosef Bacik 16772dfb1e43SQu Wenruo /* 16782dfb1e43SQu Wenruo * Get an in-memory reference of a root structure. 16792dfb1e43SQu Wenruo * 16802dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly. 16812dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not 16822dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots. 16832dfb1e43SQu Wenruo * 16842dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage. 16852dfb1e43SQu Wenruo * 16862dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the 16872dfb1e43SQu Wenruo * same root objectid. 16882dfb1e43SQu Wenruo * 16892dfb1e43SQu Wenruo * @objectid: root id 16902dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots, 16912dfb1e43SQu Wenruo * pass 0 for new allocation. 16922dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT 16932dfb1e43SQu Wenruo * for orphan roots 16942dfb1e43SQu Wenruo */ 16952dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, 16962dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev, 16972dfb1e43SQu Wenruo bool check_ref) 16985eda7b5eSChris Mason { 16995eda7b5eSChris Mason struct btrfs_root *root; 1700381cf658SDavid Sterba struct btrfs_path *path; 17011d4c08e0SDavid Sterba struct btrfs_key key; 17025eda7b5eSChris Mason int ret; 17035eda7b5eSChris Mason 170449d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 170549d11beaSJosef Bacik if (root) 170649d11beaSJosef Bacik return root; 17074df27c4dSYan, Zheng again: 170856e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid); 170948475471SStefan Behrens if (root) { 17102dfb1e43SQu Wenruo /* Shouldn't get preallocated anon_dev for cached roots */ 17112dfb1e43SQu Wenruo ASSERT(!anon_dev); 1712bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 171300246528SJosef Bacik btrfs_put_root(root); 171448475471SStefan Behrens return ERR_PTR(-ENOENT); 1715bc44d7c4SJosef Bacik } 17165eda7b5eSChris Mason return root; 171748475471SStefan Behrens } 17185eda7b5eSChris Mason 171956e9357aSDavid Sterba key.objectid = objectid; 172056e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 172156e9357aSDavid Sterba key.offset = (u64)-1; 172256e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key); 17235eda7b5eSChris Mason if (IS_ERR(root)) 17245eda7b5eSChris Mason return root; 17253394e160SChris Mason 1726c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1727d68fc57bSYan, Zheng ret = -ENOENT; 1728d68fc57bSYan, Zheng goto fail; 1729d68fc57bSYan, Zheng } 1730d68fc57bSYan, Zheng 17312dfb1e43SQu Wenruo ret = btrfs_init_fs_root(root, anon_dev); 1732cb517eabSMiao Xie if (ret) 1733cb517eabSMiao Xie goto fail; 1734cb517eabSMiao Xie 1735381cf658SDavid Sterba path = btrfs_alloc_path(); 1736381cf658SDavid Sterba if (!path) { 1737381cf658SDavid Sterba ret = -ENOMEM; 1738381cf658SDavid Sterba goto fail; 1739381cf658SDavid Sterba } 17401d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17411d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 174256e9357aSDavid Sterba key.offset = objectid; 17431d4c08e0SDavid Sterba 17441d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1745381cf658SDavid Sterba btrfs_free_path(path); 1746d68fc57bSYan, Zheng if (ret < 0) 1747d68fc57bSYan, Zheng goto fail; 1748d68fc57bSYan, Zheng if (ret == 0) 174927cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1750d68fc57bSYan, Zheng 1751cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17520f7d52f4SChris Mason if (ret) { 1753168a2f77SJia-Ju Bai if (ret == -EEXIST) { 175400246528SJosef Bacik btrfs_put_root(root); 17554df27c4dSYan, Zheng goto again; 1756168a2f77SJia-Ju Bai } 17574df27c4dSYan, Zheng goto fail; 17584df27c4dSYan, Zheng } 1759edbd8d4eSChris Mason return root; 17604df27c4dSYan, Zheng fail: 176133fab972SFilipe Manana /* 176233fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his 1763143823cfSDavid Sterba * responsibility to free it in case we fail. So we have to set our 176433fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root() 176533fab972SFilipe Manana * and once again by our caller. 176633fab972SFilipe Manana */ 176733fab972SFilipe Manana if (anon_dev) 176833fab972SFilipe Manana root->anon_dev = 0; 17698c38938cSJosef Bacik btrfs_put_root(root); 17704df27c4dSYan, Zheng return ERR_PTR(ret); 1771edbd8d4eSChris Mason } 1772edbd8d4eSChris Mason 17732dfb1e43SQu Wenruo /* 17742dfb1e43SQu Wenruo * Get in-memory reference of a root structure 17752dfb1e43SQu Wenruo * 17762dfb1e43SQu Wenruo * @objectid: tree objectid 17772dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least 17782dfb1e43SQu Wenruo * one reference 17792dfb1e43SQu Wenruo */ 17802dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 17812dfb1e43SQu Wenruo u64 objectid, bool check_ref) 17822dfb1e43SQu Wenruo { 17832dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, 0, check_ref); 17842dfb1e43SQu Wenruo } 17852dfb1e43SQu Wenruo 17862dfb1e43SQu Wenruo /* 17872dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass 17882dfb1e43SQu Wenruo * the anonymous block device id 17892dfb1e43SQu Wenruo * 17902dfb1e43SQu Wenruo * @objectid: tree objectid 17912dfb1e43SQu Wenruo * @anon_dev: if zero, allocate a new anonymous block device or use the 17922dfb1e43SQu Wenruo * parameter value 17932dfb1e43SQu Wenruo */ 17942dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 17952dfb1e43SQu Wenruo u64 objectid, dev_t anon_dev) 17962dfb1e43SQu Wenruo { 17972dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true); 17982dfb1e43SQu Wenruo } 17992dfb1e43SQu Wenruo 18008b712842SChris Mason /* 180149d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid 180249d11beaSJosef Bacik * @fs_info: the fs_info 180349d11beaSJosef Bacik * @objectid: the objectid we need to lookup 180449d11beaSJosef Bacik * 180549d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because 180649d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref 180749d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look 180849d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will 180949d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a 181049d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't 181149d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the 181249d11beaSJosef Bacik * backref code is finished using the root. 181349d11beaSJosef Bacik */ 181449d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 181549d11beaSJosef Bacik struct btrfs_path *path, 181649d11beaSJosef Bacik u64 objectid) 181749d11beaSJosef Bacik { 181849d11beaSJosef Bacik struct btrfs_root *root; 181949d11beaSJosef Bacik struct btrfs_key key; 182049d11beaSJosef Bacik 182149d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking); 182249d11beaSJosef Bacik 182349d11beaSJosef Bacik /* 182449d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but 182549d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking 182649d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root 182749d11beaSJosef Bacik * != NULL just return it. 182849d11beaSJosef Bacik */ 182949d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid); 183049d11beaSJosef Bacik if (root) 183149d11beaSJosef Bacik return root; 183249d11beaSJosef Bacik 183349d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid); 183449d11beaSJosef Bacik if (root) 183549d11beaSJosef Bacik return root; 183649d11beaSJosef Bacik 183749d11beaSJosef Bacik key.objectid = objectid; 183849d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY; 183949d11beaSJosef Bacik key.offset = (u64)-1; 184049d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key); 184149d11beaSJosef Bacik btrfs_release_path(path); 184249d11beaSJosef Bacik 184349d11beaSJosef Bacik return root; 184449d11beaSJosef Bacik } 184549d11beaSJosef Bacik 1846a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1847a74a4b97SChris Mason { 18480d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg; 1849d0278245SMiao Xie int again; 1850a74a4b97SChris Mason 1851d6fd0ae2SOmar Sandoval while (1) { 1852d0278245SMiao Xie again = 0; 18539d1a2a3aSDavid Sterba 1854fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1855fd340d0fSJosef Bacik 1856d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18572ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1858d0278245SMiao Xie goto sleep; 1859d0278245SMiao Xie 186090c711abSZygo Blaxell /* 186190c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 186290c711abSZygo Blaxell * before we have finished mounting the filesystem. 186390c711abSZygo Blaxell */ 18640b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 186590c711abSZygo Blaxell goto sleep; 186690c711abSZygo Blaxell 18670b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1868d0278245SMiao Xie goto sleep; 1869d0278245SMiao Xie 1870dc7f370cSMiao Xie /* 1871dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1872dc7f370cSMiao Xie * during the above check and trylock. 1873dc7f370cSMiao Xie */ 18742ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18750b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1876dc7f370cSMiao Xie goto sleep; 1877dc7f370cSMiao Xie } 1878dc7f370cSMiao Xie 18792ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 1880c2d6cb16SFilipe Manana 188133c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info); 18820b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1883a74a4b97SChris Mason 1884d0278245SMiao Xie /* 188505323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 188605323cd1SMiao Xie * needn't do anything special here. 1887d0278245SMiao Xie */ 18880b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 188967c5e7d4SFilipe Manana 189067c5e7d4SFilipe Manana /* 1891f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing 189267c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 189367c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 1894f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we 189567c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 189667c5e7d4SFilipe Manana * unused block groups. 189767c5e7d4SFilipe Manana */ 18980b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 189918bb8bbfSJohannes Thumshirn 190018bb8bbfSJohannes Thumshirn /* 190118bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted 190218bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free 190318bb8bbfSJohannes Thumshirn * space. 190418bb8bbfSJohannes Thumshirn */ 190518bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info); 1906d0278245SMiao Xie sleep: 1907a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags); 1908d6fd0ae2SOmar Sandoval if (kthread_should_park()) 1909d6fd0ae2SOmar Sandoval kthread_parkme(); 1910d6fd0ae2SOmar Sandoval if (kthread_should_stop()) 1911d6fd0ae2SOmar Sandoval return 0; 1912838fe188SJiri Kosina if (!again) { 1913a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 1914a74a4b97SChris Mason schedule(); 1915a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1916a74a4b97SChris Mason } 1917da288d28SFilipe Manana } 1918a74a4b97SChris Mason } 1919a74a4b97SChris Mason 1920a74a4b97SChris Mason static int transaction_kthread(void *arg) 1921a74a4b97SChris Mason { 1922a74a4b97SChris Mason struct btrfs_root *root = arg; 19230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1924a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1925a74a4b97SChris Mason struct btrfs_transaction *cur; 19268929ecfaSYan, Zheng u64 transid; 1927643900beSNikolay Borisov time64_t delta; 1928a74a4b97SChris Mason unsigned long delay; 1929914b2007SJan Kara bool cannot_commit; 1930a74a4b97SChris Mason 1931a74a4b97SChris Mason do { 1932914b2007SJan Kara cannot_commit = false; 1933ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000); 19340b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1935a74a4b97SChris Mason 19360b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19370b246afaSJeff Mahoney cur = fs_info->running_transaction; 1938a74a4b97SChris Mason if (!cur) { 19390b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1940a74a4b97SChris Mason goto sleep; 1941a74a4b97SChris Mason } 194231153d81SYan Zheng 1943643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time; 1944fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) && 1945fdfbf020SJosef Bacik cur->state < TRANS_STATE_COMMIT_START && 1946643900beSNikolay Borisov delta < fs_info->commit_interval) { 19470b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1948fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000); 1949fb8a7e94SNikolay Borisov delay = min(delay, 1950fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000)); 1951a74a4b97SChris Mason goto sleep; 1952a74a4b97SChris Mason } 19538929ecfaSYan, Zheng transid = cur->transid; 19540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 195556bec294SChris Mason 195679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1957354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1958914b2007SJan Kara if (IS_ERR(trans)) { 1959354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1960914b2007SJan Kara cannot_commit = true; 196179787eaaSJeff Mahoney goto sleep; 1962914b2007SJan Kara } 19638929ecfaSYan, Zheng if (transid == trans->transid) { 19643a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19658929ecfaSYan, Zheng } else { 19663a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19678929ecfaSYan, Zheng } 1968a74a4b97SChris Mason sleep: 19690b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19700b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1971a74a4b97SChris Mason 197284961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 19732ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 19748929ecfaSYan, Zheng if (!kthread_should_stop() && 19750b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1976914b2007SJan Kara cannot_commit)) 1977bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1978a74a4b97SChris Mason } while (!kthread_should_stop()); 1979a74a4b97SChris Mason return 0; 1980a74a4b97SChris Mason } 1981a74a4b97SChris Mason 1982af31f5e5SChris Mason /* 198301f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The 198401f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find 198501f0f9daSNikolay Borisov * anything. 1986af31f5e5SChris Mason * 1987af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1988af31f5e5SChris Mason * generation of the latest root in the array with the generation 1989af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1990af31f5e5SChris Mason */ 199101f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info) 1992af31f5e5SChris Mason { 199301f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy); 1994af31f5e5SChris Mason u64 cur; 1995af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1996af31f5e5SChris Mason int i; 1997af31f5e5SChris Mason 1998af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1999af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2000af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2001af31f5e5SChris Mason if (cur == newest_gen) 200201f0f9daSNikolay Borisov return i; 2003af31f5e5SChris Mason } 2004af31f5e5SChris Mason 200501f0f9daSNikolay Borisov return -EINVAL; 2006af31f5e5SChris Mason } 2007af31f5e5SChris Mason 2008af31f5e5SChris Mason /* 2009af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2010af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2011af31f5e5SChris Mason * done 2012af31f5e5SChris Mason */ 2013af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2014af31f5e5SChris Mason { 20156ef108ddSNikolay Borisov const int next_backup = info->backup_root_index; 2016af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2017af31f5e5SChris Mason 2018af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2019af31f5e5SChris Mason 2020af31f5e5SChris Mason /* 2021af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2022af31f5e5SChris Mason * regardless of which ones we use today 2023af31f5e5SChris Mason */ 2024af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2025af31f5e5SChris Mason 2026af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2027af31f5e5SChris Mason 2028af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2029af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2030af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2031af31f5e5SChris Mason 2032af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2033af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2034af31f5e5SChris Mason 2035af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2036af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2037af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2038af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2039af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2040af31f5e5SChris Mason 20411c56ab99SQu Wenruo if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) { 20429c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0); 2043f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0); 20449c54e80dSJosef Bacik 20459c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup, 20469c54e80dSJosef Bacik extent_root->node->start); 2047af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 204829cbcf40SJosef Bacik btrfs_header_generation(extent_root->node)); 2049af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 205029cbcf40SJosef Bacik btrfs_header_level(extent_root->node)); 2051af31f5e5SChris Mason 2052f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start); 2053f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup, 2054f7238e50SJosef Bacik btrfs_header_generation(csum_root->node)); 2055f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup, 2056f7238e50SJosef Bacik btrfs_header_level(csum_root->node)); 20579c54e80dSJosef Bacik } 2058af31f5e5SChris Mason 20597c7e82a7SChris Mason /* 20607c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20617c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20627c7e82a7SChris Mason */ 20637c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20647c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20657c7e82a7SChris Mason info->fs_root->node->start); 2066af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2067af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2068af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2069af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 20707c7e82a7SChris Mason } 2071af31f5e5SChris Mason 2072af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2073af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2074af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2075af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2076af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2077af31f5e5SChris Mason 2078af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2079af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2080af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2081af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2082af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2083af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2084af31f5e5SChris Mason 2085af31f5e5SChris Mason /* 2086af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2087af31f5e5SChris Mason * for the next commit 2088af31f5e5SChris Mason */ 2089af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2090af31f5e5SChris Mason &info->super_for_commit->super_roots, 2091af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2092af31f5e5SChris Mason } 2093af31f5e5SChris Mason 2094af31f5e5SChris Mason /* 2095bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0 2096bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots 2097bd2336b2SNikolay Borisov * 2098bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read 2099bd2336b2SNikolay Borisov * priority - priority of backup root required 2100bd2336b2SNikolay Borisov * 2101bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise. 2102bd2336b2SNikolay Borisov */ 2103bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority) 2104bd2336b2SNikolay Borisov { 2105bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2106bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy; 2107bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup; 2108bd2336b2SNikolay Borisov 2109bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) { 2110bd2336b2SNikolay Borisov if (priority == 0) 2111bd2336b2SNikolay Borisov return backup_index; 2112bd2336b2SNikolay Borisov 2113bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority; 2114bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS; 2115bd2336b2SNikolay Borisov } else { 2116bd2336b2SNikolay Borisov return -EINVAL; 2117bd2336b2SNikolay Borisov } 2118bd2336b2SNikolay Borisov 2119bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index; 2120bd2336b2SNikolay Borisov 2121bd2336b2SNikolay Borisov btrfs_set_super_generation(super, 2122bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup)); 2123bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2124bd2336b2SNikolay Borisov btrfs_set_super_root_level(super, 2125bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup)); 2126bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2127bd2336b2SNikolay Borisov 2128bd2336b2SNikolay Borisov /* 2129bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should 2130bd2336b2SNikolay Borisov * need a fsck 2131bd2336b2SNikolay Borisov */ 2132bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2133bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2134bd2336b2SNikolay Borisov 2135bd2336b2SNikolay Borisov return backup_index; 2136bd2336b2SNikolay Borisov } 2137bd2336b2SNikolay Borisov 21387abadb64SLiu Bo /* helper to cleanup workers */ 21397abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21407abadb64SLiu Bo { 2141dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2142afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 2143a31b4a43SChristoph Hellwig btrfs_destroy_workqueue(fs_info->hipri_workers); 21445cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2145d7b9416fSChristoph Hellwig if (fs_info->endio_workers) 2146d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_workers); 2147385de0efSChristoph Hellwig if (fs_info->rmw_workers) 2148385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers); 2149fed8a72dSChristoph Hellwig if (fs_info->compressed_write_workers) 2150fed8a72dSChristoph Hellwig destroy_workqueue(fs_info->compressed_write_workers); 2151fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2152fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 21535b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2154e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2155a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2156fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2157b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers) 2158b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers); 2159a9b9477dSFilipe Manana /* 2160a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2161a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2162a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2163a9b9477dSFilipe Manana */ 2164d7b9416fSChristoph Hellwig if (fs_info->endio_meta_workers) 2165d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_meta_workers); 21667abadb64SLiu Bo } 21677abadb64SLiu Bo 21682e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 21692e9f5954SRashika { 21702e9f5954SRashika if (root) { 21712e9f5954SRashika free_extent_buffer(root->node); 21722e9f5954SRashika free_extent_buffer(root->commit_root); 21732e9f5954SRashika root->node = NULL; 21742e9f5954SRashika root->commit_root = NULL; 21752e9f5954SRashika } 21762e9f5954SRashika } 21772e9f5954SRashika 2178abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info) 2179abed4aaaSJosef Bacik { 2180abed4aaaSJosef Bacik struct btrfs_root *root, *tmp; 2181abed4aaaSJosef Bacik 2182abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp, 2183abed4aaaSJosef Bacik &fs_info->global_root_tree, 2184abed4aaaSJosef Bacik rb_node) 2185abed4aaaSJosef Bacik free_root_extent_buffers(root); 2186abed4aaaSJosef Bacik } 2187abed4aaaSJosef Bacik 2188af31f5e5SChris Mason /* helper to cleanup tree roots */ 21894273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) 2190af31f5e5SChris Mason { 21912e9f5954SRashika free_root_extent_buffers(info->tree_root); 2192655b09feSJosef Bacik 2193abed4aaaSJosef Bacik free_global_root_pointers(info); 21942e9f5954SRashika free_root_extent_buffers(info->dev_root); 21952e9f5954SRashika free_root_extent_buffers(info->quota_root); 21962e9f5954SRashika free_root_extent_buffers(info->uuid_root); 21978c38938cSJosef Bacik free_root_extent_buffers(info->fs_root); 2198aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root); 21999c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root); 22004273eaffSAnand Jain if (free_chunk_root) 22012e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2202af31f5e5SChris Mason } 2203af31f5e5SChris Mason 22048c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root) 22058c38938cSJosef Bacik { 22068c38938cSJosef Bacik if (!root) 22078c38938cSJosef Bacik return; 22088c38938cSJosef Bacik 22098c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) { 22108c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 22111dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)); 22128c38938cSJosef Bacik if (root->anon_dev) 22138c38938cSJosef Bacik free_anon_bdev(root->anon_dev); 22148c38938cSJosef Bacik btrfs_drew_lock_destroy(&root->snapshot_lock); 2215923eb523SJohannes Thumshirn free_root_extent_buffers(root); 22168c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 2217fc7cbcd4SDavid Sterba spin_lock(&root->fs_info->fs_roots_radix_lock); 22188c38938cSJosef Bacik list_del_init(&root->leak_list); 2219fc7cbcd4SDavid Sterba spin_unlock(&root->fs_info->fs_roots_radix_lock); 22208c38938cSJosef Bacik #endif 22218c38938cSJosef Bacik kfree(root); 22228c38938cSJosef Bacik } 22238c38938cSJosef Bacik } 22248c38938cSJosef Bacik 2225faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2226171f6537SJosef Bacik { 2227fc7cbcd4SDavid Sterba int ret; 2228fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 2229fc7cbcd4SDavid Sterba int i; 2230171f6537SJosef Bacik 2231171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2232fc7cbcd4SDavid Sterba gang[0] = list_entry(fs_info->dead_roots.next, 2233171f6537SJosef Bacik struct btrfs_root, root_list); 2234fc7cbcd4SDavid Sterba list_del(&gang[0]->root_list); 2235171f6537SJosef Bacik 2236fc7cbcd4SDavid Sterba if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) 2237fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2238fc7cbcd4SDavid Sterba btrfs_put_root(gang[0]); 2239171f6537SJosef Bacik } 2240171f6537SJosef Bacik 2241fc7cbcd4SDavid Sterba while (1) { 2242fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2243fc7cbcd4SDavid Sterba (void **)gang, 0, 2244fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 2245fc7cbcd4SDavid Sterba if (!ret) 2246fc7cbcd4SDavid Sterba break; 2247fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 2248fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2249171f6537SJosef Bacik } 2250171f6537SJosef Bacik } 2251af31f5e5SChris Mason 2252638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2253638aa7edSEric Sandeen { 2254638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2255638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2256638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2257638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2258638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2259638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2260ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0); 2261638aa7edSEric Sandeen } 2262638aa7edSEric Sandeen 2263779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2264779a65a4SEric Sandeen { 2265779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2266779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2267779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2268779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2269779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2270779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2271907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0); 2272779a65a4SEric Sandeen } 2273779a65a4SEric Sandeen 22746bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2275f37938e0SEric Sandeen { 22762ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 2277e256927bSJosef Bacik unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID, 2278e256927bSJosef Bacik fs_info->tree_root); 22792ff7e61eSJeff Mahoney 22802ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 22812ff7e61eSJeff Mahoney set_nlink(inode, 1); 2282f37938e0SEric Sandeen /* 2283f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2284f37938e0SEric Sandeen * the real end of the address space is determined by all of 2285f37938e0SEric Sandeen * the devices in the system 2286f37938e0SEric Sandeen */ 22872ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 22882ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2289f37938e0SEric Sandeen 22902ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 229143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree, 229235da5a7eSDavid Sterba IO_TREE_BTREE_INODE_IO); 22932ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2294f37938e0SEric Sandeen 22955c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root); 2296adac5584SFilipe Manana BTRFS_I(inode)->location.objectid = BTRFS_BTREE_INODE_OBJECTID; 2297adac5584SFilipe Manana BTRFS_I(inode)->location.type = 0; 2298adac5584SFilipe Manana BTRFS_I(inode)->location.offset = 0; 22992ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 2300e256927bSJosef Bacik __insert_inode_hash(inode, hash); 2301f37938e0SEric Sandeen } 2302f37938e0SEric Sandeen 2303ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2304ad618368SEric Sandeen { 2305ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2306129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem); 23077f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait); 2308ad618368SEric Sandeen } 2309ad618368SEric Sandeen 2310f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2311f9e92e40SEric Sandeen { 2312f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2313f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2314f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2315f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2316f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2317f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2318d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2319011b46c3SQu Wenruo fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL; 2320f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2321f9e92e40SEric Sandeen } 2322f9e92e40SEric Sandeen 2323d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info) 23242a458198SEric Sandeen { 2325f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 23266f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23272a458198SEric Sandeen 23282a458198SEric Sandeen fs_info->workers = 2329a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16); 2330a31b4a43SChristoph Hellwig fs_info->hipri_workers = 2331a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker-high", 2332cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23332a458198SEric Sandeen 23342a458198SEric Sandeen fs_info->delalloc_workers = 2335cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2336cb001095SJeff Mahoney flags, max_active, 2); 23372a458198SEric Sandeen 23382a458198SEric Sandeen fs_info->flush_workers = 2339cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2340cb001095SJeff Mahoney flags, max_active, 0); 23412a458198SEric Sandeen 23422a458198SEric Sandeen fs_info->caching_workers = 2343cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23442a458198SEric Sandeen 23452a458198SEric Sandeen fs_info->fixup_workers = 2346cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23472a458198SEric Sandeen 23482a458198SEric Sandeen fs_info->endio_workers = 2349d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio", flags, max_active); 23502a458198SEric Sandeen fs_info->endio_meta_workers = 2351d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio-meta", flags, max_active); 2352385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active); 23532a458198SEric Sandeen fs_info->endio_write_workers = 2354cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2355cb001095SJeff Mahoney max_active, 2); 2356fed8a72dSChristoph Hellwig fs_info->compressed_write_workers = 2357fed8a72dSChristoph Hellwig alloc_workqueue("btrfs-compressed-write", flags, max_active); 23582a458198SEric Sandeen fs_info->endio_freespace_worker = 2359cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2360cb001095SJeff Mahoney max_active, 0); 23612a458198SEric Sandeen fs_info->delayed_workers = 2362cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2363cb001095SJeff Mahoney max_active, 0); 23642a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2365cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 2366b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers = 2367b0643e59SDennis Zhou alloc_workqueue("btrfs_discard", WQ_UNBOUND | WQ_FREEZABLE, 1); 23682a458198SEric Sandeen 2369a31b4a43SChristoph Hellwig if (!(fs_info->workers && fs_info->hipri_workers && 2370a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers && 23712a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 2372fed8a72dSChristoph Hellwig fs_info->compressed_write_workers && 23731a1a2851SQu Wenruo fs_info->endio_write_workers && 23742a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 2375f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers && 2376f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers && 2377b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) { 23782a458198SEric Sandeen return -ENOMEM; 23792a458198SEric Sandeen } 23802a458198SEric Sandeen 23812a458198SEric Sandeen return 0; 23822a458198SEric Sandeen } 23832a458198SEric Sandeen 23846d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) 23856d97c6e3SJohannes Thumshirn { 23866d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash; 2387b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type); 23886d97c6e3SJohannes Thumshirn 2389b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0); 23906d97c6e3SJohannes Thumshirn 23916d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) { 23926d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum", 2393b4e967beSDavid Sterba csum_driver); 23946d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash); 23956d97c6e3SJohannes Thumshirn } 23966d97c6e3SJohannes Thumshirn 23976d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash; 23986d97c6e3SJohannes Thumshirn 2399c8a5f8caSDavid Sterba btrfs_info(fs_info, "using %s (%s) checksum algorithm", 2400c8a5f8caSDavid Sterba btrfs_super_csum_name(csum_type), 2401c8a5f8caSDavid Sterba crypto_shash_driver_name(csum_shash)); 24026d97c6e3SJohannes Thumshirn return 0; 24036d97c6e3SJohannes Thumshirn } 24046d97c6e3SJohannes Thumshirn 240563443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 240663443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 240763443bf5SEric Sandeen { 240863443bf5SEric Sandeen int ret; 2409789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 241063443bf5SEric Sandeen struct btrfs_root *log_tree_root; 241163443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 241263443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2413581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 241463443bf5SEric Sandeen 241563443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2416f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 241763443bf5SEric Sandeen return -EIO; 241863443bf5SEric Sandeen } 241963443bf5SEric Sandeen 242096dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, 242196dfcb46SJosef Bacik GFP_KERNEL); 242263443bf5SEric Sandeen if (!log_tree_root) 242363443bf5SEric Sandeen return -ENOMEM; 242463443bf5SEric Sandeen 2425789d6a3aSQu Wenruo check.level = level; 2426789d6a3aSQu Wenruo check.transid = fs_info->generation + 1; 2427789d6a3aSQu Wenruo check.owner_root = BTRFS_TREE_LOG_OBJECTID; 2428789d6a3aSQu Wenruo log_tree_root->node = read_tree_block(fs_info, bytenr, &check); 242964c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2430f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24310eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 24328c38938cSJosef Bacik log_tree_root->node = NULL; 243300246528SJosef Bacik btrfs_put_root(log_tree_root); 24340eeff236SLiu Bo return ret; 24354eb150d6SQu Wenruo } 24364eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) { 2437f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 243800246528SJosef Bacik btrfs_put_root(log_tree_root); 243963443bf5SEric Sandeen return -EIO; 244063443bf5SEric Sandeen } 24414eb150d6SQu Wenruo 244263443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 244363443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 244463443bf5SEric Sandeen if (ret) { 24450b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 244663443bf5SEric Sandeen "Failed to recover log tree"); 244700246528SJosef Bacik btrfs_put_root(log_tree_root); 244863443bf5SEric Sandeen return ret; 244963443bf5SEric Sandeen } 245063443bf5SEric Sandeen 2451bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 24526bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 245363443bf5SEric Sandeen if (ret) 245463443bf5SEric Sandeen return ret; 245563443bf5SEric Sandeen } 245663443bf5SEric Sandeen 245763443bf5SEric Sandeen return 0; 245863443bf5SEric Sandeen } 245963443bf5SEric Sandeen 2460abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root, 2461abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid, 2462abed4aaaSJosef Bacik const char *name) 2463abed4aaaSJosef Bacik { 2464abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info; 2465abed4aaaSJosef Bacik struct btrfs_root *root; 2466f7238e50SJosef Bacik u64 max_global_id = 0; 2467abed4aaaSJosef Bacik int ret; 2468abed4aaaSJosef Bacik struct btrfs_key key = { 2469abed4aaaSJosef Bacik .objectid = objectid, 2470abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY, 2471abed4aaaSJosef Bacik .offset = 0, 2472abed4aaaSJosef Bacik }; 2473abed4aaaSJosef Bacik bool found = false; 2474abed4aaaSJosef Bacik 2475abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */ 2476abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID && 2477abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) { 2478abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2479abed4aaaSJosef Bacik return 0; 2480abed4aaaSJosef Bacik } 2481abed4aaaSJosef Bacik 2482abed4aaaSJosef Bacik while (1) { 2483abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); 2484abed4aaaSJosef Bacik if (ret < 0) 2485abed4aaaSJosef Bacik break; 2486abed4aaaSJosef Bacik 2487abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 2488abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path); 2489abed4aaaSJosef Bacik if (ret) { 2490abed4aaaSJosef Bacik if (ret > 0) 2491abed4aaaSJosef Bacik ret = 0; 2492abed4aaaSJosef Bacik break; 2493abed4aaaSJosef Bacik } 2494abed4aaaSJosef Bacik } 2495abed4aaaSJosef Bacik ret = 0; 2496abed4aaaSJosef Bacik 2497abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2498abed4aaaSJosef Bacik if (key.objectid != objectid) 2499abed4aaaSJosef Bacik break; 2500abed4aaaSJosef Bacik btrfs_release_path(path); 2501abed4aaaSJosef Bacik 2502f7238e50SJosef Bacik /* 2503f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for 2504f7238e50SJosef Bacik * everybody. 2505f7238e50SJosef Bacik */ 2506f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2507f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset); 2508f7238e50SJosef Bacik 2509abed4aaaSJosef Bacik found = true; 2510abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key); 2511abed4aaaSJosef Bacik if (IS_ERR(root)) { 2512abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2513abed4aaaSJosef Bacik ret = PTR_ERR(root); 2514abed4aaaSJosef Bacik break; 2515abed4aaaSJosef Bacik } 2516abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2517abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root); 2518abed4aaaSJosef Bacik if (ret) { 2519abed4aaaSJosef Bacik btrfs_put_root(root); 2520abed4aaaSJosef Bacik break; 2521abed4aaaSJosef Bacik } 2522abed4aaaSJosef Bacik key.offset++; 2523abed4aaaSJosef Bacik } 2524abed4aaaSJosef Bacik btrfs_release_path(path); 2525abed4aaaSJosef Bacik 2526f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID) 2527f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1; 2528f7238e50SJosef Bacik 2529abed4aaaSJosef Bacik if (!found || ret) { 2530abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID) 2531abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); 2532abed4aaaSJosef Bacik 2533abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) 2534abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT; 2535abed4aaaSJosef Bacik else 2536abed4aaaSJosef Bacik ret = 0; 2537abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name); 2538abed4aaaSJosef Bacik } 2539abed4aaaSJosef Bacik return ret; 2540abed4aaaSJosef Bacik } 2541abed4aaaSJosef Bacik 2542abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root) 2543abed4aaaSJosef Bacik { 2544abed4aaaSJosef Bacik struct btrfs_path *path; 2545abed4aaaSJosef Bacik int ret = 0; 2546abed4aaaSJosef Bacik 2547abed4aaaSJosef Bacik path = btrfs_alloc_path(); 2548abed4aaaSJosef Bacik if (!path) 2549abed4aaaSJosef Bacik return -ENOMEM; 2550abed4aaaSJosef Bacik 2551abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2552abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent"); 2553abed4aaaSJosef Bacik if (ret) 2554abed4aaaSJosef Bacik goto out; 2555abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2556abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum"); 2557abed4aaaSJosef Bacik if (ret) 2558abed4aaaSJosef Bacik goto out; 2559abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE)) 2560abed4aaaSJosef Bacik goto out; 2561abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path, 2562abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID, 2563abed4aaaSJosef Bacik "free space"); 2564abed4aaaSJosef Bacik out: 2565abed4aaaSJosef Bacik btrfs_free_path(path); 2566abed4aaaSJosef Bacik return ret; 2567abed4aaaSJosef Bacik } 2568abed4aaaSJosef Bacik 25696bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 25704bbcaa64SEric Sandeen { 25716bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2572a4f3d2c4SDavid Sterba struct btrfs_root *root; 25734bbcaa64SEric Sandeen struct btrfs_key location; 25744bbcaa64SEric Sandeen int ret; 25754bbcaa64SEric Sandeen 25766bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 25776bccf3abSJeff Mahoney 2578abed4aaaSJosef Bacik ret = load_global_roots(tree_root); 2579abed4aaaSJosef Bacik if (ret) 2580abed4aaaSJosef Bacik return ret; 2581abed4aaaSJosef Bacik 25824bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 25834bbcaa64SEric Sandeen location.offset = 0; 25844bbcaa64SEric Sandeen 25851c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) { 258614033b08SQu Wenruo location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID; 258714033b08SQu Wenruo root = btrfs_read_tree_root(tree_root, &location); 258814033b08SQu Wenruo if (IS_ERR(root)) { 258914033b08SQu Wenruo if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 259014033b08SQu Wenruo ret = PTR_ERR(root); 259114033b08SQu Wenruo goto out; 259214033b08SQu Wenruo } 259314033b08SQu Wenruo } else { 259414033b08SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 259514033b08SQu Wenruo fs_info->block_group_root = root; 259614033b08SQu Wenruo } 259714033b08SQu Wenruo } 259814033b08SQu Wenruo 259914033b08SQu Wenruo location.objectid = BTRFS_DEV_TREE_OBJECTID; 2600a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2601f50f4353SLiu Bo if (IS_ERR(root)) { 260242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2603f50f4353SLiu Bo ret = PTR_ERR(root); 2604f50f4353SLiu Bo goto out; 2605f50f4353SLiu Bo } 260642437a63SJosef Bacik } else { 2607a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2608a4f3d2c4SDavid Sterba fs_info->dev_root = root; 260942437a63SJosef Bacik } 2610820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */ 2611a8d1b164SJohannes Thumshirn ret = btrfs_init_devices_late(fs_info); 2612a8d1b164SJohannes Thumshirn if (ret) 2613a8d1b164SJohannes Thumshirn goto out; 26144bbcaa64SEric Sandeen 2615aeb935a4SQu Wenruo /* 2616aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation 2617aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root 2618aeb935a4SQu Wenruo */ 261956e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info, 262056e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true); 2621aeb935a4SQu Wenruo if (IS_ERR(root)) { 262242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2623aeb935a4SQu Wenruo ret = PTR_ERR(root); 2624aeb935a4SQu Wenruo goto out; 2625aeb935a4SQu Wenruo } 262642437a63SJosef Bacik } else { 2627aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2628aeb935a4SQu Wenruo fs_info->data_reloc_root = root; 262942437a63SJosef Bacik } 2630aeb935a4SQu Wenruo 26314bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2632a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2633a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2634a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2635afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2636a4f3d2c4SDavid Sterba fs_info->quota_root = root; 26374bbcaa64SEric Sandeen } 26384bbcaa64SEric Sandeen 26394bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2640a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2641a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 264242437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) { 2643a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 26444bbcaa64SEric Sandeen if (ret != -ENOENT) 2645f50f4353SLiu Bo goto out; 264642437a63SJosef Bacik } 26474bbcaa64SEric Sandeen } else { 2648a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2649a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 26504bbcaa64SEric Sandeen } 26514bbcaa64SEric Sandeen 26524bbcaa64SEric Sandeen return 0; 2653f50f4353SLiu Bo out: 2654f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2655f50f4353SLiu Bo location.objectid, ret); 2656f50f4353SLiu Bo return ret; 26574bbcaa64SEric Sandeen } 26584bbcaa64SEric Sandeen 2659069ec957SQu Wenruo /* 2660069ec957SQu Wenruo * Real super block validation 2661069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here. 2662069ec957SQu Wenruo * 2663069ec957SQu Wenruo * @sb: super block to check 2664069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr: 2665069ec957SQu Wenruo * 0 the primary (1st) sb 2666069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy 2667069ec957SQu Wenruo * -1 skip bytenr check 2668069ec957SQu Wenruo */ 2669a05d3c91SQu Wenruo int btrfs_validate_super(struct btrfs_fs_info *fs_info, 2670069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num) 267121a852b0SQu Wenruo { 267221a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 267321a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 267421a852b0SQu Wenruo int ret = 0; 267521a852b0SQu Wenruo 267621a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 267721a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found"); 267821a852b0SQu Wenruo ret = -EINVAL; 267921a852b0SQu Wenruo } 268021a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 268121a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 268221a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 268321a852b0SQu Wenruo ret = -EINVAL; 268421a852b0SQu Wenruo } 268521a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 268621a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d", 268721a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 268821a852b0SQu Wenruo ret = -EINVAL; 268921a852b0SQu Wenruo } 269021a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 269121a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 269221a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 269321a852b0SQu Wenruo ret = -EINVAL; 269421a852b0SQu Wenruo } 269521a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 269621a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d", 269721a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 269821a852b0SQu Wenruo ret = -EINVAL; 269921a852b0SQu Wenruo } 270021a852b0SQu Wenruo 270121a852b0SQu Wenruo /* 270221a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 270321a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 270421a852b0SQu Wenruo */ 270521a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 270621a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 270721a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 270821a852b0SQu Wenruo ret = -EINVAL; 270921a852b0SQu Wenruo } 27100bb3eb3eSQu Wenruo 27110bb3eb3eSQu Wenruo /* 27121a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE. 27131a42daabSQu Wenruo * 27141a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem, 27151a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense. 27161a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very 27171a42daabSQu Wenruo * beginning. 27180bb3eb3eSQu Wenruo */ 27191a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) { 272021a852b0SQu Wenruo btrfs_err(fs_info, 27210bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu", 272221a852b0SQu Wenruo sectorsize, PAGE_SIZE); 272321a852b0SQu Wenruo ret = -EINVAL; 272421a852b0SQu Wenruo } 27250bb3eb3eSQu Wenruo 272621a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 272721a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 272821a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 272921a852b0SQu Wenruo ret = -EINVAL; 273021a852b0SQu Wenruo } 273121a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 273221a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 273321a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize); 273421a852b0SQu Wenruo ret = -EINVAL; 273521a852b0SQu Wenruo } 273621a852b0SQu Wenruo 273721a852b0SQu Wenruo /* Root alignment check */ 273821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 273921a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu", 274021a852b0SQu Wenruo btrfs_super_root(sb)); 274121a852b0SQu Wenruo ret = -EINVAL; 274221a852b0SQu Wenruo } 274321a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 274421a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 274521a852b0SQu Wenruo btrfs_super_chunk_root(sb)); 274621a852b0SQu Wenruo ret = -EINVAL; 274721a852b0SQu Wenruo } 274821a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 274921a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu", 275021a852b0SQu Wenruo btrfs_super_log_root(sb)); 275121a852b0SQu Wenruo ret = -EINVAL; 275221a852b0SQu Wenruo } 275321a852b0SQu Wenruo 2754aefd7f70SNikolay Borisov if (memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid, 2755aefd7f70SNikolay Borisov BTRFS_FSID_SIZE)) { 2756aefd7f70SNikolay Borisov btrfs_err(fs_info, 2757aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU", 2758aefd7f70SNikolay Borisov fs_info->super_copy->fsid, fs_info->fs_devices->fsid); 2759aefd7f70SNikolay Borisov ret = -EINVAL; 2760aefd7f70SNikolay Borisov } 2761aefd7f70SNikolay Borisov 2762aefd7f70SNikolay Borisov if (btrfs_fs_incompat(fs_info, METADATA_UUID) && 2763aefd7f70SNikolay Borisov memcmp(fs_info->fs_devices->metadata_uuid, 2764aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE)) { 2765aefd7f70SNikolay Borisov btrfs_err(fs_info, 2766aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU", 2767aefd7f70SNikolay Borisov fs_info->super_copy->metadata_uuid, 2768aefd7f70SNikolay Borisov fs_info->fs_devices->metadata_uuid); 2769aefd7f70SNikolay Borisov ret = -EINVAL; 2770aefd7f70SNikolay Borisov } 2771aefd7f70SNikolay Borisov 27721c56ab99SQu Wenruo /* 27731c56ab99SQu Wenruo * Artificial requirement for block-group-tree to force newer features 27741c56ab99SQu Wenruo * (free-space-tree, no-holes) so the test matrix is smaller. 27751c56ab99SQu Wenruo */ 27761c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) && 27771c56ab99SQu Wenruo (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) || 27781c56ab99SQu Wenruo !btrfs_fs_incompat(fs_info, NO_HOLES))) { 27791c56ab99SQu Wenruo btrfs_err(fs_info, 27801c56ab99SQu Wenruo "block-group-tree feature requires fres-space-tree and no-holes"); 27811c56ab99SQu Wenruo ret = -EINVAL; 27821c56ab99SQu Wenruo } 27831c56ab99SQu Wenruo 2784de37aa51SNikolay Borisov if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid, 27857239ff4bSNikolay Borisov BTRFS_FSID_SIZE) != 0) { 278621a852b0SQu Wenruo btrfs_err(fs_info, 27877239ff4bSNikolay Borisov "dev_item UUID does not match metadata fsid: %pU != %pU", 2788de37aa51SNikolay Borisov fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid); 278921a852b0SQu Wenruo ret = -EINVAL; 279021a852b0SQu Wenruo } 279121a852b0SQu Wenruo 279221a852b0SQu Wenruo /* 279321a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are 279421a852b0SQu Wenruo * done later 279521a852b0SQu Wenruo */ 279621a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 279721a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu", 279821a852b0SQu Wenruo btrfs_super_bytes_used(sb)); 279921a852b0SQu Wenruo ret = -EINVAL; 280021a852b0SQu Wenruo } 280121a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) { 280221a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u", 280321a852b0SQu Wenruo btrfs_super_stripesize(sb)); 280421a852b0SQu Wenruo ret = -EINVAL; 280521a852b0SQu Wenruo } 280621a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31)) 280721a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu", 280821a852b0SQu Wenruo btrfs_super_num_devices(sb)); 280921a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) { 281021a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0"); 281121a852b0SQu Wenruo ret = -EINVAL; 281221a852b0SQu Wenruo } 281321a852b0SQu Wenruo 2814069ec957SQu Wenruo if (mirror_num >= 0 && 2815069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) { 281621a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u", 281721a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 281821a852b0SQu Wenruo ret = -EINVAL; 281921a852b0SQu Wenruo } 282021a852b0SQu Wenruo 282121a852b0SQu Wenruo /* 282221a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key 282321a852b0SQu Wenruo * and one chunk 282421a852b0SQu Wenruo */ 282521a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 282621a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u", 282721a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 282821a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 282921a852b0SQu Wenruo ret = -EINVAL; 283021a852b0SQu Wenruo } 283121a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 283221a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) { 283321a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu", 283421a852b0SQu Wenruo btrfs_super_sys_array_size(sb), 283521a852b0SQu Wenruo sizeof(struct btrfs_disk_key) 283621a852b0SQu Wenruo + sizeof(struct btrfs_chunk)); 283721a852b0SQu Wenruo ret = -EINVAL; 283821a852b0SQu Wenruo } 283921a852b0SQu Wenruo 284021a852b0SQu Wenruo /* 284121a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others 284221a852b0SQu Wenruo * but it's still possible that it's the one that's wrong. 284321a852b0SQu Wenruo */ 284421a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 284521a852b0SQu Wenruo btrfs_warn(fs_info, 284621a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu", 284721a852b0SQu Wenruo btrfs_super_generation(sb), 284821a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb)); 284921a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 285021a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1) 285121a852b0SQu Wenruo btrfs_warn(fs_info, 285221a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu", 285321a852b0SQu Wenruo btrfs_super_generation(sb), 285421a852b0SQu Wenruo btrfs_super_cache_generation(sb)); 285521a852b0SQu Wenruo 285621a852b0SQu Wenruo return ret; 285721a852b0SQu Wenruo } 285821a852b0SQu Wenruo 2859069ec957SQu Wenruo /* 2860069ec957SQu Wenruo * Validation of super block at mount time. 2861069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat 2862069ec957SQu Wenruo * flags will be skipped. 2863069ec957SQu Wenruo */ 2864069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info) 2865069ec957SQu Wenruo { 2866a05d3c91SQu Wenruo return btrfs_validate_super(fs_info, fs_info->super_copy, 0); 2867069ec957SQu Wenruo } 2868069ec957SQu Wenruo 286975cb857dSQu Wenruo /* 287075cb857dSQu Wenruo * Validation of super block at write time. 287175cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be 287275cb857dSQu Wenruo * overwritten soon. 287375cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here. 287475cb857dSQu Wenruo */ 287575cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info, 287675cb857dSQu Wenruo struct btrfs_super_block *sb) 287775cb857dSQu Wenruo { 287875cb857dSQu Wenruo int ret; 287975cb857dSQu Wenruo 2880a05d3c91SQu Wenruo ret = btrfs_validate_super(fs_info, sb, -1); 288175cb857dSQu Wenruo if (ret < 0) 288275cb857dSQu Wenruo goto out; 2883e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) { 288475cb857dSQu Wenruo ret = -EUCLEAN; 288575cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u", 288675cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32); 288775cb857dSQu Wenruo goto out; 288875cb857dSQu Wenruo } 288975cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 289075cb857dSQu Wenruo ret = -EUCLEAN; 289175cb857dSQu Wenruo btrfs_err(fs_info, 289275cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx", 289375cb857dSQu Wenruo btrfs_super_incompat_flags(sb), 289475cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP); 289575cb857dSQu Wenruo goto out; 289675cb857dSQu Wenruo } 289775cb857dSQu Wenruo out: 289875cb857dSQu Wenruo if (ret < 0) 289975cb857dSQu Wenruo btrfs_err(fs_info, 290075cb857dSQu Wenruo "super block corruption detected before writing it to disk"); 290175cb857dSQu Wenruo return ret; 290275cb857dSQu Wenruo } 290375cb857dSQu Wenruo 2904bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level) 2905bd676446SJosef Bacik { 2906789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 2907789d6a3aSQu Wenruo .level = level, 2908789d6a3aSQu Wenruo .transid = gen, 2909789d6a3aSQu Wenruo .owner_root = root->root_key.objectid 2910789d6a3aSQu Wenruo }; 2911bd676446SJosef Bacik int ret = 0; 2912bd676446SJosef Bacik 2913789d6a3aSQu Wenruo root->node = read_tree_block(root->fs_info, bytenr, &check); 2914bd676446SJosef Bacik if (IS_ERR(root->node)) { 2915bd676446SJosef Bacik ret = PTR_ERR(root->node); 2916bd676446SJosef Bacik root->node = NULL; 29174eb150d6SQu Wenruo return ret; 29184eb150d6SQu Wenruo } 29194eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) { 2920bd676446SJosef Bacik free_extent_buffer(root->node); 2921bd676446SJosef Bacik root->node = NULL; 29224eb150d6SQu Wenruo return -EIO; 2923bd676446SJosef Bacik } 2924bd676446SJosef Bacik 2925bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node); 2926bd676446SJosef Bacik root->commit_root = btrfs_root_node(root); 2927bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1); 2928bd676446SJosef Bacik return ret; 2929bd676446SJosef Bacik } 2930bd676446SJosef Bacik 2931bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info) 2932bd676446SJosef Bacik { 2933bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy; 2934bd676446SJosef Bacik u64 gen, bytenr; 2935bd676446SJosef Bacik int level, ret; 2936bd676446SJosef Bacik 2937bd676446SJosef Bacik bytenr = btrfs_super_root(sb); 2938bd676446SJosef Bacik gen = btrfs_super_generation(sb); 2939bd676446SJosef Bacik level = btrfs_super_root_level(sb); 2940bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level); 29419c54e80dSJosef Bacik if (ret) { 2942bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root"); 2943bd676446SJosef Bacik return ret; 2944bd676446SJosef Bacik } 29459c54e80dSJosef Bacik return 0; 29469c54e80dSJosef Bacik } 29479c54e80dSJosef Bacik 29486ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) 2949b8522a1eSNikolay Borisov { 29506ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info); 2951b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy; 2952b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root; 2953b8522a1eSNikolay Borisov bool handle_error = false; 2954b8522a1eSNikolay Borisov int ret = 0; 2955b8522a1eSNikolay Borisov int i; 2956b8522a1eSNikolay Borisov 2957b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2958b8522a1eSNikolay Borisov if (handle_error) { 2959b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node)) 2960b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node); 2961b8522a1eSNikolay Borisov tree_root->node = NULL; 2962b8522a1eSNikolay Borisov 2963b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 2964b8522a1eSNikolay Borisov break; 2965b8522a1eSNikolay Borisov 2966b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0); 2967b8522a1eSNikolay Borisov 2968b8522a1eSNikolay Borisov /* 2969b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be 2970b8522a1eSNikolay Borisov * valid 2971b8522a1eSNikolay Borisov */ 2972b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0); 2973b8522a1eSNikolay Borisov 2974b8522a1eSNikolay Borisov /* We can't trust the free space cache either */ 2975b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2976b8522a1eSNikolay Borisov 2977b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i); 29786ef108ddSNikolay Borisov backup_index = ret; 2979b8522a1eSNikolay Borisov if (ret < 0) 2980b8522a1eSNikolay Borisov return ret; 2981b8522a1eSNikolay Borisov } 2982b8522a1eSNikolay Borisov 2983bd676446SJosef Bacik ret = load_important_roots(fs_info); 2984bd676446SJosef Bacik if (ret) { 2985217f5004SNikolay Borisov handle_error = true; 2986b8522a1eSNikolay Borisov continue; 2987b8522a1eSNikolay Borisov } 2988b8522a1eSNikolay Borisov 2989336a0d8dSNikolay Borisov /* 2990336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs 2991336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user 2992336a0d8dSNikolay Borisov */ 2993453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root); 2994b8522a1eSNikolay Borisov if (ret < 0) { 2995b8522a1eSNikolay Borisov handle_error = true; 2996b8522a1eSNikolay Borisov continue; 2997b8522a1eSNikolay Borisov } 2998b8522a1eSNikolay Borisov 29996b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID); 3000b8522a1eSNikolay Borisov 3001b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info); 3002b8522a1eSNikolay Borisov if (ret < 0) { 3003b8522a1eSNikolay Borisov handle_error = true; 3004b8522a1eSNikolay Borisov continue; 3005b8522a1eSNikolay Borisov } 3006b8522a1eSNikolay Borisov 3007b8522a1eSNikolay Borisov /* All successful */ 3008bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node); 3009bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation; 3010d96b3424SFilipe Manana fs_info->last_reloc_trans = 0; 30116ef108ddSNikolay Borisov 30126ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */ 30136ef108ddSNikolay Borisov if (backup_index < 0) { 30146ef108ddSNikolay Borisov fs_info->backup_root_index = 0; 30156ef108ddSNikolay Borisov } else { 30166ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1; 30176ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS; 30186ef108ddSNikolay Borisov } 3019b8522a1eSNikolay Borisov break; 3020b8522a1eSNikolay Borisov } 3021b8522a1eSNikolay Borisov 3022b8522a1eSNikolay Borisov return ret; 3023b8522a1eSNikolay Borisov } 3024b8522a1eSNikolay Borisov 30258260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) 3026eb60ceacSChris Mason { 3027fc7cbcd4SDavid Sterba INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 302801cd3909SDavid Sterba INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 30298fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 3030facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 303124bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 3032eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 303311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 3034eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 3035a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 3036fc7cbcd4SDavid Sterba spin_lock_init(&fs_info->fs_roots_radix_lock); 303724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 30384cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 3039ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 3040f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 304147ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 304240ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock); 3043afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock); 3044c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock); 3045f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 3046abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock); 3047d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 3048f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock); 30497585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 3050573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 30510bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock); 30525f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock); 3053de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 305419c00ddcSChris Mason 3055e1489b4fSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers); 30565a9ba670SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters); 30578b53779eSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered); 30585f4403e1SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent); 30593e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_start, 30603e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_COMMIT_START); 30613e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked, 30623e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_UNBLOCKED); 30633e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed, 30643e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED); 30653e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed, 30663e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_COMPLETED); 3067e1489b4fSIoannis Angelakopoulos 30680b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 30696324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 3070f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 307147ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 307218bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs); 3073afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs); 3074bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 3075bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots); 30763fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs); 30773fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock); 3078bd647ce3SJosef Bacik #endif 3079c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree); 308066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 308166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 308266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 308366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 308466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 308566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 308666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 3087ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv, 3088ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS); 3089ba2c4d4eSJosef Bacik 3090771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 30914cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 3092034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0); 3093fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 3094abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT; 309595ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 30969ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 30974cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 3098a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 3099f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 31008b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 3101f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 3102fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 3103c8b97818SChris Mason 3104b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 3105b34b086cSChris Mason num_online_cpus() + 2, 8); 31060afbaf8cSChris Mason 3107199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 3108199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 310969fe2d75SJosef Bacik 3110638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 311121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 311221adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 311321adbd5cSStefan Behrens #endif 3114779a65a4SEric Sandeen btrfs_init_balance(fs_info); 311557056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info); 3116a2de733cSArne Jansen 311716b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock); 311808dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED; 31190f9dd46cSJosef Bacik 3120fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents, 312135da5a7eSDavid Sterba IO_TREE_FS_EXCLUDED_EXTENTS); 312239279cc3SChris Mason 31235a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 3124e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex); 3125925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 3126a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 3127a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 31281bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 31299e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 3130c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 313176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 3132803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 3133fa9c0d79SChris Mason 3134ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 3135f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 3136b0643e59SDennis Zhou btrfs_discard_init(fs_info); 3137416ac51dSArne Jansen 3138fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 3139fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 3140fa9c0d79SChris Mason 3141e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 3142f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 3143bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 31444854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 3145034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait); 31463768f368SChris Mason 3147da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 3148da17066cSJeff Mahoney fs_info->nodesize = 4096; 3149da17066cSJeff Mahoney fs_info->sectorsize = 4096; 3150ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096); 3151da17066cSJeff Mahoney fs_info->stripesize = 4096; 3152da17066cSJeff Mahoney 3153f7b12a62SNaohiro Aota fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE; 3154f7b12a62SNaohiro Aota 3155eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock); 3156eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT; 3157eede2bf3SOmar Sandoval 315818bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH; 315918bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work); 31608260edbaSJosef Bacik } 31618260edbaSJosef Bacik 31628260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb) 31638260edbaSJosef Bacik { 31648260edbaSJosef Bacik int ret; 31658260edbaSJosef Bacik 31668260edbaSJosef Bacik fs_info->sb = sb; 31678260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 31688260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 31699e967495SFilipe Manana 31705deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL); 3171ae18c37aSJosef Bacik if (ret) 3172ae18c37aSJosef Bacik return ret; 3173ae18c37aSJosef Bacik 3174ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 3175ae18c37aSJosef Bacik if (ret) 3176c75e8394SJosef Bacik return ret; 3177ae18c37aSJosef Bacik 3178ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE * 3179ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids)); 3180ae18c37aSJosef Bacik 3181ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 3182ae18c37aSJosef Bacik if (ret) 3183c75e8394SJosef Bacik return ret; 3184ae18c37aSJosef Bacik 3185ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0, 3186ae18c37aSJosef Bacik GFP_KERNEL); 3187ae18c37aSJosef Bacik if (ret) 3188c75e8394SJosef Bacik return ret; 3189ae18c37aSJosef Bacik 3190ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 3191ae18c37aSJosef Bacik GFP_KERNEL); 3192c75e8394SJosef Bacik if (!fs_info->delayed_root) 3193c75e8394SJosef Bacik return -ENOMEM; 3194ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root); 3195ae18c37aSJosef Bacik 3196a0a1db70SFilipe Manana if (sb_rdonly(sb)) 3197a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 3198a0a1db70SFilipe Manana 3199c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info); 3200ae18c37aSJosef Bacik } 3201ae18c37aSJosef Bacik 320297f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data) 320397f4dd09SNikolay Borisov { 32040d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data; 320597f4dd09SNikolay Borisov int ret; 320697f4dd09SNikolay Borisov 320797f4dd09SNikolay Borisov /* 320897f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and 320997f4dd09SNikolay Borisov * to delete all entries that contain outdated data. 321097f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree. 321197f4dd09SNikolay Borisov */ 321297f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info); 321397f4dd09SNikolay Borisov if (ret < 0) { 3214c94bec2cSJosef Bacik if (ret != -EINTR) 3215c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d", 3216c94bec2cSJosef Bacik ret); 321797f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 321897f4dd09SNikolay Borisov return ret; 321997f4dd09SNikolay Borisov } 322097f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data); 322197f4dd09SNikolay Borisov } 322297f4dd09SNikolay Borisov 322397f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) 322497f4dd09SNikolay Borisov { 322597f4dd09SNikolay Borisov struct task_struct *task; 322697f4dd09SNikolay Borisov 322797f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem); 322897f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); 322997f4dd09SNikolay Borisov if (IS_ERR(task)) { 323097f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */ 323197f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task"); 323297f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem); 323397f4dd09SNikolay Borisov return PTR_ERR(task); 323497f4dd09SNikolay Borisov } 323597f4dd09SNikolay Borisov 323697f4dd09SNikolay Borisov return 0; 323797f4dd09SNikolay Borisov } 323897f4dd09SNikolay Borisov 323944c0ca21SBoris Burkov /* 32408cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across 32418cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount 32428cd29088SBoris Burkov * code paths. 32438cd29088SBoris Burkov */ 32448cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) 32458cd29088SBoris Burkov { 32468cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32478b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE); 32488cd29088SBoris Burkov } 32498cd29088SBoris Burkov 32508cd29088SBoris Burkov /* 325144c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree 325244c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write. 325344c0ca21SBoris Burkov */ 325444c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) 325544c0ca21SBoris Burkov { 325644c0ca21SBoris Burkov int ret; 325794846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 32588b228324SBoris Burkov bool clear_free_space_tree = false; 32598b228324SBoris Burkov 32608b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 32618b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 32628b228324SBoris Burkov clear_free_space_tree = true; 32638b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 32648b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 32658b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid"); 32668b228324SBoris Burkov clear_free_space_tree = true; 32678b228324SBoris Burkov } 32688b228324SBoris Burkov 32698b228324SBoris Burkov if (clear_free_space_tree) { 32708b228324SBoris Burkov btrfs_info(fs_info, "clearing free space tree"); 32718b228324SBoris Burkov ret = btrfs_clear_free_space_tree(fs_info); 32728b228324SBoris Burkov if (ret) { 32738b228324SBoris Burkov btrfs_warn(fs_info, 32748b228324SBoris Burkov "failed to clear free space tree: %d", ret); 32758b228324SBoris Burkov goto out; 32768b228324SBoris Burkov } 32778b228324SBoris Burkov } 327844c0ca21SBoris Burkov 32798d488a8cSFilipe Manana /* 32808d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead 32818d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load 3282fc7cbcd4SDavid Sterba * them into the fs_info->fs_roots_radix tree. This must be done before 32838d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it 32848d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan 32858d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount 32868d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not 32878d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not 32888d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion. 32898d488a8cSFilipe Manana */ 32908d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info); 32918d488a8cSFilipe Manana if (ret) 32928d488a8cSFilipe Manana goto out; 32938d488a8cSFilipe Manana 329444c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info); 329544c0ca21SBoris Burkov if (ret) 329644c0ca21SBoris Burkov goto out; 329744c0ca21SBoris Burkov 32988f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem); 32998f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 33008f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 33018f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33028f1c21d7SBoris Burkov goto out; 33038f1c21d7SBoris Burkov } 33048f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem); 33058f1c21d7SBoris Burkov 330644c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex); 33077eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info); 330844c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex); 330944c0ca21SBoris Burkov if (ret < 0) { 331044c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret); 331144c0ca21SBoris Burkov goto out; 331244c0ca21SBoris Burkov } 331344c0ca21SBoris Burkov 33145011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 33155011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 33165011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree"); 33175011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info); 33185011139aSBoris Burkov if (ret) { 33195011139aSBoris Burkov btrfs_warn(fs_info, 33205011139aSBoris Burkov "failed to create free space tree: %d", ret); 33215011139aSBoris Burkov goto out; 33225011139aSBoris Burkov } 33235011139aSBoris Burkov } 33245011139aSBoris Burkov 332594846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { 332694846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 332794846229SBoris Burkov if (ret) 332894846229SBoris Burkov goto out; 332994846229SBoris Burkov } 333094846229SBoris Burkov 333144c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info); 333244c0ca21SBoris Burkov if (ret) 333344c0ca21SBoris Burkov goto out; 333444c0ca21SBoris Burkov 333544c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info); 333644c0ca21SBoris Burkov if (ret) { 333744c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace"); 333844c0ca21SBoris Burkov goto out; 333944c0ca21SBoris Burkov } 334044c0ca21SBoris Burkov 334144c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info); 334244c0ca21SBoris Burkov 334344c0ca21SBoris Burkov if (!fs_info->uuid_root) { 334444c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree"); 334544c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info); 334644c0ca21SBoris Burkov if (ret) { 334744c0ca21SBoris Burkov btrfs_warn(fs_info, 334844c0ca21SBoris Burkov "failed to create the UUID tree %d", ret); 334944c0ca21SBoris Burkov goto out; 335044c0ca21SBoris Burkov } 335144c0ca21SBoris Burkov } 335244c0ca21SBoris Burkov 335344c0ca21SBoris Burkov out: 335444c0ca21SBoris Burkov return ret; 335544c0ca21SBoris Burkov } 335644c0ca21SBoris Burkov 3357d7f67ac9SQu Wenruo /* 3358d7f67ac9SQu Wenruo * Do various sanity and dependency checks of different features. 3359d7f67ac9SQu Wenruo * 3360d7f67ac9SQu Wenruo * This is the place for less strict checks (like for subpage or artificial 3361d7f67ac9SQu Wenruo * feature dependencies). 3362d7f67ac9SQu Wenruo * 3363d7f67ac9SQu Wenruo * For strict checks or possible corruption detection, see 3364d7f67ac9SQu Wenruo * btrfs_validate_super(). 3365d7f67ac9SQu Wenruo * 3366d7f67ac9SQu Wenruo * This should be called after btrfs_parse_options(), as some mount options 3367d7f67ac9SQu Wenruo * (space cache related) can modify on-disk format like free space tree and 3368d7f67ac9SQu Wenruo * screw up certain feature dependencies. 3369d7f67ac9SQu Wenruo */ 3370d7f67ac9SQu Wenruo int btrfs_check_features(struct btrfs_fs_info *fs_info, struct super_block *sb) 3371d7f67ac9SQu Wenruo { 3372d7f67ac9SQu Wenruo struct btrfs_super_block *disk_super = fs_info->super_copy; 3373d7f67ac9SQu Wenruo u64 incompat = btrfs_super_incompat_flags(disk_super); 3374d7f67ac9SQu Wenruo const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super); 3375d7f67ac9SQu Wenruo const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP); 3376d7f67ac9SQu Wenruo 3377d7f67ac9SQu Wenruo if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) { 3378d7f67ac9SQu Wenruo btrfs_err(fs_info, 3379d7f67ac9SQu Wenruo "cannot mount because of unknown incompat features (0x%llx)", 3380d7f67ac9SQu Wenruo incompat); 3381d7f67ac9SQu Wenruo return -EINVAL; 3382d7f67ac9SQu Wenruo } 3383d7f67ac9SQu Wenruo 3384d7f67ac9SQu Wenruo /* Runtime limitation for mixed block groups. */ 3385d7f67ac9SQu Wenruo if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 3386d7f67ac9SQu Wenruo (fs_info->sectorsize != fs_info->nodesize)) { 3387d7f67ac9SQu Wenruo btrfs_err(fs_info, 3388d7f67ac9SQu Wenruo "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 3389d7f67ac9SQu Wenruo fs_info->nodesize, fs_info->sectorsize); 3390d7f67ac9SQu Wenruo return -EINVAL; 3391d7f67ac9SQu Wenruo } 3392d7f67ac9SQu Wenruo 3393d7f67ac9SQu Wenruo /* Mixed backref is an always-enabled feature. */ 3394d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 3395d7f67ac9SQu Wenruo 3396d7f67ac9SQu Wenruo /* Set compression related flags just in case. */ 3397d7f67ac9SQu Wenruo if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 3398d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 3399d7f67ac9SQu Wenruo else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 3400d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 3401d7f67ac9SQu Wenruo 3402d7f67ac9SQu Wenruo /* 3403d7f67ac9SQu Wenruo * An ancient flag, which should really be marked deprecated. 3404d7f67ac9SQu Wenruo * Such runtime limitation doesn't really need a incompat flag. 3405d7f67ac9SQu Wenruo */ 3406d7f67ac9SQu Wenruo if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) 3407d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 3408d7f67ac9SQu Wenruo 3409d7f67ac9SQu Wenruo if (compat_ro_unsupp && !sb_rdonly(sb)) { 3410d7f67ac9SQu Wenruo btrfs_err(fs_info, 3411d7f67ac9SQu Wenruo "cannot mount read-write because of unknown compat_ro features (0x%llx)", 3412d7f67ac9SQu Wenruo compat_ro); 3413d7f67ac9SQu Wenruo return -EINVAL; 3414d7f67ac9SQu Wenruo } 3415d7f67ac9SQu Wenruo 3416d7f67ac9SQu Wenruo /* 3417d7f67ac9SQu Wenruo * We have unsupported RO compat features, although RO mounted, we 3418d7f67ac9SQu Wenruo * should not cause any metadata writes, including log replay. 3419d7f67ac9SQu Wenruo * Or we could screw up whatever the new feature requires. 3420d7f67ac9SQu Wenruo */ 3421d7f67ac9SQu Wenruo if (compat_ro_unsupp && btrfs_super_log_root(disk_super) && 3422d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3423d7f67ac9SQu Wenruo btrfs_err(fs_info, 3424d7f67ac9SQu Wenruo "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay", 3425d7f67ac9SQu Wenruo compat_ro); 3426d7f67ac9SQu Wenruo return -EINVAL; 3427d7f67ac9SQu Wenruo } 3428d7f67ac9SQu Wenruo 3429d7f67ac9SQu Wenruo /* 3430d7f67ac9SQu Wenruo * Artificial limitations for block group tree, to force 3431d7f67ac9SQu Wenruo * block-group-tree to rely on no-holes and free-space-tree. 3432d7f67ac9SQu Wenruo */ 3433d7f67ac9SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) && 3434d7f67ac9SQu Wenruo (!btrfs_fs_incompat(fs_info, NO_HOLES) || 3435d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) { 3436d7f67ac9SQu Wenruo btrfs_err(fs_info, 3437d7f67ac9SQu Wenruo "block-group-tree feature requires no-holes and free-space-tree features"); 3438d7f67ac9SQu Wenruo return -EINVAL; 3439d7f67ac9SQu Wenruo } 3440d7f67ac9SQu Wenruo 3441d7f67ac9SQu Wenruo /* 3442d7f67ac9SQu Wenruo * Subpage runtime limitation on v1 cache. 3443d7f67ac9SQu Wenruo * 3444d7f67ac9SQu Wenruo * V1 space cache still has some hard codeed PAGE_SIZE usage, while 3445d7f67ac9SQu Wenruo * we're already defaulting to v2 cache, no need to bother v1 as it's 3446d7f67ac9SQu Wenruo * going to be deprecated anyway. 3447d7f67ac9SQu Wenruo */ 3448d7f67ac9SQu Wenruo if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) { 3449d7f67ac9SQu Wenruo btrfs_warn(fs_info, 3450d7f67ac9SQu Wenruo "v1 space cache is not supported for page size %lu with sectorsize %u", 3451d7f67ac9SQu Wenruo PAGE_SIZE, fs_info->sectorsize); 3452d7f67ac9SQu Wenruo return -EINVAL; 3453d7f67ac9SQu Wenruo } 3454d7f67ac9SQu Wenruo 3455d7f67ac9SQu Wenruo /* This can be called by remount, we need to protect the super block. */ 3456d7f67ac9SQu Wenruo spin_lock(&fs_info->super_lock); 3457d7f67ac9SQu Wenruo btrfs_set_super_incompat_flags(disk_super, incompat); 3458d7f67ac9SQu Wenruo spin_unlock(&fs_info->super_lock); 3459d7f67ac9SQu Wenruo 3460d7f67ac9SQu Wenruo return 0; 3461d7f67ac9SQu Wenruo } 3462d7f67ac9SQu Wenruo 3463ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices, 3464ae18c37aSJosef Bacik char *options) 3465ae18c37aSJosef Bacik { 3466ae18c37aSJosef Bacik u32 sectorsize; 3467ae18c37aSJosef Bacik u32 nodesize; 3468ae18c37aSJosef Bacik u32 stripesize; 3469ae18c37aSJosef Bacik u64 generation; 3470ae18c37aSJosef Bacik u64 features; 3471ae18c37aSJosef Bacik u16 csum_type; 3472ae18c37aSJosef Bacik struct btrfs_super_block *disk_super; 3473ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb); 3474ae18c37aSJosef Bacik struct btrfs_root *tree_root; 3475ae18c37aSJosef Bacik struct btrfs_root *chunk_root; 3476ae18c37aSJosef Bacik int ret; 3477ae18c37aSJosef Bacik int err = -EINVAL; 3478ae18c37aSJosef Bacik int level; 3479ae18c37aSJosef Bacik 34808260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb); 348153b381b3SDavid Woodhouse if (ret) { 348283c8266aSDavid Sterba err = ret; 3483ae18c37aSJosef Bacik goto fail; 348453b381b3SDavid Woodhouse } 348553b381b3SDavid Woodhouse 3486ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */ 3487ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, 3488ae18c37aSJosef Bacik GFP_KERNEL); 3489ae18c37aSJosef Bacik fs_info->tree_root = tree_root; 3490ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID, 3491ae18c37aSJosef Bacik GFP_KERNEL); 3492ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root; 3493ae18c37aSJosef Bacik if (!tree_root || !chunk_root) { 3494ae18c37aSJosef Bacik err = -ENOMEM; 3495c75e8394SJosef Bacik goto fail; 3496ae18c37aSJosef Bacik } 3497ae18c37aSJosef Bacik 3498ae18c37aSJosef Bacik fs_info->btree_inode = new_inode(sb); 3499ae18c37aSJosef Bacik if (!fs_info->btree_inode) { 3500ae18c37aSJosef Bacik err = -ENOMEM; 3501c75e8394SJosef Bacik goto fail; 3502ae18c37aSJosef Bacik } 3503ae18c37aSJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 3504ae18c37aSJosef Bacik btrfs_init_btree_inode(fs_info); 3505ae18c37aSJosef Bacik 3506d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev); 35071104a885SDavid Sterba 35081104a885SDavid Sterba /* 35091104a885SDavid Sterba * Read super block and check the signature bytes only 35101104a885SDavid Sterba */ 3511d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev); 35128f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) { 35138f32380dSJohannes Thumshirn err = PTR_ERR(disk_super); 351416cdcec7SMiao Xie goto fail_alloc; 351520b45077SDave Young } 351639279cc3SChris Mason 35171104a885SDavid Sterba /* 3518260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are 35198dc3f22cSJohannes Thumshirn * corrupted, we'll find out 35208dc3f22cSJohannes Thumshirn */ 35218f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super); 352251bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) { 35238dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u", 352451bce6c9SJohannes Thumshirn csum_type); 35258dc3f22cSJohannes Thumshirn err = -EINVAL; 35268f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35278dc3f22cSJohannes Thumshirn goto fail_alloc; 35288dc3f22cSJohannes Thumshirn } 35298dc3f22cSJohannes Thumshirn 353083c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super); 353183c68bbcSSu Yue 35326d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type); 35336d97c6e3SJohannes Thumshirn if (ret) { 35346d97c6e3SJohannes Thumshirn err = ret; 35358f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35366d97c6e3SJohannes Thumshirn goto fail_alloc; 35376d97c6e3SJohannes Thumshirn } 35386d97c6e3SJohannes Thumshirn 35398dc3f22cSJohannes Thumshirn /* 35401104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 35411104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 35421104a885SDavid Sterba */ 35433d17adeaSQu Wenruo if (btrfs_check_super_csum(fs_info, disk_super)) { 354405135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 35451104a885SDavid Sterba err = -EINVAL; 35468f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 3547141386e1SJosef Bacik goto fail_alloc; 35481104a885SDavid Sterba } 35491104a885SDavid Sterba 35501104a885SDavid Sterba /* 35511104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 35521104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 35531104a885SDavid Sterba * the whole block of INFO_SIZE 35541104a885SDavid Sterba */ 35558f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy)); 35568f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super); 35575f39d397SChris Mason 3558fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy; 3559fbc6feaeSNikolay Borisov 35600b86a832SChris Mason 3561fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super); 3562fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { 3563fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2; 3564fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features); 3565fbc6feaeSNikolay Borisov btrfs_info(fs_info, 3566fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing"); 3567fbc6feaeSNikolay Borisov } 3568fbc6feaeSNikolay Borisov 3569fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy, 3570fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit)); 3571de37aa51SNikolay Borisov 3572069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info); 35731104a885SDavid Sterba if (ret) { 357405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 35751104a885SDavid Sterba err = -EINVAL; 3576141386e1SJosef Bacik goto fail_alloc; 35771104a885SDavid Sterba } 35781104a885SDavid Sterba 35790f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 3580141386e1SJosef Bacik goto fail_alloc; 35810f7d52f4SChris Mason 3582acce952bSliubo /* check FS state, whether FS is broken. */ 358387533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 358487533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 3585acce952bSliubo 358675e7cb7fSLiu Bo /* 358775e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 358875e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 358975e7cb7fSLiu Bo */ 359075e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 359175e7cb7fSLiu Bo 35926f93e834SAnand Jain 35936f93e834SAnand Jain /* Set up fs_info before parsing mount options */ 35946f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super); 35956f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super); 35966f93e834SAnand Jain stripesize = sectorsize; 35976f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 35986f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 35996f93e834SAnand Jain 36006f93e834SAnand Jain fs_info->nodesize = nodesize; 36016f93e834SAnand Jain fs_info->sectorsize = sectorsize; 36026f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize); 36036f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 36046f93e834SAnand Jain fs_info->stripesize = stripesize; 36056f93e834SAnand Jain 36062ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 36072b82032cSYan Zheng if (ret) { 36082b82032cSYan Zheng err = ret; 3609141386e1SJosef Bacik goto fail_alloc; 36102b82032cSYan Zheng } 3611dfe25020SChris Mason 3612d7f67ac9SQu Wenruo ret = btrfs_check_features(fs_info, sb); 3613d7f67ac9SQu Wenruo if (ret < 0) { 3614d7f67ac9SQu Wenruo err = ret; 3615141386e1SJosef Bacik goto fail_alloc; 3616f2b636e8SJosef Bacik } 3617f2b636e8SJosef Bacik 36188481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) { 36198481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info; 36208481dd80SQu Wenruo 36219f73f1aeSQu Wenruo /* 36229f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is 36239f73f1aeSQu Wenruo * going to be deprecated. 36249f73f1aeSQu Wenruo * 36259f73f1aeSQu Wenruo * Force to use v2 cache for subpage case. 36269f73f1aeSQu Wenruo */ 36279f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 36289f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE, 36299f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu", 36309f73f1aeSQu Wenruo sectorsize, PAGE_SIZE); 36319f73f1aeSQu Wenruo 363295ea0486SQu Wenruo btrfs_warn(fs_info, 363395ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental", 36340bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE); 36358481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL); 36368481dd80SQu Wenruo if (!subpage_info) 36378481dd80SQu Wenruo goto fail_alloc; 36388481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize); 36398481dd80SQu Wenruo fs_info->subpage_info = subpage_info; 3640c8050b3bSQu Wenruo } 36410bb3eb3eSQu Wenruo 3642d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info); 36432a458198SEric Sandeen if (ret) { 36442a458198SEric Sandeen err = ret; 36450dc3b84aSJosef Bacik goto fail_sb_buffer; 36460dc3b84aSJosef Bacik } 36474543df7eSChris Mason 36489e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 36499e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 36504575c9ccSChris Mason 3651a061fc8dSChris Mason sb->s_blocksize = sectorsize; 3652a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 3653de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE); 3654db94535dSChris Mason 3655925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 36566bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 3657925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 365884eed90fSChris Mason if (ret) { 365905135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 36605d4f98a2SYan Zheng goto fail_sb_buffer; 366184eed90fSChris Mason } 36620b86a832SChris Mason 366384234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 3664581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 3665bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super), 3666bd676446SJosef Bacik generation, level); 3667bd676446SJosef Bacik if (ret) { 366805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 3669af31f5e5SChris Mason goto fail_tree_roots; 367083121942SDavid Woodhouse } 36710b86a832SChris Mason 3672e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 3673c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid), 3674c4ac7541SDavid Sterba BTRFS_UUID_SIZE); 3675e17cade2SChris Mason 36765b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 36772b82032cSYan Zheng if (ret) { 367805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 3679af31f5e5SChris Mason goto fail_tree_roots; 36802b82032cSYan Zheng } 36810b86a832SChris Mason 36828dabb742SStefan Behrens /* 3683bacce86aSAnand Jain * At this point we know all the devices that make this filesystem, 3684bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace 3685bacce86aSAnand Jain * target is required. So free devices that are not part of this 36861a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked 3687bacce86aSAnand Jain * below in btrfs_init_dev_replace(). 36888dabb742SStefan Behrens */ 3689bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices); 3690d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) { 369105135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 3692a6b0d5c8SChris Mason goto fail_tree_roots; 3693a6b0d5c8SChris Mason } 3694a6b0d5c8SChris Mason 3695b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info); 36964bbcaa64SEric Sandeen if (ret) 3697b8522a1eSNikolay Borisov goto fail_tree_roots; 36988929ecfaSYan, Zheng 369975ec1db8SJosef Bacik /* 370073651042SNaohiro Aota * Get zone type information of zoned block devices. This will also 370173651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the 370273651042SNaohiro Aota * zoned incompat feature flag set. 370373651042SNaohiro Aota */ 370473651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info); 370573651042SNaohiro Aota if (ret) { 370673651042SNaohiro Aota btrfs_err(fs_info, 370773651042SNaohiro Aota "zoned: failed to read device zone info: %d", 370873651042SNaohiro Aota ret); 370973651042SNaohiro Aota goto fail_block_groups; 371073651042SNaohiro Aota } 371173651042SNaohiro Aota 371273651042SNaohiro Aota /* 371375ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to 371475ec1db8SJosef Bacik * check the generation here so we can set the 371575ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the 371675ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the 371775ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree, 371875ec1db8SJosef Bacik * even though it was perfectly fine. 371975ec1db8SJosef Bacik */ 372075ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) && 372175ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super)) 372275ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 372375ec1db8SJosef Bacik 3724cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info); 3725cf90d884SQu Wenruo if (ret) { 3726cf90d884SQu Wenruo btrfs_err(fs_info, 3727cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d", 3728cf90d884SQu Wenruo ret); 3729cf90d884SQu Wenruo goto fail_block_groups; 3730cf90d884SQu Wenruo } 373168310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 373268310a5eSIlya Dryomov if (ret) { 373305135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 373468310a5eSIlya Dryomov goto fail_block_groups; 373568310a5eSIlya Dryomov } 373668310a5eSIlya Dryomov 3737733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3738733f4fbbSStefan Behrens if (ret) { 373905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3740733f4fbbSStefan Behrens goto fail_block_groups; 3741733f4fbbSStefan Behrens } 3742733f4fbbSStefan Behrens 37438dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 37448dabb742SStefan Behrens if (ret) { 374505135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 37468dabb742SStefan Behrens goto fail_block_groups; 37478dabb742SStefan Behrens } 37488dabb742SStefan Behrens 3749b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info); 3750b70f5097SNaohiro Aota if (ret) { 3751b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d", 3752b70f5097SNaohiro Aota ret); 3753b70f5097SNaohiro Aota goto fail_block_groups; 3754b70f5097SNaohiro Aota } 3755b70f5097SNaohiro Aota 3756c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices); 3757b7c35e81SAnand Jain if (ret) { 375805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 375905135f59SDavid Sterba ret); 3760b7c35e81SAnand Jain goto fail_block_groups; 3761b7c35e81SAnand Jain } 3762b7c35e81SAnand Jain 376396f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 37645ac1d209SJeff Mahoney if (ret) { 376505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3766b7c35e81SAnand Jain goto fail_fsdev_sysfs; 37675ac1d209SJeff Mahoney } 37685ac1d209SJeff Mahoney 3769c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3770c59021f8Sliubo if (ret) { 377105135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 37722365dd3cSAnand Jain goto fail_sysfs; 3773c59021f8Sliubo } 3774c59021f8Sliubo 37755b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 37761b1d1f66SJosef Bacik if (ret) { 377705135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 37782365dd3cSAnand Jain goto fail_sysfs; 37791b1d1f66SJosef Bacik } 37804330e183SQu Wenruo 378116beac87SNaohiro Aota btrfs_free_zone_cache(fs_info); 378216beac87SNaohiro Aota 37835c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices && 37845c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) { 378505135f59SDavid Sterba btrfs_warn(fs_info, 378652042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices"); 37872365dd3cSAnand Jain goto fail_sysfs; 3788292fd7fcSStefan Behrens } 37899078a3e1SChris Mason 379033c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info, 3791a74a4b97SChris Mason "btrfs-cleaner"); 379257506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 37932365dd3cSAnand Jain goto fail_sysfs; 3794a74a4b97SChris Mason 3795a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3796a74a4b97SChris Mason tree_root, 3797a74a4b97SChris Mason "btrfs-transaction"); 379857506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 37993f157a2fSChris Mason goto fail_cleaner; 3800a74a4b97SChris Mason 3801583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 3802c289811cSChris Mason !fs_info->fs_devices->rotating) { 3803583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 3804c289811cSChris Mason } 3805c289811cSChris Mason 3806572d9ab7SDavid Sterba /* 380763a7cb13SDavid Sterba * For devices supporting discard turn on discard=async automatically, 380863a7cb13SDavid Sterba * unless it's already set or disabled. This could be turned off by 380963a7cb13SDavid Sterba * nodiscard for the same mount. 381063a7cb13SDavid Sterba */ 381163a7cb13SDavid Sterba if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) || 381263a7cb13SDavid Sterba btrfs_test_opt(fs_info, DISCARD_ASYNC) || 381363a7cb13SDavid Sterba btrfs_test_opt(fs_info, NODISCARD)) && 381463a7cb13SDavid Sterba fs_info->fs_devices->discardable) { 381563a7cb13SDavid Sterba btrfs_set_and_info(fs_info, DISCARD_ASYNC, 381663a7cb13SDavid Sterba "auto enabling async discard"); 381763a7cb13SDavid Sterba btrfs_clear_opt(fs_info->mount_opt, NODISCARD); 381863a7cb13SDavid Sterba } 381963a7cb13SDavid Sterba 382021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38210b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 38222ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 38230b246afaSJeff Mahoney btrfs_test_opt(fs_info, 3824cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0, 382521adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 382621adbd5cSStefan Behrens if (ret) 382705135f59SDavid Sterba btrfs_warn(fs_info, 382805135f59SDavid Sterba "failed to initialize integrity check module: %d", 382905135f59SDavid Sterba ret); 383021adbd5cSStefan Behrens } 383121adbd5cSStefan Behrens #endif 3832bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3833bcef60f2SArne Jansen if (ret) 3834bcef60f2SArne Jansen goto fail_trans_kthread; 383521adbd5cSStefan Behrens 3836fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 3837fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 3838fd708b81SJosef Bacik 383996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 384096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 38410b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 3842e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay"); 384363443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 384479787eaaSJeff Mahoney if (ret) { 384563443bf5SEric Sandeen err = ret; 384628c16cbbSWang Shilong goto fail_qgroup; 3847e556ce2cSYan Zheng } 3848e02119d5SChris Mason } 38491a40e23bSZheng Yan 385056e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true); 38513140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 38523140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3853f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3854315bf8efSJosef Bacik fs_info->fs_root = NULL; 3855bcef60f2SArne Jansen goto fail_qgroup; 38563140c9a3SDan Carpenter } 3857c289811cSChris Mason 3858bc98a42cSDavid Howells if (sb_rdonly(sb)) 38598cd29088SBoris Burkov goto clear_oneshot; 38602b6ba629SIlya Dryomov 386144c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 38622b6ba629SIlya Dryomov if (ret) { 38636bccf3abSJeff Mahoney close_ctree(fs_info); 38642b6ba629SIlya Dryomov return ret; 3865e3acc2a6SJosef Bacik } 3866b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 3867b382a324SJan Schmidt 386844c0ca21SBoris Burkov if (fs_info->uuid_root && 386944c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 387044c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) { 387105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 387270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 387370f80175SStefan Behrens if (ret) { 387405135f59SDavid Sterba btrfs_warn(fs_info, 387505135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 38766bccf3abSJeff Mahoney close_ctree(fs_info); 387770f80175SStefan Behrens return ret; 387870f80175SStefan Behrens } 3879f7a81ea4SStefan Behrens } 388094846229SBoris Burkov 3881afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 388247ab2a6cSJosef Bacik 3883b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */ 3884b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags)) 3885b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread); 3886b4be6aefSJosef Bacik 38878cd29088SBoris Burkov clear_oneshot: 38888cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 3889ad2b2c80SAl Viro return 0; 389039279cc3SChris Mason 3891bcef60f2SArne Jansen fail_qgroup: 3892bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 38937c2ca468SChris Mason fail_trans_kthread: 38947c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 38952ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3896faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 38973f157a2fSChris Mason fail_cleaner: 3898a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 38997c2ca468SChris Mason 39007c2ca468SChris Mason /* 39017c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 39027c2ca468SChris Mason * kthreads 39037c2ca468SChris Mason */ 39047c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 39057c2ca468SChris Mason 39062365dd3cSAnand Jain fail_sysfs: 39076618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 39082365dd3cSAnand Jain 3909b7c35e81SAnand Jain fail_fsdev_sysfs: 3910b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3911b7c35e81SAnand Jain 39121b1d1f66SJosef Bacik fail_block_groups: 391354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3914af31f5e5SChris Mason 3915af31f5e5SChris Mason fail_tree_roots: 39169e3aa805SJosef Bacik if (fs_info->data_reloc_root) 39179e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root); 39184273eaffSAnand Jain free_root_pointers(fs_info, true); 39192b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3920af31f5e5SChris Mason 392139279cc3SChris Mason fail_sb_buffer: 39227abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 39235cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 392416cdcec7SMiao Xie fail_alloc: 3925586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3926586e46e2SIlya Dryomov 39274543df7eSChris Mason iput(fs_info->btree_inode); 39287e662854SQinghuang Feng fail: 3929586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3930ad2b2c80SAl Viro return err; 3931eb60ceacSChris Mason } 3932663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3933eb60ceacSChris Mason 3934314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio) 3935f2984462SChris Mason { 3936314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private; 3937314b6dd0SJohannes Thumshirn struct bio_vec *bvec; 3938314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all; 3939314b6dd0SJohannes Thumshirn struct page *page; 3940442a4f63SStefan Behrens 3941314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) { 3942314b6dd0SJohannes Thumshirn page = bvec->bv_page; 3943314b6dd0SJohannes Thumshirn 3944314b6dd0SJohannes Thumshirn if (bio->bi_status) { 3945fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3946314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)", 3947314b6dd0SJohannes Thumshirn rcu_str_deref(device->name), 3948314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status)); 3949314b6dd0SJohannes Thumshirn ClearPageUptodate(page); 3950314b6dd0SJohannes Thumshirn SetPageError(page); 3951314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device, 3952314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS); 3953314b6dd0SJohannes Thumshirn } else { 3954314b6dd0SJohannes Thumshirn SetPageUptodate(page); 3955f2984462SChris Mason } 3956314b6dd0SJohannes Thumshirn 3957314b6dd0SJohannes Thumshirn put_page(page); 3958314b6dd0SJohannes Thumshirn unlock_page(page); 3959314b6dd0SJohannes Thumshirn } 3960314b6dd0SJohannes Thumshirn 3961314b6dd0SJohannes Thumshirn bio_put(bio); 3962f2984462SChris Mason } 3963f2984462SChris Mason 39648f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 3965a05d3c91SQu Wenruo int copy_num, bool drop_cache) 396629c36d72SAnand Jain { 396729c36d72SAnand Jain struct btrfs_super_block *super; 39688f32380dSJohannes Thumshirn struct page *page; 396912659251SNaohiro Aota u64 bytenr, bytenr_orig; 39708f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping; 397112659251SNaohiro Aota int ret; 397229c36d72SAnand Jain 397312659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num); 397412659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr); 397512659251SNaohiro Aota if (ret == -ENOENT) 397612659251SNaohiro Aota return ERR_PTR(-EINVAL); 397712659251SNaohiro Aota else if (ret) 397812659251SNaohiro Aota return ERR_PTR(ret); 397912659251SNaohiro Aota 3980cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev)) 39818f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 398229c36d72SAnand Jain 3983a05d3c91SQu Wenruo if (drop_cache) { 3984a05d3c91SQu Wenruo /* This should only be called with the primary sb. */ 3985a05d3c91SQu Wenruo ASSERT(copy_num == 0); 3986a05d3c91SQu Wenruo 3987a05d3c91SQu Wenruo /* 3988a05d3c91SQu Wenruo * Drop the page of the primary superblock, so later read will 3989a05d3c91SQu Wenruo * always read from the device. 3990a05d3c91SQu Wenruo */ 3991a05d3c91SQu Wenruo invalidate_inode_pages2_range(mapping, 3992a05d3c91SQu Wenruo bytenr >> PAGE_SHIFT, 3993a05d3c91SQu Wenruo (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT); 3994a05d3c91SQu Wenruo } 3995a05d3c91SQu Wenruo 39968f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS); 39978f32380dSJohannes Thumshirn if (IS_ERR(page)) 39988f32380dSJohannes Thumshirn return ERR_CAST(page); 399929c36d72SAnand Jain 40008f32380dSJohannes Thumshirn super = page_address(page); 400196c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) { 400296c2e067SAnand Jain btrfs_release_disk_super(super); 400396c2e067SAnand Jain return ERR_PTR(-ENODATA); 400496c2e067SAnand Jain } 400596c2e067SAnand Jain 400612659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) { 40078f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40088f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL); 400929c36d72SAnand Jain } 401029c36d72SAnand Jain 40118f32380dSJohannes Thumshirn return super; 401229c36d72SAnand Jain } 401329c36d72SAnand Jain 401429c36d72SAnand Jain 40158f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) 4016a512bbf8SYan Zheng { 40178f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL; 4018a512bbf8SYan Zheng int i; 4019a512bbf8SYan Zheng u64 transid = 0; 4020a512bbf8SYan Zheng 4021a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 4022a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 4023a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 4024a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 4025a512bbf8SYan Zheng */ 4026a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 4027a05d3c91SQu Wenruo super = btrfs_read_dev_one_super(bdev, i, false); 40288f32380dSJohannes Thumshirn if (IS_ERR(super)) 4029a512bbf8SYan Zheng continue; 4030a512bbf8SYan Zheng 4031a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 40328f32380dSJohannes Thumshirn if (latest) 40338f32380dSJohannes Thumshirn btrfs_release_disk_super(super); 40348f32380dSJohannes Thumshirn 40358f32380dSJohannes Thumshirn latest = super; 4036a512bbf8SYan Zheng transid = btrfs_super_generation(super); 4037a512bbf8SYan Zheng } 4038a512bbf8SYan Zheng } 403992fc03fbSAnand Jain 40408f32380dSJohannes Thumshirn return super; 4041a512bbf8SYan Zheng } 4042a512bbf8SYan Zheng 40434eedeb75SHisashi Hifumi /* 4044abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 4045314b6dd0SJohannes Thumshirn * pages we use for writing are locked. 40464eedeb75SHisashi Hifumi * 4047abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 4048abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 4049abbb3b8eSDavid Sterba * same for write and wait phases. 40504eedeb75SHisashi Hifumi * 4051314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails. 40524eedeb75SHisashi Hifumi */ 4053a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 4054abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 4055a512bbf8SYan Zheng { 4056d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info; 4057314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping; 4058d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 4059a512bbf8SYan Zheng int i; 4060a512bbf8SYan Zheng int errors = 0; 406112659251SNaohiro Aota int ret; 406212659251SNaohiro Aota u64 bytenr, bytenr_orig; 4063a512bbf8SYan Zheng 4064a512bbf8SYan Zheng if (max_mirrors == 0) 4065a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4066a512bbf8SYan Zheng 4067d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 4068d5178578SJohannes Thumshirn 4069a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 4070314b6dd0SJohannes Thumshirn struct page *page; 4071314b6dd0SJohannes Thumshirn struct bio *bio; 4072314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super; 4073314b6dd0SJohannes Thumshirn 407412659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i); 407512659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr); 407612659251SNaohiro Aota if (ret == -ENOENT) { 407712659251SNaohiro Aota continue; 407812659251SNaohiro Aota } else if (ret < 0) { 407912659251SNaohiro Aota btrfs_err(device->fs_info, 408012659251SNaohiro Aota "couldn't get super block location for mirror %d", 408112659251SNaohiro Aota i); 408212659251SNaohiro Aota errors++; 408312659251SNaohiro Aota continue; 408412659251SNaohiro Aota } 4085935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4086935e5cc9SMiao Xie device->commit_total_bytes) 4087a512bbf8SYan Zheng break; 4088a512bbf8SYan Zheng 408912659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig); 4090a512bbf8SYan Zheng 4091fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE, 4092fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, 4093fd08001fSEric Biggers sb->csum); 4094a512bbf8SYan Zheng 4095314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, 4096314b6dd0SJohannes Thumshirn GFP_NOFS); 4097314b6dd0SJohannes Thumshirn if (!page) { 4098fb456252SJeff Mahoney btrfs_err(device->fs_info, 4099314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu", 4100f14d104dSDavid Sterba bytenr); 4101634554dcSJosef Bacik errors++; 4102634554dcSJosef Bacik continue; 4103634554dcSJosef Bacik } 4104634554dcSJosef Bacik 4105314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */ 4106314b6dd0SJohannes Thumshirn get_page(page); 4107a512bbf8SYan Zheng 4108314b6dd0SJohannes Thumshirn disk_super = page_address(page); 4109314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); 4110a512bbf8SYan Zheng 4111387125fcSChris Mason /* 4112314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache 4113314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity 4114314b6dd0SJohannes Thumshirn * checking. 4115387125fcSChris Mason */ 411607888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1, 411707888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO, 411807888c66SChristoph Hellwig GFP_NOFS); 4119314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; 4120314b6dd0SJohannes Thumshirn bio->bi_private = device; 4121314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write; 4122314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, 4123314b6dd0SJohannes Thumshirn offset_in_page(bytenr)); 4124314b6dd0SJohannes Thumshirn 4125314b6dd0SJohannes Thumshirn /* 4126314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to 4127314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk 4128314b6dd0SJohannes Thumshirn * copies might still contain the older version. 4129314b6dd0SJohannes Thumshirn */ 41301b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 4131314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 4132314b6dd0SJohannes Thumshirn 413358ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 413458ff51f1SChristoph Hellwig submit_bio(bio); 41358376d9e1SNaohiro Aota 41368376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i)) 41378376d9e1SNaohiro Aota errors++; 4138a512bbf8SYan Zheng } 4139a512bbf8SYan Zheng return errors < i ? 0 : -1; 4140a512bbf8SYan Zheng } 4141a512bbf8SYan Zheng 4142387125fcSChris Mason /* 4143abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 4144abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 4145abbb3b8eSDavid Sterba * 4146314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to 4147abbb3b8eSDavid Sterba * date. 4148abbb3b8eSDavid Sterba */ 4149abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 4150abbb3b8eSDavid Sterba { 4151abbb3b8eSDavid Sterba int i; 4152abbb3b8eSDavid Sterba int errors = 0; 4153b6a535faSHoward McLauchlan bool primary_failed = false; 415412659251SNaohiro Aota int ret; 4155abbb3b8eSDavid Sterba u64 bytenr; 4156abbb3b8eSDavid Sterba 4157abbb3b8eSDavid Sterba if (max_mirrors == 0) 4158abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 4159abbb3b8eSDavid Sterba 4160abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 4161314b6dd0SJohannes Thumshirn struct page *page; 4162314b6dd0SJohannes Thumshirn 416312659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr); 416412659251SNaohiro Aota if (ret == -ENOENT) { 416512659251SNaohiro Aota break; 416612659251SNaohiro Aota } else if (ret < 0) { 416712659251SNaohiro Aota errors++; 416812659251SNaohiro Aota if (i == 0) 416912659251SNaohiro Aota primary_failed = true; 417012659251SNaohiro Aota continue; 417112659251SNaohiro Aota } 4172abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 4173abbb3b8eSDavid Sterba device->commit_total_bytes) 4174abbb3b8eSDavid Sterba break; 4175abbb3b8eSDavid Sterba 4176314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping, 4177314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT); 4178314b6dd0SJohannes Thumshirn if (!page) { 4179abbb3b8eSDavid Sterba errors++; 4180b6a535faSHoward McLauchlan if (i == 0) 4181b6a535faSHoward McLauchlan primary_failed = true; 4182abbb3b8eSDavid Sterba continue; 4183abbb3b8eSDavid Sterba } 4184314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */ 4185314b6dd0SJohannes Thumshirn wait_on_page_locked(page); 4186314b6dd0SJohannes Thumshirn if (PageError(page)) { 4187abbb3b8eSDavid Sterba errors++; 4188b6a535faSHoward McLauchlan if (i == 0) 4189b6a535faSHoward McLauchlan primary_failed = true; 4190b6a535faSHoward McLauchlan } 4191abbb3b8eSDavid Sterba 4192314b6dd0SJohannes Thumshirn /* Drop our reference */ 4193314b6dd0SJohannes Thumshirn put_page(page); 4194abbb3b8eSDavid Sterba 4195314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */ 4196314b6dd0SJohannes Thumshirn put_page(page); 4197abbb3b8eSDavid Sterba } 4198abbb3b8eSDavid Sterba 4199b6a535faSHoward McLauchlan /* log error, force error return */ 4200b6a535faSHoward McLauchlan if (primary_failed) { 4201b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 4202b6a535faSHoward McLauchlan device->devid); 4203b6a535faSHoward McLauchlan return -1; 4204b6a535faSHoward McLauchlan } 4205b6a535faSHoward McLauchlan 4206abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 4207abbb3b8eSDavid Sterba } 4208abbb3b8eSDavid Sterba 4209abbb3b8eSDavid Sterba /* 4210387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 4211387125fcSChris Mason * for the barrier when it is done 4212387125fcSChris Mason */ 42134246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 4214387125fcSChris Mason { 4215f9e69aa9SChristoph Hellwig bio_uninit(bio); 4216387125fcSChris Mason complete(bio->bi_private); 4217387125fcSChris Mason } 4218387125fcSChris Mason 4219387125fcSChris Mason /* 42204fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 42214fc6441aSAnand Jain * done in the waiting counterpart. 4222387125fcSChris Mason */ 42234fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 4224387125fcSChris Mason { 4225f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 4226387125fcSChris Mason 4227a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4228a91cf0ffSWang Yugui /* 4229a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio 4230a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since 4231a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this 4232a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a 4233a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio 4234a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake 4235a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in 4236a91cf0ffSWang Yugui * non-debug builds. 4237a91cf0ffSWang Yugui */ 423808e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev)) 42394fc6441aSAnand Jain return; 4240a91cf0ffSWang Yugui #endif 4241387125fcSChris Mason 4242f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0, 4243f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH); 4244387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 4245387125fcSChris Mason init_completion(&device->flush_wait); 4246387125fcSChris Mason bio->bi_private = &device->flush_wait; 4247387125fcSChris Mason 424858ff51f1SChristoph Hellwig btrfsic_check_bio(bio); 424958ff51f1SChristoph Hellwig submit_bio(bio); 42501c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42514fc6441aSAnand Jain } 4252387125fcSChris Mason 42534fc6441aSAnand Jain /* 42544fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 42554fc6441aSAnand Jain */ 42568c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 42574fc6441aSAnand Jain { 4258f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio; 42594fc6441aSAnand Jain 42601c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 426158efbc9fSOmar Sandoval return BLK_STS_OK; 42624fc6441aSAnand Jain 42631c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 42642980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 42654fc6441aSAnand Jain 42668c27cb35SLinus Torvalds return bio->bi_status; 4267387125fcSChris Mason } 4268387125fcSChris Mason 4269d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 4270401b41e5SAnand Jain { 42716528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 4272401b41e5SAnand Jain return -EIO; 4273387125fcSChris Mason return 0; 4274387125fcSChris Mason } 4275387125fcSChris Mason 4276387125fcSChris Mason /* 4277387125fcSChris Mason * send an empty flush down to each device in parallel, 4278387125fcSChris Mason * then wait for them 4279387125fcSChris Mason */ 4280387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 4281387125fcSChris Mason { 4282387125fcSChris Mason struct list_head *head; 4283387125fcSChris Mason struct btrfs_device *dev; 42845af3e8ccSStefan Behrens int errors_wait = 0; 42854e4cbee9SChristoph Hellwig blk_status_t ret; 4286387125fcSChris Mason 42871538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 4288387125fcSChris Mason /* send down all the barriers */ 4289387125fcSChris Mason head = &info->fs_devices->devices; 42901538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4291e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4292f88ba6a2SHidetoshi Seto continue; 4293cea7c8bfSAnand Jain if (!dev->bdev) 4294387125fcSChris Mason continue; 4295e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4296ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4297387125fcSChris Mason continue; 4298387125fcSChris Mason 42994fc6441aSAnand Jain write_dev_flush(dev); 430058efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 4301387125fcSChris Mason } 4302387125fcSChris Mason 4303387125fcSChris Mason /* wait for all the barriers */ 43041538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4305e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 4306f88ba6a2SHidetoshi Seto continue; 4307387125fcSChris Mason if (!dev->bdev) { 43085af3e8ccSStefan Behrens errors_wait++; 4309387125fcSChris Mason continue; 4310387125fcSChris Mason } 4311e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4312ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4313387125fcSChris Mason continue; 4314387125fcSChris Mason 43154fc6441aSAnand Jain ret = wait_dev_flush(dev); 4316401b41e5SAnand Jain if (ret) { 4317401b41e5SAnand Jain dev->last_flush_error = ret; 431866b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 431966b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 43205af3e8ccSStefan Behrens errors_wait++; 4321387125fcSChris Mason } 4322401b41e5SAnand Jain } 4323401b41e5SAnand Jain 4324cea7c8bfSAnand Jain if (errors_wait) { 4325401b41e5SAnand Jain /* 4326401b41e5SAnand Jain * At some point we need the status of all disks 4327401b41e5SAnand Jain * to arrive at the volume status. So error checking 4328401b41e5SAnand Jain * is being pushed to a separate loop. 4329401b41e5SAnand Jain */ 4330d10b82feSQu Wenruo return check_barrier_error(info); 4331401b41e5SAnand Jain } 4332387125fcSChris Mason return 0; 4333387125fcSChris Mason } 4334387125fcSChris Mason 4335943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 4336943c6e99SZhao Lei { 43378789f4feSZhao Lei int raid_type; 43388789f4feSZhao Lei int min_tolerated = INT_MAX; 4339943c6e99SZhao Lei 43408789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 43418789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 43428c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43438789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 43448789f4feSZhao Lei tolerated_failures); 4345943c6e99SZhao Lei 43468789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 43478789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 43488789f4feSZhao Lei continue; 434941a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag)) 43508789f4feSZhao Lei continue; 43518c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated, 43528789f4feSZhao Lei btrfs_raid_array[raid_type]. 43538789f4feSZhao Lei tolerated_failures); 43548789f4feSZhao Lei } 4355943c6e99SZhao Lei 43568789f4feSZhao Lei if (min_tolerated == INT_MAX) { 4357ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 43588789f4feSZhao Lei min_tolerated = 0; 43598789f4feSZhao Lei } 43608789f4feSZhao Lei 43618789f4feSZhao Lei return min_tolerated; 4362943c6e99SZhao Lei } 4363943c6e99SZhao Lei 4364eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 4365f2984462SChris Mason { 4366e5e9a520SChris Mason struct list_head *head; 4367f2984462SChris Mason struct btrfs_device *dev; 4368a061fc8dSChris Mason struct btrfs_super_block *sb; 4369f2984462SChris Mason struct btrfs_dev_item *dev_item; 4370f2984462SChris Mason int ret; 4371f2984462SChris Mason int do_barriers; 4372a236aed1SChris Mason int max_errors; 4373a236aed1SChris Mason int total_errors = 0; 4374a061fc8dSChris Mason u64 flags; 4375f2984462SChris Mason 43760b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 4377fed3b381SLiu Bo 4378fed3b381SLiu Bo /* 4379fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 4380fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 4381fed3b381SLiu Bo * been consistent on disk. 4382fed3b381SLiu Bo */ 4383fed3b381SLiu Bo if (max_mirrors == 0) 43840b246afaSJeff Mahoney backup_super_roots(fs_info); 4385f2984462SChris Mason 43860b246afaSJeff Mahoney sb = fs_info->super_for_commit; 4387a061fc8dSChris Mason dev_item = &sb->dev_item; 4388e5e9a520SChris Mason 43890b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 43900b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 43910b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 4392387125fcSChris Mason 43935af3e8ccSStefan Behrens if (do_barriers) { 43940b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 43955af3e8ccSStefan Behrens if (ret) { 43965af3e8ccSStefan Behrens mutex_unlock( 43970b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 43980b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 43995af3e8ccSStefan Behrens "errors while submitting device barriers."); 44005af3e8ccSStefan Behrens return ret; 44015af3e8ccSStefan Behrens } 44025af3e8ccSStefan Behrens } 4403387125fcSChris Mason 44041538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4405dfe25020SChris Mason if (!dev->bdev) { 4406dfe25020SChris Mason total_errors++; 4407dfe25020SChris Mason continue; 4408dfe25020SChris Mason } 4409e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4410ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4411dfe25020SChris Mason continue; 4412dfe25020SChris Mason 44132b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 4414a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 4415a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 44167df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 4417935e5cc9SMiao Xie dev->commit_total_bytes); 4418ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 4419ce7213c7SMiao Xie dev->commit_bytes_used); 4420a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 4421a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 4422a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 4423a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 44247239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid, 44257239ff4bSNikolay Borisov BTRFS_FSID_SIZE); 4426a512bbf8SYan Zheng 4427a061fc8dSChris Mason flags = btrfs_super_flags(sb); 4428a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 4429f2984462SChris Mason 443075cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb); 443175cb857dSQu Wenruo if (ret < 0) { 443275cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex); 443375cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN, 443475cb857dSQu Wenruo "unexpected superblock corruption detected"); 443575cb857dSQu Wenruo return -EUCLEAN; 443675cb857dSQu Wenruo } 443775cb857dSQu Wenruo 4438abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 4439a236aed1SChris Mason if (ret) 4440a236aed1SChris Mason total_errors++; 4441f2984462SChris Mason } 4442a236aed1SChris Mason if (total_errors > max_errors) { 44430b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 4444d397712bSChris Mason total_errors); 44450b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 444679787eaaSJeff Mahoney 44479d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 44480b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44490b246afaSJeff Mahoney "%d errors while writing supers", 44500b246afaSJeff Mahoney total_errors); 44519d565ba4SStefan Behrens return -EIO; 4452a236aed1SChris Mason } 4453f2984462SChris Mason 4454a512bbf8SYan Zheng total_errors = 0; 44551538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 4456dfe25020SChris Mason if (!dev->bdev) 4457dfe25020SChris Mason continue; 4458e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 4459ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 4460dfe25020SChris Mason continue; 4461dfe25020SChris Mason 4462abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 4463a512bbf8SYan Zheng if (ret) 44641259ab75SChris Mason total_errors++; 4465f2984462SChris Mason } 44660b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 4467a236aed1SChris Mason if (total_errors > max_errors) { 44680b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 44690b246afaSJeff Mahoney "%d errors while writing supers", 44700b246afaSJeff Mahoney total_errors); 447179787eaaSJeff Mahoney return -EIO; 4472a236aed1SChris Mason } 4473f2984462SChris Mason return 0; 4474f2984462SChris Mason } 4475f2984462SChris Mason 4476cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 4477cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 4478cb517eabSMiao Xie struct btrfs_root *root) 44792619ba1fSChris Mason { 44804785e24fSJosef Bacik bool drop_ref = false; 44814785e24fSJosef Bacik 4482fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4483fc7cbcd4SDavid Sterba radix_tree_delete(&fs_info->fs_roots_radix, 4484fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid); 4485fc7cbcd4SDavid Sterba if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state)) 44864785e24fSJosef Bacik drop_ref = true; 4487fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 448876dda93cSYan, Zheng 448984961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) { 4490ef67963dSJosef Bacik ASSERT(root->log_root == NULL); 44911c1ea4f7SLiu Bo if (root->reloc_root) { 449200246528SJosef Bacik btrfs_put_root(root->reloc_root); 44931c1ea4f7SLiu Bo root->reloc_root = NULL; 44941c1ea4f7SLiu Bo } 44951c1ea4f7SLiu Bo } 44963321719eSLiu Bo 44974785e24fSJosef Bacik if (drop_ref) 449800246528SJosef Bacik btrfs_put_root(root); 44992619ba1fSChris Mason } 45002619ba1fSChris Mason 4501c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 4502c146afadSYan Zheng { 4503fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4504fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4505fc7cbcd4SDavid Sterba int i = 0; 450665d33fd7SQu Wenruo int err = 0; 4507fc7cbcd4SDavid Sterba unsigned int ret = 0; 4508c146afadSYan Zheng 4509c146afadSYan Zheng while (1) { 4510fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4511fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4512fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4513fc7cbcd4SDavid Sterba ARRAY_SIZE(gang)); 4514fc7cbcd4SDavid Sterba if (!ret) { 4515fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4516c146afadSYan Zheng break; 451765d33fd7SQu Wenruo } 4518fc7cbcd4SDavid Sterba root_objectid = gang[ret - 1]->root_key.objectid + 1; 451966b4ffd1SJosef Bacik 4520fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4521fc7cbcd4SDavid Sterba /* Avoid to grab roots in dead_roots */ 4522fc7cbcd4SDavid Sterba if (btrfs_root_refs(&gang[i]->root_item) == 0) { 4523fc7cbcd4SDavid Sterba gang[i] = NULL; 452465d33fd7SQu Wenruo continue; 4525c146afadSYan Zheng } 4526fc7cbcd4SDavid Sterba /* grab all the search result for later use */ 4527fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4528fc7cbcd4SDavid Sterba } 4529fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4530fc7cbcd4SDavid Sterba 4531fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4532fc7cbcd4SDavid Sterba if (!gang[i]) 4533fc7cbcd4SDavid Sterba continue; 4534fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4535fc7cbcd4SDavid Sterba err = btrfs_orphan_cleanup(gang[i]); 4536fc7cbcd4SDavid Sterba if (err) 4537fc7cbcd4SDavid Sterba break; 4538fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4539fc7cbcd4SDavid Sterba } 4540fc7cbcd4SDavid Sterba root_objectid++; 4541c146afadSYan Zheng } 454265d33fd7SQu Wenruo 4543fc7cbcd4SDavid Sterba /* release the uncleaned roots due to error */ 4544fc7cbcd4SDavid Sterba for (; i < ret; i++) { 4545fc7cbcd4SDavid Sterba if (gang[i]) 4546fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 454765d33fd7SQu Wenruo } 454865d33fd7SQu Wenruo return err; 4549c146afadSYan Zheng } 4550c146afadSYan Zheng 45516bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 4552c146afadSYan Zheng { 45536bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 4554c146afadSYan Zheng struct btrfs_trans_handle *trans; 4555c146afadSYan Zheng 45560b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 45572ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 45580b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 45590b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 4560c71bf099SYan, Zheng 4561c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 45620b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 45630b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4564c71bf099SYan, Zheng 45657a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 45663612b495STsutomu Itoh if (IS_ERR(trans)) 45673612b495STsutomu Itoh return PTR_ERR(trans); 45683a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 4569c146afadSYan Zheng } 4570c146afadSYan Zheng 457136c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info) 457236c86a9eSQu Wenruo { 457336c86a9eSQu Wenruo struct btrfs_transaction *trans; 457436c86a9eSQu Wenruo struct btrfs_transaction *tmp; 457536c86a9eSQu Wenruo bool found = false; 457636c86a9eSQu Wenruo 457736c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list)) 457836c86a9eSQu Wenruo return; 457936c86a9eSQu Wenruo 458036c86a9eSQu Wenruo /* 458136c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(), 458236c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock. 458336c86a9eSQu Wenruo */ 458436c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags)); 458536c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) { 458636c86a9eSQu Wenruo struct extent_state *cached = NULL; 458736c86a9eSQu Wenruo u64 dirty_bytes = 0; 458836c86a9eSQu Wenruo u64 cur = 0; 458936c86a9eSQu Wenruo u64 found_start; 459036c86a9eSQu Wenruo u64 found_end; 459136c86a9eSQu Wenruo 459236c86a9eSQu Wenruo found = true; 459336c86a9eSQu Wenruo while (!find_first_extent_bit(&trans->dirty_pages, cur, 459436c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) { 459536c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start; 459636c86a9eSQu Wenruo cur = found_end + 1; 459736c86a9eSQu Wenruo } 459836c86a9eSQu Wenruo btrfs_warn(fs_info, 459936c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed", 460036c86a9eSQu Wenruo trans->transid, dirty_bytes); 460136c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info); 460236c86a9eSQu Wenruo 460336c86a9eSQu Wenruo if (trans == fs_info->running_transaction) 460436c86a9eSQu Wenruo fs_info->running_transaction = NULL; 460536c86a9eSQu Wenruo list_del_init(&trans->list); 460636c86a9eSQu Wenruo 460736c86a9eSQu Wenruo btrfs_put_transaction(trans); 460836c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info); 460936c86a9eSQu Wenruo } 461036c86a9eSQu Wenruo ASSERT(!found); 461136c86a9eSQu Wenruo } 461236c86a9eSQu Wenruo 4613b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info) 4614eb60ceacSChris Mason { 4615c146afadSYan Zheng int ret; 4616e089f05cSChris Mason 4617afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 461831e70e52SFilipe Manana 461931e70e52SFilipe Manana /* 46208a1f1e3dSFilipe Manana * If we had UNFINISHED_DROPS we could still be processing them, so 46218a1f1e3dSFilipe Manana * clear that bit and wake up relocation so it can stop. 46228a1f1e3dSFilipe Manana * We must do this before stopping the block group reclaim task, because 46238a1f1e3dSFilipe Manana * at btrfs_relocate_block_group() we wait for this bit, and after the 46248a1f1e3dSFilipe Manana * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we 46258a1f1e3dSFilipe Manana * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will 46268a1f1e3dSFilipe Manana * return 1. 46278a1f1e3dSFilipe Manana */ 46288a1f1e3dSFilipe Manana btrfs_wake_unfinished_drop(fs_info); 46298a1f1e3dSFilipe Manana 46308a1f1e3dSFilipe Manana /* 463131e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group, 463231e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park 463331e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after 463431e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang 463531e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is 463631e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when 463731e70e52SFilipe Manana * trying to stop the async reclaim task. 463831e70e52SFilipe Manana */ 463931e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work); 4640d6fd0ae2SOmar Sandoval /* 4641d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed 4642d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet 4643d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might 4644d6fd0ae2SOmar Sandoval * still try to wake up the cleaner. 4645d6fd0ae2SOmar Sandoval */ 4646d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread); 4647a2135011SChris Mason 46487343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 4649d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 46507343dd61SJustin Maggard 4651803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 4652803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 4653803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 4654803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 4655803b2f54SStefan Behrens 4656837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 4657aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 4658837d5b6eSIlya Dryomov 46598dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 46608dabb742SStefan Behrens 4661aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 46624cb5300bSChris Mason 46634cb5300bSChris Mason /* wait for any defraggers to finish */ 46644cb5300bSChris Mason wait_event(fs_info->transaction_wait, 46654cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 46664cb5300bSChris Mason 46674cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 466826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 46694cb5300bSChris Mason 4670a362bb86SFilipe Manana /* 4671a362bb86SFilipe Manana * After we parked the cleaner kthread, ordered extents may have 4672a362bb86SFilipe Manana * completed and created new delayed iputs. If one of the async reclaim 4673a362bb86SFilipe Manana * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we 4674a362bb86SFilipe Manana * can hang forever trying to stop it, because if a delayed iput is 4675a362bb86SFilipe Manana * added after it ran btrfs_run_delayed_iputs() and before it called 4676a362bb86SFilipe Manana * btrfs_wait_on_delayed_iputs(), it will hang forever since there is 4677a362bb86SFilipe Manana * no one else to run iputs. 4678a362bb86SFilipe Manana * 4679a362bb86SFilipe Manana * So wait for all ongoing ordered extents to complete and then run 4680a362bb86SFilipe Manana * delayed iputs. This works because once we reach this point no one 4681a362bb86SFilipe Manana * can either create new ordered extents nor create delayed iputs 4682a362bb86SFilipe Manana * through some other means. 4683a362bb86SFilipe Manana * 4684a362bb86SFilipe Manana * Also note that btrfs_wait_ordered_roots() is not safe here, because 4685a362bb86SFilipe Manana * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent, 4686a362bb86SFilipe Manana * but the delayed iput for the respective inode is made only when doing 4687a362bb86SFilipe Manana * the final btrfs_put_ordered_extent() (which must happen at 4688a362bb86SFilipe Manana * btrfs_finish_ordered_io() when we are unmounting). 4689a362bb86SFilipe Manana */ 4690a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_write_workers); 4691a362bb86SFilipe Manana /* Ordered extents for free space inodes. */ 4692a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_freespace_worker); 4693a362bb86SFilipe Manana btrfs_run_delayed_iputs(fs_info); 4694a362bb86SFilipe Manana 469521c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 469657056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 4697576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work); 469821c7e756SMiao Xie 4699b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */ 4700b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 4701b0643e59SDennis Zhou 4702bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 4703e44163e1SJeff Mahoney /* 4704d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over 4705d6fd0ae2SOmar Sandoval * unused block groups. 4706e44163e1SJeff Mahoney */ 47070b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 4708e44163e1SJeff Mahoney 4709f0cc2cd7SFilipe Manana /* 4710f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running 4711f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for 4712f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction. 4713f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items() 4714f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes 4715f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes. 4716f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction 4717f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock. 4718f0cc2cd7SFilipe Manana * This is a very rare case. 4719f0cc2cd7SFilipe Manana */ 4720f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers); 4721f0cc2cd7SFilipe Manana 47226bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 4723d397712bSChris Mason if (ret) 472404892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 4725c146afadSYan Zheng } 4726ed2ff2cbSChris Mason 472784961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) 47282ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 4729acce952bSliubo 4730e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 4731e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 47328929ecfaSYan, Zheng 4733e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs)); 4734afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 4735f25784b3SYan Zheng 47365958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) { 47375958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 47385958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked"); 47395958253cSQu Wenruo } 47405958253cSQu Wenruo 474104892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 4742fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots)); 4743bcef60f2SArne Jansen 4744963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 474504892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 4746963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 4747b0c68f8bSChris Mason } 474831153d81SYan Zheng 47495deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes)) 47504297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld", 47515deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes)); 47524297ff84SJosef Bacik 47536618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 4754b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 47555ac1d209SJeff Mahoney 47561a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 47571a4319ccSLiu Bo 4758de348ee0SWang Shilong /* 4759de348ee0SWang Shilong * we must make sure there is not any read request to 4760de348ee0SWang Shilong * submit after we stopping all workers. 4761de348ee0SWang Shilong */ 4762de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 476396192499SJosef Bacik btrfs_stop_all_workers(fs_info); 476496192499SJosef Bacik 47650a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */ 476636c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info); 47670a31daa4SFilipe Manana 4768afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 47694273eaffSAnand Jain free_root_pointers(fs_info, true); 47708c38938cSJosef Bacik btrfs_free_fs_roots(fs_info); 47719ad6b7bcSChris Mason 47724e19443dSJosef Bacik /* 47734e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could 47744e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't 47754e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group 47764e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes, 47774e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards. 47784e19443dSJosef Bacik */ 47794e19443dSJosef Bacik btrfs_free_block_groups(fs_info); 47804e19443dSJosef Bacik 478113e6c37bSJosef Bacik iput(fs_info->btree_inode); 4782d6bfde87SChris Mason 478321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 47840b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 47852ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 478621adbd5cSStefan Behrens #endif 478721adbd5cSStefan Behrens 47880b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 478968c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices); 4790eb60ceacSChris Mason } 4791eb60ceacSChris Mason 4792b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4793b9fab919SChris Mason int atomic) 4794ccd467d6SChris Mason { 47951259ab75SChris Mason int ret; 4796727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 47971259ab75SChris Mason 47980b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 47991259ab75SChris Mason if (!ret) 48001259ab75SChris Mason return ret; 48011259ab75SChris Mason 48021259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4803b9fab919SChris Mason parent_transid, atomic); 4804b9fab919SChris Mason if (ret == -EAGAIN) 4805b9fab919SChris Mason return ret; 48061259ab75SChris Mason return !ret; 48075f39d397SChris Mason } 48086702ed49SChris Mason 48095f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 48105f39d397SChris Mason { 48112f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info; 48125f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4813b9473439SChris Mason int was_dirty; 4814b4ce94deSChris Mason 481506ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 481606ea65a3SJosef Bacik /* 481706ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 481852042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty 481906ea65a3SJosef Bacik * outside of the sanity tests. 482006ea65a3SJosef Bacik */ 4821b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags))) 482206ea65a3SJosef Bacik return; 482306ea65a3SJosef Bacik #endif 482449d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf); 48250b246afaSJeff Mahoney if (transid != fs_info->generation) 48265d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 48270b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 48280b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4829e2d84521SMiao Xie if (!was_dirty) 4830104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 4831e2d84521SMiao Xie buf->len, 48320b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 48331f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 483469fc6cbbSQu Wenruo /* 483569fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 483669fc6cbbSQu Wenruo * but item data not updated. 483769fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 483869fc6cbbSQu Wenruo */ 483969fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 4840cfdaad5eSDavid Sterba btrfs_check_leaf_relaxed(buf)) { 4841a4f78750SDavid Sterba btrfs_print_leaf(buf); 48421f21ef0aSFilipe Manana ASSERT(0); 48431f21ef0aSFilipe Manana } 48441f21ef0aSFilipe Manana #endif 4845eb60ceacSChris Mason } 4846eb60ceacSChris Mason 48472ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4848b53d3f5dSLiu Bo int flush_delayed) 484935b7e476SChris Mason { 4850188de649SChris Mason /* 4851188de649SChris Mason * looks as though older kernels can get into trouble with 4852188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4853188de649SChris Mason */ 4854e2d84521SMiao Xie int ret; 4855d6bfde87SChris Mason 48566933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4857d6bfde87SChris Mason return; 4858d6bfde87SChris Mason 4859b53d3f5dSLiu Bo if (flush_delayed) 48602ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 486116cdcec7SMiao Xie 4862d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4863d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH, 4864d814a491SEthan Lien fs_info->dirty_metadata_batch); 4865e2d84521SMiao Xie if (ret > 0) { 48660b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 486716cdcec7SMiao Xie } 486816cdcec7SMiao Xie } 486916cdcec7SMiao Xie 48702ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 487116cdcec7SMiao Xie { 48722ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 487335b7e476SChris Mason } 4874b53d3f5dSLiu Bo 48752ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4876b53d3f5dSLiu Bo { 48772ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4878d6bfde87SChris Mason } 48796b80053dSChris Mason 48802ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4881acce952bSliubo { 4882fe816d0fSNikolay Borisov /* cleanup FS via transaction */ 4883fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info); 4884fe816d0fSNikolay Borisov 48850b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 48862ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 48870b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4888acce952bSliubo 48890b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 48900b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4891acce952bSliubo } 4892acce952bSliubo 4893ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) 4894ef67963dSJosef Bacik { 4895fc7cbcd4SDavid Sterba struct btrfs_root *gang[8]; 4896fc7cbcd4SDavid Sterba u64 root_objectid = 0; 4897fc7cbcd4SDavid Sterba int ret; 4898ef67963dSJosef Bacik 4899fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4900fc7cbcd4SDavid Sterba while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 4901fc7cbcd4SDavid Sterba (void **)gang, root_objectid, 4902fc7cbcd4SDavid Sterba ARRAY_SIZE(gang))) != 0) { 4903fc7cbcd4SDavid Sterba int i; 4904ef67963dSJosef Bacik 4905fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) 4906fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]); 4907fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4908fc7cbcd4SDavid Sterba 4909fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) { 4910fc7cbcd4SDavid Sterba if (!gang[i]) 4911ef67963dSJosef Bacik continue; 4912fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid; 4913fc7cbcd4SDavid Sterba btrfs_free_log(NULL, gang[i]); 4914fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]); 4915ef67963dSJosef Bacik } 4916fc7cbcd4SDavid Sterba root_objectid++; 4917fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock); 4918ef67963dSJosef Bacik } 4919fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock); 4920ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info); 4921ef67963dSJosef Bacik } 4922ef67963dSJosef Bacik 4923143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4924acce952bSliubo { 4925acce952bSliubo struct btrfs_ordered_extent *ordered; 4926acce952bSliubo 4927199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4928779880efSJosef Bacik /* 4929779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4930779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4931779880efSJosef Bacik */ 4932199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4933779880efSJosef Bacik root_extent_list) 4934779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4935199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4936199c2a9cSMiao Xie } 4937199c2a9cSMiao Xie 4938199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4939199c2a9cSMiao Xie { 4940199c2a9cSMiao Xie struct btrfs_root *root; 4941199c2a9cSMiao Xie struct list_head splice; 4942199c2a9cSMiao Xie 4943199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4944199c2a9cSMiao Xie 4945199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4946199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4947199c2a9cSMiao Xie while (!list_empty(&splice)) { 4948199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4949199c2a9cSMiao Xie ordered_root); 49501de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 49511de2cfdeSJosef Bacik &fs_info->ordered_roots); 4952199c2a9cSMiao Xie 49532a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4954199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4955199c2a9cSMiao Xie 49562a85d9caSLiu Bo cond_resched(); 49572a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4958199c2a9cSMiao Xie } 4959199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 496074d5d229SJosef Bacik 496174d5d229SJosef Bacik /* 496274d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't 496374d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered 496474d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet 496574d5d229SJosef Bacik * actually get run and error out properly. 496674d5d229SJosef Bacik */ 496774d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 4968acce952bSliubo } 4969acce952bSliubo 497035a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 49712ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4972acce952bSliubo { 4973acce952bSliubo struct rb_node *node; 4974acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4975acce952bSliubo struct btrfs_delayed_ref_node *ref; 4976acce952bSliubo int ret = 0; 4977acce952bSliubo 4978acce952bSliubo delayed_refs = &trans->delayed_refs; 4979acce952bSliubo 4980acce952bSliubo spin_lock(&delayed_refs->lock); 4981d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4982cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 4983b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry"); 4984acce952bSliubo return ret; 4985acce952bSliubo } 4986acce952bSliubo 49875c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) { 4988d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 49890e0adbcfSJosef Bacik struct rb_node *n; 4990e78417d1SJosef Bacik bool pin_bytes = false; 4991acce952bSliubo 4992d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4993d7df2c79SJosef Bacik href_node); 49943069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head)) 4995b939d1abSJosef Bacik continue; 49963069bd26SJosef Bacik 4997d7df2c79SJosef Bacik spin_lock(&head->lock); 4998e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) { 49990e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 50000e0adbcfSJosef Bacik ref_node); 5001d7df2c79SJosef Bacik ref->in_tree = 0; 5002e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree); 50030e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 50041d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 50051d57ee94SWang Xiaoguang list_del(&ref->add_list); 5006d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 5007d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 5008d7df2c79SJosef Bacik } 500954067ae9SJosef Bacik if (head->must_insert_reserved) 5010e78417d1SJosef Bacik pin_bytes = true; 501178a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 5012fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head); 5013d7df2c79SJosef Bacik spin_unlock(&head->lock); 5014acce952bSliubo spin_unlock(&delayed_refs->lock); 5015e78417d1SJosef Bacik mutex_unlock(&head->mutex); 5016acce952bSliubo 5017f603bb94SNikolay Borisov if (pin_bytes) { 5018f603bb94SNikolay Borisov struct btrfs_block_group *cache; 5019f603bb94SNikolay Borisov 5020f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr); 5021f603bb94SNikolay Borisov BUG_ON(!cache); 5022f603bb94SNikolay Borisov 5023f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock); 5024f603bb94SNikolay Borisov spin_lock(&cache->lock); 5025f603bb94SNikolay Borisov cache->pinned += head->num_bytes; 5026f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info, 5027f603bb94SNikolay Borisov cache->space_info, head->num_bytes); 5028f603bb94SNikolay Borisov cache->reserved -= head->num_bytes; 5029f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes; 5030f603bb94SNikolay Borisov spin_unlock(&cache->lock); 5031f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock); 5032f603bb94SNikolay Borisov 5033f603bb94SNikolay Borisov btrfs_put_block_group(cache); 5034f603bb94SNikolay Borisov 5035f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr, 5036f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1); 5037f603bb94SNikolay Borisov } 503831890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 5039d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 5040acce952bSliubo cond_resched(); 5041acce952bSliubo spin_lock(&delayed_refs->lock); 5042acce952bSliubo } 504381f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans); 5044acce952bSliubo 5045acce952bSliubo spin_unlock(&delayed_refs->lock); 5046acce952bSliubo 5047acce952bSliubo return ret; 5048acce952bSliubo } 5049acce952bSliubo 5050143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 5051acce952bSliubo { 5052acce952bSliubo struct btrfs_inode *btrfs_inode; 5053acce952bSliubo struct list_head splice; 5054acce952bSliubo 5055acce952bSliubo INIT_LIST_HEAD(&splice); 5056acce952bSliubo 5057eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5058eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 5059acce952bSliubo 5060acce952bSliubo while (!list_empty(&splice)) { 5061fe816d0fSNikolay Borisov struct inode *inode = NULL; 5062eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 5063acce952bSliubo delalloc_inodes); 5064fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode); 5065eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5066acce952bSliubo 5067fe816d0fSNikolay Borisov /* 5068fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear 5069fe816d0fSNikolay Borisov * meanwhile. 5070fe816d0fSNikolay Borisov */ 5071fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode); 5072fe816d0fSNikolay Borisov if (inode) { 5073fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping); 5074fe816d0fSNikolay Borisov iput(inode); 5075fe816d0fSNikolay Borisov } 5076eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 5077acce952bSliubo } 5078eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 5079eb73c1b7SMiao Xie } 5080eb73c1b7SMiao Xie 5081eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 5082eb73c1b7SMiao Xie { 5083eb73c1b7SMiao Xie struct btrfs_root *root; 5084eb73c1b7SMiao Xie struct list_head splice; 5085eb73c1b7SMiao Xie 5086eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 5087eb73c1b7SMiao Xie 5088eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5089eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 5090eb73c1b7SMiao Xie while (!list_empty(&splice)) { 5091eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 5092eb73c1b7SMiao Xie delalloc_root); 509300246528SJosef Bacik root = btrfs_grab_root(root); 5094eb73c1b7SMiao Xie BUG_ON(!root); 5095eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5096eb73c1b7SMiao Xie 5097eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 509800246528SJosef Bacik btrfs_put_root(root); 5099eb73c1b7SMiao Xie 5100eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 5101eb73c1b7SMiao Xie } 5102eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 5103acce952bSliubo } 5104acce952bSliubo 51052ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 5106acce952bSliubo struct extent_io_tree *dirty_pages, 5107acce952bSliubo int mark) 5108acce952bSliubo { 5109acce952bSliubo int ret; 5110acce952bSliubo struct extent_buffer *eb; 5111acce952bSliubo u64 start = 0; 5112acce952bSliubo u64 end; 5113acce952bSliubo 5114acce952bSliubo while (1) { 5115acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5116e6138876SJosef Bacik mark, NULL); 5117acce952bSliubo if (ret) 5118acce952bSliubo break; 5119acce952bSliubo 512091166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 5121acce952bSliubo while (start <= end) { 51220b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 51230b246afaSJeff Mahoney start += fs_info->nodesize; 5124fd8b2b61SJosef Bacik if (!eb) 5125acce952bSliubo continue; 5126fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 5127acce952bSliubo 5128fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 5129fd8b2b61SJosef Bacik &eb->bflags)) 5130fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 5131fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 5132acce952bSliubo } 5133acce952bSliubo } 5134acce952bSliubo 5135acce952bSliubo return ret; 5136acce952bSliubo } 5137acce952bSliubo 51382ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 5139fe119a6eSNikolay Borisov struct extent_io_tree *unpin) 5140acce952bSliubo { 5141acce952bSliubo u64 start; 5142acce952bSliubo u64 end; 5143acce952bSliubo int ret; 5144acce952bSliubo 5145acce952bSliubo while (1) { 51460e6ec385SFilipe Manana struct extent_state *cached_state = NULL; 51470e6ec385SFilipe Manana 5148fcd5e742SLu Fengqi /* 5149fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as 5150fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty. 5151fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin 5152fcd5e742SLu Fengqi * the same extent range. 5153fcd5e742SLu Fengqi */ 5154fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex); 5155acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 51560e6ec385SFilipe Manana EXTENT_DIRTY, &cached_state); 5157fcd5e742SLu Fengqi if (ret) { 5158fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5159acce952bSliubo break; 5160fcd5e742SLu Fengqi } 5161acce952bSliubo 51620e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state); 51630e6ec385SFilipe Manana free_extent_state(cached_state); 51642ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 5165fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex); 5166acce952bSliubo cond_resched(); 5167acce952bSliubo } 5168acce952bSliubo 5169acce952bSliubo return 0; 5170acce952bSliubo } 5171acce952bSliubo 517232da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache) 5173c79a1751SLiu Bo { 5174c79a1751SLiu Bo struct inode *inode; 5175c79a1751SLiu Bo 5176c79a1751SLiu Bo inode = cache->io_ctl.inode; 5177c79a1751SLiu Bo if (inode) { 5178c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 5179c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 5180c79a1751SLiu Bo cache->io_ctl.inode = NULL; 5181c79a1751SLiu Bo iput(inode); 5182c79a1751SLiu Bo } 5183bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL); 5184c79a1751SLiu Bo btrfs_put_block_group(cache); 5185c79a1751SLiu Bo } 5186c79a1751SLiu Bo 5187c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 51882ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 5189c79a1751SLiu Bo { 519032da5386SDavid Sterba struct btrfs_block_group *cache; 5191c79a1751SLiu Bo 5192c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5193c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 5194c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 519532da5386SDavid Sterba struct btrfs_block_group, 5196c79a1751SLiu Bo dirty_list); 5197c79a1751SLiu Bo 5198c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 5199c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5200c79a1751SLiu Bo list_del_init(&cache->io_list); 5201c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5202c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5203c79a1751SLiu Bo } 5204c79a1751SLiu Bo 5205c79a1751SLiu Bo list_del_init(&cache->dirty_list); 5206c79a1751SLiu Bo spin_lock(&cache->lock); 5207c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5208c79a1751SLiu Bo spin_unlock(&cache->lock); 5209c79a1751SLiu Bo 5210c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5211c79a1751SLiu Bo btrfs_put_block_group(cache); 5212ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1); 5213c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 5214c79a1751SLiu Bo } 5215c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 5216c79a1751SLiu Bo 521745ae2c18SNikolay Borisov /* 521845ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 521945ae2c18SNikolay Borisov * to use it without any locking 522045ae2c18SNikolay Borisov */ 5221c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 5222c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 522332da5386SDavid Sterba struct btrfs_block_group, 5224c79a1751SLiu Bo io_list); 5225c79a1751SLiu Bo 5226c79a1751SLiu Bo list_del_init(&cache->io_list); 5227c79a1751SLiu Bo spin_lock(&cache->lock); 5228c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 5229c79a1751SLiu Bo spin_unlock(&cache->lock); 5230c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 5231c79a1751SLiu Bo } 5232c79a1751SLiu Bo } 5233c79a1751SLiu Bo 523449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 52352ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 523649b25e05SJeff Mahoney { 5237bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp; 5238bbbf7243SNikolay Borisov 52392ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 5240c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 5241c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 5242c79a1751SLiu Bo 5243bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list, 5244bbbf7243SNikolay Borisov post_commit_list) { 5245bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list); 5246bbbf7243SNikolay Borisov } 5247bbbf7243SNikolay Borisov 52482ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 524949b25e05SJeff Mahoney 52504a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 52510b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 525249b25e05SJeff Mahoney 52534a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 52540b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 525549b25e05SJeff Mahoney 5256ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 525767cde344SMiao Xie 52582ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 525949b25e05SJeff Mahoney EXTENT_DIRTY); 5260fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents); 526149b25e05SJeff Mahoney 5262d3575156SNaohiro Aota btrfs_free_redirty_list(cur_trans); 5263d3575156SNaohiro Aota 52644a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 52654a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 526649b25e05SJeff Mahoney } 526749b25e05SJeff Mahoney 52682ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 5269acce952bSliubo { 5270acce952bSliubo struct btrfs_transaction *t; 5271acce952bSliubo 52720b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 5273acce952bSliubo 52740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 52750b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 52760b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 5277724e2315SJosef Bacik struct btrfs_transaction, list); 5278724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 52799b64f57dSElena Reshetova refcount_inc(&t->use_count); 52800b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 52812ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 5282724e2315SJosef Bacik btrfs_put_transaction(t); 52830b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5284724e2315SJosef Bacik continue; 5285724e2315SJosef Bacik } 52860b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 5287724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 52880b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5289724e2315SJosef Bacik /* 5290724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 5291724e2315SJosef Bacik * handle open currently for this transaction. 5292724e2315SJosef Bacik */ 5293724e2315SJosef Bacik wait_event(t->writer_wait, 5294724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 5295724e2315SJosef Bacik } else { 52960b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5297724e2315SJosef Bacik } 52982ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 5299724e2315SJosef Bacik 53000b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 53010b246afaSJeff Mahoney if (t == fs_info->running_transaction) 53020b246afaSJeff Mahoney fs_info->running_transaction = NULL; 5303724e2315SJosef Bacik list_del_init(&t->list); 53040b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 5305a4abeea4SJosef Bacik 5306724e2315SJosef Bacik btrfs_put_transaction(t); 53072e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info); 53080b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 5309724e2315SJosef Bacik } 53100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 53110b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 5312ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 5313ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 53140b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 5315ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info); 53160b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 5317acce952bSliubo 5318acce952bSliubo return 0; 5319acce952bSliubo } 5320ec7d6dfdSNikolay Borisov 5321453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root) 5322ec7d6dfdSNikolay Borisov { 5323ec7d6dfdSNikolay Borisov struct btrfs_path *path; 5324ec7d6dfdSNikolay Borisov int ret; 5325ec7d6dfdSNikolay Borisov struct extent_buffer *l; 5326ec7d6dfdSNikolay Borisov struct btrfs_key search_key; 5327ec7d6dfdSNikolay Borisov struct btrfs_key found_key; 5328ec7d6dfdSNikolay Borisov int slot; 5329ec7d6dfdSNikolay Borisov 5330ec7d6dfdSNikolay Borisov path = btrfs_alloc_path(); 5331ec7d6dfdSNikolay Borisov if (!path) 5332ec7d6dfdSNikolay Borisov return -ENOMEM; 5333ec7d6dfdSNikolay Borisov 5334ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 5335ec7d6dfdSNikolay Borisov search_key.type = -1; 5336ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1; 5337ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 5338ec7d6dfdSNikolay Borisov if (ret < 0) 5339ec7d6dfdSNikolay Borisov goto error; 5340ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */ 5341ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) { 5342ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1; 5343ec7d6dfdSNikolay Borisov l = path->nodes[0]; 5344ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot); 534523125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1, 534623125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID); 5347ec7d6dfdSNikolay Borisov } else { 534823125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID; 5349ec7d6dfdSNikolay Borisov } 5350ec7d6dfdSNikolay Borisov ret = 0; 5351ec7d6dfdSNikolay Borisov error: 5352ec7d6dfdSNikolay Borisov btrfs_free_path(path); 5353ec7d6dfdSNikolay Borisov return ret; 5354ec7d6dfdSNikolay Borisov } 5355ec7d6dfdSNikolay Borisov 5356543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid) 5357ec7d6dfdSNikolay Borisov { 5358ec7d6dfdSNikolay Borisov int ret; 5359ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex); 5360ec7d6dfdSNikolay Borisov 53616b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 5362ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info, 5363ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value", 5364ec7d6dfdSNikolay Borisov root->root_key.objectid); 5365ec7d6dfdSNikolay Borisov ret = -ENOSPC; 5366ec7d6dfdSNikolay Borisov goto out; 5367ec7d6dfdSNikolay Borisov } 5368ec7d6dfdSNikolay Borisov 536923125104SNikolay Borisov *objectid = root->free_objectid++; 5370ec7d6dfdSNikolay Borisov ret = 0; 5371ec7d6dfdSNikolay Borisov out: 5372ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex); 5373ec7d6dfdSNikolay Borisov return ret; 5374ec7d6dfdSNikolay Borisov } 5375