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" 48eb60ceacSChris Mason 49d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 508b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 514df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 52fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 53acce952bSliubo int read_only); 54143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root); 55143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 56acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 57acce952bSliubo struct btrfs_root *root); 58143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t); 59143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 60acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 61acce952bSliubo struct extent_io_tree *dirty_pages, 62acce952bSliubo int mark); 63acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 64acce952bSliubo struct extent_io_tree *pinned_extents); 65ce9adaa5SChris Mason 66d352ac68SChris Mason /* 67d352ac68SChris Mason * end_io_wq structs are used to do processing in task context when an IO is 68d352ac68SChris Mason * complete. This is used during reads to verify checksums, and it is used 69d352ac68SChris Mason * by writes to insert metadata for new file extents after IO is complete. 70d352ac68SChris Mason */ 71ce9adaa5SChris Mason struct end_io_wq { 72ce9adaa5SChris Mason struct bio *bio; 73ce9adaa5SChris Mason bio_end_io_t *end_io; 74ce9adaa5SChris Mason void *private; 75ce9adaa5SChris Mason struct btrfs_fs_info *info; 76ce9adaa5SChris Mason int error; 7722c59948SChris Mason int metadata; 78ce9adaa5SChris Mason struct list_head list; 798b712842SChris Mason struct btrfs_work work; 80ce9adaa5SChris Mason }; 810da5468fSChris Mason 82d352ac68SChris Mason /* 83d352ac68SChris Mason * async submit bios are used to offload expensive checksumming 84d352ac68SChris Mason * onto the worker threads. They checksum file and metadata bios 85d352ac68SChris Mason * just before they are sent down the IO stack. 86d352ac68SChris Mason */ 8744b8bd7eSChris Mason struct async_submit_bio { 8844b8bd7eSChris Mason struct inode *inode; 8944b8bd7eSChris Mason struct bio *bio; 9044b8bd7eSChris Mason struct list_head list; 914a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start; 924a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done; 9344b8bd7eSChris Mason int rw; 9444b8bd7eSChris Mason int mirror_num; 95c8b97818SChris Mason unsigned long bio_flags; 96eaf25d93SChris Mason /* 97eaf25d93SChris Mason * bio_offset is optional, can be used if the pages in the bio 98eaf25d93SChris Mason * can't tell us where in the file the bio should go 99eaf25d93SChris Mason */ 100eaf25d93SChris Mason u64 bio_offset; 1018b712842SChris Mason struct btrfs_work work; 10279787eaaSJeff Mahoney int error; 10344b8bd7eSChris Mason }; 10444b8bd7eSChris Mason 10585d4e461SChris Mason /* 10685d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 10785d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 10885d4e461SChris Mason * the level the eb occupies in the tree. 1094008c04aSChris Mason * 11085d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 11185d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 11285d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 11385d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 11485d4e461SChris Mason * have separate keysets. 1154008c04aSChris Mason * 11685d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 11785d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 11885d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1194008c04aSChris Mason * 12085d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 12185d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 12285d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 12385d4e461SChris Mason * 12485d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 12585d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 12685d4e461SChris Mason * needs update as well. 1274008c04aSChris Mason */ 1284008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1294008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1304008c04aSChris Mason # error 1314008c04aSChris Mason # endif 13285d4e461SChris Mason 13385d4e461SChris Mason static struct btrfs_lockdep_keyset { 13485d4e461SChris Mason u64 id; /* root objectid */ 13585d4e461SChris Mason const char *name_stem; /* lock name stem */ 13685d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 13785d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 13885d4e461SChris Mason } btrfs_lockdep_keysets[] = { 13985d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 14085d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 14185d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 14285d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 14385d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 14485d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 14585d4e461SChris Mason { .id = BTRFS_ORPHAN_OBJECTID, .name_stem = "orphan" }, 14685d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 14785d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 14885d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 14985d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1504008c04aSChris Mason }; 15185d4e461SChris Mason 15285d4e461SChris Mason void __init btrfs_init_lockdep(void) 15385d4e461SChris Mason { 15485d4e461SChris Mason int i, j; 15585d4e461SChris Mason 15685d4e461SChris Mason /* initialize lockdep class names */ 15785d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 15885d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 15985d4e461SChris Mason 16085d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 16185d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 16285d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 16385d4e461SChris Mason } 16485d4e461SChris Mason } 16585d4e461SChris Mason 16685d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 16785d4e461SChris Mason int level) 16885d4e461SChris Mason { 16985d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 17085d4e461SChris Mason 17185d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 17285d4e461SChris Mason 17385d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 17485d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 17585d4e461SChris Mason if (ks->id == objectid) 17685d4e461SChris Mason break; 17785d4e461SChris Mason 17885d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 17985d4e461SChris Mason &ks->keys[level], ks->names[level]); 18085d4e461SChris Mason } 18185d4e461SChris Mason 1824008c04aSChris Mason #endif 1834008c04aSChris Mason 184d352ac68SChris Mason /* 185d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 186d352ac68SChris Mason * that covers the entire device 187d352ac68SChris Mason */ 188b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 189306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 1905f39d397SChris Mason int create) 1915f39d397SChris Mason { 1925f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1935f39d397SChris Mason struct extent_map *em; 1945f39d397SChris Mason int ret; 1955f39d397SChris Mason 196890871beSChris Mason read_lock(&em_tree->lock); 197d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 198a061fc8dSChris Mason if (em) { 199a061fc8dSChris Mason em->bdev = 200a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 201890871beSChris Mason read_unlock(&em_tree->lock); 2025f39d397SChris Mason goto out; 203a061fc8dSChris Mason } 204890871beSChris Mason read_unlock(&em_tree->lock); 2057b13b7b1SChris Mason 206172ddd60SDavid Sterba em = alloc_extent_map(); 2075f39d397SChris Mason if (!em) { 2085f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2095f39d397SChris Mason goto out; 2105f39d397SChris Mason } 2115f39d397SChris Mason em->start = 0; 2120afbaf8cSChris Mason em->len = (u64)-1; 213c8b97818SChris Mason em->block_len = (u64)-1; 2145f39d397SChris Mason em->block_start = 0; 215a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 216d1310b2eSChris Mason 217890871beSChris Mason write_lock(&em_tree->lock); 2185f39d397SChris Mason ret = add_extent_mapping(em_tree, em); 2195f39d397SChris Mason if (ret == -EEXIST) { 2200afbaf8cSChris Mason u64 failed_start = em->start; 2210afbaf8cSChris Mason u64 failed_len = em->len; 2220afbaf8cSChris Mason 2235f39d397SChris Mason free_extent_map(em); 2247b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 2250afbaf8cSChris Mason if (em) { 2267b13b7b1SChris Mason ret = 0; 2270afbaf8cSChris Mason } else { 2280afbaf8cSChris Mason em = lookup_extent_mapping(em_tree, failed_start, 2290afbaf8cSChris Mason failed_len); 2307b13b7b1SChris Mason ret = -EIO; 2310afbaf8cSChris Mason } 2325f39d397SChris Mason } else if (ret) { 2337b13b7b1SChris Mason free_extent_map(em); 2347b13b7b1SChris Mason em = NULL; 2355f39d397SChris Mason } 236890871beSChris Mason write_unlock(&em_tree->lock); 2377b13b7b1SChris Mason 2387b13b7b1SChris Mason if (ret) 2397b13b7b1SChris Mason em = ERR_PTR(ret); 2405f39d397SChris Mason out: 2415f39d397SChris Mason return em; 2425f39d397SChris Mason } 2435f39d397SChris Mason 24419c00ddcSChris Mason u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) 24519c00ddcSChris Mason { 246163e783eSDavid Woodhouse return crc32c(seed, data, len); 24719c00ddcSChris Mason } 24819c00ddcSChris Mason 24919c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 25019c00ddcSChris Mason { 2517e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 25219c00ddcSChris Mason } 25319c00ddcSChris Mason 254d352ac68SChris Mason /* 255d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 256d352ac68SChris Mason * into the csum field of the block. 257d352ac68SChris Mason */ 25819c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 25919c00ddcSChris Mason int verify) 26019c00ddcSChris Mason { 2616c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 262607d432dSJosef Bacik char *result = NULL; 26319c00ddcSChris Mason unsigned long len; 26419c00ddcSChris Mason unsigned long cur_len; 26519c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26619c00ddcSChris Mason char *kaddr; 26719c00ddcSChris Mason unsigned long map_start; 26819c00ddcSChris Mason unsigned long map_len; 26919c00ddcSChris Mason int err; 27019c00ddcSChris Mason u32 crc = ~(u32)0; 271607d432dSJosef Bacik unsigned long inline_result; 27219c00ddcSChris Mason 27319c00ddcSChris Mason len = buf->len - offset; 27419c00ddcSChris Mason while (len > 0) { 27519c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 276a6591715SChris Mason &kaddr, &map_start, &map_len); 277d397712bSChris Mason if (err) 27819c00ddcSChris Mason return 1; 27919c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 28019c00ddcSChris Mason crc = btrfs_csum_data(root, kaddr + offset - map_start, 28119c00ddcSChris Mason crc, cur_len); 28219c00ddcSChris Mason len -= cur_len; 28319c00ddcSChris Mason offset += cur_len; 28419c00ddcSChris Mason } 285607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 286607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 287607d432dSJosef Bacik if (!result) 288607d432dSJosef Bacik return 1; 289607d432dSJosef Bacik } else { 290607d432dSJosef Bacik result = (char *)&inline_result; 291607d432dSJosef Bacik } 292607d432dSJosef Bacik 29319c00ddcSChris Mason btrfs_csum_final(crc, result); 29419c00ddcSChris Mason 29519c00ddcSChris Mason if (verify) { 296607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 297e4204dedSChris Mason u32 val; 298e4204dedSChris Mason u32 found = 0; 299607d432dSJosef Bacik memcpy(&found, result, csum_size); 300e4204dedSChris Mason 301607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 3027a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs: %s checksum verify " 303193f284dSChris Mason "failed on %llu wanted %X found %X " 304193f284dSChris Mason "level %d\n", 30519c00ddcSChris Mason root->fs_info->sb->s_id, 306193f284dSChris Mason (unsigned long long)buf->start, val, found, 307193f284dSChris Mason btrfs_header_level(buf)); 308607d432dSJosef Bacik if (result != (char *)&inline_result) 309607d432dSJosef Bacik kfree(result); 31019c00ddcSChris Mason return 1; 31119c00ddcSChris Mason } 31219c00ddcSChris Mason } else { 313607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 31419c00ddcSChris Mason } 315607d432dSJosef Bacik if (result != (char *)&inline_result) 316607d432dSJosef Bacik kfree(result); 31719c00ddcSChris Mason return 0; 31819c00ddcSChris Mason } 31919c00ddcSChris Mason 320d352ac68SChris Mason /* 321d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 322d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 323d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 324d352ac68SChris Mason * in the wrong place. 325d352ac68SChris Mason */ 3261259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 327b9fab919SChris Mason struct extent_buffer *eb, u64 parent_transid, 328b9fab919SChris Mason int atomic) 3291259ab75SChris Mason { 3302ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3311259ab75SChris Mason int ret; 3321259ab75SChris Mason 3331259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3341259ab75SChris Mason return 0; 3351259ab75SChris Mason 336b9fab919SChris Mason if (atomic) 337b9fab919SChris Mason return -EAGAIN; 338b9fab919SChris Mason 3392ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 340d0082371SJeff Mahoney 0, &cached_state); 3410b32f4bbSJosef Bacik if (extent_buffer_uptodate(eb) && 3421259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3431259ab75SChris Mason ret = 0; 3441259ab75SChris Mason goto out; 3451259ab75SChris Mason } 3467a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 347193f284dSChris Mason "found %llu\n", 3481259ab75SChris Mason (unsigned long long)eb->start, 3491259ab75SChris Mason (unsigned long long)parent_transid, 3501259ab75SChris Mason (unsigned long long)btrfs_header_generation(eb)); 3511259ab75SChris Mason ret = 1; 3520b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 35333958dc6SChris Mason out: 3542ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3552ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3561259ab75SChris Mason return ret; 3571259ab75SChris Mason } 3581259ab75SChris Mason 359d352ac68SChris Mason /* 360d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 361d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 362d352ac68SChris Mason */ 363f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 364f188591eSChris Mason struct extent_buffer *eb, 365ca7a79adSChris Mason u64 start, u64 parent_transid) 366f188591eSChris Mason { 367f188591eSChris Mason struct extent_io_tree *io_tree; 368ea466794SJosef Bacik int failed = 0; 369f188591eSChris Mason int ret; 370f188591eSChris Mason int num_copies = 0; 371f188591eSChris Mason int mirror_num = 0; 372ea466794SJosef Bacik int failed_mirror = 0; 373f188591eSChris Mason 374a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 375f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 376f188591eSChris Mason while (1) { 377bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 378bb82ab88SArne Jansen WAIT_COMPLETE, 379f188591eSChris Mason btree_get_extent, mirror_num); 380256dd1bbSStefan Behrens if (!ret) { 381256dd1bbSStefan Behrens if (!verify_parent_transid(io_tree, eb, 382b9fab919SChris Mason parent_transid, 0)) 383ea466794SJosef Bacik break; 384256dd1bbSStefan Behrens else 385256dd1bbSStefan Behrens ret = -EIO; 386256dd1bbSStefan Behrens } 387d397712bSChris Mason 388a826d6dcSJosef Bacik /* 389a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 390a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 391a826d6dcSJosef Bacik * any less wrong. 392a826d6dcSJosef Bacik */ 393a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 394ea466794SJosef Bacik break; 395ea466794SJosef Bacik 396f188591eSChris Mason num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, 397f188591eSChris Mason eb->start, eb->len); 3984235298eSChris Mason if (num_copies == 1) 399ea466794SJosef Bacik break; 4004235298eSChris Mason 4015cf1ab56SJosef Bacik if (!failed_mirror) { 4025cf1ab56SJosef Bacik failed = 1; 4035cf1ab56SJosef Bacik failed_mirror = eb->read_mirror; 4045cf1ab56SJosef Bacik } 4055cf1ab56SJosef Bacik 406f188591eSChris Mason mirror_num++; 407ea466794SJosef Bacik if (mirror_num == failed_mirror) 408ea466794SJosef Bacik mirror_num++; 409ea466794SJosef Bacik 4104235298eSChris Mason if (mirror_num > num_copies) 411ea466794SJosef Bacik break; 412f188591eSChris Mason } 413ea466794SJosef Bacik 414c0901581SStefan Behrens if (failed && !ret && failed_mirror) 415ea466794SJosef Bacik repair_eb_io_failure(root, eb, failed_mirror); 416ea466794SJosef Bacik 417ea466794SJosef Bacik return ret; 418f188591eSChris Mason } 41919c00ddcSChris Mason 420d352ac68SChris Mason /* 421d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 422d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 423d352ac68SChris Mason */ 424d397712bSChris Mason 425b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 42619c00ddcSChris Mason { 427d1310b2eSChris Mason struct extent_io_tree *tree; 42835ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 42919c00ddcSChris Mason u64 found_start; 43019c00ddcSChris Mason struct extent_buffer *eb; 431f188591eSChris Mason 432d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 43319c00ddcSChris Mason 4344f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 4354f2de97aSJosef Bacik if (page != eb->pages[0]) 4364f2de97aSJosef Bacik return 0; 43719c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 43819c00ddcSChris Mason if (found_start != start) { 43955c69072SChris Mason WARN_ON(1); 4404f2de97aSJosef Bacik return 0; 44155c69072SChris Mason } 442727011e0SChris Mason if (eb->pages[0] != page) { 44355c69072SChris Mason WARN_ON(1); 4444f2de97aSJosef Bacik return 0; 44555c69072SChris Mason } 44655c69072SChris Mason if (!PageUptodate(page)) { 44755c69072SChris Mason WARN_ON(1); 4484f2de97aSJosef Bacik return 0; 44919c00ddcSChris Mason } 45019c00ddcSChris Mason csum_tree_block(root, eb, 0); 45119c00ddcSChris Mason return 0; 45219c00ddcSChris Mason } 45319c00ddcSChris Mason 4542b82032cSYan Zheng static int check_tree_block_fsid(struct btrfs_root *root, 4552b82032cSYan Zheng struct extent_buffer *eb) 4562b82032cSYan Zheng { 4572b82032cSYan Zheng struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 4582b82032cSYan Zheng u8 fsid[BTRFS_UUID_SIZE]; 4592b82032cSYan Zheng int ret = 1; 4602b82032cSYan Zheng 4612b82032cSYan Zheng read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb), 4622b82032cSYan Zheng BTRFS_FSID_SIZE); 4632b82032cSYan Zheng while (fs_devices) { 4642b82032cSYan Zheng if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) { 4652b82032cSYan Zheng ret = 0; 4662b82032cSYan Zheng break; 4672b82032cSYan Zheng } 4682b82032cSYan Zheng fs_devices = fs_devices->seed; 4692b82032cSYan Zheng } 4702b82032cSYan Zheng return ret; 4712b82032cSYan Zheng } 4722b82032cSYan Zheng 473a826d6dcSJosef Bacik #define CORRUPT(reason, eb, root, slot) \ 474a826d6dcSJosef Bacik printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \ 475a826d6dcSJosef Bacik "root=%llu, slot=%d\n", reason, \ 476a826d6dcSJosef Bacik (unsigned long long)btrfs_header_bytenr(eb), \ 477a826d6dcSJosef Bacik (unsigned long long)root->objectid, slot) 478a826d6dcSJosef Bacik 479a826d6dcSJosef Bacik static noinline int check_leaf(struct btrfs_root *root, 480a826d6dcSJosef Bacik struct extent_buffer *leaf) 481a826d6dcSJosef Bacik { 482a826d6dcSJosef Bacik struct btrfs_key key; 483a826d6dcSJosef Bacik struct btrfs_key leaf_key; 484a826d6dcSJosef Bacik u32 nritems = btrfs_header_nritems(leaf); 485a826d6dcSJosef Bacik int slot; 486a826d6dcSJosef Bacik 487a826d6dcSJosef Bacik if (nritems == 0) 488a826d6dcSJosef Bacik return 0; 489a826d6dcSJosef Bacik 490a826d6dcSJosef Bacik /* Check the 0 item */ 491a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 492a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 493a826d6dcSJosef Bacik CORRUPT("invalid item offset size pair", leaf, root, 0); 494a826d6dcSJosef Bacik return -EIO; 495a826d6dcSJosef Bacik } 496a826d6dcSJosef Bacik 497a826d6dcSJosef Bacik /* 498a826d6dcSJosef Bacik * Check to make sure each items keys are in the correct order and their 499a826d6dcSJosef Bacik * offsets make sense. We only have to loop through nritems-1 because 500a826d6dcSJosef Bacik * we check the current slot against the next slot, which verifies the 501a826d6dcSJosef Bacik * next slot's offset+size makes sense and that the current's slot 502a826d6dcSJosef Bacik * offset is correct. 503a826d6dcSJosef Bacik */ 504a826d6dcSJosef Bacik for (slot = 0; slot < nritems - 1; slot++) { 505a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 506a826d6dcSJosef Bacik btrfs_item_key_to_cpu(leaf, &key, slot + 1); 507a826d6dcSJosef Bacik 508a826d6dcSJosef Bacik /* Make sure the keys are in the right order */ 509a826d6dcSJosef Bacik if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 510a826d6dcSJosef Bacik CORRUPT("bad key order", leaf, root, slot); 511a826d6dcSJosef Bacik return -EIO; 512a826d6dcSJosef Bacik } 513a826d6dcSJosef Bacik 514a826d6dcSJosef Bacik /* 515a826d6dcSJosef Bacik * Make sure the offset and ends are right, remember that the 516a826d6dcSJosef Bacik * item data starts at the end of the leaf and grows towards the 517a826d6dcSJosef Bacik * front. 518a826d6dcSJosef Bacik */ 519a826d6dcSJosef Bacik if (btrfs_item_offset_nr(leaf, slot) != 520a826d6dcSJosef Bacik btrfs_item_end_nr(leaf, slot + 1)) { 521a826d6dcSJosef Bacik CORRUPT("slot offset bad", leaf, root, slot); 522a826d6dcSJosef Bacik return -EIO; 523a826d6dcSJosef Bacik } 524a826d6dcSJosef Bacik 525a826d6dcSJosef Bacik /* 526a826d6dcSJosef Bacik * Check to make sure that we don't point outside of the leaf, 527a826d6dcSJosef Bacik * just incase all the items are consistent to eachother, but 528a826d6dcSJosef Bacik * all point outside of the leaf. 529a826d6dcSJosef Bacik */ 530a826d6dcSJosef Bacik if (btrfs_item_end_nr(leaf, slot) > 531a826d6dcSJosef Bacik BTRFS_LEAF_DATA_SIZE(root)) { 532a826d6dcSJosef Bacik CORRUPT("slot end outside of leaf", leaf, root, slot); 533a826d6dcSJosef Bacik return -EIO; 534a826d6dcSJosef Bacik } 535a826d6dcSJosef Bacik } 536a826d6dcSJosef Bacik 537a826d6dcSJosef Bacik return 0; 538a826d6dcSJosef Bacik } 539a826d6dcSJosef Bacik 540727011e0SChris Mason struct extent_buffer *find_eb_for_page(struct extent_io_tree *tree, 541727011e0SChris Mason struct page *page, int max_walk) 542727011e0SChris Mason { 543727011e0SChris Mason struct extent_buffer *eb; 544727011e0SChris Mason u64 start = page_offset(page); 545727011e0SChris Mason u64 target = start; 546727011e0SChris Mason u64 min_start; 547727011e0SChris Mason 548727011e0SChris Mason if (start < max_walk) 549727011e0SChris Mason min_start = 0; 550727011e0SChris Mason else 551727011e0SChris Mason min_start = start - max_walk; 552727011e0SChris Mason 553727011e0SChris Mason while (start >= min_start) { 554727011e0SChris Mason eb = find_extent_buffer(tree, start, 0); 555727011e0SChris Mason if (eb) { 556727011e0SChris Mason /* 557727011e0SChris Mason * we found an extent buffer and it contains our page 558727011e0SChris Mason * horray! 559727011e0SChris Mason */ 560727011e0SChris Mason if (eb->start <= target && 561727011e0SChris Mason eb->start + eb->len > target) 562727011e0SChris Mason return eb; 563727011e0SChris Mason 564727011e0SChris Mason /* we found an extent buffer that wasn't for us */ 565727011e0SChris Mason free_extent_buffer(eb); 566727011e0SChris Mason return NULL; 567727011e0SChris Mason } 568727011e0SChris Mason if (start == 0) 569727011e0SChris Mason break; 570727011e0SChris Mason start -= PAGE_CACHE_SIZE; 571727011e0SChris Mason } 572727011e0SChris Mason return NULL; 573727011e0SChris Mason } 574727011e0SChris Mason 575b2950863SChristoph Hellwig static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 5765cf1ab56SJosef Bacik struct extent_state *state, int mirror) 577ce9adaa5SChris Mason { 578ce9adaa5SChris Mason struct extent_io_tree *tree; 579ce9adaa5SChris Mason u64 found_start; 580ce9adaa5SChris Mason int found_level; 581ce9adaa5SChris Mason struct extent_buffer *eb; 582ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 583f188591eSChris Mason int ret = 0; 584727011e0SChris Mason int reads_done; 585ce9adaa5SChris Mason 586ce9adaa5SChris Mason if (!page->private) 587ce9adaa5SChris Mason goto out; 588d397712bSChris Mason 589727011e0SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 5904f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 591d397712bSChris Mason 5920b32f4bbSJosef Bacik /* the pending IO might have been the only thing that kept this buffer 5930b32f4bbSJosef Bacik * in memory. Make sure we have a ref for all this other checks 5940b32f4bbSJosef Bacik */ 5950b32f4bbSJosef Bacik extent_buffer_get(eb); 5960b32f4bbSJosef Bacik 5970b32f4bbSJosef Bacik reads_done = atomic_dec_and_test(&eb->io_pages); 598727011e0SChris Mason if (!reads_done) 599727011e0SChris Mason goto err; 600f188591eSChris Mason 6015cf1ab56SJosef Bacik eb->read_mirror = mirror; 602ea466794SJosef Bacik if (test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { 603ea466794SJosef Bacik ret = -EIO; 604ea466794SJosef Bacik goto err; 605ea466794SJosef Bacik } 606ea466794SJosef Bacik 607ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 608727011e0SChris Mason if (found_start != eb->start) { 6097a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 610193f284dSChris Mason "%llu %llu\n", 611a1b32a59SChris Mason (unsigned long long)found_start, 612a1b32a59SChris Mason (unsigned long long)eb->start); 613f188591eSChris Mason ret = -EIO; 614ce9adaa5SChris Mason goto err; 615ce9adaa5SChris Mason } 6162b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 6177a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 618d397712bSChris Mason (unsigned long long)eb->start); 6191259ab75SChris Mason ret = -EIO; 6201259ab75SChris Mason goto err; 6211259ab75SChris Mason } 622ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 623ce9adaa5SChris Mason 62485d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 62585d4e461SChris Mason eb, found_level); 6264008c04aSChris Mason 627ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 628a826d6dcSJosef Bacik if (ret) { 629f188591eSChris Mason ret = -EIO; 630a826d6dcSJosef Bacik goto err; 631a826d6dcSJosef Bacik } 632a826d6dcSJosef Bacik 633a826d6dcSJosef Bacik /* 634a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 635a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 636a826d6dcSJosef Bacik * return -EIO. 637a826d6dcSJosef Bacik */ 638a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 639a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 640a826d6dcSJosef Bacik ret = -EIO; 641a826d6dcSJosef Bacik } 642ce9adaa5SChris Mason 6430b32f4bbSJosef Bacik if (!ret) 6440b32f4bbSJosef Bacik set_extent_buffer_uptodate(eb); 645ce9adaa5SChris Mason err: 6464bb31e92SArne Jansen if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) { 6474bb31e92SArne Jansen clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags); 6484bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6494bb31e92SArne Jansen } 6504bb31e92SArne Jansen 6510b32f4bbSJosef Bacik if (ret) 6520b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb); 6530b32f4bbSJosef Bacik free_extent_buffer(eb); 654ce9adaa5SChris Mason out: 655f188591eSChris Mason return ret; 656ce9adaa5SChris Mason } 657ce9adaa5SChris Mason 658ea466794SJosef Bacik static int btree_io_failed_hook(struct page *page, int failed_mirror) 6594bb31e92SArne Jansen { 6604bb31e92SArne Jansen struct extent_buffer *eb; 6614bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6624bb31e92SArne Jansen 6634f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 664ea466794SJosef Bacik set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); 6655cf1ab56SJosef Bacik eb->read_mirror = failed_mirror; 666ea466794SJosef Bacik if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) 6674bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6684bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6694bb31e92SArne Jansen } 6704bb31e92SArne Jansen 671ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 672ce9adaa5SChris Mason { 673ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 674ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 675ce9adaa5SChris Mason 676ce9adaa5SChris Mason fs_info = end_io_wq->info; 677ce9adaa5SChris Mason end_io_wq->error = err; 6788b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6798b712842SChris Mason end_io_wq->work.flags = 0; 680d20f7043SChris Mason 6817b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 6820cb59c99SJosef Bacik if (end_io_wq->metadata == 1) 683cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 684cad321adSChris Mason &end_io_wq->work); 6850cb59c99SJosef Bacik else if (end_io_wq->metadata == 2) 6860cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 6870cb59c99SJosef Bacik &end_io_wq->work); 688cad321adSChris Mason else 689e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 690e6dcd2dcSChris Mason &end_io_wq->work); 691d20f7043SChris Mason } else { 692d20f7043SChris Mason if (end_io_wq->metadata) 693d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 694d20f7043SChris Mason &end_io_wq->work); 695e6dcd2dcSChris Mason else 696d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 697d20f7043SChris Mason &end_io_wq->work); 698d20f7043SChris Mason } 699ce9adaa5SChris Mason } 700ce9adaa5SChris Mason 7010cb59c99SJosef Bacik /* 7020cb59c99SJosef Bacik * For the metadata arg you want 7030cb59c99SJosef Bacik * 7040cb59c99SJosef Bacik * 0 - if data 7050cb59c99SJosef Bacik * 1 - if normal metadta 7060cb59c99SJosef Bacik * 2 - if writing to the free space cache area 7070cb59c99SJosef Bacik */ 70822c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 70922c59948SChris Mason int metadata) 7100b86a832SChris Mason { 711ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 712ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 713ce9adaa5SChris Mason if (!end_io_wq) 714ce9adaa5SChris Mason return -ENOMEM; 715ce9adaa5SChris Mason 716ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 717ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 71822c59948SChris Mason end_io_wq->info = info; 719ce9adaa5SChris Mason end_io_wq->error = 0; 720ce9adaa5SChris Mason end_io_wq->bio = bio; 72122c59948SChris Mason end_io_wq->metadata = metadata; 722ce9adaa5SChris Mason 723ce9adaa5SChris Mason bio->bi_private = end_io_wq; 724ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 72522c59948SChris Mason return 0; 72622c59948SChris Mason } 72722c59948SChris Mason 728b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7294854ddd0SChris Mason { 7304854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7314854ddd0SChris Mason info->workers.max_workers, 7324854ddd0SChris Mason info->fs_devices->open_devices); 7334854ddd0SChris Mason return 256 * limit; 7344854ddd0SChris Mason } 7354854ddd0SChris Mason 7364a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7374a69a410SChris Mason { 7384a69a410SChris Mason struct async_submit_bio *async; 73979787eaaSJeff Mahoney int ret; 7404a69a410SChris Mason 7414a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 74279787eaaSJeff Mahoney ret = async->submit_bio_start(async->inode, async->rw, async->bio, 743eaf25d93SChris Mason async->mirror_num, async->bio_flags, 744eaf25d93SChris Mason async->bio_offset); 74579787eaaSJeff Mahoney if (ret) 74679787eaaSJeff Mahoney async->error = ret; 7474a69a410SChris Mason } 7484a69a410SChris Mason 7494a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7508b712842SChris Mason { 7518b712842SChris Mason struct btrfs_fs_info *fs_info; 7528b712842SChris Mason struct async_submit_bio *async; 7534854ddd0SChris Mason int limit; 7548b712842SChris Mason 7558b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7568b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7574854ddd0SChris Mason 758b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7594854ddd0SChris Mason limit = limit * 2 / 3; 7604854ddd0SChris Mason 76166657b31SJosef Bacik if (atomic_dec_return(&fs_info->nr_async_submits) < limit && 762b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7634854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7644854ddd0SChris Mason 76579787eaaSJeff Mahoney /* If an error occured we just want to clean up the bio and move on */ 76679787eaaSJeff Mahoney if (async->error) { 76779787eaaSJeff Mahoney bio_endio(async->bio, async->error); 76879787eaaSJeff Mahoney return; 76979787eaaSJeff Mahoney } 77079787eaaSJeff Mahoney 7714a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 772eaf25d93SChris Mason async->mirror_num, async->bio_flags, 773eaf25d93SChris Mason async->bio_offset); 7744a69a410SChris Mason } 7754a69a410SChris Mason 7764a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7774a69a410SChris Mason { 7784a69a410SChris Mason struct async_submit_bio *async; 7794a69a410SChris Mason 7804a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7818b712842SChris Mason kfree(async); 7828b712842SChris Mason } 7838b712842SChris Mason 78444b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 78544b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 786c8b97818SChris Mason unsigned long bio_flags, 787eaf25d93SChris Mason u64 bio_offset, 7884a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7894a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 79044b8bd7eSChris Mason { 79144b8bd7eSChris Mason struct async_submit_bio *async; 79244b8bd7eSChris Mason 79344b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 79444b8bd7eSChris Mason if (!async) 79544b8bd7eSChris Mason return -ENOMEM; 79644b8bd7eSChris Mason 79744b8bd7eSChris Mason async->inode = inode; 79844b8bd7eSChris Mason async->rw = rw; 79944b8bd7eSChris Mason async->bio = bio; 80044b8bd7eSChris Mason async->mirror_num = mirror_num; 8014a69a410SChris Mason async->submit_bio_start = submit_bio_start; 8024a69a410SChris Mason async->submit_bio_done = submit_bio_done; 8034a69a410SChris Mason 8044a69a410SChris Mason async->work.func = run_one_async_start; 8054a69a410SChris Mason async->work.ordered_func = run_one_async_done; 8064a69a410SChris Mason async->work.ordered_free = run_one_async_free; 8074a69a410SChris Mason 8088b712842SChris Mason async->work.flags = 0; 809c8b97818SChris Mason async->bio_flags = bio_flags; 810eaf25d93SChris Mason async->bio_offset = bio_offset; 8118c8bee1dSChris Mason 81279787eaaSJeff Mahoney async->error = 0; 81379787eaaSJeff Mahoney 814cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 815d313d7a3SChris Mason 8167b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 817d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 818d313d7a3SChris Mason 8198b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 8209473f16cSChris Mason 821771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 822771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 823771ed689SChris Mason wait_event(fs_info->async_submit_wait, 824771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 825771ed689SChris Mason } 826771ed689SChris Mason 82744b8bd7eSChris Mason return 0; 82844b8bd7eSChris Mason } 82944b8bd7eSChris Mason 830ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 831ce3ed71aSChris Mason { 832ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 833ce3ed71aSChris Mason int bio_index = 0; 834ce3ed71aSChris Mason struct btrfs_root *root; 83579787eaaSJeff Mahoney int ret = 0; 836ce3ed71aSChris Mason 837ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 838ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 839ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 84079787eaaSJeff Mahoney ret = csum_dirty_buffer(root, bvec->bv_page); 84179787eaaSJeff Mahoney if (ret) 84279787eaaSJeff Mahoney break; 843ce3ed71aSChris Mason bio_index++; 844ce3ed71aSChris Mason bvec++; 845ce3ed71aSChris Mason } 84679787eaaSJeff Mahoney return ret; 847ce3ed71aSChris Mason } 848ce3ed71aSChris Mason 8494a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8504a69a410SChris Mason struct bio *bio, int mirror_num, 851eaf25d93SChris Mason unsigned long bio_flags, 852eaf25d93SChris Mason u64 bio_offset) 85322c59948SChris Mason { 8548b712842SChris Mason /* 8558b712842SChris Mason * when we're called for a write, we're already in the async 8565443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8578b712842SChris Mason */ 85879787eaaSJeff Mahoney return btree_csum_one_bio(bio); 85922c59948SChris Mason } 86022c59948SChris Mason 8614a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 862eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 863eaf25d93SChris Mason u64 bio_offset) 8644a69a410SChris Mason { 8658b712842SChris Mason /* 8664a69a410SChris Mason * when we're called for a write, we're already in the async 8674a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8688b712842SChris Mason */ 8698b712842SChris Mason return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 8700b86a832SChris Mason } 8710b86a832SChris Mason 87244b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 873eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 874eaf25d93SChris Mason u64 bio_offset) 87544b8bd7eSChris Mason { 8764a69a410SChris Mason int ret; 877cad321adSChris Mason 8787b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 879f3f266abSChris Mason 880cad321adSChris Mason /* 881cad321adSChris Mason * called for a read, do the setup so that checksum validation 882cad321adSChris Mason * can happen in the async kernel threads 883cad321adSChris Mason */ 884f3f266abSChris Mason ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 885f3f266abSChris Mason bio, 1); 8861d4284bdSChris Mason if (ret) 8871d4284bdSChris Mason return ret; 8884a69a410SChris Mason return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 8896f3577bdSChris Mason mirror_num, 0); 89044b8bd7eSChris Mason } 891d313d7a3SChris Mason 892cad321adSChris Mason /* 893cad321adSChris Mason * kthread helpers are used to submit writes so that checksumming 894cad321adSChris Mason * can happen in parallel across all CPUs 895cad321adSChris Mason */ 89644b8bd7eSChris Mason return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 897c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 898eaf25d93SChris Mason bio_offset, 8994a69a410SChris Mason __btree_submit_bio_start, 9004a69a410SChris Mason __btree_submit_bio_done); 90144b8bd7eSChris Mason } 90244b8bd7eSChris Mason 9033dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 904784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 905a6bc32b8SMel Gorman struct page *newpage, struct page *page, 906a6bc32b8SMel Gorman enum migrate_mode mode) 907784b4e29SChris Mason { 908784b4e29SChris Mason /* 909784b4e29SChris Mason * we can't safely write a btree page from here, 910784b4e29SChris Mason * we haven't done the locking hook 911784b4e29SChris Mason */ 912784b4e29SChris Mason if (PageDirty(page)) 913784b4e29SChris Mason return -EAGAIN; 914784b4e29SChris Mason /* 915784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 916784b4e29SChris Mason * We must have no buffers or drop them. 917784b4e29SChris Mason */ 918784b4e29SChris Mason if (page_has_private(page) && 919784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 920784b4e29SChris Mason return -EAGAIN; 921a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 922784b4e29SChris Mason } 9233dd1462eSJan Beulich #endif 924784b4e29SChris Mason 9250da5468fSChris Mason 9260da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9270da5468fSChris Mason struct writeback_control *wbc) 9280da5468fSChris Mason { 929d1310b2eSChris Mason struct extent_io_tree *tree; 930d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 931d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 932b9473439SChris Mason struct btrfs_root *root = BTRFS_I(mapping->host)->root; 933793955bcSChris Mason u64 num_dirty; 93424ab9cd8SChris Mason unsigned long thresh = 32 * 1024 * 1024; 935448d640bSChris Mason 936448d640bSChris Mason if (wbc->for_kupdate) 937448d640bSChris Mason return 0; 938448d640bSChris Mason 939b9473439SChris Mason /* this is a bit racy, but that's ok */ 940b9473439SChris Mason num_dirty = root->fs_info->dirty_metadata_bytes; 941d397712bSChris Mason if (num_dirty < thresh) 942793955bcSChris Mason return 0; 943793955bcSChris Mason } 9440b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc); 9450da5468fSChris Mason } 9460da5468fSChris Mason 947b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9485f39d397SChris Mason { 949d1310b2eSChris Mason struct extent_io_tree *tree; 950d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9518ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9525f39d397SChris Mason } 9535f39d397SChris Mason 95470dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9555f39d397SChris Mason { 95698509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 95798509cfcSChris Mason return 0; 9580c4e538bSDavid Sterba /* 9590c4e538bSDavid Sterba * We need to mask out eg. __GFP_HIGHMEM and __GFP_DMA32 as we're doing 9600c4e538bSDavid Sterba * slab allocation from alloc_extent_state down the callchain where 9610c4e538bSDavid Sterba * it'd hit a BUG_ON as those flags are not allowed. 9620c4e538bSDavid Sterba */ 9630c4e538bSDavid Sterba gfp_flags &= ~GFP_SLAB_BUG_MASK; 9640c4e538bSDavid Sterba 9653083ee2eSJosef Bacik return try_release_extent_buffer(page, gfp_flags); 966d98237b3SChris Mason } 967d98237b3SChris Mason 9685f39d397SChris Mason static void btree_invalidatepage(struct page *page, unsigned long offset) 969d98237b3SChris Mason { 970d1310b2eSChris Mason struct extent_io_tree *tree; 971d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9725f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9735f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9749ad6b7bcSChris Mason if (PagePrivate(page)) { 975d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 976d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 9779ad6b7bcSChris Mason ClearPagePrivate(page); 9789ad6b7bcSChris Mason set_page_private(page, 0); 9799ad6b7bcSChris Mason page_cache_release(page); 9809ad6b7bcSChris Mason } 981d98237b3SChris Mason } 982d98237b3SChris Mason 9830b32f4bbSJosef Bacik static int btree_set_page_dirty(struct page *page) 9840b32f4bbSJosef Bacik { 9850b32f4bbSJosef Bacik struct extent_buffer *eb; 9860b32f4bbSJosef Bacik 9870b32f4bbSJosef Bacik BUG_ON(!PagePrivate(page)); 9880b32f4bbSJosef Bacik eb = (struct extent_buffer *)page->private; 9890b32f4bbSJosef Bacik BUG_ON(!eb); 9900b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); 9910b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs)); 9920b32f4bbSJosef Bacik btrfs_assert_tree_locked(eb); 9930b32f4bbSJosef Bacik return __set_page_dirty_nobuffers(page); 9940b32f4bbSJosef Bacik } 9950b32f4bbSJosef Bacik 9967f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 997d98237b3SChris Mason .readpage = btree_readpage, 9980da5468fSChris Mason .writepages = btree_writepages, 9995f39d397SChris Mason .releasepage = btree_releasepage, 10005f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10015a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1002784b4e29SChris Mason .migratepage = btree_migratepage, 10035a92bc88SChris Mason #endif 10040b32f4bbSJosef Bacik .set_page_dirty = btree_set_page_dirty, 1005d98237b3SChris Mason }; 1006123abc88SChris Mason 1007ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1008ca7a79adSChris Mason u64 parent_transid) 1009090d1875SChris Mason { 10105f39d397SChris Mason struct extent_buffer *buf = NULL; 10115f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1012de428b63SChris Mason int ret = 0; 1013090d1875SChris Mason 1014db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10155f39d397SChris Mason if (!buf) 1016090d1875SChris Mason return 0; 1017d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1018bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10195f39d397SChris Mason free_extent_buffer(buf); 1020de428b63SChris Mason return ret; 1021090d1875SChris Mason } 1022090d1875SChris Mason 1023ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1024ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1025ab0fff03SArne Jansen { 1026ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1027ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1028ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1029ab0fff03SArne Jansen int ret; 1030ab0fff03SArne Jansen 1031ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1032ab0fff03SArne Jansen if (!buf) 1033ab0fff03SArne Jansen return 0; 1034ab0fff03SArne Jansen 1035ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1036ab0fff03SArne Jansen 1037ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1038ab0fff03SArne Jansen btree_get_extent, mirror_num); 1039ab0fff03SArne Jansen if (ret) { 1040ab0fff03SArne Jansen free_extent_buffer(buf); 1041ab0fff03SArne Jansen return ret; 1042ab0fff03SArne Jansen } 1043ab0fff03SArne Jansen 1044ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1045ab0fff03SArne Jansen free_extent_buffer(buf); 1046ab0fff03SArne Jansen return -EIO; 10470b32f4bbSJosef Bacik } else if (extent_buffer_uptodate(buf)) { 1048ab0fff03SArne Jansen *eb = buf; 1049ab0fff03SArne Jansen } else { 1050ab0fff03SArne Jansen free_extent_buffer(buf); 1051ab0fff03SArne Jansen } 1052ab0fff03SArne Jansen return 0; 1053ab0fff03SArne Jansen } 1054ab0fff03SArne Jansen 10550999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 10560999df54SChris Mason u64 bytenr, u32 blocksize) 10570999df54SChris Mason { 10580999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10590999df54SChris Mason struct extent_buffer *eb; 10600999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1061f09d1f60SDavid Sterba bytenr, blocksize); 10620999df54SChris Mason return eb; 10630999df54SChris Mason } 10640999df54SChris Mason 10650999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 10660999df54SChris Mason u64 bytenr, u32 blocksize) 10670999df54SChris Mason { 10680999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10690999df54SChris Mason struct extent_buffer *eb; 10700999df54SChris Mason 10710999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1072727011e0SChris Mason bytenr, blocksize); 10730999df54SChris Mason return eb; 10740999df54SChris Mason } 10750999df54SChris Mason 10760999df54SChris Mason 1077e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1078e02119d5SChris Mason { 1079727011e0SChris Mason return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start, 10808aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1081e02119d5SChris Mason } 1082e02119d5SChris Mason 1083e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1084e02119d5SChris Mason { 1085727011e0SChris Mason return filemap_fdatawait_range(buf->pages[0]->mapping, 10868aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1087e02119d5SChris Mason } 1088e02119d5SChris Mason 1089db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1090ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1091e20d96d6SChris Mason { 10925f39d397SChris Mason struct extent_buffer *buf = NULL; 109319c00ddcSChris Mason int ret; 109419c00ddcSChris Mason 1095db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10965f39d397SChris Mason if (!buf) 1097d98237b3SChris Mason return NULL; 1098e4204dedSChris Mason 1099ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 11005f39d397SChris Mason return buf; 1101ce9adaa5SChris Mason 1102eb60ceacSChris Mason } 1103eb60ceacSChris Mason 1104d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11055f39d397SChris Mason struct extent_buffer *buf) 1106ed2ff2cbSChris Mason { 110755c69072SChris Mason if (btrfs_header_generation(buf) == 1108925baeddSChris Mason root->fs_info->running_transaction->transid) { 1109b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1110b4ce94deSChris Mason 1111b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1112b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 1113b9473439SChris Mason if (root->fs_info->dirty_metadata_bytes >= buf->len) 1114b9473439SChris Mason root->fs_info->dirty_metadata_bytes -= buf->len; 1115d5c13f92SJeff Mahoney else { 1116d5c13f92SJeff Mahoney spin_unlock(&root->fs_info->delalloc_lock); 1117d5c13f92SJeff Mahoney btrfs_panic(root->fs_info, -EOVERFLOW, 1118d5c13f92SJeff Mahoney "Can't clear %lu bytes from " 1119533574c6SJoe Perches " dirty_mdatadata_bytes (%llu)", 1120d5c13f92SJeff Mahoney buf->len, 1121d5c13f92SJeff Mahoney root->fs_info->dirty_metadata_bytes); 1122d5c13f92SJeff Mahoney } 1123b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 1124b9473439SChris Mason } 1125b4ce94deSChris Mason 1126b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1127b9473439SChris Mason btrfs_set_lock_blocking(buf); 11280b32f4bbSJosef Bacik clear_extent_buffer_dirty(buf); 1129925baeddSChris Mason } 11305f39d397SChris Mason } 11315f39d397SChris Mason 1132143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 113387ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11349f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1135e20d96d6SChris Mason u64 objectid) 1136d97e63b6SChris Mason { 1137cfaa7295SChris Mason root->node = NULL; 1138a28ec197SChris Mason root->commit_root = NULL; 1139db94535dSChris Mason root->sectorsize = sectorsize; 1140db94535dSChris Mason root->nodesize = nodesize; 1141db94535dSChris Mason root->leafsize = leafsize; 114287ee04ebSChris Mason root->stripesize = stripesize; 1143123abc88SChris Mason root->ref_cows = 0; 11440b86a832SChris Mason root->track_dirty = 0; 1145c71bf099SYan, Zheng root->in_radix = 0; 1146d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1147d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11480b86a832SChris Mason 11490f7d52f4SChris Mason root->objectid = objectid; 11500f7d52f4SChris Mason root->last_trans = 0; 115113a8a7c8SYan, Zheng root->highest_objectid = 0; 115258176a96SJosef Bacik root->name = NULL; 11536bef4d31SEric Paris root->inode_tree = RB_ROOT; 115416cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1155f0486c68SYan, Zheng root->block_rsv = NULL; 1156d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11570b86a832SChris Mason 11580b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11595d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1160d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11615d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1162f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 1163a2135011SChris Mason mutex_init(&root->objectid_mutex); 1164e02119d5SChris Mason mutex_init(&root->log_mutex); 11657237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11667237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11677237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11687237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11697237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11707237f183SYan Zheng atomic_set(&root->log_writers, 0); 11712ecb7923SMiao Xie atomic_set(&root->log_batch, 0); 11728a35d95fSJosef Bacik atomic_set(&root->orphan_inodes, 0); 11737237f183SYan Zheng root->log_transid = 0; 1174257c62e1SChris Mason root->last_log_commit = 0; 1175d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1176f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1177017e5369SChris Mason 11783768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11793768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11806702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 118158176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 11823f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 118358176a96SJosef Bacik init_completion(&root->kobj_unregister); 11846702ed49SChris Mason root->defrag_running = 0; 11854d775673SChris Mason root->root_key.objectid = objectid; 11860ee5dc67SAl Viro root->anon_dev = 0; 11878ea05e3aSAlexander Block 11888ea05e3aSAlexander Block spin_lock_init(&root->root_times_lock); 11893768f368SChris Mason } 11903768f368SChris Mason 1191200a5c17SJeff Mahoney static int __must_check find_and_setup_root(struct btrfs_root *tree_root, 11929f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 11939f5fae2fSChris Mason u64 objectid, 1194e20d96d6SChris Mason struct btrfs_root *root) 11953768f368SChris Mason { 11963768f368SChris Mason int ret; 1197db94535dSChris Mason u32 blocksize; 119884234f3aSYan Zheng u64 generation; 11993768f368SChris Mason 1200db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 120187ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 120287ee04ebSChris Mason root, fs_info, objectid); 12033768f368SChris Mason ret = btrfs_find_last_root(tree_root, objectid, 12043768f368SChris Mason &root->root_item, &root->root_key); 12054df27c4dSYan, Zheng if (ret > 0) 12064df27c4dSYan, Zheng return -ENOENT; 1207200a5c17SJeff Mahoney else if (ret < 0) 1208200a5c17SJeff Mahoney return ret; 12093768f368SChris Mason 121084234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1211db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1212af31f5e5SChris Mason root->commit_root = NULL; 1213db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 121484234f3aSYan Zheng blocksize, generation); 1215b9fab919SChris Mason if (!root->node || !btrfs_buffer_uptodate(root->node, generation, 0)) { 121668433b73SChris Mason free_extent_buffer(root->node); 1217af31f5e5SChris Mason root->node = NULL; 121868433b73SChris Mason return -EIO; 121968433b73SChris Mason } 12204df27c4dSYan, Zheng root->commit_root = btrfs_root_node(root); 1221d97e63b6SChris Mason return 0; 1222d97e63b6SChris Mason } 1223d97e63b6SChris Mason 1224f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12256f07e42eSAl Viro { 12266f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12276f07e42eSAl Viro if (root) 12286f07e42eSAl Viro root->fs_info = fs_info; 12296f07e42eSAl Viro return root; 12306f07e42eSAl Viro } 12316f07e42eSAl Viro 123220897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 123320897f5cSArne Jansen struct btrfs_fs_info *fs_info, 123420897f5cSArne Jansen u64 objectid) 123520897f5cSArne Jansen { 123620897f5cSArne Jansen struct extent_buffer *leaf; 123720897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root; 123820897f5cSArne Jansen struct btrfs_root *root; 123920897f5cSArne Jansen struct btrfs_key key; 124020897f5cSArne Jansen int ret = 0; 124120897f5cSArne Jansen u64 bytenr; 124220897f5cSArne Jansen 124320897f5cSArne Jansen root = btrfs_alloc_root(fs_info); 124420897f5cSArne Jansen if (!root) 124520897f5cSArne Jansen return ERR_PTR(-ENOMEM); 124620897f5cSArne Jansen 124720897f5cSArne Jansen __setup_root(tree_root->nodesize, tree_root->leafsize, 124820897f5cSArne Jansen tree_root->sectorsize, tree_root->stripesize, 124920897f5cSArne Jansen root, fs_info, objectid); 125020897f5cSArne Jansen root->root_key.objectid = objectid; 125120897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY; 125220897f5cSArne Jansen root->root_key.offset = 0; 125320897f5cSArne Jansen 125420897f5cSArne Jansen leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 125520897f5cSArne Jansen 0, objectid, NULL, 0, 0, 0); 125620897f5cSArne Jansen if (IS_ERR(leaf)) { 125720897f5cSArne Jansen ret = PTR_ERR(leaf); 125820897f5cSArne Jansen goto fail; 125920897f5cSArne Jansen } 126020897f5cSArne Jansen 126120897f5cSArne Jansen bytenr = leaf->start; 126220897f5cSArne Jansen memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 126320897f5cSArne Jansen btrfs_set_header_bytenr(leaf, leaf->start); 126420897f5cSArne Jansen btrfs_set_header_generation(leaf, trans->transid); 126520897f5cSArne Jansen btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 126620897f5cSArne Jansen btrfs_set_header_owner(leaf, objectid); 126720897f5cSArne Jansen root->node = leaf; 126820897f5cSArne Jansen 126920897f5cSArne Jansen write_extent_buffer(leaf, fs_info->fsid, 127020897f5cSArne Jansen (unsigned long)btrfs_header_fsid(leaf), 127120897f5cSArne Jansen BTRFS_FSID_SIZE); 127220897f5cSArne Jansen write_extent_buffer(leaf, fs_info->chunk_tree_uuid, 127320897f5cSArne Jansen (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 127420897f5cSArne Jansen BTRFS_UUID_SIZE); 127520897f5cSArne Jansen btrfs_mark_buffer_dirty(leaf); 127620897f5cSArne Jansen 127720897f5cSArne Jansen root->commit_root = btrfs_root_node(root); 127820897f5cSArne Jansen root->track_dirty = 1; 127920897f5cSArne Jansen 128020897f5cSArne Jansen 128120897f5cSArne Jansen root->root_item.flags = 0; 128220897f5cSArne Jansen root->root_item.byte_limit = 0; 128320897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start); 128420897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid); 128520897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0); 128620897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1); 128720897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len); 128820897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0); 128920897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0); 129020897f5cSArne Jansen root->root_item.drop_level = 0; 129120897f5cSArne Jansen 129220897f5cSArne Jansen key.objectid = objectid; 129320897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY; 129420897f5cSArne Jansen key.offset = 0; 129520897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); 129620897f5cSArne Jansen if (ret) 129720897f5cSArne Jansen goto fail; 129820897f5cSArne Jansen 129920897f5cSArne Jansen btrfs_tree_unlock(leaf); 130020897f5cSArne Jansen 130120897f5cSArne Jansen fail: 130220897f5cSArne Jansen if (ret) 130320897f5cSArne Jansen return ERR_PTR(ret); 130420897f5cSArne Jansen 130520897f5cSArne Jansen return root; 130620897f5cSArne Jansen } 130720897f5cSArne Jansen 13087237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1309e02119d5SChris Mason struct btrfs_fs_info *fs_info) 13100f7d52f4SChris Mason { 13110f7d52f4SChris Mason struct btrfs_root *root; 13120f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13137237f183SYan Zheng struct extent_buffer *leaf; 1314e02119d5SChris Mason 13156f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1316e02119d5SChris Mason if (!root) 13177237f183SYan Zheng return ERR_PTR(-ENOMEM); 1318e02119d5SChris Mason 1319e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1320e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1321e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1322e02119d5SChris Mason 1323e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1324e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1325e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 13267237f183SYan Zheng /* 13277237f183SYan Zheng * log trees do not get reference counted because they go away 13287237f183SYan Zheng * before a real commit is actually done. They do store pointers 13297237f183SYan Zheng * to file data extents, and those reference counts still get 13307237f183SYan Zheng * updated (along with back refs to the log tree). 13317237f183SYan Zheng */ 1332e02119d5SChris Mason root->ref_cows = 0; 1333e02119d5SChris Mason 13345d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 133566d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 13365581a51aSJan Schmidt 0, 0, 0); 13377237f183SYan Zheng if (IS_ERR(leaf)) { 13387237f183SYan Zheng kfree(root); 13397237f183SYan Zheng return ERR_CAST(leaf); 13407237f183SYan Zheng } 1341e02119d5SChris Mason 13425d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 13435d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 13445d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 13455d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 13465d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 13477237f183SYan Zheng root->node = leaf; 1348e02119d5SChris Mason 1349e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1350e02119d5SChris Mason (unsigned long)btrfs_header_fsid(root->node), 1351e02119d5SChris Mason BTRFS_FSID_SIZE); 1352e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1353e02119d5SChris Mason btrfs_tree_unlock(root->node); 13547237f183SYan Zheng return root; 13557237f183SYan Zheng } 13567237f183SYan Zheng 13577237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 13587237f183SYan Zheng struct btrfs_fs_info *fs_info) 13597237f183SYan Zheng { 13607237f183SYan Zheng struct btrfs_root *log_root; 13617237f183SYan Zheng 13627237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 13637237f183SYan Zheng if (IS_ERR(log_root)) 13647237f183SYan Zheng return PTR_ERR(log_root); 13657237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 13667237f183SYan Zheng fs_info->log_root_tree = log_root; 13677237f183SYan Zheng return 0; 13687237f183SYan Zheng } 13697237f183SYan Zheng 13707237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13717237f183SYan Zheng struct btrfs_root *root) 13727237f183SYan Zheng { 13737237f183SYan Zheng struct btrfs_root *log_root; 13747237f183SYan Zheng struct btrfs_inode_item *inode_item; 13757237f183SYan Zheng 13767237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 13777237f183SYan Zheng if (IS_ERR(log_root)) 13787237f183SYan Zheng return PTR_ERR(log_root); 13797237f183SYan Zheng 13807237f183SYan Zheng log_root->last_trans = trans->transid; 13817237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13827237f183SYan Zheng 13837237f183SYan Zheng inode_item = &log_root->root_item.inode; 13847237f183SYan Zheng inode_item->generation = cpu_to_le64(1); 13857237f183SYan Zheng inode_item->size = cpu_to_le64(3); 13867237f183SYan Zheng inode_item->nlink = cpu_to_le32(1); 13877237f183SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 13887237f183SYan Zheng inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 13897237f183SYan Zheng 13905d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13917237f183SYan Zheng 13927237f183SYan Zheng WARN_ON(root->log_root); 13937237f183SYan Zheng root->log_root = log_root; 13947237f183SYan Zheng root->log_transid = 0; 1395257c62e1SChris Mason root->last_log_commit = 0; 1396e02119d5SChris Mason return 0; 1397e02119d5SChris Mason } 1398e02119d5SChris Mason 1399e02119d5SChris Mason struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, 1400e02119d5SChris Mason struct btrfs_key *location) 1401e02119d5SChris Mason { 1402e02119d5SChris Mason struct btrfs_root *root; 1403e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 14040f7d52f4SChris Mason struct btrfs_path *path; 14055f39d397SChris Mason struct extent_buffer *l; 140684234f3aSYan Zheng u64 generation; 1407db94535dSChris Mason u32 blocksize; 14080f7d52f4SChris Mason int ret = 0; 14098ea05e3aSAlexander Block int slot; 14100f7d52f4SChris Mason 14116f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 14120cf6c620SChris Mason if (!root) 14130f7d52f4SChris Mason return ERR_PTR(-ENOMEM); 14140f7d52f4SChris Mason if (location->offset == (u64)-1) { 1415db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 14160f7d52f4SChris Mason location->objectid, root); 14170f7d52f4SChris Mason if (ret) { 14180f7d52f4SChris Mason kfree(root); 14190f7d52f4SChris Mason return ERR_PTR(ret); 14200f7d52f4SChris Mason } 142113a8a7c8SYan, Zheng goto out; 14220f7d52f4SChris Mason } 14230f7d52f4SChris Mason 1424db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 142587ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 142687ee04ebSChris Mason root, fs_info, location->objectid); 14270f7d52f4SChris Mason 14280f7d52f4SChris Mason path = btrfs_alloc_path(); 1429db5b493aSTsutomu Itoh if (!path) { 1430db5b493aSTsutomu Itoh kfree(root); 1431db5b493aSTsutomu Itoh return ERR_PTR(-ENOMEM); 1432db5b493aSTsutomu Itoh } 14330f7d52f4SChris Mason ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 143413a8a7c8SYan, Zheng if (ret == 0) { 14355f39d397SChris Mason l = path->nodes[0]; 14368ea05e3aSAlexander Block slot = path->slots[0]; 14378ea05e3aSAlexander Block btrfs_read_root_item(tree_root, l, slot, &root->root_item); 143844b36eb2SYan Zheng memcpy(&root->root_key, location, sizeof(*location)); 143913a8a7c8SYan, Zheng } 14400f7d52f4SChris Mason btrfs_free_path(path); 14410f7d52f4SChris Mason if (ret) { 14425e540f77STsutomu Itoh kfree(root); 144313a8a7c8SYan, Zheng if (ret > 0) 144413a8a7c8SYan, Zheng ret = -ENOENT; 14450f7d52f4SChris Mason return ERR_PTR(ret); 14460f7d52f4SChris Mason } 144713a8a7c8SYan, Zheng 144884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1449db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1450db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 145184234f3aSYan Zheng blocksize, generation); 14525d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 145379787eaaSJeff Mahoney BUG_ON(!root->node); /* -ENOMEM */ 145413a8a7c8SYan, Zheng out: 145508fe4db1SLi Zefan if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 14560f7d52f4SChris Mason root->ref_cows = 1; 145708fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 145808fe4db1SLi Zefan } 145913a8a7c8SYan, Zheng 14605eda7b5eSChris Mason return root; 14615eda7b5eSChris Mason } 14625eda7b5eSChris Mason 1463edbd8d4eSChris Mason struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 1464edbd8d4eSChris Mason struct btrfs_key *location) 14655eda7b5eSChris Mason { 14665eda7b5eSChris Mason struct btrfs_root *root; 14675eda7b5eSChris Mason int ret; 14685eda7b5eSChris Mason 1469edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1470edbd8d4eSChris Mason return fs_info->tree_root; 1471edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1472edbd8d4eSChris Mason return fs_info->extent_root; 14738f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 14748f18cf13SChris Mason return fs_info->chunk_root; 14758f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 14768f18cf13SChris Mason return fs_info->dev_root; 14770403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 14780403e47eSYan Zheng return fs_info->csum_root; 1479bcef60f2SArne Jansen if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID) 1480bcef60f2SArne Jansen return fs_info->quota_root ? fs_info->quota_root : 1481bcef60f2SArne Jansen ERR_PTR(-ENOENT); 14824df27c4dSYan, Zheng again: 14834df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 14845eda7b5eSChris Mason root = radix_tree_lookup(&fs_info->fs_roots_radix, 14855eda7b5eSChris Mason (unsigned long)location->objectid); 14864df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 14875eda7b5eSChris Mason if (root) 14885eda7b5eSChris Mason return root; 14895eda7b5eSChris Mason 1490e02119d5SChris Mason root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 14915eda7b5eSChris Mason if (IS_ERR(root)) 14925eda7b5eSChris Mason return root; 14933394e160SChris Mason 1494581bb050SLi Zefan root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1495581bb050SLi Zefan root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1496581bb050SLi Zefan GFP_NOFS); 149735a30d7cSDavid Sterba if (!root->free_ino_pinned || !root->free_ino_ctl) { 149835a30d7cSDavid Sterba ret = -ENOMEM; 1499581bb050SLi Zefan goto fail; 150035a30d7cSDavid Sterba } 1501581bb050SLi Zefan 1502581bb050SLi Zefan btrfs_init_free_ino_ctl(root); 1503581bb050SLi Zefan mutex_init(&root->fs_commit_mutex); 1504581bb050SLi Zefan spin_lock_init(&root->cache_lock); 1505581bb050SLi Zefan init_waitqueue_head(&root->cache_wait); 1506581bb050SLi Zefan 15070ee5dc67SAl Viro ret = get_anon_bdev(&root->anon_dev); 1508ac08aedfSChris Mason if (ret) 1509ac08aedfSChris Mason goto fail; 15103394e160SChris Mason 1511d68fc57bSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) { 1512d68fc57bSYan, Zheng ret = -ENOENT; 1513d68fc57bSYan, Zheng goto fail; 1514d68fc57bSYan, Zheng } 1515d68fc57bSYan, Zheng 1516d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1517d68fc57bSYan, Zheng if (ret < 0) 1518d68fc57bSYan, Zheng goto fail; 1519d68fc57bSYan, Zheng if (ret == 0) 1520d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1521d68fc57bSYan, Zheng 15224df27c4dSYan, Zheng ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 15234df27c4dSYan, Zheng if (ret) 15244df27c4dSYan, Zheng goto fail; 15254df27c4dSYan, Zheng 15264df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 15272619ba1fSChris Mason ret = radix_tree_insert(&fs_info->fs_roots_radix, 15282619ba1fSChris Mason (unsigned long)root->root_key.objectid, 15290f7d52f4SChris Mason root); 1530d68fc57bSYan, Zheng if (ret == 0) 15314df27c4dSYan, Zheng root->in_radix = 1; 1532d68fc57bSYan, Zheng 15334df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 15344df27c4dSYan, Zheng radix_tree_preload_end(); 15350f7d52f4SChris Mason if (ret) { 15364df27c4dSYan, Zheng if (ret == -EEXIST) { 15374df27c4dSYan, Zheng free_fs_root(root); 15384df27c4dSYan, Zheng goto again; 15390f7d52f4SChris Mason } 15404df27c4dSYan, Zheng goto fail; 15414df27c4dSYan, Zheng } 15424df27c4dSYan, Zheng 1543edbd8d4eSChris Mason ret = btrfs_find_dead_roots(fs_info->tree_root, 15445d4f98a2SYan Zheng root->root_key.objectid); 15454df27c4dSYan, Zheng WARN_ON(ret); 1546edbd8d4eSChris Mason return root; 15474df27c4dSYan, Zheng fail: 15484df27c4dSYan, Zheng free_fs_root(root); 15494df27c4dSYan, Zheng return ERR_PTR(ret); 1550edbd8d4eSChris Mason } 1551edbd8d4eSChris Mason 155204160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 155304160088SChris Mason { 155404160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 155504160088SChris Mason int ret = 0; 155604160088SChris Mason struct btrfs_device *device; 155704160088SChris Mason struct backing_dev_info *bdi; 1558b7967db7SChris Mason 15591f78160cSXiao Guangrong rcu_read_lock(); 15601f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1561dfe25020SChris Mason if (!device->bdev) 1562dfe25020SChris Mason continue; 156304160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 156404160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 156504160088SChris Mason ret = 1; 156604160088SChris Mason break; 156704160088SChris Mason } 156804160088SChris Mason } 15691f78160cSXiao Guangrong rcu_read_unlock(); 157004160088SChris Mason return ret; 157104160088SChris Mason } 157204160088SChris Mason 157338b66988SChris Mason /* 1574ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1575ad081f14SJens Axboe * bdi again. 1576ad081f14SJens Axboe */ 157704160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 157804160088SChris Mason { 1579ad081f14SJens Axboe int err; 1580ad081f14SJens Axboe 1581ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1582e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1583ad081f14SJens Axboe if (err) 1584ad081f14SJens Axboe return err; 1585ad081f14SJens Axboe 15864575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 158704160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 158804160088SChris Mason bdi->congested_data = info; 158904160088SChris Mason return 0; 159004160088SChris Mason } 159104160088SChris Mason 15928b712842SChris Mason /* 15938b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 15948b712842SChris Mason * functions. This is where read checksum verification actually happens 15958b712842SChris Mason */ 15968b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1597ce9adaa5SChris Mason { 1598ce9adaa5SChris Mason struct bio *bio; 15998b712842SChris Mason struct end_io_wq *end_io_wq; 16008b712842SChris Mason struct btrfs_fs_info *fs_info; 1601ce9adaa5SChris Mason int error; 1602ce9adaa5SChris Mason 16038b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1604ce9adaa5SChris Mason bio = end_io_wq->bio; 16058b712842SChris Mason fs_info = end_io_wq->info; 16068b712842SChris Mason 1607ce9adaa5SChris Mason error = end_io_wq->error; 1608ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1609ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1610ce9adaa5SChris Mason kfree(end_io_wq); 1611ce9adaa5SChris Mason bio_endio(bio, error); 1612ce9adaa5SChris Mason } 161344b8bd7eSChris Mason 1614a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1615a74a4b97SChris Mason { 1616a74a4b97SChris Mason struct btrfs_root *root = arg; 1617a74a4b97SChris Mason 1618a74a4b97SChris Mason do { 161976dda93cSYan, Zheng if (!(root->fs_info->sb->s_flags & MS_RDONLY) && 162076dda93cSYan, Zheng mutex_trylock(&root->fs_info->cleaner_mutex)) { 162124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 1622a74a4b97SChris Mason btrfs_clean_old_snapshots(root); 1623a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 16244cb5300bSChris Mason btrfs_run_defrag_inodes(root->fs_info); 162576dda93cSYan, Zheng } 1626a74a4b97SChris Mason 1627a0acae0eSTejun Heo if (!try_to_freeze()) { 1628a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16298929ecfaSYan, Zheng if (!kthread_should_stop()) 1630a74a4b97SChris Mason schedule(); 1631a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1632a74a4b97SChris Mason } 1633a74a4b97SChris Mason } while (!kthread_should_stop()); 1634a74a4b97SChris Mason return 0; 1635a74a4b97SChris Mason } 1636a74a4b97SChris Mason 1637a74a4b97SChris Mason static int transaction_kthread(void *arg) 1638a74a4b97SChris Mason { 1639a74a4b97SChris Mason struct btrfs_root *root = arg; 1640a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1641a74a4b97SChris Mason struct btrfs_transaction *cur; 16428929ecfaSYan, Zheng u64 transid; 1643a74a4b97SChris Mason unsigned long now; 1644a74a4b97SChris Mason unsigned long delay; 1645914b2007SJan Kara bool cannot_commit; 1646a74a4b97SChris Mason 1647a74a4b97SChris Mason do { 1648914b2007SJan Kara cannot_commit = false; 1649a74a4b97SChris Mason delay = HZ * 30; 1650a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1651a74a4b97SChris Mason 1652a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1653a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1654a74a4b97SChris Mason if (!cur) { 1655a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1656a74a4b97SChris Mason goto sleep; 1657a74a4b97SChris Mason } 165831153d81SYan Zheng 1659a74a4b97SChris Mason now = get_seconds(); 16608929ecfaSYan, Zheng if (!cur->blocked && 16618929ecfaSYan, Zheng (now < cur->start_time || now - cur->start_time < 30)) { 1662a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1663a74a4b97SChris Mason delay = HZ * 5; 1664a74a4b97SChris Mason goto sleep; 1665a74a4b97SChris Mason } 16668929ecfaSYan, Zheng transid = cur->transid; 1667a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 166856bec294SChris Mason 166979787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */ 16707a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 1671914b2007SJan Kara if (IS_ERR(trans)) { 1672914b2007SJan Kara cannot_commit = true; 167379787eaaSJeff Mahoney goto sleep; 1674914b2007SJan Kara } 16758929ecfaSYan, Zheng if (transid == trans->transid) { 167679787eaaSJeff Mahoney btrfs_commit_transaction(trans, root); 16778929ecfaSYan, Zheng } else { 16788929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 16798929ecfaSYan, Zheng } 1680a74a4b97SChris Mason sleep: 1681a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1682a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1683a74a4b97SChris Mason 1684a0acae0eSTejun Heo if (!try_to_freeze()) { 1685a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16868929ecfaSYan, Zheng if (!kthread_should_stop() && 1687914b2007SJan Kara (!btrfs_transaction_blocked(root->fs_info) || 1688914b2007SJan Kara cannot_commit)) 1689a74a4b97SChris Mason schedule_timeout(delay); 1690a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1691a74a4b97SChris Mason } 1692a74a4b97SChris Mason } while (!kthread_should_stop()); 1693a74a4b97SChris Mason return 0; 1694a74a4b97SChris Mason } 1695a74a4b97SChris Mason 1696af31f5e5SChris Mason /* 1697af31f5e5SChris Mason * this will find the highest generation in the array of 1698af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1699af31f5e5SChris Mason * or -1 if we can't find anything. 1700af31f5e5SChris Mason * 1701af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1702af31f5e5SChris Mason * generation of the latest root in the array with the generation 1703af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1704af31f5e5SChris Mason */ 1705af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1706af31f5e5SChris Mason { 1707af31f5e5SChris Mason u64 cur; 1708af31f5e5SChris Mason int newest_index = -1; 1709af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1710af31f5e5SChris Mason int i; 1711af31f5e5SChris Mason 1712af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1713af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1714af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1715af31f5e5SChris Mason if (cur == newest_gen) 1716af31f5e5SChris Mason newest_index = i; 1717af31f5e5SChris Mason } 1718af31f5e5SChris Mason 1719af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1720af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1721af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1722af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1723af31f5e5SChris Mason if (cur == newest_gen) 1724af31f5e5SChris Mason newest_index = 0; 1725af31f5e5SChris Mason } 1726af31f5e5SChris Mason return newest_index; 1727af31f5e5SChris Mason } 1728af31f5e5SChris Mason 1729af31f5e5SChris Mason 1730af31f5e5SChris Mason /* 1731af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1732af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1733af31f5e5SChris Mason * field in the fs_info struct 1734af31f5e5SChris Mason */ 1735af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1736af31f5e5SChris Mason u64 newest_gen) 1737af31f5e5SChris Mason { 1738af31f5e5SChris Mason int newest_index = -1; 1739af31f5e5SChris Mason 1740af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1741af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1742af31f5e5SChris Mason if (newest_index == -1) { 1743af31f5e5SChris Mason info->backup_root_index = 0; 1744af31f5e5SChris Mason } else { 1745af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1746af31f5e5SChris Mason } 1747af31f5e5SChris Mason } 1748af31f5e5SChris Mason 1749af31f5e5SChris Mason /* 1750af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1751af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1752af31f5e5SChris Mason * done 1753af31f5e5SChris Mason */ 1754af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1755af31f5e5SChris Mason { 1756af31f5e5SChris Mason int next_backup; 1757af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1758af31f5e5SChris Mason int last_backup; 1759af31f5e5SChris Mason 1760af31f5e5SChris Mason next_backup = info->backup_root_index; 1761af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1762af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1763af31f5e5SChris Mason 1764af31f5e5SChris Mason /* 1765af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1766af31f5e5SChris Mason * this happens only at umount 1767af31f5e5SChris Mason */ 1768af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1769af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1770af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1771af31f5e5SChris Mason next_backup = last_backup; 1772af31f5e5SChris Mason 1773af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1774af31f5e5SChris Mason 1775af31f5e5SChris Mason /* 1776af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1777af31f5e5SChris Mason * regardless of which ones we use today 1778af31f5e5SChris Mason */ 1779af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1780af31f5e5SChris Mason 1781af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1782af31f5e5SChris Mason 1783af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1784af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1785af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1786af31f5e5SChris Mason 1787af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1788af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1789af31f5e5SChris Mason 1790af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1791af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1792af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1793af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1794af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1795af31f5e5SChris Mason 1796af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1797af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1798af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1799af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1800af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1801af31f5e5SChris Mason 18027c7e82a7SChris Mason /* 18037c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 18047c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 18057c7e82a7SChris Mason */ 18067c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 18077c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 18087c7e82a7SChris Mason info->fs_root->node->start); 1809af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1810af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1811af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1812af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 18137c7e82a7SChris Mason } 1814af31f5e5SChris Mason 1815af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1816af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1817af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1818af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1819af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1820af31f5e5SChris Mason 1821af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1822af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1823af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1824af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1825af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1826af31f5e5SChris Mason 1827af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1828af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1829af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1830af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1831af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1832af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1833af31f5e5SChris Mason 1834af31f5e5SChris Mason /* 1835af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1836af31f5e5SChris Mason * for the next commit 1837af31f5e5SChris Mason */ 1838af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1839af31f5e5SChris Mason &info->super_for_commit->super_roots, 1840af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1841af31f5e5SChris Mason } 1842af31f5e5SChris Mason 1843af31f5e5SChris Mason /* 1844af31f5e5SChris Mason * this copies info out of the root backup array and back into 1845af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1846af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1847af31f5e5SChris Mason * tried and the last backup index you used. 1848af31f5e5SChris Mason * 1849af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1850af31f5e5SChris Mason */ 1851af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1852af31f5e5SChris Mason struct btrfs_super_block *super, 1853af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1854af31f5e5SChris Mason { 1855af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1856af31f5e5SChris Mason int newest = *backup_index; 1857af31f5e5SChris Mason 1858af31f5e5SChris Mason if (*num_backups_tried == 0) { 1859af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1860af31f5e5SChris Mason 1861af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1862af31f5e5SChris Mason if (newest == -1) 1863af31f5e5SChris Mason return -1; 1864af31f5e5SChris Mason 1865af31f5e5SChris Mason *backup_index = newest; 1866af31f5e5SChris Mason *num_backups_tried = 1; 1867af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1868af31f5e5SChris Mason /* we've tried all the backups, all done */ 1869af31f5e5SChris Mason return -1; 1870af31f5e5SChris Mason } else { 1871af31f5e5SChris Mason /* jump to the next oldest backup */ 1872af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1873af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1874af31f5e5SChris Mason *backup_index = newest; 1875af31f5e5SChris Mason *num_backups_tried += 1; 1876af31f5e5SChris Mason } 1877af31f5e5SChris Mason root_backup = super->super_roots + newest; 1878af31f5e5SChris Mason 1879af31f5e5SChris Mason btrfs_set_super_generation(super, 1880af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1881af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1882af31f5e5SChris Mason btrfs_set_super_root_level(super, 1883af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1884af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1885af31f5e5SChris Mason 1886af31f5e5SChris Mason /* 1887af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1888af31f5e5SChris Mason * need a fsck 1889af31f5e5SChris Mason */ 1890af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1891af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1892af31f5e5SChris Mason return 0; 1893af31f5e5SChris Mason } 1894af31f5e5SChris Mason 1895af31f5e5SChris Mason /* helper to cleanup tree roots */ 1896af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 1897af31f5e5SChris Mason { 1898af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 1899af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 1900af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 1901af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 1902af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 1903af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 1904af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 1905af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 1906bcef60f2SArne Jansen if (info->quota_root) { 1907bcef60f2SArne Jansen free_extent_buffer(info->quota_root->node); 1908bcef60f2SArne Jansen free_extent_buffer(info->quota_root->commit_root); 1909bcef60f2SArne Jansen } 1910af31f5e5SChris Mason 1911af31f5e5SChris Mason info->tree_root->node = NULL; 1912af31f5e5SChris Mason info->tree_root->commit_root = NULL; 1913af31f5e5SChris Mason info->dev_root->node = NULL; 1914af31f5e5SChris Mason info->dev_root->commit_root = NULL; 1915af31f5e5SChris Mason info->extent_root->node = NULL; 1916af31f5e5SChris Mason info->extent_root->commit_root = NULL; 1917af31f5e5SChris Mason info->csum_root->node = NULL; 1918af31f5e5SChris Mason info->csum_root->commit_root = NULL; 1919bcef60f2SArne Jansen if (info->quota_root) { 1920bcef60f2SArne Jansen info->quota_root->node = NULL; 1921bcef60f2SArne Jansen info->quota_root->commit_root = NULL; 1922bcef60f2SArne Jansen } 1923af31f5e5SChris Mason 1924af31f5e5SChris Mason if (chunk_root) { 1925af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 1926af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 1927af31f5e5SChris Mason info->chunk_root->node = NULL; 1928af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 1929af31f5e5SChris Mason } 1930af31f5e5SChris Mason } 1931af31f5e5SChris Mason 1932af31f5e5SChris Mason 1933ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 1934dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 1935dfe25020SChris Mason char *options) 1936eb60ceacSChris Mason { 1937db94535dSChris Mason u32 sectorsize; 1938db94535dSChris Mason u32 nodesize; 1939db94535dSChris Mason u32 leafsize; 1940db94535dSChris Mason u32 blocksize; 194187ee04ebSChris Mason u32 stripesize; 194284234f3aSYan Zheng u64 generation; 1943f2b636e8SJosef Bacik u64 features; 19443de4586cSChris Mason struct btrfs_key location; 1945a061fc8dSChris Mason struct buffer_head *bh; 19464d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 1947815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1948f84a8bd6SAl Viro struct btrfs_root *tree_root; 19494d34b278SIlya Dryomov struct btrfs_root *extent_root; 19504d34b278SIlya Dryomov struct btrfs_root *csum_root; 19514d34b278SIlya Dryomov struct btrfs_root *chunk_root; 19524d34b278SIlya Dryomov struct btrfs_root *dev_root; 1953bcef60f2SArne Jansen struct btrfs_root *quota_root; 1954e02119d5SChris Mason struct btrfs_root *log_tree_root; 1955eb60ceacSChris Mason int ret; 1956e58ca020SYan int err = -EINVAL; 1957af31f5e5SChris Mason int num_backups_tried = 0; 1958af31f5e5SChris Mason int backup_index = 0; 19594543df7eSChris Mason 1960f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 19616f07e42eSAl Viro extent_root = fs_info->extent_root = btrfs_alloc_root(fs_info); 19626f07e42eSAl Viro csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); 19636f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 19646f07e42eSAl Viro dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); 1965bcef60f2SArne Jansen quota_root = fs_info->quota_root = btrfs_alloc_root(fs_info); 19668790d502SChris Mason 1967f84a8bd6SAl Viro if (!tree_root || !extent_root || !csum_root || 1968bcef60f2SArne Jansen !chunk_root || !dev_root || !quota_root) { 196939279cc3SChris Mason err = -ENOMEM; 197039279cc3SChris Mason goto fail; 197139279cc3SChris Mason } 197276dda93cSYan, Zheng 197376dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 197476dda93cSYan, Zheng if (ret) { 197576dda93cSYan, Zheng err = ret; 197676dda93cSYan, Zheng goto fail; 197776dda93cSYan, Zheng } 197876dda93cSYan, Zheng 197976dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 198076dda93cSYan, Zheng if (ret) { 198176dda93cSYan, Zheng err = ret; 198276dda93cSYan, Zheng goto fail_srcu; 198376dda93cSYan, Zheng } 198476dda93cSYan, Zheng 198576dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 198676dda93cSYan, Zheng if (!fs_info->btree_inode) { 198776dda93cSYan, Zheng err = -ENOMEM; 198876dda93cSYan, Zheng goto fail_bdi; 198976dda93cSYan, Zheng } 199076dda93cSYan, Zheng 1991a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 19921561dedaSMiao Xie 199376dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 19948fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 1995facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 199624bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 199719c00ddcSChris Mason INIT_LIST_HEAD(&fs_info->hashers); 1998ea8c2819SChris Mason INIT_LIST_HEAD(&fs_info->delalloc_inodes); 19995a3f23d5SChris Mason INIT_LIST_HEAD(&fs_info->ordered_operations); 200011833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 20011832a6d5SChris Mason spin_lock_init(&fs_info->delalloc_lock); 2002a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 200331153d81SYan Zheng spin_lock_init(&fs_info->ref_cache_lock); 200476dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 200524bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 20064cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 20072bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 2008f29021b2SJan Schmidt spin_lock_init(&fs_info->tree_mod_seq_lock); 2009f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock); 20107585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 201119c00ddcSChris Mason 201258176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 20130b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 20146324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 2015f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); 20160b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 201766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv, 201866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL); 201966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delalloc_block_rsv, 202066d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELALLOC); 202166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS); 202266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK); 202366d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY); 202466d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv, 202566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS); 2026cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 2027771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 20288c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 20290986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 20304cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 2031f29021b2SJan Schmidt atomic_set(&fs_info->tree_mod_seq, 0); 2032e20d96d6SChris Mason fs_info->sb = sb; 20336f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 20349ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 20354cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 2036a4abeea4SJosef Bacik fs_info->trans_no_join = 0; 20372bf64758SJosef Bacik fs_info->free_chunk_space = 0; 2038f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT; 2039c8b97818SChris Mason 204090519d66SArne Jansen /* readahead state */ 204190519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 204290519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 20432c90e5d6SChris Mason 2044b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2045b34b086cSChris Mason num_online_cpus() + 2, 8); 20460afbaf8cSChris Mason 20473eaa2885SChris Mason INIT_LIST_HEAD(&fs_info->ordered_extents); 20483eaa2885SChris Mason spin_lock_init(&fs_info->ordered_extent_lock); 204916cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 205016cdcec7SMiao Xie GFP_NOFS); 205116cdcec7SMiao Xie if (!fs_info->delayed_root) { 205216cdcec7SMiao Xie err = -ENOMEM; 205316cdcec7SMiao Xie goto fail_iput; 205416cdcec7SMiao Xie } 205516cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 20563eaa2885SChris Mason 2057a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2058a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2059a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2060a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2061a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2062a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2063a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2064a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 206521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 206621adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 206721adbd5cSStefan Behrens #endif 2068a2de733cSArne Jansen 2069c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2070c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2071837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2072837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2073a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2074c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2075837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2076a061fc8dSChris Mason 20770afbaf8cSChris Mason sb->s_blocksize = 4096; 20780afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 207932a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 20800afbaf8cSChris Mason 208176dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2082bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 20830afbaf8cSChris Mason /* 20840afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 20850afbaf8cSChris Mason * the real end of the address space is determined by all of 20860afbaf8cSChris Mason * the devices in the system 20870afbaf8cSChris Mason */ 20880afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2089d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 209004160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 209104160088SChris Mason 20925d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2093d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2094f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 20950b32f4bbSJosef Bacik BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2096a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 20970da5468fSChris Mason 2098d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2099d1310b2eSChris Mason 21000f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 21010f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 21020f7d52f4SChris Mason sizeof(struct btrfs_key)); 210372ac3c0dSJosef Bacik set_bit(BTRFS_INODE_DUMMY, 210472ac3c0dSJosef Bacik &BTRFS_I(fs_info->btree_inode)->runtime_flags); 210522b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 210639279cc3SChris Mason 2107e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 21086bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2109925baeddSChris Mason 211011833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2111f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 211211833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2113f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 211411833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2115e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2116f9295749SChris Mason 2117d98237b3SChris Mason 21185a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 211930ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2120925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2121925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2122a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2123a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2124276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2125c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 212676dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2127fa9c0d79SChris Mason 2128416ac51dSArne Jansen spin_lock_init(&fs_info->qgroup_lock); 2129416ac51dSArne Jansen fs_info->qgroup_tree = RB_ROOT; 2130416ac51dSArne Jansen INIT_LIST_HEAD(&fs_info->dirty_qgroups); 2131416ac51dSArne Jansen fs_info->qgroup_seq = 1; 2132416ac51dSArne Jansen fs_info->quota_enabled = 0; 2133416ac51dSArne Jansen fs_info->pending_quota_state = 0; 2134416ac51dSArne Jansen 2135fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2136fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2137fa9c0d79SChris Mason 21387d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 21393768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2140bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 21414854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 21429a8dd150SChris Mason 21430b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 21442c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 21457eccb903SChris Mason 21463c4bb26bSChris Mason invalidate_bdev(fs_devices->latest_bdev); 2147a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 214820b45077SDave Young if (!bh) { 214920b45077SDave Young err = -EINVAL; 215016cdcec7SMiao Xie goto fail_alloc; 215120b45077SDave Young } 215239279cc3SChris Mason 21536c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 21546c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 21556c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2156a061fc8dSChris Mason brelse(bh); 21575f39d397SChris Mason 21586c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 21590b86a832SChris Mason 21606c41761fSDavid Sterba disk_super = fs_info->super_copy; 21610f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 216216cdcec7SMiao Xie goto fail_alloc; 21630f7d52f4SChris Mason 2164acce952bSliubo /* check FS state, whether FS is broken. */ 2165acce952bSliubo fs_info->fs_state |= btrfs_super_flags(disk_super); 2166acce952bSliubo 2167fcd1f065SDavid Sterba ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 2168fcd1f065SDavid Sterba if (ret) { 2169fcd1f065SDavid Sterba printk(KERN_ERR "btrfs: superblock contains fatal errors\n"); 2170fcd1f065SDavid Sterba err = ret; 2171fcd1f065SDavid Sterba goto fail_alloc; 2172fcd1f065SDavid Sterba } 2173acce952bSliubo 217475e7cb7fSLiu Bo /* 2175af31f5e5SChris Mason * run through our array of backup supers and setup 2176af31f5e5SChris Mason * our ring pointer to the oldest one 2177af31f5e5SChris Mason */ 2178af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2179af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2180af31f5e5SChris Mason 2181af31f5e5SChris Mason /* 218275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 218375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 218475e7cb7fSLiu Bo */ 218575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 218675e7cb7fSLiu Bo 21872b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 21882b82032cSYan Zheng if (ret) { 21892b82032cSYan Zheng err = ret; 219016cdcec7SMiao Xie goto fail_alloc; 21912b82032cSYan Zheng } 2192dfe25020SChris Mason 2193f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2194f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2195f2b636e8SJosef Bacik if (features) { 2196f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2197f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 219821380931SJoel Becker (unsigned long long)features); 2199f2b636e8SJosef Bacik err = -EINVAL; 220016cdcec7SMiao Xie goto fail_alloc; 2201f2b636e8SJosef Bacik } 2202f2b636e8SJosef Bacik 2203727011e0SChris Mason if (btrfs_super_leafsize(disk_super) != 2204727011e0SChris Mason btrfs_super_nodesize(disk_super)) { 2205727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2206727011e0SChris Mason "blocksizes don't match. node %d leaf %d\n", 2207727011e0SChris Mason btrfs_super_nodesize(disk_super), 2208727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2209727011e0SChris Mason err = -EINVAL; 2210727011e0SChris Mason goto fail_alloc; 2211727011e0SChris Mason } 2212727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) { 2213727011e0SChris Mason printk(KERN_ERR "BTRFS: couldn't mount because metadata " 2214727011e0SChris Mason "blocksize (%d) was too large\n", 2215727011e0SChris Mason btrfs_super_leafsize(disk_super)); 2216727011e0SChris Mason err = -EINVAL; 2217727011e0SChris Mason goto fail_alloc; 2218727011e0SChris Mason } 2219727011e0SChris Mason 22205d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 22215d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 222269e380d1SLi Zefan if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2223a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2224727011e0SChris Mason 2225727011e0SChris Mason /* 2226727011e0SChris Mason * flag our filesystem as having big metadata blocks if 2227727011e0SChris Mason * they are bigger than the page size 2228727011e0SChris Mason */ 2229727011e0SChris Mason if (btrfs_super_leafsize(disk_super) > PAGE_CACHE_SIZE) { 2230727011e0SChris Mason if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA)) 2231727011e0SChris Mason printk(KERN_INFO "btrfs flagging fs with big metadata feature\n"); 2232727011e0SChris Mason features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA; 2233727011e0SChris Mason } 2234727011e0SChris Mason 2235bc3f116fSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2236bc3f116fSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2237bc3f116fSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 2238bc3f116fSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2239bc3f116fSChris Mason 2240bc3f116fSChris Mason /* 2241bc3f116fSChris Mason * mixed block groups end up with duplicate but slightly offset 2242bc3f116fSChris Mason * extent buffers for the same range. It leads to corruptions 2243bc3f116fSChris Mason */ 2244bc3f116fSChris Mason if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) && 2245bc3f116fSChris Mason (sectorsize != leafsize)) { 2246bc3f116fSChris Mason printk(KERN_WARNING "btrfs: unequal leaf/node/sector sizes " 2247bc3f116fSChris Mason "are not allowed for mixed block groups on %s\n", 2248bc3f116fSChris Mason sb->s_id); 2249bc3f116fSChris Mason goto fail_alloc; 2250bc3f116fSChris Mason } 2251bc3f116fSChris Mason 22525d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 22535d4f98a2SYan Zheng 2254f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2255f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2256f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2257f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2258f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 225921380931SJoel Becker (unsigned long long)features); 2260f2b636e8SJosef Bacik err = -EINVAL; 226116cdcec7SMiao Xie goto fail_alloc; 2262f2b636e8SJosef Bacik } 226361d92c32SChris Mason 226461d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 226561d92c32SChris Mason "genwork", 1, NULL); 226661d92c32SChris Mason 22675443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 226861d92c32SChris Mason fs_info->thread_pool_size, 226961d92c32SChris Mason &fs_info->generic_worker); 2270c8b97818SChris Mason 2271771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 227261d92c32SChris Mason fs_info->thread_pool_size, 227361d92c32SChris Mason &fs_info->generic_worker); 2274771ed689SChris Mason 22755443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2276b720d209SChris Mason min_t(u64, fs_devices->num_devices, 227761d92c32SChris Mason fs_info->thread_pool_size), 227861d92c32SChris Mason &fs_info->generic_worker); 227961b49440SChris Mason 2280bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 2281bab39bf9SJosef Bacik 2, &fs_info->generic_worker); 2282bab39bf9SJosef Bacik 228361b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 228461b49440SChris Mason * likely that bios will be send down in a sane order to the 228561b49440SChris Mason * devices 228661b49440SChris Mason */ 228761b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 228853863232SChris Mason 2289771ed689SChris Mason fs_info->workers.idle_thresh = 16; 22904a69a410SChris Mason fs_info->workers.ordered = 1; 229161b49440SChris Mason 2292771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2293771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2294771ed689SChris Mason 229561d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 229661d92c32SChris Mason &fs_info->generic_worker); 22975443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 229861d92c32SChris Mason fs_info->thread_pool_size, 229961d92c32SChris Mason &fs_info->generic_worker); 2300d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 230161d92c32SChris Mason fs_info->thread_pool_size, 230261d92c32SChris Mason &fs_info->generic_worker); 2303cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 230461d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 230561d92c32SChris Mason &fs_info->generic_worker); 23065443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 230761d92c32SChris Mason fs_info->thread_pool_size, 230861d92c32SChris Mason &fs_info->generic_worker); 23090cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 23100cb59c99SJosef Bacik 1, &fs_info->generic_worker); 231116cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 231216cdcec7SMiao Xie fs_info->thread_pool_size, 231316cdcec7SMiao Xie &fs_info->generic_worker); 231490519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 231590519d66SArne Jansen fs_info->thread_pool_size, 231690519d66SArne Jansen &fs_info->generic_worker); 231761b49440SChris Mason 231861b49440SChris Mason /* 231961b49440SChris Mason * endios are largely parallel and should have a very 232061b49440SChris Mason * low idle thresh 232161b49440SChris Mason */ 232261b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2323b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 2324b51912c9SChris Mason 23259042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 23269042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 232790519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 23289042846bSChris Mason 23290dc3b84aSJosef Bacik /* 23300dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 23310dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 23320dc3b84aSJosef Bacik */ 23330dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 23340dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 23350dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 23360dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 23370dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 23380dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 23390dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 23400dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 23410dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 23420dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 23430dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 23440dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 23450dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 23460dc3b84aSJosef Bacik if (ret) { 2347fed425c7SIlya Dryomov err = -ENOMEM; 23480dc3b84aSJosef Bacik goto fail_sb_buffer; 23490dc3b84aSJosef Bacik } 23504543df7eSChris Mason 23514575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2352c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2353c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 23544575c9ccSChris Mason 2355db94535dSChris Mason tree_root->nodesize = nodesize; 2356db94535dSChris Mason tree_root->leafsize = leafsize; 2357db94535dSChris Mason tree_root->sectorsize = sectorsize; 235887ee04ebSChris Mason tree_root->stripesize = stripesize; 2359a061fc8dSChris Mason 2360a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2361a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2362db94535dSChris Mason 236339279cc3SChris Mason if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, 236439279cc3SChris Mason sizeof(disk_super->magic))) { 2365d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 236639279cc3SChris Mason goto fail_sb_buffer; 236739279cc3SChris Mason } 236819c00ddcSChris Mason 23698d082fb7SLiu Bo if (sectorsize != PAGE_SIZE) { 23708d082fb7SLiu Bo printk(KERN_WARNING "btrfs: Incompatible sector size(%lu) " 23718d082fb7SLiu Bo "found on %s\n", (unsigned long)sectorsize, sb->s_id); 2372941b2ddfSKeith Mannthey goto fail_sb_buffer; 2373941b2ddfSKeith Mannthey } 2374941b2ddfSKeith Mannthey 2375925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2376e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2377925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 237884eed90fSChris Mason if (ret) { 2379d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2380d397712bSChris Mason "array on %s\n", sb->s_id); 23815d4f98a2SYan Zheng goto fail_sb_buffer; 238284eed90fSChris Mason } 23830b86a832SChris Mason 23840b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 23850b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 238684234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 23870b86a832SChris Mason 23880b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 23890b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 23900b86a832SChris Mason 23910b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 23920b86a832SChris Mason btrfs_super_chunk_root(disk_super), 239384234f3aSYan Zheng blocksize, generation); 239479787eaaSJeff Mahoney BUG_ON(!chunk_root->node); /* -ENOMEM */ 239583121942SDavid Woodhouse if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 239683121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 239783121942SDavid Woodhouse sb->s_id); 2398af31f5e5SChris Mason goto fail_tree_roots; 239983121942SDavid Woodhouse } 24005d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 24015d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 24020b86a832SChris Mason 2403e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2404e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 2405e17cade2SChris Mason BTRFS_UUID_SIZE); 2406e17cade2SChris Mason 24070b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 24082b82032cSYan Zheng if (ret) { 2409d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2410d397712bSChris Mason sb->s_id); 2411af31f5e5SChris Mason goto fail_tree_roots; 24122b82032cSYan Zheng } 24130b86a832SChris Mason 2414dfe25020SChris Mason btrfs_close_extra_devices(fs_devices); 2415dfe25020SChris Mason 2416a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2417a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2418a6b0d5c8SChris Mason sb->s_id); 2419a6b0d5c8SChris Mason goto fail_tree_roots; 2420a6b0d5c8SChris Mason } 2421a6b0d5c8SChris Mason 2422af31f5e5SChris Mason retry_root_backup: 2423db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2424db94535dSChris Mason btrfs_super_root_level(disk_super)); 242584234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 24260b86a832SChris Mason 2427e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2428db94535dSChris Mason btrfs_super_root(disk_super), 242984234f3aSYan Zheng blocksize, generation); 2430af31f5e5SChris Mason if (!tree_root->node || 2431af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 243283121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 243383121942SDavid Woodhouse sb->s_id); 2434af31f5e5SChris Mason 2435af31f5e5SChris Mason goto recovery_tree_root; 243683121942SDavid Woodhouse } 2437af31f5e5SChris Mason 24385d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 24395d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 2440db94535dSChris Mason 2441db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 2442e20d96d6SChris Mason BTRFS_EXTENT_TREE_OBJECTID, extent_root); 24430b86a832SChris Mason if (ret) 2444af31f5e5SChris Mason goto recovery_tree_root; 24450b86a832SChris Mason extent_root->track_dirty = 1; 24460b86a832SChris Mason 24470b86a832SChris Mason ret = find_and_setup_root(tree_root, fs_info, 24480b86a832SChris Mason BTRFS_DEV_TREE_OBJECTID, dev_root); 24490b86a832SChris Mason if (ret) 2450af31f5e5SChris Mason goto recovery_tree_root; 24515d4f98a2SYan Zheng dev_root->track_dirty = 1; 24523768f368SChris Mason 2453d20f7043SChris Mason ret = find_and_setup_root(tree_root, fs_info, 2454d20f7043SChris Mason BTRFS_CSUM_TREE_OBJECTID, csum_root); 2455d20f7043SChris Mason if (ret) 2456af31f5e5SChris Mason goto recovery_tree_root; 2457d20f7043SChris Mason csum_root->track_dirty = 1; 2458d20f7043SChris Mason 2459bcef60f2SArne Jansen ret = find_and_setup_root(tree_root, fs_info, 2460bcef60f2SArne Jansen BTRFS_QUOTA_TREE_OBJECTID, quota_root); 2461bcef60f2SArne Jansen if (ret) { 2462bcef60f2SArne Jansen kfree(quota_root); 2463bcef60f2SArne Jansen quota_root = fs_info->quota_root = NULL; 2464bcef60f2SArne Jansen } else { 2465bcef60f2SArne Jansen quota_root->track_dirty = 1; 2466bcef60f2SArne Jansen fs_info->quota_enabled = 1; 2467bcef60f2SArne Jansen fs_info->pending_quota_state = 1; 2468bcef60f2SArne Jansen } 2469bcef60f2SArne Jansen 24708929ecfaSYan, Zheng fs_info->generation = generation; 24718929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 24728929ecfaSYan, Zheng 247368310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info); 247468310a5eSIlya Dryomov if (ret) { 247568310a5eSIlya Dryomov printk(KERN_WARNING "btrfs: failed to recover balance\n"); 247668310a5eSIlya Dryomov goto fail_block_groups; 247768310a5eSIlya Dryomov } 247868310a5eSIlya Dryomov 2479733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info); 2480733f4fbbSStefan Behrens if (ret) { 2481733f4fbbSStefan Behrens printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2482733f4fbbSStefan Behrens ret); 2483733f4fbbSStefan Behrens goto fail_block_groups; 2484733f4fbbSStefan Behrens } 2485733f4fbbSStefan Behrens 2486c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2487c59021f8Sliubo if (ret) { 2488c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2489c59021f8Sliubo goto fail_block_groups; 2490c59021f8Sliubo } 2491c59021f8Sliubo 24921b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 24931b1d1f66SJosef Bacik if (ret) { 24941b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 24951b1d1f66SJosef Bacik goto fail_block_groups; 24961b1d1f66SJosef Bacik } 24979078a3e1SChris Mason 2498a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2499a74a4b97SChris Mason "btrfs-cleaner"); 250057506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 25011b1d1f66SJosef Bacik goto fail_block_groups; 2502a74a4b97SChris Mason 2503a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2504a74a4b97SChris Mason tree_root, 2505a74a4b97SChris Mason "btrfs-transaction"); 250657506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 25073f157a2fSChris Mason goto fail_cleaner; 2508a74a4b97SChris Mason 2509c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2510c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2511c289811cSChris Mason !fs_info->fs_devices->rotating) { 2512c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2513c289811cSChris Mason "mode\n"); 2514c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2515c289811cSChris Mason } 2516c289811cSChris Mason 251721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 251821adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 251921adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 252021adbd5cSStefan Behrens btrfs_test_opt(tree_root, 252121adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 252221adbd5cSStefan Behrens 1 : 0, 252321adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 252421adbd5cSStefan Behrens if (ret) 252521adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 252621adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 252721adbd5cSStefan Behrens } 252821adbd5cSStefan Behrens #endif 2529bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info); 2530bcef60f2SArne Jansen if (ret) 2531bcef60f2SArne Jansen goto fail_trans_kthread; 253221adbd5cSStefan Behrens 2533acce952bSliubo /* do not make disk changes in broken FS */ 253468ce9682SStefan Behrens if (btrfs_super_log_root(disk_super) != 0) { 2535e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2536d18a2c44SChris Mason 25377c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2538d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2539d397712bSChris Mason "on RO media\n"); 25407c2ca468SChris Mason err = -EIO; 2541bcef60f2SArne Jansen goto fail_qgroup; 25427c2ca468SChris Mason } 2543e02119d5SChris Mason blocksize = 2544e02119d5SChris Mason btrfs_level_size(tree_root, 2545e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2546e02119d5SChris Mason 25476f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2548676e4c86SDan Carpenter if (!log_tree_root) { 2549676e4c86SDan Carpenter err = -ENOMEM; 2550bcef60f2SArne Jansen goto fail_qgroup; 2551676e4c86SDan Carpenter } 2552e02119d5SChris Mason 2553e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2554e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2555e02119d5SChris Mason 2556e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 255784234f3aSYan Zheng blocksize, 255884234f3aSYan Zheng generation + 1); 255979787eaaSJeff Mahoney /* returns with log_tree_root freed on success */ 2560e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 256179787eaaSJeff Mahoney if (ret) { 256279787eaaSJeff Mahoney btrfs_error(tree_root->fs_info, ret, 256379787eaaSJeff Mahoney "Failed to recover log tree"); 256479787eaaSJeff Mahoney free_extent_buffer(log_tree_root->node); 256579787eaaSJeff Mahoney kfree(log_tree_root); 256679787eaaSJeff Mahoney goto fail_trans_kthread; 256779787eaaSJeff Mahoney } 2568e556ce2cSYan Zheng 2569e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2570e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 257179787eaaSJeff Mahoney if (ret) 257279787eaaSJeff Mahoney goto fail_trans_kthread; 2573e556ce2cSYan Zheng } 2574e02119d5SChris Mason } 25751a40e23bSZheng Yan 257676dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 257779787eaaSJeff Mahoney if (ret) 257879787eaaSJeff Mahoney goto fail_trans_kthread; 257976dda93cSYan, Zheng 25807c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2581d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 258244c44af2SIlya Dryomov if (ret) 258344c44af2SIlya Dryomov goto fail_trans_kthread; 2584d68fc57bSYan, Zheng 25855d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2586d7ce5843SMiao Xie if (ret < 0) { 2587d7ce5843SMiao Xie printk(KERN_WARNING 2588d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2589d7ce5843SMiao Xie err = -EINVAL; 2590bcef60f2SArne Jansen goto fail_qgroup; 2591d7ce5843SMiao Xie } 25927c2ca468SChris Mason } 25931a40e23bSZheng Yan 25943de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 25953de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 25963de4586cSChris Mason location.offset = (u64)-1; 25973de4586cSChris Mason 25983de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 25993de4586cSChris Mason if (!fs_info->fs_root) 2600bcef60f2SArne Jansen goto fail_qgroup; 26013140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 26023140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 2603bcef60f2SArne Jansen goto fail_qgroup; 26043140c9a3SDan Carpenter } 2605c289811cSChris Mason 26062b6ba629SIlya Dryomov if (sb->s_flags & MS_RDONLY) 26072b6ba629SIlya Dryomov return 0; 26082b6ba629SIlya Dryomov 2609e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 26102b6ba629SIlya Dryomov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || 26112b6ba629SIlya Dryomov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { 26122b6ba629SIlya Dryomov up_read(&fs_info->cleanup_work_sem); 26132b6ba629SIlya Dryomov close_ctree(tree_root); 26142b6ba629SIlya Dryomov return ret; 26152b6ba629SIlya Dryomov } 2616e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 261759641015SIlya Dryomov 26182b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 26192b6ba629SIlya Dryomov if (ret) { 26202b6ba629SIlya Dryomov printk(KERN_WARNING "btrfs: failed to resume balance\n"); 262166b4ffd1SJosef Bacik close_ctree(tree_root); 26222b6ba629SIlya Dryomov return ret; 2623e3acc2a6SJosef Bacik } 2624e3acc2a6SJosef Bacik 2625ad2b2c80SAl Viro return 0; 262639279cc3SChris Mason 2627bcef60f2SArne Jansen fail_qgroup: 2628bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info); 26297c2ca468SChris Mason fail_trans_kthread: 26307c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 26313f157a2fSChris Mason fail_cleaner: 2632a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 26337c2ca468SChris Mason 26347c2ca468SChris Mason /* 26357c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 26367c2ca468SChris Mason * kthreads 26377c2ca468SChris Mason */ 26387c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 26397c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 26407c2ca468SChris Mason 26411b1d1f66SJosef Bacik fail_block_groups: 26421b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2643af31f5e5SChris Mason 2644af31f5e5SChris Mason fail_tree_roots: 2645af31f5e5SChris Mason free_root_pointers(fs_info, 1); 2646af31f5e5SChris Mason 264739279cc3SChris Mason fail_sb_buffer: 264861d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 2649306c8b68SChris Mason btrfs_stop_workers(&fs_info->readahead_workers); 2650247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 2651771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 26528b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 26538b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 2654d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 2655cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2656e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 26570cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 26581cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 265916cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 2660bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 266116cdcec7SMiao Xie fail_alloc: 26624543df7eSChris Mason fail_iput: 2663586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2664586e46e2SIlya Dryomov 26657c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 26664543df7eSChris Mason iput(fs_info->btree_inode); 2667ad081f14SJens Axboe fail_bdi: 26687e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 266976dda93cSYan, Zheng fail_srcu: 267076dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 26717e662854SQinghuang Feng fail: 2672586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2673ad2b2c80SAl Viro return err; 2674af31f5e5SChris Mason 2675af31f5e5SChris Mason recovery_tree_root: 2676af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2677af31f5e5SChris Mason goto fail_tree_roots; 2678af31f5e5SChris Mason 2679af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2680af31f5e5SChris Mason 2681af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2682af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2683af31f5e5SChris Mason 2684af31f5e5SChris Mason /* we can't trust the free space cache either */ 2685af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2686af31f5e5SChris Mason 2687af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2688af31f5e5SChris Mason &num_backups_tried, &backup_index); 2689af31f5e5SChris Mason if (ret == -1) 2690af31f5e5SChris Mason goto fail_block_groups; 2691af31f5e5SChris Mason goto retry_root_backup; 2692eb60ceacSChris Mason } 2693eb60ceacSChris Mason 2694f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2695f2984462SChris Mason { 2696f2984462SChris Mason if (uptodate) { 2697f2984462SChris Mason set_buffer_uptodate(bh); 2698f2984462SChris Mason } else { 2699442a4f63SStefan Behrens struct btrfs_device *device = (struct btrfs_device *) 2700442a4f63SStefan Behrens bh->b_private; 2701442a4f63SStefan Behrens 2702606686eeSJosef Bacik printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to " 2703606686eeSJosef Bacik "I/O error on %s\n", 2704606686eeSJosef Bacik rcu_str_deref(device->name)); 27051259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 27061259ab75SChris Mason * our own ways of dealing with the IO errors 27071259ab75SChris Mason */ 2708f2984462SChris Mason clear_buffer_uptodate(bh); 2709442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); 2710f2984462SChris Mason } 2711f2984462SChris Mason unlock_buffer(bh); 2712f2984462SChris Mason put_bh(bh); 2713f2984462SChris Mason } 2714f2984462SChris Mason 2715a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 2716a512bbf8SYan Zheng { 2717a512bbf8SYan Zheng struct buffer_head *bh; 2718a512bbf8SYan Zheng struct buffer_head *latest = NULL; 2719a512bbf8SYan Zheng struct btrfs_super_block *super; 2720a512bbf8SYan Zheng int i; 2721a512bbf8SYan Zheng u64 transid = 0; 2722a512bbf8SYan Zheng u64 bytenr; 2723a512bbf8SYan Zheng 2724a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 2725a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 2726a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 2727a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 2728a512bbf8SYan Zheng */ 2729a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 2730a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2731a512bbf8SYan Zheng if (bytenr + 4096 >= i_size_read(bdev->bd_inode)) 2732a512bbf8SYan Zheng break; 2733a512bbf8SYan Zheng bh = __bread(bdev, bytenr / 4096, 4096); 2734a512bbf8SYan Zheng if (!bh) 2735a512bbf8SYan Zheng continue; 2736a512bbf8SYan Zheng 2737a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 2738a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 2739a512bbf8SYan Zheng strncmp((char *)(&super->magic), BTRFS_MAGIC, 2740a512bbf8SYan Zheng sizeof(super->magic))) { 2741a512bbf8SYan Zheng brelse(bh); 2742a512bbf8SYan Zheng continue; 2743a512bbf8SYan Zheng } 2744a512bbf8SYan Zheng 2745a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 2746a512bbf8SYan Zheng brelse(latest); 2747a512bbf8SYan Zheng latest = bh; 2748a512bbf8SYan Zheng transid = btrfs_super_generation(super); 2749a512bbf8SYan Zheng } else { 2750a512bbf8SYan Zheng brelse(bh); 2751a512bbf8SYan Zheng } 2752a512bbf8SYan Zheng } 2753a512bbf8SYan Zheng return latest; 2754a512bbf8SYan Zheng } 2755a512bbf8SYan Zheng 27564eedeb75SHisashi Hifumi /* 27574eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 27584eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 27594eedeb75SHisashi Hifumi * we write are pinned. 27604eedeb75SHisashi Hifumi * 27614eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 27624eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 27634eedeb75SHisashi Hifumi * many supers on this one device should be written. 27644eedeb75SHisashi Hifumi * 27654eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 27664eedeb75SHisashi Hifumi */ 2767a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 2768a512bbf8SYan Zheng struct btrfs_super_block *sb, 2769a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 2770a512bbf8SYan Zheng { 2771a512bbf8SYan Zheng struct buffer_head *bh; 2772a512bbf8SYan Zheng int i; 2773a512bbf8SYan Zheng int ret; 2774a512bbf8SYan Zheng int errors = 0; 2775a512bbf8SYan Zheng u32 crc; 2776a512bbf8SYan Zheng u64 bytenr; 2777a512bbf8SYan Zheng 2778a512bbf8SYan Zheng if (max_mirrors == 0) 2779a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 2780a512bbf8SYan Zheng 2781a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 2782a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2783a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 2784a512bbf8SYan Zheng break; 2785a512bbf8SYan Zheng 2786a512bbf8SYan Zheng if (wait) { 2787a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 2788a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2789a512bbf8SYan Zheng BUG_ON(!bh); 2790a512bbf8SYan Zheng wait_on_buffer(bh); 27914eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 27924eedeb75SHisashi Hifumi errors++; 27934eedeb75SHisashi Hifumi 27944eedeb75SHisashi Hifumi /* drop our reference */ 27954eedeb75SHisashi Hifumi brelse(bh); 27964eedeb75SHisashi Hifumi 27974eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 2798a512bbf8SYan Zheng brelse(bh); 2799a512bbf8SYan Zheng continue; 2800a512bbf8SYan Zheng } else { 2801a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 2802a512bbf8SYan Zheng 2803a512bbf8SYan Zheng crc = ~(u32)0; 2804a512bbf8SYan Zheng crc = btrfs_csum_data(NULL, (char *)sb + 2805a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 2806a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 2807a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 2808a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 2809a512bbf8SYan Zheng 28104eedeb75SHisashi Hifumi /* 28114eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 28124eedeb75SHisashi Hifumi * caller 28134eedeb75SHisashi Hifumi */ 2814a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 2815a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2816a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 2817a512bbf8SYan Zheng 28184eedeb75SHisashi Hifumi /* one reference for submit_bh */ 2819a512bbf8SYan Zheng get_bh(bh); 28204eedeb75SHisashi Hifumi 28214eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 2822a512bbf8SYan Zheng lock_buffer(bh); 2823a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 2824442a4f63SStefan Behrens bh->b_private = device; 2825a512bbf8SYan Zheng } 2826a512bbf8SYan Zheng 2827387125fcSChris Mason /* 2828387125fcSChris Mason * we fua the first super. The others we allow 2829387125fcSChris Mason * to go down lazy. 2830387125fcSChris Mason */ 283121adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 28324eedeb75SHisashi Hifumi if (ret) 2833a512bbf8SYan Zheng errors++; 2834a512bbf8SYan Zheng } 2835a512bbf8SYan Zheng return errors < i ? 0 : -1; 2836a512bbf8SYan Zheng } 2837a512bbf8SYan Zheng 2838387125fcSChris Mason /* 2839387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 2840387125fcSChris Mason * for the barrier when it is done 2841387125fcSChris Mason */ 2842387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 2843387125fcSChris Mason { 2844387125fcSChris Mason if (err) { 2845387125fcSChris Mason if (err == -EOPNOTSUPP) 2846387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 2847387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 2848387125fcSChris Mason } 2849387125fcSChris Mason if (bio->bi_private) 2850387125fcSChris Mason complete(bio->bi_private); 2851387125fcSChris Mason bio_put(bio); 2852387125fcSChris Mason } 2853387125fcSChris Mason 2854387125fcSChris Mason /* 2855387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 2856387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 2857387125fcSChris Mason * 2858387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 2859387125fcSChris Mason * capable 2860387125fcSChris Mason */ 2861387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 2862387125fcSChris Mason { 2863387125fcSChris Mason struct bio *bio; 2864387125fcSChris Mason int ret = 0; 2865387125fcSChris Mason 2866387125fcSChris Mason if (device->nobarriers) 2867387125fcSChris Mason return 0; 2868387125fcSChris Mason 2869387125fcSChris Mason if (wait) { 2870387125fcSChris Mason bio = device->flush_bio; 2871387125fcSChris Mason if (!bio) 2872387125fcSChris Mason return 0; 2873387125fcSChris Mason 2874387125fcSChris Mason wait_for_completion(&device->flush_wait); 2875387125fcSChris Mason 2876387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 2877606686eeSJosef Bacik printk_in_rcu("btrfs: disabling barriers on dev %s\n", 2878606686eeSJosef Bacik rcu_str_deref(device->name)); 2879387125fcSChris Mason device->nobarriers = 1; 2880387125fcSChris Mason } 2881387125fcSChris Mason if (!bio_flagged(bio, BIO_UPTODATE)) { 2882387125fcSChris Mason ret = -EIO; 2883442a4f63SStefan Behrens if (!bio_flagged(bio, BIO_EOPNOTSUPP)) 2884442a4f63SStefan Behrens btrfs_dev_stat_inc_and_print(device, 2885442a4f63SStefan Behrens BTRFS_DEV_STAT_FLUSH_ERRS); 2886387125fcSChris Mason } 2887387125fcSChris Mason 2888387125fcSChris Mason /* drop the reference from the wait == 0 run */ 2889387125fcSChris Mason bio_put(bio); 2890387125fcSChris Mason device->flush_bio = NULL; 2891387125fcSChris Mason 2892387125fcSChris Mason return ret; 2893387125fcSChris Mason } 2894387125fcSChris Mason 2895387125fcSChris Mason /* 2896387125fcSChris Mason * one reference for us, and we leave it for the 2897387125fcSChris Mason * caller 2898387125fcSChris Mason */ 28999c017abcSJesper Juhl device->flush_bio = NULL; 2900387125fcSChris Mason bio = bio_alloc(GFP_NOFS, 0); 2901387125fcSChris Mason if (!bio) 2902387125fcSChris Mason return -ENOMEM; 2903387125fcSChris Mason 2904387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 2905387125fcSChris Mason bio->bi_bdev = device->bdev; 2906387125fcSChris Mason init_completion(&device->flush_wait); 2907387125fcSChris Mason bio->bi_private = &device->flush_wait; 2908387125fcSChris Mason device->flush_bio = bio; 2909387125fcSChris Mason 2910387125fcSChris Mason bio_get(bio); 291121adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 2912387125fcSChris Mason 2913387125fcSChris Mason return 0; 2914387125fcSChris Mason } 2915387125fcSChris Mason 2916387125fcSChris Mason /* 2917387125fcSChris Mason * send an empty flush down to each device in parallel, 2918387125fcSChris Mason * then wait for them 2919387125fcSChris Mason */ 2920387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 2921387125fcSChris Mason { 2922387125fcSChris Mason struct list_head *head; 2923387125fcSChris Mason struct btrfs_device *dev; 2924387125fcSChris Mason int errors = 0; 2925387125fcSChris Mason int ret; 2926387125fcSChris Mason 2927387125fcSChris Mason /* send down all the barriers */ 2928387125fcSChris Mason head = &info->fs_devices->devices; 2929387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 2930387125fcSChris Mason if (!dev->bdev) { 2931387125fcSChris Mason errors++; 2932387125fcSChris Mason continue; 2933387125fcSChris Mason } 2934387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 2935387125fcSChris Mason continue; 2936387125fcSChris Mason 2937387125fcSChris Mason ret = write_dev_flush(dev, 0); 2938387125fcSChris Mason if (ret) 2939387125fcSChris Mason errors++; 2940387125fcSChris Mason } 2941387125fcSChris Mason 2942387125fcSChris Mason /* wait for all the barriers */ 2943387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 2944387125fcSChris Mason if (!dev->bdev) { 2945387125fcSChris Mason errors++; 2946387125fcSChris Mason continue; 2947387125fcSChris Mason } 2948387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 2949387125fcSChris Mason continue; 2950387125fcSChris Mason 2951387125fcSChris Mason ret = write_dev_flush(dev, 1); 2952387125fcSChris Mason if (ret) 2953387125fcSChris Mason errors++; 2954387125fcSChris Mason } 2955387125fcSChris Mason if (errors) 2956387125fcSChris Mason return -EIO; 2957387125fcSChris Mason return 0; 2958387125fcSChris Mason } 2959387125fcSChris Mason 2960a512bbf8SYan Zheng int write_all_supers(struct btrfs_root *root, int max_mirrors) 2961f2984462SChris Mason { 2962e5e9a520SChris Mason struct list_head *head; 2963f2984462SChris Mason struct btrfs_device *dev; 2964a061fc8dSChris Mason struct btrfs_super_block *sb; 2965f2984462SChris Mason struct btrfs_dev_item *dev_item; 2966f2984462SChris Mason int ret; 2967f2984462SChris Mason int do_barriers; 2968a236aed1SChris Mason int max_errors; 2969a236aed1SChris Mason int total_errors = 0; 2970a061fc8dSChris Mason u64 flags; 2971f2984462SChris Mason 29726c41761fSDavid Sterba max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 2973f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 2974af31f5e5SChris Mason backup_super_roots(root->fs_info); 2975f2984462SChris Mason 29766c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 2977a061fc8dSChris Mason dev_item = &sb->dev_item; 2978e5e9a520SChris Mason 2979174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2980e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 2981387125fcSChris Mason 2982387125fcSChris Mason if (do_barriers) 2983387125fcSChris Mason barrier_all_devices(root->fs_info); 2984387125fcSChris Mason 29851f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 2986dfe25020SChris Mason if (!dev->bdev) { 2987dfe25020SChris Mason total_errors++; 2988dfe25020SChris Mason continue; 2989dfe25020SChris Mason } 29902b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 2991dfe25020SChris Mason continue; 2992dfe25020SChris Mason 29932b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 2994a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 2995a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 2996a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 2997a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 2998a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 2999a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 3000a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 3001a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 30022b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 3003a512bbf8SYan Zheng 3004a061fc8dSChris Mason flags = btrfs_super_flags(sb); 3005a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 3006f2984462SChris Mason 3007a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 3008a236aed1SChris Mason if (ret) 3009a236aed1SChris Mason total_errors++; 3010f2984462SChris Mason } 3011a236aed1SChris Mason if (total_errors > max_errors) { 3012d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 3013d397712bSChris Mason total_errors); 301479787eaaSJeff Mahoney 301579787eaaSJeff Mahoney /* This shouldn't happen. FUA is masked off if unsupported */ 3016a236aed1SChris Mason BUG(); 3017a236aed1SChris Mason } 3018f2984462SChris Mason 3019a512bbf8SYan Zheng total_errors = 0; 30201f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 3021dfe25020SChris Mason if (!dev->bdev) 3022dfe25020SChris Mason continue; 30232b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 3024dfe25020SChris Mason continue; 3025dfe25020SChris Mason 3026a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 3027a512bbf8SYan Zheng if (ret) 30281259ab75SChris Mason total_errors++; 3029f2984462SChris Mason } 3030174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3031a236aed1SChris Mason if (total_errors > max_errors) { 303279787eaaSJeff Mahoney btrfs_error(root->fs_info, -EIO, 303379787eaaSJeff Mahoney "%d errors while writing supers", total_errors); 303479787eaaSJeff Mahoney return -EIO; 3035a236aed1SChris Mason } 3036f2984462SChris Mason return 0; 3037f2984462SChris Mason } 3038f2984462SChris Mason 3039a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 3040a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 3041cfaa7295SChris Mason { 3042e66f709bSChris Mason int ret; 30432c90e5d6SChris Mason 3044a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 30455f39d397SChris Mason return ret; 3046cfaa7295SChris Mason } 3047cfaa7295SChris Mason 3048143bede5SJeff Mahoney void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 30492619ba1fSChris Mason { 30504df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 30512619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 30522619ba1fSChris Mason (unsigned long)root->root_key.objectid); 30534df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 305476dda93cSYan, Zheng 305576dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 305676dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 305776dda93cSYan, Zheng 3058581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 3059581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 30604df27c4dSYan, Zheng free_fs_root(root); 30614df27c4dSYan, Zheng } 30624df27c4dSYan, Zheng 30634df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 30644df27c4dSYan, Zheng { 306582d5902dSLi Zefan iput(root->cache_inode); 30664df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 30670ee5dc67SAl Viro if (root->anon_dev) 30680ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 30695f39d397SChris Mason free_extent_buffer(root->node); 30705f39d397SChris Mason free_extent_buffer(root->commit_root); 3071581bb050SLi Zefan kfree(root->free_ino_ctl); 3072581bb050SLi Zefan kfree(root->free_ino_pinned); 307358176a96SJosef Bacik kfree(root->name); 30742619ba1fSChris Mason kfree(root); 30752619ba1fSChris Mason } 30762619ba1fSChris Mason 3077143bede5SJeff Mahoney static void del_fs_roots(struct btrfs_fs_info *fs_info) 30780f7d52f4SChris Mason { 30790f7d52f4SChris Mason int ret; 30800f7d52f4SChris Mason struct btrfs_root *gang[8]; 30810f7d52f4SChris Mason int i; 30820f7d52f4SChris Mason 308376dda93cSYan, Zheng while (!list_empty(&fs_info->dead_roots)) { 308476dda93cSYan, Zheng gang[0] = list_entry(fs_info->dead_roots.next, 308576dda93cSYan, Zheng struct btrfs_root, root_list); 308676dda93cSYan, Zheng list_del(&gang[0]->root_list); 308776dda93cSYan, Zheng 308876dda93cSYan, Zheng if (gang[0]->in_radix) { 308976dda93cSYan, Zheng btrfs_free_fs_root(fs_info, gang[0]); 309076dda93cSYan, Zheng } else { 309176dda93cSYan, Zheng free_extent_buffer(gang[0]->node); 309276dda93cSYan, Zheng free_extent_buffer(gang[0]->commit_root); 309376dda93cSYan, Zheng kfree(gang[0]); 309476dda93cSYan, Zheng } 309576dda93cSYan, Zheng } 309676dda93cSYan, Zheng 30970f7d52f4SChris Mason while (1) { 30980f7d52f4SChris Mason ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 30990f7d52f4SChris Mason (void **)gang, 0, 31000f7d52f4SChris Mason ARRAY_SIZE(gang)); 31010f7d52f4SChris Mason if (!ret) 31020f7d52f4SChris Mason break; 31032619ba1fSChris Mason for (i = 0; i < ret; i++) 31045eda7b5eSChris Mason btrfs_free_fs_root(fs_info, gang[i]); 31050f7d52f4SChris Mason } 31060f7d52f4SChris Mason } 3107b4100d64SChris Mason 3108c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 3109c146afadSYan Zheng { 3110c146afadSYan Zheng u64 root_objectid = 0; 3111c146afadSYan Zheng struct btrfs_root *gang[8]; 3112c146afadSYan Zheng int i; 3113c146afadSYan Zheng int ret; 3114c146afadSYan Zheng 3115c146afadSYan Zheng while (1) { 3116c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 3117c146afadSYan Zheng (void **)gang, root_objectid, 3118c146afadSYan Zheng ARRAY_SIZE(gang)); 3119c146afadSYan Zheng if (!ret) 3120c146afadSYan Zheng break; 31215d4f98a2SYan Zheng 31225d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 3123c146afadSYan Zheng for (i = 0; i < ret; i++) { 312466b4ffd1SJosef Bacik int err; 312566b4ffd1SJosef Bacik 3126c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 312766b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 312866b4ffd1SJosef Bacik if (err) 312966b4ffd1SJosef Bacik return err; 3130c146afadSYan Zheng } 3131c146afadSYan Zheng root_objectid++; 3132c146afadSYan Zheng } 3133c146afadSYan Zheng return 0; 3134c146afadSYan Zheng } 3135c146afadSYan Zheng 3136c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 3137c146afadSYan Zheng { 3138c146afadSYan Zheng struct btrfs_trans_handle *trans; 3139c146afadSYan Zheng int ret; 3140c146afadSYan Zheng 3141c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 314224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 3143c146afadSYan Zheng btrfs_clean_old_snapshots(root); 3144c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 3145c71bf099SYan, Zheng 3146c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 3147c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 3148c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3149c71bf099SYan, Zheng 31507a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 31513612b495STsutomu Itoh if (IS_ERR(trans)) 31523612b495STsutomu Itoh return PTR_ERR(trans); 3153c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 315479787eaaSJeff Mahoney if (ret) 315579787eaaSJeff Mahoney return ret; 3156c146afadSYan Zheng /* run commit again to drop the original snapshot */ 31577a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 31583612b495STsutomu Itoh if (IS_ERR(trans)) 31593612b495STsutomu Itoh return PTR_ERR(trans); 316079787eaaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 316179787eaaSJeff Mahoney if (ret) 316279787eaaSJeff Mahoney return ret; 3163c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 316479787eaaSJeff Mahoney if (ret) { 316579787eaaSJeff Mahoney btrfs_error(root->fs_info, ret, 316679787eaaSJeff Mahoney "Failed to sync btree inode to disk."); 316779787eaaSJeff Mahoney return ret; 316879787eaaSJeff Mahoney } 3169c146afadSYan Zheng 3170a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3171c146afadSYan Zheng return ret; 3172c146afadSYan Zheng } 3173c146afadSYan Zheng 3174e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3175eb60ceacSChris Mason { 31760f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3177c146afadSYan Zheng int ret; 3178e089f05cSChris Mason 3179facda1e7SChris Mason fs_info->closing = 1; 3180a2135011SChris Mason smp_mb(); 3181a2135011SChris Mason 3182837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3183837d5b6eSIlya Dryomov btrfs_pause_balance(root->fs_info); 3184837d5b6eSIlya Dryomov 3185a2de733cSArne Jansen btrfs_scrub_cancel(root); 31864cb5300bSChris Mason 31874cb5300bSChris Mason /* wait for any defraggers to finish */ 31884cb5300bSChris Mason wait_event(fs_info->transaction_wait, 31894cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 31904cb5300bSChris Mason 31914cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 3192e3029d9fSAl Viro btrfs_run_defrag_inodes(fs_info); 31934cb5300bSChris Mason 3194c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3195c146afadSYan Zheng ret = btrfs_commit_super(root); 3196d397712bSChris Mason if (ret) 3197d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3198c146afadSYan Zheng } 3199ed2ff2cbSChris Mason 320068ce9682SStefan Behrens if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 320168ce9682SStefan Behrens btrfs_error_commit_super(root); 3202acce952bSliubo 3203300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3204300e4f8aSJosef Bacik 3205e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3206e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 32078929ecfaSYan, Zheng 3208f25784b3SYan Zheng fs_info->closing = 2; 3209f25784b3SYan Zheng smp_mb(); 3210f25784b3SYan Zheng 3211bcef60f2SArne Jansen btrfs_free_qgroup_config(root->fs_info); 3212bcef60f2SArne Jansen 3213b0c68f8bSChris Mason if (fs_info->delalloc_bytes) { 3214d397712bSChris Mason printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n", 321521380931SJoel Becker (unsigned long long)fs_info->delalloc_bytes); 3216b0c68f8bSChris Mason } 321731153d81SYan Zheng if (fs_info->total_ref_cache_size) { 3218d397712bSChris Mason printk(KERN_INFO "btrfs: at umount reference cache size %llu\n", 3219d397712bSChris Mason (unsigned long long)fs_info->total_ref_cache_size); 322031153d81SYan Zheng } 322131153d81SYan Zheng 32225f39d397SChris Mason free_extent_buffer(fs_info->extent_root->node); 32235d4f98a2SYan Zheng free_extent_buffer(fs_info->extent_root->commit_root); 32245f39d397SChris Mason free_extent_buffer(fs_info->tree_root->node); 32255d4f98a2SYan Zheng free_extent_buffer(fs_info->tree_root->commit_root); 3226e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->node); 3227e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->commit_root); 3228e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->node); 3229e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->commit_root); 3230e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->node); 3231e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->commit_root); 3232bcef60f2SArne Jansen if (fs_info->quota_root) { 3233bcef60f2SArne Jansen free_extent_buffer(fs_info->quota_root->node); 3234bcef60f2SArne Jansen free_extent_buffer(fs_info->quota_root->commit_root); 3235bcef60f2SArne Jansen } 3236d20f7043SChris Mason 3237e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3238c146afadSYan Zheng 32390f7d52f4SChris Mason del_fs_roots(fs_info); 3240d10c5f31SChris Mason 3241c146afadSYan Zheng iput(fs_info->btree_inode); 32429ad6b7bcSChris Mason 324361d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 3244247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 3245771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 32468b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 32478b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 3248d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 3249cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 3250e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 32510cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 32521cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 325316cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 3254bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 325590519d66SArne Jansen btrfs_stop_workers(&fs_info->readahead_workers); 3256d6bfde87SChris Mason 325721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 325821adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 325921adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 326021adbd5cSStefan Behrens #endif 326121adbd5cSStefan Behrens 3262dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 32630b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3264b248a415SChris Mason 326504160088SChris Mason bdi_destroy(&fs_info->bdi); 326676dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 32670b86a832SChris Mason 3268eb60ceacSChris Mason return 0; 3269eb60ceacSChris Mason } 3270eb60ceacSChris Mason 3271b9fab919SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 3272b9fab919SChris Mason int atomic) 3273ccd467d6SChris Mason { 32741259ab75SChris Mason int ret; 3275727011e0SChris Mason struct inode *btree_inode = buf->pages[0]->mapping->host; 32761259ab75SChris Mason 32770b32f4bbSJosef Bacik ret = extent_buffer_uptodate(buf); 32781259ab75SChris Mason if (!ret) 32791259ab75SChris Mason return ret; 32801259ab75SChris Mason 32811259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 3282b9fab919SChris Mason parent_transid, atomic); 3283b9fab919SChris Mason if (ret == -EAGAIN) 3284b9fab919SChris Mason return ret; 32851259ab75SChris Mason return !ret; 32865f39d397SChris Mason } 32876702ed49SChris Mason 32885f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 32895f39d397SChris Mason { 32900b32f4bbSJosef Bacik return set_extent_buffer_uptodate(buf); 32915f39d397SChris Mason } 32925f39d397SChris Mason 32935f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 32945f39d397SChris Mason { 3295727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 32965f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 3297b9473439SChris Mason int was_dirty; 3298b4ce94deSChris Mason 3299b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 3300ccd467d6SChris Mason if (transid != root->fs_info->generation) { 3301d397712bSChris Mason printk(KERN_CRIT "btrfs transid mismatch buffer %llu, " 3302d397712bSChris Mason "found %llu running %llu\n", 3303db94535dSChris Mason (unsigned long long)buf->start, 3304d397712bSChris Mason (unsigned long long)transid, 3305d397712bSChris Mason (unsigned long long)root->fs_info->generation); 3306ccd467d6SChris Mason WARN_ON(1); 3307ccd467d6SChris Mason } 33080b32f4bbSJosef Bacik was_dirty = set_extent_buffer_dirty(buf); 3309b9473439SChris Mason if (!was_dirty) { 3310b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 3311b9473439SChris Mason root->fs_info->dirty_metadata_bytes += buf->len; 3312b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 3313b9473439SChris Mason } 3314eb60ceacSChris Mason } 3315eb60ceacSChris Mason 3316d3c2fdcfSChris Mason void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) 331735b7e476SChris Mason { 3318188de649SChris Mason /* 3319188de649SChris Mason * looks as though older kernels can get into trouble with 3320188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3321188de649SChris Mason */ 3322d6bfde87SChris Mason u64 num_dirty; 3323771ed689SChris Mason unsigned long thresh = 32 * 1024 * 1024; 3324d6bfde87SChris Mason 33256933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3326d6bfde87SChris Mason return; 3327d6bfde87SChris Mason 332816cdcec7SMiao Xie btrfs_balance_delayed_items(root); 332916cdcec7SMiao Xie 333016cdcec7SMiao Xie num_dirty = root->fs_info->dirty_metadata_bytes; 333116cdcec7SMiao Xie 333216cdcec7SMiao Xie if (num_dirty > thresh) { 333316cdcec7SMiao Xie balance_dirty_pages_ratelimited_nr( 333416cdcec7SMiao Xie root->fs_info->btree_inode->i_mapping, 1); 333516cdcec7SMiao Xie } 333616cdcec7SMiao Xie return; 333716cdcec7SMiao Xie } 333816cdcec7SMiao Xie 333916cdcec7SMiao Xie void __btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) 334016cdcec7SMiao Xie { 334116cdcec7SMiao Xie /* 334216cdcec7SMiao Xie * looks as though older kernels can get into trouble with 334316cdcec7SMiao Xie * this code, they end up stuck in balance_dirty_pages forever 334416cdcec7SMiao Xie */ 334516cdcec7SMiao Xie u64 num_dirty; 334616cdcec7SMiao Xie unsigned long thresh = 32 * 1024 * 1024; 334716cdcec7SMiao Xie 334816cdcec7SMiao Xie if (current->flags & PF_MEMALLOC) 334916cdcec7SMiao Xie return; 335016cdcec7SMiao Xie 3351585ad2c3SChris Mason num_dirty = root->fs_info->dirty_metadata_bytes; 3352585ad2c3SChris Mason 3353d6bfde87SChris Mason if (num_dirty > thresh) { 3354d3c2fdcfSChris Mason balance_dirty_pages_ratelimited_nr( 3355304fced6SChris Mason root->fs_info->btree_inode->i_mapping, 1); 335635b7e476SChris Mason } 3357188de649SChris Mason return; 3358d6bfde87SChris Mason } 33596b80053dSChris Mason 3360ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 33616b80053dSChris Mason { 3362727011e0SChris Mason struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 33630b32f4bbSJosef Bacik return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 33646b80053dSChris Mason } 33650da5468fSChris Mason 336620897f5cSArne Jansen int btree_lock_page_hook(struct page *page, void *data, 336701d658f2SChris Mason void (*flush_fn)(void *)) 33684bef0848SChris Mason { 33694bef0848SChris Mason struct inode *inode = page->mapping->host; 3370b9473439SChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 33714bef0848SChris Mason struct extent_buffer *eb; 33724bef0848SChris Mason 33734f2de97aSJosef Bacik /* 33744f2de97aSJosef Bacik * We culled this eb but the page is still hanging out on the mapping, 33754f2de97aSJosef Bacik * carry on. 33764f2de97aSJosef Bacik */ 33774f2de97aSJosef Bacik if (!PagePrivate(page)) 33784bef0848SChris Mason goto out; 33794bef0848SChris Mason 33804f2de97aSJosef Bacik eb = (struct extent_buffer *)page->private; 33814f2de97aSJosef Bacik if (!eb) { 33824f2de97aSJosef Bacik WARN_ON(1); 33834f2de97aSJosef Bacik goto out; 33844f2de97aSJosef Bacik } 33854f2de97aSJosef Bacik if (page != eb->pages[0]) 33864bef0848SChris Mason goto out; 33874bef0848SChris Mason 338801d658f2SChris Mason if (!btrfs_try_tree_write_lock(eb)) { 338901d658f2SChris Mason flush_fn(data); 33904bef0848SChris Mason btrfs_tree_lock(eb); 339101d658f2SChris Mason } 33924bef0848SChris Mason btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); 3393b9473439SChris Mason 3394b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { 3395b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 3396b9473439SChris Mason if (root->fs_info->dirty_metadata_bytes >= eb->len) 3397b9473439SChris Mason root->fs_info->dirty_metadata_bytes -= eb->len; 3398b9473439SChris Mason else 3399b9473439SChris Mason WARN_ON(1); 3400b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 3401b9473439SChris Mason } 3402b9473439SChris Mason 34034bef0848SChris Mason btrfs_tree_unlock(eb); 34044bef0848SChris Mason out: 340501d658f2SChris Mason if (!trylock_page(page)) { 340601d658f2SChris Mason flush_fn(data); 34074bef0848SChris Mason lock_page(page); 340801d658f2SChris Mason } 34094bef0848SChris Mason return 0; 34104bef0848SChris Mason } 34114bef0848SChris Mason 3412fcd1f065SDavid Sterba static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3413acce952bSliubo int read_only) 3414acce952bSliubo { 3415fcd1f065SDavid Sterba if (btrfs_super_csum_type(fs_info->super_copy) >= ARRAY_SIZE(btrfs_csum_sizes)) { 3416fcd1f065SDavid Sterba printk(KERN_ERR "btrfs: unsupported checksum algorithm\n"); 3417fcd1f065SDavid Sterba return -EINVAL; 3418fcd1f065SDavid Sterba } 3419acce952bSliubo 3420fcd1f065SDavid Sterba if (read_only) 3421fcd1f065SDavid Sterba return 0; 3422fcd1f065SDavid Sterba 3423fcd1f065SDavid Sterba return 0; 3424fcd1f065SDavid Sterba } 3425fcd1f065SDavid Sterba 342668ce9682SStefan Behrens void btrfs_error_commit_super(struct btrfs_root *root) 3427acce952bSliubo { 3428acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3429acce952bSliubo btrfs_run_delayed_iputs(root); 3430acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3431acce952bSliubo 3432acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3433acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3434acce952bSliubo 3435acce952bSliubo /* cleanup FS via transaction */ 3436acce952bSliubo btrfs_cleanup_transaction(root); 3437acce952bSliubo } 3438acce952bSliubo 3439143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root) 3440acce952bSliubo { 3441acce952bSliubo struct btrfs_inode *btrfs_inode; 3442acce952bSliubo struct list_head splice; 3443acce952bSliubo 3444acce952bSliubo INIT_LIST_HEAD(&splice); 3445acce952bSliubo 3446acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3447acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3448acce952bSliubo 3449acce952bSliubo list_splice_init(&root->fs_info->ordered_operations, &splice); 3450acce952bSliubo while (!list_empty(&splice)) { 3451acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3452acce952bSliubo ordered_operations); 3453acce952bSliubo 3454acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3455acce952bSliubo 3456acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3457acce952bSliubo } 3458acce952bSliubo 3459acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3460acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3461acce952bSliubo } 3462acce952bSliubo 3463143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3464acce952bSliubo { 3465acce952bSliubo struct list_head splice; 3466acce952bSliubo struct btrfs_ordered_extent *ordered; 3467acce952bSliubo struct inode *inode; 3468acce952bSliubo 3469acce952bSliubo INIT_LIST_HEAD(&splice); 3470acce952bSliubo 3471acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3472acce952bSliubo 3473acce952bSliubo list_splice_init(&root->fs_info->ordered_extents, &splice); 3474acce952bSliubo while (!list_empty(&splice)) { 3475acce952bSliubo ordered = list_entry(splice.next, struct btrfs_ordered_extent, 3476acce952bSliubo root_extent_list); 3477acce952bSliubo 3478acce952bSliubo list_del_init(&ordered->root_extent_list); 3479acce952bSliubo atomic_inc(&ordered->refs); 3480acce952bSliubo 3481acce952bSliubo /* the inode may be getting freed (in sys_unlink path). */ 3482acce952bSliubo inode = igrab(ordered->inode); 3483acce952bSliubo 3484acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3485acce952bSliubo if (inode) 3486acce952bSliubo iput(inode); 3487acce952bSliubo 3488acce952bSliubo atomic_set(&ordered->refs, 1); 3489acce952bSliubo btrfs_put_ordered_extent(ordered); 3490acce952bSliubo 3491acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3492acce952bSliubo } 3493acce952bSliubo 3494acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3495acce952bSliubo } 3496acce952bSliubo 349779787eaaSJeff Mahoney int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3498acce952bSliubo struct btrfs_root *root) 3499acce952bSliubo { 3500acce952bSliubo struct rb_node *node; 3501acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3502acce952bSliubo struct btrfs_delayed_ref_node *ref; 3503acce952bSliubo int ret = 0; 3504acce952bSliubo 3505acce952bSliubo delayed_refs = &trans->delayed_refs; 3506acce952bSliubo 3507acce952bSliubo spin_lock(&delayed_refs->lock); 3508acce952bSliubo if (delayed_refs->num_entries == 0) { 3509cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3510acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3511acce952bSliubo return ret; 3512acce952bSliubo } 3513acce952bSliubo 3514b939d1abSJosef Bacik while ((node = rb_first(&delayed_refs->root)) != NULL) { 3515acce952bSliubo ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3516acce952bSliubo 3517acce952bSliubo atomic_set(&ref->refs, 1); 3518acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3519acce952bSliubo struct btrfs_delayed_ref_head *head; 3520acce952bSliubo 3521acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3522b939d1abSJosef Bacik if (!mutex_trylock(&head->mutex)) { 3523b939d1abSJosef Bacik atomic_inc(&ref->refs); 352479787eaaSJeff Mahoney spin_unlock(&delayed_refs->lock); 3525b939d1abSJosef Bacik 3526b939d1abSJosef Bacik /* Need to wait for the delayed ref to run */ 3527acce952bSliubo mutex_lock(&head->mutex); 3528b939d1abSJosef Bacik mutex_unlock(&head->mutex); 3529b939d1abSJosef Bacik btrfs_put_delayed_ref(ref); 3530b939d1abSJosef Bacik 3531e18fca73SJosef Bacik spin_lock(&delayed_refs->lock); 3532b939d1abSJosef Bacik continue; 3533b939d1abSJosef Bacik } 3534b939d1abSJosef Bacik 3535acce952bSliubo kfree(head->extent_op); 3536acce952bSliubo delayed_refs->num_heads--; 3537acce952bSliubo if (list_empty(&head->cluster)) 3538acce952bSliubo delayed_refs->num_heads_ready--; 3539acce952bSliubo list_del_init(&head->cluster); 3540acce952bSliubo } 3541b939d1abSJosef Bacik ref->in_tree = 0; 3542b939d1abSJosef Bacik rb_erase(&ref->rb_node, &delayed_refs->root); 3543b939d1abSJosef Bacik delayed_refs->num_entries--; 3544b939d1abSJosef Bacik 3545acce952bSliubo spin_unlock(&delayed_refs->lock); 3546acce952bSliubo btrfs_put_delayed_ref(ref); 3547acce952bSliubo 3548acce952bSliubo cond_resched(); 3549acce952bSliubo spin_lock(&delayed_refs->lock); 3550acce952bSliubo } 3551acce952bSliubo 3552acce952bSliubo spin_unlock(&delayed_refs->lock); 3553acce952bSliubo 3554acce952bSliubo return ret; 3555acce952bSliubo } 3556acce952bSliubo 3557143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t) 3558acce952bSliubo { 3559acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3560acce952bSliubo struct list_head splice; 3561acce952bSliubo 3562acce952bSliubo INIT_LIST_HEAD(&splice); 3563acce952bSliubo 3564acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3565acce952bSliubo 3566acce952bSliubo while (!list_empty(&splice)) { 3567acce952bSliubo snapshot = list_entry(splice.next, 3568acce952bSliubo struct btrfs_pending_snapshot, 3569acce952bSliubo list); 3570acce952bSliubo 3571acce952bSliubo list_del_init(&snapshot->list); 3572acce952bSliubo 3573acce952bSliubo kfree(snapshot); 3574acce952bSliubo } 3575acce952bSliubo } 3576acce952bSliubo 3577143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3578acce952bSliubo { 3579acce952bSliubo struct btrfs_inode *btrfs_inode; 3580acce952bSliubo struct list_head splice; 3581acce952bSliubo 3582acce952bSliubo INIT_LIST_HEAD(&splice); 3583acce952bSliubo 3584acce952bSliubo spin_lock(&root->fs_info->delalloc_lock); 35855be76758SDavid Sterba list_splice_init(&root->fs_info->delalloc_inodes, &splice); 3586acce952bSliubo 3587acce952bSliubo while (!list_empty(&splice)) { 3588acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3589acce952bSliubo delalloc_inodes); 3590acce952bSliubo 3591acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3592acce952bSliubo 3593acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3594acce952bSliubo } 3595acce952bSliubo 3596acce952bSliubo spin_unlock(&root->fs_info->delalloc_lock); 3597acce952bSliubo } 3598acce952bSliubo 3599acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3600acce952bSliubo struct extent_io_tree *dirty_pages, 3601acce952bSliubo int mark) 3602acce952bSliubo { 3603acce952bSliubo int ret; 3604acce952bSliubo struct page *page; 3605acce952bSliubo struct inode *btree_inode = root->fs_info->btree_inode; 3606acce952bSliubo struct extent_buffer *eb; 3607acce952bSliubo u64 start = 0; 3608acce952bSliubo u64 end; 3609acce952bSliubo u64 offset; 3610acce952bSliubo unsigned long index; 3611acce952bSliubo 3612acce952bSliubo while (1) { 3613acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3614acce952bSliubo mark); 3615acce952bSliubo if (ret) 3616acce952bSliubo break; 3617acce952bSliubo 3618acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3619acce952bSliubo while (start <= end) { 3620acce952bSliubo index = start >> PAGE_CACHE_SHIFT; 3621acce952bSliubo start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3622acce952bSliubo page = find_get_page(btree_inode->i_mapping, index); 3623acce952bSliubo if (!page) 3624acce952bSliubo continue; 3625acce952bSliubo offset = page_offset(page); 3626acce952bSliubo 3627acce952bSliubo spin_lock(&dirty_pages->buffer_lock); 3628acce952bSliubo eb = radix_tree_lookup( 3629acce952bSliubo &(&BTRFS_I(page->mapping->host)->io_tree)->buffer, 3630acce952bSliubo offset >> PAGE_CACHE_SHIFT); 3631acce952bSliubo spin_unlock(&dirty_pages->buffer_lock); 3632ee670f0aSJosef Bacik if (eb) 3633acce952bSliubo ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3634acce952bSliubo &eb->bflags); 3635acce952bSliubo if (PageWriteback(page)) 3636acce952bSliubo end_page_writeback(page); 3637acce952bSliubo 3638acce952bSliubo lock_page(page); 3639acce952bSliubo if (PageDirty(page)) { 3640acce952bSliubo clear_page_dirty_for_io(page); 3641acce952bSliubo spin_lock_irq(&page->mapping->tree_lock); 3642acce952bSliubo radix_tree_tag_clear(&page->mapping->page_tree, 3643acce952bSliubo page_index(page), 3644acce952bSliubo PAGECACHE_TAG_DIRTY); 3645acce952bSliubo spin_unlock_irq(&page->mapping->tree_lock); 3646acce952bSliubo } 3647acce952bSliubo 3648acce952bSliubo unlock_page(page); 3649ee670f0aSJosef Bacik page_cache_release(page); 3650acce952bSliubo } 3651acce952bSliubo } 3652acce952bSliubo 3653acce952bSliubo return ret; 3654acce952bSliubo } 3655acce952bSliubo 3656acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3657acce952bSliubo struct extent_io_tree *pinned_extents) 3658acce952bSliubo { 3659acce952bSliubo struct extent_io_tree *unpin; 3660acce952bSliubo u64 start; 3661acce952bSliubo u64 end; 3662acce952bSliubo int ret; 3663ed0eaa14SLiu Bo bool loop = true; 3664acce952bSliubo 3665acce952bSliubo unpin = pinned_extents; 3666ed0eaa14SLiu Bo again: 3667acce952bSliubo while (1) { 3668acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3669acce952bSliubo EXTENT_DIRTY); 3670acce952bSliubo if (ret) 3671acce952bSliubo break; 3672acce952bSliubo 3673acce952bSliubo /* opt_discard */ 36745378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 36755378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 36765378e607SLi Dongyang end + 1 - start, 36775378e607SLi Dongyang NULL); 3678acce952bSliubo 3679acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3680acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3681acce952bSliubo cond_resched(); 3682acce952bSliubo } 3683acce952bSliubo 3684ed0eaa14SLiu Bo if (loop) { 3685ed0eaa14SLiu Bo if (unpin == &root->fs_info->freed_extents[0]) 3686ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[1]; 3687ed0eaa14SLiu Bo else 3688ed0eaa14SLiu Bo unpin = &root->fs_info->freed_extents[0]; 3689ed0eaa14SLiu Bo loop = false; 3690ed0eaa14SLiu Bo goto again; 3691ed0eaa14SLiu Bo } 3692ed0eaa14SLiu Bo 3693acce952bSliubo return 0; 3694acce952bSliubo } 3695acce952bSliubo 369649b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 369749b25e05SJeff Mahoney struct btrfs_root *root) 369849b25e05SJeff Mahoney { 369949b25e05SJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, root); 370049b25e05SJeff Mahoney btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 370149b25e05SJeff Mahoney cur_trans->dirty_pages.dirty_bytes); 370249b25e05SJeff Mahoney 370349b25e05SJeff Mahoney /* FIXME: cleanup wait for commit */ 370449b25e05SJeff Mahoney cur_trans->in_commit = 1; 370549b25e05SJeff Mahoney cur_trans->blocked = 1; 370649b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_blocked_wait); 370749b25e05SJeff Mahoney 370849b25e05SJeff Mahoney cur_trans->blocked = 0; 370949b25e05SJeff Mahoney wake_up(&root->fs_info->transaction_wait); 371049b25e05SJeff Mahoney 371149b25e05SJeff Mahoney cur_trans->commit_done = 1; 371249b25e05SJeff Mahoney wake_up(&cur_trans->commit_wait); 371349b25e05SJeff Mahoney 371467cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 371567cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 371667cde344SMiao Xie 371749b25e05SJeff Mahoney btrfs_destroy_pending_snapshots(cur_trans); 371849b25e05SJeff Mahoney 371949b25e05SJeff Mahoney btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 372049b25e05SJeff Mahoney EXTENT_DIRTY); 37216e841e32SLiu Bo btrfs_destroy_pinned_extent(root, 37226e841e32SLiu Bo root->fs_info->pinned_extents); 372349b25e05SJeff Mahoney 372449b25e05SJeff Mahoney /* 372549b25e05SJeff Mahoney memset(cur_trans, 0, sizeof(*cur_trans)); 372649b25e05SJeff Mahoney kmem_cache_free(btrfs_transaction_cachep, cur_trans); 372749b25e05SJeff Mahoney */ 372849b25e05SJeff Mahoney } 372949b25e05SJeff Mahoney 373049b25e05SJeff Mahoney int btrfs_cleanup_transaction(struct btrfs_root *root) 3731acce952bSliubo { 3732acce952bSliubo struct btrfs_transaction *t; 3733acce952bSliubo LIST_HEAD(list); 3734acce952bSliubo 3735acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 3736acce952bSliubo 3737a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3738acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 3739a4abeea4SJosef Bacik root->fs_info->trans_no_join = 1; 3740a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3741a4abeea4SJosef Bacik 3742acce952bSliubo while (!list_empty(&list)) { 3743acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 3744acce952bSliubo if (!t) 3745acce952bSliubo break; 3746acce952bSliubo 3747acce952bSliubo btrfs_destroy_ordered_operations(root); 3748acce952bSliubo 3749acce952bSliubo btrfs_destroy_ordered_extents(root); 3750acce952bSliubo 3751acce952bSliubo btrfs_destroy_delayed_refs(t, root); 3752acce952bSliubo 3753acce952bSliubo btrfs_block_rsv_release(root, 3754acce952bSliubo &root->fs_info->trans_block_rsv, 3755acce952bSliubo t->dirty_pages.dirty_bytes); 3756acce952bSliubo 3757acce952bSliubo /* FIXME: cleanup wait for commit */ 3758acce952bSliubo t->in_commit = 1; 3759acce952bSliubo t->blocked = 1; 376066657b31SJosef Bacik smp_mb(); 3761acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3762acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 3763acce952bSliubo 3764acce952bSliubo t->blocked = 0; 376566657b31SJosef Bacik smp_mb(); 3766acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 3767acce952bSliubo wake_up(&root->fs_info->transaction_wait); 3768acce952bSliubo 3769acce952bSliubo t->commit_done = 1; 377066657b31SJosef Bacik smp_mb(); 3771acce952bSliubo if (waitqueue_active(&t->commit_wait)) 3772acce952bSliubo wake_up(&t->commit_wait); 3773acce952bSliubo 377467cde344SMiao Xie btrfs_destroy_delayed_inodes(root); 377567cde344SMiao Xie btrfs_assert_delayed_root_empty(root); 377667cde344SMiao Xie 3777acce952bSliubo btrfs_destroy_pending_snapshots(t); 3778acce952bSliubo 3779acce952bSliubo btrfs_destroy_delalloc_inodes(root); 3780acce952bSliubo 3781a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3782acce952bSliubo root->fs_info->running_transaction = NULL; 3783a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3784acce952bSliubo 3785acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 3786acce952bSliubo EXTENT_DIRTY); 3787acce952bSliubo 3788acce952bSliubo btrfs_destroy_pinned_extent(root, 3789acce952bSliubo root->fs_info->pinned_extents); 3790acce952bSliubo 379113c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 3792acce952bSliubo list_del_init(&t->list); 3793acce952bSliubo memset(t, 0, sizeof(*t)); 3794acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 3795acce952bSliubo } 3796acce952bSliubo 3797a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3798a4abeea4SJosef Bacik root->fs_info->trans_no_join = 0; 3799a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3800acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 3801acce952bSliubo 3802acce952bSliubo return 0; 3803acce952bSliubo } 3804acce952bSliubo 3805d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 38064bef0848SChris Mason .write_cache_pages_lock_hook = btree_lock_page_hook, 3807ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 38084bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 38090b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 3810239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 3811239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 38120da5468fSChris Mason }; 3813