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); 67fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 68acce952bSliubo int read_only); 69143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 70acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 712ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info); 72143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 732ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 74acce952bSliubo struct extent_io_tree *dirty_pages, 75acce952bSliubo int mark); 762ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 77acce952bSliubo struct extent_io_tree *pinned_extents); 782ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info); 792ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info); 80ce9adaa5SChris Mason 81d352ac68SChris Mason /* 8297eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 8397eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 84d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 85d352ac68SChris Mason */ 8697eb6b69SDavid Sterba struct btrfs_end_io_wq { 87ce9adaa5SChris Mason struct bio *bio; 88ce9adaa5SChris Mason bio_end_io_t *end_io; 89ce9adaa5SChris Mason void *private; 90ce9adaa5SChris Mason struct btrfs_fs_info *info; 91ce9adaa5SChris Mason int error; 92bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 93ce9adaa5SChris Mason struct list_head list; 948b712842SChris Mason struct btrfs_work work; 95ce9adaa5SChris Mason }; 960da5468fSChris Mason 9797eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 9897eb6b69SDavid Sterba 9997eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 10097eb6b69SDavid Sterba { 10197eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 10297eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 10397eb6b69SDavid Sterba 0, 104fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 10597eb6b69SDavid Sterba NULL); 10697eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 10797eb6b69SDavid Sterba return -ENOMEM; 10897eb6b69SDavid Sterba return 0; 10997eb6b69SDavid Sterba } 11097eb6b69SDavid Sterba 11197eb6b69SDavid Sterba void btrfs_end_io_wq_exit(void) 11297eb6b69SDavid Sterba { 11397eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 11497eb6b69SDavid Sterba } 11597eb6b69SDavid Sterba 116d352ac68SChris Mason /* 117d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 118d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 119d352ac68SChris Mason * just before they are sent down the IO stack. 120d352ac68SChris Mason */ 12144b8bd7eSChris Mason struct async_submit_bio { 12244b8bd7eSChris Mason struct inode *inode; 12344b8bd7eSChris Mason struct bio *bio; 12444b8bd7eSChris Mason struct list_head list; 1254a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1264a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 12744b8bd7eSChris Mason int mirror_num; 128c8b97818SChris Mason unsigned long bio_flags; 129eaf25d93SChris Mason /* 130eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 131eaf25d93SChris Mason * can't tell us where in the file the bio should go 132eaf25d93SChris Mason */ 133eaf25d93SChris Mason u64 bio_offset; 1348b712842SChris Mason struct btrfs_work work; 13579787eaaSJeff Mahoney int error; 13644b8bd7eSChris Mason }; 13744b8bd7eSChris Mason 13885d4e461SChris Mason /* 13985d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 14085d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 14185d4e461SChris Mason * the level the eb occupies in the tree. 1424008c04aSChris Mason * 14385d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 14485d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 14585d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 14685d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 14785d4e461SChris Mason * have separate keysets. 1484008c04aSChris Mason * 14985d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 15085d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 15185d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1524008c04aSChris Mason * 15385d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 15485d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 15585d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 15685d4e461SChris Mason * 15785d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 15885d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 15985d4e461SChris Mason * needs update as well. 1604008c04aSChris Mason */ 1614008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1624008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1634008c04aSChris Mason # error 1644008c04aSChris Mason # endif 16585d4e461SChris Mason 16685d4e461SChris Mason static struct btrfs_lockdep_keyset { 16785d4e461SChris Mason u64 id; /* root objectid */ 16885d4e461SChris Mason const char *name_stem; /* lock name stem */ 16985d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 17085d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 17185d4e461SChris Mason } btrfs_lockdep_keysets[] = { 17285d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 17385d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 17485d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 17585d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 17685d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 17785d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 17860b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 17985d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 18085d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 18185d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 18213fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 1836b20e0adSDavid Sterba { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" }, 18485d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1854008c04aSChris Mason }; 18685d4e461SChris Mason 18785d4e461SChris Mason void __init btrfs_init_lockdep(void) 18885d4e461SChris Mason { 18985d4e461SChris Mason int i, j; 19085d4e461SChris Mason 19185d4e461SChris Mason /* initialize lockdep class names */ 19285d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 19385d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 19485d4e461SChris Mason 19585d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 19685d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 19785d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 19885d4e461SChris Mason } 19985d4e461SChris Mason } 20085d4e461SChris Mason 20185d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 20285d4e461SChris Mason int level) 20385d4e461SChris Mason { 20485d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 20585d4e461SChris Mason 20685d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 20785d4e461SChris Mason 20885d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 20985d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 21085d4e461SChris Mason if (ks->id == objectid) 21185d4e461SChris Mason break; 21285d4e461SChris Mason 21385d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 21485d4e461SChris Mason &ks->keys[level], ks->names[level]); 21585d4e461SChris Mason } 21685d4e461SChris Mason 2174008c04aSChris Mason #endif 2184008c04aSChris Mason 219d352ac68SChris Mason /* 220d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 221d352ac68SChris Mason * that covers the entire device 222d352ac68SChris Mason */ 223b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 224306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2255f39d397SChris Mason int create) 2265f39d397SChris Mason { 2270b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2285f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2295f39d397SChris Mason struct extent_map *em; 2305f39d397SChris Mason int ret; 2315f39d397SChris Mason 232890871beSChris Mason read_lock(&em_tree->lock); 233d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 234a061fc8dSChris Mason if (em) { 2350b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 236890871beSChris Mason read_unlock(&em_tree->lock); 2375f39d397SChris Mason goto out; 238a061fc8dSChris Mason } 239890871beSChris Mason read_unlock(&em_tree->lock); 2407b13b7b1SChris Mason 241172ddd60SDavid Sterba em = alloc_extent_map(); 2425f39d397SChris Mason if (!em) { 2435f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2445f39d397SChris Mason goto out; 2455f39d397SChris Mason } 2465f39d397SChris Mason em->start = 0; 2470afbaf8cSChris Mason em->len = (u64)-1; 248c8b97818SChris Mason em->block_len = (u64)-1; 2495f39d397SChris Mason em->block_start = 0; 2500b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 251d1310b2eSChris Mason 252890871beSChris Mason write_lock(&em_tree->lock); 25309a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2545f39d397SChris Mason if (ret == -EEXIST) { 2555f39d397SChris Mason free_extent_map(em); 2567b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 257b4f359abSTsutomu Itoh if (!em) 2580433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2595f39d397SChris Mason } else if (ret) { 2607b13b7b1SChris Mason free_extent_map(em); 2610433f20dSTsutomu Itoh em = ERR_PTR(ret); 2625f39d397SChris Mason } 263890871beSChris Mason write_unlock(&em_tree->lock); 2647b13b7b1SChris Mason 2655f39d397SChris Mason out: 2665f39d397SChris Mason return em; 2675f39d397SChris Mason } 2685f39d397SChris Mason 269b0496686SLiu Bo u32 btrfs_csum_data(char *data, u32 seed, size_t len) 27019c00ddcSChris Mason { 2710b947affSFilipe David Borba Manana return btrfs_crc32c(seed, data, len); 27219c00ddcSChris Mason } 27319c00ddcSChris Mason 2740b5e3dafSDomagoj Tršan void btrfs_csum_final(u32 crc, u8 *result) 27519c00ddcSChris Mason { 2767e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 27719c00ddcSChris Mason } 27819c00ddcSChris Mason 279d352ac68SChris Mason /* 280d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 281d352ac68SChris Mason * into the csum field of the block. 282d352ac68SChris Mason */ 28301d58472SDaniel Dressler static int csum_tree_block(struct btrfs_fs_info *fs_info, 28401d58472SDaniel Dressler struct extent_buffer *buf, 28519c00ddcSChris Mason int verify) 28619c00ddcSChris Mason { 28701d58472SDaniel Dressler u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 288607d432dSJosef Bacik char *result = NULL; 28919c00ddcSChris Mason unsigned long len; 29019c00ddcSChris Mason unsigned long cur_len; 29119c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 29219c00ddcSChris Mason char *kaddr; 29319c00ddcSChris Mason unsigned long map_start; 29419c00ddcSChris Mason unsigned long map_len; 29519c00ddcSChris Mason int err; 29619c00ddcSChris Mason u32 crc = ~(u32)0; 297607d432dSJosef Bacik unsigned long inline_result; 29819c00ddcSChris Mason 29919c00ddcSChris Mason len = buf->len - offset; 30019c00ddcSChris Mason while (len > 0) { 30119c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 302a6591715SChris Mason &kaddr, &map_start, &map_len); 303d397712bSChris Mason if (err) 3048bd98f0eSAlex Lyakas return err; 30519c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 306b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 30719c00ddcSChris Mason crc, cur_len); 30819c00ddcSChris Mason len -= cur_len; 30919c00ddcSChris Mason offset += cur_len; 31019c00ddcSChris Mason } 311607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 31231e818feSDavid Sterba result = kzalloc(csum_size, GFP_NOFS); 313607d432dSJosef Bacik if (!result) 3148bd98f0eSAlex Lyakas return -ENOMEM; 315607d432dSJosef Bacik } else { 316607d432dSJosef Bacik result = (char *)&inline_result; 317607d432dSJosef Bacik } 318607d432dSJosef Bacik 31919c00ddcSChris Mason btrfs_csum_final(crc, result); 32019c00ddcSChris Mason 32119c00ddcSChris Mason if (verify) { 322607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 323e4204dedSChris Mason u32 val; 324e4204dedSChris Mason u32 found = 0; 325607d432dSJosef Bacik memcpy(&found, result, csum_size); 326e4204dedSChris Mason 327607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 32894647322SDavid Sterba btrfs_warn_rl(fs_info, 3295d163e0eSJeff Mahoney "%s checksum verify failed on %llu wanted %X found %X level %d", 33001d58472SDaniel Dressler fs_info->sb->s_id, buf->start, 331c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 332607d432dSJosef Bacik if (result != (char *)&inline_result) 333607d432dSJosef Bacik kfree(result); 3348bd98f0eSAlex Lyakas return -EUCLEAN; 33519c00ddcSChris Mason } 33619c00ddcSChris Mason } else { 337607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 33819c00ddcSChris Mason } 339607d432dSJosef Bacik if (result != (char *)&inline_result) 340607d432dSJosef Bacik kfree(result); 34119c00ddcSChris Mason return 0; 34219c00ddcSChris Mason } 34319c00ddcSChris Mason 344d352ac68SChris Mason /* 345d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 346d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 347d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 348d352ac68SChris Mason * in the wrong place. 349d352ac68SChris Mason */ 3501259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 351b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 352b9fab919SChris Mason int atomic) 3531259ab75SChris Mason { 3542ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3551259ab75SChris Mason int ret; 3562755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3571259ab75SChris Mason 3581259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3591259ab75SChris Mason return 0; 3601259ab75SChris Mason 361b9fab919SChris Mason if (atomic) 362b9fab919SChris Mason return -EAGAIN; 363b9fab919SChris Mason 364a26e8c9fSJosef Bacik if (need_lock) { 365a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 366a26e8c9fSJosef Bacik btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 367a26e8c9fSJosef Bacik } 368a26e8c9fSJosef Bacik 3692ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 370ff13db41SDavid Sterba &cached_state); 3710b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3721259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3731259ab75SChris Mason ret = 0; 3741259ab75SChris Mason goto out; 3751259ab75SChris Mason } 37694647322SDavid Sterba btrfs_err_rl(eb->fs_info, 37794647322SDavid Sterba "parent transid verify failed on %llu wanted %llu found %llu", 37894647322SDavid Sterba eb->start, 37929549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3801259ab75SChris Mason ret = 1; 381a26e8c9fSJosef Bacik 382a26e8c9fSJosef Bacik /* 383a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 384a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 38501327610SNicholas D Steeves * block that has been freed and re-allocated. So don't clear uptodate 386a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 387a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 388a26e8c9fSJosef Bacik * making everybody very sad. 389a26e8c9fSJosef Bacik */ 390a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3910b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 39233958dc6SChris Mason out: 3932ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3942ac55d41SJosef Bacik &cached_state, GFP_NOFS); 395472b909fSJosef Bacik if (need_lock) 396a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3971259ab75SChris Mason return ret; 3981259ab75SChris Mason } 3991259ab75SChris Mason 400d352ac68SChris Mason /* 4011104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 4021104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 4031104a885SDavid Sterba */ 404ab8d0fc4SJeff Mahoney static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 405ab8d0fc4SJeff Mahoney char *raw_disk_sb) 4061104a885SDavid Sterba { 4071104a885SDavid Sterba struct btrfs_super_block *disk_sb = 4081104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 4091104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 4101104a885SDavid Sterba int ret = 0; 4111104a885SDavid Sterba 4121104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 4131104a885SDavid Sterba u32 crc = ~(u32)0; 4141104a885SDavid Sterba const int csum_size = sizeof(crc); 4151104a885SDavid Sterba char result[csum_size]; 4161104a885SDavid Sterba 4171104a885SDavid Sterba /* 4181104a885SDavid Sterba * The super_block structure does not span the whole 4191104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 42001327610SNicholas D Steeves * is filled with zeros and is included in the checksum. 4211104a885SDavid Sterba */ 4221104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4231104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4241104a885SDavid Sterba btrfs_csum_final(crc, result); 4251104a885SDavid Sterba 4261104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 4271104a885SDavid Sterba ret = 1; 4281104a885SDavid Sterba } 4291104a885SDavid Sterba 4301104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 431ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "unsupported checksum algorithm %u", 4321104a885SDavid Sterba csum_type); 4331104a885SDavid Sterba ret = 1; 4341104a885SDavid Sterba } 4351104a885SDavid Sterba 4361104a885SDavid Sterba return ret; 4371104a885SDavid Sterba } 4381104a885SDavid Sterba 4391104a885SDavid Sterba /* 440d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 441d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 442d352ac68SChris Mason */ 4432ff7e61eSJeff Mahoney static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info, 444f188591eSChris Mason struct extent_buffer *eb, 4458436ea91SJosef Bacik u64 parent_transid) 446f188591eSChris Mason { 447f188591eSChris Mason struct extent_io_tree *io_tree; 448ea466794SJosef Bacik int failed = 0; 449f188591eSChris Mason int ret; 450f188591eSChris Mason int num_copies = 0; 451f188591eSChris Mason int mirror_num = 0; 452ea466794SJosef Bacik int failed_mirror = 0; 453f188591eSChris Mason 454a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 4550b246afaSJeff Mahoney io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; 456f188591eSChris Mason while (1) { 4578436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 458f188591eSChris Mason btree_get_extent, mirror_num); 459256dd1bbSStefan Behrens if (!ret) { 460256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 461b9fab919SChris Mason parent_transid, 0)) 462ea466794SJosef Bacik break; 463256dd1bbSStefan Behrens else 464256dd1bbSStefan Behrens ret = -EIO; 465256dd1bbSStefan Behrens } 466d397712bSChris Mason 467a826d6dcSJosef Bacik /* 468a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 469a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 470a826d6dcSJosef Bacik * any less wrong. 471a826d6dcSJosef Bacik */ 472a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 473ea466794SJosef Bacik break; 474ea466794SJosef Bacik 4750b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info, 476f188591eSChris Mason eb->start, eb->len); 4774235298eSChris Mason if (num_copies == 1) 478ea466794SJosef Bacik break; 4794235298eSChris Mason 4805cf1ab56SJosef Bacik if (!failed_mirror) { 4815cf1ab56SJosef Bacik failed = 1; 4825cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4835cf1ab56SJosef Bacik } 4845cf1ab56SJosef Bacik 485f188591eSChris Mason mirror_num++; 486ea466794SJosef Bacik if (mirror_num == failed_mirror) 487ea466794SJosef Bacik mirror_num++; 488ea466794SJosef Bacik 4894235298eSChris Mason if (mirror_num > num_copies) 490ea466794SJosef Bacik break; 491f188591eSChris Mason } 492ea466794SJosef Bacik 493c0901581SStefan Behrens if (failed && !ret && failed_mirror) 4942ff7e61eSJeff Mahoney repair_eb_io_failure(fs_info, eb, failed_mirror); 495ea466794SJosef Bacik 496ea466794SJosef Bacik return ret; 497f188591eSChris Mason } 49819c00ddcSChris Mason 499d352ac68SChris Mason /* 500d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 501d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 502d352ac68SChris Mason */ 503d397712bSChris Mason 50401d58472SDaniel Dressler static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page) 50519c00ddcSChris Mason { 5064eee4fa4SMiao Xie u64 start = page_offset(page); 50719c00ddcSChris Mason u64 found_start; 50819c00ddcSChris Mason struct extent_buffer *eb; 509f188591eSChris Mason 5104f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5114f2de97aSJosef Bacik if (page != eb->pages[0]) 5124f2de97aSJosef Bacik return 0; 5130f805531SAlex Lyakas 51419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 5150f805531SAlex Lyakas /* 5160f805531SAlex Lyakas * Please do not consolidate these warnings into a single if. 5170f805531SAlex Lyakas * It is useful to know what went wrong. 5180f805531SAlex Lyakas */ 5190f805531SAlex Lyakas if (WARN_ON(found_start != start)) 5200f805531SAlex Lyakas return -EUCLEAN; 5210f805531SAlex Lyakas if (WARN_ON(!PageUptodate(page))) 5220f805531SAlex Lyakas return -EUCLEAN; 5230f805531SAlex Lyakas 5240f805531SAlex Lyakas ASSERT(memcmp_extent_buffer(eb, fs_info->fsid, 5250f805531SAlex Lyakas btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0); 5260f805531SAlex Lyakas 5278bd98f0eSAlex Lyakas return csum_tree_block(fs_info, eb, 0); 52819c00ddcSChris Mason } 52919c00ddcSChris Mason 53001d58472SDaniel Dressler static int check_tree_block_fsid(struct btrfs_fs_info *fs_info, 5312b82032cSYan Zheng struct extent_buffer *eb) 5322b82032cSYan Zheng { 53301d58472SDaniel Dressler struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 5342b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 5352b82032cSYan Zheng int ret = 1; 5362b82032cSYan Zheng 5370a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5382b82032cSYan Zheng while (fs_devices) { 5392b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5402b82032cSYan Zheng ret = 0; 5412b82032cSYan Zheng break; 5422b82032cSYan Zheng } 5432b82032cSYan Zheng fs_devices = fs_devices->seed; 5442b82032cSYan Zheng } 5452b82032cSYan Zheng return ret; 5462b82032cSYan Zheng } 5472b82032cSYan Zheng 548a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 5490b246afaSJeff Mahoney btrfs_crit(root->fs_info, \ 5500b246afaSJeff Mahoney "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \ 5516b722c17SLiu Bo btrfs_header_level(eb) == 0 ? "leaf" : "node", \ 5526b722c17SLiu Bo reason, btrfs_header_bytenr(eb), root->objectid, slot) 553a826d6dcSJosef Bacik 554a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 555a826d6dcSJosef Bacik struct extent_buffer *leaf) 556a826d6dcSJosef Bacik { 5570b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 558a826d6dcSJosef Bacik struct btrfs_key key; 559a826d6dcSJosef Bacik struct btrfs_key leaf_key; 560a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 561a826d6dcSJosef Bacik int slot; 562a826d6dcSJosef Bacik 5631ba98d08SLiu Bo if (nritems == 0) { 5641ba98d08SLiu Bo struct btrfs_root *check_root; 5651ba98d08SLiu Bo 5661ba98d08SLiu Bo key.objectid = btrfs_header_owner(leaf); 5671ba98d08SLiu Bo key.type = BTRFS_ROOT_ITEM_KEY; 5681ba98d08SLiu Bo key.offset = (u64)-1; 5691ba98d08SLiu Bo 5700b246afaSJeff Mahoney check_root = btrfs_get_fs_root(fs_info, &key, false); 5711ba98d08SLiu Bo /* 5721ba98d08SLiu Bo * The only reason we also check NULL here is that during 5731ba98d08SLiu Bo * open_ctree() some roots has not yet been set up. 5741ba98d08SLiu Bo */ 5751ba98d08SLiu Bo if (!IS_ERR_OR_NULL(check_root)) { 5761ba98d08SLiu Bo /* if leaf is the root, then it's fine */ 5771ba98d08SLiu Bo if (leaf->start != 5781ba98d08SLiu Bo btrfs_root_bytenr(&check_root->root_item)) { 5791ba98d08SLiu Bo CORRUPT("non-root leaf's nritems is 0", 5801ba98d08SLiu Bo leaf, root, 0); 5811ba98d08SLiu Bo return -EIO; 5821ba98d08SLiu Bo } 5831ba98d08SLiu Bo } 584a826d6dcSJosef Bacik return 0; 5851ba98d08SLiu Bo } 586a826d6dcSJosef Bacik 587a826d6dcSJosef Bacik /* Check the 0 item */ 588a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 5890b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 590a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 591a826d6dcSJosef Bacik return -EIO; 592a826d6dcSJosef Bacik } 593a826d6dcSJosef Bacik 594a826d6dcSJosef Bacik /* 595a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 596a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 597a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 598a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 599a826d6dcSJosef Bacik * offset is correct. 600a826d6dcSJosef Bacik */ 601a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 602a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 603a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 604a826d6dcSJosef Bacik 605a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 606a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 607a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 608a826d6dcSJosef Bacik return -EIO; 609a826d6dcSJosef Bacik } 610a826d6dcSJosef Bacik 611a826d6dcSJosef Bacik /* 612a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 613a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 614a826d6dcSJosef Bacik * front. 615a826d6dcSJosef Bacik */ 616a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 617a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 618a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 619a826d6dcSJosef Bacik return -EIO; 620a826d6dcSJosef Bacik } 621a826d6dcSJosef Bacik 622a826d6dcSJosef Bacik /* 623a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 624a826d6dcSJosef Bacik * just in case all the items are consistent to each other, but 625a826d6dcSJosef Bacik * all point outside of the leaf. 626a826d6dcSJosef Bacik */ 627a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 6280b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 629a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 630a826d6dcSJosef Bacik return -EIO; 631a826d6dcSJosef Bacik } 632a826d6dcSJosef Bacik } 633a826d6dcSJosef Bacik 634a826d6dcSJosef Bacik return 0; 635a826d6dcSJosef Bacik } 636a826d6dcSJosef Bacik 637053ab70fSLiu Bo static int check_node(struct btrfs_root *root, struct extent_buffer *node) 638053ab70fSLiu Bo { 639053ab70fSLiu Bo unsigned long nr = btrfs_header_nritems(node); 6406b722c17SLiu Bo struct btrfs_key key, next_key; 6416b722c17SLiu Bo int slot; 6426b722c17SLiu Bo u64 bytenr; 6436b722c17SLiu Bo int ret = 0; 644053ab70fSLiu Bo 645da17066cSJeff Mahoney if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) { 646053ab70fSLiu Bo btrfs_crit(root->fs_info, 647053ab70fSLiu Bo "corrupt node: block %llu root %llu nritems %lu", 648053ab70fSLiu Bo node->start, root->objectid, nr); 649053ab70fSLiu Bo return -EIO; 650053ab70fSLiu Bo } 6516b722c17SLiu Bo 6526b722c17SLiu Bo for (slot = 0; slot < nr - 1; slot++) { 6536b722c17SLiu Bo bytenr = btrfs_node_blockptr(node, slot); 6546b722c17SLiu Bo btrfs_node_key_to_cpu(node, &key, slot); 6556b722c17SLiu Bo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 6566b722c17SLiu Bo 6576b722c17SLiu Bo if (!bytenr) { 6586b722c17SLiu Bo CORRUPT("invalid item slot", node, root, slot); 6596b722c17SLiu Bo ret = -EIO; 6606b722c17SLiu Bo goto out; 6616b722c17SLiu Bo } 6626b722c17SLiu Bo 6636b722c17SLiu Bo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 6646b722c17SLiu Bo CORRUPT("bad key order", node, root, slot); 6656b722c17SLiu Bo ret = -EIO; 6666b722c17SLiu Bo goto out; 6676b722c17SLiu Bo } 6686b722c17SLiu Bo } 6696b722c17SLiu Bo out: 6706b722c17SLiu Bo return ret; 671053ab70fSLiu Bo } 672053ab70fSLiu Bo 673facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 674facc8a22SMiao Xie u64 phy_offset, struct page *page, 675facc8a22SMiao Xie u64 start, u64 end, int mirror) 676ce9adaa5SChris Mason { 677ce9adaa5SChris Mason u64 found_start; 678ce9adaa5SChris Mason int found_level; 679ce9adaa5SChris Mason struct extent_buffer *eb; 680ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 68102873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 682f188591eSChris Mason int ret = 0; 683727011e0SChris Mason int reads_done; 684ce9adaa5SChris Mason 685ce9adaa5SChris Mason if (!page->private) 686ce9adaa5SChris Mason goto out; 687d397712bSChris Mason 6884f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 689d397712bSChris Mason 6900b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6910b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6920b32f4bbSJosef Bacik */ 6930b32f4bbSJosef Bacik extent_buffer_get(eb); 6940b32f4bbSJosef Bacik 6950b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 696727011e0SChris Mason if (!reads_done) 697727011e0SChris Mason goto err; 698f188591eSChris Mason 6995cf1ab56SJosef Bacik eb->read_mirror = mirror; 700656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 701ea466794SJosef Bacik ret = -EIO; 702ea466794SJosef Bacik goto err; 703ea466794SJosef Bacik } 704ea466794SJosef Bacik 705ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 706727011e0SChris Mason if (found_start != eb->start) { 70702873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 70894647322SDavid Sterba found_start, eb->start); 709f188591eSChris Mason ret = -EIO; 710ce9adaa5SChris Mason goto err; 711ce9adaa5SChris Mason } 71202873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 71302873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 71494647322SDavid Sterba eb->start); 7151259ab75SChris Mason ret = -EIO; 7161259ab75SChris Mason goto err; 7171259ab75SChris Mason } 718ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 7191c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 72002873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 7211c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 7221c24c3ceSJosef Bacik ret = -EIO; 7231c24c3ceSJosef Bacik goto err; 7241c24c3ceSJosef Bacik } 725ce9adaa5SChris Mason 72685d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 72785d4e461SChris Mason eb, found_level); 7284008c04aSChris Mason 72902873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 7308bd98f0eSAlex Lyakas if (ret) 731a826d6dcSJosef Bacik goto err; 732a826d6dcSJosef Bacik 733a826d6dcSJosef Bacik /* 734a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 735a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 736a826d6dcSJosef Bacik * return -EIO. 737a826d6dcSJosef Bacik */ 738a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 739a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 740a826d6dcSJosef Bacik ret = -EIO; 741a826d6dcSJosef Bacik } 742ce9adaa5SChris Mason 743053ab70fSLiu Bo if (found_level > 0 && check_node(root, eb)) 744053ab70fSLiu Bo ret = -EIO; 745053ab70fSLiu Bo 7460b32f4bbSJosef Bacik if (!ret) 7470b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 748ce9adaa5SChris Mason err: 74979fb65a1SJosef Bacik if (reads_done && 75079fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 751fc2e901fSDavid Sterba btree_readahead_hook(fs_info, eb, ret); 7524bb31e92SArne Jansen 75353b381b3SDavid Woodhouse if (ret) { 75453b381b3SDavid Woodhouse /* 75553b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 75653b381b3SDavid Woodhouse * again, we have to make sure it has something 75753b381b3SDavid Woodhouse * to decrement 75853b381b3SDavid Woodhouse */ 75953b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7600b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 76153b381b3SDavid Woodhouse } 7620b32f4bbSJosef Bacik free_extent_buffer(eb); 763ce9adaa5SChris Mason out: 764f188591eSChris Mason return ret; 765ce9adaa5SChris Mason } 766ce9adaa5SChris Mason 767ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 7684bb31e92SArne Jansen { 7694bb31e92SArne Jansen struct extent_buffer *eb; 7704bb31e92SArne Jansen 7714f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 772656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 7735cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 77453b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 775ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 776fc2e901fSDavid Sterba btree_readahead_hook(eb->fs_info, eb, -EIO); 7774bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 7784bb31e92SArne Jansen } 7794bb31e92SArne Jansen 7804246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 781ce9adaa5SChris Mason { 78297eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 783ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7849e0af237SLiu Bo struct btrfs_workqueue *wq; 7859e0af237SLiu Bo btrfs_work_func_t func; 786ce9adaa5SChris Mason 787ce9adaa5SChris Mason fs_info = end_io_wq->info; 7884246a0b6SChristoph Hellwig end_io_wq->error = bio->bi_error; 789d20f7043SChris Mason 79037226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 7919e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 7929e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 7939e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 7949e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 7959e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 7969e0af237SLiu Bo func = btrfs_freespace_write_helper; 7979e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7989e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7999e0af237SLiu Bo func = btrfs_endio_raid56_helper; 800d20f7043SChris Mason } else { 8019e0af237SLiu Bo wq = fs_info->endio_write_workers; 8029e0af237SLiu Bo func = btrfs_endio_write_helper; 803d20f7043SChris Mason } 8049e0af237SLiu Bo } else { 8058b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 8068b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 8078b110e39SMiao Xie wq = fs_info->endio_repair_workers; 8088b110e39SMiao Xie func = btrfs_endio_repair_helper; 8098b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 8109e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 8119e0af237SLiu Bo func = btrfs_endio_raid56_helper; 8129e0af237SLiu Bo } else if (end_io_wq->metadata) { 8139e0af237SLiu Bo wq = fs_info->endio_meta_workers; 8149e0af237SLiu Bo func = btrfs_endio_meta_helper; 8159e0af237SLiu Bo } else { 8169e0af237SLiu Bo wq = fs_info->endio_workers; 8179e0af237SLiu Bo func = btrfs_endio_helper; 8189e0af237SLiu Bo } 8199e0af237SLiu Bo } 8209e0af237SLiu Bo 8219e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 8229e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 823ce9adaa5SChris Mason } 824ce9adaa5SChris Mason 82522c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 826bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 8270b86a832SChris Mason { 82897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 8298b110e39SMiao Xie 83097eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 831ce9adaa5SChris Mason if (!end_io_wq) 832ce9adaa5SChris Mason return -ENOMEM; 833ce9adaa5SChris Mason 834ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 835ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 83622c59948SChris Mason end_io_wq->info = info; 837ce9adaa5SChris Mason end_io_wq->error = 0; 838ce9adaa5SChris Mason end_io_wq->bio = bio; 83922c59948SChris Mason end_io_wq->metadata = metadata; 840ce9adaa5SChris Mason 841ce9adaa5SChris Mason bio->bi_private = end_io_wq; 842ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 84322c59948SChris Mason return 0; 84422c59948SChris Mason } 84522c59948SChris Mason 846b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 8474854ddd0SChris Mason { 8484854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 8495cdc7ad3SQu Wenruo info->thread_pool_size, 8504854ddd0SChris Mason info->fs_devices->open_devices); 8514854ddd0SChris Mason return 256 * limit; 8524854ddd0SChris Mason } 8534854ddd0SChris Mason 8544a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 8554a69a410SChris Mason { 8564a69a410SChris Mason struct async_submit_bio *async; 85779787eaaSJeff Mahoney int ret; 8584a69a410SChris Mason 8594a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 86081a75f67SMike Christie ret = async->submit_bio_start(async->inode, async->bio, 861eaf25d93SChris Mason async->mirror_num, async->bio_flags, 862eaf25d93SChris Mason async->bio_offset); 86379787eaaSJeff Mahoney if (ret) 86479787eaaSJeff Mahoney async->error = ret; 8654a69a410SChris Mason } 8664a69a410SChris Mason 8674a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8688b712842SChris Mason { 8698b712842SChris Mason struct btrfs_fs_info *fs_info; 8708b712842SChris Mason struct async_submit_bio *async; 8714854ddd0SChris Mason int limit; 8728b712842SChris Mason 8738b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8748b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 8754854ddd0SChris Mason 876b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 8774854ddd0SChris Mason limit = limit * 2 / 3; 8784854ddd0SChris Mason 879ee863954SDavid Sterba /* 880ee863954SDavid Sterba * atomic_dec_return implies a barrier for waitqueue_active 881ee863954SDavid Sterba */ 88266657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 883b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 8844854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 8854854ddd0SChris Mason 886bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 88779787eaaSJeff Mahoney if (async->error) { 8884246a0b6SChristoph Hellwig async->bio->bi_error = async->error; 8894246a0b6SChristoph Hellwig bio_endio(async->bio); 89079787eaaSJeff Mahoney return; 89179787eaaSJeff Mahoney } 89279787eaaSJeff Mahoney 89381a75f67SMike Christie async->submit_bio_done(async->inode, async->bio, async->mirror_num, 89481a75f67SMike Christie async->bio_flags, async->bio_offset); 8954a69a410SChris Mason } 8964a69a410SChris Mason 8974a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8984a69a410SChris Mason { 8994a69a410SChris Mason struct async_submit_bio *async; 9004a69a410SChris Mason 9014a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 9028b712842SChris Mason kfree(async); 9038b712842SChris Mason } 9048b712842SChris Mason 90544b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 90681a75f67SMike Christie struct bio *bio, int mirror_num, 907c8b97818SChris Mason unsigned long bio_flags, 908eaf25d93SChris Mason u64 bio_offset, 9094a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 9104a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 91144b8bd7eSChris Mason { 91244b8bd7eSChris Mason struct async_submit_bio *async; 91344b8bd7eSChris Mason 91444b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 91544b8bd7eSChris Mason if (!async) 91644b8bd7eSChris Mason return -ENOMEM; 91744b8bd7eSChris Mason 91844b8bd7eSChris Mason async->inode = inode; 91944b8bd7eSChris Mason async->bio = bio; 92044b8bd7eSChris Mason async->mirror_num = mirror_num; 9214a69a410SChris Mason async->submit_bio_start = submit_bio_start; 9224a69a410SChris Mason async->submit_bio_done = submit_bio_done; 9234a69a410SChris Mason 9249e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 9255cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 9264a69a410SChris Mason 927c8b97818SChris Mason async->bio_flags = bio_flags; 928eaf25d93SChris Mason async->bio_offset = bio_offset; 9298c8bee1dSChris Mason 93079787eaaSJeff Mahoney async->error = 0; 93179787eaaSJeff Mahoney 932cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 933d313d7a3SChris Mason 9341eff9d32SJens Axboe if (bio->bi_opf & REQ_SYNC) 9355cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 936d313d7a3SChris Mason 9375cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 9389473f16cSChris Mason 939771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 940771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 941771ed689SChris Mason wait_event(fs_info->async_submit_wait, 942771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 943771ed689SChris Mason } 944771ed689SChris Mason 94544b8bd7eSChris Mason return 0; 94644b8bd7eSChris Mason } 94744b8bd7eSChris Mason 948ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 949ce3ed71aSChris Mason { 9502c30c71bSKent Overstreet struct bio_vec *bvec; 951ce3ed71aSChris Mason struct btrfs_root *root; 9522c30c71bSKent Overstreet int i, ret = 0; 953ce3ed71aSChris Mason 9542c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 955ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 95601d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 95779787eaaSJeff Mahoney if (ret) 95879787eaaSJeff Mahoney break; 959ce3ed71aSChris Mason } 9602c30c71bSKent Overstreet 96179787eaaSJeff Mahoney return ret; 962ce3ed71aSChris Mason } 963ce3ed71aSChris Mason 96481a75f67SMike Christie static int __btree_submit_bio_start(struct inode *inode, struct bio *bio, 96581a75f67SMike Christie int mirror_num, unsigned long bio_flags, 966eaf25d93SChris Mason u64 bio_offset) 96722c59948SChris Mason { 9688b712842SChris Mason /* 9698b712842SChris Mason * when we're called for a write, we're already in the async 9705443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9718b712842SChris Mason */ 97279787eaaSJeff Mahoney return btree_csum_one_bio(bio); 97322c59948SChris Mason } 97422c59948SChris Mason 97581a75f67SMike Christie static int __btree_submit_bio_done(struct inode *inode, struct bio *bio, 976eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 977eaf25d93SChris Mason u64 bio_offset) 9784a69a410SChris Mason { 97961891923SStefan Behrens int ret; 98061891923SStefan Behrens 9818b712842SChris Mason /* 9824a69a410SChris Mason * when we're called for a write, we're already in the async 9834a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 9848b712842SChris Mason */ 9852ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 9864246a0b6SChristoph Hellwig if (ret) { 9874246a0b6SChristoph Hellwig bio->bi_error = ret; 9884246a0b6SChristoph Hellwig bio_endio(bio); 9894246a0b6SChristoph Hellwig } 99061891923SStefan Behrens return ret; 9910b86a832SChris Mason } 9920b86a832SChris Mason 993de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 994de0022b9SJosef Bacik { 995de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 996de0022b9SJosef Bacik return 0; 997de0022b9SJosef Bacik #ifdef CONFIG_X86 998bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 999de0022b9SJosef Bacik return 0; 1000de0022b9SJosef Bacik #endif 1001de0022b9SJosef Bacik return 1; 1002de0022b9SJosef Bacik } 1003de0022b9SJosef Bacik 100481a75f67SMike Christie static int btree_submit_bio_hook(struct inode *inode, struct bio *bio, 1005eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 1006eaf25d93SChris Mason u64 bio_offset) 100744b8bd7eSChris Mason { 10080b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1009de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 10104a69a410SChris Mason int ret; 1011cad321adSChris Mason 101237226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 1013cad321adSChris Mason /* 1014cad321adSChris Mason * called for a read, do the setup so that checksum validation 1015cad321adSChris Mason * can happen in the async kernel threads 1016cad321adSChris Mason */ 10170b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 10180b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 10191d4284bdSChris Mason if (ret) 102061891923SStefan Behrens goto out_w_error; 10212ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 1022de0022b9SJosef Bacik } else if (!async) { 1023de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 1024de0022b9SJosef Bacik if (ret) 102561891923SStefan Behrens goto out_w_error; 10262ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 102761891923SStefan Behrens } else { 1028cad321adSChris Mason /* 102961891923SStefan Behrens * kthread helpers are used to submit writes so that 103061891923SStefan Behrens * checksumming can happen in parallel across all CPUs 1031cad321adSChris Mason */ 10320b246afaSJeff Mahoney ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, 0, 1033eaf25d93SChris Mason bio_offset, 10344a69a410SChris Mason __btree_submit_bio_start, 10354a69a410SChris Mason __btree_submit_bio_done); 103644b8bd7eSChris Mason } 103744b8bd7eSChris Mason 10384246a0b6SChristoph Hellwig if (ret) 10394246a0b6SChristoph Hellwig goto out_w_error; 10404246a0b6SChristoph Hellwig return 0; 10414246a0b6SChristoph Hellwig 104261891923SStefan Behrens out_w_error: 10434246a0b6SChristoph Hellwig bio->bi_error = ret; 10444246a0b6SChristoph Hellwig bio_endio(bio); 104561891923SStefan Behrens return ret; 104661891923SStefan Behrens } 104761891923SStefan Behrens 10483dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 1049784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 1050a6bc32b8SMel Gorman struct page *newpage, struct page *page, 1051a6bc32b8SMel Gorman enum migrate_mode mode) 1052784b4e29SChris Mason { 1053784b4e29SChris Mason /* 1054784b4e29SChris Mason * we can't safely write a btree page from here, 1055784b4e29SChris Mason * we haven't done the locking hook 1056784b4e29SChris Mason */ 1057784b4e29SChris Mason if (PageDirty(page)) 1058784b4e29SChris Mason return -EAGAIN; 1059784b4e29SChris Mason /* 1060784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 1061784b4e29SChris Mason * We must have no buffers or drop them. 1062784b4e29SChris Mason */ 1063784b4e29SChris Mason if (page_has_private(page) && 1064784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 1065784b4e29SChris Mason return -EAGAIN; 1066a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1067784b4e29SChris Mason } 10683dd1462eSJan Beulich #endif 1069784b4e29SChris Mason 10700da5468fSChris Mason 10710da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 10720da5468fSChris Mason struct writeback_control *wbc) 10730da5468fSChris Mason { 1074e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 1075e2d84521SMiao Xie int ret; 1076e2d84521SMiao Xie 1077d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 1078448d640bSChris Mason 1079448d640bSChris Mason if (wbc->for_kupdate) 1080448d640bSChris Mason return 0; 1081448d640bSChris Mason 1082e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 1083b9473439SChris Mason /* this is a bit racy, but that's ok */ 1084e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1085e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 1086e2d84521SMiao Xie if (ret < 0) 1087793955bcSChris Mason return 0; 1088793955bcSChris Mason } 10890b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 10900da5468fSChris Mason } 10910da5468fSChris Mason 1092b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 10935f39d397SChris Mason { 1094d1310b2eSChris Mason struct extent_io_tree *tree; 1095d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10968ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 10975f39d397SChris Mason } 10985f39d397SChris Mason 109970dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 11005f39d397SChris Mason { 110198509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 110298509cfcSChris Mason return 0; 11030c4e538bSDavid Sterba 1104f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1105d98237b3SChris Mason } 1106d98237b3SChris Mason 1107d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1108d47992f8SLukas Czerner unsigned int length) 1109d98237b3SChris Mason { 1110d1310b2eSChris Mason struct extent_io_tree *tree; 1111d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 11125f39d397SChris Mason extent_invalidatepage(tree, page, offset); 11135f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 11149ad6b7bcSChris Mason if (PagePrivate(page)) { 1115efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1116efe120a0SFrank Holton "page private not zero on page %llu", 1117efe120a0SFrank Holton (unsigned long long)page_offset(page)); 11189ad6b7bcSChris Mason ClearPagePrivate(page); 11199ad6b7bcSChris Mason set_page_private(page, 0); 112009cbfeafSKirill A. Shutemov put_page(page); 11219ad6b7bcSChris Mason } 1122d98237b3SChris Mason } 1123d98237b3SChris Mason 11240b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 11250b32f4bbSJosef Bacik { 1126bb146eb2SJosef Bacik #ifdef DEBUG 11270b32f4bbSJosef Bacik struct extent_buffer *eb; 11280b32f4bbSJosef Bacik 11290b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 11300b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 11310b32f4bbSJosef Bacik BUG_ON(!eb); 11320b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 11330b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 11340b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1135bb146eb2SJosef Bacik #endif 11360b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 11370b32f4bbSJosef Bacik } 11380b32f4bbSJosef Bacik 11397f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1140d98237b3SChris Mason .readpage = btree_readpage, 11410da5468fSChris Mason .writepages = btree_writepages, 11425f39d397SChris Mason .releasepage = btree_releasepage, 11435f39d397SChris Mason .invalidatepage = btree_invalidatepage, 11445a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1145784b4e29SChris Mason .migratepage = btree_migratepage, 11465a92bc88SChris Mason #endif 11470b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1148d98237b3SChris Mason }; 1149123abc88SChris Mason 11502ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1151090d1875SChris Mason { 11525f39d397SChris Mason struct extent_buffer *buf = NULL; 11532ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1154090d1875SChris Mason 11552ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1156c871b0f2SLiu Bo if (IS_ERR(buf)) 11576197d86eSDavid Sterba return; 1158d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 11598436ea91SJosef Bacik buf, WAIT_NONE, btree_get_extent, 0); 11605f39d397SChris Mason free_extent_buffer(buf); 1161090d1875SChris Mason } 1162090d1875SChris Mason 11632ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1164ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1165ab0fff03SArne Jansen { 1166ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 11672ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1168ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1169ab0fff03SArne Jansen int ret; 1170ab0fff03SArne Jansen 11712ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1172c871b0f2SLiu Bo if (IS_ERR(buf)) 1173ab0fff03SArne Jansen return 0; 1174ab0fff03SArne Jansen 1175ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1176ab0fff03SArne Jansen 11778436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 1178ab0fff03SArne Jansen btree_get_extent, mirror_num); 1179ab0fff03SArne Jansen if (ret) { 1180ab0fff03SArne Jansen free_extent_buffer(buf); 1181ab0fff03SArne Jansen return ret; 1182ab0fff03SArne Jansen } 1183ab0fff03SArne Jansen 1184ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1185ab0fff03SArne Jansen free_extent_buffer(buf); 1186ab0fff03SArne Jansen return -EIO; 11870b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1188ab0fff03SArne Jansen *eb = buf; 1189ab0fff03SArne Jansen } else { 1190ab0fff03SArne Jansen free_extent_buffer(buf); 1191ab0fff03SArne Jansen } 1192ab0fff03SArne Jansen return 0; 1193ab0fff03SArne Jansen } 1194ab0fff03SArne Jansen 11952ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 11962ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1197a83fffb7SDavid Sterba u64 bytenr) 11980999df54SChris Mason { 11990b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 12000b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 12010b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 12020999df54SChris Mason } 12030999df54SChris Mason 12040999df54SChris Mason 1205e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1206e02119d5SChris Mason { 1207727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 12088aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1209e02119d5SChris Mason } 1210e02119d5SChris Mason 1211e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1212e02119d5SChris Mason { 1213727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 12148aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1215e02119d5SChris Mason } 1216e02119d5SChris Mason 12172ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1218ce86cd59SDavid Sterba u64 parent_transid) 1219e20d96d6SChris Mason { 12205f39d397SChris Mason struct extent_buffer *buf = NULL; 122119c00ddcSChris Mason int ret; 122219c00ddcSChris Mason 12232ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1224c871b0f2SLiu Bo if (IS_ERR(buf)) 1225c871b0f2SLiu Bo return buf; 1226e4204dedSChris Mason 12272ff7e61eSJeff Mahoney ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 12280f0fe8f7SFilipe David Borba Manana if (ret) { 12290f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 123064c043deSLiu Bo return ERR_PTR(ret); 12310f0fe8f7SFilipe David Borba Manana } 12325f39d397SChris Mason return buf; 1233ce9adaa5SChris Mason 1234eb60ceacSChris Mason } 1235eb60ceacSChris Mason 123601d58472SDaniel Dressler void clean_tree_block(struct btrfs_trans_handle *trans, 123701d58472SDaniel Dressler struct btrfs_fs_info *fs_info, 12385f39d397SChris Mason struct extent_buffer *buf) 1239ed2ff2cbSChris Mason { 124055c69072SChris Mason if (btrfs_header_generation(buf) == 1241e2d84521SMiao Xie fs_info->running_transaction->transid) { 1242b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1243b4ce94deSChris Mason 1244b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1245e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1246e2d84521SMiao Xie -buf->len, 1247e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1248b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1249b9473439SChris Mason btrfs_set_lock_blocking(buf); 12500b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1251925baeddSChris Mason } 12525f39d397SChris Mason } 1253ed7b63ebSJosef Bacik } 12545f39d397SChris Mason 12558257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 12568257b2dcSMiao Xie { 12578257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 12588257b2dcSMiao Xie int ret; 12598257b2dcSMiao Xie 12608257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 12618257b2dcSMiao Xie if (!writers) 12628257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 12638257b2dcSMiao Xie 1264908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 12658257b2dcSMiao Xie if (ret < 0) { 12668257b2dcSMiao Xie kfree(writers); 12678257b2dcSMiao Xie return ERR_PTR(ret); 12688257b2dcSMiao Xie } 12698257b2dcSMiao Xie 12708257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 12718257b2dcSMiao Xie return writers; 12728257b2dcSMiao Xie } 12738257b2dcSMiao Xie 12748257b2dcSMiao Xie static void 12758257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 12768257b2dcSMiao Xie { 12778257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 12788257b2dcSMiao Xie kfree(writers); 12798257b2dcSMiao Xie } 12808257b2dcSMiao Xie 1281da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1282e20d96d6SChris Mason u64 objectid) 1283d97e63b6SChris Mason { 12847c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1285cfaa7295SChris Mason root->node = NULL; 1286a28ec197SChris Mason root->commit_root = NULL; 128727cdeb70SMiao Xie root->state = 0; 1288d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 12890b86a832SChris Mason 12900f7d52f4SChris Mason root->objectid = objectid; 12910f7d52f4SChris Mason root->last_trans = 0; 129213a8a7c8SYan, Zheng root->highest_objectid = 0; 1293eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1294199c2a9cSMiao Xie root->nr_ordered_extents = 0; 129558176a96SJosef Bacik root->name = NULL; 12966bef4d31SEric Paris root->inode_tree = RB_ROOT; 129716cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1298f0486c68SYan, Zheng root->block_rsv = NULL; 1299d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 13000b86a832SChris Mason 13010b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 13025d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1303eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1304eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1305199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1306199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 13072ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 13082ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1309d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 13105d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1311eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1312199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1313f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 13142ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 13152ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1316a2135011SChris Mason mutex_init(&root->objectid_mutex); 1317e02119d5SChris Mason mutex_init(&root->log_mutex); 131831f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1319573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 13207237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 13217237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 13227237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 13238b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 13248b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 13257237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 13267237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 13277237f183SYan Zheng atomic_set(&root->log_writers, 0); 13282ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 13298a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1330b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 13318257b2dcSMiao Xie atomic_set(&root->will_be_snapshoted, 0); 133255eeaf05SQu Wenruo atomic_set(&root->qgroup_meta_rsv, 0); 13337237f183SYan Zheng root->log_transid = 0; 1334d1433debSMiao Xie root->log_transid_committed = -1; 1335257c62e1SChris Mason root->last_log_commit = 0; 13367c0260eeSJeff Mahoney if (!dummy) 1337d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1338f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1339017e5369SChris Mason 13403768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 13413768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 13426702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 13437c0260eeSJeff Mahoney if (!dummy) 13443f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 134506ea65a3SJosef Bacik else 134606ea65a3SJosef Bacik root->defrag_trans_start = 0; 13474d775673SChris Mason root->root_key.objectid = objectid; 13480ee5dc67SAl Viro root->anon_dev = 0; 13498ea05e3aSAlexander Block 13505f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 13513768f368SChris Mason } 13523768f368SChris Mason 135374e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 135474e4d827SDavid Sterba gfp_t flags) 13556f07e42eSAl Viro { 135674e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 13576f07e42eSAl Viro if (root) 13586f07e42eSAl Viro root->fs_info = fs_info; 13596f07e42eSAl Viro return root; 13606f07e42eSAl Viro } 13616f07e42eSAl Viro 136206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 136306ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1364da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 136506ea65a3SJosef Bacik { 136606ea65a3SJosef Bacik struct btrfs_root *root; 136706ea65a3SJosef Bacik 13687c0260eeSJeff Mahoney if (!fs_info) 13697c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 13707c0260eeSJeff Mahoney 13717c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 137206ea65a3SJosef Bacik if (!root) 137306ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1374da17066cSJeff Mahoney 1375b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1376da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1377faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 137806ea65a3SJosef Bacik 137906ea65a3SJosef Bacik return root; 138006ea65a3SJosef Bacik } 138106ea65a3SJosef Bacik #endif 138206ea65a3SJosef Bacik 138320897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 138420897f5cSArne Jansen struct btrfs_fs_info *fs_info, 138520897f5cSArne Jansen u64 objectid) 138620897f5cSArne Jansen { 138720897f5cSArne Jansen struct extent_buffer *leaf; 138820897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 138920897f5cSArne Jansen struct btrfs_root *root; 139020897f5cSArne Jansen struct btrfs_key key; 139120897f5cSArne Jansen int ret = 0; 13926463fe58SStefan Behrens uuid_le uuid; 139320897f5cSArne Jansen 139474e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 139520897f5cSArne Jansen if (!root) 139620897f5cSArne Jansen return ERR_PTR(-ENOMEM); 139720897f5cSArne Jansen 1398da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 139920897f5cSArne Jansen root->root_key.objectid = objectid; 140020897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 140120897f5cSArne Jansen root->root_key.offset = 0; 140220897f5cSArne Jansen 14034d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 140420897f5cSArne Jansen if (IS_ERR(leaf)) { 140520897f5cSArne Jansen ret = PTR_ERR(leaf); 14061dd05682STsutomu Itoh leaf = NULL; 140720897f5cSArne Jansen goto fail; 140820897f5cSArne Jansen } 140920897f5cSArne Jansen 1410b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 141120897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 141220897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 141320897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 141420897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 141520897f5cSArne Jansen root->node = leaf; 141620897f5cSArne Jansen 1417d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1418d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 141920897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 142020897f5cSArne Jansen 142120897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 142227cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 142320897f5cSArne Jansen 142420897f5cSArne Jansen root->root_item.flags = 0; 142520897f5cSArne Jansen root->root_item.byte_limit = 0; 142620897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 142720897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 142820897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 142920897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 143020897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 143120897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 143220897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 14336463fe58SStefan Behrens uuid_le_gen(&uuid); 14346463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 143520897f5cSArne Jansen root->root_item.drop_level = 0; 143620897f5cSArne Jansen 143720897f5cSArne Jansen key.objectid = objectid; 143820897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 143920897f5cSArne Jansen key.offset = 0; 144020897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 144120897f5cSArne Jansen if (ret) 144220897f5cSArne Jansen goto fail; 144320897f5cSArne Jansen 144420897f5cSArne Jansen btrfs_tree_unlock(leaf); 144520897f5cSArne Jansen 144620897f5cSArne Jansen return root; 14471dd05682STsutomu Itoh 14481dd05682STsutomu Itoh fail: 14491dd05682STsutomu Itoh if (leaf) { 14501dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 145159885b39STsutomu Itoh free_extent_buffer(root->commit_root); 14521dd05682STsutomu Itoh free_extent_buffer(leaf); 14531dd05682STsutomu Itoh } 14541dd05682STsutomu Itoh kfree(root); 14551dd05682STsutomu Itoh 14561dd05682STsutomu Itoh return ERR_PTR(ret); 145720897f5cSArne Jansen } 145820897f5cSArne Jansen 14597237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1460e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14610f7d52f4SChris Mason { 14620f7d52f4SChris Mason struct btrfs_root *root; 14637237f183SYan Zheng struct extent_buffer *leaf; 1464e02119d5SChris Mason 146574e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1466e02119d5SChris Mason if (!root) 14677237f183SYan Zheng return ERR_PTR(-ENOMEM); 1468e02119d5SChris Mason 1469da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1470e02119d5SChris Mason 1471e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1472e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1473e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 147427cdeb70SMiao Xie 14757237f183SYan Zheng /* 147627cdeb70SMiao Xie * DON'T set REF_COWS for log trees 147727cdeb70SMiao Xie * 14787237f183SYan Zheng * log trees do not get reference counted because they go away 14797237f183SYan Zheng * before a real commit is actually done. They do store pointers 14807237f183SYan Zheng * to file data extents, and those reference counts still get 14817237f183SYan Zheng * updated (along with back refs to the log tree). 14827237f183SYan Zheng */ 1483e02119d5SChris Mason 14844d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14854d75f8a9SDavid Sterba NULL, 0, 0, 0); 14867237f183SYan Zheng if (IS_ERR(leaf)) { 14877237f183SYan Zheng kfree(root); 14887237f183SYan Zheng return ERR_CAST(leaf); 14897237f183SYan Zheng } 1490e02119d5SChris Mason 1491b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 14925d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 14935d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 14945d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 14955d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 14967237f183SYan Zheng root->node = leaf; 1497e02119d5SChris Mason 14980b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1499e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1500e02119d5SChris Mason btrfs_tree_unlock(root->node); 15017237f183SYan Zheng return root; 15027237f183SYan Zheng } 15037237f183SYan Zheng 15047237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 15057237f183SYan Zheng struct btrfs_fs_info *fs_info) 15067237f183SYan Zheng { 15077237f183SYan Zheng struct btrfs_root *log_root; 15087237f183SYan Zheng 15097237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 15107237f183SYan Zheng if (IS_ERR(log_root)) 15117237f183SYan Zheng return PTR_ERR(log_root); 15127237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 15137237f183SYan Zheng fs_info->log_root_tree = log_root; 15147237f183SYan Zheng return 0; 15157237f183SYan Zheng } 15167237f183SYan Zheng 15177237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 15187237f183SYan Zheng struct btrfs_root *root) 15197237f183SYan Zheng { 15200b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 15217237f183SYan Zheng struct btrfs_root *log_root; 15227237f183SYan Zheng struct btrfs_inode_item *inode_item; 15237237f183SYan Zheng 15240b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 15257237f183SYan Zheng if (IS_ERR(log_root)) 15267237f183SYan Zheng return PTR_ERR(log_root); 15277237f183SYan Zheng 15287237f183SYan Zheng log_root->last_trans = trans->transid; 15297237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 15307237f183SYan Zheng 15317237f183SYan Zheng inode_item = &log_root->root_item.inode; 15323cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 15333cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15343cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1535da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15360b246afaSJeff Mahoney fs_info->nodesize); 15373cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15387237f183SYan Zheng 15395d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15407237f183SYan Zheng 15417237f183SYan Zheng WARN_ON(root->log_root); 15427237f183SYan Zheng root->log_root = log_root; 15437237f183SYan Zheng root->log_transid = 0; 1544d1433debSMiao Xie root->log_transid_committed = -1; 1545257c62e1SChris Mason root->last_log_commit = 0; 1546e02119d5SChris Mason return 0; 1547e02119d5SChris Mason } 1548e02119d5SChris Mason 154935a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1550cb517eabSMiao Xie struct btrfs_key *key) 1551e02119d5SChris Mason { 1552e02119d5SChris Mason struct btrfs_root *root; 1553e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 15540f7d52f4SChris Mason struct btrfs_path *path; 155584234f3aSYan Zheng u64 generation; 1556cb517eabSMiao Xie int ret; 1557cb517eabSMiao Xie 1558cb517eabSMiao Xie path = btrfs_alloc_path(); 1559cb517eabSMiao Xie if (!path) 1560cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 15610f7d52f4SChris Mason 156274e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1563cb517eabSMiao Xie if (!root) { 1564cb517eabSMiao Xie ret = -ENOMEM; 1565cb517eabSMiao Xie goto alloc_fail; 15660f7d52f4SChris Mason } 15670f7d52f4SChris Mason 1568da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 15690f7d52f4SChris Mason 1570cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1571cb517eabSMiao Xie &root->root_item, &root->root_key); 15720f7d52f4SChris Mason if (ret) { 157313a8a7c8SYan, Zheng if (ret > 0) 157413a8a7c8SYan, Zheng ret = -ENOENT; 1575cb517eabSMiao Xie goto find_fail; 15760f7d52f4SChris Mason } 157713a8a7c8SYan, Zheng 157884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 15792ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15802ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1581ce86cd59SDavid Sterba generation); 158264c043deSLiu Bo if (IS_ERR(root->node)) { 158364c043deSLiu Bo ret = PTR_ERR(root->node); 1584cb517eabSMiao Xie goto find_fail; 1585cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1586cb517eabSMiao Xie ret = -EIO; 158764c043deSLiu Bo free_extent_buffer(root->node); 158864c043deSLiu Bo goto find_fail; 1589416bc658SJosef Bacik } 15905d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 159113a8a7c8SYan, Zheng out: 1592cb517eabSMiao Xie btrfs_free_path(path); 1593cb517eabSMiao Xie return root; 1594cb517eabSMiao Xie 1595cb517eabSMiao Xie find_fail: 1596cb517eabSMiao Xie kfree(root); 1597cb517eabSMiao Xie alloc_fail: 1598cb517eabSMiao Xie root = ERR_PTR(ret); 1599cb517eabSMiao Xie goto out; 1600cb517eabSMiao Xie } 1601cb517eabSMiao Xie 1602cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1603cb517eabSMiao Xie struct btrfs_key *location) 1604cb517eabSMiao Xie { 1605cb517eabSMiao Xie struct btrfs_root *root; 1606cb517eabSMiao Xie 1607cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1608cb517eabSMiao Xie if (IS_ERR(root)) 1609cb517eabSMiao Xie return root; 1610cb517eabSMiao Xie 1611cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 161227cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 161308fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 161408fe4db1SLi Zefan } 161513a8a7c8SYan, Zheng 16165eda7b5eSChris Mason return root; 16175eda7b5eSChris Mason } 16185eda7b5eSChris Mason 1619cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1620cb517eabSMiao Xie { 1621cb517eabSMiao Xie int ret; 16228257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1623cb517eabSMiao Xie 1624cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1625cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1626cb517eabSMiao Xie GFP_NOFS); 1627cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1628cb517eabSMiao Xie ret = -ENOMEM; 1629cb517eabSMiao Xie goto fail; 1630cb517eabSMiao Xie } 1631cb517eabSMiao Xie 16328257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 16338257b2dcSMiao Xie if (IS_ERR(writers)) { 16348257b2dcSMiao Xie ret = PTR_ERR(writers); 16358257b2dcSMiao Xie goto fail; 16368257b2dcSMiao Xie } 16378257b2dcSMiao Xie root->subv_writers = writers; 16388257b2dcSMiao Xie 1639cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 164057cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 164157cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1642cb517eabSMiao Xie 1643cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1644cb517eabSMiao Xie if (ret) 1645876d2cf1SLiu Bo goto fail; 1646f32e48e9SChandan Rajendra 1647f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1648f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1649f32e48e9SChandan Rajendra &root->highest_objectid); 1650f32e48e9SChandan Rajendra if (ret) { 1651f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1652876d2cf1SLiu Bo goto fail; 1653f32e48e9SChandan Rajendra } 1654f32e48e9SChandan Rajendra 1655f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1656f32e48e9SChandan Rajendra 1657f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1658f32e48e9SChandan Rajendra 1659cb517eabSMiao Xie return 0; 1660cb517eabSMiao Xie fail: 1661876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1662cb517eabSMiao Xie return ret; 1663cb517eabSMiao Xie } 1664cb517eabSMiao Xie 166535bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1666cb517eabSMiao Xie u64 root_id) 1667cb517eabSMiao Xie { 1668cb517eabSMiao Xie struct btrfs_root *root; 1669cb517eabSMiao Xie 1670cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1671cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1672cb517eabSMiao Xie (unsigned long)root_id); 1673cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1674cb517eabSMiao Xie return root; 1675cb517eabSMiao Xie } 1676cb517eabSMiao Xie 1677cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1678cb517eabSMiao Xie struct btrfs_root *root) 1679cb517eabSMiao Xie { 1680cb517eabSMiao Xie int ret; 1681cb517eabSMiao Xie 1682e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1683cb517eabSMiao Xie if (ret) 1684cb517eabSMiao Xie return ret; 1685cb517eabSMiao Xie 1686cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1687cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1688cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1689cb517eabSMiao Xie root); 1690cb517eabSMiao Xie if (ret == 0) 169127cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1692cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1693cb517eabSMiao Xie radix_tree_preload_end(); 1694cb517eabSMiao Xie 1695cb517eabSMiao Xie return ret; 1696cb517eabSMiao Xie } 1697cb517eabSMiao Xie 1698c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1699c00869f1SMiao Xie struct btrfs_key *location, 1700c00869f1SMiao Xie bool check_ref) 17015eda7b5eSChris Mason { 17025eda7b5eSChris Mason struct btrfs_root *root; 1703381cf658SDavid Sterba struct btrfs_path *path; 17041d4c08e0SDavid Sterba struct btrfs_key key; 17055eda7b5eSChris Mason int ret; 17065eda7b5eSChris Mason 1707edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1708edbd8d4eSChris Mason return fs_info->tree_root; 1709edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1710edbd8d4eSChris Mason return fs_info->extent_root; 17118f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 17128f18cf13SChris Mason return fs_info->chunk_root; 17138f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 17148f18cf13SChris Mason return fs_info->dev_root; 17150403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 17160403e47eSYan Zheng return fs_info->csum_root; 1717bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1718bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1719bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1720f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1721f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1722f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 172370f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 172470f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 172570f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 17264df27c4dSYan, Zheng again: 1727cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 172848475471SStefan Behrens if (root) { 1729c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 173048475471SStefan Behrens return ERR_PTR(-ENOENT); 17315eda7b5eSChris Mason return root; 173248475471SStefan Behrens } 17335eda7b5eSChris Mason 1734cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 17355eda7b5eSChris Mason if (IS_ERR(root)) 17365eda7b5eSChris Mason return root; 17373394e160SChris Mason 1738c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1739d68fc57bSYan, Zheng ret = -ENOENT; 1740d68fc57bSYan, Zheng goto fail; 1741d68fc57bSYan, Zheng } 1742d68fc57bSYan, Zheng 1743cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1744cb517eabSMiao Xie if (ret) 1745cb517eabSMiao Xie goto fail; 1746cb517eabSMiao Xie 1747381cf658SDavid Sterba path = btrfs_alloc_path(); 1748381cf658SDavid Sterba if (!path) { 1749381cf658SDavid Sterba ret = -ENOMEM; 1750381cf658SDavid Sterba goto fail; 1751381cf658SDavid Sterba } 17521d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17531d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 17541d4c08e0SDavid Sterba key.offset = location->objectid; 17551d4c08e0SDavid Sterba 17561d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1757381cf658SDavid Sterba btrfs_free_path(path); 1758d68fc57bSYan, Zheng if (ret < 0) 1759d68fc57bSYan, Zheng goto fail; 1760d68fc57bSYan, Zheng if (ret == 0) 176127cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1762d68fc57bSYan, Zheng 1763cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17640f7d52f4SChris Mason if (ret) { 17654df27c4dSYan, Zheng if (ret == -EEXIST) { 17664df27c4dSYan, Zheng free_fs_root(root); 17674df27c4dSYan, Zheng goto again; 17680f7d52f4SChris Mason } 17694df27c4dSYan, Zheng goto fail; 17704df27c4dSYan, Zheng } 1771edbd8d4eSChris Mason return root; 17724df27c4dSYan, Zheng fail: 17734df27c4dSYan, Zheng free_fs_root(root); 17744df27c4dSYan, Zheng return ERR_PTR(ret); 1775edbd8d4eSChris Mason } 1776edbd8d4eSChris Mason 177704160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 177804160088SChris Mason { 177904160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 178004160088SChris Mason int ret = 0; 178104160088SChris Mason struct btrfs_device *device; 178204160088SChris Mason struct backing_dev_info *bdi; 1783b7967db7SChris Mason 17841f78160cSXiao Guangrong rcu_read_lock(); 17851f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1786dfe25020SChris Mason if (!device->bdev) 1787dfe25020SChris Mason continue; 178804160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 1789ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 179004160088SChris Mason ret = 1; 179104160088SChris Mason break; 179204160088SChris Mason } 179304160088SChris Mason } 17941f78160cSXiao Guangrong rcu_read_unlock(); 179504160088SChris Mason return ret; 179604160088SChris Mason } 179704160088SChris Mason 179804160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 179904160088SChris Mason { 1800ad081f14SJens Axboe int err; 1801ad081f14SJens Axboe 1802b4caecd4SChristoph Hellwig err = bdi_setup_and_register(bdi, "btrfs"); 1803ad081f14SJens Axboe if (err) 1804ad081f14SJens Axboe return err; 1805ad081f14SJens Axboe 180609cbfeafSKirill A. Shutemov bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE; 180704160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 180804160088SChris Mason bdi->congested_data = info; 1809da2f0f74SChris Mason bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 181004160088SChris Mason return 0; 181104160088SChris Mason } 181204160088SChris Mason 18138b712842SChris Mason /* 18148b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 18158b712842SChris Mason * functions. This is where read checksum verification actually happens 18168b712842SChris Mason */ 18178b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1818ce9adaa5SChris Mason { 1819ce9adaa5SChris Mason struct bio *bio; 182097eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1821ce9adaa5SChris Mason 182297eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1823ce9adaa5SChris Mason bio = end_io_wq->bio; 18248b712842SChris Mason 18254246a0b6SChristoph Hellwig bio->bi_error = end_io_wq->error; 1826ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1827ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 182897eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 18294246a0b6SChristoph Hellwig bio_endio(bio); 1830ce9adaa5SChris Mason } 183144b8bd7eSChris Mason 1832a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1833a74a4b97SChris Mason { 1834a74a4b97SChris Mason struct btrfs_root *root = arg; 18350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1836d0278245SMiao Xie int again; 1837da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1838a74a4b97SChris Mason 1839a74a4b97SChris Mason do { 1840d0278245SMiao Xie again = 0; 18419d1a2a3aSDavid Sterba 1842d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18432ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1844d0278245SMiao Xie goto sleep; 1845d0278245SMiao Xie 184690c711abSZygo Blaxell /* 184790c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 184890c711abSZygo Blaxell * before we have finished mounting the filesystem. 184990c711abSZygo Blaxell */ 18500b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 185190c711abSZygo Blaxell goto sleep; 185290c711abSZygo Blaxell 18530b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1854d0278245SMiao Xie goto sleep; 1855d0278245SMiao Xie 1856dc7f370cSMiao Xie /* 1857dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1858dc7f370cSMiao Xie * during the above check and trylock. 1859dc7f370cSMiao Xie */ 18602ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18610b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1862dc7f370cSMiao Xie goto sleep; 1863dc7f370cSMiao Xie } 1864dc7f370cSMiao Xie 18650b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 18662ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 18670b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1868c2d6cb16SFilipe Manana 18699d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 18700b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1871a74a4b97SChris Mason 1872d0278245SMiao Xie /* 187305323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 187405323cd1SMiao Xie * needn't do anything special here. 1875d0278245SMiao Xie */ 18760b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 187767c5e7d4SFilipe Manana 187867c5e7d4SFilipe Manana /* 187967c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 188067c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 188167c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 188267c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 188367c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 188467c5e7d4SFilipe Manana * unused block groups. 188567c5e7d4SFilipe Manana */ 18860b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1887d0278245SMiao Xie sleep: 1888838fe188SJiri Kosina if (!again) { 1889a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18908929ecfaSYan, Zheng if (!kthread_should_stop()) 1891a74a4b97SChris Mason schedule(); 1892a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1893a74a4b97SChris Mason } 1894a74a4b97SChris Mason } while (!kthread_should_stop()); 1895da288d28SFilipe Manana 1896da288d28SFilipe Manana /* 1897da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1898da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1899da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1900da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1901da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1902da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1903da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1904da288d28SFilipe Manana * since work queues and other resources were already released and 1905da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1906da288d28SFilipe Manana */ 1907da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1908da288d28SFilipe Manana if (IS_ERR(trans)) { 1909da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 19100b246afaSJeff Mahoney btrfs_err(fs_info, 1911da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1912da288d28SFilipe Manana PTR_ERR(trans)); 1913da288d28SFilipe Manana } else { 1914da288d28SFilipe Manana int ret; 1915da288d28SFilipe Manana 19163a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1917da288d28SFilipe Manana if (ret) 19180b246afaSJeff Mahoney btrfs_err(fs_info, 1919da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1920da288d28SFilipe Manana ret); 1921da288d28SFilipe Manana } 1922da288d28SFilipe Manana 1923a74a4b97SChris Mason return 0; 1924a74a4b97SChris Mason } 1925a74a4b97SChris Mason 1926a74a4b97SChris Mason static int transaction_kthread(void *arg) 1927a74a4b97SChris Mason { 1928a74a4b97SChris Mason struct btrfs_root *root = arg; 19290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1930a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1931a74a4b97SChris Mason struct btrfs_transaction *cur; 19328929ecfaSYan, Zheng u64 transid; 1933a74a4b97SChris Mason unsigned long now; 1934a74a4b97SChris Mason unsigned long delay; 1935914b2007SJan Kara bool cannot_commit; 1936a74a4b97SChris Mason 1937a74a4b97SChris Mason do { 1938914b2007SJan Kara cannot_commit = false; 19390b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 19400b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1941a74a4b97SChris Mason 19420b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19430b246afaSJeff Mahoney cur = fs_info->running_transaction; 1944a74a4b97SChris Mason if (!cur) { 19450b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1946a74a4b97SChris Mason goto sleep; 1947a74a4b97SChris Mason } 194831153d81SYan Zheng 1949a74a4b97SChris Mason now = get_seconds(); 19504a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 19518b87dc17SDavid Sterba (now < cur->start_time || 19520b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 19530b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1954a74a4b97SChris Mason delay = HZ * 5; 1955a74a4b97SChris Mason goto sleep; 1956a74a4b97SChris Mason } 19578929ecfaSYan, Zheng transid = cur->transid; 19580b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 195956bec294SChris Mason 196079787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1961354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1962914b2007SJan Kara if (IS_ERR(trans)) { 1963354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1964914b2007SJan Kara cannot_commit = true; 196579787eaaSJeff Mahoney goto sleep; 1966914b2007SJan Kara } 19678929ecfaSYan, Zheng if (transid == trans->transid) { 19683a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19698929ecfaSYan, Zheng } else { 19703a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19718929ecfaSYan, Zheng } 1972a74a4b97SChris Mason sleep: 19730b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19740b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1975a74a4b97SChris Mason 19764e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 19770b246afaSJeff Mahoney &fs_info->fs_state))) 19782ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 1979a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 19808929ecfaSYan, Zheng if (!kthread_should_stop() && 19810b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1982914b2007SJan Kara cannot_commit)) 1983a74a4b97SChris Mason schedule_timeout(delay); 1984a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1985a74a4b97SChris Mason } while (!kthread_should_stop()); 1986a74a4b97SChris Mason return 0; 1987a74a4b97SChris Mason } 1988a74a4b97SChris Mason 1989af31f5e5SChris Mason /* 1990af31f5e5SChris Mason * this will find the highest generation in the array of 1991af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1992af31f5e5SChris Mason * or -1 if we can't find anything. 1993af31f5e5SChris Mason * 1994af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1995af31f5e5SChris Mason * generation of the latest root in the array with the generation 1996af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1997af31f5e5SChris Mason */ 1998af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1999af31f5e5SChris Mason { 2000af31f5e5SChris Mason u64 cur; 2001af31f5e5SChris Mason int newest_index = -1; 2002af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2003af31f5e5SChris Mason int i; 2004af31f5e5SChris Mason 2005af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2006af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2007af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2008af31f5e5SChris Mason if (cur == newest_gen) 2009af31f5e5SChris Mason newest_index = i; 2010af31f5e5SChris Mason } 2011af31f5e5SChris Mason 2012af31f5e5SChris Mason /* check to see if we actually wrapped around */ 2013af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 2014af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 2015af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2016af31f5e5SChris Mason if (cur == newest_gen) 2017af31f5e5SChris Mason newest_index = 0; 2018af31f5e5SChris Mason } 2019af31f5e5SChris Mason return newest_index; 2020af31f5e5SChris Mason } 2021af31f5e5SChris Mason 2022af31f5e5SChris Mason 2023af31f5e5SChris Mason /* 2024af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 2025af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 2026af31f5e5SChris Mason * field in the fs_info struct 2027af31f5e5SChris Mason */ 2028af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 2029af31f5e5SChris Mason u64 newest_gen) 2030af31f5e5SChris Mason { 2031af31f5e5SChris Mason int newest_index = -1; 2032af31f5e5SChris Mason 2033af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 2034af31f5e5SChris Mason /* if there was garbage in there, just move along */ 2035af31f5e5SChris Mason if (newest_index == -1) { 2036af31f5e5SChris Mason info->backup_root_index = 0; 2037af31f5e5SChris Mason } else { 2038af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 2039af31f5e5SChris Mason } 2040af31f5e5SChris Mason } 2041af31f5e5SChris Mason 2042af31f5e5SChris Mason /* 2043af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2044af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2045af31f5e5SChris Mason * done 2046af31f5e5SChris Mason */ 2047af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2048af31f5e5SChris Mason { 2049af31f5e5SChris Mason int next_backup; 2050af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2051af31f5e5SChris Mason int last_backup; 2052af31f5e5SChris Mason 2053af31f5e5SChris Mason next_backup = info->backup_root_index; 2054af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 2055af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2056af31f5e5SChris Mason 2057af31f5e5SChris Mason /* 2058af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 2059af31f5e5SChris Mason * this happens only at umount 2060af31f5e5SChris Mason */ 2061af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 2062af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 2063af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 2064af31f5e5SChris Mason next_backup = last_backup; 2065af31f5e5SChris Mason 2066af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2067af31f5e5SChris Mason 2068af31f5e5SChris Mason /* 2069af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2070af31f5e5SChris Mason * regardless of which ones we use today 2071af31f5e5SChris Mason */ 2072af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2073af31f5e5SChris Mason 2074af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2075af31f5e5SChris Mason 2076af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2077af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2078af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2079af31f5e5SChris Mason 2080af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2081af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2082af31f5e5SChris Mason 2083af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2084af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2085af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2086af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2087af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2088af31f5e5SChris Mason 2089af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 2090af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 2091af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 2092af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 2093af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 2094af31f5e5SChris Mason 20957c7e82a7SChris Mason /* 20967c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 20977c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 20987c7e82a7SChris Mason */ 20997c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 21007c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 21017c7e82a7SChris Mason info->fs_root->node->start); 2102af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2103af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2104af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2105af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21067c7e82a7SChris Mason } 2107af31f5e5SChris Mason 2108af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2109af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2110af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2111af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2112af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2113af31f5e5SChris Mason 2114af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 2115af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 2116af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 2117af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 2118af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 2119af31f5e5SChris Mason 2120af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2121af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2122af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2123af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2124af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2125af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2126af31f5e5SChris Mason 2127af31f5e5SChris Mason /* 2128af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2129af31f5e5SChris Mason * for the next commit 2130af31f5e5SChris Mason */ 2131af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2132af31f5e5SChris Mason &info->super_for_commit->super_roots, 2133af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2134af31f5e5SChris Mason } 2135af31f5e5SChris Mason 2136af31f5e5SChris Mason /* 2137af31f5e5SChris Mason * this copies info out of the root backup array and back into 2138af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 2139af31f5e5SChris Mason * the array, so you send it the number of backups you've already 2140af31f5e5SChris Mason * tried and the last backup index you used. 2141af31f5e5SChris Mason * 2142af31f5e5SChris Mason * this returns -1 when it has tried all the backups 2143af31f5e5SChris Mason */ 2144af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 2145af31f5e5SChris Mason struct btrfs_super_block *super, 2146af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 2147af31f5e5SChris Mason { 2148af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2149af31f5e5SChris Mason int newest = *backup_index; 2150af31f5e5SChris Mason 2151af31f5e5SChris Mason if (*num_backups_tried == 0) { 2152af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 2153af31f5e5SChris Mason 2154af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 2155af31f5e5SChris Mason if (newest == -1) 2156af31f5e5SChris Mason return -1; 2157af31f5e5SChris Mason 2158af31f5e5SChris Mason *backup_index = newest; 2159af31f5e5SChris Mason *num_backups_tried = 1; 2160af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2161af31f5e5SChris Mason /* we've tried all the backups, all done */ 2162af31f5e5SChris Mason return -1; 2163af31f5e5SChris Mason } else { 2164af31f5e5SChris Mason /* jump to the next oldest backup */ 2165af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2166af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2167af31f5e5SChris Mason *backup_index = newest; 2168af31f5e5SChris Mason *num_backups_tried += 1; 2169af31f5e5SChris Mason } 2170af31f5e5SChris Mason root_backup = super->super_roots + newest; 2171af31f5e5SChris Mason 2172af31f5e5SChris Mason btrfs_set_super_generation(super, 2173af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2174af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2175af31f5e5SChris Mason btrfs_set_super_root_level(super, 2176af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2177af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2178af31f5e5SChris Mason 2179af31f5e5SChris Mason /* 2180af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2181af31f5e5SChris Mason * need a fsck 2182af31f5e5SChris Mason */ 2183af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2184af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2185af31f5e5SChris Mason return 0; 2186af31f5e5SChris Mason } 2187af31f5e5SChris Mason 21887abadb64SLiu Bo /* helper to cleanup workers */ 21897abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 21907abadb64SLiu Bo { 2191dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2192afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 21935cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2194fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2195fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2196fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 21978b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2198d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2199fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 2200fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2201fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2202a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 22035b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2204e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2205736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2206a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2207fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2208a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 22097abadb64SLiu Bo } 22107abadb64SLiu Bo 22112e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22122e9f5954SRashika { 22132e9f5954SRashika if (root) { 22142e9f5954SRashika free_extent_buffer(root->node); 22152e9f5954SRashika free_extent_buffer(root->commit_root); 22162e9f5954SRashika root->node = NULL; 22172e9f5954SRashika root->commit_root = NULL; 22182e9f5954SRashika } 22192e9f5954SRashika } 22202e9f5954SRashika 2221af31f5e5SChris Mason /* helper to cleanup tree roots */ 2222af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2223af31f5e5SChris Mason { 22242e9f5954SRashika free_root_extent_buffers(info->tree_root); 2225655b09feSJosef Bacik 22262e9f5954SRashika free_root_extent_buffers(info->dev_root); 22272e9f5954SRashika free_root_extent_buffers(info->extent_root); 22282e9f5954SRashika free_root_extent_buffers(info->csum_root); 22292e9f5954SRashika free_root_extent_buffers(info->quota_root); 22302e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22312e9f5954SRashika if (chunk_root) 22322e9f5954SRashika free_root_extent_buffers(info->chunk_root); 223370f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2234af31f5e5SChris Mason } 2235af31f5e5SChris Mason 2236faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2237171f6537SJosef Bacik { 2238171f6537SJosef Bacik int ret; 2239171f6537SJosef Bacik struct btrfs_root *gang[8]; 2240171f6537SJosef Bacik int i; 2241171f6537SJosef Bacik 2242171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2243171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2244171f6537SJosef Bacik struct btrfs_root, root_list); 2245171f6537SJosef Bacik list_del(&gang[0]->root_list); 2246171f6537SJosef Bacik 224727cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2248cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2249171f6537SJosef Bacik } else { 2250171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2251171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2252b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2253171f6537SJosef Bacik } 2254171f6537SJosef Bacik } 2255171f6537SJosef Bacik 2256171f6537SJosef Bacik while (1) { 2257171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2258171f6537SJosef Bacik (void **)gang, 0, 2259171f6537SJosef Bacik ARRAY_SIZE(gang)); 2260171f6537SJosef Bacik if (!ret) 2261171f6537SJosef Bacik break; 2262171f6537SJosef Bacik for (i = 0; i < ret; i++) 2263cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2264171f6537SJosef Bacik } 22651a4319ccSLiu Bo 22661a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 22671a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 22682ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 22691a4319ccSLiu Bo } 2270171f6537SJosef Bacik } 2271af31f5e5SChris Mason 2272638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2273638aa7edSEric Sandeen { 2274638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2275638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2276638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2277638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2278638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2279638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2280638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2281638aa7edSEric Sandeen } 2282638aa7edSEric Sandeen 2283779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2284779a65a4SEric Sandeen { 2285779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2286779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2287779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2288779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2289779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2290779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2291779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2292779a65a4SEric Sandeen } 2293779a65a4SEric Sandeen 22946bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2295f37938e0SEric Sandeen { 22962ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 22972ff7e61eSJeff Mahoney 22982ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 22992ff7e61eSJeff Mahoney set_nlink(inode, 1); 2300f37938e0SEric Sandeen /* 2301f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2302f37938e0SEric Sandeen * the real end of the address space is determined by all of 2303f37938e0SEric Sandeen * the devices in the system 2304f37938e0SEric Sandeen */ 23052ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23062ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2307f37938e0SEric Sandeen 23082ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 23092ff7e61eSJeff Mahoney extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode->i_mapping); 23102ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 23112ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2312f37938e0SEric Sandeen 23132ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2314f37938e0SEric Sandeen 23152ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 23162ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 23172ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 23182ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2319f37938e0SEric Sandeen } 2320f37938e0SEric Sandeen 2321ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2322ad618368SEric Sandeen { 2323ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2324ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2325ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 232673beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 232773beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 232873beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2329ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 233073beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2331ad618368SEric Sandeen } 2332ad618368SEric Sandeen 2333f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2334f9e92e40SEric Sandeen { 2335f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2336f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2337f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2338f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2339f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2340f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2341f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2342d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2343f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2344f9e92e40SEric Sandeen } 2345f9e92e40SEric Sandeen 23462a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 23472a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 23482a458198SEric Sandeen { 23492a458198SEric Sandeen int max_active = fs_info->thread_pool_size; 23506f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23512a458198SEric Sandeen 23522a458198SEric Sandeen fs_info->workers = 2353cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2354cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23552a458198SEric Sandeen 23562a458198SEric Sandeen fs_info->delalloc_workers = 2357cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2358cb001095SJeff Mahoney flags, max_active, 2); 23592a458198SEric Sandeen 23602a458198SEric Sandeen fs_info->flush_workers = 2361cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2362cb001095SJeff Mahoney flags, max_active, 0); 23632a458198SEric Sandeen 23642a458198SEric Sandeen fs_info->caching_workers = 2365cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23662a458198SEric Sandeen 23672a458198SEric Sandeen /* 23682a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 23692a458198SEric Sandeen * likely that bios will be send down in a sane order to the 23702a458198SEric Sandeen * devices 23712a458198SEric Sandeen */ 23722a458198SEric Sandeen fs_info->submit_workers = 2373cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 23742a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 23752a458198SEric Sandeen max_active), 64); 23762a458198SEric Sandeen 23772a458198SEric Sandeen fs_info->fixup_workers = 2378cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23792a458198SEric Sandeen 23802a458198SEric Sandeen /* 23812a458198SEric Sandeen * endios are largely parallel and should have a very 23822a458198SEric Sandeen * low idle thresh 23832a458198SEric Sandeen */ 23842a458198SEric Sandeen fs_info->endio_workers = 2385cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 23862a458198SEric Sandeen fs_info->endio_meta_workers = 2387cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2388cb001095SJeff Mahoney max_active, 4); 23892a458198SEric Sandeen fs_info->endio_meta_write_workers = 2390cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2391cb001095SJeff Mahoney max_active, 2); 23922a458198SEric Sandeen fs_info->endio_raid56_workers = 2393cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2394cb001095SJeff Mahoney max_active, 4); 23952a458198SEric Sandeen fs_info->endio_repair_workers = 2396cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 23972a458198SEric Sandeen fs_info->rmw_workers = 2398cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 23992a458198SEric Sandeen fs_info->endio_write_workers = 2400cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2401cb001095SJeff Mahoney max_active, 2); 24022a458198SEric Sandeen fs_info->endio_freespace_worker = 2403cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2404cb001095SJeff Mahoney max_active, 0); 24052a458198SEric Sandeen fs_info->delayed_workers = 2406cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2407cb001095SJeff Mahoney max_active, 0); 24082a458198SEric Sandeen fs_info->readahead_workers = 2409cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2410cb001095SJeff Mahoney max_active, 2); 24112a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2412cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 24132a458198SEric Sandeen fs_info->extent_workers = 2414cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 24152a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 24162a458198SEric Sandeen max_active), 8); 24172a458198SEric Sandeen 24182a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 24192a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 24202a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 24212a458198SEric Sandeen fs_info->endio_meta_write_workers && 24222a458198SEric Sandeen fs_info->endio_repair_workers && 24232a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24242a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 24252a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 24262a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 24272a458198SEric Sandeen fs_info->extent_workers && 24282a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 24292a458198SEric Sandeen return -ENOMEM; 24302a458198SEric Sandeen } 24312a458198SEric Sandeen 24322a458198SEric Sandeen return 0; 24332a458198SEric Sandeen } 24342a458198SEric Sandeen 243563443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 243663443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 243763443bf5SEric Sandeen { 243863443bf5SEric Sandeen int ret; 243963443bf5SEric Sandeen struct btrfs_root *log_tree_root; 244063443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 244163443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 244263443bf5SEric Sandeen 244363443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2444f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 244563443bf5SEric Sandeen return -EIO; 244663443bf5SEric Sandeen } 244763443bf5SEric Sandeen 244874e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 244963443bf5SEric Sandeen if (!log_tree_root) 245063443bf5SEric Sandeen return -ENOMEM; 245163443bf5SEric Sandeen 2452da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 245363443bf5SEric Sandeen 24542ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 245563443bf5SEric Sandeen fs_info->generation + 1); 245664c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2457f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24580eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 245964c043deSLiu Bo kfree(log_tree_root); 24600eeff236SLiu Bo return ret; 246164c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2462f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 246363443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 246463443bf5SEric Sandeen kfree(log_tree_root); 246563443bf5SEric Sandeen return -EIO; 246663443bf5SEric Sandeen } 246763443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 246863443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 246963443bf5SEric Sandeen if (ret) { 24700b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 247163443bf5SEric Sandeen "Failed to recover log tree"); 247263443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 247363443bf5SEric Sandeen kfree(log_tree_root); 247463443bf5SEric Sandeen return ret; 247563443bf5SEric Sandeen } 247663443bf5SEric Sandeen 247763443bf5SEric Sandeen if (fs_info->sb->s_flags & MS_RDONLY) { 24786bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 247963443bf5SEric Sandeen if (ret) 248063443bf5SEric Sandeen return ret; 248163443bf5SEric Sandeen } 248263443bf5SEric Sandeen 248363443bf5SEric Sandeen return 0; 248463443bf5SEric Sandeen } 248563443bf5SEric Sandeen 24866bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 24874bbcaa64SEric Sandeen { 24886bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2489a4f3d2c4SDavid Sterba struct btrfs_root *root; 24904bbcaa64SEric Sandeen struct btrfs_key location; 24914bbcaa64SEric Sandeen int ret; 24924bbcaa64SEric Sandeen 24936bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 24946bccf3abSJeff Mahoney 24954bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 24964bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 24974bbcaa64SEric Sandeen location.offset = 0; 24984bbcaa64SEric Sandeen 2499a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2500a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2501a4f3d2c4SDavid Sterba return PTR_ERR(root); 2502a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2503a4f3d2c4SDavid Sterba fs_info->extent_root = root; 25044bbcaa64SEric Sandeen 25054bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2506a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2507a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2508a4f3d2c4SDavid Sterba return PTR_ERR(root); 2509a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2510a4f3d2c4SDavid Sterba fs_info->dev_root = root; 25114bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 25124bbcaa64SEric Sandeen 25134bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2514a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2515a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2516a4f3d2c4SDavid Sterba return PTR_ERR(root); 2517a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2518a4f3d2c4SDavid Sterba fs_info->csum_root = root; 25194bbcaa64SEric Sandeen 25204bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2521a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2522a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2523a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2524afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2525a4f3d2c4SDavid Sterba fs_info->quota_root = root; 25264bbcaa64SEric Sandeen } 25274bbcaa64SEric Sandeen 25284bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2529a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2530a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2531a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 25324bbcaa64SEric Sandeen if (ret != -ENOENT) 25334bbcaa64SEric Sandeen return ret; 25344bbcaa64SEric Sandeen } else { 2535a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2536a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 25374bbcaa64SEric Sandeen } 25384bbcaa64SEric Sandeen 253970f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 254070f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 254170f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 254270f6d82eSOmar Sandoval if (IS_ERR(root)) 254370f6d82eSOmar Sandoval return PTR_ERR(root); 254470f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 254570f6d82eSOmar Sandoval fs_info->free_space_root = root; 254670f6d82eSOmar Sandoval } 254770f6d82eSOmar Sandoval 25484bbcaa64SEric Sandeen return 0; 25494bbcaa64SEric Sandeen } 25504bbcaa64SEric Sandeen 2551ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2552dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2553dfe25020SChris Mason char *options) 2554eb60ceacSChris Mason { 2555db94535dSChris Mason u32 sectorsize; 2556db94535dSChris Mason u32 nodesize; 255787ee04ebSChris Mason u32 stripesize; 255884234f3aSYan Zheng u64 generation; 2559f2b636e8SJosef Bacik u64 features; 25603de4586cSChris Mason struct btrfs_key location; 2561a061fc8dSChris Mason struct buffer_head *bh; 25624d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2563815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2564f84a8bd6SAl Viro struct btrfs_root *tree_root; 25654d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2566eb60ceacSChris Mason int ret; 2567e58ca020SYan int err = -EINVAL; 2568af31f5e5SChris Mason int num_backups_tried = 0; 2569af31f5e5SChris Mason int backup_index = 0; 25705cdc7ad3SQu Wenruo int max_active; 25716675df31SOmar Sandoval int clear_free_space_tree = 0; 25724543df7eSChris Mason 257374e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 257474e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2575cb517eabSMiao Xie if (!tree_root || !chunk_root) { 257639279cc3SChris Mason err = -ENOMEM; 257739279cc3SChris Mason goto fail; 257839279cc3SChris Mason } 257976dda93cSYan, Zheng 258076dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 258176dda93cSYan, Zheng if (ret) { 258276dda93cSYan, Zheng err = ret; 258376dda93cSYan, Zheng goto fail; 258476dda93cSYan, Zheng } 258576dda93cSYan, Zheng 258676dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 258776dda93cSYan, Zheng if (ret) { 258876dda93cSYan, Zheng err = ret; 258976dda93cSYan, Zheng goto fail_srcu; 259076dda93cSYan, Zheng } 259176dda93cSYan, Zheng 2592908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2593e2d84521SMiao Xie if (ret) { 2594e2d84521SMiao Xie err = ret; 2595e2d84521SMiao Xie goto fail_bdi; 2596e2d84521SMiao Xie } 259709cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2598e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2599e2d84521SMiao Xie 2600908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2601963d678bSMiao Xie if (ret) { 2602963d678bSMiao Xie err = ret; 2603963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2604963d678bSMiao Xie } 2605963d678bSMiao Xie 2606908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2607c404e0dcSMiao Xie if (ret) { 2608c404e0dcSMiao Xie err = ret; 2609c404e0dcSMiao Xie goto fail_delalloc_bytes; 2610c404e0dcSMiao Xie } 2611c404e0dcSMiao Xie 261276dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 261376dda93cSYan, Zheng if (!fs_info->btree_inode) { 261476dda93cSYan, Zheng err = -ENOMEM; 2615c404e0dcSMiao Xie goto fail_bio_counter; 261676dda93cSYan, Zheng } 261776dda93cSYan, Zheng 2618a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 26191561dedaSMiao Xie 262076dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2621f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26228fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2623facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 262424bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2625eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 262611833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2627eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2628a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 262976dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 263024bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26314cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 26322bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2633f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2634ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2635fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2636f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 263747ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2638f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2639d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 264067c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26417585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2642573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2643c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2644de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 264519c00ddcSChris Mason 26460b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26476324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2648f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 264947ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 26500b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 265166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 265266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 265366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 265466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 265566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 265666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 265766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 265866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 265966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2660cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2661771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 26628c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 26630986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 26644cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2665fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 26662fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2667fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 26689e7cc91aSWang Xiaoguang fs_info->fs_frozen = 0; 2669e20d96d6SChris Mason fs_info->sb = sb; 267095ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 26719ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 26724cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 26732bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2674f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 26758b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2676f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 267790519d66SArne Jansen /* readahead state */ 2678d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 267990519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2680c8b97818SChris Mason 2681b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2682b34b086cSChris Mason num_online_cpus() + 2, 8); 26830afbaf8cSChris Mason 2684199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2685199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 268616cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 268774e4d827SDavid Sterba GFP_KERNEL); 268816cdcec7SMiao Xie if (!fs_info->delayed_root) { 268916cdcec7SMiao Xie err = -ENOMEM; 269016cdcec7SMiao Xie goto fail_iput; 269116cdcec7SMiao Xie } 269216cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 26933eaa2885SChris Mason 2694638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 269521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 269621adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 269721adbd5cSStefan Behrens #endif 2698779a65a4SEric Sandeen btrfs_init_balance(fs_info); 269921c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2700a2de733cSArne Jansen 2701a061fc8dSChris Mason sb->s_blocksize = 4096; 2702a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 270332a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 2704a061fc8dSChris Mason 27056bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 270639279cc3SChris Mason 2707e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 27086bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2709a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2710925baeddSChris Mason 271111833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2712f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 271311833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2714f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 271511833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2716afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2717509659cdSChris Mason 27185a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 271979154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2720925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2721a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2722a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27237d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 27241bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27259e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2726c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 272776dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2728803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2729fa9c0d79SChris Mason 2730ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2731f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2732416ac51dSArne Jansen 2733fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2734fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2735fa9c0d79SChris Mason 2736e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2737f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2738bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27394854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 27403768f368SChris Mason 274104216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 274204216820SFilipe Manana 2743da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2744da17066cSJeff Mahoney fs_info->nodesize = 4096; 2745da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2746da17066cSJeff Mahoney fs_info->stripesize = 4096; 2747da17066cSJeff Mahoney 274853b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 274953b381b3SDavid Woodhouse if (ret) { 275083c8266aSDavid Sterba err = ret; 275153b381b3SDavid Woodhouse goto fail_alloc; 275253b381b3SDavid Woodhouse } 275353b381b3SDavid Woodhouse 2754da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 27557eccb903SChris Mason 27563c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 27571104a885SDavid Sterba 27581104a885SDavid Sterba /* 27591104a885SDavid Sterba * Read super block and check the signature bytes only 27601104a885SDavid Sterba */ 2761a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 276292fc03fbSAnand Jain if (IS_ERR(bh)) { 276392fc03fbSAnand Jain err = PTR_ERR(bh); 276416cdcec7SMiao Xie goto fail_alloc; 276520b45077SDave Young } 276639279cc3SChris Mason 27671104a885SDavid Sterba /* 27681104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 27691104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 27701104a885SDavid Sterba */ 2771ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 277205135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 27731104a885SDavid Sterba err = -EINVAL; 2774b2acdddfSAnand Jain brelse(bh); 27751104a885SDavid Sterba goto fail_alloc; 27761104a885SDavid Sterba } 27771104a885SDavid Sterba 27781104a885SDavid Sterba /* 27791104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 27801104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 27811104a885SDavid Sterba * the whole block of INFO_SIZE 27821104a885SDavid Sterba */ 27836c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 27846c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 27856c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2786a061fc8dSChris Mason brelse(bh); 27875f39d397SChris Mason 27886c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 27890b86a832SChris Mason 27901104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 27911104a885SDavid Sterba if (ret) { 279205135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 27931104a885SDavid Sterba err = -EINVAL; 27941104a885SDavid Sterba goto fail_alloc; 27951104a885SDavid Sterba } 27961104a885SDavid Sterba 27976c41761fSDavid Sterba disk_super = fs_info->super_copy; 27980f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 279916cdcec7SMiao Xie goto fail_alloc; 28000f7d52f4SChris Mason 2801acce952bSliubo /* check FS state, whether FS is broken. */ 280287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 280387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2804acce952bSliubo 280575e7cb7fSLiu Bo /* 2806af31f5e5SChris Mason * run through our array of backup supers and setup 2807af31f5e5SChris Mason * our ring pointer to the oldest one 2808af31f5e5SChris Mason */ 2809af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2810af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2811af31f5e5SChris Mason 2812af31f5e5SChris Mason /* 281375e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 281475e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 281575e7cb7fSLiu Bo */ 281675e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 281775e7cb7fSLiu Bo 28182ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 28192b82032cSYan Zheng if (ret) { 28202b82032cSYan Zheng err = ret; 282116cdcec7SMiao Xie goto fail_alloc; 28222b82032cSYan Zheng } 2823dfe25020SChris Mason 2824f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2825f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2826f2b636e8SJosef Bacik if (features) { 282705135f59SDavid Sterba btrfs_err(fs_info, 282805135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2829c1c9ff7cSGeert Uytterhoeven features); 2830f2b636e8SJosef Bacik err = -EINVAL; 283116cdcec7SMiao Xie goto fail_alloc; 2832f2b636e8SJosef Bacik } 2833f2b636e8SJosef Bacik 28345d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 28355d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 28360b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2837a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2838727011e0SChris Mason 28393173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 284005135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 28413173a18fSJosef Bacik 2842727011e0SChris Mason /* 2843727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2844727011e0SChris Mason * they are bigger than the page size 2845727011e0SChris Mason */ 284609cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2847727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 284805135f59SDavid Sterba btrfs_info(fs_info, 284905135f59SDavid Sterba "flagging fs with big metadata feature"); 2850727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2851727011e0SChris Mason } 2852727011e0SChris Mason 2853bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2854bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2855b7f67055SChandan Rajendra stripesize = sectorsize; 2856707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2857963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2858bc3f116fSChris Mason 2859da17066cSJeff Mahoney /* Cache block sizes */ 2860da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2861da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2862da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2863da17066cSJeff Mahoney 2864bc3f116fSChris Mason /* 2865bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2866bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2867bc3f116fSChris Mason */ 2868bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2869707e8a07SDavid Sterba (sectorsize != nodesize)) { 287005135f59SDavid Sterba btrfs_err(fs_info, 287105135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 287205135f59SDavid Sterba nodesize, sectorsize); 2873bc3f116fSChris Mason goto fail_alloc; 2874bc3f116fSChris Mason } 2875bc3f116fSChris Mason 2876ceda0864SMiao Xie /* 2877ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2878ceda0864SMiao Xie * update the flag. 2879ceda0864SMiao Xie */ 28805d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 28815d4f98a2SYan Zheng 2882f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2883f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2884f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 288505135f59SDavid Sterba btrfs_err(fs_info, 288605135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2887c1c9ff7cSGeert Uytterhoeven features); 2888f2b636e8SJosef Bacik err = -EINVAL; 288916cdcec7SMiao Xie goto fail_alloc; 2890f2b636e8SJosef Bacik } 289161d92c32SChris Mason 28925cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 289361d92c32SChris Mason 28942a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 28952a458198SEric Sandeen if (ret) { 28962a458198SEric Sandeen err = ret; 28970dc3b84aSJosef Bacik goto fail_sb_buffer; 28980dc3b84aSJosef Bacik } 28994543df7eSChris Mason 29004575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2901c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 290209cbfeafSKirill A. Shutemov SZ_4M / PAGE_SIZE); 29034575c9ccSChris Mason 2904a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2905a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2906db94535dSChris Mason 2907925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 29086bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2909925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 291084eed90fSChris Mason if (ret) { 291105135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 29125d4f98a2SYan Zheng goto fail_sb_buffer; 291384eed90fSChris Mason } 29140b86a832SChris Mason 291584234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 29160b86a832SChris Mason 2917da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 29180b86a832SChris Mason 29192ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 29200b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2921ce86cd59SDavid Sterba generation); 292264c043deSLiu Bo if (IS_ERR(chunk_root->node) || 292364c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 292405135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2925e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2926e5fffbacSchandan free_extent_buffer(chunk_root->node); 292795ab1f64SZhao Lei chunk_root->node = NULL; 2928af31f5e5SChris Mason goto fail_tree_roots; 292983121942SDavid Woodhouse } 29305d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 29315d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 29320b86a832SChris Mason 2933e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2934b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2935e17cade2SChris Mason 29365b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 29372b82032cSYan Zheng if (ret) { 293805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2939af31f5e5SChris Mason goto fail_tree_roots; 29402b82032cSYan Zheng } 29410b86a832SChris Mason 29428dabb742SStefan Behrens /* 29438dabb742SStefan Behrens * keep the device that is marked to be the target device for the 29448dabb742SStefan Behrens * dev_replace procedure 29458dabb742SStefan Behrens */ 29469eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 0); 2947dfe25020SChris Mason 2948a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 294905135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2950a6b0d5c8SChris Mason goto fail_tree_roots; 2951a6b0d5c8SChris Mason } 2952a6b0d5c8SChris Mason 2953af31f5e5SChris Mason retry_root_backup: 295484234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 29550b86a832SChris Mason 29562ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2957db94535dSChris Mason btrfs_super_root(disk_super), 2958ce86cd59SDavid Sterba generation); 295964c043deSLiu Bo if (IS_ERR(tree_root->node) || 296064c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 296105135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2962e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2963e5fffbacSchandan free_extent_buffer(tree_root->node); 296495ab1f64SZhao Lei tree_root->node = NULL; 2965af31f5e5SChris Mason goto recovery_tree_root; 296683121942SDavid Woodhouse } 2967af31f5e5SChris Mason 29685d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 29695d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 297069e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2971db94535dSChris Mason 2972f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2973f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2974f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2975f32e48e9SChandan Rajendra if (ret) { 2976f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2977f32e48e9SChandan Rajendra goto recovery_tree_root; 2978f32e48e9SChandan Rajendra } 2979f32e48e9SChandan Rajendra 2980f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2981f32e48e9SChandan Rajendra 2982f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2983f32e48e9SChandan Rajendra 29846bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 29854bbcaa64SEric Sandeen if (ret) 2986af31f5e5SChris Mason goto recovery_tree_root; 2987f7a81ea4SStefan Behrens 29888929ecfaSYan, Zheng fs_info->generation = generation; 29898929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 29908929ecfaSYan, Zheng 299168310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 299268310a5eSIlya Dryomov if (ret) { 299305135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 299468310a5eSIlya Dryomov goto fail_block_groups; 299568310a5eSIlya Dryomov } 299668310a5eSIlya Dryomov 2997733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2998733f4fbbSStefan Behrens if (ret) { 299905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3000733f4fbbSStefan Behrens goto fail_block_groups; 3001733f4fbbSStefan Behrens } 3002733f4fbbSStefan Behrens 30038dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 30048dabb742SStefan Behrens if (ret) { 300505135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 30068dabb742SStefan Behrens goto fail_block_groups; 30078dabb742SStefan Behrens } 30088dabb742SStefan Behrens 30099eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 1); 30108dabb742SStefan Behrens 3011b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 3012b7c35e81SAnand Jain if (ret) { 301305135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 301405135f59SDavid Sterba ret); 3015b7c35e81SAnand Jain goto fail_block_groups; 3016b7c35e81SAnand Jain } 3017b7c35e81SAnand Jain 3018b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 3019b7c35e81SAnand Jain if (ret) { 302005135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 302105135f59SDavid Sterba ret); 3022b7c35e81SAnand Jain goto fail_fsdev_sysfs; 3023b7c35e81SAnand Jain } 3024b7c35e81SAnand Jain 302596f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 30265ac1d209SJeff Mahoney if (ret) { 302705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3028b7c35e81SAnand Jain goto fail_fsdev_sysfs; 30295ac1d209SJeff Mahoney } 30305ac1d209SJeff Mahoney 3031c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3032c59021f8Sliubo if (ret) { 303305135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 30342365dd3cSAnand Jain goto fail_sysfs; 3035c59021f8Sliubo } 3036c59021f8Sliubo 30375b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 30381b1d1f66SJosef Bacik if (ret) { 303905135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 30402365dd3cSAnand Jain goto fail_sysfs; 30411b1d1f66SJosef Bacik } 30425af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 30435af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 3044292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 3045292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 3046292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 304705135f59SDavid Sterba btrfs_warn(fs_info, 304805135f59SDavid Sterba "missing devices (%llu) exceeds the limit (%d), writeable mount is not allowed", 304978fa1770SZhao Lei fs_info->fs_devices->missing_devices, 305078fa1770SZhao Lei fs_info->num_tolerated_disk_barrier_failures); 30512365dd3cSAnand Jain goto fail_sysfs; 3052292fd7fcSStefan Behrens } 30539078a3e1SChris Mason 3054a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3055a74a4b97SChris Mason "btrfs-cleaner"); 305657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 30572365dd3cSAnand Jain goto fail_sysfs; 3058a74a4b97SChris Mason 3059a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3060a74a4b97SChris Mason tree_root, 3061a74a4b97SChris Mason "btrfs-transaction"); 306257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 30633f157a2fSChris Mason goto fail_cleaner; 3064a74a4b97SChris Mason 30650b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, SSD) && 30660b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOSSD) && 3067c289811cSChris Mason !fs_info->fs_devices->rotating) { 306805135f59SDavid Sterba btrfs_info(fs_info, "detected SSD devices, enabling SSD mode"); 3069c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 3070c289811cSChris Mason } 3071c289811cSChris Mason 3072572d9ab7SDavid Sterba /* 307301327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3074572d9ab7SDavid Sterba * not have to wait for transaction commit 3075572d9ab7SDavid Sterba */ 3076572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 30773818aea2SQu Wenruo 307821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 30790b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 30802ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 30810b246afaSJeff Mahoney btrfs_test_opt(fs_info, 308221adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 308321adbd5cSStefan Behrens 1 : 0, 308421adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 308521adbd5cSStefan Behrens if (ret) 308605135f59SDavid Sterba btrfs_warn(fs_info, 308705135f59SDavid Sterba "failed to initialize integrity check module: %d", 308805135f59SDavid Sterba ret); 308921adbd5cSStefan Behrens } 309021adbd5cSStefan Behrens #endif 3091bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3092bcef60f2SArne Jansen if (ret) 3093bcef60f2SArne Jansen goto fail_trans_kthread; 309421adbd5cSStefan Behrens 309596da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 309696da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 30970b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 309863443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 309979787eaaSJeff Mahoney if (ret) { 310063443bf5SEric Sandeen err = ret; 310128c16cbbSWang Shilong goto fail_qgroup; 3102e556ce2cSYan Zheng } 3103e02119d5SChris Mason } 31041a40e23bSZheng Yan 31056bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 310679787eaaSJeff Mahoney if (ret) 310728c16cbbSWang Shilong goto fail_qgroup; 310876dda93cSYan, Zheng 31097c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 3110d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 311144c44af2SIlya Dryomov if (ret) 311228c16cbbSWang Shilong goto fail_qgroup; 311390c711abSZygo Blaxell 311490c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 31155d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 311690c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3117d7ce5843SMiao Xie if (ret < 0) { 311805135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 311905135f59SDavid Sterba ret); 3120d7ce5843SMiao Xie err = -EINVAL; 3121bcef60f2SArne Jansen goto fail_qgroup; 3122d7ce5843SMiao Xie } 31237c2ca468SChris Mason } 31241a40e23bSZheng Yan 31253de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 31263de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3127cb517eabSMiao Xie location.offset = 0; 31283de4586cSChris Mason 31293de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 31303140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 31313140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3132bcef60f2SArne Jansen goto fail_qgroup; 31333140c9a3SDan Carpenter } 3134c289811cSChris Mason 31352b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 31362b6ba629SIlya Dryomov return 0; 31372b6ba629SIlya Dryomov 3138f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3139f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 31406675df31SOmar Sandoval clear_free_space_tree = 1; 31416675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 31426675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 31436675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 31446675df31SOmar Sandoval clear_free_space_tree = 1; 31456675df31SOmar Sandoval } 31466675df31SOmar Sandoval 31476675df31SOmar Sandoval if (clear_free_space_tree) { 3148f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3149f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3150f8d468a1SOmar Sandoval if (ret) { 3151f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3152f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 31536bccf3abSJeff Mahoney close_ctree(fs_info); 3154f8d468a1SOmar Sandoval return ret; 3155f8d468a1SOmar Sandoval } 3156f8d468a1SOmar Sandoval } 3157f8d468a1SOmar Sandoval 31580b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3159511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 316005135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3161511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3162511711afSChris Mason if (ret) { 316305135f59SDavid Sterba btrfs_warn(fs_info, 316405135f59SDavid Sterba "failed to create free space tree: %d", ret); 31656bccf3abSJeff Mahoney close_ctree(fs_info); 3166511711afSChris Mason return ret; 3167511711afSChris Mason } 3168511711afSChris Mason } 3169511711afSChris Mason 3170e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 31712b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 31722b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 31732b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 31746bccf3abSJeff Mahoney close_ctree(fs_info); 31752b6ba629SIlya Dryomov return ret; 31762b6ba629SIlya Dryomov } 3177e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 317859641015SIlya Dryomov 31792b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 31802b6ba629SIlya Dryomov if (ret) { 318105135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 31826bccf3abSJeff Mahoney close_ctree(fs_info); 31832b6ba629SIlya Dryomov return ret; 3184e3acc2a6SJosef Bacik } 3185e3acc2a6SJosef Bacik 31868dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 31878dabb742SStefan Behrens if (ret) { 318805135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 31896bccf3abSJeff Mahoney close_ctree(fs_info); 31908dabb742SStefan Behrens return ret; 31918dabb742SStefan Behrens } 31928dabb742SStefan Behrens 3193b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3194b382a324SJan Schmidt 31954bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 319605135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3197f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3198f7a81ea4SStefan Behrens if (ret) { 319905135f59SDavid Sterba btrfs_warn(fs_info, 320005135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 32016bccf3abSJeff Mahoney close_ctree(fs_info); 3202f7a81ea4SStefan Behrens return ret; 3203f7a81ea4SStefan Behrens } 32040b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 32054bbcaa64SEric Sandeen fs_info->generation != 32064bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 320705135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 320870f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 320970f80175SStefan Behrens if (ret) { 321005135f59SDavid Sterba btrfs_warn(fs_info, 321105135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 32126bccf3abSJeff Mahoney close_ctree(fs_info); 321370f80175SStefan Behrens return ret; 321470f80175SStefan Behrens } 321570f80175SStefan Behrens } else { 3216afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3217f7a81ea4SStefan Behrens } 3218afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 321947ab2a6cSJosef Bacik 32208dcddfa0SQu Wenruo /* 32218dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 32228dcddfa0SQu Wenruo * no need to keep the flag 32238dcddfa0SQu Wenruo */ 32248dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32258dcddfa0SQu Wenruo 3226ad2b2c80SAl Viro return 0; 322739279cc3SChris Mason 3228bcef60f2SArne Jansen fail_qgroup: 3229bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 32307c2ca468SChris Mason fail_trans_kthread: 32317c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 32322ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3233faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 32343f157a2fSChris Mason fail_cleaner: 3235a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 32367c2ca468SChris Mason 32377c2ca468SChris Mason /* 32387c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 32397c2ca468SChris Mason * kthreads 32407c2ca468SChris Mason */ 32417c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 32427c2ca468SChris Mason 32432365dd3cSAnand Jain fail_sysfs: 32446618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 32452365dd3cSAnand Jain 3246b7c35e81SAnand Jain fail_fsdev_sysfs: 3247b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3248b7c35e81SAnand Jain 32491b1d1f66SJosef Bacik fail_block_groups: 325054067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 32511b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 3252af31f5e5SChris Mason 3253af31f5e5SChris Mason fail_tree_roots: 3254af31f5e5SChris Mason free_root_pointers(fs_info, 1); 32552b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3256af31f5e5SChris Mason 325739279cc3SChris Mason fail_sb_buffer: 32587abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 325916cdcec7SMiao Xie fail_alloc: 32604543df7eSChris Mason fail_iput: 3261586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3262586e46e2SIlya Dryomov 32634543df7eSChris Mason iput(fs_info->btree_inode); 3264c404e0dcSMiao Xie fail_bio_counter: 3265c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3266963d678bSMiao Xie fail_delalloc_bytes: 3267963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3268e2d84521SMiao Xie fail_dirty_metadata_bytes: 3269e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3270ad081f14SJens Axboe fail_bdi: 32717e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 327276dda93cSYan, Zheng fail_srcu: 327376dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 32747e662854SQinghuang Feng fail: 327553b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3276586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3277ad2b2c80SAl Viro return err; 3278af31f5e5SChris Mason 3279af31f5e5SChris Mason recovery_tree_root: 32800b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3281af31f5e5SChris Mason goto fail_tree_roots; 3282af31f5e5SChris Mason 3283af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3284af31f5e5SChris Mason 3285af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3286af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3287af31f5e5SChris Mason 3288af31f5e5SChris Mason /* we can't trust the free space cache either */ 3289af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3290af31f5e5SChris Mason 3291af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3292af31f5e5SChris Mason &num_backups_tried, &backup_index); 3293af31f5e5SChris Mason if (ret == -1) 3294af31f5e5SChris Mason goto fail_block_groups; 3295af31f5e5SChris Mason goto retry_root_backup; 3296eb60ceacSChris Mason } 3297eb60ceacSChris Mason 3298f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3299f2984462SChris Mason { 3300f2984462SChris Mason if (uptodate) { 3301f2984462SChris Mason set_buffer_uptodate(bh); 3302f2984462SChris Mason } else { 3303442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3304442a4f63SStefan Behrens bh->b_private; 3305442a4f63SStefan Behrens 3306fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3307b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3308606686eeSJosef Bacik rcu_str_deref(device->name)); 330901327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 33101259ab75SChris Mason * our own ways of dealing with the IO errors 33111259ab75SChris Mason */ 3312f2984462SChris Mason clear_buffer_uptodate(bh); 3313442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3314f2984462SChris Mason } 3315f2984462SChris Mason unlock_buffer(bh); 3316f2984462SChris Mason put_bh(bh); 3317f2984462SChris Mason } 3318f2984462SChris Mason 331929c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 332029c36d72SAnand Jain struct buffer_head **bh_ret) 332129c36d72SAnand Jain { 332229c36d72SAnand Jain struct buffer_head *bh; 332329c36d72SAnand Jain struct btrfs_super_block *super; 332429c36d72SAnand Jain u64 bytenr; 332529c36d72SAnand Jain 332629c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 332729c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 332829c36d72SAnand Jain return -EINVAL; 332929c36d72SAnand Jain 333029c36d72SAnand Jain bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE); 333129c36d72SAnand Jain /* 333229c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 333329c36d72SAnand Jain * the best option we have is to mark it EIO. 333429c36d72SAnand Jain */ 333529c36d72SAnand Jain if (!bh) 333629c36d72SAnand Jain return -EIO; 333729c36d72SAnand Jain 333829c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 333929c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 334029c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 334129c36d72SAnand Jain brelse(bh); 334229c36d72SAnand Jain return -EINVAL; 334329c36d72SAnand Jain } 334429c36d72SAnand Jain 334529c36d72SAnand Jain *bh_ret = bh; 334629c36d72SAnand Jain return 0; 334729c36d72SAnand Jain } 334829c36d72SAnand Jain 334929c36d72SAnand Jain 3350a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3351a512bbf8SYan Zheng { 3352a512bbf8SYan Zheng struct buffer_head *bh; 3353a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3354a512bbf8SYan Zheng struct btrfs_super_block *super; 3355a512bbf8SYan Zheng int i; 3356a512bbf8SYan Zheng u64 transid = 0; 335792fc03fbSAnand Jain int ret = -EINVAL; 3358a512bbf8SYan Zheng 3359a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3360a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3361a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3362a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3363a512bbf8SYan Zheng */ 3364a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 336529c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 336629c36d72SAnand Jain if (ret) 3367a512bbf8SYan Zheng continue; 3368a512bbf8SYan Zheng 3369a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3370a512bbf8SYan Zheng 3371a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3372a512bbf8SYan Zheng brelse(latest); 3373a512bbf8SYan Zheng latest = bh; 3374a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3375a512bbf8SYan Zheng } else { 3376a512bbf8SYan Zheng brelse(bh); 3377a512bbf8SYan Zheng } 3378a512bbf8SYan Zheng } 337992fc03fbSAnand Jain 338092fc03fbSAnand Jain if (!latest) 338192fc03fbSAnand Jain return ERR_PTR(ret); 338292fc03fbSAnand Jain 3383a512bbf8SYan Zheng return latest; 3384a512bbf8SYan Zheng } 3385a512bbf8SYan Zheng 33864eedeb75SHisashi Hifumi /* 33874eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 33884eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 33894eedeb75SHisashi Hifumi * we write are pinned. 33904eedeb75SHisashi Hifumi * 33914eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 33924eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 33934eedeb75SHisashi Hifumi * many supers on this one device should be written. 33944eedeb75SHisashi Hifumi * 33954eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 33964eedeb75SHisashi Hifumi */ 3397a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3398a512bbf8SYan Zheng struct btrfs_super_block *sb, 3399a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 3400a512bbf8SYan Zheng { 3401a512bbf8SYan Zheng struct buffer_head *bh; 3402a512bbf8SYan Zheng int i; 3403a512bbf8SYan Zheng int ret; 3404a512bbf8SYan Zheng int errors = 0; 3405a512bbf8SYan Zheng u32 crc; 3406a512bbf8SYan Zheng u64 bytenr; 3407a512bbf8SYan Zheng 3408a512bbf8SYan Zheng if (max_mirrors == 0) 3409a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3410a512bbf8SYan Zheng 3411a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3412a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3413935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3414935e5cc9SMiao Xie device->commit_total_bytes) 3415a512bbf8SYan Zheng break; 3416a512bbf8SYan Zheng 3417a512bbf8SYan Zheng if (wait) { 3418a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3419a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3420634554dcSJosef Bacik if (!bh) { 3421634554dcSJosef Bacik errors++; 3422634554dcSJosef Bacik continue; 3423634554dcSJosef Bacik } 3424a512bbf8SYan Zheng wait_on_buffer(bh); 34254eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 34264eedeb75SHisashi Hifumi errors++; 34274eedeb75SHisashi Hifumi 34284eedeb75SHisashi Hifumi /* drop our reference */ 34294eedeb75SHisashi Hifumi brelse(bh); 34304eedeb75SHisashi Hifumi 34314eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3432a512bbf8SYan Zheng brelse(bh); 3433a512bbf8SYan Zheng continue; 3434a512bbf8SYan Zheng } else { 3435a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3436a512bbf8SYan Zheng 3437a512bbf8SYan Zheng crc = ~(u32)0; 3438b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3439a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3440a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3441a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3442a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3443a512bbf8SYan Zheng 34444eedeb75SHisashi Hifumi /* 34454eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 34464eedeb75SHisashi Hifumi * caller 34474eedeb75SHisashi Hifumi */ 3448a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3449a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3450634554dcSJosef Bacik if (!bh) { 3451fb456252SJeff Mahoney btrfs_err(device->fs_info, 3452f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3453f14d104dSDavid Sterba bytenr); 3454634554dcSJosef Bacik errors++; 3455634554dcSJosef Bacik continue; 3456634554dcSJosef Bacik } 3457634554dcSJosef Bacik 3458a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3459a512bbf8SYan Zheng 34604eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3461a512bbf8SYan Zheng get_bh(bh); 34624eedeb75SHisashi Hifumi 34634eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3464a512bbf8SYan Zheng lock_buffer(bh); 3465a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3466442a4f63SStefan Behrens bh->b_private = device; 3467a512bbf8SYan Zheng } 3468a512bbf8SYan Zheng 3469387125fcSChris Mason /* 3470387125fcSChris Mason * we fua the first super. The others we allow 3471387125fcSChris Mason * to go down lazy. 3472387125fcSChris Mason */ 3473e8117c26SWang Shilong if (i == 0) 34742a222ca9SMike Christie ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_FUA, bh); 3475e8117c26SWang Shilong else 34762a222ca9SMike Christie ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_SYNC, bh); 34774eedeb75SHisashi Hifumi if (ret) 3478a512bbf8SYan Zheng errors++; 3479a512bbf8SYan Zheng } 3480a512bbf8SYan Zheng return errors < i ? 0 : -1; 3481a512bbf8SYan Zheng } 3482a512bbf8SYan Zheng 3483387125fcSChris Mason /* 3484387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3485387125fcSChris Mason * for the barrier when it is done 3486387125fcSChris Mason */ 34874246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3488387125fcSChris Mason { 3489387125fcSChris Mason if (bio->bi_private) 3490387125fcSChris Mason complete(bio->bi_private); 3491387125fcSChris Mason bio_put(bio); 3492387125fcSChris Mason } 3493387125fcSChris Mason 3494387125fcSChris Mason /* 3495387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3496387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3497387125fcSChris Mason * 3498387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3499387125fcSChris Mason * capable 3500387125fcSChris Mason */ 3501387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3502387125fcSChris Mason { 3503387125fcSChris Mason struct bio *bio; 3504387125fcSChris Mason int ret = 0; 3505387125fcSChris Mason 3506387125fcSChris Mason if (device->nobarriers) 3507387125fcSChris Mason return 0; 3508387125fcSChris Mason 3509387125fcSChris Mason if (wait) { 3510387125fcSChris Mason bio = device->flush_bio; 3511387125fcSChris Mason if (!bio) 3512387125fcSChris Mason return 0; 3513387125fcSChris Mason 3514387125fcSChris Mason wait_for_completion(&device->flush_wait); 3515387125fcSChris Mason 35164246a0b6SChristoph Hellwig if (bio->bi_error) { 35174246a0b6SChristoph Hellwig ret = bio->bi_error; 3518442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3519442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3520387125fcSChris Mason } 3521387125fcSChris Mason 3522387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3523387125fcSChris Mason bio_put(bio); 3524387125fcSChris Mason device->flush_bio = NULL; 3525387125fcSChris Mason 3526387125fcSChris Mason return ret; 3527387125fcSChris Mason } 3528387125fcSChris Mason 3529387125fcSChris Mason /* 3530387125fcSChris Mason * one reference for us, and we leave it for the 3531387125fcSChris Mason * caller 3532387125fcSChris Mason */ 35339c017abcSJesper Juhl device->flush_bio = NULL; 35349be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3535387125fcSChris Mason if (!bio) 3536387125fcSChris Mason return -ENOMEM; 3537387125fcSChris Mason 3538387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3539387125fcSChris Mason bio->bi_bdev = device->bdev; 354037226b21SMike Christie bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH); 3541387125fcSChris Mason init_completion(&device->flush_wait); 3542387125fcSChris Mason bio->bi_private = &device->flush_wait; 3543387125fcSChris Mason device->flush_bio = bio; 3544387125fcSChris Mason 3545387125fcSChris Mason bio_get(bio); 35464e49ea4aSMike Christie btrfsic_submit_bio(bio); 3547387125fcSChris Mason 3548387125fcSChris Mason return 0; 3549387125fcSChris Mason } 3550387125fcSChris Mason 3551387125fcSChris Mason /* 3552387125fcSChris Mason * send an empty flush down to each device in parallel, 3553387125fcSChris Mason * then wait for them 3554387125fcSChris Mason */ 3555387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3556387125fcSChris Mason { 3557387125fcSChris Mason struct list_head *head; 3558387125fcSChris Mason struct btrfs_device *dev; 35595af3e8ccSStefan Behrens int errors_send = 0; 35605af3e8ccSStefan Behrens int errors_wait = 0; 3561387125fcSChris Mason int ret; 3562387125fcSChris Mason 3563387125fcSChris Mason /* send down all the barriers */ 3564387125fcSChris Mason head = &info->fs_devices->devices; 3565387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3566f88ba6a2SHidetoshi Seto if (dev->missing) 3567f88ba6a2SHidetoshi Seto continue; 3568387125fcSChris Mason if (!dev->bdev) { 35695af3e8ccSStefan Behrens errors_send++; 3570387125fcSChris Mason continue; 3571387125fcSChris Mason } 3572387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3573387125fcSChris Mason continue; 3574387125fcSChris Mason 3575387125fcSChris Mason ret = write_dev_flush(dev, 0); 3576387125fcSChris Mason if (ret) 35775af3e8ccSStefan Behrens errors_send++; 3578387125fcSChris Mason } 3579387125fcSChris Mason 3580387125fcSChris Mason /* wait for all the barriers */ 3581387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3582f88ba6a2SHidetoshi Seto if (dev->missing) 3583f88ba6a2SHidetoshi Seto continue; 3584387125fcSChris Mason if (!dev->bdev) { 35855af3e8ccSStefan Behrens errors_wait++; 3586387125fcSChris Mason continue; 3587387125fcSChris Mason } 3588387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3589387125fcSChris Mason continue; 3590387125fcSChris Mason 3591387125fcSChris Mason ret = write_dev_flush(dev, 1); 3592387125fcSChris Mason if (ret) 35935af3e8ccSStefan Behrens errors_wait++; 3594387125fcSChris Mason } 35955af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 35965af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3597387125fcSChris Mason return -EIO; 3598387125fcSChris Mason return 0; 3599387125fcSChris Mason } 3600387125fcSChris Mason 3601943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3602943c6e99SZhao Lei { 36038789f4feSZhao Lei int raid_type; 36048789f4feSZhao Lei int min_tolerated = INT_MAX; 3605943c6e99SZhao Lei 36068789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 36078789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 36088789f4feSZhao Lei min_tolerated = min(min_tolerated, 36098789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 36108789f4feSZhao Lei tolerated_failures); 3611943c6e99SZhao Lei 36128789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 36138789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 36148789f4feSZhao Lei continue; 36158789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 36168789f4feSZhao Lei continue; 36178789f4feSZhao Lei min_tolerated = min(min_tolerated, 36188789f4feSZhao Lei btrfs_raid_array[raid_type]. 36198789f4feSZhao Lei tolerated_failures); 36208789f4feSZhao Lei } 3621943c6e99SZhao Lei 36228789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3623ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 36248789f4feSZhao Lei min_tolerated = 0; 36258789f4feSZhao Lei } 36268789f4feSZhao Lei 36278789f4feSZhao Lei return min_tolerated; 3628943c6e99SZhao Lei } 3629943c6e99SZhao Lei 36305af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 36315af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 36325af3e8ccSStefan Behrens { 36335af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 36345af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 36355af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 36365af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 36375af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 36385af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 36395af3e8ccSStefan Behrens int i; 36405af3e8ccSStefan Behrens int c; 36415af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 36425af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 36435af3e8ccSStefan Behrens 36442c458045SZhao Lei for (i = 0; i < ARRAY_SIZE(types); i++) { 36455af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 36465af3e8ccSStefan Behrens 36475af3e8ccSStefan Behrens sinfo = NULL; 36485af3e8ccSStefan Behrens rcu_read_lock(); 36495af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 36505af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 36515af3e8ccSStefan Behrens sinfo = tmp; 36525af3e8ccSStefan Behrens break; 36535af3e8ccSStefan Behrens } 36545af3e8ccSStefan Behrens } 36555af3e8ccSStefan Behrens rcu_read_unlock(); 36565af3e8ccSStefan Behrens 36575af3e8ccSStefan Behrens if (!sinfo) 36585af3e8ccSStefan Behrens continue; 36595af3e8ccSStefan Behrens 36605af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 36615af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 36625af3e8ccSStefan Behrens u64 flags; 36635af3e8ccSStefan Behrens 36642c458045SZhao Lei if (list_empty(&sinfo->block_groups[c])) 36652c458045SZhao Lei continue; 36662c458045SZhao Lei 36672c458045SZhao Lei btrfs_get_block_group_info(&sinfo->block_groups[c], 36682c458045SZhao Lei &space); 36692c458045SZhao Lei if (space.total_bytes == 0 || space.used_bytes == 0) 36705af3e8ccSStefan Behrens continue; 36715af3e8ccSStefan Behrens flags = space.flags; 3672943c6e99SZhao Lei 3673943c6e99SZhao Lei num_tolerated_disk_barrier_failures = min( 3674943c6e99SZhao Lei num_tolerated_disk_barrier_failures, 3675943c6e99SZhao Lei btrfs_get_num_tolerated_disk_barrier_failures( 3676943c6e99SZhao Lei flags)); 36775af3e8ccSStefan Behrens } 36785af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 36795af3e8ccSStefan Behrens } 36805af3e8ccSStefan Behrens 36815af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 36825af3e8ccSStefan Behrens } 36835af3e8ccSStefan Behrens 36842ff7e61eSJeff Mahoney static int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3685f2984462SChris Mason { 3686e5e9a520SChris Mason struct list_head *head; 3687f2984462SChris Mason struct btrfs_device *dev; 3688a061fc8dSChris Mason struct btrfs_super_block *sb; 3689f2984462SChris Mason struct btrfs_dev_item *dev_item; 3690f2984462SChris Mason int ret; 3691f2984462SChris Mason int do_barriers; 3692a236aed1SChris Mason int max_errors; 3693a236aed1SChris Mason int total_errors = 0; 3694a061fc8dSChris Mason u64 flags; 3695f2984462SChris Mason 36960b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 36970b246afaSJeff Mahoney backup_super_roots(fs_info); 3698f2984462SChris Mason 36990b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3700a061fc8dSChris Mason dev_item = &sb->dev_item; 3701e5e9a520SChris Mason 37020b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37030b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37040b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3705387125fcSChris Mason 37065af3e8ccSStefan Behrens if (do_barriers) { 37070b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37085af3e8ccSStefan Behrens if (ret) { 37095af3e8ccSStefan Behrens mutex_unlock( 37100b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 37110b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 37125af3e8ccSStefan Behrens "errors while submitting device barriers."); 37135af3e8ccSStefan Behrens return ret; 37145af3e8ccSStefan Behrens } 37155af3e8ccSStefan Behrens } 3716387125fcSChris Mason 37171f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3718dfe25020SChris Mason if (!dev->bdev) { 3719dfe25020SChris Mason total_errors++; 3720dfe25020SChris Mason continue; 3721dfe25020SChris Mason } 37222b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3723dfe25020SChris Mason continue; 3724dfe25020SChris Mason 37252b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3726a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3727a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 37287df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3729935e5cc9SMiao Xie dev->commit_total_bytes); 3730ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3731ce7213c7SMiao Xie dev->commit_bytes_used); 3732a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3733a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3734a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3735a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 37362b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3737a512bbf8SYan Zheng 3738a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3739a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3740f2984462SChris Mason 3741a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3742a236aed1SChris Mason if (ret) 3743a236aed1SChris Mason total_errors++; 3744f2984462SChris Mason } 3745a236aed1SChris Mason if (total_errors > max_errors) { 37460b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3747d397712bSChris Mason total_errors); 37480b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 374979787eaaSJeff Mahoney 37509d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 37510b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37520b246afaSJeff Mahoney "%d errors while writing supers", 37530b246afaSJeff Mahoney total_errors); 37549d565ba4SStefan Behrens return -EIO; 3755a236aed1SChris Mason } 3756f2984462SChris Mason 3757a512bbf8SYan Zheng total_errors = 0; 37581f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3759dfe25020SChris Mason if (!dev->bdev) 3760dfe25020SChris Mason continue; 37612b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3762dfe25020SChris Mason continue; 3763dfe25020SChris Mason 3764a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3765a512bbf8SYan Zheng if (ret) 37661259ab75SChris Mason total_errors++; 3767f2984462SChris Mason } 37680b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3769a236aed1SChris Mason if (total_errors > max_errors) { 37700b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37710b246afaSJeff Mahoney "%d errors while writing supers", 37720b246afaSJeff Mahoney total_errors); 377379787eaaSJeff Mahoney return -EIO; 3774a236aed1SChris Mason } 3775f2984462SChris Mason return 0; 3776f2984462SChris Mason } 3777f2984462SChris Mason 3778a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 37792ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, int max_mirrors) 3780cfaa7295SChris Mason { 37812ff7e61eSJeff Mahoney return write_all_supers(fs_info, max_mirrors); 3782cfaa7295SChris Mason } 3783cfaa7295SChris Mason 3784cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3785cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3786cb517eabSMiao Xie struct btrfs_root *root) 37872619ba1fSChris Mason { 37884df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 37892619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 37902619ba1fSChris Mason (unsigned long)root->root_key.objectid); 37914df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 379276dda93cSYan, Zheng 379376dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 379476dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 379576dda93cSYan, Zheng 37961c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 37973321719eSLiu Bo btrfs_free_log(NULL, root); 37981c1ea4f7SLiu Bo if (root->reloc_root) { 37991c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38001c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 38011c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 38021c1ea4f7SLiu Bo root->reloc_root = NULL; 38031c1ea4f7SLiu Bo } 38041c1ea4f7SLiu Bo } 38053321719eSLiu Bo 3806faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3807581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3808faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3809581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 38104df27c4dSYan, Zheng free_fs_root(root); 38114df27c4dSYan, Zheng } 38124df27c4dSYan, Zheng 38134df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 38144df27c4dSYan, Zheng { 381557cdc8dbSDavid Sterba iput(root->ino_cache_inode); 38164df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 38172ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 38181cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 38190ee5dc67SAl Viro if (root->anon_dev) 38200ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 38218257b2dcSMiao Xie if (root->subv_writers) 38228257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 38235f39d397SChris Mason free_extent_buffer(root->node); 38245f39d397SChris Mason free_extent_buffer(root->commit_root); 3825581bb050SLi Zefan kfree(root->free_ino_ctl); 3826581bb050SLi Zefan kfree(root->free_ino_pinned); 382758176a96SJosef Bacik kfree(root->name); 3828b0feb9d9SMiao Xie btrfs_put_fs_root(root); 38292619ba1fSChris Mason } 38302619ba1fSChris Mason 3831cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3832cb517eabSMiao Xie { 3833cb517eabSMiao Xie free_fs_root(root); 3834eb60ceacSChris Mason } 3835eb60ceacSChris Mason 3836c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3837c146afadSYan Zheng { 3838c146afadSYan Zheng u64 root_objectid = 0; 3839c146afadSYan Zheng struct btrfs_root *gang[8]; 384065d33fd7SQu Wenruo int i = 0; 384165d33fd7SQu Wenruo int err = 0; 384265d33fd7SQu Wenruo unsigned int ret = 0; 384365d33fd7SQu Wenruo int index; 3844c146afadSYan Zheng 3845c146afadSYan Zheng while (1) { 384665d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3847c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3848c146afadSYan Zheng (void **)gang, root_objectid, 3849c146afadSYan Zheng ARRAY_SIZE(gang)); 385065d33fd7SQu Wenruo if (!ret) { 385165d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3852c146afadSYan Zheng break; 385365d33fd7SQu Wenruo } 38545d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 385566b4ffd1SJosef Bacik 385665d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 385765d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 385865d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 385965d33fd7SQu Wenruo gang[i] = NULL; 386065d33fd7SQu Wenruo continue; 386165d33fd7SQu Wenruo } 386265d33fd7SQu Wenruo /* grab all the search result for later use */ 386365d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 386465d33fd7SQu Wenruo } 386565d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 386665d33fd7SQu Wenruo 386765d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 386865d33fd7SQu Wenruo if (!gang[i]) 386965d33fd7SQu Wenruo continue; 3870c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 387166b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 387266b4ffd1SJosef Bacik if (err) 387365d33fd7SQu Wenruo break; 387465d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3875c146afadSYan Zheng } 3876c146afadSYan Zheng root_objectid++; 3877c146afadSYan Zheng } 387865d33fd7SQu Wenruo 387965d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 388065d33fd7SQu Wenruo for (; i < ret; i++) { 388165d33fd7SQu Wenruo if (gang[i]) 388265d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 388365d33fd7SQu Wenruo } 388465d33fd7SQu Wenruo return err; 3885c146afadSYan Zheng } 3886c146afadSYan Zheng 38876bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3888c146afadSYan Zheng { 38896bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3890c146afadSYan Zheng struct btrfs_trans_handle *trans; 3891c146afadSYan Zheng 38920b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 38932ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 38940b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 38950b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3896c71bf099SYan, Zheng 3897c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 38980b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 38990b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3900c71bf099SYan, Zheng 39017a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39023612b495STsutomu Itoh if (IS_ERR(trans)) 39033612b495STsutomu Itoh return PTR_ERR(trans); 39043a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3905c146afadSYan Zheng } 3906c146afadSYan Zheng 39076bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3908eb60ceacSChris Mason { 39096bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3910c146afadSYan Zheng int ret; 3911e089f05cSChris Mason 3912afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3913a2135011SChris Mason 39147343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3915d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 39167343dd61SJustin Maggard 3917803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3918803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3919803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3920803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3921803b2f54SStefan Behrens 3922837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3923aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3924837d5b6eSIlya Dryomov 39258dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 39268dabb742SStefan Behrens 3927aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 39284cb5300bSChris Mason 39294cb5300bSChris Mason /* wait for any defraggers to finish */ 39304cb5300bSChris Mason wait_event(fs_info->transaction_wait, 39314cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 39324cb5300bSChris Mason 39334cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 393426176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 39354cb5300bSChris Mason 393621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 393721c7e756SMiao Xie 3938c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3939e44163e1SJeff Mahoney /* 3940e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3941e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3942e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3943e44163e1SJeff Mahoney */ 39440b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3945e44163e1SJeff Mahoney 39466bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3947d397712bSChris Mason if (ret) 394804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3949c146afadSYan Zheng } 3950ed2ff2cbSChris Mason 395187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 39522ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3953acce952bSliubo 3954e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3955e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 39568929ecfaSYan, Zheng 3957afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3958f25784b3SYan Zheng 395904892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3960bcef60f2SArne Jansen 3961963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 396204892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3963963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3964b0c68f8bSChris Mason } 396531153d81SYan Zheng 39666618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3967b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 39685ac1d209SJeff Mahoney 3969faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3970d10c5f31SChris Mason 39711a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 39721a4319ccSLiu Bo 39732b1360daSJosef Bacik btrfs_free_block_groups(fs_info); 39742b1360daSJosef Bacik 3975de348ee0SWang Shilong /* 3976de348ee0SWang Shilong * we must make sure there is not any read request to 3977de348ee0SWang Shilong * submit after we stopping all workers. 3978de348ee0SWang Shilong */ 3979de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 398096192499SJosef Bacik btrfs_stop_all_workers(fs_info); 398196192499SJosef Bacik 3982afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 398313e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 39849ad6b7bcSChris Mason 398513e6c37bSJosef Bacik iput(fs_info->btree_inode); 3986d6bfde87SChris Mason 398721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 39880b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 39892ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 399021adbd5cSStefan Behrens #endif 399121adbd5cSStefan Behrens 3992dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 39930b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3994b248a415SChris Mason 3995e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3996963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3997c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 399804160088SChris Mason bdi_destroy(&fs_info->bdi); 399976dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 40000b86a832SChris Mason 400153b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 400253b381b3SDavid Woodhouse 4003cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 40041cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 400504216820SFilipe Manana 40060b246afaSJeff Mahoney lock_chunks(fs_info); 400704216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 400804216820SFilipe Manana struct extent_map *em; 400904216820SFilipe Manana 401004216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 401104216820SFilipe Manana struct extent_map, list); 401204216820SFilipe Manana list_del_init(&em->list); 401304216820SFilipe Manana free_extent_map(em); 401404216820SFilipe Manana } 40150b246afaSJeff Mahoney unlock_chunks(fs_info); 4016eb60ceacSChris Mason } 4017eb60ceacSChris Mason 4018b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4019b9fab919SChris Mason int atomic) 4020ccd467d6SChris Mason { 40211259ab75SChris Mason int ret; 4022727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 40231259ab75SChris Mason 40240b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 40251259ab75SChris Mason if (!ret) 40261259ab75SChris Mason return ret; 40271259ab75SChris Mason 40281259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4029b9fab919SChris Mason parent_transid, atomic); 4030b9fab919SChris Mason if (ret == -EAGAIN) 4031b9fab919SChris Mason return ret; 40321259ab75SChris Mason return !ret; 40335f39d397SChris Mason } 40346702ed49SChris Mason 40355f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 40365f39d397SChris Mason { 40370b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 403806ea65a3SJosef Bacik struct btrfs_root *root; 40395f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4040b9473439SChris Mason int was_dirty; 4041b4ce94deSChris Mason 404206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 404306ea65a3SJosef Bacik /* 404406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 404506ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 404606ea65a3SJosef Bacik * outside of the sanity tests. 404706ea65a3SJosef Bacik */ 404806ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 404906ea65a3SJosef Bacik return; 405006ea65a3SJosef Bacik #endif 405106ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 40520b246afaSJeff Mahoney fs_info = root->fs_info; 4053b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 40540b246afaSJeff Mahoney if (transid != fs_info->generation) 40555d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 40560b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 40570b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4058e2d84521SMiao Xie if (!was_dirty) 40590b246afaSJeff Mahoney __percpu_counter_add(&fs_info->dirty_metadata_bytes, 4060e2d84521SMiao Xie buf->len, 40610b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 40621f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40631f21ef0aSFilipe Manana if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 40642ff7e61eSJeff Mahoney btrfs_print_leaf(fs_info, buf); 40651f21ef0aSFilipe Manana ASSERT(0); 40661f21ef0aSFilipe Manana } 40671f21ef0aSFilipe Manana #endif 4068eb60ceacSChris Mason } 4069eb60ceacSChris Mason 40702ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4071b53d3f5dSLiu Bo int flush_delayed) 407235b7e476SChris Mason { 4073188de649SChris Mason /* 4074188de649SChris Mason * looks as though older kernels can get into trouble with 4075188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4076188de649SChris Mason */ 4077e2d84521SMiao Xie int ret; 4078d6bfde87SChris Mason 40796933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4080d6bfde87SChris Mason return; 4081d6bfde87SChris Mason 4082b53d3f5dSLiu Bo if (flush_delayed) 40832ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 408416cdcec7SMiao Xie 40850b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4086e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 4087e2d84521SMiao Xie if (ret > 0) { 40880b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 408916cdcec7SMiao Xie } 409016cdcec7SMiao Xie } 409116cdcec7SMiao Xie 40922ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 409316cdcec7SMiao Xie { 40942ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 409535b7e476SChris Mason } 4096b53d3f5dSLiu Bo 40972ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4098b53d3f5dSLiu Bo { 40992ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4100d6bfde87SChris Mason } 41016b80053dSChris Mason 4102ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 41036b80053dSChris Mason { 4104727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41052ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41062ff7e61eSJeff Mahoney 41072ff7e61eSJeff Mahoney return btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 41086b80053dSChris Mason } 41090da5468fSChris Mason 4110fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 4111acce952bSliubo int read_only) 4112acce952bSliubo { 4113c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 4114319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 4115319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 4116c926093eSDavid Sterba int ret = 0; 4117c926093eSDavid Sterba 4118319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 4119ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 4120319e4d06SQu Wenruo ret = -EINVAL; 4121319e4d06SQu Wenruo } 4122319e4d06SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) 4123ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "unrecognized super flag: %llu", 4124319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 412521e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 4126ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 412721e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 4128c926093eSDavid Sterba ret = -EINVAL; 4129c926093eSDavid Sterba } 413021e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 4131ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 413221e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 4133c926093eSDavid Sterba ret = -EINVAL; 4134c926093eSDavid Sterba } 413521e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 4136ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 413721e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 4138c926093eSDavid Sterba ret = -EINVAL; 4139c926093eSDavid Sterba } 4140c926093eSDavid Sterba 41411104a885SDavid Sterba /* 4142319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 4143319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 41441104a885SDavid Sterba */ 4145319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 4146319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4147ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 414875d6ad38SDavid Sterba ret = -EINVAL; 414975d6ad38SDavid Sterba } 4150319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 415109cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 4152ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4153ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 415409cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 4155319e4d06SQu Wenruo ret = -EINVAL; 4156319e4d06SQu Wenruo } 4157319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 4158319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4159ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 4160319e4d06SQu Wenruo ret = -EINVAL; 4161319e4d06SQu Wenruo } 4162319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 4163ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 4164ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 4165319e4d06SQu Wenruo ret = -EINVAL; 4166319e4d06SQu Wenruo } 4167319e4d06SQu Wenruo 4168319e4d06SQu Wenruo /* Root alignment check */ 4169319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 4170ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 4171319e4d06SQu Wenruo btrfs_super_root(sb)); 4172319e4d06SQu Wenruo ret = -EINVAL; 4173319e4d06SQu Wenruo } 4174319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 4175ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 4176319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 4177319e4d06SQu Wenruo ret = -EINVAL; 4178319e4d06SQu Wenruo } 4179319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 4180ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 4181319e4d06SQu Wenruo btrfs_super_log_root(sb)); 418275d6ad38SDavid Sterba ret = -EINVAL; 418375d6ad38SDavid Sterba } 418475d6ad38SDavid Sterba 4185c926093eSDavid Sterba if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) { 4186ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4187ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 4188c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 4189c926093eSDavid Sterba ret = -EINVAL; 4190c926093eSDavid Sterba } 4191c926093eSDavid Sterba 4192c926093eSDavid Sterba /* 4193c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 4194c926093eSDavid Sterba * done later 4195c926093eSDavid Sterba */ 419699e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 419799e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 419899e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 419999e3ecfcSLiu Bo ret = -EINVAL; 420099e3ecfcSLiu Bo } 4201b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 420299e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 420399e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 420499e3ecfcSLiu Bo ret = -EINVAL; 420599e3ecfcSLiu Bo } 420621e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 4207ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 420821e7626bSDavid Sterba btrfs_super_num_devices(sb)); 420975d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 4210ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 421175d6ad38SDavid Sterba ret = -EINVAL; 421275d6ad38SDavid Sterba } 4213c926093eSDavid Sterba 421421e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4215ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 421621e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4217c926093eSDavid Sterba ret = -EINVAL; 4218c926093eSDavid Sterba } 4219c926093eSDavid Sterba 4220c926093eSDavid Sterba /* 4221ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4222ce7fca5fSDavid Sterba * and one chunk 4223ce7fca5fSDavid Sterba */ 4224ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4225ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4226ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4227ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4228ce7fca5fSDavid Sterba ret = -EINVAL; 4229ce7fca5fSDavid Sterba } 4230ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4231ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4232ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4233ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4234ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4235ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4236ce7fca5fSDavid Sterba ret = -EINVAL; 4237ce7fca5fSDavid Sterba } 4238ce7fca5fSDavid Sterba 4239ce7fca5fSDavid Sterba /* 4240c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4241c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4242c926093eSDavid Sterba */ 424321e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4244ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4245ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4246ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4247ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 424821e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 424921e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4250ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4251ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4252ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4253ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4254c926093eSDavid Sterba 4255c926093eSDavid Sterba return ret; 4256fcd1f065SDavid Sterba } 4257fcd1f065SDavid Sterba 42582ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4259acce952bSliubo { 42600b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42612ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42620b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 42630b246afaSJeff Mahoney 42640b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42650b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4266acce952bSliubo 4267acce952bSliubo /* cleanup FS via transaction */ 42682ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4269acce952bSliubo } 4270acce952bSliubo 4271143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4272acce952bSliubo { 4273acce952bSliubo struct btrfs_ordered_extent *ordered; 4274acce952bSliubo 4275199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4276779880efSJosef Bacik /* 4277779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4278779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4279779880efSJosef Bacik */ 4280199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4281779880efSJosef Bacik root_extent_list) 4282779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4283199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4284199c2a9cSMiao Xie } 4285199c2a9cSMiao Xie 4286199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4287199c2a9cSMiao Xie { 4288199c2a9cSMiao Xie struct btrfs_root *root; 4289199c2a9cSMiao Xie struct list_head splice; 4290199c2a9cSMiao Xie 4291199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4292199c2a9cSMiao Xie 4293199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4294199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4295199c2a9cSMiao Xie while (!list_empty(&splice)) { 4296199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4297199c2a9cSMiao Xie ordered_root); 42981de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 42991de2cfdeSJosef Bacik &fs_info->ordered_roots); 4300199c2a9cSMiao Xie 43012a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4302199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4303199c2a9cSMiao Xie 43042a85d9caSLiu Bo cond_resched(); 43052a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4306199c2a9cSMiao Xie } 4307199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4308acce952bSliubo } 4309acce952bSliubo 431035a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43112ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4312acce952bSliubo { 4313acce952bSliubo struct rb_node *node; 4314acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4315acce952bSliubo struct btrfs_delayed_ref_node *ref; 4316acce952bSliubo int ret = 0; 4317acce952bSliubo 4318acce952bSliubo delayed_refs = &trans->delayed_refs; 4319acce952bSliubo 4320acce952bSliubo spin_lock(&delayed_refs->lock); 4321d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4322cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 43230b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4324acce952bSliubo return ret; 4325acce952bSliubo } 4326acce952bSliubo 4327d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4328d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 4329c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *tmp; 4330e78417d1SJosef Bacik bool pin_bytes = false; 4331acce952bSliubo 4332d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4333d7df2c79SJosef Bacik href_node); 4334b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 4335d7df2c79SJosef Bacik atomic_inc(&head->node.refs); 433679787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4337b939d1abSJosef Bacik 4338acce952bSliubo mutex_lock(&head->mutex); 4339b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4340d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4341e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4342b939d1abSJosef Bacik continue; 4343b939d1abSJosef Bacik } 4344d7df2c79SJosef Bacik spin_lock(&head->lock); 4345c6fc2454SQu Wenruo list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list, 4346c6fc2454SQu Wenruo list) { 4347d7df2c79SJosef Bacik ref->in_tree = 0; 4348c6fc2454SQu Wenruo list_del(&ref->list); 43491d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43501d57ee94SWang Xiaoguang list_del(&ref->add_list); 4351d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4352d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4353d7df2c79SJosef Bacik } 435454067ae9SJosef Bacik if (head->must_insert_reserved) 4355e78417d1SJosef Bacik pin_bytes = true; 435678a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4357acce952bSliubo delayed_refs->num_heads--; 4358d7df2c79SJosef Bacik if (head->processing == 0) 4359acce952bSliubo delayed_refs->num_heads_ready--; 4360d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4361d7df2c79SJosef Bacik head->node.in_tree = 0; 4362c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4363d7df2c79SJosef Bacik spin_unlock(&head->lock); 4364acce952bSliubo spin_unlock(&delayed_refs->lock); 4365e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4366acce952bSliubo 4367d7df2c79SJosef Bacik if (pin_bytes) 43682ff7e61eSJeff Mahoney btrfs_pin_extent(fs_info, head->node.bytenr, 4369d7df2c79SJosef Bacik head->node.num_bytes, 1); 4370d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4371acce952bSliubo cond_resched(); 4372acce952bSliubo spin_lock(&delayed_refs->lock); 4373acce952bSliubo } 4374acce952bSliubo 4375acce952bSliubo spin_unlock(&delayed_refs->lock); 4376acce952bSliubo 4377acce952bSliubo return ret; 4378acce952bSliubo } 4379acce952bSliubo 4380143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4381acce952bSliubo { 4382acce952bSliubo struct btrfs_inode *btrfs_inode; 4383acce952bSliubo struct list_head splice; 4384acce952bSliubo 4385acce952bSliubo INIT_LIST_HEAD(&splice); 4386acce952bSliubo 4387eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4388eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4389acce952bSliubo 4390acce952bSliubo while (!list_empty(&splice)) { 4391eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4392acce952bSliubo delalloc_inodes); 4393acce952bSliubo 4394acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4395df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4396df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4397eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4398acce952bSliubo 4399acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4400b216cbfbSMiao Xie 4401eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4402acce952bSliubo } 4403acce952bSliubo 4404eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4405eb73c1b7SMiao Xie } 4406eb73c1b7SMiao Xie 4407eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4408eb73c1b7SMiao Xie { 4409eb73c1b7SMiao Xie struct btrfs_root *root; 4410eb73c1b7SMiao Xie struct list_head splice; 4411eb73c1b7SMiao Xie 4412eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4413eb73c1b7SMiao Xie 4414eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4415eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4416eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4417eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4418eb73c1b7SMiao Xie delalloc_root); 4419eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4420eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4421eb73c1b7SMiao Xie BUG_ON(!root); 4422eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4423eb73c1b7SMiao Xie 4424eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4425eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4426eb73c1b7SMiao Xie 4427eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4428eb73c1b7SMiao Xie } 4429eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4430acce952bSliubo } 4431acce952bSliubo 44322ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4433acce952bSliubo struct extent_io_tree *dirty_pages, 4434acce952bSliubo int mark) 4435acce952bSliubo { 4436acce952bSliubo int ret; 4437acce952bSliubo struct extent_buffer *eb; 4438acce952bSliubo u64 start = 0; 4439acce952bSliubo u64 end; 4440acce952bSliubo 4441acce952bSliubo while (1) { 4442acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4443e6138876SJosef Bacik mark, NULL); 4444acce952bSliubo if (ret) 4445acce952bSliubo break; 4446acce952bSliubo 444791166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4448acce952bSliubo while (start <= end) { 44490b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44500b246afaSJeff Mahoney start += fs_info->nodesize; 4451fd8b2b61SJosef Bacik if (!eb) 4452acce952bSliubo continue; 4453fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4454acce952bSliubo 4455fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4456fd8b2b61SJosef Bacik &eb->bflags)) 4457fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4458fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4459acce952bSliubo } 4460acce952bSliubo } 4461acce952bSliubo 4462acce952bSliubo return ret; 4463acce952bSliubo } 4464acce952bSliubo 44652ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4466acce952bSliubo struct extent_io_tree *pinned_extents) 4467acce952bSliubo { 4468acce952bSliubo struct extent_io_tree *unpin; 4469acce952bSliubo u64 start; 4470acce952bSliubo u64 end; 4471acce952bSliubo int ret; 4472ed0eaa14SLiu Bo bool loop = true; 4473acce952bSliubo 4474acce952bSliubo unpin = pinned_extents; 4475ed0eaa14SLiu Bo again: 4476acce952bSliubo while (1) { 4477acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4478e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4479acce952bSliubo if (ret) 4480acce952bSliubo break; 4481acce952bSliubo 4482af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 44832ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4484acce952bSliubo cond_resched(); 4485acce952bSliubo } 4486acce952bSliubo 4487ed0eaa14SLiu Bo if (loop) { 44880b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44890b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4490ed0eaa14SLiu Bo else 44910b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4492ed0eaa14SLiu Bo loop = false; 4493ed0eaa14SLiu Bo goto again; 4494ed0eaa14SLiu Bo } 4495ed0eaa14SLiu Bo 4496acce952bSliubo return 0; 4497acce952bSliubo } 4498acce952bSliubo 4499c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4500c79a1751SLiu Bo { 4501c79a1751SLiu Bo struct inode *inode; 4502c79a1751SLiu Bo 4503c79a1751SLiu Bo inode = cache->io_ctl.inode; 4504c79a1751SLiu Bo if (inode) { 4505c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4506c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4507c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4508c79a1751SLiu Bo iput(inode); 4509c79a1751SLiu Bo } 4510c79a1751SLiu Bo btrfs_put_block_group(cache); 4511c79a1751SLiu Bo } 4512c79a1751SLiu Bo 4513c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45142ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4515c79a1751SLiu Bo { 4516c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4517c79a1751SLiu Bo 4518c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4519c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4520c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4521c79a1751SLiu Bo struct btrfs_block_group_cache, 4522c79a1751SLiu Bo dirty_list); 4523c79a1751SLiu Bo if (!cache) { 45240b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group dirty_bgs list"); 4525c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4526c79a1751SLiu Bo return; 4527c79a1751SLiu Bo } 4528c79a1751SLiu Bo 4529c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4530c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4531c79a1751SLiu Bo list_del_init(&cache->io_list); 4532c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4533c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4534c79a1751SLiu Bo } 4535c79a1751SLiu Bo 4536c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4537c79a1751SLiu Bo spin_lock(&cache->lock); 4538c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4539c79a1751SLiu Bo spin_unlock(&cache->lock); 4540c79a1751SLiu Bo 4541c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4542c79a1751SLiu Bo btrfs_put_block_group(cache); 4543c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4544c79a1751SLiu Bo } 4545c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4546c79a1751SLiu Bo 4547c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4548c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4549c79a1751SLiu Bo struct btrfs_block_group_cache, 4550c79a1751SLiu Bo io_list); 4551c79a1751SLiu Bo if (!cache) { 45520b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group on io_bgs list"); 4553c79a1751SLiu Bo return; 4554c79a1751SLiu Bo } 4555c79a1751SLiu Bo 4556c79a1751SLiu Bo list_del_init(&cache->io_list); 4557c79a1751SLiu Bo spin_lock(&cache->lock); 4558c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4559c79a1751SLiu Bo spin_unlock(&cache->lock); 4560c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4561c79a1751SLiu Bo } 4562c79a1751SLiu Bo } 4563c79a1751SLiu Bo 456449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45652ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 456649b25e05SJeff Mahoney { 45672ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4568c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4569c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4570c79a1751SLiu Bo 45712ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 457249b25e05SJeff Mahoney 45734a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45740b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 457549b25e05SJeff Mahoney 45764a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45770b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 457849b25e05SJeff Mahoney 4579ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4580ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 458167cde344SMiao Xie 45822ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 458349b25e05SJeff Mahoney EXTENT_DIRTY); 45842ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45850b246afaSJeff Mahoney fs_info->pinned_extents); 458649b25e05SJeff Mahoney 45874a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45884a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 45894a9d8bdeSMiao Xie 459049b25e05SJeff Mahoney /* 459149b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 459249b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 459349b25e05SJeff Mahoney */ 459449b25e05SJeff Mahoney } 459549b25e05SJeff Mahoney 45962ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4597acce952bSliubo { 4598acce952bSliubo struct btrfs_transaction *t; 4599acce952bSliubo 46000b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4601acce952bSliubo 46020b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46030b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46040b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4605724e2315SJosef Bacik struct btrfs_transaction, list); 4606724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 4607724e2315SJosef Bacik atomic_inc(&t->use_count); 46080b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46092ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4610724e2315SJosef Bacik btrfs_put_transaction(t); 46110b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4612724e2315SJosef Bacik continue; 4613724e2315SJosef Bacik } 46140b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4615724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46160b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4617724e2315SJosef Bacik /* 4618724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4619724e2315SJosef Bacik * handle open currently for this transaction. 4620724e2315SJosef Bacik */ 4621724e2315SJosef Bacik wait_event(t->writer_wait, 4622724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4623724e2315SJosef Bacik } else { 46240b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4625724e2315SJosef Bacik } 46262ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4627724e2315SJosef Bacik 46280b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46290b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46300b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4631724e2315SJosef Bacik list_del_init(&t->list); 46320b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4633a4abeea4SJosef Bacik 4634724e2315SJosef Bacik btrfs_put_transaction(t); 46352ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46360b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4637724e2315SJosef Bacik } 46380b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46390b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4640ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4641ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46422ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 46430b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46440b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4645acce952bSliubo 4646acce952bSliubo return 0; 4647acce952bSliubo } 4648acce952bSliubo 4649e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 4650ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 46514bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 46520b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4653239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4654239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 46550da5468fSChris Mason }; 4656