16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 19e20d96d6SChris Mason #include <linux/fs.h> 20d98237b3SChris Mason #include <linux/blkdev.h> 2187cbda5cSChris Mason #include <linux/scatterlist.h> 2222b0ebdaSChris Mason #include <linux/swap.h> 230f7d52f4SChris Mason #include <linux/radix-tree.h> 2435b7e476SChris Mason #include <linux/writeback.h> 25d397712bSChris Mason #include <linux/buffer_head.h> 26ce9adaa5SChris Mason #include <linux/workqueue.h> 27a74a4b97SChris Mason #include <linux/kthread.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 29784b4e29SChris Mason #include <linux/migrate.h> 307a36ddecSDavid Sterba #include <linux/ratelimit.h> 316463fe58SStefan Behrens #include <linux/uuid.h> 32803b2f54SStefan Behrens #include <linux/semaphore.h> 33540adea3SMasami Hiramatsu #include <linux/error-injection.h> 349678c543SNikolay Borisov #include <linux/crc32c.h> 357e75bf3fSDavid Sterba #include <asm/unaligned.h> 36eb60ceacSChris Mason #include "ctree.h" 37eb60ceacSChris Mason #include "disk-io.h" 38e089f05cSChris Mason #include "transaction.h" 390f7d52f4SChris Mason #include "btrfs_inode.h" 400b86a832SChris Mason #include "volumes.h" 41db94535dSChris Mason #include "print-tree.h" 42925baeddSChris Mason #include "locking.h" 43e02119d5SChris Mason #include "tree-log.h" 44fa9c0d79SChris Mason #include "free-space-cache.h" 4570f6d82eSOmar Sandoval #include "free-space-tree.h" 46581bb050SLi Zefan #include "inode-map.h" 4721adbd5cSStefan Behrens #include "check-integrity.h" 48606686eeSJosef Bacik #include "rcu-string.h" 498dabb742SStefan Behrens #include "dev-replace.h" 5053b381b3SDavid Woodhouse #include "raid56.h" 515ac1d209SJeff Mahoney #include "sysfs.h" 52fcebe456SJosef Bacik #include "qgroup.h" 53ebb8765bSAnand Jain #include "compression.h" 54557ea5ddSQu Wenruo #include "tree-checker.h" 55fd708b81SJosef Bacik #include "ref-verify.h" 56eb60ceacSChris Mason 57de0022b9SJosef Bacik #ifdef CONFIG_X86 58de0022b9SJosef Bacik #include <asm/cpufeature.h> 59de0022b9SJosef Bacik #endif 60de0022b9SJosef Bacik 61319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 62319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 63319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 64319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 65e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\ 66e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2) 67319e4d06SQu Wenruo 68e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 698b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 704df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 713d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info); 72143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 73acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 742ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 75143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 762ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 77acce952bSliubo struct extent_io_tree *dirty_pages, 78acce952bSliubo int mark); 792ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 80acce952bSliubo struct extent_io_tree *pinned_extents); 812ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 822ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 83ce9adaa5SChris Mason 84d352ac68SChris Mason /* 8597eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 8697eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 87d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 88d352ac68SChris Mason */ 8997eb6b69SDavid Sterba struct btrfs_end_io_wq { 90ce9adaa5SChris Mason struct bio *bio; 91ce9adaa5SChris Mason bio_end_io_t *end_io; 92ce9adaa5SChris Mason void *private; 93ce9adaa5SChris Mason struct btrfs_fs_info *info; 944e4cbee9SChristoph Hellwig blk_status_t status; 95bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 968b712842SChris Mason struct btrfs_work work; 97ce9adaa5SChris Mason }; 980da5468fSChris Mason 9997eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 10097eb6b69SDavid Sterba 10197eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 10297eb6b69SDavid Sterba { 10397eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 10497eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 10597eb6b69SDavid Sterba 0, 106fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 10797eb6b69SDavid Sterba NULL); 10897eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 10997eb6b69SDavid Sterba return -ENOMEM; 11097eb6b69SDavid Sterba return 0; 11197eb6b69SDavid Sterba } 11297eb6b69SDavid Sterba 113e67c718bSDavid Sterba void __cold btrfs_end_io_wq_exit(void) 11497eb6b69SDavid Sterba { 11597eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 11697eb6b69SDavid Sterba } 11797eb6b69SDavid Sterba 118d352ac68SChris Mason /* 119d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 120d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 121d352ac68SChris Mason * just before they are sent down the IO stack. 122d352ac68SChris Mason */ 12344b8bd7eSChris Mason struct async_submit_bio { 124c6100a4bSJosef Bacik void *private_data; 125c6100a4bSJosef Bacik struct btrfs_fs_info *fs_info; 12644b8bd7eSChris Mason struct bio *bio; 127a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start; 128a758781dSDavid Sterba extent_submit_bio_done_t *submit_bio_done; 12944b8bd7eSChris Mason int mirror_num; 130c8b97818SChris Mason unsigned long bio_flags; 131eaf25d93SChris Mason /* 132eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 133eaf25d93SChris Mason * can't tell us where in the file the bio should go 134eaf25d93SChris Mason */ 135eaf25d93SChris Mason u64 bio_offset; 1368b712842SChris Mason struct btrfs_work work; 1374e4cbee9SChristoph Hellwig blk_status_t status; 13844b8bd7eSChris Mason }; 13944b8bd7eSChris Mason 14085d4e461SChris Mason /* 14185d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 14285d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 14385d4e461SChris Mason * the level the eb occupies in the tree. 1444008c04aSChris Mason * 14585d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 14685d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 14785d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 14885d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 14985d4e461SChris Mason * have separate keysets. 1504008c04aSChris Mason * 15185d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 15285d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 15385d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1544008c04aSChris Mason * 15585d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 15685d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 15785d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 15885d4e461SChris Mason * 15985d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 16085d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 16185d4e461SChris Mason * needs update as well. 1624008c04aSChris Mason */ 1634008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1644008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1654008c04aSChris Mason # error 1664008c04aSChris Mason # endif 16785d4e461SChris Mason 16885d4e461SChris Mason static struct btrfs_lockdep_keyset { 16985d4e461SChris Mason u64 id; /* root objectid */ 17085d4e461SChris Mason const char *name_stem; /* lock name stem */ 17185d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 17285d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 17385d4e461SChris Mason } btrfs_lockdep_keysets[] = { 17485d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 17585d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 17685d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 17785d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 17885d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 17985d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 18060b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 18185d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 18285d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 18385d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 18413fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1856b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 18685d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1874008c04aSChris Mason }; 18885d4e461SChris Mason 18985d4e461SChris Mason void __init btrfs_init_lockdep(void) 19085d4e461SChris Mason { 19185d4e461SChris Mason int i, j; 19285d4e461SChris Mason 19385d4e461SChris Mason /* initialize lockdep class names */ 19485d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 19585d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 19685d4e461SChris Mason 19785d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 19885d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 19985d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 20085d4e461SChris Mason } 20185d4e461SChris Mason } 20285d4e461SChris Mason 20385d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 20485d4e461SChris Mason int level) 20585d4e461SChris Mason { 20685d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 20785d4e461SChris Mason 20885d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 20985d4e461SChris Mason 21085d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 21185d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 21285d4e461SChris Mason if (ks->id == objectid) 21385d4e461SChris Mason break; 21485d4e461SChris Mason 21585d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 21685d4e461SChris Mason &ks->keys[level], ks->names[level]); 21785d4e461SChris Mason } 21885d4e461SChris Mason 2194008c04aSChris Mason #endif 2204008c04aSChris Mason 221d352ac68SChris Mason /* 222d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 223d352ac68SChris Mason * that covers the entire device 224d352ac68SChris Mason */ 2256af49dbdSDavid Sterba struct extent_map *btree_get_extent(struct btrfs_inode *inode, 226306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2275f39d397SChris Mason int create) 2285f39d397SChris Mason { 229fc4f21b1SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 230fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2315f39d397SChris Mason struct extent_map *em; 2325f39d397SChris Mason int ret; 2335f39d397SChris Mason 234890871beSChris Mason read_lock(&em_tree->lock); 235d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 236a061fc8dSChris Mason if (em) { 2370b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 238890871beSChris Mason read_unlock(&em_tree->lock); 2395f39d397SChris Mason goto out; 240a061fc8dSChris Mason } 241890871beSChris Mason read_unlock(&em_tree->lock); 2427b13b7b1SChris Mason 243172ddd60SDavid Sterba em = alloc_extent_map(); 2445f39d397SChris Mason if (!em) { 2455f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2465f39d397SChris Mason goto out; 2475f39d397SChris Mason } 2485f39d397SChris Mason em->start = 0; 2490afbaf8cSChris Mason em->len = (u64)-1; 250c8b97818SChris Mason em->block_len = (u64)-1; 2515f39d397SChris Mason em->block_start = 0; 2520b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 253d1310b2eSChris Mason 254890871beSChris Mason write_lock(&em_tree->lock); 25509a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2565f39d397SChris Mason if (ret == -EEXIST) { 2575f39d397SChris Mason free_extent_map(em); 2587b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 259b4f359abSTsutomu Itoh if (!em) 2600433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2615f39d397SChris Mason } else if (ret) { 2627b13b7b1SChris Mason free_extent_map(em); 2630433f20dSTsutomu Itoh em = ERR_PTR(ret); 2645f39d397SChris Mason } 265890871beSChris Mason write_unlock(&em_tree->lock); 2667b13b7b1SChris Mason 2675f39d397SChris Mason out: 2685f39d397SChris Mason return em; 2695f39d397SChris Mason } 2705f39d397SChris Mason 2719ed57367SDavid Sterba u32 btrfs_csum_data(const char *data, u32 seed, size_t len) 27219c00ddcSChris Mason { 2739678c543SNikolay Borisov return crc32c(seed, data, len); 27419c00ddcSChris Mason } 27519c00ddcSChris Mason 2760b5e3dafSDomagoj Tršan void btrfs_csum_final(u32 crc, u8 *result) 27719c00ddcSChris Mason { 2787e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 27919c00ddcSChris Mason } 28019c00ddcSChris Mason 281d352ac68SChris Mason /* 282d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 283d352ac68SChris Mason * into the csum field of the block. 284d352ac68SChris Mason */ 28501d58472SDaniel Dressler static int csum_tree_block(struct btrfs_fs_info *fs_info, 28601d58472SDaniel Dressler struct extent_buffer *buf, 28719c00ddcSChris Mason int verify) 28819c00ddcSChris Mason { 28901d58472SDaniel Dressler u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 29071a63551SDavid Sterba char result[BTRFS_CSUM_SIZE]; 29119c00ddcSChris Mason unsigned long len; 29219c00ddcSChris Mason unsigned long cur_len; 29319c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 29419c00ddcSChris Mason char *kaddr; 29519c00ddcSChris Mason unsigned long map_start; 29619c00ddcSChris Mason unsigned long map_len; 29719c00ddcSChris Mason int err; 29819c00ddcSChris Mason u32 crc = ~(u32)0; 29919c00ddcSChris Mason 30019c00ddcSChris Mason len = buf->len - offset; 30119c00ddcSChris Mason while (len > 0) { 30219c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 303a6591715SChris Mason &kaddr, &map_start, &map_len); 304d397712bSChris Mason if (err) 3058bd98f0eSAlex Lyakas return err; 30619c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 307b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 30819c00ddcSChris Mason crc, cur_len); 30919c00ddcSChris Mason len -= cur_len; 31019c00ddcSChris Mason offset += cur_len; 31119c00ddcSChris Mason } 31271a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE); 313607d432dSJosef Bacik 31419c00ddcSChris Mason btrfs_csum_final(crc, result); 31519c00ddcSChris Mason 31619c00ddcSChris Mason if (verify) { 317607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 318e4204dedSChris Mason u32 val; 319e4204dedSChris Mason u32 found = 0; 320607d432dSJosef Bacik memcpy(&found, result, csum_size); 321e4204dedSChris Mason 322607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 32394647322SDavid Sterba btrfs_warn_rl(fs_info, 3245d163e0eSJeff Mahoney "%s checksum verify failed on %llu wanted %X found %X level %d", 32501d58472SDaniel Dressler fs_info->sb->s_id, buf->start, 326c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 3278bd98f0eSAlex Lyakas return -EUCLEAN; 32819c00ddcSChris Mason } 32919c00ddcSChris Mason } else { 330607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 33119c00ddcSChris Mason } 33271a63551SDavid Sterba 33319c00ddcSChris Mason return 0; 33419c00ddcSChris Mason } 33519c00ddcSChris Mason 336d352ac68SChris Mason /* 337d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 338d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 339d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 340d352ac68SChris Mason * in the wrong place. 341d352ac68SChris Mason */ 3421259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 343b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 344b9fab919SChris Mason int atomic) 3451259ab75SChris Mason { 3462ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3471259ab75SChris Mason int ret; 3482755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3491259ab75SChris Mason 3501259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3511259ab75SChris Mason return 0; 3521259ab75SChris Mason 353b9fab919SChris Mason if (atomic) 354b9fab919SChris Mason return -EAGAIN; 355b9fab919SChris Mason 356a26e8c9fSJosef Bacik if (need_lock) { 357a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 358a26e8c9fSJosef Bacik btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 359a26e8c9fSJosef Bacik } 360a26e8c9fSJosef Bacik 3612ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 362ff13db41SDavid Sterba &cached_state); 3630b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3641259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3651259ab75SChris Mason ret = 0; 3661259ab75SChris Mason goto out; 3671259ab75SChris Mason } 36894647322SDavid Sterba btrfs_err_rl(eb->fs_info, 36994647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 37094647322SDavid Sterba eb->start, 37129549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3721259ab75SChris Mason ret = 1; 373a26e8c9fSJosef Bacik 374a26e8c9fSJosef Bacik /* 375a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 376a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 37701327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 378a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 379a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 380a26e8c9fSJosef Bacik * making everybody very sad. 381a26e8c9fSJosef Bacik */ 382a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3830b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 38433958dc6SChris Mason out: 3852ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 386e43bbe5eSDavid Sterba &cached_state); 387472b909fSJosef Bacik if (need_lock) 388a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3891259ab75SChris Mason return ret; 3901259ab75SChris Mason } 3911259ab75SChris Mason 392d352ac68SChris Mason /* 3931104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3941104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3951104a885SDavid Sterba */ 396ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 397ab8d0fc4SJeff Mahoney char *raw_disk_sb) 3981104a885SDavid Sterba { 3991104a885SDavid Sterba struct btrfs_super_block *disk_sb = 4001104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 4011104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 4021104a885SDavid Sterba int ret = 0; 4031104a885SDavid Sterba 4041104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 4051104a885SDavid Sterba u32 crc = ~(u32)0; 406776c4a7cSNikolay Borisov char result[sizeof(crc)]; 4071104a885SDavid Sterba 4081104a885SDavid Sterba /* 4091104a885SDavid Sterba * The super_block structure does not span the whole 4101104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 41101327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 4121104a885SDavid Sterba */ 4131104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4141104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4151104a885SDavid Sterba btrfs_csum_final(crc, result); 4161104a885SDavid Sterba 417776c4a7cSNikolay Borisov if (memcmp(raw_disk_sb, result, sizeof(result))) 4181104a885SDavid Sterba ret = 1; 4191104a885SDavid Sterba } 4201104a885SDavid Sterba 4211104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 422ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 4231104a885SDavid Sterba csum_type); 4241104a885SDavid Sterba ret = 1; 4251104a885SDavid Sterba } 4261104a885SDavid Sterba 4271104a885SDavid Sterba return ret; 4281104a885SDavid Sterba } 4291104a885SDavid Sterba 430581c1760SQu Wenruo static int verify_level_key(struct btrfs_fs_info *fs_info, 431581c1760SQu Wenruo struct extent_buffer *eb, int level, 432581c1760SQu Wenruo struct btrfs_key *first_key) 433581c1760SQu Wenruo { 434581c1760SQu Wenruo int found_level; 435581c1760SQu Wenruo struct btrfs_key found_key; 436581c1760SQu Wenruo int ret; 437581c1760SQu Wenruo 438581c1760SQu Wenruo found_level = btrfs_header_level(eb); 439581c1760SQu Wenruo if (found_level != level) { 440581c1760SQu Wenruo #ifdef CONFIG_BTRFS_DEBUG 441581c1760SQu Wenruo WARN_ON(1); 442581c1760SQu Wenruo btrfs_err(fs_info, 443581c1760SQu Wenruo "tree level mismatch detected, bytenr=%llu level expected=%u has=%u", 444581c1760SQu Wenruo eb->start, level, found_level); 445581c1760SQu Wenruo #endif 446581c1760SQu Wenruo return -EIO; 447581c1760SQu Wenruo } 448581c1760SQu Wenruo 449581c1760SQu Wenruo if (!first_key) 450581c1760SQu Wenruo return 0; 451581c1760SQu Wenruo 452581c1760SQu Wenruo if (found_level) 453581c1760SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0); 454581c1760SQu Wenruo else 455581c1760SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0); 456581c1760SQu Wenruo ret = btrfs_comp_cpu_keys(first_key, &found_key); 457581c1760SQu Wenruo 458581c1760SQu Wenruo #ifdef CONFIG_BTRFS_DEBUG 459581c1760SQu Wenruo if (ret) { 460581c1760SQu Wenruo WARN_ON(1); 461581c1760SQu Wenruo btrfs_err(fs_info, 462581c1760SQu Wenruo "tree first key mismatch detected, bytenr=%llu key expected=(%llu, %u, %llu) has=(%llu, %u, %llu)", 463581c1760SQu Wenruo eb->start, first_key->objectid, first_key->type, 464581c1760SQu Wenruo first_key->offset, found_key.objectid, 465581c1760SQu Wenruo found_key.type, found_key.offset); 466581c1760SQu Wenruo } 467581c1760SQu Wenruo #endif 468581c1760SQu Wenruo return ret; 469581c1760SQu Wenruo } 470581c1760SQu Wenruo 4711104a885SDavid Sterba /* 472d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 473d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 474581c1760SQu Wenruo * 475581c1760SQu Wenruo * @parent_transid: expected transid, skip check if 0 476581c1760SQu Wenruo * @level: expected level, mandatory check 477581c1760SQu Wenruo * @first_key: expected key of first slot, skip check if NULL 478d352ac68SChris Mason */ 4792ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 480f188591eSChris Mason struct extent_buffer *eb, 481581c1760SQu Wenruo u64 parent_transid, int level, 482581c1760SQu Wenruo struct btrfs_key *first_key) 483f188591eSChris Mason { 484f188591eSChris Mason struct extent_io_tree *io_tree; 485ea466794SJosef Bacik int failed = 0; 486f188591eSChris Mason int ret; 487f188591eSChris Mason int num_copies = 0; 488f188591eSChris Mason int mirror_num = 0; 489ea466794SJosef Bacik int failed_mirror = 0; 490f188591eSChris Mason 491a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4920b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 493f188591eSChris Mason while (1) { 4948436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 4956af49dbdSDavid Sterba mirror_num); 496256dd1bbSStefan Behrens if (!ret) { 497581c1760SQu Wenruo if (verify_parent_transid(io_tree, eb, 498b9fab919SChris Mason parent_transid, 0)) 499256dd1bbSStefan Behrens ret = -EIO; 500581c1760SQu Wenruo else if (verify_level_key(fs_info, eb, level, 501581c1760SQu Wenruo first_key)) 502581c1760SQu Wenruo ret = -EUCLEAN; 503581c1760SQu Wenruo else 504581c1760SQu Wenruo break; 505256dd1bbSStefan Behrens } 506d397712bSChris Mason 507a826d6dcSJosef Bacik /* 508a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 509a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 510a826d6dcSJosef Bacik * any less wrong. 511a826d6dcSJosef Bacik */ 512581c1760SQu Wenruo if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags) || 513581c1760SQu Wenruo ret == -EUCLEAN) 514ea466794SJosef Bacik break; 515ea466794SJosef Bacik 5160b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 517f188591eSChris Mason eb->start, eb->len); 5184235298eSChris Mason if (num_copies == 1) 519ea466794SJosef Bacik break; 5204235298eSChris Mason 5215cf1ab56SJosef Bacik if (!failed_mirror) { 5225cf1ab56SJosef Bacik failed = 1; 5235cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 5245cf1ab56SJosef Bacik } 5255cf1ab56SJosef Bacik 526f188591eSChris Mason mirror_num++; 527ea466794SJosef Bacik if (mirror_num == failed_mirror) 528ea466794SJosef Bacik mirror_num++; 529ea466794SJosef Bacik 5304235298eSChris Mason if (mirror_num > num_copies) 531ea466794SJosef Bacik break; 532f188591eSChris Mason } 533ea466794SJosef Bacik 534c0901581SStefan Behrens if (failed && !ret && failed_mirror) 5352ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 536ea466794SJosef Bacik 537ea466794SJosef Bacik return ret; 538f188591eSChris Mason } 53919c00ddcSChris Mason 540d352ac68SChris Mason /* 541d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 542d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 543d352ac68SChris Mason */ 544d397712bSChris Mason 54501d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 54619c00ddcSChris Mason { 5474eee4fa4SMiao Xie u64 start = page_offset(page); 54819c00ddcSChris Mason u64 found_start; 54919c00ddcSChris Mason struct extent_buffer *eb; 550f188591eSChris Mason 5514f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5524f2de97aSJosef Bacik if (page != eb->pages[0]) 5534f2de97aSJosef Bacik return 0; 5540f805531SAlex Lyakas 55519c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5560f805531SAlex Lyakas /* 5570f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5580f805531SAlex Lyakas * It is useful to know what went wrong. 5590f805531SAlex Lyakas */ 5600f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5610f805531SAlex Lyakas return -EUCLEAN; 5620f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5630f805531SAlex Lyakas return -EUCLEAN; 5640f805531SAlex Lyakas 5650f805531SAlex Lyakas ASSERT(memcmp_extent_buffer(eb, fs_info->fsid, 5660f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5670f805531SAlex Lyakas 5688bd98f0eSAlex Lyakas return csum_tree_block(fs_info, eb, 0); 56919c00ddcSChris Mason } 57019c00ddcSChris Mason 57101d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5722b82032cSYan Zheng struct extent_buffer *eb) 5732b82032cSYan Zheng { 57401d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 57544880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5762b82032cSYan Zheng int ret = 1; 5772b82032cSYan Zheng 5780a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5792b82032cSYan Zheng while (fs_devices) { 5802b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5812b82032cSYan Zheng ret = 0; 5822b82032cSYan Zheng break; 5832b82032cSYan Zheng } 5842b82032cSYan Zheng fs_devices = fs_devices->seed; 5852b82032cSYan Zheng } 5862b82032cSYan Zheng return ret; 5872b82032cSYan Zheng } 5882b82032cSYan Zheng 589facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 590facc8a22SMiao Xie u64 phy_offset, struct page *page, 591facc8a22SMiao Xie u64 start, u64 end, int mirror) 592ce9adaa5SChris Mason { 593ce9adaa5SChris Mason u64 found_start; 594ce9adaa5SChris Mason int found_level; 595ce9adaa5SChris Mason struct extent_buffer *eb; 596ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 59702873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 598f188591eSChris Mason int ret = 0; 599727011e0SChris Mason int reads_done; 600ce9adaa5SChris Mason 601ce9adaa5SChris Mason if (!page->private) 602ce9adaa5SChris Mason goto out; 603d397712bSChris Mason 6044f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 605d397712bSChris Mason 6060b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6070b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6080b32f4bbSJosef Bacik */ 6090b32f4bbSJosef Bacik extent_buffer_get(eb); 6100b32f4bbSJosef Bacik 6110b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 612727011e0SChris Mason if (!reads_done) 613727011e0SChris Mason goto err; 614f188591eSChris Mason 6155cf1ab56SJosef Bacik eb->read_mirror = mirror; 616656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 617ea466794SJosef Bacik ret = -EIO; 618ea466794SJosef Bacik goto err; 619ea466794SJosef Bacik } 620ea466794SJosef Bacik 621ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 622727011e0SChris Mason if (found_start != eb->start) { 62302873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 62494647322SDavid Sterba found_start, eb->start); 625f188591eSChris Mason ret = -EIO; 626ce9adaa5SChris Mason goto err; 627ce9adaa5SChris Mason } 62802873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 62902873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 63094647322SDavid Sterba eb->start); 6311259ab75SChris Mason ret = -EIO; 6321259ab75SChris Mason goto err; 6331259ab75SChris Mason } 634ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6351c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 63602873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 6371c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6381c24c3ceSJosef Bacik ret = -EIO; 6391c24c3ceSJosef Bacik goto err; 6401c24c3ceSJosef Bacik } 641ce9adaa5SChris Mason 64285d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 64385d4e461SChris Mason eb, found_level); 6444008c04aSChris Mason 64502873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 6468bd98f0eSAlex Lyakas if (ret) 647a826d6dcSJosef Bacik goto err; 648a826d6dcSJosef Bacik 649a826d6dcSJosef Bacik /* 650a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 651a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 652a826d6dcSJosef Bacik * return -EIO. 653a826d6dcSJosef Bacik */ 6542f659546SQu Wenruo if (found_level == 0 && btrfs_check_leaf_full(fs_info, eb)) { 655a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 656a826d6dcSJosef Bacik ret = -EIO; 657a826d6dcSJosef Bacik } 658ce9adaa5SChris Mason 6592f659546SQu Wenruo if (found_level > 0 && btrfs_check_node(fs_info, eb)) 660053ab70fSLiu Bo ret = -EIO; 661053ab70fSLiu Bo 6620b32f4bbSJosef Bacik if (!ret) 6630b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 664ce9adaa5SChris Mason err: 66579fb65a1SJosef Bacik if (reads_done && 66679fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 667d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6684bb31e92SArne Jansen 66953b381b3SDavid Woodhouse if (ret) { 67053b381b3SDavid Woodhouse /* 67153b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 67253b381b3SDavid Woodhouse * again, we have to make sure it has something 67353b381b3SDavid Woodhouse * to decrement 67453b381b3SDavid Woodhouse */ 67553b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6760b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 67753b381b3SDavid Woodhouse } 6780b32f4bbSJosef Bacik free_extent_buffer(eb); 679ce9adaa5SChris Mason out: 680f188591eSChris Mason return ret; 681ce9adaa5SChris Mason } 682ce9adaa5SChris Mason 683ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6844bb31e92SArne Jansen { 6854bb31e92SArne Jansen struct extent_buffer *eb; 6864bb31e92SArne Jansen 6874f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 688656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 6895cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 69053b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 691ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 692d48d71aaSDavid Sterba btree_readahead_hook(eb, -EIO); 6934bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6944bb31e92SArne Jansen } 6954bb31e92SArne Jansen 6964246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 697ce9adaa5SChris Mason { 69897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 699ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7009e0af237SLiu Bo struct btrfs_workqueue *wq; 7019e0af237SLiu Bo btrfs_work_func_t func; 702ce9adaa5SChris Mason 703ce9adaa5SChris Mason fs_info = end_io_wq->info; 7044e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 705d20f7043SChris Mason 70637226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 7079e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 7089e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 7099e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 7109e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 7119e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 7129e0af237SLiu Bo func = btrfs_freespace_write_helper; 7139e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7149e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7159e0af237SLiu Bo func = btrfs_endio_raid56_helper; 716d20f7043SChris Mason } else { 7179e0af237SLiu Bo wq = fs_info->endio_write_workers; 7189e0af237SLiu Bo func = btrfs_endio_write_helper; 719d20f7043SChris Mason } 7209e0af237SLiu Bo } else { 7218b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 7228b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 7238b110e39SMiao Xie wq = fs_info->endio_repair_workers; 7248b110e39SMiao Xie func = btrfs_endio_repair_helper; 7258b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7269e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7279e0af237SLiu Bo func = btrfs_endio_raid56_helper; 7289e0af237SLiu Bo } else if (end_io_wq->metadata) { 7299e0af237SLiu Bo wq = fs_info->endio_meta_workers; 7309e0af237SLiu Bo func = btrfs_endio_meta_helper; 7319e0af237SLiu Bo } else { 7329e0af237SLiu Bo wq = fs_info->endio_workers; 7339e0af237SLiu Bo func = btrfs_endio_helper; 7349e0af237SLiu Bo } 7359e0af237SLiu Bo } 7369e0af237SLiu Bo 7379e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 7389e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 739ce9adaa5SChris Mason } 740ce9adaa5SChris Mason 7414e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 742bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7430b86a832SChris Mason { 74497eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7458b110e39SMiao Xie 74697eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 747ce9adaa5SChris Mason if (!end_io_wq) 7484e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 749ce9adaa5SChris Mason 750ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 751ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 75222c59948SChris Mason end_io_wq->info = info; 7534e4cbee9SChristoph Hellwig end_io_wq->status = 0; 754ce9adaa5SChris Mason end_io_wq->bio = bio; 75522c59948SChris Mason end_io_wq->metadata = metadata; 756ce9adaa5SChris Mason 757ce9adaa5SChris Mason bio->bi_private = end_io_wq; 758ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 75922c59948SChris Mason return 0; 76022c59948SChris Mason } 76122c59948SChris Mason 7624a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7634a69a410SChris Mason { 7644a69a410SChris Mason struct async_submit_bio *async; 7654e4cbee9SChristoph Hellwig blk_status_t ret; 7664a69a410SChris Mason 7674a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 768c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 769eaf25d93SChris Mason async->bio_offset); 77079787eaaSJeff Mahoney if (ret) 7714e4cbee9SChristoph Hellwig async->status = ret; 7724a69a410SChris Mason } 7734a69a410SChris Mason 7744a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7758b712842SChris Mason { 7768b712842SChris Mason struct async_submit_bio *async; 7778b712842SChris Mason 7788b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7794854ddd0SChris Mason 780bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7814e4cbee9SChristoph Hellwig if (async->status) { 7824e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7834246a0b6SChristoph Hellwig bio_endio(async->bio); 78479787eaaSJeff Mahoney return; 78579787eaaSJeff Mahoney } 78679787eaaSJeff Mahoney 7876c553435SDavid Sterba async->submit_bio_done(async->private_data, async->bio, async->mirror_num); 7884a69a410SChris Mason } 7894a69a410SChris Mason 7904a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7914a69a410SChris Mason { 7924a69a410SChris Mason struct async_submit_bio *async; 7934a69a410SChris Mason 7944a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7958b712842SChris Mason kfree(async); 7968b712842SChris Mason } 7978b712842SChris Mason 7988c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 799c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 800c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 801a758781dSDavid Sterba extent_submit_bio_start_t *submit_bio_start, 802a758781dSDavid Sterba extent_submit_bio_done_t *submit_bio_done) 80344b8bd7eSChris Mason { 80444b8bd7eSChris Mason struct async_submit_bio *async; 80544b8bd7eSChris Mason 80644b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 80744b8bd7eSChris Mason if (!async) 8084e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 80944b8bd7eSChris Mason 810c6100a4bSJosef Bacik async->private_data = private_data; 811c6100a4bSJosef Bacik async->fs_info = fs_info; 81244b8bd7eSChris Mason async->bio = bio; 81344b8bd7eSChris Mason async->mirror_num = mirror_num; 8144a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8154a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8164a69a410SChris Mason 8179e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 8185cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 8194a69a410SChris Mason 820c8b97818SChris Mason async->bio_flags = bio_flags; 821eaf25d93SChris Mason async->bio_offset = bio_offset; 8228c8bee1dSChris Mason 8234e4cbee9SChristoph Hellwig async->status = 0; 82479787eaaSJeff Mahoney 82567f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 8265cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 827d313d7a3SChris Mason 8285cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 82944b8bd7eSChris Mason return 0; 83044b8bd7eSChris Mason } 83144b8bd7eSChris Mason 8324e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 833ce3ed71aSChris Mason { 8342c30c71bSKent Overstreet struct bio_vec *bvec; 835ce3ed71aSChris Mason struct btrfs_root *root; 8362c30c71bSKent Overstreet int i, ret = 0; 837ce3ed71aSChris Mason 838c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8392c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 840ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 84101d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 84279787eaaSJeff Mahoney if (ret) 84379787eaaSJeff Mahoney break; 844ce3ed71aSChris Mason } 8452c30c71bSKent Overstreet 8464e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 847ce3ed71aSChris Mason } 848ce3ed71aSChris Mason 849d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio, 850eaf25d93SChris Mason u64 bio_offset) 85122c59948SChris Mason { 8528b712842SChris Mason /* 8538b712842SChris Mason * when we're called for a write, we're already in the async 8545443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8558b712842SChris Mason */ 85679787eaaSJeff Mahoney return btree_csum_one_bio(bio); 85722c59948SChris Mason } 85822c59948SChris Mason 859d0ee3934SDavid Sterba static blk_status_t btree_submit_bio_done(void *private_data, struct bio *bio, 8606c553435SDavid Sterba int mirror_num) 8614a69a410SChris Mason { 862c6100a4bSJosef Bacik struct inode *inode = private_data; 8634e4cbee9SChristoph Hellwig blk_status_t ret; 86461891923SStefan Behrens 8658b712842SChris Mason /* 8664a69a410SChris Mason * when we're called for a write, we're already in the async 8674a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8688b712842SChris Mason */ 8692ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 8704246a0b6SChristoph Hellwig if (ret) { 8714e4cbee9SChristoph Hellwig bio->bi_status = ret; 8724246a0b6SChristoph Hellwig bio_endio(bio); 8734246a0b6SChristoph Hellwig } 87461891923SStefan Behrens return ret; 8750b86a832SChris Mason } 8760b86a832SChris Mason 87718fdc679SLiu Bo static int check_async_write(struct btrfs_inode *bi) 878de0022b9SJosef Bacik { 8796300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8806300463bSLiu Bo return 0; 881de0022b9SJosef Bacik #ifdef CONFIG_X86 882bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 883de0022b9SJosef Bacik return 0; 884de0022b9SJosef Bacik #endif 885de0022b9SJosef Bacik return 1; 886de0022b9SJosef Bacik } 887de0022b9SJosef Bacik 8888c27cb35SLinus Torvalds static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio, 889eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 890eaf25d93SChris Mason u64 bio_offset) 89144b8bd7eSChris Mason { 892c6100a4bSJosef Bacik struct inode *inode = private_data; 8930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 89418fdc679SLiu Bo int async = check_async_write(BTRFS_I(inode)); 8954e4cbee9SChristoph Hellwig blk_status_t ret; 896cad321adSChris Mason 89737226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 898cad321adSChris Mason /* 899cad321adSChris Mason * called for a read, do the setup so that checksum validation 900cad321adSChris Mason * can happen in the async kernel threads 901cad321adSChris Mason */ 9020b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 9030b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 9041d4284bdSChris Mason if (ret) 90561891923SStefan Behrens goto out_w_error; 9062ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 907de0022b9SJosef Bacik } else if (!async) { 908de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 909de0022b9SJosef Bacik if (ret) 91061891923SStefan Behrens goto out_w_error; 9112ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 91261891923SStefan Behrens } else { 913cad321adSChris Mason /* 91461891923SStefan Behrens * kthread helpers are used to submit writes so that 91561891923SStefan Behrens * checksumming can happen in parallel across all CPUs 916cad321adSChris Mason */ 917c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 918c6100a4bSJosef Bacik bio_offset, private_data, 919d0ee3934SDavid Sterba btree_submit_bio_start, 920d0ee3934SDavid Sterba btree_submit_bio_done); 92144b8bd7eSChris Mason } 92244b8bd7eSChris Mason 9234246a0b6SChristoph Hellwig if (ret) 9244246a0b6SChristoph Hellwig goto out_w_error; 9254246a0b6SChristoph Hellwig return 0; 9264246a0b6SChristoph Hellwig 92761891923SStefan Behrens out_w_error: 9284e4cbee9SChristoph Hellwig bio->bi_status = ret; 9294246a0b6SChristoph Hellwig bio_endio(bio); 93061891923SStefan Behrens return ret; 93161891923SStefan Behrens } 93261891923SStefan Behrens 9333dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 934784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 935a6bc32b8SMel Gorman struct page *newpage, struct page *page, 936a6bc32b8SMel Gorman enum migrate_mode mode) 937784b4e29SChris Mason { 938784b4e29SChris Mason /* 939784b4e29SChris Mason * we can't safely write a btree page from here, 940784b4e29SChris Mason * we haven't done the locking hook 941784b4e29SChris Mason */ 942784b4e29SChris Mason if (PageDirty(page)) 943784b4e29SChris Mason return -EAGAIN; 944784b4e29SChris Mason /* 945784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 946784b4e29SChris Mason * We must have no buffers or drop them. 947784b4e29SChris Mason */ 948784b4e29SChris Mason if (page_has_private(page) && 949784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 950784b4e29SChris Mason return -EAGAIN; 951a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 952784b4e29SChris Mason } 9533dd1462eSJan Beulich #endif 954784b4e29SChris Mason 9550da5468fSChris Mason 9560da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9570da5468fSChris Mason struct writeback_control *wbc) 9580da5468fSChris Mason { 959e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 960e2d84521SMiao Xie int ret; 961e2d84521SMiao Xie 962d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 963448d640bSChris Mason 964448d640bSChris Mason if (wbc->for_kupdate) 965448d640bSChris Mason return 0; 966448d640bSChris Mason 967e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 968b9473439SChris Mason /* this is a bit racy, but that's ok */ 969e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 970e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 971e2d84521SMiao Xie if (ret < 0) 972793955bcSChris Mason return 0; 973793955bcSChris Mason } 9740b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9750da5468fSChris Mason } 9760da5468fSChris Mason 977b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9785f39d397SChris Mason { 979d1310b2eSChris Mason struct extent_io_tree *tree; 980d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9818ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9825f39d397SChris Mason } 9835f39d397SChris Mason 98470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9855f39d397SChris Mason { 98698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 98798509cfcSChris Mason return 0; 9880c4e538bSDavid Sterba 989f7a52a40SDavid Sterba return try_release_extent_buffer(page); 990d98237b3SChris Mason } 991d98237b3SChris Mason 992d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 993d47992f8SLukas Czerner unsigned int length) 994d98237b3SChris Mason { 995d1310b2eSChris Mason struct extent_io_tree *tree; 996d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9975f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9985f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9999ad6b7bcSChris Mason if (PagePrivate(page)) { 1000efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1001efe120a0SFrank Holton "page private not zero on page %llu", 1002efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10039ad6b7bcSChris Mason ClearPagePrivate(page); 10049ad6b7bcSChris Mason set_page_private(page, 0); 100509cbfeafSKirill A. Shutemov put_page(page); 10069ad6b7bcSChris Mason } 1007d98237b3SChris Mason } 1008d98237b3SChris Mason 10090b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10100b32f4bbSJosef Bacik { 1011bb146eb2SJosef Bacik #ifdef DEBUG 10120b32f4bbSJosef Bacik struct extent_buffer *eb; 10130b32f4bbSJosef Bacik 10140b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10150b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10160b32f4bbSJosef Bacik BUG_ON(!eb); 10170b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10180b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10190b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1020bb146eb2SJosef Bacik #endif 10210b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10220b32f4bbSJosef Bacik } 10230b32f4bbSJosef Bacik 10247f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1025d98237b3SChris Mason .readpage = btree_readpage, 10260da5468fSChris Mason .writepages = btree_writepages, 10275f39d397SChris Mason .releasepage = btree_releasepage, 10285f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10295a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1030784b4e29SChris Mason .migratepage = btree_migratepage, 10315a92bc88SChris Mason #endif 10320b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1033d98237b3SChris Mason }; 1034123abc88SChris Mason 10352ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1036090d1875SChris Mason { 10375f39d397SChris Mason struct extent_buffer *buf = NULL; 10382ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1039090d1875SChris Mason 10402ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1041c871b0f2SLiu Bo if (IS_ERR(buf)) 10426197d86eSDavid Sterba return; 1043d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 10446af49dbdSDavid Sterba buf, WAIT_NONE, 0); 10455f39d397SChris Mason free_extent_buffer(buf); 1046090d1875SChris Mason } 1047090d1875SChris Mason 10482ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1049ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1050ab0fff03SArne Jansen { 1051ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 10522ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1053ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1054ab0fff03SArne Jansen int ret; 1055ab0fff03SArne Jansen 10562ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1057c871b0f2SLiu Bo if (IS_ERR(buf)) 1058ab0fff03SArne Jansen return 0; 1059ab0fff03SArne Jansen 1060ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1061ab0fff03SArne Jansen 10628436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 10636af49dbdSDavid Sterba mirror_num); 1064ab0fff03SArne Jansen if (ret) { 1065ab0fff03SArne Jansen free_extent_buffer(buf); 1066ab0fff03SArne Jansen return ret; 1067ab0fff03SArne Jansen } 1068ab0fff03SArne Jansen 1069ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1070ab0fff03SArne Jansen free_extent_buffer(buf); 1071ab0fff03SArne Jansen return -EIO; 10720b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1073ab0fff03SArne Jansen *eb = buf; 1074ab0fff03SArne Jansen } else { 1075ab0fff03SArne Jansen free_extent_buffer(buf); 1076ab0fff03SArne Jansen } 1077ab0fff03SArne Jansen return 0; 1078ab0fff03SArne Jansen } 1079ab0fff03SArne Jansen 10802ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10812ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10820308af44SDavid Sterba u64 bytenr) 10830999df54SChris Mason { 10840b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10850b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10860b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10870999df54SChris Mason } 10880999df54SChris Mason 10890999df54SChris Mason 1090e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1091e02119d5SChris Mason { 1092727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 10938aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1094e02119d5SChris Mason } 1095e02119d5SChris Mason 10963189ff77SJeff Layton void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1097e02119d5SChris Mason { 10983189ff77SJeff Layton filemap_fdatawait_range(buf->pages[0]->mapping, 10998aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1100e02119d5SChris Mason } 1101e02119d5SChris Mason 1102581c1760SQu Wenruo /* 1103581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical 1104581c1760SQu Wenruo * verification. 1105581c1760SQu Wenruo * 1106581c1760SQu Wenruo * @parent_transid: expected transid of this tree block, skip check if 0 1107581c1760SQu Wenruo * @level: expected level, mandatory check 1108581c1760SQu Wenruo * @first_key: expected key in slot 0, skip check if NULL 1109581c1760SQu Wenruo */ 11102ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1111581c1760SQu Wenruo u64 parent_transid, int level, 1112581c1760SQu Wenruo struct btrfs_key *first_key) 1113e20d96d6SChris Mason { 11145f39d397SChris Mason struct extent_buffer *buf = NULL; 111519c00ddcSChris Mason int ret; 111619c00ddcSChris Mason 11172ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1118c871b0f2SLiu Bo if (IS_ERR(buf)) 1119c871b0f2SLiu Bo return buf; 1120e4204dedSChris Mason 1121581c1760SQu Wenruo ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid, 1122581c1760SQu Wenruo level, first_key); 11230f0fe8f7SFilipe David Borba Manana if (ret) { 11240f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 112564c043deSLiu Bo return ERR_PTR(ret); 11260f0fe8f7SFilipe David Borba Manana } 11275f39d397SChris Mason return buf; 1128ce9adaa5SChris Mason 1129eb60ceacSChris Mason } 1130eb60ceacSChris Mason 11317c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 11325f39d397SChris Mason struct extent_buffer *buf) 1133ed2ff2cbSChris Mason { 113455c69072SChris Mason if (btrfs_header_generation(buf) == 1135e2d84521SMiao Xie fs_info->running_transaction->transid) { 1136b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1137b4ce94deSChris Mason 1138b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1139104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1140e2d84521SMiao Xie -buf->len, 1141e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1142b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1143b9473439SChris Mason btrfs_set_lock_blocking(buf); 11440b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1145925baeddSChris Mason } 11465f39d397SChris Mason } 1147ed7b63ebSJosef Bacik } 11485f39d397SChris Mason 11498257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11508257b2dcSMiao Xie { 11518257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11528257b2dcSMiao Xie int ret; 11538257b2dcSMiao Xie 11548257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11558257b2dcSMiao Xie if (!writers) 11568257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11578257b2dcSMiao Xie 11588a5a916dSJeff Mahoney ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS); 11598257b2dcSMiao Xie if (ret < 0) { 11608257b2dcSMiao Xie kfree(writers); 11618257b2dcSMiao Xie return ERR_PTR(ret); 11628257b2dcSMiao Xie } 11638257b2dcSMiao Xie 11648257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11658257b2dcSMiao Xie return writers; 11668257b2dcSMiao Xie } 11678257b2dcSMiao Xie 11688257b2dcSMiao Xie static void 11698257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11708257b2dcSMiao Xie { 11718257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11728257b2dcSMiao Xie kfree(writers); 11738257b2dcSMiao Xie } 11748257b2dcSMiao Xie 1175da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1176e20d96d6SChris Mason u64 objectid) 1177d97e63b6SChris Mason { 11787c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1179cfaa7295SChris Mason root->node = NULL; 1180a28ec197SChris Mason root->commit_root = NULL; 118127cdeb70SMiao Xie root->state = 0; 1182d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11830b86a832SChris Mason 11840f7d52f4SChris Mason root->objectid = objectid; 11850f7d52f4SChris Mason root->last_trans = 0; 118613a8a7c8SYan, Zheng root->highest_objectid = 0; 1187eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1188199c2a9cSMiao Xie root->nr_ordered_extents = 0; 118958176a96SJosef Bacik root->name = NULL; 11906bef4d31SEric Paris root->inode_tree = RB_ROOT; 119116cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1192f0486c68SYan, Zheng root->block_rsv = NULL; 1193d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11940b86a832SChris Mason 11950b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11965d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1197eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1198eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1199199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1200199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 12012ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 12022ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1203d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 12045d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1205eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1206199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1207f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 12082ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 12092ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 12108287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock); 1211a2135011SChris Mason mutex_init(&root->objectid_mutex); 1212e02119d5SChris Mason mutex_init(&root->log_mutex); 121331f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1214573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 12157237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12167237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12177237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12188b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 12198b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 12207237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12217237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12227237f183SYan Zheng atomic_set(&root->log_writers, 0); 12232ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12248a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 12250700cea7SElena Reshetova refcount_set(&root->refs, 1); 1226ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 12277237f183SYan Zheng root->log_transid = 0; 1228d1433debSMiao Xie root->log_transid_committed = -1; 1229257c62e1SChris Mason root->last_log_commit = 0; 12307c0260eeSJeff Mahoney if (!dummy) 1231c6100a4bSJosef Bacik extent_io_tree_init(&root->dirty_log_pages, NULL); 1232017e5369SChris Mason 12333768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12343768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12356702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 12367c0260eeSJeff Mahoney if (!dummy) 12373f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 123806ea65a3SJosef Bacik else 123906ea65a3SJosef Bacik root->defrag_trans_start = 0; 12404d775673SChris Mason root->root_key.objectid = objectid; 12410ee5dc67SAl Viro root->anon_dev = 0; 12428ea05e3aSAlexander Block 12435f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12443768f368SChris Mason } 12453768f368SChris Mason 124674e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 124774e4d827SDavid Sterba gfp_t flags) 12486f07e42eSAl Viro { 124974e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12506f07e42eSAl Viro if (root) 12516f07e42eSAl Viro root->fs_info = fs_info; 12526f07e42eSAl Viro return root; 12536f07e42eSAl Viro } 12546f07e42eSAl Viro 125506ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 125606ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1257da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 125806ea65a3SJosef Bacik { 125906ea65a3SJosef Bacik struct btrfs_root *root; 126006ea65a3SJosef Bacik 12617c0260eeSJeff Mahoney if (!fs_info) 12627c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12637c0260eeSJeff Mahoney 12647c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 126506ea65a3SJosef Bacik if (!root) 126606ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1267da17066cSJeff Mahoney 1268b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1269da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1270faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 127106ea65a3SJosef Bacik 127206ea65a3SJosef Bacik return root; 127306ea65a3SJosef Bacik } 127406ea65a3SJosef Bacik #endif 127506ea65a3SJosef Bacik 127620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 127720897f5cSArne Jansen struct btrfs_fs_info *fs_info, 127820897f5cSArne Jansen u64 objectid) 127920897f5cSArne Jansen { 128020897f5cSArne Jansen struct extent_buffer *leaf; 128120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 128220897f5cSArne Jansen struct btrfs_root *root; 128320897f5cSArne Jansen struct btrfs_key key; 128420897f5cSArne Jansen int ret = 0; 128533d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 128620897f5cSArne Jansen 128774e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 128820897f5cSArne Jansen if (!root) 128920897f5cSArne Jansen return ERR_PTR(-ENOMEM); 129020897f5cSArne Jansen 1291da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 129220897f5cSArne Jansen root->root_key.objectid = objectid; 129320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 129420897f5cSArne Jansen root->root_key.offset = 0; 129520897f5cSArne Jansen 12964d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 129720897f5cSArne Jansen if (IS_ERR(leaf)) { 129820897f5cSArne Jansen ret = PTR_ERR(leaf); 12991dd05682STsutomu Itoh leaf = NULL; 130020897f5cSArne Jansen goto fail; 130120897f5cSArne Jansen } 130220897f5cSArne Jansen 1303b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 130420897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 130520897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 130620897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 130720897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 130820897f5cSArne Jansen root->node = leaf; 130920897f5cSArne Jansen 1310d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1311d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 131220897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 131320897f5cSArne Jansen 131420897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 131527cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 131620897f5cSArne Jansen 131720897f5cSArne Jansen root->root_item.flags = 0; 131820897f5cSArne Jansen root->root_item.byte_limit = 0; 131920897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 132020897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 132120897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 132220897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 132320897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 132420897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 132520897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 132633d85fdaSQu Wenruo if (is_fstree(objectid)) 13276463fe58SStefan Behrens uuid_le_gen(&uuid); 13286463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 132920897f5cSArne Jansen root->root_item.drop_level = 0; 133020897f5cSArne Jansen 133120897f5cSArne Jansen key.objectid = objectid; 133220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 133320897f5cSArne Jansen key.offset = 0; 133420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 133520897f5cSArne Jansen if (ret) 133620897f5cSArne Jansen goto fail; 133720897f5cSArne Jansen 133820897f5cSArne Jansen btrfs_tree_unlock(leaf); 133920897f5cSArne Jansen 134020897f5cSArne Jansen return root; 13411dd05682STsutomu Itoh 13421dd05682STsutomu Itoh fail: 13431dd05682STsutomu Itoh if (leaf) { 13441dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 134559885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13461dd05682STsutomu Itoh free_extent_buffer(leaf); 13471dd05682STsutomu Itoh } 13481dd05682STsutomu Itoh kfree(root); 13491dd05682STsutomu Itoh 13501dd05682STsutomu Itoh return ERR_PTR(ret); 135120897f5cSArne Jansen } 135220897f5cSArne Jansen 13537237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1354e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13550f7d52f4SChris Mason { 13560f7d52f4SChris Mason struct btrfs_root *root; 13577237f183SYan Zheng struct extent_buffer *leaf; 1358e02119d5SChris Mason 135974e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1360e02119d5SChris Mason if (!root) 13617237f183SYan Zheng return ERR_PTR(-ENOMEM); 1362e02119d5SChris Mason 1363da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1364e02119d5SChris Mason 1365e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1366e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1367e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 136827cdeb70SMiao Xie 13697237f183SYan Zheng /* 137027cdeb70SMiao Xie * DON'T set REF_COWS for log trees 137127cdeb70SMiao Xie * 13727237f183SYan Zheng * log trees do not get reference counted because they go away 13737237f183SYan Zheng * before a real commit is actually done. They do store pointers 13747237f183SYan Zheng * to file data extents, and those reference counts still get 13757237f183SYan Zheng * updated (along with back refs to the log tree). 13767237f183SYan Zheng */ 1377e02119d5SChris Mason 13784d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13794d75f8a9SDavid Sterba NULL, 0, 0, 0); 13807237f183SYan Zheng if (IS_ERR(leaf)) { 13817237f183SYan Zheng kfree(root); 13827237f183SYan Zheng return ERR_CAST(leaf); 13837237f183SYan Zheng } 1384e02119d5SChris Mason 1385b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 13865d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13875d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13885d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13895d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13907237f183SYan Zheng root->node = leaf; 1391e02119d5SChris Mason 13920b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1393e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1394e02119d5SChris Mason btrfs_tree_unlock(root->node); 13957237f183SYan Zheng return root; 13967237f183SYan Zheng } 13977237f183SYan Zheng 13987237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13997237f183SYan Zheng struct btrfs_fs_info *fs_info) 14007237f183SYan Zheng { 14017237f183SYan Zheng struct btrfs_root *log_root; 14027237f183SYan Zheng 14037237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14047237f183SYan Zheng if (IS_ERR(log_root)) 14057237f183SYan Zheng return PTR_ERR(log_root); 14067237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14077237f183SYan Zheng fs_info->log_root_tree = log_root; 14087237f183SYan Zheng return 0; 14097237f183SYan Zheng } 14107237f183SYan Zheng 14117237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14127237f183SYan Zheng struct btrfs_root *root) 14137237f183SYan Zheng { 14140b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14157237f183SYan Zheng struct btrfs_root *log_root; 14167237f183SYan Zheng struct btrfs_inode_item *inode_item; 14177237f183SYan Zheng 14180b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 14197237f183SYan Zheng if (IS_ERR(log_root)) 14207237f183SYan Zheng return PTR_ERR(log_root); 14217237f183SYan Zheng 14227237f183SYan Zheng log_root->last_trans = trans->transid; 14237237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14247237f183SYan Zheng 14257237f183SYan Zheng inode_item = &log_root->root_item.inode; 14263cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14273cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14283cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1429da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 14300b246afaSJeff Mahoney fs_info->nodesize); 14313cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14327237f183SYan Zheng 14335d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14347237f183SYan Zheng 14357237f183SYan Zheng WARN_ON(root->log_root); 14367237f183SYan Zheng root->log_root = log_root; 14377237f183SYan Zheng root->log_transid = 0; 1438d1433debSMiao Xie root->log_transid_committed = -1; 1439257c62e1SChris Mason root->last_log_commit = 0; 1440e02119d5SChris Mason return 0; 1441e02119d5SChris Mason } 1442e02119d5SChris Mason 144335a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1444cb517eabSMiao Xie struct btrfs_key *key) 1445e02119d5SChris Mason { 1446e02119d5SChris Mason struct btrfs_root *root; 1447e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14480f7d52f4SChris Mason struct btrfs_path *path; 144984234f3aSYan Zheng u64 generation; 1450cb517eabSMiao Xie int ret; 1451581c1760SQu Wenruo int level; 1452cb517eabSMiao Xie 1453cb517eabSMiao Xie path = btrfs_alloc_path(); 1454cb517eabSMiao Xie if (!path) 1455cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14560f7d52f4SChris Mason 145774e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1458cb517eabSMiao Xie if (!root) { 1459cb517eabSMiao Xie ret = -ENOMEM; 1460cb517eabSMiao Xie goto alloc_fail; 14610f7d52f4SChris Mason } 14620f7d52f4SChris Mason 1463da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 14640f7d52f4SChris Mason 1465cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1466cb517eabSMiao Xie &root->root_item, &root->root_key); 14670f7d52f4SChris Mason if (ret) { 146813a8a7c8SYan, Zheng if (ret > 0) 146913a8a7c8SYan, Zheng ret = -ENOENT; 1470cb517eabSMiao Xie goto find_fail; 14710f7d52f4SChris Mason } 147213a8a7c8SYan, Zheng 147384234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1474581c1760SQu Wenruo level = btrfs_root_level(&root->root_item); 14752ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14762ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1477581c1760SQu Wenruo generation, level, NULL); 147864c043deSLiu Bo if (IS_ERR(root->node)) { 147964c043deSLiu Bo ret = PTR_ERR(root->node); 1480cb517eabSMiao Xie goto find_fail; 1481cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1482cb517eabSMiao Xie ret = -EIO; 148364c043deSLiu Bo free_extent_buffer(root->node); 148464c043deSLiu Bo goto find_fail; 1485416bc658SJosef Bacik } 14865d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 148713a8a7c8SYan, Zheng out: 1488cb517eabSMiao Xie btrfs_free_path(path); 1489cb517eabSMiao Xie return root; 1490cb517eabSMiao Xie 1491cb517eabSMiao Xie find_fail: 1492cb517eabSMiao Xie kfree(root); 1493cb517eabSMiao Xie alloc_fail: 1494cb517eabSMiao Xie root = ERR_PTR(ret); 1495cb517eabSMiao Xie goto out; 1496cb517eabSMiao Xie } 1497cb517eabSMiao Xie 1498cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1499cb517eabSMiao Xie struct btrfs_key *location) 1500cb517eabSMiao Xie { 1501cb517eabSMiao Xie struct btrfs_root *root; 1502cb517eabSMiao Xie 1503cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1504cb517eabSMiao Xie if (IS_ERR(root)) 1505cb517eabSMiao Xie return root; 1506cb517eabSMiao Xie 1507cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 150827cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 150908fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 151008fe4db1SLi Zefan } 151113a8a7c8SYan, Zheng 15125eda7b5eSChris Mason return root; 15135eda7b5eSChris Mason } 15145eda7b5eSChris Mason 1515cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1516cb517eabSMiao Xie { 1517cb517eabSMiao Xie int ret; 15188257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1519cb517eabSMiao Xie 1520cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1521cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1522cb517eabSMiao Xie GFP_NOFS); 1523cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1524cb517eabSMiao Xie ret = -ENOMEM; 1525cb517eabSMiao Xie goto fail; 1526cb517eabSMiao Xie } 1527cb517eabSMiao Xie 15288257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 15298257b2dcSMiao Xie if (IS_ERR(writers)) { 15308257b2dcSMiao Xie ret = PTR_ERR(writers); 15318257b2dcSMiao Xie goto fail; 15328257b2dcSMiao Xie } 15338257b2dcSMiao Xie root->subv_writers = writers; 15348257b2dcSMiao Xie 1535cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 153657cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 153757cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1538cb517eabSMiao Xie 1539cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1540cb517eabSMiao Xie if (ret) 1541876d2cf1SLiu Bo goto fail; 1542f32e48e9SChandan Rajendra 1543f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1544f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1545f32e48e9SChandan Rajendra &root->highest_objectid); 1546f32e48e9SChandan Rajendra if (ret) { 1547f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1548876d2cf1SLiu Bo goto fail; 1549f32e48e9SChandan Rajendra } 1550f32e48e9SChandan Rajendra 1551f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1552f32e48e9SChandan Rajendra 1553f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1554f32e48e9SChandan Rajendra 1555cb517eabSMiao Xie return 0; 1556cb517eabSMiao Xie fail: 1557876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1558cb517eabSMiao Xie return ret; 1559cb517eabSMiao Xie } 1560cb517eabSMiao Xie 156135bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1562cb517eabSMiao Xie u64 root_id) 1563cb517eabSMiao Xie { 1564cb517eabSMiao Xie struct btrfs_root *root; 1565cb517eabSMiao Xie 1566cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1567cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1568cb517eabSMiao Xie (unsigned long)root_id); 1569cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1570cb517eabSMiao Xie return root; 1571cb517eabSMiao Xie } 1572cb517eabSMiao Xie 1573cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1574cb517eabSMiao Xie struct btrfs_root *root) 1575cb517eabSMiao Xie { 1576cb517eabSMiao Xie int ret; 1577cb517eabSMiao Xie 1578e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1579cb517eabSMiao Xie if (ret) 1580cb517eabSMiao Xie return ret; 1581cb517eabSMiao Xie 1582cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1583cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1584cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1585cb517eabSMiao Xie root); 1586cb517eabSMiao Xie if (ret == 0) 158727cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1588cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1589cb517eabSMiao Xie radix_tree_preload_end(); 1590cb517eabSMiao Xie 1591cb517eabSMiao Xie return ret; 1592cb517eabSMiao Xie } 1593cb517eabSMiao Xie 1594c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1595c00869f1SMiao Xie struct btrfs_key *location, 1596c00869f1SMiao Xie bool check_ref) 15975eda7b5eSChris Mason { 15985eda7b5eSChris Mason struct btrfs_root *root; 1599381cf658SDavid Sterba struct btrfs_path *path; 16001d4c08e0SDavid Sterba struct btrfs_key key; 16015eda7b5eSChris Mason int ret; 16025eda7b5eSChris Mason 1603edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1604edbd8d4eSChris Mason return fs_info->tree_root; 1605edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1606edbd8d4eSChris Mason return fs_info->extent_root; 16078f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 16088f18cf13SChris Mason return fs_info->chunk_root; 16098f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 16108f18cf13SChris Mason return fs_info->dev_root; 16110403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 16120403e47eSYan Zheng return fs_info->csum_root; 1613bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1614bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1615bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1616f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1617f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1618f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 161970f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 162070f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 162170f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 16224df27c4dSYan, Zheng again: 1623cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 162448475471SStefan Behrens if (root) { 1625c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 162648475471SStefan Behrens return ERR_PTR(-ENOENT); 16275eda7b5eSChris Mason return root; 162848475471SStefan Behrens } 16295eda7b5eSChris Mason 1630cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 16315eda7b5eSChris Mason if (IS_ERR(root)) 16325eda7b5eSChris Mason return root; 16333394e160SChris Mason 1634c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1635d68fc57bSYan, Zheng ret = -ENOENT; 1636d68fc57bSYan, Zheng goto fail; 1637d68fc57bSYan, Zheng } 1638d68fc57bSYan, Zheng 1639cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1640cb517eabSMiao Xie if (ret) 1641cb517eabSMiao Xie goto fail; 1642cb517eabSMiao Xie 1643381cf658SDavid Sterba path = btrfs_alloc_path(); 1644381cf658SDavid Sterba if (!path) { 1645381cf658SDavid Sterba ret = -ENOMEM; 1646381cf658SDavid Sterba goto fail; 1647381cf658SDavid Sterba } 16481d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16491d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16501d4c08e0SDavid Sterba key.offset = location->objectid; 16511d4c08e0SDavid Sterba 16521d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1653381cf658SDavid Sterba btrfs_free_path(path); 1654d68fc57bSYan, Zheng if (ret < 0) 1655d68fc57bSYan, Zheng goto fail; 1656d68fc57bSYan, Zheng if (ret == 0) 165727cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1658d68fc57bSYan, Zheng 1659cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16600f7d52f4SChris Mason if (ret) { 16614df27c4dSYan, Zheng if (ret == -EEXIST) { 16624df27c4dSYan, Zheng free_fs_root(root); 16634df27c4dSYan, Zheng goto again; 16640f7d52f4SChris Mason } 16654df27c4dSYan, Zheng goto fail; 16664df27c4dSYan, Zheng } 1667edbd8d4eSChris Mason return root; 16684df27c4dSYan, Zheng fail: 16694df27c4dSYan, Zheng free_fs_root(root); 16704df27c4dSYan, Zheng return ERR_PTR(ret); 1671edbd8d4eSChris Mason } 1672edbd8d4eSChris Mason 167304160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 167404160088SChris Mason { 167504160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 167604160088SChris Mason int ret = 0; 167704160088SChris Mason struct btrfs_device *device; 167804160088SChris Mason struct backing_dev_info *bdi; 1679b7967db7SChris Mason 16801f78160cSXiao Guangrong rcu_read_lock(); 16811f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1682dfe25020SChris Mason if (!device->bdev) 1683dfe25020SChris Mason continue; 1684efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1685ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 168604160088SChris Mason ret = 1; 168704160088SChris Mason break; 168804160088SChris Mason } 168904160088SChris Mason } 16901f78160cSXiao Guangrong rcu_read_unlock(); 169104160088SChris Mason return ret; 169204160088SChris Mason } 169304160088SChris Mason 16948b712842SChris Mason /* 16958b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16968b712842SChris Mason * functions. This is where read checksum verification actually happens 16978b712842SChris Mason */ 16988b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1699ce9adaa5SChris Mason { 1700ce9adaa5SChris Mason struct bio *bio; 170197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1702ce9adaa5SChris Mason 170397eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1704ce9adaa5SChris Mason bio = end_io_wq->bio; 17058b712842SChris Mason 17064e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1707ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1708ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 170997eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 17104246a0b6SChristoph Hellwig bio_endio(bio); 1711ce9adaa5SChris Mason } 171244b8bd7eSChris Mason 1713a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1714a74a4b97SChris Mason { 1715a74a4b97SChris Mason struct btrfs_root *root = arg; 17160b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1717d0278245SMiao Xie int again; 1718da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1719a74a4b97SChris Mason 1720a74a4b97SChris Mason do { 1721d0278245SMiao Xie again = 0; 17229d1a2a3aSDavid Sterba 1723d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 17242ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1725d0278245SMiao Xie goto sleep; 1726d0278245SMiao Xie 172790c711abSZygo Blaxell /* 172890c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 172990c711abSZygo Blaxell * before we have finished mounting the filesystem. 173090c711abSZygo Blaxell */ 17310b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 173290c711abSZygo Blaxell goto sleep; 173390c711abSZygo Blaxell 17340b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1735d0278245SMiao Xie goto sleep; 1736d0278245SMiao Xie 1737dc7f370cSMiao Xie /* 1738dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1739dc7f370cSMiao Xie * during the above check and trylock. 1740dc7f370cSMiao Xie */ 17412ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17420b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1743dc7f370cSMiao Xie goto sleep; 1744dc7f370cSMiao Xie } 1745dc7f370cSMiao Xie 17460b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 17472ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 17480b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1749c2d6cb16SFilipe Manana 17509d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17510b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1752a74a4b97SChris Mason 1753d0278245SMiao Xie /* 175405323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 175505323cd1SMiao Xie * needn't do anything special here. 1756d0278245SMiao Xie */ 17570b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 175867c5e7d4SFilipe Manana 175967c5e7d4SFilipe Manana /* 176067c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 176167c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 176267c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 176367c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 176467c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 176567c5e7d4SFilipe Manana * unused block groups. 176667c5e7d4SFilipe Manana */ 17670b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1768d0278245SMiao Xie sleep: 1769838fe188SJiri Kosina if (!again) { 1770a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17718929ecfaSYan, Zheng if (!kthread_should_stop()) 1772a74a4b97SChris Mason schedule(); 1773a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1774a74a4b97SChris Mason } 1775a74a4b97SChris Mason } while (!kthread_should_stop()); 1776da288d28SFilipe Manana 1777da288d28SFilipe Manana /* 1778da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1779da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1780da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1781da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1782da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1783da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1784da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1785da288d28SFilipe Manana * since work queues and other resources were already released and 1786da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1787da288d28SFilipe Manana */ 1788da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1789da288d28SFilipe Manana if (IS_ERR(trans)) { 1790da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 17910b246afaSJeff Mahoney btrfs_err(fs_info, 1792da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1793da288d28SFilipe Manana PTR_ERR(trans)); 1794da288d28SFilipe Manana } else { 1795da288d28SFilipe Manana int ret; 1796da288d28SFilipe Manana 17973a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1798da288d28SFilipe Manana if (ret) 17990b246afaSJeff Mahoney btrfs_err(fs_info, 1800da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1801da288d28SFilipe Manana ret); 1802da288d28SFilipe Manana } 1803da288d28SFilipe Manana 1804a74a4b97SChris Mason return 0; 1805a74a4b97SChris Mason } 1806a74a4b97SChris Mason 1807a74a4b97SChris Mason static int transaction_kthread(void *arg) 1808a74a4b97SChris Mason { 1809a74a4b97SChris Mason struct btrfs_root *root = arg; 18100b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1811a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1812a74a4b97SChris Mason struct btrfs_transaction *cur; 18138929ecfaSYan, Zheng u64 transid; 1814a74a4b97SChris Mason unsigned long now; 1815a74a4b97SChris Mason unsigned long delay; 1816914b2007SJan Kara bool cannot_commit; 1817a74a4b97SChris Mason 1818a74a4b97SChris Mason do { 1819914b2007SJan Kara cannot_commit = false; 18200b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 18210b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1822a74a4b97SChris Mason 18230b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18240b246afaSJeff Mahoney cur = fs_info->running_transaction; 1825a74a4b97SChris Mason if (!cur) { 18260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1827a74a4b97SChris Mason goto sleep; 1828a74a4b97SChris Mason } 182931153d81SYan Zheng 1830a74a4b97SChris Mason now = get_seconds(); 18314a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 18328b87dc17SDavid Sterba (now < cur->start_time || 18330b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 18340b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1835a74a4b97SChris Mason delay = HZ * 5; 1836a74a4b97SChris Mason goto sleep; 1837a74a4b97SChris Mason } 18388929ecfaSYan, Zheng transid = cur->transid; 18390b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 184056bec294SChris Mason 184179787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1842354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1843914b2007SJan Kara if (IS_ERR(trans)) { 1844354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1845914b2007SJan Kara cannot_commit = true; 184679787eaaSJeff Mahoney goto sleep; 1847914b2007SJan Kara } 18488929ecfaSYan, Zheng if (transid == trans->transid) { 18493a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18508929ecfaSYan, Zheng } else { 18513a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18528929ecfaSYan, Zheng } 1853a74a4b97SChris Mason sleep: 18540b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18550b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1856a74a4b97SChris Mason 18574e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18580b246afaSJeff Mahoney &fs_info->fs_state))) 18592ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 18608929ecfaSYan, Zheng if (!kthread_should_stop() && 18610b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1862914b2007SJan Kara cannot_commit)) 1863bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay); 1864a74a4b97SChris Mason } while (!kthread_should_stop()); 1865a74a4b97SChris Mason return 0; 1866a74a4b97SChris Mason } 1867a74a4b97SChris Mason 1868af31f5e5SChris Mason /* 1869af31f5e5SChris Mason * this will find the highest generation in the array of 1870af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1871af31f5e5SChris Mason * or -1 if we can't find anything. 1872af31f5e5SChris Mason * 1873af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1874af31f5e5SChris Mason * generation of the latest root in the array with the generation 1875af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1876af31f5e5SChris Mason */ 1877af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1878af31f5e5SChris Mason { 1879af31f5e5SChris Mason u64 cur; 1880af31f5e5SChris Mason int newest_index = -1; 1881af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1882af31f5e5SChris Mason int i; 1883af31f5e5SChris Mason 1884af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1885af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1886af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1887af31f5e5SChris Mason if (cur == newest_gen) 1888af31f5e5SChris Mason newest_index = i; 1889af31f5e5SChris Mason } 1890af31f5e5SChris Mason 1891af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1892af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1893af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1894af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1895af31f5e5SChris Mason if (cur == newest_gen) 1896af31f5e5SChris Mason newest_index = 0; 1897af31f5e5SChris Mason } 1898af31f5e5SChris Mason return newest_index; 1899af31f5e5SChris Mason } 1900af31f5e5SChris Mason 1901af31f5e5SChris Mason 1902af31f5e5SChris Mason /* 1903af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1904af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1905af31f5e5SChris Mason * field in the fs_info struct 1906af31f5e5SChris Mason */ 1907af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1908af31f5e5SChris Mason u64 newest_gen) 1909af31f5e5SChris Mason { 1910af31f5e5SChris Mason int newest_index = -1; 1911af31f5e5SChris Mason 1912af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1913af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1914af31f5e5SChris Mason if (newest_index == -1) { 1915af31f5e5SChris Mason info->backup_root_index = 0; 1916af31f5e5SChris Mason } else { 1917af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1918af31f5e5SChris Mason } 1919af31f5e5SChris Mason } 1920af31f5e5SChris Mason 1921af31f5e5SChris Mason /* 1922af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1923af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1924af31f5e5SChris Mason * done 1925af31f5e5SChris Mason */ 1926af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1927af31f5e5SChris Mason { 1928af31f5e5SChris Mason int next_backup; 1929af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1930af31f5e5SChris Mason int last_backup; 1931af31f5e5SChris Mason 1932af31f5e5SChris Mason next_backup = info->backup_root_index; 1933af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1934af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1935af31f5e5SChris Mason 1936af31f5e5SChris Mason /* 1937af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1938af31f5e5SChris Mason * this happens only at umount 1939af31f5e5SChris Mason */ 1940af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1941af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1942af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1943af31f5e5SChris Mason next_backup = last_backup; 1944af31f5e5SChris Mason 1945af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason /* 1948af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1949af31f5e5SChris Mason * regardless of which ones we use today 1950af31f5e5SChris Mason */ 1951af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1952af31f5e5SChris Mason 1953af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1954af31f5e5SChris Mason 1955af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1956af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1957af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1960af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1961af31f5e5SChris Mason 1962af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1963af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1964af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1965af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1966af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1967af31f5e5SChris Mason 1968af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1969af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1970af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1971af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1972af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1973af31f5e5SChris Mason 19747c7e82a7SChris Mason /* 19757c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19767c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19777c7e82a7SChris Mason */ 19787c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19797c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19807c7e82a7SChris Mason info->fs_root->node->start); 1981af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1982af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1983af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1984af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19857c7e82a7SChris Mason } 1986af31f5e5SChris Mason 1987af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1988af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1989af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1990af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1991af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1992af31f5e5SChris Mason 1993af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1994af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1995af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1996af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1997af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1998af31f5e5SChris Mason 1999af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2000af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2001af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2002af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2003af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2004af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2005af31f5e5SChris Mason 2006af31f5e5SChris Mason /* 2007af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2008af31f5e5SChris Mason * for the next commit 2009af31f5e5SChris Mason */ 2010af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2011af31f5e5SChris Mason &info->super_for_commit->super_roots, 2012af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2013af31f5e5SChris Mason } 2014af31f5e5SChris Mason 2015af31f5e5SChris Mason /* 2016af31f5e5SChris Mason * this copies info out of the root backup array and back into 2017af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 2018af31f5e5SChris Mason * the array, so you send it the number of backups you've already 2019af31f5e5SChris Mason * tried and the last backup index you used. 2020af31f5e5SChris Mason * 2021af31f5e5SChris Mason * this returns -1 when it has tried all the backups 2022af31f5e5SChris Mason */ 2023af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 2024af31f5e5SChris Mason struct btrfs_super_block *super, 2025af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 2026af31f5e5SChris Mason { 2027af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2028af31f5e5SChris Mason int newest = *backup_index; 2029af31f5e5SChris Mason 2030af31f5e5SChris Mason if (*num_backups_tried == 0) { 2031af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 2032af31f5e5SChris Mason 2033af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 2034af31f5e5SChris Mason if (newest == -1) 2035af31f5e5SChris Mason return -1; 2036af31f5e5SChris Mason 2037af31f5e5SChris Mason *backup_index = newest; 2038af31f5e5SChris Mason *num_backups_tried = 1; 2039af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2040af31f5e5SChris Mason /* we've tried all the backups, all done */ 2041af31f5e5SChris Mason return -1; 2042af31f5e5SChris Mason } else { 2043af31f5e5SChris Mason /* jump to the next oldest backup */ 2044af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2045af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2046af31f5e5SChris Mason *backup_index = newest; 2047af31f5e5SChris Mason *num_backups_tried += 1; 2048af31f5e5SChris Mason } 2049af31f5e5SChris Mason root_backup = super->super_roots + newest; 2050af31f5e5SChris Mason 2051af31f5e5SChris Mason btrfs_set_super_generation(super, 2052af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2053af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2054af31f5e5SChris Mason btrfs_set_super_root_level(super, 2055af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2056af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2057af31f5e5SChris Mason 2058af31f5e5SChris Mason /* 2059af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2060af31f5e5SChris Mason * need a fsck 2061af31f5e5SChris Mason */ 2062af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2063af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2064af31f5e5SChris Mason return 0; 2065af31f5e5SChris Mason } 2066af31f5e5SChris Mason 20677abadb64SLiu Bo /* helper to cleanup workers */ 20687abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20697abadb64SLiu Bo { 2070dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2071afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20725cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2073fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2074fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20758b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2076d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2077fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2078fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2079a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20805b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2081e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2082736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2083a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2084fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2085a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2086a9b9477dSFilipe Manana /* 2087a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2088a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2089a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2090a9b9477dSFilipe Manana */ 2091a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2092a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20937abadb64SLiu Bo } 20947abadb64SLiu Bo 20952e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20962e9f5954SRashika { 20972e9f5954SRashika if (root) { 20982e9f5954SRashika free_extent_buffer(root->node); 20992e9f5954SRashika free_extent_buffer(root->commit_root); 21002e9f5954SRashika root->node = NULL; 21012e9f5954SRashika root->commit_root = NULL; 21022e9f5954SRashika } 21032e9f5954SRashika } 21042e9f5954SRashika 2105af31f5e5SChris Mason /* helper to cleanup tree roots */ 2106af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2107af31f5e5SChris Mason { 21082e9f5954SRashika free_root_extent_buffers(info->tree_root); 2109655b09feSJosef Bacik 21102e9f5954SRashika free_root_extent_buffers(info->dev_root); 21112e9f5954SRashika free_root_extent_buffers(info->extent_root); 21122e9f5954SRashika free_root_extent_buffers(info->csum_root); 21132e9f5954SRashika free_root_extent_buffers(info->quota_root); 21142e9f5954SRashika free_root_extent_buffers(info->uuid_root); 21152e9f5954SRashika if (chunk_root) 21162e9f5954SRashika free_root_extent_buffers(info->chunk_root); 211770f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2118af31f5e5SChris Mason } 2119af31f5e5SChris Mason 2120faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2121171f6537SJosef Bacik { 2122171f6537SJosef Bacik int ret; 2123171f6537SJosef Bacik struct btrfs_root *gang[8]; 2124171f6537SJosef Bacik int i; 2125171f6537SJosef Bacik 2126171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2127171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2128171f6537SJosef Bacik struct btrfs_root, root_list); 2129171f6537SJosef Bacik list_del(&gang[0]->root_list); 2130171f6537SJosef Bacik 213127cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2132cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2133171f6537SJosef Bacik } else { 2134171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2135171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2136b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2137171f6537SJosef Bacik } 2138171f6537SJosef Bacik } 2139171f6537SJosef Bacik 2140171f6537SJosef Bacik while (1) { 2141171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2142171f6537SJosef Bacik (void **)gang, 0, 2143171f6537SJosef Bacik ARRAY_SIZE(gang)); 2144171f6537SJosef Bacik if (!ret) 2145171f6537SJosef Bacik break; 2146171f6537SJosef Bacik for (i = 0; i < ret; i++) 2147cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2148171f6537SJosef Bacik } 21491a4319ccSLiu Bo 21501a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 21511a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 21522ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 21531a4319ccSLiu Bo } 2154171f6537SJosef Bacik } 2155af31f5e5SChris Mason 2156638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2157638aa7edSEric Sandeen { 2158638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2159638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2160638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2161638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2162638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2163638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2164638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2165638aa7edSEric Sandeen } 2166638aa7edSEric Sandeen 2167779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2168779a65a4SEric Sandeen { 2169779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2170779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2171779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2172779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2173779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2174779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2175779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2176779a65a4SEric Sandeen } 2177779a65a4SEric Sandeen 21786bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2179f37938e0SEric Sandeen { 21802ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21812ff7e61eSJeff Mahoney 21822ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21832ff7e61eSJeff Mahoney set_nlink(inode, 1); 2184f37938e0SEric Sandeen /* 2185f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2186f37938e0SEric Sandeen * the real end of the address space is determined by all of 2187f37938e0SEric Sandeen * the devices in the system 2188f37938e0SEric Sandeen */ 21892ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21902ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2191f37938e0SEric Sandeen 21922ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 2193c6100a4bSJosef Bacik extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode); 21942ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 21952ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2196f37938e0SEric Sandeen 21972ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2198f37938e0SEric Sandeen 21992ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 22002ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 22012ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 22022ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2203f37938e0SEric Sandeen } 2204f37938e0SEric Sandeen 2205ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2206ad618368SEric Sandeen { 2207ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2208ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2209ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 221073beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 221173beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 221273beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2213ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 221473beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2215ad618368SEric Sandeen } 2216ad618368SEric Sandeen 2217f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2218f9e92e40SEric Sandeen { 2219f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2220f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2221f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2222f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2223f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2224f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2225f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2226d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2227f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2228f9e92e40SEric Sandeen } 2229f9e92e40SEric Sandeen 22302a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 22312a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 22322a458198SEric Sandeen { 2233f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size; 22346f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 22352a458198SEric Sandeen 22362a458198SEric Sandeen fs_info->workers = 2237cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2238cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 22392a458198SEric Sandeen 22402a458198SEric Sandeen fs_info->delalloc_workers = 2241cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2242cb001095SJeff Mahoney flags, max_active, 2); 22432a458198SEric Sandeen 22442a458198SEric Sandeen fs_info->flush_workers = 2245cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2246cb001095SJeff Mahoney flags, max_active, 0); 22472a458198SEric Sandeen 22482a458198SEric Sandeen fs_info->caching_workers = 2249cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22502a458198SEric Sandeen 22512a458198SEric Sandeen /* 22522a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 22532a458198SEric Sandeen * likely that bios will be send down in a sane order to the 22542a458198SEric Sandeen * devices 22552a458198SEric Sandeen */ 22562a458198SEric Sandeen fs_info->submit_workers = 2257cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 22582a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22592a458198SEric Sandeen max_active), 64); 22602a458198SEric Sandeen 22612a458198SEric Sandeen fs_info->fixup_workers = 2262cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22632a458198SEric Sandeen 22642a458198SEric Sandeen /* 22652a458198SEric Sandeen * endios are largely parallel and should have a very 22662a458198SEric Sandeen * low idle thresh 22672a458198SEric Sandeen */ 22682a458198SEric Sandeen fs_info->endio_workers = 2269cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22702a458198SEric Sandeen fs_info->endio_meta_workers = 2271cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2272cb001095SJeff Mahoney max_active, 4); 22732a458198SEric Sandeen fs_info->endio_meta_write_workers = 2274cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2275cb001095SJeff Mahoney max_active, 2); 22762a458198SEric Sandeen fs_info->endio_raid56_workers = 2277cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2278cb001095SJeff Mahoney max_active, 4); 22792a458198SEric Sandeen fs_info->endio_repair_workers = 2280cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 22812a458198SEric Sandeen fs_info->rmw_workers = 2282cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22832a458198SEric Sandeen fs_info->endio_write_workers = 2284cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2285cb001095SJeff Mahoney max_active, 2); 22862a458198SEric Sandeen fs_info->endio_freespace_worker = 2287cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2288cb001095SJeff Mahoney max_active, 0); 22892a458198SEric Sandeen fs_info->delayed_workers = 2290cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2291cb001095SJeff Mahoney max_active, 0); 22922a458198SEric Sandeen fs_info->readahead_workers = 2293cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2294cb001095SJeff Mahoney max_active, 2); 22952a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2296cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 22972a458198SEric Sandeen fs_info->extent_workers = 2298cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 22992a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 23002a458198SEric Sandeen max_active), 8); 23012a458198SEric Sandeen 23022a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 23032a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 23042a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 23052a458198SEric Sandeen fs_info->endio_meta_write_workers && 23062a458198SEric Sandeen fs_info->endio_repair_workers && 23072a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 23082a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 23092a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 23102a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 23112a458198SEric Sandeen fs_info->extent_workers && 23122a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 23132a458198SEric Sandeen return -ENOMEM; 23142a458198SEric Sandeen } 23152a458198SEric Sandeen 23162a458198SEric Sandeen return 0; 23172a458198SEric Sandeen } 23182a458198SEric Sandeen 231963443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 232063443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 232163443bf5SEric Sandeen { 232263443bf5SEric Sandeen int ret; 232363443bf5SEric Sandeen struct btrfs_root *log_tree_root; 232463443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 232563443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 2326581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super); 232763443bf5SEric Sandeen 232863443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2329f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 233063443bf5SEric Sandeen return -EIO; 233163443bf5SEric Sandeen } 233263443bf5SEric Sandeen 233374e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 233463443bf5SEric Sandeen if (!log_tree_root) 233563443bf5SEric Sandeen return -ENOMEM; 233663443bf5SEric Sandeen 2337da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 233863443bf5SEric Sandeen 23392ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 2340581c1760SQu Wenruo fs_info->generation + 1, 2341581c1760SQu Wenruo level, NULL); 234264c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2343f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 23440eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 234564c043deSLiu Bo kfree(log_tree_root); 23460eeff236SLiu Bo return ret; 234764c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2348f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 234963443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 235063443bf5SEric Sandeen kfree(log_tree_root); 235163443bf5SEric Sandeen return -EIO; 235263443bf5SEric Sandeen } 235363443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 235463443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 235563443bf5SEric Sandeen if (ret) { 23560b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 235763443bf5SEric Sandeen "Failed to recover log tree"); 235863443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 235963443bf5SEric Sandeen kfree(log_tree_root); 236063443bf5SEric Sandeen return ret; 236163443bf5SEric Sandeen } 236263443bf5SEric Sandeen 2363bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23646bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 236563443bf5SEric Sandeen if (ret) 236663443bf5SEric Sandeen return ret; 236763443bf5SEric Sandeen } 236863443bf5SEric Sandeen 236963443bf5SEric Sandeen return 0; 237063443bf5SEric Sandeen } 237163443bf5SEric Sandeen 23726bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23734bbcaa64SEric Sandeen { 23746bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2375a4f3d2c4SDavid Sterba struct btrfs_root *root; 23764bbcaa64SEric Sandeen struct btrfs_key location; 23774bbcaa64SEric Sandeen int ret; 23784bbcaa64SEric Sandeen 23796bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23806bccf3abSJeff Mahoney 23814bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23824bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23834bbcaa64SEric Sandeen location.offset = 0; 23844bbcaa64SEric Sandeen 2385a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2386f50f4353SLiu Bo if (IS_ERR(root)) { 2387f50f4353SLiu Bo ret = PTR_ERR(root); 2388f50f4353SLiu Bo goto out; 2389f50f4353SLiu Bo } 2390a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2391a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23924bbcaa64SEric Sandeen 23934bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2394a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2395f50f4353SLiu Bo if (IS_ERR(root)) { 2396f50f4353SLiu Bo ret = PTR_ERR(root); 2397f50f4353SLiu Bo goto out; 2398f50f4353SLiu Bo } 2399a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2400a4f3d2c4SDavid Sterba fs_info->dev_root = root; 24014bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 24024bbcaa64SEric Sandeen 24034bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2404a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2405f50f4353SLiu Bo if (IS_ERR(root)) { 2406f50f4353SLiu Bo ret = PTR_ERR(root); 2407f50f4353SLiu Bo goto out; 2408f50f4353SLiu Bo } 2409a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2410a4f3d2c4SDavid Sterba fs_info->csum_root = root; 24114bbcaa64SEric Sandeen 24124bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2413a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2414a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2415a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2416afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2417a4f3d2c4SDavid Sterba fs_info->quota_root = root; 24184bbcaa64SEric Sandeen } 24194bbcaa64SEric Sandeen 24204bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2421a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2422a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2423a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 24244bbcaa64SEric Sandeen if (ret != -ENOENT) 2425f50f4353SLiu Bo goto out; 24264bbcaa64SEric Sandeen } else { 2427a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2428a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 24294bbcaa64SEric Sandeen } 24304bbcaa64SEric Sandeen 243170f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 243270f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 243370f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 2434f50f4353SLiu Bo if (IS_ERR(root)) { 2435f50f4353SLiu Bo ret = PTR_ERR(root); 2436f50f4353SLiu Bo goto out; 2437f50f4353SLiu Bo } 243870f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 243970f6d82eSOmar Sandoval fs_info->free_space_root = root; 244070f6d82eSOmar Sandoval } 244170f6d82eSOmar Sandoval 24424bbcaa64SEric Sandeen return 0; 2443f50f4353SLiu Bo out: 2444f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d", 2445f50f4353SLiu Bo location.objectid, ret); 2446f50f4353SLiu Bo return ret; 24474bbcaa64SEric Sandeen } 24484bbcaa64SEric Sandeen 2449ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2450dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2451dfe25020SChris Mason char *options) 2452eb60ceacSChris Mason { 2453db94535dSChris Mason u32 sectorsize; 2454db94535dSChris Mason u32 nodesize; 245587ee04ebSChris Mason u32 stripesize; 245684234f3aSYan Zheng u64 generation; 2457f2b636e8SJosef Bacik u64 features; 24583de4586cSChris Mason struct btrfs_key location; 2459a061fc8dSChris Mason struct buffer_head *bh; 24604d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2461815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2462f84a8bd6SAl Viro struct btrfs_root *tree_root; 24634d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2464eb60ceacSChris Mason int ret; 2465e58ca020SYan int err = -EINVAL; 2466af31f5e5SChris Mason int num_backups_tried = 0; 2467af31f5e5SChris Mason int backup_index = 0; 24686675df31SOmar Sandoval int clear_free_space_tree = 0; 2469581c1760SQu Wenruo int level; 24704543df7eSChris Mason 247174e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 247274e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2473cb517eabSMiao Xie if (!tree_root || !chunk_root) { 247439279cc3SChris Mason err = -ENOMEM; 247539279cc3SChris Mason goto fail; 247639279cc3SChris Mason } 247776dda93cSYan, Zheng 247876dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 247976dda93cSYan, Zheng if (ret) { 248076dda93cSYan, Zheng err = ret; 248176dda93cSYan, Zheng goto fail; 248276dda93cSYan, Zheng } 248376dda93cSYan, Zheng 2484908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2485e2d84521SMiao Xie if (ret) { 2486e2d84521SMiao Xie err = ret; 24879e11ceeeSJan Kara goto fail_srcu; 2488e2d84521SMiao Xie } 248909cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2490e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2491e2d84521SMiao Xie 2492908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2493963d678bSMiao Xie if (ret) { 2494963d678bSMiao Xie err = ret; 2495963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2496963d678bSMiao Xie } 2497963d678bSMiao Xie 2498908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2499c404e0dcSMiao Xie if (ret) { 2500c404e0dcSMiao Xie err = ret; 2501c404e0dcSMiao Xie goto fail_delalloc_bytes; 2502c404e0dcSMiao Xie } 2503c404e0dcSMiao Xie 250476dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2505f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 25068fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2507facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 250824bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2509eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 251011833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 251175cb379dSJeff Mahoney INIT_LIST_HEAD(&fs_info->pending_raid_kobjs); 251275cb379dSJeff Mahoney spin_lock_init(&fs_info->pending_raid_kobjs_lock); 2513eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2514a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 251576dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 251624bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 25174cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2518f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2519ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2520fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2521f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 252247ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2523f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2524d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 252567c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 25267585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2527573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2528c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2529de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 253019c00ddcSChris Mason 25310b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 25326324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2533f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 253447ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 25350b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 253666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 253766d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 253866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 253966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 254066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 254166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 254266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2543771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 25444cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2545fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 25462fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2547fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2548e20d96d6SChris Mason fs_info->sb = sb; 254995ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 25509ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 25514cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2552a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2553f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 25548b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2555f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 255690519d66SArne Jansen /* readahead state */ 2557d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 255890519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2559fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2560c8b97818SChris Mason 2561b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2562b34b086cSChris Mason num_online_cpus() + 2, 8); 25630afbaf8cSChris Mason 2564199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2565199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 256669fe2d75SJosef Bacik 256769fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 256869fe2d75SJosef Bacik if (!fs_info->btree_inode) { 256969fe2d75SJosef Bacik err = -ENOMEM; 257069fe2d75SJosef Bacik goto fail_bio_counter; 257169fe2d75SJosef Bacik } 257269fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 257369fe2d75SJosef Bacik 257416cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 257574e4d827SDavid Sterba GFP_KERNEL); 257616cdcec7SMiao Xie if (!fs_info->delayed_root) { 257716cdcec7SMiao Xie err = -ENOMEM; 257816cdcec7SMiao Xie goto fail_iput; 257916cdcec7SMiao Xie } 258016cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 25813eaa2885SChris Mason 2582638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 258321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 258421adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 258521adbd5cSStefan Behrens #endif 2586779a65a4SEric Sandeen btrfs_init_balance(fs_info); 258721c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2588a2de733cSArne Jansen 25899f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 25909f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2591a061fc8dSChris Mason 25926bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 259339279cc3SChris Mason 2594e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 25956bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2596a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2597925baeddSChris Mason 2598c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[0], NULL); 2599c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[1], NULL); 260011833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2601afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2602509659cdSChris Mason 26035a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 260479154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2605925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2606a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2607a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 26087d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 26091bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 26109e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2611c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 261276dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2613803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2614fa9c0d79SChris Mason 2615ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2616f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2617416ac51dSArne Jansen 2618fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2619fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2620fa9c0d79SChris Mason 2621e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2622f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2623bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 26244854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 26253768f368SChris Mason 262604216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 262704216820SFilipe Manana 2628da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2629da17066cSJeff Mahoney fs_info->nodesize = 4096; 2630da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2631da17066cSJeff Mahoney fs_info->stripesize = 4096; 2632da17066cSJeff Mahoney 263353b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 263453b381b3SDavid Woodhouse if (ret) { 263583c8266aSDavid Sterba err = ret; 263653b381b3SDavid Woodhouse goto fail_alloc; 263753b381b3SDavid Woodhouse } 263853b381b3SDavid Woodhouse 2639da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 26407eccb903SChris Mason 26413c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 26421104a885SDavid Sterba 26431104a885SDavid Sterba /* 26441104a885SDavid Sterba * Read super block and check the signature bytes only 26451104a885SDavid Sterba */ 2646a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 264792fc03fbSAnand Jain if (IS_ERR(bh)) { 264892fc03fbSAnand Jain err = PTR_ERR(bh); 264916cdcec7SMiao Xie goto fail_alloc; 265020b45077SDave Young } 265139279cc3SChris Mason 26521104a885SDavid Sterba /* 26531104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 26541104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 26551104a885SDavid Sterba */ 2656ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 265705135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 26581104a885SDavid Sterba err = -EINVAL; 2659b2acdddfSAnand Jain brelse(bh); 26601104a885SDavid Sterba goto fail_alloc; 26611104a885SDavid Sterba } 26621104a885SDavid Sterba 26631104a885SDavid Sterba /* 26641104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 26651104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 26661104a885SDavid Sterba * the whole block of INFO_SIZE 26671104a885SDavid Sterba */ 26686c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 26696c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 26706c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2671a061fc8dSChris Mason brelse(bh); 26725f39d397SChris Mason 26736c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 26740b86a832SChris Mason 26753d3a126aSDavid Sterba ret = btrfs_check_super_valid(fs_info); 26761104a885SDavid Sterba if (ret) { 267705135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 26781104a885SDavid Sterba err = -EINVAL; 26791104a885SDavid Sterba goto fail_alloc; 26801104a885SDavid Sterba } 26811104a885SDavid Sterba 26826c41761fSDavid Sterba disk_super = fs_info->super_copy; 26830f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 268416cdcec7SMiao Xie goto fail_alloc; 26850f7d52f4SChris Mason 2686acce952bSliubo /* check FS state, whether FS is broken. */ 268787533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 268887533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2689acce952bSliubo 269075e7cb7fSLiu Bo /* 2691af31f5e5SChris Mason * run through our array of backup supers and setup 2692af31f5e5SChris Mason * our ring pointer to the oldest one 2693af31f5e5SChris Mason */ 2694af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2695af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2696af31f5e5SChris Mason 2697af31f5e5SChris Mason /* 269875e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 269975e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 270075e7cb7fSLiu Bo */ 270175e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 270275e7cb7fSLiu Bo 27032ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 27042b82032cSYan Zheng if (ret) { 27052b82032cSYan Zheng err = ret; 270616cdcec7SMiao Xie goto fail_alloc; 27072b82032cSYan Zheng } 2708dfe25020SChris Mason 2709f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2710f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2711f2b636e8SJosef Bacik if (features) { 271205135f59SDavid Sterba btrfs_err(fs_info, 271305135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2714c1c9ff7cSGeert Uytterhoeven features); 2715f2b636e8SJosef Bacik err = -EINVAL; 271616cdcec7SMiao Xie goto fail_alloc; 2717f2b636e8SJosef Bacik } 2718f2b636e8SJosef Bacik 27195d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 27205d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 27210b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2722a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 27235c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 27245c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2725727011e0SChris Mason 27263173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 272705135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 27283173a18fSJosef Bacik 2729727011e0SChris Mason /* 2730727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2731727011e0SChris Mason * they are bigger than the page size 2732727011e0SChris Mason */ 273309cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2734727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 273505135f59SDavid Sterba btrfs_info(fs_info, 273605135f59SDavid Sterba "flagging fs with big metadata feature"); 2737727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2738727011e0SChris Mason } 2739727011e0SChris Mason 2740bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2741bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2742b7f67055SChandan Rajendra stripesize = sectorsize; 2743707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2744963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2745bc3f116fSChris Mason 2746da17066cSJeff Mahoney /* Cache block sizes */ 2747da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2748da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2749da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2750da17066cSJeff Mahoney 2751bc3f116fSChris Mason /* 2752bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2753bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2754bc3f116fSChris Mason */ 2755bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2756707e8a07SDavid Sterba (sectorsize != nodesize)) { 275705135f59SDavid Sterba btrfs_err(fs_info, 275805135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 275905135f59SDavid Sterba nodesize, sectorsize); 2760bc3f116fSChris Mason goto fail_alloc; 2761bc3f116fSChris Mason } 2762bc3f116fSChris Mason 2763ceda0864SMiao Xie /* 2764ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2765ceda0864SMiao Xie * update the flag. 2766ceda0864SMiao Xie */ 27675d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 27685d4f98a2SYan Zheng 2769f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2770f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2771bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 277205135f59SDavid Sterba btrfs_err(fs_info, 277305135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2774c1c9ff7cSGeert Uytterhoeven features); 2775f2b636e8SJosef Bacik err = -EINVAL; 277616cdcec7SMiao Xie goto fail_alloc; 2777f2b636e8SJosef Bacik } 277861d92c32SChris Mason 27792a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 27802a458198SEric Sandeen if (ret) { 27812a458198SEric Sandeen err = ret; 27820dc3b84aSJosef Bacik goto fail_sb_buffer; 27830dc3b84aSJosef Bacik } 27844543df7eSChris Mason 27859e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 27869e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 27879e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 2788d4417e22SNikolay Borisov sb->s_bdi->ra_pages = VM_MAX_READAHEAD * SZ_1K / PAGE_SIZE; 27899e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 27909e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 27914575c9ccSChris Mason 2792a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2793a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2794ee87cf5eSAnand Jain memcpy(&sb->s_uuid, fs_info->fsid, BTRFS_FSID_SIZE); 2795db94535dSChris Mason 2796925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 27976bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2798925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 279984eed90fSChris Mason if (ret) { 280005135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 28015d4f98a2SYan Zheng goto fail_sb_buffer; 280284eed90fSChris Mason } 28030b86a832SChris Mason 280484234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 2805581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super); 28060b86a832SChris Mason 2807da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 28080b86a832SChris Mason 28092ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 28100b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2811581c1760SQu Wenruo generation, level, NULL); 281264c043deSLiu Bo if (IS_ERR(chunk_root->node) || 281364c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 281405135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2815e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2816e5fffbacSchandan free_extent_buffer(chunk_root->node); 281795ab1f64SZhao Lei chunk_root->node = NULL; 2818af31f5e5SChris Mason goto fail_tree_roots; 281983121942SDavid Woodhouse } 28205d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 28215d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 28220b86a832SChris Mason 2823e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2824b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2825e17cade2SChris Mason 28265b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 28272b82032cSYan Zheng if (ret) { 282805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2829af31f5e5SChris Mason goto fail_tree_roots; 28302b82032cSYan Zheng } 28310b86a832SChris Mason 28328dabb742SStefan Behrens /* 28339b99b115SAnand Jain * Keep the devid that is marked to be the target device for the 28349b99b115SAnand Jain * device replace procedure 28358dabb742SStefan Behrens */ 28369b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 0); 2837dfe25020SChris Mason 2838a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 283905135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2840a6b0d5c8SChris Mason goto fail_tree_roots; 2841a6b0d5c8SChris Mason } 2842a6b0d5c8SChris Mason 2843af31f5e5SChris Mason retry_root_backup: 284484234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 2845581c1760SQu Wenruo level = btrfs_super_root_level(disk_super); 28460b86a832SChris Mason 28472ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2848db94535dSChris Mason btrfs_super_root(disk_super), 2849581c1760SQu Wenruo generation, level, NULL); 285064c043deSLiu Bo if (IS_ERR(tree_root->node) || 285164c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 285205135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2853e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2854e5fffbacSchandan free_extent_buffer(tree_root->node); 285595ab1f64SZhao Lei tree_root->node = NULL; 2856af31f5e5SChris Mason goto recovery_tree_root; 285783121942SDavid Woodhouse } 2858af31f5e5SChris Mason 28595d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 28605d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 286169e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2862db94535dSChris Mason 2863f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2864f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2865f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2866f32e48e9SChandan Rajendra if (ret) { 2867f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2868f32e48e9SChandan Rajendra goto recovery_tree_root; 2869f32e48e9SChandan Rajendra } 2870f32e48e9SChandan Rajendra 2871f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2872f32e48e9SChandan Rajendra 2873f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2874f32e48e9SChandan Rajendra 28756bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 28764bbcaa64SEric Sandeen if (ret) 2877af31f5e5SChris Mason goto recovery_tree_root; 2878f7a81ea4SStefan Behrens 28798929ecfaSYan, Zheng fs_info->generation = generation; 28808929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 28818929ecfaSYan, Zheng 288268310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 288368310a5eSIlya Dryomov if (ret) { 288405135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 288568310a5eSIlya Dryomov goto fail_block_groups; 288668310a5eSIlya Dryomov } 288768310a5eSIlya Dryomov 2888733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2889733f4fbbSStefan Behrens if (ret) { 289005135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 2891733f4fbbSStefan Behrens goto fail_block_groups; 2892733f4fbbSStefan Behrens } 2893733f4fbbSStefan Behrens 28948dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 28958dabb742SStefan Behrens if (ret) { 289605135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 28978dabb742SStefan Behrens goto fail_block_groups; 28988dabb742SStefan Behrens } 28998dabb742SStefan Behrens 29009b99b115SAnand Jain btrfs_free_extra_devids(fs_devices, 1); 29018dabb742SStefan Behrens 2902b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 2903b7c35e81SAnand Jain if (ret) { 290405135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 290505135f59SDavid Sterba ret); 2906b7c35e81SAnand Jain goto fail_block_groups; 2907b7c35e81SAnand Jain } 2908b7c35e81SAnand Jain 2909b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 2910b7c35e81SAnand Jain if (ret) { 291105135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 291205135f59SDavid Sterba ret); 2913b7c35e81SAnand Jain goto fail_fsdev_sysfs; 2914b7c35e81SAnand Jain } 2915b7c35e81SAnand Jain 291696f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 29175ac1d209SJeff Mahoney if (ret) { 291805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 2919b7c35e81SAnand Jain goto fail_fsdev_sysfs; 29205ac1d209SJeff Mahoney } 29215ac1d209SJeff Mahoney 2922c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2923c59021f8Sliubo if (ret) { 292405135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 29252365dd3cSAnand Jain goto fail_sysfs; 2926c59021f8Sliubo } 2927c59021f8Sliubo 29285b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 29291b1d1f66SJosef Bacik if (ret) { 293005135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 29312365dd3cSAnand Jain goto fail_sysfs; 29321b1d1f66SJosef Bacik } 29334330e183SQu Wenruo 29346528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 293505135f59SDavid Sterba btrfs_warn(fs_info, 29364330e183SQu Wenruo "writeable mount is not allowed due to too many missing devices"); 29372365dd3cSAnand Jain goto fail_sysfs; 2938292fd7fcSStefan Behrens } 29399078a3e1SChris Mason 2940a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2941a74a4b97SChris Mason "btrfs-cleaner"); 294257506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 29432365dd3cSAnand Jain goto fail_sysfs; 2944a74a4b97SChris Mason 2945a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2946a74a4b97SChris Mason tree_root, 2947a74a4b97SChris Mason "btrfs-transaction"); 294857506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 29493f157a2fSChris Mason goto fail_cleaner; 2950a74a4b97SChris Mason 2951583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 2952c289811cSChris Mason !fs_info->fs_devices->rotating) { 2953583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 2954c289811cSChris Mason } 2955c289811cSChris Mason 2956572d9ab7SDavid Sterba /* 295701327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 2958572d9ab7SDavid Sterba * not have to wait for transaction commit 2959572d9ab7SDavid Sterba */ 2960572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 29613818aea2SQu Wenruo 296221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 29630b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 29642ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 29650b246afaSJeff Mahoney btrfs_test_opt(fs_info, 296621adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 296721adbd5cSStefan Behrens 1 : 0, 296821adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 296921adbd5cSStefan Behrens if (ret) 297005135f59SDavid Sterba btrfs_warn(fs_info, 297105135f59SDavid Sterba "failed to initialize integrity check module: %d", 297205135f59SDavid Sterba ret); 297321adbd5cSStefan Behrens } 297421adbd5cSStefan Behrens #endif 2975bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2976bcef60f2SArne Jansen if (ret) 2977bcef60f2SArne Jansen goto fail_trans_kthread; 297821adbd5cSStefan Behrens 2979fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 2980fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 2981fd708b81SJosef Bacik 298296da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 298396da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 29840b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 298563443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 298679787eaaSJeff Mahoney if (ret) { 298763443bf5SEric Sandeen err = ret; 298828c16cbbSWang Shilong goto fail_qgroup; 2989e556ce2cSYan Zheng } 2990e02119d5SChris Mason } 29911a40e23bSZheng Yan 29926bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 299379787eaaSJeff Mahoney if (ret) 299428c16cbbSWang Shilong goto fail_qgroup; 299576dda93cSYan, Zheng 2996bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 2997d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 299844c44af2SIlya Dryomov if (ret) 299928c16cbbSWang Shilong goto fail_qgroup; 300090c711abSZygo Blaxell 300190c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 30025d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 300390c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3004d7ce5843SMiao Xie if (ret < 0) { 300505135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 300605135f59SDavid Sterba ret); 3007d7ce5843SMiao Xie err = -EINVAL; 3008bcef60f2SArne Jansen goto fail_qgroup; 3009d7ce5843SMiao Xie } 30107c2ca468SChris Mason } 30111a40e23bSZheng Yan 30123de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 30133de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3014cb517eabSMiao Xie location.offset = 0; 30153de4586cSChris Mason 30163de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 30173140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 30183140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3019f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read fs tree: %d", err); 3020bcef60f2SArne Jansen goto fail_qgroup; 30213140c9a3SDan Carpenter } 3022c289811cSChris Mason 3023bc98a42cSDavid Howells if (sb_rdonly(sb)) 30242b6ba629SIlya Dryomov return 0; 30252b6ba629SIlya Dryomov 3026f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3027f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 30286675df31SOmar Sandoval clear_free_space_tree = 1; 30296675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 30306675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 30316675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 30326675df31SOmar Sandoval clear_free_space_tree = 1; 30336675df31SOmar Sandoval } 30346675df31SOmar Sandoval 30356675df31SOmar Sandoval if (clear_free_space_tree) { 3036f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3037f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3038f8d468a1SOmar Sandoval if (ret) { 3039f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3040f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 30416bccf3abSJeff Mahoney close_ctree(fs_info); 3042f8d468a1SOmar Sandoval return ret; 3043f8d468a1SOmar Sandoval } 3044f8d468a1SOmar Sandoval } 3045f8d468a1SOmar Sandoval 30460b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3047511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 304805135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3049511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3050511711afSChris Mason if (ret) { 305105135f59SDavid Sterba btrfs_warn(fs_info, 305205135f59SDavid Sterba "failed to create free space tree: %d", ret); 30536bccf3abSJeff Mahoney close_ctree(fs_info); 3054511711afSChris Mason return ret; 3055511711afSChris Mason } 3056511711afSChris Mason } 3057511711afSChris Mason 3058e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 30592b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 30602b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 30612b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 30626bccf3abSJeff Mahoney close_ctree(fs_info); 30632b6ba629SIlya Dryomov return ret; 30642b6ba629SIlya Dryomov } 3065e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 306659641015SIlya Dryomov 30672b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 30682b6ba629SIlya Dryomov if (ret) { 306905135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 30706bccf3abSJeff Mahoney close_ctree(fs_info); 30712b6ba629SIlya Dryomov return ret; 3072e3acc2a6SJosef Bacik } 3073e3acc2a6SJosef Bacik 30748dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 30758dabb742SStefan Behrens if (ret) { 307605135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 30776bccf3abSJeff Mahoney close_ctree(fs_info); 30788dabb742SStefan Behrens return ret; 30798dabb742SStefan Behrens } 30808dabb742SStefan Behrens 3081b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3082b382a324SJan Schmidt 30834bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 308405135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3085f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3086f7a81ea4SStefan Behrens if (ret) { 308705135f59SDavid Sterba btrfs_warn(fs_info, 308805135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 30896bccf3abSJeff Mahoney close_ctree(fs_info); 3090f7a81ea4SStefan Behrens return ret; 3091f7a81ea4SStefan Behrens } 30920b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 30934bbcaa64SEric Sandeen fs_info->generation != 30944bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 309505135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 309670f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 309770f80175SStefan Behrens if (ret) { 309805135f59SDavid Sterba btrfs_warn(fs_info, 309905135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 31006bccf3abSJeff Mahoney close_ctree(fs_info); 310170f80175SStefan Behrens return ret; 310270f80175SStefan Behrens } 310370f80175SStefan Behrens } else { 3104afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3105f7a81ea4SStefan Behrens } 3106afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 310747ab2a6cSJosef Bacik 31088dcddfa0SQu Wenruo /* 31098dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 31108dcddfa0SQu Wenruo * no need to keep the flag 31118dcddfa0SQu Wenruo */ 31128dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 31138dcddfa0SQu Wenruo 3114ad2b2c80SAl Viro return 0; 311539279cc3SChris Mason 3116bcef60f2SArne Jansen fail_qgroup: 3117bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 31187c2ca468SChris Mason fail_trans_kthread: 31197c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 31202ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3121faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 31223f157a2fSChris Mason fail_cleaner: 3123a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 31247c2ca468SChris Mason 31257c2ca468SChris Mason /* 31267c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 31277c2ca468SChris Mason * kthreads 31287c2ca468SChris Mason */ 31297c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 31307c2ca468SChris Mason 31312365dd3cSAnand Jain fail_sysfs: 31326618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 31332365dd3cSAnand Jain 3134b7c35e81SAnand Jain fail_fsdev_sysfs: 3135b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3136b7c35e81SAnand Jain 31371b1d1f66SJosef Bacik fail_block_groups: 313854067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3139af31f5e5SChris Mason 3140af31f5e5SChris Mason fail_tree_roots: 3141af31f5e5SChris Mason free_root_pointers(fs_info, 1); 31422b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3143af31f5e5SChris Mason 314439279cc3SChris Mason fail_sb_buffer: 31457abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 31465cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 314716cdcec7SMiao Xie fail_alloc: 31484543df7eSChris Mason fail_iput: 3149586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3150586e46e2SIlya Dryomov 31514543df7eSChris Mason iput(fs_info->btree_inode); 3152c404e0dcSMiao Xie fail_bio_counter: 3153c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3154963d678bSMiao Xie fail_delalloc_bytes: 3155963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3156e2d84521SMiao Xie fail_dirty_metadata_bytes: 3157e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 315876dda93cSYan, Zheng fail_srcu: 315976dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 31607e662854SQinghuang Feng fail: 316153b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3162586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3163ad2b2c80SAl Viro return err; 3164af31f5e5SChris Mason 3165af31f5e5SChris Mason recovery_tree_root: 31660b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3167af31f5e5SChris Mason goto fail_tree_roots; 3168af31f5e5SChris Mason 3169af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3170af31f5e5SChris Mason 3171af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3172af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3173af31f5e5SChris Mason 3174af31f5e5SChris Mason /* we can't trust the free space cache either */ 3175af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3176af31f5e5SChris Mason 3177af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3178af31f5e5SChris Mason &num_backups_tried, &backup_index); 3179af31f5e5SChris Mason if (ret == -1) 3180af31f5e5SChris Mason goto fail_block_groups; 3181af31f5e5SChris Mason goto retry_root_backup; 3182eb60ceacSChris Mason } 3183663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3184eb60ceacSChris Mason 3185f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3186f2984462SChris Mason { 3187f2984462SChris Mason if (uptodate) { 3188f2984462SChris Mason set_buffer_uptodate(bh); 3189f2984462SChris Mason } else { 3190442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3191442a4f63SStefan Behrens bh->b_private; 3192442a4f63SStefan Behrens 3193fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3194b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3195606686eeSJosef Bacik rcu_str_deref(device->name)); 319601327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 31971259ab75SChris Mason * our own ways of dealing with the IO errors 31981259ab75SChris Mason */ 3199f2984462SChris Mason clear_buffer_uptodate(bh); 3200442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3201f2984462SChris Mason } 3202f2984462SChris Mason unlock_buffer(bh); 3203f2984462SChris Mason put_bh(bh); 3204f2984462SChris Mason } 3205f2984462SChris Mason 320629c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 320729c36d72SAnand Jain struct buffer_head **bh_ret) 320829c36d72SAnand Jain { 320929c36d72SAnand Jain struct buffer_head *bh; 321029c36d72SAnand Jain struct btrfs_super_block *super; 321129c36d72SAnand Jain u64 bytenr; 321229c36d72SAnand Jain 321329c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 321429c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 321529c36d72SAnand Jain return -EINVAL; 321629c36d72SAnand Jain 32179f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 321829c36d72SAnand Jain /* 321929c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 322029c36d72SAnand Jain * the best option we have is to mark it EIO. 322129c36d72SAnand Jain */ 322229c36d72SAnand Jain if (!bh) 322329c36d72SAnand Jain return -EIO; 322429c36d72SAnand Jain 322529c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 322629c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 322729c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 322829c36d72SAnand Jain brelse(bh); 322929c36d72SAnand Jain return -EINVAL; 323029c36d72SAnand Jain } 323129c36d72SAnand Jain 323229c36d72SAnand Jain *bh_ret = bh; 323329c36d72SAnand Jain return 0; 323429c36d72SAnand Jain } 323529c36d72SAnand Jain 323629c36d72SAnand Jain 3237a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3238a512bbf8SYan Zheng { 3239a512bbf8SYan Zheng struct buffer_head *bh; 3240a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3241a512bbf8SYan Zheng struct btrfs_super_block *super; 3242a512bbf8SYan Zheng int i; 3243a512bbf8SYan Zheng u64 transid = 0; 324492fc03fbSAnand Jain int ret = -EINVAL; 3245a512bbf8SYan Zheng 3246a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3247a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3248a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3249a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3250a512bbf8SYan Zheng */ 3251a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 325229c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 325329c36d72SAnand Jain if (ret) 3254a512bbf8SYan Zheng continue; 3255a512bbf8SYan Zheng 3256a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3257a512bbf8SYan Zheng 3258a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3259a512bbf8SYan Zheng brelse(latest); 3260a512bbf8SYan Zheng latest = bh; 3261a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3262a512bbf8SYan Zheng } else { 3263a512bbf8SYan Zheng brelse(bh); 3264a512bbf8SYan Zheng } 3265a512bbf8SYan Zheng } 326692fc03fbSAnand Jain 326792fc03fbSAnand Jain if (!latest) 326892fc03fbSAnand Jain return ERR_PTR(ret); 326992fc03fbSAnand Jain 3270a512bbf8SYan Zheng return latest; 3271a512bbf8SYan Zheng } 3272a512bbf8SYan Zheng 32734eedeb75SHisashi Hifumi /* 3274abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3275abbb3b8eSDavid Sterba * buffer heads we write are pinned. 32764eedeb75SHisashi Hifumi * 3277abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3278abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3279abbb3b8eSDavid Sterba * same for write and wait phases. 32804eedeb75SHisashi Hifumi * 3281abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 32824eedeb75SHisashi Hifumi */ 3283a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3284abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3285a512bbf8SYan Zheng { 3286a512bbf8SYan Zheng struct buffer_head *bh; 3287a512bbf8SYan Zheng int i; 3288a512bbf8SYan Zheng int ret; 3289a512bbf8SYan Zheng int errors = 0; 3290a512bbf8SYan Zheng u32 crc; 3291a512bbf8SYan Zheng u64 bytenr; 32921b9e619cSOmar Sandoval int op_flags; 3293a512bbf8SYan Zheng 3294a512bbf8SYan Zheng if (max_mirrors == 0) 3295a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3296a512bbf8SYan Zheng 3297a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3298a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3299935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3300935e5cc9SMiao Xie device->commit_total_bytes) 3301a512bbf8SYan Zheng break; 3302a512bbf8SYan Zheng 3303a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3304a512bbf8SYan Zheng 3305a512bbf8SYan Zheng crc = ~(u32)0; 3306abbb3b8eSDavid Sterba crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc, 3307abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3308a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3309a512bbf8SYan Zheng 3310abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 33119f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3312a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3313634554dcSJosef Bacik if (!bh) { 3314fb456252SJeff Mahoney btrfs_err(device->fs_info, 3315f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3316f14d104dSDavid Sterba bytenr); 3317634554dcSJosef Bacik errors++; 3318634554dcSJosef Bacik continue; 3319634554dcSJosef Bacik } 3320634554dcSJosef Bacik 3321a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3322a512bbf8SYan Zheng 33234eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3324a512bbf8SYan Zheng get_bh(bh); 33254eedeb75SHisashi Hifumi 33264eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3327a512bbf8SYan Zheng lock_buffer(bh); 3328a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3329442a4f63SStefan Behrens bh->b_private = device; 3330a512bbf8SYan Zheng 3331387125fcSChris Mason /* 3332387125fcSChris Mason * we fua the first super. The others we allow 3333387125fcSChris Mason * to go down lazy. 3334387125fcSChris Mason */ 33351b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 33361b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 33371b9e619cSOmar Sandoval op_flags |= REQ_FUA; 33381b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 33394eedeb75SHisashi Hifumi if (ret) 3340a512bbf8SYan Zheng errors++; 3341a512bbf8SYan Zheng } 3342a512bbf8SYan Zheng return errors < i ? 0 : -1; 3343a512bbf8SYan Zheng } 3344a512bbf8SYan Zheng 3345387125fcSChris Mason /* 3346abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3347abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3348abbb3b8eSDavid Sterba * 3349abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3350abbb3b8eSDavid Sterba * date. 3351abbb3b8eSDavid Sterba */ 3352abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3353abbb3b8eSDavid Sterba { 3354abbb3b8eSDavid Sterba struct buffer_head *bh; 3355abbb3b8eSDavid Sterba int i; 3356abbb3b8eSDavid Sterba int errors = 0; 3357b6a535faSHoward McLauchlan bool primary_failed = false; 3358abbb3b8eSDavid Sterba u64 bytenr; 3359abbb3b8eSDavid Sterba 3360abbb3b8eSDavid Sterba if (max_mirrors == 0) 3361abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3362abbb3b8eSDavid Sterba 3363abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3364abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3365abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3366abbb3b8eSDavid Sterba device->commit_total_bytes) 3367abbb3b8eSDavid Sterba break; 3368abbb3b8eSDavid Sterba 33699f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 33709f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3371abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3372abbb3b8eSDavid Sterba if (!bh) { 3373abbb3b8eSDavid Sterba errors++; 3374b6a535faSHoward McLauchlan if (i == 0) 3375b6a535faSHoward McLauchlan primary_failed = true; 3376abbb3b8eSDavid Sterba continue; 3377abbb3b8eSDavid Sterba } 3378abbb3b8eSDavid Sterba wait_on_buffer(bh); 3379b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3380abbb3b8eSDavid Sterba errors++; 3381b6a535faSHoward McLauchlan if (i == 0) 3382b6a535faSHoward McLauchlan primary_failed = true; 3383b6a535faSHoward McLauchlan } 3384abbb3b8eSDavid Sterba 3385abbb3b8eSDavid Sterba /* drop our reference */ 3386abbb3b8eSDavid Sterba brelse(bh); 3387abbb3b8eSDavid Sterba 3388abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3389abbb3b8eSDavid Sterba brelse(bh); 3390abbb3b8eSDavid Sterba } 3391abbb3b8eSDavid Sterba 3392b6a535faSHoward McLauchlan /* log error, force error return */ 3393b6a535faSHoward McLauchlan if (primary_failed) { 3394b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3395b6a535faSHoward McLauchlan device->devid); 3396b6a535faSHoward McLauchlan return -1; 3397b6a535faSHoward McLauchlan } 3398b6a535faSHoward McLauchlan 3399abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3400abbb3b8eSDavid Sterba } 3401abbb3b8eSDavid Sterba 3402abbb3b8eSDavid Sterba /* 3403387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3404387125fcSChris Mason * for the barrier when it is done 3405387125fcSChris Mason */ 34064246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3407387125fcSChris Mason { 3408387125fcSChris Mason complete(bio->bi_private); 3409387125fcSChris Mason } 3410387125fcSChris Mason 3411387125fcSChris Mason /* 34124fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 34134fc6441aSAnand Jain * done in the waiting counterpart. 3414387125fcSChris Mason */ 34154fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3416387125fcSChris Mason { 3417c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3418e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3419387125fcSChris Mason 3420c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 34214fc6441aSAnand Jain return; 3422387125fcSChris Mason 3423e0ae9994SDavid Sterba bio_reset(bio); 3424387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 342574d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 34268d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3427387125fcSChris Mason init_completion(&device->flush_wait); 3428387125fcSChris Mason bio->bi_private = &device->flush_wait; 3429387125fcSChris Mason 343043a01111SLu Fengqi btrfsic_submit_bio(bio); 34311c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 34324fc6441aSAnand Jain } 3433387125fcSChris Mason 34344fc6441aSAnand Jain /* 34354fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 34364fc6441aSAnand Jain */ 34378c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 34384fc6441aSAnand Jain { 34394fc6441aSAnand Jain struct bio *bio = device->flush_bio; 34404fc6441aSAnand Jain 34411c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 344258efbc9fSOmar Sandoval return BLK_STS_OK; 34434fc6441aSAnand Jain 34441c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 34452980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 34464fc6441aSAnand Jain 34478c27cb35SLinus Torvalds return bio->bi_status; 3448387125fcSChris Mason } 3449387125fcSChris Mason 3450d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3451401b41e5SAnand Jain { 34526528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3453401b41e5SAnand Jain return -EIO; 3454387125fcSChris Mason return 0; 3455387125fcSChris Mason } 3456387125fcSChris Mason 3457387125fcSChris Mason /* 3458387125fcSChris Mason * send an empty flush down to each device in parallel, 3459387125fcSChris Mason * then wait for them 3460387125fcSChris Mason */ 3461387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3462387125fcSChris Mason { 3463387125fcSChris Mason struct list_head *head; 3464387125fcSChris Mason struct btrfs_device *dev; 34655af3e8ccSStefan Behrens int errors_wait = 0; 34664e4cbee9SChristoph Hellwig blk_status_t ret; 3467387125fcSChris Mason 34681538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3469387125fcSChris Mason /* send down all the barriers */ 3470387125fcSChris Mason head = &info->fs_devices->devices; 34711538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3472e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3473f88ba6a2SHidetoshi Seto continue; 3474cea7c8bfSAnand Jain if (!dev->bdev) 3475387125fcSChris Mason continue; 3476e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3477ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3478387125fcSChris Mason continue; 3479387125fcSChris Mason 34804fc6441aSAnand Jain write_dev_flush(dev); 348158efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3482387125fcSChris Mason } 3483387125fcSChris Mason 3484387125fcSChris Mason /* wait for all the barriers */ 34851538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3486e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3487f88ba6a2SHidetoshi Seto continue; 3488387125fcSChris Mason if (!dev->bdev) { 34895af3e8ccSStefan Behrens errors_wait++; 3490387125fcSChris Mason continue; 3491387125fcSChris Mason } 3492e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3493ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3494387125fcSChris Mason continue; 3495387125fcSChris Mason 34964fc6441aSAnand Jain ret = wait_dev_flush(dev); 3497401b41e5SAnand Jain if (ret) { 3498401b41e5SAnand Jain dev->last_flush_error = ret; 349966b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 350066b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 35015af3e8ccSStefan Behrens errors_wait++; 3502387125fcSChris Mason } 3503401b41e5SAnand Jain } 3504401b41e5SAnand Jain 3505cea7c8bfSAnand Jain if (errors_wait) { 3506401b41e5SAnand Jain /* 3507401b41e5SAnand Jain * At some point we need the status of all disks 3508401b41e5SAnand Jain * to arrive at the volume status. So error checking 3509401b41e5SAnand Jain * is being pushed to a separate loop. 3510401b41e5SAnand Jain */ 3511d10b82feSQu Wenruo return check_barrier_error(info); 3512401b41e5SAnand Jain } 3513387125fcSChris Mason return 0; 3514387125fcSChris Mason } 3515387125fcSChris Mason 3516943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3517943c6e99SZhao Lei { 35188789f4feSZhao Lei int raid_type; 35198789f4feSZhao Lei int min_tolerated = INT_MAX; 3520943c6e99SZhao Lei 35218789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 35228789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 35238789f4feSZhao Lei min_tolerated = min(min_tolerated, 35248789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 35258789f4feSZhao Lei tolerated_failures); 3526943c6e99SZhao Lei 35278789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 35288789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 35298789f4feSZhao Lei continue; 35308789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 35318789f4feSZhao Lei continue; 35328789f4feSZhao Lei min_tolerated = min(min_tolerated, 35338789f4feSZhao Lei btrfs_raid_array[raid_type]. 35348789f4feSZhao Lei tolerated_failures); 35358789f4feSZhao Lei } 3536943c6e99SZhao Lei 35378789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3538ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 35398789f4feSZhao Lei min_tolerated = 0; 35408789f4feSZhao Lei } 35418789f4feSZhao Lei 35428789f4feSZhao Lei return min_tolerated; 3543943c6e99SZhao Lei } 3544943c6e99SZhao Lei 3545eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3546f2984462SChris Mason { 3547e5e9a520SChris Mason struct list_head *head; 3548f2984462SChris Mason struct btrfs_device *dev; 3549a061fc8dSChris Mason struct btrfs_super_block *sb; 3550f2984462SChris Mason struct btrfs_dev_item *dev_item; 3551f2984462SChris Mason int ret; 3552f2984462SChris Mason int do_barriers; 3553a236aed1SChris Mason int max_errors; 3554a236aed1SChris Mason int total_errors = 0; 3555a061fc8dSChris Mason u64 flags; 3556f2984462SChris Mason 35570b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3558fed3b381SLiu Bo 3559fed3b381SLiu Bo /* 3560fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3561fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3562fed3b381SLiu Bo * been consistent on disk. 3563fed3b381SLiu Bo */ 3564fed3b381SLiu Bo if (max_mirrors == 0) 35650b246afaSJeff Mahoney backup_super_roots(fs_info); 3566f2984462SChris Mason 35670b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3568a061fc8dSChris Mason dev_item = &sb->dev_item; 3569e5e9a520SChris Mason 35700b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 35710b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 35720b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3573387125fcSChris Mason 35745af3e8ccSStefan Behrens if (do_barriers) { 35750b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 35765af3e8ccSStefan Behrens if (ret) { 35775af3e8ccSStefan Behrens mutex_unlock( 35780b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 35790b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 35805af3e8ccSStefan Behrens "errors while submitting device barriers."); 35815af3e8ccSStefan Behrens return ret; 35825af3e8ccSStefan Behrens } 35835af3e8ccSStefan Behrens } 3584387125fcSChris Mason 35851538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3586dfe25020SChris Mason if (!dev->bdev) { 3587dfe25020SChris Mason total_errors++; 3588dfe25020SChris Mason continue; 3589dfe25020SChris Mason } 3590e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3591ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3592dfe25020SChris Mason continue; 3593dfe25020SChris Mason 35942b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3595a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3596a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 35977df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3598935e5cc9SMiao Xie dev->commit_total_bytes); 3599ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3600ce7213c7SMiao Xie dev->commit_bytes_used); 3601a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3602a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3603a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3604a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 360544880fdcSAnand Jain memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE); 3606a512bbf8SYan Zheng 3607a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3608a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3609f2984462SChris Mason 3610abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3611a236aed1SChris Mason if (ret) 3612a236aed1SChris Mason total_errors++; 3613f2984462SChris Mason } 3614a236aed1SChris Mason if (total_errors > max_errors) { 36150b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3616d397712bSChris Mason total_errors); 36170b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 361879787eaaSJeff Mahoney 36199d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 36200b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 36210b246afaSJeff Mahoney "%d errors while writing supers", 36220b246afaSJeff Mahoney total_errors); 36239d565ba4SStefan Behrens return -EIO; 3624a236aed1SChris Mason } 3625f2984462SChris Mason 3626a512bbf8SYan Zheng total_errors = 0; 36271538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3628dfe25020SChris Mason if (!dev->bdev) 3629dfe25020SChris Mason continue; 3630e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3631ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3632dfe25020SChris Mason continue; 3633dfe25020SChris Mason 3634abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3635a512bbf8SYan Zheng if (ret) 36361259ab75SChris Mason total_errors++; 3637f2984462SChris Mason } 36380b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3639a236aed1SChris Mason if (total_errors > max_errors) { 36400b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 36410b246afaSJeff Mahoney "%d errors while writing supers", 36420b246afaSJeff Mahoney total_errors); 364379787eaaSJeff Mahoney return -EIO; 3644a236aed1SChris Mason } 3645f2984462SChris Mason return 0; 3646f2984462SChris Mason } 3647f2984462SChris Mason 3648cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3649cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3650cb517eabSMiao Xie struct btrfs_root *root) 36512619ba1fSChris Mason { 36524df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 36532619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 36542619ba1fSChris Mason (unsigned long)root->root_key.objectid); 36554df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 365676dda93cSYan, Zheng 365776dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 365876dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 365976dda93cSYan, Zheng 36601c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 36613321719eSLiu Bo btrfs_free_log(NULL, root); 36621c1ea4f7SLiu Bo if (root->reloc_root) { 36631c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 36641c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 36651c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 36661c1ea4f7SLiu Bo root->reloc_root = NULL; 36671c1ea4f7SLiu Bo } 36681c1ea4f7SLiu Bo } 36693321719eSLiu Bo 3670faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3671581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3672faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3673581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 36744df27c4dSYan, Zheng free_fs_root(root); 36754df27c4dSYan, Zheng } 36764df27c4dSYan, Zheng 36774df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 36784df27c4dSYan, Zheng { 367957cdc8dbSDavid Sterba iput(root->ino_cache_inode); 36804df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 36812ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 36821cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 36830ee5dc67SAl Viro if (root->anon_dev) 36840ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 36858257b2dcSMiao Xie if (root->subv_writers) 36868257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 36875f39d397SChris Mason free_extent_buffer(root->node); 36885f39d397SChris Mason free_extent_buffer(root->commit_root); 3689581bb050SLi Zefan kfree(root->free_ino_ctl); 3690581bb050SLi Zefan kfree(root->free_ino_pinned); 369158176a96SJosef Bacik kfree(root->name); 3692b0feb9d9SMiao Xie btrfs_put_fs_root(root); 36932619ba1fSChris Mason } 36942619ba1fSChris Mason 3695cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3696cb517eabSMiao Xie { 3697cb517eabSMiao Xie free_fs_root(root); 3698cfaa7295SChris Mason } 3699cfaa7295SChris Mason 3700c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3701c146afadSYan Zheng { 3702c146afadSYan Zheng u64 root_objectid = 0; 3703c146afadSYan Zheng struct btrfs_root *gang[8]; 370465d33fd7SQu Wenruo int i = 0; 370565d33fd7SQu Wenruo int err = 0; 370665d33fd7SQu Wenruo unsigned int ret = 0; 370765d33fd7SQu Wenruo int index; 3708c146afadSYan Zheng 3709c146afadSYan Zheng while (1) { 371065d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3711c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3712c146afadSYan Zheng (void **)gang, root_objectid, 3713c146afadSYan Zheng ARRAY_SIZE(gang)); 371465d33fd7SQu Wenruo if (!ret) { 371565d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3716c146afadSYan Zheng break; 371765d33fd7SQu Wenruo } 37185d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 371966b4ffd1SJosef Bacik 372065d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 372165d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 372265d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 372365d33fd7SQu Wenruo gang[i] = NULL; 372465d33fd7SQu Wenruo continue; 372565d33fd7SQu Wenruo } 372665d33fd7SQu Wenruo /* grab all the search result for later use */ 372765d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 372865d33fd7SQu Wenruo } 372965d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 373065d33fd7SQu Wenruo 373165d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 373265d33fd7SQu Wenruo if (!gang[i]) 373365d33fd7SQu Wenruo continue; 3734c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 373566b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 373666b4ffd1SJosef Bacik if (err) 373765d33fd7SQu Wenruo break; 373865d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3739c146afadSYan Zheng } 3740c146afadSYan Zheng root_objectid++; 3741c146afadSYan Zheng } 374265d33fd7SQu Wenruo 374365d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 374465d33fd7SQu Wenruo for (; i < ret; i++) { 374565d33fd7SQu Wenruo if (gang[i]) 374665d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 374765d33fd7SQu Wenruo } 374865d33fd7SQu Wenruo return err; 3749c146afadSYan Zheng } 3750c146afadSYan Zheng 37516bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3752c146afadSYan Zheng { 37536bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3754c146afadSYan Zheng struct btrfs_trans_handle *trans; 3755c146afadSYan Zheng 37560b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 37572ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 37580b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 37590b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3760c71bf099SYan, Zheng 3761c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 37620b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 37630b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3764c71bf099SYan, Zheng 37657a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 37663612b495STsutomu Itoh if (IS_ERR(trans)) 37673612b495STsutomu Itoh return PTR_ERR(trans); 37683a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3769c146afadSYan Zheng } 3770c146afadSYan Zheng 37716bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3772eb60ceacSChris Mason { 37736bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3774c146afadSYan Zheng int ret; 3775e089f05cSChris Mason 3776afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3777a2135011SChris Mason 37787343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3779d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 37807343dd61SJustin Maggard 3781803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3782803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3783803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3784803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3785803b2f54SStefan Behrens 3786837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3787aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3788837d5b6eSIlya Dryomov 37898dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 37908dabb742SStefan Behrens 3791aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 37924cb5300bSChris Mason 37934cb5300bSChris Mason /* wait for any defraggers to finish */ 37944cb5300bSChris Mason wait_event(fs_info->transaction_wait, 37954cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 37964cb5300bSChris Mason 37974cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 379826176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 37994cb5300bSChris Mason 380021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 380121c7e756SMiao Xie 3802bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 3803e44163e1SJeff Mahoney /* 3804e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3805e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3806e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3807e44163e1SJeff Mahoney */ 38080b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3809e44163e1SJeff Mahoney 38106bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3811d397712bSChris Mason if (ret) 381204892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3813c146afadSYan Zheng } 3814ed2ff2cbSChris Mason 381587533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 38162ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3817acce952bSliubo 3818e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3819e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 38208929ecfaSYan, Zheng 3821afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3822f25784b3SYan Zheng 382304892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3824bcef60f2SArne Jansen 3825963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 382604892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3827963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3828b0c68f8bSChris Mason } 382931153d81SYan Zheng 38306618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3831b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 38325ac1d209SJeff Mahoney 3833faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3834d10c5f31SChris Mason 38351a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 38361a4319ccSLiu Bo 3837de348ee0SWang Shilong /* 3838de348ee0SWang Shilong * we must make sure there is not any read request to 3839de348ee0SWang Shilong * submit after we stopping all workers. 3840de348ee0SWang Shilong */ 3841de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 384296192499SJosef Bacik btrfs_stop_all_workers(fs_info); 384396192499SJosef Bacik 38445cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 38455cdd7db6SFilipe Manana 3846afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 384713e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 38489ad6b7bcSChris Mason 384913e6c37bSJosef Bacik iput(fs_info->btree_inode); 3850d6bfde87SChris Mason 385121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 38520b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 38532ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 385421adbd5cSStefan Behrens #endif 385521adbd5cSStefan Behrens 3856dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 38570b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3858b248a415SChris Mason 3859e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3860963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3861c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 386276dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 38630b86a832SChris Mason 386453b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3865fd708b81SJosef Bacik btrfs_free_ref_cache(fs_info); 386653b381b3SDavid Woodhouse 3867cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 38681cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 386904216820SFilipe Manana 387004216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 387104216820SFilipe Manana struct extent_map *em; 387204216820SFilipe Manana 387304216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 387404216820SFilipe Manana struct extent_map, list); 387504216820SFilipe Manana list_del_init(&em->list); 387604216820SFilipe Manana free_extent_map(em); 387704216820SFilipe Manana } 3878eb60ceacSChris Mason } 3879eb60ceacSChris Mason 3880b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3881b9fab919SChris Mason int atomic) 3882ccd467d6SChris Mason { 38831259ab75SChris Mason int ret; 3884727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 38851259ab75SChris Mason 38860b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 38871259ab75SChris Mason if (!ret) 38881259ab75SChris Mason return ret; 38891259ab75SChris Mason 38901259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3891b9fab919SChris Mason parent_transid, atomic); 3892b9fab919SChris Mason if (ret == -EAGAIN) 3893b9fab919SChris Mason return ret; 38941259ab75SChris Mason return !ret; 38955f39d397SChris Mason } 38966702ed49SChris Mason 38975f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 38985f39d397SChris Mason { 38990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 390006ea65a3SJosef Bacik struct btrfs_root *root; 39015f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3902b9473439SChris Mason int was_dirty; 3903b4ce94deSChris Mason 390406ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 390506ea65a3SJosef Bacik /* 390606ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 390706ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 390806ea65a3SJosef Bacik * outside of the sanity tests. 390906ea65a3SJosef Bacik */ 391006ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 391106ea65a3SJosef Bacik return; 391206ea65a3SJosef Bacik #endif 391306ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 39140b246afaSJeff Mahoney fs_info = root->fs_info; 3915b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 39160b246afaSJeff Mahoney if (transid != fs_info->generation) 39175d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 39180b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 39190b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3920e2d84521SMiao Xie if (!was_dirty) 3921104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 3922e2d84521SMiao Xie buf->len, 39230b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 39241f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 392569fc6cbbSQu Wenruo /* 392669fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 392769fc6cbbSQu Wenruo * but item data not updated. 392869fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 392969fc6cbbSQu Wenruo */ 393069fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 39312f659546SQu Wenruo btrfs_check_leaf_relaxed(fs_info, buf)) { 3932a4f78750SDavid Sterba btrfs_print_leaf(buf); 39331f21ef0aSFilipe Manana ASSERT(0); 39341f21ef0aSFilipe Manana } 39351f21ef0aSFilipe Manana #endif 3936eb60ceacSChris Mason } 3937eb60ceacSChris Mason 39382ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 3939b53d3f5dSLiu Bo int flush_delayed) 394035b7e476SChris Mason { 3941188de649SChris Mason /* 3942188de649SChris Mason * looks as though older kernels can get into trouble with 3943188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3944188de649SChris Mason */ 3945e2d84521SMiao Xie int ret; 3946d6bfde87SChris Mason 39476933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3948d6bfde87SChris Mason return; 3949d6bfde87SChris Mason 3950b53d3f5dSLiu Bo if (flush_delayed) 39512ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 395216cdcec7SMiao Xie 39530b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 3954e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3955e2d84521SMiao Xie if (ret > 0) { 39560b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 395716cdcec7SMiao Xie } 395816cdcec7SMiao Xie } 395916cdcec7SMiao Xie 39602ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 396116cdcec7SMiao Xie { 39622ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 396335b7e476SChris Mason } 3964b53d3f5dSLiu Bo 39652ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 3966b53d3f5dSLiu Bo { 39672ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 3968d6bfde87SChris Mason } 39696b80053dSChris Mason 3970581c1760SQu Wenruo int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level, 3971581c1760SQu Wenruo struct btrfs_key *first_key) 39726b80053dSChris Mason { 3973727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 39742ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 39752ff7e61eSJeff Mahoney 3976581c1760SQu Wenruo return btree_read_extent_buffer_pages(fs_info, buf, parent_transid, 3977581c1760SQu Wenruo level, first_key); 39786b80053dSChris Mason } 39790da5468fSChris Mason 39803d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info) 3981acce952bSliubo { 3982c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 3983319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 3984319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 3985c926093eSDavid Sterba int ret = 0; 3986c926093eSDavid Sterba 3987319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 3988ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 3989319e4d06SQu Wenruo ret = -EINVAL; 3990319e4d06SQu Wenruo } 39916f794e3cSAnand Jain if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 39926f794e3cSAnand Jain btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 3993319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 39946f794e3cSAnand Jain ret = -EINVAL; 39956f794e3cSAnand Jain } 399621e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 3997ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 399821e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 3999c926093eSDavid Sterba ret = -EINVAL; 4000c926093eSDavid Sterba } 400121e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 4002ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 400321e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 4004c926093eSDavid Sterba ret = -EINVAL; 4005c926093eSDavid Sterba } 400621e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 4007ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 400821e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 4009c926093eSDavid Sterba ret = -EINVAL; 4010c926093eSDavid Sterba } 4011c926093eSDavid Sterba 40121104a885SDavid Sterba /* 4013319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 4014319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 40151104a885SDavid Sterba */ 4016319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 4017319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4018ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 401975d6ad38SDavid Sterba ret = -EINVAL; 402075d6ad38SDavid Sterba } 4021319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 402209cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 4023ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4024ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 402509cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 4026319e4d06SQu Wenruo ret = -EINVAL; 4027319e4d06SQu Wenruo } 4028319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 4029319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4030ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 4031319e4d06SQu Wenruo ret = -EINVAL; 4032319e4d06SQu Wenruo } 4033319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 4034ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 4035ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 4036319e4d06SQu Wenruo ret = -EINVAL; 4037319e4d06SQu Wenruo } 4038319e4d06SQu Wenruo 4039319e4d06SQu Wenruo /* Root alignment check */ 4040319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 4041ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 4042319e4d06SQu Wenruo btrfs_super_root(sb)); 4043319e4d06SQu Wenruo ret = -EINVAL; 4044319e4d06SQu Wenruo } 4045319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 4046ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 4047319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 4048319e4d06SQu Wenruo ret = -EINVAL; 4049319e4d06SQu Wenruo } 4050319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 4051ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 4052319e4d06SQu Wenruo btrfs_super_log_root(sb)); 405375d6ad38SDavid Sterba ret = -EINVAL; 405475d6ad38SDavid Sterba } 405575d6ad38SDavid Sterba 405644880fdcSAnand Jain if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) { 4057ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4058ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 4059c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 4060c926093eSDavid Sterba ret = -EINVAL; 4061c926093eSDavid Sterba } 4062c926093eSDavid Sterba 4063c926093eSDavid Sterba /* 4064c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 4065c926093eSDavid Sterba * done later 4066c926093eSDavid Sterba */ 406799e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 406899e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 406999e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 407099e3ecfcSLiu Bo ret = -EINVAL; 407199e3ecfcSLiu Bo } 4072b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 407399e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 407499e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 407599e3ecfcSLiu Bo ret = -EINVAL; 407699e3ecfcSLiu Bo } 407721e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 4078ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 407921e7626bSDavid Sterba btrfs_super_num_devices(sb)); 408075d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 4081ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 408275d6ad38SDavid Sterba ret = -EINVAL; 408375d6ad38SDavid Sterba } 4084c926093eSDavid Sterba 408521e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4086ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 408721e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4088c926093eSDavid Sterba ret = -EINVAL; 4089c926093eSDavid Sterba } 4090c926093eSDavid Sterba 4091c926093eSDavid Sterba /* 4092ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4093ce7fca5fSDavid Sterba * and one chunk 4094ce7fca5fSDavid Sterba */ 4095ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4096ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4097ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4098ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4099ce7fca5fSDavid Sterba ret = -EINVAL; 4100ce7fca5fSDavid Sterba } 4101ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4102ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4103ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4104ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4105ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4106ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4107ce7fca5fSDavid Sterba ret = -EINVAL; 4108ce7fca5fSDavid Sterba } 4109ce7fca5fSDavid Sterba 4110ce7fca5fSDavid Sterba /* 4111c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4112c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4113c926093eSDavid Sterba */ 411421e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4115ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4116ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4117ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4118ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 411921e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 412021e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4121ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4122ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4123ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4124ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4125c926093eSDavid Sterba 4126c926093eSDavid Sterba return ret; 4127fcd1f065SDavid Sterba } 4128fcd1f065SDavid Sterba 41292ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4130acce952bSliubo { 41310b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 41322ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 41330b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4134acce952bSliubo 41350b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 41360b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4137acce952bSliubo 4138acce952bSliubo /* cleanup FS via transaction */ 41392ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4140acce952bSliubo } 4141acce952bSliubo 4142143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4143acce952bSliubo { 4144acce952bSliubo struct btrfs_ordered_extent *ordered; 4145acce952bSliubo 4146199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4147779880efSJosef Bacik /* 4148779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4149779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4150779880efSJosef Bacik */ 4151199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4152779880efSJosef Bacik root_extent_list) 4153779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4154199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4155199c2a9cSMiao Xie } 4156199c2a9cSMiao Xie 4157199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4158199c2a9cSMiao Xie { 4159199c2a9cSMiao Xie struct btrfs_root *root; 4160199c2a9cSMiao Xie struct list_head splice; 4161199c2a9cSMiao Xie 4162199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4163199c2a9cSMiao Xie 4164199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4165199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4166199c2a9cSMiao Xie while (!list_empty(&splice)) { 4167199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4168199c2a9cSMiao Xie ordered_root); 41691de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 41701de2cfdeSJosef Bacik &fs_info->ordered_roots); 4171199c2a9cSMiao Xie 41722a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4173199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4174199c2a9cSMiao Xie 41752a85d9caSLiu Bo cond_resched(); 41762a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4177199c2a9cSMiao Xie } 4178199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4179acce952bSliubo } 4180acce952bSliubo 418135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 41822ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4183acce952bSliubo { 4184acce952bSliubo struct rb_node *node; 4185acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4186acce952bSliubo struct btrfs_delayed_ref_node *ref; 4187acce952bSliubo int ret = 0; 4188acce952bSliubo 4189acce952bSliubo delayed_refs = &trans->delayed_refs; 4190acce952bSliubo 4191acce952bSliubo spin_lock(&delayed_refs->lock); 4192d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4193cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 41940b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4195acce952bSliubo return ret; 4196acce952bSliubo } 4197acce952bSliubo 4198d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4199d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 42000e0adbcfSJosef Bacik struct rb_node *n; 4201e78417d1SJosef Bacik bool pin_bytes = false; 4202acce952bSliubo 4203d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4204d7df2c79SJosef Bacik href_node); 4205b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 4206d278850eSJosef Bacik refcount_inc(&head->refs); 420779787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4208b939d1abSJosef Bacik 4209acce952bSliubo mutex_lock(&head->mutex); 4210b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4211d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4212e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4213b939d1abSJosef Bacik continue; 4214b939d1abSJosef Bacik } 4215d7df2c79SJosef Bacik spin_lock(&head->lock); 42160e0adbcfSJosef Bacik while ((n = rb_first(&head->ref_tree)) != NULL) { 42170e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 42180e0adbcfSJosef Bacik ref_node); 4219d7df2c79SJosef Bacik ref->in_tree = 0; 42200e0adbcfSJosef Bacik rb_erase(&ref->ref_node, &head->ref_tree); 42210e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 42221d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 42231d57ee94SWang Xiaoguang list_del(&ref->add_list); 4224d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4225d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4226d7df2c79SJosef Bacik } 422754067ae9SJosef Bacik if (head->must_insert_reserved) 4228e78417d1SJosef Bacik pin_bytes = true; 422978a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4230acce952bSliubo delayed_refs->num_heads--; 4231d7df2c79SJosef Bacik if (head->processing == 0) 4232acce952bSliubo delayed_refs->num_heads_ready--; 4233d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4234c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4235d278850eSJosef Bacik RB_CLEAR_NODE(&head->href_node); 4236d7df2c79SJosef Bacik spin_unlock(&head->lock); 4237acce952bSliubo spin_unlock(&delayed_refs->lock); 4238e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4239acce952bSliubo 4240d7df2c79SJosef Bacik if (pin_bytes) 4241d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4242d278850eSJosef Bacik head->num_bytes, 1); 4243d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4244acce952bSliubo cond_resched(); 4245acce952bSliubo spin_lock(&delayed_refs->lock); 4246acce952bSliubo } 4247acce952bSliubo 4248acce952bSliubo spin_unlock(&delayed_refs->lock); 4249acce952bSliubo 4250acce952bSliubo return ret; 4251acce952bSliubo } 4252acce952bSliubo 4253143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4254acce952bSliubo { 4255acce952bSliubo struct btrfs_inode *btrfs_inode; 4256acce952bSliubo struct list_head splice; 4257acce952bSliubo 4258acce952bSliubo INIT_LIST_HEAD(&splice); 4259acce952bSliubo 4260eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4261eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4262acce952bSliubo 4263acce952bSliubo while (!list_empty(&splice)) { 4264eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4265acce952bSliubo delalloc_inodes); 4266acce952bSliubo 4267acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4268df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4269df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4270eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4271acce952bSliubo 4272acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4273b216cbfbSMiao Xie 4274eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4275acce952bSliubo } 4276acce952bSliubo 4277eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4278eb73c1b7SMiao Xie } 4279eb73c1b7SMiao Xie 4280eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4281eb73c1b7SMiao Xie { 4282eb73c1b7SMiao Xie struct btrfs_root *root; 4283eb73c1b7SMiao Xie struct list_head splice; 4284eb73c1b7SMiao Xie 4285eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4286eb73c1b7SMiao Xie 4287eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4288eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4289eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4290eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4291eb73c1b7SMiao Xie delalloc_root); 4292eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4293eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4294eb73c1b7SMiao Xie BUG_ON(!root); 4295eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4296eb73c1b7SMiao Xie 4297eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4298eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4299eb73c1b7SMiao Xie 4300eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4301eb73c1b7SMiao Xie } 4302eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4303acce952bSliubo } 4304acce952bSliubo 43052ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4306acce952bSliubo struct extent_io_tree *dirty_pages, 4307acce952bSliubo int mark) 4308acce952bSliubo { 4309acce952bSliubo int ret; 4310acce952bSliubo struct extent_buffer *eb; 4311acce952bSliubo u64 start = 0; 4312acce952bSliubo u64 end; 4313acce952bSliubo 4314acce952bSliubo while (1) { 4315acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4316e6138876SJosef Bacik mark, NULL); 4317acce952bSliubo if (ret) 4318acce952bSliubo break; 4319acce952bSliubo 432091166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4321acce952bSliubo while (start <= end) { 43220b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 43230b246afaSJeff Mahoney start += fs_info->nodesize; 4324fd8b2b61SJosef Bacik if (!eb) 4325acce952bSliubo continue; 4326fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4327acce952bSliubo 4328fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4329fd8b2b61SJosef Bacik &eb->bflags)) 4330fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4331fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4332acce952bSliubo } 4333acce952bSliubo } 4334acce952bSliubo 4335acce952bSliubo return ret; 4336acce952bSliubo } 4337acce952bSliubo 43382ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4339acce952bSliubo struct extent_io_tree *pinned_extents) 4340acce952bSliubo { 4341acce952bSliubo struct extent_io_tree *unpin; 4342acce952bSliubo u64 start; 4343acce952bSliubo u64 end; 4344acce952bSliubo int ret; 4345ed0eaa14SLiu Bo bool loop = true; 4346acce952bSliubo 4347acce952bSliubo unpin = pinned_extents; 4348ed0eaa14SLiu Bo again: 4349acce952bSliubo while (1) { 4350acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4351e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4352acce952bSliubo if (ret) 4353acce952bSliubo break; 4354acce952bSliubo 4355af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 43562ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4357acce952bSliubo cond_resched(); 4358acce952bSliubo } 4359acce952bSliubo 4360ed0eaa14SLiu Bo if (loop) { 43610b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 43620b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4363ed0eaa14SLiu Bo else 43640b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4365ed0eaa14SLiu Bo loop = false; 4366ed0eaa14SLiu Bo goto again; 4367ed0eaa14SLiu Bo } 4368ed0eaa14SLiu Bo 4369acce952bSliubo return 0; 4370acce952bSliubo } 4371acce952bSliubo 4372c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4373c79a1751SLiu Bo { 4374c79a1751SLiu Bo struct inode *inode; 4375c79a1751SLiu Bo 4376c79a1751SLiu Bo inode = cache->io_ctl.inode; 4377c79a1751SLiu Bo if (inode) { 4378c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4379c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4380c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4381c79a1751SLiu Bo iput(inode); 4382c79a1751SLiu Bo } 4383c79a1751SLiu Bo btrfs_put_block_group(cache); 4384c79a1751SLiu Bo } 4385c79a1751SLiu Bo 4386c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 43872ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4388c79a1751SLiu Bo { 4389c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4390c79a1751SLiu Bo 4391c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4392c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4393c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4394c79a1751SLiu Bo struct btrfs_block_group_cache, 4395c79a1751SLiu Bo dirty_list); 4396c79a1751SLiu Bo 4397c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4398c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4399c79a1751SLiu Bo list_del_init(&cache->io_list); 4400c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4401c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4402c79a1751SLiu Bo } 4403c79a1751SLiu Bo 4404c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4405c79a1751SLiu Bo spin_lock(&cache->lock); 4406c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4407c79a1751SLiu Bo spin_unlock(&cache->lock); 4408c79a1751SLiu Bo 4409c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4410c79a1751SLiu Bo btrfs_put_block_group(cache); 4411c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4412c79a1751SLiu Bo } 4413c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4414c79a1751SLiu Bo 441545ae2c18SNikolay Borisov /* 441645ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe 441745ae2c18SNikolay Borisov * to use it without any locking 441845ae2c18SNikolay Borisov */ 4419c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4420c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4421c79a1751SLiu Bo struct btrfs_block_group_cache, 4422c79a1751SLiu Bo io_list); 4423c79a1751SLiu Bo 4424c79a1751SLiu Bo list_del_init(&cache->io_list); 4425c79a1751SLiu Bo spin_lock(&cache->lock); 4426c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4427c79a1751SLiu Bo spin_unlock(&cache->lock); 4428c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4429c79a1751SLiu Bo } 4430c79a1751SLiu Bo } 4431c79a1751SLiu Bo 443249b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 44332ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 443449b25e05SJeff Mahoney { 44352ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4436c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4437c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4438c79a1751SLiu Bo 44392ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 444049b25e05SJeff Mahoney 44414a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 44420b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 444349b25e05SJeff Mahoney 44444a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 44450b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 444649b25e05SJeff Mahoney 4447ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4448ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 444967cde344SMiao Xie 44502ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 445149b25e05SJeff Mahoney EXTENT_DIRTY); 44522ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 44530b246afaSJeff Mahoney fs_info->pinned_extents); 445449b25e05SJeff Mahoney 44554a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 44564a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 445749b25e05SJeff Mahoney } 445849b25e05SJeff Mahoney 44592ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4460acce952bSliubo { 4461acce952bSliubo struct btrfs_transaction *t; 4462acce952bSliubo 44630b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4464acce952bSliubo 44650b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 44660b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 44670b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4468724e2315SJosef Bacik struct btrfs_transaction, list); 4469724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 44709b64f57dSElena Reshetova refcount_inc(&t->use_count); 44710b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 44722ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4473724e2315SJosef Bacik btrfs_put_transaction(t); 44740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4475724e2315SJosef Bacik continue; 4476724e2315SJosef Bacik } 44770b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4478724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 44790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4480724e2315SJosef Bacik /* 4481724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4482724e2315SJosef Bacik * handle open currently for this transaction. 4483724e2315SJosef Bacik */ 4484724e2315SJosef Bacik wait_event(t->writer_wait, 4485724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4486724e2315SJosef Bacik } else { 44870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4488724e2315SJosef Bacik } 44892ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4490724e2315SJosef Bacik 44910b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 44920b246afaSJeff Mahoney if (t == fs_info->running_transaction) 44930b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4494724e2315SJosef Bacik list_del_init(&t->list); 44950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4496a4abeea4SJosef Bacik 4497724e2315SJosef Bacik btrfs_put_transaction(t); 44982ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 44990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4500724e2315SJosef Bacik } 45010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 45020b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4503ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4504ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 45052ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 45060b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 45070b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4508acce952bSliubo 4509acce952bSliubo return 0; 4510acce952bSliubo } 4511acce952bSliubo 4512c6100a4bSJosef Bacik static struct btrfs_fs_info *btree_fs_info(void *private_data) 4513c6100a4bSJosef Bacik { 4514c6100a4bSJosef Bacik struct inode *inode = private_data; 4515c6100a4bSJosef Bacik return btrfs_sb(inode->i_sb); 4516c6100a4bSJosef Bacik } 4517c6100a4bSJosef Bacik 4518e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 45194d53dddbSDavid Sterba /* mandatory callbacks */ 45200b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 45214d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 4522239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4523239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 452420a7db8aSDavid Sterba .readpage_io_failed_hook = btree_io_failed_hook, 4525c6100a4bSJosef Bacik .set_range_writeback = btrfs_set_range_writeback, 4526c6100a4bSJosef Bacik .tree_fs_info = btree_fs_info, 45274d53dddbSDavid Sterba 45284d53dddbSDavid Sterba /* optional callbacks */ 45290da5468fSChris Mason }; 4530