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> 29163e783eSDavid Woodhouse #include <linux/crc32c.h> 305a0e3ad6STejun Heo #include <linux/slab.h> 31784b4e29SChris Mason #include <linux/migrate.h> 327a36ddecSDavid Sterba #include <linux/ratelimit.h> 336463fe58SStefan Behrens #include <linux/uuid.h> 34803b2f54SStefan Behrens #include <linux/semaphore.h> 357e75bf3fSDavid Sterba #include <asm/unaligned.h> 364b4e25f2SChris Mason #include "compat.h" 37eb60ceacSChris Mason #include "ctree.h" 38eb60ceacSChris Mason #include "disk-io.h" 39e089f05cSChris Mason #include "transaction.h" 400f7d52f4SChris Mason #include "btrfs_inode.h" 410b86a832SChris Mason #include "volumes.h" 42db94535dSChris Mason #include "print-tree.h" 438b712842SChris Mason #include "async-thread.h" 44925baeddSChris Mason #include "locking.h" 45e02119d5SChris Mason #include "tree-log.h" 46fa9c0d79SChris Mason #include "free-space-cache.h" 47581bb050SLi Zefan #include "inode-map.h" 4821adbd5cSStefan Behrens #include "check-integrity.h" 49606686eeSJosef Bacik #include "rcu-string.h" 508dabb742SStefan Behrens #include "dev-replace.h" 5153b381b3SDavid Woodhouse #include "raid56.h" 52eb60ceacSChris Mason 53de0022b9SJosef Bacik #ifdef CONFIG_X86 54de0022b9SJosef Bacik #include <asm/cpufeature.h> 55de0022b9SJosef Bacik #endif 56de0022b9SJosef Bacik 57d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 588b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 594df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 60fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 61acce952bSliubo int read_only); 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); 67aec8030aSMiao Xie static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t); 68143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 69acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 70acce952bSliubo struct extent_io_tree *dirty_pages, 71acce952bSliubo int mark); 72acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 73acce952bSliubo struct extent_io_tree *pinned_extents); 7448a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root); 7548a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root); 76ce9adaa5SChris Mason 77d352ac68SChris Mason /* 78d352ac68SChris Mason * end_io_wq structs are used to do processing in task context when an IO is 79d352ac68SChris Mason * complete. This is used during reads to verify checksums, and it is used 80d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 81d352ac68SChris Mason */ 82ce9adaa5SChris Mason struct end_io_wq { 83ce9adaa5SChris Mason struct bio *bio; 84ce9adaa5SChris Mason bio_end_io_t *end_io; 85ce9adaa5SChris Mason void *private; 86ce9adaa5SChris Mason struct btrfs_fs_info *info; 87ce9adaa5SChris Mason int error; 8822c59948SChris Mason int metadata; 89ce9adaa5SChris Mason struct list_head list; 908b712842SChris Mason struct btrfs_work work; 91ce9adaa5SChris Mason }; 920da5468fSChris Mason 93d352ac68SChris Mason /* 94d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 95d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 96d352ac68SChris Mason * just before they are sent down the IO stack. 97d352ac68SChris Mason */ 9844b8bd7eSChris Mason struct async_submit_bio { 9944b8bd7eSChris Mason struct inode *inode; 10044b8bd7eSChris Mason struct bio *bio; 10144b8bd7eSChris Mason struct list_head list; 1024a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1034a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 10444b8bd7eSChris Mason int rw; 10544b8bd7eSChris Mason int mirror_num; 106c8b97818SChris Mason unsigned long bio_flags; 107eaf25d93SChris Mason /* 108eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 109eaf25d93SChris Mason * can't tell us where in the file the bio should go 110eaf25d93SChris Mason */ 111eaf25d93SChris Mason u64 bio_offset; 1128b712842SChris Mason struct btrfs_work work; 11379787eaaSJeff Mahoney int error; 11444b8bd7eSChris Mason }; 11544b8bd7eSChris Mason 11685d4e461SChris Mason /* 11785d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 11885d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 11985d4e461SChris Mason * the level the eb occupies in the tree. 1204008c04aSChris Mason * 12185d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12285d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 12385d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 12485d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 12585d4e461SChris Mason * have separate keysets. 1264008c04aSChris Mason * 12785d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 12885d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 12985d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1304008c04aSChris Mason * 13185d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13285d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 13385d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 13485d4e461SChris Mason * 13585d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 13685d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 13785d4e461SChris Mason * needs update as well. 1384008c04aSChris Mason */ 1394008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1404008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1414008c04aSChris Mason # error 1424008c04aSChris Mason # endif 14385d4e461SChris Mason 14485d4e461SChris Mason static struct btrfs_lockdep_keyset { 14585d4e461SChris Mason u64 id; /* root objectid */ 14685d4e461SChris Mason const char *name_stem; /* lock name stem */ 14785d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 14885d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 14985d4e461SChris Mason } btrfs_lockdep_keysets[] = { 15085d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 15185d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 15285d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 15385d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 15485d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 15585d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 15660b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 15785d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 15885d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 15985d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 16013fd8da9SDavid Sterba { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" }, 16185d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1624008c04aSChris Mason }; 16385d4e461SChris Mason 16485d4e461SChris Mason void __init btrfs_init_lockdep(void) 16585d4e461SChris Mason { 16685d4e461SChris Mason int i, j; 16785d4e461SChris Mason 16885d4e461SChris Mason /* initialize lockdep class names */ 16985d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 17085d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 17185d4e461SChris Mason 17285d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 17385d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 17485d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 17585d4e461SChris Mason } 17685d4e461SChris Mason } 17785d4e461SChris Mason 17885d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 17985d4e461SChris Mason int level) 18085d4e461SChris Mason { 18185d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 18285d4e461SChris Mason 18385d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 18485d4e461SChris Mason 18585d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 18685d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 18785d4e461SChris Mason if (ks->id == objectid) 18885d4e461SChris Mason break; 18985d4e461SChris Mason 19085d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 19185d4e461SChris Mason &ks->keys[level], ks->names[level]); 19285d4e461SChris Mason } 19385d4e461SChris Mason 1944008c04aSChris Mason #endif 1954008c04aSChris Mason 196d352ac68SChris Mason /* 197d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 198d352ac68SChris Mason * that covers the entire device 199d352ac68SChris Mason */ 200b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 201306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2025f39d397SChris Mason int create) 2035f39d397SChris Mason { 2045f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2055f39d397SChris Mason struct extent_map *em; 2065f39d397SChris Mason int ret; 2075f39d397SChris Mason 208890871beSChris Mason read_lock(&em_tree->lock); 209d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 210a061fc8dSChris Mason if (em) { 211a061fc8dSChris Mason em->bdev = 212a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 213890871beSChris Mason read_unlock(&em_tree->lock); 2145f39d397SChris Mason goto out; 215a061fc8dSChris Mason } 216890871beSChris Mason read_unlock(&em_tree->lock); 2177b13b7b1SChris Mason 218172ddd60SDavid Sterba em = alloc_extent_map(); 2195f39d397SChris Mason if (!em) { 2205f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2215f39d397SChris Mason goto out; 2225f39d397SChris Mason } 2235f39d397SChris Mason em->start = 0; 2240afbaf8cSChris Mason em->len = (u64)-1; 225c8b97818SChris Mason em->block_len = (u64)-1; 2265f39d397SChris Mason em->block_start = 0; 227a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 228d1310b2eSChris Mason 229890871beSChris Mason write_lock(&em_tree->lock); 23009a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2315f39d397SChris Mason if (ret == -EEXIST) { 2325f39d397SChris Mason free_extent_map(em); 2337b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 234b4f359abSTsutomu Itoh if (!em) 2350433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2365f39d397SChris Mason } else if (ret) { 2377b13b7b1SChris Mason free_extent_map(em); 2380433f20dSTsutomu Itoh em = ERR_PTR(ret); 2395f39d397SChris Mason } 240890871beSChris Mason write_unlock(&em_tree->lock); 2417b13b7b1SChris Mason 2425f39d397SChris Mason out: 2435f39d397SChris Mason return em; 2445f39d397SChris Mason } 2455f39d397SChris Mason 246b0496686SLiu Bo u32 btrfs_csum_data(char *data, u32 seed, size_t len) 24719c00ddcSChris Mason { 248163e783eSDavid Woodhouse return crc32c(seed, data, len); 24919c00ddcSChris Mason } 25019c00ddcSChris Mason 25119c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 25219c00ddcSChris Mason { 2537e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 25419c00ddcSChris Mason } 25519c00ddcSChris Mason 256d352ac68SChris Mason /* 257d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 258d352ac68SChris Mason * into the csum field of the block. 259d352ac68SChris Mason */ 26019c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 26119c00ddcSChris Mason int verify) 26219c00ddcSChris Mason { 2636c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 264607d432dSJosef Bacik char *result = NULL; 26519c00ddcSChris Mason unsigned long len; 26619c00ddcSChris Mason unsigned long cur_len; 26719c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26819c00ddcSChris Mason char *kaddr; 26919c00ddcSChris Mason unsigned long map_start; 27019c00ddcSChris Mason unsigned long map_len; 27119c00ddcSChris Mason int err; 27219c00ddcSChris Mason u32 crc = ~(u32)0; 273607d432dSJosef Bacik unsigned long inline_result; 27419c00ddcSChris Mason 27519c00ddcSChris Mason len = buf->len - offset; 27619c00ddcSChris Mason while (len > 0) { 27719c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 278a6591715SChris Mason &kaddr, &map_start, &map_len); 279d397712bSChris Mason if (err) 28019c00ddcSChris Mason return 1; 28119c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 282b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 28319c00ddcSChris Mason crc, cur_len); 28419c00ddcSChris Mason len -= cur_len; 28519c00ddcSChris Mason offset += cur_len; 28619c00ddcSChris Mason } 287607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 288607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 289607d432dSJosef Bacik if (!result) 290607d432dSJosef Bacik return 1; 291607d432dSJosef Bacik } else { 292607d432dSJosef Bacik result = (char *)&inline_result; 293607d432dSJosef Bacik } 294607d432dSJosef Bacik 29519c00ddcSChris Mason btrfs_csum_final(crc, result); 29619c00ddcSChris Mason 29719c00ddcSChris Mason if (verify) { 298607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 299e4204dedSChris Mason u32 val; 300e4204dedSChris Mason u32 found = 0; 301607d432dSJosef Bacik memcpy(&found, result, csum_size); 302e4204dedSChris Mason 303607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 3047a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs: %s checksum verify " 305193f284dSChris Mason "failed on %llu wanted %X found %X " 306193f284dSChris Mason "level %d\n", 307c1c9ff7cSGeert Uytterhoeven root->fs_info->sb->s_id, buf->start, 308c1c9ff7cSGeert Uytterhoeven val, found, btrfs_header_level(buf)); 309607d432dSJosef Bacik if (result != (char *)&inline_result) 310607d432dSJosef Bacik kfree(result); 31119c00ddcSChris Mason return 1; 31219c00ddcSChris Mason } 31319c00ddcSChris Mason } else { 314607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 31519c00ddcSChris Mason } 316607d432dSJosef Bacik if (result != (char *)&inline_result) 317607d432dSJosef Bacik kfree(result); 31819c00ddcSChris Mason return 0; 31919c00ddcSChris Mason } 32019c00ddcSChris Mason 321d352ac68SChris Mason /* 322d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 323d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 324d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 325d352ac68SChris Mason * in the wrong place. 326d352ac68SChris Mason */ 3271259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 328b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 329b9fab919SChris Mason int atomic) 3301259ab75SChris Mason { 3312ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3321259ab75SChris Mason int ret; 3331259ab75SChris Mason 3341259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3351259ab75SChris Mason return 0; 3361259ab75SChris Mason 337b9fab919SChris Mason if (atomic) 338b9fab919SChris Mason return -EAGAIN; 339b9fab919SChris Mason 3402ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 341d0082371SJeff Mahoney 0, &cached_state); 3420b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3431259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3441259ab75SChris Mason ret = 0; 3451259ab75SChris Mason goto out; 3461259ab75SChris Mason } 3477a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 348193f284dSChris Mason "found %llu\n", 349c1c9ff7cSGeert Uytterhoeven eb->start, parent_transid, btrfs_header_generation(eb)); 3501259ab75SChris Mason ret = 1; 3510b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 35233958dc6SChris Mason out: 3532ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3542ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3551259ab75SChris Mason return ret; 3561259ab75SChris Mason } 3571259ab75SChris Mason 358d352ac68SChris Mason /* 3591104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3601104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3611104a885SDavid Sterba */ 3621104a885SDavid Sterba static int btrfs_check_super_csum(char *raw_disk_sb) 3631104a885SDavid Sterba { 3641104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3651104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 3661104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 3671104a885SDavid Sterba int ret = 0; 3681104a885SDavid Sterba 3691104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 3701104a885SDavid Sterba u32 crc = ~(u32)0; 3711104a885SDavid Sterba const int csum_size = sizeof(crc); 3721104a885SDavid Sterba char result[csum_size]; 3731104a885SDavid Sterba 3741104a885SDavid Sterba /* 3751104a885SDavid Sterba * The super_block structure does not span the whole 3761104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 3771104a885SDavid Sterba * is filled with zeros and is included in the checkum. 3781104a885SDavid Sterba */ 3791104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 3801104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3811104a885SDavid Sterba btrfs_csum_final(crc, result); 3821104a885SDavid Sterba 3831104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 3841104a885SDavid Sterba ret = 1; 385667e7d94SChris Mason 386667e7d94SChris Mason if (ret && btrfs_super_generation(disk_sb) < 10) { 387667e7d94SChris Mason printk(KERN_WARNING "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)) { 3931104a885SDavid Sterba 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 { 469d1310b2eSChris Mason struct extent_io_tree *tree; 4704eee4fa4SMiao Xie u64 start = page_offset(page); 47119c00ddcSChris Mason u64 found_start; 47219c00ddcSChris Mason struct extent_buffer *eb; 473f188591eSChris Mason 474d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 47519c00ddcSChris Mason 4764f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4774f2de97aSJosef Bacik if (page != eb->pages[0]) 4784f2de97aSJosef Bacik return 0; 47919c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 48019c00ddcSChris Mason if (found_start != start) { 48155c69072SChris Mason WARN_ON(1); 4824f2de97aSJosef Bacik return 0; 48355c69072SChris Mason } 48455c69072SChris Mason if (!PageUptodate(page)) { 48555c69072SChris Mason WARN_ON(1); 4864f2de97aSJosef Bacik return 0; 48719c00ddcSChris Mason } 48819c00ddcSChris Mason csum_tree_block(root, eb, 0); 48919c00ddcSChris Mason return 0; 49019c00ddcSChris Mason } 49119c00ddcSChris Mason 4922b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 4932b82032cSYan Zheng struct extent_buffer *eb) 4942b82032cSYan Zheng { 4952b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 4962b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 4972b82032cSYan Zheng int ret = 1; 4982b82032cSYan Zheng 499fba6aa75SGeert Uytterhoeven read_extent_buffer(eb, fsid, btrfs_header_fsid(eb), BTRFS_FSID_SIZE); 5002b82032cSYan Zheng while (fs_devices) { 5012b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5022b82032cSYan Zheng ret = 0; 5032b82032cSYan Zheng break; 5042b82032cSYan Zheng } 5052b82032cSYan Zheng fs_devices = fs_devices->seed; 5062b82032cSYan Zheng } 5072b82032cSYan Zheng return ret; 5082b82032cSYan Zheng } 5092b82032cSYan Zheng 510a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 511a826d6dcSJosef Bacik printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \ 512a826d6dcSJosef Bacik "root=%llu, slot=%d\n", reason, \ 513c1c9ff7cSGeert Uytterhoeven btrfs_header_bytenr(eb), root->objectid, slot) 514a826d6dcSJosef Bacik 515a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 516a826d6dcSJosef Bacik struct extent_buffer *leaf) 517a826d6dcSJosef Bacik { 518a826d6dcSJosef Bacik struct btrfs_key key; 519a826d6dcSJosef Bacik struct btrfs_key leaf_key; 520a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 521a826d6dcSJosef Bacik int slot; 522a826d6dcSJosef Bacik 523a826d6dcSJosef Bacik if (nritems == 0) 524a826d6dcSJosef Bacik return 0; 525a826d6dcSJosef Bacik 526a826d6dcSJosef Bacik /* Check the 0 item */ 527a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 528a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 529a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 530a826d6dcSJosef Bacik return -EIO; 531a826d6dcSJosef Bacik } 532a826d6dcSJosef Bacik 533a826d6dcSJosef Bacik /* 534a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 535a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 536a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 537a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 538a826d6dcSJosef Bacik * offset is correct. 539a826d6dcSJosef Bacik */ 540a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 541a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 542a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 543a826d6dcSJosef Bacik 544a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 545a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 546a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 547a826d6dcSJosef Bacik return -EIO; 548a826d6dcSJosef Bacik } 549a826d6dcSJosef Bacik 550a826d6dcSJosef Bacik /* 551a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 552a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 553a826d6dcSJosef Bacik * front. 554a826d6dcSJosef Bacik */ 555a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 556a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 557a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 558a826d6dcSJosef Bacik return -EIO; 559a826d6dcSJosef Bacik } 560a826d6dcSJosef Bacik 561a826d6dcSJosef Bacik /* 562a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 563a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 564a826d6dcSJosef Bacik * all point outside of the leaf. 565a826d6dcSJosef Bacik */ 566a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 567a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 568a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 569a826d6dcSJosef Bacik return -EIO; 570a826d6dcSJosef Bacik } 571a826d6dcSJosef Bacik } 572a826d6dcSJosef Bacik 573a826d6dcSJosef Bacik return 0; 574a826d6dcSJosef Bacik } 575a826d6dcSJosef Bacik 576facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 577facc8a22SMiao Xie u64 phy_offset, struct page *page, 578facc8a22SMiao Xie u64 start, u64 end, int mirror) 579ce9adaa5SChris Mason { 580ce9adaa5SChris Mason struct extent_io_tree *tree; 581ce9adaa5SChris Mason u64 found_start; 582ce9adaa5SChris Mason int found_level; 583ce9adaa5SChris Mason struct extent_buffer *eb; 584ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 585f188591eSChris Mason int ret = 0; 586727011e0SChris Mason int reads_done; 587ce9adaa5SChris Mason 588ce9adaa5SChris Mason if (!page->private) 589ce9adaa5SChris Mason goto out; 590d397712bSChris Mason 591727011e0SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 5924f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 593d397712bSChris Mason 5940b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5950b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5960b32f4bbSJosef Bacik */ 5970b32f4bbSJosef Bacik extent_buffer_get(eb); 5980b32f4bbSJosef Bacik 5990b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 600727011e0SChris Mason if (!reads_done) 601727011e0SChris Mason goto err; 602f188591eSChris Mason 6035cf1ab56SJosef Bacik eb->read_mirror = mirror; 604ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 605ea466794SJosef Bacik ret = -EIO; 606ea466794SJosef Bacik goto err; 607ea466794SJosef Bacik } 608ea466794SJosef Bacik 609ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 610727011e0SChris Mason if (found_start != eb->start) { 6117a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 612193f284dSChris Mason "%llu %llu\n", 613c1c9ff7cSGeert Uytterhoeven found_start, eb->start); 614f188591eSChris Mason ret = -EIO; 615ce9adaa5SChris Mason goto err; 616ce9adaa5SChris Mason } 6172b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 6187a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 619c1c9ff7cSGeert Uytterhoeven eb->start); 6201259ab75SChris Mason ret = -EIO; 6211259ab75SChris Mason goto err; 6221259ab75SChris Mason } 623ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6241c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 6251c24c3ceSJosef Bacik btrfs_info(root->fs_info, "bad tree block level %d\n", 6261c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6271c24c3ceSJosef Bacik ret = -EIO; 6281c24c3ceSJosef Bacik goto err; 6291c24c3ceSJosef Bacik } 630ce9adaa5SChris Mason 63185d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 63285d4e461SChris Mason eb, found_level); 6334008c04aSChris Mason 634ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 635a826d6dcSJosef Bacik if (ret) { 636f188591eSChris Mason ret = -EIO; 637a826d6dcSJosef Bacik goto err; 638a826d6dcSJosef Bacik } 639a826d6dcSJosef Bacik 640a826d6dcSJosef Bacik /* 641a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 642a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 643a826d6dcSJosef Bacik * return -EIO. 644a826d6dcSJosef Bacik */ 645a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 646a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 647a826d6dcSJosef Bacik ret = -EIO; 648a826d6dcSJosef Bacik } 649ce9adaa5SChris Mason 6500b32f4bbSJosef Bacik if (!ret) 6510b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 652ce9adaa5SChris Mason err: 65379fb65a1SJosef Bacik if (reads_done && 65479fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6554bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6564bb31e92SArne Jansen 65753b381b3SDavid Woodhouse if (ret) { 65853b381b3SDavid Woodhouse /* 65953b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 66053b381b3SDavid Woodhouse * again, we have to make sure it has something 66153b381b3SDavid Woodhouse * to decrement 66253b381b3SDavid Woodhouse */ 66353b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6640b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 66553b381b3SDavid Woodhouse } 6660b32f4bbSJosef Bacik free_extent_buffer(eb); 667ce9adaa5SChris Mason out: 668f188591eSChris Mason return ret; 669ce9adaa5SChris Mason } 670ce9adaa5SChris Mason 671ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6724bb31e92SArne Jansen { 6734bb31e92SArne Jansen struct extent_buffer *eb; 6744bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6754bb31e92SArne Jansen 6764f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 677ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6785cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 67953b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 680ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6814bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6824bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6834bb31e92SArne Jansen } 6844bb31e92SArne Jansen 685ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 686ce9adaa5SChris Mason { 687ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 688ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 689ce9adaa5SChris Mason 690ce9adaa5SChris Mason fs_info = end_io_wq->info; 691ce9adaa5SChris Mason end_io_wq->error = err; 6928b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6938b712842SChris Mason end_io_wq->work.flags = 0; 694d20f7043SChris Mason 6957b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 69653b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 697cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 698cad321adSChris Mason &end_io_wq->work); 69953b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7000cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 7010cb59c99SJosef Bacik &end_io_wq->work); 70253b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 70353b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 70453b381b3SDavid Woodhouse &end_io_wq->work); 705cad321adSChris Mason else 706e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 707e6dcd2dcSChris Mason &end_io_wq->work); 708d20f7043SChris Mason } else { 70953b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 71053b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 71153b381b3SDavid Woodhouse &end_io_wq->work); 71253b381b3SDavid Woodhouse else if (end_io_wq->metadata) 713d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 714d20f7043SChris Mason &end_io_wq->work); 715e6dcd2dcSChris Mason else 716d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 717d20f7043SChris Mason &end_io_wq->work); 718d20f7043SChris Mason } 719ce9adaa5SChris Mason } 720ce9adaa5SChris Mason 7210cb59c99SJosef Bacik /* 7220cb59c99SJosef Bacik * For the metadata arg you want 7230cb59c99SJosef Bacik * 7240cb59c99SJosef Bacik * 0 - if data 7250cb59c99SJosef Bacik * 1 - if normal metadta 7260cb59c99SJosef Bacik * 2 - if writing to the free space cache area 72753b381b3SDavid Woodhouse * 3 - raid parity work 7280cb59c99SJosef Bacik */ 72922c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 73022c59948SChris Mason int metadata) 7310b86a832SChris Mason { 732ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 733ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 734ce9adaa5SChris Mason if (!end_io_wq) 735ce9adaa5SChris Mason return -ENOMEM; 736ce9adaa5SChris Mason 737ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 738ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 73922c59948SChris Mason end_io_wq->info = info; 740ce9adaa5SChris Mason end_io_wq->error = 0; 741ce9adaa5SChris Mason end_io_wq->bio = bio; 74222c59948SChris Mason end_io_wq->metadata = metadata; 743ce9adaa5SChris Mason 744ce9adaa5SChris Mason bio->bi_private = end_io_wq; 745ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 74622c59948SChris Mason return 0; 74722c59948SChris Mason } 74822c59948SChris Mason 749b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7504854ddd0SChris Mason { 7514854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7524854ddd0SChris Mason info->workers.max_workers, 7534854ddd0SChris Mason info->fs_devices->open_devices); 7544854ddd0SChris Mason return 256 * limit; 7554854ddd0SChris Mason } 7564854ddd0SChris Mason 7574a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7584a69a410SChris Mason { 7594a69a410SChris Mason struct async_submit_bio *async; 76079787eaaSJeff Mahoney int ret; 7614a69a410SChris Mason 7624a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 76379787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 764eaf25d93SChris Mason async->mirror_num, async->bio_flags, 765eaf25d93SChris Mason async->bio_offset); 76679787eaaSJeff Mahoney if (ret) 76779787eaaSJeff Mahoney async->error = ret; 7684a69a410SChris Mason } 7694a69a410SChris Mason 7704a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7718b712842SChris Mason { 7728b712842SChris Mason struct btrfs_fs_info *fs_info; 7738b712842SChris Mason struct async_submit_bio *async; 7744854ddd0SChris Mason int limit; 7758b712842SChris Mason 7768b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7778b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7784854ddd0SChris Mason 779b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7804854ddd0SChris Mason limit = limit * 2 / 3; 7814854ddd0SChris Mason 78266657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 783b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7844854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7854854ddd0SChris Mason 78679787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 78779787eaaSJeff Mahoney if (async->error) { 78879787eaaSJeff Mahoney bio_endio(async->bio, async->error); 78979787eaaSJeff Mahoney return; 79079787eaaSJeff Mahoney } 79179787eaaSJeff Mahoney 7924a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 793eaf25d93SChris Mason async->mirror_num, async->bio_flags, 794eaf25d93SChris Mason async->bio_offset); 7954a69a410SChris Mason } 7964a69a410SChris Mason 7974a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7984a69a410SChris Mason { 7994a69a410SChris Mason struct async_submit_bio *async; 8004a69a410SChris Mason 8014a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8028b712842SChris Mason kfree(async); 8038b712842SChris Mason } 8048b712842SChris Mason 80544b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 80644b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 807c8b97818SChris Mason unsigned long bio_flags, 808eaf25d93SChris Mason u64 bio_offset, 8094a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 8104a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 81144b8bd7eSChris Mason { 81244b8bd7eSChris Mason struct async_submit_bio *async; 81344b8bd7eSChris Mason 81444b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81544b8bd7eSChris Mason if (!async) 81644b8bd7eSChris Mason return -ENOMEM; 81744b8bd7eSChris Mason 81844b8bd7eSChris Mason async->inode = inode; 81944b8bd7eSChris Mason async->rw = rw; 82044b8bd7eSChris Mason async->bio = bio; 82144b8bd7eSChris Mason async->mirror_num = mirror_num; 8224a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8234a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8244a69a410SChris Mason 8254a69a410SChris Mason async->work.func = run_one_async_start; 8264a69a410SChris Mason async->work.ordered_func = run_one_async_done; 8274a69a410SChris Mason async->work.ordered_free = run_one_async_free; 8284a69a410SChris Mason 8298b712842SChris Mason async->work.flags = 0; 830c8b97818SChris Mason async->bio_flags = bio_flags; 831eaf25d93SChris Mason async->bio_offset = bio_offset; 8328c8bee1dSChris Mason 83379787eaaSJeff Mahoney async->error = 0; 83479787eaaSJeff Mahoney 835cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 836d313d7a3SChris Mason 8377b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 838d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 839d313d7a3SChris Mason 8408b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 8419473f16cSChris Mason 842771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 843771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 844771ed689SChris Mason wait_event(fs_info->async_submit_wait, 845771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 846771ed689SChris Mason } 847771ed689SChris Mason 84844b8bd7eSChris Mason return 0; 84944b8bd7eSChris Mason } 85044b8bd7eSChris Mason 851ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 852ce3ed71aSChris Mason { 853ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 854ce3ed71aSChris Mason int bio_index = 0; 855ce3ed71aSChris Mason struct btrfs_root *root; 85679787eaaSJeff Mahoney int ret = 0; 857ce3ed71aSChris Mason 858ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 859ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 860ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 86179787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 86279787eaaSJeff Mahoney if (ret) 86379787eaaSJeff Mahoney break; 864ce3ed71aSChris Mason bio_index++; 865ce3ed71aSChris Mason bvec++; 866ce3ed71aSChris Mason } 86779787eaaSJeff Mahoney return ret; 868ce3ed71aSChris Mason } 869ce3ed71aSChris Mason 8704a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8714a69a410SChris Mason struct bio *bio, int mirror_num, 872eaf25d93SChris Mason unsigned long bio_flags, 873eaf25d93SChris Mason u64 bio_offset) 87422c59948SChris Mason { 8758b712842SChris Mason /* 8768b712842SChris Mason * when we're called for a write, we're already in the async 8775443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8788b712842SChris Mason */ 87979787eaaSJeff Mahoney return btree_csum_one_bio(bio); 88022c59948SChris Mason } 88122c59948SChris Mason 8824a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 883eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 884eaf25d93SChris Mason u64 bio_offset) 8854a69a410SChris Mason { 88661891923SStefan Behrens int ret; 88761891923SStefan Behrens 8888b712842SChris Mason /* 8894a69a410SChris Mason * when we're called for a write, we're already in the async 8904a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8918b712842SChris Mason */ 89261891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 89361891923SStefan Behrens if (ret) 89461891923SStefan Behrens bio_endio(bio, ret); 89561891923SStefan Behrens return ret; 8960b86a832SChris Mason } 8970b86a832SChris Mason 898de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 899de0022b9SJosef Bacik { 900de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 901de0022b9SJosef Bacik return 0; 902de0022b9SJosef Bacik #ifdef CONFIG_X86 903de0022b9SJosef Bacik if (cpu_has_xmm4_2) 904de0022b9SJosef Bacik return 0; 905de0022b9SJosef Bacik #endif 906de0022b9SJosef Bacik return 1; 907de0022b9SJosef Bacik } 908de0022b9SJosef Bacik 90944b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 910eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 911eaf25d93SChris Mason u64 bio_offset) 91244b8bd7eSChris Mason { 913de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 9144a69a410SChris Mason int ret; 915cad321adSChris Mason 9167b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 917cad321adSChris Mason /* 918cad321adSChris Mason * called for a read, do the setup so that checksum validation 919cad321adSChris Mason * can happen in the async kernel threads 920cad321adSChris Mason */ 921f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 922f3f266abSChris Mason bio, 1); 9231d4284bdSChris Mason if (ret) 92461891923SStefan Behrens goto out_w_error; 92561891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 9266f3577bdSChris Mason mirror_num, 0); 927de0022b9SJosef Bacik } else if (!async) { 928de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 929de0022b9SJosef Bacik if (ret) 93061891923SStefan Behrens goto out_w_error; 93161891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 932de0022b9SJosef Bacik mirror_num, 0); 93361891923SStefan Behrens } else { 934cad321adSChris Mason /* 93561891923SStefan Behrens * kthread helpers are used to submit writes so that 93661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 937cad321adSChris Mason */ 93861891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 939c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 940eaf25d93SChris Mason bio_offset, 9414a69a410SChris Mason __btree_submit_bio_start, 9424a69a410SChris Mason __btree_submit_bio_done); 94344b8bd7eSChris Mason } 94444b8bd7eSChris Mason 94561891923SStefan Behrens if (ret) { 94661891923SStefan Behrens out_w_error: 94761891923SStefan Behrens bio_endio(bio, ret); 94861891923SStefan Behrens } 94961891923SStefan Behrens return ret; 95061891923SStefan Behrens } 95161891923SStefan Behrens 9523dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 953784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 954a6bc32b8SMel Gorman struct page *newpage, struct page *page, 955a6bc32b8SMel Gorman enum migrate_mode mode) 956784b4e29SChris Mason { 957784b4e29SChris Mason /* 958784b4e29SChris Mason * we can't safely write a btree page from here, 959784b4e29SChris Mason * we haven't done the locking hook 960784b4e29SChris Mason */ 961784b4e29SChris Mason if (PageDirty(page)) 962784b4e29SChris Mason return -EAGAIN; 963784b4e29SChris Mason /* 964784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 965784b4e29SChris Mason * We must have no buffers or drop them. 966784b4e29SChris Mason */ 967784b4e29SChris Mason if (page_has_private(page) && 968784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 969784b4e29SChris Mason return -EAGAIN; 970a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 971784b4e29SChris Mason } 9723dd1462eSJan Beulich #endif 973784b4e29SChris Mason 9740da5468fSChris Mason 9750da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9760da5468fSChris Mason struct writeback_control *wbc) 9770da5468fSChris Mason { 978d1310b2eSChris Mason struct extent_io_tree *tree; 979e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 980e2d84521SMiao Xie int ret; 981e2d84521SMiao Xie 982d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 983d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 984448d640bSChris Mason 985448d640bSChris Mason if (wbc->for_kupdate) 986448d640bSChris Mason return 0; 987448d640bSChris Mason 988e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 989b9473439SChris Mason /* this is a bit racy, but that's ok */ 990e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 991e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 992e2d84521SMiao Xie if (ret < 0) 993793955bcSChris Mason return 0; 994793955bcSChris Mason } 9950b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9960da5468fSChris Mason } 9970da5468fSChris Mason 998b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9995f39d397SChris Mason { 1000d1310b2eSChris Mason struct extent_io_tree *tree; 1001d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10028ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 10035f39d397SChris Mason } 10045f39d397SChris Mason 100570dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10065f39d397SChris Mason { 100798509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 100898509cfcSChris Mason return 0; 10090c4e538bSDavid Sterba 1010f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1011d98237b3SChris Mason } 1012d98237b3SChris Mason 1013d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1014d47992f8SLukas Czerner unsigned int length) 1015d98237b3SChris Mason { 1016d1310b2eSChris Mason struct extent_io_tree *tree; 1017d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10185f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10195f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10209ad6b7bcSChris Mason if (PagePrivate(page)) { 1021d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 1022d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 10239ad6b7bcSChris Mason ClearPagePrivate(page); 10249ad6b7bcSChris Mason set_page_private(page, 0); 10259ad6b7bcSChris Mason page_cache_release(page); 10269ad6b7bcSChris Mason } 1027d98237b3SChris Mason } 1028d98237b3SChris Mason 10290b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10300b32f4bbSJosef Bacik { 1031bb146eb2SJosef Bacik #ifdef DEBUG 10320b32f4bbSJosef Bacik struct extent_buffer *eb; 10330b32f4bbSJosef Bacik 10340b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10350b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10360b32f4bbSJosef Bacik BUG_ON(!eb); 10370b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10380b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10390b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1040bb146eb2SJosef Bacik #endif 10410b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10420b32f4bbSJosef Bacik } 10430b32f4bbSJosef Bacik 10447f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1045d98237b3SChris Mason .readpage = btree_readpage, 10460da5468fSChris Mason .writepages = btree_writepages, 10475f39d397SChris Mason .releasepage = btree_releasepage, 10485f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10495a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1050784b4e29SChris Mason .migratepage = btree_migratepage, 10515a92bc88SChris Mason #endif 10520b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1053d98237b3SChris Mason }; 1054123abc88SChris Mason 1055ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1056ca7a79adSChris Mason u64 parent_transid) 1057090d1875SChris Mason { 10585f39d397SChris Mason struct extent_buffer *buf = NULL; 10595f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1060de428b63SChris Mason int ret = 0; 1061090d1875SChris Mason 1062db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10635f39d397SChris Mason if (!buf) 1064090d1875SChris Mason return 0; 1065d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1066bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10675f39d397SChris Mason free_extent_buffer(buf); 1068de428b63SChris Mason return ret; 1069090d1875SChris Mason } 1070090d1875SChris Mason 1071ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1072ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1073ab0fff03SArne Jansen { 1074ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1075ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1076ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1077ab0fff03SArne Jansen int ret; 1078ab0fff03SArne Jansen 1079ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1080ab0fff03SArne Jansen if (!buf) 1081ab0fff03SArne Jansen return 0; 1082ab0fff03SArne Jansen 1083ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1084ab0fff03SArne Jansen 1085ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1086ab0fff03SArne Jansen btree_get_extent, mirror_num); 1087ab0fff03SArne Jansen if (ret) { 1088ab0fff03SArne Jansen free_extent_buffer(buf); 1089ab0fff03SArne Jansen return ret; 1090ab0fff03SArne Jansen } 1091ab0fff03SArne Jansen 1092ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1093ab0fff03SArne Jansen free_extent_buffer(buf); 1094ab0fff03SArne Jansen return -EIO; 10950b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1096ab0fff03SArne Jansen *eb = buf; 1097ab0fff03SArne Jansen } else { 1098ab0fff03SArne Jansen free_extent_buffer(buf); 1099ab0fff03SArne Jansen } 1100ab0fff03SArne Jansen return 0; 1101ab0fff03SArne Jansen } 1102ab0fff03SArne Jansen 11030999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 11040999df54SChris Mason u64 bytenr, u32 blocksize) 11050999df54SChris Mason { 11060999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11070999df54SChris Mason struct extent_buffer *eb; 11080999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1109f09d1f60SDavid Sterba bytenr, blocksize); 11100999df54SChris Mason return eb; 11110999df54SChris Mason } 11120999df54SChris Mason 11130999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 11140999df54SChris Mason u64 bytenr, u32 blocksize) 11150999df54SChris Mason { 11160999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11170999df54SChris Mason struct extent_buffer *eb; 11180999df54SChris Mason 11190999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1120727011e0SChris Mason bytenr, blocksize); 11210999df54SChris Mason return eb; 11220999df54SChris Mason } 11230999df54SChris Mason 11240999df54SChris Mason 1125e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1126e02119d5SChris Mason { 1127727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11288aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1129e02119d5SChris Mason } 1130e02119d5SChris Mason 1131e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1132e02119d5SChris Mason { 1133727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11348aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1135e02119d5SChris Mason } 1136e02119d5SChris Mason 1137db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1138ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1139e20d96d6SChris Mason { 11405f39d397SChris Mason struct extent_buffer *buf = NULL; 114119c00ddcSChris Mason int ret; 114219c00ddcSChris Mason 1143db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11445f39d397SChris Mason if (!buf) 1145d98237b3SChris Mason return NULL; 1146e4204dedSChris Mason 1147ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11480f0fe8f7SFilipe David Borba Manana if (ret) { 11490f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 11500f0fe8f7SFilipe David Borba Manana return NULL; 11510f0fe8f7SFilipe David Borba Manana } 11525f39d397SChris Mason return buf; 1153ce9adaa5SChris Mason 1154eb60ceacSChris Mason } 1155eb60ceacSChris Mason 1156d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11575f39d397SChris Mason struct extent_buffer *buf) 1158ed2ff2cbSChris Mason { 1159e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1160e2d84521SMiao Xie 116155c69072SChris Mason if (btrfs_header_generation(buf) == 1162e2d84521SMiao Xie fs_info->running_transaction->transid) { 1163b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1164b4ce94deSChris Mason 1165b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1166e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1167e2d84521SMiao Xie -buf->len, 1168e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1169b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1170b9473439SChris Mason btrfs_set_lock_blocking(buf); 11710b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1172925baeddSChris Mason } 11735f39d397SChris Mason } 1174ed7b63ebSJosef Bacik } 11755f39d397SChris Mason 1176143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 117787ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11789f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1179e20d96d6SChris Mason u64 objectid) 1180d97e63b6SChris Mason { 1181cfaa7295SChris Mason root->node = NULL; 1182a28ec197SChris Mason root->commit_root = NULL; 1183db94535dSChris Mason root->sectorsize = sectorsize; 1184db94535dSChris Mason root->nodesize = nodesize; 1185db94535dSChris Mason root->leafsize = leafsize; 118687ee04ebSChris Mason root->stripesize = stripesize; 1187123abc88SChris Mason root->ref_cows = 0; 11880b86a832SChris Mason root->track_dirty = 0; 1189c71bf099SYan, Zheng root->in_radix = 0; 1190d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1191d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11920b86a832SChris Mason 11930f7d52f4SChris Mason root->objectid = objectid; 11940f7d52f4SChris Mason root->last_trans = 0; 119513a8a7c8SYan, Zheng root->highest_objectid = 0; 1196eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1197199c2a9cSMiao Xie root->nr_ordered_extents = 0; 119858176a96SJosef Bacik root->name = NULL; 11996bef4d31SEric Paris root->inode_tree = RB_ROOT; 120016cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1201f0486c68SYan, Zheng root->block_rsv = NULL; 1202d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 12030b86a832SChris Mason 12040b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 12055d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1206eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1207eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1208199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1209199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 12102ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 12112ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1212d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 12135d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1214eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1215199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1216f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 12172ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 12182ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1219a2135011SChris Mason mutex_init(&root->objectid_mutex); 1220e02119d5SChris Mason mutex_init(&root->log_mutex); 12217237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12227237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12237237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12247237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12257237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12267237f183SYan Zheng atomic_set(&root->log_writers, 0); 12272ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12288a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1229b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 12307237f183SYan Zheng root->log_transid = 0; 1231257c62e1SChris Mason root->last_log_commit = 0; 123206ea65a3SJosef Bacik if (fs_info) 1233d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1234f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1235017e5369SChris Mason 12363768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12373768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12386702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 123958176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 124006ea65a3SJosef Bacik if (fs_info) 12413f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 124206ea65a3SJosef Bacik else 124306ea65a3SJosef Bacik root->defrag_trans_start = 0; 124458176a96SJosef Bacik init_completion(&root->kobj_unregister); 12456702ed49SChris Mason root->defrag_running = 0; 12464d775673SChris Mason root->root_key.objectid = objectid; 12470ee5dc67SAl Viro root->anon_dev = 0; 12488ea05e3aSAlexander Block 12495f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12503768f368SChris Mason } 12513768f368SChris Mason 1252f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12536f07e42eSAl Viro { 12546f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12556f07e42eSAl Viro if (root) 12566f07e42eSAl Viro root->fs_info = fs_info; 12576f07e42eSAl Viro return root; 12586f07e42eSAl Viro } 12596f07e42eSAl Viro 126006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 126106ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */ 126206ea65a3SJosef Bacik struct btrfs_root *btrfs_alloc_dummy_root(void) 126306ea65a3SJosef Bacik { 126406ea65a3SJosef Bacik struct btrfs_root *root; 126506ea65a3SJosef Bacik 126606ea65a3SJosef Bacik root = btrfs_alloc_root(NULL); 126706ea65a3SJosef Bacik if (!root) 126806ea65a3SJosef Bacik return ERR_PTR(-ENOMEM); 126906ea65a3SJosef Bacik __setup_root(4096, 4096, 4096, 4096, root, NULL, 1); 127006ea65a3SJosef Bacik root->dummy_root = 1; 127106ea65a3SJosef Bacik 127206ea65a3SJosef Bacik return root; 127306ea65a3SJosef Bacik } 127406ea65a3SJosef Bacik #endif 127506ea65a3SJosef Bacik 127620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 127720897f5cSArne Jansen struct btrfs_fs_info *fs_info, 127820897f5cSArne Jansen u64 objectid) 127920897f5cSArne Jansen { 128020897f5cSArne Jansen struct extent_buffer *leaf; 128120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 128220897f5cSArne Jansen struct btrfs_root *root; 128320897f5cSArne Jansen struct btrfs_key key; 128420897f5cSArne Jansen int ret = 0; 128520897f5cSArne Jansen u64 bytenr; 12866463fe58SStefan Behrens uuid_le uuid; 128720897f5cSArne Jansen 128820897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 128920897f5cSArne Jansen if (!root) 129020897f5cSArne Jansen return ERR_PTR(-ENOMEM); 129120897f5cSArne Jansen 129220897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 129320897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 129420897f5cSArne Jansen root, fs_info, objectid); 129520897f5cSArne Jansen root->root_key.objectid = objectid; 129620897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 129720897f5cSArne Jansen root->root_key.offset = 0; 129820897f5cSArne Jansen 129920897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 130020897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 130120897f5cSArne Jansen if (IS_ERR(leaf)) { 130220897f5cSArne Jansen ret = PTR_ERR(leaf); 13031dd05682STsutomu Itoh leaf = NULL; 130420897f5cSArne Jansen goto fail; 130520897f5cSArne Jansen } 130620897f5cSArne Jansen 130720897f5cSArne Jansen bytenr = leaf->start; 130820897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 130920897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 131020897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 131120897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 131220897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 131320897f5cSArne Jansen root->node = leaf; 131420897f5cSArne Jansen 1315fba6aa75SGeert Uytterhoeven write_extent_buffer(leaf, fs_info->fsid, btrfs_header_fsid(leaf), 131620897f5cSArne Jansen BTRFS_FSID_SIZE); 131720897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 1318b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 131920897f5cSArne Jansen BTRFS_UUID_SIZE); 132020897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 132120897f5cSArne Jansen 132220897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 132320897f5cSArne Jansen root->track_dirty = 1; 132420897f5cSArne Jansen 132520897f5cSArne Jansen 132620897f5cSArne Jansen root->root_item.flags = 0; 132720897f5cSArne Jansen root->root_item.byte_limit = 0; 132820897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 132920897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 133020897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 133120897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 133220897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 133320897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 133420897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 13356463fe58SStefan Behrens uuid_le_gen(&uuid); 13366463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 133720897f5cSArne Jansen root->root_item.drop_level = 0; 133820897f5cSArne Jansen 133920897f5cSArne Jansen key.objectid = objectid; 134020897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 134120897f5cSArne Jansen key.offset = 0; 134220897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 134320897f5cSArne Jansen if (ret) 134420897f5cSArne Jansen goto fail; 134520897f5cSArne Jansen 134620897f5cSArne Jansen btrfs_tree_unlock(leaf); 134720897f5cSArne Jansen 134820897f5cSArne Jansen return root; 13491dd05682STsutomu Itoh 13501dd05682STsutomu Itoh fail: 13511dd05682STsutomu Itoh if (leaf) { 13521dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13531dd05682STsutomu Itoh free_extent_buffer(leaf); 13541dd05682STsutomu Itoh } 13551dd05682STsutomu Itoh kfree(root); 13561dd05682STsutomu Itoh 13571dd05682STsutomu Itoh return ERR_PTR(ret); 135820897f5cSArne Jansen } 135920897f5cSArne Jansen 13607237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1361e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13620f7d52f4SChris Mason { 13630f7d52f4SChris Mason struct btrfs_root *root; 13640f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13657237f183SYan Zheng struct extent_buffer *leaf; 1366e02119d5SChris Mason 13676f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1368e02119d5SChris Mason if (!root) 13697237f183SYan Zheng return ERR_PTR(-ENOMEM); 1370e02119d5SChris Mason 1371e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1372e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1373e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1374e02119d5SChris Mason 1375e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1376e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1377e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13787237f183SYan Zheng /* 13797237f183SYan Zheng * log trees do not get reference counted because they go away 13807237f183SYan Zheng * before a real commit is actually done. They do store pointers 13817237f183SYan Zheng * to file data extents, and those reference counts still get 13827237f183SYan Zheng * updated (along with back refs to the log tree). 13837237f183SYan Zheng */ 1384e02119d5SChris Mason root->ref_cows = 0; 1385e02119d5SChris Mason 13865d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 138766d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13885581a51aSJan Schmidt 0, 0, 0); 13897237f183SYan Zheng if (IS_ERR(leaf)) { 13907237f183SYan Zheng kfree(root); 13917237f183SYan Zheng return ERR_CAST(leaf); 13927237f183SYan Zheng } 1393e02119d5SChris Mason 13945d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13955d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13965d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13975d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13985d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13997237f183SYan Zheng root->node = leaf; 1400e02119d5SChris Mason 1401e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1402fba6aa75SGeert Uytterhoeven btrfs_header_fsid(root->node), BTRFS_FSID_SIZE); 1403e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1404e02119d5SChris Mason btrfs_tree_unlock(root->node); 14057237f183SYan Zheng return root; 14067237f183SYan Zheng } 14077237f183SYan Zheng 14087237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14097237f183SYan Zheng struct btrfs_fs_info *fs_info) 14107237f183SYan Zheng { 14117237f183SYan Zheng struct btrfs_root *log_root; 14127237f183SYan Zheng 14137237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14147237f183SYan Zheng if (IS_ERR(log_root)) 14157237f183SYan Zheng return PTR_ERR(log_root); 14167237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14177237f183SYan Zheng fs_info->log_root_tree = log_root; 14187237f183SYan Zheng return 0; 14197237f183SYan Zheng } 14207237f183SYan Zheng 14217237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14227237f183SYan Zheng struct btrfs_root *root) 14237237f183SYan Zheng { 14247237f183SYan Zheng struct btrfs_root *log_root; 14257237f183SYan Zheng struct btrfs_inode_item *inode_item; 14267237f183SYan Zheng 14277237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 14287237f183SYan Zheng if (IS_ERR(log_root)) 14297237f183SYan Zheng return PTR_ERR(log_root); 14307237f183SYan Zheng 14317237f183SYan Zheng log_root->last_trans = trans->transid; 14327237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14337237f183SYan Zheng 14347237f183SYan Zheng inode_item = &log_root->root_item.inode; 14353cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14363cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14373cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 14383cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 14393cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14407237f183SYan Zheng 14415d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14427237f183SYan Zheng 14437237f183SYan Zheng WARN_ON(root->log_root); 14447237f183SYan Zheng root->log_root = log_root; 14457237f183SYan Zheng root->log_transid = 0; 1446257c62e1SChris Mason root->last_log_commit = 0; 1447e02119d5SChris Mason return 0; 1448e02119d5SChris Mason } 1449e02119d5SChris Mason 145035a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1451cb517eabSMiao Xie struct btrfs_key *key) 1452e02119d5SChris Mason { 1453e02119d5SChris Mason struct btrfs_root *root; 1454e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14550f7d52f4SChris Mason struct btrfs_path *path; 145684234f3aSYan Zheng u64 generation; 1457db94535dSChris Mason u32 blocksize; 1458cb517eabSMiao Xie int ret; 1459cb517eabSMiao Xie 1460cb517eabSMiao Xie path = btrfs_alloc_path(); 1461cb517eabSMiao Xie if (!path) 1462cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14630f7d52f4SChris Mason 14646f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1465cb517eabSMiao Xie if (!root) { 1466cb517eabSMiao Xie ret = -ENOMEM; 1467cb517eabSMiao Xie goto alloc_fail; 14680f7d52f4SChris Mason } 14690f7d52f4SChris Mason 1470db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 147187ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 1472cb517eabSMiao Xie root, fs_info, key->objectid); 14730f7d52f4SChris Mason 1474cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1475cb517eabSMiao Xie &root->root_item, &root->root_key); 14760f7d52f4SChris Mason if (ret) { 147713a8a7c8SYan, Zheng if (ret > 0) 147813a8a7c8SYan, Zheng ret = -ENOENT; 1479cb517eabSMiao Xie goto find_fail; 14800f7d52f4SChris Mason } 148113a8a7c8SYan, Zheng 148284234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1483db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1484db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 148584234f3aSYan Zheng blocksize, generation); 1486cb517eabSMiao Xie if (!root->node) { 1487cb517eabSMiao Xie ret = -ENOMEM; 1488cb517eabSMiao Xie goto find_fail; 1489cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1490cb517eabSMiao Xie ret = -EIO; 1491cb517eabSMiao Xie goto read_fail; 1492416bc658SJosef Bacik } 14935d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 149413a8a7c8SYan, Zheng out: 1495cb517eabSMiao Xie btrfs_free_path(path); 1496cb517eabSMiao Xie return root; 1497cb517eabSMiao Xie 1498cb517eabSMiao Xie read_fail: 1499cb517eabSMiao Xie free_extent_buffer(root->node); 1500cb517eabSMiao Xie find_fail: 1501cb517eabSMiao Xie kfree(root); 1502cb517eabSMiao Xie alloc_fail: 1503cb517eabSMiao Xie root = ERR_PTR(ret); 1504cb517eabSMiao Xie goto out; 1505cb517eabSMiao Xie } 1506cb517eabSMiao Xie 1507cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1508cb517eabSMiao Xie struct btrfs_key *location) 1509cb517eabSMiao Xie { 1510cb517eabSMiao Xie struct btrfs_root *root; 1511cb517eabSMiao Xie 1512cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1513cb517eabSMiao Xie if (IS_ERR(root)) 1514cb517eabSMiao Xie return root; 1515cb517eabSMiao Xie 1516cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 15170f7d52f4SChris Mason root->ref_cows = 1; 151808fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 151908fe4db1SLi Zefan } 152013a8a7c8SYan, Zheng 15215eda7b5eSChris Mason return root; 15225eda7b5eSChris Mason } 15235eda7b5eSChris Mason 1524cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1525cb517eabSMiao Xie { 1526cb517eabSMiao Xie int ret; 1527cb517eabSMiao Xie 1528cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1529cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1530cb517eabSMiao Xie GFP_NOFS); 1531cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1532cb517eabSMiao Xie ret = -ENOMEM; 1533cb517eabSMiao Xie goto fail; 1534cb517eabSMiao Xie } 1535cb517eabSMiao Xie 1536cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 1537cb517eabSMiao Xie mutex_init(&root->fs_commit_mutex); 1538cb517eabSMiao Xie spin_lock_init(&root->cache_lock); 1539cb517eabSMiao Xie init_waitqueue_head(&root->cache_wait); 1540cb517eabSMiao Xie 1541cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1542cb517eabSMiao Xie if (ret) 1543cb517eabSMiao Xie goto fail; 1544cb517eabSMiao Xie return 0; 1545cb517eabSMiao Xie fail: 1546cb517eabSMiao Xie kfree(root->free_ino_ctl); 1547cb517eabSMiao Xie kfree(root->free_ino_pinned); 1548cb517eabSMiao Xie return ret; 1549cb517eabSMiao Xie } 1550cb517eabSMiao Xie 1551171170c1SSergei Trofimovich static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1552cb517eabSMiao Xie u64 root_id) 1553cb517eabSMiao Xie { 1554cb517eabSMiao Xie struct btrfs_root *root; 1555cb517eabSMiao Xie 1556cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1557cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1558cb517eabSMiao Xie (unsigned long)root_id); 1559cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1560cb517eabSMiao Xie return root; 1561cb517eabSMiao Xie } 1562cb517eabSMiao Xie 1563cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1564cb517eabSMiao Xie struct btrfs_root *root) 1565cb517eabSMiao Xie { 1566cb517eabSMiao Xie int ret; 1567cb517eabSMiao Xie 1568cb517eabSMiao Xie ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 1569cb517eabSMiao Xie if (ret) 1570cb517eabSMiao Xie return ret; 1571cb517eabSMiao Xie 1572cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1573cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1574cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1575cb517eabSMiao Xie root); 1576cb517eabSMiao Xie if (ret == 0) 1577cb517eabSMiao Xie root->in_radix = 1; 1578cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1579cb517eabSMiao Xie radix_tree_preload_end(); 1580cb517eabSMiao Xie 1581cb517eabSMiao Xie return ret; 1582cb517eabSMiao Xie } 1583cb517eabSMiao Xie 1584c00869f1SMiao Xie struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 1585c00869f1SMiao Xie struct btrfs_key *location, 1586c00869f1SMiao Xie bool check_ref) 15875eda7b5eSChris Mason { 15885eda7b5eSChris Mason struct btrfs_root *root; 15895eda7b5eSChris Mason int ret; 15905eda7b5eSChris Mason 1591edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1592edbd8d4eSChris Mason return fs_info->tree_root; 1593edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1594edbd8d4eSChris Mason return fs_info->extent_root; 15958f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15968f18cf13SChris Mason return fs_info->chunk_root; 15978f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15988f18cf13SChris Mason return fs_info->dev_root; 15990403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 16000403e47eSYan Zheng return fs_info->csum_root; 1601bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1602bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1603bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1604f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1605f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1606f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 16074df27c4dSYan, Zheng again: 1608cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 160948475471SStefan Behrens if (root) { 1610c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) 161148475471SStefan Behrens return ERR_PTR(-ENOENT); 16125eda7b5eSChris Mason return root; 161348475471SStefan Behrens } 16145eda7b5eSChris Mason 1615cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 16165eda7b5eSChris Mason if (IS_ERR(root)) 16175eda7b5eSChris Mason return root; 16183394e160SChris Mason 1619c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) { 1620d68fc57bSYan, Zheng ret = -ENOENT; 1621d68fc57bSYan, Zheng goto fail; 1622d68fc57bSYan, Zheng } 1623d68fc57bSYan, Zheng 1624cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1625cb517eabSMiao Xie if (ret) 1626cb517eabSMiao Xie goto fail; 1627cb517eabSMiao Xie 1628d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1629d68fc57bSYan, Zheng if (ret < 0) 1630d68fc57bSYan, Zheng goto fail; 1631d68fc57bSYan, Zheng if (ret == 0) 1632d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1633d68fc57bSYan, Zheng 1634cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16350f7d52f4SChris Mason if (ret) { 16364df27c4dSYan, Zheng if (ret == -EEXIST) { 16374df27c4dSYan, Zheng free_fs_root(root); 16384df27c4dSYan, Zheng goto again; 16390f7d52f4SChris Mason } 16404df27c4dSYan, Zheng goto fail; 16414df27c4dSYan, Zheng } 1642edbd8d4eSChris Mason return root; 16434df27c4dSYan, Zheng fail: 16444df27c4dSYan, Zheng free_fs_root(root); 16454df27c4dSYan, Zheng return ERR_PTR(ret); 1646edbd8d4eSChris Mason } 1647edbd8d4eSChris Mason 164804160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 164904160088SChris Mason { 165004160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 165104160088SChris Mason int ret = 0; 165204160088SChris Mason struct btrfs_device *device; 165304160088SChris Mason struct backing_dev_info *bdi; 1654b7967db7SChris Mason 16551f78160cSXiao Guangrong rcu_read_lock(); 16561f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1657dfe25020SChris Mason if (!device->bdev) 1658dfe25020SChris Mason continue; 165904160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 166004160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 166104160088SChris Mason ret = 1; 166204160088SChris Mason break; 166304160088SChris Mason } 166404160088SChris Mason } 16651f78160cSXiao Guangrong rcu_read_unlock(); 166604160088SChris Mason return ret; 166704160088SChris Mason } 166804160088SChris Mason 166938b66988SChris Mason /* 1670ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1671ad081f14SJens Axboe * bdi again. 1672ad081f14SJens Axboe */ 167304160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 167404160088SChris Mason { 1675ad081f14SJens Axboe int err; 1676ad081f14SJens Axboe 1677ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1678e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1679ad081f14SJens Axboe if (err) 1680ad081f14SJens Axboe return err; 1681ad081f14SJens Axboe 16824575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 168304160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 168404160088SChris Mason bdi->congested_data = info; 168504160088SChris Mason return 0; 168604160088SChris Mason } 168704160088SChris Mason 16888b712842SChris Mason /* 16898b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16908b712842SChris Mason * functions. This is where read checksum verification actually happens 16918b712842SChris Mason */ 16928b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1693ce9adaa5SChris Mason { 1694ce9adaa5SChris Mason struct bio *bio; 16958b712842SChris Mason struct end_io_wq *end_io_wq; 16968b712842SChris Mason struct btrfs_fs_info *fs_info; 1697ce9adaa5SChris Mason int error; 1698ce9adaa5SChris Mason 16998b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1700ce9adaa5SChris Mason bio = end_io_wq->bio; 17018b712842SChris Mason fs_info = end_io_wq->info; 17028b712842SChris Mason 1703ce9adaa5SChris Mason error = end_io_wq->error; 1704ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1705ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1706ce9adaa5SChris Mason kfree(end_io_wq); 1707ce9adaa5SChris Mason bio_endio(bio, error); 1708ce9adaa5SChris Mason } 170944b8bd7eSChris Mason 1710a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1711a74a4b97SChris Mason { 1712a74a4b97SChris Mason struct btrfs_root *root = arg; 1713d0278245SMiao Xie int again; 1714a74a4b97SChris Mason 1715a74a4b97SChris Mason do { 1716d0278245SMiao Xie again = 0; 17179d1a2a3aSDavid Sterba 1718d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 1719babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) 1720d0278245SMiao Xie goto sleep; 1721d0278245SMiao Xie 1722d0278245SMiao Xie if (!mutex_trylock(&root->fs_info->cleaner_mutex)) 1723d0278245SMiao Xie goto sleep; 1724d0278245SMiao Xie 1725dc7f370cSMiao Xie /* 1726dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1727dc7f370cSMiao Xie * during the above check and trylock. 1728dc7f370cSMiao Xie */ 1729babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) { 1730dc7f370cSMiao Xie mutex_unlock(&root->fs_info->cleaner_mutex); 1731dc7f370cSMiao Xie goto sleep; 1732dc7f370cSMiao Xie } 1733dc7f370cSMiao Xie 173424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 17359d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 1736a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 1737a74a4b97SChris Mason 1738d0278245SMiao Xie /* 173905323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 174005323cd1SMiao Xie * needn't do anything special here. 1741d0278245SMiao Xie */ 1742d0278245SMiao Xie btrfs_run_defrag_inodes(root->fs_info); 1743d0278245SMiao Xie sleep: 17449d1a2a3aSDavid Sterba if (!try_to_freeze() && !again) { 1745a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17468929ecfaSYan, Zheng if (!kthread_should_stop()) 1747a74a4b97SChris Mason schedule(); 1748a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1749a74a4b97SChris Mason } 1750a74a4b97SChris Mason } while (!kthread_should_stop()); 1751a74a4b97SChris Mason return 0; 1752a74a4b97SChris Mason } 1753a74a4b97SChris Mason 1754a74a4b97SChris Mason static int transaction_kthread(void *arg) 1755a74a4b97SChris Mason { 1756a74a4b97SChris Mason struct btrfs_root *root = arg; 1757a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1758a74a4b97SChris Mason struct btrfs_transaction *cur; 17598929ecfaSYan, Zheng u64 transid; 1760a74a4b97SChris Mason unsigned long now; 1761a74a4b97SChris Mason unsigned long delay; 1762914b2007SJan Kara bool cannot_commit; 1763a74a4b97SChris Mason 1764a74a4b97SChris Mason do { 1765914b2007SJan Kara cannot_commit = false; 17668b87dc17SDavid Sterba delay = HZ * root->fs_info->commit_interval; 1767a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1768a74a4b97SChris Mason 1769a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1770a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1771a74a4b97SChris Mason if (!cur) { 1772a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1773a74a4b97SChris Mason goto sleep; 1774a74a4b97SChris Mason } 177531153d81SYan Zheng 1776a74a4b97SChris Mason now = get_seconds(); 17774a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 17788b87dc17SDavid Sterba (now < cur->start_time || 17798b87dc17SDavid Sterba now - cur->start_time < root->fs_info->commit_interval)) { 1780a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1781a74a4b97SChris Mason delay = HZ * 5; 1782a74a4b97SChris Mason goto sleep; 1783a74a4b97SChris Mason } 17848929ecfaSYan, Zheng transid = cur->transid; 1785a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 178656bec294SChris Mason 178779787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1788354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1789914b2007SJan Kara if (IS_ERR(trans)) { 1790354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1791914b2007SJan Kara cannot_commit = true; 179279787eaaSJeff Mahoney goto sleep; 1793914b2007SJan Kara } 17948929ecfaSYan, Zheng if (transid == trans->transid) { 179579787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 17968929ecfaSYan, Zheng } else { 17978929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 17988929ecfaSYan, Zheng } 1799a74a4b97SChris Mason sleep: 1800a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1801a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1802a74a4b97SChris Mason 18034e121c06SJosef Bacik if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 18044e121c06SJosef Bacik &root->fs_info->fs_state))) 18054e121c06SJosef Bacik btrfs_cleanup_transaction(root); 1806a0acae0eSTejun Heo if (!try_to_freeze()) { 1807a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 18088929ecfaSYan, Zheng if (!kthread_should_stop() && 1809914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1810914b2007SJan Kara cannot_commit)) 1811a74a4b97SChris Mason schedule_timeout(delay); 1812a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1813a74a4b97SChris Mason } 1814a74a4b97SChris Mason } while (!kthread_should_stop()); 1815a74a4b97SChris Mason return 0; 1816a74a4b97SChris Mason } 1817a74a4b97SChris Mason 1818af31f5e5SChris Mason /* 1819af31f5e5SChris Mason * this will find the highest generation in the array of 1820af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1821af31f5e5SChris Mason * or -1 if we can't find anything. 1822af31f5e5SChris Mason * 1823af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1824af31f5e5SChris Mason * generation of the latest root in the array with the generation 1825af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1826af31f5e5SChris Mason */ 1827af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1828af31f5e5SChris Mason { 1829af31f5e5SChris Mason u64 cur; 1830af31f5e5SChris Mason int newest_index = -1; 1831af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1832af31f5e5SChris Mason int i; 1833af31f5e5SChris Mason 1834af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1835af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1836af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1837af31f5e5SChris Mason if (cur == newest_gen) 1838af31f5e5SChris Mason newest_index = i; 1839af31f5e5SChris Mason } 1840af31f5e5SChris Mason 1841af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1842af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1843af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1844af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1845af31f5e5SChris Mason if (cur == newest_gen) 1846af31f5e5SChris Mason newest_index = 0; 1847af31f5e5SChris Mason } 1848af31f5e5SChris Mason return newest_index; 1849af31f5e5SChris Mason } 1850af31f5e5SChris Mason 1851af31f5e5SChris Mason 1852af31f5e5SChris Mason /* 1853af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1854af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1855af31f5e5SChris Mason * field in the fs_info struct 1856af31f5e5SChris Mason */ 1857af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1858af31f5e5SChris Mason u64 newest_gen) 1859af31f5e5SChris Mason { 1860af31f5e5SChris Mason int newest_index = -1; 1861af31f5e5SChris Mason 1862af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1863af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1864af31f5e5SChris Mason if (newest_index == -1) { 1865af31f5e5SChris Mason info->backup_root_index = 0; 1866af31f5e5SChris Mason } else { 1867af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1868af31f5e5SChris Mason } 1869af31f5e5SChris Mason } 1870af31f5e5SChris Mason 1871af31f5e5SChris Mason /* 1872af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1873af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1874af31f5e5SChris Mason * done 1875af31f5e5SChris Mason */ 1876af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1877af31f5e5SChris Mason { 1878af31f5e5SChris Mason int next_backup; 1879af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1880af31f5e5SChris Mason int last_backup; 1881af31f5e5SChris Mason 1882af31f5e5SChris Mason next_backup = info->backup_root_index; 1883af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1884af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1885af31f5e5SChris Mason 1886af31f5e5SChris Mason /* 1887af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1888af31f5e5SChris Mason * this happens only at umount 1889af31f5e5SChris Mason */ 1890af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1891af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1892af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1893af31f5e5SChris Mason next_backup = last_backup; 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1896af31f5e5SChris Mason 1897af31f5e5SChris Mason /* 1898af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1899af31f5e5SChris Mason * regardless of which ones we use today 1900af31f5e5SChris Mason */ 1901af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1902af31f5e5SChris Mason 1903af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1904af31f5e5SChris Mason 1905af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1906af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1907af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1908af31f5e5SChris Mason 1909af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1910af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1911af31f5e5SChris Mason 1912af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1913af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1914af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1915af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1916af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1917af31f5e5SChris Mason 1918af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1919af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1920af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1921af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1922af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1923af31f5e5SChris Mason 19247c7e82a7SChris Mason /* 19257c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19267c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19277c7e82a7SChris Mason */ 19287c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19297c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19307c7e82a7SChris Mason info->fs_root->node->start); 1931af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1932af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1933af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1934af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19357c7e82a7SChris Mason } 1936af31f5e5SChris Mason 1937af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1938af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1939af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1940af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1941af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1942af31f5e5SChris Mason 1943af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1944af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1945af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1946af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1947af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1948af31f5e5SChris Mason 1949af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1950af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1951af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1952af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1953af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1954af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1955af31f5e5SChris Mason 1956af31f5e5SChris Mason /* 1957af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1958af31f5e5SChris Mason * for the next commit 1959af31f5e5SChris Mason */ 1960af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1961af31f5e5SChris Mason &info->super_for_commit->super_roots, 1962af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1963af31f5e5SChris Mason } 1964af31f5e5SChris Mason 1965af31f5e5SChris Mason /* 1966af31f5e5SChris Mason * this copies info out of the root backup array and back into 1967af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1968af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1969af31f5e5SChris Mason * tried and the last backup index you used. 1970af31f5e5SChris Mason * 1971af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1972af31f5e5SChris Mason */ 1973af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1974af31f5e5SChris Mason struct btrfs_super_block *super, 1975af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1976af31f5e5SChris Mason { 1977af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1978af31f5e5SChris Mason int newest = *backup_index; 1979af31f5e5SChris Mason 1980af31f5e5SChris Mason if (*num_backups_tried == 0) { 1981af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1982af31f5e5SChris Mason 1983af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1984af31f5e5SChris Mason if (newest == -1) 1985af31f5e5SChris Mason return -1; 1986af31f5e5SChris Mason 1987af31f5e5SChris Mason *backup_index = newest; 1988af31f5e5SChris Mason *num_backups_tried = 1; 1989af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1990af31f5e5SChris Mason /* we've tried all the backups, all done */ 1991af31f5e5SChris Mason return -1; 1992af31f5e5SChris Mason } else { 1993af31f5e5SChris Mason /* jump to the next oldest backup */ 1994af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1995af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1996af31f5e5SChris Mason *backup_index = newest; 1997af31f5e5SChris Mason *num_backups_tried += 1; 1998af31f5e5SChris Mason } 1999af31f5e5SChris Mason root_backup = super->super_roots + newest; 2000af31f5e5SChris Mason 2001af31f5e5SChris Mason btrfs_set_super_generation(super, 2002af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 2003af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 2004af31f5e5SChris Mason btrfs_set_super_root_level(super, 2005af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 2006af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 2007af31f5e5SChris Mason 2008af31f5e5SChris Mason /* 2009af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 2010af31f5e5SChris Mason * need a fsck 2011af31f5e5SChris Mason */ 2012af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 2013af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 2014af31f5e5SChris Mason return 0; 2015af31f5e5SChris Mason } 2016af31f5e5SChris Mason 20177abadb64SLiu Bo /* helper to cleanup workers */ 20187abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 20197abadb64SLiu Bo { 20207abadb64SLiu Bo btrfs_stop_workers(&fs_info->generic_worker); 20217abadb64SLiu Bo btrfs_stop_workers(&fs_info->fixup_workers); 20227abadb64SLiu Bo btrfs_stop_workers(&fs_info->delalloc_workers); 20237abadb64SLiu Bo btrfs_stop_workers(&fs_info->workers); 20247abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_workers); 20257abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_workers); 20267abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_raid56_workers); 20277abadb64SLiu Bo btrfs_stop_workers(&fs_info->rmw_workers); 20287abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_write_workers); 20297abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_write_workers); 20307abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_freespace_worker); 20317abadb64SLiu Bo btrfs_stop_workers(&fs_info->submit_workers); 20327abadb64SLiu Bo btrfs_stop_workers(&fs_info->delayed_workers); 20337abadb64SLiu Bo btrfs_stop_workers(&fs_info->caching_workers); 20347abadb64SLiu Bo btrfs_stop_workers(&fs_info->readahead_workers); 20357abadb64SLiu Bo btrfs_stop_workers(&fs_info->flush_workers); 20362f232036SJan Schmidt btrfs_stop_workers(&fs_info->qgroup_rescan_workers); 20377abadb64SLiu Bo } 20387abadb64SLiu Bo 2039af31f5e5SChris Mason /* helper to cleanup tree roots */ 2040af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2041af31f5e5SChris Mason { 2042af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 2043af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 2044655b09feSJosef Bacik info->tree_root->node = NULL; 2045655b09feSJosef Bacik info->tree_root->commit_root = NULL; 2046655b09feSJosef Bacik 2047655b09feSJosef Bacik if (info->dev_root) { 2048af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 2049af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 2050655b09feSJosef Bacik info->dev_root->node = NULL; 2051655b09feSJosef Bacik info->dev_root->commit_root = NULL; 2052655b09feSJosef Bacik } 2053655b09feSJosef Bacik if (info->extent_root) { 2054af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 2055af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 2056655b09feSJosef Bacik info->extent_root->node = NULL; 2057655b09feSJosef Bacik info->extent_root->commit_root = NULL; 2058655b09feSJosef Bacik } 2059655b09feSJosef Bacik if (info->csum_root) { 2060af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 2061af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 2062655b09feSJosef Bacik info->csum_root->node = NULL; 2063655b09feSJosef Bacik info->csum_root->commit_root = NULL; 2064655b09feSJosef Bacik } 2065bcef60f2SArne Jansen if (info->quota_root) { 2066bcef60f2SArne Jansen free_extent_buffer(info->quota_root->node); 2067bcef60f2SArne Jansen free_extent_buffer(info->quota_root->commit_root); 2068bcef60f2SArne Jansen info->quota_root->node = NULL; 2069bcef60f2SArne Jansen info->quota_root->commit_root = NULL; 2070bcef60f2SArne Jansen } 2071f7a81ea4SStefan Behrens if (info->uuid_root) { 2072f7a81ea4SStefan Behrens free_extent_buffer(info->uuid_root->node); 2073f7a81ea4SStefan Behrens free_extent_buffer(info->uuid_root->commit_root); 2074f7a81ea4SStefan Behrens info->uuid_root->node = NULL; 2075f7a81ea4SStefan Behrens info->uuid_root->commit_root = NULL; 2076f7a81ea4SStefan Behrens } 2077af31f5e5SChris Mason if (chunk_root) { 2078af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 2079af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 2080af31f5e5SChris Mason info->chunk_root->node = NULL; 2081af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 2082af31f5e5SChris Mason } 2083af31f5e5SChris Mason } 2084af31f5e5SChris Mason 2085171f6537SJosef Bacik static void del_fs_roots(struct btrfs_fs_info *fs_info) 2086171f6537SJosef Bacik { 2087171f6537SJosef Bacik int ret; 2088171f6537SJosef Bacik struct btrfs_root *gang[8]; 2089171f6537SJosef Bacik int i; 2090171f6537SJosef Bacik 2091171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2092171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2093171f6537SJosef Bacik struct btrfs_root, root_list); 2094171f6537SJosef Bacik list_del(&gang[0]->root_list); 2095171f6537SJosef Bacik 2096171f6537SJosef Bacik if (gang[0]->in_radix) { 2097cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2098171f6537SJosef Bacik } else { 2099171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2100171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2101b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2102171f6537SJosef Bacik } 2103171f6537SJosef Bacik } 2104171f6537SJosef Bacik 2105171f6537SJosef Bacik while (1) { 2106171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2107171f6537SJosef Bacik (void **)gang, 0, 2108171f6537SJosef Bacik ARRAY_SIZE(gang)); 2109171f6537SJosef Bacik if (!ret) 2110171f6537SJosef Bacik break; 2111171f6537SJosef Bacik for (i = 0; i < ret; i++) 2112cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2113171f6537SJosef Bacik } 2114171f6537SJosef Bacik } 2115af31f5e5SChris Mason 2116ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2117dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2118dfe25020SChris Mason char *options) 2119eb60ceacSChris Mason { 2120db94535dSChris Mason u32 sectorsize; 2121db94535dSChris Mason u32 nodesize; 2122db94535dSChris Mason u32 leafsize; 2123db94535dSChris Mason u32 blocksize; 212487ee04ebSChris Mason u32 stripesize; 212584234f3aSYan Zheng u64 generation; 2126f2b636e8SJosef Bacik u64 features; 21273de4586cSChris Mason struct btrfs_key location; 2128a061fc8dSChris Mason struct buffer_head *bh; 21294d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2130815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2131f84a8bd6SAl Viro struct btrfs_root *tree_root; 21324d34b278SIlya Dryomov struct btrfs_root *extent_root; 21334d34b278SIlya Dryomov struct btrfs_root *csum_root; 21344d34b278SIlya Dryomov struct btrfs_root *chunk_root; 21354d34b278SIlya Dryomov struct btrfs_root *dev_root; 2136bcef60f2SArne Jansen struct btrfs_root *quota_root; 2137f7a81ea4SStefan Behrens struct btrfs_root *uuid_root; 2138e02119d5SChris Mason struct btrfs_root *log_tree_root; 2139eb60ceacSChris Mason int ret; 2140e58ca020SYan int err = -EINVAL; 2141af31f5e5SChris Mason int num_backups_tried = 0; 2142af31f5e5SChris Mason int backup_index = 0; 214370f80175SStefan Behrens bool create_uuid_tree; 214470f80175SStefan Behrens bool check_uuid_tree; 21454543df7eSChris Mason 2146f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 21476f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 2148cb517eabSMiao Xie if (!tree_root || !chunk_root) { 214939279cc3SChris Mason err = -ENOMEM; 215039279cc3SChris Mason goto fail; 215139279cc3SChris Mason } 215276dda93cSYan, Zheng 215376dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 215476dda93cSYan, Zheng if (ret) { 215576dda93cSYan, Zheng err = ret; 215676dda93cSYan, Zheng goto fail; 215776dda93cSYan, Zheng } 215876dda93cSYan, Zheng 215976dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 216076dda93cSYan, Zheng if (ret) { 216176dda93cSYan, Zheng err = ret; 216276dda93cSYan, Zheng goto fail_srcu; 216376dda93cSYan, Zheng } 216476dda93cSYan, Zheng 2165e2d84521SMiao Xie ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0); 2166e2d84521SMiao Xie if (ret) { 2167e2d84521SMiao Xie err = ret; 2168e2d84521SMiao Xie goto fail_bdi; 2169e2d84521SMiao Xie } 2170e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2171e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2172e2d84521SMiao Xie 2173963d678bSMiao Xie ret = percpu_counter_init(&fs_info->delalloc_bytes, 0); 2174963d678bSMiao Xie if (ret) { 2175963d678bSMiao Xie err = ret; 2176963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2177963d678bSMiao Xie } 2178963d678bSMiao Xie 217976dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 218076dda93cSYan, Zheng if (!fs_info->btree_inode) { 218176dda93cSYan, Zheng err = -ENOMEM; 2182963d678bSMiao Xie goto fail_delalloc_bytes; 218376dda93cSYan, Zheng } 218476dda93cSYan, Zheng 2185a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 21861561dedaSMiao Xie 218776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 21888fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2189facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 219024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2191eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 219211833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2193eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2194a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 219576dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 219624bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 21974cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 21982bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2199f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2200ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2201f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 22027585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2203de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 220419c00ddcSChris Mason 220558176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 22060b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 22076324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2208f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 22090b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 221066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 221166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 221266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 221366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 221466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 221566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 221666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 221766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 221866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2219cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2220771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 22218c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 22220986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 22234cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2224fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2225e20d96d6SChris Mason fs_info->sb = sb; 22266f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 22279ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 22284cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 22292bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2230f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 22318b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2232c8b97818SChris Mason 223390519d66SArne Jansen /* readahead state */ 223490519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 223590519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 22362c90e5d6SChris Mason 2237b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2238b34b086cSChris Mason num_online_cpus() + 2, 8); 22390afbaf8cSChris Mason 2240199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2241199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 224216cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 224316cdcec7SMiao Xie GFP_NOFS); 224416cdcec7SMiao Xie if (!fs_info->delayed_root) { 224516cdcec7SMiao Xie err = -ENOMEM; 224616cdcec7SMiao Xie goto fail_iput; 224716cdcec7SMiao Xie } 224816cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 22493eaa2885SChris Mason 2250a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2251a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2252a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2253a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2254a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2255a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2256a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2257a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 225821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 225921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 226021adbd5cSStefan Behrens #endif 2261a2de733cSArne Jansen 2262c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2263c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2264837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2265837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2266a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2267c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2268837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2269a061fc8dSChris Mason 22700afbaf8cSChris Mason sb->s_blocksize = 4096; 22710afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 227232a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 22730afbaf8cSChris Mason 227476dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2275bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 22760afbaf8cSChris Mason /* 22770afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 22780afbaf8cSChris Mason * the real end of the address space is determined by all of 22790afbaf8cSChris Mason * the devices in the system 22800afbaf8cSChris Mason */ 22810afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2282d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 228304160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 228404160088SChris Mason 22855d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2286d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2287f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 22880b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2289a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 22900da5468fSChris Mason 2291d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2292d1310b2eSChris Mason 22930f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 22940f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 22950f7d52f4SChris Mason sizeof(struct btrfs_key)); 229672ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 229772ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 229822b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 229939279cc3SChris Mason 2300e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 23016bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2302a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2303925baeddSChris Mason 230411833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2305f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 230611833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2307f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 230811833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2309e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2310f9295749SChris Mason 2311d98237b3SChris Mason 23125a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 23139ffba8cdSJosef Bacik mutex_init(&fs_info->ordered_extent_flush_mutex); 231430ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2315925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2316925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2317a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2318a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2319276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2320c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 232176dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2322803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1); 2323e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2324e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2325e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2326e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2327e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2328fa9c0d79SChris Mason 2329416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2330f2f6ed3dSWang Shilong mutex_init(&fs_info->qgroup_ioctl_lock); 2331416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2332416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2333416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2334416ac51dSArne Jansen fs_info->quota_enabled = 0; 2335416ac51dSArne Jansen fs_info->pending_quota_state = 0; 23361e8f9158SWang Shilong fs_info->qgroup_ulist = NULL; 23372f232036SJan Schmidt mutex_init(&fs_info->qgroup_rescan_lock); 2338416ac51dSArne Jansen 2339fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2340fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2341fa9c0d79SChris Mason 23427d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 23433768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2344bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 23454854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 23469a8dd150SChris Mason 234753b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 234853b381b3SDavid Woodhouse if (ret) { 234983c8266aSDavid Sterba err = ret; 235053b381b3SDavid Woodhouse goto fail_alloc; 235153b381b3SDavid Woodhouse } 235253b381b3SDavid Woodhouse 23530b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 23542c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 23557eccb903SChris Mason 23563c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 23571104a885SDavid Sterba 23581104a885SDavid Sterba /* 23591104a885SDavid Sterba * Read super block and check the signature bytes only 23601104a885SDavid Sterba */ 2361a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 236220b45077SDave Young if (!bh) { 236320b45077SDave Young err = -EINVAL; 236416cdcec7SMiao Xie goto fail_alloc; 236520b45077SDave Young } 236639279cc3SChris Mason 23671104a885SDavid Sterba /* 23681104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 23691104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 23701104a885SDavid Sterba */ 23711104a885SDavid Sterba if (btrfs_check_super_csum(bh->b_data)) { 23721104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock checksum mismatch\n"); 23731104a885SDavid Sterba err = -EINVAL; 23741104a885SDavid Sterba goto fail_alloc; 23751104a885SDavid Sterba } 23761104a885SDavid Sterba 23771104a885SDavid Sterba /* 23781104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 23791104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 23801104a885SDavid Sterba * the whole block of INFO_SIZE 23811104a885SDavid Sterba */ 23826c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 23836c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 23846c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2385a061fc8dSChris Mason brelse(bh); 23865f39d397SChris Mason 23876c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 23880b86a832SChris Mason 23891104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 23901104a885SDavid Sterba if (ret) { 23911104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock contains fatal errors\n"); 23921104a885SDavid Sterba err = -EINVAL; 23931104a885SDavid Sterba goto fail_alloc; 23941104a885SDavid Sterba } 23951104a885SDavid Sterba 23966c41761fSDavid Sterba disk_super = fs_info->super_copy; 23970f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 239816cdcec7SMiao Xie goto fail_alloc; 23990f7d52f4SChris Mason 2400acce952bSliubo /* check FS state, whether FS is broken. */ 240187533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 240287533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2403acce952bSliubo 240475e7cb7fSLiu Bo /* 2405af31f5e5SChris Mason * run through our array of backup supers and setup 2406af31f5e5SChris Mason * our ring pointer to the oldest one 2407af31f5e5SChris Mason */ 2408af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2409af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2410af31f5e5SChris Mason 2411af31f5e5SChris Mason /* 241275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 241375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 241475e7cb7fSLiu Bo */ 241575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 241675e7cb7fSLiu Bo 24172b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 24182b82032cSYan Zheng if (ret) { 24192b82032cSYan Zheng err = ret; 242016cdcec7SMiao Xie goto fail_alloc; 24212b82032cSYan Zheng } 2422dfe25020SChris Mason 2423f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2424f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2425f2b636e8SJosef Bacik if (features) { 2426f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2427f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 2428c1c9ff7cSGeert Uytterhoeven features); 2429f2b636e8SJosef Bacik err = -EINVAL; 243016cdcec7SMiao Xie goto fail_alloc; 2431f2b636e8SJosef Bacik } 2432f2b636e8SJosef Bacik 2433727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2434727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2435727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2436727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2437727011e0SChris Mason btrfs_super_nodesize(disk_super), 2438727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2439727011e0SChris Mason err = -EINVAL; 2440727011e0SChris Mason goto fail_alloc; 2441727011e0SChris Mason } 2442727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2443727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2444727011e0SChris Mason "blocksize (%d) was too large\n", 2445727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2446727011e0SChris Mason err = -EINVAL; 2447727011e0SChris Mason goto fail_alloc; 2448727011e0SChris Mason } 2449727011e0SChris Mason 24505d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 24515d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 245269e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2453a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2454727011e0SChris Mason 24553173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 24563173a18fSJosef Bacik printk(KERN_ERR "btrfs: has skinny extents\n"); 24573173a18fSJosef Bacik 2458727011e0SChris Mason /* 2459727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2460727011e0SChris Mason * they are bigger than the page size 2461727011e0SChris Mason */ 2462727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2463727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2464727011e0SChris Mason printk(KERN_INFO "btrfs flagging fs with big metadata feature\n"); 2465727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2466727011e0SChris Mason } 2467727011e0SChris Mason 2468bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2469bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2470bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2471bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2472e2d84521SMiao Xie fs_info->dirty_metadata_batch = leafsize * (1 + ilog2(nr_cpu_ids)); 2473963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2474bc3f116fSChris Mason 2475bc3f116fSChris Mason /* 2476bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2477bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2478bc3f116fSChris Mason */ 2479bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2480bc3f116fSChris Mason (sectorsize != leafsize)) { 2481bc3f116fSChris Mason printk(KERN_WARNING "btrfs: unequal leaf/node/sector sizes " 2482bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2483bc3f116fSChris Mason sb->s_id); 2484bc3f116fSChris Mason goto fail_alloc; 2485bc3f116fSChris Mason } 2486bc3f116fSChris Mason 2487ceda0864SMiao Xie /* 2488ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2489ceda0864SMiao Xie * update the flag. 2490ceda0864SMiao Xie */ 24915d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 24925d4f98a2SYan Zheng 2493f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2494f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2495f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2496f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2497f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 2498c1c9ff7cSGeert Uytterhoeven features); 2499f2b636e8SJosef Bacik err = -EINVAL; 250016cdcec7SMiao Xie goto fail_alloc; 2501f2b636e8SJosef Bacik } 250261d92c32SChris Mason 250361d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 250461d92c32SChris Mason "genwork", 1, NULL); 250561d92c32SChris Mason 25065443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 250761d92c32SChris Mason fs_info->thread_pool_size, 250861d92c32SChris Mason &fs_info->generic_worker); 2509c8b97818SChris Mason 2510771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 251145d5fd14SJosef Bacik fs_info->thread_pool_size, NULL); 2512771ed689SChris Mason 25138ccf6f19SMiao Xie btrfs_init_workers(&fs_info->flush_workers, "flush_delalloc", 251445d5fd14SJosef Bacik fs_info->thread_pool_size, NULL); 25158ccf6f19SMiao Xie 25165443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2517b720d209SChris Mason min_t(u64, fs_devices->num_devices, 251845d5fd14SJosef Bacik fs_info->thread_pool_size), NULL); 251961b49440SChris Mason 2520bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 252145d5fd14SJosef Bacik fs_info->thread_pool_size, NULL); 2522bab39bf9SJosef Bacik 252361b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 252461b49440SChris Mason * likely that bios will be send down in a sane order to the 252561b49440SChris Mason * devices 252661b49440SChris Mason */ 252761b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 252853863232SChris Mason 2529771ed689SChris Mason fs_info->workers.idle_thresh = 16; 25304a69a410SChris Mason fs_info->workers.ordered = 1; 253161b49440SChris Mason 2532771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2533771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2534771ed689SChris Mason 253561d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 253661d92c32SChris Mason &fs_info->generic_worker); 25375443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 253861d92c32SChris Mason fs_info->thread_pool_size, 253961d92c32SChris Mason &fs_info->generic_worker); 2540d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 254161d92c32SChris Mason fs_info->thread_pool_size, 254261d92c32SChris Mason &fs_info->generic_worker); 2543cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 254461d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 254561d92c32SChris Mason &fs_info->generic_worker); 254653b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->endio_raid56_workers, 254753b381b3SDavid Woodhouse "endio-raid56", fs_info->thread_pool_size, 254853b381b3SDavid Woodhouse &fs_info->generic_worker); 254953b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->rmw_workers, 255053b381b3SDavid Woodhouse "rmw", fs_info->thread_pool_size, 255153b381b3SDavid Woodhouse &fs_info->generic_worker); 25525443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 255361d92c32SChris Mason fs_info->thread_pool_size, 255461d92c32SChris Mason &fs_info->generic_worker); 25550cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 25560cb59c99SJosef Bacik 1, &fs_info->generic_worker); 255716cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 255816cdcec7SMiao Xie fs_info->thread_pool_size, 255916cdcec7SMiao Xie &fs_info->generic_worker); 256090519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 256190519d66SArne Jansen fs_info->thread_pool_size, 256290519d66SArne Jansen &fs_info->generic_worker); 25632f232036SJan Schmidt btrfs_init_workers(&fs_info->qgroup_rescan_workers, "qgroup-rescan", 1, 25642f232036SJan Schmidt &fs_info->generic_worker); 256561b49440SChris Mason 256661b49440SChris Mason /* 256761b49440SChris Mason * endios are largely parallel and should have a very 256861b49440SChris Mason * low idle thresh 256961b49440SChris Mason */ 257061b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2571b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 257253b381b3SDavid Woodhouse fs_info->endio_raid56_workers.idle_thresh = 4; 257353b381b3SDavid Woodhouse fs_info->rmw_workers.idle_thresh = 2; 2574b51912c9SChris Mason 25759042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 25769042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 257790519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 25789042846bSChris Mason 25790dc3b84aSJosef Bacik /* 25800dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 25810dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 25820dc3b84aSJosef Bacik */ 25830dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 25840dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 25850dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 25860dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 25870dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 25880dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 25890dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 259053b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->rmw_workers); 259153b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->endio_raid56_workers); 25920dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 25930dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 25940dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 25950dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 25960dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 25970dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 25988ccf6f19SMiao Xie ret |= btrfs_start_workers(&fs_info->flush_workers); 25992f232036SJan Schmidt ret |= btrfs_start_workers(&fs_info->qgroup_rescan_workers); 26000dc3b84aSJosef Bacik if (ret) { 2601fed425c7SIlya Dryomov err = -ENOMEM; 26020dc3b84aSJosef Bacik goto fail_sb_buffer; 26030dc3b84aSJosef Bacik } 26044543df7eSChris Mason 26054575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2606c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2607c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 26084575c9ccSChris Mason 2609db94535dSChris Mason tree_root->nodesize = nodesize; 2610db94535dSChris Mason tree_root->leafsize = leafsize; 2611db94535dSChris Mason tree_root->sectorsize = sectorsize; 261287ee04ebSChris Mason tree_root->stripesize = stripesize; 2613a061fc8dSChris Mason 2614a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2615a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2616db94535dSChris Mason 26173cae210fSQu Wenruo if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) { 2618d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 261939279cc3SChris Mason goto fail_sb_buffer; 262039279cc3SChris Mason } 262119c00ddcSChris Mason 26228d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 26238d082fb7SLiu Bo printk(KERN_WARNING "btrfs: Incompatible sector size(%lu) " 26248d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2625941b2ddfSKeith Mannthey goto fail_sb_buffer; 2626941b2ddfSKeith Mannthey } 2627941b2ddfSKeith Mannthey 2628925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2629e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2630925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 263184eed90fSChris Mason if (ret) { 2632d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2633d397712bSChris Mason "array on %s\n", sb->s_id); 26345d4f98a2SYan Zheng goto fail_sb_buffer; 263584eed90fSChris Mason } 26360b86a832SChris Mason 26370b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 26380b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 263984234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 26400b86a832SChris Mason 26410b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 26420b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 26430b86a832SChris Mason 26440b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 26450b86a832SChris Mason btrfs_super_chunk_root(disk_super), 264684234f3aSYan Zheng blocksize, generation); 2647416bc658SJosef Bacik if (!chunk_root->node || 2648416bc658SJosef Bacik !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 264983121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 265083121942SDavid Woodhouse sb->s_id); 2651af31f5e5SChris Mason goto fail_tree_roots; 265283121942SDavid Woodhouse } 26535d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 26545d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 26550b86a832SChris Mason 2656e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2657b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE); 2658e17cade2SChris Mason 26590b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 26602b82032cSYan Zheng if (ret) { 2661d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2662d397712bSChris Mason sb->s_id); 2663af31f5e5SChris Mason goto fail_tree_roots; 26642b82032cSYan Zheng } 26650b86a832SChris Mason 26668dabb742SStefan Behrens /* 26678dabb742SStefan Behrens * keep the device that is marked to be the target device for the 26688dabb742SStefan Behrens * dev_replace procedure 26698dabb742SStefan Behrens */ 26708dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2671dfe25020SChris Mason 2672a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2673a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2674a6b0d5c8SChris Mason sb->s_id); 2675a6b0d5c8SChris Mason goto fail_tree_roots; 2676a6b0d5c8SChris Mason } 2677a6b0d5c8SChris Mason 2678af31f5e5SChris Mason retry_root_backup: 2679db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2680db94535dSChris Mason btrfs_super_root_level(disk_super)); 268184234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 26820b86a832SChris Mason 2683e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2684db94535dSChris Mason btrfs_super_root(disk_super), 268584234f3aSYan Zheng blocksize, generation); 2686af31f5e5SChris Mason if (!tree_root->node || 2687af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 268883121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 268983121942SDavid Woodhouse sb->s_id); 2690af31f5e5SChris Mason 2691af31f5e5SChris Mason goto recovery_tree_root; 269283121942SDavid Woodhouse } 2693af31f5e5SChris Mason 26945d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 26955d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 269669e9c6c6SStefan Behrens btrfs_set_root_refs(&tree_root->root_item, 1); 2697db94535dSChris Mason 2698cb517eabSMiao Xie location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 2699cb517eabSMiao Xie location.type = BTRFS_ROOT_ITEM_KEY; 2700cb517eabSMiao Xie location.offset = 0; 2701cb517eabSMiao Xie 2702cb517eabSMiao Xie extent_root = btrfs_read_tree_root(tree_root, &location); 2703cb517eabSMiao Xie if (IS_ERR(extent_root)) { 2704cb517eabSMiao Xie ret = PTR_ERR(extent_root); 2705af31f5e5SChris Mason goto recovery_tree_root; 2706cb517eabSMiao Xie } 27070b86a832SChris Mason extent_root->track_dirty = 1; 2708cb517eabSMiao Xie fs_info->extent_root = extent_root; 27090b86a832SChris Mason 2710cb517eabSMiao Xie location.objectid = BTRFS_DEV_TREE_OBJECTID; 2711cb517eabSMiao Xie dev_root = btrfs_read_tree_root(tree_root, &location); 2712cb517eabSMiao Xie if (IS_ERR(dev_root)) { 2713cb517eabSMiao Xie ret = PTR_ERR(dev_root); 2714af31f5e5SChris Mason goto recovery_tree_root; 2715cb517eabSMiao Xie } 27165d4f98a2SYan Zheng dev_root->track_dirty = 1; 2717cb517eabSMiao Xie fs_info->dev_root = dev_root; 2718cb517eabSMiao Xie btrfs_init_devices_late(fs_info); 27193768f368SChris Mason 2720cb517eabSMiao Xie location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2721cb517eabSMiao Xie csum_root = btrfs_read_tree_root(tree_root, &location); 2722cb517eabSMiao Xie if (IS_ERR(csum_root)) { 2723cb517eabSMiao Xie ret = PTR_ERR(csum_root); 2724af31f5e5SChris Mason goto recovery_tree_root; 2725cb517eabSMiao Xie } 2726d20f7043SChris Mason csum_root->track_dirty = 1; 2727cb517eabSMiao Xie fs_info->csum_root = csum_root; 2728d20f7043SChris Mason 2729cb517eabSMiao Xie location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2730cb517eabSMiao Xie quota_root = btrfs_read_tree_root(tree_root, &location); 2731cb517eabSMiao Xie if (!IS_ERR(quota_root)) { 2732bcef60f2SArne Jansen quota_root->track_dirty = 1; 2733bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2734bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2735cb517eabSMiao Xie fs_info->quota_root = quota_root; 2736bcef60f2SArne Jansen } 2737bcef60f2SArne Jansen 2738f7a81ea4SStefan Behrens location.objectid = BTRFS_UUID_TREE_OBJECTID; 2739f7a81ea4SStefan Behrens uuid_root = btrfs_read_tree_root(tree_root, &location); 2740f7a81ea4SStefan Behrens if (IS_ERR(uuid_root)) { 2741f7a81ea4SStefan Behrens ret = PTR_ERR(uuid_root); 2742f7a81ea4SStefan Behrens if (ret != -ENOENT) 2743f7a81ea4SStefan Behrens goto recovery_tree_root; 2744f7a81ea4SStefan Behrens create_uuid_tree = true; 274570f80175SStefan Behrens check_uuid_tree = false; 2746f7a81ea4SStefan Behrens } else { 2747f7a81ea4SStefan Behrens uuid_root->track_dirty = 1; 2748f7a81ea4SStefan Behrens fs_info->uuid_root = uuid_root; 274970f80175SStefan Behrens create_uuid_tree = false; 275070f80175SStefan Behrens check_uuid_tree = 275170f80175SStefan Behrens generation != btrfs_super_uuid_tree_generation(disk_super); 2752f7a81ea4SStefan Behrens } 2753f7a81ea4SStefan Behrens 27548929ecfaSYan, Zheng fs_info->generation = generation; 27558929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 27568929ecfaSYan, Zheng 275768310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 275868310a5eSIlya Dryomov if (ret) { 275968310a5eSIlya Dryomov printk(KERN_WARNING "btrfs: failed to recover balance\n"); 276068310a5eSIlya Dryomov goto fail_block_groups; 276168310a5eSIlya Dryomov } 276268310a5eSIlya Dryomov 2763733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2764733f4fbbSStefan Behrens if (ret) { 2765733f4fbbSStefan Behrens printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2766733f4fbbSStefan Behrens ret); 2767733f4fbbSStefan Behrens goto fail_block_groups; 2768733f4fbbSStefan Behrens } 2769733f4fbbSStefan Behrens 27708dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 27718dabb742SStefan Behrens if (ret) { 27728dabb742SStefan Behrens pr_err("btrfs: failed to init dev_replace: %d\n", ret); 27738dabb742SStefan Behrens goto fail_block_groups; 27748dabb742SStefan Behrens } 27758dabb742SStefan Behrens 27768dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 27778dabb742SStefan Behrens 2778c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2779c59021f8Sliubo if (ret) { 2780c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2781c59021f8Sliubo goto fail_block_groups; 2782c59021f8Sliubo } 2783c59021f8Sliubo 27841b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 27851b1d1f66SJosef Bacik if (ret) { 27861b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 27871b1d1f66SJosef Bacik goto fail_block_groups; 27881b1d1f66SJosef Bacik } 27895af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 27905af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2791292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2792292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2793292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2794292fd7fcSStefan Behrens printk(KERN_WARNING 2795292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable mount is not allowed\n"); 2796292fd7fcSStefan Behrens goto fail_block_groups; 2797292fd7fcSStefan Behrens } 27989078a3e1SChris Mason 2799a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2800a74a4b97SChris Mason "btrfs-cleaner"); 280157506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 28021b1d1f66SJosef Bacik goto fail_block_groups; 2803a74a4b97SChris Mason 2804a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2805a74a4b97SChris Mason tree_root, 2806a74a4b97SChris Mason "btrfs-transaction"); 280757506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 28083f157a2fSChris Mason goto fail_cleaner; 2809a74a4b97SChris Mason 2810c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2811c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2812c289811cSChris Mason !fs_info->fs_devices->rotating) { 2813c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2814c289811cSChris Mason "mode\n"); 2815c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2816c289811cSChris Mason } 2817c289811cSChris Mason 281821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 281921adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 282021adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 282121adbd5cSStefan Behrens btrfs_test_opt(tree_root, 282221adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 282321adbd5cSStefan Behrens 1 : 0, 282421adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 282521adbd5cSStefan Behrens if (ret) 282621adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 282721adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 282821adbd5cSStefan Behrens } 282921adbd5cSStefan Behrens #endif 2830bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2831bcef60f2SArne Jansen if (ret) 2832bcef60f2SArne Jansen goto fail_trans_kthread; 283321adbd5cSStefan Behrens 2834acce952bSliubo /* do not make disk changes in broken FS */ 283568ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2836e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2837d18a2c44SChris Mason 28387c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2839d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2840d397712bSChris Mason "on RO media\n"); 28417c2ca468SChris Mason err = -EIO; 2842bcef60f2SArne Jansen goto fail_qgroup; 28437c2ca468SChris Mason } 2844e02119d5SChris Mason blocksize = 2845e02119d5SChris Mason btrfs_level_size(tree_root, 2846e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2847e02119d5SChris Mason 28486f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2849676e4c86SDan Carpenter if (!log_tree_root) { 2850676e4c86SDan Carpenter err = -ENOMEM; 2851bcef60f2SArne Jansen goto fail_qgroup; 2852676e4c86SDan Carpenter } 2853e02119d5SChris Mason 2854e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2855e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2856e02119d5SChris Mason 2857e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 285884234f3aSYan Zheng blocksize, 285984234f3aSYan Zheng generation + 1); 2860416bc658SJosef Bacik if (!log_tree_root->node || 2861416bc658SJosef Bacik !extent_buffer_uptodate(log_tree_root->node)) { 2862416bc658SJosef Bacik printk(KERN_ERR "btrfs: failed to read log tree\n"); 2863416bc658SJosef Bacik free_extent_buffer(log_tree_root->node); 2864416bc658SJosef Bacik kfree(log_tree_root); 2865416bc658SJosef Bacik goto fail_trans_kthread; 2866416bc658SJosef Bacik } 286779787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2868e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 286979787eaaSJeff Mahoney if (ret) { 287079787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 287179787eaaSJeff Mahoney "Failed to recover log tree"); 287279787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 287379787eaaSJeff Mahoney kfree(log_tree_root); 287479787eaaSJeff Mahoney goto fail_trans_kthread; 287579787eaaSJeff Mahoney } 2876e556ce2cSYan Zheng 2877e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2878e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 287979787eaaSJeff Mahoney if (ret) 288079787eaaSJeff Mahoney goto fail_trans_kthread; 2881e556ce2cSYan Zheng } 2882e02119d5SChris Mason } 28831a40e23bSZheng Yan 288476dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 288579787eaaSJeff Mahoney if (ret) 288679787eaaSJeff Mahoney goto fail_trans_kthread; 288776dda93cSYan, Zheng 28887c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2889d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 289044c44af2SIlya Dryomov if (ret) 289144c44af2SIlya Dryomov goto fail_trans_kthread; 2892d68fc57bSYan, Zheng 28935d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2894d7ce5843SMiao Xie if (ret < 0) { 2895d7ce5843SMiao Xie printk(KERN_WARNING 2896d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2897d7ce5843SMiao Xie err = -EINVAL; 2898bcef60f2SArne Jansen goto fail_qgroup; 2899d7ce5843SMiao Xie } 29007c2ca468SChris Mason } 29011a40e23bSZheng Yan 29023de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 29033de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2904cb517eabSMiao Xie location.offset = 0; 29053de4586cSChris Mason 29063de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 29073140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 29083140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2909bcef60f2SArne Jansen goto fail_qgroup; 29103140c9a3SDan Carpenter } 2911c289811cSChris Mason 29122b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 29132b6ba629SIlya Dryomov return 0; 29142b6ba629SIlya Dryomov 2915e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 29162b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 29172b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 29182b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 29192b6ba629SIlya Dryomov close_ctree(tree_root); 29202b6ba629SIlya Dryomov return ret; 29212b6ba629SIlya Dryomov } 2922e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 292359641015SIlya Dryomov 29242b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 29252b6ba629SIlya Dryomov if (ret) { 29262b6ba629SIlya Dryomov printk(KERN_WARNING "btrfs: failed to resume balance\n"); 292766b4ffd1SJosef Bacik close_ctree(tree_root); 29282b6ba629SIlya Dryomov return ret; 2929e3acc2a6SJosef Bacik } 2930e3acc2a6SJosef Bacik 29318dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 29328dabb742SStefan Behrens if (ret) { 29338dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 29348dabb742SStefan Behrens close_ctree(tree_root); 29358dabb742SStefan Behrens return ret; 29368dabb742SStefan Behrens } 29378dabb742SStefan Behrens 2938b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 2939b382a324SJan Schmidt 2940f7a81ea4SStefan Behrens if (create_uuid_tree) { 2941f7a81ea4SStefan Behrens pr_info("btrfs: creating UUID tree\n"); 2942f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 2943f7a81ea4SStefan Behrens if (ret) { 2944f7a81ea4SStefan Behrens pr_warn("btrfs: failed to create the UUID tree %d\n", 2945f7a81ea4SStefan Behrens ret); 2946f7a81ea4SStefan Behrens close_ctree(tree_root); 2947f7a81ea4SStefan Behrens return ret; 2948f7a81ea4SStefan Behrens } 2949f420ee1eSStefan Behrens } else if (check_uuid_tree || 2950f420ee1eSStefan Behrens btrfs_test_opt(tree_root, RESCAN_UUID_TREE)) { 295170f80175SStefan Behrens pr_info("btrfs: checking UUID tree\n"); 295270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info); 295370f80175SStefan Behrens if (ret) { 295470f80175SStefan Behrens pr_warn("btrfs: failed to check the UUID tree %d\n", 295570f80175SStefan Behrens ret); 295670f80175SStefan Behrens close_ctree(tree_root); 295770f80175SStefan Behrens return ret; 295870f80175SStefan Behrens } 295970f80175SStefan Behrens } else { 296070f80175SStefan Behrens fs_info->update_uuid_tree_gen = 1; 2961f7a81ea4SStefan Behrens } 2962f7a81ea4SStefan Behrens 2963ad2b2c80SAl Viro return 0; 296439279cc3SChris Mason 2965bcef60f2SArne Jansen fail_qgroup: 2966bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 29677c2ca468SChris Mason fail_trans_kthread: 29687c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 296954067ae9SJosef Bacik btrfs_cleanup_transaction(fs_info->tree_root); 29707b5ff90eSJosef Bacik del_fs_roots(fs_info); 29713f157a2fSChris Mason fail_cleaner: 2972a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 29737c2ca468SChris Mason 29747c2ca468SChris Mason /* 29757c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 29767c2ca468SChris Mason * kthreads 29777c2ca468SChris Mason */ 29787c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 29797c2ca468SChris Mason 29801b1d1f66SJosef Bacik fail_block_groups: 298154067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 29821b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2983af31f5e5SChris Mason 2984af31f5e5SChris Mason fail_tree_roots: 2985af31f5e5SChris Mason free_root_pointers(fs_info, 1); 29862b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2987af31f5e5SChris Mason 298839279cc3SChris Mason fail_sb_buffer: 29897abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 299016cdcec7SMiao Xie fail_alloc: 29914543df7eSChris Mason fail_iput: 2992586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2993586e46e2SIlya Dryomov 29944543df7eSChris Mason iput(fs_info->btree_inode); 2995963d678bSMiao Xie fail_delalloc_bytes: 2996963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 2997e2d84521SMiao Xie fail_dirty_metadata_bytes: 2998e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 2999ad081f14SJens Axboe fail_bdi: 30007e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 300176dda93cSYan, Zheng fail_srcu: 300276dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 30037e662854SQinghuang Feng fail: 300453b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 3005586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 3006ad2b2c80SAl Viro return err; 3007af31f5e5SChris Mason 3008af31f5e5SChris Mason recovery_tree_root: 3009af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 3010af31f5e5SChris Mason goto fail_tree_roots; 3011af31f5e5SChris Mason 3012af31f5e5SChris Mason free_root_pointers(fs_info, 0); 3013af31f5e5SChris Mason 3014af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 3015af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 3016af31f5e5SChris Mason 3017af31f5e5SChris Mason /* we can't trust the free space cache either */ 3018af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 3019af31f5e5SChris Mason 3020af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 3021af31f5e5SChris Mason &num_backups_tried, &backup_index); 3022af31f5e5SChris Mason if (ret == -1) 3023af31f5e5SChris Mason goto fail_block_groups; 3024af31f5e5SChris Mason goto retry_root_backup; 3025eb60ceacSChris Mason } 3026eb60ceacSChris Mason 3027f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 3028f2984462SChris Mason { 3029f2984462SChris Mason if (uptodate) { 3030f2984462SChris Mason set_buffer_uptodate(bh); 3031f2984462SChris Mason } else { 3032442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 3033442a4f63SStefan Behrens bh->b_private; 3034442a4f63SStefan Behrens 3035606686eeSJosef Bacik printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to " 3036606686eeSJosef Bacik "I/O error on %s\n", 3037606686eeSJosef Bacik rcu_str_deref(device->name)); 30381259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 30391259ab75SChris Mason * our own ways of dealing with the IO errors 30401259ab75SChris Mason */ 3041f2984462SChris Mason clear_buffer_uptodate(bh); 3042442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3043f2984462SChris Mason } 3044f2984462SChris Mason unlock_buffer(bh); 3045f2984462SChris Mason put_bh(bh); 3046f2984462SChris Mason } 3047f2984462SChris Mason 3048a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3049a512bbf8SYan Zheng { 3050a512bbf8SYan Zheng struct buffer_head *bh; 3051a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3052a512bbf8SYan Zheng struct btrfs_super_block *super; 3053a512bbf8SYan Zheng int i; 3054a512bbf8SYan Zheng u64 transid = 0; 3055a512bbf8SYan Zheng u64 bytenr; 3056a512bbf8SYan Zheng 3057a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3058a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3059a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3060a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3061a512bbf8SYan Zheng */ 3062a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 3063a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 30648068a47eSAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= 30658068a47eSAnand Jain i_size_read(bdev->bd_inode)) 3066a512bbf8SYan Zheng break; 30678068a47eSAnand Jain bh = __bread(bdev, bytenr / 4096, 30688068a47eSAnand Jain BTRFS_SUPER_INFO_SIZE); 3069a512bbf8SYan Zheng if (!bh) 3070a512bbf8SYan Zheng continue; 3071a512bbf8SYan Zheng 3072a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3073a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 30743cae210fSQu Wenruo btrfs_super_magic(super) != BTRFS_MAGIC) { 3075a512bbf8SYan Zheng brelse(bh); 3076a512bbf8SYan Zheng continue; 3077a512bbf8SYan Zheng } 3078a512bbf8SYan Zheng 3079a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3080a512bbf8SYan Zheng brelse(latest); 3081a512bbf8SYan Zheng latest = bh; 3082a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3083a512bbf8SYan Zheng } else { 3084a512bbf8SYan Zheng brelse(bh); 3085a512bbf8SYan Zheng } 3086a512bbf8SYan Zheng } 3087a512bbf8SYan Zheng return latest; 3088a512bbf8SYan Zheng } 3089a512bbf8SYan Zheng 30904eedeb75SHisashi Hifumi /* 30914eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 30924eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 30934eedeb75SHisashi Hifumi * we write are pinned. 30944eedeb75SHisashi Hifumi * 30954eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 30964eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 30974eedeb75SHisashi Hifumi * many supers on this one device should be written. 30984eedeb75SHisashi Hifumi * 30994eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 31004eedeb75SHisashi Hifumi */ 3101a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3102a512bbf8SYan Zheng struct btrfs_super_block *sb, 3103a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 3104a512bbf8SYan Zheng { 3105a512bbf8SYan Zheng struct buffer_head *bh; 3106a512bbf8SYan Zheng int i; 3107a512bbf8SYan Zheng int ret; 3108a512bbf8SYan Zheng int errors = 0; 3109a512bbf8SYan Zheng u32 crc; 3110a512bbf8SYan Zheng u64 bytenr; 3111a512bbf8SYan Zheng 3112a512bbf8SYan Zheng if (max_mirrors == 0) 3113a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3114a512bbf8SYan Zheng 3115a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3116a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3117a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 3118a512bbf8SYan Zheng break; 3119a512bbf8SYan Zheng 3120a512bbf8SYan Zheng if (wait) { 3121a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3122a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3123634554dcSJosef Bacik if (!bh) { 3124634554dcSJosef Bacik errors++; 3125634554dcSJosef Bacik continue; 3126634554dcSJosef Bacik } 3127a512bbf8SYan Zheng wait_on_buffer(bh); 31284eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 31294eedeb75SHisashi Hifumi errors++; 31304eedeb75SHisashi Hifumi 31314eedeb75SHisashi Hifumi /* drop our reference */ 31324eedeb75SHisashi Hifumi brelse(bh); 31334eedeb75SHisashi Hifumi 31344eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3135a512bbf8SYan Zheng brelse(bh); 3136a512bbf8SYan Zheng continue; 3137a512bbf8SYan Zheng } else { 3138a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3139a512bbf8SYan Zheng 3140a512bbf8SYan Zheng crc = ~(u32)0; 3141b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3142a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3143a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3144a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3145a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3146a512bbf8SYan Zheng 31474eedeb75SHisashi Hifumi /* 31484eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 31494eedeb75SHisashi Hifumi * caller 31504eedeb75SHisashi Hifumi */ 3151a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3152a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3153634554dcSJosef Bacik if (!bh) { 3154634554dcSJosef Bacik printk(KERN_ERR "btrfs: couldn't get super " 3155634554dcSJosef Bacik "buffer head for bytenr %Lu\n", bytenr); 3156634554dcSJosef Bacik errors++; 3157634554dcSJosef Bacik continue; 3158634554dcSJosef Bacik } 3159634554dcSJosef Bacik 3160a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3161a512bbf8SYan Zheng 31624eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3163a512bbf8SYan Zheng get_bh(bh); 31644eedeb75SHisashi Hifumi 31654eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3166a512bbf8SYan Zheng lock_buffer(bh); 3167a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3168442a4f63SStefan Behrens bh->b_private = device; 3169a512bbf8SYan Zheng } 3170a512bbf8SYan Zheng 3171387125fcSChris Mason /* 3172387125fcSChris Mason * we fua the first super. The others we allow 3173387125fcSChris Mason * to go down lazy. 3174387125fcSChris Mason */ 317521adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 31764eedeb75SHisashi Hifumi if (ret) 3177a512bbf8SYan Zheng errors++; 3178a512bbf8SYan Zheng } 3179a512bbf8SYan Zheng return errors < i ? 0 : -1; 3180a512bbf8SYan Zheng } 3181a512bbf8SYan Zheng 3182387125fcSChris Mason /* 3183387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3184387125fcSChris Mason * for the barrier when it is done 3185387125fcSChris Mason */ 3186387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 3187387125fcSChris Mason { 3188387125fcSChris Mason if (err) { 3189387125fcSChris Mason if (err == -EOPNOTSUPP) 3190387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 3191387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 3192387125fcSChris Mason } 3193387125fcSChris Mason if (bio->bi_private) 3194387125fcSChris Mason complete(bio->bi_private); 3195387125fcSChris Mason bio_put(bio); 3196387125fcSChris Mason } 3197387125fcSChris Mason 3198387125fcSChris Mason /* 3199387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3200387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3201387125fcSChris Mason * 3202387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3203387125fcSChris Mason * capable 3204387125fcSChris Mason */ 3205387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3206387125fcSChris Mason { 3207387125fcSChris Mason struct bio *bio; 3208387125fcSChris Mason int ret = 0; 3209387125fcSChris Mason 3210387125fcSChris Mason if (device->nobarriers) 3211387125fcSChris Mason return 0; 3212387125fcSChris Mason 3213387125fcSChris Mason if (wait) { 3214387125fcSChris Mason bio = device->flush_bio; 3215387125fcSChris Mason if (!bio) 3216387125fcSChris Mason return 0; 3217387125fcSChris Mason 3218387125fcSChris Mason wait_for_completion(&device->flush_wait); 3219387125fcSChris Mason 3220387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 3221606686eeSJosef Bacik printk_in_rcu("btrfs: disabling barriers on dev %s\n", 3222606686eeSJosef Bacik rcu_str_deref(device->name)); 3223387125fcSChris Mason device->nobarriers = 1; 32245af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 3225387125fcSChris Mason ret = -EIO; 3226442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3227442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3228387125fcSChris Mason } 3229387125fcSChris Mason 3230387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3231387125fcSChris Mason bio_put(bio); 3232387125fcSChris Mason device->flush_bio = NULL; 3233387125fcSChris Mason 3234387125fcSChris Mason return ret; 3235387125fcSChris Mason } 3236387125fcSChris Mason 3237387125fcSChris Mason /* 3238387125fcSChris Mason * one reference for us, and we leave it for the 3239387125fcSChris Mason * caller 3240387125fcSChris Mason */ 32419c017abcSJesper Juhl device->flush_bio = NULL; 32429be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3243387125fcSChris Mason if (!bio) 3244387125fcSChris Mason return -ENOMEM; 3245387125fcSChris Mason 3246387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3247387125fcSChris Mason bio->bi_bdev = device->bdev; 3248387125fcSChris Mason init_completion(&device->flush_wait); 3249387125fcSChris Mason bio->bi_private = &device->flush_wait; 3250387125fcSChris Mason device->flush_bio = bio; 3251387125fcSChris Mason 3252387125fcSChris Mason bio_get(bio); 325321adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 3254387125fcSChris Mason 3255387125fcSChris Mason return 0; 3256387125fcSChris Mason } 3257387125fcSChris Mason 3258387125fcSChris Mason /* 3259387125fcSChris Mason * send an empty flush down to each device in parallel, 3260387125fcSChris Mason * then wait for them 3261387125fcSChris Mason */ 3262387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3263387125fcSChris Mason { 3264387125fcSChris Mason struct list_head *head; 3265387125fcSChris Mason struct btrfs_device *dev; 32665af3e8ccSStefan Behrens int errors_send = 0; 32675af3e8ccSStefan Behrens int errors_wait = 0; 3268387125fcSChris Mason int ret; 3269387125fcSChris Mason 3270387125fcSChris Mason /* send down all the barriers */ 3271387125fcSChris Mason head = &info->fs_devices->devices; 3272387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3273387125fcSChris Mason if (!dev->bdev) { 32745af3e8ccSStefan Behrens errors_send++; 3275387125fcSChris Mason continue; 3276387125fcSChris Mason } 3277387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3278387125fcSChris Mason continue; 3279387125fcSChris Mason 3280387125fcSChris Mason ret = write_dev_flush(dev, 0); 3281387125fcSChris Mason if (ret) 32825af3e8ccSStefan Behrens errors_send++; 3283387125fcSChris Mason } 3284387125fcSChris Mason 3285387125fcSChris Mason /* wait for all the barriers */ 3286387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3287387125fcSChris Mason if (!dev->bdev) { 32885af3e8ccSStefan Behrens errors_wait++; 3289387125fcSChris Mason continue; 3290387125fcSChris Mason } 3291387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3292387125fcSChris Mason continue; 3293387125fcSChris Mason 3294387125fcSChris Mason ret = write_dev_flush(dev, 1); 3295387125fcSChris Mason if (ret) 32965af3e8ccSStefan Behrens errors_wait++; 3297387125fcSChris Mason } 32985af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 32995af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3300387125fcSChris Mason return -EIO; 3301387125fcSChris Mason return 0; 3302387125fcSChris Mason } 3303387125fcSChris Mason 33045af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 33055af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 33065af3e8ccSStefan Behrens { 33075af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 33085af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 33095af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 33105af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 33115af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 33125af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 33135af3e8ccSStefan Behrens int num_types = 4; 33145af3e8ccSStefan Behrens int i; 33155af3e8ccSStefan Behrens int c; 33165af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 33175af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 33185af3e8ccSStefan Behrens 33195af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 33205af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 33215af3e8ccSStefan Behrens 33225af3e8ccSStefan Behrens sinfo = NULL; 33235af3e8ccSStefan Behrens rcu_read_lock(); 33245af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 33255af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 33265af3e8ccSStefan Behrens sinfo = tmp; 33275af3e8ccSStefan Behrens break; 33285af3e8ccSStefan Behrens } 33295af3e8ccSStefan Behrens } 33305af3e8ccSStefan Behrens rcu_read_unlock(); 33315af3e8ccSStefan Behrens 33325af3e8ccSStefan Behrens if (!sinfo) 33335af3e8ccSStefan Behrens continue; 33345af3e8ccSStefan Behrens 33355af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 33365af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 33375af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 33385af3e8ccSStefan Behrens u64 flags; 33395af3e8ccSStefan Behrens 33405af3e8ccSStefan Behrens btrfs_get_block_group_info( 33415af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 33425af3e8ccSStefan Behrens if (space.total_bytes == 0 || 33435af3e8ccSStefan Behrens space.used_bytes == 0) 33445af3e8ccSStefan Behrens continue; 33455af3e8ccSStefan Behrens flags = space.flags; 33465af3e8ccSStefan Behrens /* 33475af3e8ccSStefan Behrens * return 33485af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 33495af3e8ccSStefan Behrens * any of metadata, system or data, else 33505af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 33515af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 33525af3e8ccSStefan Behrens * are used, else 33535af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 33545af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 33555af3e8ccSStefan Behrens * configured and more than 33565af3e8ccSStefan Behrens * 2 mirrors are used. 33575af3e8ccSStefan Behrens */ 33585af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 33595af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 33605af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 33615af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 33625af3e8ccSStefan Behrens == 0))) 33635af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 336453b381b3SDavid Woodhouse else if (num_tolerated_disk_barrier_failures > 1) { 336553b381b3SDavid Woodhouse if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 336653b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5 | 336753b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID10)) { 33685af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 336953b381b3SDavid Woodhouse } else if (flags & 337015b0a89dSHenrik Nordvik BTRFS_BLOCK_GROUP_RAID6) { 337153b381b3SDavid Woodhouse num_tolerated_disk_barrier_failures = 2; 337253b381b3SDavid Woodhouse } 337353b381b3SDavid Woodhouse } 33745af3e8ccSStefan Behrens } 33755af3e8ccSStefan Behrens } 33765af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 33775af3e8ccSStefan Behrens } 33785af3e8ccSStefan Behrens 33795af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 33805af3e8ccSStefan Behrens } 33815af3e8ccSStefan Behrens 338248a3b636SEric Sandeen static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3383f2984462SChris Mason { 3384e5e9a520SChris Mason struct list_head *head; 3385f2984462SChris Mason struct btrfs_device *dev; 3386a061fc8dSChris Mason struct btrfs_super_block *sb; 3387f2984462SChris Mason struct btrfs_dev_item *dev_item; 3388f2984462SChris Mason int ret; 3389f2984462SChris Mason int do_barriers; 3390a236aed1SChris Mason int max_errors; 3391a236aed1SChris Mason int total_errors = 0; 3392a061fc8dSChris Mason u64 flags; 3393f2984462SChris Mason 3394f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3395af31f5e5SChris Mason backup_super_roots(root->fs_info); 3396f2984462SChris Mason 33976c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3398a061fc8dSChris Mason dev_item = &sb->dev_item; 3399e5e9a520SChris Mason 3400174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3401e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3402d7306801SFilipe David Borba Manana max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3403387125fcSChris Mason 34045af3e8ccSStefan Behrens if (do_barriers) { 34055af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 34065af3e8ccSStefan Behrens if (ret) { 34075af3e8ccSStefan Behrens mutex_unlock( 34085af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 34095af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 34105af3e8ccSStefan Behrens "errors while submitting device barriers."); 34115af3e8ccSStefan Behrens return ret; 34125af3e8ccSStefan Behrens } 34135af3e8ccSStefan Behrens } 3414387125fcSChris Mason 34151f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3416dfe25020SChris Mason if (!dev->bdev) { 3417dfe25020SChris Mason total_errors++; 3418dfe25020SChris Mason continue; 3419dfe25020SChris Mason } 34202b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3421dfe25020SChris Mason continue; 3422dfe25020SChris Mason 34232b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3424a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3425a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 3426a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 3427a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 3428a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3429a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3430a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3431a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 34322b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3433a512bbf8SYan Zheng 3434a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3435a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3436f2984462SChris Mason 3437a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3438a236aed1SChris Mason if (ret) 3439a236aed1SChris Mason total_errors++; 3440f2984462SChris Mason } 3441a236aed1SChris Mason if (total_errors > max_errors) { 3442d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 3443d397712bSChris Mason total_errors); 3444a724b436SStefan Behrens mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 344579787eaaSJeff Mahoney 34469d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */ 34479d565ba4SStefan Behrens btrfs_error(root->fs_info, -EIO, 34489d565ba4SStefan Behrens "%d errors while writing supers", total_errors); 34499d565ba4SStefan Behrens return -EIO; 3450a236aed1SChris Mason } 3451f2984462SChris Mason 3452a512bbf8SYan Zheng total_errors = 0; 34531f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3454dfe25020SChris Mason if (!dev->bdev) 3455dfe25020SChris Mason continue; 34562b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3457dfe25020SChris Mason continue; 3458dfe25020SChris Mason 3459a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3460a512bbf8SYan Zheng if (ret) 34611259ab75SChris Mason total_errors++; 3462f2984462SChris Mason } 3463174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3464a236aed1SChris Mason if (total_errors > max_errors) { 346579787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 346679787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 346779787eaaSJeff Mahoney return -EIO; 3468a236aed1SChris Mason } 3469f2984462SChris Mason return 0; 3470f2984462SChris Mason } 3471f2984462SChris Mason 3472a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3473a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3474cfaa7295SChris Mason { 3475e66f709bSChris Mason int ret; 34762c90e5d6SChris Mason 3477a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 34785f39d397SChris Mason return ret; 3479cfaa7295SChris Mason } 3480cfaa7295SChris Mason 3481cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3482cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3483cb517eabSMiao Xie struct btrfs_root *root) 34842619ba1fSChris Mason { 34854df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 34862619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 34872619ba1fSChris Mason (unsigned long)root->root_key.objectid); 34884df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 348976dda93cSYan, Zheng 349076dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 349176dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 349276dda93cSYan, Zheng 3493d7634482SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 34943321719eSLiu Bo btrfs_free_log(NULL, root); 34953321719eSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 34963321719eSLiu Bo } 34973321719eSLiu Bo 3498581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3499581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 35004df27c4dSYan, Zheng free_fs_root(root); 35014df27c4dSYan, Zheng } 35024df27c4dSYan, Zheng 35034df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 35044df27c4dSYan, Zheng { 350582d5902dSLi Zefan iput(root->cache_inode); 35064df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 35071cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 35081cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 35090ee5dc67SAl Viro if (root->anon_dev) 35100ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 35115f39d397SChris Mason free_extent_buffer(root->node); 35125f39d397SChris Mason free_extent_buffer(root->commit_root); 3513581bb050SLi Zefan kfree(root->free_ino_ctl); 3514581bb050SLi Zefan kfree(root->free_ino_pinned); 351558176a96SJosef Bacik kfree(root->name); 3516b0feb9d9SMiao Xie btrfs_put_fs_root(root); 35172619ba1fSChris Mason } 35182619ba1fSChris Mason 3519cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3520cb517eabSMiao Xie { 3521cb517eabSMiao Xie free_fs_root(root); 3522eb60ceacSChris Mason } 3523eb60ceacSChris Mason 3524c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3525c146afadSYan Zheng { 3526c146afadSYan Zheng u64 root_objectid = 0; 3527c146afadSYan Zheng struct btrfs_root *gang[8]; 3528c146afadSYan Zheng int i; 3529c146afadSYan Zheng int ret; 3530c146afadSYan Zheng 3531c146afadSYan Zheng while (1) { 3532c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3533c146afadSYan Zheng (void **)gang, root_objectid, 3534c146afadSYan Zheng ARRAY_SIZE(gang)); 3535c146afadSYan Zheng if (!ret) 3536c146afadSYan Zheng break; 35375d4f98a2SYan Zheng 35385d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3539c146afadSYan Zheng for (i = 0; i < ret; i++) { 354066b4ffd1SJosef Bacik int err; 354166b4ffd1SJosef Bacik 3542c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 354366b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 354466b4ffd1SJosef Bacik if (err) 354566b4ffd1SJosef Bacik return err; 3546c146afadSYan Zheng } 3547c146afadSYan Zheng root_objectid++; 3548c146afadSYan Zheng } 3549c146afadSYan Zheng return 0; 3550c146afadSYan Zheng } 3551c146afadSYan Zheng 3552c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3553c146afadSYan Zheng { 3554c146afadSYan Zheng struct btrfs_trans_handle *trans; 3555c146afadSYan Zheng int ret; 3556c146afadSYan Zheng 3557c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 355824bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3559c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 35609d1a2a3aSDavid Sterba wake_up_process(root->fs_info->cleaner_kthread); 3561c71bf099SYan, Zheng 3562c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3563c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3564c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3565c71bf099SYan, Zheng 35667a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 35673612b495STsutomu Itoh if (IS_ERR(trans)) 35683612b495STsutomu Itoh return PTR_ERR(trans); 3569c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 357079787eaaSJeff Mahoney if (ret) 357179787eaaSJeff Mahoney return ret; 3572c146afadSYan Zheng /* run commit again to drop the original snapshot */ 35737a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 35743612b495STsutomu Itoh if (IS_ERR(trans)) 35753612b495STsutomu Itoh return PTR_ERR(trans); 357679787eaaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 357779787eaaSJeff Mahoney if (ret) 357879787eaaSJeff Mahoney return ret; 3579c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 358079787eaaSJeff Mahoney if (ret) { 358179787eaaSJeff Mahoney btrfs_error(root->fs_info, ret, 358279787eaaSJeff Mahoney "Failed to sync btree inode to disk."); 358379787eaaSJeff Mahoney return ret; 358479787eaaSJeff Mahoney } 3585c146afadSYan Zheng 3586a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3587c146afadSYan Zheng return ret; 3588c146afadSYan Zheng } 3589c146afadSYan Zheng 3590e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3591eb60ceacSChris Mason { 35920f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3593c146afadSYan Zheng int ret; 3594e089f05cSChris Mason 3595facda1e7SChris Mason fs_info->closing = 1; 3596a2135011SChris Mason smp_mb(); 3597a2135011SChris Mason 3598803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */ 3599803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 3600803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */ 3601803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 3602803b2f54SStefan Behrens 3603837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3604aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3605837d5b6eSIlya Dryomov 36068dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 36078dabb742SStefan Behrens 3608aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 36094cb5300bSChris Mason 36104cb5300bSChris Mason /* wait for any defraggers to finish */ 36114cb5300bSChris Mason wait_event(fs_info->transaction_wait, 36124cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 36134cb5300bSChris Mason 36144cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 361526176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 36164cb5300bSChris Mason 3617c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3618c146afadSYan Zheng ret = btrfs_commit_super(root); 3619d397712bSChris Mason if (ret) 3620d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3621c146afadSYan Zheng } 3622ed2ff2cbSChris Mason 362387533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 362468ce9682SStefan Behrens btrfs_error_commit_super(root); 3625acce952bSliubo 3626300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3627300e4f8aSJosef Bacik 3628e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3629e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 36308929ecfaSYan, Zheng 3631f25784b3SYan Zheng fs_info->closing = 2; 3632f25784b3SYan Zheng smp_mb(); 3633f25784b3SYan Zheng 3634bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3635bcef60f2SArne Jansen 3636963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3637963d678bSMiao Xie printk(KERN_INFO "btrfs: at unmount delalloc count %lld\n", 3638963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3639b0c68f8bSChris Mason } 364031153d81SYan Zheng 3641e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3642c146afadSYan Zheng 364313e6c37bSJosef Bacik btrfs_stop_all_workers(fs_info); 36442932505aSLiu Bo 36450f7d52f4SChris Mason del_fs_roots(fs_info); 3646d10c5f31SChris Mason 364713e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 36489ad6b7bcSChris Mason 364913e6c37bSJosef Bacik iput(fs_info->btree_inode); 3650d6bfde87SChris Mason 365121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 365221adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 365321adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 365421adbd5cSStefan Behrens #endif 365521adbd5cSStefan Behrens 3656dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 36570b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3658b248a415SChris Mason 3659e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3660963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 366104160088SChris Mason bdi_destroy(&fs_info->bdi); 366276dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 36630b86a832SChris Mason 366453b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 366553b381b3SDavid Woodhouse 36661cb048f5SFilipe David Borba Manana btrfs_free_block_rsv(root, root->orphan_block_rsv); 36671cb048f5SFilipe David Borba Manana root->orphan_block_rsv = NULL; 36681cb048f5SFilipe David Borba Manana 3669eb60ceacSChris Mason return 0; 3670eb60ceacSChris Mason } 3671eb60ceacSChris Mason 3672b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3673b9fab919SChris Mason int atomic) 3674ccd467d6SChris Mason { 36751259ab75SChris Mason int ret; 3676727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 36771259ab75SChris Mason 36780b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 36791259ab75SChris Mason if (!ret) 36801259ab75SChris Mason return ret; 36811259ab75SChris Mason 36821259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3683b9fab919SChris Mason parent_transid, atomic); 3684b9fab919SChris Mason if (ret == -EAGAIN) 3685b9fab919SChris Mason return ret; 36861259ab75SChris Mason return !ret; 36875f39d397SChris Mason } 36886702ed49SChris Mason 36895f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 36905f39d397SChris Mason { 36910b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 36925f39d397SChris Mason } 36935f39d397SChris Mason 36945f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 36955f39d397SChris Mason { 369606ea65a3SJosef Bacik struct btrfs_root *root; 36975f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3698b9473439SChris Mason int was_dirty; 3699b4ce94deSChris Mason 370006ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 370106ea65a3SJosef Bacik /* 370206ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests 370306ea65a3SJosef Bacik * enabled. Normal people shouldn't be marking dummy buffers as dirty 370406ea65a3SJosef Bacik * outside of the sanity tests. 370506ea65a3SJosef Bacik */ 370606ea65a3SJosef Bacik if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags))) 370706ea65a3SJosef Bacik return; 370806ea65a3SJosef Bacik #endif 370906ea65a3SJosef Bacik root = BTRFS_I(buf->pages[0]->mapping->host)->root; 3710b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 371131b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 371231b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3713d397712bSChris Mason "found %llu running %llu\n", 3714c1c9ff7cSGeert Uytterhoeven buf->start, transid, root->fs_info->generation); 37150b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3716e2d84521SMiao Xie if (!was_dirty) 3717e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3718e2d84521SMiao Xie buf->len, 3719e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 3720eb60ceacSChris Mason } 3721eb60ceacSChris Mason 3722b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3723b53d3f5dSLiu Bo int flush_delayed) 372435b7e476SChris Mason { 3725188de649SChris Mason /* 3726188de649SChris Mason * looks as though older kernels can get into trouble with 3727188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3728188de649SChris Mason */ 3729e2d84521SMiao Xie int ret; 3730d6bfde87SChris Mason 37316933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3732d6bfde87SChris Mason return; 3733d6bfde87SChris Mason 3734b53d3f5dSLiu Bo if (flush_delayed) 373516cdcec7SMiao Xie btrfs_balance_delayed_items(root); 373616cdcec7SMiao Xie 3737e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3738e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3739e2d84521SMiao Xie if (ret > 0) { 3740d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited( 3741d0e1d66bSNamjae Jeon root->fs_info->btree_inode->i_mapping); 374216cdcec7SMiao Xie } 374316cdcec7SMiao Xie return; 374416cdcec7SMiao Xie } 374516cdcec7SMiao Xie 3746b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 374716cdcec7SMiao Xie { 3748b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 374935b7e476SChris Mason } 3750b53d3f5dSLiu Bo 3751b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3752b53d3f5dSLiu Bo { 3753b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3754d6bfde87SChris Mason } 37556b80053dSChris Mason 3756ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 37576b80053dSChris Mason { 3758727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 37590b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 37606b80053dSChris Mason } 37610da5468fSChris Mason 3762fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3763acce952bSliubo int read_only) 3764acce952bSliubo { 37651104a885SDavid Sterba /* 37661104a885SDavid Sterba * Placeholder for checks 37671104a885SDavid Sterba */ 3768fcd1f065SDavid Sterba return 0; 3769fcd1f065SDavid Sterba } 3770fcd1f065SDavid Sterba 377148a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root) 3772acce952bSliubo { 3773acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3774acce952bSliubo btrfs_run_delayed_iputs(root); 3775acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3776acce952bSliubo 3777acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3778acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3779acce952bSliubo 3780acce952bSliubo /* cleanup FS via transaction */ 3781acce952bSliubo btrfs_cleanup_transaction(root); 3782acce952bSliubo } 3783acce952bSliubo 3784569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 3785569e0f35SJosef Bacik struct btrfs_root *root) 3786acce952bSliubo { 3787acce952bSliubo struct btrfs_inode *btrfs_inode; 3788acce952bSliubo struct list_head splice; 3789acce952bSliubo 3790acce952bSliubo INIT_LIST_HEAD(&splice); 3791acce952bSliubo 3792acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3793199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3794acce952bSliubo 3795569e0f35SJosef Bacik list_splice_init(&t->ordered_operations, &splice); 3796acce952bSliubo while (!list_empty(&splice)) { 3797acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3798acce952bSliubo ordered_operations); 3799acce952bSliubo 3800acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3801199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3802acce952bSliubo 3803acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3804b216cbfbSMiao Xie 3805199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3806acce952bSliubo } 3807acce952bSliubo 3808199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3809acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3810acce952bSliubo } 3811acce952bSliubo 3812143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3813acce952bSliubo { 3814acce952bSliubo struct btrfs_ordered_extent *ordered; 3815acce952bSliubo 3816199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 3817779880efSJosef Bacik /* 3818779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3819779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3820779880efSJosef Bacik */ 3821199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 3822779880efSJosef Bacik root_extent_list) 3823779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3824199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 3825199c2a9cSMiao Xie } 3826199c2a9cSMiao Xie 3827199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 3828199c2a9cSMiao Xie { 3829199c2a9cSMiao Xie struct btrfs_root *root; 3830199c2a9cSMiao Xie struct list_head splice; 3831199c2a9cSMiao Xie 3832199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 3833199c2a9cSMiao Xie 3834199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 3835199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 3836199c2a9cSMiao Xie while (!list_empty(&splice)) { 3837199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3838199c2a9cSMiao Xie ordered_root); 38391de2cfdeSJosef Bacik list_move_tail(&root->ordered_root, 38401de2cfdeSJosef Bacik &fs_info->ordered_roots); 3841199c2a9cSMiao Xie 3842199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 3843199c2a9cSMiao Xie 3844199c2a9cSMiao Xie cond_resched_lock(&fs_info->ordered_root_lock); 3845199c2a9cSMiao Xie } 3846199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 3847acce952bSliubo } 3848acce952bSliubo 384935a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3850acce952bSliubo struct btrfs_root *root) 3851acce952bSliubo { 3852acce952bSliubo struct rb_node *node; 3853acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3854acce952bSliubo struct btrfs_delayed_ref_node *ref; 3855acce952bSliubo int ret = 0; 3856acce952bSliubo 3857acce952bSliubo delayed_refs = &trans->delayed_refs; 3858acce952bSliubo 3859acce952bSliubo spin_lock(&delayed_refs->lock); 3860acce952bSliubo if (delayed_refs->num_entries == 0) { 3861cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3862acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3863acce952bSliubo return ret; 3864acce952bSliubo } 3865acce952bSliubo 3866b939d1abSJosef Bacik while ((node = rb_first(&delayed_refs->root)) != NULL) { 3867eb12db69SJosef Bacik struct btrfs_delayed_ref_head *head = NULL; 3868e78417d1SJosef Bacik bool pin_bytes = false; 3869acce952bSliubo 3870eb12db69SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3871acce952bSliubo atomic_set(&ref->refs, 1); 3872acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3873acce952bSliubo 3874acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3875b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3876b939d1abSJosef Bacik atomic_inc(&ref->refs); 387779787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3878b939d1abSJosef Bacik 3879b939d1abSJosef Bacik /* Need to wait for the delayed ref to run */ 3880acce952bSliubo mutex_lock(&head->mutex); 3881b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3882b939d1abSJosef Bacik btrfs_put_delayed_ref(ref); 3883b939d1abSJosef Bacik 3884e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3885b939d1abSJosef Bacik continue; 3886b939d1abSJosef Bacik } 3887b939d1abSJosef Bacik 388854067ae9SJosef Bacik if (head->must_insert_reserved) 3889e78417d1SJosef Bacik pin_bytes = true; 389078a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 3891acce952bSliubo delayed_refs->num_heads--; 3892acce952bSliubo if (list_empty(&head->cluster)) 3893acce952bSliubo delayed_refs->num_heads_ready--; 3894acce952bSliubo list_del_init(&head->cluster); 3895acce952bSliubo } 3896eb12db69SJosef Bacik 3897b939d1abSJosef Bacik ref->in_tree = 0; 3898b939d1abSJosef Bacik rb_erase(&ref->rb_node, &delayed_refs->root); 3899b939d1abSJosef Bacik delayed_refs->num_entries--; 3900acce952bSliubo spin_unlock(&delayed_refs->lock); 3901e78417d1SJosef Bacik if (head) { 3902e78417d1SJosef Bacik if (pin_bytes) 3903e78417d1SJosef Bacik btrfs_pin_extent(root, ref->bytenr, 3904e78417d1SJosef Bacik ref->num_bytes, 1); 3905e78417d1SJosef Bacik mutex_unlock(&head->mutex); 3906e78417d1SJosef Bacik } 3907acce952bSliubo btrfs_put_delayed_ref(ref); 3908acce952bSliubo 3909acce952bSliubo cond_resched(); 3910acce952bSliubo spin_lock(&delayed_refs->lock); 3911acce952bSliubo } 3912acce952bSliubo 3913acce952bSliubo spin_unlock(&delayed_refs->lock); 3914acce952bSliubo 3915acce952bSliubo return ret; 3916acce952bSliubo } 3917acce952bSliubo 3918aec8030aSMiao Xie static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t) 3919acce952bSliubo { 3920acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3921acce952bSliubo struct list_head splice; 3922acce952bSliubo 3923acce952bSliubo INIT_LIST_HEAD(&splice); 3924acce952bSliubo 3925acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3926acce952bSliubo 3927acce952bSliubo while (!list_empty(&splice)) { 3928acce952bSliubo snapshot = list_entry(splice.next, 3929acce952bSliubo struct btrfs_pending_snapshot, 3930acce952bSliubo list); 3931aec8030aSMiao Xie snapshot->error = -ECANCELED; 3932acce952bSliubo list_del_init(&snapshot->list); 3933acce952bSliubo } 3934acce952bSliubo } 3935acce952bSliubo 3936143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3937acce952bSliubo { 3938acce952bSliubo struct btrfs_inode *btrfs_inode; 3939acce952bSliubo struct list_head splice; 3940acce952bSliubo 3941acce952bSliubo INIT_LIST_HEAD(&splice); 3942acce952bSliubo 3943eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3944eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 3945acce952bSliubo 3946acce952bSliubo while (!list_empty(&splice)) { 3947eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 3948acce952bSliubo delalloc_inodes); 3949acce952bSliubo 3950acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3951df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 3952df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 3953eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3954acce952bSliubo 3955acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3956b216cbfbSMiao Xie 3957eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3958acce952bSliubo } 3959acce952bSliubo 3960eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3961eb73c1b7SMiao Xie } 3962eb73c1b7SMiao Xie 3963eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 3964eb73c1b7SMiao Xie { 3965eb73c1b7SMiao Xie struct btrfs_root *root; 3966eb73c1b7SMiao Xie struct list_head splice; 3967eb73c1b7SMiao Xie 3968eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 3969eb73c1b7SMiao Xie 3970eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3971eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 3972eb73c1b7SMiao Xie while (!list_empty(&splice)) { 3973eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3974eb73c1b7SMiao Xie delalloc_root); 3975eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 3976eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 3977eb73c1b7SMiao Xie BUG_ON(!root); 3978eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3979eb73c1b7SMiao Xie 3980eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 3981eb73c1b7SMiao Xie btrfs_put_fs_root(root); 3982eb73c1b7SMiao Xie 3983eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3984eb73c1b7SMiao Xie } 3985eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3986acce952bSliubo } 3987acce952bSliubo 3988acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3989acce952bSliubo struct extent_io_tree *dirty_pages, 3990acce952bSliubo int mark) 3991acce952bSliubo { 3992acce952bSliubo int ret; 3993acce952bSliubo struct extent_buffer *eb; 3994acce952bSliubo u64 start = 0; 3995acce952bSliubo u64 end; 3996acce952bSliubo 3997acce952bSliubo while (1) { 3998acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3999e6138876SJosef Bacik mark, NULL); 4000acce952bSliubo if (ret) 4001acce952bSliubo break; 4002acce952bSliubo 4003acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 4004acce952bSliubo while (start <= end) { 4005fd8b2b61SJosef Bacik eb = btrfs_find_tree_block(root, start, 4006fd8b2b61SJosef Bacik root->leafsize); 400769a85bd8SJosef Bacik start += root->leafsize; 4008fd8b2b61SJosef Bacik if (!eb) 4009acce952bSliubo continue; 4010fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 4011acce952bSliubo 4012fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 4013fd8b2b61SJosef Bacik &eb->bflags)) 4014fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 4015fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 4016acce952bSliubo } 4017acce952bSliubo } 4018acce952bSliubo 4019acce952bSliubo return ret; 4020acce952bSliubo } 4021acce952bSliubo 4022acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 4023acce952bSliubo struct extent_io_tree *pinned_extents) 4024acce952bSliubo { 4025acce952bSliubo struct extent_io_tree *unpin; 4026acce952bSliubo u64 start; 4027acce952bSliubo u64 end; 4028acce952bSliubo int ret; 4029ed0eaa14SLiu Bo bool loop = true; 4030acce952bSliubo 4031acce952bSliubo unpin = pinned_extents; 4032ed0eaa14SLiu Bo again: 4033acce952bSliubo while (1) { 4034acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 4035e6138876SJosef Bacik EXTENT_DIRTY, NULL); 4036acce952bSliubo if (ret) 4037acce952bSliubo break; 4038acce952bSliubo 4039acce952bSliubo /* opt_discard */ 40405378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 40415378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 40425378e607SLi Dongyang end + 1 - start, 40435378e607SLi Dongyang NULL); 4044acce952bSliubo 4045acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 4046acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 4047acce952bSliubo cond_resched(); 4048acce952bSliubo } 4049acce952bSliubo 4050ed0eaa14SLiu Bo if (loop) { 4051ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 4052ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 4053ed0eaa14SLiu Bo else 4054ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 4055ed0eaa14SLiu Bo loop = false; 4056ed0eaa14SLiu Bo goto again; 4057ed0eaa14SLiu Bo } 4058ed0eaa14SLiu Bo 4059acce952bSliubo return 0; 4060acce952bSliubo } 4061acce952bSliubo 406249b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 406349b25e05SJeff Mahoney struct btrfs_root *root) 406449b25e05SJeff Mahoney { 406549b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 406649b25e05SJeff Mahoney btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 406749b25e05SJeff Mahoney cur_trans->dirty_pages.dirty_bytes); 406849b25e05SJeff Mahoney 40694a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 407049b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 407149b25e05SJeff Mahoney 4072aec8030aSMiao Xie btrfs_evict_pending_snapshots(cur_trans); 4073aec8030aSMiao Xie 40744a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 407549b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 407649b25e05SJeff Mahoney 407767cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 407867cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 407967cde344SMiao Xie 408049b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 408149b25e05SJeff Mahoney EXTENT_DIRTY); 40826e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 40836e841e32SLiu Bo root->fs_info->pinned_extents); 408449b25e05SJeff Mahoney 40854a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 40864a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 40874a9d8bdeSMiao Xie 408849b25e05SJeff Mahoney /* 408949b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 409049b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 409149b25e05SJeff Mahoney */ 409249b25e05SJeff Mahoney } 409349b25e05SJeff Mahoney 409448a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root) 4095acce952bSliubo { 4096acce952bSliubo struct btrfs_transaction *t; 4097acce952bSliubo LIST_HEAD(list); 4098acce952bSliubo 4099acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 4100acce952bSliubo 4101a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4102acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 4103ac673879SMiao Xie root->fs_info->running_transaction = NULL; 4104a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4105a4abeea4SJosef Bacik 4106acce952bSliubo while (!list_empty(&list)) { 4107acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 4108acce952bSliubo 4109569e0f35SJosef Bacik btrfs_destroy_ordered_operations(t, root); 4110acce952bSliubo 4111199c2a9cSMiao Xie btrfs_destroy_all_ordered_extents(root->fs_info); 4112acce952bSliubo 4113acce952bSliubo btrfs_destroy_delayed_refs(t, root); 4114acce952bSliubo 41154a9d8bdeSMiao Xie /* 41164a9d8bdeSMiao Xie * FIXME: cleanup wait for commit 41174a9d8bdeSMiao Xie * We needn't acquire the lock here, because we are during 41184a9d8bdeSMiao Xie * the umount, there is no other task which will change it. 41194a9d8bdeSMiao Xie */ 41204a9d8bdeSMiao Xie t->state = TRANS_STATE_COMMIT_START; 412166657b31SJosef Bacik smp_mb(); 4122acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 4123acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 4124acce952bSliubo 4125aec8030aSMiao Xie btrfs_evict_pending_snapshots(t); 4126aec8030aSMiao Xie 41274a9d8bdeSMiao Xie t->state = TRANS_STATE_UNBLOCKED; 412866657b31SJosef Bacik smp_mb(); 4129acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 4130acce952bSliubo wake_up(&root->fs_info->transaction_wait); 4131acce952bSliubo 413267cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 413367cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 413467cde344SMiao Xie 4135eb73c1b7SMiao Xie btrfs_destroy_all_delalloc_inodes(root->fs_info); 4136acce952bSliubo 4137acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 4138acce952bSliubo EXTENT_DIRTY); 4139acce952bSliubo 4140acce952bSliubo btrfs_destroy_pinned_extent(root, 4141acce952bSliubo root->fs_info->pinned_extents); 4142acce952bSliubo 41434a9d8bdeSMiao Xie t->state = TRANS_STATE_COMPLETED; 41444a9d8bdeSMiao Xie smp_mb(); 41454a9d8bdeSMiao Xie if (waitqueue_active(&t->commit_wait)) 41464a9d8bdeSMiao Xie wake_up(&t->commit_wait); 41474a9d8bdeSMiao Xie 414813c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 4149acce952bSliubo list_del_init(&t->list); 4150acce952bSliubo memset(t, 0, sizeof(*t)); 4151acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 4152acce952bSliubo } 4153acce952bSliubo 4154acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 4155acce952bSliubo 4156acce952bSliubo return 0; 4157acce952bSliubo } 4158acce952bSliubo 4159d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 4160ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 41614bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 41620b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4163239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4164239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 41650da5468fSChris Mason }; 4166