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" 428b712842SChris Mason #include "async-thread.h" 43925baeddSChris Mason #include "locking.h" 44e02119d5SChris Mason #include "tree-log.h" 45fa9c0d79SChris Mason #include "free-space-cache.h" 46581bb050SLi Zefan #include "inode-map.h" 4721adbd5cSStefan Behrens #include "check-integrity.h" 48606686eeSJosef Bacik #include "rcu-string.h" 498dabb742SStefan Behrens #include "dev-replace.h" 5053b381b3SDavid Woodhouse #include "raid56.h" 515ac1d209SJeff Mahoney #include "sysfs.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; 58fccb5d86SQu Wenruo static void end_workqueue_fn(struct btrfs_work_struct *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); 62569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 63569e0f35SJosef Bacik struct btrfs_root *root); 64143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 65acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 66acce952bSliubo struct btrfs_root *root); 67143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 68acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 69acce952bSliubo struct extent_io_tree *dirty_pages, 70acce952bSliubo int mark); 71acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 72acce952bSliubo struct extent_io_tree *pinned_extents); 7348a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root); 7448a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root); 75ce9adaa5SChris Mason 76d352ac68SChris Mason /* 77d352ac68SChris Mason * end_io_wq structs are used to do processing in task context when an IO is 78d352ac68SChris Mason * complete. This is used during reads to verify checksums, and it is used 79d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 80d352ac68SChris Mason */ 81ce9adaa5SChris Mason struct end_io_wq { 82ce9adaa5SChris Mason struct bio *bio; 83ce9adaa5SChris Mason bio_end_io_t *end_io; 84ce9adaa5SChris Mason void *private; 85ce9adaa5SChris Mason struct btrfs_fs_info *info; 86ce9adaa5SChris Mason int error; 8722c59948SChris Mason int metadata; 88ce9adaa5SChris Mason struct list_head list; 89fccb5d86SQu Wenruo struct btrfs_work_struct work; 90ce9adaa5SChris Mason }; 910da5468fSChris Mason 92d352ac68SChris Mason /* 93d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 94d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 95d352ac68SChris Mason * just before they are sent down the IO stack. 96d352ac68SChris Mason */ 9744b8bd7eSChris Mason struct async_submit_bio { 9844b8bd7eSChris Mason struct inode *inode; 9944b8bd7eSChris Mason struct bio *bio; 10044b8bd7eSChris Mason struct list_head list; 1014a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1024a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 10344b8bd7eSChris Mason int rw; 10444b8bd7eSChris Mason int mirror_num; 105c8b97818SChris Mason unsigned long bio_flags; 106eaf25d93SChris Mason /* 107eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 108eaf25d93SChris Mason * can't tell us where in the file the bio should go 109eaf25d93SChris Mason */ 110eaf25d93SChris Mason u64 bio_offset; 1115cdc7ad3SQu Wenruo struct btrfs_work_struct work; 11279787eaaSJeff Mahoney int error; 11344b8bd7eSChris Mason }; 11444b8bd7eSChris Mason 11585d4e461SChris Mason /* 11685d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 11785d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 11885d4e461SChris Mason * the level the eb occupies in the tree. 1194008c04aSChris Mason * 12085d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12185d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 12285d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 12385d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 12485d4e461SChris Mason * have separate keysets. 1254008c04aSChris Mason * 12685d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 12785d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 12885d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1294008c04aSChris Mason * 13085d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13185d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 13285d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 13385d4e461SChris Mason * 13485d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 13585d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 13685d4e461SChris Mason * needs update as well. 1374008c04aSChris Mason */ 1384008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1394008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1404008c04aSChris Mason # error 1414008c04aSChris Mason # endif 14285d4e461SChris Mason 14385d4e461SChris Mason static struct btrfs_lockdep_keyset { 14485d4e461SChris Mason u64 id; /* root objectid */ 14585d4e461SChris Mason const char *name_stem; /* lock name stem */ 14685d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 14785d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 14885d4e461SChris Mason } btrfs_lockdep_keysets[] = { 14985d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 15085d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 15185d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 15285d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 15385d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 15485d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 15560b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 15685d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 15785d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 15885d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 15913fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 16085d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1614008c04aSChris Mason }; 16285d4e461SChris Mason 16385d4e461SChris Mason void __init btrfs_init_lockdep(void) 16485d4e461SChris Mason { 16585d4e461SChris Mason int i, j; 16685d4e461SChris Mason 16785d4e461SChris Mason /* initialize lockdep class names */ 16885d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 16985d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 17085d4e461SChris Mason 17185d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 17285d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 17385d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 17485d4e461SChris Mason } 17585d4e461SChris Mason } 17685d4e461SChris Mason 17785d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 17885d4e461SChris Mason int level) 17985d4e461SChris Mason { 18085d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 18185d4e461SChris Mason 18285d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 18385d4e461SChris Mason 18485d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 18585d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 18685d4e461SChris Mason if (ks->id == objectid) 18785d4e461SChris Mason break; 18885d4e461SChris Mason 18985d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 19085d4e461SChris Mason &ks->keys[level], ks->names[level]); 19185d4e461SChris Mason } 19285d4e461SChris Mason 1934008c04aSChris Mason #endif 1944008c04aSChris Mason 195d352ac68SChris Mason /* 196d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 197d352ac68SChris Mason * that covers the entire device 198d352ac68SChris Mason */ 199b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 200306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2015f39d397SChris Mason int create) 2025f39d397SChris Mason { 2035f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2045f39d397SChris Mason struct extent_map *em; 2055f39d397SChris Mason int ret; 2065f39d397SChris Mason 207890871beSChris Mason read_lock(&em_tree->lock); 208d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 209a061fc8dSChris Mason if (em) { 210a061fc8dSChris Mason em->bdev = 211a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 212890871beSChris Mason read_unlock(&em_tree->lock); 2135f39d397SChris Mason goto out; 214a061fc8dSChris Mason } 215890871beSChris Mason read_unlock(&em_tree->lock); 2167b13b7b1SChris Mason 217172ddd60SDavid Sterba em = alloc_extent_map(); 2185f39d397SChris Mason if (!em) { 2195f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2205f39d397SChris Mason goto out; 2215f39d397SChris Mason } 2225f39d397SChris Mason em->start = 0; 2230afbaf8cSChris Mason em->len = (u64)-1; 224c8b97818SChris Mason em->block_len = (u64)-1; 2255f39d397SChris Mason em->block_start = 0; 226a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 227d1310b2eSChris Mason 228890871beSChris Mason write_lock(&em_tree->lock); 22909a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2305f39d397SChris Mason if (ret == -EEXIST) { 2315f39d397SChris Mason free_extent_map(em); 2327b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 233b4f359abSTsutomu Itoh if (!em) 2340433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2355f39d397SChris Mason } else if (ret) { 2367b13b7b1SChris Mason free_extent_map(em); 2370433f20dSTsutomu Itoh em = ERR_PTR(ret); 2385f39d397SChris Mason } 239890871beSChris Mason write_unlock(&em_tree->lock); 2407b13b7b1SChris Mason 2415f39d397SChris Mason out: 2425f39d397SChris Mason return em; 2435f39d397SChris Mason } 2445f39d397SChris Mason 245b0496686SLiu Bo u32 btrfs_csum_data(char *data, u32 seed, size_t len) 24619c00ddcSChris Mason { 2470b947affSFilipe David Borba Manana return btrfs_crc32c(seed, data, len); 24819c00ddcSChris Mason } 24919c00ddcSChris Mason 25019c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 25119c00ddcSChris Mason { 2527e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 25319c00ddcSChris Mason } 25419c00ddcSChris Mason 255d352ac68SChris Mason /* 256d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 257d352ac68SChris Mason * into the csum field of the block. 258d352ac68SChris Mason */ 25919c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 26019c00ddcSChris Mason int verify) 26119c00ddcSChris Mason { 2626c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 263607d432dSJosef Bacik char *result = NULL; 26419c00ddcSChris Mason unsigned long len; 26519c00ddcSChris Mason unsigned long cur_len; 26619c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26719c00ddcSChris Mason char *kaddr; 26819c00ddcSChris Mason unsigned long map_start; 26919c00ddcSChris Mason unsigned long map_len; 27019c00ddcSChris Mason int err; 27119c00ddcSChris Mason u32 crc = ~(u32)0; 272607d432dSJosef Bacik unsigned long inline_result; 27319c00ddcSChris Mason 27419c00ddcSChris Mason len = buf->len - offset; 27519c00ddcSChris Mason while (len > 0) { 27619c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 277a6591715SChris Mason &kaddr, &map_start, &map_len); 278d397712bSChris Mason if (err) 27919c00ddcSChris Mason return 1; 28019c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 281b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 28219c00ddcSChris Mason crc, cur_len); 28319c00ddcSChris Mason len -= cur_len; 28419c00ddcSChris Mason offset += cur_len; 28519c00ddcSChris Mason } 286607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 287607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 288607d432dSJosef Bacik if (!result) 289607d432dSJosef Bacik return 1; 290607d432dSJosef Bacik } else { 291607d432dSJosef Bacik result = (char *)&inline_result; 292607d432dSJosef Bacik } 293607d432dSJosef Bacik 29419c00ddcSChris Mason btrfs_csum_final(crc, result); 29519c00ddcSChris Mason 29619c00ddcSChris Mason if (verify) { 297607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 298e4204dedSChris Mason u32 val; 299e4204dedSChris Mason u32 found = 0; 300607d432dSJosef Bacik memcpy(&found, result, csum_size); 301e4204dedSChris Mason 302607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 303efe120a0SFrank Holton printk_ratelimited(KERN_INFO 304efe120a0SFrank Holton "BTRFS: %s checksum verify failed on %llu wanted %X found %X " 305193f284dSChris Mason "level %d\n", 306c1c9ff7cSGeert Uytterhoeven root->fs_info->sb->s_id, buf->start, 307c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 308607d432dSJosef Bacik if (result != (char *)&inline_result) 309607d432dSJosef Bacik kfree(result); 31019c00ddcSChris Mason return 1; 31119c00ddcSChris Mason } 31219c00ddcSChris Mason } else { 313607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 31419c00ddcSChris Mason } 315607d432dSJosef Bacik if (result != (char *)&inline_result) 316607d432dSJosef Bacik kfree(result); 31719c00ddcSChris Mason return 0; 31819c00ddcSChris Mason } 31919c00ddcSChris Mason 320d352ac68SChris Mason /* 321d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 322d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 323d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 324d352ac68SChris Mason * in the wrong place. 325d352ac68SChris Mason */ 3261259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 327b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 328b9fab919SChris Mason int atomic) 3291259ab75SChris Mason { 3302ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3311259ab75SChris Mason int ret; 3321259ab75SChris Mason 3331259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3341259ab75SChris Mason return 0; 3351259ab75SChris Mason 336b9fab919SChris Mason if (atomic) 337b9fab919SChris Mason return -EAGAIN; 338b9fab919SChris Mason 3392ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 340d0082371SJeff Mahoney 0, &cached_state); 3410b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3421259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3431259ab75SChris Mason ret = 0; 3441259ab75SChris Mason goto out; 3451259ab75SChris Mason } 3467a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 347193f284dSChris Mason "found %llu\n", 348c1c9ff7cSGeert Uytterhoeven eb->start, parent_transid, btrfs_header_generation(eb)); 3491259ab75SChris Mason ret = 1; 3500b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 35133958dc6SChris Mason out: 3522ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3532ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3541259ab75SChris Mason return ret; 3551259ab75SChris Mason } 3561259ab75SChris Mason 357d352ac68SChris Mason /* 3581104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3591104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3601104a885SDavid Sterba */ 3611104a885SDavid Sterba static int btrfs_check_super_csum(char *raw_disk_sb) 3621104a885SDavid Sterba { 3631104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3641104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 3651104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 3661104a885SDavid Sterba int ret = 0; 3671104a885SDavid Sterba 3681104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 3691104a885SDavid Sterba u32 crc = ~(u32)0; 3701104a885SDavid Sterba const int csum_size = sizeof(crc); 3711104a885SDavid Sterba char result[csum_size]; 3721104a885SDavid Sterba 3731104a885SDavid Sterba /* 3741104a885SDavid Sterba * The super_block structure does not span the whole 3751104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 3761104a885SDavid Sterba * is filled with zeros and is included in the checkum. 3771104a885SDavid Sterba */ 3781104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 3791104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3801104a885SDavid Sterba btrfs_csum_final(crc, result); 3811104a885SDavid Sterba 3821104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 3831104a885SDavid Sterba ret = 1; 384667e7d94SChris Mason 385667e7d94SChris Mason if (ret && btrfs_super_generation(disk_sb) < 10) { 386efe120a0SFrank Holton printk(KERN_WARNING 387efe120a0SFrank Holton "BTRFS: super block crcs don't match, older mkfs detected\n"); 388667e7d94SChris Mason ret = 0; 389667e7d94SChris Mason } 3901104a885SDavid Sterba } 3911104a885SDavid Sterba 3921104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 393efe120a0SFrank Holton printk(KERN_ERR "BTRFS: unsupported checksum algorithm %u\n", 3941104a885SDavid Sterba csum_type); 3951104a885SDavid Sterba ret = 1; 3961104a885SDavid Sterba } 3971104a885SDavid Sterba 3981104a885SDavid Sterba return ret; 3991104a885SDavid Sterba } 4001104a885SDavid Sterba 4011104a885SDavid Sterba /* 402d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 403d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 404d352ac68SChris Mason */ 405f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 406f188591eSChris Mason struct extent_buffer *eb, 407ca7a79adSChris Mason u64 start, u64 parent_transid) 408f188591eSChris Mason { 409f188591eSChris Mason struct extent_io_tree *io_tree; 410ea466794SJosef Bacik int failed = 0; 411f188591eSChris Mason int ret; 412f188591eSChris Mason int num_copies = 0; 413f188591eSChris Mason int mirror_num = 0; 414ea466794SJosef Bacik int failed_mirror = 0; 415f188591eSChris Mason 416a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 417f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 418f188591eSChris Mason while (1) { 419bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 420bb82ab88SArne Jansen WAIT_COMPLETE, 421f188591eSChris Mason btree_get_extent, mirror_num); 422256dd1bbSStefan Behrens if (!ret) { 423256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 424b9fab919SChris Mason parent_transid, 0)) 425ea466794SJosef Bacik break; 426256dd1bbSStefan Behrens else 427256dd1bbSStefan Behrens ret = -EIO; 428256dd1bbSStefan Behrens } 429d397712bSChris Mason 430a826d6dcSJosef Bacik /* 431a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 432a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 433a826d6dcSJosef Bacik * any less wrong. 434a826d6dcSJosef Bacik */ 435a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 436ea466794SJosef Bacik break; 437ea466794SJosef Bacik 4385d964051SStefan Behrens num_copies = btrfs_num_copies(root->fs_info, 439f188591eSChris Mason eb->start, eb->len); 4404235298eSChris Mason if (num_copies == 1) 441ea466794SJosef Bacik break; 4424235298eSChris Mason 4435cf1ab56SJosef Bacik if (!failed_mirror) { 4445cf1ab56SJosef Bacik failed = 1; 4455cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4465cf1ab56SJosef Bacik } 4475cf1ab56SJosef Bacik 448f188591eSChris Mason mirror_num++; 449ea466794SJosef Bacik if (mirror_num == failed_mirror) 450ea466794SJosef Bacik mirror_num++; 451ea466794SJosef Bacik 4524235298eSChris Mason if (mirror_num > num_copies) 453ea466794SJosef Bacik break; 454f188591eSChris Mason } 455ea466794SJosef Bacik 456c0901581SStefan Behrens if (failed && !ret && failed_mirror) 457ea466794SJosef Bacik repair_eb_io_failure(root, eb, failed_mirror); 458ea466794SJosef Bacik 459ea466794SJosef Bacik return ret; 460f188591eSChris Mason } 46119c00ddcSChris Mason 462d352ac68SChris Mason /* 463d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 464d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 465d352ac68SChris Mason */ 466d397712bSChris Mason 467b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 46819c00ddcSChris Mason { 4694eee4fa4SMiao Xie u64 start = page_offset(page); 47019c00ddcSChris Mason u64 found_start; 47119c00ddcSChris Mason struct extent_buffer *eb; 472f188591eSChris Mason 4734f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4744f2de97aSJosef Bacik if (page != eb->pages[0]) 4754f2de97aSJosef Bacik return 0; 47619c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 477fae7f21cSDulshani Gunawardhana if (WARN_ON(found_start != start || !PageUptodate(page))) 4784f2de97aSJosef Bacik return 0; 47919c00ddcSChris Mason csum_tree_block(root, eb, 0); 48019c00ddcSChris Mason return 0; 48119c00ddcSChris Mason } 48219c00ddcSChris Mason 4832b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 4842b82032cSYan Zheng struct extent_buffer *eb) 4852b82032cSYan Zheng { 4862b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 4872b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 4882b82032cSYan Zheng int ret = 1; 4892b82032cSYan Zheng 4900a4e5586SRoss Kirk read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE); 4912b82032cSYan Zheng while (fs_devices) { 4922b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 4932b82032cSYan Zheng ret = 0; 4942b82032cSYan Zheng break; 4952b82032cSYan Zheng } 4962b82032cSYan Zheng fs_devices = fs_devices->seed; 4972b82032cSYan Zheng } 4982b82032cSYan Zheng return ret; 4992b82032cSYan Zheng } 5002b82032cSYan Zheng 501a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 502efe120a0SFrank Holton btrfs_crit(root->fs_info, "corrupt leaf, %s: block=%llu," \ 503efe120a0SFrank Holton "root=%llu, slot=%d", reason, \ 504c1c9ff7cSGeert Uytterhoeven btrfs_header_bytenr(eb), root->objectid, slot) 505a826d6dcSJosef Bacik 506a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 507a826d6dcSJosef Bacik struct extent_buffer *leaf) 508a826d6dcSJosef Bacik { 509a826d6dcSJosef Bacik struct btrfs_key key; 510a826d6dcSJosef Bacik struct btrfs_key leaf_key; 511a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 512a826d6dcSJosef Bacik int slot; 513a826d6dcSJosef Bacik 514a826d6dcSJosef Bacik if (nritems == 0) 515a826d6dcSJosef Bacik return 0; 516a826d6dcSJosef Bacik 517a826d6dcSJosef Bacik /* Check the 0 item */ 518a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 519a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 520a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 521a826d6dcSJosef Bacik return -EIO; 522a826d6dcSJosef Bacik } 523a826d6dcSJosef Bacik 524a826d6dcSJosef Bacik /* 525a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 526a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 527a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 528a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 529a826d6dcSJosef Bacik * offset is correct. 530a826d6dcSJosef Bacik */ 531a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 532a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 533a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 534a826d6dcSJosef Bacik 535a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 536a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 537a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 538a826d6dcSJosef Bacik return -EIO; 539a826d6dcSJosef Bacik } 540a826d6dcSJosef Bacik 541a826d6dcSJosef Bacik /* 542a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 543a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 544a826d6dcSJosef Bacik * front. 545a826d6dcSJosef Bacik */ 546a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 547a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 548a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 549a826d6dcSJosef Bacik return -EIO; 550a826d6dcSJosef Bacik } 551a826d6dcSJosef Bacik 552a826d6dcSJosef Bacik /* 553a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 554a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 555a826d6dcSJosef Bacik * all point outside of the leaf. 556a826d6dcSJosef Bacik */ 557a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 558a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 559a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 560a826d6dcSJosef Bacik return -EIO; 561a826d6dcSJosef Bacik } 562a826d6dcSJosef Bacik } 563a826d6dcSJosef Bacik 564a826d6dcSJosef Bacik return 0; 565a826d6dcSJosef Bacik } 566a826d6dcSJosef Bacik 567facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 568facc8a22SMiao Xie u64 phy_offset, struct page *page, 569facc8a22SMiao Xie u64 start, u64 end, int mirror) 570ce9adaa5SChris Mason { 571ce9adaa5SChris Mason u64 found_start; 572ce9adaa5SChris Mason int found_level; 573ce9adaa5SChris Mason struct extent_buffer *eb; 574ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 575f188591eSChris Mason int ret = 0; 576727011e0SChris Mason int reads_done; 577ce9adaa5SChris Mason 578ce9adaa5SChris Mason if (!page->private) 579ce9adaa5SChris Mason goto out; 580d397712bSChris Mason 5814f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 582d397712bSChris Mason 5830b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5840b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5850b32f4bbSJosef Bacik */ 5860b32f4bbSJosef Bacik extent_buffer_get(eb); 5870b32f4bbSJosef Bacik 5880b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 589727011e0SChris Mason if (!reads_done) 590727011e0SChris Mason goto err; 591f188591eSChris Mason 5925cf1ab56SJosef Bacik eb->read_mirror = mirror; 593ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 594ea466794SJosef Bacik ret = -EIO; 595ea466794SJosef Bacik goto err; 596ea466794SJosef Bacik } 597ea466794SJosef Bacik 598ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 599727011e0SChris Mason if (found_start != eb->start) { 600efe120a0SFrank Holton printk_ratelimited(KERN_INFO "BTRFS: bad tree block start " 601193f284dSChris Mason "%llu %llu\n", 602c1c9ff7cSGeert Uytterhoeven found_start, eb->start); 603f188591eSChris Mason ret = -EIO; 604ce9adaa5SChris Mason goto err; 605ce9adaa5SChris Mason } 6062b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 607efe120a0SFrank Holton printk_ratelimited(KERN_INFO "BTRFS: bad fsid on block %llu\n", 608c1c9ff7cSGeert Uytterhoeven eb->start); 6091259ab75SChris Mason ret = -EIO; 6101259ab75SChris Mason goto err; 6111259ab75SChris Mason } 612ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6131c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 614efe120a0SFrank Holton btrfs_info(root->fs_info, "bad tree block level %d", 6151c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6161c24c3ceSJosef Bacik ret = -EIO; 6171c24c3ceSJosef Bacik goto err; 6181c24c3ceSJosef Bacik } 619ce9adaa5SChris Mason 62085d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 62185d4e461SChris Mason eb, found_level); 6224008c04aSChris Mason 623ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 624a826d6dcSJosef Bacik if (ret) { 625f188591eSChris Mason ret = -EIO; 626a826d6dcSJosef Bacik goto err; 627a826d6dcSJosef Bacik } 628a826d6dcSJosef Bacik 629a826d6dcSJosef Bacik /* 630a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 631a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 632a826d6dcSJosef Bacik * return -EIO. 633a826d6dcSJosef Bacik */ 634a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 635a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 636a826d6dcSJosef Bacik ret = -EIO; 637a826d6dcSJosef Bacik } 638ce9adaa5SChris Mason 6390b32f4bbSJosef Bacik if (!ret) 6400b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 641ce9adaa5SChris Mason err: 64279fb65a1SJosef Bacik if (reads_done && 64379fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6444bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6454bb31e92SArne Jansen 64653b381b3SDavid Woodhouse if (ret) { 64753b381b3SDavid Woodhouse /* 64853b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 64953b381b3SDavid Woodhouse * again, we have to make sure it has something 65053b381b3SDavid Woodhouse * to decrement 65153b381b3SDavid Woodhouse */ 65253b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6530b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 65453b381b3SDavid Woodhouse } 6550b32f4bbSJosef Bacik free_extent_buffer(eb); 656ce9adaa5SChris Mason out: 657f188591eSChris Mason return ret; 658ce9adaa5SChris Mason } 659ce9adaa5SChris Mason 660ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6614bb31e92SArne Jansen { 6624bb31e92SArne Jansen struct extent_buffer *eb; 6634bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6644bb31e92SArne Jansen 6654f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 666ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6675cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 66853b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 669ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6704bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6714bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6724bb31e92SArne Jansen } 6734bb31e92SArne Jansen 674ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 675ce9adaa5SChris Mason { 676ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 677ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 678ce9adaa5SChris Mason 679ce9adaa5SChris Mason fs_info = end_io_wq->info; 680ce9adaa5SChris Mason end_io_wq->error = err; 681fccb5d86SQu Wenruo btrfs_init_work(&end_io_wq->work, end_workqueue_fn, NULL, NULL); 682d20f7043SChris Mason 6837b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 68453b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 685fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_meta_write_workers, 686cad321adSChris Mason &end_io_wq->work); 68753b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 688fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_freespace_worker, 6890cb59c99SJosef Bacik &end_io_wq->work); 69053b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 691fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_raid56_workers, 69253b381b3SDavid Woodhouse &end_io_wq->work); 693cad321adSChris Mason else 694fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_write_workers, 695e6dcd2dcSChris Mason &end_io_wq->work); 696d20f7043SChris Mason } else { 69753b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 698fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_raid56_workers, 69953b381b3SDavid Woodhouse &end_io_wq->work); 70053b381b3SDavid Woodhouse else if (end_io_wq->metadata) 701fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_meta_workers, 702d20f7043SChris Mason &end_io_wq->work); 703e6dcd2dcSChris Mason else 704fccb5d86SQu Wenruo btrfs_queue_work(fs_info->endio_workers, 705d20f7043SChris Mason &end_io_wq->work); 706d20f7043SChris Mason } 707ce9adaa5SChris Mason } 708ce9adaa5SChris Mason 7090cb59c99SJosef Bacik /* 7100cb59c99SJosef Bacik * For the metadata arg you want 7110cb59c99SJosef Bacik * 7120cb59c99SJosef Bacik * 0 - if data 7130cb59c99SJosef Bacik * 1 - if normal metadta 7140cb59c99SJosef Bacik * 2 - if writing to the free space cache area 71553b381b3SDavid Woodhouse * 3 - raid parity work 7160cb59c99SJosef Bacik */ 71722c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 71822c59948SChris Mason int metadata) 7190b86a832SChris Mason { 720ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 721ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 722ce9adaa5SChris Mason if (!end_io_wq) 723ce9adaa5SChris Mason return -ENOMEM; 724ce9adaa5SChris Mason 725ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 726ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 72722c59948SChris Mason end_io_wq->info = info; 728ce9adaa5SChris Mason end_io_wq->error = 0; 729ce9adaa5SChris Mason end_io_wq->bio = bio; 73022c59948SChris Mason end_io_wq->metadata = metadata; 731ce9adaa5SChris Mason 732ce9adaa5SChris Mason bio->bi_private = end_io_wq; 733ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 73422c59948SChris Mason return 0; 73522c59948SChris Mason } 73622c59948SChris Mason 737b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7384854ddd0SChris Mason { 7394854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7405cdc7ad3SQu Wenruo info->thread_pool_size, 7414854ddd0SChris Mason info->fs_devices->open_devices); 7424854ddd0SChris Mason return 256 * limit; 7434854ddd0SChris Mason } 7444854ddd0SChris Mason 7455cdc7ad3SQu Wenruo static void run_one_async_start(struct btrfs_work_struct *work) 7464a69a410SChris Mason { 7474a69a410SChris Mason struct async_submit_bio *async; 74879787eaaSJeff Mahoney int ret; 7494a69a410SChris Mason 7504a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 75179787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 752eaf25d93SChris Mason async->mirror_num, async->bio_flags, 753eaf25d93SChris Mason async->bio_offset); 75479787eaaSJeff Mahoney if (ret) 75579787eaaSJeff Mahoney async->error = ret; 7564a69a410SChris Mason } 7574a69a410SChris Mason 7585cdc7ad3SQu Wenruo static void run_one_async_done(struct btrfs_work_struct *work) 7598b712842SChris Mason { 7608b712842SChris Mason struct btrfs_fs_info *fs_info; 7618b712842SChris Mason struct async_submit_bio *async; 7624854ddd0SChris Mason int limit; 7638b712842SChris Mason 7648b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7658b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7664854ddd0SChris Mason 767b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7684854ddd0SChris Mason limit = limit * 2 / 3; 7694854ddd0SChris Mason 77066657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 771b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7724854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7734854ddd0SChris Mason 77479787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 77579787eaaSJeff Mahoney if (async->error) { 77679787eaaSJeff Mahoney bio_endio(async->bio, async->error); 77779787eaaSJeff Mahoney return; 77879787eaaSJeff Mahoney } 77979787eaaSJeff Mahoney 7804a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 781eaf25d93SChris Mason async->mirror_num, async->bio_flags, 782eaf25d93SChris Mason async->bio_offset); 7834a69a410SChris Mason } 7844a69a410SChris Mason 7855cdc7ad3SQu Wenruo static void run_one_async_free(struct btrfs_work_struct *work) 7864a69a410SChris Mason { 7874a69a410SChris Mason struct async_submit_bio *async; 7884a69a410SChris Mason 7894a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7908b712842SChris Mason kfree(async); 7918b712842SChris Mason } 7928b712842SChris Mason 79344b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 79444b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 795c8b97818SChris Mason unsigned long bio_flags, 796eaf25d93SChris Mason u64 bio_offset, 7974a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7984a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 79944b8bd7eSChris Mason { 80044b8bd7eSChris Mason struct async_submit_bio *async; 80144b8bd7eSChris Mason 80244b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 80344b8bd7eSChris Mason if (!async) 80444b8bd7eSChris Mason return -ENOMEM; 80544b8bd7eSChris Mason 80644b8bd7eSChris Mason async->inode = inode; 80744b8bd7eSChris Mason async->rw = rw; 80844b8bd7eSChris Mason async->bio = bio; 80944b8bd7eSChris Mason async->mirror_num = mirror_num; 8104a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8114a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8124a69a410SChris Mason 8135cdc7ad3SQu Wenruo btrfs_init_work(&async->work, run_one_async_start, 8145cdc7ad3SQu Wenruo run_one_async_done, run_one_async_free); 8154a69a410SChris Mason 816c8b97818SChris Mason async->bio_flags = bio_flags; 817eaf25d93SChris Mason async->bio_offset = bio_offset; 8188c8bee1dSChris Mason 81979787eaaSJeff Mahoney async->error = 0; 82079787eaaSJeff Mahoney 821cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 822d313d7a3SChris Mason 8237b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 8245cdc7ad3SQu Wenruo btrfs_set_work_high_priority(&async->work); 825d313d7a3SChris Mason 8265cdc7ad3SQu Wenruo btrfs_queue_work(fs_info->workers, &async->work); 8279473f16cSChris Mason 828771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 829771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 830771ed689SChris Mason wait_event(fs_info->async_submit_wait, 831771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 832771ed689SChris Mason } 833771ed689SChris Mason 83444b8bd7eSChris Mason return 0; 83544b8bd7eSChris Mason } 83644b8bd7eSChris Mason 837ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 838ce3ed71aSChris Mason { 839ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 840ce3ed71aSChris Mason int bio_index = 0; 841ce3ed71aSChris Mason struct btrfs_root *root; 84279787eaaSJeff Mahoney int ret = 0; 843ce3ed71aSChris Mason 844ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 845ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 846ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 84779787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 84879787eaaSJeff Mahoney if (ret) 84979787eaaSJeff Mahoney break; 850ce3ed71aSChris Mason bio_index++; 851ce3ed71aSChris Mason bvec++; 852ce3ed71aSChris Mason } 85379787eaaSJeff Mahoney return ret; 854ce3ed71aSChris Mason } 855ce3ed71aSChris Mason 8564a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8574a69a410SChris Mason struct bio *bio, int mirror_num, 858eaf25d93SChris Mason unsigned long bio_flags, 859eaf25d93SChris Mason u64 bio_offset) 86022c59948SChris Mason { 8618b712842SChris Mason /* 8628b712842SChris Mason * when we're called for a write, we're already in the async 8635443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8648b712842SChris Mason */ 86579787eaaSJeff Mahoney return btree_csum_one_bio(bio); 86622c59948SChris Mason } 86722c59948SChris Mason 8684a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 869eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 870eaf25d93SChris Mason u64 bio_offset) 8714a69a410SChris Mason { 87261891923SStefan Behrens int ret; 87361891923SStefan Behrens 8748b712842SChris Mason /* 8754a69a410SChris Mason * when we're called for a write, we're already in the async 8764a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8778b712842SChris Mason */ 87861891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 87961891923SStefan Behrens if (ret) 88061891923SStefan Behrens bio_endio(bio, ret); 88161891923SStefan Behrens return ret; 8820b86a832SChris Mason } 8830b86a832SChris Mason 884de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 885de0022b9SJosef Bacik { 886de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 887de0022b9SJosef Bacik return 0; 888de0022b9SJosef Bacik #ifdef CONFIG_X86 889de0022b9SJosef Bacik if (cpu_has_xmm4_2) 890de0022b9SJosef Bacik return 0; 891de0022b9SJosef Bacik #endif 892de0022b9SJosef Bacik return 1; 893de0022b9SJosef Bacik } 894de0022b9SJosef Bacik 89544b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 896eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 897eaf25d93SChris Mason u64 bio_offset) 89844b8bd7eSChris Mason { 899de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 9004a69a410SChris Mason int ret; 901cad321adSChris Mason 9027b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 903cad321adSChris Mason /* 904cad321adSChris Mason * called for a read, do the setup so that checksum validation 905cad321adSChris Mason * can happen in the async kernel threads 906cad321adSChris Mason */ 907f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 908f3f266abSChris Mason bio, 1); 9091d4284bdSChris Mason if (ret) 91061891923SStefan Behrens goto out_w_error; 91161891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 9126f3577bdSChris Mason mirror_num, 0); 913de0022b9SJosef Bacik } else if (!async) { 914de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 915de0022b9SJosef Bacik if (ret) 91661891923SStefan Behrens goto out_w_error; 91761891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 918de0022b9SJosef Bacik mirror_num, 0); 91961891923SStefan Behrens } else { 920cad321adSChris Mason /* 92161891923SStefan Behrens * kthread helpers are used to submit writes so that 92261891923SStefan Behrens * checksumming can happen in parallel across all CPUs 923cad321adSChris Mason */ 92461891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 925c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 926eaf25d93SChris Mason bio_offset, 9274a69a410SChris Mason __btree_submit_bio_start, 9284a69a410SChris Mason __btree_submit_bio_done); 92944b8bd7eSChris Mason } 93044b8bd7eSChris Mason 93161891923SStefan Behrens if (ret) { 93261891923SStefan Behrens out_w_error: 93361891923SStefan Behrens bio_endio(bio, ret); 93461891923SStefan Behrens } 93561891923SStefan Behrens return ret; 93661891923SStefan Behrens } 93761891923SStefan Behrens 9383dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 939784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 940a6bc32b8SMel Gorman struct page *newpage, struct page *page, 941a6bc32b8SMel Gorman enum migrate_mode mode) 942784b4e29SChris Mason { 943784b4e29SChris Mason /* 944784b4e29SChris Mason * we can't safely write a btree page from here, 945784b4e29SChris Mason * we haven't done the locking hook 946784b4e29SChris Mason */ 947784b4e29SChris Mason if (PageDirty(page)) 948784b4e29SChris Mason return -EAGAIN; 949784b4e29SChris Mason /* 950784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 951784b4e29SChris Mason * We must have no buffers or drop them. 952784b4e29SChris Mason */ 953784b4e29SChris Mason if (page_has_private(page) && 954784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 955784b4e29SChris Mason return -EAGAIN; 956a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 957784b4e29SChris Mason } 9583dd1462eSJan Beulich #endif 959784b4e29SChris Mason 9600da5468fSChris Mason 9610da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9620da5468fSChris Mason struct writeback_control *wbc) 9630da5468fSChris Mason { 964e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 965e2d84521SMiao Xie int ret; 966e2d84521SMiao Xie 967d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 968448d640bSChris Mason 969448d640bSChris Mason if (wbc->for_kupdate) 970448d640bSChris Mason return 0; 971448d640bSChris Mason 972e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 973b9473439SChris Mason /* this is a bit racy, but that's ok */ 974e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 975e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 976e2d84521SMiao Xie if (ret < 0) 977793955bcSChris Mason return 0; 978793955bcSChris Mason } 9790b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9800da5468fSChris Mason } 9810da5468fSChris Mason 982b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9835f39d397SChris Mason { 984d1310b2eSChris Mason struct extent_io_tree *tree; 985d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9868ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9875f39d397SChris Mason } 9885f39d397SChris Mason 98970dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9905f39d397SChris Mason { 99198509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 99298509cfcSChris Mason return 0; 9930c4e538bSDavid Sterba 994f7a52a40SDavid Sterba return try_release_extent_buffer(page); 995d98237b3SChris Mason } 996d98237b3SChris Mason 997d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 998d47992f8SLukas Czerner unsigned int length) 999d98237b3SChris Mason { 1000d1310b2eSChris Mason struct extent_io_tree *tree; 1001d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10025f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10035f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10049ad6b7bcSChris Mason if (PagePrivate(page)) { 1005efe120a0SFrank Holton btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info, 1006efe120a0SFrank Holton "page private not zero on page %llu", 1007efe120a0SFrank Holton (unsigned long long)page_offset(page)); 10089ad6b7bcSChris Mason ClearPagePrivate(page); 10099ad6b7bcSChris Mason set_page_private(page, 0); 10109ad6b7bcSChris Mason page_cache_release(page); 10119ad6b7bcSChris Mason } 1012d98237b3SChris Mason } 1013d98237b3SChris Mason 10140b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10150b32f4bbSJosef Bacik { 1016bb146eb2SJosef Bacik #ifdef DEBUG 10170b32f4bbSJosef Bacik struct extent_buffer *eb; 10180b32f4bbSJosef Bacik 10190b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10200b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10210b32f4bbSJosef Bacik BUG_ON(!eb); 10220b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10230b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10240b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1025bb146eb2SJosef Bacik #endif 10260b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10270b32f4bbSJosef Bacik } 10280b32f4bbSJosef Bacik 10297f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1030d98237b3SChris Mason .readpage = btree_readpage, 10310da5468fSChris Mason .writepages = btree_writepages, 10325f39d397SChris Mason .releasepage = btree_releasepage, 10335f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10345a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1035784b4e29SChris Mason .migratepage = btree_migratepage, 10365a92bc88SChris Mason #endif 10370b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1038d98237b3SChris Mason }; 1039123abc88SChris Mason 1040ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1041ca7a79adSChris Mason u64 parent_transid) 1042090d1875SChris Mason { 10435f39d397SChris Mason struct extent_buffer *buf = NULL; 10445f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1045de428b63SChris Mason int ret = 0; 1046090d1875SChris Mason 1047db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10485f39d397SChris Mason if (!buf) 1049090d1875SChris Mason return 0; 1050d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1051bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10525f39d397SChris Mason free_extent_buffer(buf); 1053de428b63SChris Mason return ret; 1054090d1875SChris Mason } 1055090d1875SChris Mason 1056ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1057ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1058ab0fff03SArne Jansen { 1059ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1060ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1061ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1062ab0fff03SArne Jansen int ret; 1063ab0fff03SArne Jansen 1064ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1065ab0fff03SArne Jansen if (!buf) 1066ab0fff03SArne Jansen return 0; 1067ab0fff03SArne Jansen 1068ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1069ab0fff03SArne Jansen 1070ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1071ab0fff03SArne Jansen btree_get_extent, mirror_num); 1072ab0fff03SArne Jansen if (ret) { 1073ab0fff03SArne Jansen free_extent_buffer(buf); 1074ab0fff03SArne Jansen return ret; 1075ab0fff03SArne Jansen } 1076ab0fff03SArne Jansen 1077ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1078ab0fff03SArne Jansen free_extent_buffer(buf); 1079ab0fff03SArne Jansen return -EIO; 10800b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1081ab0fff03SArne Jansen *eb = buf; 1082ab0fff03SArne Jansen } else { 1083ab0fff03SArne Jansen free_extent_buffer(buf); 1084ab0fff03SArne Jansen } 1085ab0fff03SArne Jansen return 0; 1086ab0fff03SArne Jansen } 1087ab0fff03SArne Jansen 10880999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 10890999df54SChris Mason u64 bytenr, u32 blocksize) 10900999df54SChris Mason { 1091f28491e0SJosef Bacik return find_extent_buffer(root->fs_info, bytenr); 10920999df54SChris Mason } 10930999df54SChris Mason 10940999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 10950999df54SChris Mason u64 bytenr, u32 blocksize) 10960999df54SChris Mason { 1097f28491e0SJosef Bacik return alloc_extent_buffer(root->fs_info, bytenr, blocksize); 10980999df54SChris Mason } 10990999df54SChris Mason 11000999df54SChris Mason 1101e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1102e02119d5SChris Mason { 1103727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11048aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1105e02119d5SChris Mason } 1106e02119d5SChris Mason 1107e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1108e02119d5SChris Mason { 1109727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11108aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1111e02119d5SChris Mason } 1112e02119d5SChris Mason 1113db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1114ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1115e20d96d6SChris Mason { 11165f39d397SChris Mason struct extent_buffer *buf = NULL; 111719c00ddcSChris Mason int ret; 111819c00ddcSChris Mason 1119db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11205f39d397SChris Mason if (!buf) 1121d98237b3SChris Mason return NULL; 1122e4204dedSChris Mason 1123ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11240f0fe8f7SFilipe David Borba Manana if (ret) { 11250f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 11260f0fe8f7SFilipe David Borba Manana return NULL; 11270f0fe8f7SFilipe David Borba Manana } 11285f39d397SChris Mason return buf; 1129ce9adaa5SChris Mason 1130eb60ceacSChris Mason } 1131eb60ceacSChris Mason 1132d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11335f39d397SChris Mason struct extent_buffer *buf) 1134ed2ff2cbSChris Mason { 1135e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1136e2d84521SMiao Xie 113755c69072SChris Mason if (btrfs_header_generation(buf) == 1138e2d84521SMiao Xie fs_info->running_transaction->transid) { 1139b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1140b4ce94deSChris Mason 1141b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1142e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1143e2d84521SMiao Xie -buf->len, 1144e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1145b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1146b9473439SChris Mason btrfs_set_lock_blocking(buf); 11470b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1148925baeddSChris Mason } 11495f39d397SChris Mason } 1150ed7b63ebSJosef Bacik } 11515f39d397SChris Mason 1152143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 115387ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11549f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1155e20d96d6SChris Mason u64 objectid) 1156d97e63b6SChris Mason { 1157cfaa7295SChris Mason root->node = NULL; 1158a28ec197SChris Mason root->commit_root = NULL; 1159db94535dSChris Mason root->sectorsize = sectorsize; 1160db94535dSChris Mason root->nodesize = nodesize; 1161db94535dSChris Mason root->leafsize = leafsize; 116287ee04ebSChris Mason root->stripesize = stripesize; 1163123abc88SChris Mason root->ref_cows = 0; 11640b86a832SChris Mason root->track_dirty = 0; 1165c71bf099SYan, Zheng root->in_radix = 0; 1166d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1167d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11680b86a832SChris Mason 11690f7d52f4SChris Mason root->objectid = objectid; 11700f7d52f4SChris Mason root->last_trans = 0; 117113a8a7c8SYan, Zheng root->highest_objectid = 0; 1172eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1173199c2a9cSMiao Xie root->nr_ordered_extents = 0; 117458176a96SJosef Bacik root->name = NULL; 11756bef4d31SEric Paris root->inode_tree = RB_ROOT; 117616cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1177f0486c68SYan, Zheng root->block_rsv = NULL; 1178d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11790b86a832SChris Mason 11800b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11815d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1182eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1183eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1184199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1185199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 11862ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11872ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1188d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11895d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1190eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1191199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1192f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11932ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11942ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1195a2135011SChris Mason mutex_init(&root->objectid_mutex); 1196e02119d5SChris Mason mutex_init(&root->log_mutex); 11977237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11987237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11997237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12008b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]); 12018b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]); 12027237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12037237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12047237f183SYan Zheng atomic_set(&root->log_writers, 0); 12052ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12068a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1207b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 12087237f183SYan Zheng root->log_transid = 0; 1209d1433debSMiao Xie root->log_transid_committed = -1; 1210257c62e1SChris Mason root->last_log_commit = 0; 121106ea65a3SJosef Bacik if (fs_info) 1212d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1213f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1214017e5369SChris Mason 12153768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12163768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12176702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 121858176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 121906ea65a3SJosef Bacik if (fs_info) 12203f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 122106ea65a3SJosef Bacik else 122206ea65a3SJosef Bacik root->defrag_trans_start = 0; 122358176a96SJosef Bacik init_completion(&root->kobj_unregister); 12246702ed49SChris Mason root->defrag_running = 0; 12254d775673SChris Mason root->root_key.objectid = objectid; 12260ee5dc67SAl Viro root->anon_dev = 0; 12278ea05e3aSAlexander Block 12285f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12293768f368SChris Mason } 12303768f368SChris Mason 1231f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12326f07e42eSAl Viro { 12336f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12346f07e42eSAl Viro if (root) 12356f07e42eSAl Viro root->fs_info = fs_info; 12366f07e42eSAl Viro return root; 12376f07e42eSAl Viro } 12386f07e42eSAl Viro 123906ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 124006ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 124106ea65a3SJosef Bacik struct btrfs_root *btrfs_alloc_dummy_root(void) 124206ea65a3SJosef Bacik { 124306ea65a3SJosef Bacik struct btrfs_root *root; 124406ea65a3SJosef Bacik 124506ea65a3SJosef Bacik root = btrfs_alloc_root(NULL); 124606ea65a3SJosef Bacik if (!root) 124706ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 124806ea65a3SJosef Bacik __setup_root(4096, 4096, 4096, 4096, root, NULL, 1); 124906ea65a3SJosef Bacik root->dummy_root = 1; 125006ea65a3SJosef Bacik 125106ea65a3SJosef Bacik return root; 125206ea65a3SJosef Bacik } 125306ea65a3SJosef Bacik #endif 125406ea65a3SJosef Bacik 125520897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 125620897f5cSArne Jansen struct btrfs_fs_info *fs_info, 125720897f5cSArne Jansen u64 objectid) 125820897f5cSArne Jansen { 125920897f5cSArne Jansen struct extent_buffer *leaf; 126020897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 126120897f5cSArne Jansen struct btrfs_root *root; 126220897f5cSArne Jansen struct btrfs_key key; 126320897f5cSArne Jansen int ret = 0; 12646463fe58SStefan Behrens uuid_le uuid; 126520897f5cSArne Jansen 126620897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 126720897f5cSArne Jansen if (!root) 126820897f5cSArne Jansen return ERR_PTR(-ENOMEM); 126920897f5cSArne Jansen 127020897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 127120897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 127220897f5cSArne Jansen root, fs_info, objectid); 127320897f5cSArne Jansen root->root_key.objectid = objectid; 127420897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 127520897f5cSArne Jansen root->root_key.offset = 0; 127620897f5cSArne Jansen 127720897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 127820897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 127920897f5cSArne Jansen if (IS_ERR(leaf)) { 128020897f5cSArne Jansen ret = PTR_ERR(leaf); 12811dd05682STsutomu Itoh leaf = NULL; 128220897f5cSArne Jansen goto fail; 128320897f5cSArne Jansen } 128420897f5cSArne Jansen 128520897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 128620897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 128720897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 128820897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 128920897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 129020897f5cSArne Jansen root->node = leaf; 129120897f5cSArne Jansen 12920a4e5586SRoss Kirk write_extent_buffer(leaf, fs_info->fsid, btrfs_header_fsid(), 129320897f5cSArne Jansen BTRFS_FSID_SIZE); 129420897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 1295b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 129620897f5cSArne Jansen BTRFS_UUID_SIZE); 129720897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 129820897f5cSArne Jansen 129920897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 130020897f5cSArne Jansen root->track_dirty = 1; 130120897f5cSArne Jansen 130220897f5cSArne Jansen 130320897f5cSArne Jansen root->root_item.flags = 0; 130420897f5cSArne Jansen root->root_item.byte_limit = 0; 130520897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 130620897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 130720897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 130820897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 130920897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 131020897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 131120897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 13126463fe58SStefan Behrens uuid_le_gen(&uuid); 13136463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 131420897f5cSArne Jansen root->root_item.drop_level = 0; 131520897f5cSArne Jansen 131620897f5cSArne Jansen key.objectid = objectid; 131720897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131820897f5cSArne Jansen key.offset = 0; 131920897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 132020897f5cSArne Jansen if (ret) 132120897f5cSArne Jansen goto fail; 132220897f5cSArne Jansen 132320897f5cSArne Jansen btrfs_tree_unlock(leaf); 132420897f5cSArne Jansen 132520897f5cSArne Jansen return root; 13261dd05682STsutomu Itoh 13271dd05682STsutomu Itoh fail: 13281dd05682STsutomu Itoh if (leaf) { 13291dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13301dd05682STsutomu Itoh free_extent_buffer(leaf); 13311dd05682STsutomu Itoh } 13321dd05682STsutomu Itoh kfree(root); 13331dd05682STsutomu Itoh 13341dd05682STsutomu Itoh return ERR_PTR(ret); 133520897f5cSArne Jansen } 133620897f5cSArne Jansen 13377237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1338e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13390f7d52f4SChris Mason { 13400f7d52f4SChris Mason struct btrfs_root *root; 13410f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13427237f183SYan Zheng struct extent_buffer *leaf; 1343e02119d5SChris Mason 13446f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1345e02119d5SChris Mason if (!root) 13467237f183SYan Zheng return ERR_PTR(-ENOMEM); 1347e02119d5SChris Mason 1348e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1349e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1350e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1351e02119d5SChris Mason 1352e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1353e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1354e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13557237f183SYan Zheng /* 13567237f183SYan Zheng * log trees do not get reference counted because they go away 13577237f183SYan Zheng * before a real commit is actually done. They do store pointers 13587237f183SYan Zheng * to file data extents, and those reference counts still get 13597237f183SYan Zheng * updated (along with back refs to the log tree). 13607237f183SYan Zheng */ 1361e02119d5SChris Mason root->ref_cows = 0; 1362e02119d5SChris Mason 13635d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 136466d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13655581a51aSJan Schmidt 0, 0, 0); 13667237f183SYan Zheng if (IS_ERR(leaf)) { 13677237f183SYan Zheng kfree(root); 13687237f183SYan Zheng return ERR_CAST(leaf); 13697237f183SYan Zheng } 1370e02119d5SChris Mason 13715d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13725d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13735d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13745d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13755d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13767237f183SYan Zheng root->node = leaf; 1377e02119d5SChris Mason 1378e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 13790a4e5586SRoss Kirk btrfs_header_fsid(), BTRFS_FSID_SIZE); 1380e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1381e02119d5SChris Mason btrfs_tree_unlock(root->node); 13827237f183SYan Zheng return root; 13837237f183SYan Zheng } 13847237f183SYan Zheng 13857237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13867237f183SYan Zheng struct btrfs_fs_info *fs_info) 13877237f183SYan Zheng { 13887237f183SYan Zheng struct btrfs_root *log_root; 13897237f183SYan Zheng 13907237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13917237f183SYan Zheng if (IS_ERR(log_root)) 13927237f183SYan Zheng return PTR_ERR(log_root); 13937237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13947237f183SYan Zheng fs_info->log_root_tree = log_root; 13957237f183SYan Zheng return 0; 13967237f183SYan Zheng } 13977237f183SYan Zheng 13987237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13997237f183SYan Zheng struct btrfs_root *root) 14007237f183SYan Zheng { 14017237f183SYan Zheng struct btrfs_root *log_root; 14027237f183SYan Zheng struct btrfs_inode_item *inode_item; 14037237f183SYan Zheng 14047237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 14057237f183SYan Zheng if (IS_ERR(log_root)) 14067237f183SYan Zheng return PTR_ERR(log_root); 14077237f183SYan Zheng 14087237f183SYan Zheng log_root->last_trans = trans->transid; 14097237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14107237f183SYan Zheng 14117237f183SYan Zheng inode_item = &log_root->root_item.inode; 14123cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14133cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14143cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 14153cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 14163cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14177237f183SYan Zheng 14185d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14197237f183SYan Zheng 14207237f183SYan Zheng WARN_ON(root->log_root); 14217237f183SYan Zheng root->log_root = log_root; 14227237f183SYan Zheng root->log_transid = 0; 1423d1433debSMiao Xie root->log_transid_committed = -1; 1424257c62e1SChris Mason root->last_log_commit = 0; 1425e02119d5SChris Mason return 0; 1426e02119d5SChris Mason } 1427e02119d5SChris Mason 142835a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1429cb517eabSMiao Xie struct btrfs_key *key) 1430e02119d5SChris Mason { 1431e02119d5SChris Mason struct btrfs_root *root; 1432e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14330f7d52f4SChris Mason struct btrfs_path *path; 143484234f3aSYan Zheng u64 generation; 1435db94535dSChris Mason u32 blocksize; 1436cb517eabSMiao Xie int ret; 1437cb517eabSMiao Xie 1438cb517eabSMiao Xie path = btrfs_alloc_path(); 1439cb517eabSMiao Xie if (!path) 1440cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14410f7d52f4SChris Mason 14426f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1443cb517eabSMiao Xie if (!root) { 1444cb517eabSMiao Xie ret = -ENOMEM; 1445cb517eabSMiao Xie goto alloc_fail; 14460f7d52f4SChris Mason } 14470f7d52f4SChris Mason 1448db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 144987ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 1450cb517eabSMiao Xie root, fs_info, key->objectid); 14510f7d52f4SChris Mason 1452cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1453cb517eabSMiao Xie &root->root_item, &root->root_key); 14540f7d52f4SChris Mason if (ret) { 145513a8a7c8SYan, Zheng if (ret > 0) 145613a8a7c8SYan, Zheng ret = -ENOENT; 1457cb517eabSMiao Xie goto find_fail; 14580f7d52f4SChris Mason } 145913a8a7c8SYan, Zheng 146084234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1461db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1462db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 146384234f3aSYan Zheng blocksize, generation); 1464cb517eabSMiao Xie if (!root->node) { 1465cb517eabSMiao Xie ret = -ENOMEM; 1466cb517eabSMiao Xie goto find_fail; 1467cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1468cb517eabSMiao Xie ret = -EIO; 1469cb517eabSMiao Xie goto read_fail; 1470416bc658SJosef Bacik } 14715d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 147213a8a7c8SYan, Zheng out: 1473cb517eabSMiao Xie btrfs_free_path(path); 1474cb517eabSMiao Xie return root; 1475cb517eabSMiao Xie 1476cb517eabSMiao Xie read_fail: 1477cb517eabSMiao Xie free_extent_buffer(root->node); 1478cb517eabSMiao Xie find_fail: 1479cb517eabSMiao Xie kfree(root); 1480cb517eabSMiao Xie alloc_fail: 1481cb517eabSMiao Xie root = ERR_PTR(ret); 1482cb517eabSMiao Xie goto out; 1483cb517eabSMiao Xie } 1484cb517eabSMiao Xie 1485cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1486cb517eabSMiao Xie struct btrfs_key *location) 1487cb517eabSMiao Xie { 1488cb517eabSMiao Xie struct btrfs_root *root; 1489cb517eabSMiao Xie 1490cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1491cb517eabSMiao Xie if (IS_ERR(root)) 1492cb517eabSMiao Xie return root; 1493cb517eabSMiao Xie 1494cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 14950f7d52f4SChris Mason root->ref_cows = 1; 149608fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 149708fe4db1SLi Zefan } 149813a8a7c8SYan, Zheng 14995eda7b5eSChris Mason return root; 15005eda7b5eSChris Mason } 15015eda7b5eSChris Mason 1502cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1503cb517eabSMiao Xie { 1504cb517eabSMiao Xie int ret; 1505cb517eabSMiao Xie 1506cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1507cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1508cb517eabSMiao Xie GFP_NOFS); 1509cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1510cb517eabSMiao Xie ret = -ENOMEM; 1511cb517eabSMiao Xie goto fail; 1512cb517eabSMiao Xie } 1513cb517eabSMiao Xie 1514cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 1515cb517eabSMiao Xie mutex_init(&root->fs_commit_mutex); 1516cb517eabSMiao Xie spin_lock_init(&root->cache_lock); 1517cb517eabSMiao Xie init_waitqueue_head(&root->cache_wait); 1518cb517eabSMiao Xie 1519cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1520cb517eabSMiao Xie if (ret) 1521cb517eabSMiao Xie goto fail; 1522cb517eabSMiao Xie return 0; 1523cb517eabSMiao Xie fail: 1524cb517eabSMiao Xie kfree(root->free_ino_ctl); 1525cb517eabSMiao Xie kfree(root->free_ino_pinned); 1526cb517eabSMiao Xie return ret; 1527cb517eabSMiao Xie } 1528cb517eabSMiao Xie 1529171170c1SSergei Trofimovich static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1530cb517eabSMiao Xie u64 root_id) 1531cb517eabSMiao Xie { 1532cb517eabSMiao Xie struct btrfs_root *root; 1533cb517eabSMiao Xie 1534cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1535cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1536cb517eabSMiao Xie (unsigned long)root_id); 1537cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1538cb517eabSMiao Xie return root; 1539cb517eabSMiao Xie } 1540cb517eabSMiao Xie 1541cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1542cb517eabSMiao Xie struct btrfs_root *root) 1543cb517eabSMiao Xie { 1544cb517eabSMiao Xie int ret; 1545cb517eabSMiao Xie 1546cb517eabSMiao Xie ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 1547cb517eabSMiao Xie if (ret) 1548cb517eabSMiao Xie return ret; 1549cb517eabSMiao Xie 1550cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1551cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1552cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1553cb517eabSMiao Xie root); 1554cb517eabSMiao Xie if (ret == 0) 1555cb517eabSMiao Xie root->in_radix = 1; 1556cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1557cb517eabSMiao Xie radix_tree_preload_end(); 1558cb517eabSMiao Xie 1559cb517eabSMiao Xie return ret; 1560cb517eabSMiao Xie } 1561cb517eabSMiao Xie 1562c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1563c00869f1SMiao Xie struct btrfs_key *location, 1564c00869f1SMiao Xie bool check_ref) 15655eda7b5eSChris Mason { 15665eda7b5eSChris Mason struct btrfs_root *root; 15675eda7b5eSChris Mason int ret; 15685eda7b5eSChris Mason 1569edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1570edbd8d4eSChris Mason return fs_info->tree_root; 1571edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1572edbd8d4eSChris Mason return fs_info->extent_root; 15738f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15748f18cf13SChris Mason return fs_info->chunk_root; 15758f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15768f18cf13SChris Mason return fs_info->dev_root; 15770403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15780403e47eSYan Zheng return fs_info->csum_root; 1579bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1580bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1581bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1582f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1583f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1584f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 15854df27c4dSYan, Zheng again: 1586cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 158748475471SStefan Behrens if (root) { 1588c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 158948475471SStefan Behrens return ERR_PTR(-ENOENT); 15905eda7b5eSChris Mason return root; 159148475471SStefan Behrens } 15925eda7b5eSChris Mason 1593cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 15945eda7b5eSChris Mason if (IS_ERR(root)) 15955eda7b5eSChris Mason return root; 15963394e160SChris Mason 1597c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1598d68fc57bSYan, Zheng ret = -ENOENT; 1599d68fc57bSYan, Zheng goto fail; 1600d68fc57bSYan, Zheng } 1601d68fc57bSYan, Zheng 1602cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1603cb517eabSMiao Xie if (ret) 1604cb517eabSMiao Xie goto fail; 1605cb517eabSMiao Xie 16063f870c28SKelley Nielsen ret = btrfs_find_item(fs_info->tree_root, NULL, BTRFS_ORPHAN_OBJECTID, 16073f870c28SKelley Nielsen location->objectid, BTRFS_ORPHAN_ITEM_KEY, NULL); 1608d68fc57bSYan, Zheng if (ret < 0) 1609d68fc57bSYan, Zheng goto fail; 1610d68fc57bSYan, Zheng if (ret == 0) 1611d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1612d68fc57bSYan, Zheng 1613cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16140f7d52f4SChris Mason if (ret) { 16154df27c4dSYan, Zheng if (ret == -EEXIST) { 16164df27c4dSYan, Zheng free_fs_root(root); 16174df27c4dSYan, Zheng goto again; 16180f7d52f4SChris Mason } 16194df27c4dSYan, Zheng goto fail; 16204df27c4dSYan, Zheng } 1621edbd8d4eSChris Mason return root; 16224df27c4dSYan, Zheng fail: 16234df27c4dSYan, Zheng free_fs_root(root); 16244df27c4dSYan, Zheng return ERR_PTR(ret); 1625edbd8d4eSChris Mason } 1626edbd8d4eSChris Mason 162704160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 162804160088SChris Mason { 162904160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 163004160088SChris Mason int ret = 0; 163104160088SChris Mason struct btrfs_device *device; 163204160088SChris Mason struct backing_dev_info *bdi; 1633b7967db7SChris Mason 16341f78160cSXiao Guangrong rcu_read_lock(); 16351f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1636dfe25020SChris Mason if (!device->bdev) 1637dfe25020SChris Mason continue; 163804160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 163904160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 164004160088SChris Mason ret = 1; 164104160088SChris Mason break; 164204160088SChris Mason } 164304160088SChris Mason } 16441f78160cSXiao Guangrong rcu_read_unlock(); 164504160088SChris Mason return ret; 164604160088SChris Mason } 164704160088SChris Mason 164838b66988SChris Mason /* 1649ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1650ad081f14SJens Axboe * bdi again. 1651ad081f14SJens Axboe */ 165204160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 165304160088SChris Mason { 1654ad081f14SJens Axboe int err; 1655ad081f14SJens Axboe 1656ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1657e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1658ad081f14SJens Axboe if (err) 1659ad081f14SJens Axboe return err; 1660ad081f14SJens Axboe 16614575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 166204160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 166304160088SChris Mason bdi->congested_data = info; 166404160088SChris Mason return 0; 166504160088SChris Mason } 166604160088SChris Mason 16678b712842SChris Mason /* 16688b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16698b712842SChris Mason * functions. This is where read checksum verification actually happens 16708b712842SChris Mason */ 1671fccb5d86SQu Wenruo static void end_workqueue_fn(struct btrfs_work_struct *work) 1672ce9adaa5SChris Mason { 1673ce9adaa5SChris Mason struct bio *bio; 16748b712842SChris Mason struct end_io_wq *end_io_wq; 1675ce9adaa5SChris Mason int error; 1676ce9adaa5SChris Mason 16778b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1678ce9adaa5SChris Mason bio = end_io_wq->bio; 16798b712842SChris Mason 1680ce9adaa5SChris Mason error = end_io_wq->error; 1681ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1682ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1683ce9adaa5SChris Mason kfree(end_io_wq); 1684ce9adaa5SChris Mason bio_endio(bio, error); 1685ce9adaa5SChris Mason } 168644b8bd7eSChris Mason 1687a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1688a74a4b97SChris Mason { 1689a74a4b97SChris Mason struct btrfs_root *root = arg; 1690d0278245SMiao Xie int again; 1691a74a4b97SChris Mason 1692a74a4b97SChris Mason do { 1693d0278245SMiao Xie again = 0; 16949d1a2a3aSDavid Sterba 1695d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 1696babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) 1697d0278245SMiao Xie goto sleep; 1698d0278245SMiao Xie 1699d0278245SMiao Xie if (!mutex_trylock(&root->fs_info->cleaner_mutex)) 1700d0278245SMiao Xie goto sleep; 1701d0278245SMiao Xie 1702dc7f370cSMiao Xie /* 1703dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1704dc7f370cSMiao Xie * during the above check and trylock. 1705dc7f370cSMiao Xie */ 1706babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) { 1707dc7f370cSMiao Xie mutex_unlock(&root->fs_info->cleaner_mutex); 1708dc7f370cSMiao Xie goto sleep; 1709dc7f370cSMiao Xie } 1710dc7f370cSMiao Xie 171124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 17129d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 1713a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 1714a74a4b97SChris Mason 1715d0278245SMiao Xie /* 171605323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 171705323cd1SMiao Xie * needn't do anything special here. 1718d0278245SMiao Xie */ 1719d0278245SMiao Xie btrfs_run_defrag_inodes(root->fs_info); 1720d0278245SMiao Xie sleep: 17219d1a2a3aSDavid Sterba if (!try_to_freeze() && !again) { 1722a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17238929ecfaSYan, Zheng if (!kthread_should_stop()) 1724a74a4b97SChris Mason schedule(); 1725a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1726a74a4b97SChris Mason } 1727a74a4b97SChris Mason } while (!kthread_should_stop()); 1728a74a4b97SChris Mason return 0; 1729a74a4b97SChris Mason } 1730a74a4b97SChris Mason 1731a74a4b97SChris Mason static int transaction_kthread(void *arg) 1732a74a4b97SChris Mason { 1733a74a4b97SChris Mason struct btrfs_root *root = arg; 1734a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1735a74a4b97SChris Mason struct btrfs_transaction *cur; 17368929ecfaSYan, Zheng u64 transid; 1737a74a4b97SChris Mason unsigned long now; 1738a74a4b97SChris Mason unsigned long delay; 1739914b2007SJan Kara bool cannot_commit; 1740a74a4b97SChris Mason 1741a74a4b97SChris Mason do { 1742914b2007SJan Kara cannot_commit = false; 17438b87dc17SDavid Sterba delay = HZ * root->fs_info->commit_interval; 1744a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1745a74a4b97SChris Mason 1746a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1747a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1748a74a4b97SChris Mason if (!cur) { 1749a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1750a74a4b97SChris Mason goto sleep; 1751a74a4b97SChris Mason } 175231153d81SYan Zheng 1753a74a4b97SChris Mason now = get_seconds(); 17544a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 17558b87dc17SDavid Sterba (now < cur->start_time || 17568b87dc17SDavid Sterba now - cur->start_time < root->fs_info->commit_interval)) { 1757a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1758a74a4b97SChris Mason delay = HZ * 5; 1759a74a4b97SChris Mason goto sleep; 1760a74a4b97SChris Mason } 17618929ecfaSYan, Zheng transid = cur->transid; 1762a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 176356bec294SChris Mason 176479787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1765354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1766914b2007SJan Kara if (IS_ERR(trans)) { 1767354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1768914b2007SJan Kara cannot_commit = true; 176979787eaaSJeff Mahoney goto sleep; 1770914b2007SJan Kara } 17718929ecfaSYan, Zheng if (transid == trans->transid) { 177279787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 17738929ecfaSYan, Zheng } else { 17748929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 17758929ecfaSYan, Zheng } 1776a74a4b97SChris Mason sleep: 1777a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1778a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1779a74a4b97SChris Mason 17804e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 17814e121c06SJosef Bacik &root->fs_info->fs_state))) 17824e121c06SJosef Bacik btrfs_cleanup_transaction(root); 1783a0acae0eSTejun Heo if (!try_to_freeze()) { 1784a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17858929ecfaSYan, Zheng if (!kthread_should_stop() && 1786914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1787914b2007SJan Kara cannot_commit)) 1788a74a4b97SChris Mason schedule_timeout(delay); 1789a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1790a74a4b97SChris Mason } 1791a74a4b97SChris Mason } while (!kthread_should_stop()); 1792a74a4b97SChris Mason return 0; 1793a74a4b97SChris Mason } 1794a74a4b97SChris Mason 1795af31f5e5SChris Mason /* 1796af31f5e5SChris Mason * this will find the highest generation in the array of 1797af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1798af31f5e5SChris Mason * or -1 if we can't find anything. 1799af31f5e5SChris Mason * 1800af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1801af31f5e5SChris Mason * generation of the latest root in the array with the generation 1802af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1803af31f5e5SChris Mason */ 1804af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1805af31f5e5SChris Mason { 1806af31f5e5SChris Mason u64 cur; 1807af31f5e5SChris Mason int newest_index = -1; 1808af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1809af31f5e5SChris Mason int i; 1810af31f5e5SChris Mason 1811af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1812af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1813af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1814af31f5e5SChris Mason if (cur == newest_gen) 1815af31f5e5SChris Mason newest_index = i; 1816af31f5e5SChris Mason } 1817af31f5e5SChris Mason 1818af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1819af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1820af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1821af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1822af31f5e5SChris Mason if (cur == newest_gen) 1823af31f5e5SChris Mason newest_index = 0; 1824af31f5e5SChris Mason } 1825af31f5e5SChris Mason return newest_index; 1826af31f5e5SChris Mason } 1827af31f5e5SChris Mason 1828af31f5e5SChris Mason 1829af31f5e5SChris Mason /* 1830af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1831af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1832af31f5e5SChris Mason * field in the fs_info struct 1833af31f5e5SChris Mason */ 1834af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1835af31f5e5SChris Mason u64 newest_gen) 1836af31f5e5SChris Mason { 1837af31f5e5SChris Mason int newest_index = -1; 1838af31f5e5SChris Mason 1839af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1840af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1841af31f5e5SChris Mason if (newest_index == -1) { 1842af31f5e5SChris Mason info->backup_root_index = 0; 1843af31f5e5SChris Mason } else { 1844af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1845af31f5e5SChris Mason } 1846af31f5e5SChris Mason } 1847af31f5e5SChris Mason 1848af31f5e5SChris Mason /* 1849af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1850af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1851af31f5e5SChris Mason * done 1852af31f5e5SChris Mason */ 1853af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1854af31f5e5SChris Mason { 1855af31f5e5SChris Mason int next_backup; 1856af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1857af31f5e5SChris Mason int last_backup; 1858af31f5e5SChris Mason 1859af31f5e5SChris Mason next_backup = info->backup_root_index; 1860af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1861af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1862af31f5e5SChris Mason 1863af31f5e5SChris Mason /* 1864af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1865af31f5e5SChris Mason * this happens only at umount 1866af31f5e5SChris Mason */ 1867af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1868af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1869af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1870af31f5e5SChris Mason next_backup = last_backup; 1871af31f5e5SChris Mason 1872af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1873af31f5e5SChris Mason 1874af31f5e5SChris Mason /* 1875af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1876af31f5e5SChris Mason * regardless of which ones we use today 1877af31f5e5SChris Mason */ 1878af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1879af31f5e5SChris Mason 1880af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1881af31f5e5SChris Mason 1882af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1883af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1884af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1885af31f5e5SChris Mason 1886af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1887af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1890af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1891af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1892af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1893af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1896af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1897af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1898af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1899af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1900af31f5e5SChris Mason 19017c7e82a7SChris Mason /* 19027c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19037c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19047c7e82a7SChris Mason */ 19057c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19067c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19077c7e82a7SChris Mason info->fs_root->node->start); 1908af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1909af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1910af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1911af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19127c7e82a7SChris Mason } 1913af31f5e5SChris Mason 1914af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1915af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1916af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1917af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1918af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1919af31f5e5SChris Mason 1920af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1921af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1922af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1923af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1924af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1925af31f5e5SChris Mason 1926af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1927af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1928af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1929af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1930af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1931af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1932af31f5e5SChris Mason 1933af31f5e5SChris Mason /* 1934af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1935af31f5e5SChris Mason * for the next commit 1936af31f5e5SChris Mason */ 1937af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1938af31f5e5SChris Mason &info->super_for_commit->super_roots, 1939af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1940af31f5e5SChris Mason } 1941af31f5e5SChris Mason 1942af31f5e5SChris Mason /* 1943af31f5e5SChris Mason * this copies info out of the root backup array and back into 1944af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1945af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1946af31f5e5SChris Mason * tried and the last backup index you used. 1947af31f5e5SChris Mason * 1948af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1949af31f5e5SChris Mason */ 1950af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1951af31f5e5SChris Mason struct btrfs_super_block *super, 1952af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1953af31f5e5SChris Mason { 1954af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1955af31f5e5SChris Mason int newest = *backup_index; 1956af31f5e5SChris Mason 1957af31f5e5SChris Mason if (*num_backups_tried == 0) { 1958af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1959af31f5e5SChris Mason 1960af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1961af31f5e5SChris Mason if (newest == -1) 1962af31f5e5SChris Mason return -1; 1963af31f5e5SChris Mason 1964af31f5e5SChris Mason *backup_index = newest; 1965af31f5e5SChris Mason *num_backups_tried = 1; 1966af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1967af31f5e5SChris Mason /* we've tried all the backups, all done */ 1968af31f5e5SChris Mason return -1; 1969af31f5e5SChris Mason } else { 1970af31f5e5SChris Mason /* jump to the next oldest backup */ 1971af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1972af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1973af31f5e5SChris Mason *backup_index = newest; 1974af31f5e5SChris Mason *num_backups_tried += 1; 1975af31f5e5SChris Mason } 1976af31f5e5SChris Mason root_backup = super->super_roots + newest; 1977af31f5e5SChris Mason 1978af31f5e5SChris Mason btrfs_set_super_generation(super, 1979af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1980af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1981af31f5e5SChris Mason btrfs_set_super_root_level(super, 1982af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1983af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1984af31f5e5SChris Mason 1985af31f5e5SChris Mason /* 1986af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1987af31f5e5SChris Mason * need a fsck 1988af31f5e5SChris Mason */ 1989af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1990af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1991af31f5e5SChris Mason return 0; 1992af31f5e5SChris Mason } 1993af31f5e5SChris Mason 19947abadb64SLiu Bo /* helper to cleanup workers */ 19957abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19967abadb64SLiu Bo { 19977abadb64SLiu Bo btrfs_stop_workers(&fs_info->generic_worker); 1998dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers); 1999afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers); 20005cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers); 2001fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_workers); 2002fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_workers); 2003fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_raid56_workers); 2004d05a33acSQu Wenruo btrfs_destroy_workqueue(fs_info->rmw_workers); 2005fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_meta_write_workers); 2006fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers); 2007fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker); 2008a8c93d4eSQu Wenruo btrfs_destroy_workqueue(fs_info->submit_workers); 20095b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers); 2010e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers); 2011736cfa15SQu Wenruo btrfs_destroy_workqueue(fs_info->readahead_workers); 2012a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers); 2013fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers); 20147abadb64SLiu Bo } 20157abadb64SLiu Bo 20162e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root) 20172e9f5954SRashika { 20182e9f5954SRashika if (root) { 20192e9f5954SRashika free_extent_buffer(root->node); 20202e9f5954SRashika free_extent_buffer(root->commit_root); 20212e9f5954SRashika root->node = NULL; 20222e9f5954SRashika root->commit_root = NULL; 20232e9f5954SRashika } 20242e9f5954SRashika } 20252e9f5954SRashika 2026af31f5e5SChris Mason /* helper to cleanup tree roots */ 2027af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2028af31f5e5SChris Mason { 20292e9f5954SRashika free_root_extent_buffers(info->tree_root); 2030655b09feSJosef Bacik 20312e9f5954SRashika free_root_extent_buffers(info->dev_root); 20322e9f5954SRashika free_root_extent_buffers(info->extent_root); 20332e9f5954SRashika free_root_extent_buffers(info->csum_root); 20342e9f5954SRashika free_root_extent_buffers(info->quota_root); 20352e9f5954SRashika free_root_extent_buffers(info->uuid_root); 20362e9f5954SRashika if (chunk_root) 20372e9f5954SRashika free_root_extent_buffers(info->chunk_root); 2038af31f5e5SChris Mason } 2039af31f5e5SChris Mason 2040171f6537SJosef Bacik static void del_fs_roots(struct btrfs_fs_info *fs_info) 2041171f6537SJosef Bacik { 2042171f6537SJosef Bacik int ret; 2043171f6537SJosef Bacik struct btrfs_root *gang[8]; 2044171f6537SJosef Bacik int i; 2045171f6537SJosef Bacik 2046171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2047171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2048171f6537SJosef Bacik struct btrfs_root, root_list); 2049171f6537SJosef Bacik list_del(&gang[0]->root_list); 2050171f6537SJosef Bacik 2051171f6537SJosef Bacik if (gang[0]->in_radix) { 2052cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2053171f6537SJosef Bacik } else { 2054171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2055171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2056b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2057171f6537SJosef Bacik } 2058171f6537SJosef Bacik } 2059171f6537SJosef Bacik 2060171f6537SJosef Bacik while (1) { 2061171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2062171f6537SJosef Bacik (void **)gang, 0, 2063171f6537SJosef Bacik ARRAY_SIZE(gang)); 2064171f6537SJosef Bacik if (!ret) 2065171f6537SJosef Bacik break; 2066171f6537SJosef Bacik for (i = 0; i < ret; i++) 2067cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2068171f6537SJosef Bacik } 20691a4319ccSLiu Bo 20701a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20711a4319ccSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 20721a4319ccSLiu Bo btrfs_destroy_pinned_extent(fs_info->tree_root, 20731a4319ccSLiu Bo fs_info->pinned_extents); 20741a4319ccSLiu Bo } 2075171f6537SJosef Bacik } 2076af31f5e5SChris Mason 2077ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2078dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2079dfe25020SChris Mason char *options) 2080eb60ceacSChris Mason { 2081db94535dSChris Mason u32 sectorsize; 2082db94535dSChris Mason u32 nodesize; 2083db94535dSChris Mason u32 leafsize; 2084db94535dSChris Mason u32 blocksize; 208587ee04ebSChris Mason u32 stripesize; 208684234f3aSYan Zheng u64 generation; 2087f2b636e8SJosef Bacik u64 features; 20883de4586cSChris Mason struct btrfs_key location; 2089a061fc8dSChris Mason struct buffer_head *bh; 20904d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2091815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2092f84a8bd6SAl Viro struct btrfs_root *tree_root; 20934d34b278SIlya Dryomov struct btrfs_root *extent_root; 20944d34b278SIlya Dryomov struct btrfs_root *csum_root; 20954d34b278SIlya Dryomov struct btrfs_root *chunk_root; 20964d34b278SIlya Dryomov struct btrfs_root *dev_root; 2097bcef60f2SArne Jansen struct btrfs_root *quota_root; 2098f7a81ea4SStefan Behrens struct btrfs_root *uuid_root; 2099e02119d5SChris Mason struct btrfs_root *log_tree_root; 2100eb60ceacSChris Mason int ret; 2101e58ca020SYan int err = -EINVAL; 2102af31f5e5SChris Mason int num_backups_tried = 0; 2103af31f5e5SChris Mason int backup_index = 0; 21045cdc7ad3SQu Wenruo int max_active; 21055cdc7ad3SQu Wenruo int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND; 210670f80175SStefan Behrens bool create_uuid_tree; 210770f80175SStefan Behrens bool check_uuid_tree; 21084543df7eSChris Mason 2109f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 21106f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 2111cb517eabSMiao Xie if (!tree_root || !chunk_root) { 211239279cc3SChris Mason err = -ENOMEM; 211339279cc3SChris Mason goto fail; 211439279cc3SChris Mason } 211576dda93cSYan, Zheng 211676dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 211776dda93cSYan, Zheng if (ret) { 211876dda93cSYan, Zheng err = ret; 211976dda93cSYan, Zheng goto fail; 212076dda93cSYan, Zheng } 212176dda93cSYan, Zheng 212276dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 212376dda93cSYan, Zheng if (ret) { 212476dda93cSYan, Zheng err = ret; 212576dda93cSYan, Zheng goto fail_srcu; 212676dda93cSYan, Zheng } 212776dda93cSYan, Zheng 2128e2d84521SMiao Xie ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0); 2129e2d84521SMiao Xie if (ret) { 2130e2d84521SMiao Xie err = ret; 2131e2d84521SMiao Xie goto fail_bdi; 2132e2d84521SMiao Xie } 2133e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2134e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2135e2d84521SMiao Xie 2136963d678bSMiao Xie ret = percpu_counter_init(&fs_info->delalloc_bytes, 0); 2137963d678bSMiao Xie if (ret) { 2138963d678bSMiao Xie err = ret; 2139963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2140963d678bSMiao Xie } 2141963d678bSMiao Xie 2142c404e0dcSMiao Xie ret = percpu_counter_init(&fs_info->bio_counter, 0); 2143c404e0dcSMiao Xie if (ret) { 2144c404e0dcSMiao Xie err = ret; 2145c404e0dcSMiao Xie goto fail_delalloc_bytes; 2146c404e0dcSMiao Xie } 2147c404e0dcSMiao Xie 214876dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 214976dda93cSYan, Zheng if (!fs_info->btree_inode) { 215076dda93cSYan, Zheng err = -ENOMEM; 2151c404e0dcSMiao Xie goto fail_bio_counter; 215276dda93cSYan, Zheng } 215376dda93cSYan, Zheng 2154a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 21551561dedaSMiao Xie 215676dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 2157f28491e0SJosef Bacik INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); 21588fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2159facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 216024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2161eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 216211833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2163eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2164a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 216576dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 216624bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 21674cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 21682bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2169f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2170ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2171f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock); 2172f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 21737585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2174de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 217519c00ddcSChris Mason 217658176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 21770b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 21786324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2179f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 21800b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 218166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 218266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 218366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 218466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 218566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 218666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 218766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 218866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 218966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2190cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2191771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 21928c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 21930986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 21944cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2195fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2196e20d96d6SChris Mason fs_info->sb = sb; 21976f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 21989ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 21994cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 22002bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2201f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 22028b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 22030a2b2a84SJosef Bacik fs_info->avg_delayed_ref_runtime = div64_u64(NSEC_PER_SEC, 64); 220490519d66SArne Jansen /* readahead state */ 220590519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 220690519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 2207c8b97818SChris Mason 2208b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2209b34b086cSChris Mason num_online_cpus() + 2, 8); 22100afbaf8cSChris Mason 2211199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2212199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 221316cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 221416cdcec7SMiao Xie GFP_NOFS); 221516cdcec7SMiao Xie if (!fs_info->delayed_root) { 221616cdcec7SMiao Xie err = -ENOMEM; 221716cdcec7SMiao Xie goto fail_iput; 221816cdcec7SMiao Xie } 221916cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 22203eaa2885SChris Mason 2221a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2222a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2223a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2224a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2225a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2226c404e0dcSMiao Xie init_waitqueue_head(&fs_info->replace_wait); 2227a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2228a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 222921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 223021adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 223121adbd5cSStefan Behrens #endif 2232a2de733cSArne Jansen 2233c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2234c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2235837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2236837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2237a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2238c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2239837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2240a061fc8dSChris Mason 22410afbaf8cSChris Mason sb->s_blocksize = 4096; 22420afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 224332a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 22440afbaf8cSChris Mason 224576dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2246bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 22470afbaf8cSChris Mason /* 22480afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 22490afbaf8cSChris Mason * the real end of the address space is determined by all of 22500afbaf8cSChris Mason * the devices in the system 22510afbaf8cSChris Mason */ 22520afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2253d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 225404160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 225504160088SChris Mason 22565d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2257d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2258f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 22590b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2260a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 22610da5468fSChris Mason 2262d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2263d1310b2eSChris Mason 22640f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 22650f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 22660f7d52f4SChris Mason sizeof(struct btrfs_key)); 226772ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 226872ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 2269778ba82bSFilipe David Borba Manana btrfs_insert_inode_hash(fs_info->btree_inode); 227039279cc3SChris Mason 2271e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 22726bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2273a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2274925baeddSChris Mason 227511833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2276f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 227711833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2278f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 227911833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2280e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2281f9295749SChris Mason 2282d98237b3SChris Mason 22835a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 22849ffba8cdSJosef Bacik mutex_init(&fs_info->ordered_extent_flush_mutex); 228530ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2286925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2287925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2288a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2289a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2290276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2291c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 229276dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2293803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2294e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2295e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2296e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2297e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2298e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2299fa9c0d79SChris Mason 2300416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2301f2f6ed3dSWang Shilong mutex_init(&fs_info->qgroup_ioctl_lock); 2302416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2303416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2304416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2305416ac51dSArne Jansen fs_info->quota_enabled = 0; 2306416ac51dSArne Jansen fs_info->pending_quota_state = 0; 23071e8f9158SWang Shilong fs_info->qgroup_ulist = NULL; 23082f232036SJan Schmidt mutex_init(&fs_info->qgroup_rescan_lock); 2309416ac51dSArne Jansen 2310fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2311fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2312fa9c0d79SChris Mason 23137d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 23143768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2315bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 23164854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 23179a8dd150SChris Mason 231853b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 231953b381b3SDavid Woodhouse if (ret) { 232083c8266aSDavid Sterba err = ret; 232153b381b3SDavid Woodhouse goto fail_alloc; 232253b381b3SDavid Woodhouse } 232353b381b3SDavid Woodhouse 23240b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 23252c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 23267eccb903SChris Mason 23273c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 23281104a885SDavid Sterba 23291104a885SDavid Sterba /* 23301104a885SDavid Sterba * Read super block and check the signature bytes only 23311104a885SDavid Sterba */ 2332a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 233320b45077SDave Young if (!bh) { 233420b45077SDave Young err = -EINVAL; 233516cdcec7SMiao Xie goto fail_alloc; 233620b45077SDave Young } 233739279cc3SChris Mason 23381104a885SDavid Sterba /* 23391104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 23401104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 23411104a885SDavid Sterba */ 23421104a885SDavid Sterba if (btrfs_check_super_csum(bh->b_data)) { 2343efe120a0SFrank Holton printk(KERN_ERR "BTRFS: superblock checksum mismatch\n"); 23441104a885SDavid Sterba err = -EINVAL; 23451104a885SDavid Sterba goto fail_alloc; 23461104a885SDavid Sterba } 23471104a885SDavid Sterba 23481104a885SDavid Sterba /* 23491104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 23501104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 23511104a885SDavid Sterba * the whole block of INFO_SIZE 23521104a885SDavid Sterba */ 23536c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 23546c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 23556c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2356a061fc8dSChris Mason brelse(bh); 23575f39d397SChris Mason 23586c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 23590b86a832SChris Mason 23601104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 23611104a885SDavid Sterba if (ret) { 2362efe120a0SFrank Holton printk(KERN_ERR "BTRFS: superblock contains fatal errors\n"); 23631104a885SDavid Sterba err = -EINVAL; 23641104a885SDavid Sterba goto fail_alloc; 23651104a885SDavid Sterba } 23661104a885SDavid Sterba 23676c41761fSDavid Sterba disk_super = fs_info->super_copy; 23680f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 236916cdcec7SMiao Xie goto fail_alloc; 23700f7d52f4SChris Mason 2371acce952bSliubo /* check FS state, whether FS is broken. */ 237287533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 237387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2374acce952bSliubo 237575e7cb7fSLiu Bo /* 2376af31f5e5SChris Mason * run through our array of backup supers and setup 2377af31f5e5SChris Mason * our ring pointer to the oldest one 2378af31f5e5SChris Mason */ 2379af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2380af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2381af31f5e5SChris Mason 2382af31f5e5SChris Mason /* 238375e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 238475e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 238575e7cb7fSLiu Bo */ 238675e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 238775e7cb7fSLiu Bo 23882b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 23892b82032cSYan Zheng if (ret) { 23902b82032cSYan Zheng err = ret; 239116cdcec7SMiao Xie goto fail_alloc; 23922b82032cSYan Zheng } 2393dfe25020SChris Mason 2394f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2395f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2396f2b636e8SJosef Bacik if (features) { 2397f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2398f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 2399c1c9ff7cSGeert Uytterhoeven features); 2400f2b636e8SJosef Bacik err = -EINVAL; 240116cdcec7SMiao Xie goto fail_alloc; 2402f2b636e8SJosef Bacik } 2403f2b636e8SJosef Bacik 2404727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2405727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2406727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2407727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2408727011e0SChris Mason btrfs_super_nodesize(disk_super), 2409727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2410727011e0SChris Mason err = -EINVAL; 2411727011e0SChris Mason goto fail_alloc; 2412727011e0SChris Mason } 2413727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2414727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2415727011e0SChris Mason "blocksize (%d) was too large\n", 2416727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2417727011e0SChris Mason err = -EINVAL; 2418727011e0SChris Mason goto fail_alloc; 2419727011e0SChris Mason } 2420727011e0SChris Mason 24215d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 24225d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 242369e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2424a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2425727011e0SChris Mason 24263173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 2427efe120a0SFrank Holton printk(KERN_ERR "BTRFS: has skinny extents\n"); 24283173a18fSJosef Bacik 2429727011e0SChris Mason /* 2430727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2431727011e0SChris Mason * they are bigger than the page size 2432727011e0SChris Mason */ 2433727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2434727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2435efe120a0SFrank Holton printk(KERN_INFO "BTRFS: flagging fs with big metadata feature\n"); 2436727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2437727011e0SChris Mason } 2438727011e0SChris Mason 2439bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2440bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2441bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2442bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2443e2d84521SMiao Xie fs_info->dirty_metadata_batch = leafsize * (1 + ilog2(nr_cpu_ids)); 2444963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2445bc3f116fSChris Mason 2446bc3f116fSChris Mason /* 2447bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2448bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2449bc3f116fSChris Mason */ 2450bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2451bc3f116fSChris Mason (sectorsize != leafsize)) { 2452efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: unequal leaf/node/sector sizes " 2453bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2454bc3f116fSChris Mason sb->s_id); 2455bc3f116fSChris Mason goto fail_alloc; 2456bc3f116fSChris Mason } 2457bc3f116fSChris Mason 2458ceda0864SMiao Xie /* 2459ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2460ceda0864SMiao Xie * update the flag. 2461ceda0864SMiao Xie */ 24625d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 24635d4f98a2SYan Zheng 2464f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2465f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2466f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2467f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2468f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 2469c1c9ff7cSGeert Uytterhoeven features); 2470f2b636e8SJosef Bacik err = -EINVAL; 247116cdcec7SMiao Xie goto fail_alloc; 2472f2b636e8SJosef Bacik } 247361d92c32SChris Mason 24745cdc7ad3SQu Wenruo max_active = fs_info->thread_pool_size; 247561d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 247661d92c32SChris Mason "genwork", 1, NULL); 247761d92c32SChris Mason 24785cdc7ad3SQu Wenruo fs_info->workers = 24795cdc7ad3SQu Wenruo btrfs_alloc_workqueue("worker", flags | WQ_HIGHPRI, 24805cdc7ad3SQu Wenruo max_active, 16); 2481c8b97818SChris Mason 2482afe3d242SQu Wenruo fs_info->delalloc_workers = 2483afe3d242SQu Wenruo btrfs_alloc_workqueue("delalloc", flags, max_active, 2); 2484771ed689SChris Mason 2485a44903abSQu Wenruo fs_info->flush_workers = 2486a44903abSQu Wenruo btrfs_alloc_workqueue("flush_delalloc", flags, max_active, 0); 248761b49440SChris Mason 2488e66f0bb1SQu Wenruo fs_info->caching_workers = 2489e66f0bb1SQu Wenruo btrfs_alloc_workqueue("cache", flags, max_active, 0); 2490bab39bf9SJosef Bacik 2491a8c93d4eSQu Wenruo /* 2492a8c93d4eSQu Wenruo * a higher idle thresh on the submit workers makes it much more 249361b49440SChris Mason * likely that bios will be send down in a sane order to the 249461b49440SChris Mason * devices 249561b49440SChris Mason */ 2496a8c93d4eSQu Wenruo fs_info->submit_workers = 2497a8c93d4eSQu Wenruo btrfs_alloc_workqueue("submit", flags, 2498a8c93d4eSQu Wenruo min_t(u64, fs_devices->num_devices, 2499a8c93d4eSQu Wenruo max_active), 64); 250053863232SChris Mason 2501dc6e3209SQu Wenruo fs_info->fixup_workers = 2502dc6e3209SQu Wenruo btrfs_alloc_workqueue("fixup", flags, 1, 0); 2503fccb5d86SQu Wenruo 2504fccb5d86SQu Wenruo /* 2505fccb5d86SQu Wenruo * endios are largely parallel and should have a very 2506fccb5d86SQu Wenruo * low idle thresh 2507fccb5d86SQu Wenruo */ 2508fccb5d86SQu Wenruo fs_info->endio_workers = 2509fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio", flags, max_active, 4); 2510fccb5d86SQu Wenruo fs_info->endio_meta_workers = 2511fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-meta", flags, max_active, 4); 2512fccb5d86SQu Wenruo fs_info->endio_meta_write_workers = 2513fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-meta-write", flags, max_active, 2); 2514fccb5d86SQu Wenruo fs_info->endio_raid56_workers = 2515fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-raid56", flags, max_active, 4); 2516d05a33acSQu Wenruo fs_info->rmw_workers = 2517d05a33acSQu Wenruo btrfs_alloc_workqueue("rmw", flags, max_active, 2); 2518fccb5d86SQu Wenruo fs_info->endio_write_workers = 2519fccb5d86SQu Wenruo btrfs_alloc_workqueue("endio-write", flags, max_active, 2); 2520fccb5d86SQu Wenruo fs_info->endio_freespace_worker = 2521fccb5d86SQu Wenruo btrfs_alloc_workqueue("freespace-write", flags, max_active, 0); 25225b3bc44eSQu Wenruo fs_info->delayed_workers = 25235b3bc44eSQu Wenruo btrfs_alloc_workqueue("delayed-meta", flags, max_active, 0); 2524736cfa15SQu Wenruo fs_info->readahead_workers = 2525736cfa15SQu Wenruo btrfs_alloc_workqueue("readahead", flags, max_active, 2); 2526fc97fab0SQu Wenruo fs_info->qgroup_rescan_workers = 2527fc97fab0SQu Wenruo btrfs_alloc_workqueue("qgroup-rescan", flags, 1, 0); 252861b49440SChris Mason 25290dc3b84aSJosef Bacik /* 25300dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 25310dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 25320dc3b84aSJosef Bacik */ 25335cdc7ad3SQu Wenruo ret = btrfs_start_workers(&fs_info->generic_worker); 25340dc3b84aSJosef Bacik if (ret) { 2535fed425c7SIlya Dryomov err = -ENOMEM; 25360dc3b84aSJosef Bacik goto fail_sb_buffer; 25370dc3b84aSJosef Bacik } 2538a8c93d4eSQu Wenruo if (!(fs_info->workers && fs_info->delalloc_workers && 2539fccb5d86SQu Wenruo fs_info->submit_workers && fs_info->flush_workers && 2540fccb5d86SQu Wenruo fs_info->endio_workers && fs_info->endio_meta_workers && 2541fccb5d86SQu Wenruo fs_info->endio_meta_write_workers && 2542fccb5d86SQu Wenruo fs_info->endio_write_workers && fs_info->endio_raid56_workers && 2543e66f0bb1SQu Wenruo fs_info->endio_freespace_worker && fs_info->rmw_workers && 2544dc6e3209SQu Wenruo fs_info->caching_workers && fs_info->readahead_workers && 2545fc97fab0SQu Wenruo fs_info->fixup_workers && fs_info->delayed_workers && 2546fc97fab0SQu Wenruo fs_info->qgroup_rescan_workers)) { 25475cdc7ad3SQu Wenruo err = -ENOMEM; 25485cdc7ad3SQu Wenruo goto fail_sb_buffer; 25495cdc7ad3SQu Wenruo } 25504543df7eSChris Mason 25514575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2552c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2553c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 25544575c9ccSChris Mason 2555db94535dSChris Mason tree_root->nodesize = nodesize; 2556db94535dSChris Mason tree_root->leafsize = leafsize; 2557db94535dSChris Mason tree_root->sectorsize = sectorsize; 255887ee04ebSChris Mason tree_root->stripesize = stripesize; 2559a061fc8dSChris Mason 2560a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2561a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2562db94535dSChris Mason 25633cae210fSQu Wenruo if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) { 2564efe120a0SFrank Holton printk(KERN_INFO "BTRFS: valid FS not found on %s\n", sb->s_id); 256539279cc3SChris Mason goto fail_sb_buffer; 256639279cc3SChris Mason } 256719c00ddcSChris Mason 25688d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 2569efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: Incompatible sector size(%lu) " 25708d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2571941b2ddfSKeith Mannthey goto fail_sb_buffer; 2572941b2ddfSKeith Mannthey } 2573941b2ddfSKeith Mannthey 2574925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2575e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2576925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 257784eed90fSChris Mason if (ret) { 2578efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read the system " 2579d397712bSChris Mason "array on %s\n", sb->s_id); 25805d4f98a2SYan Zheng goto fail_sb_buffer; 258184eed90fSChris Mason } 25820b86a832SChris Mason 25830b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 25840b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 258584234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 25860b86a832SChris Mason 25870b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 25880b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 25890b86a832SChris Mason 25900b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 25910b86a832SChris Mason btrfs_super_chunk_root(disk_super), 259284234f3aSYan Zheng blocksize, generation); 2593416bc658SJosef Bacik if (!chunk_root->node || 2594416bc658SJosef Bacik !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 2595efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read chunk root on %s\n", 259683121942SDavid Woodhouse sb->s_id); 2597af31f5e5SChris Mason goto fail_tree_roots; 259883121942SDavid Woodhouse } 25995d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 26005d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 26010b86a832SChris Mason 2602e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2603b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2604e17cade2SChris Mason 26050b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 26062b82032cSYan Zheng if (ret) { 2607efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read chunk tree on %s\n", 2608d397712bSChris Mason sb->s_id); 2609af31f5e5SChris Mason goto fail_tree_roots; 26102b82032cSYan Zheng } 26110b86a832SChris Mason 26128dabb742SStefan Behrens /* 26138dabb742SStefan Behrens * keep the device that is marked to be the target device for the 26148dabb742SStefan Behrens * dev_replace procedure 26158dabb742SStefan Behrens */ 26168dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2617dfe25020SChris Mason 2618a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2619efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: failed to read devices on %s\n", 2620a6b0d5c8SChris Mason sb->s_id); 2621a6b0d5c8SChris Mason goto fail_tree_roots; 2622a6b0d5c8SChris Mason } 2623a6b0d5c8SChris Mason 2624af31f5e5SChris Mason retry_root_backup: 2625db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2626db94535dSChris Mason btrfs_super_root_level(disk_super)); 262784234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 26280b86a832SChris Mason 2629e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2630db94535dSChris Mason btrfs_super_root(disk_super), 263184234f3aSYan Zheng blocksize, generation); 2632af31f5e5SChris Mason if (!tree_root->node || 2633af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 2634efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to read tree root on %s\n", 263583121942SDavid Woodhouse sb->s_id); 2636af31f5e5SChris Mason 2637af31f5e5SChris Mason goto recovery_tree_root; 263883121942SDavid Woodhouse } 2639af31f5e5SChris Mason 26405d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 26415d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 264269e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2643db94535dSChris Mason 2644cb517eabSMiao Xie location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 2645cb517eabSMiao Xie location.type = BTRFS_ROOT_ITEM_KEY; 2646cb517eabSMiao Xie location.offset = 0; 2647cb517eabSMiao Xie 2648cb517eabSMiao Xie extent_root = btrfs_read_tree_root(tree_root, &location); 2649cb517eabSMiao Xie if (IS_ERR(extent_root)) { 2650cb517eabSMiao Xie ret = PTR_ERR(extent_root); 2651af31f5e5SChris Mason goto recovery_tree_root; 2652cb517eabSMiao Xie } 26530b86a832SChris Mason extent_root->track_dirty = 1; 2654cb517eabSMiao Xie fs_info->extent_root = extent_root; 26550b86a832SChris Mason 2656cb517eabSMiao Xie location.objectid = BTRFS_DEV_TREE_OBJECTID; 2657cb517eabSMiao Xie dev_root = btrfs_read_tree_root(tree_root, &location); 2658cb517eabSMiao Xie if (IS_ERR(dev_root)) { 2659cb517eabSMiao Xie ret = PTR_ERR(dev_root); 2660af31f5e5SChris Mason goto recovery_tree_root; 2661cb517eabSMiao Xie } 26625d4f98a2SYan Zheng dev_root->track_dirty = 1; 2663cb517eabSMiao Xie fs_info->dev_root = dev_root; 2664cb517eabSMiao Xie btrfs_init_devices_late(fs_info); 26653768f368SChris Mason 2666cb517eabSMiao Xie location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2667cb517eabSMiao Xie csum_root = btrfs_read_tree_root(tree_root, &location); 2668cb517eabSMiao Xie if (IS_ERR(csum_root)) { 2669cb517eabSMiao Xie ret = PTR_ERR(csum_root); 2670af31f5e5SChris Mason goto recovery_tree_root; 2671cb517eabSMiao Xie } 2672d20f7043SChris Mason csum_root->track_dirty = 1; 2673cb517eabSMiao Xie fs_info->csum_root = csum_root; 2674d20f7043SChris Mason 2675cb517eabSMiao Xie location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2676cb517eabSMiao Xie quota_root = btrfs_read_tree_root(tree_root, &location); 2677cb517eabSMiao Xie if (!IS_ERR(quota_root)) { 2678bcef60f2SArne Jansen quota_root->track_dirty = 1; 2679bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2680bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2681cb517eabSMiao Xie fs_info->quota_root = quota_root; 2682bcef60f2SArne Jansen } 2683bcef60f2SArne Jansen 2684f7a81ea4SStefan Behrens location.objectid = BTRFS_UUID_TREE_OBJECTID; 2685f7a81ea4SStefan Behrens uuid_root = btrfs_read_tree_root(tree_root, &location); 2686f7a81ea4SStefan Behrens if (IS_ERR(uuid_root)) { 2687f7a81ea4SStefan Behrens ret = PTR_ERR(uuid_root); 2688f7a81ea4SStefan Behrens if (ret != -ENOENT) 2689f7a81ea4SStefan Behrens goto recovery_tree_root; 2690f7a81ea4SStefan Behrens create_uuid_tree = true; 269170f80175SStefan Behrens check_uuid_tree = false; 2692f7a81ea4SStefan Behrens } else { 2693f7a81ea4SStefan Behrens uuid_root->track_dirty = 1; 2694f7a81ea4SStefan Behrens fs_info->uuid_root = uuid_root; 269570f80175SStefan Behrens create_uuid_tree = false; 269670f80175SStefan Behrens check_uuid_tree = 269770f80175SStefan Behrens generation != btrfs_super_uuid_tree_generation(disk_super); 2698f7a81ea4SStefan Behrens } 2699f7a81ea4SStefan Behrens 27008929ecfaSYan, Zheng fs_info->generation = generation; 27018929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 27028929ecfaSYan, Zheng 270368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 270468310a5eSIlya Dryomov if (ret) { 2705efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to recover balance\n"); 270668310a5eSIlya Dryomov goto fail_block_groups; 270768310a5eSIlya Dryomov } 270868310a5eSIlya Dryomov 2709733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2710733f4fbbSStefan Behrens if (ret) { 2711efe120a0SFrank Holton printk(KERN_ERR "BTRFS: failed to init dev_stats: %d\n", 2712733f4fbbSStefan Behrens ret); 2713733f4fbbSStefan Behrens goto fail_block_groups; 2714733f4fbbSStefan Behrens } 2715733f4fbbSStefan Behrens 27168dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 27178dabb742SStefan Behrens if (ret) { 2718efe120a0SFrank Holton pr_err("BTRFS: failed to init dev_replace: %d\n", ret); 27198dabb742SStefan Behrens goto fail_block_groups; 27208dabb742SStefan Behrens } 27218dabb742SStefan Behrens 27228dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 27238dabb742SStefan Behrens 27245ac1d209SJeff Mahoney ret = btrfs_sysfs_add_one(fs_info); 27255ac1d209SJeff Mahoney if (ret) { 2726efe120a0SFrank Holton pr_err("BTRFS: failed to init sysfs interface: %d\n", ret); 27275ac1d209SJeff Mahoney goto fail_block_groups; 27285ac1d209SJeff Mahoney } 27295ac1d209SJeff Mahoney 2730c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2731c59021f8Sliubo if (ret) { 2732efe120a0SFrank Holton printk(KERN_ERR "BTRFS: Failed to initial space info: %d\n", ret); 27332365dd3cSAnand Jain goto fail_sysfs; 2734c59021f8Sliubo } 2735c59021f8Sliubo 27361b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 27371b1d1f66SJosef Bacik if (ret) { 2738efe120a0SFrank Holton printk(KERN_ERR "BTRFS: Failed to read block groups: %d\n", ret); 27392365dd3cSAnand Jain goto fail_sysfs; 27401b1d1f66SJosef Bacik } 27415af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 27425af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2743292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2744292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2745292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2746efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: " 2747efe120a0SFrank Holton "too many missing devices, writeable mount is not allowed\n"); 27482365dd3cSAnand Jain goto fail_sysfs; 2749292fd7fcSStefan Behrens } 27509078a3e1SChris Mason 2751a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2752a74a4b97SChris Mason "btrfs-cleaner"); 275357506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 27542365dd3cSAnand Jain goto fail_sysfs; 2755a74a4b97SChris Mason 2756a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2757a74a4b97SChris Mason tree_root, 2758a74a4b97SChris Mason "btrfs-transaction"); 275957506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 27603f157a2fSChris Mason goto fail_cleaner; 2761a74a4b97SChris Mason 2762c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2763c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2764c289811cSChris Mason !fs_info->fs_devices->rotating) { 2765efe120a0SFrank Holton printk(KERN_INFO "BTRFS: detected SSD devices, enabling SSD " 2766c289811cSChris Mason "mode\n"); 2767c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2768c289811cSChris Mason } 2769c289811cSChris Mason 27703818aea2SQu Wenruo /* Set the real inode map cache flag */ 27713818aea2SQu Wenruo if (btrfs_test_opt(tree_root, CHANGE_INODE_CACHE)) 27723818aea2SQu Wenruo btrfs_set_opt(tree_root->fs_info->mount_opt, INODE_MAP_CACHE); 27733818aea2SQu Wenruo 277421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 277521adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 277621adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 277721adbd5cSStefan Behrens btrfs_test_opt(tree_root, 277821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 277921adbd5cSStefan Behrens 1 : 0, 278021adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 278121adbd5cSStefan Behrens if (ret) 2782efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to initialize" 278321adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 278421adbd5cSStefan Behrens } 278521adbd5cSStefan Behrens #endif 2786bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2787bcef60f2SArne Jansen if (ret) 2788bcef60f2SArne Jansen goto fail_trans_kthread; 278921adbd5cSStefan Behrens 2790acce952bSliubo /* do not make disk changes in broken FS */ 279168ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2792e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2793d18a2c44SChris Mason 27947c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2795efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: log replay required " 2796d397712bSChris Mason "on RO media\n"); 27977c2ca468SChris Mason err = -EIO; 2798bcef60f2SArne Jansen goto fail_qgroup; 27997c2ca468SChris Mason } 2800e02119d5SChris Mason blocksize = 2801e02119d5SChris Mason btrfs_level_size(tree_root, 2802e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2803e02119d5SChris Mason 28046f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2805676e4c86SDan Carpenter if (!log_tree_root) { 2806676e4c86SDan Carpenter err = -ENOMEM; 2807bcef60f2SArne Jansen goto fail_qgroup; 2808676e4c86SDan Carpenter } 2809e02119d5SChris Mason 2810e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2811e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2812e02119d5SChris Mason 2813e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 281484234f3aSYan Zheng blocksize, 281584234f3aSYan Zheng generation + 1); 2816416bc658SJosef Bacik if (!log_tree_root->node || 2817416bc658SJosef Bacik !extent_buffer_uptodate(log_tree_root->node)) { 2818efe120a0SFrank Holton printk(KERN_ERR "BTRFS: failed to read log tree\n"); 2819416bc658SJosef Bacik free_extent_buffer(log_tree_root->node); 2820416bc658SJosef Bacik kfree(log_tree_root); 2821416bc658SJosef Bacik goto fail_trans_kthread; 2822416bc658SJosef Bacik } 282379787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2824e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 282579787eaaSJeff Mahoney if (ret) { 282679787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 282779787eaaSJeff Mahoney "Failed to recover log tree"); 282879787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 282979787eaaSJeff Mahoney kfree(log_tree_root); 283079787eaaSJeff Mahoney goto fail_trans_kthread; 283179787eaaSJeff Mahoney } 2832e556ce2cSYan Zheng 2833e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2834e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 283579787eaaSJeff Mahoney if (ret) 283679787eaaSJeff Mahoney goto fail_trans_kthread; 2837e556ce2cSYan Zheng } 2838e02119d5SChris Mason } 28391a40e23bSZheng Yan 284076dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 284179787eaaSJeff Mahoney if (ret) 284279787eaaSJeff Mahoney goto fail_trans_kthread; 284376dda93cSYan, Zheng 28447c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2845d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 284644c44af2SIlya Dryomov if (ret) 284744c44af2SIlya Dryomov goto fail_trans_kthread; 2848d68fc57bSYan, Zheng 28495d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2850d7ce5843SMiao Xie if (ret < 0) { 2851d7ce5843SMiao Xie printk(KERN_WARNING 2852efe120a0SFrank Holton "BTRFS: failed to recover relocation\n"); 2853d7ce5843SMiao Xie err = -EINVAL; 2854bcef60f2SArne Jansen goto fail_qgroup; 2855d7ce5843SMiao Xie } 28567c2ca468SChris Mason } 28571a40e23bSZheng Yan 28583de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 28593de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2860cb517eabSMiao Xie location.offset = 0; 28613de4586cSChris Mason 28623de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 28633140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 28643140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2865bcef60f2SArne Jansen goto fail_qgroup; 28663140c9a3SDan Carpenter } 2867c289811cSChris Mason 28682b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 28692b6ba629SIlya Dryomov return 0; 28702b6ba629SIlya Dryomov 2871e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 28722b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 28732b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 28742b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 28752b6ba629SIlya Dryomov close_ctree(tree_root); 28762b6ba629SIlya Dryomov return ret; 28772b6ba629SIlya Dryomov } 2878e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 287959641015SIlya Dryomov 28802b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 28812b6ba629SIlya Dryomov if (ret) { 2882efe120a0SFrank Holton printk(KERN_WARNING "BTRFS: failed to resume balance\n"); 288366b4ffd1SJosef Bacik close_ctree(tree_root); 28842b6ba629SIlya Dryomov return ret; 2885e3acc2a6SJosef Bacik } 2886e3acc2a6SJosef Bacik 28878dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 28888dabb742SStefan Behrens if (ret) { 2889efe120a0SFrank Holton pr_warn("BTRFS: failed to resume dev_replace\n"); 28908dabb742SStefan Behrens close_ctree(tree_root); 28918dabb742SStefan Behrens return ret; 28928dabb742SStefan Behrens } 28938dabb742SStefan Behrens 2894b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 2895b382a324SJan Schmidt 2896f7a81ea4SStefan Behrens if (create_uuid_tree) { 2897efe120a0SFrank Holton pr_info("BTRFS: creating UUID tree\n"); 2898f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 2899f7a81ea4SStefan Behrens if (ret) { 2900efe120a0SFrank Holton pr_warn("BTRFS: failed to create the UUID tree %d\n", 2901f7a81ea4SStefan Behrens ret); 2902f7a81ea4SStefan Behrens close_ctree(tree_root); 2903f7a81ea4SStefan Behrens return ret; 2904f7a81ea4SStefan Behrens } 2905f420ee1eSStefan Behrens } else if (check_uuid_tree || 2906f420ee1eSStefan Behrens btrfs_test_opt(tree_root, RESCAN_UUID_TREE)) { 2907efe120a0SFrank Holton pr_info("BTRFS: checking UUID tree\n"); 290870f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 290970f80175SStefan Behrens if (ret) { 2910efe120a0SFrank Holton pr_warn("BTRFS: failed to check the UUID tree %d\n", 291170f80175SStefan Behrens ret); 291270f80175SStefan Behrens close_ctree(tree_root); 291370f80175SStefan Behrens return ret; 291470f80175SStefan Behrens } 291570f80175SStefan Behrens } else { 291670f80175SStefan Behrens fs_info->update_uuid_tree_gen = 1; 2917f7a81ea4SStefan Behrens } 2918f7a81ea4SStefan Behrens 2919ad2b2c80SAl Viro return 0; 292039279cc3SChris Mason 2921bcef60f2SArne Jansen fail_qgroup: 2922bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 29237c2ca468SChris Mason fail_trans_kthread: 29247c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 292554067ae9SJosef Bacik btrfs_cleanup_transaction(fs_info->tree_root); 29267b5ff90eSJosef Bacik del_fs_roots(fs_info); 29273f157a2fSChris Mason fail_cleaner: 2928a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 29297c2ca468SChris Mason 29307c2ca468SChris Mason /* 29317c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 29327c2ca468SChris Mason * kthreads 29337c2ca468SChris Mason */ 29347c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 29357c2ca468SChris Mason 29362365dd3cSAnand Jain fail_sysfs: 29372365dd3cSAnand Jain btrfs_sysfs_remove_one(fs_info); 29382365dd3cSAnand Jain 29391b1d1f66SJosef Bacik fail_block_groups: 294054067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 29411b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2942af31f5e5SChris Mason 2943af31f5e5SChris Mason fail_tree_roots: 2944af31f5e5SChris Mason free_root_pointers(fs_info, 1); 29452b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2946af31f5e5SChris Mason 294739279cc3SChris Mason fail_sb_buffer: 29487abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 294916cdcec7SMiao Xie fail_alloc: 29504543df7eSChris Mason fail_iput: 2951586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2952586e46e2SIlya Dryomov 29534543df7eSChris Mason iput(fs_info->btree_inode); 2954c404e0dcSMiao Xie fail_bio_counter: 2955c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 2956963d678bSMiao Xie fail_delalloc_bytes: 2957963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 2958e2d84521SMiao Xie fail_dirty_metadata_bytes: 2959e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 2960ad081f14SJens Axboe fail_bdi: 29617e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 296276dda93cSYan, Zheng fail_srcu: 296376dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 29647e662854SQinghuang Feng fail: 296553b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 2966586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2967ad2b2c80SAl Viro return err; 2968af31f5e5SChris Mason 2969af31f5e5SChris Mason recovery_tree_root: 2970af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2971af31f5e5SChris Mason goto fail_tree_roots; 2972af31f5e5SChris Mason 2973af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2974af31f5e5SChris Mason 2975af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2976af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2977af31f5e5SChris Mason 2978af31f5e5SChris Mason /* we can't trust the free space cache either */ 2979af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2980af31f5e5SChris Mason 2981af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2982af31f5e5SChris Mason &num_backups_tried, &backup_index); 2983af31f5e5SChris Mason if (ret == -1) 2984af31f5e5SChris Mason goto fail_block_groups; 2985af31f5e5SChris Mason goto retry_root_backup; 2986eb60ceacSChris Mason } 2987eb60ceacSChris Mason 2988f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2989f2984462SChris Mason { 2990f2984462SChris Mason if (uptodate) { 2991f2984462SChris Mason set_buffer_uptodate(bh); 2992f2984462SChris Mason } else { 2993442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 2994442a4f63SStefan Behrens bh->b_private; 2995442a4f63SStefan Behrens 2996efe120a0SFrank Holton printk_ratelimited_in_rcu(KERN_WARNING "BTRFS: lost page write due to " 2997606686eeSJosef Bacik "I/O error on %s\n", 2998606686eeSJosef Bacik rcu_str_deref(device->name)); 29991259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 30001259ab75SChris Mason * our own ways of dealing with the IO errors 30011259ab75SChris Mason */ 3002f2984462SChris Mason clear_buffer_uptodate(bh); 3003442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3004f2984462SChris Mason } 3005f2984462SChris Mason unlock_buffer(bh); 3006f2984462SChris Mason put_bh(bh); 3007f2984462SChris Mason } 3008f2984462SChris Mason 3009a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3010a512bbf8SYan Zheng { 3011a512bbf8SYan Zheng struct buffer_head *bh; 3012a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3013a512bbf8SYan Zheng struct btrfs_super_block *super; 3014a512bbf8SYan Zheng int i; 3015a512bbf8SYan Zheng u64 transid = 0; 3016a512bbf8SYan Zheng u64 bytenr; 3017a512bbf8SYan Zheng 3018a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3019a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3020a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3021a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3022a512bbf8SYan Zheng */ 3023a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 3024a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 30258068a47eSAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= 30268068a47eSAnand Jain i_size_read(bdev->bd_inode)) 3027a512bbf8SYan Zheng break; 30288068a47eSAnand Jain bh = __bread(bdev, bytenr / 4096, 30298068a47eSAnand Jain BTRFS_SUPER_INFO_SIZE); 3030a512bbf8SYan Zheng if (!bh) 3031a512bbf8SYan Zheng continue; 3032a512bbf8SYan Zheng 3033a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3034a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 30353cae210fSQu Wenruo btrfs_super_magic(super) != BTRFS_MAGIC) { 3036a512bbf8SYan Zheng brelse(bh); 3037a512bbf8SYan Zheng continue; 3038a512bbf8SYan Zheng } 3039a512bbf8SYan Zheng 3040a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3041a512bbf8SYan Zheng brelse(latest); 3042a512bbf8SYan Zheng latest = bh; 3043a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3044a512bbf8SYan Zheng } else { 3045a512bbf8SYan Zheng brelse(bh); 3046a512bbf8SYan Zheng } 3047a512bbf8SYan Zheng } 3048a512bbf8SYan Zheng return latest; 3049a512bbf8SYan Zheng } 3050a512bbf8SYan Zheng 30514eedeb75SHisashi Hifumi /* 30524eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 30534eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 30544eedeb75SHisashi Hifumi * we write are pinned. 30554eedeb75SHisashi Hifumi * 30564eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 30574eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 30584eedeb75SHisashi Hifumi * many supers on this one device should be written. 30594eedeb75SHisashi Hifumi * 30604eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 30614eedeb75SHisashi Hifumi */ 3062a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3063a512bbf8SYan Zheng struct btrfs_super_block *sb, 3064a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 3065a512bbf8SYan Zheng { 3066a512bbf8SYan Zheng struct buffer_head *bh; 3067a512bbf8SYan Zheng int i; 3068a512bbf8SYan Zheng int ret; 3069a512bbf8SYan Zheng int errors = 0; 3070a512bbf8SYan Zheng u32 crc; 3071a512bbf8SYan Zheng u64 bytenr; 3072a512bbf8SYan Zheng 3073a512bbf8SYan Zheng if (max_mirrors == 0) 3074a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3075a512bbf8SYan Zheng 3076a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3077a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3078a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 3079a512bbf8SYan Zheng break; 3080a512bbf8SYan Zheng 3081a512bbf8SYan Zheng if (wait) { 3082a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3083a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3084634554dcSJosef Bacik if (!bh) { 3085634554dcSJosef Bacik errors++; 3086634554dcSJosef Bacik continue; 3087634554dcSJosef Bacik } 3088a512bbf8SYan Zheng wait_on_buffer(bh); 30894eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 30904eedeb75SHisashi Hifumi errors++; 30914eedeb75SHisashi Hifumi 30924eedeb75SHisashi Hifumi /* drop our reference */ 30934eedeb75SHisashi Hifumi brelse(bh); 30944eedeb75SHisashi Hifumi 30954eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3096a512bbf8SYan Zheng brelse(bh); 3097a512bbf8SYan Zheng continue; 3098a512bbf8SYan Zheng } else { 3099a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3100a512bbf8SYan Zheng 3101a512bbf8SYan Zheng crc = ~(u32)0; 3102b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3103a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3104a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3105a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3106a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3107a512bbf8SYan Zheng 31084eedeb75SHisashi Hifumi /* 31094eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 31104eedeb75SHisashi Hifumi * caller 31114eedeb75SHisashi Hifumi */ 3112a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3113a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3114634554dcSJosef Bacik if (!bh) { 3115efe120a0SFrank Holton printk(KERN_ERR "BTRFS: couldn't get super " 3116634554dcSJosef Bacik "buffer head for bytenr %Lu\n", bytenr); 3117634554dcSJosef Bacik errors++; 3118634554dcSJosef Bacik continue; 3119634554dcSJosef Bacik } 3120634554dcSJosef Bacik 3121a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3122a512bbf8SYan Zheng 31234eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3124a512bbf8SYan Zheng get_bh(bh); 31254eedeb75SHisashi Hifumi 31264eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3127a512bbf8SYan Zheng lock_buffer(bh); 3128a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3129442a4f63SStefan Behrens bh->b_private = device; 3130a512bbf8SYan Zheng } 3131a512bbf8SYan Zheng 3132387125fcSChris Mason /* 3133387125fcSChris Mason * we fua the first super. The others we allow 3134387125fcSChris Mason * to go down lazy. 3135387125fcSChris Mason */ 3136e8117c26SWang Shilong if (i == 0) 313721adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 3138e8117c26SWang Shilong else 3139e8117c26SWang Shilong ret = btrfsic_submit_bh(WRITE_SYNC, bh); 31404eedeb75SHisashi Hifumi if (ret) 3141a512bbf8SYan Zheng errors++; 3142a512bbf8SYan Zheng } 3143a512bbf8SYan Zheng return errors < i ? 0 : -1; 3144a512bbf8SYan Zheng } 3145a512bbf8SYan Zheng 3146387125fcSChris Mason /* 3147387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3148387125fcSChris Mason * for the barrier when it is done 3149387125fcSChris Mason */ 3150387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 3151387125fcSChris Mason { 3152387125fcSChris Mason if (err) { 3153387125fcSChris Mason if (err == -EOPNOTSUPP) 3154387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 3155387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 3156387125fcSChris Mason } 3157387125fcSChris Mason if (bio->bi_private) 3158387125fcSChris Mason complete(bio->bi_private); 3159387125fcSChris Mason bio_put(bio); 3160387125fcSChris Mason } 3161387125fcSChris Mason 3162387125fcSChris Mason /* 3163387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3164387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3165387125fcSChris Mason * 3166387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3167387125fcSChris Mason * capable 3168387125fcSChris Mason */ 3169387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3170387125fcSChris Mason { 3171387125fcSChris Mason struct bio *bio; 3172387125fcSChris Mason int ret = 0; 3173387125fcSChris Mason 3174387125fcSChris Mason if (device->nobarriers) 3175387125fcSChris Mason return 0; 3176387125fcSChris Mason 3177387125fcSChris Mason if (wait) { 3178387125fcSChris Mason bio = device->flush_bio; 3179387125fcSChris Mason if (!bio) 3180387125fcSChris Mason return 0; 3181387125fcSChris Mason 3182387125fcSChris Mason wait_for_completion(&device->flush_wait); 3183387125fcSChris Mason 3184387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 3185efe120a0SFrank Holton printk_in_rcu("BTRFS: disabling barriers on dev %s\n", 3186606686eeSJosef Bacik rcu_str_deref(device->name)); 3187387125fcSChris Mason device->nobarriers = 1; 31885af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 3189387125fcSChris Mason ret = -EIO; 3190442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3191442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3192387125fcSChris Mason } 3193387125fcSChris Mason 3194387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3195387125fcSChris Mason bio_put(bio); 3196387125fcSChris Mason device->flush_bio = NULL; 3197387125fcSChris Mason 3198387125fcSChris Mason return ret; 3199387125fcSChris Mason } 3200387125fcSChris Mason 3201387125fcSChris Mason /* 3202387125fcSChris Mason * one reference for us, and we leave it for the 3203387125fcSChris Mason * caller 3204387125fcSChris Mason */ 32059c017abcSJesper Juhl device->flush_bio = NULL; 32069be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3207387125fcSChris Mason if (!bio) 3208387125fcSChris Mason return -ENOMEM; 3209387125fcSChris Mason 3210387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3211387125fcSChris Mason bio->bi_bdev = device->bdev; 3212387125fcSChris Mason init_completion(&device->flush_wait); 3213387125fcSChris Mason bio->bi_private = &device->flush_wait; 3214387125fcSChris Mason device->flush_bio = bio; 3215387125fcSChris Mason 3216387125fcSChris Mason bio_get(bio); 321721adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 3218387125fcSChris Mason 3219387125fcSChris Mason return 0; 3220387125fcSChris Mason } 3221387125fcSChris Mason 3222387125fcSChris Mason /* 3223387125fcSChris Mason * send an empty flush down to each device in parallel, 3224387125fcSChris Mason * then wait for them 3225387125fcSChris Mason */ 3226387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3227387125fcSChris Mason { 3228387125fcSChris Mason struct list_head *head; 3229387125fcSChris Mason struct btrfs_device *dev; 32305af3e8ccSStefan Behrens int errors_send = 0; 32315af3e8ccSStefan Behrens int errors_wait = 0; 3232387125fcSChris Mason int ret; 3233387125fcSChris Mason 3234387125fcSChris Mason /* send down all the barriers */ 3235387125fcSChris Mason head = &info->fs_devices->devices; 3236387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3237f88ba6a2SHidetoshi Seto if (dev->missing) 3238f88ba6a2SHidetoshi Seto continue; 3239387125fcSChris Mason if (!dev->bdev) { 32405af3e8ccSStefan Behrens errors_send++; 3241387125fcSChris Mason continue; 3242387125fcSChris Mason } 3243387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3244387125fcSChris Mason continue; 3245387125fcSChris Mason 3246387125fcSChris Mason ret = write_dev_flush(dev, 0); 3247387125fcSChris Mason if (ret) 32485af3e8ccSStefan Behrens errors_send++; 3249387125fcSChris Mason } 3250387125fcSChris Mason 3251387125fcSChris Mason /* wait for all the barriers */ 3252387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3253f88ba6a2SHidetoshi Seto if (dev->missing) 3254f88ba6a2SHidetoshi Seto continue; 3255387125fcSChris Mason if (!dev->bdev) { 32565af3e8ccSStefan Behrens errors_wait++; 3257387125fcSChris Mason continue; 3258387125fcSChris Mason } 3259387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3260387125fcSChris Mason continue; 3261387125fcSChris Mason 3262387125fcSChris Mason ret = write_dev_flush(dev, 1); 3263387125fcSChris Mason if (ret) 32645af3e8ccSStefan Behrens errors_wait++; 3265387125fcSChris Mason } 32665af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 32675af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3268387125fcSChris Mason return -EIO; 3269387125fcSChris Mason return 0; 3270387125fcSChris Mason } 3271387125fcSChris Mason 32725af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 32735af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 32745af3e8ccSStefan Behrens { 32755af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 32765af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 32775af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 32785af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 32795af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 32805af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 32815af3e8ccSStefan Behrens int num_types = 4; 32825af3e8ccSStefan Behrens int i; 32835af3e8ccSStefan Behrens int c; 32845af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 32855af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 32865af3e8ccSStefan Behrens 32875af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 32885af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 32895af3e8ccSStefan Behrens 32905af3e8ccSStefan Behrens sinfo = NULL; 32915af3e8ccSStefan Behrens rcu_read_lock(); 32925af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 32935af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 32945af3e8ccSStefan Behrens sinfo = tmp; 32955af3e8ccSStefan Behrens break; 32965af3e8ccSStefan Behrens } 32975af3e8ccSStefan Behrens } 32985af3e8ccSStefan Behrens rcu_read_unlock(); 32995af3e8ccSStefan Behrens 33005af3e8ccSStefan Behrens if (!sinfo) 33015af3e8ccSStefan Behrens continue; 33025af3e8ccSStefan Behrens 33035af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 33045af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 33055af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 33065af3e8ccSStefan Behrens u64 flags; 33075af3e8ccSStefan Behrens 33085af3e8ccSStefan Behrens btrfs_get_block_group_info( 33095af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 33105af3e8ccSStefan Behrens if (space.total_bytes == 0 || 33115af3e8ccSStefan Behrens space.used_bytes == 0) 33125af3e8ccSStefan Behrens continue; 33135af3e8ccSStefan Behrens flags = space.flags; 33145af3e8ccSStefan Behrens /* 33155af3e8ccSStefan Behrens * return 33165af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 33175af3e8ccSStefan Behrens * any of metadata, system or data, else 33185af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 33195af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 33205af3e8ccSStefan Behrens * are used, else 33215af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 33225af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 33235af3e8ccSStefan Behrens * configured and more than 33245af3e8ccSStefan Behrens * 2 mirrors are used. 33255af3e8ccSStefan Behrens */ 33265af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 33275af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 33285af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 33295af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 33305af3e8ccSStefan Behrens == 0))) 33315af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 333253b381b3SDavid Woodhouse else if (num_tolerated_disk_barrier_failures > 1) { 333353b381b3SDavid Woodhouse if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 333453b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5 | 333553b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID10)) { 33365af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 333753b381b3SDavid Woodhouse } else if (flags & 333815b0a89dSHenrik Nordvik BTRFS_BLOCK_GROUP_RAID6) { 333953b381b3SDavid Woodhouse num_tolerated_disk_barrier_failures = 2; 334053b381b3SDavid Woodhouse } 334153b381b3SDavid Woodhouse } 33425af3e8ccSStefan Behrens } 33435af3e8ccSStefan Behrens } 33445af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 33455af3e8ccSStefan Behrens } 33465af3e8ccSStefan Behrens 33475af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 33485af3e8ccSStefan Behrens } 33495af3e8ccSStefan Behrens 335048a3b636SEric Sandeen static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3351f2984462SChris Mason { 3352e5e9a520SChris Mason struct list_head *head; 3353f2984462SChris Mason struct btrfs_device *dev; 3354a061fc8dSChris Mason struct btrfs_super_block *sb; 3355f2984462SChris Mason struct btrfs_dev_item *dev_item; 3356f2984462SChris Mason int ret; 3357f2984462SChris Mason int do_barriers; 3358a236aed1SChris Mason int max_errors; 3359a236aed1SChris Mason int total_errors = 0; 3360a061fc8dSChris Mason u64 flags; 3361f2984462SChris Mason 3362f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3363af31f5e5SChris Mason backup_super_roots(root->fs_info); 3364f2984462SChris Mason 33656c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3366a061fc8dSChris Mason dev_item = &sb->dev_item; 3367e5e9a520SChris Mason 3368174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3369e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3370d7306801SFilipe David Borba Manana max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3371387125fcSChris Mason 33725af3e8ccSStefan Behrens if (do_barriers) { 33735af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 33745af3e8ccSStefan Behrens if (ret) { 33755af3e8ccSStefan Behrens mutex_unlock( 33765af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 33775af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 33785af3e8ccSStefan Behrens "errors while submitting device barriers."); 33795af3e8ccSStefan Behrens return ret; 33805af3e8ccSStefan Behrens } 33815af3e8ccSStefan Behrens } 3382387125fcSChris Mason 33831f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3384dfe25020SChris Mason if (!dev->bdev) { 3385dfe25020SChris Mason total_errors++; 3386dfe25020SChris Mason continue; 3387dfe25020SChris Mason } 33882b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3389dfe25020SChris Mason continue; 3390dfe25020SChris Mason 33912b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3392a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3393a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 3394a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 3395a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 3396a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3397a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3398a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3399a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 34002b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3401a512bbf8SYan Zheng 3402a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3403a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3404f2984462SChris Mason 3405a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3406a236aed1SChris Mason if (ret) 3407a236aed1SChris Mason total_errors++; 3408f2984462SChris Mason } 3409a236aed1SChris Mason if (total_errors > max_errors) { 3410efe120a0SFrank Holton btrfs_err(root->fs_info, "%d errors while writing supers", 3411d397712bSChris Mason total_errors); 3412a724b436SStefan Behrens mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 341379787eaaSJeff Mahoney 34149d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 34159d565ba4SStefan Behrens btrfs_error(root->fs_info, -EIO, 34169d565ba4SStefan Behrens "%d errors while writing supers", total_errors); 34179d565ba4SStefan Behrens return -EIO; 3418a236aed1SChris Mason } 3419f2984462SChris Mason 3420a512bbf8SYan Zheng total_errors = 0; 34211f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3422dfe25020SChris Mason if (!dev->bdev) 3423dfe25020SChris Mason continue; 34242b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3425dfe25020SChris Mason continue; 3426dfe25020SChris Mason 3427a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3428a512bbf8SYan Zheng if (ret) 34291259ab75SChris Mason total_errors++; 3430f2984462SChris Mason } 3431174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3432a236aed1SChris Mason if (total_errors > max_errors) { 343379787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 343479787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 343579787eaaSJeff Mahoney return -EIO; 3436a236aed1SChris Mason } 3437f2984462SChris Mason return 0; 3438f2984462SChris Mason } 3439f2984462SChris Mason 3440a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3441a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3442cfaa7295SChris Mason { 3443f570e757SRashika return write_all_supers(root, max_mirrors); 3444cfaa7295SChris Mason } 3445cfaa7295SChris Mason 3446cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3447cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3448cb517eabSMiao Xie struct btrfs_root *root) 34492619ba1fSChris Mason { 34504df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 34512619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 34522619ba1fSChris Mason (unsigned long)root->root_key.objectid); 34534df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 345476dda93cSYan, Zheng 345576dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 345676dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 345776dda93cSYan, Zheng 34581a4319ccSLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 34593321719eSLiu Bo btrfs_free_log(NULL, root); 34603321719eSLiu Bo 3461581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3462581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 34634df27c4dSYan, Zheng free_fs_root(root); 34644df27c4dSYan, Zheng } 34654df27c4dSYan, Zheng 34664df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 34674df27c4dSYan, Zheng { 346882d5902dSLi Zefan iput(root->cache_inode); 34694df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 34701cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 34711cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 34720ee5dc67SAl Viro if (root->anon_dev) 34730ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 34745f39d397SChris Mason free_extent_buffer(root->node); 34755f39d397SChris Mason free_extent_buffer(root->commit_root); 3476581bb050SLi Zefan kfree(root->free_ino_ctl); 3477581bb050SLi Zefan kfree(root->free_ino_pinned); 347858176a96SJosef Bacik kfree(root->name); 3479b0feb9d9SMiao Xie btrfs_put_fs_root(root); 34802619ba1fSChris Mason } 34812619ba1fSChris Mason 3482cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3483cb517eabSMiao Xie { 3484cb517eabSMiao Xie free_fs_root(root); 3485eb60ceacSChris Mason } 3486eb60ceacSChris Mason 3487c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3488c146afadSYan Zheng { 3489c146afadSYan Zheng u64 root_objectid = 0; 3490c146afadSYan Zheng struct btrfs_root *gang[8]; 3491c146afadSYan Zheng int i; 3492c146afadSYan Zheng int ret; 3493c146afadSYan Zheng 3494c146afadSYan Zheng while (1) { 3495c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3496c146afadSYan Zheng (void **)gang, root_objectid, 3497c146afadSYan Zheng ARRAY_SIZE(gang)); 3498c146afadSYan Zheng if (!ret) 3499c146afadSYan Zheng break; 35005d4f98a2SYan Zheng 35015d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3502c146afadSYan Zheng for (i = 0; i < ret; i++) { 350366b4ffd1SJosef Bacik int err; 350466b4ffd1SJosef Bacik 3505c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 350666b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 350766b4ffd1SJosef Bacik if (err) 350866b4ffd1SJosef Bacik return err; 3509c146afadSYan Zheng } 3510c146afadSYan Zheng root_objectid++; 3511c146afadSYan Zheng } 3512c146afadSYan Zheng return 0; 3513c146afadSYan Zheng } 3514c146afadSYan Zheng 3515c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3516c146afadSYan Zheng { 3517c146afadSYan Zheng struct btrfs_trans_handle *trans; 3518c146afadSYan Zheng 3519c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 352024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3521c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 35229d1a2a3aSDavid Sterba wake_up_process(root->fs_info->cleaner_kthread); 3523c71bf099SYan, Zheng 3524c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3525c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3526c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3527c71bf099SYan, Zheng 35287a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 35293612b495STsutomu Itoh if (IS_ERR(trans)) 35303612b495STsutomu Itoh return PTR_ERR(trans); 3531d52c1bccSLiu Bo return btrfs_commit_transaction(trans, root); 3532c146afadSYan Zheng } 3533c146afadSYan Zheng 3534e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3535eb60ceacSChris Mason { 35360f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3537c146afadSYan Zheng int ret; 3538e089f05cSChris Mason 3539facda1e7SChris Mason fs_info->closing = 1; 3540a2135011SChris Mason smp_mb(); 3541a2135011SChris Mason 3542803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3543803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3544803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3545803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3546803b2f54SStefan Behrens 3547837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3548aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3549837d5b6eSIlya Dryomov 35508dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 35518dabb742SStefan Behrens 3552aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 35534cb5300bSChris Mason 35544cb5300bSChris Mason /* wait for any defraggers to finish */ 35554cb5300bSChris Mason wait_event(fs_info->transaction_wait, 35564cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 35574cb5300bSChris Mason 35584cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 355926176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 35604cb5300bSChris Mason 3561c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3562c146afadSYan Zheng ret = btrfs_commit_super(root); 3563d397712bSChris Mason if (ret) 3564efe120a0SFrank Holton btrfs_err(root->fs_info, "commit super ret %d", ret); 3565c146afadSYan Zheng } 3566ed2ff2cbSChris Mason 356787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 356868ce9682SStefan Behrens btrfs_error_commit_super(root); 3569acce952bSliubo 3570e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3571e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 35728929ecfaSYan, Zheng 3573f25784b3SYan Zheng fs_info->closing = 2; 3574f25784b3SYan Zheng smp_mb(); 3575f25784b3SYan Zheng 3576bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3577bcef60f2SArne Jansen 3578963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3579efe120a0SFrank Holton btrfs_info(root->fs_info, "at unmount delalloc count %lld", 3580963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3581b0c68f8bSChris Mason } 358231153d81SYan Zheng 35835ac1d209SJeff Mahoney btrfs_sysfs_remove_one(fs_info); 35845ac1d209SJeff Mahoney 35850f7d52f4SChris Mason del_fs_roots(fs_info); 3586d10c5f31SChris Mason 35871a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info); 35881a4319ccSLiu Bo 35892b1360daSJosef Bacik btrfs_free_block_groups(fs_info); 35902b1360daSJosef Bacik 359196192499SJosef Bacik btrfs_stop_all_workers(fs_info); 359296192499SJosef Bacik 359313e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 35949ad6b7bcSChris Mason 359513e6c37bSJosef Bacik iput(fs_info->btree_inode); 3596d6bfde87SChris Mason 359721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 359821adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 359921adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 360021adbd5cSStefan Behrens #endif 360121adbd5cSStefan Behrens 3602dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 36030b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3604b248a415SChris Mason 3605e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3606963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 3607c404e0dcSMiao Xie percpu_counter_destroy(&fs_info->bio_counter); 360804160088SChris Mason bdi_destroy(&fs_info->bdi); 360976dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 36100b86a832SChris Mason 361153b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 361253b381b3SDavid Woodhouse 36131cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 36141cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 36151cb048f5SFilipe David Borba Manana 3616eb60ceacSChris Mason return 0; 3617eb60ceacSChris Mason } 3618eb60ceacSChris Mason 3619b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3620b9fab919SChris Mason int atomic) 3621ccd467d6SChris Mason { 36221259ab75SChris Mason int ret; 3623727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 36241259ab75SChris Mason 36250b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 36261259ab75SChris Mason if (!ret) 36271259ab75SChris Mason return ret; 36281259ab75SChris Mason 36291259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3630b9fab919SChris Mason parent_transid, atomic); 3631b9fab919SChris Mason if (ret == -EAGAIN) 3632b9fab919SChris Mason return ret; 36331259ab75SChris Mason return !ret; 36345f39d397SChris Mason } 36356702ed49SChris Mason 36365f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 36375f39d397SChris Mason { 36380b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 36395f39d397SChris Mason } 36405f39d397SChris Mason 36415f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 36425f39d397SChris Mason { 364306ea65a3SJosef Bacik struct btrfs_root *root; 36445f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3645b9473439SChris Mason int was_dirty; 3646b4ce94deSChris Mason 364706ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 364806ea65a3SJosef Bacik /* 364906ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 365006ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 365106ea65a3SJosef Bacik * outside of the sanity tests. 365206ea65a3SJosef Bacik */ 365306ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 365406ea65a3SJosef Bacik return; 365506ea65a3SJosef Bacik #endif 365606ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 3657b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 365831b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 365931b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3660d397712bSChris Mason "found %llu running %llu\n", 3661c1c9ff7cSGeert Uytterhoeven buf->start, transid, root->fs_info->generation); 36620b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3663e2d84521SMiao Xie if (!was_dirty) 3664e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3665e2d84521SMiao Xie buf->len, 3666e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 3667eb60ceacSChris Mason } 3668eb60ceacSChris Mason 3669b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3670b53d3f5dSLiu Bo int flush_delayed) 367135b7e476SChris Mason { 3672188de649SChris Mason /* 3673188de649SChris Mason * looks as though older kernels can get into trouble with 3674188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3675188de649SChris Mason */ 3676e2d84521SMiao Xie int ret; 3677d6bfde87SChris Mason 36786933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3679d6bfde87SChris Mason return; 3680d6bfde87SChris Mason 3681b53d3f5dSLiu Bo if (flush_delayed) 368216cdcec7SMiao Xie btrfs_balance_delayed_items(root); 368316cdcec7SMiao Xie 3684e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3685e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3686e2d84521SMiao Xie if (ret > 0) { 3687d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited( 3688d0e1d66bSNamjae Jeon root->fs_info->btree_inode->i_mapping); 368916cdcec7SMiao Xie } 369016cdcec7SMiao Xie return; 369116cdcec7SMiao Xie } 369216cdcec7SMiao Xie 3693b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 369416cdcec7SMiao Xie { 3695b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 369635b7e476SChris Mason } 3697b53d3f5dSLiu Bo 3698b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3699b53d3f5dSLiu Bo { 3700b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3701d6bfde87SChris Mason } 37026b80053dSChris Mason 3703ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 37046b80053dSChris Mason { 3705727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 37060b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 37076b80053dSChris Mason } 37080da5468fSChris Mason 3709fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3710acce952bSliubo int read_only) 3711acce952bSliubo { 37121104a885SDavid Sterba /* 37131104a885SDavid Sterba * Placeholder for checks 37141104a885SDavid Sterba */ 3715fcd1f065SDavid Sterba return 0; 3716fcd1f065SDavid Sterba } 3717fcd1f065SDavid Sterba 371848a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root) 3719acce952bSliubo { 3720acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3721acce952bSliubo btrfs_run_delayed_iputs(root); 3722acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3723acce952bSliubo 3724acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3725acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3726acce952bSliubo 3727acce952bSliubo /* cleanup FS via transaction */ 3728acce952bSliubo btrfs_cleanup_transaction(root); 3729acce952bSliubo } 3730acce952bSliubo 3731569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 3732569e0f35SJosef Bacik struct btrfs_root *root) 3733acce952bSliubo { 3734acce952bSliubo struct btrfs_inode *btrfs_inode; 3735acce952bSliubo struct list_head splice; 3736acce952bSliubo 3737acce952bSliubo INIT_LIST_HEAD(&splice); 3738acce952bSliubo 3739acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3740199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3741acce952bSliubo 3742569e0f35SJosef Bacik list_splice_init(&t->ordered_operations, &splice); 3743acce952bSliubo while (!list_empty(&splice)) { 3744acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3745acce952bSliubo ordered_operations); 3746acce952bSliubo 3747acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3748199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3749acce952bSliubo 3750acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3751b216cbfbSMiao Xie 3752199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3753acce952bSliubo } 3754acce952bSliubo 3755199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3756acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3757acce952bSliubo } 3758acce952bSliubo 3759143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3760acce952bSliubo { 3761acce952bSliubo struct btrfs_ordered_extent *ordered; 3762acce952bSliubo 3763199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 3764779880efSJosef Bacik /* 3765779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3766779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3767779880efSJosef Bacik */ 3768199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 3769779880efSJosef Bacik root_extent_list) 3770779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3771199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 3772199c2a9cSMiao Xie } 3773199c2a9cSMiao Xie 3774199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 3775199c2a9cSMiao Xie { 3776199c2a9cSMiao Xie struct btrfs_root *root; 3777199c2a9cSMiao Xie struct list_head splice; 3778199c2a9cSMiao Xie 3779199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 3780199c2a9cSMiao Xie 3781199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 3782199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 3783199c2a9cSMiao Xie while (!list_empty(&splice)) { 3784199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3785199c2a9cSMiao Xie ordered_root); 37861de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 37871de2cfdeSJosef Bacik &fs_info->ordered_roots); 3788199c2a9cSMiao Xie 37892a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock); 3790199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 3791199c2a9cSMiao Xie 37922a85d9caSLiu Bo cond_resched(); 37932a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock); 3794199c2a9cSMiao Xie } 3795199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 3796acce952bSliubo } 3797acce952bSliubo 379835a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3799acce952bSliubo struct btrfs_root *root) 3800acce952bSliubo { 3801acce952bSliubo struct rb_node *node; 3802acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3803acce952bSliubo struct btrfs_delayed_ref_node *ref; 3804acce952bSliubo int ret = 0; 3805acce952bSliubo 3806acce952bSliubo delayed_refs = &trans->delayed_refs; 3807acce952bSliubo 3808acce952bSliubo spin_lock(&delayed_refs->lock); 3809d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) { 3810cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3811efe120a0SFrank Holton btrfs_info(root->fs_info, "delayed_refs has NO entry"); 3812acce952bSliubo return ret; 3813acce952bSliubo } 3814acce952bSliubo 3815d7df2c79SJosef Bacik while ((node = rb_first(&delayed_refs->href_root)) != NULL) { 3816d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head; 3817e78417d1SJosef Bacik bool pin_bytes = false; 3818acce952bSliubo 3819d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head, 3820d7df2c79SJosef Bacik href_node); 3821b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3822d7df2c79SJosef Bacik atomic_inc(&head->node.refs); 382379787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3824b939d1abSJosef Bacik 3825acce952bSliubo mutex_lock(&head->mutex); 3826b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3827d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 3828e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3829b939d1abSJosef Bacik continue; 3830b939d1abSJosef Bacik } 3831d7df2c79SJosef Bacik spin_lock(&head->lock); 3832d7df2c79SJosef Bacik while ((node = rb_first(&head->ref_root)) != NULL) { 3833d7df2c79SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, 3834d7df2c79SJosef Bacik rb_node); 3835d7df2c79SJosef Bacik ref->in_tree = 0; 3836d7df2c79SJosef Bacik rb_erase(&ref->rb_node, &head->ref_root); 3837d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 3838d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref); 3839d7df2c79SJosef Bacik } 384054067ae9SJosef Bacik if (head->must_insert_reserved) 3841e78417d1SJosef Bacik pin_bytes = true; 384278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 3843acce952bSliubo delayed_refs->num_heads--; 3844d7df2c79SJosef Bacik if (head->processing == 0) 3845acce952bSliubo delayed_refs->num_heads_ready--; 3846d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries); 3847d7df2c79SJosef Bacik head->node.in_tree = 0; 3848c46effa6SLiu Bo rb_erase(&head->href_node, &delayed_refs->href_root); 3849d7df2c79SJosef Bacik spin_unlock(&head->lock); 3850acce952bSliubo spin_unlock(&delayed_refs->lock); 3851e78417d1SJosef Bacik mutex_unlock(&head->mutex); 3852acce952bSliubo 3853d7df2c79SJosef Bacik if (pin_bytes) 3854d7df2c79SJosef Bacik btrfs_pin_extent(root, head->node.bytenr, 3855d7df2c79SJosef Bacik head->node.num_bytes, 1); 3856d7df2c79SJosef Bacik btrfs_put_delayed_ref(&head->node); 3857acce952bSliubo cond_resched(); 3858acce952bSliubo spin_lock(&delayed_refs->lock); 3859acce952bSliubo } 3860acce952bSliubo 3861acce952bSliubo spin_unlock(&delayed_refs->lock); 3862acce952bSliubo 3863acce952bSliubo return ret; 3864acce952bSliubo } 3865acce952bSliubo 3866143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3867acce952bSliubo { 3868acce952bSliubo struct btrfs_inode *btrfs_inode; 3869acce952bSliubo struct list_head splice; 3870acce952bSliubo 3871acce952bSliubo INIT_LIST_HEAD(&splice); 3872acce952bSliubo 3873eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3874eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 3875acce952bSliubo 3876acce952bSliubo while (!list_empty(&splice)) { 3877eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 3878acce952bSliubo delalloc_inodes); 3879acce952bSliubo 3880acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3881df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 3882df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 3883eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3884acce952bSliubo 3885acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3886b216cbfbSMiao Xie 3887eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3888acce952bSliubo } 3889acce952bSliubo 3890eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3891eb73c1b7SMiao Xie } 3892eb73c1b7SMiao Xie 3893eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 3894eb73c1b7SMiao Xie { 3895eb73c1b7SMiao Xie struct btrfs_root *root; 3896eb73c1b7SMiao Xie struct list_head splice; 3897eb73c1b7SMiao Xie 3898eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 3899eb73c1b7SMiao Xie 3900eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3901eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 3902eb73c1b7SMiao Xie while (!list_empty(&splice)) { 3903eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3904eb73c1b7SMiao Xie delalloc_root); 3905eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 3906eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 3907eb73c1b7SMiao Xie BUG_ON(!root); 3908eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3909eb73c1b7SMiao Xie 3910eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 3911eb73c1b7SMiao Xie btrfs_put_fs_root(root); 3912eb73c1b7SMiao Xie 3913eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3914eb73c1b7SMiao Xie } 3915eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3916acce952bSliubo } 3917acce952bSliubo 3918acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3919acce952bSliubo struct extent_io_tree *dirty_pages, 3920acce952bSliubo int mark) 3921acce952bSliubo { 3922acce952bSliubo int ret; 3923acce952bSliubo struct extent_buffer *eb; 3924acce952bSliubo u64 start = 0; 3925acce952bSliubo u64 end; 3926acce952bSliubo 3927acce952bSliubo while (1) { 3928acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3929e6138876SJosef Bacik mark, NULL); 3930acce952bSliubo if (ret) 3931acce952bSliubo break; 3932acce952bSliubo 3933acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3934acce952bSliubo while (start <= end) { 3935fd8b2b61SJosef Bacik eb = btrfs_find_tree_block(root, start, 3936fd8b2b61SJosef Bacik root->leafsize); 393769a85bd8SJosef Bacik start += root->leafsize; 3938fd8b2b61SJosef Bacik if (!eb) 3939acce952bSliubo continue; 3940fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 3941acce952bSliubo 3942fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3943fd8b2b61SJosef Bacik &eb->bflags)) 3944fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 3945fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 3946acce952bSliubo } 3947acce952bSliubo } 3948acce952bSliubo 3949acce952bSliubo return ret; 3950acce952bSliubo } 3951acce952bSliubo 3952acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3953acce952bSliubo struct extent_io_tree *pinned_extents) 3954acce952bSliubo { 3955acce952bSliubo struct extent_io_tree *unpin; 3956acce952bSliubo u64 start; 3957acce952bSliubo u64 end; 3958acce952bSliubo int ret; 3959ed0eaa14SLiu Bo bool loop = true; 3960acce952bSliubo 3961acce952bSliubo unpin = pinned_extents; 3962ed0eaa14SLiu Bo again: 3963acce952bSliubo while (1) { 3964acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3965e6138876SJosef Bacik EXTENT_DIRTY, NULL); 3966acce952bSliubo if (ret) 3967acce952bSliubo break; 3968acce952bSliubo 3969acce952bSliubo /* opt_discard */ 39705378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 39715378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 39725378e607SLi Dongyang end + 1 - start, 39735378e607SLi Dongyang NULL); 3974acce952bSliubo 3975acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3976acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3977acce952bSliubo cond_resched(); 3978acce952bSliubo } 3979acce952bSliubo 3980ed0eaa14SLiu Bo if (loop) { 3981ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 3982ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 3983ed0eaa14SLiu Bo else 3984ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 3985ed0eaa14SLiu Bo loop = false; 3986ed0eaa14SLiu Bo goto again; 3987ed0eaa14SLiu Bo } 3988ed0eaa14SLiu Bo 3989acce952bSliubo return 0; 3990acce952bSliubo } 3991acce952bSliubo 399249b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 399349b25e05SJeff Mahoney struct btrfs_root *root) 399449b25e05SJeff Mahoney { 3995724e2315SJosef Bacik btrfs_destroy_ordered_operations(cur_trans, root); 3996724e2315SJosef Bacik 399749b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 399849b25e05SJeff Mahoney 39994a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 400049b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 400149b25e05SJeff Mahoney 40024a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 400349b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 400449b25e05SJeff Mahoney 400567cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 400667cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 400767cde344SMiao Xie 400849b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 400949b25e05SJeff Mahoney EXTENT_DIRTY); 40106e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 40116e841e32SLiu Bo root->fs_info->pinned_extents); 401249b25e05SJeff Mahoney 40134a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 40144a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 40154a9d8bdeSMiao Xie 401649b25e05SJeff Mahoney /* 401749b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 401849b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 401949b25e05SJeff Mahoney */ 402049b25e05SJeff Mahoney } 402149b25e05SJeff Mahoney 402248a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root) 4023acce952bSliubo { 4024acce952bSliubo struct btrfs_transaction *t; 4025acce952bSliubo 4026acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 4027acce952bSliubo 4028a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4029724e2315SJosef Bacik while (!list_empty(&root->fs_info->trans_list)) { 4030724e2315SJosef Bacik t = list_first_entry(&root->fs_info->trans_list, 4031724e2315SJosef Bacik struct btrfs_transaction, list); 4032724e2315SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_START) { 4033724e2315SJosef Bacik atomic_inc(&t->use_count); 4034724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4035724e2315SJosef Bacik btrfs_wait_for_commit(root, t->transid); 4036724e2315SJosef Bacik btrfs_put_transaction(t); 4037724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4038724e2315SJosef Bacik continue; 4039724e2315SJosef Bacik } 4040724e2315SJosef Bacik if (t == root->fs_info->running_transaction) { 4041724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING; 4042724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4043724e2315SJosef Bacik /* 4044724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans 4045724e2315SJosef Bacik * handle open currently for this transaction. 4046724e2315SJosef Bacik */ 4047724e2315SJosef Bacik wait_event(t->writer_wait, 4048724e2315SJosef Bacik atomic_read(&t->num_writers) == 0); 4049724e2315SJosef Bacik } else { 4050724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4051724e2315SJosef Bacik } 4052724e2315SJosef Bacik btrfs_cleanup_one_transaction(t, root); 4053724e2315SJosef Bacik 4054724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4055724e2315SJosef Bacik if (t == root->fs_info->running_transaction) 4056ac673879SMiao Xie root->fs_info->running_transaction = NULL; 4057724e2315SJosef Bacik list_del_init(&t->list); 4058a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4059a4abeea4SJosef Bacik 4060724e2315SJosef Bacik btrfs_put_transaction(t); 4061724e2315SJosef Bacik trace_btrfs_transaction_commit(root); 4062724e2315SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4063724e2315SJosef Bacik } 4064724e2315SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4065199c2a9cSMiao Xie btrfs_destroy_all_ordered_extents(root->fs_info); 406667cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 406767cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 4068724e2315SJosef Bacik btrfs_destroy_pinned_extent(root, root->fs_info->pinned_extents); 4069eb73c1b7SMiao Xie btrfs_destroy_all_delalloc_inodes(root->fs_info); 4070acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 4071acce952bSliubo 4072acce952bSliubo return 0; 4073acce952bSliubo } 4074acce952bSliubo 4075d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 4076ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 40774bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 40780b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4079239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4080239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 40810da5468fSChris Mason }; 4082