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> 28a74a4b97SChris Mason #include <linux/freezer.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 30784b4e29SChris Mason #include <linux/migrate.h> 317a36ddecSDavid Sterba #include <linux/ratelimit.h> 326463fe58SStefan Behrens #include <linux/uuid.h> 33803b2f54SStefan Behrens #include <linux/semaphore.h> 347e75bf3fSDavid Sterba #include <asm/unaligned.h> 35eb60ceacSChris Mason #include "ctree.h" 36eb60ceacSChris Mason #include "disk-io.h" 370b947affSFilipe David Borba Manana #include "hash.h" 38e089f05cSChris Mason #include "transaction.h" 390f7d52f4SChris Mason #include "btrfs_inode.h" 400b86a832SChris Mason #include "volumes.h" 41db94535dSChris Mason #include "print-tree.h" 42925baeddSChris Mason #include "locking.h" 43e02119d5SChris Mason #include "tree-log.h" 44fa9c0d79SChris Mason #include "free-space-cache.h" 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" 52eb60ceacSChris Mason 53de0022b9SJosef Bacik #ifdef CONFIG_X86 54de0022b9SJosef Bacik #include <asm/cpufeature.h> 55de0022b9SJosef Bacik #endif 56de0022b9SJosef Bacik 57d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 588b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 594df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 60fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 61acce952bSliubo int read_only); 62143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 63acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 64acce952bSliubo struct btrfs_root *root); 65143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 66acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 67acce952bSliubo struct extent_io_tree *dirty_pages, 68acce952bSliubo int mark); 69acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 70acce952bSliubo struct extent_io_tree *pinned_extents); 7148a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root); 7248a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root); 73ce9adaa5SChris Mason 74d352ac68SChris Mason /* 7597eb6b69SDavid Sterba * btrfs_end_io_wq structs are used to do processing in task context when an IO 7697eb6b69SDavid Sterba * is complete. This is used during reads to verify checksums, and it is used 77d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 78d352ac68SChris Mason */ 7997eb6b69SDavid Sterba struct btrfs_end_io_wq { 80ce9adaa5SChris Mason struct bio *bio; 81ce9adaa5SChris Mason bio_end_io_t *end_io; 82ce9adaa5SChris Mason void *private; 83ce9adaa5SChris Mason struct btrfs_fs_info *info; 84ce9adaa5SChris Mason int error; 85bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata; 86ce9adaa5SChris Mason struct list_head list; 878b712842SChris Mason struct btrfs_work work; 88ce9adaa5SChris Mason }; 890da5468fSChris Mason 9097eb6b69SDavid Sterba static struct kmem_cache *btrfs_end_io_wq_cache; 9197eb6b69SDavid Sterba 9297eb6b69SDavid Sterba int __init btrfs_end_io_wq_init(void) 9397eb6b69SDavid Sterba { 9497eb6b69SDavid Sterba btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq", 9597eb6b69SDavid Sterba sizeof(struct btrfs_end_io_wq), 9697eb6b69SDavid Sterba 0, 9797eb6b69SDavid Sterba SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, 9897eb6b69SDavid Sterba NULL); 9997eb6b69SDavid Sterba if (!btrfs_end_io_wq_cache) 10097eb6b69SDavid Sterba return -ENOMEM; 10197eb6b69SDavid Sterba return 0; 10297eb6b69SDavid Sterba } 10397eb6b69SDavid Sterba 10497eb6b69SDavid Sterba void btrfs_end_io_wq_exit(void) 10597eb6b69SDavid Sterba { 10697eb6b69SDavid Sterba if (btrfs_end_io_wq_cache) 10797eb6b69SDavid Sterba kmem_cache_destroy(btrfs_end_io_wq_cache); 10897eb6b69SDavid Sterba } 10997eb6b69SDavid Sterba 110d352ac68SChris Mason /* 111d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 112d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 113d352ac68SChris Mason * just before they are sent down the IO stack. 114d352ac68SChris Mason */ 11544b8bd7eSChris Mason struct async_submit_bio { 11644b8bd7eSChris Mason struct inode *inode; 11744b8bd7eSChris Mason struct bio *bio; 11844b8bd7eSChris Mason struct list_head list; 1194a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1204a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 12144b8bd7eSChris Mason int rw; 12244b8bd7eSChris Mason int mirror_num; 123c8b97818SChris Mason unsigned long bio_flags; 124eaf25d93SChris Mason /* 125eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 126eaf25d93SChris Mason * can't tell us where in the file the bio should go 127eaf25d93SChris Mason */ 128eaf25d93SChris Mason u64 bio_offset; 1298b712842SChris Mason struct btrfs_work work; 13079787eaaSJeff Mahoney int error; 13144b8bd7eSChris Mason }; 13244b8bd7eSChris Mason 13385d4e461SChris Mason /* 13485d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 13585d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 13685d4e461SChris Mason * the level the eb occupies in the tree. 1374008c04aSChris Mason * 13885d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 13985d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 14085d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 14185d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 14285d4e461SChris Mason * have separate keysets. 1434008c04aSChris Mason * 14485d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 14585d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 14685d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1474008c04aSChris Mason * 14885d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 14985d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 15085d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 15185d4e461SChris Mason * 15285d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 15385d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 15485d4e461SChris Mason * needs update as well. 1554008c04aSChris Mason */ 1564008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1574008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1584008c04aSChris Mason # error 1594008c04aSChris Mason # endif 16085d4e461SChris Mason 16185d4e461SChris Mason static struct btrfs_lockdep_keyset { 16285d4e461SChris Mason u64 id; /* root objectid */ 16385d4e461SChris Mason const char *name_stem; /* lock name stem */ 16485d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 16585d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 16685d4e461SChris Mason } btrfs_lockdep_keysets[] = { 16785d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 16885d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 16985d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 17085d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 17185d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 17285d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 17360b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 17485d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 17585d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 17685d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 17713fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 17885d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1794008c04aSChris Mason }; 18085d4e461SChris Mason 18185d4e461SChris Mason void __init btrfs_init_lockdep(void) 18285d4e461SChris Mason { 18385d4e461SChris Mason int i, j; 18485d4e461SChris Mason 18585d4e461SChris Mason /* initialize lockdep class names */ 18685d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 18785d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 18885d4e461SChris Mason 18985d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 19085d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 19185d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 19285d4e461SChris Mason } 19385d4e461SChris Mason } 19485d4e461SChris Mason 19585d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 19685d4e461SChris Mason int level) 19785d4e461SChris Mason { 19885d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 19985d4e461SChris Mason 20085d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 20185d4e461SChris Mason 20285d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 20385d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 20485d4e461SChris Mason if (ks->id == objectid) 20585d4e461SChris Mason break; 20685d4e461SChris Mason 20785d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 20885d4e461SChris Mason &ks->keys[level], ks->names[level]); 20985d4e461SChris Mason } 21085d4e461SChris Mason 2114008c04aSChris Mason #endif 2124008c04aSChris Mason 213d352ac68SChris Mason /* 214d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 215d352ac68SChris Mason * that covers the entire device 216d352ac68SChris Mason */ 217b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 218306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2195f39d397SChris Mason int create) 2205f39d397SChris Mason { 2215f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2225f39d397SChris Mason struct extent_map *em; 2235f39d397SChris Mason int ret; 2245f39d397SChris Mason 225890871beSChris Mason read_lock(&em_tree->lock); 226d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 227a061fc8dSChris Mason if (em) { 228a061fc8dSChris Mason em->bdev = 229a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 230890871beSChris Mason read_unlock(&em_tree->lock); 2315f39d397SChris Mason goto out; 232a061fc8dSChris Mason } 233890871beSChris Mason read_unlock(&em_tree->lock); 2347b13b7b1SChris Mason 235172ddd60SDavid Sterba em = alloc_extent_map(); 2365f39d397SChris Mason if (!em) { 2375f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2385f39d397SChris Mason goto out; 2395f39d397SChris Mason } 2405f39d397SChris Mason em->start = 0; 2410afbaf8cSChris Mason em->len = (u64)-1; 242c8b97818SChris Mason em->block_len = (u64)-1; 2435f39d397SChris Mason em->block_start = 0; 244a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 245d1310b2eSChris Mason 246890871beSChris Mason write_lock(&em_tree->lock); 24709a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2485f39d397SChris Mason if (ret == -EEXIST) { 2495f39d397SChris Mason free_extent_map(em); 2507b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 251b4f359abSTsutomu Itoh if (!em) 2520433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2535f39d397SChris Mason } else if (ret) { 2547b13b7b1SChris Mason free_extent_map(em); 2550433f20dSTsutomu Itoh em = ERR_PTR(ret); 2565f39d397SChris Mason } 257890871beSChris Mason write_unlock(&em_tree->lock); 2587b13b7b1SChris Mason 2595f39d397SChris Mason out: 2605f39d397SChris Mason return em; 2615f39d397SChris Mason } 2625f39d397SChris Mason 263b0496686SLiu Bo u32 btrfs_csum_data(char *data, u32 seed, size_t len) 26419c00ddcSChris Mason { 2650b947affSFilipe David Borba Manana return btrfs_crc32c(seed, data, len); 26619c00ddcSChris Mason } 26719c00ddcSChris Mason 26819c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 26919c00ddcSChris Mason { 2707e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 27119c00ddcSChris Mason } 27219c00ddcSChris Mason 273d352ac68SChris Mason /* 274d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 275d352ac68SChris Mason * into the csum field of the block. 276d352ac68SChris Mason */ 27719c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 27819c00ddcSChris Mason int verify) 27919c00ddcSChris Mason { 2806c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 281607d432dSJosef Bacik char *result = NULL; 28219c00ddcSChris Mason unsigned long len; 28319c00ddcSChris Mason unsigned long cur_len; 28419c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 28519c00ddcSChris Mason char *kaddr; 28619c00ddcSChris Mason unsigned long map_start; 28719c00ddcSChris Mason unsigned long map_len; 28819c00ddcSChris Mason int err; 28919c00ddcSChris Mason u32 crc = ~(u32)0; 290607d432dSJosef Bacik unsigned long inline_result; 29119c00ddcSChris Mason 29219c00ddcSChris Mason len = buf->len - offset; 29319c00ddcSChris Mason while (len > 0) { 29419c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 295a6591715SChris Mason &kaddr, &map_start, &map_len); 296d397712bSChris Mason if (err) 29719c00ddcSChris Mason return 1; 29819c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 299b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 30019c00ddcSChris Mason crc, cur_len); 30119c00ddcSChris Mason len -= cur_len; 30219c00ddcSChris Mason offset += cur_len; 30319c00ddcSChris Mason } 304607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 305607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 306607d432dSJosef Bacik if (!result) 307607d432dSJosef Bacik return 1; 308607d432dSJosef Bacik } else { 309607d432dSJosef Bacik result = (char *)&inline_result; 310607d432dSJosef Bacik } 311607d432dSJosef Bacik 31219c00ddcSChris Mason btrfs_csum_final(crc, result); 31319c00ddcSChris Mason 31419c00ddcSChris Mason if (verify) { 315607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 316e4204dedSChris Mason u32 val; 317e4204dedSChris Mason u32 found = 0; 318607d432dSJosef Bacik memcpy(&found, result, csum_size); 319e4204dedSChris Mason 320607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 321efe120a0SFrank Holton printk_ratelimited(KERN_INFO 322efe120a0SFrank Holton "BTRFS: %s checksum verify failed on %llu wanted %X found %X " 323193f284dSChris Mason "level %d\n", 324c1c9ff7cSGeert Uytterhoeven root->fs_info->sb->s_id, buf->start, 325c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 326607d432dSJosef Bacik if (result != (char *)&inline_result) 327607d432dSJosef Bacik kfree(result); 32819c00ddcSChris Mason return 1; 32919c00ddcSChris Mason } 33019c00ddcSChris Mason } else { 331607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 33219c00ddcSChris Mason } 333607d432dSJosef Bacik if (result != (char *)&inline_result) 334607d432dSJosef Bacik kfree(result); 33519c00ddcSChris Mason return 0; 33619c00ddcSChris Mason } 33719c00ddcSChris Mason 338d352ac68SChris Mason /* 339d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 340d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 341d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 342d352ac68SChris Mason * in the wrong place. 343d352ac68SChris Mason */ 3441259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 345b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 346b9fab919SChris Mason int atomic) 3471259ab75SChris Mason { 3482ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3491259ab75SChris Mason int ret; 3502755a0deSDavid Sterba bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB); 3511259ab75SChris Mason 3521259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3531259ab75SChris Mason return 0; 3541259ab75SChris Mason 355b9fab919SChris Mason if (atomic) 356b9fab919SChris Mason return -EAGAIN; 357b9fab919SChris Mason 358a26e8c9fSJosef Bacik if (need_lock) { 359a26e8c9fSJosef Bacik btrfs_tree_read_lock(eb); 360a26e8c9fSJosef Bacik btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 361a26e8c9fSJosef Bacik } 362a26e8c9fSJosef Bacik 3632ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 364d0082371SJeff Mahoney 0, &cached_state); 3650b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3661259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3671259ab75SChris Mason ret = 0; 3681259ab75SChris Mason goto out; 3691259ab75SChris Mason } 37029549aecSWang Shilong printk_ratelimited(KERN_INFO "BTRFS (device %s): parent transid verify failed on %llu wanted %llu found %llu\n", 37129549aecSWang Shilong eb->fs_info->sb->s_id, eb->start, 37229549aecSWang Shilong parent_transid, btrfs_header_generation(eb)); 3731259ab75SChris Mason ret = 1; 374a26e8c9fSJosef Bacik 375a26e8c9fSJosef Bacik /* 376a26e8c9fSJosef Bacik * Things reading via commit roots that don't have normal protection, 377a26e8c9fSJosef Bacik * like send, can have a really old block in cache that may point at a 378a26e8c9fSJosef Bacik * block that has been free'd and re-allocated. So don't clear uptodate 379a26e8c9fSJosef Bacik * if we find an eb that is under IO (dirty/writeback) because we could 380a26e8c9fSJosef Bacik * end up reading in the stale data and then writing it back out and 381a26e8c9fSJosef Bacik * making everybody very sad. 382a26e8c9fSJosef Bacik */ 383a26e8c9fSJosef Bacik if (!extent_buffer_under_io(eb)) 3840b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 38533958dc6SChris Mason out: 3862ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3872ac55d41SJosef Bacik &cached_state, GFP_NOFS); 388472b909fSJosef Bacik if (need_lock) 389a26e8c9fSJosef Bacik btrfs_tree_read_unlock_blocking(eb); 3901259ab75SChris Mason return ret; 3911259ab75SChris Mason } 3921259ab75SChris Mason 393d352ac68SChris Mason /* 3941104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3951104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3961104a885SDavid Sterba */ 3971104a885SDavid Sterba static int btrfs_check_super_csum(char *raw_disk_sb) 3981104a885SDavid Sterba { 3991104a885SDavid Sterba struct btrfs_super_block *disk_sb = 4001104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 4011104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 4021104a885SDavid Sterba int ret = 0; 4031104a885SDavid Sterba 4041104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 4051104a885SDavid Sterba u32 crc = ~(u32)0; 4061104a885SDavid Sterba const int csum_size = sizeof(crc); 4071104a885SDavid Sterba char result[csum_size]; 4081104a885SDavid Sterba 4091104a885SDavid Sterba /* 4101104a885SDavid Sterba * The super_block structure does not span the whole 4111104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 4121104a885SDavid Sterba * is filled with zeros and is included in the checkum. 4131104a885SDavid Sterba */ 4141104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 4151104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 4161104a885SDavid Sterba btrfs_csum_final(crc, result); 4171104a885SDavid Sterba 4181104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 4191104a885SDavid Sterba ret = 1; 420667e7d94SChris Mason 421667e7d94SChris Mason if (ret && btrfs_super_generation(disk_sb) < 10) { 422efe120a0SFrank Holton printk(KERN_WARNING 423efe120a0SFrank Holton "BTRFS: super block crcs don't match, older mkfs detected\n"); 424667e7d94SChris Mason ret = 0; 425667e7d94SChris Mason } 4261104a885SDavid Sterba } 4271104a885SDavid Sterba 4281104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 429efe120a0SFrank Holton printk(KERN_ERR "BTRFS: unsupported checksum algorithm %u\n", 4301104a885SDavid Sterba csum_type); 4311104a885SDavid Sterba ret = 1; 4321104a885SDavid Sterba } 4331104a885SDavid Sterba 4341104a885SDavid Sterba return ret; 4351104a885SDavid Sterba } 4361104a885SDavid Sterba 4371104a885SDavid Sterba /* 438d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 439d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 440d352ac68SChris Mason */ 441f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 442f188591eSChris Mason struct extent_buffer *eb, 443ca7a79adSChris Mason u64 start, u64 parent_transid) 444f188591eSChris Mason { 445f188591eSChris Mason struct extent_io_tree *io_tree; 446ea466794SJosef Bacik int failed = 0; 447f188591eSChris Mason int ret; 448f188591eSChris Mason int num_copies = 0; 449f188591eSChris Mason int mirror_num = 0; 450ea466794SJosef Bacik int failed_mirror = 0; 451f188591eSChris Mason 452a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 453f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 454f188591eSChris Mason while (1) { 455bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 456bb82ab88SArne Jansen WAIT_COMPLETE, 457f188591eSChris Mason btree_get_extent, mirror_num); 458256dd1bbSStefan Behrens if (!ret) { 459256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 460b9fab919SChris Mason parent_transid, 0)) 461ea466794SJosef Bacik break; 462256dd1bbSStefan Behrens else 463256dd1bbSStefan Behrens ret = -EIO; 464256dd1bbSStefan Behrens } 465d397712bSChris Mason 466a826d6dcSJosef Bacik /* 467a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 468a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 469a826d6dcSJosef Bacik * any less wrong. 470a826d6dcSJosef Bacik */ 471a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 472ea466794SJosef Bacik break; 473ea466794SJosef Bacik 4745d964051SStefan Behrens num_copies = btrfs_num_copies(root->fs_info, 475f188591eSChris Mason eb->start, eb->len); 4764235298eSChris Mason if (num_copies == 1) 477ea466794SJosef Bacik break; 4784235298eSChris Mason 4795cf1ab56SJosef Bacik if (!failed_mirror) { 4805cf1ab56SJosef Bacik failed = 1; 4815cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4825cf1ab56SJosef Bacik } 4835cf1ab56SJosef Bacik 484f188591eSChris Mason mirror_num++; 485ea466794SJosef Bacik if (mirror_num == failed_mirror) 486ea466794SJosef Bacik mirror_num++; 487ea466794SJosef Bacik 4884235298eSChris Mason if (mirror_num > num_copies) 489ea466794SJosef Bacik break; 490f188591eSChris Mason } 491ea466794SJosef Bacik 492c0901581SStefan Behrens if (failed && !ret && failed_mirror) 493ea466794SJosef Bacik repair_eb_io_failure(root, eb, failed_mirror); 494ea466794SJosef Bacik 495ea466794SJosef Bacik return ret; 496f188591eSChris Mason } 49719c00ddcSChris Mason 498d352ac68SChris Mason /* 499d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 500d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 501d352ac68SChris Mason */ 502d397712bSChris Mason 503b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 50419c00ddcSChris Mason { 5054eee4fa4SMiao Xie u64 start = page_offset(page); 50619c00ddcSChris Mason u64 found_start; 50719c00ddcSChris Mason struct extent_buffer *eb; 508f188591eSChris Mason 5094f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 5104f2de97aSJosef Bacik if (page != eb->pages[0]) 5114f2de97aSJosef Bacik return 0; 51219c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 513fae7f21cSDulshani Gunawardhana if (WARN_ON(found_start != start || !PageUptodate(page))) 5144f2de97aSJosef Bacik return 0; 51519c00ddcSChris Mason csum_tree_block(root, eb, 0); 51619c00ddcSChris Mason return 0; 51719c00ddcSChris Mason } 51819c00ddcSChris Mason 5192b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 5202b82032cSYan Zheng struct extent_buffer *eb) 5212b82032cSYan Zheng { 5222b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 5232b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 5242b82032cSYan Zheng int ret = 1; 5252b82032cSYan Zheng 5260a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 5272b82032cSYan Zheng while (fs_devices) { 5282b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5292b82032cSYan Zheng ret = 0; 5302b82032cSYan Zheng break; 5312b82032cSYan Zheng } 5322b82032cSYan Zheng fs_devices = fs_devices->seed; 5332b82032cSYan Zheng } 5342b82032cSYan Zheng return ret; 5352b82032cSYan Zheng } 5362b82032cSYan Zheng 537a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 538efe120a0SFrank Holton btrfs_crit(root->fs_info, "corrupt leaf, %s: block=%llu," \ 539efe120a0SFrank Holton "root=%llu, slot=%d", reason, \ 540c1c9ff7cSGeert Uytterhoeven btrfs_header_bytenr(eb), root->objectid, slot) 541a826d6dcSJosef Bacik 542a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 543a826d6dcSJosef Bacik struct extent_buffer *leaf) 544a826d6dcSJosef Bacik { 545a826d6dcSJosef Bacik struct btrfs_key key; 546a826d6dcSJosef Bacik struct btrfs_key leaf_key; 547a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 548a826d6dcSJosef Bacik int slot; 549a826d6dcSJosef Bacik 550a826d6dcSJosef Bacik if (nritems == 0) 551a826d6dcSJosef Bacik return 0; 552a826d6dcSJosef Bacik 553a826d6dcSJosef Bacik /* Check the 0 item */ 554a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 555a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 556a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 557a826d6dcSJosef Bacik return -EIO; 558a826d6dcSJosef Bacik } 559a826d6dcSJosef Bacik 560a826d6dcSJosef Bacik /* 561a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 562a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 563a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 564a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 565a826d6dcSJosef Bacik * offset is correct. 566a826d6dcSJosef Bacik */ 567a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 568a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 569a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 570a826d6dcSJosef Bacik 571a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 572a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 573a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 574a826d6dcSJosef Bacik return -EIO; 575a826d6dcSJosef Bacik } 576a826d6dcSJosef Bacik 577a826d6dcSJosef Bacik /* 578a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 579a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 580a826d6dcSJosef Bacik * front. 581a826d6dcSJosef Bacik */ 582a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 583a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 584a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 585a826d6dcSJosef Bacik return -EIO; 586a826d6dcSJosef Bacik } 587a826d6dcSJosef Bacik 588a826d6dcSJosef Bacik /* 589a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 590a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 591a826d6dcSJosef Bacik * all point outside of the leaf. 592a826d6dcSJosef Bacik */ 593a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 594a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 595a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 596a826d6dcSJosef Bacik return -EIO; 597a826d6dcSJosef Bacik } 598a826d6dcSJosef Bacik } 599a826d6dcSJosef Bacik 600a826d6dcSJosef Bacik return 0; 601a826d6dcSJosef Bacik } 602a826d6dcSJosef Bacik 603facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 604facc8a22SMiao Xie u64 phy_offset, struct page *page, 605facc8a22SMiao Xie u64 start, u64 end, int mirror) 606ce9adaa5SChris Mason { 607ce9adaa5SChris Mason u64 found_start; 608ce9adaa5SChris Mason int found_level; 609ce9adaa5SChris Mason struct extent_buffer *eb; 610ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 611f188591eSChris Mason int ret = 0; 612727011e0SChris Mason int reads_done; 613ce9adaa5SChris Mason 614ce9adaa5SChris Mason if (!page->private) 615ce9adaa5SChris Mason goto out; 616d397712bSChris Mason 6174f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 618d397712bSChris Mason 6190b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 6200b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 6210b32f4bbSJosef Bacik */ 6220b32f4bbSJosef Bacik extent_buffer_get(eb); 6230b32f4bbSJosef Bacik 6240b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 625727011e0SChris Mason if (!reads_done) 626727011e0SChris Mason goto err; 627f188591eSChris Mason 6285cf1ab56SJosef Bacik eb->read_mirror = mirror; 629656f30dbSFilipe Manana if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) { 630ea466794SJosef Bacik ret = -EIO; 631ea466794SJosef Bacik goto err; 632ea466794SJosef Bacik } 633ea466794SJosef Bacik 634ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 635727011e0SChris Mason if (found_start != eb->start) { 63629549aecSWang Shilong printk_ratelimited(KERN_INFO "BTRFS (device %s): bad tree block start " 637193f284dSChris Mason "%llu %llu\n", 63829549aecSWang Shilong eb->fs_info->sb->s_id, found_start, eb->start); 639f188591eSChris Mason ret = -EIO; 640ce9adaa5SChris Mason goto err; 641ce9adaa5SChris Mason } 6422b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 64329549aecSWang Shilong printk_ratelimited(KERN_INFO "BTRFS (device %s): bad fsid on block %llu\n", 64429549aecSWang Shilong eb->fs_info->sb->s_id, eb->start); 6451259ab75SChris Mason ret = -EIO; 6461259ab75SChris Mason goto err; 6471259ab75SChris Mason } 648ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6491c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 650efe120a0SFrank Holton btrfs_info(root->fs_info, "bad tree block level %d", 6511c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6521c24c3ceSJosef Bacik ret = -EIO; 6531c24c3ceSJosef Bacik goto err; 6541c24c3ceSJosef Bacik } 655ce9adaa5SChris Mason 65685d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 65785d4e461SChris Mason eb, found_level); 6584008c04aSChris Mason 659ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 660a826d6dcSJosef Bacik if (ret) { 661f188591eSChris Mason ret = -EIO; 662a826d6dcSJosef Bacik goto err; 663a826d6dcSJosef Bacik } 664a826d6dcSJosef Bacik 665a826d6dcSJosef Bacik /* 666a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 667a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 668a826d6dcSJosef Bacik * return -EIO. 669a826d6dcSJosef Bacik */ 670a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 671a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 672a826d6dcSJosef Bacik ret = -EIO; 673a826d6dcSJosef Bacik } 674ce9adaa5SChris Mason 6750b32f4bbSJosef Bacik if (!ret) 6760b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 677ce9adaa5SChris Mason err: 67879fb65a1SJosef Bacik if (reads_done && 67979fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6804bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6814bb31e92SArne Jansen 68253b381b3SDavid Woodhouse if (ret) { 68353b381b3SDavid Woodhouse /* 68453b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 68553b381b3SDavid Woodhouse * again, we have to make sure it has something 68653b381b3SDavid Woodhouse * to decrement 68753b381b3SDavid Woodhouse */ 68853b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6890b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 69053b381b3SDavid Woodhouse } 6910b32f4bbSJosef Bacik free_extent_buffer(eb); 692ce9adaa5SChris Mason out: 693f188591eSChris Mason return ret; 694ce9adaa5SChris Mason } 695ce9adaa5SChris Mason 696ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6974bb31e92SArne Jansen { 6984bb31e92SArne Jansen struct extent_buffer *eb; 6994bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 7004bb31e92SArne Jansen 7014f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 702656f30dbSFilipe Manana set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); 7035cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 70453b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 705ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 7064bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 7074bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 7084bb31e92SArne Jansen } 7094bb31e92SArne Jansen 710ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 711ce9adaa5SChris Mason { 71297eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq = bio->bi_private; 713ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 7149e0af237SLiu Bo struct btrfs_workqueue *wq; 7159e0af237SLiu Bo btrfs_work_func_t func; 716ce9adaa5SChris Mason 717ce9adaa5SChris Mason fs_info = end_io_wq->info; 718ce9adaa5SChris Mason end_io_wq->error = err; 719d20f7043SChris Mason 7207b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 7219e0af237SLiu Bo if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) { 7229e0af237SLiu Bo wq = fs_info->endio_meta_write_workers; 7239e0af237SLiu Bo func = btrfs_endio_meta_write_helper; 7249e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) { 7259e0af237SLiu Bo wq = fs_info->endio_freespace_worker; 7269e0af237SLiu Bo func = btrfs_freespace_write_helper; 7279e0af237SLiu Bo } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7289e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7299e0af237SLiu Bo func = btrfs_endio_raid56_helper; 730d20f7043SChris Mason } else { 7319e0af237SLiu Bo wq = fs_info->endio_write_workers; 7329e0af237SLiu Bo func = btrfs_endio_write_helper; 733d20f7043SChris Mason } 7349e0af237SLiu Bo } else { 7358b110e39SMiao Xie if (unlikely(end_io_wq->metadata == 7368b110e39SMiao Xie BTRFS_WQ_ENDIO_DIO_REPAIR)) { 7378b110e39SMiao Xie wq = fs_info->endio_repair_workers; 7388b110e39SMiao Xie func = btrfs_endio_repair_helper; 7398b110e39SMiao Xie } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) { 7409e0af237SLiu Bo wq = fs_info->endio_raid56_workers; 7419e0af237SLiu Bo func = btrfs_endio_raid56_helper; 7429e0af237SLiu Bo } else if (end_io_wq->metadata) { 7439e0af237SLiu Bo wq = fs_info->endio_meta_workers; 7449e0af237SLiu Bo func = btrfs_endio_meta_helper; 7459e0af237SLiu Bo } else { 7469e0af237SLiu Bo wq = fs_info->endio_workers; 7479e0af237SLiu Bo func = btrfs_endio_helper; 7489e0af237SLiu Bo } 7499e0af237SLiu Bo } 7509e0af237SLiu Bo 7519e0af237SLiu Bo btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL); 7529e0af237SLiu Bo btrfs_queue_work(wq, &end_io_wq->work); 753ce9adaa5SChris Mason } 754ce9adaa5SChris Mason 75522c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 756bfebd8b5SDavid Sterba enum btrfs_wq_endio_type metadata) 7570b86a832SChris Mason { 75897eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 7598b110e39SMiao Xie 76097eb6b69SDavid Sterba end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS); 761ce9adaa5SChris Mason if (!end_io_wq) 762ce9adaa5SChris Mason return -ENOMEM; 763ce9adaa5SChris Mason 764ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 765ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 76622c59948SChris Mason end_io_wq->info = info; 767ce9adaa5SChris Mason end_io_wq->error = 0; 768ce9adaa5SChris Mason end_io_wq->bio = bio; 76922c59948SChris Mason end_io_wq->metadata = metadata; 770ce9adaa5SChris Mason 771ce9adaa5SChris Mason bio->bi_private = end_io_wq; 772ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 77322c59948SChris Mason return 0; 77422c59948SChris Mason } 77522c59948SChris Mason 776b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7774854ddd0SChris Mason { 7784854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7795cdc7ad3SQu Wenruo info->thread_pool_size, 7804854ddd0SChris Mason info->fs_devices->open_devices); 7814854ddd0SChris Mason return 256 * limit; 7824854ddd0SChris Mason } 7834854ddd0SChris Mason 7844a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7854a69a410SChris Mason { 7864a69a410SChris Mason struct async_submit_bio *async; 78779787eaaSJeff Mahoney int ret; 7884a69a410SChris Mason 7894a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 79079787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 791eaf25d93SChris Mason async->mirror_num, async->bio_flags, 792eaf25d93SChris Mason async->bio_offset); 79379787eaaSJeff Mahoney if (ret) 79479787eaaSJeff Mahoney async->error = ret; 7954a69a410SChris Mason } 7964a69a410SChris Mason 7974a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7988b712842SChris Mason { 7998b712842SChris Mason struct btrfs_fs_info *fs_info; 8008b712842SChris Mason struct async_submit_bio *async; 8014854ddd0SChris Mason int limit; 8028b712842SChris Mason 8038b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 8048b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 8054854ddd0SChris Mason 806b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 8074854ddd0SChris Mason limit = limit * 2 / 3; 8084854ddd0SChris Mason 80966657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 810b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 8114854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 8124854ddd0SChris Mason 81379787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 81479787eaaSJeff Mahoney if (async->error) { 81579787eaaSJeff Mahoney bio_endio(async->bio, async->error); 81679787eaaSJeff Mahoney return; 81779787eaaSJeff Mahoney } 81879787eaaSJeff Mahoney 8194a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 820eaf25d93SChris Mason async->mirror_num, async->bio_flags, 821eaf25d93SChris Mason async->bio_offset); 8224a69a410SChris Mason } 8234a69a410SChris Mason 8244a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8254a69a410SChris Mason { 8264a69a410SChris Mason struct async_submit_bio *async; 8274a69a410SChris Mason 8284a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8298b712842SChris Mason kfree(async); 8308b712842SChris Mason } 8318b712842SChris Mason 83244b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 83344b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 834c8b97818SChris Mason unsigned long bio_flags, 835eaf25d93SChris Mason u64 bio_offset, 8364a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 8374a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 83844b8bd7eSChris Mason { 83944b8bd7eSChris Mason struct async_submit_bio *async; 84044b8bd7eSChris Mason 84144b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 84244b8bd7eSChris Mason if (!async) 84344b8bd7eSChris Mason return -ENOMEM; 84444b8bd7eSChris Mason 84544b8bd7eSChris Mason async->inode = inode; 84644b8bd7eSChris Mason async->rw = rw; 84744b8bd7eSChris Mason async->bio = bio; 84844b8bd7eSChris Mason async->mirror_num = mirror_num; 8494a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8504a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8514a69a410SChris Mason 8529e0af237SLiu Bo btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start, 8535cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 8544a69a410SChris Mason 855c8b97818SChris Mason async->bio_flags = bio_flags; 856eaf25d93SChris Mason async->bio_offset = bio_offset; 8578c8bee1dSChris Mason 85879787eaaSJeff Mahoney async->error = 0; 85979787eaaSJeff Mahoney 860cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 861d313d7a3SChris Mason 8627b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 8635cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 864d313d7a3SChris Mason 8655cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 8669473f16cSChris Mason 867771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 868771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 869771ed689SChris Mason wait_event(fs_info->async_submit_wait, 870771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 871771ed689SChris Mason } 872771ed689SChris Mason 87344b8bd7eSChris Mason return 0; 87444b8bd7eSChris Mason } 87544b8bd7eSChris Mason 876ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 877ce3ed71aSChris Mason { 8782c30c71bSKent Overstreet struct bio_vec *bvec; 879ce3ed71aSChris Mason struct btrfs_root *root; 8802c30c71bSKent Overstreet int i, ret = 0; 881ce3ed71aSChris Mason 8822c30c71bSKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 883ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 88479787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 88579787eaaSJeff Mahoney if (ret) 88679787eaaSJeff Mahoney break; 887ce3ed71aSChris Mason } 8882c30c71bSKent Overstreet 88979787eaaSJeff Mahoney return ret; 890ce3ed71aSChris Mason } 891ce3ed71aSChris Mason 8924a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8934a69a410SChris Mason struct bio *bio, int mirror_num, 894eaf25d93SChris Mason unsigned long bio_flags, 895eaf25d93SChris Mason u64 bio_offset) 89622c59948SChris Mason { 8978b712842SChris Mason /* 8988b712842SChris Mason * when we're called for a write, we're already in the async 8995443be45SChris Mason * submission context. Just jump into btrfs_map_bio 9008b712842SChris Mason */ 90179787eaaSJeff Mahoney return btree_csum_one_bio(bio); 90222c59948SChris Mason } 90322c59948SChris Mason 9044a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 905eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 906eaf25d93SChris Mason u64 bio_offset) 9074a69a410SChris Mason { 90861891923SStefan Behrens int ret; 90961891923SStefan Behrens 9108b712842SChris Mason /* 9114a69a410SChris Mason * when we're called for a write, we're already in the async 9124a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 9138b712842SChris Mason */ 91461891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 91561891923SStefan Behrens if (ret) 91661891923SStefan Behrens bio_endio(bio, ret); 91761891923SStefan Behrens return ret; 9180b86a832SChris Mason } 9190b86a832SChris Mason 920de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 921de0022b9SJosef Bacik { 922de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 923de0022b9SJosef Bacik return 0; 924de0022b9SJosef Bacik #ifdef CONFIG_X86 925de0022b9SJosef Bacik if (cpu_has_xmm4_2) 926de0022b9SJosef Bacik return 0; 927de0022b9SJosef Bacik #endif 928de0022b9SJosef Bacik return 1; 929de0022b9SJosef Bacik } 930de0022b9SJosef Bacik 93144b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 932eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 933eaf25d93SChris Mason u64 bio_offset) 93444b8bd7eSChris Mason { 935de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 9364a69a410SChris Mason int ret; 937cad321adSChris Mason 9387b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 939cad321adSChris Mason /* 940cad321adSChris Mason * called for a read, do the setup so that checksum validation 941cad321adSChris Mason * can happen in the async kernel threads 942cad321adSChris Mason */ 943f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 944bfebd8b5SDavid Sterba bio, BTRFS_WQ_ENDIO_METADATA); 9451d4284bdSChris Mason if (ret) 94661891923SStefan Behrens goto out_w_error; 94761891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 9486f3577bdSChris Mason mirror_num, 0); 949de0022b9SJosef Bacik } else if (!async) { 950de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 951de0022b9SJosef Bacik if (ret) 95261891923SStefan Behrens goto out_w_error; 95361891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 954de0022b9SJosef Bacik mirror_num, 0); 95561891923SStefan Behrens } else { 956cad321adSChris Mason /* 95761891923SStefan Behrens * kthread helpers are used to submit writes so that 95861891923SStefan Behrens * checksumming can happen in parallel across all CPUs 959cad321adSChris Mason */ 96061891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 961c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 962eaf25d93SChris Mason bio_offset, 9634a69a410SChris Mason __btree_submit_bio_start, 9644a69a410SChris Mason __btree_submit_bio_done); 96544b8bd7eSChris Mason } 96644b8bd7eSChris Mason 96761891923SStefan Behrens if (ret) { 96861891923SStefan Behrens out_w_error: 96961891923SStefan Behrens bio_endio(bio, ret); 97061891923SStefan Behrens } 97161891923SStefan Behrens return ret; 97261891923SStefan Behrens } 97361891923SStefan Behrens 9743dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 975784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 976a6bc32b8SMel Gorman struct page *newpage, struct page *page, 977a6bc32b8SMel Gorman enum migrate_mode mode) 978784b4e29SChris Mason { 979784b4e29SChris Mason /* 980784b4e29SChris Mason * we can't safely write a btree page from here, 981784b4e29SChris Mason * we haven't done the locking hook 982784b4e29SChris Mason */ 983784b4e29SChris Mason if (PageDirty(page)) 984784b4e29SChris Mason return -EAGAIN; 985784b4e29SChris Mason /* 986784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 987784b4e29SChris Mason * We must have no buffers or drop them. 988784b4e29SChris Mason */ 989784b4e29SChris Mason if (page_has_private(page) && 990784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 991784b4e29SChris Mason return -EAGAIN; 992a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 993784b4e29SChris Mason } 9943dd1462eSJan Beulich #endif 995784b4e29SChris Mason 9960da5468fSChris Mason 9970da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9980da5468fSChris Mason struct writeback_control *wbc) 9990da5468fSChris Mason { 1000e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 1001e2d84521SMiao Xie int ret; 1002e2d84521SMiao Xie 1003d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 1004448d640bSChris Mason 1005448d640bSChris Mason if (wbc->for_kupdate) 1006448d640bSChris Mason return 0; 1007448d640bSChris Mason 1008e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 1009b9473439SChris Mason /* this is a bit racy, but that's ok */ 1010e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 1011e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 1012e2d84521SMiao Xie if (ret < 0) 1013793955bcSChris Mason return 0; 1014793955bcSChris Mason } 10150b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 10160da5468fSChris Mason } 10170da5468fSChris Mason 1018b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 10195f39d397SChris Mason { 1020d1310b2eSChris Mason struct extent_io_tree *tree; 1021d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10228ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 10235f39d397SChris Mason } 10245f39d397SChris Mason 102570dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10265f39d397SChris Mason { 102798509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 102898509cfcSChris Mason return 0; 10290c4e538bSDavid Sterba 1030f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1031d98237b3SChris Mason } 1032d98237b3SChris Mason 1033d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1034d47992f8SLukas Czerner unsigned int length) 1035d98237b3SChris Mason { 1036d1310b2eSChris Mason struct extent_io_tree *tree; 1037d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10385f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10395f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10409ad6b7bcSChris Mason if (PagePrivate(page)) { 1041efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1042efe120a0SFrank Holton "page private not zero on page %llu", 1043efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10449ad6b7bcSChris Mason ClearPagePrivate(page); 10459ad6b7bcSChris Mason set_page_private(page, 0); 10469ad6b7bcSChris Mason page_cache_release(page); 10479ad6b7bcSChris Mason } 1048d98237b3SChris Mason } 1049d98237b3SChris Mason 10500b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10510b32f4bbSJosef Bacik { 1052bb146eb2SJosef Bacik #ifdef DEBUG 10530b32f4bbSJosef Bacik struct extent_buffer *eb; 10540b32f4bbSJosef Bacik 10550b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10560b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10570b32f4bbSJosef Bacik BUG_ON(!eb); 10580b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10590b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10600b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1061bb146eb2SJosef Bacik #endif 10620b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10630b32f4bbSJosef Bacik } 10640b32f4bbSJosef Bacik 10657f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1066d98237b3SChris Mason .readpage = btree_readpage, 10670da5468fSChris Mason .writepages = btree_writepages, 10685f39d397SChris Mason .releasepage = btree_releasepage, 10695f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10705a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1071784b4e29SChris Mason .migratepage = btree_migratepage, 10725a92bc88SChris Mason #endif 10730b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1074d98237b3SChris Mason }; 1075123abc88SChris Mason 10766197d86eSDavid Sterba void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) 1077090d1875SChris Mason { 10785f39d397SChris Mason struct extent_buffer *buf = NULL; 10795f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1080090d1875SChris Mason 1081db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10825f39d397SChris Mason if (!buf) 10836197d86eSDavid Sterba return; 1084d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1085bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10865f39d397SChris Mason free_extent_buffer(buf); 1087090d1875SChris Mason } 1088090d1875SChris Mason 1089ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1090ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1091ab0fff03SArne Jansen { 1092ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1093ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1094ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1095ab0fff03SArne Jansen int ret; 1096ab0fff03SArne Jansen 1097ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1098ab0fff03SArne Jansen if (!buf) 1099ab0fff03SArne Jansen return 0; 1100ab0fff03SArne Jansen 1101ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1102ab0fff03SArne Jansen 1103ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1104ab0fff03SArne Jansen btree_get_extent, mirror_num); 1105ab0fff03SArne Jansen if (ret) { 1106ab0fff03SArne Jansen free_extent_buffer(buf); 1107ab0fff03SArne Jansen return ret; 1108ab0fff03SArne Jansen } 1109ab0fff03SArne Jansen 1110ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1111ab0fff03SArne Jansen free_extent_buffer(buf); 1112ab0fff03SArne Jansen return -EIO; 11130b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1114ab0fff03SArne Jansen *eb = buf; 1115ab0fff03SArne Jansen } else { 1116ab0fff03SArne Jansen free_extent_buffer(buf); 1117ab0fff03SArne Jansen } 1118ab0fff03SArne Jansen return 0; 1119ab0fff03SArne Jansen } 1120ab0fff03SArne Jansen 11210999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 11220308af44SDavid Sterba u64 bytenr) 11230999df54SChris Mason { 1124f28491e0SJosef Bacik return find_extent_buffer(root->fs_info, bytenr); 11250999df54SChris Mason } 11260999df54SChris Mason 11270999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 11280999df54SChris Mason u64 bytenr, u32 blocksize) 11290999df54SChris Mason { 1130fccb84c9SDavid Sterba if (btrfs_test_is_dummy_root(root)) 1131faa2dbf0SJosef Bacik return alloc_test_extent_buffer(root->fs_info, bytenr, 1132faa2dbf0SJosef Bacik blocksize); 1133f28491e0SJosef Bacik return alloc_extent_buffer(root->fs_info, bytenr, blocksize); 11340999df54SChris Mason } 11350999df54SChris Mason 11360999df54SChris Mason 1137e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1138e02119d5SChris Mason { 1139727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11408aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1141e02119d5SChris Mason } 1142e02119d5SChris Mason 1143e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1144e02119d5SChris Mason { 1145727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11468aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1147e02119d5SChris Mason } 1148e02119d5SChris Mason 1149db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1150ce86cd59SDavid Sterba u64 parent_transid) 1151e20d96d6SChris Mason { 11525f39d397SChris Mason struct extent_buffer *buf = NULL; 115319c00ddcSChris Mason int ret; 115419c00ddcSChris Mason 1155ce86cd59SDavid Sterba buf = btrfs_find_create_tree_block(root, bytenr, root->nodesize); 11565f39d397SChris Mason if (!buf) 1157d98237b3SChris Mason return NULL; 1158e4204dedSChris Mason 1159ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11600f0fe8f7SFilipe David Borba Manana if (ret) { 11610f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 11620f0fe8f7SFilipe David Borba Manana return NULL; 11630f0fe8f7SFilipe David Borba Manana } 11645f39d397SChris Mason return buf; 1165ce9adaa5SChris Mason 1166eb60ceacSChris Mason } 1167eb60ceacSChris Mason 1168d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11695f39d397SChris Mason struct extent_buffer *buf) 1170ed2ff2cbSChris Mason { 1171e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1172e2d84521SMiao Xie 117355c69072SChris Mason if (btrfs_header_generation(buf) == 1174e2d84521SMiao Xie fs_info->running_transaction->transid) { 1175b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1176b4ce94deSChris Mason 1177b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1178e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1179e2d84521SMiao Xie -buf->len, 1180e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1181b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1182b9473439SChris Mason btrfs_set_lock_blocking(buf); 11830b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1184925baeddSChris Mason } 11855f39d397SChris Mason } 1186ed7b63ebSJosef Bacik } 11875f39d397SChris Mason 11888257b2dcSMiao Xie static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void) 11898257b2dcSMiao Xie { 11908257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 11918257b2dcSMiao Xie int ret; 11928257b2dcSMiao Xie 11938257b2dcSMiao Xie writers = kmalloc(sizeof(*writers), GFP_NOFS); 11948257b2dcSMiao Xie if (!writers) 11958257b2dcSMiao Xie return ERR_PTR(-ENOMEM); 11968257b2dcSMiao Xie 1197908c7f19STejun Heo ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL); 11988257b2dcSMiao Xie if (ret < 0) { 11998257b2dcSMiao Xie kfree(writers); 12008257b2dcSMiao Xie return ERR_PTR(ret); 12018257b2dcSMiao Xie } 12028257b2dcSMiao Xie 12038257b2dcSMiao Xie init_waitqueue_head(&writers->wait); 12048257b2dcSMiao Xie return writers; 12058257b2dcSMiao Xie } 12068257b2dcSMiao Xie 12078257b2dcSMiao Xie static void 12088257b2dcSMiao Xie btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers) 12098257b2dcSMiao Xie { 12108257b2dcSMiao Xie percpu_counter_destroy(&writers->counter); 12118257b2dcSMiao Xie kfree(writers); 12128257b2dcSMiao Xie } 12138257b2dcSMiao Xie 1214707e8a07SDavid Sterba static void __setup_root(u32 nodesize, u32 sectorsize, u32 stripesize, 1215707e8a07SDavid Sterba struct btrfs_root *root, struct btrfs_fs_info *fs_info, 1216e20d96d6SChris Mason u64 objectid) 1217d97e63b6SChris Mason { 1218cfaa7295SChris Mason root->node = NULL; 1219a28ec197SChris Mason root->commit_root = NULL; 1220db94535dSChris Mason root->sectorsize = sectorsize; 1221db94535dSChris Mason root->nodesize = nodesize; 122287ee04ebSChris Mason root->stripesize = stripesize; 122327cdeb70SMiao Xie root->state = 0; 1224d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 12250b86a832SChris Mason 12260f7d52f4SChris Mason root->objectid = objectid; 12270f7d52f4SChris Mason root->last_trans = 0; 122813a8a7c8SYan, Zheng root->highest_objectid = 0; 1229eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1230199c2a9cSMiao Xie root->nr_ordered_extents = 0; 123158176a96SJosef Bacik root->name = NULL; 12326bef4d31SEric Paris root->inode_tree = RB_ROOT; 123316cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1234f0486c68SYan, Zheng root->block_rsv = NULL; 1235d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 12360b86a832SChris Mason 12370b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 12385d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1239eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1240eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1241199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1242199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 12432ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 12442ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1245d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 12465d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1247eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1248199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1249f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 12502ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 12512ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1252a2135011SChris Mason mutex_init(&root->objectid_mutex); 1253e02119d5SChris Mason mutex_init(&root->log_mutex); 125431f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex); 1255573bfb72SMiao Xie mutex_init(&root->delalloc_mutex); 12567237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12577237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12587237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12598b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 12608b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 12617237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12627237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12637237f183SYan Zheng atomic_set(&root->log_writers, 0); 12642ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12658a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1266b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 12678257b2dcSMiao Xie atomic_set(&root->will_be_snapshoted, 0); 12687237f183SYan Zheng root->log_transid = 0; 1269d1433debSMiao Xie root->log_transid_committed = -1; 1270257c62e1SChris Mason root->last_log_commit = 0; 127106ea65a3SJosef Bacik if (fs_info) 1272d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1273f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1274017e5369SChris Mason 12753768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12763768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12776702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 127858176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 127906ea65a3SJosef Bacik if (fs_info) 12803f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 128106ea65a3SJosef Bacik else 128206ea65a3SJosef Bacik root->defrag_trans_start = 0; 128358176a96SJosef Bacik init_completion(&root->kobj_unregister); 12844d775673SChris Mason root->root_key.objectid = objectid; 12850ee5dc67SAl Viro root->anon_dev = 0; 12868ea05e3aSAlexander Block 12875f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12883768f368SChris Mason } 12893768f368SChris Mason 1290f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12916f07e42eSAl Viro { 12926f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12936f07e42eSAl Viro if (root) 12946f07e42eSAl Viro root->fs_info = fs_info; 12956f07e42eSAl Viro return root; 12966f07e42eSAl Viro } 12976f07e42eSAl Viro 129806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 129906ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 130006ea65a3SJosef Bacik struct btrfs_root *btrfs_alloc_dummy_root(void) 130106ea65a3SJosef Bacik { 130206ea65a3SJosef Bacik struct btrfs_root *root; 130306ea65a3SJosef Bacik 130406ea65a3SJosef Bacik root = btrfs_alloc_root(NULL); 130506ea65a3SJosef Bacik if (!root) 130606ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 1307707e8a07SDavid Sterba __setup_root(4096, 4096, 4096, root, NULL, 1); 130827cdeb70SMiao Xie set_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state); 1309faa2dbf0SJosef Bacik root->alloc_bytenr = 0; 131006ea65a3SJosef Bacik 131106ea65a3SJosef Bacik return root; 131206ea65a3SJosef Bacik } 131306ea65a3SJosef Bacik #endif 131406ea65a3SJosef Bacik 131520897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 131620897f5cSArne Jansen struct btrfs_fs_info *fs_info, 131720897f5cSArne Jansen u64 objectid) 131820897f5cSArne Jansen { 131920897f5cSArne Jansen struct extent_buffer *leaf; 132020897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 132120897f5cSArne Jansen struct btrfs_root *root; 132220897f5cSArne Jansen struct btrfs_key key; 132320897f5cSArne Jansen int ret = 0; 13246463fe58SStefan Behrens uuid_le uuid; 132520897f5cSArne Jansen 132620897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 132720897f5cSArne Jansen if (!root) 132820897f5cSArne Jansen return ERR_PTR(-ENOMEM); 132920897f5cSArne Jansen 1330707e8a07SDavid Sterba __setup_root(tree_root->nodesize, tree_root->sectorsize, 1331707e8a07SDavid Sterba tree_root->stripesize, root, fs_info, objectid); 133220897f5cSArne Jansen root->root_key.objectid = objectid; 133320897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 133420897f5cSArne Jansen root->root_key.offset = 0; 133520897f5cSArne Jansen 13364d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 133720897f5cSArne Jansen if (IS_ERR(leaf)) { 133820897f5cSArne Jansen ret = PTR_ERR(leaf); 13391dd05682STsutomu Itoh leaf = NULL; 134020897f5cSArne Jansen goto fail; 134120897f5cSArne Jansen } 134220897f5cSArne Jansen 134320897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 134420897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 134520897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 134620897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 134720897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 134820897f5cSArne Jansen root->node = leaf; 134920897f5cSArne Jansen 13500a4e5586SRoss Kirk write_extent_buffer(leaf, fs_info->fsid, btrfs_header_fsid(), 135120897f5cSArne Jansen BTRFS_FSID_SIZE); 135220897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 1353b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 135420897f5cSArne Jansen BTRFS_UUID_SIZE); 135520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 135620897f5cSArne Jansen 135720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 135827cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state); 135920897f5cSArne Jansen 136020897f5cSArne Jansen root->root_item.flags = 0; 136120897f5cSArne Jansen root->root_item.byte_limit = 0; 136220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 136320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 136420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 136520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 136620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 136720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 136820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 13696463fe58SStefan Behrens uuid_le_gen(&uuid); 13706463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 137120897f5cSArne Jansen root->root_item.drop_level = 0; 137220897f5cSArne Jansen 137320897f5cSArne Jansen key.objectid = objectid; 137420897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 137520897f5cSArne Jansen key.offset = 0; 137620897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 137720897f5cSArne Jansen if (ret) 137820897f5cSArne Jansen goto fail; 137920897f5cSArne Jansen 138020897f5cSArne Jansen btrfs_tree_unlock(leaf); 138120897f5cSArne Jansen 138220897f5cSArne Jansen return root; 13831dd05682STsutomu Itoh 13841dd05682STsutomu Itoh fail: 13851dd05682STsutomu Itoh if (leaf) { 13861dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 138759885b39STsutomu Itoh free_extent_buffer(root->commit_root); 13881dd05682STsutomu Itoh free_extent_buffer(leaf); 13891dd05682STsutomu Itoh } 13901dd05682STsutomu Itoh kfree(root); 13911dd05682STsutomu Itoh 13921dd05682STsutomu Itoh return ERR_PTR(ret); 139320897f5cSArne Jansen } 139420897f5cSArne Jansen 13957237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1396e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13970f7d52f4SChris Mason { 13980f7d52f4SChris Mason struct btrfs_root *root; 13990f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 14007237f183SYan Zheng struct extent_buffer *leaf; 1401e02119d5SChris Mason 14026f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1403e02119d5SChris Mason if (!root) 14047237f183SYan Zheng return ERR_PTR(-ENOMEM); 1405e02119d5SChris Mason 1406707e8a07SDavid Sterba __setup_root(tree_root->nodesize, tree_root->sectorsize, 1407707e8a07SDavid Sterba tree_root->stripesize, root, fs_info, 1408707e8a07SDavid Sterba BTRFS_TREE_LOG_OBJECTID); 1409e02119d5SChris Mason 1410e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1411e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1412e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 141327cdeb70SMiao Xie 14147237f183SYan Zheng /* 141527cdeb70SMiao Xie * DON'T set REF_COWS for log trees 141627cdeb70SMiao Xie * 14177237f183SYan Zheng * log trees do not get reference counted because they go away 14187237f183SYan Zheng * before a real commit is actually done. They do store pointers 14197237f183SYan Zheng * to file data extents, and those reference counts still get 14207237f183SYan Zheng * updated (along with back refs to the log tree). 14217237f183SYan Zheng */ 1422e02119d5SChris Mason 14234d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID, 14244d75f8a9SDavid Sterba NULL, 0, 0, 0); 14257237f183SYan Zheng if (IS_ERR(leaf)) { 14267237f183SYan Zheng kfree(root); 14277237f183SYan Zheng return ERR_CAST(leaf); 14287237f183SYan Zheng } 1429e02119d5SChris Mason 14305d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 14315d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 14325d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 14335d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 14345d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 14357237f183SYan Zheng root->node = leaf; 1436e02119d5SChris Mason 1437e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 14380a4e5586SRoss Kirk btrfs_header_fsid(), BTRFS_FSID_SIZE); 1439e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1440e02119d5SChris Mason btrfs_tree_unlock(root->node); 14417237f183SYan Zheng return root; 14427237f183SYan Zheng } 14437237f183SYan Zheng 14447237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14457237f183SYan Zheng struct btrfs_fs_info *fs_info) 14467237f183SYan Zheng { 14477237f183SYan Zheng struct btrfs_root *log_root; 14487237f183SYan Zheng 14497237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14507237f183SYan Zheng if (IS_ERR(log_root)) 14517237f183SYan Zheng return PTR_ERR(log_root); 14527237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14537237f183SYan Zheng fs_info->log_root_tree = log_root; 14547237f183SYan Zheng return 0; 14557237f183SYan Zheng } 14567237f183SYan Zheng 14577237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14587237f183SYan Zheng struct btrfs_root *root) 14597237f183SYan Zheng { 14607237f183SYan Zheng struct btrfs_root *log_root; 14617237f183SYan Zheng struct btrfs_inode_item *inode_item; 14627237f183SYan Zheng 14637237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 14647237f183SYan Zheng if (IS_ERR(log_root)) 14657237f183SYan Zheng return PTR_ERR(log_root); 14667237f183SYan Zheng 14677237f183SYan Zheng log_root->last_trans = trans->transid; 14687237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14697237f183SYan Zheng 14707237f183SYan Zheng inode_item = &log_root->root_item.inode; 14713cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14723cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14733cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 1474707e8a07SDavid Sterba btrfs_set_stack_inode_nbytes(inode_item, root->nodesize); 14753cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14767237f183SYan Zheng 14775d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14787237f183SYan Zheng 14797237f183SYan Zheng WARN_ON(root->log_root); 14807237f183SYan Zheng root->log_root = log_root; 14817237f183SYan Zheng root->log_transid = 0; 1482d1433debSMiao Xie root->log_transid_committed = -1; 1483257c62e1SChris Mason root->last_log_commit = 0; 1484e02119d5SChris Mason return 0; 1485e02119d5SChris Mason } 1486e02119d5SChris Mason 148735a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1488cb517eabSMiao Xie struct btrfs_key *key) 1489e02119d5SChris Mason { 1490e02119d5SChris Mason struct btrfs_root *root; 1491e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14920f7d52f4SChris Mason struct btrfs_path *path; 149384234f3aSYan Zheng u64 generation; 1494cb517eabSMiao Xie int ret; 1495cb517eabSMiao Xie 1496cb517eabSMiao Xie path = btrfs_alloc_path(); 1497cb517eabSMiao Xie if (!path) 1498cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14990f7d52f4SChris Mason 15006f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1501cb517eabSMiao Xie if (!root) { 1502cb517eabSMiao Xie ret = -ENOMEM; 1503cb517eabSMiao Xie goto alloc_fail; 15040f7d52f4SChris Mason } 15050f7d52f4SChris Mason 1506707e8a07SDavid Sterba __setup_root(tree_root->nodesize, tree_root->sectorsize, 1507707e8a07SDavid Sterba tree_root->stripesize, root, fs_info, key->objectid); 15080f7d52f4SChris Mason 1509cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1510cb517eabSMiao Xie &root->root_item, &root->root_key); 15110f7d52f4SChris Mason if (ret) { 151213a8a7c8SYan, Zheng if (ret > 0) 151313a8a7c8SYan, Zheng ret = -ENOENT; 1514cb517eabSMiao Xie goto find_fail; 15150f7d52f4SChris Mason } 151613a8a7c8SYan, Zheng 151784234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1518db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1519ce86cd59SDavid Sterba generation); 1520cb517eabSMiao Xie if (!root->node) { 1521cb517eabSMiao Xie ret = -ENOMEM; 1522cb517eabSMiao Xie goto find_fail; 1523cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1524cb517eabSMiao Xie ret = -EIO; 1525cb517eabSMiao Xie goto read_fail; 1526416bc658SJosef Bacik } 15275d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 152813a8a7c8SYan, Zheng out: 1529cb517eabSMiao Xie btrfs_free_path(path); 1530cb517eabSMiao Xie return root; 1531cb517eabSMiao Xie 1532cb517eabSMiao Xie read_fail: 1533cb517eabSMiao Xie free_extent_buffer(root->node); 1534cb517eabSMiao Xie find_fail: 1535cb517eabSMiao Xie kfree(root); 1536cb517eabSMiao Xie alloc_fail: 1537cb517eabSMiao Xie root = ERR_PTR(ret); 1538cb517eabSMiao Xie goto out; 1539cb517eabSMiao Xie } 1540cb517eabSMiao Xie 1541cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1542cb517eabSMiao Xie struct btrfs_key *location) 1543cb517eabSMiao Xie { 1544cb517eabSMiao Xie struct btrfs_root *root; 1545cb517eabSMiao Xie 1546cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1547cb517eabSMiao Xie if (IS_ERR(root)) 1548cb517eabSMiao Xie return root; 1549cb517eabSMiao Xie 1550cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 155127cdeb70SMiao Xie set_bit(BTRFS_ROOT_REF_COWS, &root->state); 155208fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 155308fe4db1SLi Zefan } 155413a8a7c8SYan, Zheng 15555eda7b5eSChris Mason return root; 15565eda7b5eSChris Mason } 15575eda7b5eSChris Mason 1558cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1559cb517eabSMiao Xie { 1560cb517eabSMiao Xie int ret; 15618257b2dcSMiao Xie struct btrfs_subvolume_writers *writers; 1562cb517eabSMiao Xie 1563cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1564cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1565cb517eabSMiao Xie GFP_NOFS); 1566cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1567cb517eabSMiao Xie ret = -ENOMEM; 1568cb517eabSMiao Xie goto fail; 1569cb517eabSMiao Xie } 1570cb517eabSMiao Xie 15718257b2dcSMiao Xie writers = btrfs_alloc_subvolume_writers(); 15728257b2dcSMiao Xie if (IS_ERR(writers)) { 15738257b2dcSMiao Xie ret = PTR_ERR(writers); 15748257b2dcSMiao Xie goto fail; 15758257b2dcSMiao Xie } 15768257b2dcSMiao Xie root->subv_writers = writers; 15778257b2dcSMiao Xie 1578cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 157957cdc8dbSDavid Sterba spin_lock_init(&root->ino_cache_lock); 158057cdc8dbSDavid Sterba init_waitqueue_head(&root->ino_cache_wait); 1581cb517eabSMiao Xie 1582cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1583cb517eabSMiao Xie if (ret) 15848257b2dcSMiao Xie goto free_writers; 1585cb517eabSMiao Xie return 0; 15868257b2dcSMiao Xie 15878257b2dcSMiao Xie free_writers: 15888257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 1589cb517eabSMiao Xie fail: 1590cb517eabSMiao Xie kfree(root->free_ino_ctl); 1591cb517eabSMiao Xie kfree(root->free_ino_pinned); 1592cb517eabSMiao Xie return ret; 1593cb517eabSMiao Xie } 1594cb517eabSMiao Xie 1595171170c1SSergei Trofimovich static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1596cb517eabSMiao Xie u64 root_id) 1597cb517eabSMiao Xie { 1598cb517eabSMiao Xie struct btrfs_root *root; 1599cb517eabSMiao Xie 1600cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1601cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1602cb517eabSMiao Xie (unsigned long)root_id); 1603cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1604cb517eabSMiao Xie return root; 1605cb517eabSMiao Xie } 1606cb517eabSMiao Xie 1607cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1608cb517eabSMiao Xie struct btrfs_root *root) 1609cb517eabSMiao Xie { 1610cb517eabSMiao Xie int ret; 1611cb517eabSMiao Xie 1612cb517eabSMiao Xie ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 1613cb517eabSMiao Xie if (ret) 1614cb517eabSMiao Xie return ret; 1615cb517eabSMiao Xie 1616cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1617cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1618cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1619cb517eabSMiao Xie root); 1620cb517eabSMiao Xie if (ret == 0) 162127cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_RADIX, &root->state); 1622cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1623cb517eabSMiao Xie radix_tree_preload_end(); 1624cb517eabSMiao Xie 1625cb517eabSMiao Xie return ret; 1626cb517eabSMiao Xie } 1627cb517eabSMiao Xie 1628c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1629c00869f1SMiao Xie struct btrfs_key *location, 1630c00869f1SMiao Xie bool check_ref) 16315eda7b5eSChris Mason { 16325eda7b5eSChris Mason struct btrfs_root *root; 1633381cf658SDavid Sterba struct btrfs_path *path; 16345eda7b5eSChris Mason int ret; 16355eda7b5eSChris Mason 1636edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1637edbd8d4eSChris Mason return fs_info->tree_root; 1638edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1639edbd8d4eSChris Mason return fs_info->extent_root; 16408f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 16418f18cf13SChris Mason return fs_info->chunk_root; 16428f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 16438f18cf13SChris Mason return fs_info->dev_root; 16440403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 16450403e47eSYan Zheng return fs_info->csum_root; 1646bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1647bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1648bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1649f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1650f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1651f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 16524df27c4dSYan, Zheng again: 1653cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 165448475471SStefan Behrens if (root) { 1655c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 165648475471SStefan Behrens return ERR_PTR(-ENOENT); 16575eda7b5eSChris Mason return root; 165848475471SStefan Behrens } 16595eda7b5eSChris Mason 1660cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 16615eda7b5eSChris Mason if (IS_ERR(root)) 16625eda7b5eSChris Mason return root; 16633394e160SChris Mason 1664c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1665d68fc57bSYan, Zheng ret = -ENOENT; 1666d68fc57bSYan, Zheng goto fail; 1667d68fc57bSYan, Zheng } 1668d68fc57bSYan, Zheng 1669cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1670cb517eabSMiao Xie if (ret) 1671cb517eabSMiao Xie goto fail; 1672cb517eabSMiao Xie 1673381cf658SDavid Sterba path = btrfs_alloc_path(); 1674381cf658SDavid Sterba if (!path) { 1675381cf658SDavid Sterba ret = -ENOMEM; 1676381cf658SDavid Sterba goto fail; 1677381cf658SDavid Sterba } 1678381cf658SDavid Sterba ret = btrfs_find_item(fs_info->tree_root, path, BTRFS_ORPHAN_OBJECTID, 16793f870c28SKelley Nielsen location->objectid, BTRFS_ORPHAN_ITEM_KEY, NULL); 1680381cf658SDavid Sterba btrfs_free_path(path); 1681d68fc57bSYan, Zheng if (ret < 0) 1682d68fc57bSYan, Zheng goto fail; 1683d68fc57bSYan, Zheng if (ret == 0) 168427cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 1685d68fc57bSYan, Zheng 1686cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16870f7d52f4SChris Mason if (ret) { 16884df27c4dSYan, Zheng if (ret == -EEXIST) { 16894df27c4dSYan, Zheng free_fs_root(root); 16904df27c4dSYan, Zheng goto again; 16910f7d52f4SChris Mason } 16924df27c4dSYan, Zheng goto fail; 16934df27c4dSYan, Zheng } 1694edbd8d4eSChris Mason return root; 16954df27c4dSYan, Zheng fail: 16964df27c4dSYan, Zheng free_fs_root(root); 16974df27c4dSYan, Zheng return ERR_PTR(ret); 1698edbd8d4eSChris Mason } 1699edbd8d4eSChris Mason 170004160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 170104160088SChris Mason { 170204160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 170304160088SChris Mason int ret = 0; 170404160088SChris Mason struct btrfs_device *device; 170504160088SChris Mason struct backing_dev_info *bdi; 1706b7967db7SChris Mason 17071f78160cSXiao Guangrong rcu_read_lock(); 17081f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1709dfe25020SChris Mason if (!device->bdev) 1710dfe25020SChris Mason continue; 171104160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 1712ff9ea323STejun Heo if (bdi_congested(bdi, bdi_bits)) { 171304160088SChris Mason ret = 1; 171404160088SChris Mason break; 171504160088SChris Mason } 171604160088SChris Mason } 17171f78160cSXiao Guangrong rcu_read_unlock(); 171804160088SChris Mason return ret; 171904160088SChris Mason } 172004160088SChris Mason 172104160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 172204160088SChris Mason { 1723ad081f14SJens Axboe int err; 1724ad081f14SJens Axboe 1725ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1726e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1727ad081f14SJens Axboe if (err) 1728ad081f14SJens Axboe return err; 1729ad081f14SJens Axboe 17304575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 173104160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 173204160088SChris Mason bdi->congested_data = info; 173304160088SChris Mason return 0; 173404160088SChris Mason } 173504160088SChris Mason 17368b712842SChris Mason /* 17378b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 17388b712842SChris Mason * functions. This is where read checksum verification actually happens 17398b712842SChris Mason */ 17408b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1741ce9adaa5SChris Mason { 1742ce9adaa5SChris Mason struct bio *bio; 174397eb6b69SDavid Sterba struct btrfs_end_io_wq *end_io_wq; 1744ce9adaa5SChris Mason int error; 1745ce9adaa5SChris Mason 174697eb6b69SDavid Sterba end_io_wq = container_of(work, struct btrfs_end_io_wq, work); 1747ce9adaa5SChris Mason bio = end_io_wq->bio; 17488b712842SChris Mason 1749ce9adaa5SChris Mason error = end_io_wq->error; 1750ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1751ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 175297eb6b69SDavid Sterba kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq); 1753bc1e79acSKent Overstreet bio_endio_nodec(bio, error); 1754ce9adaa5SChris Mason } 175544b8bd7eSChris Mason 1756a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1757a74a4b97SChris Mason { 1758a74a4b97SChris Mason struct btrfs_root *root = arg; 1759d0278245SMiao Xie int again; 1760a74a4b97SChris Mason 1761a74a4b97SChris Mason do { 1762d0278245SMiao Xie again = 0; 17639d1a2a3aSDavid Sterba 1764d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 1765babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) 1766d0278245SMiao Xie goto sleep; 1767d0278245SMiao Xie 1768d0278245SMiao Xie if (!mutex_trylock(&root->fs_info->cleaner_mutex)) 1769d0278245SMiao Xie goto sleep; 1770d0278245SMiao Xie 1771dc7f370cSMiao Xie /* 1772dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1773dc7f370cSMiao Xie * during the above check and trylock. 1774dc7f370cSMiao Xie */ 1775babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) { 1776dc7f370cSMiao Xie mutex_unlock(&root->fs_info->cleaner_mutex); 1777dc7f370cSMiao Xie goto sleep; 1778dc7f370cSMiao Xie } 1779dc7f370cSMiao Xie 178024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 178147ab2a6cSJosef Bacik btrfs_delete_unused_bgs(root->fs_info); 17829d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 1783a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 1784a74a4b97SChris Mason 1785d0278245SMiao Xie /* 178605323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 178705323cd1SMiao Xie * needn't do anything special here. 1788d0278245SMiao Xie */ 1789d0278245SMiao Xie btrfs_run_defrag_inodes(root->fs_info); 1790d0278245SMiao Xie sleep: 17919d1a2a3aSDavid Sterba if (!try_to_freeze() && !again) { 1792a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17938929ecfaSYan, Zheng if (!kthread_should_stop()) 1794a74a4b97SChris Mason schedule(); 1795a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1796a74a4b97SChris Mason } 1797a74a4b97SChris Mason } while (!kthread_should_stop()); 1798a74a4b97SChris Mason return 0; 1799a74a4b97SChris Mason } 1800a74a4b97SChris Mason 1801a74a4b97SChris Mason static int transaction_kthread(void *arg) 1802a74a4b97SChris Mason { 1803a74a4b97SChris Mason struct btrfs_root *root = arg; 1804a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1805a74a4b97SChris Mason struct btrfs_transaction *cur; 18068929ecfaSYan, Zheng u64 transid; 1807a74a4b97SChris Mason unsigned long now; 1808a74a4b97SChris Mason unsigned long delay; 1809914b2007SJan Kara bool cannot_commit; 1810a74a4b97SChris Mason 1811a74a4b97SChris Mason do { 1812914b2007SJan Kara cannot_commit = false; 18138b87dc17SDavid Sterba delay = HZ * root->fs_info->commit_interval; 1814a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1815a74a4b97SChris Mason 1816a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1817a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1818a74a4b97SChris Mason if (!cur) { 1819a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1820a74a4b97SChris Mason goto sleep; 1821a74a4b97SChris Mason } 182231153d81SYan Zheng 1823a74a4b97SChris Mason now = get_seconds(); 18244a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 18258b87dc17SDavid Sterba (now < cur->start_time || 18268b87dc17SDavid Sterba now - cur->start_time < root->fs_info->commit_interval)) { 1827a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1828a74a4b97SChris Mason delay = HZ * 5; 1829a74a4b97SChris Mason goto sleep; 1830a74a4b97SChris Mason } 18318929ecfaSYan, Zheng transid = cur->transid; 1832a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 183356bec294SChris Mason 183479787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1835354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1836914b2007SJan Kara if (IS_ERR(trans)) { 1837354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1838914b2007SJan Kara cannot_commit = true; 183979787eaaSJeff Mahoney goto sleep; 1840914b2007SJan Kara } 18418929ecfaSYan, Zheng if (transid == trans->transid) { 184279787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 18438929ecfaSYan, Zheng } else { 18448929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 18458929ecfaSYan, Zheng } 1846a74a4b97SChris Mason sleep: 1847a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1848a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1849a74a4b97SChris Mason 18504e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18514e121c06SJosef Bacik &root->fs_info->fs_state))) 18524e121c06SJosef Bacik btrfs_cleanup_transaction(root); 1853a0acae0eSTejun Heo if (!try_to_freeze()) { 1854a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18558929ecfaSYan, Zheng if (!kthread_should_stop() && 1856914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1857914b2007SJan Kara cannot_commit)) 1858a74a4b97SChris Mason schedule_timeout(delay); 1859a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1860a74a4b97SChris Mason } 1861a74a4b97SChris Mason } while (!kthread_should_stop()); 1862a74a4b97SChris Mason return 0; 1863a74a4b97SChris Mason } 1864a74a4b97SChris Mason 1865af31f5e5SChris Mason /* 1866af31f5e5SChris Mason * this will find the highest generation in the array of 1867af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1868af31f5e5SChris Mason * or -1 if we can't find anything. 1869af31f5e5SChris Mason * 1870af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1871af31f5e5SChris Mason * generation of the latest root in the array with the generation 1872af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1873af31f5e5SChris Mason */ 1874af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1875af31f5e5SChris Mason { 1876af31f5e5SChris Mason u64 cur; 1877af31f5e5SChris Mason int newest_index = -1; 1878af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1879af31f5e5SChris Mason int i; 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1882af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1883af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1884af31f5e5SChris Mason if (cur == newest_gen) 1885af31f5e5SChris Mason newest_index = i; 1886af31f5e5SChris Mason } 1887af31f5e5SChris Mason 1888af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1889af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1890af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1891af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1892af31f5e5SChris Mason if (cur == newest_gen) 1893af31f5e5SChris Mason newest_index = 0; 1894af31f5e5SChris Mason } 1895af31f5e5SChris Mason return newest_index; 1896af31f5e5SChris Mason } 1897af31f5e5SChris Mason 1898af31f5e5SChris Mason 1899af31f5e5SChris Mason /* 1900af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1901af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1902af31f5e5SChris Mason * field in the fs_info struct 1903af31f5e5SChris Mason */ 1904af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1905af31f5e5SChris Mason u64 newest_gen) 1906af31f5e5SChris Mason { 1907af31f5e5SChris Mason int newest_index = -1; 1908af31f5e5SChris Mason 1909af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1910af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1911af31f5e5SChris Mason if (newest_index == -1) { 1912af31f5e5SChris Mason info->backup_root_index = 0; 1913af31f5e5SChris Mason } else { 1914af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1915af31f5e5SChris Mason } 1916af31f5e5SChris Mason } 1917af31f5e5SChris Mason 1918af31f5e5SChris Mason /* 1919af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1920af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1921af31f5e5SChris Mason * done 1922af31f5e5SChris Mason */ 1923af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1924af31f5e5SChris Mason { 1925af31f5e5SChris Mason int next_backup; 1926af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1927af31f5e5SChris Mason int last_backup; 1928af31f5e5SChris Mason 1929af31f5e5SChris Mason next_backup = info->backup_root_index; 1930af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1931af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1932af31f5e5SChris Mason 1933af31f5e5SChris Mason /* 1934af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1935af31f5e5SChris Mason * this happens only at umount 1936af31f5e5SChris Mason */ 1937af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1938af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1939af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1940af31f5e5SChris Mason next_backup = last_backup; 1941af31f5e5SChris Mason 1942af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1943af31f5e5SChris Mason 1944af31f5e5SChris Mason /* 1945af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1946af31f5e5SChris Mason * regardless of which ones we use today 1947af31f5e5SChris Mason */ 1948af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1949af31f5e5SChris Mason 1950af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1951af31f5e5SChris Mason 1952af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1953af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1954af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1955af31f5e5SChris Mason 1956af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1957af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1960af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1961af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1962af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1963af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1964af31f5e5SChris Mason 1965af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1966af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1967af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1968af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1969af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1970af31f5e5SChris Mason 19717c7e82a7SChris Mason /* 19727c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19737c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19747c7e82a7SChris Mason */ 19757c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19767c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19777c7e82a7SChris Mason info->fs_root->node->start); 1978af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1979af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1980af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1981af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19827c7e82a7SChris Mason } 1983af31f5e5SChris Mason 1984af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1985af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1986af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1987af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1988af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1989af31f5e5SChris Mason 1990af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1991af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1992af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1993af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1994af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1995af31f5e5SChris Mason 1996af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1997af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1998af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1999af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 2000af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 2001af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 2002af31f5e5SChris Mason 2003af31f5e5SChris Mason /* 2004af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 2005af31f5e5SChris Mason * for the next commit 2006af31f5e5SChris Mason */ 2007af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 2008af31f5e5SChris Mason &info->super_for_commit->super_roots, 2009af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 2010af31f5e5SChris Mason } 2011af31f5e5SChris Mason 2012af31f5e5SChris Mason /* 2013af31f5e5SChris Mason * this copies info out of the root backup array and back into 2014af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 2015af31f5e5SChris Mason * the array, so you send it the number of backups you've already 2016af31f5e5SChris Mason * tried and the last backup index you used. 2017af31f5e5SChris Mason * 2018af31f5e5SChris Mason * this returns -1 when it has tried all the backups 2019af31f5e5SChris Mason */ 2020af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 2021af31f5e5SChris Mason struct btrfs_super_block *super, 2022af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 2023af31f5e5SChris Mason { 2024af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 2025af31f5e5SChris Mason int newest = *backup_index; 2026af31f5e5SChris Mason 2027af31f5e5SChris Mason if (*num_backups_tried == 0) { 2028af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 2029af31f5e5SChris Mason 2030af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 2031af31f5e5SChris Mason if (newest == -1) 2032af31f5e5SChris Mason return -1; 2033af31f5e5SChris Mason 2034af31f5e5SChris Mason *backup_index = newest; 2035af31f5e5SChris Mason *num_backups_tried = 1; 2036af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 2037af31f5e5SChris Mason /* we've tried all the backups, all done */ 2038af31f5e5SChris Mason return -1; 2039af31f5e5SChris Mason } else { 2040af31f5e5SChris Mason /* jump to the next oldest backup */ 2041af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 2042af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 2043af31f5e5SChris Mason *backup_index = newest; 2044af31f5e5SChris Mason *num_backups_tried += 1; 2045af31f5e5SChris Mason } 2046af31f5e5SChris Mason root_backup = super->super_roots + newest; 2047af31f5e5SChris Mason 2048af31f5e5SChris Mason btrfs_set_super_generation(super, 2049af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2050af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2051af31f5e5SChris Mason btrfs_set_super_root_level(super, 2052af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2053af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2054af31f5e5SChris Mason 2055af31f5e5SChris Mason /* 2056af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2057af31f5e5SChris Mason * need a fsck 2058af31f5e5SChris Mason */ 2059af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2060af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2061af31f5e5SChris Mason return 0; 2062af31f5e5SChris Mason } 2063af31f5e5SChris Mason 20647abadb64SLiu Bo /* helper to cleanup workers */ 20657abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20667abadb64SLiu Bo { 2067dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 2068afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20695cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2070fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2071fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2072fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 20738b110e39SMiao Xie btrfs_destroy_workqueue(fs_info->endio_repair_workers); 2074d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2075fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 2076fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2077fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2078a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20795b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2080e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2081736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2082a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2083fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 2084a79b7d4bSChris Mason btrfs_destroy_workqueue(fs_info->extent_workers); 20857abadb64SLiu Bo } 20867abadb64SLiu Bo 20872e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20882e9f5954SRashika { 20892e9f5954SRashika if (root) { 20902e9f5954SRashika free_extent_buffer(root->node); 20912e9f5954SRashika free_extent_buffer(root->commit_root); 20922e9f5954SRashika root->node = NULL; 20932e9f5954SRashika root->commit_root = NULL; 20942e9f5954SRashika } 20952e9f5954SRashika } 20962e9f5954SRashika 2097af31f5e5SChris Mason /* helper to cleanup tree roots */ 2098af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2099af31f5e5SChris Mason { 21002e9f5954SRashika free_root_extent_buffers(info->tree_root); 2101655b09feSJosef Bacik 21022e9f5954SRashika free_root_extent_buffers(info->dev_root); 21032e9f5954SRashika free_root_extent_buffers(info->extent_root); 21042e9f5954SRashika free_root_extent_buffers(info->csum_root); 21052e9f5954SRashika free_root_extent_buffers(info->quota_root); 21062e9f5954SRashika free_root_extent_buffers(info->uuid_root); 21072e9f5954SRashika if (chunk_root) 21082e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2109af31f5e5SChris Mason } 2110af31f5e5SChris Mason 2111faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info) 2112171f6537SJosef Bacik { 2113171f6537SJosef Bacik int ret; 2114171f6537SJosef Bacik struct btrfs_root *gang[8]; 2115171f6537SJosef Bacik int i; 2116171f6537SJosef Bacik 2117171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2118171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2119171f6537SJosef Bacik struct btrfs_root, root_list); 2120171f6537SJosef Bacik list_del(&gang[0]->root_list); 2121171f6537SJosef Bacik 212227cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) { 2123cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2124171f6537SJosef Bacik } else { 2125171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2126171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2127b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2128171f6537SJosef Bacik } 2129171f6537SJosef Bacik } 2130171f6537SJosef Bacik 2131171f6537SJosef Bacik while (1) { 2132171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2133171f6537SJosef Bacik (void **)gang, 0, 2134171f6537SJosef Bacik ARRAY_SIZE(gang)); 2135171f6537SJosef Bacik if (!ret) 2136171f6537SJosef Bacik break; 2137171f6537SJosef Bacik for (i = 0; i < ret; i++) 2138cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2139171f6537SJosef Bacik } 21401a4319ccSLiu Bo 21411a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 21421a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 21431a4319ccSLiu Bo btrfs_destroy_pinned_extent(fs_info->tree_root, 21441a4319ccSLiu Bo fs_info->pinned_extents); 21451a4319ccSLiu Bo } 2146171f6537SJosef Bacik } 2147af31f5e5SChris Mason 2148ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2149dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2150dfe25020SChris Mason char *options) 2151eb60ceacSChris Mason { 2152db94535dSChris Mason u32 sectorsize; 2153db94535dSChris Mason u32 nodesize; 215487ee04ebSChris Mason u32 stripesize; 215584234f3aSYan Zheng u64 generation; 2156f2b636e8SJosef Bacik u64 features; 21573de4586cSChris Mason struct btrfs_key location; 2158a061fc8dSChris Mason struct buffer_head *bh; 21594d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2160815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2161f84a8bd6SAl Viro struct btrfs_root *tree_root; 21624d34b278SIlya Dryomov struct btrfs_root *extent_root; 21634d34b278SIlya Dryomov struct btrfs_root *csum_root; 21644d34b278SIlya Dryomov struct btrfs_root *chunk_root; 21654d34b278SIlya Dryomov struct btrfs_root *dev_root; 2166bcef60f2SArne Jansen struct btrfs_root *quota_root; 2167f7a81ea4SStefan Behrens struct btrfs_root *uuid_root; 2168e02119d5SChris Mason struct btrfs_root *log_tree_root; 2169eb60ceacSChris Mason int ret; 2170e58ca020SYan int err = -EINVAL; 2171af31f5e5SChris Mason int num_backups_tried = 0; 2172af31f5e5SChris Mason int backup_index = 0; 21735cdc7ad3SQu Wenruo int max_active; 21745cdc7ad3SQu Wenruo int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 217570f80175SStefan Behrens bool create_uuid_tree; 217670f80175SStefan Behrens bool check_uuid_tree; 21774543df7eSChris Mason 2178f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 21796f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 2180cb517eabSMiao Xie if (!tree_root || !chunk_root) { 218139279cc3SChris Mason err = -ENOMEM; 218239279cc3SChris Mason goto fail; 218339279cc3SChris Mason } 218476dda93cSYan, Zheng 218576dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 218676dda93cSYan, Zheng if (ret) { 218776dda93cSYan, Zheng err = ret; 218876dda93cSYan, Zheng goto fail; 218976dda93cSYan, Zheng } 219076dda93cSYan, Zheng 219176dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 219276dda93cSYan, Zheng if (ret) { 219376dda93cSYan, Zheng err = ret; 219476dda93cSYan, Zheng goto fail_srcu; 219576dda93cSYan, Zheng } 219676dda93cSYan, Zheng 2197908c7f19STejun Heo ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2198e2d84521SMiao Xie if (ret) { 2199e2d84521SMiao Xie err = ret; 2200e2d84521SMiao Xie goto fail_bdi; 2201e2d84521SMiao Xie } 2202e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2203e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2204e2d84521SMiao Xie 2205908c7f19STejun Heo ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL); 2206963d678bSMiao Xie if (ret) { 2207963d678bSMiao Xie err = ret; 2208963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2209963d678bSMiao Xie } 2210963d678bSMiao Xie 2211908c7f19STejun Heo ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL); 2212c404e0dcSMiao Xie if (ret) { 2213c404e0dcSMiao Xie err = ret; 2214c404e0dcSMiao Xie goto fail_delalloc_bytes; 2215c404e0dcSMiao Xie } 2216c404e0dcSMiao Xie 221776dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 221876dda93cSYan, Zheng if (!fs_info->btree_inode) { 221976dda93cSYan, Zheng err = -ENOMEM; 2220c404e0dcSMiao Xie goto fail_bio_counter; 222176dda93cSYan, Zheng } 222276dda93cSYan, Zheng 2223a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 22241561dedaSMiao Xie 222576dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2226f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 22278fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2228facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 222924bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2230eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 223111833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2232eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2233a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 223476dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 223524bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 22364cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 22372bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2238f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2239ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2240fcebe456SJosef Bacik spin_lock_init(&fs_info->qgroup_op_lock); 2241f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 224247ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock); 2243f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 22447585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2245573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex); 2246de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 224719c00ddcSChris Mason 224858176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 22490b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 22506324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2251f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 225247ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs); 22530b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 225466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 225566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 225666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 225766d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 225866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 225966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 226066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 226166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 226266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2263cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2264771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 22658c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 22660986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 22674cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2268fcebe456SJosef Bacik atomic_set(&fs_info->qgroup_op_seq, 0); 2269fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2270e20d96d6SChris Mason fs_info->sb = sb; 227195ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE; 22729ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 22734cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 22742bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2275f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 22768b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 22770a2b2a84SJosef Bacik fs_info->avg_delayed_ref_runtime = div64_u64(NSEC_PER_SEC, 64); 227890519d66SArne Jansen /* readahead state */ 227990519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 228090519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2281c8b97818SChris Mason 2282b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2283b34b086cSChris Mason num_online_cpus() + 2, 8); 22840afbaf8cSChris Mason 2285199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2286199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 228716cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 228816cdcec7SMiao Xie GFP_NOFS); 228916cdcec7SMiao Xie if (!fs_info->delayed_root) { 229016cdcec7SMiao Xie err = -ENOMEM; 229116cdcec7SMiao Xie goto fail_iput; 229216cdcec7SMiao Xie } 229316cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 22943eaa2885SChris Mason 2295a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2296a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2297a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2298a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2299a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2300c404e0dcSMiao Xie init_waitqueue_head(&fs_info->replace_wait); 2301a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2302a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 230321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 230421adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 230521adbd5cSStefan Behrens #endif 2306a2de733cSArne Jansen 2307c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2308c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2309837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2310837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2311a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2312c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2313837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 231421c7e756SMiao Xie btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work); 2315a061fc8dSChris Mason 23160afbaf8cSChris Mason sb->s_blocksize = 4096; 23170afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 231832a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 23190afbaf8cSChris Mason 232076dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2321bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 23220afbaf8cSChris Mason /* 23230afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 23240afbaf8cSChris Mason * the real end of the address space is determined by all of 23250afbaf8cSChris Mason * the devices in the system 23260afbaf8cSChris Mason */ 23270afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2328d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 232904160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 233004160088SChris Mason 23315d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2332d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2333f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 23340b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2335a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 23360da5468fSChris Mason 2337d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2338d1310b2eSChris Mason 23390f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 23400f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 23410f7d52f4SChris Mason sizeof(struct btrfs_key)); 234272ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 234372ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 2344778ba82bSFilipe David Borba Manana btrfs_insert_inode_hash(fs_info->btree_inode); 234539279cc3SChris Mason 2346e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 23476bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2348a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2349925baeddSChris Mason 235011833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2351f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 235211833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2353f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 235411833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2355e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2356f9295749SChris Mason 2357d98237b3SChris Mason 23585a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 23599ffba8cdSJosef Bacik mutex_init(&fs_info->ordered_extent_flush_mutex); 236030ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2361925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2362925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2363a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2364a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 23659e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem); 2366c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 236776dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2368803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2369e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2370e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2371e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2372e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2373e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2374fa9c0d79SChris Mason 2375416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2376f2f6ed3dSWang Shilong mutex_init(&fs_info->qgroup_ioctl_lock); 2377416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2378fcebe456SJosef Bacik fs_info->qgroup_op_tree = RB_ROOT; 2379416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2380416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2381416ac51dSArne Jansen fs_info->quota_enabled = 0; 2382416ac51dSArne Jansen fs_info->pending_quota_state = 0; 23831e8f9158SWang Shilong fs_info->qgroup_ulist = NULL; 23842f232036SJan Schmidt mutex_init(&fs_info->qgroup_rescan_lock); 2385416ac51dSArne Jansen 2386fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2387fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2388fa9c0d79SChris Mason 23897d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 23903768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2391bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 23924854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 23939a8dd150SChris Mason 239404216820SFilipe Manana INIT_LIST_HEAD(&fs_info->pinned_chunks); 239504216820SFilipe Manana 239653b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 239753b381b3SDavid Woodhouse if (ret) { 239883c8266aSDavid Sterba err = ret; 239953b381b3SDavid Woodhouse goto fail_alloc; 240053b381b3SDavid Woodhouse } 240153b381b3SDavid Woodhouse 2402707e8a07SDavid Sterba __setup_root(4096, 4096, 4096, tree_root, 24032c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 24047eccb903SChris Mason 24053c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 24061104a885SDavid Sterba 24071104a885SDavid Sterba /* 24081104a885SDavid Sterba * Read super block and check the signature bytes only 24091104a885SDavid Sterba */ 2410a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 241120b45077SDave Young if (!bh) { 241220b45077SDave Young err = -EINVAL; 241316cdcec7SMiao Xie goto fail_alloc; 241420b45077SDave Young } 241539279cc3SChris Mason 24161104a885SDavid Sterba /* 24171104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 24181104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 24191104a885SDavid Sterba */ 24201104a885SDavid Sterba if (btrfs_check_super_csum(bh->b_data)) { 2421efe120a0SFrank Holton printk(KERN_ERR "BTRFS: superblock checksum mismatch\n"); 24221104a885SDavid Sterba err = -EINVAL; 24231104a885SDavid Sterba goto fail_alloc; 24241104a885SDavid Sterba } 24251104a885SDavid Sterba 24261104a885SDavid Sterba /* 24271104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 24281104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 24291104a885SDavid Sterba * the whole block of INFO_SIZE 24301104a885SDavid Sterba */ 24316c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 24326c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 24336c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2434a061fc8dSChris Mason brelse(bh); 24355f39d397SChris Mason 24366c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 24370b86a832SChris Mason 24381104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 24391104a885SDavid Sterba if (ret) { 2440efe120a0SFrank Holton printk(KERN_ERR "BTRFS: superblock contains fatal errors\n"); 24411104a885SDavid Sterba err = -EINVAL; 24421104a885SDavid Sterba goto fail_alloc; 24431104a885SDavid Sterba } 24441104a885SDavid Sterba 24456c41761fSDavid Sterba disk_super = fs_info->super_copy; 24460f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 244716cdcec7SMiao Xie goto fail_alloc; 24480f7d52f4SChris Mason 2449acce952bSliubo /* check FS state, whether FS is broken. */ 245087533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 245187533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2452acce952bSliubo 245375e7cb7fSLiu Bo /* 2454af31f5e5SChris Mason * run through our array of backup supers and setup 2455af31f5e5SChris Mason * our ring pointer to the oldest one 2456af31f5e5SChris Mason */ 2457af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2458af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2459af31f5e5SChris Mason 2460af31f5e5SChris Mason /* 246175e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 246275e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 246375e7cb7fSLiu Bo */ 246475e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 246575e7cb7fSLiu Bo 24662b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 24672b82032cSYan Zheng if (ret) { 24682b82032cSYan Zheng err = ret; 246916cdcec7SMiao Xie goto fail_alloc; 24702b82032cSYan Zheng } 2471dfe25020SChris Mason 2472f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2473f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2474f2b636e8SJosef Bacik if (features) { 2475f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2476f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 2477c1c9ff7cSGeert Uytterhoeven features); 2478f2b636e8SJosef Bacik err = -EINVAL; 247916cdcec7SMiao Xie goto fail_alloc; 2480f2b636e8SJosef Bacik } 2481f2b636e8SJosef Bacik 2482707e8a07SDavid Sterba /* 2483707e8a07SDavid Sterba * Leafsize and nodesize were always equal, this is only a sanity check. 2484707e8a07SDavid Sterba */ 2485707e8a07SDavid Sterba if (le32_to_cpu(disk_super->__unused_leafsize) != 2486727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2487727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2488727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2489727011e0SChris Mason btrfs_super_nodesize(disk_super), 2490707e8a07SDavid Sterba le32_to_cpu(disk_super->__unused_leafsize)); 2491727011e0SChris Mason err = -EINVAL; 2492727011e0SChris Mason goto fail_alloc; 2493727011e0SChris Mason } 2494707e8a07SDavid Sterba if (btrfs_super_nodesize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2495727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2496727011e0SChris Mason "blocksize (%d) was too large\n", 2497707e8a07SDavid Sterba btrfs_super_nodesize(disk_super)); 2498727011e0SChris Mason err = -EINVAL; 2499727011e0SChris Mason goto fail_alloc; 2500727011e0SChris Mason } 2501727011e0SChris Mason 25025d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 25035d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 250469e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2505a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2506727011e0SChris Mason 25073173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 2508efe120a0SFrank Holton printk(KERN_ERR "BTRFS: has skinny extents\n"); 25093173a18fSJosef Bacik 2510727011e0SChris Mason /* 2511727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2512727011e0SChris Mason * they are bigger than the page size 2513727011e0SChris Mason */ 2514707e8a07SDavid Sterba if (btrfs_super_nodesize(disk_super) > PAGE_CACHE_SIZE) { 2515727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2516efe120a0SFrank Holton printk(KERN_INFO "BTRFS: flagging fs with big metadata feature\n"); 2517727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2518727011e0SChris Mason } 2519727011e0SChris Mason 2520bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2521bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2522bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2523707e8a07SDavid Sterba fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids)); 2524963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2525bc3f116fSChris Mason 2526bc3f116fSChris Mason /* 2527bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2528bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2529bc3f116fSChris Mason */ 2530bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2531707e8a07SDavid Sterba (sectorsize != nodesize)) { 2532efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: unequal leaf/node/sector sizes " 2533bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2534bc3f116fSChris Mason sb->s_id); 2535bc3f116fSChris Mason goto fail_alloc; 2536bc3f116fSChris Mason } 2537bc3f116fSChris Mason 2538ceda0864SMiao Xie /* 2539ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2540ceda0864SMiao Xie * update the flag. 2541ceda0864SMiao Xie */ 25425d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 25435d4f98a2SYan Zheng 2544f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2545f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2546f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2547f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2548f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 2549c1c9ff7cSGeert Uytterhoeven features); 2550f2b636e8SJosef Bacik err = -EINVAL; 255116cdcec7SMiao Xie goto fail_alloc; 2552f2b636e8SJosef Bacik } 255361d92c32SChris Mason 25545cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 255561d92c32SChris Mason 25565cdc7ad3SQu Wenruo fs_info->workers = 25575cdc7ad3SQu Wenruo btrfs_alloc_workqueue("worker", flags | WQ_HIGHPRI, 25585cdc7ad3SQu Wenruo max_active, 16); 2559c8b97818SChris Mason 2560afe3d242SQu Wenruo fs_info->delalloc_workers = 2561afe3d242SQu Wenruo btrfs_alloc_workqueue("delalloc", flags, max_active, 2); 2562771ed689SChris Mason 2563a44903abSQu Wenruo fs_info->flush_workers = 2564a44903abSQu Wenruo btrfs_alloc_workqueue("flush_delalloc", flags, max_active, 0); 25658ccf6f19SMiao Xie 2566e66f0bb1SQu Wenruo fs_info->caching_workers = 2567e66f0bb1SQu Wenruo btrfs_alloc_workqueue("cache", flags, max_active, 0); 256861b49440SChris Mason 2569a8c93d4eSQu Wenruo /* 2570a8c93d4eSQu Wenruo * a higher idle thresh on the submit workers makes it much more 257161b49440SChris Mason * likely that bios will be send down in a sane order to the 257261b49440SChris Mason * devices 257361b49440SChris Mason */ 2574a8c93d4eSQu Wenruo fs_info->submit_workers = 2575a8c93d4eSQu Wenruo btrfs_alloc_workqueue("submit", flags, 2576a8c93d4eSQu Wenruo min_t(u64, fs_devices->num_devices, 2577a8c93d4eSQu Wenruo max_active), 64); 257853863232SChris Mason 2579dc6e3209SQu Wenruo fs_info->fixup_workers = 2580dc6e3209SQu Wenruo btrfs_alloc_workqueue("fixup", flags, 1, 0); 258161b49440SChris Mason 258261b49440SChris Mason /* 258361b49440SChris Mason * endios are largely parallel and should have a very 258461b49440SChris Mason * low idle thresh 258561b49440SChris Mason */ 2586fccb5d86SQu Wenruo fs_info->endio_workers = 2587fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio", flags, max_active, 4); 2588fccb5d86SQu Wenruo fs_info->endio_meta_workers = 2589fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-meta", flags, max_active, 4); 2590fccb5d86SQu Wenruo fs_info->endio_meta_write_workers = 2591fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-meta-write", flags, max_active, 2); 2592fccb5d86SQu Wenruo fs_info->endio_raid56_workers = 2593fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-raid56", flags, max_active, 4); 25948b110e39SMiao Xie fs_info->endio_repair_workers = 25958b110e39SMiao Xie btrfs_alloc_workqueue("endio-repair", flags, 1, 0); 2596d05a33acSQu Wenruo fs_info->rmw_workers = 2597d05a33acSQu Wenruo btrfs_alloc_workqueue("rmw", flags, max_active, 2); 2598fccb5d86SQu Wenruo fs_info->endio_write_workers = 2599fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-write", flags, max_active, 2); 2600fccb5d86SQu Wenruo fs_info->endio_freespace_worker = 2601fccb5d86SQu Wenruo btrfs_alloc_workqueue("freespace-write", flags, max_active, 0); 26025b3bc44eSQu Wenruo fs_info->delayed_workers = 26035b3bc44eSQu Wenruo btrfs_alloc_workqueue("delayed-meta", flags, max_active, 0); 2604736cfa15SQu Wenruo fs_info->readahead_workers = 2605736cfa15SQu Wenruo btrfs_alloc_workqueue("readahead", flags, max_active, 2); 2606fc97fab0SQu Wenruo fs_info->qgroup_rescan_workers = 2607fc97fab0SQu Wenruo btrfs_alloc_workqueue("qgroup-rescan", flags, 1, 0); 2608a79b7d4bSChris Mason fs_info->extent_workers = 2609a79b7d4bSChris Mason btrfs_alloc_workqueue("extent-refs", flags, 2610a79b7d4bSChris Mason min_t(u64, fs_devices->num_devices, 2611a79b7d4bSChris Mason max_active), 8); 2612b51912c9SChris Mason 2613a8c93d4eSQu Wenruo if (!(fs_info->workers && fs_info->delalloc_workers && 2614fccb5d86SQu Wenruo fs_info->submit_workers && fs_info->flush_workers && 2615fccb5d86SQu Wenruo fs_info->endio_workers && fs_info->endio_meta_workers && 2616fccb5d86SQu Wenruo fs_info->endio_meta_write_workers && 26178b110e39SMiao Xie fs_info->endio_repair_workers && 2618fccb5d86SQu Wenruo fs_info->endio_write_workers && fs_info->endio_raid56_workers && 2619e66f0bb1SQu Wenruo fs_info->endio_freespace_worker && fs_info->rmw_workers && 2620dc6e3209SQu Wenruo fs_info->caching_workers && fs_info->readahead_workers && 2621fc97fab0SQu Wenruo fs_info->fixup_workers && fs_info->delayed_workers && 262256094eecSAndrey Utkin fs_info->extent_workers && 2623fc97fab0SQu Wenruo fs_info->qgroup_rescan_workers)) { 2624fed425c7SIlya Dryomov err = -ENOMEM; 26250dc3b84aSJosef Bacik goto fail_sb_buffer; 26260dc3b84aSJosef Bacik } 26274543df7eSChris Mason 26284575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2629c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2630c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 26314575c9ccSChris Mason 2632db94535dSChris Mason tree_root->nodesize = nodesize; 2633db94535dSChris Mason tree_root->sectorsize = sectorsize; 263487ee04ebSChris Mason tree_root->stripesize = stripesize; 2635a061fc8dSChris Mason 2636a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2637a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2638db94535dSChris Mason 26393cae210fSQu Wenruo if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) { 2640efe120a0SFrank Holton printk(KERN_INFO "BTRFS: valid FS not found on %s\n", sb->s_id); 264139279cc3SChris Mason goto fail_sb_buffer; 264239279cc3SChris Mason } 264319c00ddcSChris Mason 26448d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 2645efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: Incompatible sector size(%lu) " 26468d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2647941b2ddfSKeith Mannthey goto fail_sb_buffer; 2648941b2ddfSKeith Mannthey } 2649941b2ddfSKeith Mannthey 2650925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2651e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2652925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 265384eed90fSChris Mason if (ret) { 2654efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read the system " 2655d397712bSChris Mason "array on %s\n", sb->s_id); 26565d4f98a2SYan Zheng goto fail_sb_buffer; 265784eed90fSChris Mason } 26580b86a832SChris Mason 265984234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 26600b86a832SChris Mason 2661707e8a07SDavid Sterba __setup_root(nodesize, sectorsize, stripesize, chunk_root, 2662707e8a07SDavid Sterba fs_info, BTRFS_CHUNK_TREE_OBJECTID); 26630b86a832SChris Mason 26640b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 26650b86a832SChris Mason btrfs_super_chunk_root(disk_super), 2666ce86cd59SDavid Sterba generation); 2667416bc658SJosef Bacik if (!chunk_root->node || 2668416bc658SJosef Bacik !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 2669efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read chunk root on %s\n", 267083121942SDavid Woodhouse sb->s_id); 2671af31f5e5SChris Mason goto fail_tree_roots; 267283121942SDavid Woodhouse } 26735d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 26745d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 26750b86a832SChris Mason 2676e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2677b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2678e17cade2SChris Mason 26790b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 26802b82032cSYan Zheng if (ret) { 2681efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read chunk tree on %s\n", 2682d397712bSChris Mason sb->s_id); 2683af31f5e5SChris Mason goto fail_tree_roots; 26842b82032cSYan Zheng } 26850b86a832SChris Mason 26868dabb742SStefan Behrens /* 26878dabb742SStefan Behrens * keep the device that is marked to be the target device for the 26888dabb742SStefan Behrens * dev_replace procedure 26898dabb742SStefan Behrens */ 26908dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2691dfe25020SChris Mason 2692a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2693efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: failed to read devices on %s\n", 2694a6b0d5c8SChris Mason sb->s_id); 2695a6b0d5c8SChris Mason goto fail_tree_roots; 2696a6b0d5c8SChris Mason } 2697a6b0d5c8SChris Mason 2698af31f5e5SChris Mason retry_root_backup: 269984234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 27000b86a832SChris Mason 2701e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2702db94535dSChris Mason btrfs_super_root(disk_super), 2703ce86cd59SDavid Sterba generation); 2704af31f5e5SChris Mason if (!tree_root->node || 2705af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 2706efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read tree root on %s\n", 270783121942SDavid Woodhouse sb->s_id); 2708af31f5e5SChris Mason 2709af31f5e5SChris Mason goto recovery_tree_root; 271083121942SDavid Woodhouse } 2711af31f5e5SChris Mason 27125d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 27135d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 271469e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2715db94535dSChris Mason 2716cb517eabSMiao Xie location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 2717cb517eabSMiao Xie location.type = BTRFS_ROOT_ITEM_KEY; 2718cb517eabSMiao Xie location.offset = 0; 2719cb517eabSMiao Xie 2720cb517eabSMiao Xie extent_root = btrfs_read_tree_root(tree_root, &location); 2721cb517eabSMiao Xie if (IS_ERR(extent_root)) { 2722cb517eabSMiao Xie ret = PTR_ERR(extent_root); 2723af31f5e5SChris Mason goto recovery_tree_root; 2724cb517eabSMiao Xie } 272527cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &extent_root->state); 2726cb517eabSMiao Xie fs_info->extent_root = extent_root; 27270b86a832SChris Mason 2728cb517eabSMiao Xie location.objectid = BTRFS_DEV_TREE_OBJECTID; 2729cb517eabSMiao Xie dev_root = btrfs_read_tree_root(tree_root, &location); 2730cb517eabSMiao Xie if (IS_ERR(dev_root)) { 2731cb517eabSMiao Xie ret = PTR_ERR(dev_root); 2732af31f5e5SChris Mason goto recovery_tree_root; 2733cb517eabSMiao Xie } 273427cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &dev_root->state); 2735cb517eabSMiao Xie fs_info->dev_root = dev_root; 2736cb517eabSMiao Xie btrfs_init_devices_late(fs_info); 27373768f368SChris Mason 2738cb517eabSMiao Xie location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2739cb517eabSMiao Xie csum_root = btrfs_read_tree_root(tree_root, &location); 2740cb517eabSMiao Xie if (IS_ERR(csum_root)) { 2741cb517eabSMiao Xie ret = PTR_ERR(csum_root); 2742af31f5e5SChris Mason goto recovery_tree_root; 2743cb517eabSMiao Xie } 274427cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &csum_root->state); 2745cb517eabSMiao Xie fs_info->csum_root = csum_root; 2746d20f7043SChris Mason 2747cb517eabSMiao Xie location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2748cb517eabSMiao Xie quota_root = btrfs_read_tree_root(tree_root, &location); 2749cb517eabSMiao Xie if (!IS_ERR(quota_root)) { 275027cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, "a_root->state); 2751bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2752bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2753cb517eabSMiao Xie fs_info->quota_root = quota_root; 2754bcef60f2SArne Jansen } 2755bcef60f2SArne Jansen 2756f7a81ea4SStefan Behrens location.objectid = BTRFS_UUID_TREE_OBJECTID; 2757f7a81ea4SStefan Behrens uuid_root = btrfs_read_tree_root(tree_root, &location); 2758f7a81ea4SStefan Behrens if (IS_ERR(uuid_root)) { 2759f7a81ea4SStefan Behrens ret = PTR_ERR(uuid_root); 2760f7a81ea4SStefan Behrens if (ret != -ENOENT) 2761f7a81ea4SStefan Behrens goto recovery_tree_root; 2762f7a81ea4SStefan Behrens create_uuid_tree = true; 276370f80175SStefan Behrens check_uuid_tree = false; 2764f7a81ea4SStefan Behrens } else { 276527cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &uuid_root->state); 2766f7a81ea4SStefan Behrens fs_info->uuid_root = uuid_root; 276770f80175SStefan Behrens create_uuid_tree = false; 276870f80175SStefan Behrens check_uuid_tree = 276970f80175SStefan Behrens generation != btrfs_super_uuid_tree_generation(disk_super); 2770f7a81ea4SStefan Behrens } 2771f7a81ea4SStefan Behrens 27728929ecfaSYan, Zheng fs_info->generation = generation; 27738929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 27748929ecfaSYan, Zheng 277568310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 277668310a5eSIlya Dryomov if (ret) { 2777efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to recover balance\n"); 277868310a5eSIlya Dryomov goto fail_block_groups; 277968310a5eSIlya Dryomov } 278068310a5eSIlya Dryomov 2781733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2782733f4fbbSStefan Behrens if (ret) { 2783efe120a0SFrank Holton printk(KERN_ERR "BTRFS: failed to init dev_stats: %d\n", 2784733f4fbbSStefan Behrens ret); 2785733f4fbbSStefan Behrens goto fail_block_groups; 2786733f4fbbSStefan Behrens } 2787733f4fbbSStefan Behrens 27888dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 27898dabb742SStefan Behrens if (ret) { 2790efe120a0SFrank Holton pr_err("BTRFS: failed to init dev_replace: %d\n", ret); 27918dabb742SStefan Behrens goto fail_block_groups; 27928dabb742SStefan Behrens } 27938dabb742SStefan Behrens 27948dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 27958dabb742SStefan Behrens 27965ac1d209SJeff Mahoney ret = btrfs_sysfs_add_one(fs_info); 27975ac1d209SJeff Mahoney if (ret) { 2798efe120a0SFrank Holton pr_err("BTRFS: failed to init sysfs interface: %d\n", ret); 27995ac1d209SJeff Mahoney goto fail_block_groups; 28005ac1d209SJeff Mahoney } 28015ac1d209SJeff Mahoney 2802c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2803c59021f8Sliubo if (ret) { 2804efe120a0SFrank Holton printk(KERN_ERR "BTRFS: Failed to initial space info: %d\n", ret); 28052365dd3cSAnand Jain goto fail_sysfs; 2806c59021f8Sliubo } 2807c59021f8Sliubo 28081b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 28091b1d1f66SJosef Bacik if (ret) { 2810efe120a0SFrank Holton printk(KERN_ERR "BTRFS: Failed to read block groups: %d\n", ret); 28112365dd3cSAnand Jain goto fail_sysfs; 28121b1d1f66SJosef Bacik } 28135af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 28145af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2815292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2816292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2817292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2818efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: " 2819efe120a0SFrank Holton "too many missing devices, writeable mount is not allowed\n"); 28202365dd3cSAnand Jain goto fail_sysfs; 2821292fd7fcSStefan Behrens } 28229078a3e1SChris Mason 2823a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2824a74a4b97SChris Mason "btrfs-cleaner"); 282557506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 28262365dd3cSAnand Jain goto fail_sysfs; 2827a74a4b97SChris Mason 2828a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2829a74a4b97SChris Mason tree_root, 2830a74a4b97SChris Mason "btrfs-transaction"); 283157506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 28323f157a2fSChris Mason goto fail_cleaner; 2833a74a4b97SChris Mason 2834c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2835c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2836c289811cSChris Mason !fs_info->fs_devices->rotating) { 2837efe120a0SFrank Holton printk(KERN_INFO "BTRFS: detected SSD devices, enabling SSD " 2838c289811cSChris Mason "mode\n"); 2839c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2840c289811cSChris Mason } 2841c289811cSChris Mason 2842572d9ab7SDavid Sterba /* 2843572d9ab7SDavid Sterba * Mount does not set all options immediatelly, we can do it now and do 2844572d9ab7SDavid Sterba * not have to wait for transaction commit 2845572d9ab7SDavid Sterba */ 2846572d9ab7SDavid Sterba btrfs_apply_pending_changes(fs_info); 28473818aea2SQu Wenruo 284821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 284921adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 285021adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 285121adbd5cSStefan Behrens btrfs_test_opt(tree_root, 285221adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 285321adbd5cSStefan Behrens 1 : 0, 285421adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 285521adbd5cSStefan Behrens if (ret) 2856efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to initialize" 285721adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 285821adbd5cSStefan Behrens } 285921adbd5cSStefan Behrens #endif 2860bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2861bcef60f2SArne Jansen if (ret) 2862bcef60f2SArne Jansen goto fail_trans_kthread; 286321adbd5cSStefan Behrens 2864acce952bSliubo /* do not make disk changes in broken FS */ 286568ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2866e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2867d18a2c44SChris Mason 28687c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2869efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: log replay required " 2870d397712bSChris Mason "on RO media\n"); 28717c2ca468SChris Mason err = -EIO; 2872bcef60f2SArne Jansen goto fail_qgroup; 28737c2ca468SChris Mason } 2874e02119d5SChris Mason 28756f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2876676e4c86SDan Carpenter if (!log_tree_root) { 2877676e4c86SDan Carpenter err = -ENOMEM; 2878bcef60f2SArne Jansen goto fail_qgroup; 2879676e4c86SDan Carpenter } 2880e02119d5SChris Mason 2881707e8a07SDavid Sterba __setup_root(nodesize, sectorsize, stripesize, 2882e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2883e02119d5SChris Mason 2884e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 288584234f3aSYan Zheng generation + 1); 2886416bc658SJosef Bacik if (!log_tree_root->node || 2887416bc658SJosef Bacik !extent_buffer_uptodate(log_tree_root->node)) { 2888efe120a0SFrank Holton printk(KERN_ERR "BTRFS: failed to read log tree\n"); 2889416bc658SJosef Bacik free_extent_buffer(log_tree_root->node); 2890416bc658SJosef Bacik kfree(log_tree_root); 289128c16cbbSWang Shilong goto fail_qgroup; 2892416bc658SJosef Bacik } 289379787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2894e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 289579787eaaSJeff Mahoney if (ret) { 289679787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 289779787eaaSJeff Mahoney "Failed to recover log tree"); 289879787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 289979787eaaSJeff Mahoney kfree(log_tree_root); 290028c16cbbSWang Shilong goto fail_qgroup; 290179787eaaSJeff Mahoney } 2902e556ce2cSYan Zheng 2903e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2904e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 290579787eaaSJeff Mahoney if (ret) 290628c16cbbSWang Shilong goto fail_qgroup; 2907e556ce2cSYan Zheng } 2908e02119d5SChris Mason } 29091a40e23bSZheng Yan 291076dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 291179787eaaSJeff Mahoney if (ret) 291228c16cbbSWang Shilong goto fail_qgroup; 291376dda93cSYan, Zheng 29147c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2915d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 291644c44af2SIlya Dryomov if (ret) 291728c16cbbSWang Shilong goto fail_qgroup; 2918d68fc57bSYan, Zheng 29195f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 29205d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 29215f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 2922d7ce5843SMiao Xie if (ret < 0) { 2923d7ce5843SMiao Xie printk(KERN_WARNING 2924efe120a0SFrank Holton "BTRFS: failed to recover relocation\n"); 2925d7ce5843SMiao Xie err = -EINVAL; 2926bcef60f2SArne Jansen goto fail_qgroup; 2927d7ce5843SMiao Xie } 29287c2ca468SChris Mason } 29291a40e23bSZheng Yan 29303de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 29313de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2932cb517eabSMiao Xie location.offset = 0; 29333de4586cSChris Mason 29343de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 29353140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 29363140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2937bcef60f2SArne Jansen goto fail_qgroup; 29383140c9a3SDan Carpenter } 2939c289811cSChris Mason 29402b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 29412b6ba629SIlya Dryomov return 0; 29422b6ba629SIlya Dryomov 2943e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 29442b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 29452b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 29462b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 29472b6ba629SIlya Dryomov close_ctree(tree_root); 29482b6ba629SIlya Dryomov return ret; 29492b6ba629SIlya Dryomov } 2950e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 295159641015SIlya Dryomov 29522b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 29532b6ba629SIlya Dryomov if (ret) { 2954efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to resume balance\n"); 295566b4ffd1SJosef Bacik close_ctree(tree_root); 29562b6ba629SIlya Dryomov return ret; 2957e3acc2a6SJosef Bacik } 2958e3acc2a6SJosef Bacik 29598dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 29608dabb742SStefan Behrens if (ret) { 2961efe120a0SFrank Holton pr_warn("BTRFS: failed to resume dev_replace\n"); 29628dabb742SStefan Behrens close_ctree(tree_root); 29638dabb742SStefan Behrens return ret; 29648dabb742SStefan Behrens } 29658dabb742SStefan Behrens 2966b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 2967b382a324SJan Schmidt 2968f7a81ea4SStefan Behrens if (create_uuid_tree) { 2969efe120a0SFrank Holton pr_info("BTRFS: creating UUID tree\n"); 2970f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 2971f7a81ea4SStefan Behrens if (ret) { 2972efe120a0SFrank Holton pr_warn("BTRFS: failed to create the UUID tree %d\n", 2973f7a81ea4SStefan Behrens ret); 2974f7a81ea4SStefan Behrens close_ctree(tree_root); 2975f7a81ea4SStefan Behrens return ret; 2976f7a81ea4SStefan Behrens } 2977f420ee1eSStefan Behrens } else if (check_uuid_tree || 2978f420ee1eSStefan Behrens btrfs_test_opt(tree_root, RESCAN_UUID_TREE)) { 2979efe120a0SFrank Holton pr_info("BTRFS: checking UUID tree\n"); 298070f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 298170f80175SStefan Behrens if (ret) { 2982efe120a0SFrank Holton pr_warn("BTRFS: failed to check the UUID tree %d\n", 298370f80175SStefan Behrens ret); 298470f80175SStefan Behrens close_ctree(tree_root); 298570f80175SStefan Behrens return ret; 298670f80175SStefan Behrens } 298770f80175SStefan Behrens } else { 298870f80175SStefan Behrens fs_info->update_uuid_tree_gen = 1; 2989f7a81ea4SStefan Behrens } 2990f7a81ea4SStefan Behrens 299147ab2a6cSJosef Bacik fs_info->open = 1; 299247ab2a6cSJosef Bacik 2993ad2b2c80SAl Viro return 0; 299439279cc3SChris Mason 2995bcef60f2SArne Jansen fail_qgroup: 2996bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 29977c2ca468SChris Mason fail_trans_kthread: 29987c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 299954067ae9SJosef Bacik btrfs_cleanup_transaction(fs_info->tree_root); 3000faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 30013f157a2fSChris Mason fail_cleaner: 3002a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 30037c2ca468SChris Mason 30047c2ca468SChris Mason /* 30057c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 30067c2ca468SChris Mason * kthreads 30077c2ca468SChris Mason */ 30087c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 30097c2ca468SChris Mason 30102365dd3cSAnand Jain fail_sysfs: 30112365dd3cSAnand Jain btrfs_sysfs_remove_one(fs_info); 30122365dd3cSAnand Jain 30131b1d1f66SJosef Bacik fail_block_groups: 301454067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 30151b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 3016af31f5e5SChris Mason 3017af31f5e5SChris Mason fail_tree_roots: 3018af31f5e5SChris Mason free_root_pointers(fs_info, 1); 30192b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 3020af31f5e5SChris Mason 302139279cc3SChris Mason fail_sb_buffer: 30227abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 302316cdcec7SMiao Xie fail_alloc: 30244543df7eSChris Mason fail_iput: 3025586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 3026586e46e2SIlya Dryomov 30274543df7eSChris Mason iput(fs_info->btree_inode); 3028c404e0dcSMiao Xie fail_bio_counter: 3029c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 3030963d678bSMiao Xie fail_delalloc_bytes: 3031963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3032e2d84521SMiao Xie fail_dirty_metadata_bytes: 3033e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3034ad081f14SJens Axboe fail_bdi: 30357e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 303676dda93cSYan, Zheng fail_srcu: 303776dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 30387e662854SQinghuang Feng fail: 303953b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3040586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3041ad2b2c80SAl Viro return err; 3042af31f5e5SChris Mason 3043af31f5e5SChris Mason recovery_tree_root: 3044af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 3045af31f5e5SChris Mason goto fail_tree_roots; 3046af31f5e5SChris Mason 3047af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3048af31f5e5SChris Mason 3049af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3050af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3051af31f5e5SChris Mason 3052af31f5e5SChris Mason /* we can't trust the free space cache either */ 3053af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3054af31f5e5SChris Mason 3055af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3056af31f5e5SChris Mason &num_backups_tried, &backup_index); 3057af31f5e5SChris Mason if (ret == -1) 3058af31f5e5SChris Mason goto fail_block_groups; 3059af31f5e5SChris Mason goto retry_root_backup; 3060eb60ceacSChris Mason } 3061eb60ceacSChris Mason 3062f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3063f2984462SChris Mason { 3064f2984462SChris Mason if (uptodate) { 3065f2984462SChris Mason set_buffer_uptodate(bh); 3066f2984462SChris Mason } else { 3067442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3068442a4f63SStefan Behrens bh->b_private; 3069442a4f63SStefan Behrens 3070efe120a0SFrank Holton printk_ratelimited_in_rcu(KERN_WARNING "BTRFS: lost page write due to " 3071606686eeSJosef Bacik "I/O error on %s\n", 3072606686eeSJosef Bacik rcu_str_deref(device->name)); 30731259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 30741259ab75SChris Mason * our own ways of dealing with the IO errors 30751259ab75SChris Mason */ 3076f2984462SChris Mason clear_buffer_uptodate(bh); 3077442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3078f2984462SChris Mason } 3079f2984462SChris Mason unlock_buffer(bh); 3080f2984462SChris Mason put_bh(bh); 3081f2984462SChris Mason } 3082f2984462SChris Mason 3083a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3084a512bbf8SYan Zheng { 3085a512bbf8SYan Zheng struct buffer_head *bh; 3086a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3087a512bbf8SYan Zheng struct btrfs_super_block *super; 3088a512bbf8SYan Zheng int i; 3089a512bbf8SYan Zheng u64 transid = 0; 3090a512bbf8SYan Zheng u64 bytenr; 3091a512bbf8SYan Zheng 3092a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3093a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3094a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3095a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3096a512bbf8SYan Zheng */ 3097a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 3098a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 30998068a47eSAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= 31008068a47eSAnand Jain i_size_read(bdev->bd_inode)) 3101a512bbf8SYan Zheng break; 31028068a47eSAnand Jain bh = __bread(bdev, bytenr / 4096, 31038068a47eSAnand Jain BTRFS_SUPER_INFO_SIZE); 3104a512bbf8SYan Zheng if (!bh) 3105a512bbf8SYan Zheng continue; 3106a512bbf8SYan Zheng 3107a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3108a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 31093cae210fSQu Wenruo btrfs_super_magic(super) != BTRFS_MAGIC) { 3110a512bbf8SYan Zheng brelse(bh); 3111a512bbf8SYan Zheng continue; 3112a512bbf8SYan Zheng } 3113a512bbf8SYan Zheng 3114a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3115a512bbf8SYan Zheng brelse(latest); 3116a512bbf8SYan Zheng latest = bh; 3117a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3118a512bbf8SYan Zheng } else { 3119a512bbf8SYan Zheng brelse(bh); 3120a512bbf8SYan Zheng } 3121a512bbf8SYan Zheng } 3122a512bbf8SYan Zheng return latest; 3123a512bbf8SYan Zheng } 3124a512bbf8SYan Zheng 31254eedeb75SHisashi Hifumi /* 31264eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 31274eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 31284eedeb75SHisashi Hifumi * we write are pinned. 31294eedeb75SHisashi Hifumi * 31304eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 31314eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 31324eedeb75SHisashi Hifumi * many supers on this one device should be written. 31334eedeb75SHisashi Hifumi * 31344eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 31354eedeb75SHisashi Hifumi */ 3136a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3137a512bbf8SYan Zheng struct btrfs_super_block *sb, 3138a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 3139a512bbf8SYan Zheng { 3140a512bbf8SYan Zheng struct buffer_head *bh; 3141a512bbf8SYan Zheng int i; 3142a512bbf8SYan Zheng int ret; 3143a512bbf8SYan Zheng int errors = 0; 3144a512bbf8SYan Zheng u32 crc; 3145a512bbf8SYan Zheng u64 bytenr; 3146a512bbf8SYan Zheng 3147a512bbf8SYan Zheng if (max_mirrors == 0) 3148a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3149a512bbf8SYan Zheng 3150a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3151a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3152935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3153935e5cc9SMiao Xie device->commit_total_bytes) 3154a512bbf8SYan Zheng break; 3155a512bbf8SYan Zheng 3156a512bbf8SYan Zheng if (wait) { 3157a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3158a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3159634554dcSJosef Bacik if (!bh) { 3160634554dcSJosef Bacik errors++; 3161634554dcSJosef Bacik continue; 3162634554dcSJosef Bacik } 3163a512bbf8SYan Zheng wait_on_buffer(bh); 31644eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 31654eedeb75SHisashi Hifumi errors++; 31664eedeb75SHisashi Hifumi 31674eedeb75SHisashi Hifumi /* drop our reference */ 31684eedeb75SHisashi Hifumi brelse(bh); 31694eedeb75SHisashi Hifumi 31704eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3171a512bbf8SYan Zheng brelse(bh); 3172a512bbf8SYan Zheng continue; 3173a512bbf8SYan Zheng } else { 3174a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3175a512bbf8SYan Zheng 3176a512bbf8SYan Zheng crc = ~(u32)0; 3177b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3178a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3179a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3180a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3181a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3182a512bbf8SYan Zheng 31834eedeb75SHisashi Hifumi /* 31844eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 31854eedeb75SHisashi Hifumi * caller 31864eedeb75SHisashi Hifumi */ 3187a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3188a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3189634554dcSJosef Bacik if (!bh) { 3190efe120a0SFrank Holton printk(KERN_ERR "BTRFS: couldn't get super " 3191634554dcSJosef Bacik "buffer head for bytenr %Lu\n", bytenr); 3192634554dcSJosef Bacik errors++; 3193634554dcSJosef Bacik continue; 3194634554dcSJosef Bacik } 3195634554dcSJosef Bacik 3196a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3197a512bbf8SYan Zheng 31984eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3199a512bbf8SYan Zheng get_bh(bh); 32004eedeb75SHisashi Hifumi 32014eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3202a512bbf8SYan Zheng lock_buffer(bh); 3203a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3204442a4f63SStefan Behrens bh->b_private = device; 3205a512bbf8SYan Zheng } 3206a512bbf8SYan Zheng 3207387125fcSChris Mason /* 3208387125fcSChris Mason * we fua the first super. The others we allow 3209387125fcSChris Mason * to go down lazy. 3210387125fcSChris Mason */ 3211e8117c26SWang Shilong if (i == 0) 321221adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 3213e8117c26SWang Shilong else 3214e8117c26SWang Shilong ret = btrfsic_submit_bh(WRITE_SYNC, bh); 32154eedeb75SHisashi Hifumi if (ret) 3216a512bbf8SYan Zheng errors++; 3217a512bbf8SYan Zheng } 3218a512bbf8SYan Zheng return errors < i ? 0 : -1; 3219a512bbf8SYan Zheng } 3220a512bbf8SYan Zheng 3221387125fcSChris Mason /* 3222387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3223387125fcSChris Mason * for the barrier when it is done 3224387125fcSChris Mason */ 3225387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 3226387125fcSChris Mason { 3227387125fcSChris Mason if (err) { 3228387125fcSChris Mason if (err == -EOPNOTSUPP) 3229387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 3230387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 3231387125fcSChris Mason } 3232387125fcSChris Mason if (bio->bi_private) 3233387125fcSChris Mason complete(bio->bi_private); 3234387125fcSChris Mason bio_put(bio); 3235387125fcSChris Mason } 3236387125fcSChris Mason 3237387125fcSChris Mason /* 3238387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3239387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3240387125fcSChris Mason * 3241387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3242387125fcSChris Mason * capable 3243387125fcSChris Mason */ 3244387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3245387125fcSChris Mason { 3246387125fcSChris Mason struct bio *bio; 3247387125fcSChris Mason int ret = 0; 3248387125fcSChris Mason 3249387125fcSChris Mason if (device->nobarriers) 3250387125fcSChris Mason return 0; 3251387125fcSChris Mason 3252387125fcSChris Mason if (wait) { 3253387125fcSChris Mason bio = device->flush_bio; 3254387125fcSChris Mason if (!bio) 3255387125fcSChris Mason return 0; 3256387125fcSChris Mason 3257387125fcSChris Mason wait_for_completion(&device->flush_wait); 3258387125fcSChris Mason 3259387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 3260efe120a0SFrank Holton printk_in_rcu("BTRFS: disabling barriers on dev %s\n", 3261606686eeSJosef Bacik rcu_str_deref(device->name)); 3262387125fcSChris Mason device->nobarriers = 1; 32635af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 3264387125fcSChris Mason ret = -EIO; 3265442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3266442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3267387125fcSChris Mason } 3268387125fcSChris Mason 3269387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3270387125fcSChris Mason bio_put(bio); 3271387125fcSChris Mason device->flush_bio = NULL; 3272387125fcSChris Mason 3273387125fcSChris Mason return ret; 3274387125fcSChris Mason } 3275387125fcSChris Mason 3276387125fcSChris Mason /* 3277387125fcSChris Mason * one reference for us, and we leave it for the 3278387125fcSChris Mason * caller 3279387125fcSChris Mason */ 32809c017abcSJesper Juhl device->flush_bio = NULL; 32819be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3282387125fcSChris Mason if (!bio) 3283387125fcSChris Mason return -ENOMEM; 3284387125fcSChris Mason 3285387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3286387125fcSChris Mason bio->bi_bdev = device->bdev; 3287387125fcSChris Mason init_completion(&device->flush_wait); 3288387125fcSChris Mason bio->bi_private = &device->flush_wait; 3289387125fcSChris Mason device->flush_bio = bio; 3290387125fcSChris Mason 3291387125fcSChris Mason bio_get(bio); 329221adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 3293387125fcSChris Mason 3294387125fcSChris Mason return 0; 3295387125fcSChris Mason } 3296387125fcSChris Mason 3297387125fcSChris Mason /* 3298387125fcSChris Mason * send an empty flush down to each device in parallel, 3299387125fcSChris Mason * then wait for them 3300387125fcSChris Mason */ 3301387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3302387125fcSChris Mason { 3303387125fcSChris Mason struct list_head *head; 3304387125fcSChris Mason struct btrfs_device *dev; 33055af3e8ccSStefan Behrens int errors_send = 0; 33065af3e8ccSStefan Behrens int errors_wait = 0; 3307387125fcSChris Mason int ret; 3308387125fcSChris Mason 3309387125fcSChris Mason /* send down all the barriers */ 3310387125fcSChris Mason head = &info->fs_devices->devices; 3311387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3312f88ba6a2SHidetoshi Seto if (dev->missing) 3313f88ba6a2SHidetoshi Seto continue; 3314387125fcSChris Mason if (!dev->bdev) { 33155af3e8ccSStefan Behrens errors_send++; 3316387125fcSChris Mason continue; 3317387125fcSChris Mason } 3318387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3319387125fcSChris Mason continue; 3320387125fcSChris Mason 3321387125fcSChris Mason ret = write_dev_flush(dev, 0); 3322387125fcSChris Mason if (ret) 33235af3e8ccSStefan Behrens errors_send++; 3324387125fcSChris Mason } 3325387125fcSChris Mason 3326387125fcSChris Mason /* wait for all the barriers */ 3327387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3328f88ba6a2SHidetoshi Seto if (dev->missing) 3329f88ba6a2SHidetoshi Seto continue; 3330387125fcSChris Mason if (!dev->bdev) { 33315af3e8ccSStefan Behrens errors_wait++; 3332387125fcSChris Mason continue; 3333387125fcSChris Mason } 3334387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3335387125fcSChris Mason continue; 3336387125fcSChris Mason 3337387125fcSChris Mason ret = write_dev_flush(dev, 1); 3338387125fcSChris Mason if (ret) 33395af3e8ccSStefan Behrens errors_wait++; 3340387125fcSChris Mason } 33415af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 33425af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3343387125fcSChris Mason return -EIO; 3344387125fcSChris Mason return 0; 3345387125fcSChris Mason } 3346387125fcSChris Mason 33475af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 33485af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 33495af3e8ccSStefan Behrens { 33505af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 33515af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 33525af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 33535af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 33545af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 33555af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 33565af3e8ccSStefan Behrens int num_types = 4; 33575af3e8ccSStefan Behrens int i; 33585af3e8ccSStefan Behrens int c; 33595af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 33605af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 33615af3e8ccSStefan Behrens 33625af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 33635af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 33645af3e8ccSStefan Behrens 33655af3e8ccSStefan Behrens sinfo = NULL; 33665af3e8ccSStefan Behrens rcu_read_lock(); 33675af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 33685af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 33695af3e8ccSStefan Behrens sinfo = tmp; 33705af3e8ccSStefan Behrens break; 33715af3e8ccSStefan Behrens } 33725af3e8ccSStefan Behrens } 33735af3e8ccSStefan Behrens rcu_read_unlock(); 33745af3e8ccSStefan Behrens 33755af3e8ccSStefan Behrens if (!sinfo) 33765af3e8ccSStefan Behrens continue; 33775af3e8ccSStefan Behrens 33785af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 33795af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 33805af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 33815af3e8ccSStefan Behrens u64 flags; 33825af3e8ccSStefan Behrens 33835af3e8ccSStefan Behrens btrfs_get_block_group_info( 33845af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 33855af3e8ccSStefan Behrens if (space.total_bytes == 0 || 33865af3e8ccSStefan Behrens space.used_bytes == 0) 33875af3e8ccSStefan Behrens continue; 33885af3e8ccSStefan Behrens flags = space.flags; 33895af3e8ccSStefan Behrens /* 33905af3e8ccSStefan Behrens * return 33915af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 33925af3e8ccSStefan Behrens * any of metadata, system or data, else 33935af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 33945af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 33955af3e8ccSStefan Behrens * are used, else 33965af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 33975af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 33985af3e8ccSStefan Behrens * configured and more than 33995af3e8ccSStefan Behrens * 2 mirrors are used. 34005af3e8ccSStefan Behrens */ 34015af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 34025af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 34035af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 34045af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 34055af3e8ccSStefan Behrens == 0))) 34065af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 340753b381b3SDavid Woodhouse else if (num_tolerated_disk_barrier_failures > 1) { 340853b381b3SDavid Woodhouse if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 340953b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5 | 341053b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID10)) { 34115af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 341253b381b3SDavid Woodhouse } else if (flags & 341315b0a89dSHenrik Nordvik BTRFS_BLOCK_GROUP_RAID6) { 341453b381b3SDavid Woodhouse num_tolerated_disk_barrier_failures = 2; 341553b381b3SDavid Woodhouse } 341653b381b3SDavid Woodhouse } 34175af3e8ccSStefan Behrens } 34185af3e8ccSStefan Behrens } 34195af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 34205af3e8ccSStefan Behrens } 34215af3e8ccSStefan Behrens 34225af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 34235af3e8ccSStefan Behrens } 34245af3e8ccSStefan Behrens 342548a3b636SEric Sandeen static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3426f2984462SChris Mason { 3427e5e9a520SChris Mason struct list_head *head; 3428f2984462SChris Mason struct btrfs_device *dev; 3429a061fc8dSChris Mason struct btrfs_super_block *sb; 3430f2984462SChris Mason struct btrfs_dev_item *dev_item; 3431f2984462SChris Mason int ret; 3432f2984462SChris Mason int do_barriers; 3433a236aed1SChris Mason int max_errors; 3434a236aed1SChris Mason int total_errors = 0; 3435a061fc8dSChris Mason u64 flags; 3436f2984462SChris Mason 3437f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3438af31f5e5SChris Mason backup_super_roots(root->fs_info); 3439f2984462SChris Mason 34406c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3441a061fc8dSChris Mason dev_item = &sb->dev_item; 3442e5e9a520SChris Mason 3443174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3444e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3445d7306801SFilipe David Borba Manana max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3446387125fcSChris Mason 34475af3e8ccSStefan Behrens if (do_barriers) { 34485af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 34495af3e8ccSStefan Behrens if (ret) { 34505af3e8ccSStefan Behrens mutex_unlock( 34515af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 34525af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 34535af3e8ccSStefan Behrens "errors while submitting device barriers."); 34545af3e8ccSStefan Behrens return ret; 34555af3e8ccSStefan Behrens } 34565af3e8ccSStefan Behrens } 3457387125fcSChris Mason 34581f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3459dfe25020SChris Mason if (!dev->bdev) { 3460dfe25020SChris Mason total_errors++; 3461dfe25020SChris Mason continue; 3462dfe25020SChris Mason } 34632b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3464dfe25020SChris Mason continue; 3465dfe25020SChris Mason 34662b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3467a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3468a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 34697df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item, 3470935e5cc9SMiao Xie dev->commit_total_bytes); 3471ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item, 3472ce7213c7SMiao Xie dev->commit_bytes_used); 3473a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3474a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3475a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3476a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 34772b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3478a512bbf8SYan Zheng 3479a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3480a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3481f2984462SChris Mason 3482a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3483a236aed1SChris Mason if (ret) 3484a236aed1SChris Mason total_errors++; 3485f2984462SChris Mason } 3486a236aed1SChris Mason if (total_errors > max_errors) { 3487efe120a0SFrank Holton btrfs_err(root->fs_info, "%d errors while writing supers", 3488d397712bSChris Mason total_errors); 3489a724b436SStefan Behrens mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 349079787eaaSJeff Mahoney 34919d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 34929d565ba4SStefan Behrens btrfs_error(root->fs_info, -EIO, 34939d565ba4SStefan Behrens "%d errors while writing supers", total_errors); 34949d565ba4SStefan Behrens return -EIO; 3495a236aed1SChris Mason } 3496f2984462SChris Mason 3497a512bbf8SYan Zheng total_errors = 0; 34981f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3499dfe25020SChris Mason if (!dev->bdev) 3500dfe25020SChris Mason continue; 35012b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3502dfe25020SChris Mason continue; 3503dfe25020SChris Mason 3504a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3505a512bbf8SYan Zheng if (ret) 35061259ab75SChris Mason total_errors++; 3507f2984462SChris Mason } 3508174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3509a236aed1SChris Mason if (total_errors > max_errors) { 351079787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 351179787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 351279787eaaSJeff Mahoney return -EIO; 3513a236aed1SChris Mason } 3514f2984462SChris Mason return 0; 3515f2984462SChris Mason } 3516f2984462SChris Mason 3517a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3518a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3519cfaa7295SChris Mason { 3520f570e757SRashika return write_all_supers(root, max_mirrors); 3521cfaa7295SChris Mason } 3522cfaa7295SChris Mason 3523cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3524cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3525cb517eabSMiao Xie struct btrfs_root *root) 35262619ba1fSChris Mason { 35274df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 35282619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 35292619ba1fSChris Mason (unsigned long)root->root_key.objectid); 35304df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 353176dda93cSYan, Zheng 353276dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 353376dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 353476dda93cSYan, Zheng 35351a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 35363321719eSLiu Bo btrfs_free_log(NULL, root); 35373321719eSLiu Bo 3538faa2dbf0SJosef Bacik if (root->free_ino_pinned) 3539581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3540faa2dbf0SJosef Bacik if (root->free_ino_ctl) 3541581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 35424df27c4dSYan, Zheng free_fs_root(root); 35434df27c4dSYan, Zheng } 35444df27c4dSYan, Zheng 35454df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 35464df27c4dSYan, Zheng { 354757cdc8dbSDavid Sterba iput(root->ino_cache_inode); 35484df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 35491cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 35501cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 35510ee5dc67SAl Viro if (root->anon_dev) 35520ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 35538257b2dcSMiao Xie if (root->subv_writers) 35548257b2dcSMiao Xie btrfs_free_subvolume_writers(root->subv_writers); 35555f39d397SChris Mason free_extent_buffer(root->node); 35565f39d397SChris Mason free_extent_buffer(root->commit_root); 3557581bb050SLi Zefan kfree(root->free_ino_ctl); 3558581bb050SLi Zefan kfree(root->free_ino_pinned); 355958176a96SJosef Bacik kfree(root->name); 3560b0feb9d9SMiao Xie btrfs_put_fs_root(root); 35612619ba1fSChris Mason } 35622619ba1fSChris Mason 3563cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3564cb517eabSMiao Xie { 3565cb517eabSMiao Xie free_fs_root(root); 3566eb60ceacSChris Mason } 3567eb60ceacSChris Mason 3568c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3569c146afadSYan Zheng { 3570c146afadSYan Zheng u64 root_objectid = 0; 3571c146afadSYan Zheng struct btrfs_root *gang[8]; 357265d33fd7SQu Wenruo int i = 0; 357365d33fd7SQu Wenruo int err = 0; 357465d33fd7SQu Wenruo unsigned int ret = 0; 357565d33fd7SQu Wenruo int index; 3576c146afadSYan Zheng 3577c146afadSYan Zheng while (1) { 357865d33fd7SQu Wenruo index = srcu_read_lock(&fs_info->subvol_srcu); 3579c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3580c146afadSYan Zheng (void **)gang, root_objectid, 3581c146afadSYan Zheng ARRAY_SIZE(gang)); 358265d33fd7SQu Wenruo if (!ret) { 358365d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 3584c146afadSYan Zheng break; 358565d33fd7SQu Wenruo } 35865d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 358766b4ffd1SJosef Bacik 358865d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 358965d33fd7SQu Wenruo /* Avoid to grab roots in dead_roots */ 359065d33fd7SQu Wenruo if (btrfs_root_refs(&gang[i]->root_item) == 0) { 359165d33fd7SQu Wenruo gang[i] = NULL; 359265d33fd7SQu Wenruo continue; 359365d33fd7SQu Wenruo } 359465d33fd7SQu Wenruo /* grab all the search result for later use */ 359565d33fd7SQu Wenruo gang[i] = btrfs_grab_fs_root(gang[i]); 359665d33fd7SQu Wenruo } 359765d33fd7SQu Wenruo srcu_read_unlock(&fs_info->subvol_srcu, index); 359865d33fd7SQu Wenruo 359965d33fd7SQu Wenruo for (i = 0; i < ret; i++) { 360065d33fd7SQu Wenruo if (!gang[i]) 360165d33fd7SQu Wenruo continue; 3602c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 360366b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 360466b4ffd1SJosef Bacik if (err) 360565d33fd7SQu Wenruo break; 360665d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 3607c146afadSYan Zheng } 3608c146afadSYan Zheng root_objectid++; 3609c146afadSYan Zheng } 361065d33fd7SQu Wenruo 361165d33fd7SQu Wenruo /* release the uncleaned roots due to error */ 361265d33fd7SQu Wenruo for (; i < ret; i++) { 361365d33fd7SQu Wenruo if (gang[i]) 361465d33fd7SQu Wenruo btrfs_put_fs_root(gang[i]); 361565d33fd7SQu Wenruo } 361665d33fd7SQu Wenruo return err; 3617c146afadSYan Zheng } 3618c146afadSYan Zheng 3619c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3620c146afadSYan Zheng { 3621c146afadSYan Zheng struct btrfs_trans_handle *trans; 3622c146afadSYan Zheng 3623c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 362424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3625c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 36269d1a2a3aSDavid Sterba wake_up_process(root->fs_info->cleaner_kthread); 3627c71bf099SYan, Zheng 3628c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3629c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3630c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3631c71bf099SYan, Zheng 36327a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 36333612b495STsutomu Itoh if (IS_ERR(trans)) 36343612b495STsutomu Itoh return PTR_ERR(trans); 3635d52c1bccSLiu Bo return btrfs_commit_transaction(trans, root); 3636c146afadSYan Zheng } 3637c146afadSYan Zheng 36383abdbd78SDavid Sterba void close_ctree(struct btrfs_root *root) 3639eb60ceacSChris Mason { 36400f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3641c146afadSYan Zheng int ret; 3642e089f05cSChris Mason 3643facda1e7SChris Mason fs_info->closing = 1; 3644a2135011SChris Mason smp_mb(); 3645a2135011SChris Mason 3646803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3647803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3648803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3649803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3650803b2f54SStefan Behrens 3651837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3652aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3653837d5b6eSIlya Dryomov 36548dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 36558dabb742SStefan Behrens 3656aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 36574cb5300bSChris Mason 36584cb5300bSChris Mason /* wait for any defraggers to finish */ 36594cb5300bSChris Mason wait_event(fs_info->transaction_wait, 36604cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 36614cb5300bSChris Mason 36624cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 366326176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 36644cb5300bSChris Mason 366521c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 366621c7e756SMiao Xie 3667c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3668c146afadSYan Zheng ret = btrfs_commit_super(root); 3669d397712bSChris Mason if (ret) 3670efe120a0SFrank Holton btrfs_err(root->fs_info, "commit super ret %d", ret); 3671c146afadSYan Zheng } 3672ed2ff2cbSChris Mason 367387533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 367468ce9682SStefan Behrens btrfs_error_commit_super(root); 3675acce952bSliubo 3676e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3677e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 36788929ecfaSYan, Zheng 3679f25784b3SYan Zheng fs_info->closing = 2; 3680f25784b3SYan Zheng smp_mb(); 3681f25784b3SYan Zheng 3682bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3683bcef60f2SArne Jansen 3684963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3685efe120a0SFrank Holton btrfs_info(root->fs_info, "at unmount delalloc count %lld", 3686963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3687b0c68f8bSChris Mason } 368831153d81SYan Zheng 36895ac1d209SJeff Mahoney btrfs_sysfs_remove_one(fs_info); 36905ac1d209SJeff Mahoney 3691faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 3692d10c5f31SChris Mason 36931a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 36941a4319ccSLiu Bo 36952b1360daSJosef Bacik btrfs_free_block_groups(fs_info); 36962b1360daSJosef Bacik 3697de348ee0SWang Shilong /* 3698de348ee0SWang Shilong * we must make sure there is not any read request to 3699de348ee0SWang Shilong * submit after we stopping all workers. 3700de348ee0SWang Shilong */ 3701de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 370296192499SJosef Bacik btrfs_stop_all_workers(fs_info); 370396192499SJosef Bacik 370447ab2a6cSJosef Bacik fs_info->open = 0; 370513e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 37069ad6b7bcSChris Mason 370713e6c37bSJosef Bacik iput(fs_info->btree_inode); 3708d6bfde87SChris Mason 370921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 371021adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 371121adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 371221adbd5cSStefan Behrens #endif 371321adbd5cSStefan Behrens 3714dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 37150b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3716b248a415SChris Mason 3717e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3718963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3719c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 372004160088SChris Mason bdi_destroy(&fs_info->bdi); 372176dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 37220b86a832SChris Mason 372353b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 372453b381b3SDavid Woodhouse 37251cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 37261cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 372704216820SFilipe Manana 372804216820SFilipe Manana lock_chunks(root); 372904216820SFilipe Manana while (!list_empty(&fs_info->pinned_chunks)) { 373004216820SFilipe Manana struct extent_map *em; 373104216820SFilipe Manana 373204216820SFilipe Manana em = list_first_entry(&fs_info->pinned_chunks, 373304216820SFilipe Manana struct extent_map, list); 373404216820SFilipe Manana list_del_init(&em->list); 373504216820SFilipe Manana free_extent_map(em); 373604216820SFilipe Manana } 373704216820SFilipe Manana unlock_chunks(root); 3738eb60ceacSChris Mason } 3739eb60ceacSChris Mason 3740b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3741b9fab919SChris Mason int atomic) 3742ccd467d6SChris Mason { 37431259ab75SChris Mason int ret; 3744727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 37451259ab75SChris Mason 37460b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 37471259ab75SChris Mason if (!ret) 37481259ab75SChris Mason return ret; 37491259ab75SChris Mason 37501259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3751b9fab919SChris Mason parent_transid, atomic); 3752b9fab919SChris Mason if (ret == -EAGAIN) 3753b9fab919SChris Mason return ret; 37541259ab75SChris Mason return !ret; 37555f39d397SChris Mason } 37566702ed49SChris Mason 37575f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 37585f39d397SChris Mason { 37590b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 37605f39d397SChris Mason } 37615f39d397SChris Mason 37625f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 37635f39d397SChris Mason { 376406ea65a3SJosef Bacik struct btrfs_root *root; 37655f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3766b9473439SChris Mason int was_dirty; 3767b4ce94deSChris Mason 376806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 376906ea65a3SJosef Bacik /* 377006ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 377106ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 377206ea65a3SJosef Bacik * outside of the sanity tests. 377306ea65a3SJosef Bacik */ 377406ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 377506ea65a3SJosef Bacik return; 377606ea65a3SJosef Bacik #endif 377706ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 3778b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 377931b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 378031b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3781d397712bSChris Mason "found %llu running %llu\n", 3782c1c9ff7cSGeert Uytterhoeven buf->start, transid, root->fs_info->generation); 37830b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3784e2d84521SMiao Xie if (!was_dirty) 3785e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3786e2d84521SMiao Xie buf->len, 3787e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 37881f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 37891f21ef0aSFilipe Manana if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 37901f21ef0aSFilipe Manana btrfs_print_leaf(root, buf); 37911f21ef0aSFilipe Manana ASSERT(0); 37921f21ef0aSFilipe Manana } 37931f21ef0aSFilipe Manana #endif 3794eb60ceacSChris Mason } 3795eb60ceacSChris Mason 3796b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3797b53d3f5dSLiu Bo int flush_delayed) 379835b7e476SChris Mason { 3799188de649SChris Mason /* 3800188de649SChris Mason * looks as though older kernels can get into trouble with 3801188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3802188de649SChris Mason */ 3803e2d84521SMiao Xie int ret; 3804d6bfde87SChris Mason 38056933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3806d6bfde87SChris Mason return; 3807d6bfde87SChris Mason 3808b53d3f5dSLiu Bo if (flush_delayed) 380916cdcec7SMiao Xie btrfs_balance_delayed_items(root); 381016cdcec7SMiao Xie 3811e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3812e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3813e2d84521SMiao Xie if (ret > 0) { 3814d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited( 3815d0e1d66bSNamjae Jeon root->fs_info->btree_inode->i_mapping); 381616cdcec7SMiao Xie } 381716cdcec7SMiao Xie return; 381816cdcec7SMiao Xie } 381916cdcec7SMiao Xie 3820b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 382116cdcec7SMiao Xie { 3822b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 382335b7e476SChris Mason } 3824b53d3f5dSLiu Bo 3825b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3826b53d3f5dSLiu Bo { 3827b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3828d6bfde87SChris Mason } 38296b80053dSChris Mason 3830ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 38316b80053dSChris Mason { 3832727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 38330b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 38346b80053dSChris Mason } 38350da5468fSChris Mason 3836fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3837acce952bSliubo int read_only) 3838acce952bSliubo { 3839c926093eSDavid Sterba struct btrfs_super_block *sb = fs_info->super_copy; 3840c926093eSDavid Sterba int ret = 0; 3841c926093eSDavid Sterba 384221e7626bSDavid Sterba if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 384321e7626bSDavid Sterba printk(KERN_ERR "BTRFS: tree_root level too big: %d >= %d\n", 384421e7626bSDavid Sterba btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 3845c926093eSDavid Sterba ret = -EINVAL; 3846c926093eSDavid Sterba } 384721e7626bSDavid Sterba if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) { 384821e7626bSDavid Sterba printk(KERN_ERR "BTRFS: chunk_root level too big: %d >= %d\n", 384921e7626bSDavid Sterba btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL); 3850c926093eSDavid Sterba ret = -EINVAL; 3851c926093eSDavid Sterba } 385221e7626bSDavid Sterba if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) { 385321e7626bSDavid Sterba printk(KERN_ERR "BTRFS: log_root level too big: %d >= %d\n", 385421e7626bSDavid Sterba btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL); 3855c926093eSDavid Sterba ret = -EINVAL; 3856c926093eSDavid Sterba } 3857c926093eSDavid Sterba 38581104a885SDavid Sterba /* 3859c926093eSDavid Sterba * The common minimum, we don't know if we can trust the nodesize/sectorsize 3860c926093eSDavid Sterba * items yet, they'll be verified later. Issue just a warning. 38611104a885SDavid Sterba */ 386221e7626bSDavid Sterba if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) 3863c926093eSDavid Sterba printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n", 3864cd743facSDavid Sterba btrfs_super_root(sb)); 386521e7626bSDavid Sterba if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) 3866cd743facSDavid Sterba printk(KERN_WARNING "BTRFS: chunk_root block unaligned: %llu\n", 3867cd743facSDavid Sterba btrfs_super_chunk_root(sb)); 386821e7626bSDavid Sterba if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) 3869cd743facSDavid Sterba printk(KERN_WARNING "BTRFS: log_root block unaligned: %llu\n", 387021e7626bSDavid Sterba btrfs_super_log_root(sb)); 3871c926093eSDavid Sterba 3872c926093eSDavid Sterba if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) { 3873c926093eSDavid Sterba printk(KERN_ERR "BTRFS: dev_item UUID does not match fsid: %pU != %pU\n", 3874c926093eSDavid Sterba fs_info->fsid, sb->dev_item.fsid); 3875c926093eSDavid Sterba ret = -EINVAL; 3876c926093eSDavid Sterba } 3877c926093eSDavid Sterba 3878c926093eSDavid Sterba /* 3879c926093eSDavid Sterba * Hint to catch really bogus numbers, bitflips or so, more exact checks are 3880c926093eSDavid Sterba * done later 3881c926093eSDavid Sterba */ 388221e7626bSDavid Sterba if (btrfs_super_num_devices(sb) > (1UL << 31)) 3883c926093eSDavid Sterba printk(KERN_WARNING "BTRFS: suspicious number of devices: %llu\n", 388421e7626bSDavid Sterba btrfs_super_num_devices(sb)); 3885c926093eSDavid Sterba 388621e7626bSDavid Sterba if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) { 3887c926093eSDavid Sterba printk(KERN_ERR "BTRFS: super offset mismatch %llu != %u\n", 388821e7626bSDavid Sterba btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 3889c926093eSDavid Sterba ret = -EINVAL; 3890c926093eSDavid Sterba } 3891c926093eSDavid Sterba 3892c926093eSDavid Sterba /* 3893c926093eSDavid Sterba * The generation is a global counter, we'll trust it more than the others 3894c926093eSDavid Sterba * but it's still possible that it's the one that's wrong. 3895c926093eSDavid Sterba */ 389621e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb)) 3897c926093eSDavid Sterba printk(KERN_WARNING 3898c926093eSDavid Sterba "BTRFS: suspicious: generation < chunk_root_generation: %llu < %llu\n", 389921e7626bSDavid Sterba btrfs_super_generation(sb), btrfs_super_chunk_root_generation(sb)); 390021e7626bSDavid Sterba if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb) 390121e7626bSDavid Sterba && btrfs_super_cache_generation(sb) != (u64)-1) 3902c926093eSDavid Sterba printk(KERN_WARNING 3903c926093eSDavid Sterba "BTRFS: suspicious: generation < cache_generation: %llu < %llu\n", 390421e7626bSDavid Sterba btrfs_super_generation(sb), btrfs_super_cache_generation(sb)); 3905c926093eSDavid Sterba 3906c926093eSDavid Sterba return ret; 3907fcd1f065SDavid Sterba } 3908fcd1f065SDavid Sterba 390948a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root) 3910acce952bSliubo { 3911acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3912acce952bSliubo btrfs_run_delayed_iputs(root); 3913acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3914acce952bSliubo 3915acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3916acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3917acce952bSliubo 3918acce952bSliubo /* cleanup FS via transaction */ 3919acce952bSliubo btrfs_cleanup_transaction(root); 3920acce952bSliubo } 3921acce952bSliubo 3922143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3923acce952bSliubo { 3924acce952bSliubo struct btrfs_ordered_extent *ordered; 3925acce952bSliubo 3926199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 3927779880efSJosef Bacik /* 3928779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3929779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3930779880efSJosef Bacik */ 3931199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 3932779880efSJosef Bacik root_extent_list) 3933779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3934199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 3935199c2a9cSMiao Xie } 3936199c2a9cSMiao Xie 3937199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 3938199c2a9cSMiao Xie { 3939199c2a9cSMiao Xie struct btrfs_root *root; 3940199c2a9cSMiao Xie struct list_head splice; 3941199c2a9cSMiao Xie 3942199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 3943199c2a9cSMiao Xie 3944199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 3945199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 3946199c2a9cSMiao Xie while (!list_empty(&splice)) { 3947199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3948199c2a9cSMiao Xie ordered_root); 39491de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 39501de2cfdeSJosef Bacik &fs_info->ordered_roots); 3951199c2a9cSMiao Xie 39522a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 3953199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 3954199c2a9cSMiao Xie 39552a85d9caSLiu Bo cond_resched(); 39562a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 3957199c2a9cSMiao Xie } 3958199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 3959acce952bSliubo } 3960acce952bSliubo 396135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3962acce952bSliubo struct btrfs_root *root) 3963acce952bSliubo { 3964acce952bSliubo struct rb_node *node; 3965acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3966acce952bSliubo struct btrfs_delayed_ref_node *ref; 3967acce952bSliubo int ret = 0; 3968acce952bSliubo 3969acce952bSliubo delayed_refs = &trans->delayed_refs; 3970acce952bSliubo 3971acce952bSliubo spin_lock(&delayed_refs->lock); 3972d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 3973cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3974efe120a0SFrank Holton btrfs_info(root->fs_info, "delayed_refs has NO entry"); 3975acce952bSliubo return ret; 3976acce952bSliubo } 3977acce952bSliubo 3978d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 3979d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 3980e78417d1SJosef Bacik bool pin_bytes = false; 3981acce952bSliubo 3982d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 3983d7df2c79SJosef Bacik href_node); 3984b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3985d7df2c79SJosef Bacik atomic_inc(&head->node.refs); 398679787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3987b939d1abSJosef Bacik 3988acce952bSliubo mutex_lock(&head->mutex); 3989b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3990d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 3991e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3992b939d1abSJosef Bacik continue; 3993b939d1abSJosef Bacik } 3994d7df2c79SJosef Bacik spin_lock(&head->lock); 3995d7df2c79SJosef Bacik while ((node = rb_first(&head->ref_root)) != NULL) { 3996d7df2c79SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, 3997d7df2c79SJosef Bacik rb_node); 3998d7df2c79SJosef Bacik ref->in_tree = 0; 3999d7df2c79SJosef Bacik rb_erase(&ref->rb_node, &head->ref_root); 4000d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4001d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 4002d7df2c79SJosef Bacik } 400354067ae9SJosef Bacik if (head->must_insert_reserved) 4004e78417d1SJosef Bacik pin_bytes = true; 400578a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 4006acce952bSliubo delayed_refs->num_heads--; 4007d7df2c79SJosef Bacik if (head->processing == 0) 4008acce952bSliubo delayed_refs->num_heads_ready--; 4009d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 4010d7df2c79SJosef Bacik head->node.in_tree = 0; 4011c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 4012d7df2c79SJosef Bacik spin_unlock(&head->lock); 4013acce952bSliubo spin_unlock(&delayed_refs->lock); 4014e78417d1SJosef Bacik mutex_unlock(&head->mutex); 4015acce952bSliubo 4016d7df2c79SJosef Bacik if (pin_bytes) 4017d7df2c79SJosef Bacik btrfs_pin_extent(root, head->node.bytenr, 4018d7df2c79SJosef Bacik head->node.num_bytes, 1); 4019d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 4020acce952bSliubo cond_resched(); 4021acce952bSliubo spin_lock(&delayed_refs->lock); 4022acce952bSliubo } 4023acce952bSliubo 4024acce952bSliubo spin_unlock(&delayed_refs->lock); 4025acce952bSliubo 4026acce952bSliubo return ret; 4027acce952bSliubo } 4028acce952bSliubo 4029143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 4030acce952bSliubo { 4031acce952bSliubo struct btrfs_inode *btrfs_inode; 4032acce952bSliubo struct list_head splice; 4033acce952bSliubo 4034acce952bSliubo INIT_LIST_HEAD(&splice); 4035acce952bSliubo 4036eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4037eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 4038acce952bSliubo 4039acce952bSliubo while (!list_empty(&splice)) { 4040eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 4041acce952bSliubo delalloc_inodes); 4042acce952bSliubo 4043acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 4044df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 4045df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 4046eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4047acce952bSliubo 4048acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 4049b216cbfbSMiao Xie 4050eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 4051acce952bSliubo } 4052acce952bSliubo 4053eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 4054eb73c1b7SMiao Xie } 4055eb73c1b7SMiao Xie 4056eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 4057eb73c1b7SMiao Xie { 4058eb73c1b7SMiao Xie struct btrfs_root *root; 4059eb73c1b7SMiao Xie struct list_head splice; 4060eb73c1b7SMiao Xie 4061eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 4062eb73c1b7SMiao Xie 4063eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4064eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 4065eb73c1b7SMiao Xie while (!list_empty(&splice)) { 4066eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 4067eb73c1b7SMiao Xie delalloc_root); 4068eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 4069eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 4070eb73c1b7SMiao Xie BUG_ON(!root); 4071eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4072eb73c1b7SMiao Xie 4073eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 4074eb73c1b7SMiao Xie btrfs_put_fs_root(root); 4075eb73c1b7SMiao Xie 4076eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 4077eb73c1b7SMiao Xie } 4078eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 4079acce952bSliubo } 4080acce952bSliubo 4081acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 4082acce952bSliubo struct extent_io_tree *dirty_pages, 4083acce952bSliubo int mark) 4084acce952bSliubo { 4085acce952bSliubo int ret; 4086acce952bSliubo struct extent_buffer *eb; 4087acce952bSliubo u64 start = 0; 4088acce952bSliubo u64 end; 4089acce952bSliubo 4090acce952bSliubo while (1) { 4091acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 4092e6138876SJosef Bacik mark, NULL); 4093acce952bSliubo if (ret) 4094acce952bSliubo break; 4095acce952bSliubo 4096acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 4097acce952bSliubo while (start <= end) { 40980308af44SDavid Sterba eb = btrfs_find_tree_block(root, start); 4099707e8a07SDavid Sterba start += root->nodesize; 4100fd8b2b61SJosef Bacik if (!eb) 4101acce952bSliubo continue; 4102fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4103acce952bSliubo 4104fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4105fd8b2b61SJosef Bacik &eb->bflags)) 4106fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4107fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4108acce952bSliubo } 4109acce952bSliubo } 4110acce952bSliubo 4111acce952bSliubo return ret; 4112acce952bSliubo } 4113acce952bSliubo 4114acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 4115acce952bSliubo struct extent_io_tree *pinned_extents) 4116acce952bSliubo { 4117acce952bSliubo struct extent_io_tree *unpin; 4118acce952bSliubo u64 start; 4119acce952bSliubo u64 end; 4120acce952bSliubo int ret; 4121ed0eaa14SLiu Bo bool loop = true; 4122acce952bSliubo 4123acce952bSliubo unpin = pinned_extents; 4124ed0eaa14SLiu Bo again: 4125acce952bSliubo while (1) { 4126acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4127e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4128acce952bSliubo if (ret) 4129acce952bSliubo break; 4130acce952bSliubo 4131acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 4132acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 4133acce952bSliubo cond_resched(); 4134acce952bSliubo } 4135acce952bSliubo 4136ed0eaa14SLiu Bo if (loop) { 4137ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 4138ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 4139ed0eaa14SLiu Bo else 4140ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 4141ed0eaa14SLiu Bo loop = false; 4142ed0eaa14SLiu Bo goto again; 4143ed0eaa14SLiu Bo } 4144ed0eaa14SLiu Bo 4145acce952bSliubo return 0; 4146acce952bSliubo } 4147acce952bSliubo 414850d9aa99SJosef Bacik static void btrfs_free_pending_ordered(struct btrfs_transaction *cur_trans, 414950d9aa99SJosef Bacik struct btrfs_fs_info *fs_info) 415050d9aa99SJosef Bacik { 415150d9aa99SJosef Bacik struct btrfs_ordered_extent *ordered; 415250d9aa99SJosef Bacik 415350d9aa99SJosef Bacik spin_lock(&fs_info->trans_lock); 415450d9aa99SJosef Bacik while (!list_empty(&cur_trans->pending_ordered)) { 415550d9aa99SJosef Bacik ordered = list_first_entry(&cur_trans->pending_ordered, 415650d9aa99SJosef Bacik struct btrfs_ordered_extent, 415750d9aa99SJosef Bacik trans_list); 415850d9aa99SJosef Bacik list_del_init(&ordered->trans_list); 415950d9aa99SJosef Bacik spin_unlock(&fs_info->trans_lock); 416050d9aa99SJosef Bacik 416150d9aa99SJosef Bacik btrfs_put_ordered_extent(ordered); 416250d9aa99SJosef Bacik spin_lock(&fs_info->trans_lock); 416350d9aa99SJosef Bacik } 416450d9aa99SJosef Bacik spin_unlock(&fs_info->trans_lock); 416550d9aa99SJosef Bacik } 416650d9aa99SJosef Bacik 416749b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 416849b25e05SJeff Mahoney struct btrfs_root *root) 416949b25e05SJeff Mahoney { 417049b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 417149b25e05SJeff Mahoney 41724a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 417349b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 417449b25e05SJeff Mahoney 41754a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 417649b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 417749b25e05SJeff Mahoney 417850d9aa99SJosef Bacik btrfs_free_pending_ordered(cur_trans, root->fs_info); 417967cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 418067cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 418167cde344SMiao Xie 418249b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 418349b25e05SJeff Mahoney EXTENT_DIRTY); 41846e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 41856e841e32SLiu Bo root->fs_info->pinned_extents); 418649b25e05SJeff Mahoney 41874a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 41884a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 41894a9d8bdeSMiao Xie 419049b25e05SJeff Mahoney /* 419149b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 419249b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 419349b25e05SJeff Mahoney */ 419449b25e05SJeff Mahoney } 419549b25e05SJeff Mahoney 419648a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root) 4197acce952bSliubo { 4198acce952bSliubo struct btrfs_transaction *t; 4199acce952bSliubo 4200acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 4201acce952bSliubo 4202a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4203724e2315SJosef Bacik while (!list_empty(&root->fs_info->trans_list)) { 4204724e2315SJosef Bacik t = list_first_entry(&root->fs_info->trans_list, 4205724e2315SJosef Bacik struct btrfs_transaction, list); 4206724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 4207724e2315SJosef Bacik atomic_inc(&t->use_count); 4208724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4209724e2315SJosef Bacik btrfs_wait_for_commit(root, t->transid); 4210724e2315SJosef Bacik btrfs_put_transaction(t); 4211724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4212724e2315SJosef Bacik continue; 4213724e2315SJosef Bacik } 4214724e2315SJosef Bacik if (t == root->fs_info->running_transaction) { 4215724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 4216724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4217724e2315SJosef Bacik /* 4218724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4219724e2315SJosef Bacik * handle open currently for this transaction. 4220724e2315SJosef Bacik */ 4221724e2315SJosef Bacik wait_event(t->writer_wait, 4222724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4223724e2315SJosef Bacik } else { 4224724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4225724e2315SJosef Bacik } 4226724e2315SJosef Bacik btrfs_cleanup_one_transaction(t, root); 4227724e2315SJosef Bacik 4228724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4229724e2315SJosef Bacik if (t == root->fs_info->running_transaction) 4230ac673879SMiao Xie root->fs_info->running_transaction = NULL; 4231724e2315SJosef Bacik list_del_init(&t->list); 4232a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4233a4abeea4SJosef Bacik 4234724e2315SJosef Bacik btrfs_put_transaction(t); 4235724e2315SJosef Bacik trace_btrfs_transaction_commit(root); 4236724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4237724e2315SJosef Bacik } 4238724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4239199c2a9cSMiao Xie btrfs_destroy_all_ordered_extents(root->fs_info); 424067cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 424167cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 4242724e2315SJosef Bacik btrfs_destroy_pinned_extent(root, root->fs_info->pinned_extents); 4243eb73c1b7SMiao Xie btrfs_destroy_all_delalloc_inodes(root->fs_info); 4244acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 4245acce952bSliubo 4246acce952bSliubo return 0; 4247acce952bSliubo } 4248acce952bSliubo 4249d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 4250ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 42514bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 42520b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4253239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4254239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 42550da5468fSChris Mason }; 4256