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" 53eb60ceacSChris Mason 54de0022b9SJosef Bacik #ifdef CONFIG_X86 55de0022b9SJosef Bacik #include <asm/cpufeature.h> 56de0022b9SJosef Bacik #endif 57de0022b9SJosef Bacik 58319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ 59319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\ 60319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\ 61319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\ 62319e4d06SQu Wenruo BTRFS_SUPER_FLAG_METADUMP) 63319e4d06SQu Wenruo 64e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops; 658b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 664df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 673d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info); 68143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 69acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 702ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 71143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 722ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 73acce952bSliubo struct extent_io_tree *dirty_pages, 74acce952bSliubo int mark); 752ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 76acce952bSliubo struct extent_io_tree *pinned_extents); 772ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 782ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 79ce9adaa5SChris Mason 80d352ac68SChris Mason /* 8197eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 8297eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 83d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 84d352ac68SChris Mason */ 8597eb6b69SDavid Sterba struct btrfs_end_io_wq { 86ce9adaa5SChris Mason struct bio *bio; 87ce9adaa5SChris Mason bio_end_io_t *end_io; 88ce9adaa5SChris Mason void *private; 89ce9adaa5SChris Mason struct btrfs_fs_info *info; 90ce9adaa5SChris Mason int error; 91bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 928b712842SChris Mason struct btrfs_work work; 93ce9adaa5SChris Mason }; 940da5468fSChris Mason 9597eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 9697eb6b69SDavid Sterba 9797eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 9897eb6b69SDavid Sterba { 9997eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 10097eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 10197eb6b69SDavid Sterba 0, 102fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 10397eb6b69SDavid Sterba NULL); 10497eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 10597eb6b69SDavid Sterba return -ENOMEM; 10697eb6b69SDavid Sterba return 0; 10797eb6b69SDavid Sterba } 10897eb6b69SDavid Sterba 10997eb6b69SDavid Sterba void btrfs_end_io_wq_exit(void) 11097eb6b69SDavid Sterba { 11197eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 11297eb6b69SDavid Sterba } 11397eb6b69SDavid Sterba 114d352ac68SChris Mason /* 115d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 116d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 117d352ac68SChris Mason * just before they are sent down the IO stack. 118d352ac68SChris Mason */ 11944b8bd7eSChris Mason struct async_submit_bio { 120c6100a4bSJosef Bacik void *private_data; 121c6100a4bSJosef Bacik struct btrfs_fs_info *fs_info; 12244b8bd7eSChris Mason struct bio *bio; 1234a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1244a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 12544b8bd7eSChris Mason int mirror_num; 126c8b97818SChris Mason unsigned long bio_flags; 127eaf25d93SChris Mason /* 128eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 129eaf25d93SChris Mason * can't tell us where in the file the bio should go 130eaf25d93SChris Mason */ 131eaf25d93SChris Mason u64 bio_offset; 1328b712842SChris Mason struct btrfs_work work; 13379787eaaSJeff Mahoney int error; 13444b8bd7eSChris Mason }; 13544b8bd7eSChris Mason 13685d4e461SChris Mason /* 13785d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 13885d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 13985d4e461SChris Mason * the level the eb occupies in the tree. 1404008c04aSChris Mason * 14185d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 14285d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 14385d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 14485d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 14585d4e461SChris Mason * have separate keysets. 1464008c04aSChris Mason * 14785d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 14885d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 14985d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1504008c04aSChris Mason * 15185d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 15285d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 15385d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 15485d4e461SChris Mason * 15585d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 15685d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 15785d4e461SChris Mason * needs update as well. 1584008c04aSChris Mason */ 1594008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1604008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1614008c04aSChris Mason # error 1624008c04aSChris Mason # endif 16385d4e461SChris Mason 16485d4e461SChris Mason static struct btrfs_lockdep_keyset { 16585d4e461SChris Mason u64 id; /* root objectid */ 16685d4e461SChris Mason const char *name_stem; /* lock name stem */ 16785d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 16885d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 16985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 17085d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 17185d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 17285d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 17385d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 17485d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 17585d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 17660b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 17785d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 17885d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 17985d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 18013fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1816b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 18285d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1834008c04aSChris Mason }; 18485d4e461SChris Mason 18585d4e461SChris Mason void __init btrfs_init_lockdep(void) 18685d4e461SChris Mason { 18785d4e461SChris Mason int i, j; 18885d4e461SChris Mason 18985d4e461SChris Mason /* initialize lockdep class names */ 19085d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 19185d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 19285d4e461SChris Mason 19385d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 19485d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 19585d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 19685d4e461SChris Mason } 19785d4e461SChris Mason } 19885d4e461SChris Mason 19985d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 20085d4e461SChris Mason int level) 20185d4e461SChris Mason { 20285d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 20385d4e461SChris Mason 20485d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 20585d4e461SChris Mason 20685d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 20785d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 20885d4e461SChris Mason if (ks->id == objectid) 20985d4e461SChris Mason break; 21085d4e461SChris Mason 21185d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 21285d4e461SChris Mason &ks->keys[level], ks->names[level]); 21385d4e461SChris Mason } 21485d4e461SChris Mason 2154008c04aSChris Mason #endif 2164008c04aSChris Mason 217d352ac68SChris Mason /* 218d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 219d352ac68SChris Mason * that covers the entire device 220d352ac68SChris Mason */ 221fc4f21b1SNikolay Borisov static struct extent_map *btree_get_extent(struct btrfs_inode *inode, 222306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2235f39d397SChris Mason int create) 2245f39d397SChris Mason { 225fc4f21b1SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 226fc4f21b1SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 2275f39d397SChris Mason struct extent_map *em; 2285f39d397SChris Mason int ret; 2295f39d397SChris Mason 230890871beSChris Mason read_lock(&em_tree->lock); 231d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 232a061fc8dSChris Mason if (em) { 2330b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 234890871beSChris Mason read_unlock(&em_tree->lock); 2355f39d397SChris Mason goto out; 236a061fc8dSChris Mason } 237890871beSChris Mason read_unlock(&em_tree->lock); 2387b13b7b1SChris Mason 239172ddd60SDavid Sterba em = alloc_extent_map(); 2405f39d397SChris Mason if (!em) { 2415f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2425f39d397SChris Mason goto out; 2435f39d397SChris Mason } 2445f39d397SChris Mason em->start = 0; 2450afbaf8cSChris Mason em->len = (u64)-1; 246c8b97818SChris Mason em->block_len = (u64)-1; 2475f39d397SChris Mason em->block_start = 0; 2480b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 249d1310b2eSChris Mason 250890871beSChris Mason write_lock(&em_tree->lock); 25109a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2525f39d397SChris Mason if (ret == -EEXIST) { 2535f39d397SChris Mason free_extent_map(em); 2547b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 255b4f359abSTsutomu Itoh if (!em) 2560433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2575f39d397SChris Mason } else if (ret) { 2587b13b7b1SChris Mason free_extent_map(em); 2590433f20dSTsutomu Itoh em = ERR_PTR(ret); 2605f39d397SChris Mason } 261890871beSChris Mason write_unlock(&em_tree->lock); 2627b13b7b1SChris Mason 2635f39d397SChris Mason out: 2645f39d397SChris Mason return em; 2655f39d397SChris Mason } 2665f39d397SChris Mason 2679ed57367SDavid Sterba u32 btrfs_csum_data(const char *data, u32 seed, size_t len) 26819c00ddcSChris Mason { 2690b947affSFilipe David Borba Manana return btrfs_crc32c(seed, data, len); 27019c00ddcSChris Mason } 27119c00ddcSChris Mason 2720b5e3dafSDomagoj Tršan void btrfs_csum_final(u32 crc, u8 *result) 27319c00ddcSChris Mason { 2747e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 27519c00ddcSChris Mason } 27619c00ddcSChris Mason 277d352ac68SChris Mason /* 278d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 279d352ac68SChris Mason * into the csum field of the block. 280d352ac68SChris Mason */ 28101d58472SDaniel Dressler static int csum_tree_block(struct btrfs_fs_info *fs_info, 28201d58472SDaniel Dressler struct extent_buffer *buf, 28319c00ddcSChris Mason int verify) 28419c00ddcSChris Mason { 28501d58472SDaniel Dressler u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 286607d432dSJosef Bacik char *result = NULL; 28719c00ddcSChris Mason unsigned long len; 28819c00ddcSChris Mason unsigned long cur_len; 28919c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 29019c00ddcSChris Mason char *kaddr; 29119c00ddcSChris Mason unsigned long map_start; 29219c00ddcSChris Mason unsigned long map_len; 29319c00ddcSChris Mason int err; 29419c00ddcSChris Mason u32 crc = ~(u32)0; 295607d432dSJosef Bacik unsigned long inline_result; 29619c00ddcSChris Mason 29719c00ddcSChris Mason len = buf->len - offset; 29819c00ddcSChris Mason while (len > 0) { 29919c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 300a6591715SChris Mason &kaddr, &map_start, &map_len); 301d397712bSChris Mason if (err) 3028bd98f0eSAlex Lyakas return err; 30319c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 304b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 30519c00ddcSChris Mason crc, cur_len); 30619c00ddcSChris Mason len -= cur_len; 30719c00ddcSChris Mason offset += cur_len; 30819c00ddcSChris Mason } 309607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 31031e818feSDavid Sterba result = kzalloc(csum_size, GFP_NOFS); 311607d432dSJosef Bacik if (!result) 3128bd98f0eSAlex Lyakas return -ENOMEM; 313607d432dSJosef Bacik } else { 314607d432dSJosef Bacik result = (char *)&inline_result; 315607d432dSJosef Bacik } 316607d432dSJosef Bacik 31719c00ddcSChris Mason btrfs_csum_final(crc, result); 31819c00ddcSChris Mason 31919c00ddcSChris Mason if (verify) { 320607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 321e4204dedSChris Mason u32 val; 322e4204dedSChris Mason u32 found = 0; 323607d432dSJosef Bacik memcpy(&found, result, csum_size); 324e4204dedSChris Mason 325607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 32694647322SDavid Sterba btrfs_warn_rl(fs_info, 3275d163e0eSJeff Mahoney "%s checksum verify failed on %llu wanted %X found %X level %d", 32801d58472SDaniel Dressler fs_info->sb->s_id, buf->start, 329c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 330607d432dSJosef Bacik if (result != (char *)&inline_result) 331607d432dSJosef Bacik kfree(result); 3328bd98f0eSAlex Lyakas return -EUCLEAN; 33319c00ddcSChris Mason } 33419c00ddcSChris Mason } else { 335607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 33619c00ddcSChris Mason } 337607d432dSJosef Bacik if (result != (char *)&inline_result) 338607d432dSJosef Bacik kfree(result); 33919c00ddcSChris Mason return 0; 34019c00ddcSChris Mason } 34119c00ddcSChris Mason 342d352ac68SChris Mason /* 343d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 344d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 345d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 346d352ac68SChris Mason * in the wrong place. 347d352ac68SChris Mason */ 3481259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 349b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 350b9fab919SChris Mason int atomic) 3511259ab75SChris Mason { 3522ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3531259ab75SChris Mason int ret; 3542755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3551259ab75SChris Mason 3561259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3571259ab75SChris Mason return 0; 3581259ab75SChris Mason 359b9fab919SChris Mason if (atomic) 360b9fab919SChris Mason return -EAGAIN; 361b9fab919SChris Mason 362a26e8c9fSJosef Bacik if (need_lock) { 363a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 364a26e8c9fSJosef Bacik btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 365a26e8c9fSJosef Bacik } 366a26e8c9fSJosef Bacik 3672ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 368ff13db41SDavid Sterba &cached_state); 3690b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3701259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3711259ab75SChris Mason ret = 0; 3721259ab75SChris Mason goto out; 3731259ab75SChris Mason } 37494647322SDavid Sterba btrfs_err_rl(eb->fs_info, 37594647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 37694647322SDavid Sterba eb->start, 37729549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3781259ab75SChris Mason ret = 1; 379a26e8c9fSJosef Bacik 380a26e8c9fSJosef Bacik /* 381a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 382a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 38301327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 384a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 385a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 386a26e8c9fSJosef Bacik * making everybody very sad. 387a26e8c9fSJosef Bacik */ 388a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3890b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 39033958dc6SChris Mason out: 3912ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3922ac55d41SJosef Bacik &cached_state, GFP_NOFS); 393472b909fSJosef Bacik if (need_lock) 394a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3951259ab75SChris Mason return ret; 3961259ab75SChris Mason } 3971259ab75SChris Mason 398d352ac68SChris Mason /* 3991104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 4001104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 4011104a885SDavid Sterba */ 402ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 403ab8d0fc4SJeff Mahoney char *raw_disk_sb) 4041104a885SDavid Sterba { 4051104a885SDavid Sterba struct btrfs_super_block *disk_sb = 4061104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 4071104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 4081104a885SDavid Sterba int ret = 0; 4091104a885SDavid Sterba 4101104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 4111104a885SDavid Sterba u32 crc = ~(u32)0; 4121104a885SDavid Sterba const int csum_size = sizeof(crc); 4131104a885SDavid Sterba char result[csum_size]; 4141104a885SDavid Sterba 4151104a885SDavid Sterba /* 4161104a885SDavid Sterba * The super_block structure does not span the whole 4171104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 41801327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 4191104a885SDavid Sterba */ 4201104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4211104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4221104a885SDavid Sterba btrfs_csum_final(crc, result); 4231104a885SDavid Sterba 4241104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 4251104a885SDavid Sterba ret = 1; 4261104a885SDavid Sterba } 4271104a885SDavid Sterba 4281104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 429ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 4301104a885SDavid Sterba csum_type); 4311104a885SDavid Sterba ret = 1; 4321104a885SDavid Sterba } 4331104a885SDavid Sterba 4341104a885SDavid Sterba return ret; 4351104a885SDavid Sterba } 4361104a885SDavid Sterba 4371104a885SDavid Sterba /* 438d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 439d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 440d352ac68SChris Mason */ 4412ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 442f188591eSChris Mason struct extent_buffer *eb, 4438436ea91SJosef Bacik u64 parent_transid) 444f188591eSChris Mason { 445f188591eSChris Mason struct extent_io_tree *io_tree; 446ea466794SJosef Bacik int failed = 0; 447f188591eSChris Mason int ret; 448f188591eSChris Mason int num_copies = 0; 449f188591eSChris Mason int mirror_num = 0; 450ea466794SJosef Bacik int failed_mirror = 0; 451f188591eSChris Mason 452a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4530b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 454f188591eSChris Mason while (1) { 4558436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 456f188591eSChris Mason btree_get_extent, mirror_num); 457256dd1bbSStefan Behrens if (!ret) { 458256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 459b9fab919SChris Mason parent_transid, 0)) 460ea466794SJosef Bacik break; 461256dd1bbSStefan Behrens else 462256dd1bbSStefan Behrens ret = -EIO; 463256dd1bbSStefan Behrens } 464d397712bSChris Mason 465a826d6dcSJosef Bacik /* 466a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 467a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 468a826d6dcSJosef Bacik * any less wrong. 469a826d6dcSJosef Bacik */ 470a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 471ea466794SJosef Bacik break; 472ea466794SJosef Bacik 4730b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 474f188591eSChris Mason eb->start, eb->len); 4754235298eSChris Mason if (num_copies == 1) 476ea466794SJosef Bacik break; 4774235298eSChris Mason 4785cf1ab56SJosef Bacik if (!failed_mirror) { 4795cf1ab56SJosef Bacik failed = 1; 4805cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4815cf1ab56SJosef Bacik } 4825cf1ab56SJosef Bacik 483f188591eSChris Mason mirror_num++; 484ea466794SJosef Bacik if (mirror_num == failed_mirror) 485ea466794SJosef Bacik mirror_num++; 486ea466794SJosef Bacik 4874235298eSChris Mason if (mirror_num > num_copies) 488ea466794SJosef Bacik break; 489f188591eSChris Mason } 490ea466794SJosef Bacik 491c0901581SStefan Behrens if (failed && !ret && failed_mirror) 4922ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 493ea466794SJosef Bacik 494ea466794SJosef Bacik return ret; 495f188591eSChris Mason } 49619c00ddcSChris Mason 497d352ac68SChris Mason /* 498d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 499d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 500d352ac68SChris Mason */ 501d397712bSChris Mason 50201d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 50319c00ddcSChris Mason { 5044eee4fa4SMiao Xie u64 start = page_offset(page); 50519c00ddcSChris Mason u64 found_start; 50619c00ddcSChris Mason struct extent_buffer *eb; 507f188591eSChris Mason 5084f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5094f2de97aSJosef Bacik if (page != eb->pages[0]) 5104f2de97aSJosef Bacik return 0; 5110f805531SAlex Lyakas 51219c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5130f805531SAlex Lyakas /* 5140f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5150f805531SAlex Lyakas * It is useful to know what went wrong. 5160f805531SAlex Lyakas */ 5170f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5180f805531SAlex Lyakas return -EUCLEAN; 5190f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5200f805531SAlex Lyakas return -EUCLEAN; 5210f805531SAlex Lyakas 5220f805531SAlex Lyakas ASSERT(memcmp_extent_buffer(eb, fs_info->fsid, 5230f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5240f805531SAlex Lyakas 5258bd98f0eSAlex Lyakas return csum_tree_block(fs_info, eb, 0); 52619c00ddcSChris Mason } 52719c00ddcSChris Mason 52801d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5292b82032cSYan Zheng struct extent_buffer *eb) 5302b82032cSYan Zheng { 53101d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 5322b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 5332b82032cSYan Zheng int ret = 1; 5342b82032cSYan Zheng 5350a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5362b82032cSYan Zheng while (fs_devices) { 5372b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5382b82032cSYan Zheng ret = 0; 5392b82032cSYan Zheng break; 5402b82032cSYan Zheng } 5412b82032cSYan Zheng fs_devices = fs_devices->seed; 5422b82032cSYan Zheng } 5432b82032cSYan Zheng return ret; 5442b82032cSYan Zheng } 5452b82032cSYan Zheng 546a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 5470b246afaSJeff Mahoney btrfs_crit(root->fs_info, \ 5480b246afaSJeff Mahoney "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \ 5496b722c17SLiu Bo btrfs_header_level(eb) == 0 ? "leaf" : "node", \ 5506b722c17SLiu Bo reason, btrfs_header_bytenr(eb), root->objectid, slot) 551a826d6dcSJosef Bacik 552a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 553a826d6dcSJosef Bacik struct extent_buffer *leaf) 554a826d6dcSJosef Bacik { 5550b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 556a826d6dcSJosef Bacik struct btrfs_key key; 557a826d6dcSJosef Bacik struct btrfs_key leaf_key; 558a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 559a826d6dcSJosef Bacik int slot; 560a826d6dcSJosef Bacik 561f177d739SFilipe Manana /* 562f177d739SFilipe Manana * Extent buffers from a relocation tree have a owner field that 563f177d739SFilipe Manana * corresponds to the subvolume tree they are based on. So just from an 564f177d739SFilipe Manana * extent buffer alone we can not find out what is the id of the 565f177d739SFilipe Manana * corresponding subvolume tree, so we can not figure out if the extent 566f177d739SFilipe Manana * buffer corresponds to the root of the relocation tree or not. So skip 567f177d739SFilipe Manana * this check for relocation trees. 568f177d739SFilipe Manana */ 569f177d739SFilipe Manana if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 5701ba98d08SLiu Bo struct btrfs_root *check_root; 5711ba98d08SLiu Bo 5721ba98d08SLiu Bo key.objectid = btrfs_header_owner(leaf); 5731ba98d08SLiu Bo key.type = BTRFS_ROOT_ITEM_KEY; 5741ba98d08SLiu Bo key.offset = (u64)-1; 5751ba98d08SLiu Bo 5760b246afaSJeff Mahoney check_root = btrfs_get_fs_root(fs_info, &key, false); 5771ba98d08SLiu Bo /* 5781ba98d08SLiu Bo * The only reason we also check NULL here is that during 5791ba98d08SLiu Bo * open_ctree() some roots has not yet been set up. 5801ba98d08SLiu Bo */ 5811ba98d08SLiu Bo if (!IS_ERR_OR_NULL(check_root)) { 582ef85b25eSLiu Bo struct extent_buffer *eb; 583ef85b25eSLiu Bo 584ef85b25eSLiu Bo eb = btrfs_root_node(check_root); 5851ba98d08SLiu Bo /* if leaf is the root, then it's fine */ 586ef85b25eSLiu Bo if (leaf != eb) { 5871ba98d08SLiu Bo CORRUPT("non-root leaf's nritems is 0", 588ef85b25eSLiu Bo leaf, check_root, 0); 589ef85b25eSLiu Bo free_extent_buffer(eb); 5901ba98d08SLiu Bo return -EIO; 5911ba98d08SLiu Bo } 592ef85b25eSLiu Bo free_extent_buffer(eb); 5931ba98d08SLiu Bo } 594a826d6dcSJosef Bacik return 0; 5951ba98d08SLiu Bo } 596a826d6dcSJosef Bacik 597f177d739SFilipe Manana if (nritems == 0) 598f177d739SFilipe Manana return 0; 599f177d739SFilipe Manana 600a826d6dcSJosef Bacik /* Check the 0 item */ 601a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 6020b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 603a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 604a826d6dcSJosef Bacik return -EIO; 605a826d6dcSJosef Bacik } 606a826d6dcSJosef Bacik 607a826d6dcSJosef Bacik /* 608a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 609a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 610a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 611a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 612a826d6dcSJosef Bacik * offset is correct. 613a826d6dcSJosef Bacik */ 614a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 615a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 616a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 617a826d6dcSJosef Bacik 618a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 619a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 620a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 621a826d6dcSJosef Bacik return -EIO; 622a826d6dcSJosef Bacik } 623a826d6dcSJosef Bacik 624a826d6dcSJosef Bacik /* 625a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 626a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 627a826d6dcSJosef Bacik * front. 628a826d6dcSJosef Bacik */ 629a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 630a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 631a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 632a826d6dcSJosef Bacik return -EIO; 633a826d6dcSJosef Bacik } 634a826d6dcSJosef Bacik 635a826d6dcSJosef Bacik /* 636a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 637a826d6dcSJosef Bacik * just in case all the items are consistent to each other, but 638a826d6dcSJosef Bacik * all point outside of the leaf. 639a826d6dcSJosef Bacik */ 640a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 6410b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 642a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 643a826d6dcSJosef Bacik return -EIO; 644a826d6dcSJosef Bacik } 645a826d6dcSJosef Bacik } 646a826d6dcSJosef Bacik 647a826d6dcSJosef Bacik return 0; 648a826d6dcSJosef Bacik } 649a826d6dcSJosef Bacik 650053ab70fSLiu Bo static int check_node(struct btrfs_root *root, struct extent_buffer *node) 651053ab70fSLiu Bo { 652053ab70fSLiu Bo unsigned long nr = btrfs_header_nritems(node); 6536b722c17SLiu Bo struct btrfs_key key, next_key; 6546b722c17SLiu Bo int slot; 6556b722c17SLiu Bo u64 bytenr; 6566b722c17SLiu Bo int ret = 0; 657053ab70fSLiu Bo 658da17066cSJeff Mahoney if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) { 659053ab70fSLiu Bo btrfs_crit(root->fs_info, 660053ab70fSLiu Bo "corrupt node: block %llu root %llu nritems %lu", 661053ab70fSLiu Bo node->start, root->objectid, nr); 662053ab70fSLiu Bo return -EIO; 663053ab70fSLiu Bo } 6646b722c17SLiu Bo 6656b722c17SLiu Bo for (slot = 0; slot < nr - 1; slot++) { 6666b722c17SLiu Bo bytenr = btrfs_node_blockptr(node, slot); 6676b722c17SLiu Bo btrfs_node_key_to_cpu(node, &key, slot); 6686b722c17SLiu Bo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 6696b722c17SLiu Bo 6706b722c17SLiu Bo if (!bytenr) { 6716b722c17SLiu Bo CORRUPT("invalid item slot", node, root, slot); 6726b722c17SLiu Bo ret = -EIO; 6736b722c17SLiu Bo goto out; 6746b722c17SLiu Bo } 6756b722c17SLiu Bo 6766b722c17SLiu Bo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 6776b722c17SLiu Bo CORRUPT("bad key order", node, root, slot); 6786b722c17SLiu Bo ret = -EIO; 6796b722c17SLiu Bo goto out; 6806b722c17SLiu Bo } 6816b722c17SLiu Bo } 6826b722c17SLiu Bo out: 6836b722c17SLiu Bo return ret; 684053ab70fSLiu Bo } 685053ab70fSLiu Bo 686facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 687facc8a22SMiao Xie u64 phy_offset, struct page *page, 688facc8a22SMiao Xie u64 start, u64 end, int mirror) 689ce9adaa5SChris Mason { 690ce9adaa5SChris Mason u64 found_start; 691ce9adaa5SChris Mason int found_level; 692ce9adaa5SChris Mason struct extent_buffer *eb; 693ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 69402873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 695f188591eSChris Mason int ret = 0; 696727011e0SChris Mason int reads_done; 697ce9adaa5SChris Mason 698ce9adaa5SChris Mason if (!page->private) 699ce9adaa5SChris Mason goto out; 700d397712bSChris Mason 7014f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 702d397712bSChris Mason 7030b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 7040b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 7050b32f4bbSJosef Bacik */ 7060b32f4bbSJosef Bacik extent_buffer_get(eb); 7070b32f4bbSJosef Bacik 7080b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 709727011e0SChris Mason if (!reads_done) 710727011e0SChris Mason goto err; 711f188591eSChris Mason 7125cf1ab56SJosef Bacik eb->read_mirror = mirror; 713656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 714ea466794SJosef Bacik ret = -EIO; 715ea466794SJosef Bacik goto err; 716ea466794SJosef Bacik } 717ea466794SJosef Bacik 718ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 719727011e0SChris Mason if (found_start != eb->start) { 72002873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 72194647322SDavid Sterba found_start, eb->start); 722f188591eSChris Mason ret = -EIO; 723ce9adaa5SChris Mason goto err; 724ce9adaa5SChris Mason } 72502873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 72602873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 72794647322SDavid Sterba eb->start); 7281259ab75SChris Mason ret = -EIO; 7291259ab75SChris Mason goto err; 7301259ab75SChris Mason } 731ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 7321c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 73302873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 7341c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 7351c24c3ceSJosef Bacik ret = -EIO; 7361c24c3ceSJosef Bacik goto err; 7371c24c3ceSJosef Bacik } 738ce9adaa5SChris Mason 73985d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 74085d4e461SChris Mason eb, found_level); 7414008c04aSChris Mason 74202873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 7438bd98f0eSAlex Lyakas if (ret) 744a826d6dcSJosef Bacik goto err; 745a826d6dcSJosef Bacik 746a826d6dcSJosef Bacik /* 747a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 748a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 749a826d6dcSJosef Bacik * return -EIO. 750a826d6dcSJosef Bacik */ 751a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 752a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 753a826d6dcSJosef Bacik ret = -EIO; 754a826d6dcSJosef Bacik } 755ce9adaa5SChris Mason 756053ab70fSLiu Bo if (found_level > 0 && check_node(root, eb)) 757053ab70fSLiu Bo ret = -EIO; 758053ab70fSLiu Bo 7590b32f4bbSJosef Bacik if (!ret) 7600b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 761ce9adaa5SChris Mason err: 76279fb65a1SJosef Bacik if (reads_done && 76379fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 764d48d71aaSDavid Sterba btree_readahead_hook(eb, ret); 7654bb31e92SArne Jansen 76653b381b3SDavid Woodhouse if (ret) { 76753b381b3SDavid Woodhouse /* 76853b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 76953b381b3SDavid Woodhouse * again, we have to make sure it has something 77053b381b3SDavid Woodhouse * to decrement 77153b381b3SDavid Woodhouse */ 77253b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7730b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 77453b381b3SDavid Woodhouse } 7750b32f4bbSJosef Bacik free_extent_buffer(eb); 776ce9adaa5SChris Mason out: 777f188591eSChris Mason return ret; 778ce9adaa5SChris Mason } 779ce9adaa5SChris Mason 780ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 7814bb31e92SArne Jansen { 7824bb31e92SArne Jansen struct extent_buffer *eb; 7834bb31e92SArne Jansen 7844f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 785656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 7865cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 78753b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 788ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 789d48d71aaSDavid Sterba btree_readahead_hook(eb, -EIO); 7904bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 7914bb31e92SArne Jansen } 7924bb31e92SArne Jansen 7934246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 794ce9adaa5SChris Mason { 79597eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 796ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7979e0af237SLiu Bo struct btrfs_workqueue *wq; 7989e0af237SLiu Bo btrfs_work_func_t func; 799ce9adaa5SChris Mason 800ce9adaa5SChris Mason fs_info = end_io_wq->info; 8014246a0b6SChristoph Hellwig end_io_wq->error = bio->bi_error; 802d20f7043SChris Mason 80337226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 8049e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 8059e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 8069e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 8079e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 8089e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 8099e0af237SLiu Bo func = btrfs_freespace_write_helper; 8109e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 8119e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 8129e0af237SLiu Bo func = btrfs_endio_raid56_helper; 813d20f7043SChris Mason } else { 8149e0af237SLiu Bo wq = fs_info->endio_write_workers; 8159e0af237SLiu Bo func = btrfs_endio_write_helper; 816d20f7043SChris Mason } 8179e0af237SLiu Bo } else { 8188b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 8198b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 8208b110e39SMiao Xie wq = fs_info->endio_repair_workers; 8218b110e39SMiao Xie func = btrfs_endio_repair_helper; 8228b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 8239e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 8249e0af237SLiu Bo func = btrfs_endio_raid56_helper; 8259e0af237SLiu Bo } else if (end_io_wq->metadata) { 8269e0af237SLiu Bo wq = fs_info->endio_meta_workers; 8279e0af237SLiu Bo func = btrfs_endio_meta_helper; 8289e0af237SLiu Bo } else { 8299e0af237SLiu Bo wq = fs_info->endio_workers; 8309e0af237SLiu Bo func = btrfs_endio_helper; 8319e0af237SLiu Bo } 8329e0af237SLiu Bo } 8339e0af237SLiu Bo 8349e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 8359e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 836ce9adaa5SChris Mason } 837ce9adaa5SChris Mason 83822c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 839bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 8400b86a832SChris Mason { 84197eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 8428b110e39SMiao Xie 84397eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 844ce9adaa5SChris Mason if (!end_io_wq) 845ce9adaa5SChris Mason return -ENOMEM; 846ce9adaa5SChris Mason 847ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 848ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 84922c59948SChris Mason end_io_wq->info = info; 850ce9adaa5SChris Mason end_io_wq->error = 0; 851ce9adaa5SChris Mason end_io_wq->bio = bio; 85222c59948SChris Mason end_io_wq->metadata = metadata; 853ce9adaa5SChris Mason 854ce9adaa5SChris Mason bio->bi_private = end_io_wq; 855ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 85622c59948SChris Mason return 0; 85722c59948SChris Mason } 85822c59948SChris Mason 859b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 8604854ddd0SChris Mason { 8614854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 8625cdc7ad3SQu Wenruo info->thread_pool_size, 8634854ddd0SChris Mason info->fs_devices->open_devices); 8644854ddd0SChris Mason return 256 * limit; 8654854ddd0SChris Mason } 8664854ddd0SChris Mason 8674a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 8684a69a410SChris Mason { 8694a69a410SChris Mason struct async_submit_bio *async; 87079787eaaSJeff Mahoney int ret; 8714a69a410SChris Mason 8724a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 873c6100a4bSJosef Bacik ret = async->submit_bio_start(async->private_data, async->bio, 874eaf25d93SChris Mason async->mirror_num, async->bio_flags, 875eaf25d93SChris Mason async->bio_offset); 87679787eaaSJeff Mahoney if (ret) 87779787eaaSJeff Mahoney async->error = ret; 8784a69a410SChris Mason } 8794a69a410SChris Mason 8804a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8818b712842SChris Mason { 8828b712842SChris Mason struct btrfs_fs_info *fs_info; 8838b712842SChris Mason struct async_submit_bio *async; 8844854ddd0SChris Mason int limit; 8858b712842SChris Mason 8868b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 887c6100a4bSJosef Bacik fs_info = async->fs_info; 8884854ddd0SChris Mason 889b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 8904854ddd0SChris Mason limit = limit * 2 / 3; 8914854ddd0SChris Mason 892ee863954SDavid Sterba /* 893ee863954SDavid Sterba * atomic_dec_return implies a barrier for waitqueue_active 894ee863954SDavid Sterba */ 89566657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 896b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 8974854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 8984854ddd0SChris Mason 899bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 90079787eaaSJeff Mahoney if (async->error) { 9014246a0b6SChristoph Hellwig async->bio->bi_error = async->error; 9024246a0b6SChristoph Hellwig bio_endio(async->bio); 90379787eaaSJeff Mahoney return; 90479787eaaSJeff Mahoney } 90579787eaaSJeff Mahoney 906c6100a4bSJosef Bacik async->submit_bio_done(async->private_data, async->bio, async->mirror_num, 90781a75f67SMike Christie async->bio_flags, async->bio_offset); 9084a69a410SChris Mason } 9094a69a410SChris Mason 9104a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 9114a69a410SChris Mason { 9124a69a410SChris Mason struct async_submit_bio *async; 9134a69a410SChris Mason 9144a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 9158b712842SChris Mason kfree(async); 9168b712842SChris Mason } 9178b712842SChris Mason 918c6100a4bSJosef Bacik int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, 919c6100a4bSJosef Bacik int mirror_num, unsigned long bio_flags, 920c6100a4bSJosef Bacik u64 bio_offset, void *private_data, 9214a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 9224a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 92344b8bd7eSChris Mason { 92444b8bd7eSChris Mason struct async_submit_bio *async; 92544b8bd7eSChris Mason 92644b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 92744b8bd7eSChris Mason if (!async) 92844b8bd7eSChris Mason return -ENOMEM; 92944b8bd7eSChris Mason 930c6100a4bSJosef Bacik async->private_data = private_data; 931c6100a4bSJosef Bacik async->fs_info = fs_info; 93244b8bd7eSChris Mason async->bio = bio; 93344b8bd7eSChris Mason async->mirror_num = mirror_num; 9344a69a410SChris Mason async->submit_bio_start = submit_bio_start; 9354a69a410SChris Mason async->submit_bio_done = submit_bio_done; 9364a69a410SChris Mason 9379e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 9385cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 9394a69a410SChris Mason 940c8b97818SChris Mason async->bio_flags = bio_flags; 941eaf25d93SChris Mason async->bio_offset = bio_offset; 9428c8bee1dSChris Mason 94379787eaaSJeff Mahoney async->error = 0; 94479787eaaSJeff Mahoney 945cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 946d313d7a3SChris Mason 94767f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 9485cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 949d313d7a3SChris Mason 9505cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 9519473f16cSChris Mason 952771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 953771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 954771ed689SChris Mason wait_event(fs_info->async_submit_wait, 955771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 956771ed689SChris Mason } 957771ed689SChris Mason 95844b8bd7eSChris Mason return 0; 95944b8bd7eSChris Mason } 96044b8bd7eSChris Mason 961ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 962ce3ed71aSChris Mason { 9632c30c71bSKent Overstreet struct bio_vec *bvec; 964ce3ed71aSChris Mason struct btrfs_root *root; 9652c30c71bSKent Overstreet int i, ret = 0; 966ce3ed71aSChris Mason 9672c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 968ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 96901d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 97079787eaaSJeff Mahoney if (ret) 97179787eaaSJeff Mahoney break; 972ce3ed71aSChris Mason } 9732c30c71bSKent Overstreet 97479787eaaSJeff Mahoney return ret; 975ce3ed71aSChris Mason } 976ce3ed71aSChris Mason 977c6100a4bSJosef Bacik static int __btree_submit_bio_start(void *private_data, struct bio *bio, 97881a75f67SMike Christie int mirror_num, unsigned long bio_flags, 979eaf25d93SChris Mason u64 bio_offset) 98022c59948SChris Mason { 9818b712842SChris Mason /* 9828b712842SChris Mason * when we're called for a write, we're already in the async 9835443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9848b712842SChris Mason */ 98579787eaaSJeff Mahoney return btree_csum_one_bio(bio); 98622c59948SChris Mason } 98722c59948SChris Mason 988c6100a4bSJosef Bacik static int __btree_submit_bio_done(void *private_data, struct bio *bio, 989eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 990eaf25d93SChris Mason u64 bio_offset) 9914a69a410SChris Mason { 992c6100a4bSJosef Bacik struct inode *inode = private_data; 99361891923SStefan Behrens int ret; 99461891923SStefan Behrens 9958b712842SChris Mason /* 9964a69a410SChris Mason * when we're called for a write, we're already in the async 9974a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 9988b712842SChris Mason */ 9992ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 10004246a0b6SChristoph Hellwig if (ret) { 10014246a0b6SChristoph Hellwig bio->bi_error = ret; 10024246a0b6SChristoph Hellwig bio_endio(bio); 10034246a0b6SChristoph Hellwig } 100461891923SStefan Behrens return ret; 10050b86a832SChris Mason } 10060b86a832SChris Mason 1007e27f6265SDavid Sterba static int check_async_write(unsigned long bio_flags) 1008de0022b9SJosef Bacik { 1009de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 1010de0022b9SJosef Bacik return 0; 1011de0022b9SJosef Bacik #ifdef CONFIG_X86 1012bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 1013de0022b9SJosef Bacik return 0; 1014de0022b9SJosef Bacik #endif 1015de0022b9SJosef Bacik return 1; 1016de0022b9SJosef Bacik } 1017de0022b9SJosef Bacik 1018c6100a4bSJosef Bacik static int btree_submit_bio_hook(void *private_data, struct bio *bio, 1019eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 1020eaf25d93SChris Mason u64 bio_offset) 102144b8bd7eSChris Mason { 1022c6100a4bSJosef Bacik struct inode *inode = private_data; 10230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1024e27f6265SDavid Sterba int async = check_async_write(bio_flags); 10254a69a410SChris Mason int ret; 1026cad321adSChris Mason 102737226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 1028cad321adSChris Mason /* 1029cad321adSChris Mason * called for a read, do the setup so that checksum validation 1030cad321adSChris Mason * can happen in the async kernel threads 1031cad321adSChris Mason */ 10320b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 10330b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 10341d4284bdSChris Mason if (ret) 103561891923SStefan Behrens goto out_w_error; 10362ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 1037de0022b9SJosef Bacik } else if (!async) { 1038de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 1039de0022b9SJosef Bacik if (ret) 104061891923SStefan Behrens goto out_w_error; 10412ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 104261891923SStefan Behrens } else { 1043cad321adSChris Mason /* 104461891923SStefan Behrens * kthread helpers are used to submit writes so that 104561891923SStefan Behrens * checksumming can happen in parallel across all CPUs 1046cad321adSChris Mason */ 1047c6100a4bSJosef Bacik ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0, 1048c6100a4bSJosef Bacik bio_offset, private_data, 10494a69a410SChris Mason __btree_submit_bio_start, 10504a69a410SChris Mason __btree_submit_bio_done); 105144b8bd7eSChris Mason } 105244b8bd7eSChris Mason 10534246a0b6SChristoph Hellwig if (ret) 10544246a0b6SChristoph Hellwig goto out_w_error; 10554246a0b6SChristoph Hellwig return 0; 10564246a0b6SChristoph Hellwig 105761891923SStefan Behrens out_w_error: 10584246a0b6SChristoph Hellwig bio->bi_error = ret; 10594246a0b6SChristoph Hellwig bio_endio(bio); 106061891923SStefan Behrens return ret; 106161891923SStefan Behrens } 106261891923SStefan Behrens 10633dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 1064784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 1065a6bc32b8SMel Gorman struct page *newpage, struct page *page, 1066a6bc32b8SMel Gorman enum migrate_mode mode) 1067784b4e29SChris Mason { 1068784b4e29SChris Mason /* 1069784b4e29SChris Mason * we can't safely write a btree page from here, 1070784b4e29SChris Mason * we haven't done the locking hook 1071784b4e29SChris Mason */ 1072784b4e29SChris Mason if (PageDirty(page)) 1073784b4e29SChris Mason return -EAGAIN; 1074784b4e29SChris Mason /* 1075784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 1076784b4e29SChris Mason * We must have no buffers or drop them. 1077784b4e29SChris Mason */ 1078784b4e29SChris Mason if (page_has_private(page) && 1079784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 1080784b4e29SChris Mason return -EAGAIN; 1081a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1082784b4e29SChris Mason } 10833dd1462eSJan Beulich #endif 1084784b4e29SChris Mason 10850da5468fSChris Mason 10860da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 10870da5468fSChris Mason struct writeback_control *wbc) 10880da5468fSChris Mason { 1089e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 1090e2d84521SMiao Xie int ret; 1091e2d84521SMiao Xie 1092d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 1093448d640bSChris Mason 1094448d640bSChris Mason if (wbc->for_kupdate) 1095448d640bSChris Mason return 0; 1096448d640bSChris Mason 1097e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 1098b9473439SChris Mason /* this is a bit racy, but that's ok */ 1099e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1100e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 1101e2d84521SMiao Xie if (ret < 0) 1102793955bcSChris Mason return 0; 1103793955bcSChris Mason } 11040b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 11050da5468fSChris Mason } 11060da5468fSChris Mason 1107b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 11085f39d397SChris Mason { 1109d1310b2eSChris Mason struct extent_io_tree *tree; 1110d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 11118ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 11125f39d397SChris Mason } 11135f39d397SChris Mason 111470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 11155f39d397SChris Mason { 111698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 111798509cfcSChris Mason return 0; 11180c4e538bSDavid Sterba 1119f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1120d98237b3SChris Mason } 1121d98237b3SChris Mason 1122d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1123d47992f8SLukas Czerner unsigned int length) 1124d98237b3SChris Mason { 1125d1310b2eSChris Mason struct extent_io_tree *tree; 1126d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 11275f39d397SChris Mason extent_invalidatepage(tree, page, offset); 11285f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 11299ad6b7bcSChris Mason if (PagePrivate(page)) { 1130efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1131efe120a0SFrank Holton "page private not zero on page %llu", 1132efe120a0SFrank Holton (unsigned long long)page_offset(page)); 11339ad6b7bcSChris Mason ClearPagePrivate(page); 11349ad6b7bcSChris Mason set_page_private(page, 0); 113509cbfeafSKirill A. Shutemov put_page(page); 11369ad6b7bcSChris Mason } 1137d98237b3SChris Mason } 1138d98237b3SChris Mason 11390b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 11400b32f4bbSJosef Bacik { 1141bb146eb2SJosef Bacik #ifdef DEBUG 11420b32f4bbSJosef Bacik struct extent_buffer *eb; 11430b32f4bbSJosef Bacik 11440b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 11450b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 11460b32f4bbSJosef Bacik BUG_ON(!eb); 11470b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 11480b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 11490b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1150bb146eb2SJosef Bacik #endif 11510b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 11520b32f4bbSJosef Bacik } 11530b32f4bbSJosef Bacik 11547f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1155d98237b3SChris Mason .readpage = btree_readpage, 11560da5468fSChris Mason .writepages = btree_writepages, 11575f39d397SChris Mason .releasepage = btree_releasepage, 11585f39d397SChris Mason .invalidatepage = btree_invalidatepage, 11595a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1160784b4e29SChris Mason .migratepage = btree_migratepage, 11615a92bc88SChris Mason #endif 11620b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1163d98237b3SChris Mason }; 1164123abc88SChris Mason 11652ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1166090d1875SChris Mason { 11675f39d397SChris Mason struct extent_buffer *buf = NULL; 11682ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1169090d1875SChris Mason 11702ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1171c871b0f2SLiu Bo if (IS_ERR(buf)) 11726197d86eSDavid Sterba return; 1173d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 11748436ea91SJosef Bacik buf, WAIT_NONE, btree_get_extent, 0); 11755f39d397SChris Mason free_extent_buffer(buf); 1176090d1875SChris Mason } 1177090d1875SChris Mason 11782ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1179ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1180ab0fff03SArne Jansen { 1181ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 11822ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1183ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1184ab0fff03SArne Jansen int ret; 1185ab0fff03SArne Jansen 11862ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1187c871b0f2SLiu Bo if (IS_ERR(buf)) 1188ab0fff03SArne Jansen return 0; 1189ab0fff03SArne Jansen 1190ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1191ab0fff03SArne Jansen 11928436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 1193ab0fff03SArne Jansen btree_get_extent, mirror_num); 1194ab0fff03SArne Jansen if (ret) { 1195ab0fff03SArne Jansen free_extent_buffer(buf); 1196ab0fff03SArne Jansen return ret; 1197ab0fff03SArne Jansen } 1198ab0fff03SArne Jansen 1199ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1200ab0fff03SArne Jansen free_extent_buffer(buf); 1201ab0fff03SArne Jansen return -EIO; 12020b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1203ab0fff03SArne Jansen *eb = buf; 1204ab0fff03SArne Jansen } else { 1205ab0fff03SArne Jansen free_extent_buffer(buf); 1206ab0fff03SArne Jansen } 1207ab0fff03SArne Jansen return 0; 1208ab0fff03SArne Jansen } 1209ab0fff03SArne Jansen 12102ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 12112ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 12120308af44SDavid Sterba u64 bytenr) 12130999df54SChris Mason { 12140b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 12150b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 12160b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 12170999df54SChris Mason } 12180999df54SChris Mason 12190999df54SChris Mason 1220e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1221e02119d5SChris Mason { 1222727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 12238aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1224e02119d5SChris Mason } 1225e02119d5SChris Mason 12263189ff77SJeff Layton void btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1227e02119d5SChris Mason { 12283189ff77SJeff Layton filemap_fdatawait_range(buf->pages[0]->mapping, 12298aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1230e02119d5SChris Mason } 1231e02119d5SChris Mason 12322ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1233ce86cd59SDavid Sterba u64 parent_transid) 1234e20d96d6SChris Mason { 12355f39d397SChris Mason struct extent_buffer *buf = NULL; 123619c00ddcSChris Mason int ret; 123719c00ddcSChris Mason 12382ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1239c871b0f2SLiu Bo if (IS_ERR(buf)) 1240c871b0f2SLiu Bo return buf; 1241e4204dedSChris Mason 12422ff7e61eSJeff Mahoney ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 12430f0fe8f7SFilipe David Borba Manana if (ret) { 12440f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 124564c043deSLiu Bo return ERR_PTR(ret); 12460f0fe8f7SFilipe David Borba Manana } 12475f39d397SChris Mason return buf; 1248ce9adaa5SChris Mason 1249eb60ceacSChris Mason } 1250eb60ceacSChris Mason 12517c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 12525f39d397SChris Mason struct extent_buffer *buf) 1253ed2ff2cbSChris Mason { 125455c69072SChris Mason if (btrfs_header_generation(buf) == 1255e2d84521SMiao Xie fs_info->running_transaction->transid) { 1256b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1257b4ce94deSChris Mason 1258b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1259e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1260e2d84521SMiao Xie -buf->len, 1261e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1262b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1263b9473439SChris Mason btrfs_set_lock_blocking(buf); 12640b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1265925baeddSChris Mason } 12665f39d397SChris Mason } 1267ed7b63ebSJosef Bacik } 12685f39d397SChris Mason 12698257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 12708257b2dcSMiao Xie { 12718257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 12728257b2dcSMiao Xie int ret; 12738257b2dcSMiao Xie 12748257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 12758257b2dcSMiao Xie if (!writers) 12768257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 12778257b2dcSMiao Xie 1278908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 12798257b2dcSMiao Xie if (ret < 0) { 12808257b2dcSMiao Xie kfree(writers); 12818257b2dcSMiao Xie return ERR_PTR(ret); 12828257b2dcSMiao Xie } 12838257b2dcSMiao Xie 12848257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 12858257b2dcSMiao Xie return writers; 12868257b2dcSMiao Xie } 12878257b2dcSMiao Xie 12888257b2dcSMiao Xie static void 12898257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 12908257b2dcSMiao Xie { 12918257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 12928257b2dcSMiao Xie kfree(writers); 12938257b2dcSMiao Xie } 12948257b2dcSMiao Xie 1295da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1296e20d96d6SChris Mason u64 objectid) 1297d97e63b6SChris Mason { 12987c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1299cfaa7295SChris Mason root->node = NULL; 1300a28ec197SChris Mason root->commit_root = NULL; 130127cdeb70SMiao Xie root->state = 0; 1302d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 13030b86a832SChris Mason 13040f7d52f4SChris Mason root->objectid = objectid; 13050f7d52f4SChris Mason root->last_trans = 0; 130613a8a7c8SYan, Zheng root->highest_objectid = 0; 1307eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1308199c2a9cSMiao Xie root->nr_ordered_extents = 0; 130958176a96SJosef Bacik root->name = NULL; 13106bef4d31SEric Paris root->inode_tree = RB_ROOT; 131116cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1312f0486c68SYan, Zheng root->block_rsv = NULL; 1313d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 13140b86a832SChris Mason 13150b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 13165d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1317eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1318eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1319199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1320199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 13212ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 13222ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1323d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 13245d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1325eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1326199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1327f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 13282ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 13292ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1330a2135011SChris Mason mutex_init(&root->objectid_mutex); 1331e02119d5SChris Mason mutex_init(&root->log_mutex); 133231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1333573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 13347237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 13357237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 13367237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 13378b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 13388b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 13397237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 13407237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 13417237f183SYan Zheng atomic_set(&root->log_writers, 0); 13422ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 13438a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 13440700cea7SElena Reshetova refcount_set(&root->refs, 1); 13458257b2dcSMiao Xie atomic_set(&root->will_be_snapshoted, 0); 1346ce0dcee6SGoldwyn Rodrigues atomic64_set(&root->qgroup_meta_rsv, 0); 13477237f183SYan Zheng root->log_transid = 0; 1348d1433debSMiao Xie root->log_transid_committed = -1; 1349257c62e1SChris Mason root->last_log_commit = 0; 13507c0260eeSJeff Mahoney if (!dummy) 1351c6100a4bSJosef Bacik extent_io_tree_init(&root->dirty_log_pages, NULL); 1352017e5369SChris Mason 13533768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 13543768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 13556702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 13567c0260eeSJeff Mahoney if (!dummy) 13573f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 135806ea65a3SJosef Bacik else 135906ea65a3SJosef Bacik root->defrag_trans_start = 0; 13604d775673SChris Mason root->root_key.objectid = objectid; 13610ee5dc67SAl Viro root->anon_dev = 0; 13628ea05e3aSAlexander Block 13635f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 13643768f368SChris Mason } 13653768f368SChris Mason 136674e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 136774e4d827SDavid Sterba gfp_t flags) 13686f07e42eSAl Viro { 136974e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 13706f07e42eSAl Viro if (root) 13716f07e42eSAl Viro root->fs_info = fs_info; 13726f07e42eSAl Viro return root; 13736f07e42eSAl Viro } 13746f07e42eSAl Viro 137506ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 137606ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1377da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 137806ea65a3SJosef Bacik { 137906ea65a3SJosef Bacik struct btrfs_root *root; 138006ea65a3SJosef Bacik 13817c0260eeSJeff Mahoney if (!fs_info) 13827c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 13837c0260eeSJeff Mahoney 13847c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 138506ea65a3SJosef Bacik if (!root) 138606ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1387da17066cSJeff Mahoney 1388b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1389da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1390faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 139106ea65a3SJosef Bacik 139206ea65a3SJosef Bacik return root; 139306ea65a3SJosef Bacik } 139406ea65a3SJosef Bacik #endif 139506ea65a3SJosef Bacik 139620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 139720897f5cSArne Jansen struct btrfs_fs_info *fs_info, 139820897f5cSArne Jansen u64 objectid) 139920897f5cSArne Jansen { 140020897f5cSArne Jansen struct extent_buffer *leaf; 140120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 140220897f5cSArne Jansen struct btrfs_root *root; 140320897f5cSArne Jansen struct btrfs_key key; 140420897f5cSArne Jansen int ret = 0; 14056463fe58SStefan Behrens uuid_le uuid; 140620897f5cSArne Jansen 140774e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 140820897f5cSArne Jansen if (!root) 140920897f5cSArne Jansen return ERR_PTR(-ENOMEM); 141020897f5cSArne Jansen 1411da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 141220897f5cSArne Jansen root->root_key.objectid = objectid; 141320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 141420897f5cSArne Jansen root->root_key.offset = 0; 141520897f5cSArne Jansen 14164d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 141720897f5cSArne Jansen if (IS_ERR(leaf)) { 141820897f5cSArne Jansen ret = PTR_ERR(leaf); 14191dd05682STsutomu Itoh leaf = NULL; 142020897f5cSArne Jansen goto fail; 142120897f5cSArne Jansen } 142220897f5cSArne Jansen 1423b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 142420897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 142520897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 142620897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 142720897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 142820897f5cSArne Jansen root->node = leaf; 142920897f5cSArne Jansen 1430d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1431d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 143220897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 143320897f5cSArne Jansen 143420897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 143527cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 143620897f5cSArne Jansen 143720897f5cSArne Jansen root->root_item.flags = 0; 143820897f5cSArne Jansen root->root_item.byte_limit = 0; 143920897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 144020897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 144120897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 144220897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 144320897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 144420897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 144520897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 14466463fe58SStefan Behrens uuid_le_gen(&uuid); 14476463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 144820897f5cSArne Jansen root->root_item.drop_level = 0; 144920897f5cSArne Jansen 145020897f5cSArne Jansen key.objectid = objectid; 145120897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 145220897f5cSArne Jansen key.offset = 0; 145320897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 145420897f5cSArne Jansen if (ret) 145520897f5cSArne Jansen goto fail; 145620897f5cSArne Jansen 145720897f5cSArne Jansen btrfs_tree_unlock(leaf); 145820897f5cSArne Jansen 145920897f5cSArne Jansen return root; 14601dd05682STsutomu Itoh 14611dd05682STsutomu Itoh fail: 14621dd05682STsutomu Itoh if (leaf) { 14631dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 146459885b39STsutomu Itoh free_extent_buffer(root->commit_root); 14651dd05682STsutomu Itoh free_extent_buffer(leaf); 14661dd05682STsutomu Itoh } 14671dd05682STsutomu Itoh kfree(root); 14681dd05682STsutomu Itoh 14691dd05682STsutomu Itoh return ERR_PTR(ret); 147020897f5cSArne Jansen } 147120897f5cSArne Jansen 14727237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1473e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14740f7d52f4SChris Mason { 14750f7d52f4SChris Mason struct btrfs_root *root; 14767237f183SYan Zheng struct extent_buffer *leaf; 1477e02119d5SChris Mason 147874e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1479e02119d5SChris Mason if (!root) 14807237f183SYan Zheng return ERR_PTR(-ENOMEM); 1481e02119d5SChris Mason 1482da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1483e02119d5SChris Mason 1484e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1485e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1486e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 148727cdeb70SMiao Xie 14887237f183SYan Zheng /* 148927cdeb70SMiao Xie * DON'T set REF_COWS for log trees 149027cdeb70SMiao Xie * 14917237f183SYan Zheng * log trees do not get reference counted because they go away 14927237f183SYan Zheng * before a real commit is actually done. They do store pointers 14937237f183SYan Zheng * to file data extents, and those reference counts still get 14947237f183SYan Zheng * updated (along with back refs to the log tree). 14957237f183SYan Zheng */ 1496e02119d5SChris Mason 14974d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14984d75f8a9SDavid Sterba NULL, 0, 0, 0); 14997237f183SYan Zheng if (IS_ERR(leaf)) { 15007237f183SYan Zheng kfree(root); 15017237f183SYan Zheng return ERR_CAST(leaf); 15027237f183SYan Zheng } 1503e02119d5SChris Mason 1504b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 15055d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 15065d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 15075d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 15085d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 15097237f183SYan Zheng root->node = leaf; 1510e02119d5SChris Mason 15110b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1512e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1513e02119d5SChris Mason btrfs_tree_unlock(root->node); 15147237f183SYan Zheng return root; 15157237f183SYan Zheng } 15167237f183SYan Zheng 15177237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 15187237f183SYan Zheng struct btrfs_fs_info *fs_info) 15197237f183SYan Zheng { 15207237f183SYan Zheng struct btrfs_root *log_root; 15217237f183SYan Zheng 15227237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 15237237f183SYan Zheng if (IS_ERR(log_root)) 15247237f183SYan Zheng return PTR_ERR(log_root); 15257237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 15267237f183SYan Zheng fs_info->log_root_tree = log_root; 15277237f183SYan Zheng return 0; 15287237f183SYan Zheng } 15297237f183SYan Zheng 15307237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 15317237f183SYan Zheng struct btrfs_root *root) 15327237f183SYan Zheng { 15330b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 15347237f183SYan Zheng struct btrfs_root *log_root; 15357237f183SYan Zheng struct btrfs_inode_item *inode_item; 15367237f183SYan Zheng 15370b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 15387237f183SYan Zheng if (IS_ERR(log_root)) 15397237f183SYan Zheng return PTR_ERR(log_root); 15407237f183SYan Zheng 15417237f183SYan Zheng log_root->last_trans = trans->transid; 15427237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 15437237f183SYan Zheng 15447237f183SYan Zheng inode_item = &log_root->root_item.inode; 15453cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 15463cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15473cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1548da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15490b246afaSJeff Mahoney fs_info->nodesize); 15503cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15517237f183SYan Zheng 15525d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15537237f183SYan Zheng 15547237f183SYan Zheng WARN_ON(root->log_root); 15557237f183SYan Zheng root->log_root = log_root; 15567237f183SYan Zheng root->log_transid = 0; 1557d1433debSMiao Xie root->log_transid_committed = -1; 1558257c62e1SChris Mason root->last_log_commit = 0; 1559e02119d5SChris Mason return 0; 1560e02119d5SChris Mason } 1561e02119d5SChris Mason 156235a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1563cb517eabSMiao Xie struct btrfs_key *key) 1564e02119d5SChris Mason { 1565e02119d5SChris Mason struct btrfs_root *root; 1566e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 15670f7d52f4SChris Mason struct btrfs_path *path; 156884234f3aSYan Zheng u64 generation; 1569cb517eabSMiao Xie int ret; 1570cb517eabSMiao Xie 1571cb517eabSMiao Xie path = btrfs_alloc_path(); 1572cb517eabSMiao Xie if (!path) 1573cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 15740f7d52f4SChris Mason 157574e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1576cb517eabSMiao Xie if (!root) { 1577cb517eabSMiao Xie ret = -ENOMEM; 1578cb517eabSMiao Xie goto alloc_fail; 15790f7d52f4SChris Mason } 15800f7d52f4SChris Mason 1581da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 15820f7d52f4SChris Mason 1583cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1584cb517eabSMiao Xie &root->root_item, &root->root_key); 15850f7d52f4SChris Mason if (ret) { 158613a8a7c8SYan, Zheng if (ret > 0) 158713a8a7c8SYan, Zheng ret = -ENOENT; 1588cb517eabSMiao Xie goto find_fail; 15890f7d52f4SChris Mason } 159013a8a7c8SYan, Zheng 159184234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 15922ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15932ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1594ce86cd59SDavid Sterba generation); 159564c043deSLiu Bo if (IS_ERR(root->node)) { 159664c043deSLiu Bo ret = PTR_ERR(root->node); 1597cb517eabSMiao Xie goto find_fail; 1598cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1599cb517eabSMiao Xie ret = -EIO; 160064c043deSLiu Bo free_extent_buffer(root->node); 160164c043deSLiu Bo goto find_fail; 1602416bc658SJosef Bacik } 16035d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 160413a8a7c8SYan, Zheng out: 1605cb517eabSMiao Xie btrfs_free_path(path); 1606cb517eabSMiao Xie return root; 1607cb517eabSMiao Xie 1608cb517eabSMiao Xie find_fail: 1609cb517eabSMiao Xie kfree(root); 1610cb517eabSMiao Xie alloc_fail: 1611cb517eabSMiao Xie root = ERR_PTR(ret); 1612cb517eabSMiao Xie goto out; 1613cb517eabSMiao Xie } 1614cb517eabSMiao Xie 1615cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1616cb517eabSMiao Xie struct btrfs_key *location) 1617cb517eabSMiao Xie { 1618cb517eabSMiao Xie struct btrfs_root *root; 1619cb517eabSMiao Xie 1620cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1621cb517eabSMiao Xie if (IS_ERR(root)) 1622cb517eabSMiao Xie return root; 1623cb517eabSMiao Xie 1624cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 162527cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 162608fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 162708fe4db1SLi Zefan } 162813a8a7c8SYan, Zheng 16295eda7b5eSChris Mason return root; 16305eda7b5eSChris Mason } 16315eda7b5eSChris Mason 1632cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1633cb517eabSMiao Xie { 1634cb517eabSMiao Xie int ret; 16358257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1636cb517eabSMiao Xie 1637cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1638cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1639cb517eabSMiao Xie GFP_NOFS); 1640cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1641cb517eabSMiao Xie ret = -ENOMEM; 1642cb517eabSMiao Xie goto fail; 1643cb517eabSMiao Xie } 1644cb517eabSMiao Xie 16458257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 16468257b2dcSMiao Xie if (IS_ERR(writers)) { 16478257b2dcSMiao Xie ret = PTR_ERR(writers); 16488257b2dcSMiao Xie goto fail; 16498257b2dcSMiao Xie } 16508257b2dcSMiao Xie root->subv_writers = writers; 16518257b2dcSMiao Xie 1652cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 165357cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 165457cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1655cb517eabSMiao Xie 1656cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1657cb517eabSMiao Xie if (ret) 1658876d2cf1SLiu Bo goto fail; 1659f32e48e9SChandan Rajendra 1660f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1661f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1662f32e48e9SChandan Rajendra &root->highest_objectid); 1663f32e48e9SChandan Rajendra if (ret) { 1664f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1665876d2cf1SLiu Bo goto fail; 1666f32e48e9SChandan Rajendra } 1667f32e48e9SChandan Rajendra 1668f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1669f32e48e9SChandan Rajendra 1670f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1671f32e48e9SChandan Rajendra 1672cb517eabSMiao Xie return 0; 1673cb517eabSMiao Xie fail: 1674876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1675cb517eabSMiao Xie return ret; 1676cb517eabSMiao Xie } 1677cb517eabSMiao Xie 167835bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1679cb517eabSMiao Xie u64 root_id) 1680cb517eabSMiao Xie { 1681cb517eabSMiao Xie struct btrfs_root *root; 1682cb517eabSMiao Xie 1683cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1684cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1685cb517eabSMiao Xie (unsigned long)root_id); 1686cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1687cb517eabSMiao Xie return root; 1688cb517eabSMiao Xie } 1689cb517eabSMiao Xie 1690cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1691cb517eabSMiao Xie struct btrfs_root *root) 1692cb517eabSMiao Xie { 1693cb517eabSMiao Xie int ret; 1694cb517eabSMiao Xie 1695e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1696cb517eabSMiao Xie if (ret) 1697cb517eabSMiao Xie return ret; 1698cb517eabSMiao Xie 1699cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1700cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1701cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1702cb517eabSMiao Xie root); 1703cb517eabSMiao Xie if (ret == 0) 170427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1705cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1706cb517eabSMiao Xie radix_tree_preload_end(); 1707cb517eabSMiao Xie 1708cb517eabSMiao Xie return ret; 1709cb517eabSMiao Xie } 1710cb517eabSMiao Xie 1711c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1712c00869f1SMiao Xie struct btrfs_key *location, 1713c00869f1SMiao Xie bool check_ref) 17145eda7b5eSChris Mason { 17155eda7b5eSChris Mason struct btrfs_root *root; 1716381cf658SDavid Sterba struct btrfs_path *path; 17171d4c08e0SDavid Sterba struct btrfs_key key; 17185eda7b5eSChris Mason int ret; 17195eda7b5eSChris Mason 1720edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1721edbd8d4eSChris Mason return fs_info->tree_root; 1722edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1723edbd8d4eSChris Mason return fs_info->extent_root; 17248f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 17258f18cf13SChris Mason return fs_info->chunk_root; 17268f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 17278f18cf13SChris Mason return fs_info->dev_root; 17280403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 17290403e47eSYan Zheng return fs_info->csum_root; 1730bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1731bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1732bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1733f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1734f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1735f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 173670f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 173770f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 173870f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 17394df27c4dSYan, Zheng again: 1740cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 174148475471SStefan Behrens if (root) { 1742c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 174348475471SStefan Behrens return ERR_PTR(-ENOENT); 17445eda7b5eSChris Mason return root; 174548475471SStefan Behrens } 17465eda7b5eSChris Mason 1747cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 17485eda7b5eSChris Mason if (IS_ERR(root)) 17495eda7b5eSChris Mason return root; 17503394e160SChris Mason 1751c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1752d68fc57bSYan, Zheng ret = -ENOENT; 1753d68fc57bSYan, Zheng goto fail; 1754d68fc57bSYan, Zheng } 1755d68fc57bSYan, Zheng 1756cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1757cb517eabSMiao Xie if (ret) 1758cb517eabSMiao Xie goto fail; 1759cb517eabSMiao Xie 1760381cf658SDavid Sterba path = btrfs_alloc_path(); 1761381cf658SDavid Sterba if (!path) { 1762381cf658SDavid Sterba ret = -ENOMEM; 1763381cf658SDavid Sterba goto fail; 1764381cf658SDavid Sterba } 17651d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17661d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 17671d4c08e0SDavid Sterba key.offset = location->objectid; 17681d4c08e0SDavid Sterba 17691d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1770381cf658SDavid Sterba btrfs_free_path(path); 1771d68fc57bSYan, Zheng if (ret < 0) 1772d68fc57bSYan, Zheng goto fail; 1773d68fc57bSYan, Zheng if (ret == 0) 177427cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1775d68fc57bSYan, Zheng 1776cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17770f7d52f4SChris Mason if (ret) { 17784df27c4dSYan, Zheng if (ret == -EEXIST) { 17794df27c4dSYan, Zheng free_fs_root(root); 17804df27c4dSYan, Zheng goto again; 17810f7d52f4SChris Mason } 17824df27c4dSYan, Zheng goto fail; 17834df27c4dSYan, Zheng } 1784edbd8d4eSChris Mason return root; 17854df27c4dSYan, Zheng fail: 17864df27c4dSYan, Zheng free_fs_root(root); 17874df27c4dSYan, Zheng return ERR_PTR(ret); 1788edbd8d4eSChris Mason } 1789edbd8d4eSChris Mason 179004160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 179104160088SChris Mason { 179204160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 179304160088SChris Mason int ret = 0; 179404160088SChris Mason struct btrfs_device *device; 179504160088SChris Mason struct backing_dev_info *bdi; 1796b7967db7SChris Mason 17971f78160cSXiao Guangrong rcu_read_lock(); 17981f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1799dfe25020SChris Mason if (!device->bdev) 1800dfe25020SChris Mason continue; 1801efa7c9f9SJan Kara bdi = device->bdev->bd_bdi; 1802ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 180304160088SChris Mason ret = 1; 180404160088SChris Mason break; 180504160088SChris Mason } 180604160088SChris Mason } 18071f78160cSXiao Guangrong rcu_read_unlock(); 180804160088SChris Mason return ret; 180904160088SChris Mason } 181004160088SChris Mason 18118b712842SChris Mason /* 18128b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 18138b712842SChris Mason * functions. This is where read checksum verification actually happens 18148b712842SChris Mason */ 18158b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1816ce9adaa5SChris Mason { 1817ce9adaa5SChris Mason struct bio *bio; 181897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1819ce9adaa5SChris Mason 182097eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1821ce9adaa5SChris Mason bio = end_io_wq->bio; 18228b712842SChris Mason 18234246a0b6SChristoph Hellwig bio->bi_error = end_io_wq->error; 1824ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1825ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 182697eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 18274246a0b6SChristoph Hellwig bio_endio(bio); 1828ce9adaa5SChris Mason } 182944b8bd7eSChris Mason 1830a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1831a74a4b97SChris Mason { 1832a74a4b97SChris Mason struct btrfs_root *root = arg; 18330b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1834d0278245SMiao Xie int again; 1835da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1836a74a4b97SChris Mason 1837a74a4b97SChris Mason do { 1838d0278245SMiao Xie again = 0; 18399d1a2a3aSDavid Sterba 1840d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18412ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1842d0278245SMiao Xie goto sleep; 1843d0278245SMiao Xie 184490c711abSZygo Blaxell /* 184590c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 184690c711abSZygo Blaxell * before we have finished mounting the filesystem. 184790c711abSZygo Blaxell */ 18480b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 184990c711abSZygo Blaxell goto sleep; 185090c711abSZygo Blaxell 18510b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1852d0278245SMiao Xie goto sleep; 1853d0278245SMiao Xie 1854dc7f370cSMiao Xie /* 1855dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1856dc7f370cSMiao Xie * during the above check and trylock. 1857dc7f370cSMiao Xie */ 18582ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18590b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1860dc7f370cSMiao Xie goto sleep; 1861dc7f370cSMiao Xie } 1862dc7f370cSMiao Xie 18630b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 18642ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 18650b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1866c2d6cb16SFilipe Manana 18679d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 18680b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1869a74a4b97SChris Mason 1870d0278245SMiao Xie /* 187105323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 187205323cd1SMiao Xie * needn't do anything special here. 1873d0278245SMiao Xie */ 18740b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 187567c5e7d4SFilipe Manana 187667c5e7d4SFilipe Manana /* 187767c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 187867c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 187967c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 188067c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 188167c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 188267c5e7d4SFilipe Manana * unused block groups. 188367c5e7d4SFilipe Manana */ 18840b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1885d0278245SMiao Xie sleep: 1886838fe188SJiri Kosina if (!again) { 1887a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18888929ecfaSYan, Zheng if (!kthread_should_stop()) 1889a74a4b97SChris Mason schedule(); 1890a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1891a74a4b97SChris Mason } 1892a74a4b97SChris Mason } while (!kthread_should_stop()); 1893da288d28SFilipe Manana 1894da288d28SFilipe Manana /* 1895da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1896da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1897da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1898da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1899da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1900da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1901da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1902da288d28SFilipe Manana * since work queues and other resources were already released and 1903da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1904da288d28SFilipe Manana */ 1905da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1906da288d28SFilipe Manana if (IS_ERR(trans)) { 1907da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 19080b246afaSJeff Mahoney btrfs_err(fs_info, 1909da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1910da288d28SFilipe Manana PTR_ERR(trans)); 1911da288d28SFilipe Manana } else { 1912da288d28SFilipe Manana int ret; 1913da288d28SFilipe Manana 19143a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1915da288d28SFilipe Manana if (ret) 19160b246afaSJeff Mahoney btrfs_err(fs_info, 1917da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1918da288d28SFilipe Manana ret); 1919da288d28SFilipe Manana } 1920da288d28SFilipe Manana 1921a74a4b97SChris Mason return 0; 1922a74a4b97SChris Mason } 1923a74a4b97SChris Mason 1924a74a4b97SChris Mason static int transaction_kthread(void *arg) 1925a74a4b97SChris Mason { 1926a74a4b97SChris Mason struct btrfs_root *root = arg; 19270b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1928a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1929a74a4b97SChris Mason struct btrfs_transaction *cur; 19308929ecfaSYan, Zheng u64 transid; 1931a74a4b97SChris Mason unsigned long now; 1932a74a4b97SChris Mason unsigned long delay; 1933914b2007SJan Kara bool cannot_commit; 1934a74a4b97SChris Mason 1935a74a4b97SChris Mason do { 1936914b2007SJan Kara cannot_commit = false; 19370b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 19380b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1939a74a4b97SChris Mason 19400b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19410b246afaSJeff Mahoney cur = fs_info->running_transaction; 1942a74a4b97SChris Mason if (!cur) { 19430b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1944a74a4b97SChris Mason goto sleep; 1945a74a4b97SChris Mason } 194631153d81SYan Zheng 1947a74a4b97SChris Mason now = get_seconds(); 19484a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 19498b87dc17SDavid Sterba (now < cur->start_time || 19500b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 19510b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1952a74a4b97SChris Mason delay = HZ * 5; 1953a74a4b97SChris Mason goto sleep; 1954a74a4b97SChris Mason } 19558929ecfaSYan, Zheng transid = cur->transid; 19560b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 195756bec294SChris Mason 195879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1959354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1960914b2007SJan Kara if (IS_ERR(trans)) { 1961354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1962914b2007SJan Kara cannot_commit = true; 196379787eaaSJeff Mahoney goto sleep; 1964914b2007SJan Kara } 19658929ecfaSYan, Zheng if (transid == trans->transid) { 19663a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19678929ecfaSYan, Zheng } else { 19683a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19698929ecfaSYan, Zheng } 1970a74a4b97SChris Mason sleep: 19710b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19720b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1973a74a4b97SChris Mason 19744e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 19750b246afaSJeff Mahoney &fs_info->fs_state))) 19762ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 1977a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 19788929ecfaSYan, Zheng if (!kthread_should_stop() && 19790b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1980914b2007SJan Kara cannot_commit)) 1981a74a4b97SChris Mason schedule_timeout(delay); 1982a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1983a74a4b97SChris Mason } while (!kthread_should_stop()); 1984a74a4b97SChris Mason return 0; 1985a74a4b97SChris Mason } 1986a74a4b97SChris Mason 1987af31f5e5SChris Mason /* 1988af31f5e5SChris Mason * this will find the highest generation in the array of 1989af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1990af31f5e5SChris Mason * or -1 if we can't find anything. 1991af31f5e5SChris Mason * 1992af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1993af31f5e5SChris Mason * generation of the latest root in the array with the generation 1994af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1995af31f5e5SChris Mason */ 1996af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1997af31f5e5SChris Mason { 1998af31f5e5SChris Mason u64 cur; 1999af31f5e5SChris Mason int newest_index = -1; 2000af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2001af31f5e5SChris Mason int i; 2002af31f5e5SChris Mason 2003af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2004af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2005af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2006af31f5e5SChris Mason if (cur == newest_gen) 2007af31f5e5SChris Mason newest_index = i; 2008af31f5e5SChris Mason } 2009af31f5e5SChris Mason 2010af31f5e5SChris Mason /* check to see if we actually wrapped around */ 2011af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 2012af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 2013af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2014af31f5e5SChris Mason if (cur == newest_gen) 2015af31f5e5SChris Mason newest_index = 0; 2016af31f5e5SChris Mason } 2017af31f5e5SChris Mason return newest_index; 2018af31f5e5SChris Mason } 2019af31f5e5SChris Mason 2020af31f5e5SChris Mason 2021af31f5e5SChris Mason /* 2022af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 2023af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 2024af31f5e5SChris Mason * field in the fs_info struct 2025af31f5e5SChris Mason */ 2026af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 2027af31f5e5SChris Mason u64 newest_gen) 2028af31f5e5SChris Mason { 2029af31f5e5SChris Mason int newest_index = -1; 2030af31f5e5SChris Mason 2031af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 2032af31f5e5SChris Mason /* if there was garbage in there, just move along */ 2033af31f5e5SChris Mason if (newest_index == -1) { 2034af31f5e5SChris Mason info->backup_root_index = 0; 2035af31f5e5SChris Mason } else { 2036af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 2037af31f5e5SChris Mason } 2038af31f5e5SChris Mason } 2039af31f5e5SChris Mason 2040af31f5e5SChris Mason /* 2041af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2042af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2043af31f5e5SChris Mason * done 2044af31f5e5SChris Mason */ 2045af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2046af31f5e5SChris Mason { 2047af31f5e5SChris Mason int next_backup; 2048af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2049af31f5e5SChris Mason int last_backup; 2050af31f5e5SChris Mason 2051af31f5e5SChris Mason next_backup = info->backup_root_index; 2052af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 2053af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2054af31f5e5SChris Mason 2055af31f5e5SChris Mason /* 2056af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 2057af31f5e5SChris Mason * this happens only at umount 2058af31f5e5SChris Mason */ 2059af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 2060af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 2061af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 2062af31f5e5SChris Mason next_backup = last_backup; 2063af31f5e5SChris Mason 2064af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2065af31f5e5SChris Mason 2066af31f5e5SChris Mason /* 2067af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2068af31f5e5SChris Mason * regardless of which ones we use today 2069af31f5e5SChris Mason */ 2070af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2071af31f5e5SChris Mason 2072af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2073af31f5e5SChris Mason 2074af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2075af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2076af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2077af31f5e5SChris Mason 2078af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2079af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2080af31f5e5SChris Mason 2081af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2082af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2083af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2084af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2085af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2086af31f5e5SChris Mason 2087af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 2088af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 2089af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 2090af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 2091af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 2092af31f5e5SChris Mason 20937c7e82a7SChris Mason /* 20947c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20957c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20967c7e82a7SChris Mason */ 20977c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 20987c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 20997c7e82a7SChris Mason info->fs_root->node->start); 2100af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2101af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2102af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2103af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21047c7e82a7SChris Mason } 2105af31f5e5SChris Mason 2106af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2107af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2108af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2109af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2110af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2111af31f5e5SChris Mason 2112af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 2113af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 2114af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 2115af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 2116af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 2117af31f5e5SChris Mason 2118af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2119af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2120af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2121af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2122af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2123af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2124af31f5e5SChris Mason 2125af31f5e5SChris Mason /* 2126af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2127af31f5e5SChris Mason * for the next commit 2128af31f5e5SChris Mason */ 2129af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2130af31f5e5SChris Mason &info->super_for_commit->super_roots, 2131af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2132af31f5e5SChris Mason } 2133af31f5e5SChris Mason 2134af31f5e5SChris Mason /* 2135af31f5e5SChris Mason * this copies info out of the root backup array and back into 2136af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 2137af31f5e5SChris Mason * the array, so you send it the number of backups you've already 2138af31f5e5SChris Mason * tried and the last backup index you used. 2139af31f5e5SChris Mason * 2140af31f5e5SChris Mason * this returns -1 when it has tried all the backups 2141af31f5e5SChris Mason */ 2142af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 2143af31f5e5SChris Mason struct btrfs_super_block *super, 2144af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 2145af31f5e5SChris Mason { 2146af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2147af31f5e5SChris Mason int newest = *backup_index; 2148af31f5e5SChris Mason 2149af31f5e5SChris Mason if (*num_backups_tried == 0) { 2150af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 2151af31f5e5SChris Mason 2152af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 2153af31f5e5SChris Mason if (newest == -1) 2154af31f5e5SChris Mason return -1; 2155af31f5e5SChris Mason 2156af31f5e5SChris Mason *backup_index = newest; 2157af31f5e5SChris Mason *num_backups_tried = 1; 2158af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2159af31f5e5SChris Mason /* we've tried all the backups, all done */ 2160af31f5e5SChris Mason return -1; 2161af31f5e5SChris Mason } else { 2162af31f5e5SChris Mason /* jump to the next oldest backup */ 2163af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2164af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2165af31f5e5SChris Mason *backup_index = newest; 2166af31f5e5SChris Mason *num_backups_tried += 1; 2167af31f5e5SChris Mason } 2168af31f5e5SChris Mason root_backup = super->super_roots + newest; 2169af31f5e5SChris Mason 2170af31f5e5SChris Mason btrfs_set_super_generation(super, 2171af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2172af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2173af31f5e5SChris Mason btrfs_set_super_root_level(super, 2174af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2175af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2176af31f5e5SChris Mason 2177af31f5e5SChris Mason /* 2178af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2179af31f5e5SChris Mason * need a fsck 2180af31f5e5SChris Mason */ 2181af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2182af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2183af31f5e5SChris Mason return 0; 2184af31f5e5SChris Mason } 2185af31f5e5SChris Mason 21867abadb64SLiu Bo /* helper to cleanup workers */ 21877abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21887abadb64SLiu Bo { 2189dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2190afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 21915cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2192fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2193fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 21948b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2195d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2196fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2197fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2198a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 21995b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2200e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2201736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2202a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2203fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2204a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 2205a9b9477dSFilipe Manana /* 2206a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy 2207a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work 2208a9b9477dSFilipe Manana * queues can do metadata I/O operations. 2209a9b9477dSFilipe Manana */ 2210a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2211a9b9477dSFilipe Manana btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 22127abadb64SLiu Bo } 22137abadb64SLiu Bo 22142e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22152e9f5954SRashika { 22162e9f5954SRashika if (root) { 22172e9f5954SRashika free_extent_buffer(root->node); 22182e9f5954SRashika free_extent_buffer(root->commit_root); 22192e9f5954SRashika root->node = NULL; 22202e9f5954SRashika root->commit_root = NULL; 22212e9f5954SRashika } 22222e9f5954SRashika } 22232e9f5954SRashika 2224af31f5e5SChris Mason /* helper to cleanup tree roots */ 2225af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2226af31f5e5SChris Mason { 22272e9f5954SRashika free_root_extent_buffers(info->tree_root); 2228655b09feSJosef Bacik 22292e9f5954SRashika free_root_extent_buffers(info->dev_root); 22302e9f5954SRashika free_root_extent_buffers(info->extent_root); 22312e9f5954SRashika free_root_extent_buffers(info->csum_root); 22322e9f5954SRashika free_root_extent_buffers(info->quota_root); 22332e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22342e9f5954SRashika if (chunk_root) 22352e9f5954SRashika free_root_extent_buffers(info->chunk_root); 223670f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2237af31f5e5SChris Mason } 2238af31f5e5SChris Mason 2239faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2240171f6537SJosef Bacik { 2241171f6537SJosef Bacik int ret; 2242171f6537SJosef Bacik struct btrfs_root *gang[8]; 2243171f6537SJosef Bacik int i; 2244171f6537SJosef Bacik 2245171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2246171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2247171f6537SJosef Bacik struct btrfs_root, root_list); 2248171f6537SJosef Bacik list_del(&gang[0]->root_list); 2249171f6537SJosef Bacik 225027cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2251cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2252171f6537SJosef Bacik } else { 2253171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2254171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2255b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2256171f6537SJosef Bacik } 2257171f6537SJosef Bacik } 2258171f6537SJosef Bacik 2259171f6537SJosef Bacik while (1) { 2260171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2261171f6537SJosef Bacik (void **)gang, 0, 2262171f6537SJosef Bacik ARRAY_SIZE(gang)); 2263171f6537SJosef Bacik if (!ret) 2264171f6537SJosef Bacik break; 2265171f6537SJosef Bacik for (i = 0; i < ret; i++) 2266cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2267171f6537SJosef Bacik } 22681a4319ccSLiu Bo 22691a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 22701a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 22712ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 22721a4319ccSLiu Bo } 2273171f6537SJosef Bacik } 2274af31f5e5SChris Mason 2275638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2276638aa7edSEric Sandeen { 2277638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2278638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2279638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2280638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2281638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2282638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2283638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2284638aa7edSEric Sandeen } 2285638aa7edSEric Sandeen 2286779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2287779a65a4SEric Sandeen { 2288779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2289779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2290779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2291779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2292779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2293779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2294779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2295779a65a4SEric Sandeen } 2296779a65a4SEric Sandeen 22976bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2298f37938e0SEric Sandeen { 22992ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23002ff7e61eSJeff Mahoney 23012ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23022ff7e61eSJeff Mahoney set_nlink(inode, 1); 2303f37938e0SEric Sandeen /* 2304f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2305f37938e0SEric Sandeen * the real end of the address space is determined by all of 2306f37938e0SEric Sandeen * the devices in the system 2307f37938e0SEric Sandeen */ 23082ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23092ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2310f37938e0SEric Sandeen 23112ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 2312c6100a4bSJosef Bacik extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode); 23132ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 23142ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2315f37938e0SEric Sandeen 23162ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2317f37938e0SEric Sandeen 23182ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 23192ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 23202ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 23212ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2322f37938e0SEric Sandeen } 2323f37938e0SEric Sandeen 2324ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2325ad618368SEric Sandeen { 2326ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2327ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2328ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 232973beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 233073beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 233173beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2332ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 233373beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2334ad618368SEric Sandeen } 2335ad618368SEric Sandeen 2336f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2337f9e92e40SEric Sandeen { 2338f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2339f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2340f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2341f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2342f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2343f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2344f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2345d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2346f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2347f9e92e40SEric Sandeen } 2348f9e92e40SEric Sandeen 23492a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 23502a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 23512a458198SEric Sandeen { 23522a458198SEric Sandeen int max_active = fs_info->thread_pool_size; 23536f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23542a458198SEric Sandeen 23552a458198SEric Sandeen fs_info->workers = 2356cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2357cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23582a458198SEric Sandeen 23592a458198SEric Sandeen fs_info->delalloc_workers = 2360cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2361cb001095SJeff Mahoney flags, max_active, 2); 23622a458198SEric Sandeen 23632a458198SEric Sandeen fs_info->flush_workers = 2364cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2365cb001095SJeff Mahoney flags, max_active, 0); 23662a458198SEric Sandeen 23672a458198SEric Sandeen fs_info->caching_workers = 2368cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23692a458198SEric Sandeen 23702a458198SEric Sandeen /* 23712a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 23722a458198SEric Sandeen * likely that bios will be send down in a sane order to the 23732a458198SEric Sandeen * devices 23742a458198SEric Sandeen */ 23752a458198SEric Sandeen fs_info->submit_workers = 2376cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 23772a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 23782a458198SEric Sandeen max_active), 64); 23792a458198SEric Sandeen 23802a458198SEric Sandeen fs_info->fixup_workers = 2381cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23822a458198SEric Sandeen 23832a458198SEric Sandeen /* 23842a458198SEric Sandeen * endios are largely parallel and should have a very 23852a458198SEric Sandeen * low idle thresh 23862a458198SEric Sandeen */ 23872a458198SEric Sandeen fs_info->endio_workers = 2388cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 23892a458198SEric Sandeen fs_info->endio_meta_workers = 2390cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2391cb001095SJeff Mahoney max_active, 4); 23922a458198SEric Sandeen fs_info->endio_meta_write_workers = 2393cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2394cb001095SJeff Mahoney max_active, 2); 23952a458198SEric Sandeen fs_info->endio_raid56_workers = 2396cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2397cb001095SJeff Mahoney max_active, 4); 23982a458198SEric Sandeen fs_info->endio_repair_workers = 2399cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 24002a458198SEric Sandeen fs_info->rmw_workers = 2401cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 24022a458198SEric Sandeen fs_info->endio_write_workers = 2403cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2404cb001095SJeff Mahoney max_active, 2); 24052a458198SEric Sandeen fs_info->endio_freespace_worker = 2406cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2407cb001095SJeff Mahoney max_active, 0); 24082a458198SEric Sandeen fs_info->delayed_workers = 2409cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2410cb001095SJeff Mahoney max_active, 0); 24112a458198SEric Sandeen fs_info->readahead_workers = 2412cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2413cb001095SJeff Mahoney max_active, 2); 24142a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2415cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 24162a458198SEric Sandeen fs_info->extent_workers = 2417cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 24182a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 24192a458198SEric Sandeen max_active), 8); 24202a458198SEric Sandeen 24212a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 24222a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 24232a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 24242a458198SEric Sandeen fs_info->endio_meta_write_workers && 24252a458198SEric Sandeen fs_info->endio_repair_workers && 24262a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24272a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 24282a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 24292a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 24302a458198SEric Sandeen fs_info->extent_workers && 24312a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 24322a458198SEric Sandeen return -ENOMEM; 24332a458198SEric Sandeen } 24342a458198SEric Sandeen 24352a458198SEric Sandeen return 0; 24362a458198SEric Sandeen } 24372a458198SEric Sandeen 243863443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 243963443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 244063443bf5SEric Sandeen { 244163443bf5SEric Sandeen int ret; 244263443bf5SEric Sandeen struct btrfs_root *log_tree_root; 244363443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 244463443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 244563443bf5SEric Sandeen 244663443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2447f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 244863443bf5SEric Sandeen return -EIO; 244963443bf5SEric Sandeen } 245063443bf5SEric Sandeen 245174e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 245263443bf5SEric Sandeen if (!log_tree_root) 245363443bf5SEric Sandeen return -ENOMEM; 245463443bf5SEric Sandeen 2455da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 245663443bf5SEric Sandeen 24572ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 245863443bf5SEric Sandeen fs_info->generation + 1); 245964c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2460f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24610eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 246264c043deSLiu Bo kfree(log_tree_root); 24630eeff236SLiu Bo return ret; 246464c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2465f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 246663443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 246763443bf5SEric Sandeen kfree(log_tree_root); 246863443bf5SEric Sandeen return -EIO; 246963443bf5SEric Sandeen } 247063443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 247163443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 247263443bf5SEric Sandeen if (ret) { 24730b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 247463443bf5SEric Sandeen "Failed to recover log tree"); 247563443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 247663443bf5SEric Sandeen kfree(log_tree_root); 247763443bf5SEric Sandeen return ret; 247863443bf5SEric Sandeen } 247963443bf5SEric Sandeen 248063443bf5SEric Sandeen if (fs_info->sb->s_flags & MS_RDONLY) { 24816bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 248263443bf5SEric Sandeen if (ret) 248363443bf5SEric Sandeen return ret; 248463443bf5SEric Sandeen } 248563443bf5SEric Sandeen 248663443bf5SEric Sandeen return 0; 248763443bf5SEric Sandeen } 248863443bf5SEric Sandeen 24896bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 24904bbcaa64SEric Sandeen { 24916bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2492a4f3d2c4SDavid Sterba struct btrfs_root *root; 24934bbcaa64SEric Sandeen struct btrfs_key location; 24944bbcaa64SEric Sandeen int ret; 24954bbcaa64SEric Sandeen 24966bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 24976bccf3abSJeff Mahoney 24984bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 24994bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 25004bbcaa64SEric Sandeen location.offset = 0; 25014bbcaa64SEric Sandeen 2502a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2503a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2504a4f3d2c4SDavid Sterba return PTR_ERR(root); 2505a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2506a4f3d2c4SDavid Sterba fs_info->extent_root = root; 25074bbcaa64SEric Sandeen 25084bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2509a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2510a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2511a4f3d2c4SDavid Sterba return PTR_ERR(root); 2512a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2513a4f3d2c4SDavid Sterba fs_info->dev_root = root; 25144bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 25154bbcaa64SEric Sandeen 25164bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2517a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2518a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2519a4f3d2c4SDavid Sterba return PTR_ERR(root); 2520a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2521a4f3d2c4SDavid Sterba fs_info->csum_root = root; 25224bbcaa64SEric Sandeen 25234bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2524a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2525a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2526a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2527afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2528a4f3d2c4SDavid Sterba fs_info->quota_root = root; 25294bbcaa64SEric Sandeen } 25304bbcaa64SEric Sandeen 25314bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2532a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2533a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2534a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 25354bbcaa64SEric Sandeen if (ret != -ENOENT) 25364bbcaa64SEric Sandeen return ret; 25374bbcaa64SEric Sandeen } else { 2538a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2539a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 25404bbcaa64SEric Sandeen } 25414bbcaa64SEric Sandeen 254270f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 254370f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 254470f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 254570f6d82eSOmar Sandoval if (IS_ERR(root)) 254670f6d82eSOmar Sandoval return PTR_ERR(root); 254770f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 254870f6d82eSOmar Sandoval fs_info->free_space_root = root; 254970f6d82eSOmar Sandoval } 255070f6d82eSOmar Sandoval 25514bbcaa64SEric Sandeen return 0; 25524bbcaa64SEric Sandeen } 25534bbcaa64SEric Sandeen 2554ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2555dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2556dfe25020SChris Mason char *options) 2557eb60ceacSChris Mason { 2558db94535dSChris Mason u32 sectorsize; 2559db94535dSChris Mason u32 nodesize; 256087ee04ebSChris Mason u32 stripesize; 256184234f3aSYan Zheng u64 generation; 2562f2b636e8SJosef Bacik u64 features; 25633de4586cSChris Mason struct btrfs_key location; 2564a061fc8dSChris Mason struct buffer_head *bh; 25654d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2566815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2567f84a8bd6SAl Viro struct btrfs_root *tree_root; 25684d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2569eb60ceacSChris Mason int ret; 2570e58ca020SYan int err = -EINVAL; 2571af31f5e5SChris Mason int num_backups_tried = 0; 2572af31f5e5SChris Mason int backup_index = 0; 25735cdc7ad3SQu Wenruo int max_active; 25746675df31SOmar Sandoval int clear_free_space_tree = 0; 25754543df7eSChris Mason 257674e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 257774e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2578cb517eabSMiao Xie if (!tree_root || !chunk_root) { 257939279cc3SChris Mason err = -ENOMEM; 258039279cc3SChris Mason goto fail; 258139279cc3SChris Mason } 258276dda93cSYan, Zheng 258376dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 258476dda93cSYan, Zheng if (ret) { 258576dda93cSYan, Zheng err = ret; 258676dda93cSYan, Zheng goto fail; 258776dda93cSYan, Zheng } 258876dda93cSYan, Zheng 2589908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2590e2d84521SMiao Xie if (ret) { 2591e2d84521SMiao Xie err = ret; 25929e11ceeeSJan Kara goto fail_srcu; 2593e2d84521SMiao Xie } 259409cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2595e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2596e2d84521SMiao Xie 2597908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2598963d678bSMiao Xie if (ret) { 2599963d678bSMiao Xie err = ret; 2600963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2601963d678bSMiao Xie } 2602963d678bSMiao Xie 2603908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2604c404e0dcSMiao Xie if (ret) { 2605c404e0dcSMiao Xie err = ret; 2606c404e0dcSMiao Xie goto fail_delalloc_bytes; 2607c404e0dcSMiao Xie } 2608c404e0dcSMiao Xie 260976dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 261076dda93cSYan, Zheng if (!fs_info->btree_inode) { 261176dda93cSYan, Zheng err = -ENOMEM; 2612c404e0dcSMiao Xie goto fail_bio_counter; 261376dda93cSYan, Zheng } 261476dda93cSYan, Zheng 2615a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 26161561dedaSMiao Xie 261776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2618f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26198fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2620facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 262124bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2622eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 262311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2624eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2625a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 262676dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 262724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26284cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 2629f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2630ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2631fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2632f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 263347ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2634f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2635d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 263667c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26377585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2638573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2639c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2640de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 264119c00ddcSChris Mason 26420b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26436324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2644f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 264547ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 26460b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 264766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 264866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 264966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 265066d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 265166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 265266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 265366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 265466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 265566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2656cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2657771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 26588c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 26590986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 26604cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2661fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 26622fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2663fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 26649e7cc91aSWang Xiaoguang fs_info->fs_frozen = 0; 2665e20d96d6SChris Mason fs_info->sb = sb; 266695ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 26679ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 26684cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2669a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0); 2670f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 26718b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2672f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 267390519d66SArne Jansen /* readahead state */ 2674d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 267590519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2676c8b97818SChris Mason 2677b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2678b34b086cSChris Mason num_online_cpus() + 2, 8); 26790afbaf8cSChris Mason 2680199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2681199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 268216cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 268374e4d827SDavid Sterba GFP_KERNEL); 268416cdcec7SMiao Xie if (!fs_info->delayed_root) { 268516cdcec7SMiao Xie err = -ENOMEM; 268616cdcec7SMiao Xie goto fail_iput; 268716cdcec7SMiao Xie } 268816cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 26893eaa2885SChris Mason 2690638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 269121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 269221adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 269321adbd5cSStefan Behrens #endif 2694779a65a4SEric Sandeen btrfs_init_balance(fs_info); 269521c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2696a2de733cSArne Jansen 2697a061fc8dSChris Mason sb->s_blocksize = 4096; 2698a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 2699a061fc8dSChris Mason 27006bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 270139279cc3SChris Mason 2702e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 27036bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2704a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2705925baeddSChris Mason 2706c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[0], NULL); 2707c6100a4bSJosef Bacik extent_io_tree_init(&fs_info->freed_extents[1], NULL); 270811833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2709afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2710509659cdSChris Mason 27115a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 271279154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2713925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2714a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2715a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27167d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 27171bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27189e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2719c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 272076dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2721803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2722fa9c0d79SChris Mason 2723ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2724f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2725416ac51dSArne Jansen 2726fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2727fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2728fa9c0d79SChris Mason 2729e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2730f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2731bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27324854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 27333768f368SChris Mason 273404216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 273504216820SFilipe Manana 2736da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2737da17066cSJeff Mahoney fs_info->nodesize = 4096; 2738da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2739da17066cSJeff Mahoney fs_info->stripesize = 4096; 2740da17066cSJeff Mahoney 274153b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 274253b381b3SDavid Woodhouse if (ret) { 274383c8266aSDavid Sterba err = ret; 274453b381b3SDavid Woodhouse goto fail_alloc; 274553b381b3SDavid Woodhouse } 274653b381b3SDavid Woodhouse 2747da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 27487eccb903SChris Mason 27493c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 27501104a885SDavid Sterba 27511104a885SDavid Sterba /* 27521104a885SDavid Sterba * Read super block and check the signature bytes only 27531104a885SDavid Sterba */ 2754a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 275592fc03fbSAnand Jain if (IS_ERR(bh)) { 275692fc03fbSAnand Jain err = PTR_ERR(bh); 275716cdcec7SMiao Xie goto fail_alloc; 275820b45077SDave Young } 275939279cc3SChris Mason 27601104a885SDavid Sterba /* 27611104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 27621104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 27631104a885SDavid Sterba */ 2764ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 276505135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 27661104a885SDavid Sterba err = -EINVAL; 2767b2acdddfSAnand Jain brelse(bh); 27681104a885SDavid Sterba goto fail_alloc; 27691104a885SDavid Sterba } 27701104a885SDavid Sterba 27711104a885SDavid Sterba /* 27721104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 27731104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 27741104a885SDavid Sterba * the whole block of INFO_SIZE 27751104a885SDavid Sterba */ 27766c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 27776c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 27786c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2779a061fc8dSChris Mason brelse(bh); 27805f39d397SChris Mason 27816c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 27820b86a832SChris Mason 27833d3a126aSDavid Sterba ret = btrfs_check_super_valid(fs_info); 27841104a885SDavid Sterba if (ret) { 278505135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 27861104a885SDavid Sterba err = -EINVAL; 27871104a885SDavid Sterba goto fail_alloc; 27881104a885SDavid Sterba } 27891104a885SDavid Sterba 27906c41761fSDavid Sterba disk_super = fs_info->super_copy; 27910f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 279216cdcec7SMiao Xie goto fail_alloc; 27930f7d52f4SChris Mason 2794acce952bSliubo /* check FS state, whether FS is broken. */ 279587533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 279687533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2797acce952bSliubo 279875e7cb7fSLiu Bo /* 2799af31f5e5SChris Mason * run through our array of backup supers and setup 2800af31f5e5SChris Mason * our ring pointer to the oldest one 2801af31f5e5SChris Mason */ 2802af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2803af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2804af31f5e5SChris Mason 2805af31f5e5SChris Mason /* 280675e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 280775e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 280875e7cb7fSLiu Bo */ 280975e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 281075e7cb7fSLiu Bo 28112ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 28122b82032cSYan Zheng if (ret) { 28132b82032cSYan Zheng err = ret; 281416cdcec7SMiao Xie goto fail_alloc; 28152b82032cSYan Zheng } 2816dfe25020SChris Mason 2817f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2818f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2819f2b636e8SJosef Bacik if (features) { 282005135f59SDavid Sterba btrfs_err(fs_info, 282105135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2822c1c9ff7cSGeert Uytterhoeven features); 2823f2b636e8SJosef Bacik err = -EINVAL; 282416cdcec7SMiao Xie goto fail_alloc; 2825f2b636e8SJosef Bacik } 2826f2b636e8SJosef Bacik 28275d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 28285d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 28290b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2830a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2831727011e0SChris Mason 28323173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 283305135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 28343173a18fSJosef Bacik 2835727011e0SChris Mason /* 2836727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2837727011e0SChris Mason * they are bigger than the page size 2838727011e0SChris Mason */ 283909cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2840727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 284105135f59SDavid Sterba btrfs_info(fs_info, 284205135f59SDavid Sterba "flagging fs with big metadata feature"); 2843727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2844727011e0SChris Mason } 2845727011e0SChris Mason 2846bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2847bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2848b7f67055SChandan Rajendra stripesize = sectorsize; 2849707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2850963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2851bc3f116fSChris Mason 2852da17066cSJeff Mahoney /* Cache block sizes */ 2853da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2854da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2855da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2856da17066cSJeff Mahoney 2857bc3f116fSChris Mason /* 2858bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2859bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2860bc3f116fSChris Mason */ 2861bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2862707e8a07SDavid Sterba (sectorsize != nodesize)) { 286305135f59SDavid Sterba btrfs_err(fs_info, 286405135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 286505135f59SDavid Sterba nodesize, sectorsize); 2866bc3f116fSChris Mason goto fail_alloc; 2867bc3f116fSChris Mason } 2868bc3f116fSChris Mason 2869ceda0864SMiao Xie /* 2870ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2871ceda0864SMiao Xie * update the flag. 2872ceda0864SMiao Xie */ 28735d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 28745d4f98a2SYan Zheng 2875f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2876f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2877f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 287805135f59SDavid Sterba btrfs_err(fs_info, 287905135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2880c1c9ff7cSGeert Uytterhoeven features); 2881f2b636e8SJosef Bacik err = -EINVAL; 288216cdcec7SMiao Xie goto fail_alloc; 2883f2b636e8SJosef Bacik } 288461d92c32SChris Mason 28855cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 288661d92c32SChris Mason 28872a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 28882a458198SEric Sandeen if (ret) { 28892a458198SEric Sandeen err = ret; 28900dc3b84aSJosef Bacik goto fail_sb_buffer; 28910dc3b84aSJosef Bacik } 28924543df7eSChris Mason 28939e11ceeeSJan Kara sb->s_bdi->congested_fn = btrfs_congested_fn; 28949e11ceeeSJan Kara sb->s_bdi->congested_data = fs_info; 28959e11ceeeSJan Kara sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 28969e11ceeeSJan Kara sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE; 28979e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super); 28989e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE); 28994575c9ccSChris Mason 2900a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2901a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2902db94535dSChris Mason 2903925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 29046bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2905925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 290684eed90fSChris Mason if (ret) { 290705135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 29085d4f98a2SYan Zheng goto fail_sb_buffer; 290984eed90fSChris Mason } 29100b86a832SChris Mason 291184234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 29120b86a832SChris Mason 2913da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 29140b86a832SChris Mason 29152ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 29160b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2917ce86cd59SDavid Sterba generation); 291864c043deSLiu Bo if (IS_ERR(chunk_root->node) || 291964c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 292005135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2921e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2922e5fffbacSchandan free_extent_buffer(chunk_root->node); 292395ab1f64SZhao Lei chunk_root->node = NULL; 2924af31f5e5SChris Mason goto fail_tree_roots; 292583121942SDavid Woodhouse } 29265d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 29275d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 29280b86a832SChris Mason 2929e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2930b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2931e17cade2SChris Mason 29325b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 29332b82032cSYan Zheng if (ret) { 293405135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2935af31f5e5SChris Mason goto fail_tree_roots; 29362b82032cSYan Zheng } 29370b86a832SChris Mason 29388dabb742SStefan Behrens /* 29398dabb742SStefan Behrens * keep the device that is marked to be the target device for the 29408dabb742SStefan Behrens * dev_replace procedure 29418dabb742SStefan Behrens */ 29429eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 0); 2943dfe25020SChris Mason 2944a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 294505135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2946a6b0d5c8SChris Mason goto fail_tree_roots; 2947a6b0d5c8SChris Mason } 2948a6b0d5c8SChris Mason 2949af31f5e5SChris Mason retry_root_backup: 295084234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 29510b86a832SChris Mason 29522ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2953db94535dSChris Mason btrfs_super_root(disk_super), 2954ce86cd59SDavid Sterba generation); 295564c043deSLiu Bo if (IS_ERR(tree_root->node) || 295664c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 295705135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2958e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2959e5fffbacSchandan free_extent_buffer(tree_root->node); 296095ab1f64SZhao Lei tree_root->node = NULL; 2961af31f5e5SChris Mason goto recovery_tree_root; 296283121942SDavid Woodhouse } 2963af31f5e5SChris Mason 29645d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 29655d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 296669e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2967db94535dSChris Mason 2968f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2969f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2970f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2971f32e48e9SChandan Rajendra if (ret) { 2972f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2973f32e48e9SChandan Rajendra goto recovery_tree_root; 2974f32e48e9SChandan Rajendra } 2975f32e48e9SChandan Rajendra 2976f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2977f32e48e9SChandan Rajendra 2978f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2979f32e48e9SChandan Rajendra 29806bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 29814bbcaa64SEric Sandeen if (ret) 2982af31f5e5SChris Mason goto recovery_tree_root; 2983f7a81ea4SStefan Behrens 29848929ecfaSYan, Zheng fs_info->generation = generation; 29858929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 29868929ecfaSYan, Zheng 298768310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 298868310a5eSIlya Dryomov if (ret) { 298905135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 299068310a5eSIlya Dryomov goto fail_block_groups; 299168310a5eSIlya Dryomov } 299268310a5eSIlya Dryomov 2993733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2994733f4fbbSStefan Behrens if (ret) { 299505135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 2996733f4fbbSStefan Behrens goto fail_block_groups; 2997733f4fbbSStefan Behrens } 2998733f4fbbSStefan Behrens 29998dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 30008dabb742SStefan Behrens if (ret) { 300105135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 30028dabb742SStefan Behrens goto fail_block_groups; 30038dabb742SStefan Behrens } 30048dabb742SStefan Behrens 30059eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 1); 30068dabb742SStefan Behrens 3007b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 3008b7c35e81SAnand Jain if (ret) { 300905135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 301005135f59SDavid Sterba ret); 3011b7c35e81SAnand Jain goto fail_block_groups; 3012b7c35e81SAnand Jain } 3013b7c35e81SAnand Jain 3014b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 3015b7c35e81SAnand Jain if (ret) { 301605135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 301705135f59SDavid Sterba ret); 3018b7c35e81SAnand Jain goto fail_fsdev_sysfs; 3019b7c35e81SAnand Jain } 3020b7c35e81SAnand Jain 302196f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 30225ac1d209SJeff Mahoney if (ret) { 302305135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3024b7c35e81SAnand Jain goto fail_fsdev_sysfs; 30255ac1d209SJeff Mahoney } 30265ac1d209SJeff Mahoney 3027c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3028c59021f8Sliubo if (ret) { 302905135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 30302365dd3cSAnand Jain goto fail_sysfs; 3031c59021f8Sliubo } 3032c59021f8Sliubo 30335b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 30341b1d1f66SJosef Bacik if (ret) { 303505135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 30362365dd3cSAnand Jain goto fail_sysfs; 30371b1d1f66SJosef Bacik } 30385af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 30395af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 3040292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 3041292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 3042292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 304305135f59SDavid Sterba btrfs_warn(fs_info, 304405135f59SDavid Sterba "missing devices (%llu) exceeds the limit (%d), writeable mount is not allowed", 304578fa1770SZhao Lei fs_info->fs_devices->missing_devices, 304678fa1770SZhao Lei fs_info->num_tolerated_disk_barrier_failures); 30472365dd3cSAnand Jain goto fail_sysfs; 3048292fd7fcSStefan Behrens } 30499078a3e1SChris Mason 3050a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3051a74a4b97SChris Mason "btrfs-cleaner"); 305257506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 30532365dd3cSAnand Jain goto fail_sysfs; 3054a74a4b97SChris Mason 3055a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3056a74a4b97SChris Mason tree_root, 3057a74a4b97SChris Mason "btrfs-transaction"); 305857506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 30593f157a2fSChris Mason goto fail_cleaner; 3060a74a4b97SChris Mason 30610b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, SSD) && 30620b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOSSD) && 3063c289811cSChris Mason !fs_info->fs_devices->rotating) { 306405135f59SDavid Sterba btrfs_info(fs_info, "detected SSD devices, enabling SSD mode"); 3065c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 3066c289811cSChris Mason } 3067c289811cSChris Mason 3068572d9ab7SDavid Sterba /* 306901327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3070572d9ab7SDavid Sterba * not have to wait for transaction commit 3071572d9ab7SDavid Sterba */ 3072572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 30733818aea2SQu Wenruo 307421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 30750b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 30762ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 30770b246afaSJeff Mahoney btrfs_test_opt(fs_info, 307821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 307921adbd5cSStefan Behrens 1 : 0, 308021adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 308121adbd5cSStefan Behrens if (ret) 308205135f59SDavid Sterba btrfs_warn(fs_info, 308305135f59SDavid Sterba "failed to initialize integrity check module: %d", 308405135f59SDavid Sterba ret); 308521adbd5cSStefan Behrens } 308621adbd5cSStefan Behrens #endif 3087bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3088bcef60f2SArne Jansen if (ret) 3089bcef60f2SArne Jansen goto fail_trans_kthread; 309021adbd5cSStefan Behrens 309196da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 309296da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 30930b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 309463443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 309579787eaaSJeff Mahoney if (ret) { 309663443bf5SEric Sandeen err = ret; 309728c16cbbSWang Shilong goto fail_qgroup; 3098e556ce2cSYan Zheng } 3099e02119d5SChris Mason } 31001a40e23bSZheng Yan 31016bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 310279787eaaSJeff Mahoney if (ret) 310328c16cbbSWang Shilong goto fail_qgroup; 310476dda93cSYan, Zheng 31057c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 3106d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 310744c44af2SIlya Dryomov if (ret) 310828c16cbbSWang Shilong goto fail_qgroup; 310990c711abSZygo Blaxell 311090c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 31115d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 311290c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3113d7ce5843SMiao Xie if (ret < 0) { 311405135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 311505135f59SDavid Sterba ret); 3116d7ce5843SMiao Xie err = -EINVAL; 3117bcef60f2SArne Jansen goto fail_qgroup; 3118d7ce5843SMiao Xie } 31197c2ca468SChris Mason } 31201a40e23bSZheng Yan 31213de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 31223de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3123cb517eabSMiao Xie location.offset = 0; 31243de4586cSChris Mason 31253de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 31263140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 31273140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3128bcef60f2SArne Jansen goto fail_qgroup; 31293140c9a3SDan Carpenter } 3130c289811cSChris Mason 31312b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 31322b6ba629SIlya Dryomov return 0; 31332b6ba629SIlya Dryomov 3134f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3135f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 31366675df31SOmar Sandoval clear_free_space_tree = 1; 31376675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 31386675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 31396675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 31406675df31SOmar Sandoval clear_free_space_tree = 1; 31416675df31SOmar Sandoval } 31426675df31SOmar Sandoval 31436675df31SOmar Sandoval if (clear_free_space_tree) { 3144f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3145f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3146f8d468a1SOmar Sandoval if (ret) { 3147f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3148f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 31496bccf3abSJeff Mahoney close_ctree(fs_info); 3150f8d468a1SOmar Sandoval return ret; 3151f8d468a1SOmar Sandoval } 3152f8d468a1SOmar Sandoval } 3153f8d468a1SOmar Sandoval 31540b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3155511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 315605135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3157511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3158511711afSChris Mason if (ret) { 315905135f59SDavid Sterba btrfs_warn(fs_info, 316005135f59SDavid Sterba "failed to create free space tree: %d", ret); 31616bccf3abSJeff Mahoney close_ctree(fs_info); 3162511711afSChris Mason return ret; 3163511711afSChris Mason } 3164511711afSChris Mason } 3165511711afSChris Mason 3166e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 31672b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 31682b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 31692b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 31706bccf3abSJeff Mahoney close_ctree(fs_info); 31712b6ba629SIlya Dryomov return ret; 31722b6ba629SIlya Dryomov } 3173e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 317459641015SIlya Dryomov 31752b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 31762b6ba629SIlya Dryomov if (ret) { 317705135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 31786bccf3abSJeff Mahoney close_ctree(fs_info); 31792b6ba629SIlya Dryomov return ret; 3180e3acc2a6SJosef Bacik } 3181e3acc2a6SJosef Bacik 31828dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 31838dabb742SStefan Behrens if (ret) { 318405135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 31856bccf3abSJeff Mahoney close_ctree(fs_info); 31868dabb742SStefan Behrens return ret; 31878dabb742SStefan Behrens } 31888dabb742SStefan Behrens 3189b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3190b382a324SJan Schmidt 31914bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 319205135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3193f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3194f7a81ea4SStefan Behrens if (ret) { 319505135f59SDavid Sterba btrfs_warn(fs_info, 319605135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 31976bccf3abSJeff Mahoney close_ctree(fs_info); 3198f7a81ea4SStefan Behrens return ret; 3199f7a81ea4SStefan Behrens } 32000b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 32014bbcaa64SEric Sandeen fs_info->generation != 32024bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 320305135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 320470f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 320570f80175SStefan Behrens if (ret) { 320605135f59SDavid Sterba btrfs_warn(fs_info, 320705135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 32086bccf3abSJeff Mahoney close_ctree(fs_info); 320970f80175SStefan Behrens return ret; 321070f80175SStefan Behrens } 321170f80175SStefan Behrens } else { 3212afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3213f7a81ea4SStefan Behrens } 3214afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 321547ab2a6cSJosef Bacik 32168dcddfa0SQu Wenruo /* 32178dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 32188dcddfa0SQu Wenruo * no need to keep the flag 32198dcddfa0SQu Wenruo */ 32208dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32218dcddfa0SQu Wenruo 3222ad2b2c80SAl Viro return 0; 322339279cc3SChris Mason 3224bcef60f2SArne Jansen fail_qgroup: 3225bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 32267c2ca468SChris Mason fail_trans_kthread: 32277c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 32282ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3229faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 32303f157a2fSChris Mason fail_cleaner: 3231a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 32327c2ca468SChris Mason 32337c2ca468SChris Mason /* 32347c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 32357c2ca468SChris Mason * kthreads 32367c2ca468SChris Mason */ 32377c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 32387c2ca468SChris Mason 32392365dd3cSAnand Jain fail_sysfs: 32406618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 32412365dd3cSAnand Jain 3242b7c35e81SAnand Jain fail_fsdev_sysfs: 3243b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3244b7c35e81SAnand Jain 32451b1d1f66SJosef Bacik fail_block_groups: 324654067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 3247af31f5e5SChris Mason 3248af31f5e5SChris Mason fail_tree_roots: 3249af31f5e5SChris Mason free_root_pointers(fs_info, 1); 32502b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3251af31f5e5SChris Mason 325239279cc3SChris Mason fail_sb_buffer: 32537abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 32545cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 325516cdcec7SMiao Xie fail_alloc: 32564543df7eSChris Mason fail_iput: 3257586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3258586e46e2SIlya Dryomov 32594543df7eSChris Mason iput(fs_info->btree_inode); 3260c404e0dcSMiao Xie fail_bio_counter: 3261c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3262963d678bSMiao Xie fail_delalloc_bytes: 3263963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3264e2d84521SMiao Xie fail_dirty_metadata_bytes: 3265e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 326676dda93cSYan, Zheng fail_srcu: 326776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 32687e662854SQinghuang Feng fail: 326953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3270586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3271ad2b2c80SAl Viro return err; 3272af31f5e5SChris Mason 3273af31f5e5SChris Mason recovery_tree_root: 32740b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3275af31f5e5SChris Mason goto fail_tree_roots; 3276af31f5e5SChris Mason 3277af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3278af31f5e5SChris Mason 3279af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3280af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3281af31f5e5SChris Mason 3282af31f5e5SChris Mason /* we can't trust the free space cache either */ 3283af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3284af31f5e5SChris Mason 3285af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3286af31f5e5SChris Mason &num_backups_tried, &backup_index); 3287af31f5e5SChris Mason if (ret == -1) 3288af31f5e5SChris Mason goto fail_block_groups; 3289af31f5e5SChris Mason goto retry_root_backup; 3290eb60ceacSChris Mason } 3291eb60ceacSChris Mason 3292f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3293f2984462SChris Mason { 3294f2984462SChris Mason if (uptodate) { 3295f2984462SChris Mason set_buffer_uptodate(bh); 3296f2984462SChris Mason } else { 3297442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3298442a4f63SStefan Behrens bh->b_private; 3299442a4f63SStefan Behrens 3300fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3301b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3302606686eeSJosef Bacik rcu_str_deref(device->name)); 330301327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 33041259ab75SChris Mason * our own ways of dealing with the IO errors 33051259ab75SChris Mason */ 3306f2984462SChris Mason clear_buffer_uptodate(bh); 3307442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3308f2984462SChris Mason } 3309f2984462SChris Mason unlock_buffer(bh); 3310f2984462SChris Mason put_bh(bh); 3311f2984462SChris Mason } 3312f2984462SChris Mason 331329c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 331429c36d72SAnand Jain struct buffer_head **bh_ret) 331529c36d72SAnand Jain { 331629c36d72SAnand Jain struct buffer_head *bh; 331729c36d72SAnand Jain struct btrfs_super_block *super; 331829c36d72SAnand Jain u64 bytenr; 331929c36d72SAnand Jain 332029c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 332129c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 332229c36d72SAnand Jain return -EINVAL; 332329c36d72SAnand Jain 332429c36d72SAnand Jain bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE); 332529c36d72SAnand Jain /* 332629c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 332729c36d72SAnand Jain * the best option we have is to mark it EIO. 332829c36d72SAnand Jain */ 332929c36d72SAnand Jain if (!bh) 333029c36d72SAnand Jain return -EIO; 333129c36d72SAnand Jain 333229c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 333329c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 333429c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 333529c36d72SAnand Jain brelse(bh); 333629c36d72SAnand Jain return -EINVAL; 333729c36d72SAnand Jain } 333829c36d72SAnand Jain 333929c36d72SAnand Jain *bh_ret = bh; 334029c36d72SAnand Jain return 0; 334129c36d72SAnand Jain } 334229c36d72SAnand Jain 334329c36d72SAnand Jain 3344a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3345a512bbf8SYan Zheng { 3346a512bbf8SYan Zheng struct buffer_head *bh; 3347a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3348a512bbf8SYan Zheng struct btrfs_super_block *super; 3349a512bbf8SYan Zheng int i; 3350a512bbf8SYan Zheng u64 transid = 0; 335192fc03fbSAnand Jain int ret = -EINVAL; 3352a512bbf8SYan Zheng 3353a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3354a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3355a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3356a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3357a512bbf8SYan Zheng */ 3358a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 335929c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 336029c36d72SAnand Jain if (ret) 3361a512bbf8SYan Zheng continue; 3362a512bbf8SYan Zheng 3363a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3364a512bbf8SYan Zheng 3365a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3366a512bbf8SYan Zheng brelse(latest); 3367a512bbf8SYan Zheng latest = bh; 3368a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3369a512bbf8SYan Zheng } else { 3370a512bbf8SYan Zheng brelse(bh); 3371a512bbf8SYan Zheng } 3372a512bbf8SYan Zheng } 337392fc03fbSAnand Jain 337492fc03fbSAnand Jain if (!latest) 337592fc03fbSAnand Jain return ERR_PTR(ret); 337692fc03fbSAnand Jain 3377a512bbf8SYan Zheng return latest; 3378a512bbf8SYan Zheng } 3379a512bbf8SYan Zheng 33804eedeb75SHisashi Hifumi /* 33814eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 33824eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 33834eedeb75SHisashi Hifumi * we write are pinned. 33844eedeb75SHisashi Hifumi * 33854eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 33864eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 33874eedeb75SHisashi Hifumi * many supers on this one device should be written. 33884eedeb75SHisashi Hifumi * 33894eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 33904eedeb75SHisashi Hifumi */ 3391a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3392a512bbf8SYan Zheng struct btrfs_super_block *sb, 3393b75f5062SDavid Sterba int wait, int max_mirrors) 3394a512bbf8SYan Zheng { 3395a512bbf8SYan Zheng struct buffer_head *bh; 3396a512bbf8SYan Zheng int i; 3397a512bbf8SYan Zheng int ret; 3398a512bbf8SYan Zheng int errors = 0; 3399a512bbf8SYan Zheng u32 crc; 3400a512bbf8SYan Zheng u64 bytenr; 3401a512bbf8SYan Zheng 3402a512bbf8SYan Zheng if (max_mirrors == 0) 3403a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3404a512bbf8SYan Zheng 3405a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3406a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3407935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3408935e5cc9SMiao Xie device->commit_total_bytes) 3409a512bbf8SYan Zheng break; 3410a512bbf8SYan Zheng 3411a512bbf8SYan Zheng if (wait) { 3412a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3413a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3414634554dcSJosef Bacik if (!bh) { 3415634554dcSJosef Bacik errors++; 3416634554dcSJosef Bacik continue; 3417634554dcSJosef Bacik } 3418a512bbf8SYan Zheng wait_on_buffer(bh); 34194eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 34204eedeb75SHisashi Hifumi errors++; 34214eedeb75SHisashi Hifumi 34224eedeb75SHisashi Hifumi /* drop our reference */ 34234eedeb75SHisashi Hifumi brelse(bh); 34244eedeb75SHisashi Hifumi 34254eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3426a512bbf8SYan Zheng brelse(bh); 3427a512bbf8SYan Zheng continue; 3428a512bbf8SYan Zheng } else { 3429a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3430a512bbf8SYan Zheng 3431a512bbf8SYan Zheng crc = ~(u32)0; 34329ed57367SDavid Sterba crc = btrfs_csum_data((const char *)sb + 3433a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3434a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3435a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3436a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3437a512bbf8SYan Zheng 34384eedeb75SHisashi Hifumi /* 34394eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 34404eedeb75SHisashi Hifumi * caller 34414eedeb75SHisashi Hifumi */ 3442a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3443a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3444634554dcSJosef Bacik if (!bh) { 3445fb456252SJeff Mahoney btrfs_err(device->fs_info, 3446f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3447f14d104dSDavid Sterba bytenr); 3448634554dcSJosef Bacik errors++; 3449634554dcSJosef Bacik continue; 3450634554dcSJosef Bacik } 3451634554dcSJosef Bacik 3452a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3453a512bbf8SYan Zheng 34544eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3455a512bbf8SYan Zheng get_bh(bh); 34564eedeb75SHisashi Hifumi 34574eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3458a512bbf8SYan Zheng lock_buffer(bh); 3459a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3460442a4f63SStefan Behrens bh->b_private = device; 3461a512bbf8SYan Zheng } 3462a512bbf8SYan Zheng 3463387125fcSChris Mason /* 3464387125fcSChris Mason * we fua the first super. The others we allow 3465387125fcSChris Mason * to go down lazy. 3466387125fcSChris Mason */ 34678d910125SJan Kara if (i == 0) { 34688d910125SJan Kara ret = btrfsic_submit_bh(REQ_OP_WRITE, 34698d910125SJan Kara REQ_SYNC | REQ_FUA, bh); 34708d910125SJan Kara } else { 347170fd7614SChristoph Hellwig ret = btrfsic_submit_bh(REQ_OP_WRITE, REQ_SYNC, bh); 34728d910125SJan Kara } 34734eedeb75SHisashi Hifumi if (ret) 3474a512bbf8SYan Zheng errors++; 3475a512bbf8SYan Zheng } 3476a512bbf8SYan Zheng return errors < i ? 0 : -1; 3477a512bbf8SYan Zheng } 3478a512bbf8SYan Zheng 3479387125fcSChris Mason /* 3480387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3481387125fcSChris Mason * for the barrier when it is done 3482387125fcSChris Mason */ 34834246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3484387125fcSChris Mason { 3485387125fcSChris Mason if (bio->bi_private) 3486387125fcSChris Mason complete(bio->bi_private); 3487387125fcSChris Mason bio_put(bio); 3488387125fcSChris Mason } 3489387125fcSChris Mason 3490387125fcSChris Mason /* 34914fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is 34924fc6441aSAnand Jain * done in the waiting counterpart. 3493387125fcSChris Mason */ 34944fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device) 3495387125fcSChris Mason { 3496c2a9c7abSAnand Jain struct request_queue *q = bdev_get_queue(device->bdev); 3497387125fcSChris Mason struct bio *bio; 3498387125fcSChris Mason 3499c2a9c7abSAnand Jain if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 35004fc6441aSAnand Jain return; 3501387125fcSChris Mason 3502387125fcSChris Mason /* 3503387125fcSChris Mason * one reference for us, and we leave it for the 3504387125fcSChris Mason * caller 3505387125fcSChris Mason */ 35069c017abcSJesper Juhl device->flush_bio = NULL; 3507c5e4c3d7SDavid Sterba bio = btrfs_io_bio_alloc(0); 3508387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3509387125fcSChris Mason bio->bi_bdev = device->bdev; 35108d910125SJan Kara bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH; 3511387125fcSChris Mason init_completion(&device->flush_wait); 3512387125fcSChris Mason bio->bi_private = &device->flush_wait; 3513387125fcSChris Mason device->flush_bio = bio; 3514387125fcSChris Mason 3515387125fcSChris Mason bio_get(bio); 35164e49ea4aSMike Christie btrfsic_submit_bio(bio); 35174fc6441aSAnand Jain } 3518387125fcSChris Mason 35194fc6441aSAnand Jain /* 35204fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it. 35214fc6441aSAnand Jain */ 35224fc6441aSAnand Jain static int wait_dev_flush(struct btrfs_device *device) 35234fc6441aSAnand Jain { 35244fc6441aSAnand Jain int ret = 0; 35254fc6441aSAnand Jain struct bio *bio = device->flush_bio; 35264fc6441aSAnand Jain 35274fc6441aSAnand Jain if (!bio) 3528387125fcSChris Mason return 0; 35294fc6441aSAnand Jain 35304fc6441aSAnand Jain wait_for_completion(&device->flush_wait); 35314fc6441aSAnand Jain 35324fc6441aSAnand Jain if (bio->bi_error) { 35334fc6441aSAnand Jain ret = bio->bi_error; 35344fc6441aSAnand Jain btrfs_dev_stat_inc_and_print(device, 35354fc6441aSAnand Jain BTRFS_DEV_STAT_FLUSH_ERRS); 35364fc6441aSAnand Jain } 35374fc6441aSAnand Jain 35384fc6441aSAnand Jain /* drop the reference from the wait == 0 run */ 35394fc6441aSAnand Jain bio_put(bio); 35404fc6441aSAnand Jain device->flush_bio = NULL; 35414fc6441aSAnand Jain 35424fc6441aSAnand Jain return ret; 3543387125fcSChris Mason } 3544387125fcSChris Mason 3545401b41e5SAnand Jain static int check_barrier_error(struct btrfs_fs_devices *fsdevs) 3546401b41e5SAnand Jain { 3547401b41e5SAnand Jain int dev_flush_error = 0; 3548401b41e5SAnand Jain struct btrfs_device *dev; 3549401b41e5SAnand Jain 3550401b41e5SAnand Jain list_for_each_entry_rcu(dev, &fsdevs->devices, dev_list) { 355112b9bf0bSAnand Jain if (!dev->bdev || dev->last_flush_error) 3552401b41e5SAnand Jain dev_flush_error++; 3553401b41e5SAnand Jain } 3554401b41e5SAnand Jain 355512b9bf0bSAnand Jain if (dev_flush_error > 355612b9bf0bSAnand Jain fsdevs->fs_info->num_tolerated_disk_barrier_failures) 3557401b41e5SAnand Jain return -EIO; 3558401b41e5SAnand Jain 3559401b41e5SAnand Jain return 0; 3560401b41e5SAnand Jain } 3561401b41e5SAnand Jain 3562387125fcSChris Mason /* 3563387125fcSChris Mason * send an empty flush down to each device in parallel, 3564387125fcSChris Mason * then wait for them 3565387125fcSChris Mason */ 3566387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3567387125fcSChris Mason { 3568387125fcSChris Mason struct list_head *head; 3569387125fcSChris Mason struct btrfs_device *dev; 35705af3e8ccSStefan Behrens int errors_wait = 0; 3571387125fcSChris Mason int ret; 3572387125fcSChris Mason 3573387125fcSChris Mason /* send down all the barriers */ 3574387125fcSChris Mason head = &info->fs_devices->devices; 3575387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3576f88ba6a2SHidetoshi Seto if (dev->missing) 3577f88ba6a2SHidetoshi Seto continue; 3578cea7c8bfSAnand Jain if (!dev->bdev) 3579387125fcSChris Mason continue; 3580387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3581387125fcSChris Mason continue; 3582387125fcSChris Mason 35834fc6441aSAnand Jain write_dev_flush(dev); 358412b9bf0bSAnand Jain dev->last_flush_error = 0; 3585387125fcSChris Mason } 3586387125fcSChris Mason 3587387125fcSChris Mason /* wait for all the barriers */ 3588387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3589f88ba6a2SHidetoshi Seto if (dev->missing) 3590f88ba6a2SHidetoshi Seto continue; 3591387125fcSChris Mason if (!dev->bdev) { 35925af3e8ccSStefan Behrens errors_wait++; 3593387125fcSChris Mason continue; 3594387125fcSChris Mason } 3595387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3596387125fcSChris Mason continue; 3597387125fcSChris Mason 35984fc6441aSAnand Jain ret = wait_dev_flush(dev); 3599401b41e5SAnand Jain if (ret) { 3600401b41e5SAnand Jain dev->last_flush_error = ret; 36015af3e8ccSStefan Behrens errors_wait++; 3602387125fcSChris Mason } 3603401b41e5SAnand Jain } 3604401b41e5SAnand Jain 3605cea7c8bfSAnand Jain if (errors_wait) { 3606401b41e5SAnand Jain /* 3607401b41e5SAnand Jain * At some point we need the status of all disks 3608401b41e5SAnand Jain * to arrive at the volume status. So error checking 3609401b41e5SAnand Jain * is being pushed to a separate loop. 3610401b41e5SAnand Jain */ 3611401b41e5SAnand Jain return check_barrier_error(info->fs_devices); 3612401b41e5SAnand Jain } 3613387125fcSChris Mason return 0; 3614387125fcSChris Mason } 3615387125fcSChris Mason 3616943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3617943c6e99SZhao Lei { 36188789f4feSZhao Lei int raid_type; 36198789f4feSZhao Lei int min_tolerated = INT_MAX; 3620943c6e99SZhao Lei 36218789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 36228789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 36238789f4feSZhao Lei min_tolerated = min(min_tolerated, 36248789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 36258789f4feSZhao Lei tolerated_failures); 3626943c6e99SZhao Lei 36278789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 36288789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 36298789f4feSZhao Lei continue; 36308789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 36318789f4feSZhao Lei continue; 36328789f4feSZhao Lei min_tolerated = min(min_tolerated, 36338789f4feSZhao Lei btrfs_raid_array[raid_type]. 36348789f4feSZhao Lei tolerated_failures); 36358789f4feSZhao Lei } 3636943c6e99SZhao Lei 36378789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3638ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 36398789f4feSZhao Lei min_tolerated = 0; 36408789f4feSZhao Lei } 36418789f4feSZhao Lei 36428789f4feSZhao Lei return min_tolerated; 3643943c6e99SZhao Lei } 3644943c6e99SZhao Lei 36455af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 36465af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 36475af3e8ccSStefan Behrens { 36485af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 36495af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 36505af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 36515af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 36525af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 36535af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 36545af3e8ccSStefan Behrens int i; 36555af3e8ccSStefan Behrens int c; 36565af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 36575af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 36585af3e8ccSStefan Behrens 36592c458045SZhao Lei for (i = 0; i < ARRAY_SIZE(types); i++) { 36605af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 36615af3e8ccSStefan Behrens 36625af3e8ccSStefan Behrens sinfo = NULL; 36635af3e8ccSStefan Behrens rcu_read_lock(); 36645af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 36655af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 36665af3e8ccSStefan Behrens sinfo = tmp; 36675af3e8ccSStefan Behrens break; 36685af3e8ccSStefan Behrens } 36695af3e8ccSStefan Behrens } 36705af3e8ccSStefan Behrens rcu_read_unlock(); 36715af3e8ccSStefan Behrens 36725af3e8ccSStefan Behrens if (!sinfo) 36735af3e8ccSStefan Behrens continue; 36745af3e8ccSStefan Behrens 36755af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 36765af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 36775af3e8ccSStefan Behrens u64 flags; 36785af3e8ccSStefan Behrens 36792c458045SZhao Lei if (list_empty(&sinfo->block_groups[c])) 36802c458045SZhao Lei continue; 36812c458045SZhao Lei 36822c458045SZhao Lei btrfs_get_block_group_info(&sinfo->block_groups[c], 36832c458045SZhao Lei &space); 36842c458045SZhao Lei if (space.total_bytes == 0 || space.used_bytes == 0) 36855af3e8ccSStefan Behrens continue; 36865af3e8ccSStefan Behrens flags = space.flags; 3687943c6e99SZhao Lei 3688943c6e99SZhao Lei num_tolerated_disk_barrier_failures = min( 3689943c6e99SZhao Lei num_tolerated_disk_barrier_failures, 3690943c6e99SZhao Lei btrfs_get_num_tolerated_disk_barrier_failures( 3691943c6e99SZhao Lei flags)); 36925af3e8ccSStefan Behrens } 36935af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 36945af3e8ccSStefan Behrens } 36955af3e8ccSStefan Behrens 36965af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 36975af3e8ccSStefan Behrens } 36985af3e8ccSStefan Behrens 3699eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3700f2984462SChris Mason { 3701e5e9a520SChris Mason struct list_head *head; 3702f2984462SChris Mason struct btrfs_device *dev; 3703a061fc8dSChris Mason struct btrfs_super_block *sb; 3704f2984462SChris Mason struct btrfs_dev_item *dev_item; 3705f2984462SChris Mason int ret; 3706f2984462SChris Mason int do_barriers; 3707a236aed1SChris Mason int max_errors; 3708a236aed1SChris Mason int total_errors = 0; 3709a061fc8dSChris Mason u64 flags; 3710f2984462SChris Mason 37110b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 37120b246afaSJeff Mahoney backup_super_roots(fs_info); 3713f2984462SChris Mason 37140b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3715a061fc8dSChris Mason dev_item = &sb->dev_item; 3716e5e9a520SChris Mason 37170b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37180b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37190b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3720387125fcSChris Mason 37215af3e8ccSStefan Behrens if (do_barriers) { 37220b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37235af3e8ccSStefan Behrens if (ret) { 37245af3e8ccSStefan Behrens mutex_unlock( 37250b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 37260b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 37275af3e8ccSStefan Behrens "errors while submitting device barriers."); 37285af3e8ccSStefan Behrens return ret; 37295af3e8ccSStefan Behrens } 37305af3e8ccSStefan Behrens } 3731387125fcSChris Mason 37321f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3733dfe25020SChris Mason if (!dev->bdev) { 3734dfe25020SChris Mason total_errors++; 3735dfe25020SChris Mason continue; 3736dfe25020SChris Mason } 37372b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3738dfe25020SChris Mason continue; 3739dfe25020SChris Mason 37402b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3741a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3742a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 37437df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3744935e5cc9SMiao Xie dev->commit_total_bytes); 3745ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3746ce7213c7SMiao Xie dev->commit_bytes_used); 3747a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3748a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3749a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3750a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 37512b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3752a512bbf8SYan Zheng 3753a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3754a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3755f2984462SChris Mason 3756b75f5062SDavid Sterba ret = write_dev_supers(dev, sb, 0, max_mirrors); 3757a236aed1SChris Mason if (ret) 3758a236aed1SChris Mason total_errors++; 3759f2984462SChris Mason } 3760a236aed1SChris Mason if (total_errors > max_errors) { 37610b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3762d397712bSChris Mason total_errors); 37630b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 376479787eaaSJeff Mahoney 37659d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 37660b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37670b246afaSJeff Mahoney "%d errors while writing supers", 37680b246afaSJeff Mahoney total_errors); 37699d565ba4SStefan Behrens return -EIO; 3770a236aed1SChris Mason } 3771f2984462SChris Mason 3772a512bbf8SYan Zheng total_errors = 0; 37731f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3774dfe25020SChris Mason if (!dev->bdev) 3775dfe25020SChris Mason continue; 37762b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3777dfe25020SChris Mason continue; 3778dfe25020SChris Mason 3779b75f5062SDavid Sterba ret = write_dev_supers(dev, sb, 1, max_mirrors); 3780a512bbf8SYan Zheng if (ret) 37811259ab75SChris Mason total_errors++; 3782f2984462SChris Mason } 37830b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3784a236aed1SChris Mason if (total_errors > max_errors) { 37850b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37860b246afaSJeff Mahoney "%d errors while writing supers", 37870b246afaSJeff Mahoney total_errors); 378879787eaaSJeff Mahoney return -EIO; 3789a236aed1SChris Mason } 3790f2984462SChris Mason return 0; 3791f2984462SChris Mason } 3792f2984462SChris Mason 3793cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3794cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3795cb517eabSMiao Xie struct btrfs_root *root) 37962619ba1fSChris Mason { 37974df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 37982619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 37992619ba1fSChris Mason (unsigned long)root->root_key.objectid); 38004df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 380176dda93cSYan, Zheng 380276dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 380376dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 380476dda93cSYan, Zheng 38051c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 38063321719eSLiu Bo btrfs_free_log(NULL, root); 38071c1ea4f7SLiu Bo if (root->reloc_root) { 38081c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38091c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 38101c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 38111c1ea4f7SLiu Bo root->reloc_root = NULL; 38121c1ea4f7SLiu Bo } 38131c1ea4f7SLiu Bo } 38143321719eSLiu Bo 3815faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3816581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3817faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3818581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 38194df27c4dSYan, Zheng free_fs_root(root); 38204df27c4dSYan, Zheng } 38214df27c4dSYan, Zheng 38224df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 38234df27c4dSYan, Zheng { 382457cdc8dbSDavid Sterba iput(root->ino_cache_inode); 38254df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 38262ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 38271cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 38280ee5dc67SAl Viro if (root->anon_dev) 38290ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 38308257b2dcSMiao Xie if (root->subv_writers) 38318257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 38325f39d397SChris Mason free_extent_buffer(root->node); 38335f39d397SChris Mason free_extent_buffer(root->commit_root); 3834581bb050SLi Zefan kfree(root->free_ino_ctl); 3835581bb050SLi Zefan kfree(root->free_ino_pinned); 383658176a96SJosef Bacik kfree(root->name); 3837b0feb9d9SMiao Xie btrfs_put_fs_root(root); 38382619ba1fSChris Mason } 38392619ba1fSChris Mason 3840cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3841cb517eabSMiao Xie { 3842cb517eabSMiao Xie free_fs_root(root); 3843cfaa7295SChris Mason } 3844cfaa7295SChris Mason 3845c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3846c146afadSYan Zheng { 3847c146afadSYan Zheng u64 root_objectid = 0; 3848c146afadSYan Zheng struct btrfs_root *gang[8]; 384965d33fd7SQu Wenruo int i = 0; 385065d33fd7SQu Wenruo int err = 0; 385165d33fd7SQu Wenruo unsigned int ret = 0; 385265d33fd7SQu Wenruo int index; 3853c146afadSYan Zheng 3854c146afadSYan Zheng while (1) { 385565d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3856c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3857c146afadSYan Zheng (void **)gang, root_objectid, 3858c146afadSYan Zheng ARRAY_SIZE(gang)); 385965d33fd7SQu Wenruo if (!ret) { 386065d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3861c146afadSYan Zheng break; 386265d33fd7SQu Wenruo } 38635d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 386466b4ffd1SJosef Bacik 386565d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 386665d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 386765d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 386865d33fd7SQu Wenruo gang[i] = NULL; 386965d33fd7SQu Wenruo continue; 387065d33fd7SQu Wenruo } 387165d33fd7SQu Wenruo /* grab all the search result for later use */ 387265d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 387365d33fd7SQu Wenruo } 387465d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 387565d33fd7SQu Wenruo 387665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 387765d33fd7SQu Wenruo if (!gang[i]) 387865d33fd7SQu Wenruo continue; 3879c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 388066b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 388166b4ffd1SJosef Bacik if (err) 388265d33fd7SQu Wenruo break; 388365d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3884c146afadSYan Zheng } 3885c146afadSYan Zheng root_objectid++; 3886c146afadSYan Zheng } 388765d33fd7SQu Wenruo 388865d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 388965d33fd7SQu Wenruo for (; i < ret; i++) { 389065d33fd7SQu Wenruo if (gang[i]) 389165d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 389265d33fd7SQu Wenruo } 389365d33fd7SQu Wenruo return err; 3894c146afadSYan Zheng } 3895c146afadSYan Zheng 38966bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3897c146afadSYan Zheng { 38986bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3899c146afadSYan Zheng struct btrfs_trans_handle *trans; 3900c146afadSYan Zheng 39010b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39022ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39030b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39040b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3905c71bf099SYan, Zheng 3906c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39070b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39080b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3909c71bf099SYan, Zheng 39107a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39113612b495STsutomu Itoh if (IS_ERR(trans)) 39123612b495STsutomu Itoh return PTR_ERR(trans); 39133a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3914c146afadSYan Zheng } 3915c146afadSYan Zheng 39166bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3917eb60ceacSChris Mason { 39186bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3919c146afadSYan Zheng int ret; 3920e089f05cSChris Mason 3921afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3922a2135011SChris Mason 39237343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3924d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 39257343dd61SJustin Maggard 3926803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3927803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3928803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3929803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3930803b2f54SStefan Behrens 3931837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3932aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3933837d5b6eSIlya Dryomov 39348dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 39358dabb742SStefan Behrens 3936aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 39374cb5300bSChris Mason 39384cb5300bSChris Mason /* wait for any defraggers to finish */ 39394cb5300bSChris Mason wait_event(fs_info->transaction_wait, 39404cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 39414cb5300bSChris Mason 39424cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 394326176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 39444cb5300bSChris Mason 394521c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 394621c7e756SMiao Xie 3947c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3948e44163e1SJeff Mahoney /* 3949e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3950e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3951e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3952e44163e1SJeff Mahoney */ 39530b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3954e44163e1SJeff Mahoney 39556bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3956d397712bSChris Mason if (ret) 395704892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3958c146afadSYan Zheng } 3959ed2ff2cbSChris Mason 396087533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 39612ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3962acce952bSliubo 3963e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3964e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 39658929ecfaSYan, Zheng 3966afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3967f25784b3SYan Zheng 396804892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3969bcef60f2SArne Jansen 3970963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 397104892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3972963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3973b0c68f8bSChris Mason } 397431153d81SYan Zheng 39756618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3976b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 39775ac1d209SJeff Mahoney 3978faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3979d10c5f31SChris Mason 39801a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 39811a4319ccSLiu Bo 3982de348ee0SWang Shilong /* 3983de348ee0SWang Shilong * we must make sure there is not any read request to 3984de348ee0SWang Shilong * submit after we stopping all workers. 3985de348ee0SWang Shilong */ 3986de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 398796192499SJosef Bacik btrfs_stop_all_workers(fs_info); 398896192499SJosef Bacik 39895cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info); 39905cdd7db6SFilipe Manana 3991afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 399213e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 39939ad6b7bcSChris Mason 399413e6c37bSJosef Bacik iput(fs_info->btree_inode); 3995d6bfde87SChris Mason 399621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 39970b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 39982ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 399921adbd5cSStefan Behrens #endif 400021adbd5cSStefan Behrens 4001dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 40020b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 4003b248a415SChris Mason 4004e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 4005963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 4006c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 400776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 40080b86a832SChris Mason 400953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 401053b381b3SDavid Woodhouse 4011cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 40121cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 401304216820SFilipe Manana 401434441361SDavid Sterba mutex_lock(&fs_info->chunk_mutex); 401504216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 401604216820SFilipe Manana struct extent_map *em; 401704216820SFilipe Manana 401804216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 401904216820SFilipe Manana struct extent_map, list); 402004216820SFilipe Manana list_del_init(&em->list); 402104216820SFilipe Manana free_extent_map(em); 402204216820SFilipe Manana } 402334441361SDavid Sterba mutex_unlock(&fs_info->chunk_mutex); 4024eb60ceacSChris Mason } 4025eb60ceacSChris Mason 4026b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4027b9fab919SChris Mason int atomic) 4028ccd467d6SChris Mason { 40291259ab75SChris Mason int ret; 4030727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 40311259ab75SChris Mason 40320b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 40331259ab75SChris Mason if (!ret) 40341259ab75SChris Mason return ret; 40351259ab75SChris Mason 40361259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4037b9fab919SChris Mason parent_transid, atomic); 4038b9fab919SChris Mason if (ret == -EAGAIN) 4039b9fab919SChris Mason return ret; 40401259ab75SChris Mason return !ret; 40415f39d397SChris Mason } 40426702ed49SChris Mason 40435f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 40445f39d397SChris Mason { 40450b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 404606ea65a3SJosef Bacik struct btrfs_root *root; 40475f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4048b9473439SChris Mason int was_dirty; 4049b4ce94deSChris Mason 405006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 405106ea65a3SJosef Bacik /* 405206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 405306ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 405406ea65a3SJosef Bacik * outside of the sanity tests. 405506ea65a3SJosef Bacik */ 405606ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 405706ea65a3SJosef Bacik return; 405806ea65a3SJosef Bacik #endif 405906ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 40600b246afaSJeff Mahoney fs_info = root->fs_info; 4061b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 40620b246afaSJeff Mahoney if (transid != fs_info->generation) 40635d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 40640b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 40650b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4066e2d84521SMiao Xie if (!was_dirty) 40670b246afaSJeff Mahoney __percpu_counter_add(&fs_info->dirty_metadata_bytes, 4068e2d84521SMiao Xie buf->len, 40690b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 40701f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40711f21ef0aSFilipe Manana if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 40722ff7e61eSJeff Mahoney btrfs_print_leaf(fs_info, buf); 40731f21ef0aSFilipe Manana ASSERT(0); 40741f21ef0aSFilipe Manana } 40751f21ef0aSFilipe Manana #endif 4076eb60ceacSChris Mason } 4077eb60ceacSChris Mason 40782ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4079b53d3f5dSLiu Bo int flush_delayed) 408035b7e476SChris Mason { 4081188de649SChris Mason /* 4082188de649SChris Mason * looks as though older kernels can get into trouble with 4083188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4084188de649SChris Mason */ 4085e2d84521SMiao Xie int ret; 4086d6bfde87SChris Mason 40876933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4088d6bfde87SChris Mason return; 4089d6bfde87SChris Mason 4090b53d3f5dSLiu Bo if (flush_delayed) 40912ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 409216cdcec7SMiao Xie 40930b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4094e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 4095e2d84521SMiao Xie if (ret > 0) { 40960b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 409716cdcec7SMiao Xie } 409816cdcec7SMiao Xie } 409916cdcec7SMiao Xie 41002ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 410116cdcec7SMiao Xie { 41022ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 410335b7e476SChris Mason } 4104b53d3f5dSLiu Bo 41052ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4106b53d3f5dSLiu Bo { 41072ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4108d6bfde87SChris Mason } 41096b80053dSChris Mason 4110ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 41116b80053dSChris Mason { 4112727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41132ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41142ff7e61eSJeff Mahoney 41152ff7e61eSJeff Mahoney return btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 41166b80053dSChris Mason } 41170da5468fSChris Mason 41183d3a126aSDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info) 4119acce952bSliubo { 4120c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 4121319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 4122319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 4123c926093eSDavid Sterba int ret = 0; 4124c926093eSDavid Sterba 4125319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 4126ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 4127319e4d06SQu Wenruo ret = -EINVAL; 4128319e4d06SQu Wenruo } 4129319e4d06SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) 4130ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "unrecognized super flag: %llu", 4131319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 413221e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 4133ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 413421e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 4135c926093eSDavid Sterba ret = -EINVAL; 4136c926093eSDavid Sterba } 413721e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 4138ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 413921e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 4140c926093eSDavid Sterba ret = -EINVAL; 4141c926093eSDavid Sterba } 414221e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 4143ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 414421e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 4145c926093eSDavid Sterba ret = -EINVAL; 4146c926093eSDavid Sterba } 4147c926093eSDavid Sterba 41481104a885SDavid Sterba /* 4149319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 4150319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 41511104a885SDavid Sterba */ 4152319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 4153319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4154ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 415575d6ad38SDavid Sterba ret = -EINVAL; 415675d6ad38SDavid Sterba } 4157319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 415809cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 4159ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4160ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 416109cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 4162319e4d06SQu Wenruo ret = -EINVAL; 4163319e4d06SQu Wenruo } 4164319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 4165319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4166ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 4167319e4d06SQu Wenruo ret = -EINVAL; 4168319e4d06SQu Wenruo } 4169319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 4170ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 4171ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 4172319e4d06SQu Wenruo ret = -EINVAL; 4173319e4d06SQu Wenruo } 4174319e4d06SQu Wenruo 4175319e4d06SQu Wenruo /* Root alignment check */ 4176319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 4177ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 4178319e4d06SQu Wenruo btrfs_super_root(sb)); 4179319e4d06SQu Wenruo ret = -EINVAL; 4180319e4d06SQu Wenruo } 4181319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 4182ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 4183319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 4184319e4d06SQu Wenruo ret = -EINVAL; 4185319e4d06SQu Wenruo } 4186319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 4187ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 4188319e4d06SQu Wenruo btrfs_super_log_root(sb)); 418975d6ad38SDavid Sterba ret = -EINVAL; 419075d6ad38SDavid Sterba } 419175d6ad38SDavid Sterba 4192c926093eSDavid Sterba if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) { 4193ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4194ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 4195c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 4196c926093eSDavid Sterba ret = -EINVAL; 4197c926093eSDavid Sterba } 4198c926093eSDavid Sterba 4199c926093eSDavid Sterba /* 4200c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 4201c926093eSDavid Sterba * done later 4202c926093eSDavid Sterba */ 420399e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 420499e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 420599e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 420699e3ecfcSLiu Bo ret = -EINVAL; 420799e3ecfcSLiu Bo } 4208b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 420999e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 421099e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 421199e3ecfcSLiu Bo ret = -EINVAL; 421299e3ecfcSLiu Bo } 421321e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 4214ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 421521e7626bSDavid Sterba btrfs_super_num_devices(sb)); 421675d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 4217ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 421875d6ad38SDavid Sterba ret = -EINVAL; 421975d6ad38SDavid Sterba } 4220c926093eSDavid Sterba 422121e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4222ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 422321e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4224c926093eSDavid Sterba ret = -EINVAL; 4225c926093eSDavid Sterba } 4226c926093eSDavid Sterba 4227c926093eSDavid Sterba /* 4228ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4229ce7fca5fSDavid Sterba * and one chunk 4230ce7fca5fSDavid Sterba */ 4231ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4232ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4233ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4234ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4235ce7fca5fSDavid Sterba ret = -EINVAL; 4236ce7fca5fSDavid Sterba } 4237ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4238ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4239ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4240ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4241ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4242ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4243ce7fca5fSDavid Sterba ret = -EINVAL; 4244ce7fca5fSDavid Sterba } 4245ce7fca5fSDavid Sterba 4246ce7fca5fSDavid Sterba /* 4247c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4248c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4249c926093eSDavid Sterba */ 425021e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4251ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4252ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4253ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4254ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 425521e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 425621e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4257ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4258ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4259ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4260ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4261c926093eSDavid Sterba 4262c926093eSDavid Sterba return ret; 4263fcd1f065SDavid Sterba } 4264fcd1f065SDavid Sterba 42652ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4266acce952bSliubo { 42670b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42682ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42690b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4270acce952bSliubo 42710b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42720b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4273acce952bSliubo 4274acce952bSliubo /* cleanup FS via transaction */ 42752ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4276acce952bSliubo } 4277acce952bSliubo 4278143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4279acce952bSliubo { 4280acce952bSliubo struct btrfs_ordered_extent *ordered; 4281acce952bSliubo 4282199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4283779880efSJosef Bacik /* 4284779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4285779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4286779880efSJosef Bacik */ 4287199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4288779880efSJosef Bacik root_extent_list) 4289779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4290199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4291199c2a9cSMiao Xie } 4292199c2a9cSMiao Xie 4293199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4294199c2a9cSMiao Xie { 4295199c2a9cSMiao Xie struct btrfs_root *root; 4296199c2a9cSMiao Xie struct list_head splice; 4297199c2a9cSMiao Xie 4298199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4299199c2a9cSMiao Xie 4300199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4301199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4302199c2a9cSMiao Xie while (!list_empty(&splice)) { 4303199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4304199c2a9cSMiao Xie ordered_root); 43051de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 43061de2cfdeSJosef Bacik &fs_info->ordered_roots); 4307199c2a9cSMiao Xie 43082a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4309199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4310199c2a9cSMiao Xie 43112a85d9caSLiu Bo cond_resched(); 43122a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4313199c2a9cSMiao Xie } 4314199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4315acce952bSliubo } 4316acce952bSliubo 431735a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43182ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4319acce952bSliubo { 4320acce952bSliubo struct rb_node *node; 4321acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4322acce952bSliubo struct btrfs_delayed_ref_node *ref; 4323acce952bSliubo int ret = 0; 4324acce952bSliubo 4325acce952bSliubo delayed_refs = &trans->delayed_refs; 4326acce952bSliubo 4327acce952bSliubo spin_lock(&delayed_refs->lock); 4328d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4329cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 43300b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4331acce952bSliubo return ret; 4332acce952bSliubo } 4333acce952bSliubo 4334d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4335d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 4336c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *tmp; 4337e78417d1SJosef Bacik bool pin_bytes = false; 4338acce952bSliubo 4339d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4340d7df2c79SJosef Bacik href_node); 4341b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 43426df8cdf5SElena Reshetova refcount_inc(&head->node.refs); 434379787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4344b939d1abSJosef Bacik 4345acce952bSliubo mutex_lock(&head->mutex); 4346b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4347d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4348e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4349b939d1abSJosef Bacik continue; 4350b939d1abSJosef Bacik } 4351d7df2c79SJosef Bacik spin_lock(&head->lock); 4352c6fc2454SQu Wenruo list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list, 4353c6fc2454SQu Wenruo list) { 4354d7df2c79SJosef Bacik ref->in_tree = 0; 4355c6fc2454SQu Wenruo list_del(&ref->list); 43561d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43571d57ee94SWang Xiaoguang list_del(&ref->add_list); 4358d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4359d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4360d7df2c79SJosef Bacik } 436154067ae9SJosef Bacik if (head->must_insert_reserved) 4362e78417d1SJosef Bacik pin_bytes = true; 436378a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4364acce952bSliubo delayed_refs->num_heads--; 4365d7df2c79SJosef Bacik if (head->processing == 0) 4366acce952bSliubo delayed_refs->num_heads_ready--; 4367d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4368d7df2c79SJosef Bacik head->node.in_tree = 0; 4369c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4370d7df2c79SJosef Bacik spin_unlock(&head->lock); 4371acce952bSliubo spin_unlock(&delayed_refs->lock); 4372e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4373acce952bSliubo 4374d7df2c79SJosef Bacik if (pin_bytes) 43752ff7e61eSJeff Mahoney btrfs_pin_extent(fs_info, head->node.bytenr, 4376d7df2c79SJosef Bacik head->node.num_bytes, 1); 4377d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4378acce952bSliubo cond_resched(); 4379acce952bSliubo spin_lock(&delayed_refs->lock); 4380acce952bSliubo } 4381acce952bSliubo 4382acce952bSliubo spin_unlock(&delayed_refs->lock); 4383acce952bSliubo 4384acce952bSliubo return ret; 4385acce952bSliubo } 4386acce952bSliubo 4387143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4388acce952bSliubo { 4389acce952bSliubo struct btrfs_inode *btrfs_inode; 4390acce952bSliubo struct list_head splice; 4391acce952bSliubo 4392acce952bSliubo INIT_LIST_HEAD(&splice); 4393acce952bSliubo 4394eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4395eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4396acce952bSliubo 4397acce952bSliubo while (!list_empty(&splice)) { 4398eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4399acce952bSliubo delalloc_inodes); 4400acce952bSliubo 4401acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4402df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4403df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4404eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4405acce952bSliubo 4406acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4407b216cbfbSMiao Xie 4408eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4409acce952bSliubo } 4410acce952bSliubo 4411eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4412eb73c1b7SMiao Xie } 4413eb73c1b7SMiao Xie 4414eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4415eb73c1b7SMiao Xie { 4416eb73c1b7SMiao Xie struct btrfs_root *root; 4417eb73c1b7SMiao Xie struct list_head splice; 4418eb73c1b7SMiao Xie 4419eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4420eb73c1b7SMiao Xie 4421eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4422eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4423eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4424eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4425eb73c1b7SMiao Xie delalloc_root); 4426eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4427eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4428eb73c1b7SMiao Xie BUG_ON(!root); 4429eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4430eb73c1b7SMiao Xie 4431eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4432eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4433eb73c1b7SMiao Xie 4434eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4435eb73c1b7SMiao Xie } 4436eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4437acce952bSliubo } 4438acce952bSliubo 44392ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4440acce952bSliubo struct extent_io_tree *dirty_pages, 4441acce952bSliubo int mark) 4442acce952bSliubo { 4443acce952bSliubo int ret; 4444acce952bSliubo struct extent_buffer *eb; 4445acce952bSliubo u64 start = 0; 4446acce952bSliubo u64 end; 4447acce952bSliubo 4448acce952bSliubo while (1) { 4449acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4450e6138876SJosef Bacik mark, NULL); 4451acce952bSliubo if (ret) 4452acce952bSliubo break; 4453acce952bSliubo 445491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4455acce952bSliubo while (start <= end) { 44560b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44570b246afaSJeff Mahoney start += fs_info->nodesize; 4458fd8b2b61SJosef Bacik if (!eb) 4459acce952bSliubo continue; 4460fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4461acce952bSliubo 4462fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4463fd8b2b61SJosef Bacik &eb->bflags)) 4464fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4465fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4466acce952bSliubo } 4467acce952bSliubo } 4468acce952bSliubo 4469acce952bSliubo return ret; 4470acce952bSliubo } 4471acce952bSliubo 44722ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4473acce952bSliubo struct extent_io_tree *pinned_extents) 4474acce952bSliubo { 4475acce952bSliubo struct extent_io_tree *unpin; 4476acce952bSliubo u64 start; 4477acce952bSliubo u64 end; 4478acce952bSliubo int ret; 4479ed0eaa14SLiu Bo bool loop = true; 4480acce952bSliubo 4481acce952bSliubo unpin = pinned_extents; 4482ed0eaa14SLiu Bo again: 4483acce952bSliubo while (1) { 4484acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4485e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4486acce952bSliubo if (ret) 4487acce952bSliubo break; 4488acce952bSliubo 4489af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 44902ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4491acce952bSliubo cond_resched(); 4492acce952bSliubo } 4493acce952bSliubo 4494ed0eaa14SLiu Bo if (loop) { 44950b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44960b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4497ed0eaa14SLiu Bo else 44980b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4499ed0eaa14SLiu Bo loop = false; 4500ed0eaa14SLiu Bo goto again; 4501ed0eaa14SLiu Bo } 4502ed0eaa14SLiu Bo 4503acce952bSliubo return 0; 4504acce952bSliubo } 4505acce952bSliubo 4506c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4507c79a1751SLiu Bo { 4508c79a1751SLiu Bo struct inode *inode; 4509c79a1751SLiu Bo 4510c79a1751SLiu Bo inode = cache->io_ctl.inode; 4511c79a1751SLiu Bo if (inode) { 4512c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4513c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4514c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4515c79a1751SLiu Bo iput(inode); 4516c79a1751SLiu Bo } 4517c79a1751SLiu Bo btrfs_put_block_group(cache); 4518c79a1751SLiu Bo } 4519c79a1751SLiu Bo 4520c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45212ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4522c79a1751SLiu Bo { 4523c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4524c79a1751SLiu Bo 4525c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4526c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4527c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4528c79a1751SLiu Bo struct btrfs_block_group_cache, 4529c79a1751SLiu Bo dirty_list); 4530c79a1751SLiu Bo if (!cache) { 45310b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group dirty_bgs list"); 4532c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4533c79a1751SLiu Bo return; 4534c79a1751SLiu Bo } 4535c79a1751SLiu Bo 4536c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4537c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4538c79a1751SLiu Bo list_del_init(&cache->io_list); 4539c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4540c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4541c79a1751SLiu Bo } 4542c79a1751SLiu Bo 4543c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4544c79a1751SLiu Bo spin_lock(&cache->lock); 4545c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4546c79a1751SLiu Bo spin_unlock(&cache->lock); 4547c79a1751SLiu Bo 4548c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4549c79a1751SLiu Bo btrfs_put_block_group(cache); 4550c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4551c79a1751SLiu Bo } 4552c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4553c79a1751SLiu Bo 4554c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4555c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4556c79a1751SLiu Bo struct btrfs_block_group_cache, 4557c79a1751SLiu Bo io_list); 4558c79a1751SLiu Bo if (!cache) { 45590b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group on io_bgs list"); 4560c79a1751SLiu Bo return; 4561c79a1751SLiu Bo } 4562c79a1751SLiu Bo 4563c79a1751SLiu Bo list_del_init(&cache->io_list); 4564c79a1751SLiu Bo spin_lock(&cache->lock); 4565c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4566c79a1751SLiu Bo spin_unlock(&cache->lock); 4567c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4568c79a1751SLiu Bo } 4569c79a1751SLiu Bo } 4570c79a1751SLiu Bo 457149b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 457349b25e05SJeff Mahoney { 45742ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4575c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4576c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4577c79a1751SLiu Bo 45782ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 457949b25e05SJeff Mahoney 45804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45810b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 458249b25e05SJeff Mahoney 45834a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45840b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 458549b25e05SJeff Mahoney 4586ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4587ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 458867cde344SMiao Xie 45892ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 459049b25e05SJeff Mahoney EXTENT_DIRTY); 45912ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45920b246afaSJeff Mahoney fs_info->pinned_extents); 459349b25e05SJeff Mahoney 45944a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45954a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 459649b25e05SJeff Mahoney } 459749b25e05SJeff Mahoney 45982ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4599acce952bSliubo { 4600acce952bSliubo struct btrfs_transaction *t; 4601acce952bSliubo 46020b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4603acce952bSliubo 46040b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46050b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46060b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4607724e2315SJosef Bacik struct btrfs_transaction, list); 4608724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 46099b64f57dSElena Reshetova refcount_inc(&t->use_count); 46100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46112ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4612724e2315SJosef Bacik btrfs_put_transaction(t); 46130b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4614724e2315SJosef Bacik continue; 4615724e2315SJosef Bacik } 46160b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4617724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46180b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4619724e2315SJosef Bacik /* 4620724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4621724e2315SJosef Bacik * handle open currently for this transaction. 4622724e2315SJosef Bacik */ 4623724e2315SJosef Bacik wait_event(t->writer_wait, 4624724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4625724e2315SJosef Bacik } else { 46260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4627724e2315SJosef Bacik } 46282ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4629724e2315SJosef Bacik 46300b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46310b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46320b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4633724e2315SJosef Bacik list_del_init(&t->list); 46340b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4635a4abeea4SJosef Bacik 4636724e2315SJosef Bacik btrfs_put_transaction(t); 46372ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4639724e2315SJosef Bacik } 46400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46410b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4642ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4643ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46442ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 46450b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46460b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4647acce952bSliubo 4648acce952bSliubo return 0; 4649acce952bSliubo } 4650acce952bSliubo 4651c6100a4bSJosef Bacik static struct btrfs_fs_info *btree_fs_info(void *private_data) 4652c6100a4bSJosef Bacik { 4653c6100a4bSJosef Bacik struct inode *inode = private_data; 4654c6100a4bSJosef Bacik return btrfs_sb(inode->i_sb); 4655c6100a4bSJosef Bacik } 4656c6100a4bSJosef Bacik 4657e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 46584d53dddbSDavid Sterba /* mandatory callbacks */ 46590b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 46604d53dddbSDavid Sterba .readpage_end_io_hook = btree_readpage_end_io_hook, 4661239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4662239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 466320a7db8aSDavid Sterba .readpage_io_failed_hook = btree_io_failed_hook, 4664c6100a4bSJosef Bacik .set_range_writeback = btrfs_set_range_writeback, 4665c6100a4bSJosef Bacik .tree_fs_info = btree_fs_info, 46664d53dddbSDavid Sterba 46674d53dddbSDavid Sterba /* optional callbacks */ 46680da5468fSChris Mason }; 4669