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 579b2950863SChristoph Hellwig static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 5805cf1ab56SJosef Bacik struct extent_state *state, int mirror) 581ce9adaa5SChris Mason { 582ce9adaa5SChris Mason struct extent_io_tree *tree; 583ce9adaa5SChris Mason u64 found_start; 584ce9adaa5SChris Mason int found_level; 585ce9adaa5SChris Mason struct extent_buffer *eb; 586ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 587f188591eSChris Mason int ret = 0; 588727011e0SChris Mason int reads_done; 589ce9adaa5SChris Mason 590ce9adaa5SChris Mason if (!page->private) 591ce9adaa5SChris Mason goto out; 592d397712bSChris Mason 593727011e0SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 5944f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 595d397712bSChris Mason 5960b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5970b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5980b32f4bbSJosef Bacik */ 5990b32f4bbSJosef Bacik extent_buffer_get(eb); 6000b32f4bbSJosef Bacik 6010b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 602727011e0SChris Mason if (!reads_done) 603727011e0SChris Mason goto err; 604f188591eSChris Mason 6055cf1ab56SJosef Bacik eb->read_mirror = mirror; 606ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 607ea466794SJosef Bacik ret = -EIO; 608ea466794SJosef Bacik goto err; 609ea466794SJosef Bacik } 610ea466794SJosef Bacik 611ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 612727011e0SChris Mason if (found_start != eb->start) { 6137a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 614193f284dSChris Mason "%llu %llu\n", 615a1b32a59SChris Mason (unsigned long long)found_start, 616a1b32a59SChris Mason (unsigned long long)eb->start); 617f188591eSChris Mason ret = -EIO; 618ce9adaa5SChris Mason goto err; 619ce9adaa5SChris Mason } 6202b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 6217a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 622d397712bSChris Mason (unsigned long long)eb->start); 6231259ab75SChris Mason ret = -EIO; 6241259ab75SChris Mason goto err; 6251259ab75SChris Mason } 626ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 6271c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) { 6281c24c3ceSJosef Bacik btrfs_info(root->fs_info, "bad tree block level %d\n", 6291c24c3ceSJosef Bacik (int)btrfs_header_level(eb)); 6301c24c3ceSJosef Bacik ret = -EIO; 6311c24c3ceSJosef Bacik goto err; 6321c24c3ceSJosef Bacik } 633ce9adaa5SChris Mason 63485d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 63585d4e461SChris Mason eb, found_level); 6364008c04aSChris Mason 637ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 638a826d6dcSJosef Bacik if (ret) { 639f188591eSChris Mason ret = -EIO; 640a826d6dcSJosef Bacik goto err; 641a826d6dcSJosef Bacik } 642a826d6dcSJosef Bacik 643a826d6dcSJosef Bacik /* 644a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 645a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 646a826d6dcSJosef Bacik * return -EIO. 647a826d6dcSJosef Bacik */ 648a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 649a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 650a826d6dcSJosef Bacik ret = -EIO; 651a826d6dcSJosef Bacik } 652ce9adaa5SChris Mason 6530b32f4bbSJosef Bacik if (!ret) 6540b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 655ce9adaa5SChris Mason err: 65679fb65a1SJosef Bacik if (reads_done && 65779fb65a1SJosef Bacik test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6584bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6594bb31e92SArne Jansen 66053b381b3SDavid Woodhouse if (ret) { 66153b381b3SDavid Woodhouse /* 66253b381b3SDavid Woodhouse * our io error hook is going to dec the io pages 66353b381b3SDavid Woodhouse * again, we have to make sure it has something 66453b381b3SDavid Woodhouse * to decrement 66553b381b3SDavid Woodhouse */ 66653b381b3SDavid Woodhouse atomic_inc(&eb->io_pages); 6670b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 66853b381b3SDavid Woodhouse } 6690b32f4bbSJosef Bacik free_extent_buffer(eb); 670ce9adaa5SChris Mason out: 671f188591eSChris Mason return ret; 672ce9adaa5SChris Mason } 673ce9adaa5SChris Mason 674ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6754bb31e92SArne Jansen { 6764bb31e92SArne Jansen struct extent_buffer *eb; 6774bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6784bb31e92SArne Jansen 6794f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 680ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6815cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 68253b381b3SDavid Woodhouse atomic_dec(&eb->io_pages); 683ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6844bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6854bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6864bb31e92SArne Jansen } 6874bb31e92SArne Jansen 688ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 689ce9adaa5SChris Mason { 690ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 691ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 692ce9adaa5SChris Mason 693ce9adaa5SChris Mason fs_info = end_io_wq->info; 694ce9adaa5SChris Mason end_io_wq->error = err; 6958b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6968b712842SChris Mason end_io_wq->work.flags = 0; 697d20f7043SChris Mason 6987b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 69953b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) 700cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 701cad321adSChris Mason &end_io_wq->work); 70253b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) 7030cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 7040cb59c99SJosef Bacik &end_io_wq->work); 70553b381b3SDavid Woodhouse else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 70653b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 70753b381b3SDavid Woodhouse &end_io_wq->work); 708cad321adSChris Mason else 709e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 710e6dcd2dcSChris Mason &end_io_wq->work); 711d20f7043SChris Mason } else { 71253b381b3SDavid Woodhouse if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) 71353b381b3SDavid Woodhouse btrfs_queue_worker(&fs_info->endio_raid56_workers, 71453b381b3SDavid Woodhouse &end_io_wq->work); 71553b381b3SDavid Woodhouse else if (end_io_wq->metadata) 716d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 717d20f7043SChris Mason &end_io_wq->work); 718e6dcd2dcSChris Mason else 719d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 720d20f7043SChris Mason &end_io_wq->work); 721d20f7043SChris Mason } 722ce9adaa5SChris Mason } 723ce9adaa5SChris Mason 7240cb59c99SJosef Bacik /* 7250cb59c99SJosef Bacik * For the metadata arg you want 7260cb59c99SJosef Bacik * 7270cb59c99SJosef Bacik * 0 - if data 7280cb59c99SJosef Bacik * 1 - if normal metadta 7290cb59c99SJosef Bacik * 2 - if writing to the free space cache area 73053b381b3SDavid Woodhouse * 3 - raid parity work 7310cb59c99SJosef Bacik */ 73222c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 73322c59948SChris Mason int metadata) 7340b86a832SChris Mason { 735ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 736ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 737ce9adaa5SChris Mason if (!end_io_wq) 738ce9adaa5SChris Mason return -ENOMEM; 739ce9adaa5SChris Mason 740ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 741ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 74222c59948SChris Mason end_io_wq->info = info; 743ce9adaa5SChris Mason end_io_wq->error = 0; 744ce9adaa5SChris Mason end_io_wq->bio = bio; 74522c59948SChris Mason end_io_wq->metadata = metadata; 746ce9adaa5SChris Mason 747ce9adaa5SChris Mason bio->bi_private = end_io_wq; 748ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 74922c59948SChris Mason return 0; 75022c59948SChris Mason } 75122c59948SChris Mason 752b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7534854ddd0SChris Mason { 7544854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7554854ddd0SChris Mason info->workers.max_workers, 7564854ddd0SChris Mason info->fs_devices->open_devices); 7574854ddd0SChris Mason return 256 * limit; 7584854ddd0SChris Mason } 7594854ddd0SChris Mason 7604a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7614a69a410SChris Mason { 7624a69a410SChris Mason struct async_submit_bio *async; 76379787eaaSJeff Mahoney int ret; 7644a69a410SChris Mason 7654a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 76679787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 767eaf25d93SChris Mason async->mirror_num, async->bio_flags, 768eaf25d93SChris Mason async->bio_offset); 76979787eaaSJeff Mahoney if (ret) 77079787eaaSJeff Mahoney async->error = ret; 7714a69a410SChris Mason } 7724a69a410SChris Mason 7734a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7748b712842SChris Mason { 7758b712842SChris Mason struct btrfs_fs_info *fs_info; 7768b712842SChris Mason struct async_submit_bio *async; 7774854ddd0SChris Mason int limit; 7788b712842SChris Mason 7798b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7808b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7814854ddd0SChris Mason 782b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7834854ddd0SChris Mason limit = limit * 2 / 3; 7844854ddd0SChris Mason 78566657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 786b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7874854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7884854ddd0SChris Mason 78979787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 79079787eaaSJeff Mahoney if (async->error) { 79179787eaaSJeff Mahoney bio_endio(async->bio, async->error); 79279787eaaSJeff Mahoney return; 79379787eaaSJeff Mahoney } 79479787eaaSJeff Mahoney 7954a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 796eaf25d93SChris Mason async->mirror_num, async->bio_flags, 797eaf25d93SChris Mason async->bio_offset); 7984a69a410SChris Mason } 7994a69a410SChris Mason 8004a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 8014a69a410SChris Mason { 8024a69a410SChris Mason struct async_submit_bio *async; 8034a69a410SChris Mason 8044a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 8058b712842SChris Mason kfree(async); 8068b712842SChris Mason } 8078b712842SChris Mason 80844b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 80944b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 810c8b97818SChris Mason unsigned long bio_flags, 811eaf25d93SChris Mason u64 bio_offset, 8124a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 8134a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 81444b8bd7eSChris Mason { 81544b8bd7eSChris Mason struct async_submit_bio *async; 81644b8bd7eSChris Mason 81744b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 81844b8bd7eSChris Mason if (!async) 81944b8bd7eSChris Mason return -ENOMEM; 82044b8bd7eSChris Mason 82144b8bd7eSChris Mason async->inode = inode; 82244b8bd7eSChris Mason async->rw = rw; 82344b8bd7eSChris Mason async->bio = bio; 82444b8bd7eSChris Mason async->mirror_num = mirror_num; 8254a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8264a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8274a69a410SChris Mason 8284a69a410SChris Mason async->work.func = run_one_async_start; 8294a69a410SChris Mason async->work.ordered_func = run_one_async_done; 8304a69a410SChris Mason async->work.ordered_free = run_one_async_free; 8314a69a410SChris Mason 8328b712842SChris Mason async->work.flags = 0; 833c8b97818SChris Mason async->bio_flags = bio_flags; 834eaf25d93SChris Mason async->bio_offset = bio_offset; 8358c8bee1dSChris Mason 83679787eaaSJeff Mahoney async->error = 0; 83779787eaaSJeff Mahoney 838cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 839d313d7a3SChris Mason 8407b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 841d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 842d313d7a3SChris Mason 8438b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 8449473f16cSChris Mason 845771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 846771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 847771ed689SChris Mason wait_event(fs_info->async_submit_wait, 848771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 849771ed689SChris Mason } 850771ed689SChris Mason 85144b8bd7eSChris Mason return 0; 85244b8bd7eSChris Mason } 85344b8bd7eSChris Mason 854ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 855ce3ed71aSChris Mason { 856ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 857ce3ed71aSChris Mason int bio_index = 0; 858ce3ed71aSChris Mason struct btrfs_root *root; 85979787eaaSJeff Mahoney int ret = 0; 860ce3ed71aSChris Mason 861ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 862ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 863ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 86479787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 86579787eaaSJeff Mahoney if (ret) 86679787eaaSJeff Mahoney break; 867ce3ed71aSChris Mason bio_index++; 868ce3ed71aSChris Mason bvec++; 869ce3ed71aSChris Mason } 87079787eaaSJeff Mahoney return ret; 871ce3ed71aSChris Mason } 872ce3ed71aSChris Mason 8734a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8744a69a410SChris Mason struct bio *bio, int mirror_num, 875eaf25d93SChris Mason unsigned long bio_flags, 876eaf25d93SChris Mason u64 bio_offset) 87722c59948SChris Mason { 8788b712842SChris Mason /* 8798b712842SChris Mason * when we're called for a write, we're already in the async 8805443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8818b712842SChris Mason */ 88279787eaaSJeff Mahoney return btree_csum_one_bio(bio); 88322c59948SChris Mason } 88422c59948SChris Mason 8854a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 886eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 887eaf25d93SChris Mason u64 bio_offset) 8884a69a410SChris Mason { 88961891923SStefan Behrens int ret; 89061891923SStefan Behrens 8918b712842SChris Mason /* 8924a69a410SChris Mason * when we're called for a write, we're already in the async 8934a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8948b712842SChris Mason */ 89561891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 89661891923SStefan Behrens if (ret) 89761891923SStefan Behrens bio_endio(bio, ret); 89861891923SStefan Behrens return ret; 8990b86a832SChris Mason } 9000b86a832SChris Mason 901de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 902de0022b9SJosef Bacik { 903de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 904de0022b9SJosef Bacik return 0; 905de0022b9SJosef Bacik #ifdef CONFIG_X86 906de0022b9SJosef Bacik if (cpu_has_xmm4_2) 907de0022b9SJosef Bacik return 0; 908de0022b9SJosef Bacik #endif 909de0022b9SJosef Bacik return 1; 910de0022b9SJosef Bacik } 911de0022b9SJosef Bacik 91244b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 913eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 914eaf25d93SChris Mason u64 bio_offset) 91544b8bd7eSChris Mason { 916de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 9174a69a410SChris Mason int ret; 918cad321adSChris Mason 9197b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 920cad321adSChris Mason /* 921cad321adSChris Mason * called for a read, do the setup so that checksum validation 922cad321adSChris Mason * can happen in the async kernel threads 923cad321adSChris Mason */ 924f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 925f3f266abSChris Mason bio, 1); 9261d4284bdSChris Mason if (ret) 92761891923SStefan Behrens goto out_w_error; 92861891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 9296f3577bdSChris Mason mirror_num, 0); 930de0022b9SJosef Bacik } else if (!async) { 931de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 932de0022b9SJosef Bacik if (ret) 93361891923SStefan Behrens goto out_w_error; 93461891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 935de0022b9SJosef Bacik mirror_num, 0); 93661891923SStefan Behrens } else { 937cad321adSChris Mason /* 93861891923SStefan Behrens * kthread helpers are used to submit writes so that 93961891923SStefan Behrens * checksumming can happen in parallel across all CPUs 940cad321adSChris Mason */ 94161891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 942c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 943eaf25d93SChris Mason bio_offset, 9444a69a410SChris Mason __btree_submit_bio_start, 9454a69a410SChris Mason __btree_submit_bio_done); 94644b8bd7eSChris Mason } 94744b8bd7eSChris Mason 94861891923SStefan Behrens if (ret) { 94961891923SStefan Behrens out_w_error: 95061891923SStefan Behrens bio_endio(bio, ret); 95161891923SStefan Behrens } 95261891923SStefan Behrens return ret; 95361891923SStefan Behrens } 95461891923SStefan Behrens 9553dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 956784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 957a6bc32b8SMel Gorman struct page *newpage, struct page *page, 958a6bc32b8SMel Gorman enum migrate_mode mode) 959784b4e29SChris Mason { 960784b4e29SChris Mason /* 961784b4e29SChris Mason * we can't safely write a btree page from here, 962784b4e29SChris Mason * we haven't done the locking hook 963784b4e29SChris Mason */ 964784b4e29SChris Mason if (PageDirty(page)) 965784b4e29SChris Mason return -EAGAIN; 966784b4e29SChris Mason /* 967784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 968784b4e29SChris Mason * We must have no buffers or drop them. 969784b4e29SChris Mason */ 970784b4e29SChris Mason if (page_has_private(page) && 971784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 972784b4e29SChris Mason return -EAGAIN; 973a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 974784b4e29SChris Mason } 9753dd1462eSJan Beulich #endif 976784b4e29SChris Mason 9770da5468fSChris Mason 9780da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9790da5468fSChris Mason struct writeback_control *wbc) 9800da5468fSChris Mason { 981d1310b2eSChris Mason struct extent_io_tree *tree; 982e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 983e2d84521SMiao Xie int ret; 984e2d84521SMiao Xie 985d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 986d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 987448d640bSChris Mason 988448d640bSChris Mason if (wbc->for_kupdate) 989448d640bSChris Mason return 0; 990448d640bSChris Mason 991e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 992b9473439SChris Mason /* this is a bit racy, but that's ok */ 993e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 994e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 995e2d84521SMiao Xie if (ret < 0) 996793955bcSChris Mason return 0; 997793955bcSChris Mason } 9980b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9990da5468fSChris Mason } 10000da5468fSChris Mason 1001b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 10025f39d397SChris Mason { 1003d1310b2eSChris Mason struct extent_io_tree *tree; 1004d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10058ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 10065f39d397SChris Mason } 10075f39d397SChris Mason 100870dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 10095f39d397SChris Mason { 101098509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 101198509cfcSChris Mason return 0; 10120c4e538bSDavid Sterba 1013f7a52a40SDavid Sterba return try_release_extent_buffer(page); 1014d98237b3SChris Mason } 1015d98237b3SChris Mason 10165f39d397SChris Mason static void btree_invalidatepage(struct page *page, unsigned long offset) 1017d98237b3SChris Mason { 1018d1310b2eSChris Mason struct extent_io_tree *tree; 1019d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 10205f39d397SChris Mason extent_invalidatepage(tree, page, offset); 10215f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 10229ad6b7bcSChris Mason if (PagePrivate(page)) { 1023d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 1024d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 10259ad6b7bcSChris Mason ClearPagePrivate(page); 10269ad6b7bcSChris Mason set_page_private(page, 0); 10279ad6b7bcSChris Mason page_cache_release(page); 10289ad6b7bcSChris Mason } 1029d98237b3SChris Mason } 1030d98237b3SChris Mason 10310b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10320b32f4bbSJosef Bacik { 1033bb146eb2SJosef Bacik #ifdef DEBUG 10340b32f4bbSJosef Bacik struct extent_buffer *eb; 10350b32f4bbSJosef Bacik 10360b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10370b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10380b32f4bbSJosef Bacik BUG_ON(!eb); 10390b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10400b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10410b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1042bb146eb2SJosef Bacik #endif 10430b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10440b32f4bbSJosef Bacik } 10450b32f4bbSJosef Bacik 10467f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1047d98237b3SChris Mason .readpage = btree_readpage, 10480da5468fSChris Mason .writepages = btree_writepages, 10495f39d397SChris Mason .releasepage = btree_releasepage, 10505f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10515a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1052784b4e29SChris Mason .migratepage = btree_migratepage, 10535a92bc88SChris Mason #endif 10540b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1055d98237b3SChris Mason }; 1056123abc88SChris Mason 1057ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1058ca7a79adSChris Mason u64 parent_transid) 1059090d1875SChris Mason { 10605f39d397SChris Mason struct extent_buffer *buf = NULL; 10615f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1062de428b63SChris Mason int ret = 0; 1063090d1875SChris Mason 1064db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10655f39d397SChris Mason if (!buf) 1066090d1875SChris Mason return 0; 1067d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1068bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10695f39d397SChris Mason free_extent_buffer(buf); 1070de428b63SChris Mason return ret; 1071090d1875SChris Mason } 1072090d1875SChris Mason 1073ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1074ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1075ab0fff03SArne Jansen { 1076ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1077ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1078ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1079ab0fff03SArne Jansen int ret; 1080ab0fff03SArne Jansen 1081ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1082ab0fff03SArne Jansen if (!buf) 1083ab0fff03SArne Jansen return 0; 1084ab0fff03SArne Jansen 1085ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1086ab0fff03SArne Jansen 1087ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1088ab0fff03SArne Jansen btree_get_extent, mirror_num); 1089ab0fff03SArne Jansen if (ret) { 1090ab0fff03SArne Jansen free_extent_buffer(buf); 1091ab0fff03SArne Jansen return ret; 1092ab0fff03SArne Jansen } 1093ab0fff03SArne Jansen 1094ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1095ab0fff03SArne Jansen free_extent_buffer(buf); 1096ab0fff03SArne Jansen return -EIO; 10970b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1098ab0fff03SArne Jansen *eb = buf; 1099ab0fff03SArne Jansen } else { 1100ab0fff03SArne Jansen free_extent_buffer(buf); 1101ab0fff03SArne Jansen } 1102ab0fff03SArne Jansen return 0; 1103ab0fff03SArne Jansen } 1104ab0fff03SArne Jansen 11050999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 11060999df54SChris Mason u64 bytenr, u32 blocksize) 11070999df54SChris Mason { 11080999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11090999df54SChris Mason struct extent_buffer *eb; 11100999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1111f09d1f60SDavid Sterba bytenr, blocksize); 11120999df54SChris Mason return eb; 11130999df54SChris Mason } 11140999df54SChris Mason 11150999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 11160999df54SChris Mason u64 bytenr, u32 blocksize) 11170999df54SChris Mason { 11180999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 11190999df54SChris Mason struct extent_buffer *eb; 11200999df54SChris Mason 11210999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1122727011e0SChris Mason bytenr, blocksize); 11230999df54SChris Mason return eb; 11240999df54SChris Mason } 11250999df54SChris Mason 11260999df54SChris Mason 1127e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1128e02119d5SChris Mason { 1129727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11308aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1131e02119d5SChris Mason } 1132e02119d5SChris Mason 1133e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1134e02119d5SChris Mason { 1135727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11368aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1137e02119d5SChris Mason } 1138e02119d5SChris Mason 1139db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1140ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1141e20d96d6SChris Mason { 11425f39d397SChris Mason struct extent_buffer *buf = NULL; 114319c00ddcSChris Mason int ret; 114419c00ddcSChris Mason 1145db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11465f39d397SChris Mason if (!buf) 1147d98237b3SChris Mason return NULL; 1148e4204dedSChris Mason 1149ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11505f39d397SChris Mason return buf; 1151ce9adaa5SChris Mason 1152eb60ceacSChris Mason } 1153eb60ceacSChris Mason 1154d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11555f39d397SChris Mason struct extent_buffer *buf) 1156ed2ff2cbSChris Mason { 1157e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1158e2d84521SMiao Xie 115955c69072SChris Mason if (btrfs_header_generation(buf) == 1160e2d84521SMiao Xie fs_info->running_transaction->transid) { 1161b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1162b4ce94deSChris Mason 1163b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1164e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1165e2d84521SMiao Xie -buf->len, 1166e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1167b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1168b9473439SChris Mason btrfs_set_lock_blocking(buf); 11690b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1170925baeddSChris Mason } 11715f39d397SChris Mason } 1172ed7b63ebSJosef Bacik } 11735f39d397SChris Mason 1174143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 117587ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11769f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1177e20d96d6SChris Mason u64 objectid) 1178d97e63b6SChris Mason { 1179cfaa7295SChris Mason root->node = NULL; 1180a28ec197SChris Mason root->commit_root = NULL; 1181db94535dSChris Mason root->sectorsize = sectorsize; 1182db94535dSChris Mason root->nodesize = nodesize; 1183db94535dSChris Mason root->leafsize = leafsize; 118487ee04ebSChris Mason root->stripesize = stripesize; 1185123abc88SChris Mason root->ref_cows = 0; 11860b86a832SChris Mason root->track_dirty = 0; 1187c71bf099SYan, Zheng root->in_radix = 0; 1188d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1189d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11900b86a832SChris Mason 11910f7d52f4SChris Mason root->objectid = objectid; 11920f7d52f4SChris Mason root->last_trans = 0; 119313a8a7c8SYan, Zheng root->highest_objectid = 0; 119458176a96SJosef Bacik root->name = NULL; 11956bef4d31SEric Paris root->inode_tree = RB_ROOT; 119616cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1197f0486c68SYan, Zheng root->block_rsv = NULL; 1198d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11990b86a832SChris Mason 12000b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 12015d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 12022ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 12032ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1204d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 12055d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1206f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 12072ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 12082ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1209a2135011SChris Mason mutex_init(&root->objectid_mutex); 1210e02119d5SChris Mason mutex_init(&root->log_mutex); 12117237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 12127237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 12137237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 12147237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 12157237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 12167237f183SYan Zheng atomic_set(&root->log_writers, 0); 12172ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 12188a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 12197237f183SYan Zheng root->log_transid = 0; 1220257c62e1SChris Mason root->last_log_commit = 0; 1221d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1222f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1223017e5369SChris Mason 12243768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 12253768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 12266702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 122758176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 12283f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 122958176a96SJosef Bacik init_completion(&root->kobj_unregister); 12306702ed49SChris Mason root->defrag_running = 0; 12314d775673SChris Mason root->root_key.objectid = objectid; 12320ee5dc67SAl Viro root->anon_dev = 0; 12338ea05e3aSAlexander Block 12345f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12353768f368SChris Mason } 12363768f368SChris Mason 1237200a5c17SJeff Mahoney static int __must_check find_and_setup_root(struct btrfs_root *tree_root, 12389f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 12399f5fae2fSChris Mason u64 objectid, 1240e20d96d6SChris Mason struct btrfs_root *root) 12413768f368SChris Mason { 12423768f368SChris Mason int ret; 1243db94535dSChris Mason u32 blocksize; 124484234f3aSYan Zheng u64 generation; 12453768f368SChris Mason 1246db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 124787ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 124887ee04ebSChris Mason root, fs_info, objectid); 12493768f368SChris Mason ret = btrfs_find_last_root(tree_root, objectid, 12503768f368SChris Mason &root->root_item, &root->root_key); 12514df27c4dSYan, Zheng if (ret > 0) 12524df27c4dSYan, Zheng return -ENOENT; 1253200a5c17SJeff Mahoney else if (ret < 0) 1254200a5c17SJeff Mahoney return ret; 12553768f368SChris Mason 125684234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1257db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1258af31f5e5SChris Mason root->commit_root = NULL; 1259db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 126084234f3aSYan Zheng blocksize, generation); 1261b9fab919SChris Mason if (!root->node || !btrfs_buffer_uptodate(root->node, generation, 0)) { 126268433b73SChris Mason free_extent_buffer(root->node); 1263af31f5e5SChris Mason root->node = NULL; 126468433b73SChris Mason return -EIO; 126568433b73SChris Mason } 12664df27c4dSYan, Zheng root->commit_root = btrfs_root_node(root); 1267d97e63b6SChris Mason return 0; 1268d97e63b6SChris Mason } 1269d97e63b6SChris Mason 1270f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12716f07e42eSAl Viro { 12726f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12736f07e42eSAl Viro if (root) 12746f07e42eSAl Viro root->fs_info = fs_info; 12756f07e42eSAl Viro return root; 12766f07e42eSAl Viro } 12776f07e42eSAl Viro 127820897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 127920897f5cSArne Jansen struct btrfs_fs_info *fs_info, 128020897f5cSArne Jansen u64 objectid) 128120897f5cSArne Jansen { 128220897f5cSArne Jansen struct extent_buffer *leaf; 128320897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 128420897f5cSArne Jansen struct btrfs_root *root; 128520897f5cSArne Jansen struct btrfs_key key; 128620897f5cSArne Jansen int ret = 0; 128720897f5cSArne Jansen u64 bytenr; 12886463fe58SStefan Behrens uuid_le uuid; 128920897f5cSArne Jansen 129020897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 129120897f5cSArne Jansen if (!root) 129220897f5cSArne Jansen return ERR_PTR(-ENOMEM); 129320897f5cSArne Jansen 129420897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 129520897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 129620897f5cSArne Jansen root, fs_info, objectid); 129720897f5cSArne Jansen root->root_key.objectid = objectid; 129820897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 129920897f5cSArne Jansen root->root_key.offset = 0; 130020897f5cSArne Jansen 130120897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 130220897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 130320897f5cSArne Jansen if (IS_ERR(leaf)) { 130420897f5cSArne Jansen ret = PTR_ERR(leaf); 13051dd05682STsutomu Itoh leaf = NULL; 130620897f5cSArne Jansen goto fail; 130720897f5cSArne Jansen } 130820897f5cSArne Jansen 130920897f5cSArne Jansen bytenr = leaf->start; 131020897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 131120897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 131220897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 131320897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 131420897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 131520897f5cSArne Jansen root->node = leaf; 131620897f5cSArne Jansen 131720897f5cSArne Jansen write_extent_buffer(leaf, fs_info->fsid, 131820897f5cSArne Jansen (unsigned long)btrfs_header_fsid(leaf), 131920897f5cSArne Jansen BTRFS_FSID_SIZE); 132020897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 132120897f5cSArne Jansen (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 132220897f5cSArne Jansen BTRFS_UUID_SIZE); 132320897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 132420897f5cSArne Jansen 132520897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 132620897f5cSArne Jansen root->track_dirty = 1; 132720897f5cSArne Jansen 132820897f5cSArne Jansen 132920897f5cSArne Jansen root->root_item.flags = 0; 133020897f5cSArne Jansen root->root_item.byte_limit = 0; 133120897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 133220897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 133320897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 133420897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 133520897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 133620897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 133720897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 13386463fe58SStefan Behrens uuid_le_gen(&uuid); 13396463fe58SStefan Behrens memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE); 134020897f5cSArne Jansen root->root_item.drop_level = 0; 134120897f5cSArne Jansen 134220897f5cSArne Jansen key.objectid = objectid; 134320897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 134420897f5cSArne Jansen key.offset = 0; 134520897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 134620897f5cSArne Jansen if (ret) 134720897f5cSArne Jansen goto fail; 134820897f5cSArne Jansen 134920897f5cSArne Jansen btrfs_tree_unlock(leaf); 135020897f5cSArne Jansen 135120897f5cSArne Jansen return root; 13521dd05682STsutomu Itoh 13531dd05682STsutomu Itoh fail: 13541dd05682STsutomu Itoh if (leaf) { 13551dd05682STsutomu Itoh btrfs_tree_unlock(leaf); 13561dd05682STsutomu Itoh free_extent_buffer(leaf); 13571dd05682STsutomu Itoh } 13581dd05682STsutomu Itoh kfree(root); 13591dd05682STsutomu Itoh 13601dd05682STsutomu Itoh return ERR_PTR(ret); 136120897f5cSArne Jansen } 136220897f5cSArne Jansen 13637237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1364e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13650f7d52f4SChris Mason { 13660f7d52f4SChris Mason struct btrfs_root *root; 13670f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13687237f183SYan Zheng struct extent_buffer *leaf; 1369e02119d5SChris Mason 13706f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1371e02119d5SChris Mason if (!root) 13727237f183SYan Zheng return ERR_PTR(-ENOMEM); 1373e02119d5SChris Mason 1374e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1375e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1376e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1377e02119d5SChris Mason 1378e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1379e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1380e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13817237f183SYan Zheng /* 13827237f183SYan Zheng * log trees do not get reference counted because they go away 13837237f183SYan Zheng * before a real commit is actually done. They do store pointers 13847237f183SYan Zheng * to file data extents, and those reference counts still get 13857237f183SYan Zheng * updated (along with back refs to the log tree). 13867237f183SYan Zheng */ 1387e02119d5SChris Mason root->ref_cows = 0; 1388e02119d5SChris Mason 13895d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 139066d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13915581a51aSJan Schmidt 0, 0, 0); 13927237f183SYan Zheng if (IS_ERR(leaf)) { 13937237f183SYan Zheng kfree(root); 13947237f183SYan Zheng return ERR_CAST(leaf); 13957237f183SYan Zheng } 1396e02119d5SChris Mason 13975d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13985d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13995d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 14005d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 14015d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 14027237f183SYan Zheng root->node = leaf; 1403e02119d5SChris Mason 1404e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1405e02119d5SChris Mason (unsigned long)btrfs_header_fsid(root->node), 1406e02119d5SChris Mason BTRFS_FSID_SIZE); 1407e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1408e02119d5SChris Mason btrfs_tree_unlock(root->node); 14097237f183SYan Zheng return root; 14107237f183SYan Zheng } 14117237f183SYan Zheng 14127237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 14137237f183SYan Zheng struct btrfs_fs_info *fs_info) 14147237f183SYan Zheng { 14157237f183SYan Zheng struct btrfs_root *log_root; 14167237f183SYan Zheng 14177237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 14187237f183SYan Zheng if (IS_ERR(log_root)) 14197237f183SYan Zheng return PTR_ERR(log_root); 14207237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 14217237f183SYan Zheng fs_info->log_root_tree = log_root; 14227237f183SYan Zheng return 0; 14237237f183SYan Zheng } 14247237f183SYan Zheng 14257237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 14267237f183SYan Zheng struct btrfs_root *root) 14277237f183SYan Zheng { 14287237f183SYan Zheng struct btrfs_root *log_root; 14297237f183SYan Zheng struct btrfs_inode_item *inode_item; 14307237f183SYan Zheng 14317237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 14327237f183SYan Zheng if (IS_ERR(log_root)) 14337237f183SYan Zheng return PTR_ERR(log_root); 14347237f183SYan Zheng 14357237f183SYan Zheng log_root->last_trans = trans->transid; 14367237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14377237f183SYan Zheng 14387237f183SYan Zheng inode_item = &log_root->root_item.inode; 14397237f183SYan Zheng inode_item->generation = cpu_to_le64(1); 14407237f183SYan Zheng inode_item->size = cpu_to_le64(3); 14417237f183SYan Zheng inode_item->nlink = cpu_to_le32(1); 14427237f183SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 14437237f183SYan Zheng inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 14447237f183SYan Zheng 14455d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14467237f183SYan Zheng 14477237f183SYan Zheng WARN_ON(root->log_root); 14487237f183SYan Zheng root->log_root = log_root; 14497237f183SYan Zheng root->log_transid = 0; 1450257c62e1SChris Mason root->last_log_commit = 0; 1451e02119d5SChris Mason return 0; 1452e02119d5SChris Mason } 1453e02119d5SChris Mason 1454e02119d5SChris Mason struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, 1455e02119d5SChris Mason struct btrfs_key *location) 1456e02119d5SChris Mason { 1457e02119d5SChris Mason struct btrfs_root *root; 1458e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14590f7d52f4SChris Mason struct btrfs_path *path; 14605f39d397SChris Mason struct extent_buffer *l; 146184234f3aSYan Zheng u64 generation; 1462db94535dSChris Mason u32 blocksize; 14630f7d52f4SChris Mason int ret = 0; 14648ea05e3aSAlexander Block int slot; 14650f7d52f4SChris Mason 14666f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 14670cf6c620SChris Mason if (!root) 14680f7d52f4SChris Mason return ERR_PTR(-ENOMEM); 14690f7d52f4SChris Mason if (location->offset == (u64)-1) { 1470db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 14710f7d52f4SChris Mason location->objectid, root); 14720f7d52f4SChris Mason if (ret) { 14730f7d52f4SChris Mason kfree(root); 14740f7d52f4SChris Mason return ERR_PTR(ret); 14750f7d52f4SChris Mason } 147613a8a7c8SYan, Zheng goto out; 14770f7d52f4SChris Mason } 14780f7d52f4SChris Mason 1479db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 148087ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 148187ee04ebSChris Mason root, fs_info, location->objectid); 14820f7d52f4SChris Mason 14830f7d52f4SChris Mason path = btrfs_alloc_path(); 1484db5b493aSTsutomu Itoh if (!path) { 1485db5b493aSTsutomu Itoh kfree(root); 1486db5b493aSTsutomu Itoh return ERR_PTR(-ENOMEM); 1487db5b493aSTsutomu Itoh } 14880f7d52f4SChris Mason ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 148913a8a7c8SYan, Zheng if (ret == 0) { 14905f39d397SChris Mason l = path->nodes[0]; 14918ea05e3aSAlexander Block slot = path->slots[0]; 14925fbf83c1SStefan Behrens btrfs_read_root_item(l, slot, &root->root_item); 149344b36eb2SYan Zheng memcpy(&root->root_key, location, sizeof(*location)); 149413a8a7c8SYan, Zheng } 14950f7d52f4SChris Mason btrfs_free_path(path); 14960f7d52f4SChris Mason if (ret) { 14975e540f77STsutomu Itoh kfree(root); 149813a8a7c8SYan, Zheng if (ret > 0) 149913a8a7c8SYan, Zheng ret = -ENOENT; 15000f7d52f4SChris Mason return ERR_PTR(ret); 15010f7d52f4SChris Mason } 150213a8a7c8SYan, Zheng 150384234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1504db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1505db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 150684234f3aSYan Zheng blocksize, generation); 1507416bc658SJosef Bacik if (!root->node || !extent_buffer_uptodate(root->node)) { 1508416bc658SJosef Bacik ret = (!root->node) ? -ENOMEM : -EIO; 1509416bc658SJosef Bacik 1510416bc658SJosef Bacik free_extent_buffer(root->node); 1511416bc658SJosef Bacik kfree(root); 1512416bc658SJosef Bacik return ERR_PTR(ret); 1513416bc658SJosef Bacik } 1514416bc658SJosef Bacik 15155d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 151613a8a7c8SYan, Zheng out: 151708fe4db1SLi Zefan if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 15180f7d52f4SChris Mason root->ref_cows = 1; 151908fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 152008fe4db1SLi Zefan } 152113a8a7c8SYan, Zheng 15225eda7b5eSChris Mason return root; 15235eda7b5eSChris Mason } 15245eda7b5eSChris Mason 1525edbd8d4eSChris Mason struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 1526edbd8d4eSChris Mason struct btrfs_key *location) 15275eda7b5eSChris Mason { 15285eda7b5eSChris Mason struct btrfs_root *root; 15295eda7b5eSChris Mason int ret; 15305eda7b5eSChris Mason 1531edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1532edbd8d4eSChris Mason return fs_info->tree_root; 1533edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1534edbd8d4eSChris Mason return fs_info->extent_root; 15358f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 15368f18cf13SChris Mason return fs_info->chunk_root; 15378f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 15388f18cf13SChris Mason return fs_info->dev_root; 15390403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 15400403e47eSYan Zheng return fs_info->csum_root; 1541bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1542bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1543bcef60f2SArne Jansen ERR_PTR(-ENOENT); 15444df27c4dSYan, Zheng again: 15454df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 15465eda7b5eSChris Mason root = radix_tree_lookup(&fs_info->fs_roots_radix, 15475eda7b5eSChris Mason (unsigned long)location->objectid); 15484df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 15495eda7b5eSChris Mason if (root) 15505eda7b5eSChris Mason return root; 15515eda7b5eSChris Mason 1552e02119d5SChris Mason root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 15535eda7b5eSChris Mason if (IS_ERR(root)) 15545eda7b5eSChris Mason return root; 15553394e160SChris Mason 1556581bb050SLi Zefan root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1557581bb050SLi Zefan root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1558581bb050SLi Zefan GFP_NOFS); 155935a30d7cSDavid Sterba if (!root->free_ino_pinned || !root->free_ino_ctl) { 156035a30d7cSDavid Sterba ret = -ENOMEM; 1561581bb050SLi Zefan goto fail; 156235a30d7cSDavid Sterba } 1563581bb050SLi Zefan 1564581bb050SLi Zefan btrfs_init_free_ino_ctl(root); 1565581bb050SLi Zefan mutex_init(&root->fs_commit_mutex); 1566581bb050SLi Zefan spin_lock_init(&root->cache_lock); 1567581bb050SLi Zefan init_waitqueue_head(&root->cache_wait); 1568581bb050SLi Zefan 15690ee5dc67SAl Viro ret = get_anon_bdev(&root->anon_dev); 1570ac08aedfSChris Mason if (ret) 1571ac08aedfSChris Mason goto fail; 15723394e160SChris Mason 1573d68fc57bSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) { 1574d68fc57bSYan, Zheng ret = -ENOENT; 1575d68fc57bSYan, Zheng goto fail; 1576d68fc57bSYan, Zheng } 1577d68fc57bSYan, Zheng 1578d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1579d68fc57bSYan, Zheng if (ret < 0) 1580d68fc57bSYan, Zheng goto fail; 1581d68fc57bSYan, Zheng if (ret == 0) 1582d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1583d68fc57bSYan, Zheng 15844df27c4dSYan, Zheng ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 15854df27c4dSYan, Zheng if (ret) 15864df27c4dSYan, Zheng goto fail; 15874df27c4dSYan, Zheng 15884df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 15892619ba1fSChris Mason ret = radix_tree_insert(&fs_info->fs_roots_radix, 15902619ba1fSChris Mason (unsigned long)root->root_key.objectid, 15910f7d52f4SChris Mason root); 1592d68fc57bSYan, Zheng if (ret == 0) 15934df27c4dSYan, Zheng root->in_radix = 1; 1594d68fc57bSYan, Zheng 15954df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 15964df27c4dSYan, Zheng radix_tree_preload_end(); 15970f7d52f4SChris Mason if (ret) { 15984df27c4dSYan, Zheng if (ret == -EEXIST) { 15994df27c4dSYan, Zheng free_fs_root(root); 16004df27c4dSYan, Zheng goto again; 16010f7d52f4SChris Mason } 16024df27c4dSYan, Zheng goto fail; 16034df27c4dSYan, Zheng } 16044df27c4dSYan, Zheng 1605edbd8d4eSChris Mason ret = btrfs_find_dead_roots(fs_info->tree_root, 16065d4f98a2SYan Zheng root->root_key.objectid); 16074df27c4dSYan, Zheng WARN_ON(ret); 1608edbd8d4eSChris Mason return root; 16094df27c4dSYan, Zheng fail: 16104df27c4dSYan, Zheng free_fs_root(root); 16114df27c4dSYan, Zheng return ERR_PTR(ret); 1612edbd8d4eSChris Mason } 1613edbd8d4eSChris Mason 161404160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 161504160088SChris Mason { 161604160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 161704160088SChris Mason int ret = 0; 161804160088SChris Mason struct btrfs_device *device; 161904160088SChris Mason struct backing_dev_info *bdi; 1620b7967db7SChris Mason 16211f78160cSXiao Guangrong rcu_read_lock(); 16221f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1623dfe25020SChris Mason if (!device->bdev) 1624dfe25020SChris Mason continue; 162504160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 162604160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 162704160088SChris Mason ret = 1; 162804160088SChris Mason break; 162904160088SChris Mason } 163004160088SChris Mason } 16311f78160cSXiao Guangrong rcu_read_unlock(); 163204160088SChris Mason return ret; 163304160088SChris Mason } 163404160088SChris Mason 163538b66988SChris Mason /* 1636ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1637ad081f14SJens Axboe * bdi again. 1638ad081f14SJens Axboe */ 163904160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 164004160088SChris Mason { 1641ad081f14SJens Axboe int err; 1642ad081f14SJens Axboe 1643ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1644e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1645ad081f14SJens Axboe if (err) 1646ad081f14SJens Axboe return err; 1647ad081f14SJens Axboe 16484575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 164904160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 165004160088SChris Mason bdi->congested_data = info; 165104160088SChris Mason return 0; 165204160088SChris Mason } 165304160088SChris Mason 16548b712842SChris Mason /* 16558b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16568b712842SChris Mason * functions. This is where read checksum verification actually happens 16578b712842SChris Mason */ 16588b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1659ce9adaa5SChris Mason { 1660ce9adaa5SChris Mason struct bio *bio; 16618b712842SChris Mason struct end_io_wq *end_io_wq; 16628b712842SChris Mason struct btrfs_fs_info *fs_info; 1663ce9adaa5SChris Mason int error; 1664ce9adaa5SChris Mason 16658b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1666ce9adaa5SChris Mason bio = end_io_wq->bio; 16678b712842SChris Mason fs_info = end_io_wq->info; 16688b712842SChris Mason 1669ce9adaa5SChris Mason error = end_io_wq->error; 1670ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1671ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1672ce9adaa5SChris Mason kfree(end_io_wq); 1673ce9adaa5SChris Mason bio_endio(bio, error); 1674ce9adaa5SChris Mason } 167544b8bd7eSChris Mason 1676a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1677a74a4b97SChris Mason { 1678a74a4b97SChris Mason struct btrfs_root *root = arg; 1679a74a4b97SChris Mason 1680a74a4b97SChris Mason do { 16819d1a2a3aSDavid Sterba int again = 0; 16829d1a2a3aSDavid Sterba 168376dda93cSYan, Zheng if (!(root->fs_info->sb->s_flags & MS_RDONLY) && 16849d1a2a3aSDavid Sterba down_read_trylock(&root->fs_info->sb->s_umount)) { 16859d1a2a3aSDavid Sterba if (mutex_trylock(&root->fs_info->cleaner_mutex)) { 168624bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 16879d1a2a3aSDavid Sterba again = btrfs_clean_one_deleted_snapshot(root); 1688a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 16899d1a2a3aSDavid Sterba } 16904cb5300bSChris Mason btrfs_run_defrag_inodes(root->fs_info); 16919d1a2a3aSDavid Sterba up_read(&root->fs_info->sb->s_umount); 169276dda93cSYan, Zheng } 1693a74a4b97SChris Mason 16949d1a2a3aSDavid Sterba if (!try_to_freeze() && !again) { 1695a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16968929ecfaSYan, Zheng if (!kthread_should_stop()) 1697a74a4b97SChris Mason schedule(); 1698a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1699a74a4b97SChris Mason } 1700a74a4b97SChris Mason } while (!kthread_should_stop()); 1701a74a4b97SChris Mason return 0; 1702a74a4b97SChris Mason } 1703a74a4b97SChris Mason 1704a74a4b97SChris Mason static int transaction_kthread(void *arg) 1705a74a4b97SChris Mason { 1706a74a4b97SChris Mason struct btrfs_root *root = arg; 1707a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1708a74a4b97SChris Mason struct btrfs_transaction *cur; 17098929ecfaSYan, Zheng u64 transid; 1710a74a4b97SChris Mason unsigned long now; 1711a74a4b97SChris Mason unsigned long delay; 1712914b2007SJan Kara bool cannot_commit; 1713a74a4b97SChris Mason 1714a74a4b97SChris Mason do { 1715914b2007SJan Kara cannot_commit = false; 1716a74a4b97SChris Mason delay = HZ * 30; 1717a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1718a74a4b97SChris Mason 1719a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1720a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1721a74a4b97SChris Mason if (!cur) { 1722a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1723a74a4b97SChris Mason goto sleep; 1724a74a4b97SChris Mason } 172531153d81SYan Zheng 1726a74a4b97SChris Mason now = get_seconds(); 17278929ecfaSYan, Zheng if (!cur->blocked && 17288929ecfaSYan, Zheng (now < cur->start_time || now - cur->start_time < 30)) { 1729a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1730a74a4b97SChris Mason delay = HZ * 5; 1731a74a4b97SChris Mason goto sleep; 1732a74a4b97SChris Mason } 17338929ecfaSYan, Zheng transid = cur->transid; 1734a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 173556bec294SChris Mason 173679787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1737354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1738914b2007SJan Kara if (IS_ERR(trans)) { 1739354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1740914b2007SJan Kara cannot_commit = true; 174179787eaaSJeff Mahoney goto sleep; 1742914b2007SJan Kara } 17438929ecfaSYan, Zheng if (transid == trans->transid) { 174479787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 17458929ecfaSYan, Zheng } else { 17468929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 17478929ecfaSYan, Zheng } 1748a74a4b97SChris Mason sleep: 1749a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1750a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1751a74a4b97SChris Mason 1752a0acae0eSTejun Heo if (!try_to_freeze()) { 1753a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17548929ecfaSYan, Zheng if (!kthread_should_stop() && 1755914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1756914b2007SJan Kara cannot_commit)) 1757a74a4b97SChris Mason schedule_timeout(delay); 1758a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1759a74a4b97SChris Mason } 1760a74a4b97SChris Mason } while (!kthread_should_stop()); 1761a74a4b97SChris Mason return 0; 1762a74a4b97SChris Mason } 1763a74a4b97SChris Mason 1764af31f5e5SChris Mason /* 1765af31f5e5SChris Mason * this will find the highest generation in the array of 1766af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1767af31f5e5SChris Mason * or -1 if we can't find anything. 1768af31f5e5SChris Mason * 1769af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1770af31f5e5SChris Mason * generation of the latest root in the array with the generation 1771af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1772af31f5e5SChris Mason */ 1773af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1774af31f5e5SChris Mason { 1775af31f5e5SChris Mason u64 cur; 1776af31f5e5SChris Mason int newest_index = -1; 1777af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1778af31f5e5SChris Mason int i; 1779af31f5e5SChris Mason 1780af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1781af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1782af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1783af31f5e5SChris Mason if (cur == newest_gen) 1784af31f5e5SChris Mason newest_index = i; 1785af31f5e5SChris Mason } 1786af31f5e5SChris Mason 1787af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1788af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1789af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1790af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1791af31f5e5SChris Mason if (cur == newest_gen) 1792af31f5e5SChris Mason newest_index = 0; 1793af31f5e5SChris Mason } 1794af31f5e5SChris Mason return newest_index; 1795af31f5e5SChris Mason } 1796af31f5e5SChris Mason 1797af31f5e5SChris Mason 1798af31f5e5SChris Mason /* 1799af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1800af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1801af31f5e5SChris Mason * field in the fs_info struct 1802af31f5e5SChris Mason */ 1803af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1804af31f5e5SChris Mason u64 newest_gen) 1805af31f5e5SChris Mason { 1806af31f5e5SChris Mason int newest_index = -1; 1807af31f5e5SChris Mason 1808af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1809af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1810af31f5e5SChris Mason if (newest_index == -1) { 1811af31f5e5SChris Mason info->backup_root_index = 0; 1812af31f5e5SChris Mason } else { 1813af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1814af31f5e5SChris Mason } 1815af31f5e5SChris Mason } 1816af31f5e5SChris Mason 1817af31f5e5SChris Mason /* 1818af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1819af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1820af31f5e5SChris Mason * done 1821af31f5e5SChris Mason */ 1822af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1823af31f5e5SChris Mason { 1824af31f5e5SChris Mason int next_backup; 1825af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1826af31f5e5SChris Mason int last_backup; 1827af31f5e5SChris Mason 1828af31f5e5SChris Mason next_backup = info->backup_root_index; 1829af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1830af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1831af31f5e5SChris Mason 1832af31f5e5SChris Mason /* 1833af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1834af31f5e5SChris Mason * this happens only at umount 1835af31f5e5SChris Mason */ 1836af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1837af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1838af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1839af31f5e5SChris Mason next_backup = last_backup; 1840af31f5e5SChris Mason 1841af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1842af31f5e5SChris Mason 1843af31f5e5SChris Mason /* 1844af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1845af31f5e5SChris Mason * regardless of which ones we use today 1846af31f5e5SChris Mason */ 1847af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1848af31f5e5SChris Mason 1849af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1850af31f5e5SChris Mason 1851af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1852af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1853af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1854af31f5e5SChris Mason 1855af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1856af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1857af31f5e5SChris Mason 1858af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1859af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1860af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1861af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1862af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1863af31f5e5SChris Mason 1864af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1865af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1866af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1867af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1868af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1869af31f5e5SChris Mason 18707c7e82a7SChris Mason /* 18717c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18727c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18737c7e82a7SChris Mason */ 18747c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18757c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18767c7e82a7SChris Mason info->fs_root->node->start); 1877af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1878af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1879af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1880af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18817c7e82a7SChris Mason } 1882af31f5e5SChris Mason 1883af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1884af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1885af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1886af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1887af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1890af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1891af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1892af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1893af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1896af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1897af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1898af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1899af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1900af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1901af31f5e5SChris Mason 1902af31f5e5SChris Mason /* 1903af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1904af31f5e5SChris Mason * for the next commit 1905af31f5e5SChris Mason */ 1906af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1907af31f5e5SChris Mason &info->super_for_commit->super_roots, 1908af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1909af31f5e5SChris Mason } 1910af31f5e5SChris Mason 1911af31f5e5SChris Mason /* 1912af31f5e5SChris Mason * this copies info out of the root backup array and back into 1913af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1914af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1915af31f5e5SChris Mason * tried and the last backup index you used. 1916af31f5e5SChris Mason * 1917af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1918af31f5e5SChris Mason */ 1919af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1920af31f5e5SChris Mason struct btrfs_super_block *super, 1921af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1922af31f5e5SChris Mason { 1923af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1924af31f5e5SChris Mason int newest = *backup_index; 1925af31f5e5SChris Mason 1926af31f5e5SChris Mason if (*num_backups_tried == 0) { 1927af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1928af31f5e5SChris Mason 1929af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1930af31f5e5SChris Mason if (newest == -1) 1931af31f5e5SChris Mason return -1; 1932af31f5e5SChris Mason 1933af31f5e5SChris Mason *backup_index = newest; 1934af31f5e5SChris Mason *num_backups_tried = 1; 1935af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1936af31f5e5SChris Mason /* we've tried all the backups, all done */ 1937af31f5e5SChris Mason return -1; 1938af31f5e5SChris Mason } else { 1939af31f5e5SChris Mason /* jump to the next oldest backup */ 1940af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1941af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1942af31f5e5SChris Mason *backup_index = newest; 1943af31f5e5SChris Mason *num_backups_tried += 1; 1944af31f5e5SChris Mason } 1945af31f5e5SChris Mason root_backup = super->super_roots + newest; 1946af31f5e5SChris Mason 1947af31f5e5SChris Mason btrfs_set_super_generation(super, 1948af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1949af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1950af31f5e5SChris Mason btrfs_set_super_root_level(super, 1951af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1952af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1953af31f5e5SChris Mason 1954af31f5e5SChris Mason /* 1955af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1956af31f5e5SChris Mason * need a fsck 1957af31f5e5SChris Mason */ 1958af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1959af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1960af31f5e5SChris Mason return 0; 1961af31f5e5SChris Mason } 1962af31f5e5SChris Mason 19637abadb64SLiu Bo /* helper to cleanup workers */ 19647abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) 19657abadb64SLiu Bo { 19667abadb64SLiu Bo btrfs_stop_workers(&fs_info->generic_worker); 19677abadb64SLiu Bo btrfs_stop_workers(&fs_info->fixup_workers); 19687abadb64SLiu Bo btrfs_stop_workers(&fs_info->delalloc_workers); 19697abadb64SLiu Bo btrfs_stop_workers(&fs_info->workers); 19707abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_workers); 19717abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_workers); 19727abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_raid56_workers); 19737abadb64SLiu Bo btrfs_stop_workers(&fs_info->rmw_workers); 19747abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_meta_write_workers); 19757abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_write_workers); 19767abadb64SLiu Bo btrfs_stop_workers(&fs_info->endio_freespace_worker); 19777abadb64SLiu Bo btrfs_stop_workers(&fs_info->submit_workers); 19787abadb64SLiu Bo btrfs_stop_workers(&fs_info->delayed_workers); 19797abadb64SLiu Bo btrfs_stop_workers(&fs_info->caching_workers); 19807abadb64SLiu Bo btrfs_stop_workers(&fs_info->readahead_workers); 19817abadb64SLiu Bo btrfs_stop_workers(&fs_info->flush_workers); 19822f232036SJan Schmidt btrfs_stop_workers(&fs_info->qgroup_rescan_workers); 19837abadb64SLiu Bo } 19847abadb64SLiu Bo 1985af31f5e5SChris Mason /* helper to cleanup tree roots */ 1986af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 1987af31f5e5SChris Mason { 1988af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 1989af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 1990655b09feSJosef Bacik info->tree_root->node = NULL; 1991655b09feSJosef Bacik info->tree_root->commit_root = NULL; 1992655b09feSJosef Bacik 1993655b09feSJosef Bacik if (info->dev_root) { 1994af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 1995af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 1996655b09feSJosef Bacik info->dev_root->node = NULL; 1997655b09feSJosef Bacik info->dev_root->commit_root = NULL; 1998655b09feSJosef Bacik } 1999655b09feSJosef Bacik if (info->extent_root) { 2000af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 2001af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 2002655b09feSJosef Bacik info->extent_root->node = NULL; 2003655b09feSJosef Bacik info->extent_root->commit_root = NULL; 2004655b09feSJosef Bacik } 2005655b09feSJosef Bacik if (info->csum_root) { 2006af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 2007af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 2008655b09feSJosef Bacik info->csum_root->node = NULL; 2009655b09feSJosef Bacik info->csum_root->commit_root = NULL; 2010655b09feSJosef Bacik } 2011bcef60f2SArne Jansen if (info->quota_root) { 2012bcef60f2SArne Jansen free_extent_buffer(info->quota_root->node); 2013bcef60f2SArne Jansen free_extent_buffer(info->quota_root->commit_root); 2014bcef60f2SArne Jansen info->quota_root->node = NULL; 2015bcef60f2SArne Jansen info->quota_root->commit_root = NULL; 2016bcef60f2SArne Jansen } 2017af31f5e5SChris Mason if (chunk_root) { 2018af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 2019af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 2020af31f5e5SChris Mason info->chunk_root->node = NULL; 2021af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 2022af31f5e5SChris Mason } 2023af31f5e5SChris Mason } 2024af31f5e5SChris Mason 2025171f6537SJosef Bacik static void del_fs_roots(struct btrfs_fs_info *fs_info) 2026171f6537SJosef Bacik { 2027171f6537SJosef Bacik int ret; 2028171f6537SJosef Bacik struct btrfs_root *gang[8]; 2029171f6537SJosef Bacik int i; 2030171f6537SJosef Bacik 2031171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) { 2032171f6537SJosef Bacik gang[0] = list_entry(fs_info->dead_roots.next, 2033171f6537SJosef Bacik struct btrfs_root, root_list); 2034171f6537SJosef Bacik list_del(&gang[0]->root_list); 2035171f6537SJosef Bacik 2036171f6537SJosef Bacik if (gang[0]->in_radix) { 2037171f6537SJosef Bacik btrfs_free_fs_root(fs_info, gang[0]); 2038171f6537SJosef Bacik } else { 2039171f6537SJosef Bacik free_extent_buffer(gang[0]->node); 2040171f6537SJosef Bacik free_extent_buffer(gang[0]->commit_root); 2041171f6537SJosef Bacik kfree(gang[0]); 2042171f6537SJosef Bacik } 2043171f6537SJosef Bacik } 2044171f6537SJosef Bacik 2045171f6537SJosef Bacik while (1) { 2046171f6537SJosef Bacik ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2047171f6537SJosef Bacik (void **)gang, 0, 2048171f6537SJosef Bacik ARRAY_SIZE(gang)); 2049171f6537SJosef Bacik if (!ret) 2050171f6537SJosef Bacik break; 2051171f6537SJosef Bacik for (i = 0; i < ret; i++) 2052171f6537SJosef Bacik btrfs_free_fs_root(fs_info, gang[i]); 2053171f6537SJosef Bacik } 2054171f6537SJosef Bacik } 2055af31f5e5SChris Mason 2056ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 2057dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 2058dfe25020SChris Mason char *options) 2059eb60ceacSChris Mason { 2060db94535dSChris Mason u32 sectorsize; 2061db94535dSChris Mason u32 nodesize; 2062db94535dSChris Mason u32 leafsize; 2063db94535dSChris Mason u32 blocksize; 206487ee04ebSChris Mason u32 stripesize; 206584234f3aSYan Zheng u64 generation; 2066f2b636e8SJosef Bacik u64 features; 20673de4586cSChris Mason struct btrfs_key location; 2068a061fc8dSChris Mason struct buffer_head *bh; 20694d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 2070815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2071f84a8bd6SAl Viro struct btrfs_root *tree_root; 20724d34b278SIlya Dryomov struct btrfs_root *extent_root; 20734d34b278SIlya Dryomov struct btrfs_root *csum_root; 20744d34b278SIlya Dryomov struct btrfs_root *chunk_root; 20754d34b278SIlya Dryomov struct btrfs_root *dev_root; 2076bcef60f2SArne Jansen struct btrfs_root *quota_root; 2077e02119d5SChris Mason struct btrfs_root *log_tree_root; 2078eb60ceacSChris Mason int ret; 2079e58ca020SYan int err = -EINVAL; 2080af31f5e5SChris Mason int num_backups_tried = 0; 2081af31f5e5SChris Mason int backup_index = 0; 20824543df7eSChris Mason 2083f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 20846f07e42eSAl Viro extent_root = fs_info->extent_root = btrfs_alloc_root(fs_info); 20856f07e42eSAl Viro csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); 20866f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 20876f07e42eSAl Viro dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); 2088bcef60f2SArne Jansen quota_root = fs_info->quota_root = btrfs_alloc_root(fs_info); 20898790d502SChris Mason 2090f84a8bd6SAl Viro if (!tree_root || !extent_root || !csum_root || 2091bcef60f2SArne Jansen !chunk_root || !dev_root || !quota_root) { 209239279cc3SChris Mason err = -ENOMEM; 209339279cc3SChris Mason goto fail; 209439279cc3SChris Mason } 209576dda93cSYan, Zheng 209676dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 209776dda93cSYan, Zheng if (ret) { 209876dda93cSYan, Zheng err = ret; 209976dda93cSYan, Zheng goto fail; 210076dda93cSYan, Zheng } 210176dda93cSYan, Zheng 210276dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 210376dda93cSYan, Zheng if (ret) { 210476dda93cSYan, Zheng err = ret; 210576dda93cSYan, Zheng goto fail_srcu; 210676dda93cSYan, Zheng } 210776dda93cSYan, Zheng 2108e2d84521SMiao Xie ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0); 2109e2d84521SMiao Xie if (ret) { 2110e2d84521SMiao Xie err = ret; 2111e2d84521SMiao Xie goto fail_bdi; 2112e2d84521SMiao Xie } 2113e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2114e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2115e2d84521SMiao Xie 2116963d678bSMiao Xie ret = percpu_counter_init(&fs_info->delalloc_bytes, 0); 2117963d678bSMiao Xie if (ret) { 2118963d678bSMiao Xie err = ret; 2119963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2120963d678bSMiao Xie } 2121963d678bSMiao Xie 212276dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 212376dda93cSYan, Zheng if (!fs_info->btree_inode) { 212476dda93cSYan, Zheng err = -ENOMEM; 2125963d678bSMiao Xie goto fail_delalloc_bytes; 212676dda93cSYan, Zheng } 212776dda93cSYan, Zheng 2128a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 21291561dedaSMiao Xie 213076dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 21318fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2132facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 213324bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2134ea8c2819SChris Mason INIT_LIST_HEAD(&fs_info->delalloc_inodes); 213511833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 21361832a6d5SChris Mason spin_lock_init(&fs_info->delalloc_lock); 2137a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 213876dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 213924bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 21404cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 21412bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2142f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2143ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock); 2144f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 21457585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2146de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 214719c00ddcSChris Mason 214858176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 21490b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 21506324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2151f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 21520b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 215366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 215466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 215566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 215666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 215766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 215866d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 215966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 216066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 216166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2162cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2163771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 21648c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 21650986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 21664cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2167fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 2168e20d96d6SChris Mason fs_info->sb = sb; 21696f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 21709ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 21714cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2172a4abeea4SJosef Bacik fs_info->trans_no_join = 0; 21732bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2174f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 2175c8b97818SChris Mason 217690519d66SArne Jansen /* readahead state */ 217790519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 217890519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 21792c90e5d6SChris Mason 2180b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2181b34b086cSChris Mason num_online_cpus() + 2, 8); 21820afbaf8cSChris Mason 21833eaa2885SChris Mason INIT_LIST_HEAD(&fs_info->ordered_extents); 21843eaa2885SChris Mason spin_lock_init(&fs_info->ordered_extent_lock); 218516cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 218616cdcec7SMiao Xie GFP_NOFS); 218716cdcec7SMiao Xie if (!fs_info->delayed_root) { 218816cdcec7SMiao Xie err = -ENOMEM; 218916cdcec7SMiao Xie goto fail_iput; 219016cdcec7SMiao Xie } 219116cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 21923eaa2885SChris Mason 2193a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2194a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2195a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2196a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2197a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2198a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2199a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2200a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 220121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 220221adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 220321adbd5cSStefan Behrens #endif 2204a2de733cSArne Jansen 2205c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2206c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2207837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2208837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2209a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2210c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2211837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2212a061fc8dSChris Mason 22130afbaf8cSChris Mason sb->s_blocksize = 4096; 22140afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 221532a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 22160afbaf8cSChris Mason 221776dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2218bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 22190afbaf8cSChris Mason /* 22200afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 22210afbaf8cSChris Mason * the real end of the address space is determined by all of 22220afbaf8cSChris Mason * the devices in the system 22230afbaf8cSChris Mason */ 22240afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2225d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 222604160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 222704160088SChris Mason 22285d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2229d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2230f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 22310b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2232a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 22330da5468fSChris Mason 2234d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2235d1310b2eSChris Mason 22360f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 22370f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 22380f7d52f4SChris Mason sizeof(struct btrfs_key)); 223972ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 224072ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 224122b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 224239279cc3SChris Mason 2243e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 22446bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2245a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2246925baeddSChris Mason 224711833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2248f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 224911833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2250f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 225111833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2252e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2253f9295749SChris Mason 2254d98237b3SChris Mason 22555a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 225630ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2257925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2258925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2259a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2260a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2261276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2262c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 226376dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2264e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2265e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2266e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2267e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2268e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2269fa9c0d79SChris Mason 2270416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2271f2f6ed3dSWang Shilong mutex_init(&fs_info->qgroup_ioctl_lock); 2272416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2273416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2274416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2275416ac51dSArne Jansen fs_info->quota_enabled = 0; 2276416ac51dSArne Jansen fs_info->pending_quota_state = 0; 22772f232036SJan Schmidt mutex_init(&fs_info->qgroup_rescan_lock); 2278416ac51dSArne Jansen 2279fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2280fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2281fa9c0d79SChris Mason 22827d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 22833768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2284bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 22854854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 22869a8dd150SChris Mason 228753b381b3SDavid Woodhouse ret = btrfs_alloc_stripe_hash_table(fs_info); 228853b381b3SDavid Woodhouse if (ret) { 228983c8266aSDavid Sterba err = ret; 229053b381b3SDavid Woodhouse goto fail_alloc; 229153b381b3SDavid Woodhouse } 229253b381b3SDavid Woodhouse 22930b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 22942c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 22957eccb903SChris Mason 22963c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 22971104a885SDavid Sterba 22981104a885SDavid Sterba /* 22991104a885SDavid Sterba * Read super block and check the signature bytes only 23001104a885SDavid Sterba */ 2301a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 230220b45077SDave Young if (!bh) { 230320b45077SDave Young err = -EINVAL; 230416cdcec7SMiao Xie goto fail_alloc; 230520b45077SDave Young } 230639279cc3SChris Mason 23071104a885SDavid Sterba /* 23081104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside. 23091104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k). 23101104a885SDavid Sterba */ 23111104a885SDavid Sterba if (btrfs_check_super_csum(bh->b_data)) { 23121104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock checksum mismatch\n"); 23131104a885SDavid Sterba err = -EINVAL; 23141104a885SDavid Sterba goto fail_alloc; 23151104a885SDavid Sterba } 23161104a885SDavid Sterba 23171104a885SDavid Sterba /* 23181104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the 23191104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from 23201104a885SDavid Sterba * the whole block of INFO_SIZE 23211104a885SDavid Sterba */ 23226c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 23236c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 23246c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2325a061fc8dSChris Mason brelse(bh); 23265f39d397SChris Mason 23276c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 23280b86a832SChris Mason 23291104a885SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 23301104a885SDavid Sterba if (ret) { 23311104a885SDavid Sterba printk(KERN_ERR "btrfs: superblock contains fatal errors\n"); 23321104a885SDavid Sterba err = -EINVAL; 23331104a885SDavid Sterba goto fail_alloc; 23341104a885SDavid Sterba } 23351104a885SDavid Sterba 23366c41761fSDavid Sterba disk_super = fs_info->super_copy; 23370f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 233816cdcec7SMiao Xie goto fail_alloc; 23390f7d52f4SChris Mason 2340acce952bSliubo /* check FS state, whether FS is broken. */ 234187533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 234287533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2343acce952bSliubo 234475e7cb7fSLiu Bo /* 2345af31f5e5SChris Mason * run through our array of backup supers and setup 2346af31f5e5SChris Mason * our ring pointer to the oldest one 2347af31f5e5SChris Mason */ 2348af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2349af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2350af31f5e5SChris Mason 2351af31f5e5SChris Mason /* 235275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 235375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 235475e7cb7fSLiu Bo */ 235575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 235675e7cb7fSLiu Bo 23572b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 23582b82032cSYan Zheng if (ret) { 23592b82032cSYan Zheng err = ret; 236016cdcec7SMiao Xie goto fail_alloc; 23612b82032cSYan Zheng } 2362dfe25020SChris Mason 2363f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2364f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2365f2b636e8SJosef Bacik if (features) { 2366f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2367f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 236821380931SJoel Becker (unsigned long long)features); 2369f2b636e8SJosef Bacik err = -EINVAL; 237016cdcec7SMiao Xie goto fail_alloc; 2371f2b636e8SJosef Bacik } 2372f2b636e8SJosef Bacik 2373727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2374727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2375727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2376727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2377727011e0SChris Mason btrfs_super_nodesize(disk_super), 2378727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2379727011e0SChris Mason err = -EINVAL; 2380727011e0SChris Mason goto fail_alloc; 2381727011e0SChris Mason } 2382727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2383727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2384727011e0SChris Mason "blocksize (%d) was too large\n", 2385727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2386727011e0SChris Mason err = -EINVAL; 2387727011e0SChris Mason goto fail_alloc; 2388727011e0SChris Mason } 2389727011e0SChris Mason 23905d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 23915d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 239269e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2393a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2394727011e0SChris Mason 23953173a18fSJosef Bacik if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 23963173a18fSJosef Bacik printk(KERN_ERR "btrfs: has skinny extents\n"); 23973173a18fSJosef Bacik 2398727011e0SChris Mason /* 2399727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2400727011e0SChris Mason * they are bigger than the page size 2401727011e0SChris Mason */ 2402727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2403727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2404727011e0SChris Mason printk(KERN_INFO "btrfs flagging fs with big metadata feature\n"); 2405727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2406727011e0SChris Mason } 2407727011e0SChris Mason 2408bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2409bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2410bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2411bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2412e2d84521SMiao Xie fs_info->dirty_metadata_batch = leafsize * (1 + ilog2(nr_cpu_ids)); 2413963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2414bc3f116fSChris Mason 2415bc3f116fSChris Mason /* 2416bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2417bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2418bc3f116fSChris Mason */ 2419bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2420bc3f116fSChris Mason (sectorsize != leafsize)) { 2421bc3f116fSChris Mason printk(KERN_WARNING "btrfs: unequal leaf/node/sector sizes " 2422bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2423bc3f116fSChris Mason sb->s_id); 2424bc3f116fSChris Mason goto fail_alloc; 2425bc3f116fSChris Mason } 2426bc3f116fSChris Mason 2427ceda0864SMiao Xie /* 2428ceda0864SMiao Xie * Needn't use the lock because there is no other task which will 2429ceda0864SMiao Xie * update the flag. 2430ceda0864SMiao Xie */ 24315d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 24325d4f98a2SYan Zheng 2433f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2434f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2435f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2436f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2437f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 243821380931SJoel Becker (unsigned long long)features); 2439f2b636e8SJosef Bacik err = -EINVAL; 244016cdcec7SMiao Xie goto fail_alloc; 2441f2b636e8SJosef Bacik } 244261d92c32SChris Mason 244361d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 244461d92c32SChris Mason "genwork", 1, NULL); 244561d92c32SChris Mason 24465443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 244761d92c32SChris Mason fs_info->thread_pool_size, 244861d92c32SChris Mason &fs_info->generic_worker); 2449c8b97818SChris Mason 2450771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 245161d92c32SChris Mason fs_info->thread_pool_size, 245261d92c32SChris Mason &fs_info->generic_worker); 2453771ed689SChris Mason 24548ccf6f19SMiao Xie btrfs_init_workers(&fs_info->flush_workers, "flush_delalloc", 24558ccf6f19SMiao Xie fs_info->thread_pool_size, 24568ccf6f19SMiao Xie &fs_info->generic_worker); 24578ccf6f19SMiao Xie 24585443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2459b720d209SChris Mason min_t(u64, fs_devices->num_devices, 246061d92c32SChris Mason fs_info->thread_pool_size), 246161d92c32SChris Mason &fs_info->generic_worker); 246261b49440SChris Mason 2463bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 2464bab39bf9SJosef Bacik 2, &fs_info->generic_worker); 2465bab39bf9SJosef Bacik 246661b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 246761b49440SChris Mason * likely that bios will be send down in a sane order to the 246861b49440SChris Mason * devices 246961b49440SChris Mason */ 247061b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 247153863232SChris Mason 2472771ed689SChris Mason fs_info->workers.idle_thresh = 16; 24734a69a410SChris Mason fs_info->workers.ordered = 1; 247461b49440SChris Mason 2475771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2476771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2477771ed689SChris Mason 247861d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 247961d92c32SChris Mason &fs_info->generic_worker); 24805443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 248161d92c32SChris Mason fs_info->thread_pool_size, 248261d92c32SChris Mason &fs_info->generic_worker); 2483d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 248461d92c32SChris Mason fs_info->thread_pool_size, 248561d92c32SChris Mason &fs_info->generic_worker); 2486cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 248761d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 248861d92c32SChris Mason &fs_info->generic_worker); 248953b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->endio_raid56_workers, 249053b381b3SDavid Woodhouse "endio-raid56", fs_info->thread_pool_size, 249153b381b3SDavid Woodhouse &fs_info->generic_worker); 249253b381b3SDavid Woodhouse btrfs_init_workers(&fs_info->rmw_workers, 249353b381b3SDavid Woodhouse "rmw", fs_info->thread_pool_size, 249453b381b3SDavid Woodhouse &fs_info->generic_worker); 24955443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 249661d92c32SChris Mason fs_info->thread_pool_size, 249761d92c32SChris Mason &fs_info->generic_worker); 24980cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 24990cb59c99SJosef Bacik 1, &fs_info->generic_worker); 250016cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 250116cdcec7SMiao Xie fs_info->thread_pool_size, 250216cdcec7SMiao Xie &fs_info->generic_worker); 250390519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 250490519d66SArne Jansen fs_info->thread_pool_size, 250590519d66SArne Jansen &fs_info->generic_worker); 25062f232036SJan Schmidt btrfs_init_workers(&fs_info->qgroup_rescan_workers, "qgroup-rescan", 1, 25072f232036SJan Schmidt &fs_info->generic_worker); 250861b49440SChris Mason 250961b49440SChris Mason /* 251061b49440SChris Mason * endios are largely parallel and should have a very 251161b49440SChris Mason * low idle thresh 251261b49440SChris Mason */ 251361b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2514b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 251553b381b3SDavid Woodhouse fs_info->endio_raid56_workers.idle_thresh = 4; 251653b381b3SDavid Woodhouse fs_info->rmw_workers.idle_thresh = 2; 2517b51912c9SChris Mason 25189042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 25199042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 252090519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 25219042846bSChris Mason 25220dc3b84aSJosef Bacik /* 25230dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 25240dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 25250dc3b84aSJosef Bacik */ 25260dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 25270dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 25280dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 25290dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 25300dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 25310dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 25320dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 253353b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->rmw_workers); 253453b381b3SDavid Woodhouse ret |= btrfs_start_workers(&fs_info->endio_raid56_workers); 25350dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 25360dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 25370dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 25380dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 25390dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 25400dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 25418ccf6f19SMiao Xie ret |= btrfs_start_workers(&fs_info->flush_workers); 25422f232036SJan Schmidt ret |= btrfs_start_workers(&fs_info->qgroup_rescan_workers); 25430dc3b84aSJosef Bacik if (ret) { 2544fed425c7SIlya Dryomov err = -ENOMEM; 25450dc3b84aSJosef Bacik goto fail_sb_buffer; 25460dc3b84aSJosef Bacik } 25474543df7eSChris Mason 25484575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2549c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2550c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 25514575c9ccSChris Mason 2552db94535dSChris Mason tree_root->nodesize = nodesize; 2553db94535dSChris Mason tree_root->leafsize = leafsize; 2554db94535dSChris Mason tree_root->sectorsize = sectorsize; 255587ee04ebSChris Mason tree_root->stripesize = stripesize; 2556a061fc8dSChris Mason 2557a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2558a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2559db94535dSChris Mason 2560cdb4c574SZach Brown if (disk_super->magic != cpu_to_le64(BTRFS_MAGIC)) { 2561d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 256239279cc3SChris Mason goto fail_sb_buffer; 256339279cc3SChris Mason } 256419c00ddcSChris Mason 25658d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 25668d082fb7SLiu Bo printk(KERN_WARNING "btrfs: Incompatible sector size(%lu) " 25678d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2568941b2ddfSKeith Mannthey goto fail_sb_buffer; 2569941b2ddfSKeith Mannthey } 2570941b2ddfSKeith Mannthey 2571925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2572e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2573925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 257484eed90fSChris Mason if (ret) { 2575d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2576d397712bSChris Mason "array on %s\n", sb->s_id); 25775d4f98a2SYan Zheng goto fail_sb_buffer; 257884eed90fSChris Mason } 25790b86a832SChris Mason 25800b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 25810b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 258284234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 25830b86a832SChris Mason 25840b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 25850b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 25860b86a832SChris Mason 25870b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 25880b86a832SChris Mason btrfs_super_chunk_root(disk_super), 258984234f3aSYan Zheng blocksize, generation); 2590416bc658SJosef Bacik if (!chunk_root->node || 2591416bc658SJosef Bacik !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 259283121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 259383121942SDavid Woodhouse sb->s_id); 2594af31f5e5SChris Mason goto fail_tree_roots; 259583121942SDavid Woodhouse } 25965d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 25975d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 25980b86a832SChris Mason 2599e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2600e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 2601e17cade2SChris Mason BTRFS_UUID_SIZE); 2602e17cade2SChris Mason 26030b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 26042b82032cSYan Zheng if (ret) { 2605d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2606d397712bSChris Mason sb->s_id); 2607af31f5e5SChris Mason goto fail_tree_roots; 26082b82032cSYan Zheng } 26090b86a832SChris Mason 26108dabb742SStefan Behrens /* 26118dabb742SStefan Behrens * keep the device that is marked to be the target device for the 26128dabb742SStefan Behrens * dev_replace procedure 26138dabb742SStefan Behrens */ 26148dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2615dfe25020SChris Mason 2616a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2617a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2618a6b0d5c8SChris Mason sb->s_id); 2619a6b0d5c8SChris Mason goto fail_tree_roots; 2620a6b0d5c8SChris Mason } 2621a6b0d5c8SChris Mason 2622af31f5e5SChris Mason retry_root_backup: 2623db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2624db94535dSChris Mason btrfs_super_root_level(disk_super)); 262584234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 26260b86a832SChris Mason 2627e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2628db94535dSChris Mason btrfs_super_root(disk_super), 262984234f3aSYan Zheng blocksize, generation); 2630af31f5e5SChris Mason if (!tree_root->node || 2631af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 263283121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 263383121942SDavid Woodhouse sb->s_id); 2634af31f5e5SChris Mason 2635af31f5e5SChris Mason goto recovery_tree_root; 263683121942SDavid Woodhouse } 2637af31f5e5SChris Mason 26385d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 26395d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 2640db94535dSChris Mason 2641db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 2642e20d96d6SChris Mason BTRFS_EXTENT_TREE_OBJECTID, extent_root); 26430b86a832SChris Mason if (ret) 2644af31f5e5SChris Mason goto recovery_tree_root; 26450b86a832SChris Mason extent_root->track_dirty = 1; 26460b86a832SChris Mason 26470b86a832SChris Mason ret = find_and_setup_root(tree_root, fs_info, 26480b86a832SChris Mason BTRFS_DEV_TREE_OBJECTID, dev_root); 26490b86a832SChris Mason if (ret) 2650af31f5e5SChris Mason goto recovery_tree_root; 26515d4f98a2SYan Zheng dev_root->track_dirty = 1; 26523768f368SChris Mason 2653d20f7043SChris Mason ret = find_and_setup_root(tree_root, fs_info, 2654d20f7043SChris Mason BTRFS_CSUM_TREE_OBJECTID, csum_root); 2655d20f7043SChris Mason if (ret) 2656af31f5e5SChris Mason goto recovery_tree_root; 2657d20f7043SChris Mason csum_root->track_dirty = 1; 2658d20f7043SChris Mason 2659bcef60f2SArne Jansen ret = find_and_setup_root(tree_root, fs_info, 2660bcef60f2SArne Jansen BTRFS_QUOTA_TREE_OBJECTID, quota_root); 2661bcef60f2SArne Jansen if (ret) { 2662bcef60f2SArne Jansen kfree(quota_root); 2663bcef60f2SArne Jansen quota_root = fs_info->quota_root = NULL; 2664bcef60f2SArne Jansen } else { 2665bcef60f2SArne Jansen quota_root->track_dirty = 1; 2666bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2667bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2668bcef60f2SArne Jansen } 2669bcef60f2SArne Jansen 26708929ecfaSYan, Zheng fs_info->generation = generation; 26718929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 26728929ecfaSYan, Zheng 267368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 267468310a5eSIlya Dryomov if (ret) { 267568310a5eSIlya Dryomov printk(KERN_WARNING "btrfs: failed to recover balance\n"); 267668310a5eSIlya Dryomov goto fail_block_groups; 267768310a5eSIlya Dryomov } 267868310a5eSIlya Dryomov 2679733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2680733f4fbbSStefan Behrens if (ret) { 2681733f4fbbSStefan Behrens printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2682733f4fbbSStefan Behrens ret); 2683733f4fbbSStefan Behrens goto fail_block_groups; 2684733f4fbbSStefan Behrens } 2685733f4fbbSStefan Behrens 26868dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 26878dabb742SStefan Behrens if (ret) { 26888dabb742SStefan Behrens pr_err("btrfs: failed to init dev_replace: %d\n", ret); 26898dabb742SStefan Behrens goto fail_block_groups; 26908dabb742SStefan Behrens } 26918dabb742SStefan Behrens 26928dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 26938dabb742SStefan Behrens 2694c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2695c59021f8Sliubo if (ret) { 2696c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2697c59021f8Sliubo goto fail_block_groups; 2698c59021f8Sliubo } 2699c59021f8Sliubo 27001b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 27011b1d1f66SJosef Bacik if (ret) { 27021b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 27031b1d1f66SJosef Bacik goto fail_block_groups; 27041b1d1f66SJosef Bacik } 27055af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 27065af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2707292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2708292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2709292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2710292fd7fcSStefan Behrens printk(KERN_WARNING 2711292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable mount is not allowed\n"); 2712292fd7fcSStefan Behrens goto fail_block_groups; 2713292fd7fcSStefan Behrens } 27149078a3e1SChris Mason 2715a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2716a74a4b97SChris Mason "btrfs-cleaner"); 271757506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 27181b1d1f66SJosef Bacik goto fail_block_groups; 2719a74a4b97SChris Mason 2720a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2721a74a4b97SChris Mason tree_root, 2722a74a4b97SChris Mason "btrfs-transaction"); 272357506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 27243f157a2fSChris Mason goto fail_cleaner; 2725a74a4b97SChris Mason 2726c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2727c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2728c289811cSChris Mason !fs_info->fs_devices->rotating) { 2729c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2730c289811cSChris Mason "mode\n"); 2731c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2732c289811cSChris Mason } 2733c289811cSChris Mason 273421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 273521adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 273621adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 273721adbd5cSStefan Behrens btrfs_test_opt(tree_root, 273821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 273921adbd5cSStefan Behrens 1 : 0, 274021adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 274121adbd5cSStefan Behrens if (ret) 274221adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 274321adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 274421adbd5cSStefan Behrens } 274521adbd5cSStefan Behrens #endif 2746bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2747bcef60f2SArne Jansen if (ret) 2748bcef60f2SArne Jansen goto fail_trans_kthread; 274921adbd5cSStefan Behrens 2750acce952bSliubo /* do not make disk changes in broken FS */ 275168ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2752e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2753d18a2c44SChris Mason 27547c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2755d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2756d397712bSChris Mason "on RO media\n"); 27577c2ca468SChris Mason err = -EIO; 2758bcef60f2SArne Jansen goto fail_qgroup; 27597c2ca468SChris Mason } 2760e02119d5SChris Mason blocksize = 2761e02119d5SChris Mason btrfs_level_size(tree_root, 2762e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2763e02119d5SChris Mason 27646f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2765676e4c86SDan Carpenter if (!log_tree_root) { 2766676e4c86SDan Carpenter err = -ENOMEM; 2767bcef60f2SArne Jansen goto fail_qgroup; 2768676e4c86SDan Carpenter } 2769e02119d5SChris Mason 2770e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2771e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2772e02119d5SChris Mason 2773e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 277484234f3aSYan Zheng blocksize, 277584234f3aSYan Zheng generation + 1); 2776416bc658SJosef Bacik if (!log_tree_root->node || 2777416bc658SJosef Bacik !extent_buffer_uptodate(log_tree_root->node)) { 2778416bc658SJosef Bacik printk(KERN_ERR "btrfs: failed to read log tree\n"); 2779416bc658SJosef Bacik free_extent_buffer(log_tree_root->node); 2780416bc658SJosef Bacik kfree(log_tree_root); 2781416bc658SJosef Bacik goto fail_trans_kthread; 2782416bc658SJosef Bacik } 278379787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2784e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 278579787eaaSJeff Mahoney if (ret) { 278679787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 278779787eaaSJeff Mahoney "Failed to recover log tree"); 278879787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 278979787eaaSJeff Mahoney kfree(log_tree_root); 279079787eaaSJeff Mahoney goto fail_trans_kthread; 279179787eaaSJeff Mahoney } 2792e556ce2cSYan Zheng 2793e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2794e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 279579787eaaSJeff Mahoney if (ret) 279679787eaaSJeff Mahoney goto fail_trans_kthread; 2797e556ce2cSYan Zheng } 2798e02119d5SChris Mason } 27991a40e23bSZheng Yan 280076dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 280179787eaaSJeff Mahoney if (ret) 280279787eaaSJeff Mahoney goto fail_trans_kthread; 280376dda93cSYan, Zheng 28047c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2805d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 280644c44af2SIlya Dryomov if (ret) 280744c44af2SIlya Dryomov goto fail_trans_kthread; 2808d68fc57bSYan, Zheng 28095d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2810d7ce5843SMiao Xie if (ret < 0) { 2811d7ce5843SMiao Xie printk(KERN_WARNING 2812d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2813d7ce5843SMiao Xie err = -EINVAL; 2814bcef60f2SArne Jansen goto fail_qgroup; 2815d7ce5843SMiao Xie } 28167c2ca468SChris Mason } 28171a40e23bSZheng Yan 28183de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 28193de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 28203de4586cSChris Mason location.offset = (u64)-1; 28213de4586cSChris Mason 28223de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 28233de4586cSChris Mason if (!fs_info->fs_root) 2824bcef60f2SArne Jansen goto fail_qgroup; 28253140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 28263140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2827bcef60f2SArne Jansen goto fail_qgroup; 28283140c9a3SDan Carpenter } 2829c289811cSChris Mason 28302b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 28312b6ba629SIlya Dryomov return 0; 28322b6ba629SIlya Dryomov 2833e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 28342b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 28352b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 28362b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 28372b6ba629SIlya Dryomov close_ctree(tree_root); 28382b6ba629SIlya Dryomov return ret; 28392b6ba629SIlya Dryomov } 2840e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 284159641015SIlya Dryomov 28422b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 28432b6ba629SIlya Dryomov if (ret) { 28442b6ba629SIlya Dryomov printk(KERN_WARNING "btrfs: failed to resume balance\n"); 284566b4ffd1SJosef Bacik close_ctree(tree_root); 28462b6ba629SIlya Dryomov return ret; 2847e3acc2a6SJosef Bacik } 2848e3acc2a6SJosef Bacik 28498dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 28508dabb742SStefan Behrens if (ret) { 28518dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 28528dabb742SStefan Behrens close_ctree(tree_root); 28538dabb742SStefan Behrens return ret; 28548dabb742SStefan Behrens } 28558dabb742SStefan Behrens 2856ad2b2c80SAl Viro return 0; 285739279cc3SChris Mason 2858bcef60f2SArne Jansen fail_qgroup: 2859bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 28607c2ca468SChris Mason fail_trans_kthread: 28617c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 286254067ae9SJosef Bacik btrfs_cleanup_transaction(fs_info->tree_root); 28637b5ff90eSJosef Bacik del_fs_roots(fs_info); 28643f157a2fSChris Mason fail_cleaner: 2865a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 28667c2ca468SChris Mason 28677c2ca468SChris Mason /* 28687c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 28697c2ca468SChris Mason * kthreads 28707c2ca468SChris Mason */ 28717c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 28727c2ca468SChris Mason 28731b1d1f66SJosef Bacik fail_block_groups: 287454067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info); 28751b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2876af31f5e5SChris Mason 2877af31f5e5SChris Mason fail_tree_roots: 2878af31f5e5SChris Mason free_root_pointers(fs_info, 1); 28792b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2880af31f5e5SChris Mason 288139279cc3SChris Mason fail_sb_buffer: 28827abadb64SLiu Bo btrfs_stop_all_workers(fs_info); 288316cdcec7SMiao Xie fail_alloc: 28844543df7eSChris Mason fail_iput: 2885586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2886586e46e2SIlya Dryomov 28874543df7eSChris Mason iput(fs_info->btree_inode); 2888963d678bSMiao Xie fail_delalloc_bytes: 2889963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 2890e2d84521SMiao Xie fail_dirty_metadata_bytes: 2891e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 2892ad081f14SJens Axboe fail_bdi: 28937e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 289476dda93cSYan, Zheng fail_srcu: 289576dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 28967e662854SQinghuang Feng fail: 289753b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 2898586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2899ad2b2c80SAl Viro return err; 2900af31f5e5SChris Mason 2901af31f5e5SChris Mason recovery_tree_root: 2902af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2903af31f5e5SChris Mason goto fail_tree_roots; 2904af31f5e5SChris Mason 2905af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2906af31f5e5SChris Mason 2907af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2908af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2909af31f5e5SChris Mason 2910af31f5e5SChris Mason /* we can't trust the free space cache either */ 2911af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2912af31f5e5SChris Mason 2913af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2914af31f5e5SChris Mason &num_backups_tried, &backup_index); 2915af31f5e5SChris Mason if (ret == -1) 2916af31f5e5SChris Mason goto fail_block_groups; 2917af31f5e5SChris Mason goto retry_root_backup; 2918eb60ceacSChris Mason } 2919eb60ceacSChris Mason 2920f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2921f2984462SChris Mason { 2922f2984462SChris Mason if (uptodate) { 2923f2984462SChris Mason set_buffer_uptodate(bh); 2924f2984462SChris Mason } else { 2925442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 2926442a4f63SStefan Behrens bh->b_private; 2927442a4f63SStefan Behrens 2928606686eeSJosef Bacik printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to " 2929606686eeSJosef Bacik "I/O error on %s\n", 2930606686eeSJosef Bacik rcu_str_deref(device->name)); 29311259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 29321259ab75SChris Mason * our own ways of dealing with the IO errors 29331259ab75SChris Mason */ 2934f2984462SChris Mason clear_buffer_uptodate(bh); 2935442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 2936f2984462SChris Mason } 2937f2984462SChris Mason unlock_buffer(bh); 2938f2984462SChris Mason put_bh(bh); 2939f2984462SChris Mason } 2940f2984462SChris Mason 2941a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 2942a512bbf8SYan Zheng { 2943a512bbf8SYan Zheng struct buffer_head *bh; 2944a512bbf8SYan Zheng struct buffer_head *latest = NULL; 2945a512bbf8SYan Zheng struct btrfs_super_block *super; 2946a512bbf8SYan Zheng int i; 2947a512bbf8SYan Zheng u64 transid = 0; 2948a512bbf8SYan Zheng u64 bytenr; 2949a512bbf8SYan Zheng 2950a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 2951a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 2952a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 2953a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 2954a512bbf8SYan Zheng */ 2955a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 2956a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2957a512bbf8SYan Zheng if (bytenr + 4096 >= i_size_read(bdev->bd_inode)) 2958a512bbf8SYan Zheng break; 2959a512bbf8SYan Zheng bh = __bread(bdev, bytenr / 4096, 4096); 2960a512bbf8SYan Zheng if (!bh) 2961a512bbf8SYan Zheng continue; 2962a512bbf8SYan Zheng 2963a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 2964a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 2965cdb4c574SZach Brown super->magic != cpu_to_le64(BTRFS_MAGIC)) { 2966a512bbf8SYan Zheng brelse(bh); 2967a512bbf8SYan Zheng continue; 2968a512bbf8SYan Zheng } 2969a512bbf8SYan Zheng 2970a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 2971a512bbf8SYan Zheng brelse(latest); 2972a512bbf8SYan Zheng latest = bh; 2973a512bbf8SYan Zheng transid = btrfs_super_generation(super); 2974a512bbf8SYan Zheng } else { 2975a512bbf8SYan Zheng brelse(bh); 2976a512bbf8SYan Zheng } 2977a512bbf8SYan Zheng } 2978a512bbf8SYan Zheng return latest; 2979a512bbf8SYan Zheng } 2980a512bbf8SYan Zheng 29814eedeb75SHisashi Hifumi /* 29824eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 29834eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 29844eedeb75SHisashi Hifumi * we write are pinned. 29854eedeb75SHisashi Hifumi * 29864eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 29874eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 29884eedeb75SHisashi Hifumi * many supers on this one device should be written. 29894eedeb75SHisashi Hifumi * 29904eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 29914eedeb75SHisashi Hifumi */ 2992a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 2993a512bbf8SYan Zheng struct btrfs_super_block *sb, 2994a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 2995a512bbf8SYan Zheng { 2996a512bbf8SYan Zheng struct buffer_head *bh; 2997a512bbf8SYan Zheng int i; 2998a512bbf8SYan Zheng int ret; 2999a512bbf8SYan Zheng int errors = 0; 3000a512bbf8SYan Zheng u32 crc; 3001a512bbf8SYan Zheng u64 bytenr; 3002a512bbf8SYan Zheng 3003a512bbf8SYan Zheng if (max_mirrors == 0) 3004a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 3005a512bbf8SYan Zheng 3006a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 3007a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 3008a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 3009a512bbf8SYan Zheng break; 3010a512bbf8SYan Zheng 3011a512bbf8SYan Zheng if (wait) { 3012a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 3013a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3014634554dcSJosef Bacik if (!bh) { 3015634554dcSJosef Bacik errors++; 3016634554dcSJosef Bacik continue; 3017634554dcSJosef Bacik } 3018a512bbf8SYan Zheng wait_on_buffer(bh); 30194eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 30204eedeb75SHisashi Hifumi errors++; 30214eedeb75SHisashi Hifumi 30224eedeb75SHisashi Hifumi /* drop our reference */ 30234eedeb75SHisashi Hifumi brelse(bh); 30244eedeb75SHisashi Hifumi 30254eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 3026a512bbf8SYan Zheng brelse(bh); 3027a512bbf8SYan Zheng continue; 3028a512bbf8SYan Zheng } else { 3029a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 3030a512bbf8SYan Zheng 3031a512bbf8SYan Zheng crc = ~(u32)0; 3032b0496686SLiu Bo crc = btrfs_csum_data((char *)sb + 3033a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 3034a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 3035a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 3036a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 3037a512bbf8SYan Zheng 30384eedeb75SHisashi Hifumi /* 30394eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 30404eedeb75SHisashi Hifumi * caller 30414eedeb75SHisashi Hifumi */ 3042a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 3043a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 3044634554dcSJosef Bacik if (!bh) { 3045634554dcSJosef Bacik printk(KERN_ERR "btrfs: couldn't get super " 3046634554dcSJosef Bacik "buffer head for bytenr %Lu\n", bytenr); 3047634554dcSJosef Bacik errors++; 3048634554dcSJosef Bacik continue; 3049634554dcSJosef Bacik } 3050634554dcSJosef Bacik 3051a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 3052a512bbf8SYan Zheng 30534eedeb75SHisashi Hifumi /* one reference for submit_bh */ 3054a512bbf8SYan Zheng get_bh(bh); 30554eedeb75SHisashi Hifumi 30564eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 3057a512bbf8SYan Zheng lock_buffer(bh); 3058a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 3059442a4f63SStefan Behrens bh->b_private = device; 3060a512bbf8SYan Zheng } 3061a512bbf8SYan Zheng 3062387125fcSChris Mason /* 3063387125fcSChris Mason * we fua the first super. The others we allow 3064387125fcSChris Mason * to go down lazy. 3065387125fcSChris Mason */ 306621adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 30674eedeb75SHisashi Hifumi if (ret) 3068a512bbf8SYan Zheng errors++; 3069a512bbf8SYan Zheng } 3070a512bbf8SYan Zheng return errors < i ? 0 : -1; 3071a512bbf8SYan Zheng } 3072a512bbf8SYan Zheng 3073387125fcSChris Mason /* 3074387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 3075387125fcSChris Mason * for the barrier when it is done 3076387125fcSChris Mason */ 3077387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 3078387125fcSChris Mason { 3079387125fcSChris Mason if (err) { 3080387125fcSChris Mason if (err == -EOPNOTSUPP) 3081387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 3082387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 3083387125fcSChris Mason } 3084387125fcSChris Mason if (bio->bi_private) 3085387125fcSChris Mason complete(bio->bi_private); 3086387125fcSChris Mason bio_put(bio); 3087387125fcSChris Mason } 3088387125fcSChris Mason 3089387125fcSChris Mason /* 3090387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 3091387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 3092387125fcSChris Mason * 3093387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 3094387125fcSChris Mason * capable 3095387125fcSChris Mason */ 3096387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 3097387125fcSChris Mason { 3098387125fcSChris Mason struct bio *bio; 3099387125fcSChris Mason int ret = 0; 3100387125fcSChris Mason 3101387125fcSChris Mason if (device->nobarriers) 3102387125fcSChris Mason return 0; 3103387125fcSChris Mason 3104387125fcSChris Mason if (wait) { 3105387125fcSChris Mason bio = device->flush_bio; 3106387125fcSChris Mason if (!bio) 3107387125fcSChris Mason return 0; 3108387125fcSChris Mason 3109387125fcSChris Mason wait_for_completion(&device->flush_wait); 3110387125fcSChris Mason 3111387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 3112606686eeSJosef Bacik printk_in_rcu("btrfs: disabling barriers on dev %s\n", 3113606686eeSJosef Bacik rcu_str_deref(device->name)); 3114387125fcSChris Mason device->nobarriers = 1; 31155af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 3116387125fcSChris Mason ret = -EIO; 3117442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 3118442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 3119387125fcSChris Mason } 3120387125fcSChris Mason 3121387125fcSChris Mason /* drop the reference from the wait == 0 run */ 3122387125fcSChris Mason bio_put(bio); 3123387125fcSChris Mason device->flush_bio = NULL; 3124387125fcSChris Mason 3125387125fcSChris Mason return ret; 3126387125fcSChris Mason } 3127387125fcSChris Mason 3128387125fcSChris Mason /* 3129387125fcSChris Mason * one reference for us, and we leave it for the 3130387125fcSChris Mason * caller 3131387125fcSChris Mason */ 31329c017abcSJesper Juhl device->flush_bio = NULL; 31339be3395bSChris Mason bio = btrfs_io_bio_alloc(GFP_NOFS, 0); 3134387125fcSChris Mason if (!bio) 3135387125fcSChris Mason return -ENOMEM; 3136387125fcSChris Mason 3137387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 3138387125fcSChris Mason bio->bi_bdev = device->bdev; 3139387125fcSChris Mason init_completion(&device->flush_wait); 3140387125fcSChris Mason bio->bi_private = &device->flush_wait; 3141387125fcSChris Mason device->flush_bio = bio; 3142387125fcSChris Mason 3143387125fcSChris Mason bio_get(bio); 314421adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 3145387125fcSChris Mason 3146387125fcSChris Mason return 0; 3147387125fcSChris Mason } 3148387125fcSChris Mason 3149387125fcSChris Mason /* 3150387125fcSChris Mason * send an empty flush down to each device in parallel, 3151387125fcSChris Mason * then wait for them 3152387125fcSChris Mason */ 3153387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 3154387125fcSChris Mason { 3155387125fcSChris Mason struct list_head *head; 3156387125fcSChris Mason struct btrfs_device *dev; 31575af3e8ccSStefan Behrens int errors_send = 0; 31585af3e8ccSStefan Behrens int errors_wait = 0; 3159387125fcSChris Mason int ret; 3160387125fcSChris Mason 3161387125fcSChris Mason /* send down all the barriers */ 3162387125fcSChris Mason head = &info->fs_devices->devices; 3163387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3164387125fcSChris Mason if (!dev->bdev) { 31655af3e8ccSStefan Behrens errors_send++; 3166387125fcSChris Mason continue; 3167387125fcSChris Mason } 3168387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3169387125fcSChris Mason continue; 3170387125fcSChris Mason 3171387125fcSChris Mason ret = write_dev_flush(dev, 0); 3172387125fcSChris Mason if (ret) 31735af3e8ccSStefan Behrens errors_send++; 3174387125fcSChris Mason } 3175387125fcSChris Mason 3176387125fcSChris Mason /* wait for all the barriers */ 3177387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3178387125fcSChris Mason if (!dev->bdev) { 31795af3e8ccSStefan Behrens errors_wait++; 3180387125fcSChris Mason continue; 3181387125fcSChris Mason } 3182387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3183387125fcSChris Mason continue; 3184387125fcSChris Mason 3185387125fcSChris Mason ret = write_dev_flush(dev, 1); 3186387125fcSChris Mason if (ret) 31875af3e8ccSStefan Behrens errors_wait++; 3188387125fcSChris Mason } 31895af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 31905af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3191387125fcSChris Mason return -EIO; 3192387125fcSChris Mason return 0; 3193387125fcSChris Mason } 3194387125fcSChris Mason 31955af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 31965af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 31975af3e8ccSStefan Behrens { 31985af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 31995af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 32005af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 32015af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 32025af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 32035af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 32045af3e8ccSStefan Behrens int num_types = 4; 32055af3e8ccSStefan Behrens int i; 32065af3e8ccSStefan Behrens int c; 32075af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 32085af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 32095af3e8ccSStefan Behrens 32105af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 32115af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 32125af3e8ccSStefan Behrens 32135af3e8ccSStefan Behrens sinfo = NULL; 32145af3e8ccSStefan Behrens rcu_read_lock(); 32155af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 32165af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 32175af3e8ccSStefan Behrens sinfo = tmp; 32185af3e8ccSStefan Behrens break; 32195af3e8ccSStefan Behrens } 32205af3e8ccSStefan Behrens } 32215af3e8ccSStefan Behrens rcu_read_unlock(); 32225af3e8ccSStefan Behrens 32235af3e8ccSStefan Behrens if (!sinfo) 32245af3e8ccSStefan Behrens continue; 32255af3e8ccSStefan Behrens 32265af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 32275af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 32285af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 32295af3e8ccSStefan Behrens u64 flags; 32305af3e8ccSStefan Behrens 32315af3e8ccSStefan Behrens btrfs_get_block_group_info( 32325af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 32335af3e8ccSStefan Behrens if (space.total_bytes == 0 || 32345af3e8ccSStefan Behrens space.used_bytes == 0) 32355af3e8ccSStefan Behrens continue; 32365af3e8ccSStefan Behrens flags = space.flags; 32375af3e8ccSStefan Behrens /* 32385af3e8ccSStefan Behrens * return 32395af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 32405af3e8ccSStefan Behrens * any of metadata, system or data, else 32415af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 32425af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 32435af3e8ccSStefan Behrens * are used, else 32445af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 32455af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 32465af3e8ccSStefan Behrens * configured and more than 32475af3e8ccSStefan Behrens * 2 mirrors are used. 32485af3e8ccSStefan Behrens */ 32495af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 32505af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 32515af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 32525af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 32535af3e8ccSStefan Behrens == 0))) 32545af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 325553b381b3SDavid Woodhouse else if (num_tolerated_disk_barrier_failures > 1) { 325653b381b3SDavid Woodhouse if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 325753b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5 | 325853b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID10)) { 32595af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 326053b381b3SDavid Woodhouse } else if (flags & 326153b381b3SDavid Woodhouse BTRFS_BLOCK_GROUP_RAID5) { 326253b381b3SDavid Woodhouse num_tolerated_disk_barrier_failures = 2; 326353b381b3SDavid Woodhouse } 326453b381b3SDavid Woodhouse } 32655af3e8ccSStefan Behrens } 32665af3e8ccSStefan Behrens } 32675af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 32685af3e8ccSStefan Behrens } 32695af3e8ccSStefan Behrens 32705af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 32715af3e8ccSStefan Behrens } 32725af3e8ccSStefan Behrens 327348a3b636SEric Sandeen static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3274f2984462SChris Mason { 3275e5e9a520SChris Mason struct list_head *head; 3276f2984462SChris Mason struct btrfs_device *dev; 3277a061fc8dSChris Mason struct btrfs_super_block *sb; 3278f2984462SChris Mason struct btrfs_dev_item *dev_item; 3279f2984462SChris Mason int ret; 3280f2984462SChris Mason int do_barriers; 3281a236aed1SChris Mason int max_errors; 3282a236aed1SChris Mason int total_errors = 0; 3283a061fc8dSChris Mason u64 flags; 3284f2984462SChris Mason 32856c41761fSDavid Sterba max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3286f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3287af31f5e5SChris Mason backup_super_roots(root->fs_info); 3288f2984462SChris Mason 32896c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3290a061fc8dSChris Mason dev_item = &sb->dev_item; 3291e5e9a520SChris Mason 3292174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3293e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3294387125fcSChris Mason 32955af3e8ccSStefan Behrens if (do_barriers) { 32965af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 32975af3e8ccSStefan Behrens if (ret) { 32985af3e8ccSStefan Behrens mutex_unlock( 32995af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 33005af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 33015af3e8ccSStefan Behrens "errors while submitting device barriers."); 33025af3e8ccSStefan Behrens return ret; 33035af3e8ccSStefan Behrens } 33045af3e8ccSStefan Behrens } 3305387125fcSChris Mason 33061f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3307dfe25020SChris Mason if (!dev->bdev) { 3308dfe25020SChris Mason total_errors++; 3309dfe25020SChris Mason continue; 3310dfe25020SChris Mason } 33112b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3312dfe25020SChris Mason continue; 3313dfe25020SChris Mason 33142b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3315a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3316a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 3317a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 3318a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 3319a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3320a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3321a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3322a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 33232b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3324a512bbf8SYan Zheng 3325a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3326a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3327f2984462SChris Mason 3328a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3329a236aed1SChris Mason if (ret) 3330a236aed1SChris Mason total_errors++; 3331f2984462SChris Mason } 3332a236aed1SChris Mason if (total_errors > max_errors) { 3333d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 3334d397712bSChris Mason total_errors); 333579787eaaSJeff Mahoney 333679787eaaSJeff Mahoney /* This shouldn't happen. FUA is masked off if unsupported */ 3337a236aed1SChris Mason BUG(); 3338a236aed1SChris Mason } 3339f2984462SChris Mason 3340a512bbf8SYan Zheng total_errors = 0; 33411f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3342dfe25020SChris Mason if (!dev->bdev) 3343dfe25020SChris Mason continue; 33442b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3345dfe25020SChris Mason continue; 3346dfe25020SChris Mason 3347a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3348a512bbf8SYan Zheng if (ret) 33491259ab75SChris Mason total_errors++; 3350f2984462SChris Mason } 3351174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3352a236aed1SChris Mason if (total_errors > max_errors) { 335379787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 335479787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 335579787eaaSJeff Mahoney return -EIO; 3356a236aed1SChris Mason } 3357f2984462SChris Mason return 0; 3358f2984462SChris Mason } 3359f2984462SChris Mason 3360a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3361a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3362cfaa7295SChris Mason { 3363e66f709bSChris Mason int ret; 33642c90e5d6SChris Mason 3365a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 33665f39d397SChris Mason return ret; 3367cfaa7295SChris Mason } 3368cfaa7295SChris Mason 3369143bede5SJeff Mahoney void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 33702619ba1fSChris Mason { 33714df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 33722619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 33732619ba1fSChris Mason (unsigned long)root->root_key.objectid); 33744df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 337576dda93cSYan, Zheng 337676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 337776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 337876dda93cSYan, Zheng 3379d7634482SLiu Bo if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 33803321719eSLiu Bo btrfs_free_log(NULL, root); 33813321719eSLiu Bo btrfs_free_log_root_tree(NULL, fs_info); 33823321719eSLiu Bo } 33833321719eSLiu Bo 3384581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3385581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 33864df27c4dSYan, Zheng free_fs_root(root); 33874df27c4dSYan, Zheng } 33884df27c4dSYan, Zheng 33894df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 33904df27c4dSYan, Zheng { 339182d5902dSLi Zefan iput(root->cache_inode); 33924df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 33930ee5dc67SAl Viro if (root->anon_dev) 33940ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 33955f39d397SChris Mason free_extent_buffer(root->node); 33965f39d397SChris Mason free_extent_buffer(root->commit_root); 3397581bb050SLi Zefan kfree(root->free_ino_ctl); 3398581bb050SLi Zefan kfree(root->free_ino_pinned); 339958176a96SJosef Bacik kfree(root->name); 34002619ba1fSChris Mason kfree(root); 34012619ba1fSChris Mason } 34022619ba1fSChris Mason 3403c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3404c146afadSYan Zheng { 3405c146afadSYan Zheng u64 root_objectid = 0; 3406c146afadSYan Zheng struct btrfs_root *gang[8]; 3407c146afadSYan Zheng int i; 3408c146afadSYan Zheng int ret; 3409c146afadSYan Zheng 3410c146afadSYan Zheng while (1) { 3411c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3412c146afadSYan Zheng (void **)gang, root_objectid, 3413c146afadSYan Zheng ARRAY_SIZE(gang)); 3414c146afadSYan Zheng if (!ret) 3415c146afadSYan Zheng break; 34165d4f98a2SYan Zheng 34175d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3418c146afadSYan Zheng for (i = 0; i < ret; i++) { 341966b4ffd1SJosef Bacik int err; 342066b4ffd1SJosef Bacik 3421c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 342266b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 342366b4ffd1SJosef Bacik if (err) 342466b4ffd1SJosef Bacik return err; 3425c146afadSYan Zheng } 3426c146afadSYan Zheng root_objectid++; 3427c146afadSYan Zheng } 3428c146afadSYan Zheng return 0; 3429c146afadSYan Zheng } 3430c146afadSYan Zheng 3431c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3432c146afadSYan Zheng { 3433c146afadSYan Zheng struct btrfs_trans_handle *trans; 3434c146afadSYan Zheng int ret; 3435c146afadSYan Zheng 3436c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 343724bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3438c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 34399d1a2a3aSDavid Sterba wake_up_process(root->fs_info->cleaner_kthread); 3440c71bf099SYan, Zheng 3441c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3442c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3443c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3444c71bf099SYan, Zheng 34457a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 34463612b495STsutomu Itoh if (IS_ERR(trans)) 34473612b495STsutomu Itoh return PTR_ERR(trans); 3448c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 344979787eaaSJeff Mahoney if (ret) 345079787eaaSJeff Mahoney return ret; 3451c146afadSYan Zheng /* run commit again to drop the original snapshot */ 34527a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 34533612b495STsutomu Itoh if (IS_ERR(trans)) 34543612b495STsutomu Itoh return PTR_ERR(trans); 345579787eaaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 345679787eaaSJeff Mahoney if (ret) 345779787eaaSJeff Mahoney return ret; 3458c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 345979787eaaSJeff Mahoney if (ret) { 346079787eaaSJeff Mahoney btrfs_error(root->fs_info, ret, 346179787eaaSJeff Mahoney "Failed to sync btree inode to disk."); 346279787eaaSJeff Mahoney return ret; 346379787eaaSJeff Mahoney } 3464c146afadSYan Zheng 3465a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3466c146afadSYan Zheng return ret; 3467c146afadSYan Zheng } 3468c146afadSYan Zheng 3469e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3470eb60ceacSChris Mason { 34710f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3472c146afadSYan Zheng int ret; 3473e089f05cSChris Mason 3474facda1e7SChris Mason fs_info->closing = 1; 3475a2135011SChris Mason smp_mb(); 3476a2135011SChris Mason 3477837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3478aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3479837d5b6eSIlya Dryomov 34808dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 34818dabb742SStefan Behrens 3482aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 34834cb5300bSChris Mason 34844cb5300bSChris Mason /* wait for any defraggers to finish */ 34854cb5300bSChris Mason wait_event(fs_info->transaction_wait, 34864cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 34874cb5300bSChris Mason 34884cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 348926176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 34904cb5300bSChris Mason 3491c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3492c146afadSYan Zheng ret = btrfs_commit_super(root); 3493d397712bSChris Mason if (ret) 3494d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3495c146afadSYan Zheng } 3496ed2ff2cbSChris Mason 349787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 349868ce9682SStefan Behrens btrfs_error_commit_super(root); 3499acce952bSliubo 3500300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3501300e4f8aSJosef Bacik 3502e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3503e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 35048929ecfaSYan, Zheng 3505f25784b3SYan Zheng fs_info->closing = 2; 3506f25784b3SYan Zheng smp_mb(); 3507f25784b3SYan Zheng 3508bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3509bcef60f2SArne Jansen 3510963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3511963d678bSMiao Xie printk(KERN_INFO "btrfs: at unmount delalloc count %lld\n", 3512963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3513b0c68f8bSChris Mason } 351431153d81SYan Zheng 3515e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3516c146afadSYan Zheng 351713e6c37bSJosef Bacik btrfs_stop_all_workers(fs_info); 35182932505aSLiu Bo 35190f7d52f4SChris Mason del_fs_roots(fs_info); 3520d10c5f31SChris Mason 352113e6c37bSJosef Bacik free_root_pointers(fs_info, 1); 35229ad6b7bcSChris Mason 352313e6c37bSJosef Bacik iput(fs_info->btree_inode); 3524d6bfde87SChris Mason 352521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 352621adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 352721adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 352821adbd5cSStefan Behrens #endif 352921adbd5cSStefan Behrens 3530dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 35310b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3532b248a415SChris Mason 3533e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3534963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 353504160088SChris Mason bdi_destroy(&fs_info->bdi); 353676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 35370b86a832SChris Mason 353853b381b3SDavid Woodhouse btrfs_free_stripe_hash_table(fs_info); 353953b381b3SDavid Woodhouse 3540eb60ceacSChris Mason return 0; 3541eb60ceacSChris Mason } 3542eb60ceacSChris Mason 3543b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3544b9fab919SChris Mason int atomic) 3545ccd467d6SChris Mason { 35461259ab75SChris Mason int ret; 3547727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 35481259ab75SChris Mason 35490b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 35501259ab75SChris Mason if (!ret) 35511259ab75SChris Mason return ret; 35521259ab75SChris Mason 35531259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3554b9fab919SChris Mason parent_transid, atomic); 3555b9fab919SChris Mason if (ret == -EAGAIN) 3556b9fab919SChris Mason return ret; 35571259ab75SChris Mason return !ret; 35585f39d397SChris Mason } 35596702ed49SChris Mason 35605f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 35615f39d397SChris Mason { 35620b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 35635f39d397SChris Mason } 35645f39d397SChris Mason 35655f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 35665f39d397SChris Mason { 3567727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 35685f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3569b9473439SChris Mason int was_dirty; 3570b4ce94deSChris Mason 3571b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 357231b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 357331b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3574d397712bSChris Mason "found %llu running %llu\n", 3575db94535dSChris Mason (unsigned long long)buf->start, 3576d397712bSChris Mason (unsigned long long)transid, 3577d397712bSChris Mason (unsigned long long)root->fs_info->generation); 35780b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3579e2d84521SMiao Xie if (!was_dirty) 3580e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3581e2d84521SMiao Xie buf->len, 3582e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 3583eb60ceacSChris Mason } 3584eb60ceacSChris Mason 3585b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3586b53d3f5dSLiu Bo int flush_delayed) 358735b7e476SChris Mason { 3588188de649SChris Mason /* 3589188de649SChris Mason * looks as though older kernels can get into trouble with 3590188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3591188de649SChris Mason */ 3592e2d84521SMiao Xie int ret; 3593d6bfde87SChris Mason 35946933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3595d6bfde87SChris Mason return; 3596d6bfde87SChris Mason 3597b53d3f5dSLiu Bo if (flush_delayed) 359816cdcec7SMiao Xie btrfs_balance_delayed_items(root); 359916cdcec7SMiao Xie 3600e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3601e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3602e2d84521SMiao Xie if (ret > 0) { 3603d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited( 3604d0e1d66bSNamjae Jeon root->fs_info->btree_inode->i_mapping); 360516cdcec7SMiao Xie } 360616cdcec7SMiao Xie return; 360716cdcec7SMiao Xie } 360816cdcec7SMiao Xie 3609b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 361016cdcec7SMiao Xie { 3611b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 361235b7e476SChris Mason } 3613b53d3f5dSLiu Bo 3614b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3615b53d3f5dSLiu Bo { 3616b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3617d6bfde87SChris Mason } 36186b80053dSChris Mason 3619ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 36206b80053dSChris Mason { 3621727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 36220b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 36236b80053dSChris Mason } 36240da5468fSChris Mason 3625fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3626acce952bSliubo int read_only) 3627acce952bSliubo { 36281104a885SDavid Sterba /* 36291104a885SDavid Sterba * Placeholder for checks 36301104a885SDavid Sterba */ 3631fcd1f065SDavid Sterba return 0; 3632fcd1f065SDavid Sterba } 3633fcd1f065SDavid Sterba 363448a3b636SEric Sandeen static void btrfs_error_commit_super(struct btrfs_root *root) 3635acce952bSliubo { 3636acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3637acce952bSliubo btrfs_run_delayed_iputs(root); 3638acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3639acce952bSliubo 3640acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3641acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3642acce952bSliubo 3643acce952bSliubo /* cleanup FS via transaction */ 3644acce952bSliubo btrfs_cleanup_transaction(root); 3645acce952bSliubo } 3646acce952bSliubo 3647569e0f35SJosef Bacik static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, 3648569e0f35SJosef Bacik struct btrfs_root *root) 3649acce952bSliubo { 3650acce952bSliubo struct btrfs_inode *btrfs_inode; 3651acce952bSliubo struct list_head splice; 3652acce952bSliubo 3653acce952bSliubo INIT_LIST_HEAD(&splice); 3654acce952bSliubo 3655acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3656acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3657acce952bSliubo 3658569e0f35SJosef Bacik list_splice_init(&t->ordered_operations, &splice); 3659acce952bSliubo while (!list_empty(&splice)) { 3660acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3661acce952bSliubo ordered_operations); 3662acce952bSliubo 3663acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3664b216cbfbSMiao Xie spin_unlock(&root->fs_info->ordered_extent_lock); 3665acce952bSliubo 3666acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3667b216cbfbSMiao Xie 3668b216cbfbSMiao Xie spin_lock(&root->fs_info->ordered_extent_lock); 3669acce952bSliubo } 3670acce952bSliubo 3671acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3672acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3673acce952bSliubo } 3674acce952bSliubo 3675143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3676acce952bSliubo { 3677acce952bSliubo struct btrfs_ordered_extent *ordered; 3678acce952bSliubo 3679acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3680779880efSJosef Bacik /* 3681779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3682779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3683779880efSJosef Bacik */ 3684779880efSJosef Bacik list_for_each_entry(ordered, &root->fs_info->ordered_extents, 3685779880efSJosef Bacik root_extent_list) 3686779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3687acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3688acce952bSliubo } 3689acce952bSliubo 369079787eaaSJeff Mahoney int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3691acce952bSliubo struct btrfs_root *root) 3692acce952bSliubo { 3693acce952bSliubo struct rb_node *node; 3694acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3695acce952bSliubo struct btrfs_delayed_ref_node *ref; 3696acce952bSliubo int ret = 0; 3697acce952bSliubo 3698acce952bSliubo delayed_refs = &trans->delayed_refs; 3699acce952bSliubo 3700acce952bSliubo spin_lock(&delayed_refs->lock); 3701acce952bSliubo if (delayed_refs->num_entries == 0) { 3702cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3703acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3704acce952bSliubo return ret; 3705acce952bSliubo } 3706acce952bSliubo 3707b939d1abSJosef Bacik while ((node = rb_first(&delayed_refs->root)) != NULL) { 3708eb12db69SJosef Bacik struct btrfs_delayed_ref_head *head = NULL; 3709acce952bSliubo 3710eb12db69SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3711acce952bSliubo atomic_set(&ref->refs, 1); 3712acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3713acce952bSliubo 3714acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3715b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3716b939d1abSJosef Bacik atomic_inc(&ref->refs); 371779787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3718b939d1abSJosef Bacik 3719b939d1abSJosef Bacik /* Need to wait for the delayed ref to run */ 3720acce952bSliubo mutex_lock(&head->mutex); 3721b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3722b939d1abSJosef Bacik btrfs_put_delayed_ref(ref); 3723b939d1abSJosef Bacik 3724e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3725b939d1abSJosef Bacik continue; 3726b939d1abSJosef Bacik } 3727b939d1abSJosef Bacik 372854067ae9SJosef Bacik if (head->must_insert_reserved) 372954067ae9SJosef Bacik btrfs_pin_extent(root, ref->bytenr, 373054067ae9SJosef Bacik ref->num_bytes, 1); 373178a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 3732acce952bSliubo delayed_refs->num_heads--; 3733acce952bSliubo if (list_empty(&head->cluster)) 3734acce952bSliubo delayed_refs->num_heads_ready--; 3735acce952bSliubo list_del_init(&head->cluster); 3736acce952bSliubo } 3737eb12db69SJosef Bacik 3738b939d1abSJosef Bacik ref->in_tree = 0; 3739b939d1abSJosef Bacik rb_erase(&ref->rb_node, &delayed_refs->root); 3740b939d1abSJosef Bacik delayed_refs->num_entries--; 3741eb12db69SJosef Bacik if (head) 3742eb12db69SJosef Bacik mutex_unlock(&head->mutex); 3743acce952bSliubo spin_unlock(&delayed_refs->lock); 3744acce952bSliubo btrfs_put_delayed_ref(ref); 3745acce952bSliubo 3746acce952bSliubo cond_resched(); 3747acce952bSliubo spin_lock(&delayed_refs->lock); 3748acce952bSliubo } 3749acce952bSliubo 3750acce952bSliubo spin_unlock(&delayed_refs->lock); 3751acce952bSliubo 3752acce952bSliubo return ret; 3753acce952bSliubo } 3754acce952bSliubo 3755aec8030aSMiao Xie static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t) 3756acce952bSliubo { 3757acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3758acce952bSliubo struct list_head splice; 3759acce952bSliubo 3760acce952bSliubo INIT_LIST_HEAD(&splice); 3761acce952bSliubo 3762acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3763acce952bSliubo 3764acce952bSliubo while (!list_empty(&splice)) { 3765acce952bSliubo snapshot = list_entry(splice.next, 3766acce952bSliubo struct btrfs_pending_snapshot, 3767acce952bSliubo list); 3768aec8030aSMiao Xie snapshot->error = -ECANCELED; 3769acce952bSliubo list_del_init(&snapshot->list); 3770acce952bSliubo } 3771acce952bSliubo } 3772acce952bSliubo 3773143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3774acce952bSliubo { 3775acce952bSliubo struct btrfs_inode *btrfs_inode; 3776acce952bSliubo struct list_head splice; 3777acce952bSliubo 3778acce952bSliubo INIT_LIST_HEAD(&splice); 3779acce952bSliubo 3780acce952bSliubo spin_lock(&root->fs_info->delalloc_lock); 37815be76758SDavid Sterba list_splice_init(&root->fs_info->delalloc_inodes, &splice); 3782acce952bSliubo 3783acce952bSliubo while (!list_empty(&splice)) { 3784acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3785acce952bSliubo delalloc_inodes); 3786acce952bSliubo 3787acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3788df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 3789df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 3790b216cbfbSMiao Xie spin_unlock(&root->fs_info->delalloc_lock); 3791acce952bSliubo 3792acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3793b216cbfbSMiao Xie 3794b216cbfbSMiao Xie spin_lock(&root->fs_info->delalloc_lock); 3795acce952bSliubo } 3796acce952bSliubo 3797acce952bSliubo spin_unlock(&root->fs_info->delalloc_lock); 3798acce952bSliubo } 3799acce952bSliubo 3800acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3801acce952bSliubo struct extent_io_tree *dirty_pages, 3802acce952bSliubo int mark) 3803acce952bSliubo { 3804acce952bSliubo int ret; 3805acce952bSliubo struct extent_buffer *eb; 3806acce952bSliubo u64 start = 0; 3807acce952bSliubo u64 end; 3808acce952bSliubo 3809acce952bSliubo while (1) { 3810acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3811e6138876SJosef Bacik mark, NULL); 3812acce952bSliubo if (ret) 3813acce952bSliubo break; 3814acce952bSliubo 3815acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3816acce952bSliubo while (start <= end) { 3817fd8b2b61SJosef Bacik eb = btrfs_find_tree_block(root, start, 3818fd8b2b61SJosef Bacik root->leafsize); 381969a85bd8SJosef Bacik start += root->leafsize; 3820fd8b2b61SJosef Bacik if (!eb) 3821acce952bSliubo continue; 3822fd8b2b61SJosef Bacik wait_on_extent_buffer_writeback(eb); 3823acce952bSliubo 3824fd8b2b61SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3825fd8b2b61SJosef Bacik &eb->bflags)) 3826fd8b2b61SJosef Bacik clear_extent_buffer_dirty(eb); 3827fd8b2b61SJosef Bacik free_extent_buffer_stale(eb); 3828acce952bSliubo } 3829acce952bSliubo } 3830acce952bSliubo 3831acce952bSliubo return ret; 3832acce952bSliubo } 3833acce952bSliubo 3834acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3835acce952bSliubo struct extent_io_tree *pinned_extents) 3836acce952bSliubo { 3837acce952bSliubo struct extent_io_tree *unpin; 3838acce952bSliubo u64 start; 3839acce952bSliubo u64 end; 3840acce952bSliubo int ret; 3841ed0eaa14SLiu Bo bool loop = true; 3842acce952bSliubo 3843acce952bSliubo unpin = pinned_extents; 3844ed0eaa14SLiu Bo again: 3845acce952bSliubo while (1) { 3846acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3847e6138876SJosef Bacik EXTENT_DIRTY, NULL); 3848acce952bSliubo if (ret) 3849acce952bSliubo break; 3850acce952bSliubo 3851acce952bSliubo /* opt_discard */ 38525378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 38535378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 38545378e607SLi Dongyang end + 1 - start, 38555378e607SLi Dongyang NULL); 3856acce952bSliubo 3857acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3858acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3859acce952bSliubo cond_resched(); 3860acce952bSliubo } 3861acce952bSliubo 3862ed0eaa14SLiu Bo if (loop) { 3863ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 3864ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 3865ed0eaa14SLiu Bo else 3866ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 3867ed0eaa14SLiu Bo loop = false; 3868ed0eaa14SLiu Bo goto again; 3869ed0eaa14SLiu Bo } 3870ed0eaa14SLiu Bo 3871acce952bSliubo return 0; 3872acce952bSliubo } 3873acce952bSliubo 387449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 387549b25e05SJeff Mahoney struct btrfs_root *root) 387649b25e05SJeff Mahoney { 387749b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 387849b25e05SJeff Mahoney btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 387949b25e05SJeff Mahoney cur_trans->dirty_pages.dirty_bytes); 388049b25e05SJeff Mahoney 388149b25e05SJeff Mahoney /* FIXME: cleanup wait for commit */ 388249b25e05SJeff Mahoney cur_trans->in_commit = 1; 388349b25e05SJeff Mahoney cur_trans->blocked = 1; 388449b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 388549b25e05SJeff Mahoney 3886aec8030aSMiao Xie btrfs_evict_pending_snapshots(cur_trans); 3887aec8030aSMiao Xie 388849b25e05SJeff Mahoney cur_trans->blocked = 0; 388949b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 389049b25e05SJeff Mahoney 389149b25e05SJeff Mahoney cur_trans->commit_done = 1; 389249b25e05SJeff Mahoney wake_up(&cur_trans->commit_wait); 389349b25e05SJeff Mahoney 389467cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 389567cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 389667cde344SMiao Xie 389749b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 389849b25e05SJeff Mahoney EXTENT_DIRTY); 38996e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 39006e841e32SLiu Bo root->fs_info->pinned_extents); 390149b25e05SJeff Mahoney 390249b25e05SJeff Mahoney /* 390349b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 390449b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 390549b25e05SJeff Mahoney */ 390649b25e05SJeff Mahoney } 390749b25e05SJeff Mahoney 390848a3b636SEric Sandeen static int btrfs_cleanup_transaction(struct btrfs_root *root) 3909acce952bSliubo { 3910acce952bSliubo struct btrfs_transaction *t; 3911acce952bSliubo LIST_HEAD(list); 3912acce952bSliubo 3913acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 3914acce952bSliubo 3915a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3916acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 3917a4abeea4SJosef Bacik root->fs_info->trans_no_join = 1; 3918a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3919a4abeea4SJosef Bacik 3920acce952bSliubo while (!list_empty(&list)) { 3921acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 3922acce952bSliubo 3923569e0f35SJosef Bacik btrfs_destroy_ordered_operations(t, root); 3924acce952bSliubo 3925acce952bSliubo btrfs_destroy_ordered_extents(root); 3926acce952bSliubo 3927acce952bSliubo btrfs_destroy_delayed_refs(t, root); 3928acce952bSliubo 3929acce952bSliubo /* FIXME: cleanup wait for commit */ 3930acce952bSliubo t->in_commit = 1; 3931acce952bSliubo t->blocked = 1; 393266657b31SJosef Bacik smp_mb(); 3933acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3934acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 3935acce952bSliubo 3936aec8030aSMiao Xie btrfs_evict_pending_snapshots(t); 3937aec8030aSMiao Xie 3938acce952bSliubo t->blocked = 0; 393966657b31SJosef Bacik smp_mb(); 3940acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 3941acce952bSliubo wake_up(&root->fs_info->transaction_wait); 3942acce952bSliubo 3943acce952bSliubo t->commit_done = 1; 394466657b31SJosef Bacik smp_mb(); 3945acce952bSliubo if (waitqueue_active(&t->commit_wait)) 3946acce952bSliubo wake_up(&t->commit_wait); 3947acce952bSliubo 394867cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 394967cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 395067cde344SMiao Xie 3951acce952bSliubo btrfs_destroy_delalloc_inodes(root); 3952acce952bSliubo 3953a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3954acce952bSliubo root->fs_info->running_transaction = NULL; 3955a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3956acce952bSliubo 3957acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 3958acce952bSliubo EXTENT_DIRTY); 3959acce952bSliubo 3960acce952bSliubo btrfs_destroy_pinned_extent(root, 3961acce952bSliubo root->fs_info->pinned_extents); 3962acce952bSliubo 396313c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 3964acce952bSliubo list_del_init(&t->list); 3965acce952bSliubo memset(t, 0, sizeof(*t)); 3966acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 3967acce952bSliubo } 3968acce952bSliubo 3969a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3970a4abeea4SJosef Bacik root->fs_info->trans_no_join = 0; 3971a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3972acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 3973acce952bSliubo 3974acce952bSliubo return 0; 3975acce952bSliubo } 3976acce952bSliubo 3977d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 3978ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 39794bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 39800b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 3981239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 3982239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 39830da5468fSChris Mason }; 3984