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> 347e75bf3fSDavid Sterba #include <asm/unaligned.h> 35eb60ceacSChris Mason #include "ctree.h" 36eb60ceacSChris Mason #include "disk-io.h" 370b947affSFilipe David Borba Manana #include "hash.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 11397eb6b69SDavid Sterba void 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; 1274a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1284a69a410SChris Mason extent_submit_bio_hook_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 { 2730b947affSFilipe David Borba Manana return btrfs_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; 4061104a885SDavid Sterba const int csum_size = sizeof(crc); 4071104a885SDavid Sterba char result[csum_size]; 4081104a885SDavid Sterba 4091104a885SDavid Sterba /* 4101104a885SDavid Sterba * The super_block structure does not span the whole 4111104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 41201327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 4131104a885SDavid Sterba */ 4141104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4151104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4161104a885SDavid Sterba btrfs_csum_final(crc, result); 4171104a885SDavid Sterba 4181104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 4191104a885SDavid Sterba ret = 1; 4201104a885SDavid Sterba } 4211104a885SDavid Sterba 4221104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 423ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 4241104a885SDavid Sterba csum_type); 4251104a885SDavid Sterba ret = 1; 4261104a885SDavid Sterba } 4271104a885SDavid Sterba 4281104a885SDavid Sterba return ret; 4291104a885SDavid Sterba } 4301104a885SDavid Sterba 4311104a885SDavid Sterba /* 432d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 433d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 434d352ac68SChris Mason */ 4352ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 436f188591eSChris Mason struct extent_buffer *eb, 4378436ea91SJosef Bacik u64 parent_transid) 438f188591eSChris Mason { 439f188591eSChris Mason struct extent_io_tree *io_tree; 440ea466794SJosef Bacik int failed = 0; 441f188591eSChris Mason int ret; 442f188591eSChris Mason int num_copies = 0; 443f188591eSChris Mason int mirror_num = 0; 444ea466794SJosef Bacik int failed_mirror = 0; 445f188591eSChris Mason 446a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4470b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 448f188591eSChris Mason while (1) { 4498436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 4506af49dbdSDavid Sterba mirror_num); 451256dd1bbSStefan Behrens if (!ret) { 452256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 453b9fab919SChris Mason parent_transid, 0)) 454ea466794SJosef Bacik break; 455256dd1bbSStefan Behrens else 456256dd1bbSStefan Behrens ret = -EIO; 457256dd1bbSStefan Behrens } 458d397712bSChris Mason 459a826d6dcSJosef Bacik /* 460a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 461a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 462a826d6dcSJosef Bacik * any less wrong. 463a826d6dcSJosef Bacik */ 464a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 465ea466794SJosef Bacik break; 466ea466794SJosef Bacik 4670b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 468f188591eSChris Mason eb->start, eb->len); 4694235298eSChris Mason if (num_copies == 1) 470ea466794SJosef Bacik break; 4714235298eSChris Mason 4725cf1ab56SJosef Bacik if (!failed_mirror) { 4735cf1ab56SJosef Bacik failed = 1; 4745cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4755cf1ab56SJosef Bacik } 4765cf1ab56SJosef Bacik 477f188591eSChris Mason mirror_num++; 478ea466794SJosef Bacik if (mirror_num == failed_mirror) 479ea466794SJosef Bacik mirror_num++; 480ea466794SJosef Bacik 4814235298eSChris Mason if (mirror_num > num_copies) 482ea466794SJosef Bacik break; 483f188591eSChris Mason } 484ea466794SJosef Bacik 485c0901581SStefan Behrens if (failed && !ret && failed_mirror) 4862ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 487ea466794SJosef Bacik 488ea466794SJosef Bacik return ret; 489f188591eSChris Mason } 49019c00ddcSChris Mason 491d352ac68SChris Mason /* 492d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 493d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 494d352ac68SChris Mason */ 495d397712bSChris Mason 49601d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 49719c00ddcSChris Mason { 4984eee4fa4SMiao Xie u64 start = page_offset(page); 49919c00ddcSChris Mason u64 found_start; 50019c00ddcSChris Mason struct extent_buffer *eb; 501f188591eSChris Mason 5024f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5034f2de97aSJosef Bacik if (page != eb->pages[0]) 5044f2de97aSJosef Bacik return 0; 5050f805531SAlex Lyakas 50619c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5070f805531SAlex Lyakas /* 5080f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5090f805531SAlex Lyakas * It is useful to know what went wrong. 5100f805531SAlex Lyakas */ 5110f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5120f805531SAlex Lyakas return -EUCLEAN; 5130f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5140f805531SAlex Lyakas return -EUCLEAN; 5150f805531SAlex Lyakas 5160f805531SAlex Lyakas ASSERT(memcmp_extent_buffer(eb, fs_info->fsid, 5170f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5180f805531SAlex Lyakas 5198bd98f0eSAlex Lyakas return csum_tree_block(fs_info, eb, 0); 52019c00ddcSChris Mason } 52119c00ddcSChris Mason 52201d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5232b82032cSYan Zheng struct extent_buffer *eb) 5242b82032cSYan Zheng { 52501d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 52644880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5272b82032cSYan Zheng int ret = 1; 5282b82032cSYan Zheng 5290a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5302b82032cSYan Zheng while (fs_devices) { 5312b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5322b82032cSYan Zheng ret = 0; 5332b82032cSYan Zheng break; 5342b82032cSYan Zheng } 5352b82032cSYan Zheng fs_devices = fs_devices->seed; 5362b82032cSYan Zheng } 5372b82032cSYan Zheng return ret; 5382b82032cSYan Zheng } 5392b82032cSYan Zheng 540facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 541facc8a22SMiao Xie u64 phy_offset, struct page *page, 542facc8a22SMiao Xie u64 start, u64 end, int mirror) 543ce9adaa5SChris Mason { 544ce9adaa5SChris Mason u64 found_start; 545ce9adaa5SChris Mason int found_level; 546ce9adaa5SChris Mason struct extent_buffer *eb; 547ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 54802873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 549f188591eSChris Mason int ret = 0; 550727011e0SChris Mason int reads_done; 551ce9adaa5SChris Mason 552ce9adaa5SChris Mason if (!page->private) 553ce9adaa5SChris Mason goto out; 554d397712bSChris Mason 5554f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 556d397712bSChris Mason 5570b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5580b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5590b32f4bbSJosef Bacik */ 5600b32f4bbSJosef Bacik extent_buffer_get(eb); 5610b32f4bbSJosef Bacik 5620b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 563727011e0SChris Mason if (!reads_done) 564727011e0SChris Mason goto err; 565f188591eSChris Mason 5665cf1ab56SJosef Bacik eb->read_mirror = mirror; 567656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 568ea466794SJosef Bacik ret = -EIO; 569ea466794SJosef Bacik goto err; 570ea466794SJosef Bacik } 571ea466794SJosef Bacik 572ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 573727011e0SChris Mason if (found_start != eb->start) { 57402873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 57594647322SDavid Sterba found_start, eb->start); 576f188591eSChris Mason ret = -EIO; 577ce9adaa5SChris Mason goto err; 578ce9adaa5SChris Mason } 57902873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 58002873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 58194647322SDavid Sterba eb->start); 5821259ab75SChris Mason ret = -EIO; 5831259ab75SChris Mason goto err; 5841259ab75SChris Mason } 585ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5861c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 58702873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 5881c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 5891c24c3ceSJosef Bacik ret = -EIO; 5901c24c3ceSJosef Bacik goto err; 5911c24c3ceSJosef Bacik } 592ce9adaa5SChris Mason 59385d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 59485d4e461SChris Mason eb, found_level); 5954008c04aSChris Mason 59602873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 5978bd98f0eSAlex Lyakas if (ret) 598a826d6dcSJosef Bacik goto err; 599a826d6dcSJosef Bacik 600a826d6dcSJosef Bacik /* 601a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 602a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 603a826d6dcSJosef Bacik * return -EIO. 604a826d6dcSJosef Bacik */ 60569fc6cbbSQu Wenruo if (found_level == 0 && btrfs_check_leaf_full(root, eb)) { 606a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 607a826d6dcSJosef Bacik ret = -EIO; 608a826d6dcSJosef Bacik } 609ce9adaa5SChris Mason 610557ea5ddSQu Wenruo if (found_level > 0 && btrfs_check_node(root, eb)) 611053ab70fSLiu Bo ret = -EIO; 612053ab70fSLiu Bo 6130b32f4bbSJosef Bacik if (!ret) 6140b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 615ce9adaa5SChris Mason err: 61679fb65a1SJosef Bacik if (reads_done && 61779fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 618d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6194bb31e92SArne Jansen 62053b381b3SDavid Woodhouse if (ret) { 62153b381b3SDavid Woodhouse /* 62253b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 62353b381b3SDavid Woodhouse * again, we have to make sure it has something 62453b381b3SDavid Woodhouse * to decrement 62553b381b3SDavid Woodhouse */ 62653b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6270b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 62853b381b3SDavid Woodhouse } 6290b32f4bbSJosef Bacik free_extent_buffer(eb); 630ce9adaa5SChris Mason out: 631f188591eSChris Mason return ret; 632ce9adaa5SChris Mason } 633ce9adaa5SChris Mason 634ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6354bb31e92SArne Jansen { 6364bb31e92SArne Jansen struct extent_buffer *eb; 6374bb31e92SArne Jansen 6384f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 639656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 6405cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 64153b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 642ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 643d48d71aaSDavid Sterba btree_readahead_hook(eb, -EIO); 6444bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6454bb31e92SArne Jansen } 6464bb31e92SArne Jansen 6474246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 648ce9adaa5SChris Mason { 64997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 650ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6519e0af237SLiu Bo struct btrfs_workqueue *wq; 6529e0af237SLiu Bo btrfs_work_func_t func; 653ce9adaa5SChris Mason 654ce9adaa5SChris Mason fs_info = end_io_wq->info; 6554e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 656d20f7043SChris Mason 65737226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 6589e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 6599e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 6609e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 6619e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 6629e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 6639e0af237SLiu Bo func = btrfs_freespace_write_helper; 6649e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 6659e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 6669e0af237SLiu Bo func = btrfs_endio_raid56_helper; 667d20f7043SChris Mason } else { 6689e0af237SLiu Bo wq = fs_info->endio_write_workers; 6699e0af237SLiu Bo func = btrfs_endio_write_helper; 670d20f7043SChris Mason } 6719e0af237SLiu Bo } else { 6728b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 6738b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 6748b110e39SMiao Xie wq = fs_info->endio_repair_workers; 6758b110e39SMiao Xie func = btrfs_endio_repair_helper; 6768b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 6779e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 6789e0af237SLiu Bo func = btrfs_endio_raid56_helper; 6799e0af237SLiu Bo } else if (end_io_wq->metadata) { 6809e0af237SLiu Bo wq = fs_info->endio_meta_workers; 6819e0af237SLiu Bo func = btrfs_endio_meta_helper; 6829e0af237SLiu Bo } else { 6839e0af237SLiu Bo wq = fs_info->endio_workers; 6849e0af237SLiu Bo func = btrfs_endio_helper; 6859e0af237SLiu Bo } 6869e0af237SLiu Bo } 6879e0af237SLiu Bo 6889e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 6899e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 690ce9adaa5SChris Mason } 691ce9adaa5SChris Mason 6924e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 693bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 6940b86a832SChris Mason { 69597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 6968b110e39SMiao Xie 69797eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 698ce9adaa5SChris Mason if (!end_io_wq) 6994e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 700ce9adaa5SChris Mason 701ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 702ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 70322c59948SChris Mason end_io_wq->info = info; 7044e4cbee9SChristoph Hellwig end_io_wq->status = 0; 705ce9adaa5SChris Mason end_io_wq->bio = bio; 70622c59948SChris Mason end_io_wq->metadata = metadata; 707ce9adaa5SChris Mason 708ce9adaa5SChris Mason bio->bi_private = end_io_wq; 709ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 71022c59948SChris Mason return 0; 71122c59948SChris Mason } 71222c59948SChris Mason 713b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7144854ddd0SChris Mason { 7154854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7165cdc7ad3SQu Wenruo info->thread_pool_size, 7174854ddd0SChris Mason info->fs_devices->open_devices); 7184854ddd0SChris Mason return 256 * limit; 7194854ddd0SChris Mason } 7204854ddd0SChris Mason 7214a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7224a69a410SChris Mason { 7234a69a410SChris Mason struct async_submit_bio *async; 7244e4cbee9SChristoph Hellwig blk_status_t ret; 7254a69a410SChris Mason 7264a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 727c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 728eaf25d93SChris Mason async->mirror_num, async->bio_flags, 729eaf25d93SChris Mason async->bio_offset); 73079787eaaSJeff Mahoney if (ret) 7314e4cbee9SChristoph Hellwig async->status = ret; 7324a69a410SChris Mason } 7334a69a410SChris Mason 7344a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7358b712842SChris Mason { 7368b712842SChris Mason struct async_submit_bio *async; 7378b712842SChris Mason 7388b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7394854ddd0SChris Mason 740bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7414e4cbee9SChristoph Hellwig if (async->status) { 7424e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7434246a0b6SChristoph Hellwig bio_endio(async->bio); 74479787eaaSJeff Mahoney return; 74579787eaaSJeff Mahoney } 74679787eaaSJeff Mahoney 747c6100a4bSJosef Bacik async->submit_bio_done(async->private_data, async->bio, async->mirror_num, 74881a75f67SMike Christie async->bio_flags, async->bio_offset); 7494a69a410SChris Mason } 7504a69a410SChris Mason 7514a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7524a69a410SChris Mason { 7534a69a410SChris Mason struct async_submit_bio *async; 7544a69a410SChris Mason 7554a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7568b712842SChris Mason kfree(async); 7578b712842SChris Mason } 7588b712842SChris Mason 7598c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 760c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 761c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 7624a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7634a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 76444b8bd7eSChris Mason { 76544b8bd7eSChris Mason struct async_submit_bio *async; 76644b8bd7eSChris Mason 76744b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 76844b8bd7eSChris Mason if (!async) 7694e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 77044b8bd7eSChris Mason 771c6100a4bSJosef Bacik async->private_data = private_data; 772c6100a4bSJosef Bacik async->fs_info = fs_info; 77344b8bd7eSChris Mason async->bio = bio; 77444b8bd7eSChris Mason async->mirror_num = mirror_num; 7754a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7764a69a410SChris Mason async->submit_bio_done = submit_bio_done; 7774a69a410SChris Mason 7789e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 7795cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 7804a69a410SChris Mason 781c8b97818SChris Mason async->bio_flags = bio_flags; 782eaf25d93SChris Mason async->bio_offset = bio_offset; 7838c8bee1dSChris Mason 7844e4cbee9SChristoph Hellwig async->status = 0; 78579787eaaSJeff Mahoney 78667f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 7875cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 788d313d7a3SChris Mason 7895cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 79044b8bd7eSChris Mason return 0; 79144b8bd7eSChris Mason } 79244b8bd7eSChris Mason 7934e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 794ce3ed71aSChris Mason { 7952c30c71bSKent Overstreet struct bio_vec *bvec; 796ce3ed71aSChris Mason struct btrfs_root *root; 7972c30c71bSKent Overstreet int i, ret = 0; 798ce3ed71aSChris Mason 799c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8002c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 801ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 80201d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 80379787eaaSJeff Mahoney if (ret) 80479787eaaSJeff Mahoney break; 805ce3ed71aSChris Mason } 8062c30c71bSKent Overstreet 8074e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 808ce3ed71aSChris Mason } 809ce3ed71aSChris Mason 8108c27cb35SLinus Torvalds static blk_status_t __btree_submit_bio_start(void *private_data, struct bio *bio, 81181a75f67SMike Christie int mirror_num, unsigned long bio_flags, 812eaf25d93SChris Mason u64 bio_offset) 81322c59948SChris Mason { 8148b712842SChris Mason /* 8158b712842SChris Mason * when we're called for a write, we're already in the async 8165443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8178b712842SChris Mason */ 81879787eaaSJeff Mahoney return btree_csum_one_bio(bio); 81922c59948SChris Mason } 82022c59948SChris Mason 8218c27cb35SLinus Torvalds static blk_status_t __btree_submit_bio_done(void *private_data, struct bio *bio, 822eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 823eaf25d93SChris Mason u64 bio_offset) 8244a69a410SChris Mason { 825c6100a4bSJosef Bacik struct inode *inode = private_data; 8264e4cbee9SChristoph Hellwig blk_status_t ret; 82761891923SStefan Behrens 8288b712842SChris Mason /* 8294a69a410SChris Mason * when we're called for a write, we're already in the async 8304a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8318b712842SChris Mason */ 8322ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 8334246a0b6SChristoph Hellwig if (ret) { 8344e4cbee9SChristoph Hellwig bio->bi_status = ret; 8354246a0b6SChristoph Hellwig bio_endio(bio); 8364246a0b6SChristoph Hellwig } 83761891923SStefan Behrens return ret; 8380b86a832SChris Mason } 8390b86a832SChris Mason 84018fdc679SLiu Bo static int check_async_write(struct btrfs_inode *bi) 841de0022b9SJosef Bacik { 8426300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8436300463bSLiu Bo return 0; 844de0022b9SJosef Bacik #ifdef CONFIG_X86 845bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 846de0022b9SJosef Bacik return 0; 847de0022b9SJosef Bacik #endif 848de0022b9SJosef Bacik return 1; 849de0022b9SJosef Bacik } 850de0022b9SJosef Bacik 8518c27cb35SLinus Torvalds static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio, 852eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 853eaf25d93SChris Mason u64 bio_offset) 85444b8bd7eSChris Mason { 855c6100a4bSJosef Bacik struct inode *inode = private_data; 8560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 85718fdc679SLiu Bo int async = check_async_write(BTRFS_I(inode)); 8584e4cbee9SChristoph Hellwig blk_status_t ret; 859cad321adSChris Mason 86037226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 861cad321adSChris Mason /* 862cad321adSChris Mason * called for a read, do the setup so that checksum validation 863cad321adSChris Mason * can happen in the async kernel threads 864cad321adSChris Mason */ 8650b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8660b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8671d4284bdSChris Mason if (ret) 86861891923SStefan Behrens goto out_w_error; 8692ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 870de0022b9SJosef Bacik } else if (!async) { 871de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 872de0022b9SJosef Bacik if (ret) 87361891923SStefan Behrens goto out_w_error; 8742ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 87561891923SStefan Behrens } else { 876cad321adSChris Mason /* 87761891923SStefan Behrens * kthread helpers are used to submit writes so that 87861891923SStefan Behrens * checksumming can happen in parallel across all CPUs 879cad321adSChris Mason */ 880c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 881c6100a4bSJosef Bacik bio_offset, private_data, 8824a69a410SChris Mason __btree_submit_bio_start, 8834a69a410SChris Mason __btree_submit_bio_done); 88444b8bd7eSChris Mason } 88544b8bd7eSChris Mason 8864246a0b6SChristoph Hellwig if (ret) 8874246a0b6SChristoph Hellwig goto out_w_error; 8884246a0b6SChristoph Hellwig return 0; 8894246a0b6SChristoph Hellwig 89061891923SStefan Behrens out_w_error: 8914e4cbee9SChristoph Hellwig bio->bi_status = ret; 8924246a0b6SChristoph Hellwig bio_endio(bio); 89361891923SStefan Behrens return ret; 89461891923SStefan Behrens } 89561891923SStefan Behrens 8963dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 897784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 898a6bc32b8SMel Gorman struct page *newpage, struct page *page, 899a6bc32b8SMel Gorman enum migrate_mode mode) 900784b4e29SChris Mason { 901784b4e29SChris Mason /* 902784b4e29SChris Mason * we can't safely write a btree page from here, 903784b4e29SChris Mason * we haven't done the locking hook 904784b4e29SChris Mason */ 905784b4e29SChris Mason if (PageDirty(page)) 906784b4e29SChris Mason return -EAGAIN; 907784b4e29SChris Mason /* 908784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 909784b4e29SChris Mason * We must have no buffers or drop them. 910784b4e29SChris Mason */ 911784b4e29SChris Mason if (page_has_private(page) && 912784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 913784b4e29SChris Mason return -EAGAIN; 914a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 915784b4e29SChris Mason } 9163dd1462eSJan Beulich #endif 917784b4e29SChris Mason 9180da5468fSChris Mason 9190da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9200da5468fSChris Mason struct writeback_control *wbc) 9210da5468fSChris Mason { 922e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 923e2d84521SMiao Xie int ret; 924e2d84521SMiao Xie 925d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 926448d640bSChris Mason 927448d640bSChris Mason if (wbc->for_kupdate) 928448d640bSChris Mason return 0; 929448d640bSChris Mason 930e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 931b9473439SChris Mason /* this is a bit racy, but that's ok */ 932e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 933e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 934e2d84521SMiao Xie if (ret < 0) 935793955bcSChris Mason return 0; 936793955bcSChris Mason } 9370b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9380da5468fSChris Mason } 9390da5468fSChris Mason 940b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9415f39d397SChris Mason { 942d1310b2eSChris Mason struct extent_io_tree *tree; 943d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9448ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9455f39d397SChris Mason } 9465f39d397SChris Mason 94770dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9485f39d397SChris Mason { 94998509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 95098509cfcSChris Mason return 0; 9510c4e538bSDavid Sterba 952f7a52a40SDavid Sterba return try_release_extent_buffer(page); 953d98237b3SChris Mason } 954d98237b3SChris Mason 955d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 956d47992f8SLukas Czerner unsigned int length) 957d98237b3SChris Mason { 958d1310b2eSChris Mason struct extent_io_tree *tree; 959d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9605f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9615f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9629ad6b7bcSChris Mason if (PagePrivate(page)) { 963efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 964efe120a0SFrank Holton "page private not zero on page %llu", 965efe120a0SFrank Holton (unsigned long long)page_offset(page)); 9669ad6b7bcSChris Mason ClearPagePrivate(page); 9679ad6b7bcSChris Mason set_page_private(page, 0); 96809cbfeafSKirill A. Shutemov put_page(page); 9699ad6b7bcSChris Mason } 970d98237b3SChris Mason } 971d98237b3SChris Mason 9720b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9730b32f4bbSJosef Bacik { 974bb146eb2SJosef Bacik #ifdef DEBUG 9750b32f4bbSJosef Bacik struct extent_buffer *eb; 9760b32f4bbSJosef Bacik 9770b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9780b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9790b32f4bbSJosef Bacik BUG_ON(!eb); 9800b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9810b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9820b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 983bb146eb2SJosef Bacik #endif 9840b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9850b32f4bbSJosef Bacik } 9860b32f4bbSJosef Bacik 9877f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 988d98237b3SChris Mason .readpage = btree_readpage, 9890da5468fSChris Mason .writepages = btree_writepages, 9905f39d397SChris Mason .releasepage = btree_releasepage, 9915f39d397SChris Mason .invalidatepage = btree_invalidatepage, 9925a92bc88SChris Mason #ifdef CONFIG_MIGRATION 993784b4e29SChris Mason .migratepage = btree_migratepage, 9945a92bc88SChris Mason #endif 9950b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 996d98237b3SChris Mason }; 997123abc88SChris Mason 9982ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 999090d1875SChris Mason { 10005f39d397SChris Mason struct extent_buffer *buf = NULL; 10012ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1002090d1875SChris Mason 10032ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1004c871b0f2SLiu Bo if (IS_ERR(buf)) 10056197d86eSDavid Sterba return; 1006d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 10076af49dbdSDavid Sterba buf, WAIT_NONE, 0); 10085f39d397SChris Mason free_extent_buffer(buf); 1009090d1875SChris Mason } 1010090d1875SChris Mason 10112ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1012ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1013ab0fff03SArne Jansen { 1014ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 10152ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1016ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1017ab0fff03SArne Jansen int ret; 1018ab0fff03SArne Jansen 10192ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1020c871b0f2SLiu Bo if (IS_ERR(buf)) 1021ab0fff03SArne Jansen return 0; 1022ab0fff03SArne Jansen 1023ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1024ab0fff03SArne Jansen 10258436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 10266af49dbdSDavid Sterba mirror_num); 1027ab0fff03SArne Jansen if (ret) { 1028ab0fff03SArne Jansen free_extent_buffer(buf); 1029ab0fff03SArne Jansen return ret; 1030ab0fff03SArne Jansen } 1031ab0fff03SArne Jansen 1032ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1033ab0fff03SArne Jansen free_extent_buffer(buf); 1034ab0fff03SArne Jansen return -EIO; 10350b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1036ab0fff03SArne Jansen *eb = buf; 1037ab0fff03SArne Jansen } else { 1038ab0fff03SArne Jansen free_extent_buffer(buf); 1039ab0fff03SArne Jansen } 1040ab0fff03SArne Jansen return 0; 1041ab0fff03SArne Jansen } 1042ab0fff03SArne Jansen 10432ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10442ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10450308af44SDavid Sterba u64 bytenr) 10460999df54SChris Mason { 10470b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10480b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10490b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10500999df54SChris Mason } 10510999df54SChris Mason 10520999df54SChris Mason 1053e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1054e02119d5SChris Mason { 1055727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 10568aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1057e02119d5SChris Mason } 1058e02119d5SChris Mason 10593189ff77SJeff Layton void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1060e02119d5SChris Mason { 10613189ff77SJeff Layton filemap_fdatawait_range(buf->pages[0]->mapping, 10628aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1063e02119d5SChris Mason } 1064e02119d5SChris Mason 10652ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1066ce86cd59SDavid Sterba u64 parent_transid) 1067e20d96d6SChris Mason { 10685f39d397SChris Mason struct extent_buffer *buf = NULL; 106919c00ddcSChris Mason int ret; 107019c00ddcSChris Mason 10712ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1072c871b0f2SLiu Bo if (IS_ERR(buf)) 1073c871b0f2SLiu Bo return buf; 1074e4204dedSChris Mason 10752ff7e61eSJeff Mahoney ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 10760f0fe8f7SFilipe David Borba Manana if (ret) { 10770f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 107864c043deSLiu Bo return ERR_PTR(ret); 10790f0fe8f7SFilipe David Borba Manana } 10805f39d397SChris Mason return buf; 1081ce9adaa5SChris Mason 1082eb60ceacSChris Mason } 1083eb60ceacSChris Mason 10847c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 10855f39d397SChris Mason struct extent_buffer *buf) 1086ed2ff2cbSChris Mason { 108755c69072SChris Mason if (btrfs_header_generation(buf) == 1088e2d84521SMiao Xie fs_info->running_transaction->transid) { 1089b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1090b4ce94deSChris Mason 1091b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1092104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1093e2d84521SMiao Xie -buf->len, 1094e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1095b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1096b9473439SChris Mason btrfs_set_lock_blocking(buf); 10970b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1098925baeddSChris Mason } 10995f39d397SChris Mason } 1100ed7b63ebSJosef Bacik } 11015f39d397SChris Mason 11028257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11038257b2dcSMiao Xie { 11048257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11058257b2dcSMiao Xie int ret; 11068257b2dcSMiao Xie 11078257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11088257b2dcSMiao Xie if (!writers) 11098257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11108257b2dcSMiao Xie 1111908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 11128257b2dcSMiao Xie if (ret < 0) { 11138257b2dcSMiao Xie kfree(writers); 11148257b2dcSMiao Xie return ERR_PTR(ret); 11158257b2dcSMiao Xie } 11168257b2dcSMiao Xie 11178257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11188257b2dcSMiao Xie return writers; 11198257b2dcSMiao Xie } 11208257b2dcSMiao Xie 11218257b2dcSMiao Xie static void 11228257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11238257b2dcSMiao Xie { 11248257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11258257b2dcSMiao Xie kfree(writers); 11268257b2dcSMiao Xie } 11278257b2dcSMiao Xie 1128da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1129e20d96d6SChris Mason u64 objectid) 1130d97e63b6SChris Mason { 11317c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1132cfaa7295SChris Mason root->node = NULL; 1133a28ec197SChris Mason root->commit_root = NULL; 113427cdeb70SMiao Xie root->state = 0; 1135d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11360b86a832SChris Mason 11370f7d52f4SChris Mason root->objectid = objectid; 11380f7d52f4SChris Mason root->last_trans = 0; 113913a8a7c8SYan, Zheng root->highest_objectid = 0; 1140eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1141199c2a9cSMiao Xie root->nr_ordered_extents = 0; 114258176a96SJosef Bacik root->name = NULL; 11436bef4d31SEric Paris root->inode_tree = RB_ROOT; 114416cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1145f0486c68SYan, Zheng root->block_rsv = NULL; 1146d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11470b86a832SChris Mason 11480b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11495d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1150eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1151eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1152199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1153199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 11542ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11552ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1156d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11575d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1158eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1159199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1160f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11612ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11622ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1163a2135011SChris Mason mutex_init(&root->objectid_mutex); 1164e02119d5SChris Mason mutex_init(&root->log_mutex); 116531f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1166573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11677237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11687237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11697237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11708b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11718b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11727237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11737237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11747237f183SYan Zheng atomic_set(&root->log_writers, 0); 11752ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11768a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 11770700cea7SElena Reshetova refcount_set(&root->refs, 1); 1178ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 1179ce0dcee6SGoldwyn Rodrigues atomic64_set(&root->qgroup_meta_rsv, 0); 11807237f183SYan Zheng root->log_transid = 0; 1181d1433debSMiao Xie root->log_transid_committed = -1; 1182257c62e1SChris Mason root->last_log_commit = 0; 11837c0260eeSJeff Mahoney if (!dummy) 1184c6100a4bSJosef Bacik extent_io_tree_init(&root->dirty_log_pages, NULL); 1185017e5369SChris Mason 11863768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11873768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11886702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11897c0260eeSJeff Mahoney if (!dummy) 11903f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119106ea65a3SJosef Bacik else 119206ea65a3SJosef Bacik root->defrag_trans_start = 0; 11934d775673SChris Mason root->root_key.objectid = objectid; 11940ee5dc67SAl Viro root->anon_dev = 0; 11958ea05e3aSAlexander Block 11965f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 11973768f368SChris Mason } 11983768f368SChris Mason 119974e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 120074e4d827SDavid Sterba gfp_t flags) 12016f07e42eSAl Viro { 120274e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12036f07e42eSAl Viro if (root) 12046f07e42eSAl Viro root->fs_info = fs_info; 12056f07e42eSAl Viro return root; 12066f07e42eSAl Viro } 12076f07e42eSAl Viro 120806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 120906ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1210da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 121106ea65a3SJosef Bacik { 121206ea65a3SJosef Bacik struct btrfs_root *root; 121306ea65a3SJosef Bacik 12147c0260eeSJeff Mahoney if (!fs_info) 12157c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12167c0260eeSJeff Mahoney 12177c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 121806ea65a3SJosef Bacik if (!root) 121906ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1220da17066cSJeff Mahoney 1221b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1222da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1223faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 122406ea65a3SJosef Bacik 122506ea65a3SJosef Bacik return root; 122606ea65a3SJosef Bacik } 122706ea65a3SJosef Bacik #endif 122806ea65a3SJosef Bacik 122920897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 123020897f5cSArne Jansen struct btrfs_fs_info *fs_info, 123120897f5cSArne Jansen u64 objectid) 123220897f5cSArne Jansen { 123320897f5cSArne Jansen struct extent_buffer *leaf; 123420897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 123520897f5cSArne Jansen struct btrfs_root *root; 123620897f5cSArne Jansen struct btrfs_key key; 123720897f5cSArne Jansen int ret = 0; 123833d85fdaSQu Wenruo uuid_le uuid = NULL_UUID_LE; 123920897f5cSArne Jansen 124074e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 124120897f5cSArne Jansen if (!root) 124220897f5cSArne Jansen return ERR_PTR(-ENOMEM); 124320897f5cSArne Jansen 1244da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 124520897f5cSArne Jansen root->root_key.objectid = objectid; 124620897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 124720897f5cSArne Jansen root->root_key.offset = 0; 124820897f5cSArne Jansen 12494d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 125020897f5cSArne Jansen if (IS_ERR(leaf)) { 125120897f5cSArne Jansen ret = PTR_ERR(leaf); 12521dd05682STsutomu Itoh leaf = NULL; 125320897f5cSArne Jansen goto fail; 125420897f5cSArne Jansen } 125520897f5cSArne Jansen 1256b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 125720897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 125820897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 125920897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 126020897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 126120897f5cSArne Jansen root->node = leaf; 126220897f5cSArne Jansen 1263d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1264d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 126520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 126620897f5cSArne Jansen 126720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 126827cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 126920897f5cSArne Jansen 127020897f5cSArne Jansen root->root_item.flags = 0; 127120897f5cSArne Jansen root->root_item.byte_limit = 0; 127220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 127320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 127420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 127520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 127620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 127720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 127820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 127933d85fdaSQu Wenruo if (is_fstree(objectid)) 12806463fe58SStefan Behrens uuid_le_gen(&uuid); 12816463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 128220897f5cSArne Jansen root->root_item.drop_level = 0; 128320897f5cSArne Jansen 128420897f5cSArne Jansen key.objectid = objectid; 128520897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 128620897f5cSArne Jansen key.offset = 0; 128720897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 128820897f5cSArne Jansen if (ret) 128920897f5cSArne Jansen goto fail; 129020897f5cSArne Jansen 129120897f5cSArne Jansen btrfs_tree_unlock(leaf); 129220897f5cSArne Jansen 129320897f5cSArne Jansen return root; 12941dd05682STsutomu Itoh 12951dd05682STsutomu Itoh fail: 12961dd05682STsutomu Itoh if (leaf) { 12971dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 129859885b39STsutomu Itoh free_extent_buffer(root->commit_root); 12991dd05682STsutomu Itoh free_extent_buffer(leaf); 13001dd05682STsutomu Itoh } 13011dd05682STsutomu Itoh kfree(root); 13021dd05682STsutomu Itoh 13031dd05682STsutomu Itoh return ERR_PTR(ret); 130420897f5cSArne Jansen } 130520897f5cSArne Jansen 13067237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1307e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13080f7d52f4SChris Mason { 13090f7d52f4SChris Mason struct btrfs_root *root; 13107237f183SYan Zheng struct extent_buffer *leaf; 1311e02119d5SChris Mason 131274e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1313e02119d5SChris Mason if (!root) 13147237f183SYan Zheng return ERR_PTR(-ENOMEM); 1315e02119d5SChris Mason 1316da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1317e02119d5SChris Mason 1318e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1319e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1320e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 132127cdeb70SMiao Xie 13227237f183SYan Zheng /* 132327cdeb70SMiao Xie * DON'T set REF_COWS for log trees 132427cdeb70SMiao Xie * 13257237f183SYan Zheng * log trees do not get reference counted because they go away 13267237f183SYan Zheng * before a real commit is actually done. They do store pointers 13277237f183SYan Zheng * to file data extents, and those reference counts still get 13287237f183SYan Zheng * updated (along with back refs to the log tree). 13297237f183SYan Zheng */ 1330e02119d5SChris Mason 13314d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13324d75f8a9SDavid Sterba NULL, 0, 0, 0); 13337237f183SYan Zheng if (IS_ERR(leaf)) { 13347237f183SYan Zheng kfree(root); 13357237f183SYan Zheng return ERR_CAST(leaf); 13367237f183SYan Zheng } 1337e02119d5SChris Mason 1338b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 13395d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13405d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13415d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13425d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13437237f183SYan Zheng root->node = leaf; 1344e02119d5SChris Mason 13450b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1346e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1347e02119d5SChris Mason btrfs_tree_unlock(root->node); 13487237f183SYan Zheng return root; 13497237f183SYan Zheng } 13507237f183SYan Zheng 13517237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13527237f183SYan Zheng struct btrfs_fs_info *fs_info) 13537237f183SYan Zheng { 13547237f183SYan Zheng struct btrfs_root *log_root; 13557237f183SYan Zheng 13567237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13577237f183SYan Zheng if (IS_ERR(log_root)) 13587237f183SYan Zheng return PTR_ERR(log_root); 13597237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13607237f183SYan Zheng fs_info->log_root_tree = log_root; 13617237f183SYan Zheng return 0; 13627237f183SYan Zheng } 13637237f183SYan Zheng 13647237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13657237f183SYan Zheng struct btrfs_root *root) 13667237f183SYan Zheng { 13670b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13687237f183SYan Zheng struct btrfs_root *log_root; 13697237f183SYan Zheng struct btrfs_inode_item *inode_item; 13707237f183SYan Zheng 13710b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13727237f183SYan Zheng if (IS_ERR(log_root)) 13737237f183SYan Zheng return PTR_ERR(log_root); 13747237f183SYan Zheng 13757237f183SYan Zheng log_root->last_trans = trans->transid; 13767237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13777237f183SYan Zheng 13787237f183SYan Zheng inode_item = &log_root->root_item.inode; 13793cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13803cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13813cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1382da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13830b246afaSJeff Mahoney fs_info->nodesize); 13843cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13857237f183SYan Zheng 13865d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13877237f183SYan Zheng 13887237f183SYan Zheng WARN_ON(root->log_root); 13897237f183SYan Zheng root->log_root = log_root; 13907237f183SYan Zheng root->log_transid = 0; 1391d1433debSMiao Xie root->log_transid_committed = -1; 1392257c62e1SChris Mason root->last_log_commit = 0; 1393e02119d5SChris Mason return 0; 1394e02119d5SChris Mason } 1395e02119d5SChris Mason 139635a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1397cb517eabSMiao Xie struct btrfs_key *key) 1398e02119d5SChris Mason { 1399e02119d5SChris Mason struct btrfs_root *root; 1400e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14010f7d52f4SChris Mason struct btrfs_path *path; 140284234f3aSYan Zheng u64 generation; 1403cb517eabSMiao Xie int ret; 1404cb517eabSMiao Xie 1405cb517eabSMiao Xie path = btrfs_alloc_path(); 1406cb517eabSMiao Xie if (!path) 1407cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14080f7d52f4SChris Mason 140974e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1410cb517eabSMiao Xie if (!root) { 1411cb517eabSMiao Xie ret = -ENOMEM; 1412cb517eabSMiao Xie goto alloc_fail; 14130f7d52f4SChris Mason } 14140f7d52f4SChris Mason 1415da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 14160f7d52f4SChris Mason 1417cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1418cb517eabSMiao Xie &root->root_item, &root->root_key); 14190f7d52f4SChris Mason if (ret) { 142013a8a7c8SYan, Zheng if (ret > 0) 142113a8a7c8SYan, Zheng ret = -ENOENT; 1422cb517eabSMiao Xie goto find_fail; 14230f7d52f4SChris Mason } 142413a8a7c8SYan, Zheng 142584234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 14262ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14272ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1428ce86cd59SDavid Sterba generation); 142964c043deSLiu Bo if (IS_ERR(root->node)) { 143064c043deSLiu Bo ret = PTR_ERR(root->node); 1431cb517eabSMiao Xie goto find_fail; 1432cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1433cb517eabSMiao Xie ret = -EIO; 143464c043deSLiu Bo free_extent_buffer(root->node); 143564c043deSLiu Bo goto find_fail; 1436416bc658SJosef Bacik } 14375d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 143813a8a7c8SYan, Zheng out: 1439cb517eabSMiao Xie btrfs_free_path(path); 1440cb517eabSMiao Xie return root; 1441cb517eabSMiao Xie 1442cb517eabSMiao Xie find_fail: 1443cb517eabSMiao Xie kfree(root); 1444cb517eabSMiao Xie alloc_fail: 1445cb517eabSMiao Xie root = ERR_PTR(ret); 1446cb517eabSMiao Xie goto out; 1447cb517eabSMiao Xie } 1448cb517eabSMiao Xie 1449cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1450cb517eabSMiao Xie struct btrfs_key *location) 1451cb517eabSMiao Xie { 1452cb517eabSMiao Xie struct btrfs_root *root; 1453cb517eabSMiao Xie 1454cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1455cb517eabSMiao Xie if (IS_ERR(root)) 1456cb517eabSMiao Xie return root; 1457cb517eabSMiao Xie 1458cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 145927cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 146008fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 146108fe4db1SLi Zefan } 146213a8a7c8SYan, Zheng 14635eda7b5eSChris Mason return root; 14645eda7b5eSChris Mason } 14655eda7b5eSChris Mason 1466cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1467cb517eabSMiao Xie { 1468cb517eabSMiao Xie int ret; 14698257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1470cb517eabSMiao Xie 1471cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1472cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1473cb517eabSMiao Xie GFP_NOFS); 1474cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1475cb517eabSMiao Xie ret = -ENOMEM; 1476cb517eabSMiao Xie goto fail; 1477cb517eabSMiao Xie } 1478cb517eabSMiao Xie 14798257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14808257b2dcSMiao Xie if (IS_ERR(writers)) { 14818257b2dcSMiao Xie ret = PTR_ERR(writers); 14828257b2dcSMiao Xie goto fail; 14838257b2dcSMiao Xie } 14848257b2dcSMiao Xie root->subv_writers = writers; 14858257b2dcSMiao Xie 1486cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 148757cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 148857cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1489cb517eabSMiao Xie 1490cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1491cb517eabSMiao Xie if (ret) 1492876d2cf1SLiu Bo goto fail; 1493f32e48e9SChandan Rajendra 1494f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1495f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1496f32e48e9SChandan Rajendra &root->highest_objectid); 1497f32e48e9SChandan Rajendra if (ret) { 1498f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1499876d2cf1SLiu Bo goto fail; 1500f32e48e9SChandan Rajendra } 1501f32e48e9SChandan Rajendra 1502f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1503f32e48e9SChandan Rajendra 1504f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1505f32e48e9SChandan Rajendra 1506cb517eabSMiao Xie return 0; 1507cb517eabSMiao Xie fail: 1508876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1509cb517eabSMiao Xie return ret; 1510cb517eabSMiao Xie } 1511cb517eabSMiao Xie 151235bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1513cb517eabSMiao Xie u64 root_id) 1514cb517eabSMiao Xie { 1515cb517eabSMiao Xie struct btrfs_root *root; 1516cb517eabSMiao Xie 1517cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1518cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1519cb517eabSMiao Xie (unsigned long)root_id); 1520cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1521cb517eabSMiao Xie return root; 1522cb517eabSMiao Xie } 1523cb517eabSMiao Xie 1524cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1525cb517eabSMiao Xie struct btrfs_root *root) 1526cb517eabSMiao Xie { 1527cb517eabSMiao Xie int ret; 1528cb517eabSMiao Xie 1529e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1530cb517eabSMiao Xie if (ret) 1531cb517eabSMiao Xie return ret; 1532cb517eabSMiao Xie 1533cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1534cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1535cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1536cb517eabSMiao Xie root); 1537cb517eabSMiao Xie if (ret == 0) 153827cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1539cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1540cb517eabSMiao Xie radix_tree_preload_end(); 1541cb517eabSMiao Xie 1542cb517eabSMiao Xie return ret; 1543cb517eabSMiao Xie } 1544cb517eabSMiao Xie 1545c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1546c00869f1SMiao Xie struct btrfs_key *location, 1547c00869f1SMiao Xie bool check_ref) 15485eda7b5eSChris Mason { 15495eda7b5eSChris Mason struct btrfs_root *root; 1550381cf658SDavid Sterba struct btrfs_path *path; 15511d4c08e0SDavid Sterba struct btrfs_key key; 15525eda7b5eSChris Mason int ret; 15535eda7b5eSChris Mason 1554edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1555edbd8d4eSChris Mason return fs_info->tree_root; 1556edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1557edbd8d4eSChris Mason return fs_info->extent_root; 15588f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15598f18cf13SChris Mason return fs_info->chunk_root; 15608f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15618f18cf13SChris Mason return fs_info->dev_root; 15620403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15630403e47eSYan Zheng return fs_info->csum_root; 1564bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1565bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1566bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1567f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1568f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1569f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 157070f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 157170f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 157270f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 15734df27c4dSYan, Zheng again: 1574cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 157548475471SStefan Behrens if (root) { 1576c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 157748475471SStefan Behrens return ERR_PTR(-ENOENT); 15785eda7b5eSChris Mason return root; 157948475471SStefan Behrens } 15805eda7b5eSChris Mason 1581cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 15825eda7b5eSChris Mason if (IS_ERR(root)) 15835eda7b5eSChris Mason return root; 15843394e160SChris Mason 1585c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1586d68fc57bSYan, Zheng ret = -ENOENT; 1587d68fc57bSYan, Zheng goto fail; 1588d68fc57bSYan, Zheng } 1589d68fc57bSYan, Zheng 1590cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1591cb517eabSMiao Xie if (ret) 1592cb517eabSMiao Xie goto fail; 1593cb517eabSMiao Xie 1594381cf658SDavid Sterba path = btrfs_alloc_path(); 1595381cf658SDavid Sterba if (!path) { 1596381cf658SDavid Sterba ret = -ENOMEM; 1597381cf658SDavid Sterba goto fail; 1598381cf658SDavid Sterba } 15991d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16001d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16011d4c08e0SDavid Sterba key.offset = location->objectid; 16021d4c08e0SDavid Sterba 16031d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1604381cf658SDavid Sterba btrfs_free_path(path); 1605d68fc57bSYan, Zheng if (ret < 0) 1606d68fc57bSYan, Zheng goto fail; 1607d68fc57bSYan, Zheng if (ret == 0) 160827cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1609d68fc57bSYan, Zheng 1610cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16110f7d52f4SChris Mason if (ret) { 16124df27c4dSYan, Zheng if (ret == -EEXIST) { 16134df27c4dSYan, Zheng free_fs_root(root); 16144df27c4dSYan, Zheng goto again; 16150f7d52f4SChris Mason } 16164df27c4dSYan, Zheng goto fail; 16174df27c4dSYan, Zheng } 1618edbd8d4eSChris Mason return root; 16194df27c4dSYan, Zheng fail: 16204df27c4dSYan, Zheng free_fs_root(root); 16214df27c4dSYan, Zheng return ERR_PTR(ret); 1622edbd8d4eSChris Mason } 1623edbd8d4eSChris Mason 162404160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 162504160088SChris Mason { 162604160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 162704160088SChris Mason int ret = 0; 162804160088SChris Mason struct btrfs_device *device; 162904160088SChris Mason struct backing_dev_info *bdi; 1630b7967db7SChris Mason 16311f78160cSXiao Guangrong rcu_read_lock(); 16321f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1633dfe25020SChris Mason if (!device->bdev) 1634dfe25020SChris Mason continue; 1635efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1636ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 163704160088SChris Mason ret = 1; 163804160088SChris Mason break; 163904160088SChris Mason } 164004160088SChris Mason } 16411f78160cSXiao Guangrong rcu_read_unlock(); 164204160088SChris Mason return ret; 164304160088SChris Mason } 164404160088SChris Mason 16458b712842SChris Mason /* 16468b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16478b712842SChris Mason * functions. This is where read checksum verification actually happens 16488b712842SChris Mason */ 16498b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1650ce9adaa5SChris Mason { 1651ce9adaa5SChris Mason struct bio *bio; 165297eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1653ce9adaa5SChris Mason 165497eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1655ce9adaa5SChris Mason bio = end_io_wq->bio; 16568b712842SChris Mason 16574e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1658ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1659ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 166097eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 16614246a0b6SChristoph Hellwig bio_endio(bio); 1662ce9adaa5SChris Mason } 166344b8bd7eSChris Mason 1664a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1665a74a4b97SChris Mason { 1666a74a4b97SChris Mason struct btrfs_root *root = arg; 16670b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1668d0278245SMiao Xie int again; 1669da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1670a74a4b97SChris Mason 1671a74a4b97SChris Mason do { 1672d0278245SMiao Xie again = 0; 16739d1a2a3aSDavid Sterba 1674d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 16752ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1676d0278245SMiao Xie goto sleep; 1677d0278245SMiao Xie 167890c711abSZygo Blaxell /* 167990c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 168090c711abSZygo Blaxell * before we have finished mounting the filesystem. 168190c711abSZygo Blaxell */ 16820b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 168390c711abSZygo Blaxell goto sleep; 168490c711abSZygo Blaxell 16850b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1686d0278245SMiao Xie goto sleep; 1687d0278245SMiao Xie 1688dc7f370cSMiao Xie /* 1689dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1690dc7f370cSMiao Xie * during the above check and trylock. 1691dc7f370cSMiao Xie */ 16922ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 16930b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1694dc7f370cSMiao Xie goto sleep; 1695dc7f370cSMiao Xie } 1696dc7f370cSMiao Xie 16970b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 16982ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 16990b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1700c2d6cb16SFilipe Manana 17019d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17020b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1703a74a4b97SChris Mason 1704d0278245SMiao Xie /* 170505323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 170605323cd1SMiao Xie * needn't do anything special here. 1707d0278245SMiao Xie */ 17080b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 170967c5e7d4SFilipe Manana 171067c5e7d4SFilipe Manana /* 171167c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 171267c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 171367c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 171467c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 171567c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 171667c5e7d4SFilipe Manana * unused block groups. 171767c5e7d4SFilipe Manana */ 17180b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1719d0278245SMiao Xie sleep: 1720838fe188SJiri Kosina if (!again) { 1721a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17228929ecfaSYan, Zheng if (!kthread_should_stop()) 1723a74a4b97SChris Mason schedule(); 1724a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1725a74a4b97SChris Mason } 1726a74a4b97SChris Mason } while (!kthread_should_stop()); 1727da288d28SFilipe Manana 1728da288d28SFilipe Manana /* 1729da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1730da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1731da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1732da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1733da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1734da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1735da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1736da288d28SFilipe Manana * since work queues and other resources were already released and 1737da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1738da288d28SFilipe Manana */ 1739da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1740da288d28SFilipe Manana if (IS_ERR(trans)) { 1741da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 17420b246afaSJeff Mahoney btrfs_err(fs_info, 1743da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1744da288d28SFilipe Manana PTR_ERR(trans)); 1745da288d28SFilipe Manana } else { 1746da288d28SFilipe Manana int ret; 1747da288d28SFilipe Manana 17483a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1749da288d28SFilipe Manana if (ret) 17500b246afaSJeff Mahoney btrfs_err(fs_info, 1751da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1752da288d28SFilipe Manana ret); 1753da288d28SFilipe Manana } 1754da288d28SFilipe Manana 1755a74a4b97SChris Mason return 0; 1756a74a4b97SChris Mason } 1757a74a4b97SChris Mason 1758a74a4b97SChris Mason static int transaction_kthread(void *arg) 1759a74a4b97SChris Mason { 1760a74a4b97SChris Mason struct btrfs_root *root = arg; 17610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1762a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1763a74a4b97SChris Mason struct btrfs_transaction *cur; 17648929ecfaSYan, Zheng u64 transid; 1765a74a4b97SChris Mason unsigned long now; 1766a74a4b97SChris Mason unsigned long delay; 1767914b2007SJan Kara bool cannot_commit; 1768a74a4b97SChris Mason 1769a74a4b97SChris Mason do { 1770914b2007SJan Kara cannot_commit = false; 17710b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17720b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1773a74a4b97SChris Mason 17740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17750b246afaSJeff Mahoney cur = fs_info->running_transaction; 1776a74a4b97SChris Mason if (!cur) { 17770b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1778a74a4b97SChris Mason goto sleep; 1779a74a4b97SChris Mason } 178031153d81SYan Zheng 1781a74a4b97SChris Mason now = get_seconds(); 17824a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 17838b87dc17SDavid Sterba (now < cur->start_time || 17840b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17850b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1786a74a4b97SChris Mason delay = HZ * 5; 1787a74a4b97SChris Mason goto sleep; 1788a74a4b97SChris Mason } 17898929ecfaSYan, Zheng transid = cur->transid; 17900b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 179156bec294SChris Mason 179279787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1793354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1794914b2007SJan Kara if (IS_ERR(trans)) { 1795354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1796914b2007SJan Kara cannot_commit = true; 179779787eaaSJeff Mahoney goto sleep; 1798914b2007SJan Kara } 17998929ecfaSYan, Zheng if (transid == trans->transid) { 18003a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18018929ecfaSYan, Zheng } else { 18023a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18038929ecfaSYan, Zheng } 1804a74a4b97SChris Mason sleep: 18050b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18060b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1807a74a4b97SChris Mason 18084e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18090b246afaSJeff Mahoney &fs_info->fs_state))) 18102ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 1811a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18128929ecfaSYan, Zheng if (!kthread_should_stop() && 18130b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1814914b2007SJan Kara cannot_commit)) 1815a74a4b97SChris Mason schedule_timeout(delay); 1816a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1817a74a4b97SChris Mason } while (!kthread_should_stop()); 1818a74a4b97SChris Mason return 0; 1819a74a4b97SChris Mason } 1820a74a4b97SChris Mason 1821af31f5e5SChris Mason /* 1822af31f5e5SChris Mason * this will find the highest generation in the array of 1823af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1824af31f5e5SChris Mason * or -1 if we can't find anything. 1825af31f5e5SChris Mason * 1826af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1827af31f5e5SChris Mason * generation of the latest root in the array with the generation 1828af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1829af31f5e5SChris Mason */ 1830af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1831af31f5e5SChris Mason { 1832af31f5e5SChris Mason u64 cur; 1833af31f5e5SChris Mason int newest_index = -1; 1834af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1835af31f5e5SChris Mason int i; 1836af31f5e5SChris Mason 1837af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1838af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1839af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1840af31f5e5SChris Mason if (cur == newest_gen) 1841af31f5e5SChris Mason newest_index = i; 1842af31f5e5SChris Mason } 1843af31f5e5SChris Mason 1844af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1845af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1846af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1847af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1848af31f5e5SChris Mason if (cur == newest_gen) 1849af31f5e5SChris Mason newest_index = 0; 1850af31f5e5SChris Mason } 1851af31f5e5SChris Mason return newest_index; 1852af31f5e5SChris Mason } 1853af31f5e5SChris Mason 1854af31f5e5SChris Mason 1855af31f5e5SChris Mason /* 1856af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1857af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1858af31f5e5SChris Mason * field in the fs_info struct 1859af31f5e5SChris Mason */ 1860af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1861af31f5e5SChris Mason u64 newest_gen) 1862af31f5e5SChris Mason { 1863af31f5e5SChris Mason int newest_index = -1; 1864af31f5e5SChris Mason 1865af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1866af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1867af31f5e5SChris Mason if (newest_index == -1) { 1868af31f5e5SChris Mason info->backup_root_index = 0; 1869af31f5e5SChris Mason } else { 1870af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1871af31f5e5SChris Mason } 1872af31f5e5SChris Mason } 1873af31f5e5SChris Mason 1874af31f5e5SChris Mason /* 1875af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1876af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1877af31f5e5SChris Mason * done 1878af31f5e5SChris Mason */ 1879af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1880af31f5e5SChris Mason { 1881af31f5e5SChris Mason int next_backup; 1882af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1883af31f5e5SChris Mason int last_backup; 1884af31f5e5SChris Mason 1885af31f5e5SChris Mason next_backup = info->backup_root_index; 1886af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1887af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason /* 1890af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1891af31f5e5SChris Mason * this happens only at umount 1892af31f5e5SChris Mason */ 1893af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1894af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1895af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1896af31f5e5SChris Mason next_backup = last_backup; 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1899af31f5e5SChris Mason 1900af31f5e5SChris Mason /* 1901af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1902af31f5e5SChris Mason * regardless of which ones we use today 1903af31f5e5SChris Mason */ 1904af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1905af31f5e5SChris Mason 1906af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1907af31f5e5SChris Mason 1908af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1909af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1910af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1911af31f5e5SChris Mason 1912af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1913af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1914af31f5e5SChris Mason 1915af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1916af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1917af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1918af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1919af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1920af31f5e5SChris Mason 1921af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1922af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1923af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1924af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1925af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1926af31f5e5SChris Mason 19277c7e82a7SChris Mason /* 19287c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19297c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19307c7e82a7SChris Mason */ 19317c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19327c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19337c7e82a7SChris Mason info->fs_root->node->start); 1934af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1935af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1936af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1937af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19387c7e82a7SChris Mason } 1939af31f5e5SChris Mason 1940af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1941af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1942af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1943af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1944af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1945af31f5e5SChris Mason 1946af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1947af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1948af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1949af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1950af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1951af31f5e5SChris Mason 1952af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1953af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1954af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1955af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1956af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1957af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason /* 1960af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1961af31f5e5SChris Mason * for the next commit 1962af31f5e5SChris Mason */ 1963af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1964af31f5e5SChris Mason &info->super_for_commit->super_roots, 1965af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1966af31f5e5SChris Mason } 1967af31f5e5SChris Mason 1968af31f5e5SChris Mason /* 1969af31f5e5SChris Mason * this copies info out of the root backup array and back into 1970af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1971af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1972af31f5e5SChris Mason * tried and the last backup index you used. 1973af31f5e5SChris Mason * 1974af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1975af31f5e5SChris Mason */ 1976af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1977af31f5e5SChris Mason struct btrfs_super_block *super, 1978af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1979af31f5e5SChris Mason { 1980af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1981af31f5e5SChris Mason int newest = *backup_index; 1982af31f5e5SChris Mason 1983af31f5e5SChris Mason if (*num_backups_tried == 0) { 1984af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1985af31f5e5SChris Mason 1986af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1987af31f5e5SChris Mason if (newest == -1) 1988af31f5e5SChris Mason return -1; 1989af31f5e5SChris Mason 1990af31f5e5SChris Mason *backup_index = newest; 1991af31f5e5SChris Mason *num_backups_tried = 1; 1992af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1993af31f5e5SChris Mason /* we've tried all the backups, all done */ 1994af31f5e5SChris Mason return -1; 1995af31f5e5SChris Mason } else { 1996af31f5e5SChris Mason /* jump to the next oldest backup */ 1997af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1998af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1999af31f5e5SChris Mason *backup_index = newest; 2000af31f5e5SChris Mason *num_backups_tried += 1; 2001af31f5e5SChris Mason } 2002af31f5e5SChris Mason root_backup = super->super_roots + newest; 2003af31f5e5SChris Mason 2004af31f5e5SChris Mason btrfs_set_super_generation(super, 2005af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2006af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2007af31f5e5SChris Mason btrfs_set_super_root_level(super, 2008af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2009af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2010af31f5e5SChris Mason 2011af31f5e5SChris Mason /* 2012af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2013af31f5e5SChris Mason * need a fsck 2014af31f5e5SChris Mason */ 2015af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2016af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2017af31f5e5SChris Mason return 0; 2018af31f5e5SChris Mason } 2019af31f5e5SChris Mason 20207abadb64SLiu Bo /* helper to cleanup workers */ 20217abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20227abadb64SLiu Bo { 2023dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2024afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20255cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2026fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2027fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20288b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2029d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2030fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2031fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2032a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20335b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2034e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2035736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2036a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2037fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2038a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2039a9b9477dSFilipe Manana /* 2040a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2041a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2042a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2043a9b9477dSFilipe Manana */ 2044a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2045a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20467abadb64SLiu Bo } 20477abadb64SLiu Bo 20482e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20492e9f5954SRashika { 20502e9f5954SRashika if (root) { 20512e9f5954SRashika free_extent_buffer(root->node); 20522e9f5954SRashika free_extent_buffer(root->commit_root); 20532e9f5954SRashika root->node = NULL; 20542e9f5954SRashika root->commit_root = NULL; 20552e9f5954SRashika } 20562e9f5954SRashika } 20572e9f5954SRashika 2058af31f5e5SChris Mason /* helper to cleanup tree roots */ 2059af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2060af31f5e5SChris Mason { 20612e9f5954SRashika free_root_extent_buffers(info->tree_root); 2062655b09feSJosef Bacik 20632e9f5954SRashika free_root_extent_buffers(info->dev_root); 20642e9f5954SRashika free_root_extent_buffers(info->extent_root); 20652e9f5954SRashika free_root_extent_buffers(info->csum_root); 20662e9f5954SRashika free_root_extent_buffers(info->quota_root); 20672e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20682e9f5954SRashika if (chunk_root) 20692e9f5954SRashika free_root_extent_buffers(info->chunk_root); 207070f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2071af31f5e5SChris Mason } 2072af31f5e5SChris Mason 2073faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2074171f6537SJosef Bacik { 2075171f6537SJosef Bacik int ret; 2076171f6537SJosef Bacik struct btrfs_root *gang[8]; 2077171f6537SJosef Bacik int i; 2078171f6537SJosef Bacik 2079171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2080171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2081171f6537SJosef Bacik struct btrfs_root, root_list); 2082171f6537SJosef Bacik list_del(&gang[0]->root_list); 2083171f6537SJosef Bacik 208427cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2085cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2086171f6537SJosef Bacik } else { 2087171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2088171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2089b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2090171f6537SJosef Bacik } 2091171f6537SJosef Bacik } 2092171f6537SJosef Bacik 2093171f6537SJosef Bacik while (1) { 2094171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2095171f6537SJosef Bacik (void **)gang, 0, 2096171f6537SJosef Bacik ARRAY_SIZE(gang)); 2097171f6537SJosef Bacik if (!ret) 2098171f6537SJosef Bacik break; 2099171f6537SJosef Bacik for (i = 0; i < ret; i++) 2100cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2101171f6537SJosef Bacik } 21021a4319ccSLiu Bo 21031a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 21041a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 21052ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 21061a4319ccSLiu Bo } 2107171f6537SJosef Bacik } 2108af31f5e5SChris Mason 2109638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2110638aa7edSEric Sandeen { 2111638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2112638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2113638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2114638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2115638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2116638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2117638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2118638aa7edSEric Sandeen } 2119638aa7edSEric Sandeen 2120779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2121779a65a4SEric Sandeen { 2122779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2123779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2124779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2125779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2126779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2127779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2128779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2129779a65a4SEric Sandeen } 2130779a65a4SEric Sandeen 21316bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2132f37938e0SEric Sandeen { 21332ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21342ff7e61eSJeff Mahoney 21352ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21362ff7e61eSJeff Mahoney set_nlink(inode, 1); 2137f37938e0SEric Sandeen /* 2138f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2139f37938e0SEric Sandeen * the real end of the address space is determined by all of 2140f37938e0SEric Sandeen * the devices in the system 2141f37938e0SEric Sandeen */ 21422ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21432ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2144f37938e0SEric Sandeen 21452ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 2146c6100a4bSJosef Bacik extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode); 21472ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 21482ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2149f37938e0SEric Sandeen 21502ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2151f37938e0SEric Sandeen 21522ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21532ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21542ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21552ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2156f37938e0SEric Sandeen } 2157f37938e0SEric Sandeen 2158ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2159ad618368SEric Sandeen { 2160ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2161ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2162ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 216373beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 216473beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 216573beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2166ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 216773beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2168ad618368SEric Sandeen } 2169ad618368SEric Sandeen 2170f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2171f9e92e40SEric Sandeen { 2172f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2173f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2174f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2175f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2176f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2177f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2178f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2179d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2180f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2181f9e92e40SEric Sandeen } 2182f9e92e40SEric Sandeen 21832a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21842a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21852a458198SEric Sandeen { 21862a458198SEric Sandeen int max_active = fs_info->thread_pool_size; 21876f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21882a458198SEric Sandeen 21892a458198SEric Sandeen fs_info->workers = 2190cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2191cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21922a458198SEric Sandeen 21932a458198SEric Sandeen fs_info->delalloc_workers = 2194cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2195cb001095SJeff Mahoney flags, max_active, 2); 21962a458198SEric Sandeen 21972a458198SEric Sandeen fs_info->flush_workers = 2198cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2199cb001095SJeff Mahoney flags, max_active, 0); 22002a458198SEric Sandeen 22012a458198SEric Sandeen fs_info->caching_workers = 2202cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22032a458198SEric Sandeen 22042a458198SEric Sandeen /* 22052a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 22062a458198SEric Sandeen * likely that bios will be send down in a sane order to the 22072a458198SEric Sandeen * devices 22082a458198SEric Sandeen */ 22092a458198SEric Sandeen fs_info->submit_workers = 2210cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 22112a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22122a458198SEric Sandeen max_active), 64); 22132a458198SEric Sandeen 22142a458198SEric Sandeen fs_info->fixup_workers = 2215cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22162a458198SEric Sandeen 22172a458198SEric Sandeen /* 22182a458198SEric Sandeen * endios are largely parallel and should have a very 22192a458198SEric Sandeen * low idle thresh 22202a458198SEric Sandeen */ 22212a458198SEric Sandeen fs_info->endio_workers = 2222cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22232a458198SEric Sandeen fs_info->endio_meta_workers = 2224cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2225cb001095SJeff Mahoney max_active, 4); 22262a458198SEric Sandeen fs_info->endio_meta_write_workers = 2227cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2228cb001095SJeff Mahoney max_active, 2); 22292a458198SEric Sandeen fs_info->endio_raid56_workers = 2230cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2231cb001095SJeff Mahoney max_active, 4); 22322a458198SEric Sandeen fs_info->endio_repair_workers = 2233cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 22342a458198SEric Sandeen fs_info->rmw_workers = 2235cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22362a458198SEric Sandeen fs_info->endio_write_workers = 2237cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2238cb001095SJeff Mahoney max_active, 2); 22392a458198SEric Sandeen fs_info->endio_freespace_worker = 2240cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2241cb001095SJeff Mahoney max_active, 0); 22422a458198SEric Sandeen fs_info->delayed_workers = 2243cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2244cb001095SJeff Mahoney max_active, 0); 22452a458198SEric Sandeen fs_info->readahead_workers = 2246cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2247cb001095SJeff Mahoney max_active, 2); 22482a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2249cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 22502a458198SEric Sandeen fs_info->extent_workers = 2251cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 22522a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22532a458198SEric Sandeen max_active), 8); 22542a458198SEric Sandeen 22552a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 22562a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 22572a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22582a458198SEric Sandeen fs_info->endio_meta_write_workers && 22592a458198SEric Sandeen fs_info->endio_repair_workers && 22602a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22612a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22622a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22632a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 22642a458198SEric Sandeen fs_info->extent_workers && 22652a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 22662a458198SEric Sandeen return -ENOMEM; 22672a458198SEric Sandeen } 22682a458198SEric Sandeen 22692a458198SEric Sandeen return 0; 22702a458198SEric Sandeen } 22712a458198SEric Sandeen 227263443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 227363443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 227463443bf5SEric Sandeen { 227563443bf5SEric Sandeen int ret; 227663443bf5SEric Sandeen struct btrfs_root *log_tree_root; 227763443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 227863443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 227963443bf5SEric Sandeen 228063443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2281f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 228263443bf5SEric Sandeen return -EIO; 228363443bf5SEric Sandeen } 228463443bf5SEric Sandeen 228574e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 228663443bf5SEric Sandeen if (!log_tree_root) 228763443bf5SEric Sandeen return -ENOMEM; 228863443bf5SEric Sandeen 2289da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 229063443bf5SEric Sandeen 22912ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 229263443bf5SEric Sandeen fs_info->generation + 1); 229364c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2294f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 22950eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 229664c043deSLiu Bo kfree(log_tree_root); 22970eeff236SLiu Bo return ret; 229864c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2299f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 230063443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 230163443bf5SEric Sandeen kfree(log_tree_root); 230263443bf5SEric Sandeen return -EIO; 230363443bf5SEric Sandeen } 230463443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 230563443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 230663443bf5SEric Sandeen if (ret) { 23070b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 230863443bf5SEric Sandeen "Failed to recover log tree"); 230963443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 231063443bf5SEric Sandeen kfree(log_tree_root); 231163443bf5SEric Sandeen return ret; 231263443bf5SEric Sandeen } 231363443bf5SEric Sandeen 2314bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23156bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 231663443bf5SEric Sandeen if (ret) 231763443bf5SEric Sandeen return ret; 231863443bf5SEric Sandeen } 231963443bf5SEric Sandeen 232063443bf5SEric Sandeen return 0; 232163443bf5SEric Sandeen } 232263443bf5SEric Sandeen 23236bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23244bbcaa64SEric Sandeen { 23256bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2326a4f3d2c4SDavid Sterba struct btrfs_root *root; 23274bbcaa64SEric Sandeen struct btrfs_key location; 23284bbcaa64SEric Sandeen int ret; 23294bbcaa64SEric Sandeen 23306bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23316bccf3abSJeff Mahoney 23324bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23334bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23344bbcaa64SEric Sandeen location.offset = 0; 23354bbcaa64SEric Sandeen 2336a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2337a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2338a4f3d2c4SDavid Sterba return PTR_ERR(root); 2339a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2340a4f3d2c4SDavid Sterba fs_info->extent_root = root; 23414bbcaa64SEric Sandeen 23424bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2343a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2344a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2345a4f3d2c4SDavid Sterba return PTR_ERR(root); 2346a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2347a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23484bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23494bbcaa64SEric Sandeen 23504bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2351a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2352a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2353a4f3d2c4SDavid Sterba return PTR_ERR(root); 2354a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2355a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23564bbcaa64SEric Sandeen 23574bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2358a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2359a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2360a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2361afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2362a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23634bbcaa64SEric Sandeen } 23644bbcaa64SEric Sandeen 23654bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2366a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2367a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2368a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23694bbcaa64SEric Sandeen if (ret != -ENOENT) 23704bbcaa64SEric Sandeen return ret; 23714bbcaa64SEric Sandeen } else { 2372a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2373a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23744bbcaa64SEric Sandeen } 23754bbcaa64SEric Sandeen 237670f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 237770f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 237870f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 237970f6d82eSOmar Sandoval if (IS_ERR(root)) 238070f6d82eSOmar Sandoval return PTR_ERR(root); 238170f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 238270f6d82eSOmar Sandoval fs_info->free_space_root = root; 238370f6d82eSOmar Sandoval } 238470f6d82eSOmar Sandoval 23854bbcaa64SEric Sandeen return 0; 23864bbcaa64SEric Sandeen } 23874bbcaa64SEric Sandeen 2388ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2389dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2390dfe25020SChris Mason char *options) 2391eb60ceacSChris Mason { 2392db94535dSChris Mason u32 sectorsize; 2393db94535dSChris Mason u32 nodesize; 239487ee04ebSChris Mason u32 stripesize; 239584234f3aSYan Zheng u64 generation; 2396f2b636e8SJosef Bacik u64 features; 23973de4586cSChris Mason struct btrfs_key location; 2398a061fc8dSChris Mason struct buffer_head *bh; 23994d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2400815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2401f84a8bd6SAl Viro struct btrfs_root *tree_root; 24024d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2403eb60ceacSChris Mason int ret; 2404e58ca020SYan int err = -EINVAL; 2405af31f5e5SChris Mason int num_backups_tried = 0; 2406af31f5e5SChris Mason int backup_index = 0; 24075cdc7ad3SQu Wenruo int max_active; 24086675df31SOmar Sandoval int clear_free_space_tree = 0; 24094543df7eSChris Mason 241074e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 241174e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2412cb517eabSMiao Xie if (!tree_root || !chunk_root) { 241339279cc3SChris Mason err = -ENOMEM; 241439279cc3SChris Mason goto fail; 241539279cc3SChris Mason } 241676dda93cSYan, Zheng 241776dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 241876dda93cSYan, Zheng if (ret) { 241976dda93cSYan, Zheng err = ret; 242076dda93cSYan, Zheng goto fail; 242176dda93cSYan, Zheng } 242276dda93cSYan, Zheng 2423908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2424e2d84521SMiao Xie if (ret) { 2425e2d84521SMiao Xie err = ret; 24269e11ceeeSJan Kara goto fail_srcu; 2427e2d84521SMiao Xie } 242809cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2429e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2430e2d84521SMiao Xie 2431908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2432963d678bSMiao Xie if (ret) { 2433963d678bSMiao Xie err = ret; 2434963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2435963d678bSMiao Xie } 2436963d678bSMiao Xie 2437908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2438c404e0dcSMiao Xie if (ret) { 2439c404e0dcSMiao Xie err = ret; 2440c404e0dcSMiao Xie goto fail_delalloc_bytes; 2441c404e0dcSMiao Xie } 2442c404e0dcSMiao Xie 244376dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2444f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 24458fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2446facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 244724bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2448eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 244911833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2450eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2451a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 245276dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 245324bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 24544cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2455f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2456ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2457fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2458f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 245947ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2460f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2461d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 246267c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 24637585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2464573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2465c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2466de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 246719c00ddcSChris Mason 24680b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 24696324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2470f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 247147ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 24720b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 247366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 247466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 247566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 247666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 247766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 247866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 247966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2480771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 24814cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2482fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 24832fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2484fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2485e20d96d6SChris Mason fs_info->sb = sb; 248695ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 24879ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 24884cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2489a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2490f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 24918b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2492f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 249390519d66SArne Jansen /* readahead state */ 2494d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 249590519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2496fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2497c8b97818SChris Mason 2498b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2499b34b086cSChris Mason num_online_cpus() + 2, 8); 25000afbaf8cSChris Mason 2501199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2502199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 250369fe2d75SJosef Bacik 250469fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 250569fe2d75SJosef Bacik if (!fs_info->btree_inode) { 250669fe2d75SJosef Bacik err = -ENOMEM; 250769fe2d75SJosef Bacik goto fail_bio_counter; 250869fe2d75SJosef Bacik } 250969fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 251069fe2d75SJosef Bacik 251116cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 251274e4d827SDavid Sterba GFP_KERNEL); 251316cdcec7SMiao Xie if (!fs_info->delayed_root) { 251416cdcec7SMiao Xie err = -ENOMEM; 251516cdcec7SMiao Xie goto fail_iput; 251616cdcec7SMiao Xie } 251716cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 25183eaa2885SChris Mason 2519638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 252021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 252121adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 252221adbd5cSStefan Behrens #endif 2523779a65a4SEric Sandeen btrfs_init_balance(fs_info); 252421c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2525a2de733cSArne Jansen 25269f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 25279f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2528a061fc8dSChris Mason 25296bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 253039279cc3SChris Mason 2531e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 25326bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2533a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2534925baeddSChris Mason 2535c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[0], NULL); 2536c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[1], NULL); 253711833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2538afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2539509659cdSChris Mason 25405a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 254179154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2542925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2543a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2544a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 25457d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 25461bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 25479e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2548c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 254976dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2550803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2551fa9c0d79SChris Mason 2552ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2553f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2554416ac51dSArne Jansen 2555fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2556fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2557fa9c0d79SChris Mason 2558e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2559f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2560bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 25614854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 25623768f368SChris Mason 256304216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 256404216820SFilipe Manana 2565da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2566da17066cSJeff Mahoney fs_info->nodesize = 4096; 2567da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2568da17066cSJeff Mahoney fs_info->stripesize = 4096; 2569da17066cSJeff Mahoney 257053b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 257153b381b3SDavid Woodhouse if (ret) { 257283c8266aSDavid Sterba err = ret; 257353b381b3SDavid Woodhouse goto fail_alloc; 257453b381b3SDavid Woodhouse } 257553b381b3SDavid Woodhouse 2576da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 25777eccb903SChris Mason 25783c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 25791104a885SDavid Sterba 25801104a885SDavid Sterba /* 25811104a885SDavid Sterba * Read super block and check the signature bytes only 25821104a885SDavid Sterba */ 2583a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 258492fc03fbSAnand Jain if (IS_ERR(bh)) { 258592fc03fbSAnand Jain err = PTR_ERR(bh); 258616cdcec7SMiao Xie goto fail_alloc; 258720b45077SDave Young } 258839279cc3SChris Mason 25891104a885SDavid Sterba /* 25901104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 25911104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 25921104a885SDavid Sterba */ 2593ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 259405135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 25951104a885SDavid Sterba err = -EINVAL; 2596b2acdddfSAnand Jain brelse(bh); 25971104a885SDavid Sterba goto fail_alloc; 25981104a885SDavid Sterba } 25991104a885SDavid Sterba 26001104a885SDavid Sterba /* 26011104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 26021104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 26031104a885SDavid Sterba * the whole block of INFO_SIZE 26041104a885SDavid Sterba */ 26056c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 26066c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 26076c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2608a061fc8dSChris Mason brelse(bh); 26095f39d397SChris Mason 26106c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 26110b86a832SChris Mason 26123d3a126aSDavid Sterba ret = btrfs_check_super_valid(fs_info); 26131104a885SDavid Sterba if (ret) { 261405135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 26151104a885SDavid Sterba err = -EINVAL; 26161104a885SDavid Sterba goto fail_alloc; 26171104a885SDavid Sterba } 26181104a885SDavid Sterba 26196c41761fSDavid Sterba disk_super = fs_info->super_copy; 26200f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 262116cdcec7SMiao Xie goto fail_alloc; 26220f7d52f4SChris Mason 2623acce952bSliubo /* check FS state, whether FS is broken. */ 262487533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 262587533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2626acce952bSliubo 262775e7cb7fSLiu Bo /* 2628af31f5e5SChris Mason * run through our array of backup supers and setup 2629af31f5e5SChris Mason * our ring pointer to the oldest one 2630af31f5e5SChris Mason */ 2631af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2632af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2633af31f5e5SChris Mason 2634af31f5e5SChris Mason /* 263575e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 263675e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 263775e7cb7fSLiu Bo */ 263875e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 263975e7cb7fSLiu Bo 26402ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 26412b82032cSYan Zheng if (ret) { 26422b82032cSYan Zheng err = ret; 264316cdcec7SMiao Xie goto fail_alloc; 26442b82032cSYan Zheng } 2645dfe25020SChris Mason 2646f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2647f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2648f2b636e8SJosef Bacik if (features) { 264905135f59SDavid Sterba btrfs_err(fs_info, 265005135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2651c1c9ff7cSGeert Uytterhoeven features); 2652f2b636e8SJosef Bacik err = -EINVAL; 265316cdcec7SMiao Xie goto fail_alloc; 2654f2b636e8SJosef Bacik } 2655f2b636e8SJosef Bacik 26565d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 26575d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 26580b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2659a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 26605c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 26615c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2662727011e0SChris Mason 26633173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 266405135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 26653173a18fSJosef Bacik 2666727011e0SChris Mason /* 2667727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2668727011e0SChris Mason * they are bigger than the page size 2669727011e0SChris Mason */ 267009cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2671727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 267205135f59SDavid Sterba btrfs_info(fs_info, 267305135f59SDavid Sterba "flagging fs with big metadata feature"); 2674727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2675727011e0SChris Mason } 2676727011e0SChris Mason 2677bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2678bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2679b7f67055SChandan Rajendra stripesize = sectorsize; 2680707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2681963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2682bc3f116fSChris Mason 2683da17066cSJeff Mahoney /* Cache block sizes */ 2684da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2685da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2686da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2687da17066cSJeff Mahoney 2688bc3f116fSChris Mason /* 2689bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2690bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2691bc3f116fSChris Mason */ 2692bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2693707e8a07SDavid Sterba (sectorsize != nodesize)) { 269405135f59SDavid Sterba btrfs_err(fs_info, 269505135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 269605135f59SDavid Sterba nodesize, sectorsize); 2697bc3f116fSChris Mason goto fail_alloc; 2698bc3f116fSChris Mason } 2699bc3f116fSChris Mason 2700ceda0864SMiao Xie /* 2701ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2702ceda0864SMiao Xie * update the flag. 2703ceda0864SMiao Xie */ 27045d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 27055d4f98a2SYan Zheng 2706f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2707f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2708bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 270905135f59SDavid Sterba btrfs_err(fs_info, 271005135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2711c1c9ff7cSGeert Uytterhoeven features); 2712f2b636e8SJosef Bacik err = -EINVAL; 271316cdcec7SMiao Xie goto fail_alloc; 2714f2b636e8SJosef Bacik } 271561d92c32SChris Mason 27165cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 271761d92c32SChris Mason 27182a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 27192a458198SEric Sandeen if (ret) { 27202a458198SEric Sandeen err = ret; 27210dc3b84aSJosef Bacik goto fail_sb_buffer; 27220dc3b84aSJosef Bacik } 27234543df7eSChris Mason 27249e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 27259e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 27269e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 2727d4417e22SNikolay Borisov sb->s_bdi->ra_pages = VM_MAX_READAHEAD * SZ_1K / PAGE_SIZE; 27289e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 27299e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 27304575c9ccSChris Mason 2731a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2732a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2733ee87cf5eSAnand Jain memcpy(&sb->s_uuid, fs_info->fsid, BTRFS_FSID_SIZE); 2734db94535dSChris Mason 2735925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 27366bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2737925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 273884eed90fSChris Mason if (ret) { 273905135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 27405d4f98a2SYan Zheng goto fail_sb_buffer; 274184eed90fSChris Mason } 27420b86a832SChris Mason 274384234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 27440b86a832SChris Mason 2745da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 27460b86a832SChris Mason 27472ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 27480b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2749ce86cd59SDavid Sterba generation); 275064c043deSLiu Bo if (IS_ERR(chunk_root->node) || 275164c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 275205135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2753e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2754e5fffbacSchandan free_extent_buffer(chunk_root->node); 275595ab1f64SZhao Lei chunk_root->node = NULL; 2756af31f5e5SChris Mason goto fail_tree_roots; 275783121942SDavid Woodhouse } 27585d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 27595d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 27600b86a832SChris Mason 2761e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2762b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2763e17cade2SChris Mason 27645b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 27652b82032cSYan Zheng if (ret) { 276605135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2767af31f5e5SChris Mason goto fail_tree_roots; 27682b82032cSYan Zheng } 27690b86a832SChris Mason 27708dabb742SStefan Behrens /* 27718dabb742SStefan Behrens * keep the device that is marked to be the target device for the 27728dabb742SStefan Behrens * dev_replace procedure 27738dabb742SStefan Behrens */ 27749eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 0); 2775dfe25020SChris Mason 2776a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 277705135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2778a6b0d5c8SChris Mason goto fail_tree_roots; 2779a6b0d5c8SChris Mason } 2780a6b0d5c8SChris Mason 2781af31f5e5SChris Mason retry_root_backup: 278284234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 27830b86a832SChris Mason 27842ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2785db94535dSChris Mason btrfs_super_root(disk_super), 2786ce86cd59SDavid Sterba generation); 278764c043deSLiu Bo if (IS_ERR(tree_root->node) || 278864c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 278905135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2790e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2791e5fffbacSchandan free_extent_buffer(tree_root->node); 279295ab1f64SZhao Lei tree_root->node = NULL; 2793af31f5e5SChris Mason goto recovery_tree_root; 279483121942SDavid Woodhouse } 2795af31f5e5SChris Mason 27965d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 27975d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 279869e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2799db94535dSChris Mason 2800f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2801f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2802f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2803f32e48e9SChandan Rajendra if (ret) { 2804f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2805f32e48e9SChandan Rajendra goto recovery_tree_root; 2806f32e48e9SChandan Rajendra } 2807f32e48e9SChandan Rajendra 2808f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2809f32e48e9SChandan Rajendra 2810f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2811f32e48e9SChandan Rajendra 28126bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 28134bbcaa64SEric Sandeen if (ret) 2814af31f5e5SChris Mason goto recovery_tree_root; 2815f7a81ea4SStefan Behrens 28168929ecfaSYan, Zheng fs_info->generation = generation; 28178929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 28188929ecfaSYan, Zheng 281968310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 282068310a5eSIlya Dryomov if (ret) { 282105135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 282268310a5eSIlya Dryomov goto fail_block_groups; 282368310a5eSIlya Dryomov } 282468310a5eSIlya Dryomov 2825733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2826733f4fbbSStefan Behrens if (ret) { 282705135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 2828733f4fbbSStefan Behrens goto fail_block_groups; 2829733f4fbbSStefan Behrens } 2830733f4fbbSStefan Behrens 28318dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 28328dabb742SStefan Behrens if (ret) { 283305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 28348dabb742SStefan Behrens goto fail_block_groups; 28358dabb742SStefan Behrens } 28368dabb742SStefan Behrens 28379eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 1); 28388dabb742SStefan Behrens 2839b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 2840b7c35e81SAnand Jain if (ret) { 284105135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 284205135f59SDavid Sterba ret); 2843b7c35e81SAnand Jain goto fail_block_groups; 2844b7c35e81SAnand Jain } 2845b7c35e81SAnand Jain 2846b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 2847b7c35e81SAnand Jain if (ret) { 284805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 284905135f59SDavid Sterba ret); 2850b7c35e81SAnand Jain goto fail_fsdev_sysfs; 2851b7c35e81SAnand Jain } 2852b7c35e81SAnand Jain 285396f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 28545ac1d209SJeff Mahoney if (ret) { 285505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 2856b7c35e81SAnand Jain goto fail_fsdev_sysfs; 28575ac1d209SJeff Mahoney } 28585ac1d209SJeff Mahoney 2859c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2860c59021f8Sliubo if (ret) { 286105135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 28622365dd3cSAnand Jain goto fail_sysfs; 2863c59021f8Sliubo } 2864c59021f8Sliubo 28655b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 28661b1d1f66SJosef Bacik if (ret) { 286705135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 28682365dd3cSAnand Jain goto fail_sysfs; 28691b1d1f66SJosef Bacik } 28704330e183SQu Wenruo 28716528b99dSAnand Jain if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) { 287205135f59SDavid Sterba btrfs_warn(fs_info, 28734330e183SQu Wenruo "writeable mount is not allowed due to too many missing devices"); 28742365dd3cSAnand Jain goto fail_sysfs; 2875292fd7fcSStefan Behrens } 28769078a3e1SChris Mason 2877a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2878a74a4b97SChris Mason "btrfs-cleaner"); 287957506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 28802365dd3cSAnand Jain goto fail_sysfs; 2881a74a4b97SChris Mason 2882a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2883a74a4b97SChris Mason tree_root, 2884a74a4b97SChris Mason "btrfs-transaction"); 288557506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 28863f157a2fSChris Mason goto fail_cleaner; 2887a74a4b97SChris Mason 2888583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 2889c289811cSChris Mason !fs_info->fs_devices->rotating) { 2890583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 2891c289811cSChris Mason } 2892c289811cSChris Mason 2893572d9ab7SDavid Sterba /* 289401327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 2895572d9ab7SDavid Sterba * not have to wait for transaction commit 2896572d9ab7SDavid Sterba */ 2897572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 28983818aea2SQu Wenruo 289921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 29000b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 29012ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 29020b246afaSJeff Mahoney btrfs_test_opt(fs_info, 290321adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 290421adbd5cSStefan Behrens 1 : 0, 290521adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 290621adbd5cSStefan Behrens if (ret) 290705135f59SDavid Sterba btrfs_warn(fs_info, 290805135f59SDavid Sterba "failed to initialize integrity check module: %d", 290905135f59SDavid Sterba ret); 291021adbd5cSStefan Behrens } 291121adbd5cSStefan Behrens #endif 2912bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2913bcef60f2SArne Jansen if (ret) 2914bcef60f2SArne Jansen goto fail_trans_kthread; 291521adbd5cSStefan Behrens 2916fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 2917fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 2918fd708b81SJosef Bacik 291996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 292096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 29210b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 292263443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 292379787eaaSJeff Mahoney if (ret) { 292463443bf5SEric Sandeen err = ret; 292528c16cbbSWang Shilong goto fail_qgroup; 2926e556ce2cSYan Zheng } 2927e02119d5SChris Mason } 29281a40e23bSZheng Yan 29296bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 293079787eaaSJeff Mahoney if (ret) 293128c16cbbSWang Shilong goto fail_qgroup; 293276dda93cSYan, Zheng 2933bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 2934d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 293544c44af2SIlya Dryomov if (ret) 293628c16cbbSWang Shilong goto fail_qgroup; 293790c711abSZygo Blaxell 293890c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 29395d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 294090c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 2941d7ce5843SMiao Xie if (ret < 0) { 294205135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 294305135f59SDavid Sterba ret); 2944d7ce5843SMiao Xie err = -EINVAL; 2945bcef60f2SArne Jansen goto fail_qgroup; 2946d7ce5843SMiao Xie } 29477c2ca468SChris Mason } 29481a40e23bSZheng Yan 29493de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 29503de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2951cb517eabSMiao Xie location.offset = 0; 29523de4586cSChris Mason 29533de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 29543140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 29553140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2956bcef60f2SArne Jansen goto fail_qgroup; 29573140c9a3SDan Carpenter } 2958c289811cSChris Mason 2959bc98a42cSDavid Howells if (sb_rdonly(sb)) 29602b6ba629SIlya Dryomov return 0; 29612b6ba629SIlya Dryomov 2962f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 2963f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 29646675df31SOmar Sandoval clear_free_space_tree = 1; 29656675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 29666675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 29676675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 29686675df31SOmar Sandoval clear_free_space_tree = 1; 29696675df31SOmar Sandoval } 29706675df31SOmar Sandoval 29716675df31SOmar Sandoval if (clear_free_space_tree) { 2972f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 2973f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 2974f8d468a1SOmar Sandoval if (ret) { 2975f8d468a1SOmar Sandoval btrfs_warn(fs_info, 2976f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 29776bccf3abSJeff Mahoney close_ctree(fs_info); 2978f8d468a1SOmar Sandoval return ret; 2979f8d468a1SOmar Sandoval } 2980f8d468a1SOmar Sandoval } 2981f8d468a1SOmar Sandoval 29820b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 2983511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 298405135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 2985511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 2986511711afSChris Mason if (ret) { 298705135f59SDavid Sterba btrfs_warn(fs_info, 298805135f59SDavid Sterba "failed to create free space tree: %d", ret); 29896bccf3abSJeff Mahoney close_ctree(fs_info); 2990511711afSChris Mason return ret; 2991511711afSChris Mason } 2992511711afSChris Mason } 2993511711afSChris Mason 2994e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 29952b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 29962b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 29972b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 29986bccf3abSJeff Mahoney close_ctree(fs_info); 29992b6ba629SIlya Dryomov return ret; 30002b6ba629SIlya Dryomov } 3001e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 300259641015SIlya Dryomov 30032b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 30042b6ba629SIlya Dryomov if (ret) { 300505135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 30066bccf3abSJeff Mahoney close_ctree(fs_info); 30072b6ba629SIlya Dryomov return ret; 3008e3acc2a6SJosef Bacik } 3009e3acc2a6SJosef Bacik 30108dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 30118dabb742SStefan Behrens if (ret) { 301205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 30136bccf3abSJeff Mahoney close_ctree(fs_info); 30148dabb742SStefan Behrens return ret; 30158dabb742SStefan Behrens } 30168dabb742SStefan Behrens 3017b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3018b382a324SJan Schmidt 30194bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 302005135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3021f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3022f7a81ea4SStefan Behrens if (ret) { 302305135f59SDavid Sterba btrfs_warn(fs_info, 302405135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 30256bccf3abSJeff Mahoney close_ctree(fs_info); 3026f7a81ea4SStefan Behrens return ret; 3027f7a81ea4SStefan Behrens } 30280b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 30294bbcaa64SEric Sandeen fs_info->generation != 30304bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 303105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 303270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 303370f80175SStefan Behrens if (ret) { 303405135f59SDavid Sterba btrfs_warn(fs_info, 303505135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 30366bccf3abSJeff Mahoney close_ctree(fs_info); 303770f80175SStefan Behrens return ret; 303870f80175SStefan Behrens } 303970f80175SStefan Behrens } else { 3040afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3041f7a81ea4SStefan Behrens } 3042afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 304347ab2a6cSJosef Bacik 30448dcddfa0SQu Wenruo /* 30458dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 30468dcddfa0SQu Wenruo * no need to keep the flag 30478dcddfa0SQu Wenruo */ 30488dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 30498dcddfa0SQu Wenruo 3050ad2b2c80SAl Viro return 0; 305139279cc3SChris Mason 3052bcef60f2SArne Jansen fail_qgroup: 3053bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 30547c2ca468SChris Mason fail_trans_kthread: 30557c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 30562ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3057faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 30583f157a2fSChris Mason fail_cleaner: 3059a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 30607c2ca468SChris Mason 30617c2ca468SChris Mason /* 30627c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 30637c2ca468SChris Mason * kthreads 30647c2ca468SChris Mason */ 30657c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 30667c2ca468SChris Mason 30672365dd3cSAnand Jain fail_sysfs: 30686618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 30692365dd3cSAnand Jain 3070b7c35e81SAnand Jain fail_fsdev_sysfs: 3071b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3072b7c35e81SAnand Jain 30731b1d1f66SJosef Bacik fail_block_groups: 307454067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3075af31f5e5SChris Mason 3076af31f5e5SChris Mason fail_tree_roots: 3077af31f5e5SChris Mason free_root_pointers(fs_info, 1); 30782b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3079af31f5e5SChris Mason 308039279cc3SChris Mason fail_sb_buffer: 30817abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 30825cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 308316cdcec7SMiao Xie fail_alloc: 30844543df7eSChris Mason fail_iput: 3085586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3086586e46e2SIlya Dryomov 30874543df7eSChris Mason iput(fs_info->btree_inode); 3088c404e0dcSMiao Xie fail_bio_counter: 3089c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3090963d678bSMiao Xie fail_delalloc_bytes: 3091963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3092e2d84521SMiao Xie fail_dirty_metadata_bytes: 3093e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 309476dda93cSYan, Zheng fail_srcu: 309576dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 30967e662854SQinghuang Feng fail: 309753b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3098586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3099ad2b2c80SAl Viro return err; 3100af31f5e5SChris Mason 3101af31f5e5SChris Mason recovery_tree_root: 31020b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3103af31f5e5SChris Mason goto fail_tree_roots; 3104af31f5e5SChris Mason 3105af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3106af31f5e5SChris Mason 3107af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3108af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3109af31f5e5SChris Mason 3110af31f5e5SChris Mason /* we can't trust the free space cache either */ 3111af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3112af31f5e5SChris Mason 3113af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3114af31f5e5SChris Mason &num_backups_tried, &backup_index); 3115af31f5e5SChris Mason if (ret == -1) 3116af31f5e5SChris Mason goto fail_block_groups; 3117af31f5e5SChris Mason goto retry_root_backup; 3118eb60ceacSChris Mason } 3119663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO); 3120eb60ceacSChris Mason 3121f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3122f2984462SChris Mason { 3123f2984462SChris Mason if (uptodate) { 3124f2984462SChris Mason set_buffer_uptodate(bh); 3125f2984462SChris Mason } else { 3126442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3127442a4f63SStefan Behrens bh->b_private; 3128442a4f63SStefan Behrens 3129fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3130b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3131606686eeSJosef Bacik rcu_str_deref(device->name)); 313201327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 31331259ab75SChris Mason * our own ways of dealing with the IO errors 31341259ab75SChris Mason */ 3135f2984462SChris Mason clear_buffer_uptodate(bh); 3136442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3137f2984462SChris Mason } 3138f2984462SChris Mason unlock_buffer(bh); 3139f2984462SChris Mason put_bh(bh); 3140f2984462SChris Mason } 3141f2984462SChris Mason 314229c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 314329c36d72SAnand Jain struct buffer_head **bh_ret) 314429c36d72SAnand Jain { 314529c36d72SAnand Jain struct buffer_head *bh; 314629c36d72SAnand Jain struct btrfs_super_block *super; 314729c36d72SAnand Jain u64 bytenr; 314829c36d72SAnand Jain 314929c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 315029c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 315129c36d72SAnand Jain return -EINVAL; 315229c36d72SAnand Jain 31539f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 315429c36d72SAnand Jain /* 315529c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 315629c36d72SAnand Jain * the best option we have is to mark it EIO. 315729c36d72SAnand Jain */ 315829c36d72SAnand Jain if (!bh) 315929c36d72SAnand Jain return -EIO; 316029c36d72SAnand Jain 316129c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 316229c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 316329c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 316429c36d72SAnand Jain brelse(bh); 316529c36d72SAnand Jain return -EINVAL; 316629c36d72SAnand Jain } 316729c36d72SAnand Jain 316829c36d72SAnand Jain *bh_ret = bh; 316929c36d72SAnand Jain return 0; 317029c36d72SAnand Jain } 317129c36d72SAnand Jain 317229c36d72SAnand Jain 3173a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3174a512bbf8SYan Zheng { 3175a512bbf8SYan Zheng struct buffer_head *bh; 3176a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3177a512bbf8SYan Zheng struct btrfs_super_block *super; 3178a512bbf8SYan Zheng int i; 3179a512bbf8SYan Zheng u64 transid = 0; 318092fc03fbSAnand Jain int ret = -EINVAL; 3181a512bbf8SYan Zheng 3182a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3183a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3184a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3185a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3186a512bbf8SYan Zheng */ 3187a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 318829c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 318929c36d72SAnand Jain if (ret) 3190a512bbf8SYan Zheng continue; 3191a512bbf8SYan Zheng 3192a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3193a512bbf8SYan Zheng 3194a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3195a512bbf8SYan Zheng brelse(latest); 3196a512bbf8SYan Zheng latest = bh; 3197a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3198a512bbf8SYan Zheng } else { 3199a512bbf8SYan Zheng brelse(bh); 3200a512bbf8SYan Zheng } 3201a512bbf8SYan Zheng } 320292fc03fbSAnand Jain 320392fc03fbSAnand Jain if (!latest) 320492fc03fbSAnand Jain return ERR_PTR(ret); 320592fc03fbSAnand Jain 3206a512bbf8SYan Zheng return latest; 3207a512bbf8SYan Zheng } 3208a512bbf8SYan Zheng 32094eedeb75SHisashi Hifumi /* 3210abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3211abbb3b8eSDavid Sterba * buffer heads we write are pinned. 32124eedeb75SHisashi Hifumi * 3213abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3214abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3215abbb3b8eSDavid Sterba * same for write and wait phases. 32164eedeb75SHisashi Hifumi * 3217abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 32184eedeb75SHisashi Hifumi */ 3219a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3220abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3221a512bbf8SYan Zheng { 3222a512bbf8SYan Zheng struct buffer_head *bh; 3223a512bbf8SYan Zheng int i; 3224a512bbf8SYan Zheng int ret; 3225a512bbf8SYan Zheng int errors = 0; 3226a512bbf8SYan Zheng u32 crc; 3227a512bbf8SYan Zheng u64 bytenr; 32281b9e619cSOmar Sandoval int op_flags; 3229a512bbf8SYan Zheng 3230a512bbf8SYan Zheng if (max_mirrors == 0) 3231a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3232a512bbf8SYan Zheng 3233a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3234a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3235935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3236935e5cc9SMiao Xie device->commit_total_bytes) 3237a512bbf8SYan Zheng break; 3238a512bbf8SYan Zheng 3239a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3240a512bbf8SYan Zheng 3241a512bbf8SYan Zheng crc = ~(u32)0; 3242abbb3b8eSDavid Sterba crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc, 3243abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3244a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3245a512bbf8SYan Zheng 3246abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 32479f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3248a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3249634554dcSJosef Bacik if (!bh) { 3250fb456252SJeff Mahoney btrfs_err(device->fs_info, 3251f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3252f14d104dSDavid Sterba bytenr); 3253634554dcSJosef Bacik errors++; 3254634554dcSJosef Bacik continue; 3255634554dcSJosef Bacik } 3256634554dcSJosef Bacik 3257a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3258a512bbf8SYan Zheng 32594eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3260a512bbf8SYan Zheng get_bh(bh); 32614eedeb75SHisashi Hifumi 32624eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3263a512bbf8SYan Zheng lock_buffer(bh); 3264a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3265442a4f63SStefan Behrens bh->b_private = device; 3266a512bbf8SYan Zheng 3267387125fcSChris Mason /* 3268387125fcSChris Mason * we fua the first super. The others we allow 3269387125fcSChris Mason * to go down lazy. 3270387125fcSChris Mason */ 32711b9e619cSOmar Sandoval op_flags = REQ_SYNC | REQ_META | REQ_PRIO; 32721b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) 32731b9e619cSOmar Sandoval op_flags |= REQ_FUA; 32741b9e619cSOmar Sandoval ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); 32754eedeb75SHisashi Hifumi if (ret) 3276a512bbf8SYan Zheng errors++; 3277a512bbf8SYan Zheng } 3278a512bbf8SYan Zheng return errors < i ? 0 : -1; 3279a512bbf8SYan Zheng } 3280a512bbf8SYan Zheng 3281387125fcSChris Mason /* 3282abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3283abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3284abbb3b8eSDavid Sterba * 3285abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3286abbb3b8eSDavid Sterba * date. 3287abbb3b8eSDavid Sterba */ 3288abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3289abbb3b8eSDavid Sterba { 3290abbb3b8eSDavid Sterba struct buffer_head *bh; 3291abbb3b8eSDavid Sterba int i; 3292abbb3b8eSDavid Sterba int errors = 0; 3293b6a535faSHoward McLauchlan bool primary_failed = false; 3294abbb3b8eSDavid Sterba u64 bytenr; 3295abbb3b8eSDavid Sterba 3296abbb3b8eSDavid Sterba if (max_mirrors == 0) 3297abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3298abbb3b8eSDavid Sterba 3299abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3300abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3301abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3302abbb3b8eSDavid Sterba device->commit_total_bytes) 3303abbb3b8eSDavid Sterba break; 3304abbb3b8eSDavid Sterba 33059f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 33069f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3307abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3308abbb3b8eSDavid Sterba if (!bh) { 3309abbb3b8eSDavid Sterba errors++; 3310b6a535faSHoward McLauchlan if (i == 0) 3311b6a535faSHoward McLauchlan primary_failed = true; 3312abbb3b8eSDavid Sterba continue; 3313abbb3b8eSDavid Sterba } 3314abbb3b8eSDavid Sterba wait_on_buffer(bh); 3315b6a535faSHoward McLauchlan if (!buffer_uptodate(bh)) { 3316abbb3b8eSDavid Sterba errors++; 3317b6a535faSHoward McLauchlan if (i == 0) 3318b6a535faSHoward McLauchlan primary_failed = true; 3319b6a535faSHoward McLauchlan } 3320abbb3b8eSDavid Sterba 3321abbb3b8eSDavid Sterba /* drop our reference */ 3322abbb3b8eSDavid Sterba brelse(bh); 3323abbb3b8eSDavid Sterba 3324abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3325abbb3b8eSDavid Sterba brelse(bh); 3326abbb3b8eSDavid Sterba } 3327abbb3b8eSDavid Sterba 3328b6a535faSHoward McLauchlan /* log error, force error return */ 3329b6a535faSHoward McLauchlan if (primary_failed) { 3330b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu", 3331b6a535faSHoward McLauchlan device->devid); 3332b6a535faSHoward McLauchlan return -1; 3333b6a535faSHoward McLauchlan } 3334b6a535faSHoward McLauchlan 3335abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3336abbb3b8eSDavid Sterba } 3337abbb3b8eSDavid Sterba 3338abbb3b8eSDavid Sterba /* 3339387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3340387125fcSChris Mason * for the barrier when it is done 3341387125fcSChris Mason */ 33424246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3343387125fcSChris Mason { 3344387125fcSChris Mason complete(bio->bi_private); 3345387125fcSChris Mason } 3346387125fcSChris Mason 3347387125fcSChris Mason /* 33484fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 33494fc6441aSAnand Jain * done in the waiting counterpart. 3350387125fcSChris Mason */ 33514fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3352387125fcSChris Mason { 3353c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3354e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3355387125fcSChris Mason 3356c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 33574fc6441aSAnand Jain return; 3358387125fcSChris Mason 3359e0ae9994SDavid Sterba bio_reset(bio); 3360387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 336174d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 33628d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3363387125fcSChris Mason init_completion(&device->flush_wait); 3364387125fcSChris Mason bio->bi_private = &device->flush_wait; 3365387125fcSChris Mason 336643a01111SLu Fengqi btrfsic_submit_bio(bio); 33671c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 33684fc6441aSAnand Jain } 3369387125fcSChris Mason 33704fc6441aSAnand Jain /* 33714fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 33724fc6441aSAnand Jain */ 33738c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 33744fc6441aSAnand Jain { 33754fc6441aSAnand Jain struct bio *bio = device->flush_bio; 33764fc6441aSAnand Jain 33771c3063b6SAnand Jain if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)) 337858efbc9fSOmar Sandoval return BLK_STS_OK; 33794fc6441aSAnand Jain 33801c3063b6SAnand Jain clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state); 33812980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 33824fc6441aSAnand Jain 33838c27cb35SLinus Torvalds return bio->bi_status; 3384387125fcSChris Mason } 3385387125fcSChris Mason 3386d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3387401b41e5SAnand Jain { 33886528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) 3389401b41e5SAnand Jain return -EIO; 3390387125fcSChris Mason return 0; 3391387125fcSChris Mason } 3392387125fcSChris Mason 3393387125fcSChris Mason /* 3394387125fcSChris Mason * send an empty flush down to each device in parallel, 3395387125fcSChris Mason * then wait for them 3396387125fcSChris Mason */ 3397387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3398387125fcSChris Mason { 3399387125fcSChris Mason struct list_head *head; 3400387125fcSChris Mason struct btrfs_device *dev; 34015af3e8ccSStefan Behrens int errors_wait = 0; 34024e4cbee9SChristoph Hellwig blk_status_t ret; 3403387125fcSChris Mason 34041538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex); 3405387125fcSChris Mason /* send down all the barriers */ 3406387125fcSChris Mason head = &info->fs_devices->devices; 34071538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3408e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3409f88ba6a2SHidetoshi Seto continue; 3410cea7c8bfSAnand Jain if (!dev->bdev) 3411387125fcSChris Mason continue; 3412e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3413ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3414387125fcSChris Mason continue; 3415387125fcSChris Mason 34164fc6441aSAnand Jain write_dev_flush(dev); 341758efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3418387125fcSChris Mason } 3419387125fcSChris Mason 3420387125fcSChris Mason /* wait for all the barriers */ 34211538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3422e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 3423f88ba6a2SHidetoshi Seto continue; 3424387125fcSChris Mason if (!dev->bdev) { 34255af3e8ccSStefan Behrens errors_wait++; 3426387125fcSChris Mason continue; 3427387125fcSChris Mason } 3428e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3429ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3430387125fcSChris Mason continue; 3431387125fcSChris Mason 34324fc6441aSAnand Jain ret = wait_dev_flush(dev); 3433401b41e5SAnand Jain if (ret) { 3434401b41e5SAnand Jain dev->last_flush_error = ret; 343566b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 343666b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 34375af3e8ccSStefan Behrens errors_wait++; 3438387125fcSChris Mason } 3439401b41e5SAnand Jain } 3440401b41e5SAnand Jain 3441cea7c8bfSAnand Jain if (errors_wait) { 3442401b41e5SAnand Jain /* 3443401b41e5SAnand Jain * At some point we need the status of all disks 3444401b41e5SAnand Jain * to arrive at the volume status. So error checking 3445401b41e5SAnand Jain * is being pushed to a separate loop. 3446401b41e5SAnand Jain */ 3447d10b82feSQu Wenruo return check_barrier_error(info); 3448401b41e5SAnand Jain } 3449387125fcSChris Mason return 0; 3450387125fcSChris Mason } 3451387125fcSChris Mason 3452943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3453943c6e99SZhao Lei { 34548789f4feSZhao Lei int raid_type; 34558789f4feSZhao Lei int min_tolerated = INT_MAX; 3456943c6e99SZhao Lei 34578789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 34588789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 34598789f4feSZhao Lei min_tolerated = min(min_tolerated, 34608789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 34618789f4feSZhao Lei tolerated_failures); 3462943c6e99SZhao Lei 34638789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 34648789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 34658789f4feSZhao Lei continue; 34668789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 34678789f4feSZhao Lei continue; 34688789f4feSZhao Lei min_tolerated = min(min_tolerated, 34698789f4feSZhao Lei btrfs_raid_array[raid_type]. 34708789f4feSZhao Lei tolerated_failures); 34718789f4feSZhao Lei } 3472943c6e99SZhao Lei 34738789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3474ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 34758789f4feSZhao Lei min_tolerated = 0; 34768789f4feSZhao Lei } 34778789f4feSZhao Lei 34788789f4feSZhao Lei return min_tolerated; 3479943c6e99SZhao Lei } 3480943c6e99SZhao Lei 3481eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3482f2984462SChris Mason { 3483e5e9a520SChris Mason struct list_head *head; 3484f2984462SChris Mason struct btrfs_device *dev; 3485a061fc8dSChris Mason struct btrfs_super_block *sb; 3486f2984462SChris Mason struct btrfs_dev_item *dev_item; 3487f2984462SChris Mason int ret; 3488f2984462SChris Mason int do_barriers; 3489a236aed1SChris Mason int max_errors; 3490a236aed1SChris Mason int total_errors = 0; 3491a061fc8dSChris Mason u64 flags; 3492f2984462SChris Mason 34930b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3494fed3b381SLiu Bo 3495fed3b381SLiu Bo /* 3496fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3497fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3498fed3b381SLiu Bo * been consistent on disk. 3499fed3b381SLiu Bo */ 3500fed3b381SLiu Bo if (max_mirrors == 0) 35010b246afaSJeff Mahoney backup_super_roots(fs_info); 3502f2984462SChris Mason 35030b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3504a061fc8dSChris Mason dev_item = &sb->dev_item; 3505e5e9a520SChris Mason 35060b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 35070b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 35080b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3509387125fcSChris Mason 35105af3e8ccSStefan Behrens if (do_barriers) { 35110b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 35125af3e8ccSStefan Behrens if (ret) { 35135af3e8ccSStefan Behrens mutex_unlock( 35140b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 35150b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 35165af3e8ccSStefan Behrens "errors while submitting device barriers."); 35175af3e8ccSStefan Behrens return ret; 35185af3e8ccSStefan Behrens } 35195af3e8ccSStefan Behrens } 3520387125fcSChris Mason 35211538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3522dfe25020SChris Mason if (!dev->bdev) { 3523dfe25020SChris Mason total_errors++; 3524dfe25020SChris Mason continue; 3525dfe25020SChris Mason } 3526e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3527ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3528dfe25020SChris Mason continue; 3529dfe25020SChris Mason 35302b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3531a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3532a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 35337df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3534935e5cc9SMiao Xie dev->commit_total_bytes); 3535ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3536ce7213c7SMiao Xie dev->commit_bytes_used); 3537a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3538a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3539a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3540a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 354144880fdcSAnand Jain memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE); 3542a512bbf8SYan Zheng 3543a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3544a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3545f2984462SChris Mason 3546abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3547a236aed1SChris Mason if (ret) 3548a236aed1SChris Mason total_errors++; 3549f2984462SChris Mason } 3550a236aed1SChris Mason if (total_errors > max_errors) { 35510b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3552d397712bSChris Mason total_errors); 35530b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 355479787eaaSJeff Mahoney 35559d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 35560b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 35570b246afaSJeff Mahoney "%d errors while writing supers", 35580b246afaSJeff Mahoney total_errors); 35599d565ba4SStefan Behrens return -EIO; 3560a236aed1SChris Mason } 3561f2984462SChris Mason 3562a512bbf8SYan Zheng total_errors = 0; 35631538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) { 3564dfe25020SChris Mason if (!dev->bdev) 3565dfe25020SChris Mason continue; 3566e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || 3567ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) 3568dfe25020SChris Mason continue; 3569dfe25020SChris Mason 3570abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3571a512bbf8SYan Zheng if (ret) 35721259ab75SChris Mason total_errors++; 3573f2984462SChris Mason } 35740b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3575a236aed1SChris Mason if (total_errors > max_errors) { 35760b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 35770b246afaSJeff Mahoney "%d errors while writing supers", 35780b246afaSJeff Mahoney total_errors); 357979787eaaSJeff Mahoney return -EIO; 3580a236aed1SChris Mason } 3581f2984462SChris Mason return 0; 3582f2984462SChris Mason } 3583f2984462SChris Mason 3584cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3585cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3586cb517eabSMiao Xie struct btrfs_root *root) 35872619ba1fSChris Mason { 35884df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 35892619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 35902619ba1fSChris Mason (unsigned long)root->root_key.objectid); 35914df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 359276dda93cSYan, Zheng 359376dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 359476dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 359576dda93cSYan, Zheng 35961c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 35973321719eSLiu Bo btrfs_free_log(NULL, root); 35981c1ea4f7SLiu Bo if (root->reloc_root) { 35991c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 36001c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 36011c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 36021c1ea4f7SLiu Bo root->reloc_root = NULL; 36031c1ea4f7SLiu Bo } 36041c1ea4f7SLiu Bo } 36053321719eSLiu Bo 3606faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3607581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3608faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3609581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 36104df27c4dSYan, Zheng free_fs_root(root); 36114df27c4dSYan, Zheng } 36124df27c4dSYan, Zheng 36134df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 36144df27c4dSYan, Zheng { 361557cdc8dbSDavid Sterba iput(root->ino_cache_inode); 36164df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 36172ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 36181cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 36190ee5dc67SAl Viro if (root->anon_dev) 36200ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 36218257b2dcSMiao Xie if (root->subv_writers) 36228257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 36235f39d397SChris Mason free_extent_buffer(root->node); 36245f39d397SChris Mason free_extent_buffer(root->commit_root); 3625581bb050SLi Zefan kfree(root->free_ino_ctl); 3626581bb050SLi Zefan kfree(root->free_ino_pinned); 362758176a96SJosef Bacik kfree(root->name); 3628b0feb9d9SMiao Xie btrfs_put_fs_root(root); 36292619ba1fSChris Mason } 36302619ba1fSChris Mason 3631cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3632cb517eabSMiao Xie { 3633cb517eabSMiao Xie free_fs_root(root); 3634cfaa7295SChris Mason } 3635cfaa7295SChris Mason 3636c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3637c146afadSYan Zheng { 3638c146afadSYan Zheng u64 root_objectid = 0; 3639c146afadSYan Zheng struct btrfs_root *gang[8]; 364065d33fd7SQu Wenruo int i = 0; 364165d33fd7SQu Wenruo int err = 0; 364265d33fd7SQu Wenruo unsigned int ret = 0; 364365d33fd7SQu Wenruo int index; 3644c146afadSYan Zheng 3645c146afadSYan Zheng while (1) { 364665d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3647c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3648c146afadSYan Zheng (void **)gang, root_objectid, 3649c146afadSYan Zheng ARRAY_SIZE(gang)); 365065d33fd7SQu Wenruo if (!ret) { 365165d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3652c146afadSYan Zheng break; 365365d33fd7SQu Wenruo } 36545d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 365566b4ffd1SJosef Bacik 365665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 365765d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 365865d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 365965d33fd7SQu Wenruo gang[i] = NULL; 366065d33fd7SQu Wenruo continue; 366165d33fd7SQu Wenruo } 366265d33fd7SQu Wenruo /* grab all the search result for later use */ 366365d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 366465d33fd7SQu Wenruo } 366565d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 366665d33fd7SQu Wenruo 366765d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 366865d33fd7SQu Wenruo if (!gang[i]) 366965d33fd7SQu Wenruo continue; 3670c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 367166b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 367266b4ffd1SJosef Bacik if (err) 367365d33fd7SQu Wenruo break; 367465d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3675c146afadSYan Zheng } 3676c146afadSYan Zheng root_objectid++; 3677c146afadSYan Zheng } 367865d33fd7SQu Wenruo 367965d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 368065d33fd7SQu Wenruo for (; i < ret; i++) { 368165d33fd7SQu Wenruo if (gang[i]) 368265d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 368365d33fd7SQu Wenruo } 368465d33fd7SQu Wenruo return err; 3685c146afadSYan Zheng } 3686c146afadSYan Zheng 36876bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3688c146afadSYan Zheng { 36896bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3690c146afadSYan Zheng struct btrfs_trans_handle *trans; 3691c146afadSYan Zheng 36920b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 36932ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 36940b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 36950b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3696c71bf099SYan, Zheng 3697c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 36980b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 36990b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3700c71bf099SYan, Zheng 37017a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 37023612b495STsutomu Itoh if (IS_ERR(trans)) 37033612b495STsutomu Itoh return PTR_ERR(trans); 37043a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3705c146afadSYan Zheng } 3706c146afadSYan Zheng 37076bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3708eb60ceacSChris Mason { 37096bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3710c146afadSYan Zheng int ret; 3711e089f05cSChris Mason 3712afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3713a2135011SChris Mason 37147343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3715d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 37167343dd61SJustin Maggard 3717803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3718803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3719803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3720803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3721803b2f54SStefan Behrens 3722837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3723aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3724837d5b6eSIlya Dryomov 37258dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 37268dabb742SStefan Behrens 3727aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 37284cb5300bSChris Mason 37294cb5300bSChris Mason /* wait for any defraggers to finish */ 37304cb5300bSChris Mason wait_event(fs_info->transaction_wait, 37314cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 37324cb5300bSChris Mason 37334cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 373426176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 37354cb5300bSChris Mason 373621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 373721c7e756SMiao Xie 3738bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 3739e44163e1SJeff Mahoney /* 3740e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3741e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3742e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3743e44163e1SJeff Mahoney */ 37440b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3745e44163e1SJeff Mahoney 37466bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3747d397712bSChris Mason if (ret) 374804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3749c146afadSYan Zheng } 3750ed2ff2cbSChris Mason 375187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 37522ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3753acce952bSliubo 3754e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3755e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 37568929ecfaSYan, Zheng 3757afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3758f25784b3SYan Zheng 375904892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3760bcef60f2SArne Jansen 3761963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 376204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3763963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3764b0c68f8bSChris Mason } 376531153d81SYan Zheng 37666618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3767b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 37685ac1d209SJeff Mahoney 3769faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3770d10c5f31SChris Mason 37711a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 37721a4319ccSLiu Bo 3773de348ee0SWang Shilong /* 3774de348ee0SWang Shilong * we must make sure there is not any read request to 3775de348ee0SWang Shilong * submit after we stopping all workers. 3776de348ee0SWang Shilong */ 3777de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 377896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 377996192499SJosef Bacik 37805cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 37815cdd7db6SFilipe Manana 3782afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 378313e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 37849ad6b7bcSChris Mason 378513e6c37bSJosef Bacik iput(fs_info->btree_inode); 3786d6bfde87SChris Mason 378721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 37880b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 37892ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 379021adbd5cSStefan Behrens #endif 379121adbd5cSStefan Behrens 3792dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 37930b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3794b248a415SChris Mason 3795e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3796963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3797c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 379876dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 37990b86a832SChris Mason 380053b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3801fd708b81SJosef Bacik btrfs_free_ref_cache(fs_info); 380253b381b3SDavid Woodhouse 3803cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 38041cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 380504216820SFilipe Manana 380604216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 380704216820SFilipe Manana struct extent_map *em; 380804216820SFilipe Manana 380904216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 381004216820SFilipe Manana struct extent_map, list); 381104216820SFilipe Manana list_del_init(&em->list); 381204216820SFilipe Manana free_extent_map(em); 381304216820SFilipe Manana } 3814eb60ceacSChris Mason } 3815eb60ceacSChris Mason 3816b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3817b9fab919SChris Mason int atomic) 3818ccd467d6SChris Mason { 38191259ab75SChris Mason int ret; 3820727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 38211259ab75SChris Mason 38220b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 38231259ab75SChris Mason if (!ret) 38241259ab75SChris Mason return ret; 38251259ab75SChris Mason 38261259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3827b9fab919SChris Mason parent_transid, atomic); 3828b9fab919SChris Mason if (ret == -EAGAIN) 3829b9fab919SChris Mason return ret; 38301259ab75SChris Mason return !ret; 38315f39d397SChris Mason } 38326702ed49SChris Mason 38335f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 38345f39d397SChris Mason { 38350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 383606ea65a3SJosef Bacik struct btrfs_root *root; 38375f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3838b9473439SChris Mason int was_dirty; 3839b4ce94deSChris Mason 384006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 384106ea65a3SJosef Bacik /* 384206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 384306ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 384406ea65a3SJosef Bacik * outside of the sanity tests. 384506ea65a3SJosef Bacik */ 384606ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 384706ea65a3SJosef Bacik return; 384806ea65a3SJosef Bacik #endif 384906ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 38500b246afaSJeff Mahoney fs_info = root->fs_info; 3851b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 38520b246afaSJeff Mahoney if (transid != fs_info->generation) 38535d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 38540b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 38550b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3856e2d84521SMiao Xie if (!was_dirty) 3857104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 3858e2d84521SMiao Xie buf->len, 38590b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 38601f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 386169fc6cbbSQu Wenruo /* 386269fc6cbbSQu Wenruo * Since btrfs_mark_buffer_dirty() can be called with item pointer set 386369fc6cbbSQu Wenruo * but item data not updated. 386469fc6cbbSQu Wenruo * So here we should only check item pointers, not item data. 386569fc6cbbSQu Wenruo */ 386669fc6cbbSQu Wenruo if (btrfs_header_level(buf) == 0 && 386769fc6cbbSQu Wenruo btrfs_check_leaf_relaxed(root, buf)) { 3868a4f78750SDavid Sterba btrfs_print_leaf(buf); 38691f21ef0aSFilipe Manana ASSERT(0); 38701f21ef0aSFilipe Manana } 38711f21ef0aSFilipe Manana #endif 3872eb60ceacSChris Mason } 3873eb60ceacSChris Mason 38742ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 3875b53d3f5dSLiu Bo int flush_delayed) 387635b7e476SChris Mason { 3877188de649SChris Mason /* 3878188de649SChris Mason * looks as though older kernels can get into trouble with 3879188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3880188de649SChris Mason */ 3881e2d84521SMiao Xie int ret; 3882d6bfde87SChris Mason 38836933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3884d6bfde87SChris Mason return; 3885d6bfde87SChris Mason 3886b53d3f5dSLiu Bo if (flush_delayed) 38872ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 388816cdcec7SMiao Xie 38890b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 3890e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3891e2d84521SMiao Xie if (ret > 0) { 38920b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 389316cdcec7SMiao Xie } 389416cdcec7SMiao Xie } 389516cdcec7SMiao Xie 38962ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 389716cdcec7SMiao Xie { 38982ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 389935b7e476SChris Mason } 3900b53d3f5dSLiu Bo 39012ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 3902b53d3f5dSLiu Bo { 39032ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 3904d6bfde87SChris Mason } 39056b80053dSChris Mason 3906ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 39076b80053dSChris Mason { 3908727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 39092ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 39102ff7e61eSJeff Mahoney 39112ff7e61eSJeff Mahoney return btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 39126b80053dSChris Mason } 39130da5468fSChris Mason 39143d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info) 3915acce952bSliubo { 3916c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 3917319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 3918319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 3919c926093eSDavid Sterba int ret = 0; 3920c926093eSDavid Sterba 3921319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 3922ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 3923319e4d06SQu Wenruo ret = -EINVAL; 3924319e4d06SQu Wenruo } 39256f794e3cSAnand Jain if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) { 39266f794e3cSAnand Jain btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu", 3927319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 39286f794e3cSAnand Jain ret = -EINVAL; 39296f794e3cSAnand Jain } 393021e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 3931ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 393221e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 3933c926093eSDavid Sterba ret = -EINVAL; 3934c926093eSDavid Sterba } 393521e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 3936ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 393721e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 3938c926093eSDavid Sterba ret = -EINVAL; 3939c926093eSDavid Sterba } 394021e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 3941ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 394221e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 3943c926093eSDavid Sterba ret = -EINVAL; 3944c926093eSDavid Sterba } 3945c926093eSDavid Sterba 39461104a885SDavid Sterba /* 3947319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 3948319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 39491104a885SDavid Sterba */ 3950319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 3951319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 3952ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 395375d6ad38SDavid Sterba ret = -EINVAL; 395475d6ad38SDavid Sterba } 3955319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 395609cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 3957ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 3958ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 395909cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 3960319e4d06SQu Wenruo ret = -EINVAL; 3961319e4d06SQu Wenruo } 3962319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 3963319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 3964ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 3965319e4d06SQu Wenruo ret = -EINVAL; 3966319e4d06SQu Wenruo } 3967319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 3968ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 3969ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 3970319e4d06SQu Wenruo ret = -EINVAL; 3971319e4d06SQu Wenruo } 3972319e4d06SQu Wenruo 3973319e4d06SQu Wenruo /* Root alignment check */ 3974319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 3975ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 3976319e4d06SQu Wenruo btrfs_super_root(sb)); 3977319e4d06SQu Wenruo ret = -EINVAL; 3978319e4d06SQu Wenruo } 3979319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 3980ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 3981319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 3982319e4d06SQu Wenruo ret = -EINVAL; 3983319e4d06SQu Wenruo } 3984319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 3985ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 3986319e4d06SQu Wenruo btrfs_super_log_root(sb)); 398775d6ad38SDavid Sterba ret = -EINVAL; 398875d6ad38SDavid Sterba } 398975d6ad38SDavid Sterba 399044880fdcSAnand Jain if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) { 3991ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 3992ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 3993c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 3994c926093eSDavid Sterba ret = -EINVAL; 3995c926093eSDavid Sterba } 3996c926093eSDavid Sterba 3997c926093eSDavid Sterba /* 3998c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 3999c926093eSDavid Sterba * done later 4000c926093eSDavid Sterba */ 400199e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 400299e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 400399e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 400499e3ecfcSLiu Bo ret = -EINVAL; 400599e3ecfcSLiu Bo } 4006b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 400799e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 400899e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 400999e3ecfcSLiu Bo ret = -EINVAL; 401099e3ecfcSLiu Bo } 401121e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 4012ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 401321e7626bSDavid Sterba btrfs_super_num_devices(sb)); 401475d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 4015ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 401675d6ad38SDavid Sterba ret = -EINVAL; 401775d6ad38SDavid Sterba } 4018c926093eSDavid Sterba 401921e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4020ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 402121e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4022c926093eSDavid Sterba ret = -EINVAL; 4023c926093eSDavid Sterba } 4024c926093eSDavid Sterba 4025c926093eSDavid Sterba /* 4026ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4027ce7fca5fSDavid Sterba * and one chunk 4028ce7fca5fSDavid Sterba */ 4029ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4030ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4031ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4032ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4033ce7fca5fSDavid Sterba ret = -EINVAL; 4034ce7fca5fSDavid Sterba } 4035ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4036ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4037ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4038ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4039ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4040ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4041ce7fca5fSDavid Sterba ret = -EINVAL; 4042ce7fca5fSDavid Sterba } 4043ce7fca5fSDavid Sterba 4044ce7fca5fSDavid Sterba /* 4045c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4046c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4047c926093eSDavid Sterba */ 404821e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4049ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4050ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4051ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4052ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 405321e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 405421e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4055ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4056ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4057ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4058ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4059c926093eSDavid Sterba 4060c926093eSDavid Sterba return ret; 4061fcd1f065SDavid Sterba } 4062fcd1f065SDavid Sterba 40632ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4064acce952bSliubo { 40650b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 40662ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 40670b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4068acce952bSliubo 40690b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 40700b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4071acce952bSliubo 4072acce952bSliubo /* cleanup FS via transaction */ 40732ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4074acce952bSliubo } 4075acce952bSliubo 4076143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4077acce952bSliubo { 4078acce952bSliubo struct btrfs_ordered_extent *ordered; 4079acce952bSliubo 4080199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4081779880efSJosef Bacik /* 4082779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4083779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4084779880efSJosef Bacik */ 4085199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4086779880efSJosef Bacik root_extent_list) 4087779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4088199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4089199c2a9cSMiao Xie } 4090199c2a9cSMiao Xie 4091199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4092199c2a9cSMiao Xie { 4093199c2a9cSMiao Xie struct btrfs_root *root; 4094199c2a9cSMiao Xie struct list_head splice; 4095199c2a9cSMiao Xie 4096199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4097199c2a9cSMiao Xie 4098199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4099199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4100199c2a9cSMiao Xie while (!list_empty(&splice)) { 4101199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4102199c2a9cSMiao Xie ordered_root); 41031de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 41041de2cfdeSJosef Bacik &fs_info->ordered_roots); 4105199c2a9cSMiao Xie 41062a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4107199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4108199c2a9cSMiao Xie 41092a85d9caSLiu Bo cond_resched(); 41102a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4111199c2a9cSMiao Xie } 4112199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4113acce952bSliubo } 4114acce952bSliubo 411535a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 41162ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4117acce952bSliubo { 4118acce952bSliubo struct rb_node *node; 4119acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4120acce952bSliubo struct btrfs_delayed_ref_node *ref; 4121acce952bSliubo int ret = 0; 4122acce952bSliubo 4123acce952bSliubo delayed_refs = &trans->delayed_refs; 4124acce952bSliubo 4125acce952bSliubo spin_lock(&delayed_refs->lock); 4126d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4127cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 41280b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4129acce952bSliubo return ret; 4130acce952bSliubo } 4131acce952bSliubo 4132d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4133d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 41340e0adbcfSJosef Bacik struct rb_node *n; 4135e78417d1SJosef Bacik bool pin_bytes = false; 4136acce952bSliubo 4137d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4138d7df2c79SJosef Bacik href_node); 4139b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 4140d278850eSJosef Bacik refcount_inc(&head->refs); 414179787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4142b939d1abSJosef Bacik 4143acce952bSliubo mutex_lock(&head->mutex); 4144b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4145d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4146e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4147b939d1abSJosef Bacik continue; 4148b939d1abSJosef Bacik } 4149d7df2c79SJosef Bacik spin_lock(&head->lock); 41500e0adbcfSJosef Bacik while ((n = rb_first(&head->ref_tree)) != NULL) { 41510e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node, 41520e0adbcfSJosef Bacik ref_node); 4153d7df2c79SJosef Bacik ref->in_tree = 0; 41540e0adbcfSJosef Bacik rb_erase(&ref->ref_node, &head->ref_tree); 41550e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node); 41561d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 41571d57ee94SWang Xiaoguang list_del(&ref->add_list); 4158d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4159d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4160d7df2c79SJosef Bacik } 416154067ae9SJosef Bacik if (head->must_insert_reserved) 4162e78417d1SJosef Bacik pin_bytes = true; 416378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4164acce952bSliubo delayed_refs->num_heads--; 4165d7df2c79SJosef Bacik if (head->processing == 0) 4166acce952bSliubo delayed_refs->num_heads_ready--; 4167d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4168c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4169d278850eSJosef Bacik RB_CLEAR_NODE(&head->href_node); 4170d7df2c79SJosef Bacik spin_unlock(&head->lock); 4171acce952bSliubo spin_unlock(&delayed_refs->lock); 4172e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4173acce952bSliubo 4174d7df2c79SJosef Bacik if (pin_bytes) 4175d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4176d278850eSJosef Bacik head->num_bytes, 1); 4177d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4178acce952bSliubo cond_resched(); 4179acce952bSliubo spin_lock(&delayed_refs->lock); 4180acce952bSliubo } 4181acce952bSliubo 4182acce952bSliubo spin_unlock(&delayed_refs->lock); 4183acce952bSliubo 4184acce952bSliubo return ret; 4185acce952bSliubo } 4186acce952bSliubo 4187143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4188acce952bSliubo { 4189acce952bSliubo struct btrfs_inode *btrfs_inode; 4190acce952bSliubo struct list_head splice; 4191acce952bSliubo 4192acce952bSliubo INIT_LIST_HEAD(&splice); 4193acce952bSliubo 4194eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4195eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4196acce952bSliubo 4197acce952bSliubo while (!list_empty(&splice)) { 4198eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4199acce952bSliubo delalloc_inodes); 4200acce952bSliubo 4201acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4202df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4203df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4204eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4205acce952bSliubo 4206acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4207b216cbfbSMiao Xie 4208eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4209acce952bSliubo } 4210acce952bSliubo 4211eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4212eb73c1b7SMiao Xie } 4213eb73c1b7SMiao Xie 4214eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4215eb73c1b7SMiao Xie { 4216eb73c1b7SMiao Xie struct btrfs_root *root; 4217eb73c1b7SMiao Xie struct list_head splice; 4218eb73c1b7SMiao Xie 4219eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4220eb73c1b7SMiao Xie 4221eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4222eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4223eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4224eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4225eb73c1b7SMiao Xie delalloc_root); 4226eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4227eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4228eb73c1b7SMiao Xie BUG_ON(!root); 4229eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4230eb73c1b7SMiao Xie 4231eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4232eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4233eb73c1b7SMiao Xie 4234eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4235eb73c1b7SMiao Xie } 4236eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4237acce952bSliubo } 4238acce952bSliubo 42392ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4240acce952bSliubo struct extent_io_tree *dirty_pages, 4241acce952bSliubo int mark) 4242acce952bSliubo { 4243acce952bSliubo int ret; 4244acce952bSliubo struct extent_buffer *eb; 4245acce952bSliubo u64 start = 0; 4246acce952bSliubo u64 end; 4247acce952bSliubo 4248acce952bSliubo while (1) { 4249acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4250e6138876SJosef Bacik mark, NULL); 4251acce952bSliubo if (ret) 4252acce952bSliubo break; 4253acce952bSliubo 425491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4255acce952bSliubo while (start <= end) { 42560b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 42570b246afaSJeff Mahoney start += fs_info->nodesize; 4258fd8b2b61SJosef Bacik if (!eb) 4259acce952bSliubo continue; 4260fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4261acce952bSliubo 4262fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4263fd8b2b61SJosef Bacik &eb->bflags)) 4264fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4265fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4266acce952bSliubo } 4267acce952bSliubo } 4268acce952bSliubo 4269acce952bSliubo return ret; 4270acce952bSliubo } 4271acce952bSliubo 42722ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4273acce952bSliubo struct extent_io_tree *pinned_extents) 4274acce952bSliubo { 4275acce952bSliubo struct extent_io_tree *unpin; 4276acce952bSliubo u64 start; 4277acce952bSliubo u64 end; 4278acce952bSliubo int ret; 4279ed0eaa14SLiu Bo bool loop = true; 4280acce952bSliubo 4281acce952bSliubo unpin = pinned_extents; 4282ed0eaa14SLiu Bo again: 4283acce952bSliubo while (1) { 4284acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4285e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4286acce952bSliubo if (ret) 4287acce952bSliubo break; 4288acce952bSliubo 4289af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 42902ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4291acce952bSliubo cond_resched(); 4292acce952bSliubo } 4293acce952bSliubo 4294ed0eaa14SLiu Bo if (loop) { 42950b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 42960b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4297ed0eaa14SLiu Bo else 42980b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4299ed0eaa14SLiu Bo loop = false; 4300ed0eaa14SLiu Bo goto again; 4301ed0eaa14SLiu Bo } 4302ed0eaa14SLiu Bo 4303acce952bSliubo return 0; 4304acce952bSliubo } 4305acce952bSliubo 4306c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4307c79a1751SLiu Bo { 4308c79a1751SLiu Bo struct inode *inode; 4309c79a1751SLiu Bo 4310c79a1751SLiu Bo inode = cache->io_ctl.inode; 4311c79a1751SLiu Bo if (inode) { 4312c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4313c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4314c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4315c79a1751SLiu Bo iput(inode); 4316c79a1751SLiu Bo } 4317c79a1751SLiu Bo btrfs_put_block_group(cache); 4318c79a1751SLiu Bo } 4319c79a1751SLiu Bo 4320c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 43212ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4322c79a1751SLiu Bo { 4323c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4324c79a1751SLiu Bo 4325c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4326c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4327c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4328c79a1751SLiu Bo struct btrfs_block_group_cache, 4329c79a1751SLiu Bo dirty_list); 4330c79a1751SLiu Bo if (!cache) { 43310b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group dirty_bgs list"); 4332c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4333c79a1751SLiu Bo return; 4334c79a1751SLiu Bo } 4335c79a1751SLiu Bo 4336c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4337c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4338c79a1751SLiu Bo list_del_init(&cache->io_list); 4339c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4340c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4341c79a1751SLiu Bo } 4342c79a1751SLiu Bo 4343c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4344c79a1751SLiu Bo spin_lock(&cache->lock); 4345c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4346c79a1751SLiu Bo spin_unlock(&cache->lock); 4347c79a1751SLiu Bo 4348c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4349c79a1751SLiu Bo btrfs_put_block_group(cache); 4350c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4351c79a1751SLiu Bo } 4352c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4353c79a1751SLiu Bo 4354c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4355c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4356c79a1751SLiu Bo struct btrfs_block_group_cache, 4357c79a1751SLiu Bo io_list); 4358c79a1751SLiu Bo if (!cache) { 43590b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group on io_bgs list"); 4360c79a1751SLiu Bo return; 4361c79a1751SLiu Bo } 4362c79a1751SLiu Bo 4363c79a1751SLiu Bo list_del_init(&cache->io_list); 4364c79a1751SLiu Bo spin_lock(&cache->lock); 4365c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4366c79a1751SLiu Bo spin_unlock(&cache->lock); 4367c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4368c79a1751SLiu Bo } 4369c79a1751SLiu Bo } 4370c79a1751SLiu Bo 437149b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 43722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 437349b25e05SJeff Mahoney { 43742ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4375c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4376c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4377c79a1751SLiu Bo 43782ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 437949b25e05SJeff Mahoney 43804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 43810b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 438249b25e05SJeff Mahoney 43834a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 43840b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 438549b25e05SJeff Mahoney 4386ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4387ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 438867cde344SMiao Xie 43892ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 439049b25e05SJeff Mahoney EXTENT_DIRTY); 43912ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 43920b246afaSJeff Mahoney fs_info->pinned_extents); 439349b25e05SJeff Mahoney 43944a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 43954a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 439649b25e05SJeff Mahoney } 439749b25e05SJeff Mahoney 43982ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4399acce952bSliubo { 4400acce952bSliubo struct btrfs_transaction *t; 4401acce952bSliubo 44020b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4403acce952bSliubo 44040b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 44050b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 44060b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4407724e2315SJosef Bacik struct btrfs_transaction, list); 4408724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 44099b64f57dSElena Reshetova refcount_inc(&t->use_count); 44100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 44112ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4412724e2315SJosef Bacik btrfs_put_transaction(t); 44130b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4414724e2315SJosef Bacik continue; 4415724e2315SJosef Bacik } 44160b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4417724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 44180b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4419724e2315SJosef Bacik /* 4420724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4421724e2315SJosef Bacik * handle open currently for this transaction. 4422724e2315SJosef Bacik */ 4423724e2315SJosef Bacik wait_event(t->writer_wait, 4424724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4425724e2315SJosef Bacik } else { 44260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4427724e2315SJosef Bacik } 44282ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4429724e2315SJosef Bacik 44300b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 44310b246afaSJeff Mahoney if (t == fs_info->running_transaction) 44320b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4433724e2315SJosef Bacik list_del_init(&t->list); 44340b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4435a4abeea4SJosef Bacik 4436724e2315SJosef Bacik btrfs_put_transaction(t); 44372ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 44380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4439724e2315SJosef Bacik } 44400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 44410b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4442ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4443ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 44442ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 44450b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 44460b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4447acce952bSliubo 4448acce952bSliubo return 0; 4449acce952bSliubo } 4450acce952bSliubo 4451c6100a4bSJosef Bacik static struct btrfs_fs_info *btree_fs_info(void *private_data) 4452c6100a4bSJosef Bacik { 4453c6100a4bSJosef Bacik struct inode *inode = private_data; 4454c6100a4bSJosef Bacik return btrfs_sb(inode->i_sb); 4455c6100a4bSJosef Bacik } 4456c6100a4bSJosef Bacik 4457e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 44584d53dddbSDavid Sterba /* mandatory callbacks */ 44590b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 44604d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 4461239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4462239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 446320a7db8aSDavid Sterba .readpage_io_failed_hook = btree_io_failed_hook, 4464c6100a4bSJosef Bacik .set_range_writeback = btrfs_set_range_writeback, 4465c6100a4bSJosef Bacik .tree_fs_info = btree_fs_info, 44664d53dddbSDavid Sterba 44674d53dddbSDavid Sterba /* optional callbacks */ 44680da5468fSChris Mason }; 4469