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> 347e75bf3fSDavid Sterba #include <asm/unaligned.h> 354b4e25f2SChris Mason #include "compat.h" 36eb60ceacSChris Mason #include "ctree.h" 37eb60ceacSChris Mason #include "disk-io.h" 38e089f05cSChris Mason #include "transaction.h" 390f7d52f4SChris Mason #include "btrfs_inode.h" 400b86a832SChris Mason #include "volumes.h" 41db94535dSChris Mason #include "print-tree.h" 428b712842SChris Mason #include "async-thread.h" 43925baeddSChris Mason #include "locking.h" 44e02119d5SChris Mason #include "tree-log.h" 45fa9c0d79SChris Mason #include "free-space-cache.h" 46581bb050SLi Zefan #include "inode-map.h" 4721adbd5cSStefan Behrens #include "check-integrity.h" 48606686eeSJosef Bacik #include "rcu-string.h" 498dabb742SStefan Behrens #include "dev-replace.h" 5053b381b3SDavid Woodhouse #include "raid56.h" 51eb60ceacSChris Mason 52de0022b9SJosef Bacik #ifdef CONFIG_X86 53de0022b9SJosef Bacik #include <asm/cpufeature.h> 54de0022b9SJosef Bacik #endif 55de0022b9SJosef Bacik 56d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 578b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 584df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 59fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 60acce952bSliubo int read_only); 61569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 62569e0f35SJosef Bacik struct btrfs_root *root); 63143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 64acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 65acce952bSliubo struct btrfs_root *root); 66aec8030aSMiao Xie static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t); 67143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 68acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 69acce952bSliubo struct extent_io_tree *dirty_pages, 70acce952bSliubo int mark); 71acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 72acce952bSliubo struct extent_io_tree *pinned_extents); 7348a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root); 7448a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root); 75ce9adaa5SChris Mason 76d352ac68SChris Mason /* 77d352ac68SChris Mason * end_io_wq structs are used to do processing in task context when an IO is 78d352ac68SChris Mason * complete. This is used during reads to verify checksums, and it is used 79d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 80d352ac68SChris Mason */ 81ce9adaa5SChris Mason struct end_io_wq { 82ce9adaa5SChris Mason struct bio *bio; 83ce9adaa5SChris Mason bio_end_io_t *end_io; 84ce9adaa5SChris Mason void *private; 85ce9adaa5SChris Mason struct btrfs_fs_info *info; 86ce9adaa5SChris Mason int error; 8722c59948SChris Mason int metadata; 88ce9adaa5SChris Mason struct list_head list; 898b712842SChris Mason struct btrfs_work work; 90ce9adaa5SChris Mason }; 910da5468fSChris Mason 92d352ac68SChris Mason /* 93d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 94d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 95d352ac68SChris Mason * just before they are sent down the IO stack. 96d352ac68SChris Mason */ 9744b8bd7eSChris Mason struct async_submit_bio { 9844b8bd7eSChris Mason struct inode *inode; 9944b8bd7eSChris Mason struct bio *bio; 10044b8bd7eSChris Mason struct list_head list; 1014a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 1024a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 10344b8bd7eSChris Mason int rw; 10444b8bd7eSChris Mason int mirror_num; 105c8b97818SChris Mason unsigned long bio_flags; 106eaf25d93SChris Mason /* 107eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 108eaf25d93SChris Mason * can't tell us where in the file the bio should go 109eaf25d93SChris Mason */ 110eaf25d93SChris Mason u64 bio_offset; 1118b712842SChris Mason struct btrfs_work work; 11279787eaaSJeff Mahoney int error; 11344b8bd7eSChris Mason }; 11444b8bd7eSChris Mason 11585d4e461SChris Mason /* 11685d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 11785d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 11885d4e461SChris Mason * the level the eb occupies in the tree. 1194008c04aSChris Mason * 12085d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 12185d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 12285d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 12385d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 12485d4e461SChris Mason * have separate keysets. 1254008c04aSChris Mason * 12685d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 12785d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 12885d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1294008c04aSChris Mason * 13085d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 13185d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 13285d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 13385d4e461SChris Mason * 13485d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 13585d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 13685d4e461SChris Mason * needs update as well. 1374008c04aSChris Mason */ 1384008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1394008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1404008c04aSChris Mason # error 1414008c04aSChris Mason # endif 14285d4e461SChris Mason 14385d4e461SChris Mason static struct btrfs_lockdep_keyset { 14485d4e461SChris Mason u64 id; /* root objectid */ 14585d4e461SChris Mason const char *name_stem; /* lock name stem */ 14685d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 14785d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 14885d4e461SChris Mason } btrfs_lockdep_keysets[] = { 14985d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 15085d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 15185d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 15285d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 15385d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 15485d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 15560b62978SDavid Sterba { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" }, 15685d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 15785d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 15885d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 15985d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1604008c04aSChris Mason }; 16185d4e461SChris Mason 16285d4e461SChris Mason void __init btrfs_init_lockdep(void) 16385d4e461SChris Mason { 16485d4e461SChris Mason int i, j; 16585d4e461SChris Mason 16685d4e461SChris Mason /* initialize lockdep class names */ 16785d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 16885d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 16985d4e461SChris Mason 17085d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 17185d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 17285d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 17385d4e461SChris Mason } 17485d4e461SChris Mason } 17585d4e461SChris Mason 17685d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 17785d4e461SChris Mason int level) 17885d4e461SChris Mason { 17985d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 18085d4e461SChris Mason 18185d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 18285d4e461SChris Mason 18385d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 18485d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 18585d4e461SChris Mason if (ks->id == objectid) 18685d4e461SChris Mason break; 18785d4e461SChris Mason 18885d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 18985d4e461SChris Mason &ks->keys[level], ks->names[level]); 19085d4e461SChris Mason } 19185d4e461SChris Mason 1924008c04aSChris Mason #endif 1934008c04aSChris Mason 194d352ac68SChris Mason /* 195d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 196d352ac68SChris Mason * that covers the entire device 197d352ac68SChris Mason */ 198b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 199306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 2005f39d397SChris Mason int create) 2015f39d397SChris Mason { 2025f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2035f39d397SChris Mason struct extent_map *em; 2045f39d397SChris Mason int ret; 2055f39d397SChris Mason 206890871beSChris Mason read_lock(&em_tree->lock); 207d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 208a061fc8dSChris Mason if (em) { 209a061fc8dSChris Mason em->bdev = 210a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 211890871beSChris Mason read_unlock(&em_tree->lock); 2125f39d397SChris Mason goto out; 213a061fc8dSChris Mason } 214890871beSChris Mason read_unlock(&em_tree->lock); 2157b13b7b1SChris Mason 216172ddd60SDavid Sterba em = alloc_extent_map(); 2175f39d397SChris Mason if (!em) { 2185f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2195f39d397SChris Mason goto out; 2205f39d397SChris Mason } 2215f39d397SChris Mason em->start = 0; 2220afbaf8cSChris Mason em->len = (u64)-1; 223c8b97818SChris Mason em->block_len = (u64)-1; 2245f39d397SChris Mason em->block_start = 0; 225a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 226d1310b2eSChris Mason 227890871beSChris Mason write_lock(&em_tree->lock); 22809a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2295f39d397SChris Mason if (ret == -EEXIST) { 2305f39d397SChris Mason free_extent_map(em); 2317b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 232b4f359abSTsutomu Itoh if (!em) 2330433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2345f39d397SChris Mason } else if (ret) { 2357b13b7b1SChris Mason free_extent_map(em); 2360433f20dSTsutomu Itoh em = ERR_PTR(ret); 2375f39d397SChris Mason } 238890871beSChris Mason write_unlock(&em_tree->lock); 2397b13b7b1SChris Mason 2405f39d397SChris Mason out: 2415f39d397SChris Mason return em; 2425f39d397SChris Mason } 2435f39d397SChris Mason 244b0496686SLiu Bo u32 btrfs_csum_data(char *data, u32 seed, size_t len) 24519c00ddcSChris Mason { 246163e783eSDavid Woodhouse return crc32c(seed, data, len); 24719c00ddcSChris Mason } 24819c00ddcSChris Mason 24919c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 25019c00ddcSChris Mason { 2517e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 25219c00ddcSChris Mason } 25319c00ddcSChris Mason 254d352ac68SChris Mason /* 255d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 256d352ac68SChris Mason * into the csum field of the block. 257d352ac68SChris Mason */ 25819c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 25919c00ddcSChris Mason int verify) 26019c00ddcSChris Mason { 2616c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 262607d432dSJosef Bacik char *result = NULL; 26319c00ddcSChris Mason unsigned long len; 26419c00ddcSChris Mason unsigned long cur_len; 26519c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26619c00ddcSChris Mason char *kaddr; 26719c00ddcSChris Mason unsigned long map_start; 26819c00ddcSChris Mason unsigned long map_len; 26919c00ddcSChris Mason int err; 27019c00ddcSChris Mason u32 crc = ~(u32)0; 271607d432dSJosef Bacik unsigned long inline_result; 27219c00ddcSChris Mason 27319c00ddcSChris Mason len = buf->len - offset; 27419c00ddcSChris Mason while (len > 0) { 27519c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 276a6591715SChris Mason &kaddr, &map_start, &map_len); 277d397712bSChris Mason if (err) 27819c00ddcSChris Mason return 1; 27919c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 280b0496686SLiu Bo crc = btrfs_csum_data(kaddr + offset - map_start, 28119c00ddcSChris Mason crc, cur_len); 28219c00ddcSChris Mason len -= cur_len; 28319c00ddcSChris Mason offset += cur_len; 28419c00ddcSChris Mason } 285607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 286607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 287607d432dSJosef Bacik if (!result) 288607d432dSJosef Bacik return 1; 289607d432dSJosef Bacik } else { 290607d432dSJosef Bacik result = (char *)&inline_result; 291607d432dSJosef Bacik } 292607d432dSJosef Bacik 29319c00ddcSChris Mason btrfs_csum_final(crc, result); 29419c00ddcSChris Mason 29519c00ddcSChris Mason if (verify) { 296607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 297e4204dedSChris Mason u32 val; 298e4204dedSChris Mason u32 found = 0; 299607d432dSJosef Bacik memcpy(&found, result, csum_size); 300e4204dedSChris Mason 301607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 3027a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs: %s checksum verify " 303193f284dSChris Mason "failed on %llu wanted %X found %X " 304193f284dSChris Mason "level %d\n", 30519c00ddcSChris Mason root->fs_info->sb->s_id, 306193f284dSChris Mason (unsigned long long)buf->start, val, found, 307193f284dSChris Mason btrfs_header_level(buf)); 308607d432dSJosef Bacik if (result != (char *)&inline_result) 309607d432dSJosef Bacik kfree(result); 31019c00ddcSChris Mason return 1; 31119c00ddcSChris Mason } 31219c00ddcSChris Mason } else { 313607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 31419c00ddcSChris Mason } 315607d432dSJosef Bacik if (result != (char *)&inline_result) 316607d432dSJosef Bacik kfree(result); 31719c00ddcSChris Mason return 0; 31819c00ddcSChris Mason } 31919c00ddcSChris Mason 320d352ac68SChris Mason /* 321d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 322d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 323d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 324d352ac68SChris Mason * in the wrong place. 325d352ac68SChris Mason */ 3261259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 327b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 328b9fab919SChris Mason int atomic) 3291259ab75SChris Mason { 3302ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3311259ab75SChris Mason int ret; 3321259ab75SChris Mason 3331259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3341259ab75SChris Mason return 0; 3351259ab75SChris Mason 336b9fab919SChris Mason if (atomic) 337b9fab919SChris Mason return -EAGAIN; 338b9fab919SChris Mason 3392ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 340d0082371SJeff Mahoney 0, &cached_state); 3410b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3421259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3431259ab75SChris Mason ret = 0; 3441259ab75SChris Mason goto out; 3451259ab75SChris Mason } 3467a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 347193f284dSChris Mason "found %llu\n", 3481259ab75SChris Mason (unsigned long long)eb->start, 3491259ab75SChris Mason (unsigned long long)parent_transid, 3501259ab75SChris Mason (unsigned long long)btrfs_header_generation(eb)); 3511259ab75SChris Mason ret = 1; 3520b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 35333958dc6SChris Mason out: 3542ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3552ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3561259ab75SChris Mason return ret; 3571259ab75SChris Mason } 3581259ab75SChris Mason 359d352ac68SChris Mason /* 3601104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that 3611104a885SDavid Sterba * algorithm. Pass the raw disk superblock data. 3621104a885SDavid Sterba */ 3631104a885SDavid Sterba static int btrfs_check_super_csum(char *raw_disk_sb) 3641104a885SDavid Sterba { 3651104a885SDavid Sterba struct btrfs_super_block *disk_sb = 3661104a885SDavid Sterba (struct btrfs_super_block *)raw_disk_sb; 3671104a885SDavid Sterba u16 csum_type = btrfs_super_csum_type(disk_sb); 3681104a885SDavid Sterba int ret = 0; 3691104a885SDavid Sterba 3701104a885SDavid Sterba if (csum_type == BTRFS_CSUM_TYPE_CRC32) { 3711104a885SDavid Sterba u32 crc = ~(u32)0; 3721104a885SDavid Sterba const int csum_size = sizeof(crc); 3731104a885SDavid Sterba char result[csum_size]; 3741104a885SDavid Sterba 3751104a885SDavid Sterba /* 3761104a885SDavid Sterba * The super_block structure does not span the whole 3771104a885SDavid Sterba * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space 3781104a885SDavid Sterba * is filled with zeros and is included in the checkum. 3791104a885SDavid Sterba */ 3801104a885SDavid Sterba crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, 3811104a885SDavid Sterba crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 3821104a885SDavid Sterba btrfs_csum_final(crc, result); 3831104a885SDavid Sterba 3841104a885SDavid Sterba if (memcmp(raw_disk_sb, result, csum_size)) 3851104a885SDavid Sterba ret = 1; 386667e7d94SChris Mason 387667e7d94SChris Mason if (ret && btrfs_super_generation(disk_sb) < 10) { 388667e7d94SChris Mason printk(KERN_WARNING "btrfs: super block crcs don't match, older mkfs detected\n"); 389667e7d94SChris Mason ret = 0; 390667e7d94SChris Mason } 3911104a885SDavid Sterba } 3921104a885SDavid Sterba 3931104a885SDavid Sterba if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { 3941104a885SDavid Sterba printk(KERN_ERR "btrfs: unsupported checksum algorithm %u\n", 3951104a885SDavid Sterba csum_type); 3961104a885SDavid Sterba ret = 1; 3971104a885SDavid Sterba } 3981104a885SDavid Sterba 3991104a885SDavid Sterba return ret; 4001104a885SDavid Sterba } 4011104a885SDavid Sterba 4021104a885SDavid Sterba /* 403d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 404d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 405d352ac68SChris Mason */ 406f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 407f188591eSChris Mason struct extent_buffer *eb, 408ca7a79adSChris Mason u64 start, u64 parent_transid) 409f188591eSChris Mason { 410f188591eSChris Mason struct extent_io_tree *io_tree; 411ea466794SJosef Bacik int failed = 0; 412f188591eSChris Mason int ret; 413f188591eSChris Mason int num_copies = 0; 414f188591eSChris Mason int mirror_num = 0; 415ea466794SJosef Bacik int failed_mirror = 0; 416f188591eSChris Mason 417a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 418f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 419f188591eSChris Mason while (1) { 420bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 421bb82ab88SArne Jansen WAIT_COMPLETE, 422f188591eSChris Mason btree_get_extent, mirror_num); 423256dd1bbSStefan Behrens if (!ret) { 424256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 425b9fab919SChris Mason parent_transid, 0)) 426ea466794SJosef Bacik break; 427256dd1bbSStefan Behrens else 428256dd1bbSStefan Behrens ret = -EIO; 429256dd1bbSStefan Behrens } 430d397712bSChris Mason 431a826d6dcSJosef Bacik /* 432a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 433a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 434a826d6dcSJosef Bacik * any less wrong. 435a826d6dcSJosef Bacik */ 436a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 437ea466794SJosef Bacik break; 438ea466794SJosef Bacik 4395d964051SStefan Behrens num_copies = btrfs_num_copies(root->fs_info, 440f188591eSChris Mason eb->start, eb->len); 4414235298eSChris Mason if (num_copies == 1) 442ea466794SJosef Bacik break; 4434235298eSChris Mason 4445cf1ab56SJosef Bacik if (!failed_mirror) { 4455cf1ab56SJosef Bacik failed = 1; 4465cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4475cf1ab56SJosef Bacik } 4485cf1ab56SJosef Bacik 449f188591eSChris Mason mirror_num++; 450ea466794SJosef Bacik if (mirror_num == failed_mirror) 451ea466794SJosef Bacik mirror_num++; 452ea466794SJosef Bacik 4534235298eSChris Mason if (mirror_num > num_copies) 454ea466794SJosef Bacik break; 455f188591eSChris Mason } 456ea466794SJosef Bacik 457c0901581SStefan Behrens if (failed && !ret && failed_mirror) 458ea466794SJosef Bacik repair_eb_io_failure(root, eb, failed_mirror); 459ea466794SJosef Bacik 460ea466794SJosef Bacik return ret; 461f188591eSChris Mason } 46219c00ddcSChris Mason 463d352ac68SChris Mason /* 464d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 465d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 466d352ac68SChris Mason */ 467d397712bSChris Mason 468b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 46919c00ddcSChris Mason { 470d1310b2eSChris Mason struct extent_io_tree *tree; 4714eee4fa4SMiao Xie u64 start = page_offset(page); 47219c00ddcSChris Mason u64 found_start; 47319c00ddcSChris Mason struct extent_buffer *eb; 474f188591eSChris Mason 475d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 47619c00ddcSChris Mason 4774f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4784f2de97aSJosef Bacik if (page != eb->pages[0]) 4794f2de97aSJosef Bacik return 0; 48019c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 48119c00ddcSChris Mason if (found_start != start) { 48255c69072SChris Mason WARN_ON(1); 4834f2de97aSJosef Bacik return 0; 48455c69072SChris Mason } 48555c69072SChris Mason if (!PageUptodate(page)) { 48655c69072SChris Mason WARN_ON(1); 4874f2de97aSJosef Bacik return 0; 48819c00ddcSChris Mason } 48919c00ddcSChris Mason csum_tree_block(root, eb, 0); 49019c00ddcSChris Mason return 0; 49119c00ddcSChris Mason } 49219c00ddcSChris Mason 4932b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 4942b82032cSYan Zheng struct extent_buffer *eb) 4952b82032cSYan Zheng { 4962b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 4972b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 4982b82032cSYan Zheng int ret = 1; 4992b82032cSYan Zheng 5002b82032cSYan Zheng read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb), 5012b82032cSYan Zheng BTRFS_FSID_SIZE); 5022b82032cSYan Zheng while (fs_devices) { 5032b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 5042b82032cSYan Zheng ret = 0; 5052b82032cSYan Zheng break; 5062b82032cSYan Zheng } 5072b82032cSYan Zheng fs_devices = fs_devices->seed; 5082b82032cSYan Zheng } 5092b82032cSYan Zheng return ret; 5102b82032cSYan Zheng } 5112b82032cSYan Zheng 512a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 513a826d6dcSJosef Bacik printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \ 514a826d6dcSJosef Bacik "root=%llu, slot=%d\n", reason, \ 515a826d6dcSJosef Bacik (unsigned long long)btrfs_header_bytenr(eb), \ 516a826d6dcSJosef Bacik (unsigned long long)root->objectid, slot) 517a826d6dcSJosef Bacik 518a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 519a826d6dcSJosef Bacik struct extent_buffer *leaf) 520a826d6dcSJosef Bacik { 521a826d6dcSJosef Bacik struct btrfs_key key; 522a826d6dcSJosef Bacik struct btrfs_key leaf_key; 523a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 524a826d6dcSJosef Bacik int slot; 525a826d6dcSJosef Bacik 526a826d6dcSJosef Bacik if (nritems == 0) 527a826d6dcSJosef Bacik return 0; 528a826d6dcSJosef Bacik 529a826d6dcSJosef Bacik /* Check the 0 item */ 530a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 531a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 532a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 533a826d6dcSJosef Bacik return -EIO; 534a826d6dcSJosef Bacik } 535a826d6dcSJosef Bacik 536a826d6dcSJosef Bacik /* 537a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 538a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 539a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 540a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 541a826d6dcSJosef Bacik * offset is correct. 542a826d6dcSJosef Bacik */ 543a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 544a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 545a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 546a826d6dcSJosef Bacik 547a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 548a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 549a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 550a826d6dcSJosef Bacik return -EIO; 551a826d6dcSJosef Bacik } 552a826d6dcSJosef Bacik 553a826d6dcSJosef Bacik /* 554a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 555a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 556a826d6dcSJosef Bacik * front. 557a826d6dcSJosef Bacik */ 558a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 559a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 560a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 561a826d6dcSJosef Bacik return -EIO; 562a826d6dcSJosef Bacik } 563a826d6dcSJosef Bacik 564a826d6dcSJosef Bacik /* 565a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 566a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 567a826d6dcSJosef Bacik * all point outside of the leaf. 568a826d6dcSJosef Bacik */ 569a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 570a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 571a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 572a826d6dcSJosef Bacik return -EIO; 573a826d6dcSJosef Bacik } 574a826d6dcSJosef Bacik } 575a826d6dcSJosef Bacik 576a826d6dcSJosef Bacik return 0; 577a826d6dcSJosef Bacik } 578a826d6dcSJosef Bacik 579facc8a22SMiao Xie static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, 580facc8a22SMiao Xie u64 phy_offset, struct page *page, 581facc8a22SMiao Xie u64 start, u64 end, int mirror) 582ce9adaa5SChris Mason { 583ce9adaa5SChris Mason struct extent_io_tree *tree; 584ce9adaa5SChris Mason u64 found_start; 585ce9adaa5SChris Mason int found_level; 586ce9adaa5SChris Mason struct extent_buffer *eb; 587ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 588f188591eSChris Mason int ret = 0; 589727011e0SChris Mason int reads_done; 590ce9adaa5SChris Mason 591ce9adaa5SChris Mason if (!page->private) 592ce9adaa5SChris Mason goto out; 593d397712bSChris Mason 594727011e0SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 5954f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 596d397712bSChris Mason 5970b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5980b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5990b32f4bbSJosef Bacik */ 6000b32f4bbSJosef Bacik extent_buffer_get(eb); 6010b32f4bbSJosef Bacik 6020b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 603727011e0SChris Mason if (!reads_done) 604727011e0SChris Mason goto err; 605f188591eSChris Mason 6065cf1ab56SJosef Bacik eb->read_mirror = mirror; 607ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 608ea466794SJosef Bacik ret = -EIO; 609ea466794SJosef Bacik goto err; 610ea466794SJosef Bacik } 611ea466794SJosef Bacik 612ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 613727011e0SChris Mason if (found_start != eb->start) { 6147a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 615193f284dSChris Mason "%llu %llu\n", 616a1b32a59SChris Mason (unsigned long long)found_start, 617a1b32a59SChris Mason (unsigned long long)eb->start); 618f188591eSChris Mason ret = -EIO; 619ce9adaa5SChris Mason goto err; 620ce9adaa5SChris Mason } 6212b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 6227a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 623d397712bSChris Mason (unsigned long long)eb->start); 6241259ab75SChris Mason ret = -EIO; 6251259ab75SChris Mason goto err; 6261259ab75SChris Mason } 627ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6281c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 6291c24c3ceSJosef Bacik btrfs_info(root->fs_info, "bad tree block level %d\n", 6301c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6311c24c3ceSJosef Bacik ret = -EIO; 6321c24c3ceSJosef Bacik goto err; 6331c24c3ceSJosef Bacik } 634ce9adaa5SChris Mason 63585d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 63685d4e461SChris Mason eb, found_level); 6374008c04aSChris Mason 638ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 639a826d6dcSJosef Bacik if (ret) { 640f188591eSChris Mason ret = -EIO; 641a826d6dcSJosef Bacik goto err; 642a826d6dcSJosef Bacik } 643a826d6dcSJosef Bacik 644a826d6dcSJosef Bacik /* 645a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 646a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 647a826d6dcSJosef Bacik * return -EIO. 648a826d6dcSJosef Bacik */ 649a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 650a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 651a826d6dcSJosef Bacik ret = -EIO; 652a826d6dcSJosef Bacik } 653ce9adaa5SChris Mason 6540b32f4bbSJosef Bacik if (!ret) 6550b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 656ce9adaa5SChris Mason err: 65779fb65a1SJosef Bacik if (reads_done && 65879fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6594bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6604bb31e92SArne Jansen 66153b381b3SDavid Woodhouse if (ret) { 66253b381b3SDavid Woodhouse /* 66353b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 66453b381b3SDavid Woodhouse * again, we have to make sure it has something 66553b381b3SDavid Woodhouse * to decrement 66653b381b3SDavid Woodhouse */ 66753b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6680b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 66953b381b3SDavid Woodhouse } 6700b32f4bbSJosef Bacik free_extent_buffer(eb); 671ce9adaa5SChris Mason out: 672f188591eSChris Mason return ret; 673ce9adaa5SChris Mason } 674ce9adaa5SChris Mason 675ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6764bb31e92SArne Jansen { 6774bb31e92SArne Jansen struct extent_buffer *eb; 6784bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6794bb31e92SArne Jansen 6804f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 681ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6825cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 68353b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 684ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6854bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6864bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6874bb31e92SArne Jansen } 6884bb31e92SArne Jansen 689ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 690ce9adaa5SChris Mason { 691ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 692ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 693ce9adaa5SChris Mason 694ce9adaa5SChris Mason fs_info = end_io_wq->info; 695ce9adaa5SChris Mason end_io_wq->error = err; 6968b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6978b712842SChris Mason end_io_wq->work.flags = 0; 698d20f7043SChris Mason 6997b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 70053b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 701cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 702cad321adSChris Mason &end_io_wq->work); 70353b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7040cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 7050cb59c99SJosef Bacik &end_io_wq->work); 70653b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 70753b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 70853b381b3SDavid Woodhouse &end_io_wq->work); 709cad321adSChris Mason else 710e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 711e6dcd2dcSChris Mason &end_io_wq->work); 712d20f7043SChris Mason } else { 71353b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 71453b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 71553b381b3SDavid Woodhouse &end_io_wq->work); 71653b381b3SDavid Woodhouse else if (end_io_wq->metadata) 717d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 718d20f7043SChris Mason &end_io_wq->work); 719e6dcd2dcSChris Mason else 720d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 721d20f7043SChris Mason &end_io_wq->work); 722d20f7043SChris Mason } 723ce9adaa5SChris Mason } 724ce9adaa5SChris Mason 7250cb59c99SJosef Bacik /* 7260cb59c99SJosef Bacik * For the metadata arg you want 7270cb59c99SJosef Bacik * 7280cb59c99SJosef Bacik * 0 - if data 7290cb59c99SJosef Bacik * 1 - if normal metadta 7300cb59c99SJosef Bacik * 2 - if writing to the free space cache area 73153b381b3SDavid Woodhouse * 3 - raid parity work 7320cb59c99SJosef Bacik */ 73322c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 73422c59948SChris Mason int metadata) 7350b86a832SChris Mason { 736ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 737ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 738ce9adaa5SChris Mason if (!end_io_wq) 739ce9adaa5SChris Mason return -ENOMEM; 740ce9adaa5SChris Mason 741ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 742ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 74322c59948SChris Mason end_io_wq->info = info; 744ce9adaa5SChris Mason end_io_wq->error = 0; 745ce9adaa5SChris Mason end_io_wq->bio = bio; 74622c59948SChris Mason end_io_wq->metadata = metadata; 747ce9adaa5SChris Mason 748ce9adaa5SChris Mason bio->bi_private = end_io_wq; 749ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 75022c59948SChris Mason return 0; 75122c59948SChris Mason } 75222c59948SChris Mason 753b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7544854ddd0SChris Mason { 7554854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7564854ddd0SChris Mason info->workers.max_workers, 7574854ddd0SChris Mason info->fs_devices->open_devices); 7584854ddd0SChris Mason return 256 * limit; 7594854ddd0SChris Mason } 7604854ddd0SChris Mason 7614a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7624a69a410SChris Mason { 7634a69a410SChris Mason struct async_submit_bio *async; 76479787eaaSJeff Mahoney int ret; 7654a69a410SChris Mason 7664a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 76779787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 768eaf25d93SChris Mason async->mirror_num, async->bio_flags, 769eaf25d93SChris Mason async->bio_offset); 77079787eaaSJeff Mahoney if (ret) 77179787eaaSJeff Mahoney async->error = ret; 7724a69a410SChris Mason } 7734a69a410SChris Mason 7744a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7758b712842SChris Mason { 7768b712842SChris Mason struct btrfs_fs_info *fs_info; 7778b712842SChris Mason struct async_submit_bio *async; 7784854ddd0SChris Mason int limit; 7798b712842SChris Mason 7808b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7818b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7824854ddd0SChris Mason 783b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7844854ddd0SChris Mason limit = limit * 2 / 3; 7854854ddd0SChris Mason 78666657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 787b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7884854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7894854ddd0SChris Mason 79079787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 79179787eaaSJeff Mahoney if (async->error) { 79279787eaaSJeff Mahoney bio_endio(async->bio, async->error); 79379787eaaSJeff Mahoney return; 79479787eaaSJeff Mahoney } 79579787eaaSJeff Mahoney 7964a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 797eaf25d93SChris Mason async->mirror_num, async->bio_flags, 798eaf25d93SChris Mason async->bio_offset); 7994a69a410SChris Mason } 8004a69a410SChris Mason 8014a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8024a69a410SChris Mason { 8034a69a410SChris Mason struct async_submit_bio *async; 8044a69a410SChris Mason 8054a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8068b712842SChris Mason kfree(async); 8078b712842SChris Mason } 8088b712842SChris Mason 80944b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 81044b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 811c8b97818SChris Mason unsigned long bio_flags, 812eaf25d93SChris Mason u64 bio_offset, 8134a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 8144a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 81544b8bd7eSChris Mason { 81644b8bd7eSChris Mason struct async_submit_bio *async; 81744b8bd7eSChris Mason 81844b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81944b8bd7eSChris Mason if (!async) 82044b8bd7eSChris Mason return -ENOMEM; 82144b8bd7eSChris Mason 82244b8bd7eSChris Mason async->inode = inode; 82344b8bd7eSChris Mason async->rw = rw; 82444b8bd7eSChris Mason async->bio = bio; 82544b8bd7eSChris Mason async->mirror_num = mirror_num; 8264a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8274a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8284a69a410SChris Mason 8294a69a410SChris Mason async->work.func = run_one_async_start; 8304a69a410SChris Mason async->work.ordered_func = run_one_async_done; 8314a69a410SChris Mason async->work.ordered_free = run_one_async_free; 8324a69a410SChris Mason 8338b712842SChris Mason async->work.flags = 0; 834c8b97818SChris Mason async->bio_flags = bio_flags; 835eaf25d93SChris Mason async->bio_offset = bio_offset; 8368c8bee1dSChris Mason 83779787eaaSJeff Mahoney async->error = 0; 83879787eaaSJeff Mahoney 839cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 840d313d7a3SChris Mason 8417b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 842d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 843d313d7a3SChris Mason 8448b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 8459473f16cSChris Mason 846771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 847771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 848771ed689SChris Mason wait_event(fs_info->async_submit_wait, 849771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 850771ed689SChris Mason } 851771ed689SChris Mason 85244b8bd7eSChris Mason return 0; 85344b8bd7eSChris Mason } 85444b8bd7eSChris Mason 855ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 856ce3ed71aSChris Mason { 857ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 858ce3ed71aSChris Mason int bio_index = 0; 859ce3ed71aSChris Mason struct btrfs_root *root; 86079787eaaSJeff Mahoney int ret = 0; 861ce3ed71aSChris Mason 862ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 863ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 864ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 86579787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 86679787eaaSJeff Mahoney if (ret) 86779787eaaSJeff Mahoney break; 868ce3ed71aSChris Mason bio_index++; 869ce3ed71aSChris Mason bvec++; 870ce3ed71aSChris Mason } 87179787eaaSJeff Mahoney return ret; 872ce3ed71aSChris Mason } 873ce3ed71aSChris Mason 8744a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8754a69a410SChris Mason struct bio *bio, int mirror_num, 876eaf25d93SChris Mason unsigned long bio_flags, 877eaf25d93SChris Mason u64 bio_offset) 87822c59948SChris Mason { 8798b712842SChris Mason /* 8808b712842SChris Mason * when we're called for a write, we're already in the async 8815443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8828b712842SChris Mason */ 88379787eaaSJeff Mahoney return btree_csum_one_bio(bio); 88422c59948SChris Mason } 88522c59948SChris Mason 8864a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 887eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 888eaf25d93SChris Mason u64 bio_offset) 8894a69a410SChris Mason { 89061891923SStefan Behrens int ret; 89161891923SStefan Behrens 8928b712842SChris Mason /* 8934a69a410SChris Mason * when we're called for a write, we're already in the async 8944a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8958b712842SChris Mason */ 89661891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 89761891923SStefan Behrens if (ret) 89861891923SStefan Behrens bio_endio(bio, ret); 89961891923SStefan Behrens return ret; 9000b86a832SChris Mason } 9010b86a832SChris Mason 902de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 903de0022b9SJosef Bacik { 904de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 905de0022b9SJosef Bacik return 0; 906de0022b9SJosef Bacik #ifdef CONFIG_X86 907de0022b9SJosef Bacik if (cpu_has_xmm4_2) 908de0022b9SJosef Bacik return 0; 909de0022b9SJosef Bacik #endif 910de0022b9SJosef Bacik return 1; 911de0022b9SJosef Bacik } 912de0022b9SJosef Bacik 91344b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 914eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 915eaf25d93SChris Mason u64 bio_offset) 91644b8bd7eSChris Mason { 917de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 9184a69a410SChris Mason int ret; 919cad321adSChris Mason 9207b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 921cad321adSChris Mason /* 922cad321adSChris Mason * called for a read, do the setup so that checksum validation 923cad321adSChris Mason * can happen in the async kernel threads 924cad321adSChris Mason */ 925f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 926f3f266abSChris Mason bio, 1); 9271d4284bdSChris Mason if (ret) 92861891923SStefan Behrens goto out_w_error; 92961891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 9306f3577bdSChris Mason mirror_num, 0); 931de0022b9SJosef Bacik } else if (!async) { 932de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 933de0022b9SJosef Bacik if (ret) 93461891923SStefan Behrens goto out_w_error; 93561891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 936de0022b9SJosef Bacik mirror_num, 0); 93761891923SStefan Behrens } else { 938cad321adSChris Mason /* 93961891923SStefan Behrens * kthread helpers are used to submit writes so that 94061891923SStefan Behrens * checksumming can happen in parallel across all CPUs 941cad321adSChris Mason */ 94261891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 943c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 944eaf25d93SChris Mason bio_offset, 9454a69a410SChris Mason __btree_submit_bio_start, 9464a69a410SChris Mason __btree_submit_bio_done); 94744b8bd7eSChris Mason } 94844b8bd7eSChris Mason 94961891923SStefan Behrens if (ret) { 95061891923SStefan Behrens out_w_error: 95161891923SStefan Behrens bio_endio(bio, ret); 95261891923SStefan Behrens } 95361891923SStefan Behrens return ret; 95461891923SStefan Behrens } 95561891923SStefan Behrens 9563dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 957784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 958a6bc32b8SMel Gorman struct page *newpage, struct page *page, 959a6bc32b8SMel Gorman enum migrate_mode mode) 960784b4e29SChris Mason { 961784b4e29SChris Mason /* 962784b4e29SChris Mason * we can't safely write a btree page from here, 963784b4e29SChris Mason * we haven't done the locking hook 964784b4e29SChris Mason */ 965784b4e29SChris Mason if (PageDirty(page)) 966784b4e29SChris Mason return -EAGAIN; 967784b4e29SChris Mason /* 968784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 969784b4e29SChris Mason * We must have no buffers or drop them. 970784b4e29SChris Mason */ 971784b4e29SChris Mason if (page_has_private(page) && 972784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 973784b4e29SChris Mason return -EAGAIN; 974a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 975784b4e29SChris Mason } 9763dd1462eSJan Beulich #endif 977784b4e29SChris Mason 9780da5468fSChris Mason 9790da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9800da5468fSChris Mason struct writeback_control *wbc) 9810da5468fSChris Mason { 982d1310b2eSChris Mason struct extent_io_tree *tree; 983e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 984e2d84521SMiao Xie int ret; 985e2d84521SMiao Xie 986d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 987d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 988448d640bSChris Mason 989448d640bSChris Mason if (wbc->for_kupdate) 990448d640bSChris Mason return 0; 991448d640bSChris Mason 992e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 993b9473439SChris Mason /* this is a bit racy, but that's ok */ 994e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 995e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 996e2d84521SMiao Xie if (ret < 0) 997793955bcSChris Mason return 0; 998793955bcSChris Mason } 9990b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 10000da5468fSChris Mason } 10010da5468fSChris Mason 1002b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 10035f39d397SChris Mason { 1004d1310b2eSChris Mason struct extent_io_tree *tree; 1005d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10068ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 10075f39d397SChris Mason } 10085f39d397SChris Mason 100970dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10105f39d397SChris Mason { 101198509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 101298509cfcSChris Mason return 0; 10130c4e538bSDavid Sterba 1014f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1015d98237b3SChris Mason } 1016d98237b3SChris Mason 1017d47992f8SLukas Czerner static void btree_invalidatepage(struct page *page, unsigned int offset, 1018d47992f8SLukas Czerner unsigned int length) 1019d98237b3SChris Mason { 1020d1310b2eSChris Mason struct extent_io_tree *tree; 1021d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10225f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10235f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10249ad6b7bcSChris Mason if (PagePrivate(page)) { 1025d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 1026d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 10279ad6b7bcSChris Mason ClearPagePrivate(page); 10289ad6b7bcSChris Mason set_page_private(page, 0); 10299ad6b7bcSChris Mason page_cache_release(page); 10309ad6b7bcSChris Mason } 1031d98237b3SChris Mason } 1032d98237b3SChris Mason 10330b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10340b32f4bbSJosef Bacik { 1035bb146eb2SJosef Bacik #ifdef DEBUG 10360b32f4bbSJosef Bacik struct extent_buffer *eb; 10370b32f4bbSJosef Bacik 10380b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10390b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10400b32f4bbSJosef Bacik BUG_ON(!eb); 10410b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10420b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10430b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1044bb146eb2SJosef Bacik #endif 10450b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10460b32f4bbSJosef Bacik } 10470b32f4bbSJosef Bacik 10487f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1049d98237b3SChris Mason .readpage = btree_readpage, 10500da5468fSChris Mason .writepages = btree_writepages, 10515f39d397SChris Mason .releasepage = btree_releasepage, 10525f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10535a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1054784b4e29SChris Mason .migratepage = btree_migratepage, 10555a92bc88SChris Mason #endif 10560b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1057d98237b3SChris Mason }; 1058123abc88SChris Mason 1059ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1060ca7a79adSChris Mason u64 parent_transid) 1061090d1875SChris Mason { 10625f39d397SChris Mason struct extent_buffer *buf = NULL; 10635f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1064de428b63SChris Mason int ret = 0; 1065090d1875SChris Mason 1066db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10675f39d397SChris Mason if (!buf) 1068090d1875SChris Mason return 0; 1069d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1070bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10715f39d397SChris Mason free_extent_buffer(buf); 1072de428b63SChris Mason return ret; 1073090d1875SChris Mason } 1074090d1875SChris Mason 1075ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1076ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1077ab0fff03SArne Jansen { 1078ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1079ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1080ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1081ab0fff03SArne Jansen int ret; 1082ab0fff03SArne Jansen 1083ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1084ab0fff03SArne Jansen if (!buf) 1085ab0fff03SArne Jansen return 0; 1086ab0fff03SArne Jansen 1087ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1088ab0fff03SArne Jansen 1089ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1090ab0fff03SArne Jansen btree_get_extent, mirror_num); 1091ab0fff03SArne Jansen if (ret) { 1092ab0fff03SArne Jansen free_extent_buffer(buf); 1093ab0fff03SArne Jansen return ret; 1094ab0fff03SArne Jansen } 1095ab0fff03SArne Jansen 1096ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1097ab0fff03SArne Jansen free_extent_buffer(buf); 1098ab0fff03SArne Jansen return -EIO; 10990b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1100ab0fff03SArne Jansen *eb = buf; 1101ab0fff03SArne Jansen } else { 1102ab0fff03SArne Jansen free_extent_buffer(buf); 1103ab0fff03SArne Jansen } 1104ab0fff03SArne Jansen return 0; 1105ab0fff03SArne Jansen } 1106ab0fff03SArne Jansen 11070999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 11080999df54SChris Mason u64 bytenr, u32 blocksize) 11090999df54SChris Mason { 11100999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11110999df54SChris Mason struct extent_buffer *eb; 11120999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1113f09d1f60SDavid Sterba bytenr, blocksize); 11140999df54SChris Mason return eb; 11150999df54SChris Mason } 11160999df54SChris Mason 11170999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 11180999df54SChris Mason u64 bytenr, u32 blocksize) 11190999df54SChris Mason { 11200999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11210999df54SChris Mason struct extent_buffer *eb; 11220999df54SChris Mason 11230999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1124727011e0SChris Mason bytenr, blocksize); 11250999df54SChris Mason return eb; 11260999df54SChris Mason } 11270999df54SChris Mason 11280999df54SChris Mason 1129e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1130e02119d5SChris Mason { 1131727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11328aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1133e02119d5SChris Mason } 1134e02119d5SChris Mason 1135e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1136e02119d5SChris Mason { 1137727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11388aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1139e02119d5SChris Mason } 1140e02119d5SChris Mason 1141db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1142ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1143e20d96d6SChris Mason { 11445f39d397SChris Mason struct extent_buffer *buf = NULL; 114519c00ddcSChris Mason int ret; 114619c00ddcSChris Mason 1147db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11485f39d397SChris Mason if (!buf) 1149d98237b3SChris Mason return NULL; 1150e4204dedSChris Mason 1151ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11520f0fe8f7SFilipe David Borba Manana if (ret) { 11530f0fe8f7SFilipe David Borba Manana free_extent_buffer(buf); 11540f0fe8f7SFilipe David Borba Manana return NULL; 11550f0fe8f7SFilipe David Borba Manana } 11565f39d397SChris Mason return buf; 1157ce9adaa5SChris Mason 1158eb60ceacSChris Mason } 1159eb60ceacSChris Mason 1160d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11615f39d397SChris Mason struct extent_buffer *buf) 1162ed2ff2cbSChris Mason { 1163e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1164e2d84521SMiao Xie 116555c69072SChris Mason if (btrfs_header_generation(buf) == 1166e2d84521SMiao Xie fs_info->running_transaction->transid) { 1167b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1168b4ce94deSChris Mason 1169b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1170e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1171e2d84521SMiao Xie -buf->len, 1172e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1173b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1174b9473439SChris Mason btrfs_set_lock_blocking(buf); 11750b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1176925baeddSChris Mason } 11775f39d397SChris Mason } 1178ed7b63ebSJosef Bacik } 11795f39d397SChris Mason 1180143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 118187ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11829f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1183e20d96d6SChris Mason u64 objectid) 1184d97e63b6SChris Mason { 1185cfaa7295SChris Mason root->node = NULL; 1186a28ec197SChris Mason root->commit_root = NULL; 1187db94535dSChris Mason root->sectorsize = sectorsize; 1188db94535dSChris Mason root->nodesize = nodesize; 1189db94535dSChris Mason root->leafsize = leafsize; 119087ee04ebSChris Mason root->stripesize = stripesize; 1191123abc88SChris Mason root->ref_cows = 0; 11920b86a832SChris Mason root->track_dirty = 0; 1193c71bf099SYan, Zheng root->in_radix = 0; 1194d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1195d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11960b86a832SChris Mason 11970f7d52f4SChris Mason root->objectid = objectid; 11980f7d52f4SChris Mason root->last_trans = 0; 119913a8a7c8SYan, Zheng root->highest_objectid = 0; 1200eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0; 1201199c2a9cSMiao Xie root->nr_ordered_extents = 0; 120258176a96SJosef Bacik root->name = NULL; 12036bef4d31SEric Paris root->inode_tree = RB_ROOT; 120416cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1205f0486c68SYan, Zheng root->block_rsv = NULL; 1206d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 12070b86a832SChris Mason 12080b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 12095d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1210eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes); 1211eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root); 1212199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents); 1213199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root); 12142ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 12152ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1216d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 12175d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1218eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock); 1219199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock); 1220f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 12212ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 12222ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1223a2135011SChris Mason mutex_init(&root->objectid_mutex); 1224e02119d5SChris Mason mutex_init(&root->log_mutex); 12257237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12267237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12277237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12287237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12297237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12307237f183SYan Zheng atomic_set(&root->log_writers, 0); 12312ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12328a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 1233b0feb9d9SMiao Xie atomic_set(&root->refs, 1); 12347237f183SYan Zheng root->log_transid = 0; 1235257c62e1SChris Mason root->last_log_commit = 0; 1236d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1237f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1238017e5369SChris Mason 12393768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12403768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12416702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 124258176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 12433f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 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 126020897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 126120897f5cSArne Jansen struct btrfs_fs_info *fs_info, 126220897f5cSArne Jansen u64 objectid) 126320897f5cSArne Jansen { 126420897f5cSArne Jansen struct extent_buffer *leaf; 126520897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 126620897f5cSArne Jansen struct btrfs_root *root; 126720897f5cSArne Jansen struct btrfs_key key; 126820897f5cSArne Jansen int ret = 0; 126920897f5cSArne Jansen u64 bytenr; 12706463fe58SStefan Behrens uuid_le uuid; 127120897f5cSArne Jansen 127220897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 127320897f5cSArne Jansen if (!root) 127420897f5cSArne Jansen return ERR_PTR(-ENOMEM); 127520897f5cSArne Jansen 127620897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 127720897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 127820897f5cSArne Jansen root, fs_info, objectid); 127920897f5cSArne Jansen root->root_key.objectid = objectid; 128020897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 128120897f5cSArne Jansen root->root_key.offset = 0; 128220897f5cSArne Jansen 128320897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 128420897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 128520897f5cSArne Jansen if (IS_ERR(leaf)) { 128620897f5cSArne Jansen ret = PTR_ERR(leaf); 12871dd05682STsutomu Itoh leaf = NULL; 128820897f5cSArne Jansen goto fail; 128920897f5cSArne Jansen } 129020897f5cSArne Jansen 129120897f5cSArne Jansen bytenr = leaf->start; 129220897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 129320897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 129420897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 129520897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 129620897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 129720897f5cSArne Jansen root->node = leaf; 129820897f5cSArne Jansen 129920897f5cSArne Jansen write_extent_buffer(leaf, fs_info->fsid, 130020897f5cSArne Jansen (unsigned long)btrfs_header_fsid(leaf), 130120897f5cSArne Jansen BTRFS_FSID_SIZE); 130220897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 130320897f5cSArne Jansen (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 130420897f5cSArne Jansen BTRFS_UUID_SIZE); 130520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 130620897f5cSArne Jansen 130720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 130820897f5cSArne Jansen root->track_dirty = 1; 130920897f5cSArne Jansen 131020897f5cSArne Jansen 131120897f5cSArne Jansen root->root_item.flags = 0; 131220897f5cSArne Jansen root->root_item.byte_limit = 0; 131320897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 131420897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 131520897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 131620897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 131720897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 131820897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 131920897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 13206463fe58SStefan Behrens uuid_le_gen(&uuid); 13216463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 132220897f5cSArne Jansen root->root_item.drop_level = 0; 132320897f5cSArne Jansen 132420897f5cSArne Jansen key.objectid = objectid; 132520897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 132620897f5cSArne Jansen key.offset = 0; 132720897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 132820897f5cSArne Jansen if (ret) 132920897f5cSArne Jansen goto fail; 133020897f5cSArne Jansen 133120897f5cSArne Jansen btrfs_tree_unlock(leaf); 133220897f5cSArne Jansen 133320897f5cSArne Jansen return root; 13341dd05682STsutomu Itoh 13351dd05682STsutomu Itoh fail: 13361dd05682STsutomu Itoh if (leaf) { 13371dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13381dd05682STsutomu Itoh free_extent_buffer(leaf); 13391dd05682STsutomu Itoh } 13401dd05682STsutomu Itoh kfree(root); 13411dd05682STsutomu Itoh 13421dd05682STsutomu Itoh return ERR_PTR(ret); 134320897f5cSArne Jansen } 134420897f5cSArne Jansen 13457237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1346e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13470f7d52f4SChris Mason { 13480f7d52f4SChris Mason struct btrfs_root *root; 13490f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13507237f183SYan Zheng struct extent_buffer *leaf; 1351e02119d5SChris Mason 13526f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1353e02119d5SChris Mason if (!root) 13547237f183SYan Zheng return ERR_PTR(-ENOMEM); 1355e02119d5SChris Mason 1356e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1357e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1358e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1359e02119d5SChris Mason 1360e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1361e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1362e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13637237f183SYan Zheng /* 13647237f183SYan Zheng * log trees do not get reference counted because they go away 13657237f183SYan Zheng * before a real commit is actually done. They do store pointers 13667237f183SYan Zheng * to file data extents, and those reference counts still get 13677237f183SYan Zheng * updated (along with back refs to the log tree). 13687237f183SYan Zheng */ 1369e02119d5SChris Mason root->ref_cows = 0; 1370e02119d5SChris Mason 13715d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 137266d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13735581a51aSJan Schmidt 0, 0, 0); 13747237f183SYan Zheng if (IS_ERR(leaf)) { 13757237f183SYan Zheng kfree(root); 13767237f183SYan Zheng return ERR_CAST(leaf); 13777237f183SYan Zheng } 1378e02119d5SChris Mason 13795d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13805d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13815d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13825d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13835d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13847237f183SYan Zheng root->node = leaf; 1385e02119d5SChris Mason 1386e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1387e02119d5SChris Mason (unsigned long)btrfs_header_fsid(root->node), 1388e02119d5SChris Mason BTRFS_FSID_SIZE); 1389e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1390e02119d5SChris Mason btrfs_tree_unlock(root->node); 13917237f183SYan Zheng return root; 13927237f183SYan Zheng } 13937237f183SYan Zheng 13947237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13957237f183SYan Zheng struct btrfs_fs_info *fs_info) 13967237f183SYan Zheng { 13977237f183SYan Zheng struct btrfs_root *log_root; 13987237f183SYan Zheng 13997237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14007237f183SYan Zheng if (IS_ERR(log_root)) 14017237f183SYan Zheng return PTR_ERR(log_root); 14027237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14037237f183SYan Zheng fs_info->log_root_tree = log_root; 14047237f183SYan Zheng return 0; 14057237f183SYan Zheng } 14067237f183SYan Zheng 14077237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14087237f183SYan Zheng struct btrfs_root *root) 14097237f183SYan Zheng { 14107237f183SYan Zheng struct btrfs_root *log_root; 14117237f183SYan Zheng struct btrfs_inode_item *inode_item; 14127237f183SYan Zheng 14137237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 14147237f183SYan Zheng if (IS_ERR(log_root)) 14157237f183SYan Zheng return PTR_ERR(log_root); 14167237f183SYan Zheng 14177237f183SYan Zheng log_root->last_trans = trans->transid; 14187237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14197237f183SYan Zheng 14207237f183SYan Zheng inode_item = &log_root->root_item.inode; 14213cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 14223cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 14233cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 14243cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 14253cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 14267237f183SYan Zheng 14275d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14287237f183SYan Zheng 14297237f183SYan Zheng WARN_ON(root->log_root); 14307237f183SYan Zheng root->log_root = log_root; 14317237f183SYan Zheng root->log_transid = 0; 1432257c62e1SChris Mason root->last_log_commit = 0; 1433e02119d5SChris Mason return 0; 1434e02119d5SChris Mason } 1435e02119d5SChris Mason 143635a3621bSStefan Behrens static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 1437cb517eabSMiao Xie struct btrfs_key *key) 1438e02119d5SChris Mason { 1439e02119d5SChris Mason struct btrfs_root *root; 1440e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14410f7d52f4SChris Mason struct btrfs_path *path; 144284234f3aSYan Zheng u64 generation; 1443db94535dSChris Mason u32 blocksize; 1444cb517eabSMiao Xie int ret; 1445cb517eabSMiao Xie 1446cb517eabSMiao Xie path = btrfs_alloc_path(); 1447cb517eabSMiao Xie if (!path) 1448cb517eabSMiao Xie return ERR_PTR(-ENOMEM); 14490f7d52f4SChris Mason 14506f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1451cb517eabSMiao Xie if (!root) { 1452cb517eabSMiao Xie ret = -ENOMEM; 1453cb517eabSMiao Xie goto alloc_fail; 14540f7d52f4SChris Mason } 14550f7d52f4SChris Mason 1456db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 145787ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 1458cb517eabSMiao Xie root, fs_info, key->objectid); 14590f7d52f4SChris Mason 1460cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path, 1461cb517eabSMiao Xie &root->root_item, &root->root_key); 14620f7d52f4SChris Mason if (ret) { 146313a8a7c8SYan, Zheng if (ret > 0) 146413a8a7c8SYan, Zheng ret = -ENOENT; 1465cb517eabSMiao Xie goto find_fail; 14660f7d52f4SChris Mason } 146713a8a7c8SYan, Zheng 146884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1469db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1470db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 147184234f3aSYan Zheng blocksize, generation); 1472cb517eabSMiao Xie if (!root->node) { 1473cb517eabSMiao Xie ret = -ENOMEM; 1474cb517eabSMiao Xie goto find_fail; 1475cb517eabSMiao Xie } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { 1476cb517eabSMiao Xie ret = -EIO; 1477cb517eabSMiao Xie goto read_fail; 1478416bc658SJosef Bacik } 14795d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 148013a8a7c8SYan, Zheng out: 1481cb517eabSMiao Xie btrfs_free_path(path); 1482cb517eabSMiao Xie return root; 1483cb517eabSMiao Xie 1484cb517eabSMiao Xie read_fail: 1485cb517eabSMiao Xie free_extent_buffer(root->node); 1486cb517eabSMiao Xie find_fail: 1487cb517eabSMiao Xie kfree(root); 1488cb517eabSMiao Xie alloc_fail: 1489cb517eabSMiao Xie root = ERR_PTR(ret); 1490cb517eabSMiao Xie goto out; 1491cb517eabSMiao Xie } 1492cb517eabSMiao Xie 1493cb517eabSMiao Xie struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, 1494cb517eabSMiao Xie struct btrfs_key *location) 1495cb517eabSMiao Xie { 1496cb517eabSMiao Xie struct btrfs_root *root; 1497cb517eabSMiao Xie 1498cb517eabSMiao Xie root = btrfs_read_tree_root(tree_root, location); 1499cb517eabSMiao Xie if (IS_ERR(root)) 1500cb517eabSMiao Xie return root; 1501cb517eabSMiao Xie 1502cb517eabSMiao Xie if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 15030f7d52f4SChris Mason root->ref_cows = 1; 150408fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 150508fe4db1SLi Zefan } 150613a8a7c8SYan, Zheng 15075eda7b5eSChris Mason return root; 15085eda7b5eSChris Mason } 15095eda7b5eSChris Mason 1510cb517eabSMiao Xie int btrfs_init_fs_root(struct btrfs_root *root) 1511cb517eabSMiao Xie { 1512cb517eabSMiao Xie int ret; 1513cb517eabSMiao Xie 1514cb517eabSMiao Xie root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1515cb517eabSMiao Xie root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1516cb517eabSMiao Xie GFP_NOFS); 1517cb517eabSMiao Xie if (!root->free_ino_pinned || !root->free_ino_ctl) { 1518cb517eabSMiao Xie ret = -ENOMEM; 1519cb517eabSMiao Xie goto fail; 1520cb517eabSMiao Xie } 1521cb517eabSMiao Xie 1522cb517eabSMiao Xie btrfs_init_free_ino_ctl(root); 1523cb517eabSMiao Xie mutex_init(&root->fs_commit_mutex); 1524cb517eabSMiao Xie spin_lock_init(&root->cache_lock); 1525cb517eabSMiao Xie init_waitqueue_head(&root->cache_wait); 1526cb517eabSMiao Xie 1527cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev); 1528cb517eabSMiao Xie if (ret) 1529cb517eabSMiao Xie goto fail; 1530cb517eabSMiao Xie return 0; 1531cb517eabSMiao Xie fail: 1532cb517eabSMiao Xie kfree(root->free_ino_ctl); 1533cb517eabSMiao Xie kfree(root->free_ino_pinned); 1534cb517eabSMiao Xie return ret; 1535cb517eabSMiao Xie } 1536cb517eabSMiao Xie 1537171170c1SSergei Trofimovich static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 1538cb517eabSMiao Xie u64 root_id) 1539cb517eabSMiao Xie { 1540cb517eabSMiao Xie struct btrfs_root *root; 1541cb517eabSMiao Xie 1542cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1543cb517eabSMiao Xie root = radix_tree_lookup(&fs_info->fs_roots_radix, 1544cb517eabSMiao Xie (unsigned long)root_id); 1545cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1546cb517eabSMiao Xie return root; 1547cb517eabSMiao Xie } 1548cb517eabSMiao Xie 1549cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 1550cb517eabSMiao Xie struct btrfs_root *root) 1551cb517eabSMiao Xie { 1552cb517eabSMiao Xie int ret; 1553cb517eabSMiao Xie 1554cb517eabSMiao Xie ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 1555cb517eabSMiao Xie if (ret) 1556cb517eabSMiao Xie return ret; 1557cb517eabSMiao Xie 1558cb517eabSMiao Xie spin_lock(&fs_info->fs_roots_radix_lock); 1559cb517eabSMiao Xie ret = radix_tree_insert(&fs_info->fs_roots_radix, 1560cb517eabSMiao Xie (unsigned long)root->root_key.objectid, 1561cb517eabSMiao Xie root); 1562cb517eabSMiao Xie if (ret == 0) 1563cb517eabSMiao Xie root->in_radix = 1; 1564cb517eabSMiao Xie spin_unlock(&fs_info->fs_roots_radix_lock); 1565cb517eabSMiao Xie radix_tree_preload_end(); 1566cb517eabSMiao Xie 1567cb517eabSMiao Xie return ret; 1568cb517eabSMiao Xie } 1569cb517eabSMiao Xie 1570edbd8d4eSChris Mason struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 1571edbd8d4eSChris Mason struct btrfs_key *location) 15725eda7b5eSChris Mason { 15735eda7b5eSChris Mason struct btrfs_root *root; 15745eda7b5eSChris Mason int ret; 15755eda7b5eSChris Mason 1576edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1577edbd8d4eSChris Mason return fs_info->tree_root; 1578edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1579edbd8d4eSChris Mason return fs_info->extent_root; 15808f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15818f18cf13SChris Mason return fs_info->chunk_root; 15828f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15838f18cf13SChris Mason return fs_info->dev_root; 15840403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15850403e47eSYan Zheng return fs_info->csum_root; 1586bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1587bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1588bcef60f2SArne Jansen ERR_PTR(-ENOENT); 1589f7a81ea4SStefan Behrens if (location->objectid == BTRFS_UUID_TREE_OBJECTID) 1590f7a81ea4SStefan Behrens return fs_info->uuid_root ? fs_info->uuid_root : 1591f7a81ea4SStefan Behrens ERR_PTR(-ENOENT); 15924df27c4dSYan, Zheng again: 1593cb517eabSMiao Xie root = btrfs_lookup_fs_root(fs_info, location->objectid); 15945eda7b5eSChris Mason if (root) 15955eda7b5eSChris Mason return root; 15965eda7b5eSChris Mason 1597cb517eabSMiao Xie root = btrfs_read_fs_root(fs_info->tree_root, location); 15985eda7b5eSChris Mason if (IS_ERR(root)) 15995eda7b5eSChris Mason return root; 16003394e160SChris Mason 1601d68fc57bSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) { 1602d68fc57bSYan, Zheng ret = -ENOENT; 1603d68fc57bSYan, Zheng goto fail; 1604d68fc57bSYan, Zheng } 1605d68fc57bSYan, Zheng 1606cb517eabSMiao Xie ret = btrfs_init_fs_root(root); 1607cb517eabSMiao Xie if (ret) 1608cb517eabSMiao Xie goto fail; 1609cb517eabSMiao Xie 1610d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1611d68fc57bSYan, Zheng if (ret < 0) 1612d68fc57bSYan, Zheng goto fail; 1613d68fc57bSYan, Zheng if (ret == 0) 1614d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1615d68fc57bSYan, Zheng 1616cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root); 16170f7d52f4SChris Mason if (ret) { 16184df27c4dSYan, Zheng if (ret == -EEXIST) { 16194df27c4dSYan, Zheng free_fs_root(root); 16204df27c4dSYan, Zheng goto again; 16210f7d52f4SChris Mason } 16224df27c4dSYan, Zheng goto fail; 16234df27c4dSYan, Zheng } 1624edbd8d4eSChris Mason return root; 16254df27c4dSYan, Zheng fail: 16264df27c4dSYan, Zheng free_fs_root(root); 16274df27c4dSYan, Zheng return ERR_PTR(ret); 1628edbd8d4eSChris Mason } 1629edbd8d4eSChris Mason 163004160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 163104160088SChris Mason { 163204160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 163304160088SChris Mason int ret = 0; 163404160088SChris Mason struct btrfs_device *device; 163504160088SChris Mason struct backing_dev_info *bdi; 1636b7967db7SChris Mason 16371f78160cSXiao Guangrong rcu_read_lock(); 16381f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1639dfe25020SChris Mason if (!device->bdev) 1640dfe25020SChris Mason continue; 164104160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 164204160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 164304160088SChris Mason ret = 1; 164404160088SChris Mason break; 164504160088SChris Mason } 164604160088SChris Mason } 16471f78160cSXiao Guangrong rcu_read_unlock(); 164804160088SChris Mason return ret; 164904160088SChris Mason } 165004160088SChris Mason 165138b66988SChris Mason /* 1652ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1653ad081f14SJens Axboe * bdi again. 1654ad081f14SJens Axboe */ 165504160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 165604160088SChris Mason { 1657ad081f14SJens Axboe int err; 1658ad081f14SJens Axboe 1659ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1660e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1661ad081f14SJens Axboe if (err) 1662ad081f14SJens Axboe return err; 1663ad081f14SJens Axboe 16644575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 166504160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 166604160088SChris Mason bdi->congested_data = info; 166704160088SChris Mason return 0; 166804160088SChris Mason } 166904160088SChris Mason 16708b712842SChris Mason /* 16718b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16728b712842SChris Mason * functions. This is where read checksum verification actually happens 16738b712842SChris Mason */ 16748b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1675ce9adaa5SChris Mason { 1676ce9adaa5SChris Mason struct bio *bio; 16778b712842SChris Mason struct end_io_wq *end_io_wq; 16788b712842SChris Mason struct btrfs_fs_info *fs_info; 1679ce9adaa5SChris Mason int error; 1680ce9adaa5SChris Mason 16818b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1682ce9adaa5SChris Mason bio = end_io_wq->bio; 16838b712842SChris Mason fs_info = end_io_wq->info; 16848b712842SChris Mason 1685ce9adaa5SChris Mason error = end_io_wq->error; 1686ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1687ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1688ce9adaa5SChris Mason kfree(end_io_wq); 1689ce9adaa5SChris Mason bio_endio(bio, error); 1690ce9adaa5SChris Mason } 169144b8bd7eSChris Mason 1692a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1693a74a4b97SChris Mason { 1694a74a4b97SChris Mason struct btrfs_root *root = arg; 1695d0278245SMiao Xie int again; 1696a74a4b97SChris Mason 1697a74a4b97SChris Mason do { 1698d0278245SMiao Xie again = 0; 16999d1a2a3aSDavid Sterba 1700d0278245SMiao Xie /* Make the cleaner go to sleep early. */ 1701babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) 1702d0278245SMiao Xie goto sleep; 1703d0278245SMiao Xie 1704d0278245SMiao Xie if (!mutex_trylock(&root->fs_info->cleaner_mutex)) 1705d0278245SMiao Xie goto sleep; 1706d0278245SMiao Xie 1707dc7f370cSMiao Xie /* 1708dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs 1709dc7f370cSMiao Xie * during the above check and trylock. 1710dc7f370cSMiao Xie */ 1711babbf170SMiao Xie if (btrfs_need_cleaner_sleep(root)) { 1712dc7f370cSMiao Xie mutex_unlock(&root->fs_info->cleaner_mutex); 1713dc7f370cSMiao Xie goto sleep; 1714dc7f370cSMiao Xie } 1715dc7f370cSMiao Xie 171624bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 17179d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 1718a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 1719a74a4b97SChris Mason 1720d0278245SMiao Xie /* 172105323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount, 172205323cd1SMiao Xie * needn't do anything special here. 1723d0278245SMiao Xie */ 1724d0278245SMiao Xie btrfs_run_defrag_inodes(root->fs_info); 1725d0278245SMiao Xie sleep: 17269d1a2a3aSDavid Sterba if (!try_to_freeze() && !again) { 1727a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17288929ecfaSYan, Zheng if (!kthread_should_stop()) 1729a74a4b97SChris Mason schedule(); 1730a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1731a74a4b97SChris Mason } 1732a74a4b97SChris Mason } while (!kthread_should_stop()); 1733a74a4b97SChris Mason return 0; 1734a74a4b97SChris Mason } 1735a74a4b97SChris Mason 1736a74a4b97SChris Mason static int transaction_kthread(void *arg) 1737a74a4b97SChris Mason { 1738a74a4b97SChris Mason struct btrfs_root *root = arg; 1739a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1740a74a4b97SChris Mason struct btrfs_transaction *cur; 17418929ecfaSYan, Zheng u64 transid; 1742a74a4b97SChris Mason unsigned long now; 1743a74a4b97SChris Mason unsigned long delay; 1744914b2007SJan Kara bool cannot_commit; 1745a74a4b97SChris Mason 1746a74a4b97SChris Mason do { 1747914b2007SJan Kara cannot_commit = false; 17488b87dc17SDavid Sterba delay = HZ * root->fs_info->commit_interval; 1749a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1750a74a4b97SChris Mason 1751a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1752a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1753a74a4b97SChris Mason if (!cur) { 1754a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1755a74a4b97SChris Mason goto sleep; 1756a74a4b97SChris Mason } 175731153d81SYan Zheng 1758a74a4b97SChris Mason now = get_seconds(); 17594a9d8bdeSMiao Xie if (cur->state < TRANS_STATE_BLOCKED && 17608b87dc17SDavid Sterba (now < cur->start_time || 17618b87dc17SDavid Sterba now - cur->start_time < root->fs_info->commit_interval)) { 1762a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1763a74a4b97SChris Mason delay = HZ * 5; 1764a74a4b97SChris Mason goto sleep; 1765a74a4b97SChris Mason } 17668929ecfaSYan, Zheng transid = cur->transid; 1767a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 176856bec294SChris Mason 176979787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1770354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1771914b2007SJan Kara if (IS_ERR(trans)) { 1772354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1773914b2007SJan Kara cannot_commit = true; 177479787eaaSJeff Mahoney goto sleep; 1775914b2007SJan Kara } 17768929ecfaSYan, Zheng if (transid == trans->transid) { 177779787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 17788929ecfaSYan, Zheng } else { 17798929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 17808929ecfaSYan, Zheng } 1781a74a4b97SChris Mason sleep: 1782a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1783a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1784a74a4b97SChris Mason 1785a0acae0eSTejun Heo if (!try_to_freeze()) { 1786a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17878929ecfaSYan, Zheng if (!kthread_should_stop() && 1788914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1789914b2007SJan Kara cannot_commit)) 1790a74a4b97SChris Mason schedule_timeout(delay); 1791a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1792a74a4b97SChris Mason } 1793a74a4b97SChris Mason } while (!kthread_should_stop()); 1794a74a4b97SChris Mason return 0; 1795a74a4b97SChris Mason } 1796a74a4b97SChris Mason 1797af31f5e5SChris Mason /* 1798af31f5e5SChris Mason * this will find the highest generation in the array of 1799af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1800af31f5e5SChris Mason * or -1 if we can't find anything. 1801af31f5e5SChris Mason * 1802af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1803af31f5e5SChris Mason * generation of the latest root in the array with the generation 1804af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1805af31f5e5SChris Mason */ 1806af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1807af31f5e5SChris Mason { 1808af31f5e5SChris Mason u64 cur; 1809af31f5e5SChris Mason int newest_index = -1; 1810af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1811af31f5e5SChris Mason int i; 1812af31f5e5SChris Mason 1813af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1814af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1815af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1816af31f5e5SChris Mason if (cur == newest_gen) 1817af31f5e5SChris Mason newest_index = i; 1818af31f5e5SChris Mason } 1819af31f5e5SChris Mason 1820af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1821af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1822af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1823af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1824af31f5e5SChris Mason if (cur == newest_gen) 1825af31f5e5SChris Mason newest_index = 0; 1826af31f5e5SChris Mason } 1827af31f5e5SChris Mason return newest_index; 1828af31f5e5SChris Mason } 1829af31f5e5SChris Mason 1830af31f5e5SChris Mason 1831af31f5e5SChris Mason /* 1832af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1833af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1834af31f5e5SChris Mason * field in the fs_info struct 1835af31f5e5SChris Mason */ 1836af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1837af31f5e5SChris Mason u64 newest_gen) 1838af31f5e5SChris Mason { 1839af31f5e5SChris Mason int newest_index = -1; 1840af31f5e5SChris Mason 1841af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1842af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1843af31f5e5SChris Mason if (newest_index == -1) { 1844af31f5e5SChris Mason info->backup_root_index = 0; 1845af31f5e5SChris Mason } else { 1846af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1847af31f5e5SChris Mason } 1848af31f5e5SChris Mason } 1849af31f5e5SChris Mason 1850af31f5e5SChris Mason /* 1851af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1852af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1853af31f5e5SChris Mason * done 1854af31f5e5SChris Mason */ 1855af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1856af31f5e5SChris Mason { 1857af31f5e5SChris Mason int next_backup; 1858af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1859af31f5e5SChris Mason int last_backup; 1860af31f5e5SChris Mason 1861af31f5e5SChris Mason next_backup = info->backup_root_index; 1862af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1863af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1864af31f5e5SChris Mason 1865af31f5e5SChris Mason /* 1866af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1867af31f5e5SChris Mason * this happens only at umount 1868af31f5e5SChris Mason */ 1869af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1870af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1871af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1872af31f5e5SChris Mason next_backup = last_backup; 1873af31f5e5SChris Mason 1874af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1875af31f5e5SChris Mason 1876af31f5e5SChris Mason /* 1877af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1878af31f5e5SChris Mason * regardless of which ones we use today 1879af31f5e5SChris Mason */ 1880af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1881af31f5e5SChris Mason 1882af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1883af31f5e5SChris Mason 1884af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1885af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1886af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1887af31f5e5SChris Mason 1888af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1889af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1890af31f5e5SChris Mason 1891af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1892af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1893af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1894af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1895af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1896af31f5e5SChris Mason 1897af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1898af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1899af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1900af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1901af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1902af31f5e5SChris Mason 19037c7e82a7SChris Mason /* 19047c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 19057c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 19067c7e82a7SChris Mason */ 19077c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 19087c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 19097c7e82a7SChris Mason info->fs_root->node->start); 1910af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1911af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1912af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1913af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 19147c7e82a7SChris Mason } 1915af31f5e5SChris Mason 1916af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1917af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1918af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1919af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1920af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1921af31f5e5SChris Mason 1922af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1923af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1924af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1925af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1926af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1927af31f5e5SChris Mason 1928af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1929af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1930af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1931af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1932af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1933af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1934af31f5e5SChris Mason 1935af31f5e5SChris Mason /* 1936af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1937af31f5e5SChris Mason * for the next commit 1938af31f5e5SChris Mason */ 1939af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1940af31f5e5SChris Mason &info->super_for_commit->super_roots, 1941af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1942af31f5e5SChris Mason } 1943af31f5e5SChris Mason 1944af31f5e5SChris Mason /* 1945af31f5e5SChris Mason * this copies info out of the root backup array and back into 1946af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1947af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1948af31f5e5SChris Mason * tried and the last backup index you used. 1949af31f5e5SChris Mason * 1950af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1951af31f5e5SChris Mason */ 1952af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1953af31f5e5SChris Mason struct btrfs_super_block *super, 1954af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1955af31f5e5SChris Mason { 1956af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1957af31f5e5SChris Mason int newest = *backup_index; 1958af31f5e5SChris Mason 1959af31f5e5SChris Mason if (*num_backups_tried == 0) { 1960af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1961af31f5e5SChris Mason 1962af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1963af31f5e5SChris Mason if (newest == -1) 1964af31f5e5SChris Mason return -1; 1965af31f5e5SChris Mason 1966af31f5e5SChris Mason *backup_index = newest; 1967af31f5e5SChris Mason *num_backups_tried = 1; 1968af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1969af31f5e5SChris Mason /* we've tried all the backups, all done */ 1970af31f5e5SChris Mason return -1; 1971af31f5e5SChris Mason } else { 1972af31f5e5SChris Mason /* jump to the next oldest backup */ 1973af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1974af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1975af31f5e5SChris Mason *backup_index = newest; 1976af31f5e5SChris Mason *num_backups_tried += 1; 1977af31f5e5SChris Mason } 1978af31f5e5SChris Mason root_backup = super->super_roots + newest; 1979af31f5e5SChris Mason 1980af31f5e5SChris Mason btrfs_set_super_generation(super, 1981af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1982af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1983af31f5e5SChris Mason btrfs_set_super_root_level(super, 1984af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1985af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1986af31f5e5SChris Mason 1987af31f5e5SChris Mason /* 1988af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1989af31f5e5SChris Mason * need a fsck 1990af31f5e5SChris Mason */ 1991af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1992af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1993af31f5e5SChris Mason return 0; 1994af31f5e5SChris Mason } 1995af31f5e5SChris Mason 19967abadb64SLiu Bo /* helper to cleanup workers */ 19977abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19987abadb64SLiu Bo { 19997abadb64SLiu Bo btrfs_stop_workers(&fs_info->generic_worker); 20007abadb64SLiu Bo btrfs_stop_workers(&fs_info->fixup_workers); 20017abadb64SLiu Bo btrfs_stop_workers(&fs_info->delalloc_workers); 20027abadb64SLiu Bo btrfs_stop_workers(&fs_info->workers); 20037abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_workers); 20047abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_workers); 20057abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_raid56_workers); 20067abadb64SLiu Bo btrfs_stop_workers(&fs_info->rmw_workers); 20077abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_write_workers); 20087abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_write_workers); 20097abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_freespace_worker); 20107abadb64SLiu Bo btrfs_stop_workers(&fs_info->submit_workers); 20117abadb64SLiu Bo btrfs_stop_workers(&fs_info->delayed_workers); 20127abadb64SLiu Bo btrfs_stop_workers(&fs_info->caching_workers); 20137abadb64SLiu Bo btrfs_stop_workers(&fs_info->readahead_workers); 20147abadb64SLiu Bo btrfs_stop_workers(&fs_info->flush_workers); 20152f232036SJan Schmidt btrfs_stop_workers(&fs_info->qgroup_rescan_workers); 20167abadb64SLiu Bo } 20177abadb64SLiu Bo 2018af31f5e5SChris Mason /* helper to cleanup tree roots */ 2019af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 2020af31f5e5SChris Mason { 2021af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 2022af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 2023655b09feSJosef Bacik info->tree_root->node = NULL; 2024655b09feSJosef Bacik info->tree_root->commit_root = NULL; 2025655b09feSJosef Bacik 2026655b09feSJosef Bacik if (info->dev_root) { 2027af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 2028af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 2029655b09feSJosef Bacik info->dev_root->node = NULL; 2030655b09feSJosef Bacik info->dev_root->commit_root = NULL; 2031655b09feSJosef Bacik } 2032655b09feSJosef Bacik if (info->extent_root) { 2033af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 2034af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 2035655b09feSJosef Bacik info->extent_root->node = NULL; 2036655b09feSJosef Bacik info->extent_root->commit_root = NULL; 2037655b09feSJosef Bacik } 2038655b09feSJosef Bacik if (info->csum_root) { 2039af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 2040af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 2041655b09feSJosef Bacik info->csum_root->node = NULL; 2042655b09feSJosef Bacik info->csum_root->commit_root = NULL; 2043655b09feSJosef Bacik } 2044bcef60f2SArne Jansen if (info->quota_root) { 2045bcef60f2SArne Jansen free_extent_buffer(info->quota_root->node); 2046bcef60f2SArne Jansen free_extent_buffer(info->quota_root->commit_root); 2047bcef60f2SArne Jansen info->quota_root->node = NULL; 2048bcef60f2SArne Jansen info->quota_root->commit_root = NULL; 2049bcef60f2SArne Jansen } 2050f7a81ea4SStefan Behrens if (info->uuid_root) { 2051f7a81ea4SStefan Behrens free_extent_buffer(info->uuid_root->node); 2052f7a81ea4SStefan Behrens free_extent_buffer(info->uuid_root->commit_root); 2053f7a81ea4SStefan Behrens info->uuid_root->node = NULL; 2054f7a81ea4SStefan Behrens info->uuid_root->commit_root = NULL; 2055f7a81ea4SStefan Behrens } 2056af31f5e5SChris Mason if (chunk_root) { 2057af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 2058af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 2059af31f5e5SChris Mason info->chunk_root->node = NULL; 2060af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 2061af31f5e5SChris Mason } 2062af31f5e5SChris Mason } 2063af31f5e5SChris Mason 2064171f6537SJosef Bacik static void del_fs_roots(struct btrfs_fs_info *fs_info) 2065171f6537SJosef Bacik { 2066171f6537SJosef Bacik int ret; 2067171f6537SJosef Bacik struct btrfs_root *gang[8]; 2068171f6537SJosef Bacik int i; 2069171f6537SJosef Bacik 2070171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2071171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2072171f6537SJosef Bacik struct btrfs_root, root_list); 2073171f6537SJosef Bacik list_del(&gang[0]->root_list); 2074171f6537SJosef Bacik 2075171f6537SJosef Bacik if (gang[0]->in_radix) { 2076cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[0]); 2077171f6537SJosef Bacik } else { 2078171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2079171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2080b0feb9d9SMiao Xie btrfs_put_fs_root(gang[0]); 2081171f6537SJosef Bacik } 2082171f6537SJosef Bacik } 2083171f6537SJosef Bacik 2084171f6537SJosef Bacik while (1) { 2085171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2086171f6537SJosef Bacik (void **)gang, 0, 2087171f6537SJosef Bacik ARRAY_SIZE(gang)); 2088171f6537SJosef Bacik if (!ret) 2089171f6537SJosef Bacik break; 2090171f6537SJosef Bacik for (i = 0; i < ret; i++) 2091cb517eabSMiao Xie btrfs_drop_and_free_fs_root(fs_info, gang[i]); 2092171f6537SJosef Bacik } 2093171f6537SJosef Bacik } 2094af31f5e5SChris Mason 2095ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2096dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2097dfe25020SChris Mason char *options) 2098eb60ceacSChris Mason { 2099db94535dSChris Mason u32 sectorsize; 2100db94535dSChris Mason u32 nodesize; 2101db94535dSChris Mason u32 leafsize; 2102db94535dSChris Mason u32 blocksize; 210387ee04ebSChris Mason u32 stripesize; 210484234f3aSYan Zheng u64 generation; 2105f2b636e8SJosef Bacik u64 features; 21063de4586cSChris Mason struct btrfs_key location; 2107a061fc8dSChris Mason struct buffer_head *bh; 21084d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2109815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2110f84a8bd6SAl Viro struct btrfs_root *tree_root; 21114d34b278SIlya Dryomov struct btrfs_root *extent_root; 21124d34b278SIlya Dryomov struct btrfs_root *csum_root; 21134d34b278SIlya Dryomov struct btrfs_root *chunk_root; 21144d34b278SIlya Dryomov struct btrfs_root *dev_root; 2115bcef60f2SArne Jansen struct btrfs_root *quota_root; 2116f7a81ea4SStefan Behrens struct btrfs_root *uuid_root; 2117e02119d5SChris Mason struct btrfs_root *log_tree_root; 2118eb60ceacSChris Mason int ret; 2119e58ca020SYan int err = -EINVAL; 2120af31f5e5SChris Mason int num_backups_tried = 0; 2121af31f5e5SChris Mason int backup_index = 0; 2122f7a81ea4SStefan Behrens bool create_uuid_tree = false; 21234543df7eSChris Mason 2124f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 21256f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 2126cb517eabSMiao Xie if (!tree_root || !chunk_root) { 212739279cc3SChris Mason err = -ENOMEM; 212839279cc3SChris Mason goto fail; 212939279cc3SChris Mason } 213076dda93cSYan, Zheng 213176dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 213276dda93cSYan, Zheng if (ret) { 213376dda93cSYan, Zheng err = ret; 213476dda93cSYan, Zheng goto fail; 213576dda93cSYan, Zheng } 213676dda93cSYan, Zheng 213776dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 213876dda93cSYan, Zheng if (ret) { 213976dda93cSYan, Zheng err = ret; 214076dda93cSYan, Zheng goto fail_srcu; 214176dda93cSYan, Zheng } 214276dda93cSYan, Zheng 2143e2d84521SMiao Xie ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0); 2144e2d84521SMiao Xie if (ret) { 2145e2d84521SMiao Xie err = ret; 2146e2d84521SMiao Xie goto fail_bdi; 2147e2d84521SMiao Xie } 2148e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2149e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2150e2d84521SMiao Xie 2151963d678bSMiao Xie ret = percpu_counter_init(&fs_info->delalloc_bytes, 0); 2152963d678bSMiao Xie if (ret) { 2153963d678bSMiao Xie err = ret; 2154963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2155963d678bSMiao Xie } 2156963d678bSMiao Xie 215776dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 215876dda93cSYan, Zheng if (!fs_info->btree_inode) { 215976dda93cSYan, Zheng err = -ENOMEM; 2160963d678bSMiao Xie goto fail_delalloc_bytes; 216176dda93cSYan, Zheng } 216276dda93cSYan, Zheng 2163a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 21641561dedaSMiao Xie 216576dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 21668fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2167facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 216824bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2169eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots); 217011833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 2171eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock); 2172a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 217376dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 217424bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 21754cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 21762bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2177f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2178ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2179f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 21807585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2181de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 218219c00ddcSChris Mason 218358176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 21840b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 21856324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2186f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 21870b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 218866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 218966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 219066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 219166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 219266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 219366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 219466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 219566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 219666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2197cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2198771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 21998c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 22000986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 22014cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2202fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2203e20d96d6SChris Mason fs_info->sb = sb; 22046f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 22059ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 22064cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 22072bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2208f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 22098b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 2210c8b97818SChris Mason 221190519d66SArne Jansen /* readahead state */ 221290519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 221390519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 22142c90e5d6SChris Mason 2215b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2216b34b086cSChris Mason num_online_cpus() + 2, 8); 22170afbaf8cSChris Mason 2218199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots); 2219199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock); 222016cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 222116cdcec7SMiao Xie GFP_NOFS); 222216cdcec7SMiao Xie if (!fs_info->delayed_root) { 222316cdcec7SMiao Xie err = -ENOMEM; 222416cdcec7SMiao Xie goto fail_iput; 222516cdcec7SMiao Xie } 222616cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 22273eaa2885SChris Mason 2228a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2229a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2230a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2231a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2232a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2233a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2234a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2235a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 223621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 223721adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 223821adbd5cSStefan Behrens #endif 2239a2de733cSArne Jansen 2240c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2241c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2242837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2243837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2244a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2245c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2246837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2247a061fc8dSChris Mason 22480afbaf8cSChris Mason sb->s_blocksize = 4096; 22490afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 225032a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 22510afbaf8cSChris Mason 225276dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2253bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 22540afbaf8cSChris Mason /* 22550afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 22560afbaf8cSChris Mason * the real end of the address space is determined by all of 22570afbaf8cSChris Mason * the devices in the system 22580afbaf8cSChris Mason */ 22590afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2260d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 226104160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 226204160088SChris Mason 22635d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2264d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2265f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 22660b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2267a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 22680da5468fSChris Mason 2269d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2270d1310b2eSChris Mason 22710f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 22720f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 22730f7d52f4SChris Mason sizeof(struct btrfs_key)); 227472ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 227572ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 227622b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 227739279cc3SChris Mason 2278e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 22796bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2280a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2281925baeddSChris Mason 228211833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2283f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 228411833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2285f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 228611833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2287e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2288f9295749SChris Mason 2289d98237b3SChris Mason 22905a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 22919ffba8cdSJosef Bacik mutex_init(&fs_info->ordered_extent_flush_mutex); 229230ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2293925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2294925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2295a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2296a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2297276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2298c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 229976dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2300e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2301e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2302e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2303e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2304e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2305fa9c0d79SChris Mason 2306416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2307f2f6ed3dSWang Shilong mutex_init(&fs_info->qgroup_ioctl_lock); 2308416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2309416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2310416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2311416ac51dSArne Jansen fs_info->quota_enabled = 0; 2312416ac51dSArne Jansen fs_info->pending_quota_state = 0; 23131e8f9158SWang Shilong fs_info->qgroup_ulist = NULL; 23142f232036SJan Schmidt mutex_init(&fs_info->qgroup_rescan_lock); 2315416ac51dSArne Jansen 2316fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2317fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2318fa9c0d79SChris Mason 23197d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 23203768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2321bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 23224854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 23239a8dd150SChris Mason 232453b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 232553b381b3SDavid Woodhouse if (ret) { 232683c8266aSDavid Sterba err = ret; 232753b381b3SDavid Woodhouse goto fail_alloc; 232853b381b3SDavid Woodhouse } 232953b381b3SDavid Woodhouse 23300b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 23312c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 23327eccb903SChris Mason 23333c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 23341104a885SDavid Sterba 23351104a885SDavid Sterba /* 23361104a885SDavid Sterba * Read super block and check the signature bytes only 23371104a885SDavid Sterba */ 2338a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 233920b45077SDave Young if (!bh) { 234020b45077SDave Young err = -EINVAL; 234116cdcec7SMiao Xie goto fail_alloc; 234220b45077SDave Young } 234339279cc3SChris Mason 23441104a885SDavid Sterba /* 23451104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 23461104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 23471104a885SDavid Sterba */ 23481104a885SDavid Sterba if (btrfs_check_super_csum(bh->b_data)) { 23491104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock checksum mismatch\n"); 23501104a885SDavid Sterba err = -EINVAL; 23511104a885SDavid Sterba goto fail_alloc; 23521104a885SDavid Sterba } 23531104a885SDavid Sterba 23541104a885SDavid Sterba /* 23551104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 23561104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 23571104a885SDavid Sterba * the whole block of INFO_SIZE 23581104a885SDavid Sterba */ 23596c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 23606c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 23616c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2362a061fc8dSChris Mason brelse(bh); 23635f39d397SChris Mason 23646c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 23650b86a832SChris Mason 23661104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 23671104a885SDavid Sterba if (ret) { 23681104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock contains fatal errors\n"); 23691104a885SDavid Sterba err = -EINVAL; 23701104a885SDavid Sterba goto fail_alloc; 23711104a885SDavid Sterba } 23721104a885SDavid Sterba 23736c41761fSDavid Sterba disk_super = fs_info->super_copy; 23740f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 237516cdcec7SMiao Xie goto fail_alloc; 23760f7d52f4SChris Mason 2377acce952bSliubo /* check FS state, whether FS is broken. */ 237887533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 237987533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2380acce952bSliubo 238175e7cb7fSLiu Bo /* 2382af31f5e5SChris Mason * run through our array of backup supers and setup 2383af31f5e5SChris Mason * our ring pointer to the oldest one 2384af31f5e5SChris Mason */ 2385af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2386af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2387af31f5e5SChris Mason 2388af31f5e5SChris Mason /* 238975e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 239075e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 239175e7cb7fSLiu Bo */ 239275e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 239375e7cb7fSLiu Bo 23942b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 23952b82032cSYan Zheng if (ret) { 23962b82032cSYan Zheng err = ret; 239716cdcec7SMiao Xie goto fail_alloc; 23982b82032cSYan Zheng } 2399dfe25020SChris Mason 2400f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2401f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2402f2b636e8SJosef Bacik if (features) { 2403f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2404f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 240521380931SJoel Becker (unsigned long long)features); 2406f2b636e8SJosef Bacik err = -EINVAL; 240716cdcec7SMiao Xie goto fail_alloc; 2408f2b636e8SJosef Bacik } 2409f2b636e8SJosef Bacik 2410727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2411727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2412727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2413727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2414727011e0SChris Mason btrfs_super_nodesize(disk_super), 2415727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2416727011e0SChris Mason err = -EINVAL; 2417727011e0SChris Mason goto fail_alloc; 2418727011e0SChris Mason } 2419727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2420727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2421727011e0SChris Mason "blocksize (%d) was too large\n", 2422727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2423727011e0SChris Mason err = -EINVAL; 2424727011e0SChris Mason goto fail_alloc; 2425727011e0SChris Mason } 2426727011e0SChris Mason 24275d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 24285d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 242969e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2430a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2431727011e0SChris Mason 24323173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 24333173a18fSJosef Bacik printk(KERN_ERR "btrfs: has skinny extents\n"); 24343173a18fSJosef Bacik 2435727011e0SChris Mason /* 2436727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2437727011e0SChris Mason * they are bigger than the page size 2438727011e0SChris Mason */ 2439727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2440727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2441727011e0SChris Mason printk(KERN_INFO "btrfs flagging fs with big metadata feature\n"); 2442727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2443727011e0SChris Mason } 2444727011e0SChris Mason 2445bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2446bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2447bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2448bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2449e2d84521SMiao Xie fs_info->dirty_metadata_batch = leafsize * (1 + ilog2(nr_cpu_ids)); 2450963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2451bc3f116fSChris Mason 2452bc3f116fSChris Mason /* 2453bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2454bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2455bc3f116fSChris Mason */ 2456bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2457bc3f116fSChris Mason (sectorsize != leafsize)) { 2458bc3f116fSChris Mason printk(KERN_WARNING "btrfs: unequal leaf/node/sector sizes " 2459bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2460bc3f116fSChris Mason sb->s_id); 2461bc3f116fSChris Mason goto fail_alloc; 2462bc3f116fSChris Mason } 2463bc3f116fSChris Mason 2464ceda0864SMiao Xie /* 2465ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2466ceda0864SMiao Xie * update the flag. 2467ceda0864SMiao Xie */ 24685d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 24695d4f98a2SYan Zheng 2470f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2471f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2472f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2473f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2474f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 247521380931SJoel Becker (unsigned long long)features); 2476f2b636e8SJosef Bacik err = -EINVAL; 247716cdcec7SMiao Xie goto fail_alloc; 2478f2b636e8SJosef Bacik } 247961d92c32SChris Mason 248061d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 248161d92c32SChris Mason "genwork", 1, NULL); 248261d92c32SChris Mason 24835443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 248461d92c32SChris Mason fs_info->thread_pool_size, 248561d92c32SChris Mason &fs_info->generic_worker); 2486c8b97818SChris Mason 2487771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 248861d92c32SChris Mason fs_info->thread_pool_size, 248961d92c32SChris Mason &fs_info->generic_worker); 2490771ed689SChris Mason 24918ccf6f19SMiao Xie btrfs_init_workers(&fs_info->flush_workers, "flush_delalloc", 24928ccf6f19SMiao Xie fs_info->thread_pool_size, 24938ccf6f19SMiao Xie &fs_info->generic_worker); 24948ccf6f19SMiao Xie 24955443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2496b720d209SChris Mason min_t(u64, fs_devices->num_devices, 249761d92c32SChris Mason fs_info->thread_pool_size), 249861d92c32SChris Mason &fs_info->generic_worker); 249961b49440SChris Mason 2500bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 2501bab39bf9SJosef Bacik 2, &fs_info->generic_worker); 2502bab39bf9SJosef Bacik 250361b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 250461b49440SChris Mason * likely that bios will be send down in a sane order to the 250561b49440SChris Mason * devices 250661b49440SChris Mason */ 250761b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 250853863232SChris Mason 2509771ed689SChris Mason fs_info->workers.idle_thresh = 16; 25104a69a410SChris Mason fs_info->workers.ordered = 1; 251161b49440SChris Mason 2512771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2513771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2514771ed689SChris Mason 251561d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 251661d92c32SChris Mason &fs_info->generic_worker); 25175443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 251861d92c32SChris Mason fs_info->thread_pool_size, 251961d92c32SChris Mason &fs_info->generic_worker); 2520d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 252161d92c32SChris Mason fs_info->thread_pool_size, 252261d92c32SChris Mason &fs_info->generic_worker); 2523cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 252461d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 252561d92c32SChris Mason &fs_info->generic_worker); 252653b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->endio_raid56_workers, 252753b381b3SDavid Woodhouse "endio-raid56", fs_info->thread_pool_size, 252853b381b3SDavid Woodhouse &fs_info->generic_worker); 252953b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->rmw_workers, 253053b381b3SDavid Woodhouse "rmw", fs_info->thread_pool_size, 253153b381b3SDavid Woodhouse &fs_info->generic_worker); 25325443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 253361d92c32SChris Mason fs_info->thread_pool_size, 253461d92c32SChris Mason &fs_info->generic_worker); 25350cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 25360cb59c99SJosef Bacik 1, &fs_info->generic_worker); 253716cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 253816cdcec7SMiao Xie fs_info->thread_pool_size, 253916cdcec7SMiao Xie &fs_info->generic_worker); 254090519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 254190519d66SArne Jansen fs_info->thread_pool_size, 254290519d66SArne Jansen &fs_info->generic_worker); 25432f232036SJan Schmidt btrfs_init_workers(&fs_info->qgroup_rescan_workers, "qgroup-rescan", 1, 25442f232036SJan Schmidt &fs_info->generic_worker); 254561b49440SChris Mason 254661b49440SChris Mason /* 254761b49440SChris Mason * endios are largely parallel and should have a very 254861b49440SChris Mason * low idle thresh 254961b49440SChris Mason */ 255061b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2551b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 255253b381b3SDavid Woodhouse fs_info->endio_raid56_workers.idle_thresh = 4; 255353b381b3SDavid Woodhouse fs_info->rmw_workers.idle_thresh = 2; 2554b51912c9SChris Mason 25559042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 25569042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 255790519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 25589042846bSChris Mason 25590dc3b84aSJosef Bacik /* 25600dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 25610dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 25620dc3b84aSJosef Bacik */ 25630dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 25640dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 25650dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 25660dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 25670dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 25680dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 25690dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 257053b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->rmw_workers); 257153b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->endio_raid56_workers); 25720dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 25730dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 25740dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 25750dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 25760dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 25770dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 25788ccf6f19SMiao Xie ret |= btrfs_start_workers(&fs_info->flush_workers); 25792f232036SJan Schmidt ret |= btrfs_start_workers(&fs_info->qgroup_rescan_workers); 25800dc3b84aSJosef Bacik if (ret) { 2581fed425c7SIlya Dryomov err = -ENOMEM; 25820dc3b84aSJosef Bacik goto fail_sb_buffer; 25830dc3b84aSJosef Bacik } 25844543df7eSChris Mason 25854575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2586c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2587c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 25884575c9ccSChris Mason 2589db94535dSChris Mason tree_root->nodesize = nodesize; 2590db94535dSChris Mason tree_root->leafsize = leafsize; 2591db94535dSChris Mason tree_root->sectorsize = sectorsize; 259287ee04ebSChris Mason tree_root->stripesize = stripesize; 2593a061fc8dSChris Mason 2594a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2595a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2596db94535dSChris Mason 25973cae210fSQu Wenruo if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) { 2598d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 259939279cc3SChris Mason goto fail_sb_buffer; 260039279cc3SChris Mason } 260119c00ddcSChris Mason 26028d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 26038d082fb7SLiu Bo printk(KERN_WARNING "btrfs: Incompatible sector size(%lu) " 26048d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2605941b2ddfSKeith Mannthey goto fail_sb_buffer; 2606941b2ddfSKeith Mannthey } 2607941b2ddfSKeith Mannthey 2608925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2609e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2610925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 261184eed90fSChris Mason if (ret) { 2612d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2613d397712bSChris Mason "array on %s\n", sb->s_id); 26145d4f98a2SYan Zheng goto fail_sb_buffer; 261584eed90fSChris Mason } 26160b86a832SChris Mason 26170b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 26180b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 261984234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 26200b86a832SChris Mason 26210b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 26220b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 26230b86a832SChris Mason 26240b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 26250b86a832SChris Mason btrfs_super_chunk_root(disk_super), 262684234f3aSYan Zheng blocksize, generation); 2627416bc658SJosef Bacik if (!chunk_root->node || 2628416bc658SJosef Bacik !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 262983121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 263083121942SDavid Woodhouse sb->s_id); 2631af31f5e5SChris Mason goto fail_tree_roots; 263283121942SDavid Woodhouse } 26335d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 26345d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 26350b86a832SChris Mason 2636e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2637e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 2638e17cade2SChris Mason BTRFS_UUID_SIZE); 2639e17cade2SChris Mason 26400b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 26412b82032cSYan Zheng if (ret) { 2642d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2643d397712bSChris Mason sb->s_id); 2644af31f5e5SChris Mason goto fail_tree_roots; 26452b82032cSYan Zheng } 26460b86a832SChris Mason 26478dabb742SStefan Behrens /* 26488dabb742SStefan Behrens * keep the device that is marked to be the target device for the 26498dabb742SStefan Behrens * dev_replace procedure 26508dabb742SStefan Behrens */ 26518dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2652dfe25020SChris Mason 2653a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2654a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2655a6b0d5c8SChris Mason sb->s_id); 2656a6b0d5c8SChris Mason goto fail_tree_roots; 2657a6b0d5c8SChris Mason } 2658a6b0d5c8SChris Mason 2659af31f5e5SChris Mason retry_root_backup: 2660db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2661db94535dSChris Mason btrfs_super_root_level(disk_super)); 266284234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 26630b86a832SChris Mason 2664e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2665db94535dSChris Mason btrfs_super_root(disk_super), 266684234f3aSYan Zheng blocksize, generation); 2667af31f5e5SChris Mason if (!tree_root->node || 2668af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 266983121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 267083121942SDavid Woodhouse sb->s_id); 2671af31f5e5SChris Mason 2672af31f5e5SChris Mason goto recovery_tree_root; 267383121942SDavid Woodhouse } 2674af31f5e5SChris Mason 26755d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 26765d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 2677db94535dSChris Mason 2678cb517eabSMiao Xie location.objectid = BTRFS_EXTENT_TREE_OBJECTID; 2679cb517eabSMiao Xie location.type = BTRFS_ROOT_ITEM_KEY; 2680cb517eabSMiao Xie location.offset = 0; 2681cb517eabSMiao Xie 2682cb517eabSMiao Xie extent_root = btrfs_read_tree_root(tree_root, &location); 2683cb517eabSMiao Xie if (IS_ERR(extent_root)) { 2684cb517eabSMiao Xie ret = PTR_ERR(extent_root); 2685af31f5e5SChris Mason goto recovery_tree_root; 2686cb517eabSMiao Xie } 26870b86a832SChris Mason extent_root->track_dirty = 1; 2688cb517eabSMiao Xie fs_info->extent_root = extent_root; 26890b86a832SChris Mason 2690cb517eabSMiao Xie location.objectid = BTRFS_DEV_TREE_OBJECTID; 2691cb517eabSMiao Xie dev_root = btrfs_read_tree_root(tree_root, &location); 2692cb517eabSMiao Xie if (IS_ERR(dev_root)) { 2693cb517eabSMiao Xie ret = PTR_ERR(dev_root); 2694af31f5e5SChris Mason goto recovery_tree_root; 2695cb517eabSMiao Xie } 26965d4f98a2SYan Zheng dev_root->track_dirty = 1; 2697cb517eabSMiao Xie fs_info->dev_root = dev_root; 2698cb517eabSMiao Xie btrfs_init_devices_late(fs_info); 26993768f368SChris Mason 2700cb517eabSMiao Xie location.objectid = BTRFS_CSUM_TREE_OBJECTID; 2701cb517eabSMiao Xie csum_root = btrfs_read_tree_root(tree_root, &location); 2702cb517eabSMiao Xie if (IS_ERR(csum_root)) { 2703cb517eabSMiao Xie ret = PTR_ERR(csum_root); 2704af31f5e5SChris Mason goto recovery_tree_root; 2705cb517eabSMiao Xie } 2706d20f7043SChris Mason csum_root->track_dirty = 1; 2707cb517eabSMiao Xie fs_info->csum_root = csum_root; 2708d20f7043SChris Mason 2709cb517eabSMiao Xie location.objectid = BTRFS_QUOTA_TREE_OBJECTID; 2710cb517eabSMiao Xie quota_root = btrfs_read_tree_root(tree_root, &location); 2711cb517eabSMiao Xie if (!IS_ERR(quota_root)) { 2712bcef60f2SArne Jansen quota_root->track_dirty = 1; 2713bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2714bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2715cb517eabSMiao Xie fs_info->quota_root = quota_root; 2716bcef60f2SArne Jansen } 2717bcef60f2SArne Jansen 2718f7a81ea4SStefan Behrens location.objectid = BTRFS_UUID_TREE_OBJECTID; 2719f7a81ea4SStefan Behrens uuid_root = btrfs_read_tree_root(tree_root, &location); 2720f7a81ea4SStefan Behrens if (IS_ERR(uuid_root)) { 2721f7a81ea4SStefan Behrens ret = PTR_ERR(uuid_root); 2722f7a81ea4SStefan Behrens if (ret != -ENOENT) 2723f7a81ea4SStefan Behrens goto recovery_tree_root; 2724f7a81ea4SStefan Behrens create_uuid_tree = true; 2725f7a81ea4SStefan Behrens } else { 2726f7a81ea4SStefan Behrens uuid_root->track_dirty = 1; 2727f7a81ea4SStefan Behrens fs_info->uuid_root = uuid_root; 2728f7a81ea4SStefan Behrens } 2729f7a81ea4SStefan Behrens 27308929ecfaSYan, Zheng fs_info->generation = generation; 27318929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 27328929ecfaSYan, Zheng 273368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 273468310a5eSIlya Dryomov if (ret) { 273568310a5eSIlya Dryomov printk(KERN_WARNING "btrfs: failed to recover balance\n"); 273668310a5eSIlya Dryomov goto fail_block_groups; 273768310a5eSIlya Dryomov } 273868310a5eSIlya Dryomov 2739733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2740733f4fbbSStefan Behrens if (ret) { 2741733f4fbbSStefan Behrens printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2742733f4fbbSStefan Behrens ret); 2743733f4fbbSStefan Behrens goto fail_block_groups; 2744733f4fbbSStefan Behrens } 2745733f4fbbSStefan Behrens 27468dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 27478dabb742SStefan Behrens if (ret) { 27488dabb742SStefan Behrens pr_err("btrfs: failed to init dev_replace: %d\n", ret); 27498dabb742SStefan Behrens goto fail_block_groups; 27508dabb742SStefan Behrens } 27518dabb742SStefan Behrens 27528dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 27538dabb742SStefan Behrens 2754c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2755c59021f8Sliubo if (ret) { 2756c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2757c59021f8Sliubo goto fail_block_groups; 2758c59021f8Sliubo } 2759c59021f8Sliubo 27601b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 27611b1d1f66SJosef Bacik if (ret) { 27621b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 27631b1d1f66SJosef Bacik goto fail_block_groups; 27641b1d1f66SJosef Bacik } 27655af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 27665af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2767292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2768292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2769292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2770292fd7fcSStefan Behrens printk(KERN_WARNING 2771292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable mount is not allowed\n"); 2772292fd7fcSStefan Behrens goto fail_block_groups; 2773292fd7fcSStefan Behrens } 27749078a3e1SChris Mason 2775a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2776a74a4b97SChris Mason "btrfs-cleaner"); 277757506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 27781b1d1f66SJosef Bacik goto fail_block_groups; 2779a74a4b97SChris Mason 2780a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2781a74a4b97SChris Mason tree_root, 2782a74a4b97SChris Mason "btrfs-transaction"); 278357506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 27843f157a2fSChris Mason goto fail_cleaner; 2785a74a4b97SChris Mason 2786c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2787c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2788c289811cSChris Mason !fs_info->fs_devices->rotating) { 2789c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2790c289811cSChris Mason "mode\n"); 2791c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2792c289811cSChris Mason } 2793c289811cSChris Mason 279421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 279521adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 279621adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 279721adbd5cSStefan Behrens btrfs_test_opt(tree_root, 279821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 279921adbd5cSStefan Behrens 1 : 0, 280021adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 280121adbd5cSStefan Behrens if (ret) 280221adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 280321adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 280421adbd5cSStefan Behrens } 280521adbd5cSStefan Behrens #endif 2806bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2807bcef60f2SArne Jansen if (ret) 2808bcef60f2SArne Jansen goto fail_trans_kthread; 280921adbd5cSStefan Behrens 2810acce952bSliubo /* do not make disk changes in broken FS */ 281168ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2812e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2813d18a2c44SChris Mason 28147c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2815d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2816d397712bSChris Mason "on RO media\n"); 28177c2ca468SChris Mason err = -EIO; 2818bcef60f2SArne Jansen goto fail_qgroup; 28197c2ca468SChris Mason } 2820e02119d5SChris Mason blocksize = 2821e02119d5SChris Mason btrfs_level_size(tree_root, 2822e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2823e02119d5SChris Mason 28246f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2825676e4c86SDan Carpenter if (!log_tree_root) { 2826676e4c86SDan Carpenter err = -ENOMEM; 2827bcef60f2SArne Jansen goto fail_qgroup; 2828676e4c86SDan Carpenter } 2829e02119d5SChris Mason 2830e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2831e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2832e02119d5SChris Mason 2833e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 283484234f3aSYan Zheng blocksize, 283584234f3aSYan Zheng generation + 1); 2836416bc658SJosef Bacik if (!log_tree_root->node || 2837416bc658SJosef Bacik !extent_buffer_uptodate(log_tree_root->node)) { 2838416bc658SJosef Bacik printk(KERN_ERR "btrfs: failed to read log tree\n"); 2839416bc658SJosef Bacik free_extent_buffer(log_tree_root->node); 2840416bc658SJosef Bacik kfree(log_tree_root); 2841416bc658SJosef Bacik goto fail_trans_kthread; 2842416bc658SJosef Bacik } 284379787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2844e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 284579787eaaSJeff Mahoney if (ret) { 284679787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 284779787eaaSJeff Mahoney "Failed to recover log tree"); 284879787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 284979787eaaSJeff Mahoney kfree(log_tree_root); 285079787eaaSJeff Mahoney goto fail_trans_kthread; 285179787eaaSJeff Mahoney } 2852e556ce2cSYan Zheng 2853e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2854e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 285579787eaaSJeff Mahoney if (ret) 285679787eaaSJeff Mahoney goto fail_trans_kthread; 2857e556ce2cSYan Zheng } 2858e02119d5SChris Mason } 28591a40e23bSZheng Yan 286076dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 286179787eaaSJeff Mahoney if (ret) 286279787eaaSJeff Mahoney goto fail_trans_kthread; 286376dda93cSYan, Zheng 28647c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2865d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 286644c44af2SIlya Dryomov if (ret) 286744c44af2SIlya Dryomov goto fail_trans_kthread; 2868d68fc57bSYan, Zheng 28695d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2870d7ce5843SMiao Xie if (ret < 0) { 2871d7ce5843SMiao Xie printk(KERN_WARNING 2872d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2873d7ce5843SMiao Xie err = -EINVAL; 2874bcef60f2SArne Jansen goto fail_qgroup; 2875d7ce5843SMiao Xie } 28767c2ca468SChris Mason } 28771a40e23bSZheng Yan 28783de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 28793de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 2880cb517eabSMiao Xie location.offset = 0; 28813de4586cSChris Mason 28823de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 28833140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 28843140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2885bcef60f2SArne Jansen goto fail_qgroup; 28863140c9a3SDan Carpenter } 2887c289811cSChris Mason 28882b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 28892b6ba629SIlya Dryomov return 0; 28902b6ba629SIlya Dryomov 2891e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 28922b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 28932b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 28942b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 28952b6ba629SIlya Dryomov close_ctree(tree_root); 28962b6ba629SIlya Dryomov return ret; 28972b6ba629SIlya Dryomov } 2898e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 289959641015SIlya Dryomov 29002b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 29012b6ba629SIlya Dryomov if (ret) { 29022b6ba629SIlya Dryomov printk(KERN_WARNING "btrfs: failed to resume balance\n"); 290366b4ffd1SJosef Bacik close_ctree(tree_root); 29042b6ba629SIlya Dryomov return ret; 2905e3acc2a6SJosef Bacik } 2906e3acc2a6SJosef Bacik 29078dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 29088dabb742SStefan Behrens if (ret) { 29098dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 29108dabb742SStefan Behrens close_ctree(tree_root); 29118dabb742SStefan Behrens return ret; 29128dabb742SStefan Behrens } 29138dabb742SStefan Behrens 2914b382a324SJan Schmidt btrfs_qgroup_rescan_resume(fs_info); 2915b382a324SJan Schmidt 2916f7a81ea4SStefan Behrens if (create_uuid_tree) { 2917f7a81ea4SStefan Behrens pr_info("btrfs: creating UUID tree\n"); 2918f7a81ea4SStefan Behrens ret = btrfs_create_uuid_tree(fs_info); 2919f7a81ea4SStefan Behrens if (ret) { 2920f7a81ea4SStefan Behrens pr_warn("btrfs: failed to create the UUID tree %d\n", 2921f7a81ea4SStefan Behrens ret); 2922f7a81ea4SStefan Behrens close_ctree(tree_root); 2923f7a81ea4SStefan Behrens return ret; 2924f7a81ea4SStefan Behrens } 2925f7a81ea4SStefan Behrens } 2926f7a81ea4SStefan Behrens 2927ad2b2c80SAl Viro return 0; 292839279cc3SChris Mason 2929bcef60f2SArne Jansen fail_qgroup: 2930bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 29317c2ca468SChris Mason fail_trans_kthread: 29327c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 293354067ae9SJosef Bacik btrfs_cleanup_transaction(fs_info->tree_root); 29347b5ff90eSJosef Bacik del_fs_roots(fs_info); 29353f157a2fSChris Mason fail_cleaner: 2936a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 29377c2ca468SChris Mason 29387c2ca468SChris Mason /* 29397c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 29407c2ca468SChris Mason * kthreads 29417c2ca468SChris Mason */ 29427c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 29437c2ca468SChris Mason 29441b1d1f66SJosef Bacik fail_block_groups: 294554067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 29461b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2947af31f5e5SChris Mason 2948af31f5e5SChris Mason fail_tree_roots: 2949af31f5e5SChris Mason free_root_pointers(fs_info, 1); 29502b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2951af31f5e5SChris Mason 295239279cc3SChris Mason fail_sb_buffer: 29537abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 295416cdcec7SMiao Xie fail_alloc: 29554543df7eSChris Mason fail_iput: 2956586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2957586e46e2SIlya Dryomov 29584543df7eSChris Mason iput(fs_info->btree_inode); 2959963d678bSMiao Xie fail_delalloc_bytes: 2960963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 2961e2d84521SMiao Xie fail_dirty_metadata_bytes: 2962e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 2963ad081f14SJens Axboe fail_bdi: 29647e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 296576dda93cSYan, Zheng fail_srcu: 296676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 29677e662854SQinghuang Feng fail: 296853b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 2969586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2970ad2b2c80SAl Viro return err; 2971af31f5e5SChris Mason 2972af31f5e5SChris Mason recovery_tree_root: 2973af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2974af31f5e5SChris Mason goto fail_tree_roots; 2975af31f5e5SChris Mason 2976af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2977af31f5e5SChris Mason 2978af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2979af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2980af31f5e5SChris Mason 2981af31f5e5SChris Mason /* we can't trust the free space cache either */ 2982af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2983af31f5e5SChris Mason 2984af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2985af31f5e5SChris Mason &num_backups_tried, &backup_index); 2986af31f5e5SChris Mason if (ret == -1) 2987af31f5e5SChris Mason goto fail_block_groups; 2988af31f5e5SChris Mason goto retry_root_backup; 2989eb60ceacSChris Mason } 2990eb60ceacSChris Mason 2991f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2992f2984462SChris Mason { 2993f2984462SChris Mason if (uptodate) { 2994f2984462SChris Mason set_buffer_uptodate(bh); 2995f2984462SChris Mason } else { 2996442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 2997442a4f63SStefan Behrens bh->b_private; 2998442a4f63SStefan Behrens 2999606686eeSJosef Bacik printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to " 3000606686eeSJosef Bacik "I/O error on %s\n", 3001606686eeSJosef Bacik rcu_str_deref(device->name)); 30021259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 30031259ab75SChris Mason * our own ways of dealing with the IO errors 30041259ab75SChris Mason */ 3005f2984462SChris Mason clear_buffer_uptodate(bh); 3006442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 3007f2984462SChris Mason } 3008f2984462SChris Mason unlock_buffer(bh); 3009f2984462SChris Mason put_bh(bh); 3010f2984462SChris Mason } 3011f2984462SChris Mason 3012a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3013a512bbf8SYan Zheng { 3014a512bbf8SYan Zheng struct buffer_head *bh; 3015a512bbf8SYan Zheng struct buffer_head *latest = NULL; 3016a512bbf8SYan Zheng struct btrfs_super_block *super; 3017a512bbf8SYan Zheng int i; 3018a512bbf8SYan Zheng u64 transid = 0; 3019a512bbf8SYan Zheng u64 bytenr; 3020a512bbf8SYan Zheng 3021a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 3022a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 3023a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 3024a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3025a512bbf8SYan Zheng */ 3026a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 3027a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 30288068a47eSAnand Jain if (bytenr + BTRFS_SUPER_INFO_SIZE >= 30298068a47eSAnand Jain i_size_read(bdev->bd_inode)) 3030a512bbf8SYan Zheng break; 30318068a47eSAnand Jain bh = __bread(bdev, bytenr / 4096, 30328068a47eSAnand Jain BTRFS_SUPER_INFO_SIZE); 3033a512bbf8SYan Zheng if (!bh) 3034a512bbf8SYan Zheng continue; 3035a512bbf8SYan Zheng 3036a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 3037a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 30383cae210fSQu Wenruo btrfs_super_magic(super) != BTRFS_MAGIC) { 3039a512bbf8SYan Zheng brelse(bh); 3040a512bbf8SYan Zheng continue; 3041a512bbf8SYan Zheng } 3042a512bbf8SYan Zheng 3043a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 3044a512bbf8SYan Zheng brelse(latest); 3045a512bbf8SYan Zheng latest = bh; 3046a512bbf8SYan Zheng transid = btrfs_super_generation(super); 3047a512bbf8SYan Zheng } else { 3048a512bbf8SYan Zheng brelse(bh); 3049a512bbf8SYan Zheng } 3050a512bbf8SYan Zheng } 3051a512bbf8SYan Zheng return latest; 3052a512bbf8SYan Zheng } 3053a512bbf8SYan Zheng 30544eedeb75SHisashi Hifumi /* 30554eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 30564eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 30574eedeb75SHisashi Hifumi * we write are pinned. 30584eedeb75SHisashi Hifumi * 30594eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 30604eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 30614eedeb75SHisashi Hifumi * many supers on this one device should be written. 30624eedeb75SHisashi Hifumi * 30634eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 30644eedeb75SHisashi Hifumi */ 3065a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 3066a512bbf8SYan Zheng struct btrfs_super_block *sb, 3067a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 3068a512bbf8SYan Zheng { 3069a512bbf8SYan Zheng struct buffer_head *bh; 3070a512bbf8SYan Zheng int i; 3071a512bbf8SYan Zheng int ret; 3072a512bbf8SYan Zheng int errors = 0; 3073a512bbf8SYan Zheng u32 crc; 3074a512bbf8SYan Zheng u64 bytenr; 3075a512bbf8SYan Zheng 3076a512bbf8SYan Zheng if (max_mirrors == 0) 3077a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3078a512bbf8SYan Zheng 3079a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3080a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3081a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 3082a512bbf8SYan Zheng break; 3083a512bbf8SYan Zheng 3084a512bbf8SYan Zheng if (wait) { 3085a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3086a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3087634554dcSJosef Bacik if (!bh) { 3088634554dcSJosef Bacik errors++; 3089634554dcSJosef Bacik continue; 3090634554dcSJosef Bacik } 3091a512bbf8SYan Zheng wait_on_buffer(bh); 30924eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 30934eedeb75SHisashi Hifumi errors++; 30944eedeb75SHisashi Hifumi 30954eedeb75SHisashi Hifumi /* drop our reference */ 30964eedeb75SHisashi Hifumi brelse(bh); 30974eedeb75SHisashi Hifumi 30984eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3099a512bbf8SYan Zheng brelse(bh); 3100a512bbf8SYan Zheng continue; 3101a512bbf8SYan Zheng } else { 3102a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3103a512bbf8SYan Zheng 3104a512bbf8SYan Zheng crc = ~(u32)0; 3105b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3106a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3107a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3108a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3109a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3110a512bbf8SYan Zheng 31114eedeb75SHisashi Hifumi /* 31124eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 31134eedeb75SHisashi Hifumi * caller 31144eedeb75SHisashi Hifumi */ 3115a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3116a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3117634554dcSJosef Bacik if (!bh) { 3118634554dcSJosef Bacik printk(KERN_ERR "btrfs: couldn't get super " 3119634554dcSJosef Bacik "buffer head for bytenr %Lu\n", bytenr); 3120634554dcSJosef Bacik errors++; 3121634554dcSJosef Bacik continue; 3122634554dcSJosef Bacik } 3123634554dcSJosef Bacik 3124a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3125a512bbf8SYan Zheng 31264eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3127a512bbf8SYan Zheng get_bh(bh); 31284eedeb75SHisashi Hifumi 31294eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3130a512bbf8SYan Zheng lock_buffer(bh); 3131a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3132442a4f63SStefan Behrens bh->b_private = device; 3133a512bbf8SYan Zheng } 3134a512bbf8SYan Zheng 3135387125fcSChris Mason /* 3136387125fcSChris Mason * we fua the first super. The others we allow 3137387125fcSChris Mason * to go down lazy. 3138387125fcSChris Mason */ 313921adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 31404eedeb75SHisashi Hifumi if (ret) 3141a512bbf8SYan Zheng errors++; 3142a512bbf8SYan Zheng } 3143a512bbf8SYan Zheng return errors < i ? 0 : -1; 3144a512bbf8SYan Zheng } 3145a512bbf8SYan Zheng 3146387125fcSChris Mason /* 3147387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3148387125fcSChris Mason * for the barrier when it is done 3149387125fcSChris Mason */ 3150387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 3151387125fcSChris Mason { 3152387125fcSChris Mason if (err) { 3153387125fcSChris Mason if (err == -EOPNOTSUPP) 3154387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 3155387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 3156387125fcSChris Mason } 3157387125fcSChris Mason if (bio->bi_private) 3158387125fcSChris Mason complete(bio->bi_private); 3159387125fcSChris Mason bio_put(bio); 3160387125fcSChris Mason } 3161387125fcSChris Mason 3162387125fcSChris Mason /* 3163387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3164387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3165387125fcSChris Mason * 3166387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3167387125fcSChris Mason * capable 3168387125fcSChris Mason */ 3169387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3170387125fcSChris Mason { 3171387125fcSChris Mason struct bio *bio; 3172387125fcSChris Mason int ret = 0; 3173387125fcSChris Mason 3174387125fcSChris Mason if (device->nobarriers) 3175387125fcSChris Mason return 0; 3176387125fcSChris Mason 3177387125fcSChris Mason if (wait) { 3178387125fcSChris Mason bio = device->flush_bio; 3179387125fcSChris Mason if (!bio) 3180387125fcSChris Mason return 0; 3181387125fcSChris Mason 3182387125fcSChris Mason wait_for_completion(&device->flush_wait); 3183387125fcSChris Mason 3184387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 3185606686eeSJosef Bacik printk_in_rcu("btrfs: disabling barriers on dev %s\n", 3186606686eeSJosef Bacik rcu_str_deref(device->name)); 3187387125fcSChris Mason device->nobarriers = 1; 31885af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 3189387125fcSChris Mason ret = -EIO; 3190442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3191442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3192387125fcSChris Mason } 3193387125fcSChris Mason 3194387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3195387125fcSChris Mason bio_put(bio); 3196387125fcSChris Mason device->flush_bio = NULL; 3197387125fcSChris Mason 3198387125fcSChris Mason return ret; 3199387125fcSChris Mason } 3200387125fcSChris Mason 3201387125fcSChris Mason /* 3202387125fcSChris Mason * one reference for us, and we leave it for the 3203387125fcSChris Mason * caller 3204387125fcSChris Mason */ 32059c017abcSJesper Juhl device->flush_bio = NULL; 32069be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3207387125fcSChris Mason if (!bio) 3208387125fcSChris Mason return -ENOMEM; 3209387125fcSChris Mason 3210387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3211387125fcSChris Mason bio->bi_bdev = device->bdev; 3212387125fcSChris Mason init_completion(&device->flush_wait); 3213387125fcSChris Mason bio->bi_private = &device->flush_wait; 3214387125fcSChris Mason device->flush_bio = bio; 3215387125fcSChris Mason 3216387125fcSChris Mason bio_get(bio); 321721adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 3218387125fcSChris Mason 3219387125fcSChris Mason return 0; 3220387125fcSChris Mason } 3221387125fcSChris Mason 3222387125fcSChris Mason /* 3223387125fcSChris Mason * send an empty flush down to each device in parallel, 3224387125fcSChris Mason * then wait for them 3225387125fcSChris Mason */ 3226387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3227387125fcSChris Mason { 3228387125fcSChris Mason struct list_head *head; 3229387125fcSChris Mason struct btrfs_device *dev; 32305af3e8ccSStefan Behrens int errors_send = 0; 32315af3e8ccSStefan Behrens int errors_wait = 0; 3232387125fcSChris Mason int ret; 3233387125fcSChris Mason 3234387125fcSChris Mason /* send down all the barriers */ 3235387125fcSChris Mason head = &info->fs_devices->devices; 3236387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3237387125fcSChris Mason if (!dev->bdev) { 32385af3e8ccSStefan Behrens errors_send++; 3239387125fcSChris Mason continue; 3240387125fcSChris Mason } 3241387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3242387125fcSChris Mason continue; 3243387125fcSChris Mason 3244387125fcSChris Mason ret = write_dev_flush(dev, 0); 3245387125fcSChris Mason if (ret) 32465af3e8ccSStefan Behrens errors_send++; 3247387125fcSChris Mason } 3248387125fcSChris Mason 3249387125fcSChris Mason /* wait for all the barriers */ 3250387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3251387125fcSChris Mason if (!dev->bdev) { 32525af3e8ccSStefan Behrens errors_wait++; 3253387125fcSChris Mason continue; 3254387125fcSChris Mason } 3255387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3256387125fcSChris Mason continue; 3257387125fcSChris Mason 3258387125fcSChris Mason ret = write_dev_flush(dev, 1); 3259387125fcSChris Mason if (ret) 32605af3e8ccSStefan Behrens errors_wait++; 3261387125fcSChris Mason } 32625af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 32635af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3264387125fcSChris Mason return -EIO; 3265387125fcSChris Mason return 0; 3266387125fcSChris Mason } 3267387125fcSChris Mason 32685af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 32695af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 32705af3e8ccSStefan Behrens { 32715af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 32725af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 32735af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 32745af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 32755af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 32765af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 32775af3e8ccSStefan Behrens int num_types = 4; 32785af3e8ccSStefan Behrens int i; 32795af3e8ccSStefan Behrens int c; 32805af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 32815af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 32825af3e8ccSStefan Behrens 32835af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 32845af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 32855af3e8ccSStefan Behrens 32865af3e8ccSStefan Behrens sinfo = NULL; 32875af3e8ccSStefan Behrens rcu_read_lock(); 32885af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 32895af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 32905af3e8ccSStefan Behrens sinfo = tmp; 32915af3e8ccSStefan Behrens break; 32925af3e8ccSStefan Behrens } 32935af3e8ccSStefan Behrens } 32945af3e8ccSStefan Behrens rcu_read_unlock(); 32955af3e8ccSStefan Behrens 32965af3e8ccSStefan Behrens if (!sinfo) 32975af3e8ccSStefan Behrens continue; 32985af3e8ccSStefan Behrens 32995af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 33005af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 33015af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 33025af3e8ccSStefan Behrens u64 flags; 33035af3e8ccSStefan Behrens 33045af3e8ccSStefan Behrens btrfs_get_block_group_info( 33055af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 33065af3e8ccSStefan Behrens if (space.total_bytes == 0 || 33075af3e8ccSStefan Behrens space.used_bytes == 0) 33085af3e8ccSStefan Behrens continue; 33095af3e8ccSStefan Behrens flags = space.flags; 33105af3e8ccSStefan Behrens /* 33115af3e8ccSStefan Behrens * return 33125af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 33135af3e8ccSStefan Behrens * any of metadata, system or data, else 33145af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 33155af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 33165af3e8ccSStefan Behrens * are used, else 33175af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 33185af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 33195af3e8ccSStefan Behrens * configured and more than 33205af3e8ccSStefan Behrens * 2 mirrors are used. 33215af3e8ccSStefan Behrens */ 33225af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 33235af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 33245af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 33255af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 33265af3e8ccSStefan Behrens == 0))) 33275af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 332853b381b3SDavid Woodhouse else if (num_tolerated_disk_barrier_failures > 1) { 332953b381b3SDavid Woodhouse if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 333053b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5 | 333153b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID10)) { 33325af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 333353b381b3SDavid Woodhouse } else if (flags & 333415b0a89dSHenrik Nordvik BTRFS_BLOCK_GROUP_RAID6) { 333553b381b3SDavid Woodhouse num_tolerated_disk_barrier_failures = 2; 333653b381b3SDavid Woodhouse } 333753b381b3SDavid Woodhouse } 33385af3e8ccSStefan Behrens } 33395af3e8ccSStefan Behrens } 33405af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 33415af3e8ccSStefan Behrens } 33425af3e8ccSStefan Behrens 33435af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 33445af3e8ccSStefan Behrens } 33455af3e8ccSStefan Behrens 334648a3b636SEric Sandeen static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3347f2984462SChris Mason { 3348e5e9a520SChris Mason struct list_head *head; 3349f2984462SChris Mason struct btrfs_device *dev; 3350a061fc8dSChris Mason struct btrfs_super_block *sb; 3351f2984462SChris Mason struct btrfs_dev_item *dev_item; 3352f2984462SChris Mason int ret; 3353f2984462SChris Mason int do_barriers; 3354a236aed1SChris Mason int max_errors; 3355a236aed1SChris Mason int total_errors = 0; 3356a061fc8dSChris Mason u64 flags; 3357f2984462SChris Mason 33586c41761fSDavid Sterba max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3359f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3360af31f5e5SChris Mason backup_super_roots(root->fs_info); 3361f2984462SChris Mason 33626c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3363a061fc8dSChris Mason dev_item = &sb->dev_item; 3364e5e9a520SChris Mason 3365174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3366e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3367387125fcSChris Mason 33685af3e8ccSStefan Behrens if (do_barriers) { 33695af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 33705af3e8ccSStefan Behrens if (ret) { 33715af3e8ccSStefan Behrens mutex_unlock( 33725af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 33735af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 33745af3e8ccSStefan Behrens "errors while submitting device barriers."); 33755af3e8ccSStefan Behrens return ret; 33765af3e8ccSStefan Behrens } 33775af3e8ccSStefan Behrens } 3378387125fcSChris Mason 33791f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3380dfe25020SChris Mason if (!dev->bdev) { 3381dfe25020SChris Mason total_errors++; 3382dfe25020SChris Mason continue; 3383dfe25020SChris Mason } 33842b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3385dfe25020SChris Mason continue; 3386dfe25020SChris Mason 33872b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3388a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3389a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 3390a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 3391a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 3392a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3393a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3394a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3395a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 33962b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3397a512bbf8SYan Zheng 3398a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3399a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3400f2984462SChris Mason 3401a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3402a236aed1SChris Mason if (ret) 3403a236aed1SChris Mason total_errors++; 3404f2984462SChris Mason } 3405a236aed1SChris Mason if (total_errors > max_errors) { 3406d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 3407d397712bSChris Mason total_errors); 340879787eaaSJeff Mahoney 340979787eaaSJeff Mahoney /* This shouldn't happen. FUA is masked off if unsupported */ 3410a236aed1SChris Mason BUG(); 3411a236aed1SChris Mason } 3412f2984462SChris Mason 3413a512bbf8SYan Zheng total_errors = 0; 34141f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3415dfe25020SChris Mason if (!dev->bdev) 3416dfe25020SChris Mason continue; 34172b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3418dfe25020SChris Mason continue; 3419dfe25020SChris Mason 3420a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3421a512bbf8SYan Zheng if (ret) 34221259ab75SChris Mason total_errors++; 3423f2984462SChris Mason } 3424174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3425a236aed1SChris Mason if (total_errors > max_errors) { 342679787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 342779787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 342879787eaaSJeff Mahoney return -EIO; 3429a236aed1SChris Mason } 3430f2984462SChris Mason return 0; 3431f2984462SChris Mason } 3432f2984462SChris Mason 3433a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3434a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3435cfaa7295SChris Mason { 3436e66f709bSChris Mason int ret; 34372c90e5d6SChris Mason 3438a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 34395f39d397SChris Mason return ret; 3440cfaa7295SChris Mason } 3441cfaa7295SChris Mason 3442cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */ 3443cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 3444cb517eabSMiao Xie struct btrfs_root *root) 34452619ba1fSChris Mason { 34464df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 34472619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 34482619ba1fSChris Mason (unsigned long)root->root_key.objectid); 34494df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 345076dda93cSYan, Zheng 345176dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 345276dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 345376dda93cSYan, Zheng 3454d7634482SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 34553321719eSLiu Bo btrfs_free_log(NULL, root); 34563321719eSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 34573321719eSLiu Bo } 34583321719eSLiu Bo 3459581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3460581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 34614df27c4dSYan, Zheng free_fs_root(root); 34624df27c4dSYan, Zheng } 34634df27c4dSYan, Zheng 34644df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 34654df27c4dSYan, Zheng { 346682d5902dSLi Zefan iput(root->cache_inode); 34674df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 34680ee5dc67SAl Viro if (root->anon_dev) 34690ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 34705f39d397SChris Mason free_extent_buffer(root->node); 34715f39d397SChris Mason free_extent_buffer(root->commit_root); 3472581bb050SLi Zefan kfree(root->free_ino_ctl); 3473581bb050SLi Zefan kfree(root->free_ino_pinned); 347458176a96SJosef Bacik kfree(root->name); 3475b0feb9d9SMiao Xie btrfs_put_fs_root(root); 34762619ba1fSChris Mason } 34772619ba1fSChris Mason 3478cb517eabSMiao Xie void btrfs_free_fs_root(struct btrfs_root *root) 3479cb517eabSMiao Xie { 3480cb517eabSMiao Xie free_fs_root(root); 3481eb60ceacSChris Mason } 3482eb60ceacSChris Mason 3483c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3484c146afadSYan Zheng { 3485c146afadSYan Zheng u64 root_objectid = 0; 3486c146afadSYan Zheng struct btrfs_root *gang[8]; 3487c146afadSYan Zheng int i; 3488c146afadSYan Zheng int ret; 3489c146afadSYan Zheng 3490c146afadSYan Zheng while (1) { 3491c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3492c146afadSYan Zheng (void **)gang, root_objectid, 3493c146afadSYan Zheng ARRAY_SIZE(gang)); 3494c146afadSYan Zheng if (!ret) 3495c146afadSYan Zheng break; 34965d4f98a2SYan Zheng 34975d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3498c146afadSYan Zheng for (i = 0; i < ret; i++) { 349966b4ffd1SJosef Bacik int err; 350066b4ffd1SJosef Bacik 3501c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 350266b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 350366b4ffd1SJosef Bacik if (err) 350466b4ffd1SJosef Bacik return err; 3505c146afadSYan Zheng } 3506c146afadSYan Zheng root_objectid++; 3507c146afadSYan Zheng } 3508c146afadSYan Zheng return 0; 3509c146afadSYan Zheng } 3510c146afadSYan Zheng 3511c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3512c146afadSYan Zheng { 3513c146afadSYan Zheng struct btrfs_trans_handle *trans; 3514c146afadSYan Zheng int ret; 3515c146afadSYan Zheng 3516c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 351724bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3518c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 35199d1a2a3aSDavid Sterba wake_up_process(root->fs_info->cleaner_kthread); 3520c71bf099SYan, Zheng 3521c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3522c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3523c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3524c71bf099SYan, Zheng 35257a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 35263612b495STsutomu Itoh if (IS_ERR(trans)) 35273612b495STsutomu Itoh return PTR_ERR(trans); 3528c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 352979787eaaSJeff Mahoney if (ret) 353079787eaaSJeff Mahoney return ret; 3531c146afadSYan Zheng /* run commit again to drop the original snapshot */ 35327a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 35333612b495STsutomu Itoh if (IS_ERR(trans)) 35343612b495STsutomu Itoh return PTR_ERR(trans); 353579787eaaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 353679787eaaSJeff Mahoney if (ret) 353779787eaaSJeff Mahoney return ret; 3538c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 353979787eaaSJeff Mahoney if (ret) { 354079787eaaSJeff Mahoney btrfs_error(root->fs_info, ret, 354179787eaaSJeff Mahoney "Failed to sync btree inode to disk."); 354279787eaaSJeff Mahoney return ret; 354379787eaaSJeff Mahoney } 3544c146afadSYan Zheng 3545a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3546c146afadSYan Zheng return ret; 3547c146afadSYan Zheng } 3548c146afadSYan Zheng 3549e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3550eb60ceacSChris Mason { 35510f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3552c146afadSYan Zheng int ret; 3553e089f05cSChris Mason 3554facda1e7SChris Mason fs_info->closing = 1; 3555a2135011SChris Mason smp_mb(); 3556a2135011SChris Mason 3557837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3558aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3559837d5b6eSIlya Dryomov 35608dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 35618dabb742SStefan Behrens 3562aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 35634cb5300bSChris Mason 35644cb5300bSChris Mason /* wait for any defraggers to finish */ 35654cb5300bSChris Mason wait_event(fs_info->transaction_wait, 35664cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 35674cb5300bSChris Mason 35684cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 356926176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 35704cb5300bSChris Mason 3571c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3572c146afadSYan Zheng ret = btrfs_commit_super(root); 3573d397712bSChris Mason if (ret) 3574d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3575c146afadSYan Zheng } 3576ed2ff2cbSChris Mason 357787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 357868ce9682SStefan Behrens btrfs_error_commit_super(root); 3579acce952bSliubo 3580300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3581300e4f8aSJosef Bacik 3582e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3583e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 35848929ecfaSYan, Zheng 3585f25784b3SYan Zheng fs_info->closing = 2; 3586f25784b3SYan Zheng smp_mb(); 3587f25784b3SYan Zheng 3588bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3589bcef60f2SArne Jansen 3590963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3591963d678bSMiao Xie printk(KERN_INFO "btrfs: at unmount delalloc count %lld\n", 3592963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3593b0c68f8bSChris Mason } 359431153d81SYan Zheng 3595e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3596c146afadSYan Zheng 359713e6c37bSJosef Bacik btrfs_stop_all_workers(fs_info); 35982932505aSLiu Bo 35990f7d52f4SChris Mason del_fs_roots(fs_info); 3600d10c5f31SChris Mason 360113e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 36029ad6b7bcSChris Mason 360313e6c37bSJosef Bacik iput(fs_info->btree_inode); 3604d6bfde87SChris Mason 360521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 360621adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 360721adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 360821adbd5cSStefan Behrens #endif 360921adbd5cSStefan Behrens 3610dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 36110b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3612b248a415SChris Mason 3613e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3614963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 361504160088SChris Mason bdi_destroy(&fs_info->bdi); 361676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 36170b86a832SChris Mason 361853b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 361953b381b3SDavid Woodhouse 3620eb60ceacSChris Mason return 0; 3621eb60ceacSChris Mason } 3622eb60ceacSChris Mason 3623b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3624b9fab919SChris Mason int atomic) 3625ccd467d6SChris Mason { 36261259ab75SChris Mason int ret; 3627727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 36281259ab75SChris Mason 36290b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 36301259ab75SChris Mason if (!ret) 36311259ab75SChris Mason return ret; 36321259ab75SChris Mason 36331259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3634b9fab919SChris Mason parent_transid, atomic); 3635b9fab919SChris Mason if (ret == -EAGAIN) 3636b9fab919SChris Mason return ret; 36371259ab75SChris Mason return !ret; 36385f39d397SChris Mason } 36396702ed49SChris Mason 36405f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 36415f39d397SChris Mason { 36420b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 36435f39d397SChris Mason } 36445f39d397SChris Mason 36455f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 36465f39d397SChris Mason { 3647727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 36485f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3649b9473439SChris Mason int was_dirty; 3650b4ce94deSChris Mason 3651b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 365231b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 365331b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3654d397712bSChris Mason "found %llu running %llu\n", 3655db94535dSChris Mason (unsigned long long)buf->start, 3656d397712bSChris Mason (unsigned long long)transid, 3657d397712bSChris Mason (unsigned long long)root->fs_info->generation); 36580b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3659e2d84521SMiao Xie if (!was_dirty) 3660e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3661e2d84521SMiao Xie buf->len, 3662e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 3663eb60ceacSChris Mason } 3664eb60ceacSChris Mason 3665b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3666b53d3f5dSLiu Bo int flush_delayed) 366735b7e476SChris Mason { 3668188de649SChris Mason /* 3669188de649SChris Mason * looks as though older kernels can get into trouble with 3670188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3671188de649SChris Mason */ 3672e2d84521SMiao Xie int ret; 3673d6bfde87SChris Mason 36746933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3675d6bfde87SChris Mason return; 3676d6bfde87SChris Mason 3677b53d3f5dSLiu Bo if (flush_delayed) 367816cdcec7SMiao Xie btrfs_balance_delayed_items(root); 367916cdcec7SMiao Xie 3680e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3681e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3682e2d84521SMiao Xie if (ret > 0) { 3683d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited( 3684d0e1d66bSNamjae Jeon root->fs_info->btree_inode->i_mapping); 368516cdcec7SMiao Xie } 368616cdcec7SMiao Xie return; 368716cdcec7SMiao Xie } 368816cdcec7SMiao Xie 3689b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 369016cdcec7SMiao Xie { 3691b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 369235b7e476SChris Mason } 3693b53d3f5dSLiu Bo 3694b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3695b53d3f5dSLiu Bo { 3696b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3697d6bfde87SChris Mason } 36986b80053dSChris Mason 3699ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 37006b80053dSChris Mason { 3701727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 37020b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 37036b80053dSChris Mason } 37040da5468fSChris Mason 3705fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3706acce952bSliubo int read_only) 3707acce952bSliubo { 37081104a885SDavid Sterba /* 37091104a885SDavid Sterba * Placeholder for checks 37101104a885SDavid Sterba */ 3711fcd1f065SDavid Sterba return 0; 3712fcd1f065SDavid Sterba } 3713fcd1f065SDavid Sterba 371448a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root) 3715acce952bSliubo { 3716acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3717acce952bSliubo btrfs_run_delayed_iputs(root); 3718acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3719acce952bSliubo 3720acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3721acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3722acce952bSliubo 3723acce952bSliubo /* cleanup FS via transaction */ 3724acce952bSliubo btrfs_cleanup_transaction(root); 3725acce952bSliubo } 3726acce952bSliubo 3727569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 3728569e0f35SJosef Bacik struct btrfs_root *root) 3729acce952bSliubo { 3730acce952bSliubo struct btrfs_inode *btrfs_inode; 3731acce952bSliubo struct list_head splice; 3732acce952bSliubo 3733acce952bSliubo INIT_LIST_HEAD(&splice); 3734acce952bSliubo 3735acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3736199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3737acce952bSliubo 3738569e0f35SJosef Bacik list_splice_init(&t->ordered_operations, &splice); 3739acce952bSliubo while (!list_empty(&splice)) { 3740acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3741acce952bSliubo ordered_operations); 3742acce952bSliubo 3743acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3744199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3745acce952bSliubo 3746acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3747b216cbfbSMiao Xie 3748199c2a9cSMiao Xie spin_lock(&root->fs_info->ordered_root_lock); 3749acce952bSliubo } 3750acce952bSliubo 3751199c2a9cSMiao Xie spin_unlock(&root->fs_info->ordered_root_lock); 3752acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3753acce952bSliubo } 3754acce952bSliubo 3755143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3756acce952bSliubo { 3757acce952bSliubo struct btrfs_ordered_extent *ordered; 3758acce952bSliubo 3759199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock); 3760779880efSJosef Bacik /* 3761779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3762779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3763779880efSJosef Bacik */ 3764199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents, 3765779880efSJosef Bacik root_extent_list) 3766779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3767199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock); 3768199c2a9cSMiao Xie } 3769199c2a9cSMiao Xie 3770199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) 3771199c2a9cSMiao Xie { 3772199c2a9cSMiao Xie struct btrfs_root *root; 3773199c2a9cSMiao Xie struct list_head splice; 3774199c2a9cSMiao Xie 3775199c2a9cSMiao Xie INIT_LIST_HEAD(&splice); 3776199c2a9cSMiao Xie 3777199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock); 3778199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice); 3779199c2a9cSMiao Xie while (!list_empty(&splice)) { 3780199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3781199c2a9cSMiao Xie ordered_root); 3782199c2a9cSMiao Xie list_del_init(&root->ordered_root); 3783199c2a9cSMiao Xie 3784199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root); 3785199c2a9cSMiao Xie 3786199c2a9cSMiao Xie cond_resched_lock(&fs_info->ordered_root_lock); 3787199c2a9cSMiao Xie } 3788199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock); 3789acce952bSliubo } 3790acce952bSliubo 379135a3621bSStefan Behrens static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3792acce952bSliubo struct btrfs_root *root) 3793acce952bSliubo { 3794acce952bSliubo struct rb_node *node; 3795acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3796acce952bSliubo struct btrfs_delayed_ref_node *ref; 3797acce952bSliubo int ret = 0; 3798acce952bSliubo 3799acce952bSliubo delayed_refs = &trans->delayed_refs; 3800acce952bSliubo 3801acce952bSliubo spin_lock(&delayed_refs->lock); 3802acce952bSliubo if (delayed_refs->num_entries == 0) { 3803cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3804acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3805acce952bSliubo return ret; 3806acce952bSliubo } 3807acce952bSliubo 3808b939d1abSJosef Bacik while ((node = rb_first(&delayed_refs->root)) != NULL) { 3809eb12db69SJosef Bacik struct btrfs_delayed_ref_head *head = NULL; 3810e78417d1SJosef Bacik bool pin_bytes = false; 3811acce952bSliubo 3812eb12db69SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3813acce952bSliubo atomic_set(&ref->refs, 1); 3814acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3815acce952bSliubo 3816acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3817b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3818b939d1abSJosef Bacik atomic_inc(&ref->refs); 381979787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3820b939d1abSJosef Bacik 3821b939d1abSJosef Bacik /* Need to wait for the delayed ref to run */ 3822acce952bSliubo mutex_lock(&head->mutex); 3823b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3824b939d1abSJosef Bacik btrfs_put_delayed_ref(ref); 3825b939d1abSJosef Bacik 3826e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3827b939d1abSJosef Bacik continue; 3828b939d1abSJosef Bacik } 3829b939d1abSJosef Bacik 383054067ae9SJosef Bacik if (head->must_insert_reserved) 3831e78417d1SJosef Bacik pin_bytes = true; 383278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 3833acce952bSliubo delayed_refs->num_heads--; 3834acce952bSliubo if (list_empty(&head->cluster)) 3835acce952bSliubo delayed_refs->num_heads_ready--; 3836acce952bSliubo list_del_init(&head->cluster); 3837acce952bSliubo } 3838eb12db69SJosef Bacik 3839b939d1abSJosef Bacik ref->in_tree = 0; 3840b939d1abSJosef Bacik rb_erase(&ref->rb_node, &delayed_refs->root); 3841b939d1abSJosef Bacik delayed_refs->num_entries--; 3842acce952bSliubo spin_unlock(&delayed_refs->lock); 3843e78417d1SJosef Bacik if (head) { 3844e78417d1SJosef Bacik if (pin_bytes) 3845e78417d1SJosef Bacik btrfs_pin_extent(root, ref->bytenr, 3846e78417d1SJosef Bacik ref->num_bytes, 1); 3847e78417d1SJosef Bacik mutex_unlock(&head->mutex); 3848e78417d1SJosef Bacik } 3849acce952bSliubo btrfs_put_delayed_ref(ref); 3850acce952bSliubo 3851acce952bSliubo cond_resched(); 3852acce952bSliubo spin_lock(&delayed_refs->lock); 3853acce952bSliubo } 3854acce952bSliubo 3855acce952bSliubo spin_unlock(&delayed_refs->lock); 3856acce952bSliubo 3857acce952bSliubo return ret; 3858acce952bSliubo } 3859acce952bSliubo 3860aec8030aSMiao Xie static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t) 3861acce952bSliubo { 3862acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3863acce952bSliubo struct list_head splice; 3864acce952bSliubo 3865acce952bSliubo INIT_LIST_HEAD(&splice); 3866acce952bSliubo 3867acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3868acce952bSliubo 3869acce952bSliubo while (!list_empty(&splice)) { 3870acce952bSliubo snapshot = list_entry(splice.next, 3871acce952bSliubo struct btrfs_pending_snapshot, 3872acce952bSliubo list); 3873aec8030aSMiao Xie snapshot->error = -ECANCELED; 3874acce952bSliubo list_del_init(&snapshot->list); 3875acce952bSliubo } 3876acce952bSliubo } 3877acce952bSliubo 3878143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3879acce952bSliubo { 3880acce952bSliubo struct btrfs_inode *btrfs_inode; 3881acce952bSliubo struct list_head splice; 3882acce952bSliubo 3883acce952bSliubo INIT_LIST_HEAD(&splice); 3884acce952bSliubo 3885eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3886eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice); 3887acce952bSliubo 3888acce952bSliubo while (!list_empty(&splice)) { 3889eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode, 3890acce952bSliubo delalloc_inodes); 3891acce952bSliubo 3892acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3893df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 3894df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 3895eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3896acce952bSliubo 3897acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3898b216cbfbSMiao Xie 3899eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock); 3900acce952bSliubo } 3901acce952bSliubo 3902eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock); 3903eb73c1b7SMiao Xie } 3904eb73c1b7SMiao Xie 3905eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) 3906eb73c1b7SMiao Xie { 3907eb73c1b7SMiao Xie struct btrfs_root *root; 3908eb73c1b7SMiao Xie struct list_head splice; 3909eb73c1b7SMiao Xie 3910eb73c1b7SMiao Xie INIT_LIST_HEAD(&splice); 3911eb73c1b7SMiao Xie 3912eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3913eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice); 3914eb73c1b7SMiao Xie while (!list_empty(&splice)) { 3915eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root, 3916eb73c1b7SMiao Xie delalloc_root); 3917eb73c1b7SMiao Xie list_del_init(&root->delalloc_root); 3918eb73c1b7SMiao Xie root = btrfs_grab_fs_root(root); 3919eb73c1b7SMiao Xie BUG_ON(!root); 3920eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3921eb73c1b7SMiao Xie 3922eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root); 3923eb73c1b7SMiao Xie btrfs_put_fs_root(root); 3924eb73c1b7SMiao Xie 3925eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock); 3926eb73c1b7SMiao Xie } 3927eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock); 3928acce952bSliubo } 3929acce952bSliubo 3930acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3931acce952bSliubo struct extent_io_tree *dirty_pages, 3932acce952bSliubo int mark) 3933acce952bSliubo { 3934acce952bSliubo int ret; 3935acce952bSliubo struct extent_buffer *eb; 3936acce952bSliubo u64 start = 0; 3937acce952bSliubo u64 end; 3938acce952bSliubo 3939acce952bSliubo while (1) { 3940acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3941e6138876SJosef Bacik mark, NULL); 3942acce952bSliubo if (ret) 3943acce952bSliubo break; 3944acce952bSliubo 3945acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3946acce952bSliubo while (start <= end) { 3947fd8b2b61SJosef Bacik eb = btrfs_find_tree_block(root, start, 3948fd8b2b61SJosef Bacik root->leafsize); 394969a85bd8SJosef Bacik start += root->leafsize; 3950fd8b2b61SJosef Bacik if (!eb) 3951acce952bSliubo continue; 3952fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 3953acce952bSliubo 3954fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3955fd8b2b61SJosef Bacik &eb->bflags)) 3956fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 3957fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 3958acce952bSliubo } 3959acce952bSliubo } 3960acce952bSliubo 3961acce952bSliubo return ret; 3962acce952bSliubo } 3963acce952bSliubo 3964acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3965acce952bSliubo struct extent_io_tree *pinned_extents) 3966acce952bSliubo { 3967acce952bSliubo struct extent_io_tree *unpin; 3968acce952bSliubo u64 start; 3969acce952bSliubo u64 end; 3970acce952bSliubo int ret; 3971ed0eaa14SLiu Bo bool loop = true; 3972acce952bSliubo 3973acce952bSliubo unpin = pinned_extents; 3974ed0eaa14SLiu Bo again: 3975acce952bSliubo while (1) { 3976acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3977e6138876SJosef Bacik EXTENT_DIRTY, NULL); 3978acce952bSliubo if (ret) 3979acce952bSliubo break; 3980acce952bSliubo 3981acce952bSliubo /* opt_discard */ 39825378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 39835378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 39845378e607SLi Dongyang end + 1 - start, 39855378e607SLi Dongyang NULL); 3986acce952bSliubo 3987acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3988acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3989acce952bSliubo cond_resched(); 3990acce952bSliubo } 3991acce952bSliubo 3992ed0eaa14SLiu Bo if (loop) { 3993ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 3994ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 3995ed0eaa14SLiu Bo else 3996ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 3997ed0eaa14SLiu Bo loop = false; 3998ed0eaa14SLiu Bo goto again; 3999ed0eaa14SLiu Bo } 4000ed0eaa14SLiu Bo 4001acce952bSliubo return 0; 4002acce952bSliubo } 4003acce952bSliubo 400449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 400549b25e05SJeff Mahoney struct btrfs_root *root) 400649b25e05SJeff Mahoney { 400749b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 400849b25e05SJeff Mahoney btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 400949b25e05SJeff Mahoney cur_trans->dirty_pages.dirty_bytes); 401049b25e05SJeff Mahoney 40114a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 401249b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 401349b25e05SJeff Mahoney 4014aec8030aSMiao Xie btrfs_evict_pending_snapshots(cur_trans); 4015aec8030aSMiao Xie 40164a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 401749b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 401849b25e05SJeff Mahoney 401967cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 402067cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 402167cde344SMiao Xie 402249b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 402349b25e05SJeff Mahoney EXTENT_DIRTY); 40246e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 40256e841e32SLiu Bo root->fs_info->pinned_extents); 402649b25e05SJeff Mahoney 40274a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED; 40284a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait); 40294a9d8bdeSMiao Xie 403049b25e05SJeff Mahoney /* 403149b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 403249b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 403349b25e05SJeff Mahoney */ 403449b25e05SJeff Mahoney } 403549b25e05SJeff Mahoney 403648a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root) 4037acce952bSliubo { 4038acce952bSliubo struct btrfs_transaction *t; 4039acce952bSliubo LIST_HEAD(list); 4040acce952bSliubo 4041acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 4042acce952bSliubo 4043a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 4044acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 4045ac673879SMiao Xie root->fs_info->running_transaction = NULL; 4046a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 4047a4abeea4SJosef Bacik 4048acce952bSliubo while (!list_empty(&list)) { 4049acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 4050acce952bSliubo 4051569e0f35SJosef Bacik btrfs_destroy_ordered_operations(t, root); 4052acce952bSliubo 4053199c2a9cSMiao Xie btrfs_destroy_all_ordered_extents(root->fs_info); 4054acce952bSliubo 4055acce952bSliubo btrfs_destroy_delayed_refs(t, root); 4056acce952bSliubo 40574a9d8bdeSMiao Xie /* 40584a9d8bdeSMiao Xie * FIXME: cleanup wait for commit 40594a9d8bdeSMiao Xie * We needn't acquire the lock here, because we are during 40604a9d8bdeSMiao Xie * the umount, there is no other task which will change it. 40614a9d8bdeSMiao Xie */ 40624a9d8bdeSMiao Xie t->state = TRANS_STATE_COMMIT_START; 406366657b31SJosef Bacik smp_mb(); 4064acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 4065acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 4066acce952bSliubo 4067aec8030aSMiao Xie btrfs_evict_pending_snapshots(t); 4068aec8030aSMiao Xie 40694a9d8bdeSMiao Xie t->state = TRANS_STATE_UNBLOCKED; 407066657b31SJosef Bacik smp_mb(); 4071acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 4072acce952bSliubo wake_up(&root->fs_info->transaction_wait); 4073acce952bSliubo 407467cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 407567cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 407667cde344SMiao Xie 4077eb73c1b7SMiao Xie btrfs_destroy_all_delalloc_inodes(root->fs_info); 4078acce952bSliubo 4079acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 4080acce952bSliubo EXTENT_DIRTY); 4081acce952bSliubo 4082acce952bSliubo btrfs_destroy_pinned_extent(root, 4083acce952bSliubo root->fs_info->pinned_extents); 4084acce952bSliubo 40854a9d8bdeSMiao Xie t->state = TRANS_STATE_COMPLETED; 40864a9d8bdeSMiao Xie smp_mb(); 40874a9d8bdeSMiao Xie if (waitqueue_active(&t->commit_wait)) 40884a9d8bdeSMiao Xie wake_up(&t->commit_wait); 40894a9d8bdeSMiao Xie 409013c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 4091acce952bSliubo list_del_init(&t->list); 4092acce952bSliubo memset(t, 0, sizeof(*t)); 4093acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 4094acce952bSliubo } 4095acce952bSliubo 4096acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 4097acce952bSliubo 4098acce952bSliubo return 0; 4099acce952bSliubo } 4100acce952bSliubo 4101d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 4102ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 41034bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 41040b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 4105239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 4106239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 41070da5468fSChris Mason }; 4108