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> 337e75bf3fSDavid Sterba #include <asm/unaligned.h> 344b4e25f2SChris Mason #include "compat.h" 35eb60ceacSChris Mason #include "ctree.h" 36eb60ceacSChris Mason #include "disk-io.h" 37e089f05cSChris Mason #include "transaction.h" 380f7d52f4SChris Mason #include "btrfs_inode.h" 390b86a832SChris Mason #include "volumes.h" 40db94535dSChris Mason #include "print-tree.h" 418b712842SChris Mason #include "async-thread.h" 42925baeddSChris Mason #include "locking.h" 43e02119d5SChris Mason #include "tree-log.h" 44fa9c0d79SChris Mason #include "free-space-cache.h" 45581bb050SLi Zefan #include "inode-map.h" 4621adbd5cSStefan Behrens #include "check-integrity.h" 47606686eeSJosef Bacik #include "rcu-string.h" 488dabb742SStefan Behrens #include "dev-replace.h" 49eb60ceacSChris Mason 50de0022b9SJosef Bacik #ifdef CONFIG_X86 51de0022b9SJosef Bacik #include <asm/cpufeature.h> 52de0022b9SJosef Bacik #endif 53de0022b9SJosef Bacik 54d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 558b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 564df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 57fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 58acce952bSliubo int read_only); 59143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root); 60143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 61acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 62acce952bSliubo struct btrfs_root *root); 63143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t); 64143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 65acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 66acce952bSliubo struct extent_io_tree *dirty_pages, 67acce952bSliubo int mark); 68acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 69acce952bSliubo struct extent_io_tree *pinned_extents); 70ce9adaa5SChris Mason 71d352ac68SChris Mason /* 72d352ac68SChris Mason * end_io_wq structs are used to do processing in task context when an IO is 73d352ac68SChris Mason * complete. This is used during reads to verify checksums, and it is used 74d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 75d352ac68SChris Mason */ 76ce9adaa5SChris Mason struct end_io_wq { 77ce9adaa5SChris Mason struct bio *bio; 78ce9adaa5SChris Mason bio_end_io_t *end_io; 79ce9adaa5SChris Mason void *private; 80ce9adaa5SChris Mason struct btrfs_fs_info *info; 81ce9adaa5SChris Mason int error; 8222c59948SChris Mason int metadata; 83ce9adaa5SChris Mason struct list_head list; 848b712842SChris Mason struct btrfs_work work; 85ce9adaa5SChris Mason }; 860da5468fSChris Mason 87d352ac68SChris Mason /* 88d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 89d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 90d352ac68SChris Mason * just before they are sent down the IO stack. 91d352ac68SChris Mason */ 9244b8bd7eSChris Mason struct async_submit_bio { 9344b8bd7eSChris Mason struct inode *inode; 9444b8bd7eSChris Mason struct bio *bio; 9544b8bd7eSChris Mason struct list_head list; 964a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 974a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 9844b8bd7eSChris Mason int rw; 9944b8bd7eSChris Mason int mirror_num; 100c8b97818SChris Mason unsigned long bio_flags; 101eaf25d93SChris Mason /* 102eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 103eaf25d93SChris Mason * can't tell us where in the file the bio should go 104eaf25d93SChris Mason */ 105eaf25d93SChris Mason u64 bio_offset; 1068b712842SChris Mason struct btrfs_work work; 10779787eaaSJeff Mahoney int error; 10844b8bd7eSChris Mason }; 10944b8bd7eSChris Mason 11085d4e461SChris Mason /* 11185d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 11285d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 11385d4e461SChris Mason * the level the eb occupies in the tree. 1144008c04aSChris Mason * 11585d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 11685d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 11785d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 11885d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 11985d4e461SChris Mason * have separate keysets. 1204008c04aSChris Mason * 12185d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 12285d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 12385d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1244008c04aSChris Mason * 12585d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 12685d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 12785d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 12885d4e461SChris Mason * 12985d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 13085d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 13185d4e461SChris Mason * needs update as well. 1324008c04aSChris Mason */ 1334008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1344008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1354008c04aSChris Mason # error 1364008c04aSChris Mason # endif 13785d4e461SChris Mason 13885d4e461SChris Mason static struct btrfs_lockdep_keyset { 13985d4e461SChris Mason u64 id; /* root objectid */ 14085d4e461SChris Mason const char *name_stem; /* lock name stem */ 14185d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 14285d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 14385d4e461SChris Mason } btrfs_lockdep_keysets[] = { 14485d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 14585d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 14685d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 14785d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 14885d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 14985d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 15085d4e461SChris Mason { .id = BTRFS_ORPHAN_OBJECTID, .name_stem = "orphan" }, 15185d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 15285d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 15385d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 15485d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1554008c04aSChris Mason }; 15685d4e461SChris Mason 15785d4e461SChris Mason void __init btrfs_init_lockdep(void) 15885d4e461SChris Mason { 15985d4e461SChris Mason int i, j; 16085d4e461SChris Mason 16185d4e461SChris Mason /* initialize lockdep class names */ 16285d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 16385d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 16485d4e461SChris Mason 16585d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 16685d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 16785d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 16885d4e461SChris Mason } 16985d4e461SChris Mason } 17085d4e461SChris Mason 17185d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 17285d4e461SChris Mason int level) 17385d4e461SChris Mason { 17485d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 17585d4e461SChris Mason 17685d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 17785d4e461SChris Mason 17885d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 17985d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 18085d4e461SChris Mason if (ks->id == objectid) 18185d4e461SChris Mason break; 18285d4e461SChris Mason 18385d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 18485d4e461SChris Mason &ks->keys[level], ks->names[level]); 18585d4e461SChris Mason } 18685d4e461SChris Mason 1874008c04aSChris Mason #endif 1884008c04aSChris Mason 189d352ac68SChris Mason /* 190d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 191d352ac68SChris Mason * that covers the entire device 192d352ac68SChris Mason */ 193b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 194306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 1955f39d397SChris Mason int create) 1965f39d397SChris Mason { 1975f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1985f39d397SChris Mason struct extent_map *em; 1995f39d397SChris Mason int ret; 2005f39d397SChris Mason 201890871beSChris Mason read_lock(&em_tree->lock); 202d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 203a061fc8dSChris Mason if (em) { 204a061fc8dSChris Mason em->bdev = 205a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 206890871beSChris Mason read_unlock(&em_tree->lock); 2075f39d397SChris Mason goto out; 208a061fc8dSChris Mason } 209890871beSChris Mason read_unlock(&em_tree->lock); 2107b13b7b1SChris Mason 211172ddd60SDavid Sterba em = alloc_extent_map(); 2125f39d397SChris Mason if (!em) { 2135f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2145f39d397SChris Mason goto out; 2155f39d397SChris Mason } 2165f39d397SChris Mason em->start = 0; 2170afbaf8cSChris Mason em->len = (u64)-1; 218c8b97818SChris Mason em->block_len = (u64)-1; 2195f39d397SChris Mason em->block_start = 0; 220a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 221d1310b2eSChris Mason 222890871beSChris Mason write_lock(&em_tree->lock); 2235f39d397SChris Mason ret = add_extent_mapping(em_tree, em); 2245f39d397SChris Mason if (ret == -EEXIST) { 2255f39d397SChris Mason free_extent_map(em); 2267b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 227b4f359abSTsutomu Itoh if (!em) 2280433f20dSTsutomu Itoh em = ERR_PTR(-EIO); 2295f39d397SChris Mason } else if (ret) { 2307b13b7b1SChris Mason free_extent_map(em); 2310433f20dSTsutomu Itoh em = ERR_PTR(ret); 2325f39d397SChris Mason } 233890871beSChris Mason write_unlock(&em_tree->lock); 2347b13b7b1SChris Mason 2355f39d397SChris Mason out: 2365f39d397SChris Mason return em; 2375f39d397SChris Mason } 2385f39d397SChris Mason 23919c00ddcSChris Mason u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) 24019c00ddcSChris Mason { 241163e783eSDavid Woodhouse return crc32c(seed, data, len); 24219c00ddcSChris Mason } 24319c00ddcSChris Mason 24419c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 24519c00ddcSChris Mason { 2467e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 24719c00ddcSChris Mason } 24819c00ddcSChris Mason 249d352ac68SChris Mason /* 250d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 251d352ac68SChris Mason * into the csum field of the block. 252d352ac68SChris Mason */ 25319c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 25419c00ddcSChris Mason int verify) 25519c00ddcSChris Mason { 2566c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 257607d432dSJosef Bacik char *result = NULL; 25819c00ddcSChris Mason unsigned long len; 25919c00ddcSChris Mason unsigned long cur_len; 26019c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26119c00ddcSChris Mason char *kaddr; 26219c00ddcSChris Mason unsigned long map_start; 26319c00ddcSChris Mason unsigned long map_len; 26419c00ddcSChris Mason int err; 26519c00ddcSChris Mason u32 crc = ~(u32)0; 266607d432dSJosef Bacik unsigned long inline_result; 26719c00ddcSChris Mason 26819c00ddcSChris Mason len = buf->len - offset; 26919c00ddcSChris Mason while (len > 0) { 27019c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 271a6591715SChris Mason &kaddr, &map_start, &map_len); 272d397712bSChris Mason if (err) 27319c00ddcSChris Mason return 1; 27419c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 27519c00ddcSChris Mason crc = btrfs_csum_data(root, kaddr + offset - map_start, 27619c00ddcSChris Mason crc, cur_len); 27719c00ddcSChris Mason len -= cur_len; 27819c00ddcSChris Mason offset += cur_len; 27919c00ddcSChris Mason } 280607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 281607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 282607d432dSJosef Bacik if (!result) 283607d432dSJosef Bacik return 1; 284607d432dSJosef Bacik } else { 285607d432dSJosef Bacik result = (char *)&inline_result; 286607d432dSJosef Bacik } 287607d432dSJosef Bacik 28819c00ddcSChris Mason btrfs_csum_final(crc, result); 28919c00ddcSChris Mason 29019c00ddcSChris Mason if (verify) { 291607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 292e4204dedSChris Mason u32 val; 293e4204dedSChris Mason u32 found = 0; 294607d432dSJosef Bacik memcpy(&found, result, csum_size); 295e4204dedSChris Mason 296607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 2977a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs: %s checksum verify " 298193f284dSChris Mason "failed on %llu wanted %X found %X " 299193f284dSChris Mason "level %d\n", 30019c00ddcSChris Mason root->fs_info->sb->s_id, 301193f284dSChris Mason (unsigned long long)buf->start, val, found, 302193f284dSChris Mason btrfs_header_level(buf)); 303607d432dSJosef Bacik if (result != (char *)&inline_result) 304607d432dSJosef Bacik kfree(result); 30519c00ddcSChris Mason return 1; 30619c00ddcSChris Mason } 30719c00ddcSChris Mason } else { 308607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 30919c00ddcSChris Mason } 310607d432dSJosef Bacik if (result != (char *)&inline_result) 311607d432dSJosef Bacik kfree(result); 31219c00ddcSChris Mason return 0; 31319c00ddcSChris Mason } 31419c00ddcSChris Mason 315d352ac68SChris Mason /* 316d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 317d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 318d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 319d352ac68SChris Mason * in the wrong place. 320d352ac68SChris Mason */ 3211259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 322b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 323b9fab919SChris Mason int atomic) 3241259ab75SChris Mason { 3252ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3261259ab75SChris Mason int ret; 3271259ab75SChris Mason 3281259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3291259ab75SChris Mason return 0; 3301259ab75SChris Mason 331b9fab919SChris Mason if (atomic) 332b9fab919SChris Mason return -EAGAIN; 333b9fab919SChris Mason 3342ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 335d0082371SJeff Mahoney 0, &cached_state); 3360b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3371259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3381259ab75SChris Mason ret = 0; 3391259ab75SChris Mason goto out; 3401259ab75SChris Mason } 3417a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 342193f284dSChris Mason "found %llu\n", 3431259ab75SChris Mason (unsigned long long)eb->start, 3441259ab75SChris Mason (unsigned long long)parent_transid, 3451259ab75SChris Mason (unsigned long long)btrfs_header_generation(eb)); 3461259ab75SChris Mason ret = 1; 3470b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 34833958dc6SChris Mason out: 3492ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3502ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3511259ab75SChris Mason return ret; 3521259ab75SChris Mason } 3531259ab75SChris Mason 354d352ac68SChris Mason /* 355d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 356d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 357d352ac68SChris Mason */ 358f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 359f188591eSChris Mason struct extent_buffer *eb, 360ca7a79adSChris Mason u64 start, u64 parent_transid) 361f188591eSChris Mason { 362f188591eSChris Mason struct extent_io_tree *io_tree; 363ea466794SJosef Bacik int failed = 0; 364f188591eSChris Mason int ret; 365f188591eSChris Mason int num_copies = 0; 366f188591eSChris Mason int mirror_num = 0; 367ea466794SJosef Bacik int failed_mirror = 0; 368f188591eSChris Mason 369a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 370f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 371f188591eSChris Mason while (1) { 372bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 373bb82ab88SArne Jansen WAIT_COMPLETE, 374f188591eSChris Mason btree_get_extent, mirror_num); 375256dd1bbSStefan Behrens if (!ret) { 376256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 377b9fab919SChris Mason parent_transid, 0)) 378ea466794SJosef Bacik break; 379256dd1bbSStefan Behrens else 380256dd1bbSStefan Behrens ret = -EIO; 381256dd1bbSStefan Behrens } 382d397712bSChris Mason 383a826d6dcSJosef Bacik /* 384a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 385a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 386a826d6dcSJosef Bacik * any less wrong. 387a826d6dcSJosef Bacik */ 388a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 389ea466794SJosef Bacik break; 390ea466794SJosef Bacik 3915d964051SStefan Behrens num_copies = btrfs_num_copies(root->fs_info, 392f188591eSChris Mason eb->start, eb->len); 3934235298eSChris Mason if (num_copies == 1) 394ea466794SJosef Bacik break; 3954235298eSChris Mason 3965cf1ab56SJosef Bacik if (!failed_mirror) { 3975cf1ab56SJosef Bacik failed = 1; 3985cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 3995cf1ab56SJosef Bacik } 4005cf1ab56SJosef Bacik 401f188591eSChris Mason mirror_num++; 402ea466794SJosef Bacik if (mirror_num == failed_mirror) 403ea466794SJosef Bacik mirror_num++; 404ea466794SJosef Bacik 4054235298eSChris Mason if (mirror_num > num_copies) 406ea466794SJosef Bacik break; 407f188591eSChris Mason } 408ea466794SJosef Bacik 409c0901581SStefan Behrens if (failed && !ret && failed_mirror) 410ea466794SJosef Bacik repair_eb_io_failure(root, eb, failed_mirror); 411ea466794SJosef Bacik 412ea466794SJosef Bacik return ret; 413f188591eSChris Mason } 41419c00ddcSChris Mason 415d352ac68SChris Mason /* 416d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 417d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 418d352ac68SChris Mason */ 419d397712bSChris Mason 420b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 42119c00ddcSChris Mason { 422d1310b2eSChris Mason struct extent_io_tree *tree; 4234eee4fa4SMiao Xie u64 start = page_offset(page); 42419c00ddcSChris Mason u64 found_start; 42519c00ddcSChris Mason struct extent_buffer *eb; 426f188591eSChris Mason 427d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 42819c00ddcSChris Mason 4294f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4304f2de97aSJosef Bacik if (page != eb->pages[0]) 4314f2de97aSJosef Bacik return 0; 43219c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 43319c00ddcSChris Mason if (found_start != start) { 43455c69072SChris Mason WARN_ON(1); 4354f2de97aSJosef Bacik return 0; 43655c69072SChris Mason } 43755c69072SChris Mason if (!PageUptodate(page)) { 43855c69072SChris Mason WARN_ON(1); 4394f2de97aSJosef Bacik return 0; 44019c00ddcSChris Mason } 44119c00ddcSChris Mason csum_tree_block(root, eb, 0); 44219c00ddcSChris Mason return 0; 44319c00ddcSChris Mason } 44419c00ddcSChris Mason 4452b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 4462b82032cSYan Zheng struct extent_buffer *eb) 4472b82032cSYan Zheng { 4482b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 4492b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 4502b82032cSYan Zheng int ret = 1; 4512b82032cSYan Zheng 4522b82032cSYan Zheng read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb), 4532b82032cSYan Zheng BTRFS_FSID_SIZE); 4542b82032cSYan Zheng while (fs_devices) { 4552b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 4562b82032cSYan Zheng ret = 0; 4572b82032cSYan Zheng break; 4582b82032cSYan Zheng } 4592b82032cSYan Zheng fs_devices = fs_devices->seed; 4602b82032cSYan Zheng } 4612b82032cSYan Zheng return ret; 4622b82032cSYan Zheng } 4632b82032cSYan Zheng 464a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 465a826d6dcSJosef Bacik printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \ 466a826d6dcSJosef Bacik "root=%llu, slot=%d\n", reason, \ 467a826d6dcSJosef Bacik (unsigned long long)btrfs_header_bytenr(eb), \ 468a826d6dcSJosef Bacik (unsigned long long)root->objectid, slot) 469a826d6dcSJosef Bacik 470a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 471a826d6dcSJosef Bacik struct extent_buffer *leaf) 472a826d6dcSJosef Bacik { 473a826d6dcSJosef Bacik struct btrfs_key key; 474a826d6dcSJosef Bacik struct btrfs_key leaf_key; 475a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 476a826d6dcSJosef Bacik int slot; 477a826d6dcSJosef Bacik 478a826d6dcSJosef Bacik if (nritems == 0) 479a826d6dcSJosef Bacik return 0; 480a826d6dcSJosef Bacik 481a826d6dcSJosef Bacik /* Check the 0 item */ 482a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 483a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 484a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 485a826d6dcSJosef Bacik return -EIO; 486a826d6dcSJosef Bacik } 487a826d6dcSJosef Bacik 488a826d6dcSJosef Bacik /* 489a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 490a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 491a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 492a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 493a826d6dcSJosef Bacik * offset is correct. 494a826d6dcSJosef Bacik */ 495a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 496a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 497a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 498a826d6dcSJosef Bacik 499a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 500a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 501a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 502a826d6dcSJosef Bacik return -EIO; 503a826d6dcSJosef Bacik } 504a826d6dcSJosef Bacik 505a826d6dcSJosef Bacik /* 506a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 507a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 508a826d6dcSJosef Bacik * front. 509a826d6dcSJosef Bacik */ 510a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 511a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 512a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 513a826d6dcSJosef Bacik return -EIO; 514a826d6dcSJosef Bacik } 515a826d6dcSJosef Bacik 516a826d6dcSJosef Bacik /* 517a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 518a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 519a826d6dcSJosef Bacik * all point outside of the leaf. 520a826d6dcSJosef Bacik */ 521a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 522a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 523a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 524a826d6dcSJosef Bacik return -EIO; 525a826d6dcSJosef Bacik } 526a826d6dcSJosef Bacik } 527a826d6dcSJosef Bacik 528a826d6dcSJosef Bacik return 0; 529a826d6dcSJosef Bacik } 530a826d6dcSJosef Bacik 531727011e0SChris Mason struct extent_buffer *find_eb_for_page(struct extent_io_tree *tree, 532727011e0SChris Mason struct page *page, int max_walk) 533727011e0SChris Mason { 534727011e0SChris Mason struct extent_buffer *eb; 535727011e0SChris Mason u64 start = page_offset(page); 536727011e0SChris Mason u64 target = start; 537727011e0SChris Mason u64 min_start; 538727011e0SChris Mason 539727011e0SChris Mason if (start < max_walk) 540727011e0SChris Mason min_start = 0; 541727011e0SChris Mason else 542727011e0SChris Mason min_start = start - max_walk; 543727011e0SChris Mason 544727011e0SChris Mason while (start >= min_start) { 545727011e0SChris Mason eb = find_extent_buffer(tree, start, 0); 546727011e0SChris Mason if (eb) { 547727011e0SChris Mason /* 548727011e0SChris Mason * we found an extent buffer and it contains our page 549727011e0SChris Mason * horray! 550727011e0SChris Mason */ 551727011e0SChris Mason if (eb->start <= target && 552727011e0SChris Mason eb->start + eb->len > target) 553727011e0SChris Mason return eb; 554727011e0SChris Mason 555727011e0SChris Mason /* we found an extent buffer that wasn't for us */ 556727011e0SChris Mason free_extent_buffer(eb); 557727011e0SChris Mason return NULL; 558727011e0SChris Mason } 559727011e0SChris Mason if (start == 0) 560727011e0SChris Mason break; 561727011e0SChris Mason start -= PAGE_CACHE_SIZE; 562727011e0SChris Mason } 563727011e0SChris Mason return NULL; 564727011e0SChris Mason } 565727011e0SChris Mason 566b2950863SChristoph Hellwig static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 5675cf1ab56SJosef Bacik struct extent_state *state, int mirror) 568ce9adaa5SChris Mason { 569ce9adaa5SChris Mason struct extent_io_tree *tree; 570ce9adaa5SChris Mason u64 found_start; 571ce9adaa5SChris Mason int found_level; 572ce9adaa5SChris Mason struct extent_buffer *eb; 573ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 574f188591eSChris Mason int ret = 0; 575727011e0SChris Mason int reads_done; 576ce9adaa5SChris Mason 577ce9adaa5SChris Mason if (!page->private) 578ce9adaa5SChris Mason goto out; 579d397712bSChris Mason 580727011e0SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 5814f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 582d397712bSChris Mason 5830b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5840b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5850b32f4bbSJosef Bacik */ 5860b32f4bbSJosef Bacik extent_buffer_get(eb); 5870b32f4bbSJosef Bacik 5880b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 589727011e0SChris Mason if (!reads_done) 590727011e0SChris Mason goto err; 591f188591eSChris Mason 5925cf1ab56SJosef Bacik eb->read_mirror = mirror; 593ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 594ea466794SJosef Bacik ret = -EIO; 595ea466794SJosef Bacik goto err; 596ea466794SJosef Bacik } 597ea466794SJosef Bacik 598ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 599727011e0SChris Mason if (found_start != eb->start) { 6007a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 601193f284dSChris Mason "%llu %llu\n", 602a1b32a59SChris Mason (unsigned long long)found_start, 603a1b32a59SChris Mason (unsigned long long)eb->start); 604f188591eSChris Mason ret = -EIO; 605ce9adaa5SChris Mason goto err; 606ce9adaa5SChris Mason } 6072b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 6087a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 609d397712bSChris Mason (unsigned long long)eb->start); 6101259ab75SChris Mason ret = -EIO; 6111259ab75SChris Mason goto err; 6121259ab75SChris Mason } 613ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 614ce9adaa5SChris Mason 61585d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 61685d4e461SChris Mason eb, found_level); 6174008c04aSChris Mason 618ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 619a826d6dcSJosef Bacik if (ret) { 620f188591eSChris Mason ret = -EIO; 621a826d6dcSJosef Bacik goto err; 622a826d6dcSJosef Bacik } 623a826d6dcSJosef Bacik 624a826d6dcSJosef Bacik /* 625a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 626a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 627a826d6dcSJosef Bacik * return -EIO. 628a826d6dcSJosef Bacik */ 629a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 630a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 631a826d6dcSJosef Bacik ret = -EIO; 632a826d6dcSJosef Bacik } 633ce9adaa5SChris Mason 6340b32f4bbSJosef Bacik if (!ret) 6350b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 636ce9adaa5SChris Mason err: 6374bb31e92SArne Jansen if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) { 6384bb31e92SArne Jansen clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags); 6394bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6404bb31e92SArne Jansen } 6414bb31e92SArne Jansen 6420b32f4bbSJosef Bacik if (ret) 6430b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 6440b32f4bbSJosef Bacik free_extent_buffer(eb); 645ce9adaa5SChris Mason out: 646f188591eSChris Mason return ret; 647ce9adaa5SChris Mason } 648ce9adaa5SChris Mason 649ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6504bb31e92SArne Jansen { 6514bb31e92SArne Jansen struct extent_buffer *eb; 6524bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6534bb31e92SArne Jansen 6544f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 655ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6565cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 657ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6584bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6594bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6604bb31e92SArne Jansen } 6614bb31e92SArne Jansen 662ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 663ce9adaa5SChris Mason { 664ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 665ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 666ce9adaa5SChris Mason 667ce9adaa5SChris Mason fs_info = end_io_wq->info; 668ce9adaa5SChris Mason end_io_wq->error = err; 6698b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6708b712842SChris Mason end_io_wq->work.flags = 0; 671d20f7043SChris Mason 6727b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 6730cb59c99SJosef Bacik if (end_io_wq->metadata == 1) 674cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 675cad321adSChris Mason &end_io_wq->work); 6760cb59c99SJosef Bacik else if (end_io_wq->metadata == 2) 6770cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 6780cb59c99SJosef Bacik &end_io_wq->work); 679cad321adSChris Mason else 680e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 681e6dcd2dcSChris Mason &end_io_wq->work); 682d20f7043SChris Mason } else { 683d20f7043SChris Mason if (end_io_wq->metadata) 684d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 685d20f7043SChris Mason &end_io_wq->work); 686e6dcd2dcSChris Mason else 687d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 688d20f7043SChris Mason &end_io_wq->work); 689d20f7043SChris Mason } 690ce9adaa5SChris Mason } 691ce9adaa5SChris Mason 6920cb59c99SJosef Bacik /* 6930cb59c99SJosef Bacik * For the metadata arg you want 6940cb59c99SJosef Bacik * 6950cb59c99SJosef Bacik * 0 - if data 6960cb59c99SJosef Bacik * 1 - if normal metadta 6970cb59c99SJosef Bacik * 2 - if writing to the free space cache area 6980cb59c99SJosef Bacik */ 69922c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 70022c59948SChris Mason int metadata) 7010b86a832SChris Mason { 702ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 703ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 704ce9adaa5SChris Mason if (!end_io_wq) 705ce9adaa5SChris Mason return -ENOMEM; 706ce9adaa5SChris Mason 707ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 708ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 70922c59948SChris Mason end_io_wq->info = info; 710ce9adaa5SChris Mason end_io_wq->error = 0; 711ce9adaa5SChris Mason end_io_wq->bio = bio; 71222c59948SChris Mason end_io_wq->metadata = metadata; 713ce9adaa5SChris Mason 714ce9adaa5SChris Mason bio->bi_private = end_io_wq; 715ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 71622c59948SChris Mason return 0; 71722c59948SChris Mason } 71822c59948SChris Mason 719b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7204854ddd0SChris Mason { 7214854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7224854ddd0SChris Mason info->workers.max_workers, 7234854ddd0SChris Mason info->fs_devices->open_devices); 7244854ddd0SChris Mason return 256 * limit; 7254854ddd0SChris Mason } 7264854ddd0SChris Mason 7274a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7284a69a410SChris Mason { 7294a69a410SChris Mason struct async_submit_bio *async; 73079787eaaSJeff Mahoney int ret; 7314a69a410SChris Mason 7324a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 73379787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 734eaf25d93SChris Mason async->mirror_num, async->bio_flags, 735eaf25d93SChris Mason async->bio_offset); 73679787eaaSJeff Mahoney if (ret) 73779787eaaSJeff Mahoney async->error = ret; 7384a69a410SChris Mason } 7394a69a410SChris Mason 7404a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7418b712842SChris Mason { 7428b712842SChris Mason struct btrfs_fs_info *fs_info; 7438b712842SChris Mason struct async_submit_bio *async; 7444854ddd0SChris Mason int limit; 7458b712842SChris Mason 7468b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7478b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7484854ddd0SChris Mason 749b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7504854ddd0SChris Mason limit = limit * 2 / 3; 7514854ddd0SChris Mason 75266657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 753b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7544854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7554854ddd0SChris Mason 75679787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 75779787eaaSJeff Mahoney if (async->error) { 75879787eaaSJeff Mahoney bio_endio(async->bio, async->error); 75979787eaaSJeff Mahoney return; 76079787eaaSJeff Mahoney } 76179787eaaSJeff Mahoney 7624a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 763eaf25d93SChris Mason async->mirror_num, async->bio_flags, 764eaf25d93SChris Mason async->bio_offset); 7654a69a410SChris Mason } 7664a69a410SChris Mason 7674a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7684a69a410SChris Mason { 7694a69a410SChris Mason struct async_submit_bio *async; 7704a69a410SChris Mason 7714a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7728b712842SChris Mason kfree(async); 7738b712842SChris Mason } 7748b712842SChris Mason 77544b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 77644b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 777c8b97818SChris Mason unsigned long bio_flags, 778eaf25d93SChris Mason u64 bio_offset, 7794a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7804a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 78144b8bd7eSChris Mason { 78244b8bd7eSChris Mason struct async_submit_bio *async; 78344b8bd7eSChris Mason 78444b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 78544b8bd7eSChris Mason if (!async) 78644b8bd7eSChris Mason return -ENOMEM; 78744b8bd7eSChris Mason 78844b8bd7eSChris Mason async->inode = inode; 78944b8bd7eSChris Mason async->rw = rw; 79044b8bd7eSChris Mason async->bio = bio; 79144b8bd7eSChris Mason async->mirror_num = mirror_num; 7924a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7934a69a410SChris Mason async->submit_bio_done = submit_bio_done; 7944a69a410SChris Mason 7954a69a410SChris Mason async->work.func = run_one_async_start; 7964a69a410SChris Mason async->work.ordered_func = run_one_async_done; 7974a69a410SChris Mason async->work.ordered_free = run_one_async_free; 7984a69a410SChris Mason 7998b712842SChris Mason async->work.flags = 0; 800c8b97818SChris Mason async->bio_flags = bio_flags; 801eaf25d93SChris Mason async->bio_offset = bio_offset; 8028c8bee1dSChris Mason 80379787eaaSJeff Mahoney async->error = 0; 80479787eaaSJeff Mahoney 805cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 806d313d7a3SChris Mason 8077b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 808d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 809d313d7a3SChris Mason 8108b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 8119473f16cSChris Mason 812771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 813771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 814771ed689SChris Mason wait_event(fs_info->async_submit_wait, 815771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 816771ed689SChris Mason } 817771ed689SChris Mason 81844b8bd7eSChris Mason return 0; 81944b8bd7eSChris Mason } 82044b8bd7eSChris Mason 821ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 822ce3ed71aSChris Mason { 823ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 824ce3ed71aSChris Mason int bio_index = 0; 825ce3ed71aSChris Mason struct btrfs_root *root; 82679787eaaSJeff Mahoney int ret = 0; 827ce3ed71aSChris Mason 828ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 829ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 830ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 83179787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 83279787eaaSJeff Mahoney if (ret) 83379787eaaSJeff Mahoney break; 834ce3ed71aSChris Mason bio_index++; 835ce3ed71aSChris Mason bvec++; 836ce3ed71aSChris Mason } 83779787eaaSJeff Mahoney return ret; 838ce3ed71aSChris Mason } 839ce3ed71aSChris Mason 8404a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8414a69a410SChris Mason struct bio *bio, int mirror_num, 842eaf25d93SChris Mason unsigned long bio_flags, 843eaf25d93SChris Mason u64 bio_offset) 84422c59948SChris Mason { 8458b712842SChris Mason /* 8468b712842SChris Mason * when we're called for a write, we're already in the async 8475443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8488b712842SChris Mason */ 84979787eaaSJeff Mahoney return btree_csum_one_bio(bio); 85022c59948SChris Mason } 85122c59948SChris Mason 8524a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 853eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 854eaf25d93SChris Mason u64 bio_offset) 8554a69a410SChris Mason { 85661891923SStefan Behrens int ret; 85761891923SStefan Behrens 8588b712842SChris Mason /* 8594a69a410SChris Mason * when we're called for a write, we're already in the async 8604a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8618b712842SChris Mason */ 86261891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 86361891923SStefan Behrens if (ret) 86461891923SStefan Behrens bio_endio(bio, ret); 86561891923SStefan Behrens return ret; 8660b86a832SChris Mason } 8670b86a832SChris Mason 868de0022b9SJosef Bacik static int check_async_write(struct inode *inode, unsigned long bio_flags) 869de0022b9SJosef Bacik { 870de0022b9SJosef Bacik if (bio_flags & EXTENT_BIO_TREE_LOG) 871de0022b9SJosef Bacik return 0; 872de0022b9SJosef Bacik #ifdef CONFIG_X86 873de0022b9SJosef Bacik if (cpu_has_xmm4_2) 874de0022b9SJosef Bacik return 0; 875de0022b9SJosef Bacik #endif 876de0022b9SJosef Bacik return 1; 877de0022b9SJosef Bacik } 878de0022b9SJosef Bacik 87944b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 880eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 881eaf25d93SChris Mason u64 bio_offset) 88244b8bd7eSChris Mason { 883de0022b9SJosef Bacik int async = check_async_write(inode, bio_flags); 8844a69a410SChris Mason int ret; 885cad321adSChris Mason 8867b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 887cad321adSChris Mason /* 888cad321adSChris Mason * called for a read, do the setup so that checksum validation 889cad321adSChris Mason * can happen in the async kernel threads 890cad321adSChris Mason */ 891f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 892f3f266abSChris Mason bio, 1); 8931d4284bdSChris Mason if (ret) 89461891923SStefan Behrens goto out_w_error; 89561891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 8966f3577bdSChris Mason mirror_num, 0); 897de0022b9SJosef Bacik } else if (!async) { 898de0022b9SJosef Bacik ret = btree_csum_one_bio(bio); 899de0022b9SJosef Bacik if (ret) 90061891923SStefan Behrens goto out_w_error; 90161891923SStefan Behrens ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 902de0022b9SJosef Bacik mirror_num, 0); 90361891923SStefan Behrens } else { 904cad321adSChris Mason /* 90561891923SStefan Behrens * kthread helpers are used to submit writes so that 90661891923SStefan Behrens * checksumming can happen in parallel across all CPUs 907cad321adSChris Mason */ 90861891923SStefan Behrens ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 909c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 910eaf25d93SChris Mason bio_offset, 9114a69a410SChris Mason __btree_submit_bio_start, 9124a69a410SChris Mason __btree_submit_bio_done); 91344b8bd7eSChris Mason } 91444b8bd7eSChris Mason 91561891923SStefan Behrens if (ret) { 91661891923SStefan Behrens out_w_error: 91761891923SStefan Behrens bio_endio(bio, ret); 91861891923SStefan Behrens } 91961891923SStefan Behrens return ret; 92061891923SStefan Behrens } 92161891923SStefan Behrens 9223dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 923784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 924a6bc32b8SMel Gorman struct page *newpage, struct page *page, 925a6bc32b8SMel Gorman enum migrate_mode mode) 926784b4e29SChris Mason { 927784b4e29SChris Mason /* 928784b4e29SChris Mason * we can't safely write a btree page from here, 929784b4e29SChris Mason * we haven't done the locking hook 930784b4e29SChris Mason */ 931784b4e29SChris Mason if (PageDirty(page)) 932784b4e29SChris Mason return -EAGAIN; 933784b4e29SChris Mason /* 934784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 935784b4e29SChris Mason * We must have no buffers or drop them. 936784b4e29SChris Mason */ 937784b4e29SChris Mason if (page_has_private(page) && 938784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 939784b4e29SChris Mason return -EAGAIN; 940a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 941784b4e29SChris Mason } 9423dd1462eSJan Beulich #endif 943784b4e29SChris Mason 9440da5468fSChris Mason 9450da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9460da5468fSChris Mason struct writeback_control *wbc) 9470da5468fSChris Mason { 948d1310b2eSChris Mason struct extent_io_tree *tree; 949e2d84521SMiao Xie struct btrfs_fs_info *fs_info; 950e2d84521SMiao Xie int ret; 951e2d84521SMiao Xie 952d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 953d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 954448d640bSChris Mason 955448d640bSChris Mason if (wbc->for_kupdate) 956448d640bSChris Mason return 0; 957448d640bSChris Mason 958e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info; 959b9473439SChris Mason /* this is a bit racy, but that's ok */ 960e2d84521SMiao Xie ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 961e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 962e2d84521SMiao Xie if (ret < 0) 963793955bcSChris Mason return 0; 964793955bcSChris Mason } 9650b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9660da5468fSChris Mason } 9670da5468fSChris Mason 968b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9695f39d397SChris Mason { 970d1310b2eSChris Mason struct extent_io_tree *tree; 971d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9728ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9735f39d397SChris Mason } 9745f39d397SChris Mason 97570dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9765f39d397SChris Mason { 97798509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 97898509cfcSChris Mason return 0; 9790c4e538bSDavid Sterba /* 9800c4e538bSDavid Sterba * We need to mask out eg. __GFP_HIGHMEM and __GFP_DMA32 as we're doing 9810c4e538bSDavid Sterba * slab allocation from alloc_extent_state down the callchain where 9820c4e538bSDavid Sterba * it'd hit a BUG_ON as those flags are not allowed. 9830c4e538bSDavid Sterba */ 9840c4e538bSDavid Sterba gfp_flags &= ~GFP_SLAB_BUG_MASK; 9850c4e538bSDavid Sterba 9863083ee2eSJosef Bacik return try_release_extent_buffer(page, gfp_flags); 987d98237b3SChris Mason } 988d98237b3SChris Mason 9895f39d397SChris Mason static void btree_invalidatepage(struct page *page, unsigned long offset) 990d98237b3SChris Mason { 991d1310b2eSChris Mason struct extent_io_tree *tree; 992d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9935f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9945f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9959ad6b7bcSChris Mason if (PagePrivate(page)) { 996d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 997d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 9989ad6b7bcSChris Mason ClearPagePrivate(page); 9999ad6b7bcSChris Mason set_page_private(page, 0); 10009ad6b7bcSChris Mason page_cache_release(page); 10019ad6b7bcSChris Mason } 1002d98237b3SChris Mason } 1003d98237b3SChris Mason 10040b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 10050b32f4bbSJosef Bacik { 1006bb146eb2SJosef Bacik #ifdef DEBUG 10070b32f4bbSJosef Bacik struct extent_buffer *eb; 10080b32f4bbSJosef Bacik 10090b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 10100b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 10110b32f4bbSJosef Bacik BUG_ON(!eb); 10120b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 10130b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 10140b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 1015bb146eb2SJosef Bacik #endif 10160b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 10170b32f4bbSJosef Bacik } 10180b32f4bbSJosef Bacik 10197f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1020d98237b3SChris Mason .readpage = btree_readpage, 10210da5468fSChris Mason .writepages = btree_writepages, 10225f39d397SChris Mason .releasepage = btree_releasepage, 10235f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10245a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1025784b4e29SChris Mason .migratepage = btree_migratepage, 10265a92bc88SChris Mason #endif 10270b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1028d98237b3SChris Mason }; 1029123abc88SChris Mason 1030ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1031ca7a79adSChris Mason u64 parent_transid) 1032090d1875SChris Mason { 10335f39d397SChris Mason struct extent_buffer *buf = NULL; 10345f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1035de428b63SChris Mason int ret = 0; 1036090d1875SChris Mason 1037db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10385f39d397SChris Mason if (!buf) 1039090d1875SChris Mason return 0; 1040d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1041bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10425f39d397SChris Mason free_extent_buffer(buf); 1043de428b63SChris Mason return ret; 1044090d1875SChris Mason } 1045090d1875SChris Mason 1046ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1047ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1048ab0fff03SArne Jansen { 1049ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1050ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1051ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1052ab0fff03SArne Jansen int ret; 1053ab0fff03SArne Jansen 1054ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1055ab0fff03SArne Jansen if (!buf) 1056ab0fff03SArne Jansen return 0; 1057ab0fff03SArne Jansen 1058ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1059ab0fff03SArne Jansen 1060ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1061ab0fff03SArne Jansen btree_get_extent, mirror_num); 1062ab0fff03SArne Jansen if (ret) { 1063ab0fff03SArne Jansen free_extent_buffer(buf); 1064ab0fff03SArne Jansen return ret; 1065ab0fff03SArne Jansen } 1066ab0fff03SArne Jansen 1067ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1068ab0fff03SArne Jansen free_extent_buffer(buf); 1069ab0fff03SArne Jansen return -EIO; 10700b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1071ab0fff03SArne Jansen *eb = buf; 1072ab0fff03SArne Jansen } else { 1073ab0fff03SArne Jansen free_extent_buffer(buf); 1074ab0fff03SArne Jansen } 1075ab0fff03SArne Jansen return 0; 1076ab0fff03SArne Jansen } 1077ab0fff03SArne Jansen 10780999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 10790999df54SChris Mason u64 bytenr, u32 blocksize) 10800999df54SChris Mason { 10810999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10820999df54SChris Mason struct extent_buffer *eb; 10830999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1084f09d1f60SDavid Sterba bytenr, blocksize); 10850999df54SChris Mason return eb; 10860999df54SChris Mason } 10870999df54SChris Mason 10880999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 10890999df54SChris Mason u64 bytenr, u32 blocksize) 10900999df54SChris Mason { 10910999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10920999df54SChris Mason struct extent_buffer *eb; 10930999df54SChris Mason 10940999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1095727011e0SChris Mason bytenr, blocksize); 10960999df54SChris Mason return eb; 10970999df54SChris Mason } 10980999df54SChris Mason 10990999df54SChris Mason 1100e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1101e02119d5SChris Mason { 1102727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 11038aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1104e02119d5SChris Mason } 1105e02119d5SChris Mason 1106e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1107e02119d5SChris Mason { 1108727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 11098aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1110e02119d5SChris Mason } 1111e02119d5SChris Mason 1112db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1113ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1114e20d96d6SChris Mason { 11155f39d397SChris Mason struct extent_buffer *buf = NULL; 111619c00ddcSChris Mason int ret; 111719c00ddcSChris Mason 1118db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11195f39d397SChris Mason if (!buf) 1120d98237b3SChris Mason return NULL; 1121e4204dedSChris Mason 1122ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11235f39d397SChris Mason return buf; 1124ce9adaa5SChris Mason 1125eb60ceacSChris Mason } 1126eb60ceacSChris Mason 1127d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11285f39d397SChris Mason struct extent_buffer *buf) 1129ed2ff2cbSChris Mason { 1130e2d84521SMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 1131e2d84521SMiao Xie 113255c69072SChris Mason if (btrfs_header_generation(buf) == 1133e2d84521SMiao Xie fs_info->running_transaction->transid) { 1134b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1135b4ce94deSChris Mason 1136b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1137e2d84521SMiao Xie __percpu_counter_add(&fs_info->dirty_metadata_bytes, 1138e2d84521SMiao Xie -buf->len, 1139e2d84521SMiao Xie fs_info->dirty_metadata_batch); 1140b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1141b9473439SChris Mason btrfs_set_lock_blocking(buf); 11420b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1143925baeddSChris Mason } 11445f39d397SChris Mason } 1145ed7b63ebSJosef Bacik } 11465f39d397SChris Mason 1147143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 114887ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11499f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1150e20d96d6SChris Mason u64 objectid) 1151d97e63b6SChris Mason { 1152cfaa7295SChris Mason root->node = NULL; 1153a28ec197SChris Mason root->commit_root = NULL; 1154db94535dSChris Mason root->sectorsize = sectorsize; 1155db94535dSChris Mason root->nodesize = nodesize; 1156db94535dSChris Mason root->leafsize = leafsize; 115787ee04ebSChris Mason root->stripesize = stripesize; 1158123abc88SChris Mason root->ref_cows = 0; 11590b86a832SChris Mason root->track_dirty = 0; 1160c71bf099SYan, Zheng root->in_radix = 0; 1161d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1162d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11630b86a832SChris Mason 11640f7d52f4SChris Mason root->objectid = objectid; 11650f7d52f4SChris Mason root->last_trans = 0; 116613a8a7c8SYan, Zheng root->highest_objectid = 0; 116758176a96SJosef Bacik root->name = NULL; 11686bef4d31SEric Paris root->inode_tree = RB_ROOT; 116916cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1170f0486c68SYan, Zheng root->block_rsv = NULL; 1171d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11720b86a832SChris Mason 11730b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11745d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 11752ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]); 11762ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]); 1177d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11785d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1179f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 11802ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]); 11812ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]); 1182a2135011SChris Mason mutex_init(&root->objectid_mutex); 1183e02119d5SChris Mason mutex_init(&root->log_mutex); 11847237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11857237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11867237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11877237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11887237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11897237f183SYan Zheng atomic_set(&root->log_writers, 0); 11902ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11918a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 11927237f183SYan Zheng root->log_transid = 0; 1193257c62e1SChris Mason root->last_log_commit = 0; 1194d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1195f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1196017e5369SChris Mason 11973768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11983768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11996702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 120058176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 12013f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 120258176a96SJosef Bacik init_completion(&root->kobj_unregister); 12036702ed49SChris Mason root->defrag_running = 0; 12044d775673SChris Mason root->root_key.objectid = objectid; 12050ee5dc67SAl Viro root->anon_dev = 0; 12068ea05e3aSAlexander Block 12075f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock); 12083768f368SChris Mason } 12093768f368SChris Mason 1210200a5c17SJeff Mahoney static int __must_check find_and_setup_root(struct btrfs_root *tree_root, 12119f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 12129f5fae2fSChris Mason u64 objectid, 1213e20d96d6SChris Mason struct btrfs_root *root) 12143768f368SChris Mason { 12153768f368SChris Mason int ret; 1216db94535dSChris Mason u32 blocksize; 121784234f3aSYan Zheng u64 generation; 12183768f368SChris Mason 1219db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 122087ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 122187ee04ebSChris Mason root, fs_info, objectid); 12223768f368SChris Mason ret = btrfs_find_last_root(tree_root, objectid, 12233768f368SChris Mason &root->root_item, &root->root_key); 12244df27c4dSYan, Zheng if (ret > 0) 12254df27c4dSYan, Zheng return -ENOENT; 1226200a5c17SJeff Mahoney else if (ret < 0) 1227200a5c17SJeff Mahoney return ret; 12283768f368SChris Mason 122984234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1230db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1231af31f5e5SChris Mason root->commit_root = NULL; 1232db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 123384234f3aSYan Zheng blocksize, generation); 1234b9fab919SChris Mason if (!root->node || !btrfs_buffer_uptodate(root->node, generation, 0)) { 123568433b73SChris Mason free_extent_buffer(root->node); 1236af31f5e5SChris Mason root->node = NULL; 123768433b73SChris Mason return -EIO; 123868433b73SChris Mason } 12394df27c4dSYan, Zheng root->commit_root = btrfs_root_node(root); 1240d97e63b6SChris Mason return 0; 1241d97e63b6SChris Mason } 1242d97e63b6SChris Mason 1243f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12446f07e42eSAl Viro { 12456f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12466f07e42eSAl Viro if (root) 12476f07e42eSAl Viro root->fs_info = fs_info; 12486f07e42eSAl Viro return root; 12496f07e42eSAl Viro } 12506f07e42eSAl Viro 125120897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 125220897f5cSArne Jansen struct btrfs_fs_info *fs_info, 125320897f5cSArne Jansen u64 objectid) 125420897f5cSArne Jansen { 125520897f5cSArne Jansen struct extent_buffer *leaf; 125620897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 125720897f5cSArne Jansen struct btrfs_root *root; 125820897f5cSArne Jansen struct btrfs_key key; 125920897f5cSArne Jansen int ret = 0; 126020897f5cSArne Jansen u64 bytenr; 126120897f5cSArne Jansen 126220897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 126320897f5cSArne Jansen if (!root) 126420897f5cSArne Jansen return ERR_PTR(-ENOMEM); 126520897f5cSArne Jansen 126620897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 126720897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 126820897f5cSArne Jansen root, fs_info, objectid); 126920897f5cSArne Jansen root->root_key.objectid = objectid; 127020897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 127120897f5cSArne Jansen root->root_key.offset = 0; 127220897f5cSArne Jansen 127320897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 127420897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 127520897f5cSArne Jansen if (IS_ERR(leaf)) { 127620897f5cSArne Jansen ret = PTR_ERR(leaf); 127720897f5cSArne Jansen goto fail; 127820897f5cSArne Jansen } 127920897f5cSArne Jansen 128020897f5cSArne Jansen bytenr = leaf->start; 128120897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 128220897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 128320897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 128420897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 128520897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 128620897f5cSArne Jansen root->node = leaf; 128720897f5cSArne Jansen 128820897f5cSArne Jansen write_extent_buffer(leaf, fs_info->fsid, 128920897f5cSArne Jansen (unsigned long)btrfs_header_fsid(leaf), 129020897f5cSArne Jansen BTRFS_FSID_SIZE); 129120897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 129220897f5cSArne Jansen (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 129320897f5cSArne Jansen BTRFS_UUID_SIZE); 129420897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 129520897f5cSArne Jansen 129620897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 129720897f5cSArne Jansen root->track_dirty = 1; 129820897f5cSArne Jansen 129920897f5cSArne Jansen 130020897f5cSArne Jansen root->root_item.flags = 0; 130120897f5cSArne Jansen root->root_item.byte_limit = 0; 130220897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 130320897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 130420897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 130520897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 130620897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 130720897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 130820897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 130920897f5cSArne Jansen root->root_item.drop_level = 0; 131020897f5cSArne Jansen 131120897f5cSArne Jansen key.objectid = objectid; 131220897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 131320897f5cSArne Jansen key.offset = 0; 131420897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 131520897f5cSArne Jansen if (ret) 131620897f5cSArne Jansen goto fail; 131720897f5cSArne Jansen 131820897f5cSArne Jansen btrfs_tree_unlock(leaf); 131920897f5cSArne Jansen 132020897f5cSArne Jansen fail: 132120897f5cSArne Jansen if (ret) 132220897f5cSArne Jansen return ERR_PTR(ret); 132320897f5cSArne Jansen 132420897f5cSArne Jansen return root; 132520897f5cSArne Jansen } 132620897f5cSArne Jansen 13277237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1328e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13290f7d52f4SChris Mason { 13300f7d52f4SChris Mason struct btrfs_root *root; 13310f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13327237f183SYan Zheng struct extent_buffer *leaf; 1333e02119d5SChris Mason 13346f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1335e02119d5SChris Mason if (!root) 13367237f183SYan Zheng return ERR_PTR(-ENOMEM); 1337e02119d5SChris Mason 1338e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1339e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1340e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1341e02119d5SChris Mason 1342e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1343e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1344e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13457237f183SYan Zheng /* 13467237f183SYan Zheng * log trees do not get reference counted because they go away 13477237f183SYan Zheng * before a real commit is actually done. They do store pointers 13487237f183SYan Zheng * to file data extents, and those reference counts still get 13497237f183SYan Zheng * updated (along with back refs to the log tree). 13507237f183SYan Zheng */ 1351e02119d5SChris Mason root->ref_cows = 0; 1352e02119d5SChris Mason 13535d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 135466d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13555581a51aSJan Schmidt 0, 0, 0); 13567237f183SYan Zheng if (IS_ERR(leaf)) { 13577237f183SYan Zheng kfree(root); 13587237f183SYan Zheng return ERR_CAST(leaf); 13597237f183SYan Zheng } 1360e02119d5SChris Mason 13615d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13625d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13635d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13645d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13655d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13667237f183SYan Zheng root->node = leaf; 1367e02119d5SChris Mason 1368e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1369e02119d5SChris Mason (unsigned long)btrfs_header_fsid(root->node), 1370e02119d5SChris Mason BTRFS_FSID_SIZE); 1371e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1372e02119d5SChris Mason btrfs_tree_unlock(root->node); 13737237f183SYan Zheng return root; 13747237f183SYan Zheng } 13757237f183SYan Zheng 13767237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13777237f183SYan Zheng struct btrfs_fs_info *fs_info) 13787237f183SYan Zheng { 13797237f183SYan Zheng struct btrfs_root *log_root; 13807237f183SYan Zheng 13817237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13827237f183SYan Zheng if (IS_ERR(log_root)) 13837237f183SYan Zheng return PTR_ERR(log_root); 13847237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13857237f183SYan Zheng fs_info->log_root_tree = log_root; 13867237f183SYan Zheng return 0; 13877237f183SYan Zheng } 13887237f183SYan Zheng 13897237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13907237f183SYan Zheng struct btrfs_root *root) 13917237f183SYan Zheng { 13927237f183SYan Zheng struct btrfs_root *log_root; 13937237f183SYan Zheng struct btrfs_inode_item *inode_item; 13947237f183SYan Zheng 13957237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 13967237f183SYan Zheng if (IS_ERR(log_root)) 13977237f183SYan Zheng return PTR_ERR(log_root); 13987237f183SYan Zheng 13997237f183SYan Zheng log_root->last_trans = trans->transid; 14007237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 14017237f183SYan Zheng 14027237f183SYan Zheng inode_item = &log_root->root_item.inode; 14037237f183SYan Zheng inode_item->generation = cpu_to_le64(1); 14047237f183SYan Zheng inode_item->size = cpu_to_le64(3); 14057237f183SYan Zheng inode_item->nlink = cpu_to_le32(1); 14067237f183SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 14077237f183SYan Zheng inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 14087237f183SYan Zheng 14095d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 14107237f183SYan Zheng 14117237f183SYan Zheng WARN_ON(root->log_root); 14127237f183SYan Zheng root->log_root = log_root; 14137237f183SYan Zheng root->log_transid = 0; 1414257c62e1SChris Mason root->last_log_commit = 0; 1415e02119d5SChris Mason return 0; 1416e02119d5SChris Mason } 1417e02119d5SChris Mason 1418e02119d5SChris Mason struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, 1419e02119d5SChris Mason struct btrfs_key *location) 1420e02119d5SChris Mason { 1421e02119d5SChris Mason struct btrfs_root *root; 1422e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14230f7d52f4SChris Mason struct btrfs_path *path; 14245f39d397SChris Mason struct extent_buffer *l; 142584234f3aSYan Zheng u64 generation; 1426db94535dSChris Mason u32 blocksize; 14270f7d52f4SChris Mason int ret = 0; 14288ea05e3aSAlexander Block int slot; 14290f7d52f4SChris Mason 14306f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 14310cf6c620SChris Mason if (!root) 14320f7d52f4SChris Mason return ERR_PTR(-ENOMEM); 14330f7d52f4SChris Mason if (location->offset == (u64)-1) { 1434db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 14350f7d52f4SChris Mason location->objectid, root); 14360f7d52f4SChris Mason if (ret) { 14370f7d52f4SChris Mason kfree(root); 14380f7d52f4SChris Mason return ERR_PTR(ret); 14390f7d52f4SChris Mason } 144013a8a7c8SYan, Zheng goto out; 14410f7d52f4SChris Mason } 14420f7d52f4SChris Mason 1443db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 144487ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 144587ee04ebSChris Mason root, fs_info, location->objectid); 14460f7d52f4SChris Mason 14470f7d52f4SChris Mason path = btrfs_alloc_path(); 1448db5b493aSTsutomu Itoh if (!path) { 1449db5b493aSTsutomu Itoh kfree(root); 1450db5b493aSTsutomu Itoh return ERR_PTR(-ENOMEM); 1451db5b493aSTsutomu Itoh } 14520f7d52f4SChris Mason ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 145313a8a7c8SYan, Zheng if (ret == 0) { 14545f39d397SChris Mason l = path->nodes[0]; 14558ea05e3aSAlexander Block slot = path->slots[0]; 14568ea05e3aSAlexander Block btrfs_read_root_item(tree_root, l, slot, &root->root_item); 145744b36eb2SYan Zheng memcpy(&root->root_key, location, sizeof(*location)); 145813a8a7c8SYan, Zheng } 14590f7d52f4SChris Mason btrfs_free_path(path); 14600f7d52f4SChris Mason if (ret) { 14615e540f77STsutomu Itoh kfree(root); 146213a8a7c8SYan, Zheng if (ret > 0) 146313a8a7c8SYan, Zheng ret = -ENOENT; 14640f7d52f4SChris Mason return ERR_PTR(ret); 14650f7d52f4SChris Mason } 146613a8a7c8SYan, Zheng 146784234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1468db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1469db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 147084234f3aSYan Zheng blocksize, generation); 14715d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 147279787eaaSJeff Mahoney BUG_ON(!root->node); /* -ENOMEM */ 147313a8a7c8SYan, Zheng out: 147408fe4db1SLi Zefan if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 14750f7d52f4SChris Mason root->ref_cows = 1; 147608fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 147708fe4db1SLi Zefan } 147813a8a7c8SYan, Zheng 14795eda7b5eSChris Mason return root; 14805eda7b5eSChris Mason } 14815eda7b5eSChris Mason 1482edbd8d4eSChris Mason struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 1483edbd8d4eSChris Mason struct btrfs_key *location) 14845eda7b5eSChris Mason { 14855eda7b5eSChris Mason struct btrfs_root *root; 14865eda7b5eSChris Mason int ret; 14875eda7b5eSChris Mason 1488edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1489edbd8d4eSChris Mason return fs_info->tree_root; 1490edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1491edbd8d4eSChris Mason return fs_info->extent_root; 14928f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 14938f18cf13SChris Mason return fs_info->chunk_root; 14948f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 14958f18cf13SChris Mason return fs_info->dev_root; 14960403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 14970403e47eSYan Zheng return fs_info->csum_root; 1498bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1499bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1500bcef60f2SArne Jansen ERR_PTR(-ENOENT); 15014df27c4dSYan, Zheng again: 15024df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 15035eda7b5eSChris Mason root = radix_tree_lookup(&fs_info->fs_roots_radix, 15045eda7b5eSChris Mason (unsigned long)location->objectid); 15054df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 15065eda7b5eSChris Mason if (root) 15075eda7b5eSChris Mason return root; 15085eda7b5eSChris Mason 1509e02119d5SChris Mason root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 15105eda7b5eSChris Mason if (IS_ERR(root)) 15115eda7b5eSChris Mason return root; 15123394e160SChris Mason 1513581bb050SLi Zefan root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1514581bb050SLi Zefan root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1515581bb050SLi Zefan GFP_NOFS); 151635a30d7cSDavid Sterba if (!root->free_ino_pinned || !root->free_ino_ctl) { 151735a30d7cSDavid Sterba ret = -ENOMEM; 1518581bb050SLi Zefan goto fail; 151935a30d7cSDavid Sterba } 1520581bb050SLi Zefan 1521581bb050SLi Zefan btrfs_init_free_ino_ctl(root); 1522581bb050SLi Zefan mutex_init(&root->fs_commit_mutex); 1523581bb050SLi Zefan spin_lock_init(&root->cache_lock); 1524581bb050SLi Zefan init_waitqueue_head(&root->cache_wait); 1525581bb050SLi Zefan 15260ee5dc67SAl Viro ret = get_anon_bdev(&root->anon_dev); 1527ac08aedfSChris Mason if (ret) 1528ac08aedfSChris Mason goto fail; 15293394e160SChris Mason 1530d68fc57bSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) { 1531d68fc57bSYan, Zheng ret = -ENOENT; 1532d68fc57bSYan, Zheng goto fail; 1533d68fc57bSYan, Zheng } 1534d68fc57bSYan, Zheng 1535d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1536d68fc57bSYan, Zheng if (ret < 0) 1537d68fc57bSYan, Zheng goto fail; 1538d68fc57bSYan, Zheng if (ret == 0) 1539d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1540d68fc57bSYan, Zheng 15414df27c4dSYan, Zheng ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 15424df27c4dSYan, Zheng if (ret) 15434df27c4dSYan, Zheng goto fail; 15444df27c4dSYan, Zheng 15454df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 15462619ba1fSChris Mason ret = radix_tree_insert(&fs_info->fs_roots_radix, 15472619ba1fSChris Mason (unsigned long)root->root_key.objectid, 15480f7d52f4SChris Mason root); 1549d68fc57bSYan, Zheng if (ret == 0) 15504df27c4dSYan, Zheng root->in_radix = 1; 1551d68fc57bSYan, Zheng 15524df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 15534df27c4dSYan, Zheng radix_tree_preload_end(); 15540f7d52f4SChris Mason if (ret) { 15554df27c4dSYan, Zheng if (ret == -EEXIST) { 15564df27c4dSYan, Zheng free_fs_root(root); 15574df27c4dSYan, Zheng goto again; 15580f7d52f4SChris Mason } 15594df27c4dSYan, Zheng goto fail; 15604df27c4dSYan, Zheng } 15614df27c4dSYan, Zheng 1562edbd8d4eSChris Mason ret = btrfs_find_dead_roots(fs_info->tree_root, 15635d4f98a2SYan Zheng root->root_key.objectid); 15644df27c4dSYan, Zheng WARN_ON(ret); 1565edbd8d4eSChris Mason return root; 15664df27c4dSYan, Zheng fail: 15674df27c4dSYan, Zheng free_fs_root(root); 15684df27c4dSYan, Zheng return ERR_PTR(ret); 1569edbd8d4eSChris Mason } 1570edbd8d4eSChris Mason 157104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 157204160088SChris Mason { 157304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 157404160088SChris Mason int ret = 0; 157504160088SChris Mason struct btrfs_device *device; 157604160088SChris Mason struct backing_dev_info *bdi; 1577b7967db7SChris Mason 15781f78160cSXiao Guangrong rcu_read_lock(); 15791f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1580dfe25020SChris Mason if (!device->bdev) 1581dfe25020SChris Mason continue; 158204160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 158304160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 158404160088SChris Mason ret = 1; 158504160088SChris Mason break; 158604160088SChris Mason } 158704160088SChris Mason } 15881f78160cSXiao Guangrong rcu_read_unlock(); 158904160088SChris Mason return ret; 159004160088SChris Mason } 159104160088SChris Mason 159238b66988SChris Mason /* 1593ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1594ad081f14SJens Axboe * bdi again. 1595ad081f14SJens Axboe */ 159604160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 159704160088SChris Mason { 1598ad081f14SJens Axboe int err; 1599ad081f14SJens Axboe 1600ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1601e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1602ad081f14SJens Axboe if (err) 1603ad081f14SJens Axboe return err; 1604ad081f14SJens Axboe 16054575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 160604160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 160704160088SChris Mason bdi->congested_data = info; 160804160088SChris Mason return 0; 160904160088SChris Mason } 161004160088SChris Mason 16118b712842SChris Mason /* 16128b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 16138b712842SChris Mason * functions. This is where read checksum verification actually happens 16148b712842SChris Mason */ 16158b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1616ce9adaa5SChris Mason { 1617ce9adaa5SChris Mason struct bio *bio; 16188b712842SChris Mason struct end_io_wq *end_io_wq; 16198b712842SChris Mason struct btrfs_fs_info *fs_info; 1620ce9adaa5SChris Mason int error; 1621ce9adaa5SChris Mason 16228b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1623ce9adaa5SChris Mason bio = end_io_wq->bio; 16248b712842SChris Mason fs_info = end_io_wq->info; 16258b712842SChris Mason 1626ce9adaa5SChris Mason error = end_io_wq->error; 1627ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1628ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1629ce9adaa5SChris Mason kfree(end_io_wq); 1630ce9adaa5SChris Mason bio_endio(bio, error); 1631ce9adaa5SChris Mason } 163244b8bd7eSChris Mason 1633a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1634a74a4b97SChris Mason { 1635a74a4b97SChris Mason struct btrfs_root *root = arg; 1636a74a4b97SChris Mason 1637a74a4b97SChris Mason do { 163876dda93cSYan, Zheng if (!(root->fs_info->sb->s_flags & MS_RDONLY) && 163976dda93cSYan, Zheng mutex_trylock(&root->fs_info->cleaner_mutex)) { 164024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 1641a74a4b97SChris Mason btrfs_clean_old_snapshots(root); 1642a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 16434cb5300bSChris Mason btrfs_run_defrag_inodes(root->fs_info); 164476dda93cSYan, Zheng } 1645a74a4b97SChris Mason 1646a0acae0eSTejun Heo if (!try_to_freeze()) { 1647a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16488929ecfaSYan, Zheng if (!kthread_should_stop()) 1649a74a4b97SChris Mason schedule(); 1650a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1651a74a4b97SChris Mason } 1652a74a4b97SChris Mason } while (!kthread_should_stop()); 1653a74a4b97SChris Mason return 0; 1654a74a4b97SChris Mason } 1655a74a4b97SChris Mason 1656a74a4b97SChris Mason static int transaction_kthread(void *arg) 1657a74a4b97SChris Mason { 1658a74a4b97SChris Mason struct btrfs_root *root = arg; 1659a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1660a74a4b97SChris Mason struct btrfs_transaction *cur; 16618929ecfaSYan, Zheng u64 transid; 1662a74a4b97SChris Mason unsigned long now; 1663a74a4b97SChris Mason unsigned long delay; 1664914b2007SJan Kara bool cannot_commit; 1665a74a4b97SChris Mason 1666a74a4b97SChris Mason do { 1667914b2007SJan Kara cannot_commit = false; 1668a74a4b97SChris Mason delay = HZ * 30; 1669a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1670a74a4b97SChris Mason 1671a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1672a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1673a74a4b97SChris Mason if (!cur) { 1674a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1675a74a4b97SChris Mason goto sleep; 1676a74a4b97SChris Mason } 167731153d81SYan Zheng 1678a74a4b97SChris Mason now = get_seconds(); 16798929ecfaSYan, Zheng if (!cur->blocked && 16808929ecfaSYan, Zheng (now < cur->start_time || now - cur->start_time < 30)) { 1681a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1682a74a4b97SChris Mason delay = HZ * 5; 1683a74a4b97SChris Mason goto sleep; 1684a74a4b97SChris Mason } 16858929ecfaSYan, Zheng transid = cur->transid; 1686a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 168756bec294SChris Mason 168879787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 1689354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1690914b2007SJan Kara if (IS_ERR(trans)) { 1691354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT) 1692914b2007SJan Kara cannot_commit = true; 169379787eaaSJeff Mahoney goto sleep; 1694914b2007SJan Kara } 16958929ecfaSYan, Zheng if (transid == trans->transid) { 169679787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 16978929ecfaSYan, Zheng } else { 16988929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 16998929ecfaSYan, Zheng } 1700a74a4b97SChris Mason sleep: 1701a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1702a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1703a74a4b97SChris Mason 1704a0acae0eSTejun Heo if (!try_to_freeze()) { 1705a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 17068929ecfaSYan, Zheng if (!kthread_should_stop() && 1707914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1708914b2007SJan Kara cannot_commit)) 1709a74a4b97SChris Mason schedule_timeout(delay); 1710a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1711a74a4b97SChris Mason } 1712a74a4b97SChris Mason } while (!kthread_should_stop()); 1713a74a4b97SChris Mason return 0; 1714a74a4b97SChris Mason } 1715a74a4b97SChris Mason 1716af31f5e5SChris Mason /* 1717af31f5e5SChris Mason * this will find the highest generation in the array of 1718af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1719af31f5e5SChris Mason * or -1 if we can't find anything. 1720af31f5e5SChris Mason * 1721af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1722af31f5e5SChris Mason * generation of the latest root in the array with the generation 1723af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1724af31f5e5SChris Mason */ 1725af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1726af31f5e5SChris Mason { 1727af31f5e5SChris Mason u64 cur; 1728af31f5e5SChris Mason int newest_index = -1; 1729af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1730af31f5e5SChris Mason int i; 1731af31f5e5SChris Mason 1732af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1733af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1734af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1735af31f5e5SChris Mason if (cur == newest_gen) 1736af31f5e5SChris Mason newest_index = i; 1737af31f5e5SChris Mason } 1738af31f5e5SChris Mason 1739af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1740af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1741af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1742af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1743af31f5e5SChris Mason if (cur == newest_gen) 1744af31f5e5SChris Mason newest_index = 0; 1745af31f5e5SChris Mason } 1746af31f5e5SChris Mason return newest_index; 1747af31f5e5SChris Mason } 1748af31f5e5SChris Mason 1749af31f5e5SChris Mason 1750af31f5e5SChris Mason /* 1751af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1752af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1753af31f5e5SChris Mason * field in the fs_info struct 1754af31f5e5SChris Mason */ 1755af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1756af31f5e5SChris Mason u64 newest_gen) 1757af31f5e5SChris Mason { 1758af31f5e5SChris Mason int newest_index = -1; 1759af31f5e5SChris Mason 1760af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1761af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1762af31f5e5SChris Mason if (newest_index == -1) { 1763af31f5e5SChris Mason info->backup_root_index = 0; 1764af31f5e5SChris Mason } else { 1765af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1766af31f5e5SChris Mason } 1767af31f5e5SChris Mason } 1768af31f5e5SChris Mason 1769af31f5e5SChris Mason /* 1770af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1771af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1772af31f5e5SChris Mason * done 1773af31f5e5SChris Mason */ 1774af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1775af31f5e5SChris Mason { 1776af31f5e5SChris Mason int next_backup; 1777af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1778af31f5e5SChris Mason int last_backup; 1779af31f5e5SChris Mason 1780af31f5e5SChris Mason next_backup = info->backup_root_index; 1781af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1782af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1783af31f5e5SChris Mason 1784af31f5e5SChris Mason /* 1785af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1786af31f5e5SChris Mason * this happens only at umount 1787af31f5e5SChris Mason */ 1788af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1789af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1790af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1791af31f5e5SChris Mason next_backup = last_backup; 1792af31f5e5SChris Mason 1793af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1794af31f5e5SChris Mason 1795af31f5e5SChris Mason /* 1796af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1797af31f5e5SChris Mason * regardless of which ones we use today 1798af31f5e5SChris Mason */ 1799af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1800af31f5e5SChris Mason 1801af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1802af31f5e5SChris Mason 1803af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1804af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1805af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1806af31f5e5SChris Mason 1807af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1808af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1809af31f5e5SChris Mason 1810af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1811af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1812af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1813af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1814af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1815af31f5e5SChris Mason 1816af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1817af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1818af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1819af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1820af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1821af31f5e5SChris Mason 18227c7e82a7SChris Mason /* 18237c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18247c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18257c7e82a7SChris Mason */ 18267c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18277c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18287c7e82a7SChris Mason info->fs_root->node->start); 1829af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1830af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1831af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1832af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18337c7e82a7SChris Mason } 1834af31f5e5SChris Mason 1835af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1836af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1837af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1838af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1839af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1840af31f5e5SChris Mason 1841af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1842af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1843af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1844af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1845af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1846af31f5e5SChris Mason 1847af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1848af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1849af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1850af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1851af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1852af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1853af31f5e5SChris Mason 1854af31f5e5SChris Mason /* 1855af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1856af31f5e5SChris Mason * for the next commit 1857af31f5e5SChris Mason */ 1858af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1859af31f5e5SChris Mason &info->super_for_commit->super_roots, 1860af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1861af31f5e5SChris Mason } 1862af31f5e5SChris Mason 1863af31f5e5SChris Mason /* 1864af31f5e5SChris Mason * this copies info out of the root backup array and back into 1865af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1866af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1867af31f5e5SChris Mason * tried and the last backup index you used. 1868af31f5e5SChris Mason * 1869af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1870af31f5e5SChris Mason */ 1871af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1872af31f5e5SChris Mason struct btrfs_super_block *super, 1873af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1874af31f5e5SChris Mason { 1875af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1876af31f5e5SChris Mason int newest = *backup_index; 1877af31f5e5SChris Mason 1878af31f5e5SChris Mason if (*num_backups_tried == 0) { 1879af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1880af31f5e5SChris Mason 1881af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1882af31f5e5SChris Mason if (newest == -1) 1883af31f5e5SChris Mason return -1; 1884af31f5e5SChris Mason 1885af31f5e5SChris Mason *backup_index = newest; 1886af31f5e5SChris Mason *num_backups_tried = 1; 1887af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1888af31f5e5SChris Mason /* we've tried all the backups, all done */ 1889af31f5e5SChris Mason return -1; 1890af31f5e5SChris Mason } else { 1891af31f5e5SChris Mason /* jump to the next oldest backup */ 1892af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1893af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1894af31f5e5SChris Mason *backup_index = newest; 1895af31f5e5SChris Mason *num_backups_tried += 1; 1896af31f5e5SChris Mason } 1897af31f5e5SChris Mason root_backup = super->super_roots + newest; 1898af31f5e5SChris Mason 1899af31f5e5SChris Mason btrfs_set_super_generation(super, 1900af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1901af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1902af31f5e5SChris Mason btrfs_set_super_root_level(super, 1903af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1904af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1905af31f5e5SChris Mason 1906af31f5e5SChris Mason /* 1907af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1908af31f5e5SChris Mason * need a fsck 1909af31f5e5SChris Mason */ 1910af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1911af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1912af31f5e5SChris Mason return 0; 1913af31f5e5SChris Mason } 1914af31f5e5SChris Mason 1915af31f5e5SChris Mason /* helper to cleanup tree roots */ 1916af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 1917af31f5e5SChris Mason { 1918af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 1919af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 1920af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 1921af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 1922af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 1923af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 1924af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 1925af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 1926bcef60f2SArne Jansen if (info->quota_root) { 1927bcef60f2SArne Jansen free_extent_buffer(info->quota_root->node); 1928bcef60f2SArne Jansen free_extent_buffer(info->quota_root->commit_root); 1929bcef60f2SArne Jansen } 1930af31f5e5SChris Mason 1931af31f5e5SChris Mason info->tree_root->node = NULL; 1932af31f5e5SChris Mason info->tree_root->commit_root = NULL; 1933af31f5e5SChris Mason info->dev_root->node = NULL; 1934af31f5e5SChris Mason info->dev_root->commit_root = NULL; 1935af31f5e5SChris Mason info->extent_root->node = NULL; 1936af31f5e5SChris Mason info->extent_root->commit_root = NULL; 1937af31f5e5SChris Mason info->csum_root->node = NULL; 1938af31f5e5SChris Mason info->csum_root->commit_root = NULL; 1939bcef60f2SArne Jansen if (info->quota_root) { 1940bcef60f2SArne Jansen info->quota_root->node = NULL; 1941bcef60f2SArne Jansen info->quota_root->commit_root = NULL; 1942bcef60f2SArne Jansen } 1943af31f5e5SChris Mason 1944af31f5e5SChris Mason if (chunk_root) { 1945af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 1946af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 1947af31f5e5SChris Mason info->chunk_root->node = NULL; 1948af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 1949af31f5e5SChris Mason } 1950af31f5e5SChris Mason } 1951af31f5e5SChris Mason 1952af31f5e5SChris Mason 1953ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 1954dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 1955dfe25020SChris Mason char *options) 1956eb60ceacSChris Mason { 1957db94535dSChris Mason u32 sectorsize; 1958db94535dSChris Mason u32 nodesize; 1959db94535dSChris Mason u32 leafsize; 1960db94535dSChris Mason u32 blocksize; 196187ee04ebSChris Mason u32 stripesize; 196284234f3aSYan Zheng u64 generation; 1963f2b636e8SJosef Bacik u64 features; 19643de4586cSChris Mason struct btrfs_key location; 1965a061fc8dSChris Mason struct buffer_head *bh; 19664d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 1967815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1968f84a8bd6SAl Viro struct btrfs_root *tree_root; 19694d34b278SIlya Dryomov struct btrfs_root *extent_root; 19704d34b278SIlya Dryomov struct btrfs_root *csum_root; 19714d34b278SIlya Dryomov struct btrfs_root *chunk_root; 19724d34b278SIlya Dryomov struct btrfs_root *dev_root; 1973bcef60f2SArne Jansen struct btrfs_root *quota_root; 1974e02119d5SChris Mason struct btrfs_root *log_tree_root; 1975eb60ceacSChris Mason int ret; 1976e58ca020SYan int err = -EINVAL; 1977af31f5e5SChris Mason int num_backups_tried = 0; 1978af31f5e5SChris Mason int backup_index = 0; 19794543df7eSChris Mason 1980f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 19816f07e42eSAl Viro extent_root = fs_info->extent_root = btrfs_alloc_root(fs_info); 19826f07e42eSAl Viro csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); 19836f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 19846f07e42eSAl Viro dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); 1985bcef60f2SArne Jansen quota_root = fs_info->quota_root = btrfs_alloc_root(fs_info); 19868790d502SChris Mason 1987f84a8bd6SAl Viro if (!tree_root || !extent_root || !csum_root || 1988bcef60f2SArne Jansen !chunk_root || !dev_root || !quota_root) { 198939279cc3SChris Mason err = -ENOMEM; 199039279cc3SChris Mason goto fail; 199139279cc3SChris Mason } 199276dda93cSYan, Zheng 199376dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 199476dda93cSYan, Zheng if (ret) { 199576dda93cSYan, Zheng err = ret; 199676dda93cSYan, Zheng goto fail; 199776dda93cSYan, Zheng } 199876dda93cSYan, Zheng 199976dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 200076dda93cSYan, Zheng if (ret) { 200176dda93cSYan, Zheng err = ret; 200276dda93cSYan, Zheng goto fail_srcu; 200376dda93cSYan, Zheng } 200476dda93cSYan, Zheng 2005e2d84521SMiao Xie ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0); 2006e2d84521SMiao Xie if (ret) { 2007e2d84521SMiao Xie err = ret; 2008e2d84521SMiao Xie goto fail_bdi; 2009e2d84521SMiao Xie } 2010e2d84521SMiao Xie fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE * 2011e2d84521SMiao Xie (1 + ilog2(nr_cpu_ids)); 2012e2d84521SMiao Xie 2013963d678bSMiao Xie ret = percpu_counter_init(&fs_info->delalloc_bytes, 0); 2014963d678bSMiao Xie if (ret) { 2015963d678bSMiao Xie err = ret; 2016963d678bSMiao Xie goto fail_dirty_metadata_bytes; 2017963d678bSMiao Xie } 2018963d678bSMiao Xie 201976dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 202076dda93cSYan, Zheng if (!fs_info->btree_inode) { 202176dda93cSYan, Zheng err = -ENOMEM; 2022963d678bSMiao Xie goto fail_delalloc_bytes; 202376dda93cSYan, Zheng } 202476dda93cSYan, Zheng 2025a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 20261561dedaSMiao Xie 202776dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 20288fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 2029facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 203024bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 2031ea8c2819SChris Mason INIT_LIST_HEAD(&fs_info->delalloc_inodes); 20325a3f23d5SChris Mason INIT_LIST_HEAD(&fs_info->ordered_operations); 203311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 20341832a6d5SChris Mason spin_lock_init(&fs_info->delalloc_lock); 2035a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 203676dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 203724bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 20384cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 20392bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2040f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2041f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 20427585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 2043de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock); 204419c00ddcSChris Mason 204558176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 20460b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 20476324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2048f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 20490b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 205066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 205166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 205266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 205366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 205466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 205566d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 205666d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 205766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 205866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2059cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2060771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 20618c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 20620986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 20634cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2064f29021b2SJan Schmidt atomic_set(&fs_info->tree_mod_seq, 0); 2065e20d96d6SChris Mason fs_info->sb = sb; 20666f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 20679ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 20684cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2069a4abeea4SJosef Bacik fs_info->trans_no_join = 0; 20702bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2071f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 2072c8b97818SChris Mason 207390519d66SArne Jansen /* readahead state */ 207490519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 207590519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 20762c90e5d6SChris Mason 2077b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2078b34b086cSChris Mason num_online_cpus() + 2, 8); 20790afbaf8cSChris Mason 20803eaa2885SChris Mason INIT_LIST_HEAD(&fs_info->ordered_extents); 20813eaa2885SChris Mason spin_lock_init(&fs_info->ordered_extent_lock); 208216cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 208316cdcec7SMiao Xie GFP_NOFS); 208416cdcec7SMiao Xie if (!fs_info->delayed_root) { 208516cdcec7SMiao Xie err = -ENOMEM; 208616cdcec7SMiao Xie goto fail_iput; 208716cdcec7SMiao Xie } 208816cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 20893eaa2885SChris Mason 2090a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2091a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2092a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2093a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2094a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2095a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2096a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2097a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 209821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 209921adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 210021adbd5cSStefan Behrens #endif 2101a2de733cSArne Jansen 2102c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2103c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2104837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2105837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2106a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2107c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2108837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2109a061fc8dSChris Mason 21100afbaf8cSChris Mason sb->s_blocksize = 4096; 21110afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 211232a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 21130afbaf8cSChris Mason 211476dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2115bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 21160afbaf8cSChris Mason /* 21170afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 21180afbaf8cSChris Mason * the real end of the address space is determined by all of 21190afbaf8cSChris Mason * the devices in the system 21200afbaf8cSChris Mason */ 21210afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2122d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 212304160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 212404160088SChris Mason 21255d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2126d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2127f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 21280b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2129a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 21300da5468fSChris Mason 2131d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2132d1310b2eSChris Mason 21330f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 21340f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 21350f7d52f4SChris Mason sizeof(struct btrfs_key)); 213672ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 213772ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 213822b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 213939279cc3SChris Mason 2140e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 21416bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2142a1897fddSLiu Bo fs_info->first_logical_byte = (u64)-1; 2143925baeddSChris Mason 214411833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2145f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 214611833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2147f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 214811833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2149e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2150f9295749SChris Mason 2151d98237b3SChris Mason 21525a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 215330ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2154925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2155925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2156a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2157a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2158276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2159c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 216076dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2161e922e087SStefan Behrens fs_info->dev_replace.lock_owner = 0; 2162e922e087SStefan Behrens atomic_set(&fs_info->dev_replace.nesting_level, 0); 2163e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount); 2164e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock_management_lock); 2165e922e087SStefan Behrens mutex_init(&fs_info->dev_replace.lock); 2166fa9c0d79SChris Mason 2167416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2168416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2169416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2170416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2171416ac51dSArne Jansen fs_info->quota_enabled = 0; 2172416ac51dSArne Jansen fs_info->pending_quota_state = 0; 2173416ac51dSArne Jansen 2174fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2175fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2176fa9c0d79SChris Mason 21777d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 21783768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2179bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 21804854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 21819a8dd150SChris Mason 21820b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 21832c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 21847eccb903SChris Mason 21853c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 2186a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 218720b45077SDave Young if (!bh) { 218820b45077SDave Young err = -EINVAL; 218916cdcec7SMiao Xie goto fail_alloc; 219020b45077SDave Young } 219139279cc3SChris Mason 21926c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 21936c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 21946c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2195a061fc8dSChris Mason brelse(bh); 21965f39d397SChris Mason 21976c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 21980b86a832SChris Mason 21996c41761fSDavid Sterba disk_super = fs_info->super_copy; 22000f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 220116cdcec7SMiao Xie goto fail_alloc; 22020f7d52f4SChris Mason 2203acce952bSliubo /* check FS state, whether FS is broken. */ 220487533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR) 220587533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 2206acce952bSliubo 2207fcd1f065SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 2208fcd1f065SDavid Sterba if (ret) { 2209fcd1f065SDavid Sterba printk(KERN_ERR "btrfs: superblock contains fatal errors\n"); 2210fcd1f065SDavid Sterba err = ret; 2211fcd1f065SDavid Sterba goto fail_alloc; 2212fcd1f065SDavid Sterba } 2213acce952bSliubo 221475e7cb7fSLiu Bo /* 2215af31f5e5SChris Mason * run through our array of backup supers and setup 2216af31f5e5SChris Mason * our ring pointer to the oldest one 2217af31f5e5SChris Mason */ 2218af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2219af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2220af31f5e5SChris Mason 2221af31f5e5SChris Mason /* 222275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 222375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 222475e7cb7fSLiu Bo */ 222575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 222675e7cb7fSLiu Bo 22272b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 22282b82032cSYan Zheng if (ret) { 22292b82032cSYan Zheng err = ret; 223016cdcec7SMiao Xie goto fail_alloc; 22312b82032cSYan Zheng } 2232dfe25020SChris Mason 2233f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2234f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2235f2b636e8SJosef Bacik if (features) { 2236f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2237f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 223821380931SJoel Becker (unsigned long long)features); 2239f2b636e8SJosef Bacik err = -EINVAL; 224016cdcec7SMiao Xie goto fail_alloc; 2241f2b636e8SJosef Bacik } 2242f2b636e8SJosef Bacik 2243727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2244727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2245727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2246727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2247727011e0SChris Mason btrfs_super_nodesize(disk_super), 2248727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2249727011e0SChris Mason err = -EINVAL; 2250727011e0SChris Mason goto fail_alloc; 2251727011e0SChris Mason } 2252727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2253727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2254727011e0SChris Mason "blocksize (%d) was too large\n", 2255727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2256727011e0SChris Mason err = -EINVAL; 2257727011e0SChris Mason goto fail_alloc; 2258727011e0SChris Mason } 2259727011e0SChris Mason 22605d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 22615d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 226269e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2263a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2264727011e0SChris Mason 2265727011e0SChris Mason /* 2266727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2267727011e0SChris Mason * they are bigger than the page size 2268727011e0SChris Mason */ 2269727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2270727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2271727011e0SChris Mason printk(KERN_INFO "btrfs flagging fs with big metadata feature\n"); 2272727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2273727011e0SChris Mason } 2274727011e0SChris Mason 2275bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2276bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2277bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2278bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2279e2d84521SMiao Xie fs_info->dirty_metadata_batch = leafsize * (1 + ilog2(nr_cpu_ids)); 2280963d678bSMiao Xie fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids)); 2281bc3f116fSChris Mason 2282bc3f116fSChris Mason /* 2283bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2284bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2285bc3f116fSChris Mason */ 2286bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2287bc3f116fSChris Mason (sectorsize != leafsize)) { 2288bc3f116fSChris Mason printk(KERN_WARNING "btrfs: unequal leaf/node/sector sizes " 2289bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2290bc3f116fSChris Mason sb->s_id); 2291bc3f116fSChris Mason goto fail_alloc; 2292bc3f116fSChris Mason } 2293bc3f116fSChris Mason 22945d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 22955d4f98a2SYan Zheng 2296f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2297f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2298f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2299f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2300f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 230121380931SJoel Becker (unsigned long long)features); 2302f2b636e8SJosef Bacik err = -EINVAL; 230316cdcec7SMiao Xie goto fail_alloc; 2304f2b636e8SJosef Bacik } 230561d92c32SChris Mason 230661d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 230761d92c32SChris Mason "genwork", 1, NULL); 230861d92c32SChris Mason 23095443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 231061d92c32SChris Mason fs_info->thread_pool_size, 231161d92c32SChris Mason &fs_info->generic_worker); 2312c8b97818SChris Mason 2313771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 231461d92c32SChris Mason fs_info->thread_pool_size, 231561d92c32SChris Mason &fs_info->generic_worker); 2316771ed689SChris Mason 23178ccf6f19SMiao Xie btrfs_init_workers(&fs_info->flush_workers, "flush_delalloc", 23188ccf6f19SMiao Xie fs_info->thread_pool_size, 23198ccf6f19SMiao Xie &fs_info->generic_worker); 23208ccf6f19SMiao Xie 23215443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2322b720d209SChris Mason min_t(u64, fs_devices->num_devices, 232361d92c32SChris Mason fs_info->thread_pool_size), 232461d92c32SChris Mason &fs_info->generic_worker); 232561b49440SChris Mason 2326bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 2327bab39bf9SJosef Bacik 2, &fs_info->generic_worker); 2328bab39bf9SJosef Bacik 232961b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 233061b49440SChris Mason * likely that bios will be send down in a sane order to the 233161b49440SChris Mason * devices 233261b49440SChris Mason */ 233361b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 233453863232SChris Mason 2335771ed689SChris Mason fs_info->workers.idle_thresh = 16; 23364a69a410SChris Mason fs_info->workers.ordered = 1; 233761b49440SChris Mason 2338771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2339771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2340771ed689SChris Mason 234161d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 234261d92c32SChris Mason &fs_info->generic_worker); 23435443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 234461d92c32SChris Mason fs_info->thread_pool_size, 234561d92c32SChris Mason &fs_info->generic_worker); 2346d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 234761d92c32SChris Mason fs_info->thread_pool_size, 234861d92c32SChris Mason &fs_info->generic_worker); 2349cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 235061d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 235161d92c32SChris Mason &fs_info->generic_worker); 23525443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 235361d92c32SChris Mason fs_info->thread_pool_size, 235461d92c32SChris Mason &fs_info->generic_worker); 23550cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 23560cb59c99SJosef Bacik 1, &fs_info->generic_worker); 235716cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 235816cdcec7SMiao Xie fs_info->thread_pool_size, 235916cdcec7SMiao Xie &fs_info->generic_worker); 236090519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 236190519d66SArne Jansen fs_info->thread_pool_size, 236290519d66SArne Jansen &fs_info->generic_worker); 236361b49440SChris Mason 236461b49440SChris Mason /* 236561b49440SChris Mason * endios are largely parallel and should have a very 236661b49440SChris Mason * low idle thresh 236761b49440SChris Mason */ 236861b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2369b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 2370b51912c9SChris Mason 23719042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 23729042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 237390519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 23749042846bSChris Mason 23750dc3b84aSJosef Bacik /* 23760dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 23770dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 23780dc3b84aSJosef Bacik */ 23790dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 23800dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 23810dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 23820dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 23830dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 23840dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 23850dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 23860dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 23870dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 23880dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 23890dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 23900dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 23910dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 23928ccf6f19SMiao Xie ret |= btrfs_start_workers(&fs_info->flush_workers); 23930dc3b84aSJosef Bacik if (ret) { 2394fed425c7SIlya Dryomov err = -ENOMEM; 23950dc3b84aSJosef Bacik goto fail_sb_buffer; 23960dc3b84aSJosef Bacik } 23974543df7eSChris Mason 23984575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2399c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2400c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 24014575c9ccSChris Mason 2402db94535dSChris Mason tree_root->nodesize = nodesize; 2403db94535dSChris Mason tree_root->leafsize = leafsize; 2404db94535dSChris Mason tree_root->sectorsize = sectorsize; 240587ee04ebSChris Mason tree_root->stripesize = stripesize; 2406a061fc8dSChris Mason 2407a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2408a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2409db94535dSChris Mason 241039279cc3SChris Mason if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, 241139279cc3SChris Mason sizeof(disk_super->magic))) { 2412d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 241339279cc3SChris Mason goto fail_sb_buffer; 241439279cc3SChris Mason } 241519c00ddcSChris Mason 24168d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 24178d082fb7SLiu Bo printk(KERN_WARNING "btrfs: Incompatible sector size(%lu) " 24188d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2419941b2ddfSKeith Mannthey goto fail_sb_buffer; 2420941b2ddfSKeith Mannthey } 2421941b2ddfSKeith Mannthey 2422925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2423e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2424925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 242584eed90fSChris Mason if (ret) { 2426d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2427d397712bSChris Mason "array on %s\n", sb->s_id); 24285d4f98a2SYan Zheng goto fail_sb_buffer; 242984eed90fSChris Mason } 24300b86a832SChris Mason 24310b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 24320b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 243384234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 24340b86a832SChris Mason 24350b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 24360b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 24370b86a832SChris Mason 24380b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 24390b86a832SChris Mason btrfs_super_chunk_root(disk_super), 244084234f3aSYan Zheng blocksize, generation); 244179787eaaSJeff Mahoney BUG_ON(!chunk_root->node); /* -ENOMEM */ 244283121942SDavid Woodhouse if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 244383121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 244483121942SDavid Woodhouse sb->s_id); 2445af31f5e5SChris Mason goto fail_tree_roots; 244683121942SDavid Woodhouse } 24475d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 24485d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 24490b86a832SChris Mason 2450e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2451e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 2452e17cade2SChris Mason BTRFS_UUID_SIZE); 2453e17cade2SChris Mason 24540b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 24552b82032cSYan Zheng if (ret) { 2456d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2457d397712bSChris Mason sb->s_id); 2458af31f5e5SChris Mason goto fail_tree_roots; 24592b82032cSYan Zheng } 24600b86a832SChris Mason 24618dabb742SStefan Behrens /* 24628dabb742SStefan Behrens * keep the device that is marked to be the target device for the 24638dabb742SStefan Behrens * dev_replace procedure 24648dabb742SStefan Behrens */ 24658dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 0); 2466dfe25020SChris Mason 2467a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2468a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2469a6b0d5c8SChris Mason sb->s_id); 2470a6b0d5c8SChris Mason goto fail_tree_roots; 2471a6b0d5c8SChris Mason } 2472a6b0d5c8SChris Mason 2473af31f5e5SChris Mason retry_root_backup: 2474db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2475db94535dSChris Mason btrfs_super_root_level(disk_super)); 247684234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 24770b86a832SChris Mason 2478e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2479db94535dSChris Mason btrfs_super_root(disk_super), 248084234f3aSYan Zheng blocksize, generation); 2481af31f5e5SChris Mason if (!tree_root->node || 2482af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 248383121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 248483121942SDavid Woodhouse sb->s_id); 2485af31f5e5SChris Mason 2486af31f5e5SChris Mason goto recovery_tree_root; 248783121942SDavid Woodhouse } 2488af31f5e5SChris Mason 24895d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 24905d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 2491db94535dSChris Mason 2492db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 2493e20d96d6SChris Mason BTRFS_EXTENT_TREE_OBJECTID, extent_root); 24940b86a832SChris Mason if (ret) 2495af31f5e5SChris Mason goto recovery_tree_root; 24960b86a832SChris Mason extent_root->track_dirty = 1; 24970b86a832SChris Mason 24980b86a832SChris Mason ret = find_and_setup_root(tree_root, fs_info, 24990b86a832SChris Mason BTRFS_DEV_TREE_OBJECTID, dev_root); 25000b86a832SChris Mason if (ret) 2501af31f5e5SChris Mason goto recovery_tree_root; 25025d4f98a2SYan Zheng dev_root->track_dirty = 1; 25033768f368SChris Mason 2504d20f7043SChris Mason ret = find_and_setup_root(tree_root, fs_info, 2505d20f7043SChris Mason BTRFS_CSUM_TREE_OBJECTID, csum_root); 2506d20f7043SChris Mason if (ret) 2507af31f5e5SChris Mason goto recovery_tree_root; 2508d20f7043SChris Mason csum_root->track_dirty = 1; 2509d20f7043SChris Mason 2510bcef60f2SArne Jansen ret = find_and_setup_root(tree_root, fs_info, 2511bcef60f2SArne Jansen BTRFS_QUOTA_TREE_OBJECTID, quota_root); 2512bcef60f2SArne Jansen if (ret) { 2513bcef60f2SArne Jansen kfree(quota_root); 2514bcef60f2SArne Jansen quota_root = fs_info->quota_root = NULL; 2515bcef60f2SArne Jansen } else { 2516bcef60f2SArne Jansen quota_root->track_dirty = 1; 2517bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2518bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2519bcef60f2SArne Jansen } 2520bcef60f2SArne Jansen 25218929ecfaSYan, Zheng fs_info->generation = generation; 25228929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 25238929ecfaSYan, Zheng 252468310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 252568310a5eSIlya Dryomov if (ret) { 252668310a5eSIlya Dryomov printk(KERN_WARNING "btrfs: failed to recover balance\n"); 252768310a5eSIlya Dryomov goto fail_block_groups; 252868310a5eSIlya Dryomov } 252968310a5eSIlya Dryomov 2530733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2531733f4fbbSStefan Behrens if (ret) { 2532733f4fbbSStefan Behrens printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2533733f4fbbSStefan Behrens ret); 2534733f4fbbSStefan Behrens goto fail_block_groups; 2535733f4fbbSStefan Behrens } 2536733f4fbbSStefan Behrens 25378dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info); 25388dabb742SStefan Behrens if (ret) { 25398dabb742SStefan Behrens pr_err("btrfs: failed to init dev_replace: %d\n", ret); 25408dabb742SStefan Behrens goto fail_block_groups; 25418dabb742SStefan Behrens } 25428dabb742SStefan Behrens 25438dabb742SStefan Behrens btrfs_close_extra_devices(fs_info, fs_devices, 1); 25448dabb742SStefan Behrens 2545c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2546c59021f8Sliubo if (ret) { 2547c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2548c59021f8Sliubo goto fail_block_groups; 2549c59021f8Sliubo } 2550c59021f8Sliubo 25511b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 25521b1d1f66SJosef Bacik if (ret) { 25531b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 25541b1d1f66SJosef Bacik goto fail_block_groups; 25551b1d1f66SJosef Bacik } 25565af3e8ccSStefan Behrens fs_info->num_tolerated_disk_barrier_failures = 25575af3e8ccSStefan Behrens btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); 2558292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 2559292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 2560292fd7fcSStefan Behrens !(sb->s_flags & MS_RDONLY)) { 2561292fd7fcSStefan Behrens printk(KERN_WARNING 2562292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable mount is not allowed\n"); 2563292fd7fcSStefan Behrens goto fail_block_groups; 2564292fd7fcSStefan Behrens } 25659078a3e1SChris Mason 2566a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2567a74a4b97SChris Mason "btrfs-cleaner"); 256857506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 25691b1d1f66SJosef Bacik goto fail_block_groups; 2570a74a4b97SChris Mason 2571a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2572a74a4b97SChris Mason tree_root, 2573a74a4b97SChris Mason "btrfs-transaction"); 257457506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 25753f157a2fSChris Mason goto fail_cleaner; 2576a74a4b97SChris Mason 2577c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2578c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2579c289811cSChris Mason !fs_info->fs_devices->rotating) { 2580c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2581c289811cSChris Mason "mode\n"); 2582c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2583c289811cSChris Mason } 2584c289811cSChris Mason 258521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 258621adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 258721adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 258821adbd5cSStefan Behrens btrfs_test_opt(tree_root, 258921adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 259021adbd5cSStefan Behrens 1 : 0, 259121adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 259221adbd5cSStefan Behrens if (ret) 259321adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 259421adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 259521adbd5cSStefan Behrens } 259621adbd5cSStefan Behrens #endif 2597bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2598bcef60f2SArne Jansen if (ret) 2599bcef60f2SArne Jansen goto fail_trans_kthread; 260021adbd5cSStefan Behrens 2601acce952bSliubo /* do not make disk changes in broken FS */ 260268ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2603e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2604d18a2c44SChris Mason 26057c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2606d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2607d397712bSChris Mason "on RO media\n"); 26087c2ca468SChris Mason err = -EIO; 2609bcef60f2SArne Jansen goto fail_qgroup; 26107c2ca468SChris Mason } 2611e02119d5SChris Mason blocksize = 2612e02119d5SChris Mason btrfs_level_size(tree_root, 2613e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2614e02119d5SChris Mason 26156f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2616676e4c86SDan Carpenter if (!log_tree_root) { 2617676e4c86SDan Carpenter err = -ENOMEM; 2618bcef60f2SArne Jansen goto fail_qgroup; 2619676e4c86SDan Carpenter } 2620e02119d5SChris Mason 2621e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2622e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2623e02119d5SChris Mason 2624e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 262584234f3aSYan Zheng blocksize, 262684234f3aSYan Zheng generation + 1); 262779787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2628e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 262979787eaaSJeff Mahoney if (ret) { 263079787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 263179787eaaSJeff Mahoney "Failed to recover log tree"); 263279787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 263379787eaaSJeff Mahoney kfree(log_tree_root); 263479787eaaSJeff Mahoney goto fail_trans_kthread; 263579787eaaSJeff Mahoney } 2636e556ce2cSYan Zheng 2637e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2638e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 263979787eaaSJeff Mahoney if (ret) 264079787eaaSJeff Mahoney goto fail_trans_kthread; 2641e556ce2cSYan Zheng } 2642e02119d5SChris Mason } 26431a40e23bSZheng Yan 264476dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 264579787eaaSJeff Mahoney if (ret) 264679787eaaSJeff Mahoney goto fail_trans_kthread; 264776dda93cSYan, Zheng 26487c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2649d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 265044c44af2SIlya Dryomov if (ret) 265144c44af2SIlya Dryomov goto fail_trans_kthread; 2652d68fc57bSYan, Zheng 26535d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2654d7ce5843SMiao Xie if (ret < 0) { 2655d7ce5843SMiao Xie printk(KERN_WARNING 2656d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2657d7ce5843SMiao Xie err = -EINVAL; 2658bcef60f2SArne Jansen goto fail_qgroup; 2659d7ce5843SMiao Xie } 26607c2ca468SChris Mason } 26611a40e23bSZheng Yan 26623de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 26633de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 26643de4586cSChris Mason location.offset = (u64)-1; 26653de4586cSChris Mason 26663de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 26673de4586cSChris Mason if (!fs_info->fs_root) 2668bcef60f2SArne Jansen goto fail_qgroup; 26693140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 26703140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2671bcef60f2SArne Jansen goto fail_qgroup; 26723140c9a3SDan Carpenter } 2673c289811cSChris Mason 26742b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 26752b6ba629SIlya Dryomov return 0; 26762b6ba629SIlya Dryomov 2677e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 26782b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 26792b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 26802b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 26812b6ba629SIlya Dryomov close_ctree(tree_root); 26822b6ba629SIlya Dryomov return ret; 26832b6ba629SIlya Dryomov } 2684e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 268559641015SIlya Dryomov 26862b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 26872b6ba629SIlya Dryomov if (ret) { 26882b6ba629SIlya Dryomov printk(KERN_WARNING "btrfs: failed to resume balance\n"); 268966b4ffd1SJosef Bacik close_ctree(tree_root); 26902b6ba629SIlya Dryomov return ret; 2691e3acc2a6SJosef Bacik } 2692e3acc2a6SJosef Bacik 26938dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 26948dabb742SStefan Behrens if (ret) { 26958dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 26968dabb742SStefan Behrens close_ctree(tree_root); 26978dabb742SStefan Behrens return ret; 26988dabb742SStefan Behrens } 26998dabb742SStefan Behrens 2700ad2b2c80SAl Viro return 0; 270139279cc3SChris Mason 2702bcef60f2SArne Jansen fail_qgroup: 2703bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 27047c2ca468SChris Mason fail_trans_kthread: 27057c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 27063f157a2fSChris Mason fail_cleaner: 2707a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 27087c2ca468SChris Mason 27097c2ca468SChris Mason /* 27107c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 27117c2ca468SChris Mason * kthreads 27127c2ca468SChris Mason */ 27137c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 27147c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 27157c2ca468SChris Mason 27161b1d1f66SJosef Bacik fail_block_groups: 27171b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2718af31f5e5SChris Mason 2719af31f5e5SChris Mason fail_tree_roots: 2720af31f5e5SChris Mason free_root_pointers(fs_info, 1); 2721af31f5e5SChris Mason 272239279cc3SChris Mason fail_sb_buffer: 272361d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 2724306c8b68SChris Mason btrfs_stop_workers(&fs_info->readahead_workers); 2725247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 2726771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 27278b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 27288b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 2729d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 2730cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2731e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 27320cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 27331cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 273416cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 2735bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 27368ccf6f19SMiao Xie btrfs_stop_workers(&fs_info->flush_workers); 273716cdcec7SMiao Xie fail_alloc: 27384543df7eSChris Mason fail_iput: 2739586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2740586e46e2SIlya Dryomov 27417c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 27424543df7eSChris Mason iput(fs_info->btree_inode); 2743963d678bSMiao Xie fail_delalloc_bytes: 2744963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 2745e2d84521SMiao Xie fail_dirty_metadata_bytes: 2746e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 2747ad081f14SJens Axboe fail_bdi: 27487e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 274976dda93cSYan, Zheng fail_srcu: 275076dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 27517e662854SQinghuang Feng fail: 2752586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2753ad2b2c80SAl Viro return err; 2754af31f5e5SChris Mason 2755af31f5e5SChris Mason recovery_tree_root: 2756af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2757af31f5e5SChris Mason goto fail_tree_roots; 2758af31f5e5SChris Mason 2759af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2760af31f5e5SChris Mason 2761af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2762af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2763af31f5e5SChris Mason 2764af31f5e5SChris Mason /* we can't trust the free space cache either */ 2765af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2766af31f5e5SChris Mason 2767af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2768af31f5e5SChris Mason &num_backups_tried, &backup_index); 2769af31f5e5SChris Mason if (ret == -1) 2770af31f5e5SChris Mason goto fail_block_groups; 2771af31f5e5SChris Mason goto retry_root_backup; 2772eb60ceacSChris Mason } 2773eb60ceacSChris Mason 2774f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2775f2984462SChris Mason { 2776f2984462SChris Mason if (uptodate) { 2777f2984462SChris Mason set_buffer_uptodate(bh); 2778f2984462SChris Mason } else { 2779442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 2780442a4f63SStefan Behrens bh->b_private; 2781442a4f63SStefan Behrens 2782606686eeSJosef Bacik printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to " 2783606686eeSJosef Bacik "I/O error on %s\n", 2784606686eeSJosef Bacik rcu_str_deref(device->name)); 27851259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 27861259ab75SChris Mason * our own ways of dealing with the IO errors 27871259ab75SChris Mason */ 2788f2984462SChris Mason clear_buffer_uptodate(bh); 2789442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 2790f2984462SChris Mason } 2791f2984462SChris Mason unlock_buffer(bh); 2792f2984462SChris Mason put_bh(bh); 2793f2984462SChris Mason } 2794f2984462SChris Mason 2795a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 2796a512bbf8SYan Zheng { 2797a512bbf8SYan Zheng struct buffer_head *bh; 2798a512bbf8SYan Zheng struct buffer_head *latest = NULL; 2799a512bbf8SYan Zheng struct btrfs_super_block *super; 2800a512bbf8SYan Zheng int i; 2801a512bbf8SYan Zheng u64 transid = 0; 2802a512bbf8SYan Zheng u64 bytenr; 2803a512bbf8SYan Zheng 2804a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 2805a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 2806a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 2807a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 2808a512bbf8SYan Zheng */ 2809a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 2810a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2811a512bbf8SYan Zheng if (bytenr + 4096 >= i_size_read(bdev->bd_inode)) 2812a512bbf8SYan Zheng break; 2813a512bbf8SYan Zheng bh = __bread(bdev, bytenr / 4096, 4096); 2814a512bbf8SYan Zheng if (!bh) 2815a512bbf8SYan Zheng continue; 2816a512bbf8SYan Zheng 2817a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 2818a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 2819a512bbf8SYan Zheng strncmp((char *)(&super->magic), BTRFS_MAGIC, 2820a512bbf8SYan Zheng sizeof(super->magic))) { 2821a512bbf8SYan Zheng brelse(bh); 2822a512bbf8SYan Zheng continue; 2823a512bbf8SYan Zheng } 2824a512bbf8SYan Zheng 2825a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 2826a512bbf8SYan Zheng brelse(latest); 2827a512bbf8SYan Zheng latest = bh; 2828a512bbf8SYan Zheng transid = btrfs_super_generation(super); 2829a512bbf8SYan Zheng } else { 2830a512bbf8SYan Zheng brelse(bh); 2831a512bbf8SYan Zheng } 2832a512bbf8SYan Zheng } 2833a512bbf8SYan Zheng return latest; 2834a512bbf8SYan Zheng } 2835a512bbf8SYan Zheng 28364eedeb75SHisashi Hifumi /* 28374eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 28384eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 28394eedeb75SHisashi Hifumi * we write are pinned. 28404eedeb75SHisashi Hifumi * 28414eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 28424eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 28434eedeb75SHisashi Hifumi * many supers on this one device should be written. 28444eedeb75SHisashi Hifumi * 28454eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 28464eedeb75SHisashi Hifumi */ 2847a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 2848a512bbf8SYan Zheng struct btrfs_super_block *sb, 2849a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 2850a512bbf8SYan Zheng { 2851a512bbf8SYan Zheng struct buffer_head *bh; 2852a512bbf8SYan Zheng int i; 2853a512bbf8SYan Zheng int ret; 2854a512bbf8SYan Zheng int errors = 0; 2855a512bbf8SYan Zheng u32 crc; 2856a512bbf8SYan Zheng u64 bytenr; 2857a512bbf8SYan Zheng 2858a512bbf8SYan Zheng if (max_mirrors == 0) 2859a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 2860a512bbf8SYan Zheng 2861a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 2862a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2863a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 2864a512bbf8SYan Zheng break; 2865a512bbf8SYan Zheng 2866a512bbf8SYan Zheng if (wait) { 2867a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 2868a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2869a512bbf8SYan Zheng BUG_ON(!bh); 2870a512bbf8SYan Zheng wait_on_buffer(bh); 28714eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 28724eedeb75SHisashi Hifumi errors++; 28734eedeb75SHisashi Hifumi 28744eedeb75SHisashi Hifumi /* drop our reference */ 28754eedeb75SHisashi Hifumi brelse(bh); 28764eedeb75SHisashi Hifumi 28774eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 2878a512bbf8SYan Zheng brelse(bh); 2879a512bbf8SYan Zheng continue; 2880a512bbf8SYan Zheng } else { 2881a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 2882a512bbf8SYan Zheng 2883a512bbf8SYan Zheng crc = ~(u32)0; 2884a512bbf8SYan Zheng crc = btrfs_csum_data(NULL, (char *)sb + 2885a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 2886a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 2887a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 2888a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 2889a512bbf8SYan Zheng 28904eedeb75SHisashi Hifumi /* 28914eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 28924eedeb75SHisashi Hifumi * caller 28934eedeb75SHisashi Hifumi */ 2894a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 2895a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2896a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 2897a512bbf8SYan Zheng 28984eedeb75SHisashi Hifumi /* one reference for submit_bh */ 2899a512bbf8SYan Zheng get_bh(bh); 29004eedeb75SHisashi Hifumi 29014eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 2902a512bbf8SYan Zheng lock_buffer(bh); 2903a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 2904442a4f63SStefan Behrens bh->b_private = device; 2905a512bbf8SYan Zheng } 2906a512bbf8SYan Zheng 2907387125fcSChris Mason /* 2908387125fcSChris Mason * we fua the first super. The others we allow 2909387125fcSChris Mason * to go down lazy. 2910387125fcSChris Mason */ 291121adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 29124eedeb75SHisashi Hifumi if (ret) 2913a512bbf8SYan Zheng errors++; 2914a512bbf8SYan Zheng } 2915a512bbf8SYan Zheng return errors < i ? 0 : -1; 2916a512bbf8SYan Zheng } 2917a512bbf8SYan Zheng 2918387125fcSChris Mason /* 2919387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 2920387125fcSChris Mason * for the barrier when it is done 2921387125fcSChris Mason */ 2922387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 2923387125fcSChris Mason { 2924387125fcSChris Mason if (err) { 2925387125fcSChris Mason if (err == -EOPNOTSUPP) 2926387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 2927387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 2928387125fcSChris Mason } 2929387125fcSChris Mason if (bio->bi_private) 2930387125fcSChris Mason complete(bio->bi_private); 2931387125fcSChris Mason bio_put(bio); 2932387125fcSChris Mason } 2933387125fcSChris Mason 2934387125fcSChris Mason /* 2935387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 2936387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 2937387125fcSChris Mason * 2938387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 2939387125fcSChris Mason * capable 2940387125fcSChris Mason */ 2941387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 2942387125fcSChris Mason { 2943387125fcSChris Mason struct bio *bio; 2944387125fcSChris Mason int ret = 0; 2945387125fcSChris Mason 2946387125fcSChris Mason if (device->nobarriers) 2947387125fcSChris Mason return 0; 2948387125fcSChris Mason 2949387125fcSChris Mason if (wait) { 2950387125fcSChris Mason bio = device->flush_bio; 2951387125fcSChris Mason if (!bio) 2952387125fcSChris Mason return 0; 2953387125fcSChris Mason 2954387125fcSChris Mason wait_for_completion(&device->flush_wait); 2955387125fcSChris Mason 2956387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 2957606686eeSJosef Bacik printk_in_rcu("btrfs: disabling barriers on dev %s\n", 2958606686eeSJosef Bacik rcu_str_deref(device->name)); 2959387125fcSChris Mason device->nobarriers = 1; 29605af3e8ccSStefan Behrens } else if (!bio_flagged(bio, BIO_UPTODATE)) { 2961387125fcSChris Mason ret = -EIO; 2962442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 2963442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 2964387125fcSChris Mason } 2965387125fcSChris Mason 2966387125fcSChris Mason /* drop the reference from the wait == 0 run */ 2967387125fcSChris Mason bio_put(bio); 2968387125fcSChris Mason device->flush_bio = NULL; 2969387125fcSChris Mason 2970387125fcSChris Mason return ret; 2971387125fcSChris Mason } 2972387125fcSChris Mason 2973387125fcSChris Mason /* 2974387125fcSChris Mason * one reference for us, and we leave it for the 2975387125fcSChris Mason * caller 2976387125fcSChris Mason */ 29779c017abcSJesper Juhl device->flush_bio = NULL; 2978387125fcSChris Mason bio = bio_alloc(GFP_NOFS, 0); 2979387125fcSChris Mason if (!bio) 2980387125fcSChris Mason return -ENOMEM; 2981387125fcSChris Mason 2982387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 2983387125fcSChris Mason bio->bi_bdev = device->bdev; 2984387125fcSChris Mason init_completion(&device->flush_wait); 2985387125fcSChris Mason bio->bi_private = &device->flush_wait; 2986387125fcSChris Mason device->flush_bio = bio; 2987387125fcSChris Mason 2988387125fcSChris Mason bio_get(bio); 298921adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 2990387125fcSChris Mason 2991387125fcSChris Mason return 0; 2992387125fcSChris Mason } 2993387125fcSChris Mason 2994387125fcSChris Mason /* 2995387125fcSChris Mason * send an empty flush down to each device in parallel, 2996387125fcSChris Mason * then wait for them 2997387125fcSChris Mason */ 2998387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 2999387125fcSChris Mason { 3000387125fcSChris Mason struct list_head *head; 3001387125fcSChris Mason struct btrfs_device *dev; 30025af3e8ccSStefan Behrens int errors_send = 0; 30035af3e8ccSStefan Behrens int errors_wait = 0; 3004387125fcSChris Mason int ret; 3005387125fcSChris Mason 3006387125fcSChris Mason /* send down all the barriers */ 3007387125fcSChris Mason head = &info->fs_devices->devices; 3008387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3009387125fcSChris Mason if (!dev->bdev) { 30105af3e8ccSStefan Behrens errors_send++; 3011387125fcSChris Mason continue; 3012387125fcSChris Mason } 3013387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3014387125fcSChris Mason continue; 3015387125fcSChris Mason 3016387125fcSChris Mason ret = write_dev_flush(dev, 0); 3017387125fcSChris Mason if (ret) 30185af3e8ccSStefan Behrens errors_send++; 3019387125fcSChris Mason } 3020387125fcSChris Mason 3021387125fcSChris Mason /* wait for all the barriers */ 3022387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 3023387125fcSChris Mason if (!dev->bdev) { 30245af3e8ccSStefan Behrens errors_wait++; 3025387125fcSChris Mason continue; 3026387125fcSChris Mason } 3027387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 3028387125fcSChris Mason continue; 3029387125fcSChris Mason 3030387125fcSChris Mason ret = write_dev_flush(dev, 1); 3031387125fcSChris Mason if (ret) 30325af3e8ccSStefan Behrens errors_wait++; 3033387125fcSChris Mason } 30345af3e8ccSStefan Behrens if (errors_send > info->num_tolerated_disk_barrier_failures || 30355af3e8ccSStefan Behrens errors_wait > info->num_tolerated_disk_barrier_failures) 3036387125fcSChris Mason return -EIO; 3037387125fcSChris Mason return 0; 3038387125fcSChris Mason } 3039387125fcSChris Mason 30405af3e8ccSStefan Behrens int btrfs_calc_num_tolerated_disk_barrier_failures( 30415af3e8ccSStefan Behrens struct btrfs_fs_info *fs_info) 30425af3e8ccSStefan Behrens { 30435af3e8ccSStefan Behrens struct btrfs_ioctl_space_info space; 30445af3e8ccSStefan Behrens struct btrfs_space_info *sinfo; 30455af3e8ccSStefan Behrens u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 30465af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_SYSTEM, 30475af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_METADATA, 30485af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 30495af3e8ccSStefan Behrens int num_types = 4; 30505af3e8ccSStefan Behrens int i; 30515af3e8ccSStefan Behrens int c; 30525af3e8ccSStefan Behrens int num_tolerated_disk_barrier_failures = 30535af3e8ccSStefan Behrens (int)fs_info->fs_devices->num_devices; 30545af3e8ccSStefan Behrens 30555af3e8ccSStefan Behrens for (i = 0; i < num_types; i++) { 30565af3e8ccSStefan Behrens struct btrfs_space_info *tmp; 30575af3e8ccSStefan Behrens 30585af3e8ccSStefan Behrens sinfo = NULL; 30595af3e8ccSStefan Behrens rcu_read_lock(); 30605af3e8ccSStefan Behrens list_for_each_entry_rcu(tmp, &fs_info->space_info, list) { 30615af3e8ccSStefan Behrens if (tmp->flags == types[i]) { 30625af3e8ccSStefan Behrens sinfo = tmp; 30635af3e8ccSStefan Behrens break; 30645af3e8ccSStefan Behrens } 30655af3e8ccSStefan Behrens } 30665af3e8ccSStefan Behrens rcu_read_unlock(); 30675af3e8ccSStefan Behrens 30685af3e8ccSStefan Behrens if (!sinfo) 30695af3e8ccSStefan Behrens continue; 30705af3e8ccSStefan Behrens 30715af3e8ccSStefan Behrens down_read(&sinfo->groups_sem); 30725af3e8ccSStefan Behrens for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 30735af3e8ccSStefan Behrens if (!list_empty(&sinfo->block_groups[c])) { 30745af3e8ccSStefan Behrens u64 flags; 30755af3e8ccSStefan Behrens 30765af3e8ccSStefan Behrens btrfs_get_block_group_info( 30775af3e8ccSStefan Behrens &sinfo->block_groups[c], &space); 30785af3e8ccSStefan Behrens if (space.total_bytes == 0 || 30795af3e8ccSStefan Behrens space.used_bytes == 0) 30805af3e8ccSStefan Behrens continue; 30815af3e8ccSStefan Behrens flags = space.flags; 30825af3e8ccSStefan Behrens /* 30835af3e8ccSStefan Behrens * return 30845af3e8ccSStefan Behrens * 0: if dup, single or RAID0 is configured for 30855af3e8ccSStefan Behrens * any of metadata, system or data, else 30865af3e8ccSStefan Behrens * 1: if RAID5 is configured, or if RAID1 or 30875af3e8ccSStefan Behrens * RAID10 is configured and only two mirrors 30885af3e8ccSStefan Behrens * are used, else 30895af3e8ccSStefan Behrens * 2: if RAID6 is configured, else 30905af3e8ccSStefan Behrens * num_mirrors - 1: if RAID1 or RAID10 is 30915af3e8ccSStefan Behrens * configured and more than 30925af3e8ccSStefan Behrens * 2 mirrors are used. 30935af3e8ccSStefan Behrens */ 30945af3e8ccSStefan Behrens if (num_tolerated_disk_barrier_failures > 0 && 30955af3e8ccSStefan Behrens ((flags & (BTRFS_BLOCK_GROUP_DUP | 30965af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID0)) || 30975af3e8ccSStefan Behrens ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) 30985af3e8ccSStefan Behrens == 0))) 30995af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 0; 31005af3e8ccSStefan Behrens else if (num_tolerated_disk_barrier_failures > 1 31015af3e8ccSStefan Behrens && 31025af3e8ccSStefan Behrens (flags & (BTRFS_BLOCK_GROUP_RAID1 | 31035af3e8ccSStefan Behrens BTRFS_BLOCK_GROUP_RAID10))) 31045af3e8ccSStefan Behrens num_tolerated_disk_barrier_failures = 1; 31055af3e8ccSStefan Behrens } 31065af3e8ccSStefan Behrens } 31075af3e8ccSStefan Behrens up_read(&sinfo->groups_sem); 31085af3e8ccSStefan Behrens } 31095af3e8ccSStefan Behrens 31105af3e8ccSStefan Behrens return num_tolerated_disk_barrier_failures; 31115af3e8ccSStefan Behrens } 31125af3e8ccSStefan Behrens 3113a512bbf8SYan Zheng int write_all_supers(struct btrfs_root *root, int max_mirrors) 3114f2984462SChris Mason { 3115e5e9a520SChris Mason struct list_head *head; 3116f2984462SChris Mason struct btrfs_device *dev; 3117a061fc8dSChris Mason struct btrfs_super_block *sb; 3118f2984462SChris Mason struct btrfs_dev_item *dev_item; 3119f2984462SChris Mason int ret; 3120f2984462SChris Mason int do_barriers; 3121a236aed1SChris Mason int max_errors; 3122a236aed1SChris Mason int total_errors = 0; 3123a061fc8dSChris Mason u64 flags; 3124f2984462SChris Mason 31256c41761fSDavid Sterba max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3126f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 3127af31f5e5SChris Mason backup_super_roots(root->fs_info); 3128f2984462SChris Mason 31296c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 3130a061fc8dSChris Mason dev_item = &sb->dev_item; 3131e5e9a520SChris Mason 3132174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3133e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 3134387125fcSChris Mason 31355af3e8ccSStefan Behrens if (do_barriers) { 31365af3e8ccSStefan Behrens ret = barrier_all_devices(root->fs_info); 31375af3e8ccSStefan Behrens if (ret) { 31385af3e8ccSStefan Behrens mutex_unlock( 31395af3e8ccSStefan Behrens &root->fs_info->fs_devices->device_list_mutex); 31405af3e8ccSStefan Behrens btrfs_error(root->fs_info, ret, 31415af3e8ccSStefan Behrens "errors while submitting device barriers."); 31425af3e8ccSStefan Behrens return ret; 31435af3e8ccSStefan Behrens } 31445af3e8ccSStefan Behrens } 3145387125fcSChris Mason 31461f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3147dfe25020SChris Mason if (!dev->bdev) { 3148dfe25020SChris Mason total_errors++; 3149dfe25020SChris Mason continue; 3150dfe25020SChris Mason } 31512b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3152dfe25020SChris Mason continue; 3153dfe25020SChris Mason 31542b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 3155a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 3156a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 3157a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 3158a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 3159a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 3160a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3161a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3162a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 31632b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3164a512bbf8SYan Zheng 3165a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3166a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3167f2984462SChris Mason 3168a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3169a236aed1SChris Mason if (ret) 3170a236aed1SChris Mason total_errors++; 3171f2984462SChris Mason } 3172a236aed1SChris Mason if (total_errors > max_errors) { 3173d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 3174d397712bSChris Mason total_errors); 317579787eaaSJeff Mahoney 317679787eaaSJeff Mahoney /* This shouldn't happen. FUA is masked off if unsupported */ 3177a236aed1SChris Mason BUG(); 3178a236aed1SChris Mason } 3179f2984462SChris Mason 3180a512bbf8SYan Zheng total_errors = 0; 31811f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3182dfe25020SChris Mason if (!dev->bdev) 3183dfe25020SChris Mason continue; 31842b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3185dfe25020SChris Mason continue; 3186dfe25020SChris Mason 3187a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3188a512bbf8SYan Zheng if (ret) 31891259ab75SChris Mason total_errors++; 3190f2984462SChris Mason } 3191174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3192a236aed1SChris Mason if (total_errors > max_errors) { 319379787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 319479787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 319579787eaaSJeff Mahoney return -EIO; 3196a236aed1SChris Mason } 3197f2984462SChris Mason return 0; 3198f2984462SChris Mason } 3199f2984462SChris Mason 3200a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3201a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3202cfaa7295SChris Mason { 3203e66f709bSChris Mason int ret; 32042c90e5d6SChris Mason 3205a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 32065f39d397SChris Mason return ret; 3207cfaa7295SChris Mason } 3208cfaa7295SChris Mason 3209143bede5SJeff Mahoney void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 32102619ba1fSChris Mason { 32114df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 32122619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 32132619ba1fSChris Mason (unsigned long)root->root_key.objectid); 32144df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 321576dda93cSYan, Zheng 321676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 321776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 321876dda93cSYan, Zheng 3219581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3220581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 32214df27c4dSYan, Zheng free_fs_root(root); 32224df27c4dSYan, Zheng } 32234df27c4dSYan, Zheng 32244df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 32254df27c4dSYan, Zheng { 322682d5902dSLi Zefan iput(root->cache_inode); 32274df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 32280ee5dc67SAl Viro if (root->anon_dev) 32290ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 32305f39d397SChris Mason free_extent_buffer(root->node); 32315f39d397SChris Mason free_extent_buffer(root->commit_root); 3232581bb050SLi Zefan kfree(root->free_ino_ctl); 3233581bb050SLi Zefan kfree(root->free_ino_pinned); 323458176a96SJosef Bacik kfree(root->name); 32352619ba1fSChris Mason kfree(root); 32362619ba1fSChris Mason } 32372619ba1fSChris Mason 3238143bede5SJeff Mahoney static void del_fs_roots(struct btrfs_fs_info *fs_info) 32390f7d52f4SChris Mason { 32400f7d52f4SChris Mason int ret; 32410f7d52f4SChris Mason struct btrfs_root *gang[8]; 32420f7d52f4SChris Mason int i; 32430f7d52f4SChris Mason 324476dda93cSYan, Zheng while (!list_empty(&fs_info->dead_roots)) { 324576dda93cSYan, Zheng gang[0] = list_entry(fs_info->dead_roots.next, 324676dda93cSYan, Zheng struct btrfs_root, root_list); 324776dda93cSYan, Zheng list_del(&gang[0]->root_list); 324876dda93cSYan, Zheng 324976dda93cSYan, Zheng if (gang[0]->in_radix) { 325076dda93cSYan, Zheng btrfs_free_fs_root(fs_info, gang[0]); 325176dda93cSYan, Zheng } else { 325276dda93cSYan, Zheng free_extent_buffer(gang[0]->node); 325376dda93cSYan, Zheng free_extent_buffer(gang[0]->commit_root); 325476dda93cSYan, Zheng kfree(gang[0]); 325576dda93cSYan, Zheng } 325676dda93cSYan, Zheng } 325776dda93cSYan, Zheng 32580f7d52f4SChris Mason while (1) { 32590f7d52f4SChris Mason ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 32600f7d52f4SChris Mason (void **)gang, 0, 32610f7d52f4SChris Mason ARRAY_SIZE(gang)); 32620f7d52f4SChris Mason if (!ret) 32630f7d52f4SChris Mason break; 32642619ba1fSChris Mason for (i = 0; i < ret; i++) 32655eda7b5eSChris Mason btrfs_free_fs_root(fs_info, gang[i]); 32660f7d52f4SChris Mason } 32670f7d52f4SChris Mason } 3268b4100d64SChris Mason 3269c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3270c146afadSYan Zheng { 3271c146afadSYan Zheng u64 root_objectid = 0; 3272c146afadSYan Zheng struct btrfs_root *gang[8]; 3273c146afadSYan Zheng int i; 3274c146afadSYan Zheng int ret; 3275c146afadSYan Zheng 3276c146afadSYan Zheng while (1) { 3277c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3278c146afadSYan Zheng (void **)gang, root_objectid, 3279c146afadSYan Zheng ARRAY_SIZE(gang)); 3280c146afadSYan Zheng if (!ret) 3281c146afadSYan Zheng break; 32825d4f98a2SYan Zheng 32835d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3284c146afadSYan Zheng for (i = 0; i < ret; i++) { 328566b4ffd1SJosef Bacik int err; 328666b4ffd1SJosef Bacik 3287c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 328866b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 328966b4ffd1SJosef Bacik if (err) 329066b4ffd1SJosef Bacik return err; 3291c146afadSYan Zheng } 3292c146afadSYan Zheng root_objectid++; 3293c146afadSYan Zheng } 3294c146afadSYan Zheng return 0; 3295c146afadSYan Zheng } 3296c146afadSYan Zheng 3297c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3298c146afadSYan Zheng { 3299c146afadSYan Zheng struct btrfs_trans_handle *trans; 3300c146afadSYan Zheng int ret; 3301c146afadSYan Zheng 3302c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 330324bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3304c146afadSYan Zheng btrfs_clean_old_snapshots(root); 3305c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 3306c71bf099SYan, Zheng 3307c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3308c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3309c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3310c71bf099SYan, Zheng 33117a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 33123612b495STsutomu Itoh if (IS_ERR(trans)) 33133612b495STsutomu Itoh return PTR_ERR(trans); 3314c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 331579787eaaSJeff Mahoney if (ret) 331679787eaaSJeff Mahoney return ret; 3317c146afadSYan Zheng /* run commit again to drop the original snapshot */ 33187a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 33193612b495STsutomu Itoh if (IS_ERR(trans)) 33203612b495STsutomu Itoh return PTR_ERR(trans); 332179787eaaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 332279787eaaSJeff Mahoney if (ret) 332379787eaaSJeff Mahoney return ret; 3324c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 332579787eaaSJeff Mahoney if (ret) { 332679787eaaSJeff Mahoney btrfs_error(root->fs_info, ret, 332779787eaaSJeff Mahoney "Failed to sync btree inode to disk."); 332879787eaaSJeff Mahoney return ret; 332979787eaaSJeff Mahoney } 3330c146afadSYan Zheng 3331a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3332c146afadSYan Zheng return ret; 3333c146afadSYan Zheng } 3334c146afadSYan Zheng 3335e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3336eb60ceacSChris Mason { 33370f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3338c146afadSYan Zheng int ret; 3339e089f05cSChris Mason 3340facda1e7SChris Mason fs_info->closing = 1; 3341a2135011SChris Mason smp_mb(); 3342a2135011SChris Mason 3343837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3344aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info); 3345837d5b6eSIlya Dryomov 33468dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 33478dabb742SStefan Behrens 3348aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info); 33494cb5300bSChris Mason 33504cb5300bSChris Mason /* wait for any defraggers to finish */ 33514cb5300bSChris Mason wait_event(fs_info->transaction_wait, 33524cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 33534cb5300bSChris Mason 33544cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 335526176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 33564cb5300bSChris Mason 3357c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3358c146afadSYan Zheng ret = btrfs_commit_super(root); 3359d397712bSChris Mason if (ret) 3360d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3361c146afadSYan Zheng } 3362ed2ff2cbSChris Mason 336387533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 336468ce9682SStefan Behrens btrfs_error_commit_super(root); 3365acce952bSliubo 3366300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3367300e4f8aSJosef Bacik 3368e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3369e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 33708929ecfaSYan, Zheng 3371f25784b3SYan Zheng fs_info->closing = 2; 3372f25784b3SYan Zheng smp_mb(); 3373f25784b3SYan Zheng 3374bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3375bcef60f2SArne Jansen 3376963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) { 3377963d678bSMiao Xie printk(KERN_INFO "btrfs: at unmount delalloc count %lld\n", 3378963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes)); 3379b0c68f8bSChris Mason } 338031153d81SYan Zheng 33815f39d397SChris Mason free_extent_buffer(fs_info->extent_root->node); 33825d4f98a2SYan Zheng free_extent_buffer(fs_info->extent_root->commit_root); 33835f39d397SChris Mason free_extent_buffer(fs_info->tree_root->node); 33845d4f98a2SYan Zheng free_extent_buffer(fs_info->tree_root->commit_root); 3385e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->node); 3386e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->commit_root); 3387e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->node); 3388e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->commit_root); 3389e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->node); 3390e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->commit_root); 3391bcef60f2SArne Jansen if (fs_info->quota_root) { 3392bcef60f2SArne Jansen free_extent_buffer(fs_info->quota_root->node); 3393bcef60f2SArne Jansen free_extent_buffer(fs_info->quota_root->commit_root); 3394bcef60f2SArne Jansen } 3395d20f7043SChris Mason 3396e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3397c146afadSYan Zheng 33980f7d52f4SChris Mason del_fs_roots(fs_info); 3399d10c5f31SChris Mason 3400c146afadSYan Zheng iput(fs_info->btree_inode); 34019ad6b7bcSChris Mason 340261d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 3403247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 3404771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 34058b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 34068b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 3407d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 3408cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 3409e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 34100cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 34111cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 341216cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 3413bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 341490519d66SArne Jansen btrfs_stop_workers(&fs_info->readahead_workers); 34158ccf6f19SMiao Xie btrfs_stop_workers(&fs_info->flush_workers); 3416d6bfde87SChris Mason 341721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 341821adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 341921adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 342021adbd5cSStefan Behrens #endif 342121adbd5cSStefan Behrens 3422dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 34230b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3424b248a415SChris Mason 3425e2d84521SMiao Xie percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 3426963d678bSMiao Xie percpu_counter_destroy(&fs_info->delalloc_bytes); 342704160088SChris Mason bdi_destroy(&fs_info->bdi); 342876dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 34290b86a832SChris Mason 3430eb60ceacSChris Mason return 0; 3431eb60ceacSChris Mason } 3432eb60ceacSChris Mason 3433b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3434b9fab919SChris Mason int atomic) 3435ccd467d6SChris Mason { 34361259ab75SChris Mason int ret; 3437727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 34381259ab75SChris Mason 34390b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 34401259ab75SChris Mason if (!ret) 34411259ab75SChris Mason return ret; 34421259ab75SChris Mason 34431259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3444b9fab919SChris Mason parent_transid, atomic); 3445b9fab919SChris Mason if (ret == -EAGAIN) 3446b9fab919SChris Mason return ret; 34471259ab75SChris Mason return !ret; 34485f39d397SChris Mason } 34496702ed49SChris Mason 34505f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 34515f39d397SChris Mason { 34520b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 34535f39d397SChris Mason } 34545f39d397SChris Mason 34555f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 34565f39d397SChris Mason { 3457727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 34585f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3459b9473439SChris Mason int was_dirty; 3460b4ce94deSChris Mason 3461b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 346231b1a2bdSJulia Lawall if (transid != root->fs_info->generation) 346331b1a2bdSJulia Lawall WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, " 3464d397712bSChris Mason "found %llu running %llu\n", 3465db94535dSChris Mason (unsigned long long)buf->start, 3466d397712bSChris Mason (unsigned long long)transid, 3467d397712bSChris Mason (unsigned long long)root->fs_info->generation); 34680b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3469e2d84521SMiao Xie if (!was_dirty) 3470e2d84521SMiao Xie __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 3471e2d84521SMiao Xie buf->len, 3472e2d84521SMiao Xie root->fs_info->dirty_metadata_batch); 3473eb60ceacSChris Mason } 3474eb60ceacSChris Mason 3475b53d3f5dSLiu Bo static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 3476b53d3f5dSLiu Bo int flush_delayed) 347735b7e476SChris Mason { 3478188de649SChris Mason /* 3479188de649SChris Mason * looks as though older kernels can get into trouble with 3480188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3481188de649SChris Mason */ 3482e2d84521SMiao Xie int ret; 3483d6bfde87SChris Mason 34846933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3485d6bfde87SChris Mason return; 3486d6bfde87SChris Mason 3487b53d3f5dSLiu Bo if (flush_delayed) 348816cdcec7SMiao Xie btrfs_balance_delayed_items(root); 348916cdcec7SMiao Xie 3490e2d84521SMiao Xie ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 3491e2d84521SMiao Xie BTRFS_DIRTY_METADATA_THRESH); 3492e2d84521SMiao Xie if (ret > 0) { 349316cdcec7SMiao Xie balance_dirty_pages_ratelimited_nr( 349416cdcec7SMiao Xie root->fs_info->btree_inode->i_mapping, 1); 349516cdcec7SMiao Xie } 349616cdcec7SMiao Xie return; 349716cdcec7SMiao Xie } 349816cdcec7SMiao Xie 3499b53d3f5dSLiu Bo void btrfs_btree_balance_dirty(struct btrfs_root *root) 350016cdcec7SMiao Xie { 3501b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 1); 350235b7e476SChris Mason } 3503b53d3f5dSLiu Bo 3504b53d3f5dSLiu Bo void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 3505b53d3f5dSLiu Bo { 3506b53d3f5dSLiu Bo __btrfs_btree_balance_dirty(root, 0); 3507d6bfde87SChris Mason } 35086b80053dSChris Mason 3509ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 35106b80053dSChris Mason { 3511727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 35120b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 35136b80053dSChris Mason } 35140da5468fSChris Mason 3515fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3516acce952bSliubo int read_only) 3517acce952bSliubo { 3518fcd1f065SDavid Sterba if (btrfs_super_csum_type(fs_info->super_copy) >= ARRAY_SIZE(btrfs_csum_sizes)) { 3519fcd1f065SDavid Sterba printk(KERN_ERR "btrfs: unsupported checksum algorithm\n"); 3520fcd1f065SDavid Sterba return -EINVAL; 3521fcd1f065SDavid Sterba } 3522acce952bSliubo 3523fcd1f065SDavid Sterba if (read_only) 3524fcd1f065SDavid Sterba return 0; 3525fcd1f065SDavid Sterba 3526fcd1f065SDavid Sterba return 0; 3527fcd1f065SDavid Sterba } 3528fcd1f065SDavid Sterba 352968ce9682SStefan Behrens void btrfs_error_commit_super(struct btrfs_root *root) 3530acce952bSliubo { 3531acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3532acce952bSliubo btrfs_run_delayed_iputs(root); 3533acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3534acce952bSliubo 3535acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3536acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3537acce952bSliubo 3538acce952bSliubo /* cleanup FS via transaction */ 3539acce952bSliubo btrfs_cleanup_transaction(root); 3540acce952bSliubo } 3541acce952bSliubo 3542143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root) 3543acce952bSliubo { 3544acce952bSliubo struct btrfs_inode *btrfs_inode; 3545acce952bSliubo struct list_head splice; 3546acce952bSliubo 3547acce952bSliubo INIT_LIST_HEAD(&splice); 3548acce952bSliubo 3549acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3550acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3551acce952bSliubo 3552acce952bSliubo list_splice_init(&root->fs_info->ordered_operations, &splice); 3553acce952bSliubo while (!list_empty(&splice)) { 3554acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3555acce952bSliubo ordered_operations); 3556acce952bSliubo 3557acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3558acce952bSliubo 3559acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3560acce952bSliubo } 3561acce952bSliubo 3562acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3563acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3564acce952bSliubo } 3565acce952bSliubo 3566143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3567acce952bSliubo { 3568acce952bSliubo struct btrfs_ordered_extent *ordered; 3569acce952bSliubo 3570acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3571779880efSJosef Bacik /* 3572779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will 3573779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up. 3574779880efSJosef Bacik */ 3575779880efSJosef Bacik list_for_each_entry(ordered, &root->fs_info->ordered_extents, 3576779880efSJosef Bacik root_extent_list) 3577779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 3578acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3579acce952bSliubo } 3580acce952bSliubo 358179787eaaSJeff Mahoney int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3582acce952bSliubo struct btrfs_root *root) 3583acce952bSliubo { 3584acce952bSliubo struct rb_node *node; 3585acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3586acce952bSliubo struct btrfs_delayed_ref_node *ref; 3587acce952bSliubo int ret = 0; 3588acce952bSliubo 3589acce952bSliubo delayed_refs = &trans->delayed_refs; 3590acce952bSliubo 3591acce952bSliubo spin_lock(&delayed_refs->lock); 3592acce952bSliubo if (delayed_refs->num_entries == 0) { 3593cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3594acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3595acce952bSliubo return ret; 3596acce952bSliubo } 3597acce952bSliubo 3598b939d1abSJosef Bacik while ((node = rb_first(&delayed_refs->root)) != NULL) { 3599eb12db69SJosef Bacik struct btrfs_delayed_ref_head *head = NULL; 3600acce952bSliubo 3601eb12db69SJosef Bacik ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3602acce952bSliubo atomic_set(&ref->refs, 1); 3603acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3604acce952bSliubo 3605acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3606b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3607b939d1abSJosef Bacik atomic_inc(&ref->refs); 360879787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3609b939d1abSJosef Bacik 3610b939d1abSJosef Bacik /* Need to wait for the delayed ref to run */ 3611acce952bSliubo mutex_lock(&head->mutex); 3612b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3613b939d1abSJosef Bacik btrfs_put_delayed_ref(ref); 3614b939d1abSJosef Bacik 3615e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3616b939d1abSJosef Bacik continue; 3617b939d1abSJosef Bacik } 3618b939d1abSJosef Bacik 361978a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op); 3620acce952bSliubo delayed_refs->num_heads--; 3621acce952bSliubo if (list_empty(&head->cluster)) 3622acce952bSliubo delayed_refs->num_heads_ready--; 3623acce952bSliubo list_del_init(&head->cluster); 3624acce952bSliubo } 3625eb12db69SJosef Bacik 3626b939d1abSJosef Bacik ref->in_tree = 0; 3627b939d1abSJosef Bacik rb_erase(&ref->rb_node, &delayed_refs->root); 3628b939d1abSJosef Bacik delayed_refs->num_entries--; 3629eb12db69SJosef Bacik if (head) 3630eb12db69SJosef Bacik mutex_unlock(&head->mutex); 3631acce952bSliubo spin_unlock(&delayed_refs->lock); 3632acce952bSliubo btrfs_put_delayed_ref(ref); 3633acce952bSliubo 3634acce952bSliubo cond_resched(); 3635acce952bSliubo spin_lock(&delayed_refs->lock); 3636acce952bSliubo } 3637acce952bSliubo 3638acce952bSliubo spin_unlock(&delayed_refs->lock); 3639acce952bSliubo 3640acce952bSliubo return ret; 3641acce952bSliubo } 3642acce952bSliubo 3643143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t) 3644acce952bSliubo { 3645acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3646acce952bSliubo struct list_head splice; 3647acce952bSliubo 3648acce952bSliubo INIT_LIST_HEAD(&splice); 3649acce952bSliubo 3650acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3651acce952bSliubo 3652acce952bSliubo while (!list_empty(&splice)) { 3653acce952bSliubo snapshot = list_entry(splice.next, 3654acce952bSliubo struct btrfs_pending_snapshot, 3655acce952bSliubo list); 3656acce952bSliubo 3657acce952bSliubo list_del_init(&snapshot->list); 3658acce952bSliubo 3659acce952bSliubo kfree(snapshot); 3660acce952bSliubo } 3661acce952bSliubo } 3662acce952bSliubo 3663143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3664acce952bSliubo { 3665acce952bSliubo struct btrfs_inode *btrfs_inode; 3666acce952bSliubo struct list_head splice; 3667acce952bSliubo 3668acce952bSliubo INIT_LIST_HEAD(&splice); 3669acce952bSliubo 3670acce952bSliubo spin_lock(&root->fs_info->delalloc_lock); 36715be76758SDavid Sterba list_splice_init(&root->fs_info->delalloc_inodes, &splice); 3672acce952bSliubo 3673acce952bSliubo while (!list_empty(&splice)) { 3674acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3675acce952bSliubo delalloc_inodes); 3676acce952bSliubo 3677acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3678df0af1a5SMiao Xie clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, 3679df0af1a5SMiao Xie &btrfs_inode->runtime_flags); 3680acce952bSliubo 3681acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3682acce952bSliubo } 3683acce952bSliubo 3684acce952bSliubo spin_unlock(&root->fs_info->delalloc_lock); 3685acce952bSliubo } 3686acce952bSliubo 3687acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3688acce952bSliubo struct extent_io_tree *dirty_pages, 3689acce952bSliubo int mark) 3690acce952bSliubo { 3691acce952bSliubo int ret; 3692acce952bSliubo struct page *page; 3693acce952bSliubo struct inode *btree_inode = root->fs_info->btree_inode; 3694acce952bSliubo struct extent_buffer *eb; 3695acce952bSliubo u64 start = 0; 3696acce952bSliubo u64 end; 3697acce952bSliubo u64 offset; 3698acce952bSliubo unsigned long index; 3699acce952bSliubo 3700acce952bSliubo while (1) { 3701acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3702e6138876SJosef Bacik mark, NULL); 3703acce952bSliubo if (ret) 3704acce952bSliubo break; 3705acce952bSliubo 3706acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3707acce952bSliubo while (start <= end) { 3708acce952bSliubo index = start >> PAGE_CACHE_SHIFT; 3709acce952bSliubo start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3710acce952bSliubo page = find_get_page(btree_inode->i_mapping, index); 3711acce952bSliubo if (!page) 3712acce952bSliubo continue; 3713acce952bSliubo offset = page_offset(page); 3714acce952bSliubo 3715acce952bSliubo spin_lock(&dirty_pages->buffer_lock); 3716acce952bSliubo eb = radix_tree_lookup( 3717acce952bSliubo &(&BTRFS_I(page->mapping->host)->io_tree)->buffer, 3718acce952bSliubo offset >> PAGE_CACHE_SHIFT); 3719acce952bSliubo spin_unlock(&dirty_pages->buffer_lock); 3720ee670f0aSJosef Bacik if (eb) 3721acce952bSliubo ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3722acce952bSliubo &eb->bflags); 3723acce952bSliubo if (PageWriteback(page)) 3724acce952bSliubo end_page_writeback(page); 3725acce952bSliubo 3726acce952bSliubo lock_page(page); 3727acce952bSliubo if (PageDirty(page)) { 3728acce952bSliubo clear_page_dirty_for_io(page); 3729acce952bSliubo spin_lock_irq(&page->mapping->tree_lock); 3730acce952bSliubo radix_tree_tag_clear(&page->mapping->page_tree, 3731acce952bSliubo page_index(page), 3732acce952bSliubo PAGECACHE_TAG_DIRTY); 3733acce952bSliubo spin_unlock_irq(&page->mapping->tree_lock); 3734acce952bSliubo } 3735acce952bSliubo 3736acce952bSliubo unlock_page(page); 3737ee670f0aSJosef Bacik page_cache_release(page); 3738acce952bSliubo } 3739acce952bSliubo } 3740acce952bSliubo 3741acce952bSliubo return ret; 3742acce952bSliubo } 3743acce952bSliubo 3744acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3745acce952bSliubo struct extent_io_tree *pinned_extents) 3746acce952bSliubo { 3747acce952bSliubo struct extent_io_tree *unpin; 3748acce952bSliubo u64 start; 3749acce952bSliubo u64 end; 3750acce952bSliubo int ret; 3751ed0eaa14SLiu Bo bool loop = true; 3752acce952bSliubo 3753acce952bSliubo unpin = pinned_extents; 3754ed0eaa14SLiu Bo again: 3755acce952bSliubo while (1) { 3756acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3757e6138876SJosef Bacik EXTENT_DIRTY, NULL); 3758acce952bSliubo if (ret) 3759acce952bSliubo break; 3760acce952bSliubo 3761acce952bSliubo /* opt_discard */ 37625378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 37635378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 37645378e607SLi Dongyang end + 1 - start, 37655378e607SLi Dongyang NULL); 3766acce952bSliubo 3767acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3768acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3769acce952bSliubo cond_resched(); 3770acce952bSliubo } 3771acce952bSliubo 3772ed0eaa14SLiu Bo if (loop) { 3773ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 3774ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 3775ed0eaa14SLiu Bo else 3776ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 3777ed0eaa14SLiu Bo loop = false; 3778ed0eaa14SLiu Bo goto again; 3779ed0eaa14SLiu Bo } 3780ed0eaa14SLiu Bo 3781acce952bSliubo return 0; 3782acce952bSliubo } 3783acce952bSliubo 378449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 378549b25e05SJeff Mahoney struct btrfs_root *root) 378649b25e05SJeff Mahoney { 378749b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 378849b25e05SJeff Mahoney btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 378949b25e05SJeff Mahoney cur_trans->dirty_pages.dirty_bytes); 379049b25e05SJeff Mahoney 379149b25e05SJeff Mahoney /* FIXME: cleanup wait for commit */ 379249b25e05SJeff Mahoney cur_trans->in_commit = 1; 379349b25e05SJeff Mahoney cur_trans->blocked = 1; 379449b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 379549b25e05SJeff Mahoney 379649b25e05SJeff Mahoney cur_trans->blocked = 0; 379749b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 379849b25e05SJeff Mahoney 379949b25e05SJeff Mahoney cur_trans->commit_done = 1; 380049b25e05SJeff Mahoney wake_up(&cur_trans->commit_wait); 380149b25e05SJeff Mahoney 380267cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 380367cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 380467cde344SMiao Xie 380549b25e05SJeff Mahoney btrfs_destroy_pending_snapshots(cur_trans); 380649b25e05SJeff Mahoney 380749b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 380849b25e05SJeff Mahoney EXTENT_DIRTY); 38096e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 38106e841e32SLiu Bo root->fs_info->pinned_extents); 381149b25e05SJeff Mahoney 381249b25e05SJeff Mahoney /* 381349b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 381449b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 381549b25e05SJeff Mahoney */ 381649b25e05SJeff Mahoney } 381749b25e05SJeff Mahoney 381849b25e05SJeff Mahoney int btrfs_cleanup_transaction(struct btrfs_root *root) 3819acce952bSliubo { 3820acce952bSliubo struct btrfs_transaction *t; 3821acce952bSliubo LIST_HEAD(list); 3822acce952bSliubo 3823acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 3824acce952bSliubo 3825a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3826acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 3827a4abeea4SJosef Bacik root->fs_info->trans_no_join = 1; 3828a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3829a4abeea4SJosef Bacik 3830acce952bSliubo while (!list_empty(&list)) { 3831acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 3832acce952bSliubo 3833acce952bSliubo btrfs_destroy_ordered_operations(root); 3834acce952bSliubo 3835acce952bSliubo btrfs_destroy_ordered_extents(root); 3836acce952bSliubo 3837acce952bSliubo btrfs_destroy_delayed_refs(t, root); 3838acce952bSliubo 3839acce952bSliubo btrfs_block_rsv_release(root, 3840acce952bSliubo &root->fs_info->trans_block_rsv, 3841acce952bSliubo t->dirty_pages.dirty_bytes); 3842acce952bSliubo 3843acce952bSliubo /* FIXME: cleanup wait for commit */ 3844acce952bSliubo t->in_commit = 1; 3845acce952bSliubo t->blocked = 1; 384666657b31SJosef Bacik smp_mb(); 3847acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3848acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 3849acce952bSliubo 3850acce952bSliubo t->blocked = 0; 385166657b31SJosef Bacik smp_mb(); 3852acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 3853acce952bSliubo wake_up(&root->fs_info->transaction_wait); 3854acce952bSliubo 3855acce952bSliubo t->commit_done = 1; 385666657b31SJosef Bacik smp_mb(); 3857acce952bSliubo if (waitqueue_active(&t->commit_wait)) 3858acce952bSliubo wake_up(&t->commit_wait); 3859acce952bSliubo 386067cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 386167cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 386267cde344SMiao Xie 3863acce952bSliubo btrfs_destroy_pending_snapshots(t); 3864acce952bSliubo 3865acce952bSliubo btrfs_destroy_delalloc_inodes(root); 3866acce952bSliubo 3867a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3868acce952bSliubo root->fs_info->running_transaction = NULL; 3869a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3870acce952bSliubo 3871acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 3872acce952bSliubo EXTENT_DIRTY); 3873acce952bSliubo 3874acce952bSliubo btrfs_destroy_pinned_extent(root, 3875acce952bSliubo root->fs_info->pinned_extents); 3876acce952bSliubo 387713c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 3878acce952bSliubo list_del_init(&t->list); 3879acce952bSliubo memset(t, 0, sizeof(*t)); 3880acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 3881acce952bSliubo } 3882acce952bSliubo 3883a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3884a4abeea4SJosef Bacik root->fs_info->trans_no_join = 0; 3885a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3886acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 3887acce952bSliubo 3888acce952bSliubo return 0; 3889acce952bSliubo } 3890acce952bSliubo 3891d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 3892ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 38934bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 38940b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 3895239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 3896239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 38970da5468fSChris Mason }; 3898