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 563f177d739SFilipe Manana /* 564f177d739SFilipe Manana * Extent buffers from a relocation tree have a owner field that 565f177d739SFilipe Manana * corresponds to the subvolume tree they are based on. So just from an 566f177d739SFilipe Manana * extent buffer alone we can not find out what is the id of the 567f177d739SFilipe Manana * corresponding subvolume tree, so we can not figure out if the extent 568f177d739SFilipe Manana * buffer corresponds to the root of the relocation tree or not. So skip 569f177d739SFilipe Manana * this check for relocation trees. 570f177d739SFilipe Manana */ 571f177d739SFilipe Manana if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 5721ba98d08SLiu Bo struct btrfs_root *check_root; 5731ba98d08SLiu Bo 5741ba98d08SLiu Bo key.objectid = btrfs_header_owner(leaf); 5751ba98d08SLiu Bo key.type = BTRFS_ROOT_ITEM_KEY; 5761ba98d08SLiu Bo key.offset = (u64)-1; 5771ba98d08SLiu Bo 5780b246afaSJeff Mahoney check_root = btrfs_get_fs_root(fs_info, &key, false); 5791ba98d08SLiu Bo /* 5801ba98d08SLiu Bo * The only reason we also check NULL here is that during 5811ba98d08SLiu Bo * open_ctree() some roots has not yet been set up. 5821ba98d08SLiu Bo */ 5831ba98d08SLiu Bo if (!IS_ERR_OR_NULL(check_root)) { 584ef85b25eSLiu Bo struct extent_buffer *eb; 585ef85b25eSLiu Bo 586ef85b25eSLiu Bo eb = btrfs_root_node(check_root); 5871ba98d08SLiu Bo /* if leaf is the root, then it's fine */ 588ef85b25eSLiu Bo if (leaf != eb) { 5891ba98d08SLiu Bo CORRUPT("non-root leaf's nritems is 0", 590ef85b25eSLiu Bo leaf, check_root, 0); 591ef85b25eSLiu Bo free_extent_buffer(eb); 5921ba98d08SLiu Bo return -EIO; 5931ba98d08SLiu Bo } 594ef85b25eSLiu Bo free_extent_buffer(eb); 5951ba98d08SLiu Bo } 596a826d6dcSJosef Bacik return 0; 5971ba98d08SLiu Bo } 598a826d6dcSJosef Bacik 599f177d739SFilipe Manana if (nritems == 0) 600f177d739SFilipe Manana return 0; 601f177d739SFilipe Manana 602a826d6dcSJosef Bacik /* Check the 0 item */ 603a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 6040b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 605a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 606a826d6dcSJosef Bacik return -EIO; 607a826d6dcSJosef Bacik } 608a826d6dcSJosef Bacik 609a826d6dcSJosef Bacik /* 610a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 611a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 612a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 613a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 614a826d6dcSJosef Bacik * offset is correct. 615a826d6dcSJosef Bacik */ 616a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 617a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 618a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 619a826d6dcSJosef Bacik 620a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 621a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 622a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 623a826d6dcSJosef Bacik return -EIO; 624a826d6dcSJosef Bacik } 625a826d6dcSJosef Bacik 626a826d6dcSJosef Bacik /* 627a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 628a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 629a826d6dcSJosef Bacik * front. 630a826d6dcSJosef Bacik */ 631a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 632a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 633a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 634a826d6dcSJosef Bacik return -EIO; 635a826d6dcSJosef Bacik } 636a826d6dcSJosef Bacik 637a826d6dcSJosef Bacik /* 638a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 639a826d6dcSJosef Bacik * just in case all the items are consistent to each other, but 640a826d6dcSJosef Bacik * all point outside of the leaf. 641a826d6dcSJosef Bacik */ 642a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 6430b246afaSJeff Mahoney BTRFS_LEAF_DATA_SIZE(fs_info)) { 644a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 645a826d6dcSJosef Bacik return -EIO; 646a826d6dcSJosef Bacik } 647a826d6dcSJosef Bacik } 648a826d6dcSJosef Bacik 649a826d6dcSJosef Bacik return 0; 650a826d6dcSJosef Bacik } 651a826d6dcSJosef Bacik 652053ab70fSLiu Bo static int check_node(struct btrfs_root *root, struct extent_buffer *node) 653053ab70fSLiu Bo { 654053ab70fSLiu Bo unsigned long nr = btrfs_header_nritems(node); 6556b722c17SLiu Bo struct btrfs_key key, next_key; 6566b722c17SLiu Bo int slot; 6576b722c17SLiu Bo u64 bytenr; 6586b722c17SLiu Bo int ret = 0; 659053ab70fSLiu Bo 660da17066cSJeff Mahoney if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) { 661053ab70fSLiu Bo btrfs_crit(root->fs_info, 662053ab70fSLiu Bo "corrupt node: block %llu root %llu nritems %lu", 663053ab70fSLiu Bo node->start, root->objectid, nr); 664053ab70fSLiu Bo return -EIO; 665053ab70fSLiu Bo } 6666b722c17SLiu Bo 6676b722c17SLiu Bo for (slot = 0; slot < nr - 1; slot++) { 6686b722c17SLiu Bo bytenr = btrfs_node_blockptr(node, slot); 6696b722c17SLiu Bo btrfs_node_key_to_cpu(node, &key, slot); 6706b722c17SLiu Bo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 6716b722c17SLiu Bo 6726b722c17SLiu Bo if (!bytenr) { 6736b722c17SLiu Bo CORRUPT("invalid item slot", node, root, slot); 6746b722c17SLiu Bo ret = -EIO; 6756b722c17SLiu Bo goto out; 6766b722c17SLiu Bo } 6776b722c17SLiu Bo 6786b722c17SLiu Bo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 6796b722c17SLiu Bo CORRUPT("bad key order", node, root, slot); 6806b722c17SLiu Bo ret = -EIO; 6816b722c17SLiu Bo goto out; 6826b722c17SLiu Bo } 6836b722c17SLiu Bo } 6846b722c17SLiu Bo out: 6856b722c17SLiu Bo return ret; 686053ab70fSLiu Bo } 687053ab70fSLiu Bo 688facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 689facc8a22SMiao Xie u64 phy_offset, struct page *page, 690facc8a22SMiao Xie u64 start, u64 end, int mirror) 691ce9adaa5SChris Mason { 692ce9adaa5SChris Mason u64 found_start; 693ce9adaa5SChris Mason int found_level; 694ce9adaa5SChris Mason struct extent_buffer *eb; 695ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 69602873e43SZhao Lei struct btrfs_fs_info *fs_info = root->fs_info; 697f188591eSChris Mason int ret = 0; 698727011e0SChris Mason int reads_done; 699ce9adaa5SChris Mason 700ce9adaa5SChris Mason if (!page->private) 701ce9adaa5SChris Mason goto out; 702d397712bSChris Mason 7034f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 704d397712bSChris Mason 7050b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 7060b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 7070b32f4bbSJosef Bacik */ 7080b32f4bbSJosef Bacik extent_buffer_get(eb); 7090b32f4bbSJosef Bacik 7100b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 711727011e0SChris Mason if (!reads_done) 712727011e0SChris Mason goto err; 713f188591eSChris Mason 7145cf1ab56SJosef Bacik eb->read_mirror = mirror; 715656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 716ea466794SJosef Bacik ret = -EIO; 717ea466794SJosef Bacik goto err; 718ea466794SJosef Bacik } 719ea466794SJosef Bacik 720ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 721727011e0SChris Mason if (found_start != eb->start) { 72202873e43SZhao Lei btrfs_err_rl(fs_info, "bad tree block start %llu %llu", 72394647322SDavid Sterba found_start, eb->start); 724f188591eSChris Mason ret = -EIO; 725ce9adaa5SChris Mason goto err; 726ce9adaa5SChris Mason } 72702873e43SZhao Lei if (check_tree_block_fsid(fs_info, eb)) { 72802873e43SZhao Lei btrfs_err_rl(fs_info, "bad fsid on block %llu", 72994647322SDavid Sterba eb->start); 7301259ab75SChris Mason ret = -EIO; 7311259ab75SChris Mason goto err; 7321259ab75SChris Mason } 733ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 7341c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 73502873e43SZhao Lei btrfs_err(fs_info, "bad tree block level %d", 7361c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 7371c24c3ceSJosef Bacik ret = -EIO; 7381c24c3ceSJosef Bacik goto err; 7391c24c3ceSJosef Bacik } 740ce9adaa5SChris Mason 74185d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 74285d4e461SChris Mason eb, found_level); 7434008c04aSChris Mason 74402873e43SZhao Lei ret = csum_tree_block(fs_info, eb, 1); 7458bd98f0eSAlex Lyakas if (ret) 746a826d6dcSJosef Bacik goto err; 747a826d6dcSJosef Bacik 748a826d6dcSJosef Bacik /* 749a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 750a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 751a826d6dcSJosef Bacik * return -EIO. 752a826d6dcSJosef Bacik */ 753a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 754a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 755a826d6dcSJosef Bacik ret = -EIO; 756a826d6dcSJosef Bacik } 757ce9adaa5SChris Mason 758053ab70fSLiu Bo if (found_level > 0 && check_node(root, eb)) 759053ab70fSLiu Bo ret = -EIO; 760053ab70fSLiu Bo 7610b32f4bbSJosef Bacik if (!ret) 7620b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 763ce9adaa5SChris Mason err: 76479fb65a1SJosef Bacik if (reads_done && 76579fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 766fc2e901fSDavid Sterba btree_readahead_hook(fs_info, eb, ret); 7674bb31e92SArne Jansen 76853b381b3SDavid Woodhouse if (ret) { 76953b381b3SDavid Woodhouse /* 77053b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 77153b381b3SDavid Woodhouse * again, we have to make sure it has something 77253b381b3SDavid Woodhouse * to decrement 77353b381b3SDavid Woodhouse */ 77453b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 7750b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 77653b381b3SDavid Woodhouse } 7770b32f4bbSJosef Bacik free_extent_buffer(eb); 778ce9adaa5SChris Mason out: 779f188591eSChris Mason return ret; 780ce9adaa5SChris Mason } 781ce9adaa5SChris Mason 782ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 7834bb31e92SArne Jansen { 7844bb31e92SArne Jansen struct extent_buffer *eb; 7854bb31e92SArne Jansen 7864f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 787656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 7885cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 78953b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 790ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 791fc2e901fSDavid Sterba btree_readahead_hook(eb->fs_info, eb, -EIO); 7924bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 7934bb31e92SArne Jansen } 7944bb31e92SArne Jansen 7954246a0b6SChristoph Hellwig static void end_workqueue_bio(struct bio *bio) 796ce9adaa5SChris Mason { 79797eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 798ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7999e0af237SLiu Bo struct btrfs_workqueue *wq; 8009e0af237SLiu Bo btrfs_work_func_t func; 801ce9adaa5SChris Mason 802ce9adaa5SChris Mason fs_info = end_io_wq->info; 8034246a0b6SChristoph Hellwig end_io_wq->error = bio->bi_error; 804d20f7043SChris Mason 80537226b21SMike Christie if (bio_op(bio) == REQ_OP_WRITE) { 8069e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 8079e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 8089e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 8099e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 8109e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 8119e0af237SLiu Bo func = btrfs_freespace_write_helper; 8129e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 8139e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 8149e0af237SLiu Bo func = btrfs_endio_raid56_helper; 815d20f7043SChris Mason } else { 8169e0af237SLiu Bo wq = fs_info->endio_write_workers; 8179e0af237SLiu Bo func = btrfs_endio_write_helper; 818d20f7043SChris Mason } 8199e0af237SLiu Bo } else { 8208b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 8218b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 8228b110e39SMiao Xie wq = fs_info->endio_repair_workers; 8238b110e39SMiao Xie func = btrfs_endio_repair_helper; 8248b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 8259e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 8269e0af237SLiu Bo func = btrfs_endio_raid56_helper; 8279e0af237SLiu Bo } else if (end_io_wq->metadata) { 8289e0af237SLiu Bo wq = fs_info->endio_meta_workers; 8299e0af237SLiu Bo func = btrfs_endio_meta_helper; 8309e0af237SLiu Bo } else { 8319e0af237SLiu Bo wq = fs_info->endio_workers; 8329e0af237SLiu Bo func = btrfs_endio_helper; 8339e0af237SLiu Bo } 8349e0af237SLiu Bo } 8359e0af237SLiu Bo 8369e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 8379e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 838ce9adaa5SChris Mason } 839ce9adaa5SChris Mason 84022c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 841bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 8420b86a832SChris Mason { 84397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 8448b110e39SMiao Xie 84597eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 846ce9adaa5SChris Mason if (!end_io_wq) 847ce9adaa5SChris Mason return -ENOMEM; 848ce9adaa5SChris Mason 849ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 850ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 85122c59948SChris Mason end_io_wq->info = info; 852ce9adaa5SChris Mason end_io_wq->error = 0; 853ce9adaa5SChris Mason end_io_wq->bio = bio; 85422c59948SChris Mason end_io_wq->metadata = metadata; 855ce9adaa5SChris Mason 856ce9adaa5SChris Mason bio->bi_private = end_io_wq; 857ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 85822c59948SChris Mason return 0; 85922c59948SChris Mason } 86022c59948SChris Mason 861b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 8624854ddd0SChris Mason { 8634854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 8645cdc7ad3SQu Wenruo info->thread_pool_size, 8654854ddd0SChris Mason info->fs_devices->open_devices); 8664854ddd0SChris Mason return 256 * limit; 8674854ddd0SChris Mason } 8684854ddd0SChris Mason 8694a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 8704a69a410SChris Mason { 8714a69a410SChris Mason struct async_submit_bio *async; 87279787eaaSJeff Mahoney int ret; 8734a69a410SChris Mason 8744a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 87581a75f67SMike Christie ret = async->submit_bio_start(async->inode, async->bio, 876eaf25d93SChris Mason async->mirror_num, async->bio_flags, 877eaf25d93SChris Mason async->bio_offset); 87879787eaaSJeff Mahoney if (ret) 87979787eaaSJeff Mahoney async->error = ret; 8804a69a410SChris Mason } 8814a69a410SChris Mason 8824a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 8838b712842SChris Mason { 8848b712842SChris Mason struct btrfs_fs_info *fs_info; 8858b712842SChris Mason struct async_submit_bio *async; 8864854ddd0SChris Mason int limit; 8878b712842SChris Mason 8888b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8898b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 8904854ddd0SChris Mason 891b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 8924854ddd0SChris Mason limit = limit * 2 / 3; 8934854ddd0SChris Mason 894ee863954SDavid Sterba /* 895ee863954SDavid Sterba * atomic_dec_return implies a barrier for waitqueue_active 896ee863954SDavid Sterba */ 89766657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 898b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 8994854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 9004854ddd0SChris Mason 901bb7ab3b9SAdam Buchbinder /* If an error occurred we just want to clean up the bio and move on */ 90279787eaaSJeff Mahoney if (async->error) { 9034246a0b6SChristoph Hellwig async->bio->bi_error = async->error; 9044246a0b6SChristoph Hellwig bio_endio(async->bio); 90579787eaaSJeff Mahoney return; 90679787eaaSJeff Mahoney } 90779787eaaSJeff Mahoney 90881a75f67SMike Christie async->submit_bio_done(async->inode, async->bio, async->mirror_num, 90981a75f67SMike Christie async->bio_flags, async->bio_offset); 9104a69a410SChris Mason } 9114a69a410SChris Mason 9124a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 9134a69a410SChris Mason { 9144a69a410SChris Mason struct async_submit_bio *async; 9154a69a410SChris Mason 9164a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 9178b712842SChris Mason kfree(async); 9188b712842SChris Mason } 9198b712842SChris Mason 92044b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 92181a75f67SMike Christie struct bio *bio, int mirror_num, 922c8b97818SChris Mason unsigned long bio_flags, 923eaf25d93SChris Mason u64 bio_offset, 9244a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 9254a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 92644b8bd7eSChris Mason { 92744b8bd7eSChris Mason struct async_submit_bio *async; 92844b8bd7eSChris Mason 92944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 93044b8bd7eSChris Mason if (!async) 93144b8bd7eSChris Mason return -ENOMEM; 93244b8bd7eSChris Mason 93344b8bd7eSChris Mason async->inode = inode; 93444b8bd7eSChris Mason async->bio = bio; 93544b8bd7eSChris Mason async->mirror_num = mirror_num; 9364a69a410SChris Mason async->submit_bio_start = submit_bio_start; 9374a69a410SChris Mason async->submit_bio_done = submit_bio_done; 9384a69a410SChris Mason 9399e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 9405cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 9414a69a410SChris Mason 942c8b97818SChris Mason async->bio_flags = bio_flags; 943eaf25d93SChris Mason async->bio_offset = bio_offset; 9448c8bee1dSChris Mason 94579787eaaSJeff Mahoney async->error = 0; 94679787eaaSJeff Mahoney 947cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 948d313d7a3SChris Mason 94967f055c7SChristoph Hellwig if (op_is_sync(bio->bi_opf)) 9505cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 951d313d7a3SChris Mason 9525cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 9539473f16cSChris Mason 954771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 955771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 956771ed689SChris Mason wait_event(fs_info->async_submit_wait, 957771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 958771ed689SChris Mason } 959771ed689SChris Mason 96044b8bd7eSChris Mason return 0; 96144b8bd7eSChris Mason } 96244b8bd7eSChris Mason 963ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 964ce3ed71aSChris Mason { 9652c30c71bSKent Overstreet struct bio_vec *bvec; 966ce3ed71aSChris Mason struct btrfs_root *root; 9672c30c71bSKent Overstreet int i, ret = 0; 968ce3ed71aSChris Mason 9692c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 970ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 97101d58472SDaniel Dressler ret = csum_dirty_buffer(root->fs_info, bvec->bv_page); 97279787eaaSJeff Mahoney if (ret) 97379787eaaSJeff Mahoney break; 974ce3ed71aSChris Mason } 9752c30c71bSKent Overstreet 97679787eaaSJeff Mahoney return ret; 977ce3ed71aSChris Mason } 978ce3ed71aSChris Mason 97981a75f67SMike Christie static int __btree_submit_bio_start(struct inode *inode, struct bio *bio, 98081a75f67SMike Christie int mirror_num, unsigned long bio_flags, 981eaf25d93SChris Mason u64 bio_offset) 98222c59948SChris Mason { 9838b712842SChris Mason /* 9848b712842SChris Mason * when we're called for a write, we're already in the async 9855443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9868b712842SChris Mason */ 98779787eaaSJeff Mahoney return btree_csum_one_bio(bio); 98822c59948SChris Mason } 98922c59948SChris Mason 99081a75f67SMike Christie static int __btree_submit_bio_done(struct inode *inode, struct bio *bio, 991eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 992eaf25d93SChris Mason u64 bio_offset) 9934a69a410SChris Mason { 99461891923SStefan Behrens int ret; 99561891923SStefan Behrens 9968b712842SChris Mason /* 9974a69a410SChris Mason * when we're called for a write, we're already in the async 9984a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 9998b712842SChris Mason */ 10002ff7e61eSJeff Mahoney ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1); 10014246a0b6SChristoph Hellwig if (ret) { 10024246a0b6SChristoph Hellwig bio->bi_error = ret; 10034246a0b6SChristoph Hellwig bio_endio(bio); 10044246a0b6SChristoph Hellwig } 100561891923SStefan Behrens return ret; 10060b86a832SChris Mason } 10070b86a832SChris Mason 1008e27f6265SDavid Sterba static int check_async_write(unsigned long bio_flags) 1009de0022b9SJosef Bacik { 1010de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 1011de0022b9SJosef Bacik return 0; 1012de0022b9SJosef Bacik #ifdef CONFIG_X86 1013bc696ca0SBorislav Petkov if (static_cpu_has(X86_FEATURE_XMM4_2)) 1014de0022b9SJosef Bacik return 0; 1015de0022b9SJosef Bacik #endif 1016de0022b9SJosef Bacik return 1; 1017de0022b9SJosef Bacik } 1018de0022b9SJosef Bacik 101981a75f67SMike Christie static int btree_submit_bio_hook(struct inode *inode, struct bio *bio, 1020eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 1021eaf25d93SChris Mason u64 bio_offset) 102244b8bd7eSChris Mason { 10230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1024e27f6265SDavid Sterba int async = check_async_write(bio_flags); 10254a69a410SChris Mason int ret; 1026cad321adSChris Mason 102737226b21SMike Christie if (bio_op(bio) != REQ_OP_WRITE) { 1028cad321adSChris Mason /* 1029cad321adSChris Mason * called for a read, do the setup so that checksum validation 1030cad321adSChris Mason * can happen in the async kernel threads 1031cad321adSChris Mason */ 10320b246afaSJeff Mahoney ret = btrfs_bio_wq_end_io(fs_info, bio, 10330b246afaSJeff Mahoney BTRFS_WQ_ENDIO_METADATA); 10341d4284bdSChris Mason if (ret) 103561891923SStefan Behrens goto out_w_error; 10362ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 1037de0022b9SJosef Bacik } else if (!async) { 1038de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 1039de0022b9SJosef Bacik if (ret) 104061891923SStefan Behrens goto out_w_error; 10412ff7e61eSJeff Mahoney ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); 104261891923SStefan Behrens } else { 1043cad321adSChris Mason /* 104461891923SStefan Behrens * kthread helpers are used to submit writes so that 104561891923SStefan Behrens * checksumming can happen in parallel across all CPUs 1046cad321adSChris Mason */ 10470b246afaSJeff Mahoney ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, 0, 1048eaf25d93SChris Mason bio_offset, 10494a69a410SChris Mason __btree_submit_bio_start, 10504a69a410SChris Mason __btree_submit_bio_done); 105144b8bd7eSChris Mason } 105244b8bd7eSChris Mason 10534246a0b6SChristoph Hellwig if (ret) 10544246a0b6SChristoph Hellwig goto out_w_error; 10554246a0b6SChristoph Hellwig return 0; 10564246a0b6SChristoph Hellwig 105761891923SStefan Behrens out_w_error: 10584246a0b6SChristoph Hellwig bio->bi_error = ret; 10594246a0b6SChristoph Hellwig bio_endio(bio); 106061891923SStefan Behrens return ret; 106161891923SStefan Behrens } 106261891923SStefan Behrens 10633dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 1064784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 1065a6bc32b8SMel Gorman struct page *newpage, struct page *page, 1066a6bc32b8SMel Gorman enum migrate_mode mode) 1067784b4e29SChris Mason { 1068784b4e29SChris Mason /* 1069784b4e29SChris Mason * we can't safely write a btree page from here, 1070784b4e29SChris Mason * we haven't done the locking hook 1071784b4e29SChris Mason */ 1072784b4e29SChris Mason if (PageDirty(page)) 1073784b4e29SChris Mason return -EAGAIN; 1074784b4e29SChris Mason /* 1075784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 1076784b4e29SChris Mason * We must have no buffers or drop them. 1077784b4e29SChris Mason */ 1078784b4e29SChris Mason if (page_has_private(page) && 1079784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 1080784b4e29SChris Mason return -EAGAIN; 1081a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1082784b4e29SChris Mason } 10833dd1462eSJan Beulich #endif 1084784b4e29SChris Mason 10850da5468fSChris Mason 10860da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 10870da5468fSChris Mason struct writeback_control *wbc) 10880da5468fSChris Mason { 1089e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 1090e2d84521SMiao Xie int ret; 1091e2d84521SMiao Xie 1092d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 1093448d640bSChris Mason 1094448d640bSChris Mason if (wbc->for_kupdate) 1095448d640bSChris Mason return 0; 1096448d640bSChris Mason 1097e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 1098b9473439SChris Mason /* this is a bit racy, but that's ok */ 1099e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1100e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 1101e2d84521SMiao Xie if (ret < 0) 1102793955bcSChris Mason return 0; 1103793955bcSChris Mason } 11040b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 11050da5468fSChris Mason } 11060da5468fSChris Mason 1107b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 11085f39d397SChris Mason { 1109d1310b2eSChris Mason struct extent_io_tree *tree; 1110d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 11118ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 11125f39d397SChris Mason } 11135f39d397SChris Mason 111470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 11155f39d397SChris Mason { 111698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 111798509cfcSChris Mason return 0; 11180c4e538bSDavid Sterba 1119f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1120d98237b3SChris Mason } 1121d98237b3SChris Mason 1122d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1123d47992f8SLukas Czerner unsigned int length) 1124d98237b3SChris Mason { 1125d1310b2eSChris Mason struct extent_io_tree *tree; 1126d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 11275f39d397SChris Mason extent_invalidatepage(tree, page, offset); 11285f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 11299ad6b7bcSChris Mason if (PagePrivate(page)) { 1130efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1131efe120a0SFrank Holton "page private not zero on page %llu", 1132efe120a0SFrank Holton (unsigned long long)page_offset(page)); 11339ad6b7bcSChris Mason ClearPagePrivate(page); 11349ad6b7bcSChris Mason set_page_private(page, 0); 113509cbfeafSKirill A. Shutemov put_page(page); 11369ad6b7bcSChris Mason } 1137d98237b3SChris Mason } 1138d98237b3SChris Mason 11390b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 11400b32f4bbSJosef Bacik { 1141bb146eb2SJosef Bacik #ifdef DEBUG 11420b32f4bbSJosef Bacik struct extent_buffer *eb; 11430b32f4bbSJosef Bacik 11440b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 11450b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 11460b32f4bbSJosef Bacik BUG_ON(!eb); 11470b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 11480b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 11490b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1150bb146eb2SJosef Bacik #endif 11510b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 11520b32f4bbSJosef Bacik } 11530b32f4bbSJosef Bacik 11547f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1155d98237b3SChris Mason .readpage = btree_readpage, 11560da5468fSChris Mason .writepages = btree_writepages, 11575f39d397SChris Mason .releasepage = btree_releasepage, 11585f39d397SChris Mason .invalidatepage = btree_invalidatepage, 11595a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1160784b4e29SChris Mason .migratepage = btree_migratepage, 11615a92bc88SChris Mason #endif 11620b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1163d98237b3SChris Mason }; 1164123abc88SChris Mason 11652ff7e61eSJeff Mahoney void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr) 1166090d1875SChris Mason { 11675f39d397SChris Mason struct extent_buffer *buf = NULL; 11682ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1169090d1875SChris Mason 11702ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1171c871b0f2SLiu Bo if (IS_ERR(buf)) 11726197d86eSDavid Sterba return; 1173d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 11748436ea91SJosef Bacik buf, WAIT_NONE, btree_get_extent, 0); 11755f39d397SChris Mason free_extent_buffer(buf); 1176090d1875SChris Mason } 1177090d1875SChris Mason 11782ff7e61eSJeff Mahoney int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr, 1179ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1180ab0fff03SArne Jansen { 1181ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 11822ff7e61eSJeff Mahoney struct inode *btree_inode = fs_info->btree_inode; 1183ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1184ab0fff03SArne Jansen int ret; 1185ab0fff03SArne Jansen 11862ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1187c871b0f2SLiu Bo if (IS_ERR(buf)) 1188ab0fff03SArne Jansen return 0; 1189ab0fff03SArne Jansen 1190ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1191ab0fff03SArne Jansen 11928436ea91SJosef Bacik ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK, 1193ab0fff03SArne Jansen btree_get_extent, mirror_num); 1194ab0fff03SArne Jansen if (ret) { 1195ab0fff03SArne Jansen free_extent_buffer(buf); 1196ab0fff03SArne Jansen return ret; 1197ab0fff03SArne Jansen } 1198ab0fff03SArne Jansen 1199ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1200ab0fff03SArne Jansen free_extent_buffer(buf); 1201ab0fff03SArne Jansen return -EIO; 12020b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1203ab0fff03SArne Jansen *eb = buf; 1204ab0fff03SArne Jansen } else { 1205ab0fff03SArne Jansen free_extent_buffer(buf); 1206ab0fff03SArne Jansen } 1207ab0fff03SArne Jansen return 0; 1208ab0fff03SArne Jansen } 1209ab0fff03SArne Jansen 12102ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block( 12112ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 12120308af44SDavid Sterba u64 bytenr) 12130999df54SChris Mason { 12140b246afaSJeff Mahoney if (btrfs_is_testing(fs_info)) 12150b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr); 12160b246afaSJeff Mahoney return alloc_extent_buffer(fs_info, bytenr); 12170999df54SChris Mason } 12180999df54SChris Mason 12190999df54SChris Mason 1220e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1221e02119d5SChris Mason { 1222727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 12238aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1224e02119d5SChris Mason } 1225e02119d5SChris Mason 1226e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1227e02119d5SChris Mason { 1228727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 12298aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1230e02119d5SChris Mason } 1231e02119d5SChris Mason 12322ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 1233ce86cd59SDavid Sterba u64 parent_transid) 1234e20d96d6SChris Mason { 12355f39d397SChris Mason struct extent_buffer *buf = NULL; 123619c00ddcSChris Mason int ret; 123719c00ddcSChris Mason 12382ff7e61eSJeff Mahoney buf = btrfs_find_create_tree_block(fs_info, bytenr); 1239c871b0f2SLiu Bo if (IS_ERR(buf)) 1240c871b0f2SLiu Bo return buf; 1241e4204dedSChris Mason 12422ff7e61eSJeff Mahoney ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 12430f0fe8f7SFilipe David Borba Manana if (ret) { 12440f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 124564c043deSLiu Bo return ERR_PTR(ret); 12460f0fe8f7SFilipe David Borba Manana } 12475f39d397SChris Mason return buf; 1248ce9adaa5SChris Mason 1249eb60ceacSChris Mason } 1250eb60ceacSChris Mason 12517c302b49SDavid Sterba void clean_tree_block(struct btrfs_fs_info *fs_info, 12525f39d397SChris Mason struct extent_buffer *buf) 1253ed2ff2cbSChris Mason { 125455c69072SChris Mason if (btrfs_header_generation(buf) == 1255e2d84521SMiao Xie fs_info->running_transaction->transid) { 1256b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1257b4ce94deSChris Mason 1258b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1259e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1260e2d84521SMiao Xie -buf->len, 1261e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1262b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1263b9473439SChris Mason btrfs_set_lock_blocking(buf); 12640b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1265925baeddSChris Mason } 12665f39d397SChris Mason } 1267ed7b63ebSJosef Bacik } 12685f39d397SChris Mason 12698257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 12708257b2dcSMiao Xie { 12718257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 12728257b2dcSMiao Xie int ret; 12738257b2dcSMiao Xie 12748257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 12758257b2dcSMiao Xie if (!writers) 12768257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 12778257b2dcSMiao Xie 1278908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 12798257b2dcSMiao Xie if (ret < 0) { 12808257b2dcSMiao Xie kfree(writers); 12818257b2dcSMiao Xie return ERR_PTR(ret); 12828257b2dcSMiao Xie } 12838257b2dcSMiao Xie 12848257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 12858257b2dcSMiao Xie return writers; 12868257b2dcSMiao Xie } 12878257b2dcSMiao Xie 12888257b2dcSMiao Xie static void 12898257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 12908257b2dcSMiao Xie { 12918257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 12928257b2dcSMiao Xie kfree(writers); 12938257b2dcSMiao Xie } 12948257b2dcSMiao Xie 1295da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1296e20d96d6SChris Mason u64 objectid) 1297d97e63b6SChris Mason { 12987c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1299cfaa7295SChris Mason root->node = NULL; 1300a28ec197SChris Mason root->commit_root = NULL; 130127cdeb70SMiao Xie root->state = 0; 1302d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 13030b86a832SChris Mason 13040f7d52f4SChris Mason root->objectid = objectid; 13050f7d52f4SChris Mason root->last_trans = 0; 130613a8a7c8SYan, Zheng root->highest_objectid = 0; 1307eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1308199c2a9cSMiao Xie root->nr_ordered_extents = 0; 130958176a96SJosef Bacik root->name = NULL; 13106bef4d31SEric Paris root->inode_tree = RB_ROOT; 131116cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1312f0486c68SYan, Zheng root->block_rsv = NULL; 1313d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 13140b86a832SChris Mason 13150b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 13165d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1317eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1318eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1319199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1320199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 13212ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 13222ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1323d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 13245d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1325eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1326199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1327f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 13282ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 13292ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1330a2135011SChris Mason mutex_init(&root->objectid_mutex); 1331e02119d5SChris Mason mutex_init(&root->log_mutex); 133231f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1333573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 13347237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 13357237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 13367237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 13378b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 13388b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 13397237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 13407237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 13417237f183SYan Zheng atomic_set(&root->log_writers, 0); 13422ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 13438a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1344b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 13458257b2dcSMiao Xie atomic_set(&root->will_be_snapshoted, 0); 134655eeaf05SQu Wenruo atomic_set(&root->qgroup_meta_rsv, 0); 13477237f183SYan Zheng root->log_transid = 0; 1348d1433debSMiao Xie root->log_transid_committed = -1; 1349257c62e1SChris Mason root->last_log_commit = 0; 13507c0260eeSJeff Mahoney if (!dummy) 1351d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1352f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1353017e5369SChris Mason 13543768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 13553768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 13566702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 13577c0260eeSJeff Mahoney if (!dummy) 13583f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 135906ea65a3SJosef Bacik else 136006ea65a3SJosef Bacik root->defrag_trans_start = 0; 13614d775673SChris Mason root->root_key.objectid = objectid; 13620ee5dc67SAl Viro root->anon_dev = 0; 13638ea05e3aSAlexander Block 13645f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 13653768f368SChris Mason } 13663768f368SChris Mason 136774e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, 136874e4d827SDavid Sterba gfp_t flags) 13696f07e42eSAl Viro { 137074e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags); 13716f07e42eSAl Viro if (root) 13726f07e42eSAl Viro root->fs_info = fs_info; 13736f07e42eSAl Viro return root; 13746f07e42eSAl Viro } 13756f07e42eSAl Viro 137606ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 137706ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 1378da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) 137906ea65a3SJosef Bacik { 138006ea65a3SJosef Bacik struct btrfs_root *root; 138106ea65a3SJosef Bacik 13827c0260eeSJeff Mahoney if (!fs_info) 13837c0260eeSJeff Mahoney return ERR_PTR(-EINVAL); 13847c0260eeSJeff Mahoney 13857c0260eeSJeff Mahoney root = btrfs_alloc_root(fs_info, GFP_KERNEL); 138606ea65a3SJosef Bacik if (!root) 138706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1388da17066cSJeff Mahoney 1389b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */ 1390da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 1391faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 139206ea65a3SJosef Bacik 139306ea65a3SJosef Bacik return root; 139406ea65a3SJosef Bacik } 139506ea65a3SJosef Bacik #endif 139606ea65a3SJosef Bacik 139720897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 139820897f5cSArne Jansen struct btrfs_fs_info *fs_info, 139920897f5cSArne Jansen u64 objectid) 140020897f5cSArne Jansen { 140120897f5cSArne Jansen struct extent_buffer *leaf; 140220897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 140320897f5cSArne Jansen struct btrfs_root *root; 140420897f5cSArne Jansen struct btrfs_key key; 140520897f5cSArne Jansen int ret = 0; 14066463fe58SStefan Behrens uuid_le uuid; 140720897f5cSArne Jansen 140874e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_KERNEL); 140920897f5cSArne Jansen if (!root) 141020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 141120897f5cSArne Jansen 1412da17066cSJeff Mahoney __setup_root(root, fs_info, objectid); 141320897f5cSArne Jansen root->root_key.objectid = objectid; 141420897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 141520897f5cSArne Jansen root->root_key.offset = 0; 141620897f5cSArne Jansen 14174d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 141820897f5cSArne Jansen if (IS_ERR(leaf)) { 141920897f5cSArne Jansen ret = PTR_ERR(leaf); 14201dd05682STsutomu Itoh leaf = NULL; 142120897f5cSArne Jansen goto fail; 142220897f5cSArne Jansen } 142320897f5cSArne Jansen 1424b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 142520897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 142620897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 142720897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 142820897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 142920897f5cSArne Jansen root->node = leaf; 143020897f5cSArne Jansen 1431d24ee97bSDavid Sterba write_extent_buffer_fsid(leaf, fs_info->fsid); 1432d24ee97bSDavid Sterba write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 143320897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 143420897f5cSArne Jansen 143520897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 143627cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 143720897f5cSArne Jansen 143820897f5cSArne Jansen root->root_item.flags = 0; 143920897f5cSArne Jansen root->root_item.byte_limit = 0; 144020897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 144120897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 144220897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 144320897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 144420897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 144520897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 144620897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 14476463fe58SStefan Behrens uuid_le_gen(&uuid); 14486463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 144920897f5cSArne Jansen root->root_item.drop_level = 0; 145020897f5cSArne Jansen 145120897f5cSArne Jansen key.objectid = objectid; 145220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 145320897f5cSArne Jansen key.offset = 0; 145420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 145520897f5cSArne Jansen if (ret) 145620897f5cSArne Jansen goto fail; 145720897f5cSArne Jansen 145820897f5cSArne Jansen btrfs_tree_unlock(leaf); 145920897f5cSArne Jansen 146020897f5cSArne Jansen return root; 14611dd05682STsutomu Itoh 14621dd05682STsutomu Itoh fail: 14631dd05682STsutomu Itoh if (leaf) { 14641dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 146559885b39STsutomu Itoh free_extent_buffer(root->commit_root); 14661dd05682STsutomu Itoh free_extent_buffer(leaf); 14671dd05682STsutomu Itoh } 14681dd05682STsutomu Itoh kfree(root); 14691dd05682STsutomu Itoh 14701dd05682STsutomu Itoh return ERR_PTR(ret); 147120897f5cSArne Jansen } 147220897f5cSArne Jansen 14737237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1474e02119d5SChris Mason struct btrfs_fs_info *fs_info) 14750f7d52f4SChris Mason { 14760f7d52f4SChris Mason struct btrfs_root *root; 14777237f183SYan Zheng struct extent_buffer *leaf; 1478e02119d5SChris Mason 147974e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1480e02119d5SChris Mason if (!root) 14817237f183SYan Zheng return ERR_PTR(-ENOMEM); 1482e02119d5SChris Mason 1483da17066cSJeff Mahoney __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1484e02119d5SChris Mason 1485e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1486e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1487e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 148827cdeb70SMiao Xie 14897237f183SYan Zheng /* 149027cdeb70SMiao Xie * DON'T set REF_COWS for log trees 149127cdeb70SMiao Xie * 14927237f183SYan Zheng * log trees do not get reference counted because they go away 14937237f183SYan Zheng * before a real commit is actually done. They do store pointers 14947237f183SYan Zheng * to file data extents, and those reference counts still get 14957237f183SYan Zheng * updated (along with back refs to the log tree). 14967237f183SYan Zheng */ 1497e02119d5SChris Mason 14984d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14994d75f8a9SDavid Sterba NULL, 0, 0, 0); 15007237f183SYan Zheng if (IS_ERR(leaf)) { 15017237f183SYan Zheng kfree(root); 15027237f183SYan Zheng return ERR_CAST(leaf); 15037237f183SYan Zheng } 1504e02119d5SChris Mason 1505b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 15065d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 15075d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 15085d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 15095d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 15107237f183SYan Zheng root->node = leaf; 1511e02119d5SChris Mason 15120b246afaSJeff Mahoney write_extent_buffer_fsid(root->node, fs_info->fsid); 1513e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1514e02119d5SChris Mason btrfs_tree_unlock(root->node); 15157237f183SYan Zheng return root; 15167237f183SYan Zheng } 15177237f183SYan Zheng 15187237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 15197237f183SYan Zheng struct btrfs_fs_info *fs_info) 15207237f183SYan Zheng { 15217237f183SYan Zheng struct btrfs_root *log_root; 15227237f183SYan Zheng 15237237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 15247237f183SYan Zheng if (IS_ERR(log_root)) 15257237f183SYan Zheng return PTR_ERR(log_root); 15267237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 15277237f183SYan Zheng fs_info->log_root_tree = log_root; 15287237f183SYan Zheng return 0; 15297237f183SYan Zheng } 15307237f183SYan Zheng 15317237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 15327237f183SYan Zheng struct btrfs_root *root) 15337237f183SYan Zheng { 15340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 15357237f183SYan Zheng struct btrfs_root *log_root; 15367237f183SYan Zheng struct btrfs_inode_item *inode_item; 15377237f183SYan Zheng 15380b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info); 15397237f183SYan Zheng if (IS_ERR(log_root)) 15407237f183SYan Zheng return PTR_ERR(log_root); 15417237f183SYan Zheng 15427237f183SYan Zheng log_root->last_trans = trans->transid; 15437237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 15447237f183SYan Zheng 15457237f183SYan Zheng inode_item = &log_root->root_item.inode; 15463cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 15473cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 15483cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1549da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 15500b246afaSJeff Mahoney fs_info->nodesize); 15513cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 15527237f183SYan Zheng 15535d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 15547237f183SYan Zheng 15557237f183SYan Zheng WARN_ON(root->log_root); 15567237f183SYan Zheng root->log_root = log_root; 15577237f183SYan Zheng root->log_transid = 0; 1558d1433debSMiao Xie root->log_transid_committed = -1; 1559257c62e1SChris Mason root->last_log_commit = 0; 1560e02119d5SChris Mason return 0; 1561e02119d5SChris Mason } 1562e02119d5SChris Mason 156335a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1564cb517eabSMiao Xie struct btrfs_key *key) 1565e02119d5SChris Mason { 1566e02119d5SChris Mason struct btrfs_root *root; 1567e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 15680f7d52f4SChris Mason struct btrfs_path *path; 156984234f3aSYan Zheng u64 generation; 1570cb517eabSMiao Xie int ret; 1571cb517eabSMiao Xie 1572cb517eabSMiao Xie path = btrfs_alloc_path(); 1573cb517eabSMiao Xie if (!path) 1574cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 15750f7d52f4SChris Mason 157674e4d827SDavid Sterba root = btrfs_alloc_root(fs_info, GFP_NOFS); 1577cb517eabSMiao Xie if (!root) { 1578cb517eabSMiao Xie ret = -ENOMEM; 1579cb517eabSMiao Xie goto alloc_fail; 15800f7d52f4SChris Mason } 15810f7d52f4SChris Mason 1582da17066cSJeff Mahoney __setup_root(root, fs_info, key->objectid); 15830f7d52f4SChris Mason 1584cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1585cb517eabSMiao Xie &root->root_item, &root->root_key); 15860f7d52f4SChris Mason if (ret) { 158713a8a7c8SYan, Zheng if (ret > 0) 158813a8a7c8SYan, Zheng ret = -ENOENT; 1589cb517eabSMiao Xie goto find_fail; 15900f7d52f4SChris Mason } 159113a8a7c8SYan, Zheng 159284234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 15932ff7e61eSJeff Mahoney root->node = read_tree_block(fs_info, 15942ff7e61eSJeff Mahoney btrfs_root_bytenr(&root->root_item), 1595ce86cd59SDavid Sterba generation); 159664c043deSLiu Bo if (IS_ERR(root->node)) { 159764c043deSLiu Bo ret = PTR_ERR(root->node); 1598cb517eabSMiao Xie goto find_fail; 1599cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1600cb517eabSMiao Xie ret = -EIO; 160164c043deSLiu Bo free_extent_buffer(root->node); 160264c043deSLiu Bo goto find_fail; 1603416bc658SJosef Bacik } 16045d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 160513a8a7c8SYan, Zheng out: 1606cb517eabSMiao Xie btrfs_free_path(path); 1607cb517eabSMiao Xie return root; 1608cb517eabSMiao Xie 1609cb517eabSMiao Xie find_fail: 1610cb517eabSMiao Xie kfree(root); 1611cb517eabSMiao Xie alloc_fail: 1612cb517eabSMiao Xie root = ERR_PTR(ret); 1613cb517eabSMiao Xie goto out; 1614cb517eabSMiao Xie } 1615cb517eabSMiao Xie 1616cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1617cb517eabSMiao Xie struct btrfs_key *location) 1618cb517eabSMiao Xie { 1619cb517eabSMiao Xie struct btrfs_root *root; 1620cb517eabSMiao Xie 1621cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1622cb517eabSMiao Xie if (IS_ERR(root)) 1623cb517eabSMiao Xie return root; 1624cb517eabSMiao Xie 1625cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 162627cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 162708fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 162808fe4db1SLi Zefan } 162913a8a7c8SYan, Zheng 16305eda7b5eSChris Mason return root; 16315eda7b5eSChris Mason } 16325eda7b5eSChris Mason 1633cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1634cb517eabSMiao Xie { 1635cb517eabSMiao Xie int ret; 16368257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1637cb517eabSMiao Xie 1638cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1639cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1640cb517eabSMiao Xie GFP_NOFS); 1641cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1642cb517eabSMiao Xie ret = -ENOMEM; 1643cb517eabSMiao Xie goto fail; 1644cb517eabSMiao Xie } 1645cb517eabSMiao Xie 16468257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 16478257b2dcSMiao Xie if (IS_ERR(writers)) { 16488257b2dcSMiao Xie ret = PTR_ERR(writers); 16498257b2dcSMiao Xie goto fail; 16508257b2dcSMiao Xie } 16518257b2dcSMiao Xie root->subv_writers = writers; 16528257b2dcSMiao Xie 1653cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 165457cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 165557cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1656cb517eabSMiao Xie 1657cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1658cb517eabSMiao Xie if (ret) 1659876d2cf1SLiu Bo goto fail; 1660f32e48e9SChandan Rajendra 1661f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex); 1662f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(root, 1663f32e48e9SChandan Rajendra &root->highest_objectid); 1664f32e48e9SChandan Rajendra if (ret) { 1665f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1666876d2cf1SLiu Bo goto fail; 1667f32e48e9SChandan Rajendra } 1668f32e48e9SChandan Rajendra 1669f32e48e9SChandan Rajendra ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 1670f32e48e9SChandan Rajendra 1671f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex); 1672f32e48e9SChandan Rajendra 1673cb517eabSMiao Xie return 0; 1674cb517eabSMiao Xie fail: 1675876d2cf1SLiu Bo /* the caller is responsible to call free_fs_root */ 1676cb517eabSMiao Xie return ret; 1677cb517eabSMiao Xie } 1678cb517eabSMiao Xie 167935bbb97fSJeff Mahoney struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1680cb517eabSMiao Xie u64 root_id) 1681cb517eabSMiao Xie { 1682cb517eabSMiao Xie struct btrfs_root *root; 1683cb517eabSMiao Xie 1684cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1685cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1686cb517eabSMiao Xie (unsigned long)root_id); 1687cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1688cb517eabSMiao Xie return root; 1689cb517eabSMiao Xie } 1690cb517eabSMiao Xie 1691cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1692cb517eabSMiao Xie struct btrfs_root *root) 1693cb517eabSMiao Xie { 1694cb517eabSMiao Xie int ret; 1695cb517eabSMiao Xie 1696e1860a77SDavid Sterba ret = radix_tree_preload(GFP_NOFS); 1697cb517eabSMiao Xie if (ret) 1698cb517eabSMiao Xie return ret; 1699cb517eabSMiao Xie 1700cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1701cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1702cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1703cb517eabSMiao Xie root); 1704cb517eabSMiao Xie if (ret == 0) 170527cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1706cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1707cb517eabSMiao Xie radix_tree_preload_end(); 1708cb517eabSMiao Xie 1709cb517eabSMiao Xie return ret; 1710cb517eabSMiao Xie } 1711cb517eabSMiao Xie 1712c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1713c00869f1SMiao Xie struct btrfs_key *location, 1714c00869f1SMiao Xie bool check_ref) 17155eda7b5eSChris Mason { 17165eda7b5eSChris Mason struct btrfs_root *root; 1717381cf658SDavid Sterba struct btrfs_path *path; 17181d4c08e0SDavid Sterba struct btrfs_key key; 17195eda7b5eSChris Mason int ret; 17205eda7b5eSChris Mason 1721edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1722edbd8d4eSChris Mason return fs_info->tree_root; 1723edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1724edbd8d4eSChris Mason return fs_info->extent_root; 17258f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 17268f18cf13SChris Mason return fs_info->chunk_root; 17278f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 17288f18cf13SChris Mason return fs_info->dev_root; 17290403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 17300403e47eSYan Zheng return fs_info->csum_root; 1731bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1732bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1733bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1734f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1735f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1736f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 173770f6d82eSOmar Sandoval if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) 173870f6d82eSOmar Sandoval return fs_info->free_space_root ? fs_info->free_space_root : 173970f6d82eSOmar Sandoval ERR_PTR(-ENOENT); 17404df27c4dSYan, Zheng again: 1741cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 174248475471SStefan Behrens if (root) { 1743c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 174448475471SStefan Behrens return ERR_PTR(-ENOENT); 17455eda7b5eSChris Mason return root; 174648475471SStefan Behrens } 17475eda7b5eSChris Mason 1748cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 17495eda7b5eSChris Mason if (IS_ERR(root)) 17505eda7b5eSChris Mason return root; 17513394e160SChris Mason 1752c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1753d68fc57bSYan, Zheng ret = -ENOENT; 1754d68fc57bSYan, Zheng goto fail; 1755d68fc57bSYan, Zheng } 1756d68fc57bSYan, Zheng 1757cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1758cb517eabSMiao Xie if (ret) 1759cb517eabSMiao Xie goto fail; 1760cb517eabSMiao Xie 1761381cf658SDavid Sterba path = btrfs_alloc_path(); 1762381cf658SDavid Sterba if (!path) { 1763381cf658SDavid Sterba ret = -ENOMEM; 1764381cf658SDavid Sterba goto fail; 1765381cf658SDavid Sterba } 17661d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID; 17671d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY; 17681d4c08e0SDavid Sterba key.offset = location->objectid; 17691d4c08e0SDavid Sterba 17701d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 1771381cf658SDavid Sterba btrfs_free_path(path); 1772d68fc57bSYan, Zheng if (ret < 0) 1773d68fc57bSYan, Zheng goto fail; 1774d68fc57bSYan, Zheng if (ret == 0) 177527cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1776d68fc57bSYan, Zheng 1777cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 17780f7d52f4SChris Mason if (ret) { 17794df27c4dSYan, Zheng if (ret == -EEXIST) { 17804df27c4dSYan, Zheng free_fs_root(root); 17814df27c4dSYan, Zheng goto again; 17820f7d52f4SChris Mason } 17834df27c4dSYan, Zheng goto fail; 17844df27c4dSYan, Zheng } 1785edbd8d4eSChris Mason return root; 17864df27c4dSYan, Zheng fail: 17874df27c4dSYan, Zheng free_fs_root(root); 17884df27c4dSYan, Zheng return ERR_PTR(ret); 1789edbd8d4eSChris Mason } 1790edbd8d4eSChris Mason 179104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 179204160088SChris Mason { 179304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 179404160088SChris Mason int ret = 0; 179504160088SChris Mason struct btrfs_device *device; 179604160088SChris Mason struct backing_dev_info *bdi; 1797b7967db7SChris Mason 17981f78160cSXiao Guangrong rcu_read_lock(); 17991f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1800dfe25020SChris Mason if (!device->bdev) 1801dfe25020SChris Mason continue; 180204160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 1803ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 180404160088SChris Mason ret = 1; 180504160088SChris Mason break; 180604160088SChris Mason } 180704160088SChris Mason } 18081f78160cSXiao Guangrong rcu_read_unlock(); 180904160088SChris Mason return ret; 181004160088SChris Mason } 181104160088SChris Mason 181204160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 181304160088SChris Mason { 1814ad081f14SJens Axboe int err; 1815ad081f14SJens Axboe 1816b4caecd4SChristoph Hellwig err = bdi_setup_and_register(bdi, "btrfs"); 1817ad081f14SJens Axboe if (err) 1818ad081f14SJens Axboe return err; 1819ad081f14SJens Axboe 182009cbfeafSKirill A. Shutemov bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE; 182104160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 182204160088SChris Mason bdi->congested_data = info; 1823da2f0f74SChris Mason bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK; 182404160088SChris Mason return 0; 182504160088SChris Mason } 182604160088SChris Mason 18278b712842SChris Mason /* 18288b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 18298b712842SChris Mason * functions. This is where read checksum verification actually happens 18308b712842SChris Mason */ 18318b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1832ce9adaa5SChris Mason { 1833ce9adaa5SChris Mason struct bio *bio; 183497eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1835ce9adaa5SChris Mason 183697eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1837ce9adaa5SChris Mason bio = end_io_wq->bio; 18388b712842SChris Mason 18394246a0b6SChristoph Hellwig bio->bi_error = end_io_wq->error; 1840ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1841ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 184297eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 18434246a0b6SChristoph Hellwig bio_endio(bio); 1844ce9adaa5SChris Mason } 184544b8bd7eSChris Mason 1846a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1847a74a4b97SChris Mason { 1848a74a4b97SChris Mason struct btrfs_root *root = arg; 18490b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1850d0278245SMiao Xie int again; 1851da288d28SFilipe Manana struct btrfs_trans_handle *trans; 1852a74a4b97SChris Mason 1853a74a4b97SChris Mason do { 1854d0278245SMiao Xie again = 0; 18559d1a2a3aSDavid Sterba 1856d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 18572ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) 1858d0278245SMiao Xie goto sleep; 1859d0278245SMiao Xie 186090c711abSZygo Blaxell /* 186190c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block 186290c711abSZygo Blaxell * before we have finished mounting the filesystem. 186390c711abSZygo Blaxell */ 18640b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 186590c711abSZygo Blaxell goto sleep; 186690c711abSZygo Blaxell 18670b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex)) 1868d0278245SMiao Xie goto sleep; 1869d0278245SMiao Xie 1870dc7f370cSMiao Xie /* 1871dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1872dc7f370cSMiao Xie * during the above check and trylock. 1873dc7f370cSMiao Xie */ 18742ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) { 18750b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1876dc7f370cSMiao Xie goto sleep; 1877dc7f370cSMiao Xie } 1878dc7f370cSMiao Xie 18790b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 18802ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 18810b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1882c2d6cb16SFilipe Manana 18839d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 18840b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 1885a74a4b97SChris Mason 1886d0278245SMiao Xie /* 188705323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 188805323cd1SMiao Xie * needn't do anything special here. 1889d0278245SMiao Xie */ 18900b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info); 189167c5e7d4SFilipe Manana 189267c5e7d4SFilipe Manana /* 189367c5e7d4SFilipe Manana * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 189467c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation 189567c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group) 189667c5e7d4SFilipe Manana * after acquiring fs_info->delete_unused_bgs_mutex. So we 189767c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting 189867c5e7d4SFilipe Manana * unused block groups. 189967c5e7d4SFilipe Manana */ 19000b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1901d0278245SMiao Xie sleep: 1902838fe188SJiri Kosina if (!again) { 1903a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 19048929ecfaSYan, Zheng if (!kthread_should_stop()) 1905a74a4b97SChris Mason schedule(); 1906a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1907a74a4b97SChris Mason } 1908a74a4b97SChris Mason } while (!kthread_should_stop()); 1909da288d28SFilipe Manana 1910da288d28SFilipe Manana /* 1911da288d28SFilipe Manana * Transaction kthread is stopped before us and wakes us up. 1912da288d28SFilipe Manana * However we might have started a new transaction and COWed some 1913da288d28SFilipe Manana * tree blocks when deleting unused block groups for example. So 1914da288d28SFilipe Manana * make sure we commit the transaction we started to have a clean 1915da288d28SFilipe Manana * shutdown when evicting the btree inode - if it has dirty pages 1916da288d28SFilipe Manana * when we do the final iput() on it, eviction will trigger a 1917da288d28SFilipe Manana * writeback for it which will fail with null pointer dereferences 1918da288d28SFilipe Manana * since work queues and other resources were already released and 1919da288d28SFilipe Manana * destroyed by the time the iput/eviction/writeback is made. 1920da288d28SFilipe Manana */ 1921da288d28SFilipe Manana trans = btrfs_attach_transaction(root); 1922da288d28SFilipe Manana if (IS_ERR(trans)) { 1923da288d28SFilipe Manana if (PTR_ERR(trans) != -ENOENT) 19240b246afaSJeff Mahoney btrfs_err(fs_info, 1925da288d28SFilipe Manana "cleaner transaction attach returned %ld", 1926da288d28SFilipe Manana PTR_ERR(trans)); 1927da288d28SFilipe Manana } else { 1928da288d28SFilipe Manana int ret; 1929da288d28SFilipe Manana 19303a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 1931da288d28SFilipe Manana if (ret) 19320b246afaSJeff Mahoney btrfs_err(fs_info, 1933da288d28SFilipe Manana "cleaner open transaction commit returned %d", 1934da288d28SFilipe Manana ret); 1935da288d28SFilipe Manana } 1936da288d28SFilipe Manana 1937a74a4b97SChris Mason return 0; 1938a74a4b97SChris Mason } 1939a74a4b97SChris Mason 1940a74a4b97SChris Mason static int transaction_kthread(void *arg) 1941a74a4b97SChris Mason { 1942a74a4b97SChris Mason struct btrfs_root *root = arg; 19430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1944a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1945a74a4b97SChris Mason struct btrfs_transaction *cur; 19468929ecfaSYan, Zheng u64 transid; 1947a74a4b97SChris Mason unsigned long now; 1948a74a4b97SChris Mason unsigned long delay; 1949914b2007SJan Kara bool cannot_commit; 1950a74a4b97SChris Mason 1951a74a4b97SChris Mason do { 1952914b2007SJan Kara cannot_commit = false; 19530b246afaSJeff Mahoney delay = HZ * fs_info->commit_interval; 19540b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 1955a74a4b97SChris Mason 19560b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19570b246afaSJeff Mahoney cur = fs_info->running_transaction; 1958a74a4b97SChris Mason if (!cur) { 19590b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1960a74a4b97SChris Mason goto sleep; 1961a74a4b97SChris Mason } 196231153d81SYan Zheng 1963a74a4b97SChris Mason now = get_seconds(); 19644a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 19658b87dc17SDavid Sterba (now < cur->start_time || 19660b246afaSJeff Mahoney now - cur->start_time < fs_info->commit_interval)) { 19670b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1968a74a4b97SChris Mason delay = HZ * 5; 1969a74a4b97SChris Mason goto sleep; 1970a74a4b97SChris Mason } 19718929ecfaSYan, Zheng transid = cur->transid; 19720b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 197356bec294SChris Mason 197479787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1975354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1976914b2007SJan Kara if (IS_ERR(trans)) { 1977354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1978914b2007SJan Kara cannot_commit = true; 197979787eaaSJeff Mahoney goto sleep; 1980914b2007SJan Kara } 19818929ecfaSYan, Zheng if (transid == trans->transid) { 19823a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19838929ecfaSYan, Zheng } else { 19843a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19858929ecfaSYan, Zheng } 1986a74a4b97SChris Mason sleep: 19870b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 19880b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 1989a74a4b97SChris Mason 19904e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 19910b246afaSJeff Mahoney &fs_info->fs_state))) 19922ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 1993a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 19948929ecfaSYan, Zheng if (!kthread_should_stop() && 19950b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) || 1996914b2007SJan Kara cannot_commit)) 1997a74a4b97SChris Mason schedule_timeout(delay); 1998a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1999a74a4b97SChris Mason } while (!kthread_should_stop()); 2000a74a4b97SChris Mason return 0; 2001a74a4b97SChris Mason } 2002a74a4b97SChris Mason 2003af31f5e5SChris Mason /* 2004af31f5e5SChris Mason * this will find the highest generation in the array of 2005af31f5e5SChris Mason * root backups. The index of the highest array is returned, 2006af31f5e5SChris Mason * or -1 if we can't find anything. 2007af31f5e5SChris Mason * 2008af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 2009af31f5e5SChris Mason * generation of the latest root in the array with the generation 2010af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 2011af31f5e5SChris Mason */ 2012af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 2013af31f5e5SChris Mason { 2014af31f5e5SChris Mason u64 cur; 2015af31f5e5SChris Mason int newest_index = -1; 2016af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2017af31f5e5SChris Mason int i; 2018af31f5e5SChris Mason 2019af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 2020af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 2021af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2022af31f5e5SChris Mason if (cur == newest_gen) 2023af31f5e5SChris Mason newest_index = i; 2024af31f5e5SChris Mason } 2025af31f5e5SChris Mason 2026af31f5e5SChris Mason /* check to see if we actually wrapped around */ 2027af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 2028af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 2029af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 2030af31f5e5SChris Mason if (cur == newest_gen) 2031af31f5e5SChris Mason newest_index = 0; 2032af31f5e5SChris Mason } 2033af31f5e5SChris Mason return newest_index; 2034af31f5e5SChris Mason } 2035af31f5e5SChris Mason 2036af31f5e5SChris Mason 2037af31f5e5SChris Mason /* 2038af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 2039af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 2040af31f5e5SChris Mason * field in the fs_info struct 2041af31f5e5SChris Mason */ 2042af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 2043af31f5e5SChris Mason u64 newest_gen) 2044af31f5e5SChris Mason { 2045af31f5e5SChris Mason int newest_index = -1; 2046af31f5e5SChris Mason 2047af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 2048af31f5e5SChris Mason /* if there was garbage in there, just move along */ 2049af31f5e5SChris Mason if (newest_index == -1) { 2050af31f5e5SChris Mason info->backup_root_index = 0; 2051af31f5e5SChris Mason } else { 2052af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 2053af31f5e5SChris Mason } 2054af31f5e5SChris Mason } 2055af31f5e5SChris Mason 2056af31f5e5SChris Mason /* 2057af31f5e5SChris Mason * copy all the root pointers into the super backup array. 2058af31f5e5SChris Mason * this will bump the backup pointer by one when it is 2059af31f5e5SChris Mason * done 2060af31f5e5SChris Mason */ 2061af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 2062af31f5e5SChris Mason { 2063af31f5e5SChris Mason int next_backup; 2064af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2065af31f5e5SChris Mason int last_backup; 2066af31f5e5SChris Mason 2067af31f5e5SChris Mason next_backup = info->backup_root_index; 2068af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 2069af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2070af31f5e5SChris Mason 2071af31f5e5SChris Mason /* 2072af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 2073af31f5e5SChris Mason * this happens only at umount 2074af31f5e5SChris Mason */ 2075af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 2076af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 2077af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 2078af31f5e5SChris Mason next_backup = last_backup; 2079af31f5e5SChris Mason 2080af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 2081af31f5e5SChris Mason 2082af31f5e5SChris Mason /* 2083af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 2084af31f5e5SChris Mason * regardless of which ones we use today 2085af31f5e5SChris Mason */ 2086af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 2087af31f5e5SChris Mason 2088af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 2089af31f5e5SChris Mason 2090af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 2091af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 2092af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 2093af31f5e5SChris Mason 2094af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 2095af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 2096af31f5e5SChris Mason 2097af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 2098af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 2099af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 2100af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 2101af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 2102af31f5e5SChris Mason 2103af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 2104af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 2105af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 2106af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 2107af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 2108af31f5e5SChris Mason 21097c7e82a7SChris Mason /* 21107c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 21117c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 21127c7e82a7SChris Mason */ 21137c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 21147c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 21157c7e82a7SChris Mason info->fs_root->node->start); 2116af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 2117af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 2118af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 2119af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 21207c7e82a7SChris Mason } 2121af31f5e5SChris Mason 2122af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 2123af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 2124af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 2125af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 2126af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 2127af31f5e5SChris Mason 2128af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 2129af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 2130af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 2131af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 2132af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 2133af31f5e5SChris Mason 2134af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 2135af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 2136af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 2137af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2138af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2139af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2140af31f5e5SChris Mason 2141af31f5e5SChris Mason /* 2142af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2143af31f5e5SChris Mason * for the next commit 2144af31f5e5SChris Mason */ 2145af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2146af31f5e5SChris Mason &info->super_for_commit->super_roots, 2147af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2148af31f5e5SChris Mason } 2149af31f5e5SChris Mason 2150af31f5e5SChris Mason /* 2151af31f5e5SChris Mason * this copies info out of the root backup array and back into 2152af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 2153af31f5e5SChris Mason * the array, so you send it the number of backups you've already 2154af31f5e5SChris Mason * tried and the last backup index you used. 2155af31f5e5SChris Mason * 2156af31f5e5SChris Mason * this returns -1 when it has tried all the backups 2157af31f5e5SChris Mason */ 2158af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 2159af31f5e5SChris Mason struct btrfs_super_block *super, 2160af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 2161af31f5e5SChris Mason { 2162af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2163af31f5e5SChris Mason int newest = *backup_index; 2164af31f5e5SChris Mason 2165af31f5e5SChris Mason if (*num_backups_tried == 0) { 2166af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 2167af31f5e5SChris Mason 2168af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 2169af31f5e5SChris Mason if (newest == -1) 2170af31f5e5SChris Mason return -1; 2171af31f5e5SChris Mason 2172af31f5e5SChris Mason *backup_index = newest; 2173af31f5e5SChris Mason *num_backups_tried = 1; 2174af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2175af31f5e5SChris Mason /* we've tried all the backups, all done */ 2176af31f5e5SChris Mason return -1; 2177af31f5e5SChris Mason } else { 2178af31f5e5SChris Mason /* jump to the next oldest backup */ 2179af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2180af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2181af31f5e5SChris Mason *backup_index = newest; 2182af31f5e5SChris Mason *num_backups_tried += 1; 2183af31f5e5SChris Mason } 2184af31f5e5SChris Mason root_backup = super->super_roots + newest; 2185af31f5e5SChris Mason 2186af31f5e5SChris Mason btrfs_set_super_generation(super, 2187af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2188af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2189af31f5e5SChris Mason btrfs_set_super_root_level(super, 2190af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2191af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2192af31f5e5SChris Mason 2193af31f5e5SChris Mason /* 2194af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2195af31f5e5SChris Mason * need a fsck 2196af31f5e5SChris Mason */ 2197af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2198af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2199af31f5e5SChris Mason return 0; 2200af31f5e5SChris Mason } 2201af31f5e5SChris Mason 22027abadb64SLiu Bo /* helper to cleanup workers */ 22037abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 22047abadb64SLiu Bo { 2205dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2206afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 22075cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2208fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2209fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2210fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 22118b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2212d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2213fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 2214fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2215fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2216a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 22175b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2218e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2219736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2220a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2221fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2222a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 22237abadb64SLiu Bo } 22247abadb64SLiu Bo 22252e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 22262e9f5954SRashika { 22272e9f5954SRashika if (root) { 22282e9f5954SRashika free_extent_buffer(root->node); 22292e9f5954SRashika free_extent_buffer(root->commit_root); 22302e9f5954SRashika root->node = NULL; 22312e9f5954SRashika root->commit_root = NULL; 22322e9f5954SRashika } 22332e9f5954SRashika } 22342e9f5954SRashika 2235af31f5e5SChris Mason /* helper to cleanup tree roots */ 2236af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2237af31f5e5SChris Mason { 22382e9f5954SRashika free_root_extent_buffers(info->tree_root); 2239655b09feSJosef Bacik 22402e9f5954SRashika free_root_extent_buffers(info->dev_root); 22412e9f5954SRashika free_root_extent_buffers(info->extent_root); 22422e9f5954SRashika free_root_extent_buffers(info->csum_root); 22432e9f5954SRashika free_root_extent_buffers(info->quota_root); 22442e9f5954SRashika free_root_extent_buffers(info->uuid_root); 22452e9f5954SRashika if (chunk_root) 22462e9f5954SRashika free_root_extent_buffers(info->chunk_root); 224770f6d82eSOmar Sandoval free_root_extent_buffers(info->free_space_root); 2248af31f5e5SChris Mason } 2249af31f5e5SChris Mason 2250faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2251171f6537SJosef Bacik { 2252171f6537SJosef Bacik int ret; 2253171f6537SJosef Bacik struct btrfs_root *gang[8]; 2254171f6537SJosef Bacik int i; 2255171f6537SJosef Bacik 2256171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2257171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2258171f6537SJosef Bacik struct btrfs_root, root_list); 2259171f6537SJosef Bacik list_del(&gang[0]->root_list); 2260171f6537SJosef Bacik 226127cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2262cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2263171f6537SJosef Bacik } else { 2264171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2265171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2266b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2267171f6537SJosef Bacik } 2268171f6537SJosef Bacik } 2269171f6537SJosef Bacik 2270171f6537SJosef Bacik while (1) { 2271171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2272171f6537SJosef Bacik (void **)gang, 0, 2273171f6537SJosef Bacik ARRAY_SIZE(gang)); 2274171f6537SJosef Bacik if (!ret) 2275171f6537SJosef Bacik break; 2276171f6537SJosef Bacik for (i = 0; i < ret; i++) 2277cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2278171f6537SJosef Bacik } 22791a4319ccSLiu Bo 22801a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 22811a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 22822ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 22831a4319ccSLiu Bo } 2284171f6537SJosef Bacik } 2285af31f5e5SChris Mason 2286638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info) 2287638aa7edSEric Sandeen { 2288638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock); 2289638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0); 2290638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0); 2291638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0); 2292638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0); 2293638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait); 2294638aa7edSEric Sandeen fs_info->scrub_workers_refcnt = 0; 2295638aa7edSEric Sandeen } 2296638aa7edSEric Sandeen 2297779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info) 2298779a65a4SEric Sandeen { 2299779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock); 2300779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex); 2301779a65a4SEric Sandeen atomic_set(&fs_info->balance_running, 0); 2302779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0); 2303779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0); 2304779a65a4SEric Sandeen fs_info->balance_ctl = NULL; 2305779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q); 2306779a65a4SEric Sandeen } 2307779a65a4SEric Sandeen 23086bccf3abSJeff Mahoney static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2309f37938e0SEric Sandeen { 23102ff7e61eSJeff Mahoney struct inode *inode = fs_info->btree_inode; 23112ff7e61eSJeff Mahoney 23122ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 23132ff7e61eSJeff Mahoney set_nlink(inode, 1); 2314f37938e0SEric Sandeen /* 2315f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int. 2316f37938e0SEric Sandeen * the real end of the address space is determined by all of 2317f37938e0SEric Sandeen * the devices in the system 2318f37938e0SEric Sandeen */ 23192ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX; 23202ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops; 2321f37938e0SEric Sandeen 23222ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 23232ff7e61eSJeff Mahoney extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode->i_mapping); 23242ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.track_uptodate = 0; 23252ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree); 2326f37938e0SEric Sandeen 23272ff7e61eSJeff Mahoney BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops; 2328f37938e0SEric Sandeen 23292ff7e61eSJeff Mahoney BTRFS_I(inode)->root = fs_info->tree_root; 23302ff7e61eSJeff Mahoney memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key)); 23312ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); 23322ff7e61eSJeff Mahoney btrfs_insert_inode_hash(inode); 2333f37938e0SEric Sandeen } 2334f37938e0SEric Sandeen 2335ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2336ad618368SEric Sandeen { 2337ad618368SEric Sandeen fs_info->dev_replace.lock_owner = 0; 2338ad618368SEric Sandeen atomic_set(&fs_info->dev_replace.nesting_level, 0); 2339ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 234073beece9SLiu Bo rwlock_init(&fs_info->dev_replace.lock); 234173beece9SLiu Bo atomic_set(&fs_info->dev_replace.read_locks, 0); 234273beece9SLiu Bo atomic_set(&fs_info->dev_replace.blocking_readers, 0); 2343ad618368SEric Sandeen init_waitqueue_head(&fs_info->replace_wait); 234473beece9SLiu Bo init_waitqueue_head(&fs_info->dev_replace.read_lock_wq); 2345ad618368SEric Sandeen } 2346ad618368SEric Sandeen 2347f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info) 2348f9e92e40SEric Sandeen { 2349f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock); 2350f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock); 2351f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT; 2352f9e92e40SEric Sandeen fs_info->qgroup_op_tree = RB_ROOT; 2353f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2354f9e92e40SEric Sandeen fs_info->qgroup_seq = 1; 2355f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL; 2356d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false; 2357f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock); 2358f9e92e40SEric Sandeen } 2359f9e92e40SEric Sandeen 23602a458198SEric Sandeen static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, 23612a458198SEric Sandeen struct btrfs_fs_devices *fs_devices) 23622a458198SEric Sandeen { 23632a458198SEric Sandeen int max_active = fs_info->thread_pool_size; 23646f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 23652a458198SEric Sandeen 23662a458198SEric Sandeen fs_info->workers = 2367cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "worker", 2368cb001095SJeff Mahoney flags | WQ_HIGHPRI, max_active, 16); 23692a458198SEric Sandeen 23702a458198SEric Sandeen fs_info->delalloc_workers = 2371cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc", 2372cb001095SJeff Mahoney flags, max_active, 2); 23732a458198SEric Sandeen 23742a458198SEric Sandeen fs_info->flush_workers = 2375cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc", 2376cb001095SJeff Mahoney flags, max_active, 0); 23772a458198SEric Sandeen 23782a458198SEric Sandeen fs_info->caching_workers = 2379cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0); 23802a458198SEric Sandeen 23812a458198SEric Sandeen /* 23822a458198SEric Sandeen * a higher idle thresh on the submit workers makes it much more 23832a458198SEric Sandeen * likely that bios will be send down in a sane order to the 23842a458198SEric Sandeen * devices 23852a458198SEric Sandeen */ 23862a458198SEric Sandeen fs_info->submit_workers = 2387cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "submit", flags, 23882a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 23892a458198SEric Sandeen max_active), 64); 23902a458198SEric Sandeen 23912a458198SEric Sandeen fs_info->fixup_workers = 2392cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0); 23932a458198SEric Sandeen 23942a458198SEric Sandeen /* 23952a458198SEric Sandeen * endios are largely parallel and should have a very 23962a458198SEric Sandeen * low idle thresh 23972a458198SEric Sandeen */ 23982a458198SEric Sandeen fs_info->endio_workers = 2399cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4); 24002a458198SEric Sandeen fs_info->endio_meta_workers = 2401cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta", flags, 2402cb001095SJeff Mahoney max_active, 4); 24032a458198SEric Sandeen fs_info->endio_meta_write_workers = 2404cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags, 2405cb001095SJeff Mahoney max_active, 2); 24062a458198SEric Sandeen fs_info->endio_raid56_workers = 2407cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-raid56", flags, 2408cb001095SJeff Mahoney max_active, 4); 24092a458198SEric Sandeen fs_info->endio_repair_workers = 2410cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0); 24112a458198SEric Sandeen fs_info->rmw_workers = 2412cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2); 24132a458198SEric Sandeen fs_info->endio_write_workers = 2414cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags, 2415cb001095SJeff Mahoney max_active, 2); 24162a458198SEric Sandeen fs_info->endio_freespace_worker = 2417cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags, 2418cb001095SJeff Mahoney max_active, 0); 24192a458198SEric Sandeen fs_info->delayed_workers = 2420cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags, 2421cb001095SJeff Mahoney max_active, 0); 24222a458198SEric Sandeen fs_info->readahead_workers = 2423cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "readahead", flags, 2424cb001095SJeff Mahoney max_active, 2); 24252a458198SEric Sandeen fs_info->qgroup_rescan_workers = 2426cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0); 24272a458198SEric Sandeen fs_info->extent_workers = 2428cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "extent-refs", flags, 24292a458198SEric Sandeen min_t(u64, fs_devices->num_devices, 24302a458198SEric Sandeen max_active), 8); 24312a458198SEric Sandeen 24322a458198SEric Sandeen if (!(fs_info->workers && fs_info->delalloc_workers && 24332a458198SEric Sandeen fs_info->submit_workers && fs_info->flush_workers && 24342a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers && 24352a458198SEric Sandeen fs_info->endio_meta_write_workers && 24362a458198SEric Sandeen fs_info->endio_repair_workers && 24372a458198SEric Sandeen fs_info->endio_write_workers && fs_info->endio_raid56_workers && 24382a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers && 24392a458198SEric Sandeen fs_info->caching_workers && fs_info->readahead_workers && 24402a458198SEric Sandeen fs_info->fixup_workers && fs_info->delayed_workers && 24412a458198SEric Sandeen fs_info->extent_workers && 24422a458198SEric Sandeen fs_info->qgroup_rescan_workers)) { 24432a458198SEric Sandeen return -ENOMEM; 24442a458198SEric Sandeen } 24452a458198SEric Sandeen 24462a458198SEric Sandeen return 0; 24472a458198SEric Sandeen } 24482a458198SEric Sandeen 244963443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info, 245063443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices) 245163443bf5SEric Sandeen { 245263443bf5SEric Sandeen int ret; 245363443bf5SEric Sandeen struct btrfs_root *log_tree_root; 245463443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy; 245563443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super); 245663443bf5SEric Sandeen 245763443bf5SEric Sandeen if (fs_devices->rw_devices == 0) { 2458f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media"); 245963443bf5SEric Sandeen return -EIO; 246063443bf5SEric Sandeen } 246163443bf5SEric Sandeen 246274e4d827SDavid Sterba log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 246363443bf5SEric Sandeen if (!log_tree_root) 246463443bf5SEric Sandeen return -ENOMEM; 246563443bf5SEric Sandeen 2466da17066cSJeff Mahoney __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 246763443bf5SEric Sandeen 24682ff7e61eSJeff Mahoney log_tree_root->node = read_tree_block(fs_info, bytenr, 246963443bf5SEric Sandeen fs_info->generation + 1); 247064c043deSLiu Bo if (IS_ERR(log_tree_root->node)) { 2471f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree"); 24720eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node); 247364c043deSLiu Bo kfree(log_tree_root); 24740eeff236SLiu Bo return ret; 247564c043deSLiu Bo } else if (!extent_buffer_uptodate(log_tree_root->node)) { 2476f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree"); 247763443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 247863443bf5SEric Sandeen kfree(log_tree_root); 247963443bf5SEric Sandeen return -EIO; 248063443bf5SEric Sandeen } 248163443bf5SEric Sandeen /* returns with log_tree_root freed on success */ 248263443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root); 248363443bf5SEric Sandeen if (ret) { 24840b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 248563443bf5SEric Sandeen "Failed to recover log tree"); 248663443bf5SEric Sandeen free_extent_buffer(log_tree_root->node); 248763443bf5SEric Sandeen kfree(log_tree_root); 248863443bf5SEric Sandeen return ret; 248963443bf5SEric Sandeen } 249063443bf5SEric Sandeen 249163443bf5SEric Sandeen if (fs_info->sb->s_flags & MS_RDONLY) { 24926bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 249363443bf5SEric Sandeen if (ret) 249463443bf5SEric Sandeen return ret; 249563443bf5SEric Sandeen } 249663443bf5SEric Sandeen 249763443bf5SEric Sandeen return 0; 249863443bf5SEric Sandeen } 249963443bf5SEric Sandeen 25006bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info) 25014bbcaa64SEric Sandeen { 25026bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 2503a4f3d2c4SDavid Sterba struct btrfs_root *root; 25044bbcaa64SEric Sandeen struct btrfs_key location; 25054bbcaa64SEric Sandeen int ret; 25064bbcaa64SEric Sandeen 25076bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root); 25086bccf3abSJeff Mahoney 25094bbcaa64SEric Sandeen location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 25104bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY; 25114bbcaa64SEric Sandeen location.offset = 0; 25124bbcaa64SEric Sandeen 2513a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2514a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2515a4f3d2c4SDavid Sterba return PTR_ERR(root); 2516a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2517a4f3d2c4SDavid Sterba fs_info->extent_root = root; 25184bbcaa64SEric Sandeen 25194bbcaa64SEric Sandeen location.objectid = BTRFS_DEV_TREE_OBJECTID; 2520a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2521a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2522a4f3d2c4SDavid Sterba return PTR_ERR(root); 2523a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2524a4f3d2c4SDavid Sterba fs_info->dev_root = root; 25254bbcaa64SEric Sandeen btrfs_init_devices_late(fs_info); 25264bbcaa64SEric Sandeen 25274bbcaa64SEric Sandeen location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2528a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2529a4f3d2c4SDavid Sterba if (IS_ERR(root)) 2530a4f3d2c4SDavid Sterba return PTR_ERR(root); 2531a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2532a4f3d2c4SDavid Sterba fs_info->csum_root = root; 25334bbcaa64SEric Sandeen 25344bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2535a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2536a4f3d2c4SDavid Sterba if (!IS_ERR(root)) { 2537a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2538afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); 2539a4f3d2c4SDavid Sterba fs_info->quota_root = root; 25404bbcaa64SEric Sandeen } 25414bbcaa64SEric Sandeen 25424bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID; 2543a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location); 2544a4f3d2c4SDavid Sterba if (IS_ERR(root)) { 2545a4f3d2c4SDavid Sterba ret = PTR_ERR(root); 25464bbcaa64SEric Sandeen if (ret != -ENOENT) 25474bbcaa64SEric Sandeen return ret; 25484bbcaa64SEric Sandeen } else { 2549a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 2550a4f3d2c4SDavid Sterba fs_info->uuid_root = root; 25514bbcaa64SEric Sandeen } 25524bbcaa64SEric Sandeen 255370f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 255470f6d82eSOmar Sandoval location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID; 255570f6d82eSOmar Sandoval root = btrfs_read_tree_root(tree_root, &location); 255670f6d82eSOmar Sandoval if (IS_ERR(root)) 255770f6d82eSOmar Sandoval return PTR_ERR(root); 255870f6d82eSOmar Sandoval set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 255970f6d82eSOmar Sandoval fs_info->free_space_root = root; 256070f6d82eSOmar Sandoval } 256170f6d82eSOmar Sandoval 25624bbcaa64SEric Sandeen return 0; 25634bbcaa64SEric Sandeen } 25644bbcaa64SEric Sandeen 2565ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2566dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2567dfe25020SChris Mason char *options) 2568eb60ceacSChris Mason { 2569db94535dSChris Mason u32 sectorsize; 2570db94535dSChris Mason u32 nodesize; 257187ee04ebSChris Mason u32 stripesize; 257284234f3aSYan Zheng u64 generation; 2573f2b636e8SJosef Bacik u64 features; 25743de4586cSChris Mason struct btrfs_key location; 2575a061fc8dSChris Mason struct buffer_head *bh; 25764d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2577815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2578f84a8bd6SAl Viro struct btrfs_root *tree_root; 25794d34b278SIlya Dryomov struct btrfs_root *chunk_root; 2580eb60ceacSChris Mason int ret; 2581e58ca020SYan int err = -EINVAL; 2582af31f5e5SChris Mason int num_backups_tried = 0; 2583af31f5e5SChris Mason int backup_index = 0; 25845cdc7ad3SQu Wenruo int max_active; 25856675df31SOmar Sandoval int clear_free_space_tree = 0; 25864543df7eSChris Mason 258774e4d827SDavid Sterba tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 258874e4d827SDavid Sterba chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); 2589cb517eabSMiao Xie if (!tree_root || !chunk_root) { 259039279cc3SChris Mason err = -ENOMEM; 259139279cc3SChris Mason goto fail; 259239279cc3SChris Mason } 259376dda93cSYan, Zheng 259476dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 259576dda93cSYan, Zheng if (ret) { 259676dda93cSYan, Zheng err = ret; 259776dda93cSYan, Zheng goto fail; 259876dda93cSYan, Zheng } 259976dda93cSYan, Zheng 260076dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 260176dda93cSYan, Zheng if (ret) { 260276dda93cSYan, Zheng err = ret; 260376dda93cSYan, Zheng goto fail_srcu; 260476dda93cSYan, Zheng } 260576dda93cSYan, Zheng 2606908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2607e2d84521SMiao Xie if (ret) { 2608e2d84521SMiao Xie err = ret; 2609e2d84521SMiao Xie goto fail_bdi; 2610e2d84521SMiao Xie } 261109cbfeafSKirill A. Shutemov fs_info->dirty_metadata_batch = PAGE_SIZE * 2612e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2613e2d84521SMiao Xie 2614908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2615963d678bSMiao Xie if (ret) { 2616963d678bSMiao Xie err = ret; 2617963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2618963d678bSMiao Xie } 2619963d678bSMiao Xie 2620908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2621c404e0dcSMiao Xie if (ret) { 2622c404e0dcSMiao Xie err = ret; 2623c404e0dcSMiao Xie goto fail_delalloc_bytes; 2624c404e0dcSMiao Xie } 2625c404e0dcSMiao Xie 262676dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 262776dda93cSYan, Zheng if (!fs_info->btree_inode) { 262876dda93cSYan, Zheng err = -ENOMEM; 2629c404e0dcSMiao Xie goto fail_bio_counter; 263076dda93cSYan, Zheng } 263176dda93cSYan, Zheng 2632a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 26331561dedaSMiao Xie 263476dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2635f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 26368fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2637facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 263824bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2639eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 264011833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2641eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2642a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 264376dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 264424bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 26454cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 26462bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2647f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2648ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2649fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2650f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 265147ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2652f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 2653d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex); 265467c5e7d4SFilipe Manana mutex_init(&fs_info->delete_unused_bgs_mutex); 26557585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2656573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2657c2d6cb16SFilipe Manana mutex_init(&fs_info->cleaner_delayed_iput_mutex); 2658de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 265919c00ddcSChris Mason 26600b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 26616324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2662f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 266347ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 26640b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 266566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 266666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 266766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 266866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 266966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 267066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 267166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 267266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 267366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2674cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2675771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 26768c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 26770986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 26784cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2679fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 26802fefd558SZhao Lei atomic_set(&fs_info->reada_works_cnt, 0); 2681fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 26829e7cc91aSWang Xiaoguang fs_info->fs_frozen = 0; 2683e20d96d6SChris Mason fs_info->sb = sb; 268495ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 26859ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 26864cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 26872bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2688f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 26898b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2690f8c269d7SDavid Sterba fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */ 269190519d66SArne Jansen /* readahead state */ 2692d0164adcSMel Gorman INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 269390519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2694c8b97818SChris Mason 2695b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2696b34b086cSChris Mason num_online_cpus() + 2, 8); 26970afbaf8cSChris Mason 2698199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2699199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 270016cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 270174e4d827SDavid Sterba GFP_KERNEL); 270216cdcec7SMiao Xie if (!fs_info->delayed_root) { 270316cdcec7SMiao Xie err = -ENOMEM; 270416cdcec7SMiao Xie goto fail_iput; 270516cdcec7SMiao Xie } 270616cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 27073eaa2885SChris Mason 2708638aa7edSEric Sandeen btrfs_init_scrub(fs_info); 270921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 271021adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 271121adbd5cSStefan Behrens #endif 2712779a65a4SEric Sandeen btrfs_init_balance(fs_info); 271321c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2714a2de733cSArne Jansen 2715a061fc8dSChris Mason sb->s_blocksize = 4096; 2716a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 271732a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 2718a061fc8dSChris Mason 27196bccf3abSJeff Mahoney btrfs_init_btree_inode(fs_info); 272039279cc3SChris Mason 2721e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 27226bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2723a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2724925baeddSChris Mason 272511833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2726f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 272711833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2728f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 272911833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2730afcdd129SJosef Bacik set_bit(BTRFS_FS_BARRIER, &fs_info->flags); 2731509659cdSChris Mason 27325a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 273379154b1bSChris Mason mutex_init(&fs_info->tree_log_mutex); 2734925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2735a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2736a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 27377d9eb12cSChris Mason mutex_init(&fs_info->volume_mutex); 27381bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex); 27399e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2740c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 274176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2742803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2743fa9c0d79SChris Mason 2744ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info); 2745f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info); 2746416ac51dSArne Jansen 2747fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2748fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2749fa9c0d79SChris Mason 2750e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 2751f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2752bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 27534854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 27543768f368SChris Mason 275504216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 275604216820SFilipe Manana 2757da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */ 2758da17066cSJeff Mahoney fs_info->nodesize = 4096; 2759da17066cSJeff Mahoney fs_info->sectorsize = 4096; 2760da17066cSJeff Mahoney fs_info->stripesize = 4096; 2761da17066cSJeff Mahoney 276253b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 276353b381b3SDavid Woodhouse if (ret) { 276483c8266aSDavid Sterba err = ret; 276553b381b3SDavid Woodhouse goto fail_alloc; 276653b381b3SDavid Woodhouse } 276753b381b3SDavid Woodhouse 2768da17066cSJeff Mahoney __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); 27697eccb903SChris Mason 27703c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 27711104a885SDavid Sterba 27721104a885SDavid Sterba /* 27731104a885SDavid Sterba * Read super block and check the signature bytes only 27741104a885SDavid Sterba */ 2775a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 277692fc03fbSAnand Jain if (IS_ERR(bh)) { 277792fc03fbSAnand Jain err = PTR_ERR(bh); 277816cdcec7SMiao Xie goto fail_alloc; 277920b45077SDave Young } 278039279cc3SChris Mason 27811104a885SDavid Sterba /* 27821104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 27831104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 27841104a885SDavid Sterba */ 2785ab8d0fc4SJeff Mahoney if (btrfs_check_super_csum(fs_info, bh->b_data)) { 278605135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch"); 27871104a885SDavid Sterba err = -EINVAL; 2788b2acdddfSAnand Jain brelse(bh); 27891104a885SDavid Sterba goto fail_alloc; 27901104a885SDavid Sterba } 27911104a885SDavid Sterba 27921104a885SDavid Sterba /* 27931104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 27941104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 27951104a885SDavid Sterba * the whole block of INFO_SIZE 27961104a885SDavid Sterba */ 27976c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 27986c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 27996c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2800a061fc8dSChris Mason brelse(bh); 28015f39d397SChris Mason 28026c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 28030b86a832SChris Mason 28041104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 28051104a885SDavid Sterba if (ret) { 280605135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors"); 28071104a885SDavid Sterba err = -EINVAL; 28081104a885SDavid Sterba goto fail_alloc; 28091104a885SDavid Sterba } 28101104a885SDavid Sterba 28116c41761fSDavid Sterba disk_super = fs_info->super_copy; 28120f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 281316cdcec7SMiao Xie goto fail_alloc; 28140f7d52f4SChris Mason 2815acce952bSliubo /* check FS state, whether FS is broken. */ 281687533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 281787533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2818acce952bSliubo 281975e7cb7fSLiu Bo /* 2820af31f5e5SChris Mason * run through our array of backup supers and setup 2821af31f5e5SChris Mason * our ring pointer to the oldest one 2822af31f5e5SChris Mason */ 2823af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2824af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2825af31f5e5SChris Mason 2826af31f5e5SChris Mason /* 282775e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 282875e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 282975e7cb7fSLiu Bo */ 283075e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 283175e7cb7fSLiu Bo 28322ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags); 28332b82032cSYan Zheng if (ret) { 28342b82032cSYan Zheng err = ret; 283516cdcec7SMiao Xie goto fail_alloc; 28362b82032cSYan Zheng } 2837dfe25020SChris Mason 2838f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2839f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2840f2b636e8SJosef Bacik if (features) { 284105135f59SDavid Sterba btrfs_err(fs_info, 284205135f59SDavid Sterba "cannot mount because of unsupported optional features (%llx)", 2843c1c9ff7cSGeert Uytterhoeven features); 2844f2b636e8SJosef Bacik err = -EINVAL; 284516cdcec7SMiao Xie goto fail_alloc; 2846f2b636e8SJosef Bacik } 2847f2b636e8SJosef Bacik 28485d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 28495d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 28500b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 2851a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2852727011e0SChris Mason 28533173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 285405135f59SDavid Sterba btrfs_info(fs_info, "has skinny extents"); 28553173a18fSJosef Bacik 2856727011e0SChris Mason /* 2857727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2858727011e0SChris Mason * they are bigger than the page size 2859727011e0SChris Mason */ 286009cbfeafSKirill A. Shutemov if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) { 2861727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 286205135f59SDavid Sterba btrfs_info(fs_info, 286305135f59SDavid Sterba "flagging fs with big metadata feature"); 2864727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2865727011e0SChris Mason } 2866727011e0SChris Mason 2867bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2868bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2869b7f67055SChandan Rajendra stripesize = sectorsize; 2870707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2871963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2872bc3f116fSChris Mason 2873da17066cSJeff Mahoney /* Cache block sizes */ 2874da17066cSJeff Mahoney fs_info->nodesize = nodesize; 2875da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 2876da17066cSJeff Mahoney fs_info->stripesize = stripesize; 2877da17066cSJeff Mahoney 2878bc3f116fSChris Mason /* 2879bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2880bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2881bc3f116fSChris Mason */ 2882bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2883707e8a07SDavid Sterba (sectorsize != nodesize)) { 288405135f59SDavid Sterba btrfs_err(fs_info, 288505135f59SDavid Sterba "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups", 288605135f59SDavid Sterba nodesize, sectorsize); 2887bc3f116fSChris Mason goto fail_alloc; 2888bc3f116fSChris Mason } 2889bc3f116fSChris Mason 2890ceda0864SMiao Xie /* 2891ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2892ceda0864SMiao Xie * update the flag. 2893ceda0864SMiao Xie */ 28945d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 28955d4f98a2SYan Zheng 2896f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2897f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2898f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 289905135f59SDavid Sterba btrfs_err(fs_info, 290005135f59SDavid Sterba "cannot mount read-write because of unsupported optional features (%llx)", 2901c1c9ff7cSGeert Uytterhoeven features); 2902f2b636e8SJosef Bacik err = -EINVAL; 290316cdcec7SMiao Xie goto fail_alloc; 2904f2b636e8SJosef Bacik } 290561d92c32SChris Mason 29065cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 290761d92c32SChris Mason 29082a458198SEric Sandeen ret = btrfs_init_workqueues(fs_info, fs_devices); 29092a458198SEric Sandeen if (ret) { 29102a458198SEric Sandeen err = ret; 29110dc3b84aSJosef Bacik goto fail_sb_buffer; 29120dc3b84aSJosef Bacik } 29134543df7eSChris Mason 29144575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2915c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 291609cbfeafSKirill A. Shutemov SZ_4M / PAGE_SIZE); 29174575c9ccSChris Mason 2918a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2919a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2920db94535dSChris Mason 2921925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 29226bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info); 2923925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 292484eed90fSChris Mason if (ret) { 292505135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret); 29265d4f98a2SYan Zheng goto fail_sb_buffer; 292784eed90fSChris Mason } 29280b86a832SChris Mason 292984234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 29300b86a832SChris Mason 2931da17066cSJeff Mahoney __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 29320b86a832SChris Mason 29332ff7e61eSJeff Mahoney chunk_root->node = read_tree_block(fs_info, 29340b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2935ce86cd59SDavid Sterba generation); 293664c043deSLiu Bo if (IS_ERR(chunk_root->node) || 293764c043deSLiu Bo !extent_buffer_uptodate(chunk_root->node)) { 293805135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root"); 2939e5fffbacSchandan if (!IS_ERR(chunk_root->node)) 2940e5fffbacSchandan free_extent_buffer(chunk_root->node); 294195ab1f64SZhao Lei chunk_root->node = NULL; 2942af31f5e5SChris Mason goto fail_tree_roots; 294383121942SDavid Woodhouse } 29445d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 29455d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 29460b86a832SChris Mason 2947e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2948b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2949e17cade2SChris Mason 29505b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info); 29512b82032cSYan Zheng if (ret) { 295205135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret); 2953af31f5e5SChris Mason goto fail_tree_roots; 29542b82032cSYan Zheng } 29550b86a832SChris Mason 29568dabb742SStefan Behrens /* 29578dabb742SStefan Behrens * keep the device that is marked to be the target device for the 29588dabb742SStefan Behrens * dev_replace procedure 29598dabb742SStefan Behrens */ 29609eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 0); 2961dfe25020SChris Mason 2962a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 296305135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices"); 2964a6b0d5c8SChris Mason goto fail_tree_roots; 2965a6b0d5c8SChris Mason } 2966a6b0d5c8SChris Mason 2967af31f5e5SChris Mason retry_root_backup: 296884234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 29690b86a832SChris Mason 29702ff7e61eSJeff Mahoney tree_root->node = read_tree_block(fs_info, 2971db94535dSChris Mason btrfs_super_root(disk_super), 2972ce86cd59SDavid Sterba generation); 297364c043deSLiu Bo if (IS_ERR(tree_root->node) || 297464c043deSLiu Bo !extent_buffer_uptodate(tree_root->node)) { 297505135f59SDavid Sterba btrfs_warn(fs_info, "failed to read tree root"); 2976e5fffbacSchandan if (!IS_ERR(tree_root->node)) 2977e5fffbacSchandan free_extent_buffer(tree_root->node); 297895ab1f64SZhao Lei tree_root->node = NULL; 2979af31f5e5SChris Mason goto recovery_tree_root; 298083121942SDavid Woodhouse } 2981af31f5e5SChris Mason 29825d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 29835d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 298469e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2985db94535dSChris Mason 2986f32e48e9SChandan Rajendra mutex_lock(&tree_root->objectid_mutex); 2987f32e48e9SChandan Rajendra ret = btrfs_find_highest_objectid(tree_root, 2988f32e48e9SChandan Rajendra &tree_root->highest_objectid); 2989f32e48e9SChandan Rajendra if (ret) { 2990f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2991f32e48e9SChandan Rajendra goto recovery_tree_root; 2992f32e48e9SChandan Rajendra } 2993f32e48e9SChandan Rajendra 2994f32e48e9SChandan Rajendra ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID); 2995f32e48e9SChandan Rajendra 2996f32e48e9SChandan Rajendra mutex_unlock(&tree_root->objectid_mutex); 2997f32e48e9SChandan Rajendra 29986bccf3abSJeff Mahoney ret = btrfs_read_roots(fs_info); 29994bbcaa64SEric Sandeen if (ret) 3000af31f5e5SChris Mason goto recovery_tree_root; 3001f7a81ea4SStefan Behrens 30028929ecfaSYan, Zheng fs_info->generation = generation; 30038929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 30048929ecfaSYan, Zheng 300568310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 300668310a5eSIlya Dryomov if (ret) { 300705135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret); 300868310a5eSIlya Dryomov goto fail_block_groups; 300968310a5eSIlya Dryomov } 301068310a5eSIlya Dryomov 3011733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 3012733f4fbbSStefan Behrens if (ret) { 301305135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret); 3014733f4fbbSStefan Behrens goto fail_block_groups; 3015733f4fbbSStefan Behrens } 3016733f4fbbSStefan Behrens 30178dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 30188dabb742SStefan Behrens if (ret) { 301905135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret); 30208dabb742SStefan Behrens goto fail_block_groups; 30218dabb742SStefan Behrens } 30228dabb742SStefan Behrens 30239eaed21eSEric Sandeen btrfs_close_extra_devices(fs_devices, 1); 30248dabb742SStefan Behrens 3025b7c35e81SAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices, NULL); 3026b7c35e81SAnand Jain if (ret) { 302705135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d", 302805135f59SDavid Sterba ret); 3029b7c35e81SAnand Jain goto fail_block_groups; 3030b7c35e81SAnand Jain } 3031b7c35e81SAnand Jain 3032b7c35e81SAnand Jain ret = btrfs_sysfs_add_device(fs_devices); 3033b7c35e81SAnand Jain if (ret) { 303405135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs device interface: %d", 303505135f59SDavid Sterba ret); 3036b7c35e81SAnand Jain goto fail_fsdev_sysfs; 3037b7c35e81SAnand Jain } 3038b7c35e81SAnand Jain 303996f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info); 30405ac1d209SJeff Mahoney if (ret) { 304105135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret); 3042b7c35e81SAnand Jain goto fail_fsdev_sysfs; 30435ac1d209SJeff Mahoney } 30445ac1d209SJeff Mahoney 3045c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 3046c59021f8Sliubo if (ret) { 304705135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret); 30482365dd3cSAnand Jain goto fail_sysfs; 3049c59021f8Sliubo } 3050c59021f8Sliubo 30515b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info); 30521b1d1f66SJosef Bacik if (ret) { 305305135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret); 30542365dd3cSAnand Jain goto fail_sysfs; 30551b1d1f66SJosef Bacik } 30565af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 30575af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 3058292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 3059292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 3060292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 306105135f59SDavid Sterba btrfs_warn(fs_info, 306205135f59SDavid Sterba "missing devices (%llu) exceeds the limit (%d), writeable mount is not allowed", 306378fa1770SZhao Lei fs_info->fs_devices->missing_devices, 306478fa1770SZhao Lei fs_info->num_tolerated_disk_barrier_failures); 30652365dd3cSAnand Jain goto fail_sysfs; 3066292fd7fcSStefan Behrens } 30679078a3e1SChris Mason 3068a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 3069a74a4b97SChris Mason "btrfs-cleaner"); 307057506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 30712365dd3cSAnand Jain goto fail_sysfs; 3072a74a4b97SChris Mason 3073a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 3074a74a4b97SChris Mason tree_root, 3075a74a4b97SChris Mason "btrfs-transaction"); 307657506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 30773f157a2fSChris Mason goto fail_cleaner; 3078a74a4b97SChris Mason 30790b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, SSD) && 30800b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOSSD) && 3081c289811cSChris Mason !fs_info->fs_devices->rotating) { 308205135f59SDavid Sterba btrfs_info(fs_info, "detected SSD devices, enabling SSD mode"); 3083c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 3084c289811cSChris Mason } 3085c289811cSChris Mason 3086572d9ab7SDavid Sterba /* 308701327610SNicholas D Steeves * Mount does not set all options immediately, we can do it now and do 3088572d9ab7SDavid Sterba * not have to wait for transaction commit 3089572d9ab7SDavid Sterba */ 3090572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 30913818aea2SQu Wenruo 309221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 30930b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 30942ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices, 30950b246afaSJeff Mahoney btrfs_test_opt(fs_info, 309621adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 309721adbd5cSStefan Behrens 1 : 0, 309821adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 309921adbd5cSStefan Behrens if (ret) 310005135f59SDavid Sterba btrfs_warn(fs_info, 310105135f59SDavid Sterba "failed to initialize integrity check module: %d", 310205135f59SDavid Sterba ret); 310321adbd5cSStefan Behrens } 310421adbd5cSStefan Behrens #endif 3105bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 3106bcef60f2SArne Jansen if (ret) 3107bcef60f2SArne Jansen goto fail_trans_kthread; 310821adbd5cSStefan Behrens 310996da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */ 311096da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 && 31110b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) { 311263443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices); 311379787eaaSJeff Mahoney if (ret) { 311463443bf5SEric Sandeen err = ret; 311528c16cbbSWang Shilong goto fail_qgroup; 3116e556ce2cSYan Zheng } 3117e02119d5SChris Mason } 31181a40e23bSZheng Yan 31196bccf3abSJeff Mahoney ret = btrfs_find_orphan_roots(fs_info); 312079787eaaSJeff Mahoney if (ret) 312128c16cbbSWang Shilong goto fail_qgroup; 312276dda93cSYan, Zheng 31237c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 3124d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 312544c44af2SIlya Dryomov if (ret) 312628c16cbbSWang Shilong goto fail_qgroup; 312790c711abSZygo Blaxell 312890c711abSZygo Blaxell mutex_lock(&fs_info->cleaner_mutex); 31295d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 313090c711abSZygo Blaxell mutex_unlock(&fs_info->cleaner_mutex); 3131d7ce5843SMiao Xie if (ret < 0) { 313205135f59SDavid Sterba btrfs_warn(fs_info, "failed to recover relocation: %d", 313305135f59SDavid Sterba ret); 3134d7ce5843SMiao Xie err = -EINVAL; 3135bcef60f2SArne Jansen goto fail_qgroup; 3136d7ce5843SMiao Xie } 31377c2ca468SChris Mason } 31381a40e23bSZheng Yan 31393de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 31403de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 3141cb517eabSMiao Xie location.offset = 0; 31423de4586cSChris Mason 31433de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 31443140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 31453140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 3146bcef60f2SArne Jansen goto fail_qgroup; 31473140c9a3SDan Carpenter } 3148c289811cSChris Mason 31492b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 31502b6ba629SIlya Dryomov return 0; 31512b6ba629SIlya Dryomov 3152f8d468a1SOmar Sandoval if (btrfs_test_opt(fs_info, CLEAR_CACHE) && 3153f8d468a1SOmar Sandoval btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 31546675df31SOmar Sandoval clear_free_space_tree = 1; 31556675df31SOmar Sandoval } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 31566675df31SOmar Sandoval !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) { 31576675df31SOmar Sandoval btrfs_warn(fs_info, "free space tree is invalid"); 31586675df31SOmar Sandoval clear_free_space_tree = 1; 31596675df31SOmar Sandoval } 31606675df31SOmar Sandoval 31616675df31SOmar Sandoval if (clear_free_space_tree) { 3162f8d468a1SOmar Sandoval btrfs_info(fs_info, "clearing free space tree"); 3163f8d468a1SOmar Sandoval ret = btrfs_clear_free_space_tree(fs_info); 3164f8d468a1SOmar Sandoval if (ret) { 3165f8d468a1SOmar Sandoval btrfs_warn(fs_info, 3166f8d468a1SOmar Sandoval "failed to clear free space tree: %d", ret); 31676bccf3abSJeff Mahoney close_ctree(fs_info); 3168f8d468a1SOmar Sandoval return ret; 3169f8d468a1SOmar Sandoval } 3170f8d468a1SOmar Sandoval } 3171f8d468a1SOmar Sandoval 31720b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && 3173511711afSChris Mason !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 317405135f59SDavid Sterba btrfs_info(fs_info, "creating free space tree"); 3175511711afSChris Mason ret = btrfs_create_free_space_tree(fs_info); 3176511711afSChris Mason if (ret) { 317705135f59SDavid Sterba btrfs_warn(fs_info, 317805135f59SDavid Sterba "failed to create free space tree: %d", ret); 31796bccf3abSJeff Mahoney close_ctree(fs_info); 3180511711afSChris Mason return ret; 3181511711afSChris Mason } 3182511711afSChris Mason } 3183511711afSChris Mason 3184e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 31852b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 31862b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 31872b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 31886bccf3abSJeff Mahoney close_ctree(fs_info); 31892b6ba629SIlya Dryomov return ret; 31902b6ba629SIlya Dryomov } 3191e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 319259641015SIlya Dryomov 31932b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 31942b6ba629SIlya Dryomov if (ret) { 319505135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume balance: %d", ret); 31966bccf3abSJeff Mahoney close_ctree(fs_info); 31972b6ba629SIlya Dryomov return ret; 3198e3acc2a6SJosef Bacik } 3199e3acc2a6SJosef Bacik 32008dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 32018dabb742SStefan Behrens if (ret) { 320205135f59SDavid Sterba btrfs_warn(fs_info, "failed to resume device replace: %d", ret); 32036bccf3abSJeff Mahoney close_ctree(fs_info); 32048dabb742SStefan Behrens return ret; 32058dabb742SStefan Behrens } 32068dabb742SStefan Behrens 3207b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 3208b382a324SJan Schmidt 32094bbcaa64SEric Sandeen if (!fs_info->uuid_root) { 321005135f59SDavid Sterba btrfs_info(fs_info, "creating UUID tree"); 3211f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 3212f7a81ea4SStefan Behrens if (ret) { 321305135f59SDavid Sterba btrfs_warn(fs_info, 321405135f59SDavid Sterba "failed to create the UUID tree: %d", ret); 32156bccf3abSJeff Mahoney close_ctree(fs_info); 3216f7a81ea4SStefan Behrens return ret; 3217f7a81ea4SStefan Behrens } 32180b246afaSJeff Mahoney } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) || 32194bbcaa64SEric Sandeen fs_info->generation != 32204bbcaa64SEric Sandeen btrfs_super_uuid_tree_generation(disk_super)) { 322105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree"); 322270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 322370f80175SStefan Behrens if (ret) { 322405135f59SDavid Sterba btrfs_warn(fs_info, 322505135f59SDavid Sterba "failed to check the UUID tree: %d", ret); 32266bccf3abSJeff Mahoney close_ctree(fs_info); 322770f80175SStefan Behrens return ret; 322870f80175SStefan Behrens } 322970f80175SStefan Behrens } else { 3230afcdd129SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); 3231f7a81ea4SStefan Behrens } 3232afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 323347ab2a6cSJosef Bacik 32348dcddfa0SQu Wenruo /* 32358dcddfa0SQu Wenruo * backuproot only affect mount behavior, and if open_ctree succeeded, 32368dcddfa0SQu Wenruo * no need to keep the flag 32378dcddfa0SQu Wenruo */ 32388dcddfa0SQu Wenruo btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT); 32398dcddfa0SQu Wenruo 3240ad2b2c80SAl Viro return 0; 324139279cc3SChris Mason 3242bcef60f2SArne Jansen fail_qgroup: 3243bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 32447c2ca468SChris Mason fail_trans_kthread: 32457c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 32462ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 3247faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 32483f157a2fSChris Mason fail_cleaner: 3249a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 32507c2ca468SChris Mason 32517c2ca468SChris Mason /* 32527c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 32537c2ca468SChris Mason * kthreads 32547c2ca468SChris Mason */ 32557c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 32567c2ca468SChris Mason 32572365dd3cSAnand Jain fail_sysfs: 32586618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 32592365dd3cSAnand Jain 3260b7c35e81SAnand Jain fail_fsdev_sysfs: 3261b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3262b7c35e81SAnand Jain 32631b1d1f66SJosef Bacik fail_block_groups: 326454067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 32651b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 3266af31f5e5SChris Mason 3267af31f5e5SChris Mason fail_tree_roots: 3268af31f5e5SChris Mason free_root_pointers(fs_info, 1); 32692b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3270af31f5e5SChris Mason 327139279cc3SChris Mason fail_sb_buffer: 32727abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 327316cdcec7SMiao Xie fail_alloc: 32744543df7eSChris Mason fail_iput: 3275586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3276586e46e2SIlya Dryomov 32774543df7eSChris Mason iput(fs_info->btree_inode); 3278c404e0dcSMiao Xie fail_bio_counter: 3279c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3280963d678bSMiao Xie fail_delalloc_bytes: 3281963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3282e2d84521SMiao Xie fail_dirty_metadata_bytes: 3283e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3284ad081f14SJens Axboe fail_bdi: 32857e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 328676dda93cSYan, Zheng fail_srcu: 328776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 32887e662854SQinghuang Feng fail: 328953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3290586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3291ad2b2c80SAl Viro return err; 3292af31f5e5SChris Mason 3293af31f5e5SChris Mason recovery_tree_root: 32940b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USEBACKUPROOT)) 3295af31f5e5SChris Mason goto fail_tree_roots; 3296af31f5e5SChris Mason 3297af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3298af31f5e5SChris Mason 3299af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3300af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3301af31f5e5SChris Mason 3302af31f5e5SChris Mason /* we can't trust the free space cache either */ 3303af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3304af31f5e5SChris Mason 3305af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3306af31f5e5SChris Mason &num_backups_tried, &backup_index); 3307af31f5e5SChris Mason if (ret == -1) 3308af31f5e5SChris Mason goto fail_block_groups; 3309af31f5e5SChris Mason goto retry_root_backup; 3310eb60ceacSChris Mason } 3311eb60ceacSChris Mason 3312f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3313f2984462SChris Mason { 3314f2984462SChris Mason if (uptodate) { 3315f2984462SChris Mason set_buffer_uptodate(bh); 3316f2984462SChris Mason } else { 3317442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3318442a4f63SStefan Behrens bh->b_private; 3319442a4f63SStefan Behrens 3320fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info, 3321b14af3b4SDavid Sterba "lost page write due to IO error on %s", 3322606686eeSJosef Bacik rcu_str_deref(device->name)); 332301327610SNicholas D Steeves /* note, we don't set_buffer_write_io_error because we have 33241259ab75SChris Mason * our own ways of dealing with the IO errors 33251259ab75SChris Mason */ 3326f2984462SChris Mason clear_buffer_uptodate(bh); 3327442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3328f2984462SChris Mason } 3329f2984462SChris Mason unlock_buffer(bh); 3330f2984462SChris Mason put_bh(bh); 3331f2984462SChris Mason } 3332f2984462SChris Mason 333329c36d72SAnand Jain int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 333429c36d72SAnand Jain struct buffer_head **bh_ret) 333529c36d72SAnand Jain { 333629c36d72SAnand Jain struct buffer_head *bh; 333729c36d72SAnand Jain struct btrfs_super_block *super; 333829c36d72SAnand Jain u64 bytenr; 333929c36d72SAnand Jain 334029c36d72SAnand Jain bytenr = btrfs_sb_offset(copy_num); 334129c36d72SAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode)) 334229c36d72SAnand Jain return -EINVAL; 334329c36d72SAnand Jain 334429c36d72SAnand Jain bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE); 334529c36d72SAnand Jain /* 334629c36d72SAnand Jain * If we fail to read from the underlying devices, as of now 334729c36d72SAnand Jain * the best option we have is to mark it EIO. 334829c36d72SAnand Jain */ 334929c36d72SAnand Jain if (!bh) 335029c36d72SAnand Jain return -EIO; 335129c36d72SAnand Jain 335229c36d72SAnand Jain super = (struct btrfs_super_block *)bh->b_data; 335329c36d72SAnand Jain if (btrfs_super_bytenr(super) != bytenr || 335429c36d72SAnand Jain btrfs_super_magic(super) != BTRFS_MAGIC) { 335529c36d72SAnand Jain brelse(bh); 335629c36d72SAnand Jain return -EINVAL; 335729c36d72SAnand Jain } 335829c36d72SAnand Jain 335929c36d72SAnand Jain *bh_ret = bh; 336029c36d72SAnand Jain return 0; 336129c36d72SAnand Jain } 336229c36d72SAnand Jain 336329c36d72SAnand Jain 3364a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3365a512bbf8SYan Zheng { 3366a512bbf8SYan Zheng struct buffer_head *bh; 3367a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3368a512bbf8SYan Zheng struct btrfs_super_block *super; 3369a512bbf8SYan Zheng int i; 3370a512bbf8SYan Zheng u64 transid = 0; 337192fc03fbSAnand Jain int ret = -EINVAL; 3372a512bbf8SYan Zheng 3373a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3374a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3375a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3376a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3377a512bbf8SYan Zheng */ 3378a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 337929c36d72SAnand Jain ret = btrfs_read_dev_one_super(bdev, i, &bh); 338029c36d72SAnand Jain if (ret) 3381a512bbf8SYan Zheng continue; 3382a512bbf8SYan Zheng 3383a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3384a512bbf8SYan Zheng 3385a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3386a512bbf8SYan Zheng brelse(latest); 3387a512bbf8SYan Zheng latest = bh; 3388a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3389a512bbf8SYan Zheng } else { 3390a512bbf8SYan Zheng brelse(bh); 3391a512bbf8SYan Zheng } 3392a512bbf8SYan Zheng } 339392fc03fbSAnand Jain 339492fc03fbSAnand Jain if (!latest) 339592fc03fbSAnand Jain return ERR_PTR(ret); 339692fc03fbSAnand Jain 3397a512bbf8SYan Zheng return latest; 3398a512bbf8SYan Zheng } 3399a512bbf8SYan Zheng 34004eedeb75SHisashi Hifumi /* 34014eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 34024eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 34034eedeb75SHisashi Hifumi * we write are pinned. 34044eedeb75SHisashi Hifumi * 34054eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 34064eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 34074eedeb75SHisashi Hifumi * many supers on this one device should be written. 34084eedeb75SHisashi Hifumi * 34094eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 34104eedeb75SHisashi Hifumi */ 3411a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3412a512bbf8SYan Zheng struct btrfs_super_block *sb, 3413b75f5062SDavid Sterba int wait, int max_mirrors) 3414a512bbf8SYan Zheng { 3415a512bbf8SYan Zheng struct buffer_head *bh; 3416a512bbf8SYan Zheng int i; 3417a512bbf8SYan Zheng int ret; 3418a512bbf8SYan Zheng int errors = 0; 3419a512bbf8SYan Zheng u32 crc; 3420a512bbf8SYan Zheng u64 bytenr; 3421a512bbf8SYan Zheng 3422a512bbf8SYan Zheng if (max_mirrors == 0) 3423a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3424a512bbf8SYan Zheng 3425a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3426a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3427935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3428935e5cc9SMiao Xie device->commit_total_bytes) 3429a512bbf8SYan Zheng break; 3430a512bbf8SYan Zheng 3431a512bbf8SYan Zheng if (wait) { 3432a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3433a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3434634554dcSJosef Bacik if (!bh) { 3435634554dcSJosef Bacik errors++; 3436634554dcSJosef Bacik continue; 3437634554dcSJosef Bacik } 3438a512bbf8SYan Zheng wait_on_buffer(bh); 34394eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 34404eedeb75SHisashi Hifumi errors++; 34414eedeb75SHisashi Hifumi 34424eedeb75SHisashi Hifumi /* drop our reference */ 34434eedeb75SHisashi Hifumi brelse(bh); 34444eedeb75SHisashi Hifumi 34454eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3446a512bbf8SYan Zheng brelse(bh); 3447a512bbf8SYan Zheng continue; 3448a512bbf8SYan Zheng } else { 3449a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3450a512bbf8SYan Zheng 3451a512bbf8SYan Zheng crc = ~(u32)0; 3452b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3453a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3454a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3455a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3456a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3457a512bbf8SYan Zheng 34584eedeb75SHisashi Hifumi /* 34594eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 34604eedeb75SHisashi Hifumi * caller 34614eedeb75SHisashi Hifumi */ 3462a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3463a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3464634554dcSJosef Bacik if (!bh) { 3465fb456252SJeff Mahoney btrfs_err(device->fs_info, 3466f14d104dSDavid Sterba "couldn't get super buffer head for bytenr %llu", 3467f14d104dSDavid Sterba bytenr); 3468634554dcSJosef Bacik errors++; 3469634554dcSJosef Bacik continue; 3470634554dcSJosef Bacik } 3471634554dcSJosef Bacik 3472a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3473a512bbf8SYan Zheng 34744eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3475a512bbf8SYan Zheng get_bh(bh); 34764eedeb75SHisashi Hifumi 34774eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3478a512bbf8SYan Zheng lock_buffer(bh); 3479a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3480442a4f63SStefan Behrens bh->b_private = device; 3481a512bbf8SYan Zheng } 3482a512bbf8SYan Zheng 3483387125fcSChris Mason /* 3484387125fcSChris Mason * we fua the first super. The others we allow 3485387125fcSChris Mason * to go down lazy. 3486387125fcSChris Mason */ 3487e8117c26SWang Shilong if (i == 0) 348870fd7614SChristoph Hellwig ret = btrfsic_submit_bh(REQ_OP_WRITE, REQ_FUA, bh); 3489e8117c26SWang Shilong else 349070fd7614SChristoph Hellwig ret = btrfsic_submit_bh(REQ_OP_WRITE, REQ_SYNC, bh); 34914eedeb75SHisashi Hifumi if (ret) 3492a512bbf8SYan Zheng errors++; 3493a512bbf8SYan Zheng } 3494a512bbf8SYan Zheng return errors < i ? 0 : -1; 3495a512bbf8SYan Zheng } 3496a512bbf8SYan Zheng 3497387125fcSChris Mason /* 3498387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3499387125fcSChris Mason * for the barrier when it is done 3500387125fcSChris Mason */ 35014246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio) 3502387125fcSChris Mason { 3503387125fcSChris Mason if (bio->bi_private) 3504387125fcSChris Mason complete(bio->bi_private); 3505387125fcSChris Mason bio_put(bio); 3506387125fcSChris Mason } 3507387125fcSChris Mason 3508387125fcSChris Mason /* 3509387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3510387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3511387125fcSChris Mason * 3512387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3513387125fcSChris Mason * capable 3514387125fcSChris Mason */ 3515387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3516387125fcSChris Mason { 3517387125fcSChris Mason struct bio *bio; 3518387125fcSChris Mason int ret = 0; 3519387125fcSChris Mason 3520387125fcSChris Mason if (device->nobarriers) 3521387125fcSChris Mason return 0; 3522387125fcSChris Mason 3523387125fcSChris Mason if (wait) { 3524387125fcSChris Mason bio = device->flush_bio; 3525387125fcSChris Mason if (!bio) 3526387125fcSChris Mason return 0; 3527387125fcSChris Mason 3528387125fcSChris Mason wait_for_completion(&device->flush_wait); 3529387125fcSChris Mason 35304246a0b6SChristoph Hellwig if (bio->bi_error) { 35314246a0b6SChristoph Hellwig ret = bio->bi_error; 3532442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3533442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3534387125fcSChris Mason } 3535387125fcSChris Mason 3536387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3537387125fcSChris Mason bio_put(bio); 3538387125fcSChris Mason device->flush_bio = NULL; 3539387125fcSChris Mason 3540387125fcSChris Mason return ret; 3541387125fcSChris Mason } 3542387125fcSChris Mason 3543387125fcSChris Mason /* 3544387125fcSChris Mason * one reference for us, and we leave it for the 3545387125fcSChris Mason * caller 3546387125fcSChris Mason */ 35479c017abcSJesper Juhl device->flush_bio = NULL; 35489be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3549387125fcSChris Mason if (!bio) 3550387125fcSChris Mason return -ENOMEM; 3551387125fcSChris Mason 3552387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3553387125fcSChris Mason bio->bi_bdev = device->bdev; 355470fd7614SChristoph Hellwig bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; 3555387125fcSChris Mason init_completion(&device->flush_wait); 3556387125fcSChris Mason bio->bi_private = &device->flush_wait; 3557387125fcSChris Mason device->flush_bio = bio; 3558387125fcSChris Mason 3559387125fcSChris Mason bio_get(bio); 35604e49ea4aSMike Christie btrfsic_submit_bio(bio); 3561387125fcSChris Mason 3562387125fcSChris Mason return 0; 3563387125fcSChris Mason } 3564387125fcSChris Mason 3565387125fcSChris Mason /* 3566387125fcSChris Mason * send an empty flush down to each device in parallel, 3567387125fcSChris Mason * then wait for them 3568387125fcSChris Mason */ 3569387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3570387125fcSChris Mason { 3571387125fcSChris Mason struct list_head *head; 3572387125fcSChris Mason struct btrfs_device *dev; 35735af3e8ccSStefan Behrens int errors_send = 0; 35745af3e8ccSStefan Behrens int errors_wait = 0; 3575387125fcSChris Mason int ret; 3576387125fcSChris Mason 3577387125fcSChris Mason /* send down all the barriers */ 3578387125fcSChris Mason head = &info->fs_devices->devices; 3579387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3580f88ba6a2SHidetoshi Seto if (dev->missing) 3581f88ba6a2SHidetoshi Seto continue; 3582387125fcSChris Mason if (!dev->bdev) { 35835af3e8ccSStefan Behrens errors_send++; 3584387125fcSChris Mason continue; 3585387125fcSChris Mason } 3586387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3587387125fcSChris Mason continue; 3588387125fcSChris Mason 3589387125fcSChris Mason ret = write_dev_flush(dev, 0); 3590387125fcSChris Mason if (ret) 35915af3e8ccSStefan Behrens errors_send++; 3592387125fcSChris Mason } 3593387125fcSChris Mason 3594387125fcSChris Mason /* wait for all the barriers */ 3595387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3596f88ba6a2SHidetoshi Seto if (dev->missing) 3597f88ba6a2SHidetoshi Seto continue; 3598387125fcSChris Mason if (!dev->bdev) { 35995af3e8ccSStefan Behrens errors_wait++; 3600387125fcSChris Mason continue; 3601387125fcSChris Mason } 3602387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3603387125fcSChris Mason continue; 3604387125fcSChris Mason 3605387125fcSChris Mason ret = write_dev_flush(dev, 1); 3606387125fcSChris Mason if (ret) 36075af3e8ccSStefan Behrens errors_wait++; 3608387125fcSChris Mason } 36095af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 36105af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3611387125fcSChris Mason return -EIO; 3612387125fcSChris Mason return 0; 3613387125fcSChris Mason } 3614387125fcSChris Mason 3615943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags) 3616943c6e99SZhao Lei { 36178789f4feSZhao Lei int raid_type; 36188789f4feSZhao Lei int min_tolerated = INT_MAX; 3619943c6e99SZhao Lei 36208789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 || 36218789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE)) 36228789f4feSZhao Lei min_tolerated = min(min_tolerated, 36238789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE]. 36248789f4feSZhao Lei tolerated_failures); 3625943c6e99SZhao Lei 36268789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 36278789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE) 36288789f4feSZhao Lei continue; 36298789f4feSZhao Lei if (!(flags & btrfs_raid_group[raid_type])) 36308789f4feSZhao Lei continue; 36318789f4feSZhao Lei min_tolerated = min(min_tolerated, 36328789f4feSZhao Lei btrfs_raid_array[raid_type]. 36338789f4feSZhao Lei tolerated_failures); 36348789f4feSZhao Lei } 3635943c6e99SZhao Lei 36368789f4feSZhao Lei if (min_tolerated == INT_MAX) { 3637ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags); 36388789f4feSZhao Lei min_tolerated = 0; 36398789f4feSZhao Lei } 36408789f4feSZhao Lei 36418789f4feSZhao Lei return min_tolerated; 3642943c6e99SZhao Lei } 3643943c6e99SZhao Lei 36445af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 36455af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 36465af3e8ccSStefan Behrens { 36475af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 36485af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 36495af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 36505af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 36515af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 36525af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 36535af3e8ccSStefan Behrens int i; 36545af3e8ccSStefan Behrens int c; 36555af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 36565af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 36575af3e8ccSStefan Behrens 36582c458045SZhao Lei for (i = 0; i < ARRAY_SIZE(types); i++) { 36595af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 36605af3e8ccSStefan Behrens 36615af3e8ccSStefan Behrens sinfo = NULL; 36625af3e8ccSStefan Behrens rcu_read_lock(); 36635af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 36645af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 36655af3e8ccSStefan Behrens sinfo = tmp; 36665af3e8ccSStefan Behrens break; 36675af3e8ccSStefan Behrens } 36685af3e8ccSStefan Behrens } 36695af3e8ccSStefan Behrens rcu_read_unlock(); 36705af3e8ccSStefan Behrens 36715af3e8ccSStefan Behrens if (!sinfo) 36725af3e8ccSStefan Behrens continue; 36735af3e8ccSStefan Behrens 36745af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 36755af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 36765af3e8ccSStefan Behrens u64 flags; 36775af3e8ccSStefan Behrens 36782c458045SZhao Lei if (list_empty(&sinfo->block_groups[c])) 36792c458045SZhao Lei continue; 36802c458045SZhao Lei 36812c458045SZhao Lei btrfs_get_block_group_info(&sinfo->block_groups[c], 36822c458045SZhao Lei &space); 36832c458045SZhao Lei if (space.total_bytes == 0 || space.used_bytes == 0) 36845af3e8ccSStefan Behrens continue; 36855af3e8ccSStefan Behrens flags = space.flags; 3686943c6e99SZhao Lei 3687943c6e99SZhao Lei num_tolerated_disk_barrier_failures = min( 3688943c6e99SZhao Lei num_tolerated_disk_barrier_failures, 3689943c6e99SZhao Lei btrfs_get_num_tolerated_disk_barrier_failures( 3690943c6e99SZhao Lei flags)); 36915af3e8ccSStefan Behrens } 36925af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 36935af3e8ccSStefan Behrens } 36945af3e8ccSStefan Behrens 36955af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 36965af3e8ccSStefan Behrens } 36975af3e8ccSStefan Behrens 3698eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) 3699f2984462SChris Mason { 3700e5e9a520SChris Mason struct list_head *head; 3701f2984462SChris Mason struct btrfs_device *dev; 3702a061fc8dSChris Mason struct btrfs_super_block *sb; 3703f2984462SChris Mason struct btrfs_dev_item *dev_item; 3704f2984462SChris Mason int ret; 3705f2984462SChris Mason int do_barriers; 3706a236aed1SChris Mason int max_errors; 3707a236aed1SChris Mason int total_errors = 0; 3708a061fc8dSChris Mason u64 flags; 3709f2984462SChris Mason 37100b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER); 37110b246afaSJeff Mahoney backup_super_roots(fs_info); 3712f2984462SChris Mason 37130b246afaSJeff Mahoney sb = fs_info->super_for_commit; 3714a061fc8dSChris Mason dev_item = &sb->dev_item; 3715e5e9a520SChris Mason 37160b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex); 37170b246afaSJeff Mahoney head = &fs_info->fs_devices->devices; 37180b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1; 3719387125fcSChris Mason 37205af3e8ccSStefan Behrens if (do_barriers) { 37210b246afaSJeff Mahoney ret = barrier_all_devices(fs_info); 37225af3e8ccSStefan Behrens if (ret) { 37235af3e8ccSStefan Behrens mutex_unlock( 37240b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex); 37250b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 37265af3e8ccSStefan Behrens "errors while submitting device barriers."); 37275af3e8ccSStefan Behrens return ret; 37285af3e8ccSStefan Behrens } 37295af3e8ccSStefan Behrens } 3730387125fcSChris Mason 37311f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3732dfe25020SChris Mason if (!dev->bdev) { 3733dfe25020SChris Mason total_errors++; 3734dfe25020SChris Mason continue; 3735dfe25020SChris Mason } 37362b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3737dfe25020SChris Mason continue; 3738dfe25020SChris Mason 37392b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3740a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3741a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 37427df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3743935e5cc9SMiao Xie dev->commit_total_bytes); 3744ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3745ce7213c7SMiao Xie dev->commit_bytes_used); 3746a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3747a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3748a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3749a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 37502b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3751a512bbf8SYan Zheng 3752a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3753a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3754f2984462SChris Mason 3755b75f5062SDavid Sterba ret = write_dev_supers(dev, sb, 0, max_mirrors); 3756a236aed1SChris Mason if (ret) 3757a236aed1SChris Mason total_errors++; 3758f2984462SChris Mason } 3759a236aed1SChris Mason if (total_errors > max_errors) { 37600b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers", 3761d397712bSChris Mason total_errors); 37620b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 376379787eaaSJeff Mahoney 37649d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 37650b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37660b246afaSJeff Mahoney "%d errors while writing supers", 37670b246afaSJeff Mahoney total_errors); 37689d565ba4SStefan Behrens return -EIO; 3769a236aed1SChris Mason } 3770f2984462SChris Mason 3771a512bbf8SYan Zheng total_errors = 0; 37721f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3773dfe25020SChris Mason if (!dev->bdev) 3774dfe25020SChris Mason continue; 37752b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3776dfe25020SChris Mason continue; 3777dfe25020SChris Mason 3778b75f5062SDavid Sterba ret = write_dev_supers(dev, sb, 1, max_mirrors); 3779a512bbf8SYan Zheng if (ret) 37801259ab75SChris Mason total_errors++; 3781f2984462SChris Mason } 37820b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex); 3783a236aed1SChris Mason if (total_errors > max_errors) { 37840b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO, 37850b246afaSJeff Mahoney "%d errors while writing supers", 37860b246afaSJeff Mahoney total_errors); 378779787eaaSJeff Mahoney return -EIO; 3788a236aed1SChris Mason } 3789f2984462SChris Mason return 0; 3790f2984462SChris Mason } 3791f2984462SChris Mason 3792cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3793cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3794cb517eabSMiao Xie struct btrfs_root *root) 37952619ba1fSChris Mason { 37964df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 37972619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 37982619ba1fSChris Mason (unsigned long)root->root_key.objectid); 37994df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 380076dda93cSYan, Zheng 380176dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 380276dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 380376dda93cSYan, Zheng 38041c1ea4f7SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 38053321719eSLiu Bo btrfs_free_log(NULL, root); 38061c1ea4f7SLiu Bo if (root->reloc_root) { 38071c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->node); 38081c1ea4f7SLiu Bo free_extent_buffer(root->reloc_root->commit_root); 38091c1ea4f7SLiu Bo btrfs_put_fs_root(root->reloc_root); 38101c1ea4f7SLiu Bo root->reloc_root = NULL; 38111c1ea4f7SLiu Bo } 38121c1ea4f7SLiu Bo } 38133321719eSLiu Bo 3814faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3815581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3816faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3817581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 38184df27c4dSYan, Zheng free_fs_root(root); 38194df27c4dSYan, Zheng } 38204df27c4dSYan, Zheng 38214df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 38224df27c4dSYan, Zheng { 382357cdc8dbSDavid Sterba iput(root->ino_cache_inode); 38244df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 38252ff7e61eSJeff Mahoney btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv); 38261cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 38270ee5dc67SAl Viro if (root->anon_dev) 38280ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 38298257b2dcSMiao Xie if (root->subv_writers) 38308257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 38315f39d397SChris Mason free_extent_buffer(root->node); 38325f39d397SChris Mason free_extent_buffer(root->commit_root); 3833581bb050SLi Zefan kfree(root->free_ino_ctl); 3834581bb050SLi Zefan kfree(root->free_ino_pinned); 383558176a96SJosef Bacik kfree(root->name); 3836b0feb9d9SMiao Xie btrfs_put_fs_root(root); 38372619ba1fSChris Mason } 38382619ba1fSChris Mason 3839cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3840cb517eabSMiao Xie { 3841cb517eabSMiao Xie free_fs_root(root); 3842cfaa7295SChris Mason } 3843cfaa7295SChris Mason 3844c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3845c146afadSYan Zheng { 3846c146afadSYan Zheng u64 root_objectid = 0; 3847c146afadSYan Zheng struct btrfs_root *gang[8]; 384865d33fd7SQu Wenruo int i = 0; 384965d33fd7SQu Wenruo int err = 0; 385065d33fd7SQu Wenruo unsigned int ret = 0; 385165d33fd7SQu Wenruo int index; 3852c146afadSYan Zheng 3853c146afadSYan Zheng while (1) { 385465d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3855c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3856c146afadSYan Zheng (void **)gang, root_objectid, 3857c146afadSYan Zheng ARRAY_SIZE(gang)); 385865d33fd7SQu Wenruo if (!ret) { 385965d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3860c146afadSYan Zheng break; 386165d33fd7SQu Wenruo } 38625d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 386366b4ffd1SJosef Bacik 386465d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 386565d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 386665d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 386765d33fd7SQu Wenruo gang[i] = NULL; 386865d33fd7SQu Wenruo continue; 386965d33fd7SQu Wenruo } 387065d33fd7SQu Wenruo /* grab all the search result for later use */ 387165d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 387265d33fd7SQu Wenruo } 387365d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 387465d33fd7SQu Wenruo 387565d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 387665d33fd7SQu Wenruo if (!gang[i]) 387765d33fd7SQu Wenruo continue; 3878c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 387966b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 388066b4ffd1SJosef Bacik if (err) 388165d33fd7SQu Wenruo break; 388265d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3883c146afadSYan Zheng } 3884c146afadSYan Zheng root_objectid++; 3885c146afadSYan Zheng } 388665d33fd7SQu Wenruo 388765d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 388865d33fd7SQu Wenruo for (; i < ret; i++) { 388965d33fd7SQu Wenruo if (gang[i]) 389065d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 389165d33fd7SQu Wenruo } 389265d33fd7SQu Wenruo return err; 3893c146afadSYan Zheng } 3894c146afadSYan Zheng 38956bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info) 3896c146afadSYan Zheng { 38976bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3898c146afadSYan Zheng struct btrfs_trans_handle *trans; 3899c146afadSYan Zheng 39000b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 39012ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 39020b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 39030b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread); 3904c71bf099SYan, Zheng 3905c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 39060b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 39070b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 3908c71bf099SYan, Zheng 39097a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 39103612b495STsutomu Itoh if (IS_ERR(trans)) 39113612b495STsutomu Itoh return PTR_ERR(trans); 39123a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 3913c146afadSYan Zheng } 3914c146afadSYan Zheng 39156bccf3abSJeff Mahoney void close_ctree(struct btrfs_fs_info *fs_info) 3916eb60ceacSChris Mason { 39176bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 3918c146afadSYan Zheng int ret; 3919e089f05cSChris Mason 3920afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags); 3921a2135011SChris Mason 39227343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */ 3923d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false); 39247343dd61SJustin Maggard 3925803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3926803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3927803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3928803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3929803b2f54SStefan Behrens 3930837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3931aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3932837d5b6eSIlya Dryomov 39338dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 39348dabb742SStefan Behrens 3935aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 39364cb5300bSChris Mason 39374cb5300bSChris Mason /* wait for any defraggers to finish */ 39384cb5300bSChris Mason wait_event(fs_info->transaction_wait, 39394cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 39404cb5300bSChris Mason 39414cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 394226176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 39434cb5300bSChris Mason 394421c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 394521c7e756SMiao Xie 3946c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3947e44163e1SJeff Mahoney /* 3948e44163e1SJeff Mahoney * If the cleaner thread is stopped and there are 3949e44163e1SJeff Mahoney * block groups queued for removal, the deletion will be 3950e44163e1SJeff Mahoney * skipped when we quit the cleaner thread. 3951e44163e1SJeff Mahoney */ 39520b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info); 3953e44163e1SJeff Mahoney 39546bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 3955d397712bSChris Mason if (ret) 395604892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret); 3957c146afadSYan Zheng } 3958ed2ff2cbSChris Mason 395987533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 39602ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info); 3961acce952bSliubo 3962e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3963e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 39648929ecfaSYan, Zheng 3965afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags); 3966f25784b3SYan Zheng 396704892340SEric Sandeen btrfs_free_qgroup_config(fs_info); 3968bcef60f2SArne Jansen 3969963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 397004892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld", 3971963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3972b0c68f8bSChris Mason } 397331153d81SYan Zheng 39746618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 3975b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices); 39765ac1d209SJeff Mahoney 3977faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3978d10c5f31SChris Mason 39791a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 39801a4319ccSLiu Bo 39812b1360daSJosef Bacik btrfs_free_block_groups(fs_info); 39822b1360daSJosef Bacik 3983de348ee0SWang Shilong /* 3984de348ee0SWang Shilong * we must make sure there is not any read request to 3985de348ee0SWang Shilong * submit after we stopping all workers. 3986de348ee0SWang Shilong */ 3987de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 398896192499SJosef Bacik btrfs_stop_all_workers(fs_info); 398996192499SJosef Bacik 3990afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags); 399113e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 39929ad6b7bcSChris Mason 399313e6c37bSJosef Bacik iput(fs_info->btree_inode); 3994d6bfde87SChris Mason 399521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 39960b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 39972ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices); 399821adbd5cSStefan Behrens #endif 399921adbd5cSStefan Behrens 4000dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 40010b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 4002b248a415SChris Mason 4003e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 4004963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 4005c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 400604160088SChris Mason bdi_destroy(&fs_info->bdi); 400776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 40080b86a832SChris Mason 400953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 401053b381b3SDavid Woodhouse 4011cdfb080eSChris Mason __btrfs_free_block_rsv(root->orphan_block_rsv); 40121cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 401304216820SFilipe Manana 401434441361SDavid Sterba mutex_lock(&fs_info->chunk_mutex); 401504216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 401604216820SFilipe Manana struct extent_map *em; 401704216820SFilipe Manana 401804216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 401904216820SFilipe Manana struct extent_map, list); 402004216820SFilipe Manana list_del_init(&em->list); 402104216820SFilipe Manana free_extent_map(em); 402204216820SFilipe Manana } 402334441361SDavid Sterba mutex_unlock(&fs_info->chunk_mutex); 4024eb60ceacSChris Mason } 4025eb60ceacSChris Mason 4026b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4027b9fab919SChris Mason int atomic) 4028ccd467d6SChris Mason { 40291259ab75SChris Mason int ret; 4030727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 40311259ab75SChris Mason 40320b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 40331259ab75SChris Mason if (!ret) 40341259ab75SChris Mason return ret; 40351259ab75SChris Mason 40361259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 4037b9fab919SChris Mason parent_transid, atomic); 4038b9fab919SChris Mason if (ret == -EAGAIN) 4039b9fab919SChris Mason return ret; 40401259ab75SChris Mason return !ret; 40415f39d397SChris Mason } 40426702ed49SChris Mason 40435f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 40445f39d397SChris Mason { 40450b246afaSJeff Mahoney struct btrfs_fs_info *fs_info; 404606ea65a3SJosef Bacik struct btrfs_root *root; 40475f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 4048b9473439SChris Mason int was_dirty; 4049b4ce94deSChris Mason 405006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 405106ea65a3SJosef Bacik /* 405206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 405306ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 405406ea65a3SJosef Bacik * outside of the sanity tests. 405506ea65a3SJosef Bacik */ 405606ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 405706ea65a3SJosef Bacik return; 405806ea65a3SJosef Bacik #endif 405906ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 40600b246afaSJeff Mahoney fs_info = root->fs_info; 4061b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 40620b246afaSJeff Mahoney if (transid != fs_info->generation) 40635d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 40640b246afaSJeff Mahoney buf->start, transid, fs_info->generation); 40650b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 4066e2d84521SMiao Xie if (!was_dirty) 40670b246afaSJeff Mahoney __percpu_counter_add(&fs_info->dirty_metadata_bytes, 4068e2d84521SMiao Xie buf->len, 40690b246afaSJeff Mahoney fs_info->dirty_metadata_batch); 40701f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 40711f21ef0aSFilipe Manana if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 40722ff7e61eSJeff Mahoney btrfs_print_leaf(fs_info, buf); 40731f21ef0aSFilipe Manana ASSERT(0); 40741f21ef0aSFilipe Manana } 40751f21ef0aSFilipe Manana #endif 4076eb60ceacSChris Mason } 4077eb60ceacSChris Mason 40782ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info, 4079b53d3f5dSLiu Bo int flush_delayed) 408035b7e476SChris Mason { 4081188de649SChris Mason /* 4082188de649SChris Mason * looks as though older kernels can get into trouble with 4083188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 4084188de649SChris Mason */ 4085e2d84521SMiao Xie int ret; 4086d6bfde87SChris Mason 40876933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 4088d6bfde87SChris Mason return; 4089d6bfde87SChris Mason 4090b53d3f5dSLiu Bo if (flush_delayed) 40912ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info); 409216cdcec7SMiao Xie 40930b246afaSJeff Mahoney ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4094e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 4095e2d84521SMiao Xie if (ret > 0) { 40960b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping); 409716cdcec7SMiao Xie } 409816cdcec7SMiao Xie } 409916cdcec7SMiao Xie 41002ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info) 410116cdcec7SMiao Xie { 41022ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1); 410335b7e476SChris Mason } 4104b53d3f5dSLiu Bo 41052ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info) 4106b53d3f5dSLiu Bo { 41072ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0); 4108d6bfde87SChris Mason } 41096b80053dSChris Mason 4110ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 41116b80053dSChris Mason { 4112727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 41132ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41142ff7e61eSJeff Mahoney 41152ff7e61eSJeff Mahoney return btree_read_extent_buffer_pages(fs_info, buf, parent_transid); 41166b80053dSChris Mason } 41170da5468fSChris Mason 4118fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 4119acce952bSliubo int read_only) 4120acce952bSliubo { 4121c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 4122319e4d06SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb); 4123319e4d06SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb); 4124c926093eSDavid Sterba int ret = 0; 4125c926093eSDavid Sterba 4126319e4d06SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) { 4127ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "no valid FS found"); 4128319e4d06SQu Wenruo ret = -EINVAL; 4129319e4d06SQu Wenruo } 4130319e4d06SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) 4131ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "unrecognized super flag: %llu", 4132319e4d06SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP); 413321e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 4134ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "tree_root level too big: %d >= %d", 413521e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 4136c926093eSDavid Sterba ret = -EINVAL; 4137c926093eSDavid Sterba } 413821e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 4139ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "chunk_root level too big: %d >= %d", 414021e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 4141c926093eSDavid Sterba ret = -EINVAL; 4142c926093eSDavid Sterba } 414321e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 4144ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "log_root level too big: %d >= %d", 414521e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 4146c926093eSDavid Sterba ret = -EINVAL; 4147c926093eSDavid Sterba } 4148c926093eSDavid Sterba 41491104a885SDavid Sterba /* 4150319e4d06SQu Wenruo * Check sectorsize and nodesize first, other check will need it. 4151319e4d06SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here. 41521104a885SDavid Sterba */ 4153319e4d06SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 || 4154319e4d06SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4155ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize); 415675d6ad38SDavid Sterba ret = -EINVAL; 415775d6ad38SDavid Sterba } 4158319e4d06SQu Wenruo /* Only PAGE SIZE is supported yet */ 415909cbfeafSKirill A. Shutemov if (sectorsize != PAGE_SIZE) { 4160ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4161ab8d0fc4SJeff Mahoney "sectorsize %llu not supported yet, only support %lu", 416209cbfeafSKirill A. Shutemov sectorsize, PAGE_SIZE); 4163319e4d06SQu Wenruo ret = -EINVAL; 4164319e4d06SQu Wenruo } 4165319e4d06SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize || 4166319e4d06SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { 4167ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid nodesize %llu", nodesize); 4168319e4d06SQu Wenruo ret = -EINVAL; 4169319e4d06SQu Wenruo } 4170319e4d06SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) { 4171ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "invalid leafsize %u, should be %llu", 4172ab8d0fc4SJeff Mahoney le32_to_cpu(sb->__unused_leafsize), nodesize); 4173319e4d06SQu Wenruo ret = -EINVAL; 4174319e4d06SQu Wenruo } 4175319e4d06SQu Wenruo 4176319e4d06SQu Wenruo /* Root alignment check */ 4177319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) { 4178ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "tree_root block unaligned: %llu", 4179319e4d06SQu Wenruo btrfs_super_root(sb)); 4180319e4d06SQu Wenruo ret = -EINVAL; 4181319e4d06SQu Wenruo } 4182319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) { 4183ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "chunk_root block unaligned: %llu", 4184319e4d06SQu Wenruo btrfs_super_chunk_root(sb)); 4185319e4d06SQu Wenruo ret = -EINVAL; 4186319e4d06SQu Wenruo } 4187319e4d06SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) { 4188ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "log_root block unaligned: %llu", 4189319e4d06SQu Wenruo btrfs_super_log_root(sb)); 419075d6ad38SDavid Sterba ret = -EINVAL; 419175d6ad38SDavid Sterba } 419275d6ad38SDavid Sterba 4193c926093eSDavid Sterba if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) { 4194ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 4195ab8d0fc4SJeff Mahoney "dev_item UUID does not match fsid: %pU != %pU", 4196c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 4197c926093eSDavid Sterba ret = -EINVAL; 4198c926093eSDavid Sterba } 4199c926093eSDavid Sterba 4200c926093eSDavid Sterba /* 4201c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 4202c926093eSDavid Sterba * done later 4203c926093eSDavid Sterba */ 420499e3ecfcSLiu Bo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) { 420599e3ecfcSLiu Bo btrfs_err(fs_info, "bytes_used is too small %llu", 420699e3ecfcSLiu Bo btrfs_super_bytes_used(sb)); 420799e3ecfcSLiu Bo ret = -EINVAL; 420899e3ecfcSLiu Bo } 4209b7f67055SChandan Rajendra if (!is_power_of_2(btrfs_super_stripesize(sb))) { 421099e3ecfcSLiu Bo btrfs_err(fs_info, "invalid stripesize %u", 421199e3ecfcSLiu Bo btrfs_super_stripesize(sb)); 421299e3ecfcSLiu Bo ret = -EINVAL; 421399e3ecfcSLiu Bo } 421421e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 4215ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, "suspicious number of devices: %llu", 421621e7626bSDavid Sterba btrfs_super_num_devices(sb)); 421775d6ad38SDavid Sterba if (btrfs_super_num_devices(sb) == 0) { 4218ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "number of devices is 0"); 421975d6ad38SDavid Sterba ret = -EINVAL; 422075d6ad38SDavid Sterba } 4221c926093eSDavid Sterba 422221e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 4223ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "super offset mismatch %llu != %u", 422421e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 4225c926093eSDavid Sterba ret = -EINVAL; 4226c926093eSDavid Sterba } 4227c926093eSDavid Sterba 4228c926093eSDavid Sterba /* 4229ce7fca5fSDavid Sterba * Obvious sys_chunk_array corruptions, it must hold at least one key 4230ce7fca5fSDavid Sterba * and one chunk 4231ce7fca5fSDavid Sterba */ 4232ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4233ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too big %u > %u", 4234ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4235ce7fca5fSDavid Sterba BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 4236ce7fca5fSDavid Sterba ret = -EINVAL; 4237ce7fca5fSDavid Sterba } 4238ce7fca5fSDavid Sterba if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) 4239ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)) { 4240ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "system chunk array too small %u < %zu", 4241ce7fca5fSDavid Sterba btrfs_super_sys_array_size(sb), 4242ce7fca5fSDavid Sterba sizeof(struct btrfs_disk_key) 4243ce7fca5fSDavid Sterba + sizeof(struct btrfs_chunk)); 4244ce7fca5fSDavid Sterba ret = -EINVAL; 4245ce7fca5fSDavid Sterba } 4246ce7fca5fSDavid Sterba 4247ce7fca5fSDavid Sterba /* 4248c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 4249c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 4250c926093eSDavid Sterba */ 425121e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 4252ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4253ab8d0fc4SJeff Mahoney "suspicious: generation < chunk_root_generation: %llu < %llu", 4254ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4255ab8d0fc4SJeff Mahoney btrfs_super_chunk_root_generation(sb)); 425621e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 425721e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 4258ab8d0fc4SJeff Mahoney btrfs_warn(fs_info, 4259ab8d0fc4SJeff Mahoney "suspicious: generation < cache_generation: %llu < %llu", 4260ab8d0fc4SJeff Mahoney btrfs_super_generation(sb), 4261ab8d0fc4SJeff Mahoney btrfs_super_cache_generation(sb)); 4262c926093eSDavid Sterba 4263c926093eSDavid Sterba return ret; 4264fcd1f065SDavid Sterba } 4265fcd1f065SDavid Sterba 42662ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) 4267acce952bSliubo { 42680b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex); 42692ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 42700b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex); 4271acce952bSliubo 42720b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem); 42730b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem); 4274acce952bSliubo 4275acce952bSliubo /* cleanup FS via transaction */ 42762ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info); 4277acce952bSliubo } 4278acce952bSliubo 4279143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4280acce952bSliubo { 4281acce952bSliubo struct btrfs_ordered_extent *ordered; 4282acce952bSliubo 4283199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 4284779880efSJosef Bacik /* 4285779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 4286779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 4287779880efSJosef Bacik */ 4288199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 4289779880efSJosef Bacik root_extent_list) 4290779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 4291199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 4292199c2a9cSMiao Xie } 4293199c2a9cSMiao Xie 4294199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 4295199c2a9cSMiao Xie { 4296199c2a9cSMiao Xie struct btrfs_root *root; 4297199c2a9cSMiao Xie struct list_head splice; 4298199c2a9cSMiao Xie 4299199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 4300199c2a9cSMiao Xie 4301199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 4302199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 4303199c2a9cSMiao Xie while (!list_empty(&splice)) { 4304199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4305199c2a9cSMiao Xie ordered_root); 43061de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 43071de2cfdeSJosef Bacik &fs_info->ordered_roots); 4308199c2a9cSMiao Xie 43092a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 4310199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 4311199c2a9cSMiao Xie 43122a85d9caSLiu Bo cond_resched(); 43132a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 4314199c2a9cSMiao Xie } 4315199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 4316acce952bSliubo } 4317acce952bSliubo 431835a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 43192ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4320acce952bSliubo { 4321acce952bSliubo struct rb_node *node; 4322acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 4323acce952bSliubo struct btrfs_delayed_ref_node *ref; 4324acce952bSliubo int ret = 0; 4325acce952bSliubo 4326acce952bSliubo delayed_refs = &trans->delayed_refs; 4327acce952bSliubo 4328acce952bSliubo spin_lock(&delayed_refs->lock); 4329d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 4330cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 43310b246afaSJeff Mahoney btrfs_info(fs_info, "delayed_refs has NO entry"); 4332acce952bSliubo return ret; 4333acce952bSliubo } 4334acce952bSliubo 4335d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 4336d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 4337c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *tmp; 4338e78417d1SJosef Bacik bool pin_bytes = false; 4339acce952bSliubo 4340d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 4341d7df2c79SJosef Bacik href_node); 4342b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 4343d7df2c79SJosef Bacik atomic_inc(&head->node.refs); 434479787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 4345b939d1abSJosef Bacik 4346acce952bSliubo mutex_lock(&head->mutex); 4347b939d1abSJosef Bacik mutex_unlock(&head->mutex); 4348d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4349e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 4350b939d1abSJosef Bacik continue; 4351b939d1abSJosef Bacik } 4352d7df2c79SJosef Bacik spin_lock(&head->lock); 4353c6fc2454SQu Wenruo list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list, 4354c6fc2454SQu Wenruo list) { 4355d7df2c79SJosef Bacik ref->in_tree = 0; 4356c6fc2454SQu Wenruo list_del(&ref->list); 43571d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list)) 43581d57ee94SWang Xiaoguang list_del(&ref->add_list); 4359d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4360d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4361d7df2c79SJosef Bacik } 436254067ae9SJosef Bacik if (head->must_insert_reserved) 4363e78417d1SJosef Bacik pin_bytes = true; 436478a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4365acce952bSliubo delayed_refs->num_heads--; 4366d7df2c79SJosef Bacik if (head->processing == 0) 4367acce952bSliubo delayed_refs->num_heads_ready--; 4368d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4369d7df2c79SJosef Bacik head->node.in_tree = 0; 4370c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4371d7df2c79SJosef Bacik spin_unlock(&head->lock); 4372acce952bSliubo spin_unlock(&delayed_refs->lock); 4373e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4374acce952bSliubo 4375d7df2c79SJosef Bacik if (pin_bytes) 43762ff7e61eSJeff Mahoney btrfs_pin_extent(fs_info, head->node.bytenr, 4377d7df2c79SJosef Bacik head->node.num_bytes, 1); 4378d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4379acce952bSliubo cond_resched(); 4380acce952bSliubo spin_lock(&delayed_refs->lock); 4381acce952bSliubo } 4382acce952bSliubo 4383acce952bSliubo spin_unlock(&delayed_refs->lock); 4384acce952bSliubo 4385acce952bSliubo return ret; 4386acce952bSliubo } 4387acce952bSliubo 4388143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4389acce952bSliubo { 4390acce952bSliubo struct btrfs_inode *btrfs_inode; 4391acce952bSliubo struct list_head splice; 4392acce952bSliubo 4393acce952bSliubo INIT_LIST_HEAD(&splice); 4394acce952bSliubo 4395eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4396eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4397acce952bSliubo 4398acce952bSliubo while (!list_empty(&splice)) { 4399eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4400acce952bSliubo delalloc_inodes); 4401acce952bSliubo 4402acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4403df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4404df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4405eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4406acce952bSliubo 4407acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4408b216cbfbSMiao Xie 4409eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4410acce952bSliubo } 4411acce952bSliubo 4412eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4413eb73c1b7SMiao Xie } 4414eb73c1b7SMiao Xie 4415eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4416eb73c1b7SMiao Xie { 4417eb73c1b7SMiao Xie struct btrfs_root *root; 4418eb73c1b7SMiao Xie struct list_head splice; 4419eb73c1b7SMiao Xie 4420eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4421eb73c1b7SMiao Xie 4422eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4423eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4424eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4425eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4426eb73c1b7SMiao Xie delalloc_root); 4427eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4428eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4429eb73c1b7SMiao Xie BUG_ON(!root); 4430eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4431eb73c1b7SMiao Xie 4432eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4433eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4434eb73c1b7SMiao Xie 4435eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4436eb73c1b7SMiao Xie } 4437eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4438acce952bSliubo } 4439acce952bSliubo 44402ff7e61eSJeff Mahoney static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info, 4441acce952bSliubo struct extent_io_tree *dirty_pages, 4442acce952bSliubo int mark) 4443acce952bSliubo { 4444acce952bSliubo int ret; 4445acce952bSliubo struct extent_buffer *eb; 4446acce952bSliubo u64 start = 0; 4447acce952bSliubo u64 end; 4448acce952bSliubo 4449acce952bSliubo while (1) { 4450acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4451e6138876SJosef Bacik mark, NULL); 4452acce952bSliubo if (ret) 4453acce952bSliubo break; 4454acce952bSliubo 445591166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark); 4456acce952bSliubo while (start <= end) { 44570b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start); 44580b246afaSJeff Mahoney start += fs_info->nodesize; 4459fd8b2b61SJosef Bacik if (!eb) 4460acce952bSliubo continue; 4461fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4462acce952bSliubo 4463fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4464fd8b2b61SJosef Bacik &eb->bflags)) 4465fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4466fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4467acce952bSliubo } 4468acce952bSliubo } 4469acce952bSliubo 4470acce952bSliubo return ret; 4471acce952bSliubo } 4472acce952bSliubo 44732ff7e61eSJeff Mahoney static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info, 4474acce952bSliubo struct extent_io_tree *pinned_extents) 4475acce952bSliubo { 4476acce952bSliubo struct extent_io_tree *unpin; 4477acce952bSliubo u64 start; 4478acce952bSliubo u64 end; 4479acce952bSliubo int ret; 4480ed0eaa14SLiu Bo bool loop = true; 4481acce952bSliubo 4482acce952bSliubo unpin = pinned_extents; 4483ed0eaa14SLiu Bo again: 4484acce952bSliubo while (1) { 4485acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4486e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4487acce952bSliubo if (ret) 4488acce952bSliubo break; 4489acce952bSliubo 4490af6f8f60SDavid Sterba clear_extent_dirty(unpin, start, end); 44912ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end); 4492acce952bSliubo cond_resched(); 4493acce952bSliubo } 4494acce952bSliubo 4495ed0eaa14SLiu Bo if (loop) { 44960b246afaSJeff Mahoney if (unpin == &fs_info->freed_extents[0]) 44970b246afaSJeff Mahoney unpin = &fs_info->freed_extents[1]; 4498ed0eaa14SLiu Bo else 44990b246afaSJeff Mahoney unpin = &fs_info->freed_extents[0]; 4500ed0eaa14SLiu Bo loop = false; 4501ed0eaa14SLiu Bo goto again; 4502ed0eaa14SLiu Bo } 4503ed0eaa14SLiu Bo 4504acce952bSliubo return 0; 4505acce952bSliubo } 4506acce952bSliubo 4507c79a1751SLiu Bo static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache) 4508c79a1751SLiu Bo { 4509c79a1751SLiu Bo struct inode *inode; 4510c79a1751SLiu Bo 4511c79a1751SLiu Bo inode = cache->io_ctl.inode; 4512c79a1751SLiu Bo if (inode) { 4513c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping); 4514c79a1751SLiu Bo BTRFS_I(inode)->generation = 0; 4515c79a1751SLiu Bo cache->io_ctl.inode = NULL; 4516c79a1751SLiu Bo iput(inode); 4517c79a1751SLiu Bo } 4518c79a1751SLiu Bo btrfs_put_block_group(cache); 4519c79a1751SLiu Bo } 4520c79a1751SLiu Bo 4521c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 45222ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 4523c79a1751SLiu Bo { 4524c79a1751SLiu Bo struct btrfs_block_group_cache *cache; 4525c79a1751SLiu Bo 4526c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4527c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) { 4528c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs, 4529c79a1751SLiu Bo struct btrfs_block_group_cache, 4530c79a1751SLiu Bo dirty_list); 4531c79a1751SLiu Bo if (!cache) { 45320b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group dirty_bgs list"); 4533c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4534c79a1751SLiu Bo return; 4535c79a1751SLiu Bo } 4536c79a1751SLiu Bo 4537c79a1751SLiu Bo if (!list_empty(&cache->io_list)) { 4538c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4539c79a1751SLiu Bo list_del_init(&cache->io_list); 4540c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4541c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4542c79a1751SLiu Bo } 4543c79a1751SLiu Bo 4544c79a1751SLiu Bo list_del_init(&cache->dirty_list); 4545c79a1751SLiu Bo spin_lock(&cache->lock); 4546c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4547c79a1751SLiu Bo spin_unlock(&cache->lock); 4548c79a1751SLiu Bo 4549c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4550c79a1751SLiu Bo btrfs_put_block_group(cache); 4551c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock); 4552c79a1751SLiu Bo } 4553c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock); 4554c79a1751SLiu Bo 4555c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) { 4556c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs, 4557c79a1751SLiu Bo struct btrfs_block_group_cache, 4558c79a1751SLiu Bo io_list); 4559c79a1751SLiu Bo if (!cache) { 45600b246afaSJeff Mahoney btrfs_err(fs_info, "orphan block group on io_bgs list"); 4561c79a1751SLiu Bo return; 4562c79a1751SLiu Bo } 4563c79a1751SLiu Bo 4564c79a1751SLiu Bo list_del_init(&cache->io_list); 4565c79a1751SLiu Bo spin_lock(&cache->lock); 4566c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR; 4567c79a1751SLiu Bo spin_unlock(&cache->lock); 4568c79a1751SLiu Bo btrfs_cleanup_bg_io(cache); 4569c79a1751SLiu Bo } 4570c79a1751SLiu Bo } 4571c79a1751SLiu Bo 457249b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 45732ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 457449b25e05SJeff Mahoney { 45752ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 4576c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs)); 4577c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs)); 4578c79a1751SLiu Bo 45792ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info); 458049b25e05SJeff Mahoney 45814a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 45820b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 458349b25e05SJeff Mahoney 45844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 45850b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 458649b25e05SJeff Mahoney 4587ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4588ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 458967cde344SMiao Xie 45902ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, 459149b25e05SJeff Mahoney EXTENT_DIRTY); 45922ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, 45930b246afaSJeff Mahoney fs_info->pinned_extents); 459449b25e05SJeff Mahoney 45954a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 45964a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 45974a9d8bdeSMiao Xie 459849b25e05SJeff Mahoney /* 459949b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 460049b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 460149b25e05SJeff Mahoney */ 460249b25e05SJeff Mahoney } 460349b25e05SJeff Mahoney 46042ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info) 4605acce952bSliubo { 4606acce952bSliubo struct btrfs_transaction *t; 4607acce952bSliubo 46080b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex); 4609acce952bSliubo 46100b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46110b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) { 46120b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list, 4613724e2315SJosef Bacik struct btrfs_transaction, list); 4614724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 4615724e2315SJosef Bacik atomic_inc(&t->use_count); 46160b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46172ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid); 4618724e2315SJosef Bacik btrfs_put_transaction(t); 46190b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4620724e2315SJosef Bacik continue; 4621724e2315SJosef Bacik } 46220b246afaSJeff Mahoney if (t == fs_info->running_transaction) { 4623724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 46240b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4625724e2315SJosef Bacik /* 4626724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4627724e2315SJosef Bacik * handle open currently for this transaction. 4628724e2315SJosef Bacik */ 4629724e2315SJosef Bacik wait_event(t->writer_wait, 4630724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4631724e2315SJosef Bacik } else { 46320b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4633724e2315SJosef Bacik } 46342ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info); 4635724e2315SJosef Bacik 46360b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 46370b246afaSJeff Mahoney if (t == fs_info->running_transaction) 46380b246afaSJeff Mahoney fs_info->running_transaction = NULL; 4639724e2315SJosef Bacik list_del_init(&t->list); 46400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 4641a4abeea4SJosef Bacik 4642724e2315SJosef Bacik btrfs_put_transaction(t); 46432ff7e61eSJeff Mahoney trace_btrfs_transaction_commit(fs_info->tree_root); 46440b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4645724e2315SJosef Bacik } 46460b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 46470b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info); 4648ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info); 4649ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 46502ff7e61eSJeff Mahoney btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents); 46510b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info); 46520b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex); 4653acce952bSliubo 4654acce952bSliubo return 0; 4655acce952bSliubo } 4656acce952bSliubo 4657e8c9f186SDavid Sterba static const struct extent_io_ops btree_extent_io_ops = { 4658ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 46594bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 46600b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4661239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4662239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 46630da5468fSChris Mason }; 4664