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> 337e75bf3fSDavid Sterba #include <asm/unaligned.h> 34eb60ceacSChris Mason #include "ctree.h" 35eb60ceacSChris Mason #include "disk-io.h" 360b947affSFilipe David Borba Manana #include "hash.h" 37e089f05cSChris Mason #include "transaction.h" 380f7d52f4SChris Mason #include "btrfs_inode.h" 390b86a832SChris Mason #include "volumes.h" 40db94535dSChris Mason #include "print-tree.h" 41925baeddSChris Mason #include "locking.h" 42e02119d5SChris Mason #include "tree-log.h" 43fa9c0d79SChris Mason #include "free-space-cache.h" 4470f6d82eSOmar Sandoval #include "free-space-tree.h" 45581bb050SLi Zefan #include "inode-map.h" 4621adbd5cSStefan Behrens #include "check-integrity.h" 47606686eeSJosef Bacik #include "rcu-string.h" 488dabb742SStefan Behrens #include "dev-replace.h" 4953b381b3SDavid Woodhouse #include "raid56.h" 505ac1d209SJeff Mahoney #include "sysfs.h" 51fcebe456SJosef Bacik #include "qgroup.h" 52ebb8765bSAnand Jain #include "compression.h" 53557ea5ddSQu Wenruo #include "tree-checker.h" 54fd708b81SJosef Bacik #include "ref-verify.h" 55eb60ceacSChris Mason 56de0022b9SJosef Bacik #ifdef CONFIG_X86 57de0022b9SJosef Bacik #include <asm/cpufeature.h> 58de0022b9SJosef Bacik #endif 59de0022b9SJosef Bacik 60319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 61319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 62319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 63319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 64319e4d06SQu Wenruo BTRFS_SUPER_FLAG_METADUMP) 65319e4d06SQu Wenruo 66e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 678b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 684df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 693d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info); 70143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 71acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 73143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 742ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 75acce952bSliubo struct extent_io_tree *dirty_pages, 76acce952bSliubo int mark); 772ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 78acce952bSliubo struct extent_io_tree *pinned_extents); 792ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 802ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 81ce9adaa5SChris Mason 82d352ac68SChris Mason /* 8397eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 8497eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 85d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 86d352ac68SChris Mason */ 8797eb6b69SDavid Sterba struct btrfs_end_io_wq { 88ce9adaa5SChris Mason struct bio *bio; 89ce9adaa5SChris Mason bio_end_io_t *end_io; 90ce9adaa5SChris Mason void *private; 91ce9adaa5SChris Mason struct btrfs_fs_info *info; 924e4cbee9SChristoph Hellwig blk_status_t status; 93bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 948b712842SChris Mason struct btrfs_work work; 95ce9adaa5SChris Mason }; 960da5468fSChris Mason 9797eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 9897eb6b69SDavid Sterba 9997eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 10097eb6b69SDavid Sterba { 10197eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 10297eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 10397eb6b69SDavid Sterba 0, 104fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 10597eb6b69SDavid Sterba NULL); 10697eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 10797eb6b69SDavid Sterba return -ENOMEM; 10897eb6b69SDavid Sterba return 0; 10997eb6b69SDavid Sterba } 11097eb6b69SDavid Sterba 11197eb6b69SDavid Sterba void btrfs_end_io_wq_exit(void) 11297eb6b69SDavid Sterba { 11397eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 11497eb6b69SDavid Sterba } 11597eb6b69SDavid Sterba 116d352ac68SChris Mason /* 117d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 118d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 119d352ac68SChris Mason * just before they are sent down the IO stack. 120d352ac68SChris Mason */ 12144b8bd7eSChris Mason struct async_submit_bio { 122c6100a4bSJosef Bacik void *private_data; 123c6100a4bSJosef Bacik struct btrfs_fs_info *fs_info; 12444b8bd7eSChris Mason struct bio *bio; 1254a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1264a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 12744b8bd7eSChris Mason int mirror_num; 128c8b97818SChris Mason unsigned long bio_flags; 129eaf25d93SChris Mason /* 130eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 131eaf25d93SChris Mason * can't tell us where in the file the bio should go 132eaf25d93SChris Mason */ 133eaf25d93SChris Mason u64 bio_offset; 1348b712842SChris Mason struct btrfs_work work; 1354e4cbee9SChristoph Hellwig blk_status_t status; 13644b8bd7eSChris Mason }; 13744b8bd7eSChris Mason 13885d4e461SChris Mason /* 13985d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 14085d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 14185d4e461SChris Mason * the level the eb occupies in the tree. 1424008c04aSChris Mason * 14385d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 14485d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 14585d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 14685d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 14785d4e461SChris Mason * have separate keysets. 1484008c04aSChris Mason * 14985d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 15085d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 15185d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1524008c04aSChris Mason * 15385d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 15485d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 15585d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 15685d4e461SChris Mason * 15785d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 15885d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 15985d4e461SChris Mason * needs update as well. 1604008c04aSChris Mason */ 1614008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1624008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1634008c04aSChris Mason # error 1644008c04aSChris Mason # endif 16585d4e461SChris Mason 16685d4e461SChris Mason static struct btrfs_lockdep_keyset { 16785d4e461SChris Mason u64 id; /* root objectid */ 16885d4e461SChris Mason const char *name_stem; /* lock name stem */ 16985d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 17085d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 17185d4e461SChris Mason } btrfs_lockdep_keysets[] = { 17285d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 17385d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 17485d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 17585d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 17685d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 17785d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 17860b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 17985d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 18085d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 18185d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 18213fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1836b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 18485d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1854008c04aSChris Mason }; 18685d4e461SChris Mason 18785d4e461SChris Mason void __init btrfs_init_lockdep(void) 18885d4e461SChris Mason { 18985d4e461SChris Mason int i, j; 19085d4e461SChris Mason 19185d4e461SChris Mason /* initialize lockdep class names */ 19285d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 19385d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 19485d4e461SChris Mason 19585d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 19685d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 19785d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 19885d4e461SChris Mason } 19985d4e461SChris Mason } 20085d4e461SChris Mason 20185d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 20285d4e461SChris Mason int level) 20385d4e461SChris Mason { 20485d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 20585d4e461SChris Mason 20685d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 20785d4e461SChris Mason 20885d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 20985d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 21085d4e461SChris Mason if (ks->id == objectid) 21185d4e461SChris Mason break; 21285d4e461SChris Mason 21385d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 21485d4e461SChris Mason &ks->keys[level], ks->names[level]); 21585d4e461SChris Mason } 21685d4e461SChris Mason 2174008c04aSChris Mason #endif 2184008c04aSChris Mason 219d352ac68SChris Mason /* 220d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 221d352ac68SChris Mason * that covers the entire device 222d352ac68SChris Mason */ 223fc4f21b1SNikolay Borisov static struct extent_map *btree_get_extent(struct btrfs_inode *inode, 224306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2255f39d397SChris Mason int create) 2265f39d397SChris Mason { 227fc4f21b1SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 228fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2295f39d397SChris Mason struct extent_map *em; 2305f39d397SChris Mason int ret; 2315f39d397SChris Mason 232890871beSChris Mason read_lock(&em_tree->lock); 233d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 234a061fc8dSChris Mason if (em) { 2350b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 236890871beSChris Mason read_unlock(&em_tree->lock); 2375f39d397SChris Mason goto out; 238a061fc8dSChris Mason } 239890871beSChris Mason read_unlock(&em_tree->lock); 2407b13b7b1SChris Mason 241172ddd60SDavid Sterba em = alloc_extent_map(); 2425f39d397SChris Mason if (!em) { 2435f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2445f39d397SChris Mason goto out; 2455f39d397SChris Mason } 2465f39d397SChris Mason em->start = 0; 2470afbaf8cSChris Mason em->len = (u64)-1; 248c8b97818SChris Mason em->block_len = (u64)-1; 2495f39d397SChris Mason em->block_start = 0; 2500b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 251d1310b2eSChris Mason 252890871beSChris Mason write_lock(&em_tree->lock); 25309a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2545f39d397SChris Mason if (ret == -EEXIST) { 2555f39d397SChris Mason free_extent_map(em); 2567b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 257b4f359abSTsutomu Itoh if (!em) 2580433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2595f39d397SChris Mason } else if (ret) { 2607b13b7b1SChris Mason free_extent_map(em); 2610433f20dSTsutomu Itoh em = ERR_PTR(ret); 2625f39d397SChris Mason } 263890871beSChris Mason write_unlock(&em_tree->lock); 2647b13b7b1SChris Mason 2655f39d397SChris Mason out: 2665f39d397SChris Mason return em; 2675f39d397SChris Mason } 2685f39d397SChris Mason 2699ed57367SDavid Sterba u32 btrfs_csum_data(const char *data, u32 seed, size_t len) 27019c00ddcSChris Mason { 2710b947affSFilipe David Borba Manana return btrfs_crc32c(seed, data, len); 27219c00ddcSChris Mason } 27319c00ddcSChris Mason 2740b5e3dafSDomagoj Tršan void btrfs_csum_final(u32 crc, u8 *result) 27519c00ddcSChris Mason { 2767e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 27719c00ddcSChris Mason } 27819c00ddcSChris Mason 279d352ac68SChris Mason /* 280d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 281d352ac68SChris Mason * into the csum field of the block. 282d352ac68SChris Mason */ 28301d58472SDaniel Dressler static int csum_tree_block(struct btrfs_fs_info *fs_info, 28401d58472SDaniel Dressler struct extent_buffer *buf, 28519c00ddcSChris Mason int verify) 28619c00ddcSChris Mason { 28701d58472SDaniel Dressler u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 288607d432dSJosef Bacik char *result = NULL; 28919c00ddcSChris Mason unsigned long len; 29019c00ddcSChris Mason unsigned long cur_len; 29119c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 29219c00ddcSChris Mason char *kaddr; 29319c00ddcSChris Mason unsigned long map_start; 29419c00ddcSChris Mason unsigned long map_len; 29519c00ddcSChris Mason int err; 29619c00ddcSChris Mason u32 crc = ~(u32)0; 297607d432dSJosef Bacik unsigned long inline_result; 29819c00ddcSChris Mason 29919c00ddcSChris Mason len = buf->len - offset; 30019c00ddcSChris Mason while (len > 0) { 30119c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 302a6591715SChris Mason &kaddr, &map_start, &map_len); 303d397712bSChris Mason if (err) 3048bd98f0eSAlex Lyakas return err; 30519c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 306b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 30719c00ddcSChris Mason crc, cur_len); 30819c00ddcSChris Mason len -= cur_len; 30919c00ddcSChris Mason offset += cur_len; 31019c00ddcSChris Mason } 311607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 31231e818feSDavid Sterba result = kzalloc(csum_size, GFP_NOFS); 313607d432dSJosef Bacik if (!result) 3148bd98f0eSAlex Lyakas return -ENOMEM; 315607d432dSJosef Bacik } else { 316607d432dSJosef Bacik result = (char *)&inline_result; 317607d432dSJosef Bacik } 318607d432dSJosef Bacik 31919c00ddcSChris Mason btrfs_csum_final(crc, result); 32019c00ddcSChris Mason 32119c00ddcSChris Mason if (verify) { 322607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 323e4204dedSChris Mason u32 val; 324e4204dedSChris Mason u32 found = 0; 325607d432dSJosef Bacik memcpy(&found, result, csum_size); 326e4204dedSChris Mason 327607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 32894647322SDavid Sterba btrfs_warn_rl(fs_info, 3295d163e0eSJeff Mahoney "%s checksum verify failed on %llu wanted %X found %X level %d", 33001d58472SDaniel Dressler fs_info->sb->s_id, buf->start, 331c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 332607d432dSJosef Bacik if (result != (char *)&inline_result) 333607d432dSJosef Bacik kfree(result); 3348bd98f0eSAlex Lyakas return -EUCLEAN; 33519c00ddcSChris Mason } 33619c00ddcSChris Mason } else { 337607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 33819c00ddcSChris Mason } 339607d432dSJosef Bacik if (result != (char *)&inline_result) 340607d432dSJosef Bacik kfree(result); 34119c00ddcSChris Mason return 0; 34219c00ddcSChris Mason } 34319c00ddcSChris Mason 344d352ac68SChris Mason /* 345d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 346d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 347d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 348d352ac68SChris Mason * in the wrong place. 349d352ac68SChris Mason */ 3501259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 351b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 352b9fab919SChris Mason int atomic) 3531259ab75SChris Mason { 3542ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3551259ab75SChris Mason int ret; 3562755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3571259ab75SChris Mason 3581259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3591259ab75SChris Mason return 0; 3601259ab75SChris Mason 361b9fab919SChris Mason if (atomic) 362b9fab919SChris Mason return -EAGAIN; 363b9fab919SChris Mason 364a26e8c9fSJosef Bacik if (need_lock) { 365a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 366a26e8c9fSJosef Bacik btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 367a26e8c9fSJosef Bacik } 368a26e8c9fSJosef Bacik 3692ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 370ff13db41SDavid Sterba &cached_state); 3710b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3721259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3731259ab75SChris Mason ret = 0; 3741259ab75SChris Mason goto out; 3751259ab75SChris Mason } 37694647322SDavid Sterba btrfs_err_rl(eb->fs_info, 37794647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 37894647322SDavid Sterba eb->start, 37929549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3801259ab75SChris Mason ret = 1; 381a26e8c9fSJosef Bacik 382a26e8c9fSJosef Bacik /* 383a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 384a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 38501327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 386a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 387a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 388a26e8c9fSJosef Bacik * making everybody very sad. 389a26e8c9fSJosef Bacik */ 390a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3910b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 39233958dc6SChris Mason out: 3932ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3942ac55d41SJosef Bacik &cached_state, GFP_NOFS); 395472b909fSJosef Bacik if (need_lock) 396a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3971259ab75SChris Mason return ret; 3981259ab75SChris Mason } 3991259ab75SChris Mason 400d352ac68SChris Mason /* 4011104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 4021104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 4031104a885SDavid Sterba */ 404ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 405ab8d0fc4SJeff Mahoney char *raw_disk_sb) 4061104a885SDavid Sterba { 4071104a885SDavid Sterba struct btrfs_super_block *disk_sb = 4081104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 4091104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 4101104a885SDavid Sterba int ret = 0; 4111104a885SDavid Sterba 4121104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 4131104a885SDavid Sterba u32 crc = ~(u32)0; 4141104a885SDavid Sterba const int csum_size = sizeof(crc); 4151104a885SDavid Sterba char result[csum_size]; 4161104a885SDavid Sterba 4171104a885SDavid Sterba /* 4181104a885SDavid Sterba * The super_block structure does not span the whole 4191104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 42001327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 4211104a885SDavid Sterba */ 4221104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4231104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4241104a885SDavid Sterba btrfs_csum_final(crc, result); 4251104a885SDavid Sterba 4261104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 4271104a885SDavid Sterba ret = 1; 4281104a885SDavid Sterba } 4291104a885SDavid Sterba 4301104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 431ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 4321104a885SDavid Sterba csum_type); 4331104a885SDavid Sterba ret = 1; 4341104a885SDavid Sterba } 4351104a885SDavid Sterba 4361104a885SDavid Sterba return ret; 4371104a885SDavid Sterba } 4381104a885SDavid Sterba 4391104a885SDavid Sterba /* 440d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 441d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 442d352ac68SChris Mason */ 4432ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 444f188591eSChris Mason struct extent_buffer *eb, 4458436ea91SJosef Bacik u64 parent_transid) 446f188591eSChris Mason { 447f188591eSChris Mason struct extent_io_tree *io_tree; 448ea466794SJosef Bacik int failed = 0; 449f188591eSChris Mason int ret; 450f188591eSChris Mason int num_copies = 0; 451f188591eSChris Mason int mirror_num = 0; 452ea466794SJosef Bacik int failed_mirror = 0; 453f188591eSChris Mason 454a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4550b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 456f188591eSChris Mason while (1) { 4578436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 458f188591eSChris Mason btree_get_extent, mirror_num); 459256dd1bbSStefan Behrens if (!ret) { 460256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 461b9fab919SChris Mason parent_transid, 0)) 462ea466794SJosef Bacik break; 463256dd1bbSStefan Behrens else 464256dd1bbSStefan Behrens ret = -EIO; 465256dd1bbSStefan Behrens } 466d397712bSChris Mason 467a826d6dcSJosef Bacik /* 468a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 469a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 470a826d6dcSJosef Bacik * any less wrong. 471a826d6dcSJosef Bacik */ 472a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 473ea466794SJosef Bacik break; 474ea466794SJosef Bacik 4750b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 476f188591eSChris Mason eb->start, eb->len); 4774235298eSChris Mason if (num_copies == 1) 478ea466794SJosef Bacik break; 4794235298eSChris Mason 4805cf1ab56SJosef Bacik if (!failed_mirror) { 4815cf1ab56SJosef Bacik failed = 1; 4825cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4835cf1ab56SJosef Bacik } 4845cf1ab56SJosef Bacik 485f188591eSChris Mason mirror_num++; 486ea466794SJosef Bacik if (mirror_num == failed_mirror) 487ea466794SJosef Bacik mirror_num++; 488ea466794SJosef Bacik 4894235298eSChris Mason if (mirror_num > num_copies) 490ea466794SJosef Bacik break; 491f188591eSChris Mason } 492ea466794SJosef Bacik 493c0901581SStefan Behrens if (failed && !ret && failed_mirror) 4942ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 495ea466794SJosef Bacik 496ea466794SJosef Bacik return ret; 497f188591eSChris Mason } 49819c00ddcSChris Mason 499d352ac68SChris Mason /* 500d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 501d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 502d352ac68SChris Mason */ 503d397712bSChris Mason 50401d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 50519c00ddcSChris Mason { 5064eee4fa4SMiao Xie u64 start = page_offset(page); 50719c00ddcSChris Mason u64 found_start; 50819c00ddcSChris Mason struct extent_buffer *eb; 509f188591eSChris Mason 5104f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5114f2de97aSJosef Bacik if (page != eb->pages[0]) 5124f2de97aSJosef Bacik return 0; 5130f805531SAlex Lyakas 51419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5150f805531SAlex Lyakas /* 5160f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5170f805531SAlex Lyakas * It is useful to know what went wrong. 5180f805531SAlex Lyakas */ 5190f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5200f805531SAlex Lyakas return -EUCLEAN; 5210f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5220f805531SAlex Lyakas return -EUCLEAN; 5230f805531SAlex Lyakas 5240f805531SAlex Lyakas ASSERT(memcmp_extent_buffer(eb, fs_info->fsid, 5250f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5260f805531SAlex Lyakas 5278bd98f0eSAlex Lyakas return csum_tree_block(fs_info, eb, 0); 52819c00ddcSChris Mason } 52919c00ddcSChris Mason 53001d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5312b82032cSYan Zheng struct extent_buffer *eb) 5322b82032cSYan Zheng { 53301d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 53444880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE]; 5352b82032cSYan Zheng int ret = 1; 5362b82032cSYan Zheng 5370a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5382b82032cSYan Zheng while (fs_devices) { 5392b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5402b82032cSYan Zheng ret = 0; 5412b82032cSYan Zheng break; 5422b82032cSYan Zheng } 5432b82032cSYan Zheng fs_devices = fs_devices->seed; 5442b82032cSYan Zheng } 5452b82032cSYan Zheng return ret; 5462b82032cSYan Zheng } 5472b82032cSYan Zheng 548facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 549facc8a22SMiao Xie u64 phy_offset, struct page *page, 550facc8a22SMiao Xie u64 start, u64 end, int mirror) 551ce9adaa5SChris Mason { 552ce9adaa5SChris Mason u64 found_start; 553ce9adaa5SChris Mason int found_level; 554ce9adaa5SChris Mason struct extent_buffer *eb; 555ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 55602873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 557f188591eSChris Mason int ret = 0; 558727011e0SChris Mason int reads_done; 559ce9adaa5SChris Mason 560ce9adaa5SChris Mason if (!page->private) 561ce9adaa5SChris Mason goto out; 562d397712bSChris Mason 5634f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 564d397712bSChris Mason 5650b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5660b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5670b32f4bbSJosef Bacik */ 5680b32f4bbSJosef Bacik extent_buffer_get(eb); 5690b32f4bbSJosef Bacik 5700b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 571727011e0SChris Mason if (!reads_done) 572727011e0SChris Mason goto err; 573f188591eSChris Mason 5745cf1ab56SJosef Bacik eb->read_mirror = mirror; 575656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 576ea466794SJosef Bacik ret = -EIO; 577ea466794SJosef Bacik goto err; 578ea466794SJosef Bacik } 579ea466794SJosef Bacik 580ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 581727011e0SChris Mason if (found_start != eb->start) { 58202873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 58394647322SDavid Sterba found_start, eb->start); 584f188591eSChris Mason ret = -EIO; 585ce9adaa5SChris Mason goto err; 586ce9adaa5SChris Mason } 58702873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 58802873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 58994647322SDavid Sterba eb->start); 5901259ab75SChris Mason ret = -EIO; 5911259ab75SChris Mason goto err; 5921259ab75SChris Mason } 593ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 5941c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 59502873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 5961c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 5971c24c3ceSJosef Bacik ret = -EIO; 5981c24c3ceSJosef Bacik goto err; 5991c24c3ceSJosef Bacik } 600ce9adaa5SChris Mason 60185d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 60285d4e461SChris Mason eb, found_level); 6034008c04aSChris Mason 60402873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 6058bd98f0eSAlex Lyakas if (ret) 606a826d6dcSJosef Bacik goto err; 607a826d6dcSJosef Bacik 608a826d6dcSJosef Bacik /* 609a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 610a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 611a826d6dcSJosef Bacik * return -EIO. 612a826d6dcSJosef Bacik */ 613557ea5ddSQu Wenruo if (found_level == 0 && btrfs_check_leaf(root, eb)) { 614a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 615a826d6dcSJosef Bacik ret = -EIO; 616a826d6dcSJosef Bacik } 617ce9adaa5SChris Mason 618557ea5ddSQu Wenruo if (found_level > 0 && btrfs_check_node(root, eb)) 619053ab70fSLiu Bo ret = -EIO; 620053ab70fSLiu Bo 6210b32f4bbSJosef Bacik if (!ret) 6220b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 623ce9adaa5SChris Mason err: 62479fb65a1SJosef Bacik if (reads_done && 62579fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 626d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 6274bb31e92SArne Jansen 62853b381b3SDavid Woodhouse if (ret) { 62953b381b3SDavid Woodhouse /* 63053b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 63153b381b3SDavid Woodhouse * again, we have to make sure it has something 63253b381b3SDavid Woodhouse * to decrement 63353b381b3SDavid Woodhouse */ 63453b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6350b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 63653b381b3SDavid Woodhouse } 6370b32f4bbSJosef Bacik free_extent_buffer(eb); 638ce9adaa5SChris Mason out: 639f188591eSChris Mason return ret; 640ce9adaa5SChris Mason } 641ce9adaa5SChris Mason 642ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6434bb31e92SArne Jansen { 6444bb31e92SArne Jansen struct extent_buffer *eb; 6454bb31e92SArne Jansen 6464f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 647656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 6485cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 64953b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 650ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 651d48d71aaSDavid Sterba btree_readahead_hook(eb, -EIO); 6524bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6534bb31e92SArne Jansen } 6544bb31e92SArne Jansen 6554246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 656ce9adaa5SChris Mason { 65797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 658ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 6599e0af237SLiu Bo struct btrfs_workqueue *wq; 6609e0af237SLiu Bo btrfs_work_func_t func; 661ce9adaa5SChris Mason 662ce9adaa5SChris Mason fs_info = end_io_wq->info; 6634e4cbee9SChristoph Hellwig end_io_wq->status = bio->bi_status; 664d20f7043SChris Mason 66537226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 6669e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 6679e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 6689e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 6699e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 6709e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 6719e0af237SLiu Bo func = btrfs_freespace_write_helper; 6729e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 6739e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 6749e0af237SLiu Bo func = btrfs_endio_raid56_helper; 675d20f7043SChris Mason } else { 6769e0af237SLiu Bo wq = fs_info->endio_write_workers; 6779e0af237SLiu Bo func = btrfs_endio_write_helper; 678d20f7043SChris Mason } 6799e0af237SLiu Bo } else { 6808b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 6818b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 6828b110e39SMiao Xie wq = fs_info->endio_repair_workers; 6838b110e39SMiao Xie func = btrfs_endio_repair_helper; 6848b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 6859e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 6869e0af237SLiu Bo func = btrfs_endio_raid56_helper; 6879e0af237SLiu Bo } else if (end_io_wq->metadata) { 6889e0af237SLiu Bo wq = fs_info->endio_meta_workers; 6899e0af237SLiu Bo func = btrfs_endio_meta_helper; 6909e0af237SLiu Bo } else { 6919e0af237SLiu Bo wq = fs_info->endio_workers; 6929e0af237SLiu Bo func = btrfs_endio_helper; 6939e0af237SLiu Bo } 6949e0af237SLiu Bo } 6959e0af237SLiu Bo 6969e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 6979e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 698ce9adaa5SChris Mason } 699ce9adaa5SChris Mason 7004e4cbee9SChristoph Hellwig blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 701bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7020b86a832SChris Mason { 70397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7048b110e39SMiao Xie 70597eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 706ce9adaa5SChris Mason if (!end_io_wq) 7074e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 708ce9adaa5SChris Mason 709ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 710ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 71122c59948SChris Mason end_io_wq->info = info; 7124e4cbee9SChristoph Hellwig end_io_wq->status = 0; 713ce9adaa5SChris Mason end_io_wq->bio = bio; 71422c59948SChris Mason end_io_wq->metadata = metadata; 715ce9adaa5SChris Mason 716ce9adaa5SChris Mason bio->bi_private = end_io_wq; 717ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 71822c59948SChris Mason return 0; 71922c59948SChris Mason } 72022c59948SChris Mason 721b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7224854ddd0SChris Mason { 7234854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7245cdc7ad3SQu Wenruo info->thread_pool_size, 7254854ddd0SChris Mason info->fs_devices->open_devices); 7264854ddd0SChris Mason return 256 * limit; 7274854ddd0SChris Mason } 7284854ddd0SChris Mason 7294a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7304a69a410SChris Mason { 7314a69a410SChris Mason struct async_submit_bio *async; 7324e4cbee9SChristoph Hellwig blk_status_t ret; 7334a69a410SChris Mason 7344a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 735c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 736eaf25d93SChris Mason async->mirror_num, async->bio_flags, 737eaf25d93SChris Mason async->bio_offset); 73879787eaaSJeff Mahoney if (ret) 7394e4cbee9SChristoph Hellwig async->status = ret; 7404a69a410SChris Mason } 7414a69a410SChris Mason 7424a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7438b712842SChris Mason { 7448b712842SChris Mason struct async_submit_bio *async; 7458b712842SChris Mason 7468b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7474854ddd0SChris Mason 748bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 7494e4cbee9SChristoph Hellwig if (async->status) { 7504e4cbee9SChristoph Hellwig async->bio->bi_status = async->status; 7514246a0b6SChristoph Hellwig bio_endio(async->bio); 75279787eaaSJeff Mahoney return; 75379787eaaSJeff Mahoney } 75479787eaaSJeff Mahoney 755c6100a4bSJosef Bacik async->submit_bio_done(async->private_data, async->bio, async->mirror_num, 75681a75f67SMike Christie async->bio_flags, async->bio_offset); 7574a69a410SChris Mason } 7584a69a410SChris Mason 7594a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7604a69a410SChris Mason { 7614a69a410SChris Mason struct async_submit_bio *async; 7624a69a410SChris Mason 7634a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7648b712842SChris Mason kfree(async); 7658b712842SChris Mason } 7668b712842SChris Mason 7678c27cb35SLinus Torvalds blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 768c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 769c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 7704a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7714a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 77244b8bd7eSChris Mason { 77344b8bd7eSChris Mason struct async_submit_bio *async; 77444b8bd7eSChris Mason 77544b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 77644b8bd7eSChris Mason if (!async) 7774e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 77844b8bd7eSChris Mason 779c6100a4bSJosef Bacik async->private_data = private_data; 780c6100a4bSJosef Bacik async->fs_info = fs_info; 78144b8bd7eSChris Mason async->bio = bio; 78244b8bd7eSChris Mason async->mirror_num = mirror_num; 7834a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7844a69a410SChris Mason async->submit_bio_done = submit_bio_done; 7854a69a410SChris Mason 7869e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 7875cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 7884a69a410SChris Mason 789c8b97818SChris Mason async->bio_flags = bio_flags; 790eaf25d93SChris Mason async->bio_offset = bio_offset; 7918c8bee1dSChris Mason 7924e4cbee9SChristoph Hellwig async->status = 0; 79379787eaaSJeff Mahoney 79467f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 7955cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 796d313d7a3SChris Mason 7975cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 79844b8bd7eSChris Mason return 0; 79944b8bd7eSChris Mason } 80044b8bd7eSChris Mason 8014e4cbee9SChristoph Hellwig static blk_status_t btree_csum_one_bio(struct bio *bio) 802ce3ed71aSChris Mason { 8032c30c71bSKent Overstreet struct bio_vec *bvec; 804ce3ed71aSChris Mason struct btrfs_root *root; 8052c30c71bSKent Overstreet int i, ret = 0; 806ce3ed71aSChris Mason 807c09abff8SDavid Sterba ASSERT(!bio_flagged(bio, BIO_CLONED)); 8082c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 809ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 81001d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 81179787eaaSJeff Mahoney if (ret) 81279787eaaSJeff Mahoney break; 813ce3ed71aSChris Mason } 8142c30c71bSKent Overstreet 8154e4cbee9SChristoph Hellwig return errno_to_blk_status(ret); 816ce3ed71aSChris Mason } 817ce3ed71aSChris Mason 8188c27cb35SLinus Torvalds static blk_status_t __btree_submit_bio_start(void *private_data, struct bio *bio, 81981a75f67SMike Christie int mirror_num, unsigned long bio_flags, 820eaf25d93SChris Mason u64 bio_offset) 82122c59948SChris Mason { 8228b712842SChris Mason /* 8238b712842SChris Mason * when we're called for a write, we're already in the async 8245443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8258b712842SChris Mason */ 82679787eaaSJeff Mahoney return btree_csum_one_bio(bio); 82722c59948SChris Mason } 82822c59948SChris Mason 8298c27cb35SLinus Torvalds static blk_status_t __btree_submit_bio_done(void *private_data, struct bio *bio, 830eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 831eaf25d93SChris Mason u64 bio_offset) 8324a69a410SChris Mason { 833c6100a4bSJosef Bacik struct inode *inode = private_data; 8344e4cbee9SChristoph Hellwig blk_status_t ret; 83561891923SStefan Behrens 8368b712842SChris Mason /* 8374a69a410SChris Mason * when we're called for a write, we're already in the async 8384a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8398b712842SChris Mason */ 8402ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 8414246a0b6SChristoph Hellwig if (ret) { 8424e4cbee9SChristoph Hellwig bio->bi_status = ret; 8434246a0b6SChristoph Hellwig bio_endio(bio); 8444246a0b6SChristoph Hellwig } 84561891923SStefan Behrens return ret; 8460b86a832SChris Mason } 8470b86a832SChris Mason 84818fdc679SLiu Bo static int check_async_write(struct btrfs_inode *bi) 849de0022b9SJosef Bacik { 8506300463bSLiu Bo if (atomic_read(&bi->sync_writers)) 8516300463bSLiu Bo return 0; 852de0022b9SJosef Bacik #ifdef CONFIG_X86 853bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 854de0022b9SJosef Bacik return 0; 855de0022b9SJosef Bacik #endif 856de0022b9SJosef Bacik return 1; 857de0022b9SJosef Bacik } 858de0022b9SJosef Bacik 8598c27cb35SLinus Torvalds static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio, 860eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 861eaf25d93SChris Mason u64 bio_offset) 86244b8bd7eSChris Mason { 863c6100a4bSJosef Bacik struct inode *inode = private_data; 8640b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 86518fdc679SLiu Bo int async = check_async_write(BTRFS_I(inode)); 8664e4cbee9SChristoph Hellwig blk_status_t ret; 867cad321adSChris Mason 86837226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 869cad321adSChris Mason /* 870cad321adSChris Mason * called for a read, do the setup so that checksum validation 871cad321adSChris Mason * can happen in the async kernel threads 872cad321adSChris Mason */ 8730b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 8740b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 8751d4284bdSChris Mason if (ret) 87661891923SStefan Behrens goto out_w_error; 8772ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 878de0022b9SJosef Bacik } else if (!async) { 879de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 880de0022b9SJosef Bacik if (ret) 88161891923SStefan Behrens goto out_w_error; 8822ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 88361891923SStefan Behrens } else { 884cad321adSChris Mason /* 88561891923SStefan Behrens * kthread helpers are used to submit writes so that 88661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 887cad321adSChris Mason */ 888c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 889c6100a4bSJosef Bacik bio_offset, private_data, 8904a69a410SChris Mason __btree_submit_bio_start, 8914a69a410SChris Mason __btree_submit_bio_done); 89244b8bd7eSChris Mason } 89344b8bd7eSChris Mason 8944246a0b6SChristoph Hellwig if (ret) 8954246a0b6SChristoph Hellwig goto out_w_error; 8964246a0b6SChristoph Hellwig return 0; 8974246a0b6SChristoph Hellwig 89861891923SStefan Behrens out_w_error: 8994e4cbee9SChristoph Hellwig bio->bi_status = ret; 9004246a0b6SChristoph Hellwig bio_endio(bio); 90161891923SStefan Behrens return ret; 90261891923SStefan Behrens } 90361891923SStefan Behrens 9043dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 905784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 906a6bc32b8SMel Gorman struct page *newpage, struct page *page, 907a6bc32b8SMel Gorman enum migrate_mode mode) 908784b4e29SChris Mason { 909784b4e29SChris Mason /* 910784b4e29SChris Mason * we can't safely write a btree page from here, 911784b4e29SChris Mason * we haven't done the locking hook 912784b4e29SChris Mason */ 913784b4e29SChris Mason if (PageDirty(page)) 914784b4e29SChris Mason return -EAGAIN; 915784b4e29SChris Mason /* 916784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 917784b4e29SChris Mason * We must have no buffers or drop them. 918784b4e29SChris Mason */ 919784b4e29SChris Mason if (page_has_private(page) && 920784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 921784b4e29SChris Mason return -EAGAIN; 922a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 923784b4e29SChris Mason } 9243dd1462eSJan Beulich #endif 925784b4e29SChris Mason 9260da5468fSChris Mason 9270da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9280da5468fSChris Mason struct writeback_control *wbc) 9290da5468fSChris Mason { 930e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 931e2d84521SMiao Xie int ret; 932e2d84521SMiao Xie 933d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 934448d640bSChris Mason 935448d640bSChris Mason if (wbc->for_kupdate) 936448d640bSChris Mason return 0; 937448d640bSChris Mason 938e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 939b9473439SChris Mason /* this is a bit racy, but that's ok */ 940e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 941e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 942e2d84521SMiao Xie if (ret < 0) 943793955bcSChris Mason return 0; 944793955bcSChris Mason } 9450b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9460da5468fSChris Mason } 9470da5468fSChris Mason 948b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9495f39d397SChris Mason { 950d1310b2eSChris Mason struct extent_io_tree *tree; 951d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9528ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9535f39d397SChris Mason } 9545f39d397SChris Mason 95570dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9565f39d397SChris Mason { 95798509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 95898509cfcSChris Mason return 0; 9590c4e538bSDavid Sterba 960f7a52a40SDavid Sterba return try_release_extent_buffer(page); 961d98237b3SChris Mason } 962d98237b3SChris Mason 963d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 964d47992f8SLukas Czerner unsigned int length) 965d98237b3SChris Mason { 966d1310b2eSChris Mason struct extent_io_tree *tree; 967d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9685f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9695f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9709ad6b7bcSChris Mason if (PagePrivate(page)) { 971efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 972efe120a0SFrank Holton "page private not zero on page %llu", 973efe120a0SFrank Holton (unsigned long long)page_offset(page)); 9749ad6b7bcSChris Mason ClearPagePrivate(page); 9759ad6b7bcSChris Mason set_page_private(page, 0); 97609cbfeafSKirill A. Shutemov put_page(page); 9779ad6b7bcSChris Mason } 978d98237b3SChris Mason } 979d98237b3SChris Mason 9800b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9810b32f4bbSJosef Bacik { 982bb146eb2SJosef Bacik #ifdef DEBUG 9830b32f4bbSJosef Bacik struct extent_buffer *eb; 9840b32f4bbSJosef Bacik 9850b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9860b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9870b32f4bbSJosef Bacik BUG_ON(!eb); 9880b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9890b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9900b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 991bb146eb2SJosef Bacik #endif 9920b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9930b32f4bbSJosef Bacik } 9940b32f4bbSJosef Bacik 9957f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 996d98237b3SChris Mason .readpage = btree_readpage, 9970da5468fSChris Mason .writepages = btree_writepages, 9985f39d397SChris Mason .releasepage = btree_releasepage, 9995f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10005a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1001784b4e29SChris Mason .migratepage = btree_migratepage, 10025a92bc88SChris Mason #endif 10030b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1004d98237b3SChris Mason }; 1005123abc88SChris Mason 10062ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1007090d1875SChris Mason { 10085f39d397SChris Mason struct extent_buffer *buf = NULL; 10092ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1010090d1875SChris Mason 10112ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1012c871b0f2SLiu Bo if (IS_ERR(buf)) 10136197d86eSDavid Sterba return; 1014d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 10158436ea91SJosef Bacik buf, WAIT_NONE, btree_get_extent, 0); 10165f39d397SChris Mason free_extent_buffer(buf); 1017090d1875SChris Mason } 1018090d1875SChris Mason 10192ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1020ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1021ab0fff03SArne Jansen { 1022ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 10232ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1024ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1025ab0fff03SArne Jansen int ret; 1026ab0fff03SArne Jansen 10272ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1028c871b0f2SLiu Bo if (IS_ERR(buf)) 1029ab0fff03SArne Jansen return 0; 1030ab0fff03SArne Jansen 1031ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1032ab0fff03SArne Jansen 10338436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 1034ab0fff03SArne Jansen btree_get_extent, mirror_num); 1035ab0fff03SArne Jansen if (ret) { 1036ab0fff03SArne Jansen free_extent_buffer(buf); 1037ab0fff03SArne Jansen return ret; 1038ab0fff03SArne Jansen } 1039ab0fff03SArne Jansen 1040ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1041ab0fff03SArne Jansen free_extent_buffer(buf); 1042ab0fff03SArne Jansen return -EIO; 10430b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1044ab0fff03SArne Jansen *eb = buf; 1045ab0fff03SArne Jansen } else { 1046ab0fff03SArne Jansen free_extent_buffer(buf); 1047ab0fff03SArne Jansen } 1048ab0fff03SArne Jansen return 0; 1049ab0fff03SArne Jansen } 1050ab0fff03SArne Jansen 10512ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 10522ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 10530308af44SDavid Sterba u64 bytenr) 10540999df54SChris Mason { 10550b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 10560b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 10570b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 10580999df54SChris Mason } 10590999df54SChris Mason 10600999df54SChris Mason 1061e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1062e02119d5SChris Mason { 1063727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 10648aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1065e02119d5SChris Mason } 1066e02119d5SChris Mason 10673189ff77SJeff Layton void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1068e02119d5SChris Mason { 10693189ff77SJeff Layton filemap_fdatawait_range(buf->pages[0]->mapping, 10708aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1071e02119d5SChris Mason } 1072e02119d5SChris Mason 10732ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1074ce86cd59SDavid Sterba u64 parent_transid) 1075e20d96d6SChris Mason { 10765f39d397SChris Mason struct extent_buffer *buf = NULL; 107719c00ddcSChris Mason int ret; 107819c00ddcSChris Mason 10792ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1080c871b0f2SLiu Bo if (IS_ERR(buf)) 1081c871b0f2SLiu Bo return buf; 1082e4204dedSChris Mason 10832ff7e61eSJeff Mahoney ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 10840f0fe8f7SFilipe David Borba Manana if (ret) { 10850f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 108664c043deSLiu Bo return ERR_PTR(ret); 10870f0fe8f7SFilipe David Borba Manana } 10885f39d397SChris Mason return buf; 1089ce9adaa5SChris Mason 1090eb60ceacSChris Mason } 1091eb60ceacSChris Mason 10927c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 10935f39d397SChris Mason struct extent_buffer *buf) 1094ed2ff2cbSChris Mason { 109555c69072SChris Mason if (btrfs_header_generation(buf) == 1096e2d84521SMiao Xie fs_info->running_transaction->transid) { 1097b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1098b4ce94deSChris Mason 1099b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1100104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 1101e2d84521SMiao Xie -buf->len, 1102e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1103b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1104b9473439SChris Mason btrfs_set_lock_blocking(buf); 11050b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1106925baeddSChris Mason } 11075f39d397SChris Mason } 1108ed7b63ebSJosef Bacik } 11095f39d397SChris Mason 11108257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11118257b2dcSMiao Xie { 11128257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11138257b2dcSMiao Xie int ret; 11148257b2dcSMiao Xie 11158257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11168257b2dcSMiao Xie if (!writers) 11178257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11188257b2dcSMiao Xie 1119908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 11208257b2dcSMiao Xie if (ret < 0) { 11218257b2dcSMiao Xie kfree(writers); 11228257b2dcSMiao Xie return ERR_PTR(ret); 11238257b2dcSMiao Xie } 11248257b2dcSMiao Xie 11258257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 11268257b2dcSMiao Xie return writers; 11278257b2dcSMiao Xie } 11288257b2dcSMiao Xie 11298257b2dcSMiao Xie static void 11308257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 11318257b2dcSMiao Xie { 11328257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 11338257b2dcSMiao Xie kfree(writers); 11348257b2dcSMiao Xie } 11358257b2dcSMiao Xie 1136da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1137e20d96d6SChris Mason u64 objectid) 1138d97e63b6SChris Mason { 11397c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1140cfaa7295SChris Mason root->node = NULL; 1141a28ec197SChris Mason root->commit_root = NULL; 114227cdeb70SMiao Xie root->state = 0; 1143d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11440b86a832SChris Mason 11450f7d52f4SChris Mason root->objectid = objectid; 11460f7d52f4SChris Mason root->last_trans = 0; 114713a8a7c8SYan, Zheng root->highest_objectid = 0; 1148eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1149199c2a9cSMiao Xie root->nr_ordered_extents = 0; 115058176a96SJosef Bacik root->name = NULL; 11516bef4d31SEric Paris root->inode_tree = RB_ROOT; 115216cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1153f0486c68SYan, Zheng root->block_rsv = NULL; 1154d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11550b86a832SChris Mason 11560b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11575d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1158eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1159eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1160199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1161199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 11622ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11632ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1164d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11655d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1166eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1167199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1168f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11692ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11702ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1171a2135011SChris Mason mutex_init(&root->objectid_mutex); 1172e02119d5SChris Mason mutex_init(&root->log_mutex); 117331f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1174573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 11757237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11767237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11777237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11788b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 11798b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 11807237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11817237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11827237f183SYan Zheng atomic_set(&root->log_writers, 0); 11832ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11848a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 11850700cea7SElena Reshetova refcount_set(&root->refs, 1); 1186ea14b57fSDavid Sterba atomic_set(&root->will_be_snapshotted, 0); 1187ce0dcee6SGoldwyn Rodrigues atomic64_set(&root->qgroup_meta_rsv, 0); 11887237f183SYan Zheng root->log_transid = 0; 1189d1433debSMiao Xie root->log_transid_committed = -1; 1190257c62e1SChris Mason root->last_log_commit = 0; 11917c0260eeSJeff Mahoney if (!dummy) 1192c6100a4bSJosef Bacik extent_io_tree_init(&root->dirty_log_pages, NULL); 1193017e5369SChris Mason 11943768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11953768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11966702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 11977c0260eeSJeff Mahoney if (!dummy) 11983f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119906ea65a3SJosef Bacik else 120006ea65a3SJosef Bacik root->defrag_trans_start = 0; 12014d775673SChris Mason root->root_key.objectid = objectid; 12020ee5dc67SAl Viro root->anon_dev = 0; 12038ea05e3aSAlexander Block 12045f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12053768f368SChris Mason } 12063768f368SChris Mason 120774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 120874e4d827SDavid Sterba gfp_t flags) 12096f07e42eSAl Viro { 121074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 12116f07e42eSAl Viro if (root) 12126f07e42eSAl Viro root->fs_info = fs_info; 12136f07e42eSAl Viro return root; 12146f07e42eSAl Viro } 12156f07e42eSAl Viro 121606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 121706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1218da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 121906ea65a3SJosef Bacik { 122006ea65a3SJosef Bacik struct btrfs_root *root; 122106ea65a3SJosef Bacik 12227c0260eeSJeff Mahoney if (!fs_info) 12237c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 12247c0260eeSJeff Mahoney 12257c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 122606ea65a3SJosef Bacik if (!root) 122706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1228da17066cSJeff Mahoney 1229b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1230da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1231faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 123206ea65a3SJosef Bacik 123306ea65a3SJosef Bacik return root; 123406ea65a3SJosef Bacik } 123506ea65a3SJosef Bacik #endif 123606ea65a3SJosef Bacik 123720897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 123820897f5cSArne Jansen struct btrfs_fs_info *fs_info, 123920897f5cSArne Jansen u64 objectid) 124020897f5cSArne Jansen { 124120897f5cSArne Jansen struct extent_buffer *leaf; 124220897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 124320897f5cSArne Jansen struct btrfs_root *root; 124420897f5cSArne Jansen struct btrfs_key key; 124520897f5cSArne Jansen int ret = 0; 12466463fe58SStefan Behrens uuid_le uuid; 124720897f5cSArne Jansen 124874e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 124920897f5cSArne Jansen if (!root) 125020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 125120897f5cSArne Jansen 1252da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 125320897f5cSArne Jansen root->root_key.objectid = objectid; 125420897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 125520897f5cSArne Jansen root->root_key.offset = 0; 125620897f5cSArne Jansen 12574d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 125820897f5cSArne Jansen if (IS_ERR(leaf)) { 125920897f5cSArne Jansen ret = PTR_ERR(leaf); 12601dd05682STsutomu Itoh leaf = NULL; 126120897f5cSArne Jansen goto fail; 126220897f5cSArne Jansen } 126320897f5cSArne Jansen 1264b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 126520897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 126620897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 126720897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 126820897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 126920897f5cSArne Jansen root->node = leaf; 127020897f5cSArne Jansen 1271d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1272d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 127320897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 127420897f5cSArne Jansen 127520897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 127627cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 127720897f5cSArne Jansen 127820897f5cSArne Jansen root->root_item.flags = 0; 127920897f5cSArne Jansen root->root_item.byte_limit = 0; 128020897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 128120897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 128220897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 128320897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 128420897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 128520897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 128620897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 12876463fe58SStefan Behrens uuid_le_gen(&uuid); 12886463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 128920897f5cSArne Jansen root->root_item.drop_level = 0; 129020897f5cSArne Jansen 129120897f5cSArne Jansen key.objectid = objectid; 129220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 129320897f5cSArne Jansen key.offset = 0; 129420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129520897f5cSArne Jansen if (ret) 129620897f5cSArne Jansen goto fail; 129720897f5cSArne Jansen 129820897f5cSArne Jansen btrfs_tree_unlock(leaf); 129920897f5cSArne Jansen 130020897f5cSArne Jansen return root; 13011dd05682STsutomu Itoh 13021dd05682STsutomu Itoh fail: 13031dd05682STsutomu Itoh if (leaf) { 13041dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 130559885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13061dd05682STsutomu Itoh free_extent_buffer(leaf); 13071dd05682STsutomu Itoh } 13081dd05682STsutomu Itoh kfree(root); 13091dd05682STsutomu Itoh 13101dd05682STsutomu Itoh return ERR_PTR(ret); 131120897f5cSArne Jansen } 131220897f5cSArne Jansen 13137237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1314e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13150f7d52f4SChris Mason { 13160f7d52f4SChris Mason struct btrfs_root *root; 13177237f183SYan Zheng struct extent_buffer *leaf; 1318e02119d5SChris Mason 131974e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1320e02119d5SChris Mason if (!root) 13217237f183SYan Zheng return ERR_PTR(-ENOMEM); 1322e02119d5SChris Mason 1323da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1324e02119d5SChris Mason 1325e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1326e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1327e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 132827cdeb70SMiao Xie 13297237f183SYan Zheng /* 133027cdeb70SMiao Xie * DON'T set REF_COWS for log trees 133127cdeb70SMiao Xie * 13327237f183SYan Zheng * log trees do not get reference counted because they go away 13337237f183SYan Zheng * before a real commit is actually done. They do store pointers 13347237f183SYan Zheng * to file data extents, and those reference counts still get 13357237f183SYan Zheng * updated (along with back refs to the log tree). 13367237f183SYan Zheng */ 1337e02119d5SChris Mason 13384d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 13394d75f8a9SDavid Sterba NULL, 0, 0, 0); 13407237f183SYan Zheng if (IS_ERR(leaf)) { 13417237f183SYan Zheng kfree(root); 13427237f183SYan Zheng return ERR_CAST(leaf); 13437237f183SYan Zheng } 1344e02119d5SChris Mason 1345b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 13465d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13475d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13485d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13495d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13507237f183SYan Zheng root->node = leaf; 1351e02119d5SChris Mason 13520b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1353e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1354e02119d5SChris Mason btrfs_tree_unlock(root->node); 13557237f183SYan Zheng return root; 13567237f183SYan Zheng } 13577237f183SYan Zheng 13587237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13597237f183SYan Zheng struct btrfs_fs_info *fs_info) 13607237f183SYan Zheng { 13617237f183SYan Zheng struct btrfs_root *log_root; 13627237f183SYan Zheng 13637237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13647237f183SYan Zheng if (IS_ERR(log_root)) 13657237f183SYan Zheng return PTR_ERR(log_root); 13667237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13677237f183SYan Zheng fs_info->log_root_tree = log_root; 13687237f183SYan Zheng return 0; 13697237f183SYan Zheng } 13707237f183SYan Zheng 13717237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13727237f183SYan Zheng struct btrfs_root *root) 13737237f183SYan Zheng { 13740b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 13757237f183SYan Zheng struct btrfs_root *log_root; 13767237f183SYan Zheng struct btrfs_inode_item *inode_item; 13777237f183SYan Zheng 13780b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 13797237f183SYan Zheng if (IS_ERR(log_root)) 13807237f183SYan Zheng return PTR_ERR(log_root); 13817237f183SYan Zheng 13827237f183SYan Zheng log_root->last_trans = trans->transid; 13837237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13847237f183SYan Zheng 13857237f183SYan Zheng inode_item = &log_root->root_item.inode; 13863cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 13873cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 13883cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1389da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 13900b246afaSJeff Mahoney fs_info->nodesize); 13913cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 13927237f183SYan Zheng 13935d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13947237f183SYan Zheng 13957237f183SYan Zheng WARN_ON(root->log_root); 13967237f183SYan Zheng root->log_root = log_root; 13977237f183SYan Zheng root->log_transid = 0; 1398d1433debSMiao Xie root->log_transid_committed = -1; 1399257c62e1SChris Mason root->last_log_commit = 0; 1400e02119d5SChris Mason return 0; 1401e02119d5SChris Mason } 1402e02119d5SChris Mason 140335a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1404cb517eabSMiao Xie struct btrfs_key *key) 1405e02119d5SChris Mason { 1406e02119d5SChris Mason struct btrfs_root *root; 1407e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14080f7d52f4SChris Mason struct btrfs_path *path; 140984234f3aSYan Zheng u64 generation; 1410cb517eabSMiao Xie int ret; 1411cb517eabSMiao Xie 1412cb517eabSMiao Xie path = btrfs_alloc_path(); 1413cb517eabSMiao Xie if (!path) 1414cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14150f7d52f4SChris Mason 141674e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1417cb517eabSMiao Xie if (!root) { 1418cb517eabSMiao Xie ret = -ENOMEM; 1419cb517eabSMiao Xie goto alloc_fail; 14200f7d52f4SChris Mason } 14210f7d52f4SChris Mason 1422da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 14230f7d52f4SChris Mason 1424cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1425cb517eabSMiao Xie &root->root_item, &root->root_key); 14260f7d52f4SChris Mason if (ret) { 142713a8a7c8SYan, Zheng if (ret > 0) 142813a8a7c8SYan, Zheng ret = -ENOENT; 1429cb517eabSMiao Xie goto find_fail; 14300f7d52f4SChris Mason } 143113a8a7c8SYan, Zheng 143284234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 14332ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 14342ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1435ce86cd59SDavid Sterba generation); 143664c043deSLiu Bo if (IS_ERR(root->node)) { 143764c043deSLiu Bo ret = PTR_ERR(root->node); 1438cb517eabSMiao Xie goto find_fail; 1439cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1440cb517eabSMiao Xie ret = -EIO; 144164c043deSLiu Bo free_extent_buffer(root->node); 144264c043deSLiu Bo goto find_fail; 1443416bc658SJosef Bacik } 14445d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 144513a8a7c8SYan, Zheng out: 1446cb517eabSMiao Xie btrfs_free_path(path); 1447cb517eabSMiao Xie return root; 1448cb517eabSMiao Xie 1449cb517eabSMiao Xie find_fail: 1450cb517eabSMiao Xie kfree(root); 1451cb517eabSMiao Xie alloc_fail: 1452cb517eabSMiao Xie root = ERR_PTR(ret); 1453cb517eabSMiao Xie goto out; 1454cb517eabSMiao Xie } 1455cb517eabSMiao Xie 1456cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1457cb517eabSMiao Xie struct btrfs_key *location) 1458cb517eabSMiao Xie { 1459cb517eabSMiao Xie struct btrfs_root *root; 1460cb517eabSMiao Xie 1461cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1462cb517eabSMiao Xie if (IS_ERR(root)) 1463cb517eabSMiao Xie return root; 1464cb517eabSMiao Xie 1465cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 146627cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 146708fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 146808fe4db1SLi Zefan } 146913a8a7c8SYan, Zheng 14705eda7b5eSChris Mason return root; 14715eda7b5eSChris Mason } 14725eda7b5eSChris Mason 1473cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1474cb517eabSMiao Xie { 1475cb517eabSMiao Xie int ret; 14768257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1477cb517eabSMiao Xie 1478cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1479cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1480cb517eabSMiao Xie GFP_NOFS); 1481cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1482cb517eabSMiao Xie ret = -ENOMEM; 1483cb517eabSMiao Xie goto fail; 1484cb517eabSMiao Xie } 1485cb517eabSMiao Xie 14868257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 14878257b2dcSMiao Xie if (IS_ERR(writers)) { 14888257b2dcSMiao Xie ret = PTR_ERR(writers); 14898257b2dcSMiao Xie goto fail; 14908257b2dcSMiao Xie } 14918257b2dcSMiao Xie root->subv_writers = writers; 14928257b2dcSMiao Xie 1493cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 149457cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 149557cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1496cb517eabSMiao Xie 1497cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1498cb517eabSMiao Xie if (ret) 1499876d2cf1SLiu Bo goto fail; 1500f32e48e9SChandan Rajendra 1501f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1502f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1503f32e48e9SChandan Rajendra &root->highest_objectid); 1504f32e48e9SChandan Rajendra if (ret) { 1505f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1506876d2cf1SLiu Bo goto fail; 1507f32e48e9SChandan Rajendra } 1508f32e48e9SChandan Rajendra 1509f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1510f32e48e9SChandan Rajendra 1511f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1512f32e48e9SChandan Rajendra 1513cb517eabSMiao Xie return 0; 1514cb517eabSMiao Xie fail: 1515876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1516cb517eabSMiao Xie return ret; 1517cb517eabSMiao Xie } 1518cb517eabSMiao Xie 151935bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1520cb517eabSMiao Xie u64 root_id) 1521cb517eabSMiao Xie { 1522cb517eabSMiao Xie struct btrfs_root *root; 1523cb517eabSMiao Xie 1524cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1525cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1526cb517eabSMiao Xie (unsigned long)root_id); 1527cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1528cb517eabSMiao Xie return root; 1529cb517eabSMiao Xie } 1530cb517eabSMiao Xie 1531cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1532cb517eabSMiao Xie struct btrfs_root *root) 1533cb517eabSMiao Xie { 1534cb517eabSMiao Xie int ret; 1535cb517eabSMiao Xie 1536e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1537cb517eabSMiao Xie if (ret) 1538cb517eabSMiao Xie return ret; 1539cb517eabSMiao Xie 1540cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1541cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1542cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1543cb517eabSMiao Xie root); 1544cb517eabSMiao Xie if (ret == 0) 154527cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1546cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1547cb517eabSMiao Xie radix_tree_preload_end(); 1548cb517eabSMiao Xie 1549cb517eabSMiao Xie return ret; 1550cb517eabSMiao Xie } 1551cb517eabSMiao Xie 1552c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1553c00869f1SMiao Xie struct btrfs_key *location, 1554c00869f1SMiao Xie bool check_ref) 15555eda7b5eSChris Mason { 15565eda7b5eSChris Mason struct btrfs_root *root; 1557381cf658SDavid Sterba struct btrfs_path *path; 15581d4c08e0SDavid Sterba struct btrfs_key key; 15595eda7b5eSChris Mason int ret; 15605eda7b5eSChris Mason 1561edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1562edbd8d4eSChris Mason return fs_info->tree_root; 1563edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1564edbd8d4eSChris Mason return fs_info->extent_root; 15658f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15668f18cf13SChris Mason return fs_info->chunk_root; 15678f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15688f18cf13SChris Mason return fs_info->dev_root; 15690403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15700403e47eSYan Zheng return fs_info->csum_root; 1571bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1572bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1573bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1574f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1575f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1576f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 157770f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 157870f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 157970f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 15804df27c4dSYan, Zheng again: 1581cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 158248475471SStefan Behrens if (root) { 1583c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 158448475471SStefan Behrens return ERR_PTR(-ENOENT); 15855eda7b5eSChris Mason return root; 158648475471SStefan Behrens } 15875eda7b5eSChris Mason 1588cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 15895eda7b5eSChris Mason if (IS_ERR(root)) 15905eda7b5eSChris Mason return root; 15913394e160SChris Mason 1592c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1593d68fc57bSYan, Zheng ret = -ENOENT; 1594d68fc57bSYan, Zheng goto fail; 1595d68fc57bSYan, Zheng } 1596d68fc57bSYan, Zheng 1597cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1598cb517eabSMiao Xie if (ret) 1599cb517eabSMiao Xie goto fail; 1600cb517eabSMiao Xie 1601381cf658SDavid Sterba path = btrfs_alloc_path(); 1602381cf658SDavid Sterba if (!path) { 1603381cf658SDavid Sterba ret = -ENOMEM; 1604381cf658SDavid Sterba goto fail; 1605381cf658SDavid Sterba } 16061d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 16071d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 16081d4c08e0SDavid Sterba key.offset = location->objectid; 16091d4c08e0SDavid Sterba 16101d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1611381cf658SDavid Sterba btrfs_free_path(path); 1612d68fc57bSYan, Zheng if (ret < 0) 1613d68fc57bSYan, Zheng goto fail; 1614d68fc57bSYan, Zheng if (ret == 0) 161527cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1616d68fc57bSYan, Zheng 1617cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16180f7d52f4SChris Mason if (ret) { 16194df27c4dSYan, Zheng if (ret == -EEXIST) { 16204df27c4dSYan, Zheng free_fs_root(root); 16214df27c4dSYan, Zheng goto again; 16220f7d52f4SChris Mason } 16234df27c4dSYan, Zheng goto fail; 16244df27c4dSYan, Zheng } 1625edbd8d4eSChris Mason return root; 16264df27c4dSYan, Zheng fail: 16274df27c4dSYan, Zheng free_fs_root(root); 16284df27c4dSYan, Zheng return ERR_PTR(ret); 1629edbd8d4eSChris Mason } 1630edbd8d4eSChris Mason 163104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 163204160088SChris Mason { 163304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 163404160088SChris Mason int ret = 0; 163504160088SChris Mason struct btrfs_device *device; 163604160088SChris Mason struct backing_dev_info *bdi; 1637b7967db7SChris Mason 16381f78160cSXiao Guangrong rcu_read_lock(); 16391f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1640dfe25020SChris Mason if (!device->bdev) 1641dfe25020SChris Mason continue; 1642efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1643ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 164404160088SChris Mason ret = 1; 164504160088SChris Mason break; 164604160088SChris Mason } 164704160088SChris Mason } 16481f78160cSXiao Guangrong rcu_read_unlock(); 164904160088SChris Mason return ret; 165004160088SChris Mason } 165104160088SChris Mason 16528b712842SChris Mason /* 16538b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16548b712842SChris Mason * functions. This is where read checksum verification actually happens 16558b712842SChris Mason */ 16568b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1657ce9adaa5SChris Mason { 1658ce9adaa5SChris Mason struct bio *bio; 165997eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1660ce9adaa5SChris Mason 166197eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1662ce9adaa5SChris Mason bio = end_io_wq->bio; 16638b712842SChris Mason 16644e4cbee9SChristoph Hellwig bio->bi_status = end_io_wq->status; 1665ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1666ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 166797eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 16684246a0b6SChristoph Hellwig bio_endio(bio); 1669ce9adaa5SChris Mason } 167044b8bd7eSChris Mason 1671a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1672a74a4b97SChris Mason { 1673a74a4b97SChris Mason struct btrfs_root *root = arg; 16740b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1675d0278245SMiao Xie int again; 1676da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1677a74a4b97SChris Mason 1678a74a4b97SChris Mason do { 1679d0278245SMiao Xie again = 0; 16809d1a2a3aSDavid Sterba 1681d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 16822ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1683d0278245SMiao Xie goto sleep; 1684d0278245SMiao Xie 168590c711abSZygo Blaxell /* 168690c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 168790c711abSZygo Blaxell * before we have finished mounting the filesystem. 168890c711abSZygo Blaxell */ 16890b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 169090c711abSZygo Blaxell goto sleep; 169190c711abSZygo Blaxell 16920b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1693d0278245SMiao Xie goto sleep; 1694d0278245SMiao Xie 1695dc7f370cSMiao Xie /* 1696dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1697dc7f370cSMiao Xie * during the above check and trylock. 1698dc7f370cSMiao Xie */ 16992ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 17000b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1701dc7f370cSMiao Xie goto sleep; 1702dc7f370cSMiao Xie } 1703dc7f370cSMiao Xie 17040b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 17052ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 17060b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1707c2d6cb16SFilipe Manana 17089d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 17090b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1710a74a4b97SChris Mason 1711d0278245SMiao Xie /* 171205323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 171305323cd1SMiao Xie * needn't do anything special here. 1714d0278245SMiao Xie */ 17150b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 171667c5e7d4SFilipe Manana 171767c5e7d4SFilipe Manana /* 171867c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 171967c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 172067c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 172167c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 172267c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 172367c5e7d4SFilipe Manana * unused block groups. 172467c5e7d4SFilipe Manana */ 17250b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1726d0278245SMiao Xie sleep: 1727838fe188SJiri Kosina if (!again) { 1728a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17298929ecfaSYan, Zheng if (!kthread_should_stop()) 1730a74a4b97SChris Mason schedule(); 1731a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1732a74a4b97SChris Mason } 1733a74a4b97SChris Mason } while (!kthread_should_stop()); 1734da288d28SFilipe Manana 1735da288d28SFilipe Manana /* 1736da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1737da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1738da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1739da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1740da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1741da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1742da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1743da288d28SFilipe Manana * since work queues and other resources were already released and 1744da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1745da288d28SFilipe Manana */ 1746da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1747da288d28SFilipe Manana if (IS_ERR(trans)) { 1748da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 17490b246afaSJeff Mahoney btrfs_err(fs_info, 1750da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1751da288d28SFilipe Manana PTR_ERR(trans)); 1752da288d28SFilipe Manana } else { 1753da288d28SFilipe Manana int ret; 1754da288d28SFilipe Manana 17553a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1756da288d28SFilipe Manana if (ret) 17570b246afaSJeff Mahoney btrfs_err(fs_info, 1758da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1759da288d28SFilipe Manana ret); 1760da288d28SFilipe Manana } 1761da288d28SFilipe Manana 1762a74a4b97SChris Mason return 0; 1763a74a4b97SChris Mason } 1764a74a4b97SChris Mason 1765a74a4b97SChris Mason static int transaction_kthread(void *arg) 1766a74a4b97SChris Mason { 1767a74a4b97SChris Mason struct btrfs_root *root = arg; 17680b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1769a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1770a74a4b97SChris Mason struct btrfs_transaction *cur; 17718929ecfaSYan, Zheng u64 transid; 1772a74a4b97SChris Mason unsigned long now; 1773a74a4b97SChris Mason unsigned long delay; 1774914b2007SJan Kara bool cannot_commit; 1775a74a4b97SChris Mason 1776a74a4b97SChris Mason do { 1777914b2007SJan Kara cannot_commit = false; 17780b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 17790b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1780a74a4b97SChris Mason 17810b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 17820b246afaSJeff Mahoney cur = fs_info->running_transaction; 1783a74a4b97SChris Mason if (!cur) { 17840b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1785a74a4b97SChris Mason goto sleep; 1786a74a4b97SChris Mason } 178731153d81SYan Zheng 1788a74a4b97SChris Mason now = get_seconds(); 17894a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 17908b87dc17SDavid Sterba (now < cur->start_time || 17910b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 17920b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1793a74a4b97SChris Mason delay = HZ * 5; 1794a74a4b97SChris Mason goto sleep; 1795a74a4b97SChris Mason } 17968929ecfaSYan, Zheng transid = cur->transid; 17970b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 179856bec294SChris Mason 179979787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1800354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1801914b2007SJan Kara if (IS_ERR(trans)) { 1802354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1803914b2007SJan Kara cannot_commit = true; 180479787eaaSJeff Mahoney goto sleep; 1805914b2007SJan Kara } 18068929ecfaSYan, Zheng if (transid == trans->transid) { 18073a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 18088929ecfaSYan, Zheng } else { 18093a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18108929ecfaSYan, Zheng } 1811a74a4b97SChris Mason sleep: 18120b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 18130b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1814a74a4b97SChris Mason 18154e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18160b246afaSJeff Mahoney &fs_info->fs_state))) 18172ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 1818a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18198929ecfaSYan, Zheng if (!kthread_should_stop() && 18200b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1821914b2007SJan Kara cannot_commit)) 1822a74a4b97SChris Mason schedule_timeout(delay); 1823a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1824a74a4b97SChris Mason } while (!kthread_should_stop()); 1825a74a4b97SChris Mason return 0; 1826a74a4b97SChris Mason } 1827a74a4b97SChris Mason 1828af31f5e5SChris Mason /* 1829af31f5e5SChris Mason * this will find the highest generation in the array of 1830af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1831af31f5e5SChris Mason * or -1 if we can't find anything. 1832af31f5e5SChris Mason * 1833af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1834af31f5e5SChris Mason * generation of the latest root in the array with the generation 1835af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1836af31f5e5SChris Mason */ 1837af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1838af31f5e5SChris Mason { 1839af31f5e5SChris Mason u64 cur; 1840af31f5e5SChris Mason int newest_index = -1; 1841af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1842af31f5e5SChris Mason int i; 1843af31f5e5SChris Mason 1844af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1845af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1846af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1847af31f5e5SChris Mason if (cur == newest_gen) 1848af31f5e5SChris Mason newest_index = i; 1849af31f5e5SChris Mason } 1850af31f5e5SChris Mason 1851af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1852af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1853af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1854af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1855af31f5e5SChris Mason if (cur == newest_gen) 1856af31f5e5SChris Mason newest_index = 0; 1857af31f5e5SChris Mason } 1858af31f5e5SChris Mason return newest_index; 1859af31f5e5SChris Mason } 1860af31f5e5SChris Mason 1861af31f5e5SChris Mason 1862af31f5e5SChris Mason /* 1863af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1864af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1865af31f5e5SChris Mason * field in the fs_info struct 1866af31f5e5SChris Mason */ 1867af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1868af31f5e5SChris Mason u64 newest_gen) 1869af31f5e5SChris Mason { 1870af31f5e5SChris Mason int newest_index = -1; 1871af31f5e5SChris Mason 1872af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1873af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1874af31f5e5SChris Mason if (newest_index == -1) { 1875af31f5e5SChris Mason info->backup_root_index = 0; 1876af31f5e5SChris Mason } else { 1877af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1878af31f5e5SChris Mason } 1879af31f5e5SChris Mason } 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason /* 1882af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1883af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1884af31f5e5SChris Mason * done 1885af31f5e5SChris Mason */ 1886af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1887af31f5e5SChris Mason { 1888af31f5e5SChris Mason int next_backup; 1889af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1890af31f5e5SChris Mason int last_backup; 1891af31f5e5SChris Mason 1892af31f5e5SChris Mason next_backup = info->backup_root_index; 1893af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1894af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1895af31f5e5SChris Mason 1896af31f5e5SChris Mason /* 1897af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1898af31f5e5SChris Mason * this happens only at umount 1899af31f5e5SChris Mason */ 1900af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1901af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1902af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1903af31f5e5SChris Mason next_backup = last_backup; 1904af31f5e5SChris Mason 1905af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1906af31f5e5SChris Mason 1907af31f5e5SChris Mason /* 1908af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1909af31f5e5SChris Mason * regardless of which ones we use today 1910af31f5e5SChris Mason */ 1911af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1912af31f5e5SChris Mason 1913af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1914af31f5e5SChris Mason 1915af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1916af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1917af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1918af31f5e5SChris Mason 1919af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1927af31f5e5SChris Mason 1928af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1929af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1930af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1931af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1932af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1933af31f5e5SChris Mason 19347c7e82a7SChris Mason /* 19357c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19367c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19377c7e82a7SChris Mason */ 19387c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19397c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19407c7e82a7SChris Mason info->fs_root->node->start); 1941af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1942af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1943af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1944af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19457c7e82a7SChris Mason } 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1948af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1949af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1950af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1951af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1952af31f5e5SChris Mason 1953af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1954af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1955af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1956af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1957af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1960af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1961af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1962af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1963af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1964af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1965af31f5e5SChris Mason 1966af31f5e5SChris Mason /* 1967af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1968af31f5e5SChris Mason * for the next commit 1969af31f5e5SChris Mason */ 1970af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1971af31f5e5SChris Mason &info->super_for_commit->super_roots, 1972af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1973af31f5e5SChris Mason } 1974af31f5e5SChris Mason 1975af31f5e5SChris Mason /* 1976af31f5e5SChris Mason * this copies info out of the root backup array and back into 1977af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1978af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1979af31f5e5SChris Mason * tried and the last backup index you used. 1980af31f5e5SChris Mason * 1981af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1982af31f5e5SChris Mason */ 1983af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1984af31f5e5SChris Mason struct btrfs_super_block *super, 1985af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1986af31f5e5SChris Mason { 1987af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1988af31f5e5SChris Mason int newest = *backup_index; 1989af31f5e5SChris Mason 1990af31f5e5SChris Mason if (*num_backups_tried == 0) { 1991af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1992af31f5e5SChris Mason 1993af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1994af31f5e5SChris Mason if (newest == -1) 1995af31f5e5SChris Mason return -1; 1996af31f5e5SChris Mason 1997af31f5e5SChris Mason *backup_index = newest; 1998af31f5e5SChris Mason *num_backups_tried = 1; 1999af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2000af31f5e5SChris Mason /* we've tried all the backups, all done */ 2001af31f5e5SChris Mason return -1; 2002af31f5e5SChris Mason } else { 2003af31f5e5SChris Mason /* jump to the next oldest backup */ 2004af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2005af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2006af31f5e5SChris Mason *backup_index = newest; 2007af31f5e5SChris Mason *num_backups_tried += 1; 2008af31f5e5SChris Mason } 2009af31f5e5SChris Mason root_backup = super->super_roots + newest; 2010af31f5e5SChris Mason 2011af31f5e5SChris Mason btrfs_set_super_generation(super, 2012af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2013af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2014af31f5e5SChris Mason btrfs_set_super_root_level(super, 2015af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2016af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2017af31f5e5SChris Mason 2018af31f5e5SChris Mason /* 2019af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2020af31f5e5SChris Mason * need a fsck 2021af31f5e5SChris Mason */ 2022af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2023af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2024af31f5e5SChris Mason return 0; 2025af31f5e5SChris Mason } 2026af31f5e5SChris Mason 20277abadb64SLiu Bo /* helper to cleanup workers */ 20287abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20297abadb64SLiu Bo { 2030dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2031afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20325cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2033fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2034fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20358b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2036d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2037fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2038fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2039a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20405b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2041e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2042736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2043a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2044fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2045a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2046a9b9477dSFilipe Manana /* 2047a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2048a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2049a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2050a9b9477dSFilipe Manana */ 2051a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2052a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 20537abadb64SLiu Bo } 20547abadb64SLiu Bo 20552e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20562e9f5954SRashika { 20572e9f5954SRashika if (root) { 20582e9f5954SRashika free_extent_buffer(root->node); 20592e9f5954SRashika free_extent_buffer(root->commit_root); 20602e9f5954SRashika root->node = NULL; 20612e9f5954SRashika root->commit_root = NULL; 20622e9f5954SRashika } 20632e9f5954SRashika } 20642e9f5954SRashika 2065af31f5e5SChris Mason /* helper to cleanup tree roots */ 2066af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2067af31f5e5SChris Mason { 20682e9f5954SRashika free_root_extent_buffers(info->tree_root); 2069655b09feSJosef Bacik 20702e9f5954SRashika free_root_extent_buffers(info->dev_root); 20712e9f5954SRashika free_root_extent_buffers(info->extent_root); 20722e9f5954SRashika free_root_extent_buffers(info->csum_root); 20732e9f5954SRashika free_root_extent_buffers(info->quota_root); 20742e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20752e9f5954SRashika if (chunk_root) 20762e9f5954SRashika free_root_extent_buffers(info->chunk_root); 207770f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2078af31f5e5SChris Mason } 2079af31f5e5SChris Mason 2080faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2081171f6537SJosef Bacik { 2082171f6537SJosef Bacik int ret; 2083171f6537SJosef Bacik struct btrfs_root *gang[8]; 2084171f6537SJosef Bacik int i; 2085171f6537SJosef Bacik 2086171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2087171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2088171f6537SJosef Bacik struct btrfs_root, root_list); 2089171f6537SJosef Bacik list_del(&gang[0]->root_list); 2090171f6537SJosef Bacik 209127cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2092cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2093171f6537SJosef Bacik } else { 2094171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2095171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2096b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2097171f6537SJosef Bacik } 2098171f6537SJosef Bacik } 2099171f6537SJosef Bacik 2100171f6537SJosef Bacik while (1) { 2101171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2102171f6537SJosef Bacik (void **)gang, 0, 2103171f6537SJosef Bacik ARRAY_SIZE(gang)); 2104171f6537SJosef Bacik if (!ret) 2105171f6537SJosef Bacik break; 2106171f6537SJosef Bacik for (i = 0; i < ret; i++) 2107cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2108171f6537SJosef Bacik } 21091a4319ccSLiu Bo 21101a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 21111a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 21122ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 21131a4319ccSLiu Bo } 2114171f6537SJosef Bacik } 2115af31f5e5SChris Mason 2116638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2117638aa7edSEric Sandeen { 2118638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2119638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2120638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2121638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2122638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2123638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2124638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2125638aa7edSEric Sandeen } 2126638aa7edSEric Sandeen 2127779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2128779a65a4SEric Sandeen { 2129779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2130779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2131779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2132779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2133779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2134779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2135779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2136779a65a4SEric Sandeen } 2137779a65a4SEric Sandeen 21386bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2139f37938e0SEric Sandeen { 21402ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 21412ff7e61eSJeff Mahoney 21422ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 21432ff7e61eSJeff Mahoney set_nlink(inode, 1); 2144f37938e0SEric Sandeen /* 2145f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2146f37938e0SEric Sandeen * the real end of the address space is determined by all of 2147f37938e0SEric Sandeen * the devices in the system 2148f37938e0SEric Sandeen */ 21492ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 21502ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2151f37938e0SEric Sandeen 21522ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 2153c6100a4bSJosef Bacik extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode); 21542ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 21552ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2156f37938e0SEric Sandeen 21572ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2158f37938e0SEric Sandeen 21592ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 21602ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 21612ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 21622ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2163f37938e0SEric Sandeen } 2164f37938e0SEric Sandeen 2165ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2166ad618368SEric Sandeen { 2167ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2168ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2169ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 217073beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 217173beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 217273beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2173ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 217473beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2175ad618368SEric Sandeen } 2176ad618368SEric Sandeen 2177f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2178f9e92e40SEric Sandeen { 2179f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2180f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2181f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2182f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2183f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2184f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2185f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2186d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2187f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2188f9e92e40SEric Sandeen } 2189f9e92e40SEric Sandeen 21902a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 21912a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 21922a458198SEric Sandeen { 21932a458198SEric Sandeen int max_active = fs_info->thread_pool_size; 21946f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 21952a458198SEric Sandeen 21962a458198SEric Sandeen fs_info->workers = 2197cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2198cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 21992a458198SEric Sandeen 22002a458198SEric Sandeen fs_info->delalloc_workers = 2201cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2202cb001095SJeff Mahoney flags, max_active, 2); 22032a458198SEric Sandeen 22042a458198SEric Sandeen fs_info->flush_workers = 2205cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2206cb001095SJeff Mahoney flags, max_active, 0); 22072a458198SEric Sandeen 22082a458198SEric Sandeen fs_info->caching_workers = 2209cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 22102a458198SEric Sandeen 22112a458198SEric Sandeen /* 22122a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 22132a458198SEric Sandeen * likely that bios will be send down in a sane order to the 22142a458198SEric Sandeen * devices 22152a458198SEric Sandeen */ 22162a458198SEric Sandeen fs_info->submit_workers = 2217cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 22182a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22192a458198SEric Sandeen max_active), 64); 22202a458198SEric Sandeen 22212a458198SEric Sandeen fs_info->fixup_workers = 2222cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 22232a458198SEric Sandeen 22242a458198SEric Sandeen /* 22252a458198SEric Sandeen * endios are largely parallel and should have a very 22262a458198SEric Sandeen * low idle thresh 22272a458198SEric Sandeen */ 22282a458198SEric Sandeen fs_info->endio_workers = 2229cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 22302a458198SEric Sandeen fs_info->endio_meta_workers = 2231cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2232cb001095SJeff Mahoney max_active, 4); 22332a458198SEric Sandeen fs_info->endio_meta_write_workers = 2234cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2235cb001095SJeff Mahoney max_active, 2); 22362a458198SEric Sandeen fs_info->endio_raid56_workers = 2237cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2238cb001095SJeff Mahoney max_active, 4); 22392a458198SEric Sandeen fs_info->endio_repair_workers = 2240cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 22412a458198SEric Sandeen fs_info->rmw_workers = 2242cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 22432a458198SEric Sandeen fs_info->endio_write_workers = 2244cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2245cb001095SJeff Mahoney max_active, 2); 22462a458198SEric Sandeen fs_info->endio_freespace_worker = 2247cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2248cb001095SJeff Mahoney max_active, 0); 22492a458198SEric Sandeen fs_info->delayed_workers = 2250cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2251cb001095SJeff Mahoney max_active, 0); 22522a458198SEric Sandeen fs_info->readahead_workers = 2253cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2254cb001095SJeff Mahoney max_active, 2); 22552a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2256cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 22572a458198SEric Sandeen fs_info->extent_workers = 2258cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 22592a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 22602a458198SEric Sandeen max_active), 8); 22612a458198SEric Sandeen 22622a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 22632a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 22642a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 22652a458198SEric Sandeen fs_info->endio_meta_write_workers && 22662a458198SEric Sandeen fs_info->endio_repair_workers && 22672a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 22682a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 22692a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 22702a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 22712a458198SEric Sandeen fs_info->extent_workers && 22722a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 22732a458198SEric Sandeen return -ENOMEM; 22742a458198SEric Sandeen } 22752a458198SEric Sandeen 22762a458198SEric Sandeen return 0; 22772a458198SEric Sandeen } 22782a458198SEric Sandeen 227963443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 228063443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 228163443bf5SEric Sandeen { 228263443bf5SEric Sandeen int ret; 228363443bf5SEric Sandeen struct btrfs_root *log_tree_root; 228463443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 228563443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 228663443bf5SEric Sandeen 228763443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2288f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 228963443bf5SEric Sandeen return -EIO; 229063443bf5SEric Sandeen } 229163443bf5SEric Sandeen 229274e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 229363443bf5SEric Sandeen if (!log_tree_root) 229463443bf5SEric Sandeen return -ENOMEM; 229563443bf5SEric Sandeen 2296da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 229763443bf5SEric Sandeen 22982ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 229963443bf5SEric Sandeen fs_info->generation + 1); 230064c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2301f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 23020eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 230364c043deSLiu Bo kfree(log_tree_root); 23040eeff236SLiu Bo return ret; 230564c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2306f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 230763443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 230863443bf5SEric Sandeen kfree(log_tree_root); 230963443bf5SEric Sandeen return -EIO; 231063443bf5SEric Sandeen } 231163443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 231263443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 231363443bf5SEric Sandeen if (ret) { 23140b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 231563443bf5SEric Sandeen "Failed to recover log tree"); 231663443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 231763443bf5SEric Sandeen kfree(log_tree_root); 231863443bf5SEric Sandeen return ret; 231963443bf5SEric Sandeen } 232063443bf5SEric Sandeen 2321bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 23226bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 232363443bf5SEric Sandeen if (ret) 232463443bf5SEric Sandeen return ret; 232563443bf5SEric Sandeen } 232663443bf5SEric Sandeen 232763443bf5SEric Sandeen return 0; 232863443bf5SEric Sandeen } 232963443bf5SEric Sandeen 23306bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 23314bbcaa64SEric Sandeen { 23326bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2333a4f3d2c4SDavid Sterba struct btrfs_root *root; 23344bbcaa64SEric Sandeen struct btrfs_key location; 23354bbcaa64SEric Sandeen int ret; 23364bbcaa64SEric Sandeen 23376bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 23386bccf3abSJeff Mahoney 23394bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 23404bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 23414bbcaa64SEric Sandeen location.offset = 0; 23424bbcaa64SEric Sandeen 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->extent_root = root; 23484bbcaa64SEric Sandeen 23494bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2350a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2351a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2352a4f3d2c4SDavid Sterba return PTR_ERR(root); 2353a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2354a4f3d2c4SDavid Sterba fs_info->dev_root = root; 23554bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 23564bbcaa64SEric Sandeen 23574bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2358a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2359a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2360a4f3d2c4SDavid Sterba return PTR_ERR(root); 2361a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2362a4f3d2c4SDavid Sterba fs_info->csum_root = root; 23634bbcaa64SEric Sandeen 23644bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2365a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2366a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2367a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2368afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2369a4f3d2c4SDavid Sterba fs_info->quota_root = root; 23704bbcaa64SEric Sandeen } 23714bbcaa64SEric Sandeen 23724bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2373a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2374a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2375a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 23764bbcaa64SEric Sandeen if (ret != -ENOENT) 23774bbcaa64SEric Sandeen return ret; 23784bbcaa64SEric Sandeen } else { 2379a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2380a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 23814bbcaa64SEric Sandeen } 23824bbcaa64SEric Sandeen 238370f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 238470f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 238570f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 238670f6d82eSOmar Sandoval if (IS_ERR(root)) 238770f6d82eSOmar Sandoval return PTR_ERR(root); 238870f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 238970f6d82eSOmar Sandoval fs_info->free_space_root = root; 239070f6d82eSOmar Sandoval } 239170f6d82eSOmar Sandoval 23924bbcaa64SEric Sandeen return 0; 23934bbcaa64SEric Sandeen } 23944bbcaa64SEric Sandeen 2395ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2396dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2397dfe25020SChris Mason char *options) 2398eb60ceacSChris Mason { 2399db94535dSChris Mason u32 sectorsize; 2400db94535dSChris Mason u32 nodesize; 240187ee04ebSChris Mason u32 stripesize; 240284234f3aSYan Zheng u64 generation; 2403f2b636e8SJosef Bacik u64 features; 24043de4586cSChris Mason struct btrfs_key location; 2405a061fc8dSChris Mason struct buffer_head *bh; 24064d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2407815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2408f84a8bd6SAl Viro struct btrfs_root *tree_root; 24094d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2410eb60ceacSChris Mason int ret; 2411e58ca020SYan int err = -EINVAL; 2412af31f5e5SChris Mason int num_backups_tried = 0; 2413af31f5e5SChris Mason int backup_index = 0; 24145cdc7ad3SQu Wenruo int max_active; 24156675df31SOmar Sandoval int clear_free_space_tree = 0; 24164543df7eSChris Mason 241774e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 241874e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2419cb517eabSMiao Xie if (!tree_root || !chunk_root) { 242039279cc3SChris Mason err = -ENOMEM; 242139279cc3SChris Mason goto fail; 242239279cc3SChris Mason } 242376dda93cSYan, Zheng 242476dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 242576dda93cSYan, Zheng if (ret) { 242676dda93cSYan, Zheng err = ret; 242776dda93cSYan, Zheng goto fail; 242876dda93cSYan, Zheng } 242976dda93cSYan, Zheng 2430908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2431e2d84521SMiao Xie if (ret) { 2432e2d84521SMiao Xie err = ret; 24339e11ceeeSJan Kara goto fail_srcu; 2434e2d84521SMiao Xie } 243509cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2436e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2437e2d84521SMiao Xie 2438908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2439963d678bSMiao Xie if (ret) { 2440963d678bSMiao Xie err = ret; 2441963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2442963d678bSMiao Xie } 2443963d678bSMiao Xie 2444908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2445c404e0dcSMiao Xie if (ret) { 2446c404e0dcSMiao Xie err = ret; 2447c404e0dcSMiao Xie goto fail_delalloc_bytes; 2448c404e0dcSMiao Xie } 2449c404e0dcSMiao Xie 245076dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2451f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 24528fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2453facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 245424bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2455eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 245611833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2457eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2458a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 245976dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 246024bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 24614cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2462f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2463ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2464fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2465f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 246647ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2467f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2468d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 246967c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 24707585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2471573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2472c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2473de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 247419c00ddcSChris Mason 24750b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 24766324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2477f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 247847ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 24790b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 248066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 248166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 248266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 248366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 248466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 248566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 248666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2487771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 24884cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2489fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 24902fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2491fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2492e20d96d6SChris Mason fs_info->sb = sb; 249395ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 24949ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 24954cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2496a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2497f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 24988b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2499f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 250090519d66SArne Jansen /* readahead state */ 2501d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 250290519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2503fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info); 2504c8b97818SChris Mason 2505b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2506b34b086cSChris Mason num_online_cpus() + 2, 8); 25070afbaf8cSChris Mason 2508199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2509199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 251069fe2d75SJosef Bacik 251169fe2d75SJosef Bacik fs_info->btree_inode = new_inode(sb); 251269fe2d75SJosef Bacik if (!fs_info->btree_inode) { 251369fe2d75SJosef Bacik err = -ENOMEM; 251469fe2d75SJosef Bacik goto fail_bio_counter; 251569fe2d75SJosef Bacik } 251669fe2d75SJosef Bacik mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 251769fe2d75SJosef Bacik 251816cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 251974e4d827SDavid Sterba GFP_KERNEL); 252016cdcec7SMiao Xie if (!fs_info->delayed_root) { 252116cdcec7SMiao Xie err = -ENOMEM; 252216cdcec7SMiao Xie goto fail_iput; 252316cdcec7SMiao Xie } 252416cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 25253eaa2885SChris Mason 2526638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 252721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 252821adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 252921adbd5cSStefan Behrens #endif 2530779a65a4SEric Sandeen btrfs_init_balance(fs_info); 253121c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2532a2de733cSArne Jansen 25339f6d2510SDavid Sterba sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE; 25349f6d2510SDavid Sterba sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE); 2535a061fc8dSChris Mason 25366bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 253739279cc3SChris Mason 2538e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 25396bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2540a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2541925baeddSChris Mason 2542c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[0], NULL); 2543c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[1], NULL); 254411833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2545afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2546509659cdSChris Mason 25475a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 254879154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2549925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2550a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2551a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 25527d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 25531bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 25549e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2555c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 255676dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2557803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2558fa9c0d79SChris Mason 2559ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2560f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2561416ac51dSArne Jansen 2562fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2563fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2564fa9c0d79SChris Mason 2565e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2566f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2567bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 25684854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 25693768f368SChris Mason 257004216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 257104216820SFilipe Manana 2572da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2573da17066cSJeff Mahoney fs_info->nodesize = 4096; 2574da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2575da17066cSJeff Mahoney fs_info->stripesize = 4096; 2576da17066cSJeff Mahoney 257753b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 257853b381b3SDavid Woodhouse if (ret) { 257983c8266aSDavid Sterba err = ret; 258053b381b3SDavid Woodhouse goto fail_alloc; 258153b381b3SDavid Woodhouse } 258253b381b3SDavid Woodhouse 2583da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 25847eccb903SChris Mason 25853c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 25861104a885SDavid Sterba 25871104a885SDavid Sterba /* 25881104a885SDavid Sterba * Read super block and check the signature bytes only 25891104a885SDavid Sterba */ 2590a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 259192fc03fbSAnand Jain if (IS_ERR(bh)) { 259292fc03fbSAnand Jain err = PTR_ERR(bh); 259316cdcec7SMiao Xie goto fail_alloc; 259420b45077SDave Young } 259539279cc3SChris Mason 25961104a885SDavid Sterba /* 25971104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 25981104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 25991104a885SDavid Sterba */ 2600ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 260105135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 26021104a885SDavid Sterba err = -EINVAL; 2603b2acdddfSAnand Jain brelse(bh); 26041104a885SDavid Sterba goto fail_alloc; 26051104a885SDavid Sterba } 26061104a885SDavid Sterba 26071104a885SDavid Sterba /* 26081104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 26091104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 26101104a885SDavid Sterba * the whole block of INFO_SIZE 26111104a885SDavid Sterba */ 26126c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 26136c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 26146c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2615a061fc8dSChris Mason brelse(bh); 26165f39d397SChris Mason 26176c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 26180b86a832SChris Mason 26193d3a126aSDavid Sterba ret = btrfs_check_super_valid(fs_info); 26201104a885SDavid Sterba if (ret) { 262105135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 26221104a885SDavid Sterba err = -EINVAL; 26231104a885SDavid Sterba goto fail_alloc; 26241104a885SDavid Sterba } 26251104a885SDavid Sterba 26266c41761fSDavid Sterba disk_super = fs_info->super_copy; 26270f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 262816cdcec7SMiao Xie goto fail_alloc; 26290f7d52f4SChris Mason 2630acce952bSliubo /* check FS state, whether FS is broken. */ 263187533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 263287533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2633acce952bSliubo 263475e7cb7fSLiu Bo /* 2635af31f5e5SChris Mason * run through our array of backup supers and setup 2636af31f5e5SChris Mason * our ring pointer to the oldest one 2637af31f5e5SChris Mason */ 2638af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2639af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2640af31f5e5SChris Mason 2641af31f5e5SChris Mason /* 264275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 264375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 264475e7cb7fSLiu Bo */ 264575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 264675e7cb7fSLiu Bo 26472ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 26482b82032cSYan Zheng if (ret) { 26492b82032cSYan Zheng err = ret; 265016cdcec7SMiao Xie goto fail_alloc; 26512b82032cSYan Zheng } 2652dfe25020SChris Mason 2653f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2654f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2655f2b636e8SJosef Bacik if (features) { 265605135f59SDavid Sterba btrfs_err(fs_info, 265705135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2658c1c9ff7cSGeert Uytterhoeven features); 2659f2b636e8SJosef Bacik err = -EINVAL; 266016cdcec7SMiao Xie goto fail_alloc; 2661f2b636e8SJosef Bacik } 2662f2b636e8SJosef Bacik 26635d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 26645d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 26650b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2666a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 26675c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD) 26685c1aab1dSNick Terrell features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD; 2669727011e0SChris Mason 26703173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 267105135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 26723173a18fSJosef Bacik 2673727011e0SChris Mason /* 2674727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2675727011e0SChris Mason * they are bigger than the page size 2676727011e0SChris Mason */ 267709cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2678727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 267905135f59SDavid Sterba btrfs_info(fs_info, 268005135f59SDavid Sterba "flagging fs with big metadata feature"); 2681727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2682727011e0SChris Mason } 2683727011e0SChris Mason 2684bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2685bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2686b7f67055SChandan Rajendra stripesize = sectorsize; 2687707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2688963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2689bc3f116fSChris Mason 2690da17066cSJeff Mahoney /* Cache block sizes */ 2691da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2692da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2693da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2694da17066cSJeff Mahoney 2695bc3f116fSChris Mason /* 2696bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2697bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2698bc3f116fSChris Mason */ 2699bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2700707e8a07SDavid Sterba (sectorsize != nodesize)) { 270105135f59SDavid Sterba btrfs_err(fs_info, 270205135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 270305135f59SDavid Sterba nodesize, sectorsize); 2704bc3f116fSChris Mason goto fail_alloc; 2705bc3f116fSChris Mason } 2706bc3f116fSChris Mason 2707ceda0864SMiao Xie /* 2708ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2709ceda0864SMiao Xie * update the flag. 2710ceda0864SMiao Xie */ 27115d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 27125d4f98a2SYan Zheng 2713f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2714f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2715bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) { 271605135f59SDavid Sterba btrfs_err(fs_info, 271705135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2718c1c9ff7cSGeert Uytterhoeven features); 2719f2b636e8SJosef Bacik err = -EINVAL; 272016cdcec7SMiao Xie goto fail_alloc; 2721f2b636e8SJosef Bacik } 272261d92c32SChris Mason 27235cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 272461d92c32SChris Mason 27252a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 27262a458198SEric Sandeen if (ret) { 27272a458198SEric Sandeen err = ret; 27280dc3b84aSJosef Bacik goto fail_sb_buffer; 27290dc3b84aSJosef Bacik } 27304543df7eSChris Mason 27319e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 27329e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 27339e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 2734d4417e22SNikolay Borisov sb->s_bdi->ra_pages = VM_MAX_READAHEAD * SZ_1K / PAGE_SIZE; 27359e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 27369e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 27374575c9ccSChris Mason 2738a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2739a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2740ee87cf5eSAnand Jain memcpy(&sb->s_uuid, fs_info->fsid, BTRFS_FSID_SIZE); 2741db94535dSChris Mason 2742925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 27436bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2744925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 274584eed90fSChris Mason if (ret) { 274605135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 27475d4f98a2SYan Zheng goto fail_sb_buffer; 274884eed90fSChris Mason } 27490b86a832SChris Mason 275084234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 27510b86a832SChris Mason 2752da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 27530b86a832SChris Mason 27542ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 27550b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2756ce86cd59SDavid Sterba generation); 275764c043deSLiu Bo if (IS_ERR(chunk_root->node) || 275864c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 275905135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2760e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2761e5fffbacSchandan free_extent_buffer(chunk_root->node); 276295ab1f64SZhao Lei chunk_root->node = NULL; 2763af31f5e5SChris Mason goto fail_tree_roots; 276483121942SDavid Woodhouse } 27655d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 27665d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 27670b86a832SChris Mason 2768e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2769b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2770e17cade2SChris Mason 27715b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 27722b82032cSYan Zheng if (ret) { 277305135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2774af31f5e5SChris Mason goto fail_tree_roots; 27752b82032cSYan Zheng } 27760b86a832SChris Mason 27778dabb742SStefan Behrens /* 27788dabb742SStefan Behrens * keep the device that is marked to be the target device for the 27798dabb742SStefan Behrens * dev_replace procedure 27808dabb742SStefan Behrens */ 27819eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 0); 2782dfe25020SChris Mason 2783a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 278405135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2785a6b0d5c8SChris Mason goto fail_tree_roots; 2786a6b0d5c8SChris Mason } 2787a6b0d5c8SChris Mason 2788af31f5e5SChris Mason retry_root_backup: 278984234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 27900b86a832SChris Mason 27912ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2792db94535dSChris Mason btrfs_super_root(disk_super), 2793ce86cd59SDavid Sterba generation); 279464c043deSLiu Bo if (IS_ERR(tree_root->node) || 279564c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 279605135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2797e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2798e5fffbacSchandan free_extent_buffer(tree_root->node); 279995ab1f64SZhao Lei tree_root->node = NULL; 2800af31f5e5SChris Mason goto recovery_tree_root; 280183121942SDavid Woodhouse } 2802af31f5e5SChris Mason 28035d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 28045d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 280569e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2806db94535dSChris Mason 2807f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2808f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2809f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2810f32e48e9SChandan Rajendra if (ret) { 2811f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2812f32e48e9SChandan Rajendra goto recovery_tree_root; 2813f32e48e9SChandan Rajendra } 2814f32e48e9SChandan Rajendra 2815f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2816f32e48e9SChandan Rajendra 2817f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2818f32e48e9SChandan Rajendra 28196bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 28204bbcaa64SEric Sandeen if (ret) 2821af31f5e5SChris Mason goto recovery_tree_root; 2822f7a81ea4SStefan Behrens 28238929ecfaSYan, Zheng fs_info->generation = generation; 28248929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 28258929ecfaSYan, Zheng 282668310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 282768310a5eSIlya Dryomov if (ret) { 282805135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 282968310a5eSIlya Dryomov goto fail_block_groups; 283068310a5eSIlya Dryomov } 283168310a5eSIlya Dryomov 2832733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2833733f4fbbSStefan Behrens if (ret) { 283405135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 2835733f4fbbSStefan Behrens goto fail_block_groups; 2836733f4fbbSStefan Behrens } 2837733f4fbbSStefan Behrens 28388dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 28398dabb742SStefan Behrens if (ret) { 284005135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 28418dabb742SStefan Behrens goto fail_block_groups; 28428dabb742SStefan Behrens } 28438dabb742SStefan Behrens 28449eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 1); 28458dabb742SStefan Behrens 2846b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 2847b7c35e81SAnand Jain if (ret) { 284805135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 284905135f59SDavid Sterba ret); 2850b7c35e81SAnand Jain goto fail_block_groups; 2851b7c35e81SAnand Jain } 2852b7c35e81SAnand Jain 2853b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 2854b7c35e81SAnand Jain if (ret) { 285505135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 285605135f59SDavid Sterba ret); 2857b7c35e81SAnand Jain goto fail_fsdev_sysfs; 2858b7c35e81SAnand Jain } 2859b7c35e81SAnand Jain 286096f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 28615ac1d209SJeff Mahoney if (ret) { 286205135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 2863b7c35e81SAnand Jain goto fail_fsdev_sysfs; 28645ac1d209SJeff Mahoney } 28655ac1d209SJeff Mahoney 2866c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2867c59021f8Sliubo if (ret) { 286805135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 28692365dd3cSAnand Jain goto fail_sysfs; 2870c59021f8Sliubo } 2871c59021f8Sliubo 28725b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 28731b1d1f66SJosef Bacik if (ret) { 287405135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 28752365dd3cSAnand Jain goto fail_sysfs; 28761b1d1f66SJosef Bacik } 28774330e183SQu Wenruo 28780f0d1272SLinus Torvalds if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info)) { 287905135f59SDavid Sterba btrfs_warn(fs_info, 28804330e183SQu Wenruo "writeable mount is not allowed due to too many missing devices"); 28812365dd3cSAnand Jain goto fail_sysfs; 2882292fd7fcSStefan Behrens } 28839078a3e1SChris Mason 2884a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2885a74a4b97SChris Mason "btrfs-cleaner"); 288657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 28872365dd3cSAnand Jain goto fail_sysfs; 2888a74a4b97SChris Mason 2889a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2890a74a4b97SChris Mason tree_root, 2891a74a4b97SChris Mason "btrfs-transaction"); 289257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 28933f157a2fSChris Mason goto fail_cleaner; 2894a74a4b97SChris Mason 2895583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) && 2896c289811cSChris Mason !fs_info->fs_devices->rotating) { 2897583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations"); 2898c289811cSChris Mason } 2899c289811cSChris Mason 2900572d9ab7SDavid Sterba /* 290101327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 2902572d9ab7SDavid Sterba * not have to wait for transaction commit 2903572d9ab7SDavid Sterba */ 2904572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 29053818aea2SQu Wenruo 290621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 29070b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 29082ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 29090b246afaSJeff Mahoney btrfs_test_opt(fs_info, 291021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 291121adbd5cSStefan Behrens 1 : 0, 291221adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 291321adbd5cSStefan Behrens if (ret) 291405135f59SDavid Sterba btrfs_warn(fs_info, 291505135f59SDavid Sterba "failed to initialize integrity check module: %d", 291605135f59SDavid Sterba ret); 291721adbd5cSStefan Behrens } 291821adbd5cSStefan Behrens #endif 2919bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2920bcef60f2SArne Jansen if (ret) 2921bcef60f2SArne Jansen goto fail_trans_kthread; 292221adbd5cSStefan Behrens 2923fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info)) 2924fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree"); 2925fd708b81SJosef Bacik 292696da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 292796da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 29280b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 292963443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 293079787eaaSJeff Mahoney if (ret) { 293163443bf5SEric Sandeen err = ret; 293228c16cbbSWang Shilong goto fail_qgroup; 2933e556ce2cSYan Zheng } 2934e02119d5SChris Mason } 29351a40e23bSZheng Yan 29366bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 293779787eaaSJeff Mahoney if (ret) 293828c16cbbSWang Shilong goto fail_qgroup; 293976dda93cSYan, Zheng 2940bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 2941d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 294244c44af2SIlya Dryomov if (ret) 294328c16cbbSWang Shilong goto fail_qgroup; 294490c711abSZygo Blaxell 294590c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 29465d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 294790c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 2948d7ce5843SMiao Xie if (ret < 0) { 294905135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 295005135f59SDavid Sterba ret); 2951d7ce5843SMiao Xie err = -EINVAL; 2952bcef60f2SArne Jansen goto fail_qgroup; 2953d7ce5843SMiao Xie } 29547c2ca468SChris Mason } 29551a40e23bSZheng Yan 29563de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 29573de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2958cb517eabSMiao Xie location.offset = 0; 29593de4586cSChris Mason 29603de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 29613140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 29623140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2963bcef60f2SArne Jansen goto fail_qgroup; 29643140c9a3SDan Carpenter } 2965c289811cSChris Mason 2966bc98a42cSDavid Howells if (sb_rdonly(sb)) 29672b6ba629SIlya Dryomov return 0; 29682b6ba629SIlya Dryomov 2969f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 2970f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 29716675df31SOmar Sandoval clear_free_space_tree = 1; 29726675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 29736675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 29746675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 29756675df31SOmar Sandoval clear_free_space_tree = 1; 29766675df31SOmar Sandoval } 29776675df31SOmar Sandoval 29786675df31SOmar Sandoval if (clear_free_space_tree) { 2979f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 2980f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 2981f8d468a1SOmar Sandoval if (ret) { 2982f8d468a1SOmar Sandoval btrfs_warn(fs_info, 2983f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 29846bccf3abSJeff Mahoney close_ctree(fs_info); 2985f8d468a1SOmar Sandoval return ret; 2986f8d468a1SOmar Sandoval } 2987f8d468a1SOmar Sandoval } 2988f8d468a1SOmar Sandoval 29890b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 2990511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 299105135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 2992511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 2993511711afSChris Mason if (ret) { 299405135f59SDavid Sterba btrfs_warn(fs_info, 299505135f59SDavid Sterba "failed to create free space tree: %d", ret); 29966bccf3abSJeff Mahoney close_ctree(fs_info); 2997511711afSChris Mason return ret; 2998511711afSChris Mason } 2999511711afSChris Mason } 3000511711afSChris Mason 3001e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 30022b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 30032b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 30042b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 30056bccf3abSJeff Mahoney close_ctree(fs_info); 30062b6ba629SIlya Dryomov return ret; 30072b6ba629SIlya Dryomov } 3008e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 300959641015SIlya Dryomov 30102b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 30112b6ba629SIlya Dryomov if (ret) { 301205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 30136bccf3abSJeff Mahoney close_ctree(fs_info); 30142b6ba629SIlya Dryomov return ret; 3015e3acc2a6SJosef Bacik } 3016e3acc2a6SJosef Bacik 30178dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 30188dabb742SStefan Behrens if (ret) { 301905135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 30206bccf3abSJeff Mahoney close_ctree(fs_info); 30218dabb742SStefan Behrens return ret; 30228dabb742SStefan Behrens } 30238dabb742SStefan Behrens 3024b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3025b382a324SJan Schmidt 30264bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 302705135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3028f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3029f7a81ea4SStefan Behrens if (ret) { 303005135f59SDavid Sterba btrfs_warn(fs_info, 303105135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 30326bccf3abSJeff Mahoney close_ctree(fs_info); 3033f7a81ea4SStefan Behrens return ret; 3034f7a81ea4SStefan Behrens } 30350b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 30364bbcaa64SEric Sandeen fs_info->generation != 30374bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 303805135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 303970f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 304070f80175SStefan Behrens if (ret) { 304105135f59SDavid Sterba btrfs_warn(fs_info, 304205135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 30436bccf3abSJeff Mahoney close_ctree(fs_info); 304470f80175SStefan Behrens return ret; 304570f80175SStefan Behrens } 304670f80175SStefan Behrens } else { 3047afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3048f7a81ea4SStefan Behrens } 3049afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 305047ab2a6cSJosef Bacik 30518dcddfa0SQu Wenruo /* 30528dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 30538dcddfa0SQu Wenruo * no need to keep the flag 30548dcddfa0SQu Wenruo */ 30558dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 30568dcddfa0SQu Wenruo 3057ad2b2c80SAl Viro return 0; 305839279cc3SChris Mason 3059bcef60f2SArne Jansen fail_qgroup: 3060bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 30617c2ca468SChris Mason fail_trans_kthread: 30627c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 30632ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3064faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 30653f157a2fSChris Mason fail_cleaner: 3066a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 30677c2ca468SChris Mason 30687c2ca468SChris Mason /* 30697c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 30707c2ca468SChris Mason * kthreads 30717c2ca468SChris Mason */ 30727c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 30737c2ca468SChris Mason 30742365dd3cSAnand Jain fail_sysfs: 30756618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 30762365dd3cSAnand Jain 3077b7c35e81SAnand Jain fail_fsdev_sysfs: 3078b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3079b7c35e81SAnand Jain 30801b1d1f66SJosef Bacik fail_block_groups: 308154067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3082af31f5e5SChris Mason 3083af31f5e5SChris Mason fail_tree_roots: 3084af31f5e5SChris Mason free_root_pointers(fs_info, 1); 30852b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3086af31f5e5SChris Mason 308739279cc3SChris Mason fail_sb_buffer: 30887abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 30895cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 309016cdcec7SMiao Xie fail_alloc: 30914543df7eSChris Mason fail_iput: 3092586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3093586e46e2SIlya Dryomov 30944543df7eSChris Mason iput(fs_info->btree_inode); 3095c404e0dcSMiao Xie fail_bio_counter: 3096c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3097963d678bSMiao Xie fail_delalloc_bytes: 3098963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3099e2d84521SMiao Xie fail_dirty_metadata_bytes: 3100e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 310176dda93cSYan, Zheng fail_srcu: 310276dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 31037e662854SQinghuang Feng fail: 310453b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3105586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3106ad2b2c80SAl Viro return err; 3107af31f5e5SChris Mason 3108af31f5e5SChris Mason recovery_tree_root: 31090b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3110af31f5e5SChris Mason goto fail_tree_roots; 3111af31f5e5SChris Mason 3112af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3113af31f5e5SChris Mason 3114af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3115af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3116af31f5e5SChris Mason 3117af31f5e5SChris Mason /* we can't trust the free space cache either */ 3118af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3119af31f5e5SChris Mason 3120af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3121af31f5e5SChris Mason &num_backups_tried, &backup_index); 3122af31f5e5SChris Mason if (ret == -1) 3123af31f5e5SChris Mason goto fail_block_groups; 3124af31f5e5SChris Mason goto retry_root_backup; 3125eb60ceacSChris Mason } 3126eb60ceacSChris Mason 3127f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3128f2984462SChris Mason { 3129f2984462SChris Mason if (uptodate) { 3130f2984462SChris Mason set_buffer_uptodate(bh); 3131f2984462SChris Mason } else { 3132442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3133442a4f63SStefan Behrens bh->b_private; 3134442a4f63SStefan Behrens 3135fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3136b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3137606686eeSJosef Bacik rcu_str_deref(device->name)); 313801327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 31391259ab75SChris Mason * our own ways of dealing with the IO errors 31401259ab75SChris Mason */ 3141f2984462SChris Mason clear_buffer_uptodate(bh); 3142442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3143f2984462SChris Mason } 3144f2984462SChris Mason unlock_buffer(bh); 3145f2984462SChris Mason put_bh(bh); 3146f2984462SChris Mason } 3147f2984462SChris Mason 314829c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 314929c36d72SAnand Jain struct buffer_head **bh_ret) 315029c36d72SAnand Jain { 315129c36d72SAnand Jain struct buffer_head *bh; 315229c36d72SAnand Jain struct btrfs_super_block *super; 315329c36d72SAnand Jain u64 bytenr; 315429c36d72SAnand Jain 315529c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 315629c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 315729c36d72SAnand Jain return -EINVAL; 315829c36d72SAnand Jain 31599f6d2510SDavid Sterba bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE); 316029c36d72SAnand Jain /* 316129c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 316229c36d72SAnand Jain * the best option we have is to mark it EIO. 316329c36d72SAnand Jain */ 316429c36d72SAnand Jain if (!bh) 316529c36d72SAnand Jain return -EIO; 316629c36d72SAnand Jain 316729c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 316829c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 316929c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 317029c36d72SAnand Jain brelse(bh); 317129c36d72SAnand Jain return -EINVAL; 317229c36d72SAnand Jain } 317329c36d72SAnand Jain 317429c36d72SAnand Jain *bh_ret = bh; 317529c36d72SAnand Jain return 0; 317629c36d72SAnand Jain } 317729c36d72SAnand Jain 317829c36d72SAnand Jain 3179a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3180a512bbf8SYan Zheng { 3181a512bbf8SYan Zheng struct buffer_head *bh; 3182a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3183a512bbf8SYan Zheng struct btrfs_super_block *super; 3184a512bbf8SYan Zheng int i; 3185a512bbf8SYan Zheng u64 transid = 0; 318692fc03fbSAnand Jain int ret = -EINVAL; 3187a512bbf8SYan Zheng 3188a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3189a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3190a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3191a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3192a512bbf8SYan Zheng */ 3193a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 319429c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 319529c36d72SAnand Jain if (ret) 3196a512bbf8SYan Zheng continue; 3197a512bbf8SYan Zheng 3198a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3199a512bbf8SYan Zheng 3200a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3201a512bbf8SYan Zheng brelse(latest); 3202a512bbf8SYan Zheng latest = bh; 3203a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3204a512bbf8SYan Zheng } else { 3205a512bbf8SYan Zheng brelse(bh); 3206a512bbf8SYan Zheng } 3207a512bbf8SYan Zheng } 320892fc03fbSAnand Jain 320992fc03fbSAnand Jain if (!latest) 321092fc03fbSAnand Jain return ERR_PTR(ret); 321192fc03fbSAnand Jain 3212a512bbf8SYan Zheng return latest; 3213a512bbf8SYan Zheng } 3214a512bbf8SYan Zheng 32154eedeb75SHisashi Hifumi /* 3216abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the 3217abbb3b8eSDavid Sterba * buffer heads we write are pinned. 32184eedeb75SHisashi Hifumi * 3219abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit 3220abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be 3221abbb3b8eSDavid Sterba * same for write and wait phases. 32224eedeb75SHisashi Hifumi * 3223abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or submission fails. 32244eedeb75SHisashi Hifumi */ 3225a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3226abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors) 3227a512bbf8SYan Zheng { 3228a512bbf8SYan Zheng struct buffer_head *bh; 3229a512bbf8SYan Zheng int i; 3230a512bbf8SYan Zheng int ret; 3231a512bbf8SYan Zheng int errors = 0; 3232a512bbf8SYan Zheng u32 crc; 3233a512bbf8SYan Zheng u64 bytenr; 3234a512bbf8SYan Zheng 3235a512bbf8SYan Zheng if (max_mirrors == 0) 3236a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3237a512bbf8SYan Zheng 3238a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3239a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3240935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3241935e5cc9SMiao Xie device->commit_total_bytes) 3242a512bbf8SYan Zheng break; 3243a512bbf8SYan Zheng 3244a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3245a512bbf8SYan Zheng 3246a512bbf8SYan Zheng crc = ~(u32)0; 3247abbb3b8eSDavid Sterba crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc, 3248abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3249a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3250a512bbf8SYan Zheng 3251abbb3b8eSDavid Sterba /* One reference for us, and we leave it for the caller */ 32529f6d2510SDavid Sterba bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, 3253a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3254634554dcSJosef Bacik if (!bh) { 3255fb456252SJeff Mahoney btrfs_err(device->fs_info, 3256f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3257f14d104dSDavid Sterba bytenr); 3258634554dcSJosef Bacik errors++; 3259634554dcSJosef Bacik continue; 3260634554dcSJosef Bacik } 3261634554dcSJosef Bacik 3262a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3263a512bbf8SYan Zheng 32644eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3265a512bbf8SYan Zheng get_bh(bh); 32664eedeb75SHisashi Hifumi 32674eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3268a512bbf8SYan Zheng lock_buffer(bh); 3269a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3270442a4f63SStefan Behrens bh->b_private = device; 3271a512bbf8SYan Zheng 3272387125fcSChris Mason /* 3273387125fcSChris Mason * we fua the first super. The others we allow 3274387125fcSChris Mason * to go down lazy. 3275387125fcSChris Mason */ 32768d910125SJan Kara if (i == 0) { 32778d910125SJan Kara ret = btrfsic_submit_bh(REQ_OP_WRITE, 3278db95c876SDavid Sterba REQ_SYNC | REQ_FUA | REQ_META | REQ_PRIO, bh); 32798d910125SJan Kara } else { 3280db95c876SDavid Sterba ret = btrfsic_submit_bh(REQ_OP_WRITE, 3281db95c876SDavid Sterba REQ_SYNC | REQ_META | REQ_PRIO, bh); 32828d910125SJan Kara } 32834eedeb75SHisashi Hifumi if (ret) 3284a512bbf8SYan Zheng errors++; 3285a512bbf8SYan Zheng } 3286a512bbf8SYan Zheng return errors < i ? 0 : -1; 3287a512bbf8SYan Zheng } 3288a512bbf8SYan Zheng 3289387125fcSChris Mason /* 3290abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers, 3291abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases. 3292abbb3b8eSDavid Sterba * 3293abbb3b8eSDavid Sterba * Return number of errors when buffer head is not found or not marked up to 3294abbb3b8eSDavid Sterba * date. 3295abbb3b8eSDavid Sterba */ 3296abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) 3297abbb3b8eSDavid Sterba { 3298abbb3b8eSDavid Sterba struct buffer_head *bh; 3299abbb3b8eSDavid Sterba int i; 3300abbb3b8eSDavid Sterba int errors = 0; 3301abbb3b8eSDavid Sterba u64 bytenr; 3302abbb3b8eSDavid Sterba 3303abbb3b8eSDavid Sterba if (max_mirrors == 0) 3304abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3305abbb3b8eSDavid Sterba 3306abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) { 3307abbb3b8eSDavid Sterba bytenr = btrfs_sb_offset(i); 3308abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3309abbb3b8eSDavid Sterba device->commit_total_bytes) 3310abbb3b8eSDavid Sterba break; 3311abbb3b8eSDavid Sterba 33129f6d2510SDavid Sterba bh = __find_get_block(device->bdev, 33139f6d2510SDavid Sterba bytenr / BTRFS_BDEV_BLOCKSIZE, 3314abbb3b8eSDavid Sterba BTRFS_SUPER_INFO_SIZE); 3315abbb3b8eSDavid Sterba if (!bh) { 3316abbb3b8eSDavid Sterba errors++; 3317abbb3b8eSDavid Sterba continue; 3318abbb3b8eSDavid Sterba } 3319abbb3b8eSDavid Sterba wait_on_buffer(bh); 3320abbb3b8eSDavid Sterba if (!buffer_uptodate(bh)) 3321abbb3b8eSDavid Sterba errors++; 3322abbb3b8eSDavid Sterba 3323abbb3b8eSDavid Sterba /* drop our reference */ 3324abbb3b8eSDavid Sterba brelse(bh); 3325abbb3b8eSDavid Sterba 3326abbb3b8eSDavid Sterba /* drop the reference from the writing run */ 3327abbb3b8eSDavid Sterba brelse(bh); 3328abbb3b8eSDavid Sterba } 3329abbb3b8eSDavid Sterba 3330abbb3b8eSDavid Sterba return errors < i ? 0 : -1; 3331abbb3b8eSDavid Sterba } 3332abbb3b8eSDavid Sterba 3333abbb3b8eSDavid Sterba /* 3334387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3335387125fcSChris Mason * for the barrier when it is done 3336387125fcSChris Mason */ 33374246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3338387125fcSChris Mason { 3339387125fcSChris Mason complete(bio->bi_private); 3340387125fcSChris Mason } 3341387125fcSChris Mason 3342387125fcSChris Mason /* 33434fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 33444fc6441aSAnand Jain * done in the waiting counterpart. 3345387125fcSChris Mason */ 33464fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3347387125fcSChris Mason { 3348c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3349e0ae9994SDavid Sterba struct bio *bio = device->flush_bio; 3350387125fcSChris Mason 3351c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 33524fc6441aSAnand Jain return; 3353387125fcSChris Mason 3354e0ae9994SDavid Sterba bio_reset(bio); 3355387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 335674d46992SChristoph Hellwig bio_set_dev(bio, device->bdev); 33578d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3358387125fcSChris Mason init_completion(&device->flush_wait); 3359387125fcSChris Mason bio->bi_private = &device->flush_wait; 3360387125fcSChris Mason 336143a01111SLu Fengqi btrfsic_submit_bio(bio); 3362e0ae9994SDavid Sterba device->flush_bio_sent = 1; 33634fc6441aSAnand Jain } 3364387125fcSChris Mason 33654fc6441aSAnand Jain /* 33664fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 33674fc6441aSAnand Jain */ 33688c27cb35SLinus Torvalds static blk_status_t wait_dev_flush(struct btrfs_device *device) 33694fc6441aSAnand Jain { 33704fc6441aSAnand Jain struct bio *bio = device->flush_bio; 33714fc6441aSAnand Jain 3372e0ae9994SDavid Sterba if (!device->flush_bio_sent) 337358efbc9fSOmar Sandoval return BLK_STS_OK; 33744fc6441aSAnand Jain 3375e0ae9994SDavid Sterba device->flush_bio_sent = 0; 33762980d574SDavid Sterba wait_for_completion_io(&device->flush_wait); 33774fc6441aSAnand Jain 33788c27cb35SLinus Torvalds return bio->bi_status; 3379387125fcSChris Mason } 3380387125fcSChris Mason 3381d10b82feSQu Wenruo static int check_barrier_error(struct btrfs_fs_info *fs_info) 3382401b41e5SAnand Jain { 3383d10b82feSQu Wenruo if (!btrfs_check_rw_degradable(fs_info)) 3384401b41e5SAnand Jain return -EIO; 3385387125fcSChris Mason return 0; 3386387125fcSChris Mason } 3387387125fcSChris Mason 3388387125fcSChris Mason /* 3389387125fcSChris Mason * send an empty flush down to each device in parallel, 3390387125fcSChris Mason * then wait for them 3391387125fcSChris Mason */ 3392387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3393387125fcSChris Mason { 3394387125fcSChris Mason struct list_head *head; 3395387125fcSChris Mason struct btrfs_device *dev; 33965af3e8ccSStefan Behrens int errors_wait = 0; 33974e4cbee9SChristoph Hellwig blk_status_t ret; 3398387125fcSChris Mason 3399387125fcSChris Mason /* send down all the barriers */ 3400387125fcSChris Mason head = &info->fs_devices->devices; 3401387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3402f88ba6a2SHidetoshi Seto if (dev->missing) 3403f88ba6a2SHidetoshi Seto continue; 3404cea7c8bfSAnand Jain if (!dev->bdev) 3405387125fcSChris Mason continue; 3406387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3407387125fcSChris Mason continue; 3408387125fcSChris Mason 34094fc6441aSAnand Jain write_dev_flush(dev); 341058efbc9fSOmar Sandoval dev->last_flush_error = BLK_STS_OK; 3411387125fcSChris Mason } 3412387125fcSChris Mason 3413387125fcSChris Mason /* wait for all the barriers */ 3414387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3415f88ba6a2SHidetoshi Seto if (dev->missing) 3416f88ba6a2SHidetoshi Seto continue; 3417387125fcSChris Mason if (!dev->bdev) { 34185af3e8ccSStefan Behrens errors_wait++; 3419387125fcSChris Mason continue; 3420387125fcSChris Mason } 3421387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3422387125fcSChris Mason continue; 3423387125fcSChris Mason 34244fc6441aSAnand Jain ret = wait_dev_flush(dev); 3425401b41e5SAnand Jain if (ret) { 3426401b41e5SAnand Jain dev->last_flush_error = ret; 342766b4993eSDavid Sterba btrfs_dev_stat_inc_and_print(dev, 342866b4993eSDavid Sterba BTRFS_DEV_STAT_FLUSH_ERRS); 34295af3e8ccSStefan Behrens errors_wait++; 3430387125fcSChris Mason } 3431401b41e5SAnand Jain } 3432401b41e5SAnand Jain 3433cea7c8bfSAnand Jain if (errors_wait) { 3434401b41e5SAnand Jain /* 3435401b41e5SAnand Jain * At some point we need the status of all disks 3436401b41e5SAnand Jain * to arrive at the volume status. So error checking 3437401b41e5SAnand Jain * is being pushed to a separate loop. 3438401b41e5SAnand Jain */ 3439d10b82feSQu Wenruo return check_barrier_error(info); 3440401b41e5SAnand Jain } 3441387125fcSChris Mason return 0; 3442387125fcSChris Mason } 3443387125fcSChris Mason 3444943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3445943c6e99SZhao Lei { 34468789f4feSZhao Lei int raid_type; 34478789f4feSZhao Lei int min_tolerated = INT_MAX; 3448943c6e99SZhao Lei 34498789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 34508789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 34518789f4feSZhao Lei min_tolerated = min(min_tolerated, 34528789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 34538789f4feSZhao Lei tolerated_failures); 3454943c6e99SZhao Lei 34558789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 34568789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 34578789f4feSZhao Lei continue; 34588789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 34598789f4feSZhao Lei continue; 34608789f4feSZhao Lei min_tolerated = min(min_tolerated, 34618789f4feSZhao Lei btrfs_raid_array[raid_type]. 34628789f4feSZhao Lei tolerated_failures); 34638789f4feSZhao Lei } 3464943c6e99SZhao Lei 34658789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3466ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 34678789f4feSZhao Lei min_tolerated = 0; 34688789f4feSZhao Lei } 34698789f4feSZhao Lei 34708789f4feSZhao Lei return min_tolerated; 3471943c6e99SZhao Lei } 3472943c6e99SZhao Lei 3473eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3474f2984462SChris Mason { 3475e5e9a520SChris Mason struct list_head *head; 3476f2984462SChris Mason struct btrfs_device *dev; 3477a061fc8dSChris Mason struct btrfs_super_block *sb; 3478f2984462SChris Mason struct btrfs_dev_item *dev_item; 3479f2984462SChris Mason int ret; 3480f2984462SChris Mason int do_barriers; 3481a236aed1SChris Mason int max_errors; 3482a236aed1SChris Mason int total_errors = 0; 3483a061fc8dSChris Mason u64 flags; 3484f2984462SChris Mason 34850b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 3486fed3b381SLiu Bo 3487fed3b381SLiu Bo /* 3488fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction, 3489fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not 3490fed3b381SLiu Bo * been consistent on disk. 3491fed3b381SLiu Bo */ 3492fed3b381SLiu Bo if (max_mirrors == 0) 34930b246afaSJeff Mahoney backup_super_roots(fs_info); 3494f2984462SChris Mason 34950b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3496a061fc8dSChris Mason dev_item = &sb->dev_item; 3497e5e9a520SChris Mason 34980b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 34990b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 35000b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3501387125fcSChris Mason 35025af3e8ccSStefan Behrens if (do_barriers) { 35030b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 35045af3e8ccSStefan Behrens if (ret) { 35055af3e8ccSStefan Behrens mutex_unlock( 35060b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 35070b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 35085af3e8ccSStefan Behrens "errors while submitting device barriers."); 35095af3e8ccSStefan Behrens return ret; 35105af3e8ccSStefan Behrens } 35115af3e8ccSStefan Behrens } 3512387125fcSChris Mason 35131f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3514dfe25020SChris Mason if (!dev->bdev) { 3515dfe25020SChris Mason total_errors++; 3516dfe25020SChris Mason continue; 3517dfe25020SChris Mason } 35182b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3519dfe25020SChris Mason continue; 3520dfe25020SChris Mason 35212b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3522a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3523a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 35247df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3525935e5cc9SMiao Xie dev->commit_total_bytes); 3526ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3527ce7213c7SMiao Xie dev->commit_bytes_used); 3528a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3529a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3530a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3531a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 353244880fdcSAnand Jain memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE); 3533a512bbf8SYan Zheng 3534a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3535a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3536f2984462SChris Mason 3537abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors); 3538a236aed1SChris Mason if (ret) 3539a236aed1SChris Mason total_errors++; 3540f2984462SChris Mason } 3541a236aed1SChris Mason if (total_errors > max_errors) { 35420b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3543d397712bSChris Mason total_errors); 35440b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 354579787eaaSJeff Mahoney 35469d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 35470b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 35480b246afaSJeff Mahoney "%d errors while writing supers", 35490b246afaSJeff Mahoney total_errors); 35509d565ba4SStefan Behrens return -EIO; 3551a236aed1SChris Mason } 3552f2984462SChris Mason 3553a512bbf8SYan Zheng total_errors = 0; 35541f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3555dfe25020SChris Mason if (!dev->bdev) 3556dfe25020SChris Mason continue; 35572b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3558dfe25020SChris Mason continue; 3559dfe25020SChris Mason 3560abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors); 3561a512bbf8SYan Zheng if (ret) 35621259ab75SChris Mason total_errors++; 3563f2984462SChris Mason } 35640b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3565a236aed1SChris Mason if (total_errors > max_errors) { 35660b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 35670b246afaSJeff Mahoney "%d errors while writing supers", 35680b246afaSJeff Mahoney total_errors); 356979787eaaSJeff Mahoney return -EIO; 3570a236aed1SChris Mason } 3571f2984462SChris Mason return 0; 3572f2984462SChris Mason } 3573f2984462SChris Mason 3574cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3575cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3576cb517eabSMiao Xie struct btrfs_root *root) 35772619ba1fSChris Mason { 35784df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 35792619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 35802619ba1fSChris Mason (unsigned long)root->root_key.objectid); 35814df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 358276dda93cSYan, Zheng 358376dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 358476dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 358576dda93cSYan, Zheng 35861c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 35873321719eSLiu Bo btrfs_free_log(NULL, root); 35881c1ea4f7SLiu Bo if (root->reloc_root) { 35891c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 35901c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 35911c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 35921c1ea4f7SLiu Bo root->reloc_root = NULL; 35931c1ea4f7SLiu Bo } 35941c1ea4f7SLiu Bo } 35953321719eSLiu Bo 3596faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3597581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3598faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3599581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 36004df27c4dSYan, Zheng free_fs_root(root); 36014df27c4dSYan, Zheng } 36024df27c4dSYan, Zheng 36034df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 36044df27c4dSYan, Zheng { 360557cdc8dbSDavid Sterba iput(root->ino_cache_inode); 36064df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 36072ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 36081cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 36090ee5dc67SAl Viro if (root->anon_dev) 36100ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 36118257b2dcSMiao Xie if (root->subv_writers) 36128257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 36135f39d397SChris Mason free_extent_buffer(root->node); 36145f39d397SChris Mason free_extent_buffer(root->commit_root); 3615581bb050SLi Zefan kfree(root->free_ino_ctl); 3616581bb050SLi Zefan kfree(root->free_ino_pinned); 361758176a96SJosef Bacik kfree(root->name); 3618b0feb9d9SMiao Xie btrfs_put_fs_root(root); 36192619ba1fSChris Mason } 36202619ba1fSChris Mason 3621cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3622cb517eabSMiao Xie { 3623cb517eabSMiao Xie free_fs_root(root); 3624cfaa7295SChris Mason } 3625cfaa7295SChris Mason 3626c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3627c146afadSYan Zheng { 3628c146afadSYan Zheng u64 root_objectid = 0; 3629c146afadSYan Zheng struct btrfs_root *gang[8]; 363065d33fd7SQu Wenruo int i = 0; 363165d33fd7SQu Wenruo int err = 0; 363265d33fd7SQu Wenruo unsigned int ret = 0; 363365d33fd7SQu Wenruo int index; 3634c146afadSYan Zheng 3635c146afadSYan Zheng while (1) { 363665d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3637c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3638c146afadSYan Zheng (void **)gang, root_objectid, 3639c146afadSYan Zheng ARRAY_SIZE(gang)); 364065d33fd7SQu Wenruo if (!ret) { 364165d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3642c146afadSYan Zheng break; 364365d33fd7SQu Wenruo } 36445d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 364566b4ffd1SJosef Bacik 364665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 364765d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 364865d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 364965d33fd7SQu Wenruo gang[i] = NULL; 365065d33fd7SQu Wenruo continue; 365165d33fd7SQu Wenruo } 365265d33fd7SQu Wenruo /* grab all the search result for later use */ 365365d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 365465d33fd7SQu Wenruo } 365565d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 365665d33fd7SQu Wenruo 365765d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 365865d33fd7SQu Wenruo if (!gang[i]) 365965d33fd7SQu Wenruo continue; 3660c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 366166b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 366266b4ffd1SJosef Bacik if (err) 366365d33fd7SQu Wenruo break; 366465d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3665c146afadSYan Zheng } 3666c146afadSYan Zheng root_objectid++; 3667c146afadSYan Zheng } 366865d33fd7SQu Wenruo 366965d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 367065d33fd7SQu Wenruo for (; i < ret; i++) { 367165d33fd7SQu Wenruo if (gang[i]) 367265d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 367365d33fd7SQu Wenruo } 367465d33fd7SQu Wenruo return err; 3675c146afadSYan Zheng } 3676c146afadSYan Zheng 36776bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3678c146afadSYan Zheng { 36796bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3680c146afadSYan Zheng struct btrfs_trans_handle *trans; 3681c146afadSYan Zheng 36820b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 36832ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 36840b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 36850b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3686c71bf099SYan, Zheng 3687c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 36880b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 36890b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3690c71bf099SYan, Zheng 36917a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 36923612b495STsutomu Itoh if (IS_ERR(trans)) 36933612b495STsutomu Itoh return PTR_ERR(trans); 36943a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3695c146afadSYan Zheng } 3696c146afadSYan Zheng 36976bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3698eb60ceacSChris Mason { 36996bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3700c146afadSYan Zheng int ret; 3701e089f05cSChris Mason 3702afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3703a2135011SChris Mason 37047343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3705d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 37067343dd61SJustin Maggard 3707803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3708803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3709803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3710803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3711803b2f54SStefan Behrens 3712837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3713aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3714837d5b6eSIlya Dryomov 37158dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 37168dabb742SStefan Behrens 3717aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 37184cb5300bSChris Mason 37194cb5300bSChris Mason /* wait for any defraggers to finish */ 37204cb5300bSChris Mason wait_event(fs_info->transaction_wait, 37214cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 37224cb5300bSChris Mason 37234cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 372426176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 37254cb5300bSChris Mason 372621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 372721c7e756SMiao Xie 3728bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) { 3729e44163e1SJeff Mahoney /* 3730e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3731e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3732e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3733e44163e1SJeff Mahoney */ 37340b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3735e44163e1SJeff Mahoney 37366bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3737d397712bSChris Mason if (ret) 373804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3739c146afadSYan Zheng } 3740ed2ff2cbSChris Mason 374187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 37422ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3743acce952bSliubo 3744e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3745e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 37468929ecfaSYan, Zheng 3747afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3748f25784b3SYan Zheng 374904892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3750bcef60f2SArne Jansen 3751963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 375204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3753963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3754b0c68f8bSChris Mason } 375531153d81SYan Zheng 37566618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3757b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 37585ac1d209SJeff Mahoney 3759faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3760d10c5f31SChris Mason 37611a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 37621a4319ccSLiu Bo 3763de348ee0SWang Shilong /* 3764de348ee0SWang Shilong * we must make sure there is not any read request to 3765de348ee0SWang Shilong * submit after we stopping all workers. 3766de348ee0SWang Shilong */ 3767de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 376896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 376996192499SJosef Bacik 37705cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 37715cdd7db6SFilipe Manana 3772afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 377313e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 37749ad6b7bcSChris Mason 377513e6c37bSJosef Bacik iput(fs_info->btree_inode); 3776d6bfde87SChris Mason 377721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 37780b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 37792ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 378021adbd5cSStefan Behrens #endif 378121adbd5cSStefan Behrens 3782dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 37830b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3784b248a415SChris Mason 3785e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3786963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3787c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 378876dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 37890b86a832SChris Mason 379053b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3791fd708b81SJosef Bacik btrfs_free_ref_cache(fs_info); 379253b381b3SDavid Woodhouse 3793cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 37941cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 379504216820SFilipe Manana 379604216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 379704216820SFilipe Manana struct extent_map *em; 379804216820SFilipe Manana 379904216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 380004216820SFilipe Manana struct extent_map, list); 380104216820SFilipe Manana list_del_init(&em->list); 380204216820SFilipe Manana free_extent_map(em); 380304216820SFilipe Manana } 3804eb60ceacSChris Mason } 3805eb60ceacSChris Mason 3806b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3807b9fab919SChris Mason int atomic) 3808ccd467d6SChris Mason { 38091259ab75SChris Mason int ret; 3810727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 38111259ab75SChris Mason 38120b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 38131259ab75SChris Mason if (!ret) 38141259ab75SChris Mason return ret; 38151259ab75SChris Mason 38161259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3817b9fab919SChris Mason parent_transid, atomic); 3818b9fab919SChris Mason if (ret == -EAGAIN) 3819b9fab919SChris Mason return ret; 38201259ab75SChris Mason return !ret; 38215f39d397SChris Mason } 38226702ed49SChris Mason 38235f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 38245f39d397SChris Mason { 38250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 382606ea65a3SJosef Bacik struct btrfs_root *root; 38275f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3828b9473439SChris Mason int was_dirty; 3829b4ce94deSChris Mason 383006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 383106ea65a3SJosef Bacik /* 383206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 383306ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 383406ea65a3SJosef Bacik * outside of the sanity tests. 383506ea65a3SJosef Bacik */ 383606ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 383706ea65a3SJosef Bacik return; 383806ea65a3SJosef Bacik #endif 383906ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 38400b246afaSJeff Mahoney fs_info = root->fs_info; 3841b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 38420b246afaSJeff Mahoney if (transid != fs_info->generation) 38435d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 38440b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 38450b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3846e2d84521SMiao Xie if (!was_dirty) 3847104b4e51SNikolay Borisov percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, 3848e2d84521SMiao Xie buf->len, 38490b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 38501f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 3851557ea5ddSQu Wenruo if (btrfs_header_level(buf) == 0 && btrfs_check_leaf(root, buf)) { 3852a4f78750SDavid Sterba btrfs_print_leaf(buf); 38531f21ef0aSFilipe Manana ASSERT(0); 38541f21ef0aSFilipe Manana } 38551f21ef0aSFilipe Manana #endif 3856eb60ceacSChris Mason } 3857eb60ceacSChris Mason 38582ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 3859b53d3f5dSLiu Bo int flush_delayed) 386035b7e476SChris Mason { 3861188de649SChris Mason /* 3862188de649SChris Mason * looks as though older kernels can get into trouble with 3863188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3864188de649SChris Mason */ 3865e2d84521SMiao Xie int ret; 3866d6bfde87SChris Mason 38676933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3868d6bfde87SChris Mason return; 3869d6bfde87SChris Mason 3870b53d3f5dSLiu Bo if (flush_delayed) 38712ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 387216cdcec7SMiao Xie 38730b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 3874e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3875e2d84521SMiao Xie if (ret > 0) { 38760b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 387716cdcec7SMiao Xie } 387816cdcec7SMiao Xie } 387916cdcec7SMiao Xie 38802ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 388116cdcec7SMiao Xie { 38822ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 388335b7e476SChris Mason } 3884b53d3f5dSLiu Bo 38852ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 3886b53d3f5dSLiu Bo { 38872ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 3888d6bfde87SChris Mason } 38896b80053dSChris Mason 3890ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 38916b80053dSChris Mason { 3892727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 38932ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 38942ff7e61eSJeff Mahoney 38952ff7e61eSJeff Mahoney return btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 38966b80053dSChris Mason } 38970da5468fSChris Mason 38983d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info) 3899acce952bSliubo { 3900c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 3901319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 3902319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 3903c926093eSDavid Sterba int ret = 0; 3904c926093eSDavid Sterba 3905319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 3906ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 3907319e4d06SQu Wenruo ret = -EINVAL; 3908319e4d06SQu Wenruo } 3909319e4d06SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) 3910ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "unrecognized super flag: %llu", 3911319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 391221e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 3913ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 391421e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 3915c926093eSDavid Sterba ret = -EINVAL; 3916c926093eSDavid Sterba } 391721e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 3918ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 391921e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 3920c926093eSDavid Sterba ret = -EINVAL; 3921c926093eSDavid Sterba } 392221e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 3923ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 392421e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 3925c926093eSDavid Sterba ret = -EINVAL; 3926c926093eSDavid Sterba } 3927c926093eSDavid Sterba 39281104a885SDavid Sterba /* 3929319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 3930319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 39311104a885SDavid Sterba */ 3932319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 3933319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 3934ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 393575d6ad38SDavid Sterba ret = -EINVAL; 393675d6ad38SDavid Sterba } 3937319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 393809cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 3939ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 3940ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 394109cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 3942319e4d06SQu Wenruo ret = -EINVAL; 3943319e4d06SQu Wenruo } 3944319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 3945319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 3946ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 3947319e4d06SQu Wenruo ret = -EINVAL; 3948319e4d06SQu Wenruo } 3949319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 3950ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 3951ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 3952319e4d06SQu Wenruo ret = -EINVAL; 3953319e4d06SQu Wenruo } 3954319e4d06SQu Wenruo 3955319e4d06SQu Wenruo /* Root alignment check */ 3956319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 3957ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 3958319e4d06SQu Wenruo btrfs_super_root(sb)); 3959319e4d06SQu Wenruo ret = -EINVAL; 3960319e4d06SQu Wenruo } 3961319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 3962ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 3963319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 3964319e4d06SQu Wenruo ret = -EINVAL; 3965319e4d06SQu Wenruo } 3966319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 3967ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 3968319e4d06SQu Wenruo btrfs_super_log_root(sb)); 396975d6ad38SDavid Sterba ret = -EINVAL; 397075d6ad38SDavid Sterba } 397175d6ad38SDavid Sterba 397244880fdcSAnand Jain if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) { 3973ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 3974ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 3975c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 3976c926093eSDavid Sterba ret = -EINVAL; 3977c926093eSDavid Sterba } 3978c926093eSDavid Sterba 3979c926093eSDavid Sterba /* 3980c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 3981c926093eSDavid Sterba * done later 3982c926093eSDavid Sterba */ 398399e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 398499e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 398599e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 398699e3ecfcSLiu Bo ret = -EINVAL; 398799e3ecfcSLiu Bo } 3988b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 398999e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 399099e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 399199e3ecfcSLiu Bo ret = -EINVAL; 399299e3ecfcSLiu Bo } 399321e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 3994ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 399521e7626bSDavid Sterba btrfs_super_num_devices(sb)); 399675d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 3997ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 399875d6ad38SDavid Sterba ret = -EINVAL; 399975d6ad38SDavid Sterba } 4000c926093eSDavid Sterba 400121e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4002ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 400321e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4004c926093eSDavid Sterba ret = -EINVAL; 4005c926093eSDavid Sterba } 4006c926093eSDavid Sterba 4007c926093eSDavid Sterba /* 4008ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4009ce7fca5fSDavid Sterba * and one chunk 4010ce7fca5fSDavid Sterba */ 4011ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4012ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4013ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4014ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4015ce7fca5fSDavid Sterba ret = -EINVAL; 4016ce7fca5fSDavid Sterba } 4017ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4018ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4019ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4020ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4021ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4022ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4023ce7fca5fSDavid Sterba ret = -EINVAL; 4024ce7fca5fSDavid Sterba } 4025ce7fca5fSDavid Sterba 4026ce7fca5fSDavid Sterba /* 4027c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4028c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4029c926093eSDavid Sterba */ 403021e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4031ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4032ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4033ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4034ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 403521e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 403621e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4037ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4038ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4039ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4040ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4041c926093eSDavid Sterba 4042c926093eSDavid Sterba return ret; 4043fcd1f065SDavid Sterba } 4044fcd1f065SDavid Sterba 40452ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4046acce952bSliubo { 40470b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 40482ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 40490b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4050acce952bSliubo 40510b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 40520b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4053acce952bSliubo 4054acce952bSliubo /* cleanup FS via transaction */ 40552ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4056acce952bSliubo } 4057acce952bSliubo 4058143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4059acce952bSliubo { 4060acce952bSliubo struct btrfs_ordered_extent *ordered; 4061acce952bSliubo 4062199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4063779880efSJosef Bacik /* 4064779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4065779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4066779880efSJosef Bacik */ 4067199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4068779880efSJosef Bacik root_extent_list) 4069779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4070199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4071199c2a9cSMiao Xie } 4072199c2a9cSMiao Xie 4073199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4074199c2a9cSMiao Xie { 4075199c2a9cSMiao Xie struct btrfs_root *root; 4076199c2a9cSMiao Xie struct list_head splice; 4077199c2a9cSMiao Xie 4078199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4079199c2a9cSMiao Xie 4080199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4081199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4082199c2a9cSMiao Xie while (!list_empty(&splice)) { 4083199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4084199c2a9cSMiao Xie ordered_root); 40851de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 40861de2cfdeSJosef Bacik &fs_info->ordered_roots); 4087199c2a9cSMiao Xie 40882a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4089199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4090199c2a9cSMiao Xie 40912a85d9caSLiu Bo cond_resched(); 40922a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4093199c2a9cSMiao Xie } 4094199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4095acce952bSliubo } 4096acce952bSliubo 409735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 40982ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4099acce952bSliubo { 4100acce952bSliubo struct rb_node *node; 4101acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4102acce952bSliubo struct btrfs_delayed_ref_node *ref; 4103acce952bSliubo int ret = 0; 4104acce952bSliubo 4105acce952bSliubo delayed_refs = &trans->delayed_refs; 4106acce952bSliubo 4107acce952bSliubo spin_lock(&delayed_refs->lock); 4108d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4109cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 41100b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4111acce952bSliubo return ret; 4112acce952bSliubo } 4113acce952bSliubo 4114d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4115d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 4116c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *tmp; 4117e78417d1SJosef Bacik bool pin_bytes = false; 4118acce952bSliubo 4119d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4120d7df2c79SJosef Bacik href_node); 4121b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 4122d278850eSJosef Bacik refcount_inc(&head->refs); 412379787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4124b939d1abSJosef Bacik 4125acce952bSliubo mutex_lock(&head->mutex); 4126b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4127d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4128e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4129b939d1abSJosef Bacik continue; 4130b939d1abSJosef Bacik } 4131d7df2c79SJosef Bacik spin_lock(&head->lock); 4132c6fc2454SQu Wenruo list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list, 4133c6fc2454SQu Wenruo list) { 4134d7df2c79SJosef Bacik ref->in_tree = 0; 4135c6fc2454SQu Wenruo list_del(&ref->list); 41361d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 41371d57ee94SWang Xiaoguang list_del(&ref->add_list); 4138d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4139d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4140d7df2c79SJosef Bacik } 414154067ae9SJosef Bacik if (head->must_insert_reserved) 4142e78417d1SJosef Bacik pin_bytes = true; 414378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4144acce952bSliubo delayed_refs->num_heads--; 4145d7df2c79SJosef Bacik if (head->processing == 0) 4146acce952bSliubo delayed_refs->num_heads_ready--; 4147d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4148c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4149d278850eSJosef Bacik RB_CLEAR_NODE(&head->href_node); 4150d7df2c79SJosef Bacik spin_unlock(&head->lock); 4151acce952bSliubo spin_unlock(&delayed_refs->lock); 4152e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4153acce952bSliubo 4154d7df2c79SJosef Bacik if (pin_bytes) 4155d278850eSJosef Bacik btrfs_pin_extent(fs_info, head->bytenr, 4156d278850eSJosef Bacik head->num_bytes, 1); 4157d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 4158acce952bSliubo cond_resched(); 4159acce952bSliubo spin_lock(&delayed_refs->lock); 4160acce952bSliubo } 4161acce952bSliubo 4162acce952bSliubo spin_unlock(&delayed_refs->lock); 4163acce952bSliubo 4164acce952bSliubo return ret; 4165acce952bSliubo } 4166acce952bSliubo 4167143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4168acce952bSliubo { 4169acce952bSliubo struct btrfs_inode *btrfs_inode; 4170acce952bSliubo struct list_head splice; 4171acce952bSliubo 4172acce952bSliubo INIT_LIST_HEAD(&splice); 4173acce952bSliubo 4174eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4175eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4176acce952bSliubo 4177acce952bSliubo while (!list_empty(&splice)) { 4178eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4179acce952bSliubo delalloc_inodes); 4180acce952bSliubo 4181acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4182df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4183df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4184eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4185acce952bSliubo 4186acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4187b216cbfbSMiao Xie 4188eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4189acce952bSliubo } 4190acce952bSliubo 4191eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4192eb73c1b7SMiao Xie } 4193eb73c1b7SMiao Xie 4194eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4195eb73c1b7SMiao Xie { 4196eb73c1b7SMiao Xie struct btrfs_root *root; 4197eb73c1b7SMiao Xie struct list_head splice; 4198eb73c1b7SMiao Xie 4199eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4200eb73c1b7SMiao Xie 4201eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4202eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4203eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4204eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4205eb73c1b7SMiao Xie delalloc_root); 4206eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4207eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4208eb73c1b7SMiao Xie BUG_ON(!root); 4209eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4210eb73c1b7SMiao Xie 4211eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4212eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4213eb73c1b7SMiao Xie 4214eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4215eb73c1b7SMiao Xie } 4216eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4217acce952bSliubo } 4218acce952bSliubo 42192ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4220acce952bSliubo struct extent_io_tree *dirty_pages, 4221acce952bSliubo int mark) 4222acce952bSliubo { 4223acce952bSliubo int ret; 4224acce952bSliubo struct extent_buffer *eb; 4225acce952bSliubo u64 start = 0; 4226acce952bSliubo u64 end; 4227acce952bSliubo 4228acce952bSliubo while (1) { 4229acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4230e6138876SJosef Bacik mark, NULL); 4231acce952bSliubo if (ret) 4232acce952bSliubo break; 4233acce952bSliubo 423491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4235acce952bSliubo while (start <= end) { 42360b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 42370b246afaSJeff Mahoney start += fs_info->nodesize; 4238fd8b2b61SJosef Bacik if (!eb) 4239acce952bSliubo continue; 4240fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4241acce952bSliubo 4242fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4243fd8b2b61SJosef Bacik &eb->bflags)) 4244fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4245fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4246acce952bSliubo } 4247acce952bSliubo } 4248acce952bSliubo 4249acce952bSliubo return ret; 4250acce952bSliubo } 4251acce952bSliubo 42522ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4253acce952bSliubo struct extent_io_tree *pinned_extents) 4254acce952bSliubo { 4255acce952bSliubo struct extent_io_tree *unpin; 4256acce952bSliubo u64 start; 4257acce952bSliubo u64 end; 4258acce952bSliubo int ret; 4259ed0eaa14SLiu Bo bool loop = true; 4260acce952bSliubo 4261acce952bSliubo unpin = pinned_extents; 4262ed0eaa14SLiu Bo again: 4263acce952bSliubo while (1) { 4264acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4265e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4266acce952bSliubo if (ret) 4267acce952bSliubo break; 4268acce952bSliubo 4269af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 42702ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4271acce952bSliubo cond_resched(); 4272acce952bSliubo } 4273acce952bSliubo 4274ed0eaa14SLiu Bo if (loop) { 42750b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 42760b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4277ed0eaa14SLiu Bo else 42780b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4279ed0eaa14SLiu Bo loop = false; 4280ed0eaa14SLiu Bo goto again; 4281ed0eaa14SLiu Bo } 4282ed0eaa14SLiu Bo 4283acce952bSliubo return 0; 4284acce952bSliubo } 4285acce952bSliubo 4286c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4287c79a1751SLiu Bo { 4288c79a1751SLiu Bo struct inode *inode; 4289c79a1751SLiu Bo 4290c79a1751SLiu Bo inode = cache->io_ctl.inode; 4291c79a1751SLiu Bo if (inode) { 4292c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4293c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4294c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4295c79a1751SLiu Bo iput(inode); 4296c79a1751SLiu Bo } 4297c79a1751SLiu Bo btrfs_put_block_group(cache); 4298c79a1751SLiu Bo } 4299c79a1751SLiu Bo 4300c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 43012ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4302c79a1751SLiu Bo { 4303c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4304c79a1751SLiu Bo 4305c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4306c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4307c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4308c79a1751SLiu Bo struct btrfs_block_group_cache, 4309c79a1751SLiu Bo dirty_list); 4310c79a1751SLiu Bo if (!cache) { 43110b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group dirty_bgs list"); 4312c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4313c79a1751SLiu Bo return; 4314c79a1751SLiu Bo } 4315c79a1751SLiu Bo 4316c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4317c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4318c79a1751SLiu Bo list_del_init(&cache->io_list); 4319c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4320c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4321c79a1751SLiu Bo } 4322c79a1751SLiu Bo 4323c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4324c79a1751SLiu Bo spin_lock(&cache->lock); 4325c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4326c79a1751SLiu Bo spin_unlock(&cache->lock); 4327c79a1751SLiu Bo 4328c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4329c79a1751SLiu Bo btrfs_put_block_group(cache); 4330c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4331c79a1751SLiu Bo } 4332c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4333c79a1751SLiu Bo 4334c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4335c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4336c79a1751SLiu Bo struct btrfs_block_group_cache, 4337c79a1751SLiu Bo io_list); 4338c79a1751SLiu Bo if (!cache) { 43390b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group on io_bgs list"); 4340c79a1751SLiu Bo return; 4341c79a1751SLiu Bo } 4342c79a1751SLiu Bo 4343c79a1751SLiu Bo list_del_init(&cache->io_list); 4344c79a1751SLiu Bo spin_lock(&cache->lock); 4345c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4346c79a1751SLiu Bo spin_unlock(&cache->lock); 4347c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4348c79a1751SLiu Bo } 4349c79a1751SLiu Bo } 4350c79a1751SLiu Bo 435149b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 43522ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 435349b25e05SJeff Mahoney { 43542ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4355c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4356c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4357c79a1751SLiu Bo 43582ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 435949b25e05SJeff Mahoney 43604a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 43610b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 436249b25e05SJeff Mahoney 43634a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 43640b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 436549b25e05SJeff Mahoney 4366ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4367ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 436867cde344SMiao Xie 43692ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 437049b25e05SJeff Mahoney EXTENT_DIRTY); 43712ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 43720b246afaSJeff Mahoney fs_info->pinned_extents); 437349b25e05SJeff Mahoney 43744a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 43754a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 437649b25e05SJeff Mahoney } 437749b25e05SJeff Mahoney 43782ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4379acce952bSliubo { 4380acce952bSliubo struct btrfs_transaction *t; 4381acce952bSliubo 43820b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4383acce952bSliubo 43840b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 43850b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 43860b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4387724e2315SJosef Bacik struct btrfs_transaction, list); 4388724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 43899b64f57dSElena Reshetova refcount_inc(&t->use_count); 43900b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 43912ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4392724e2315SJosef Bacik btrfs_put_transaction(t); 43930b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4394724e2315SJosef Bacik continue; 4395724e2315SJosef Bacik } 43960b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4397724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 43980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4399724e2315SJosef Bacik /* 4400724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4401724e2315SJosef Bacik * handle open currently for this transaction. 4402724e2315SJosef Bacik */ 4403724e2315SJosef Bacik wait_event(t->writer_wait, 4404724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4405724e2315SJosef Bacik } else { 44060b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4407724e2315SJosef Bacik } 44082ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4409724e2315SJosef Bacik 44100b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 44110b246afaSJeff Mahoney if (t == fs_info->running_transaction) 44120b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4413724e2315SJosef Bacik list_del_init(&t->list); 44140b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4415a4abeea4SJosef Bacik 4416724e2315SJosef Bacik btrfs_put_transaction(t); 44172ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 44180b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4419724e2315SJosef Bacik } 44200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 44210b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4422ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4423ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 44242ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 44250b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 44260b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4427acce952bSliubo 4428acce952bSliubo return 0; 4429acce952bSliubo } 4430acce952bSliubo 4431c6100a4bSJosef Bacik static struct btrfs_fs_info *btree_fs_info(void *private_data) 4432c6100a4bSJosef Bacik { 4433c6100a4bSJosef Bacik struct inode *inode = private_data; 4434c6100a4bSJosef Bacik return btrfs_sb(inode->i_sb); 4435c6100a4bSJosef Bacik } 4436c6100a4bSJosef Bacik 4437e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 44384d53dddbSDavid Sterba /* mandatory callbacks */ 44390b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 44404d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 4441239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4442239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 444320a7db8aSDavid Sterba .readpage_io_failed_hook = btree_io_failed_hook, 4444c6100a4bSJosef Bacik .set_range_writeback = btrfs_set_range_writeback, 4445c6100a4bSJosef Bacik .tree_fs_info = btree_fs_info, 44464d53dddbSDavid Sterba 44474d53dddbSDavid Sterba /* optional callbacks */ 44480da5468fSChris Mason }; 4449