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" 47eb60ceacSChris Mason 48d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops; 498b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work); 504df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root); 51acce952bSliubo static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 52acce952bSliubo int read_only); 53143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root); 54143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 55acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 56acce952bSliubo struct btrfs_root *root); 57143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t); 58143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 59acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 60acce952bSliubo struct extent_io_tree *dirty_pages, 61acce952bSliubo int mark); 62acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 63acce952bSliubo struct extent_io_tree *pinned_extents); 64acce952bSliubo static int btrfs_cleanup_transaction(struct btrfs_root *root); 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; 10244b8bd7eSChris Mason }; 10344b8bd7eSChris Mason 10485d4e461SChris Mason /* 10585d4e461SChris Mason * Lockdep class keys for extent_buffer->lock's in this root. For a given 10685d4e461SChris Mason * eb, the lockdep key is determined by the btrfs_root it belongs to and 10785d4e461SChris Mason * the level the eb occupies in the tree. 1084008c04aSChris Mason * 10985d4e461SChris Mason * Different roots are used for different purposes and may nest inside each 11085d4e461SChris Mason * other and they require separate keysets. As lockdep keys should be 11185d4e461SChris Mason * static, assign keysets according to the purpose of the root as indicated 11285d4e461SChris Mason * by btrfs_root->objectid. This ensures that all special purpose roots 11385d4e461SChris Mason * have separate keysets. 1144008c04aSChris Mason * 11585d4e461SChris Mason * Lock-nesting across peer nodes is always done with the immediate parent 11685d4e461SChris Mason * node locked thus preventing deadlock. As lockdep doesn't know this, use 11785d4e461SChris Mason * subclass to avoid triggering lockdep warning in such cases. 1184008c04aSChris Mason * 11985d4e461SChris Mason * The key is set by the readpage_end_io_hook after the buffer has passed 12085d4e461SChris Mason * csum validation but before the pages are unlocked. It is also set by 12185d4e461SChris Mason * btrfs_init_new_buffer on freshly allocated blocks. 12285d4e461SChris Mason * 12385d4e461SChris Mason * We also add a check to make sure the highest level of the tree is the 12485d4e461SChris Mason * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code 12585d4e461SChris Mason * needs update as well. 1264008c04aSChris Mason */ 1274008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC 1284008c04aSChris Mason # if BTRFS_MAX_LEVEL != 8 1294008c04aSChris Mason # error 1304008c04aSChris Mason # endif 13185d4e461SChris Mason 13285d4e461SChris Mason static struct btrfs_lockdep_keyset { 13385d4e461SChris Mason u64 id; /* root objectid */ 13485d4e461SChris Mason const char *name_stem; /* lock name stem */ 13585d4e461SChris Mason char names[BTRFS_MAX_LEVEL + 1][20]; 13685d4e461SChris Mason struct lock_class_key keys[BTRFS_MAX_LEVEL + 1]; 13785d4e461SChris Mason } btrfs_lockdep_keysets[] = { 13885d4e461SChris Mason { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" }, 13985d4e461SChris Mason { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" }, 14085d4e461SChris Mason { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" }, 14185d4e461SChris Mason { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" }, 14285d4e461SChris Mason { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" }, 14385d4e461SChris Mason { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" }, 14485d4e461SChris Mason { .id = BTRFS_ORPHAN_OBJECTID, .name_stem = "orphan" }, 14585d4e461SChris Mason { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" }, 14685d4e461SChris Mason { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" }, 14785d4e461SChris Mason { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" }, 14885d4e461SChris Mason { .id = 0, .name_stem = "tree" }, 1494008c04aSChris Mason }; 15085d4e461SChris Mason 15185d4e461SChris Mason void __init btrfs_init_lockdep(void) 15285d4e461SChris Mason { 15385d4e461SChris Mason int i, j; 15485d4e461SChris Mason 15585d4e461SChris Mason /* initialize lockdep class names */ 15685d4e461SChris Mason for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) { 15785d4e461SChris Mason struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i]; 15885d4e461SChris Mason 15985d4e461SChris Mason for (j = 0; j < ARRAY_SIZE(ks->names); j++) 16085d4e461SChris Mason snprintf(ks->names[j], sizeof(ks->names[j]), 16185d4e461SChris Mason "btrfs-%s-%02d", ks->name_stem, j); 16285d4e461SChris Mason } 16385d4e461SChris Mason } 16485d4e461SChris Mason 16585d4e461SChris Mason void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, 16685d4e461SChris Mason int level) 16785d4e461SChris Mason { 16885d4e461SChris Mason struct btrfs_lockdep_keyset *ks; 16985d4e461SChris Mason 17085d4e461SChris Mason BUG_ON(level >= ARRAY_SIZE(ks->keys)); 17185d4e461SChris Mason 17285d4e461SChris Mason /* find the matching keyset, id 0 is the default entry */ 17385d4e461SChris Mason for (ks = btrfs_lockdep_keysets; ks->id; ks++) 17485d4e461SChris Mason if (ks->id == objectid) 17585d4e461SChris Mason break; 17685d4e461SChris Mason 17785d4e461SChris Mason lockdep_set_class_and_name(&eb->lock, 17885d4e461SChris Mason &ks->keys[level], ks->names[level]); 17985d4e461SChris Mason } 18085d4e461SChris Mason 1814008c04aSChris Mason #endif 1824008c04aSChris Mason 183d352ac68SChris Mason /* 184d352ac68SChris Mason * extents on the btree inode are pretty simple, there's one extent 185d352ac68SChris Mason * that covers the entire device 186d352ac68SChris Mason */ 187b2950863SChristoph Hellwig static struct extent_map *btree_get_extent(struct inode *inode, 188306e16ceSDavid Sterba struct page *page, size_t pg_offset, u64 start, u64 len, 1895f39d397SChris Mason int create) 1905f39d397SChris Mason { 1915f39d397SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1925f39d397SChris Mason struct extent_map *em; 1935f39d397SChris Mason int ret; 1945f39d397SChris Mason 195890871beSChris Mason read_lock(&em_tree->lock); 196d1310b2eSChris Mason em = lookup_extent_mapping(em_tree, start, len); 197a061fc8dSChris Mason if (em) { 198a061fc8dSChris Mason em->bdev = 199a061fc8dSChris Mason BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 200890871beSChris Mason read_unlock(&em_tree->lock); 2015f39d397SChris Mason goto out; 202a061fc8dSChris Mason } 203890871beSChris Mason read_unlock(&em_tree->lock); 2047b13b7b1SChris Mason 205172ddd60SDavid Sterba em = alloc_extent_map(); 2065f39d397SChris Mason if (!em) { 2075f39d397SChris Mason em = ERR_PTR(-ENOMEM); 2085f39d397SChris Mason goto out; 2095f39d397SChris Mason } 2105f39d397SChris Mason em->start = 0; 2110afbaf8cSChris Mason em->len = (u64)-1; 212c8b97818SChris Mason em->block_len = (u64)-1; 2135f39d397SChris Mason em->block_start = 0; 214a061fc8dSChris Mason em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 215d1310b2eSChris Mason 216890871beSChris Mason write_lock(&em_tree->lock); 2175f39d397SChris Mason ret = add_extent_mapping(em_tree, em); 2185f39d397SChris Mason if (ret == -EEXIST) { 2190afbaf8cSChris Mason u64 failed_start = em->start; 2200afbaf8cSChris Mason u64 failed_len = em->len; 2210afbaf8cSChris Mason 2225f39d397SChris Mason free_extent_map(em); 2237b13b7b1SChris Mason em = lookup_extent_mapping(em_tree, start, len); 2240afbaf8cSChris Mason if (em) { 2257b13b7b1SChris Mason ret = 0; 2260afbaf8cSChris Mason } else { 2270afbaf8cSChris Mason em = lookup_extent_mapping(em_tree, failed_start, 2280afbaf8cSChris Mason failed_len); 2297b13b7b1SChris Mason ret = -EIO; 2300afbaf8cSChris Mason } 2315f39d397SChris Mason } else if (ret) { 2327b13b7b1SChris Mason free_extent_map(em); 2337b13b7b1SChris Mason em = NULL; 2345f39d397SChris Mason } 235890871beSChris Mason write_unlock(&em_tree->lock); 2367b13b7b1SChris Mason 2377b13b7b1SChris Mason if (ret) 2387b13b7b1SChris Mason em = ERR_PTR(ret); 2395f39d397SChris Mason out: 2405f39d397SChris Mason return em; 2415f39d397SChris Mason } 2425f39d397SChris Mason 24319c00ddcSChris Mason u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) 24419c00ddcSChris Mason { 245163e783eSDavid Woodhouse return crc32c(seed, data, len); 24619c00ddcSChris Mason } 24719c00ddcSChris Mason 24819c00ddcSChris Mason void btrfs_csum_final(u32 crc, char *result) 24919c00ddcSChris Mason { 2507e75bf3fSDavid Sterba put_unaligned_le32(~crc, result); 25119c00ddcSChris Mason } 25219c00ddcSChris Mason 253d352ac68SChris Mason /* 254d352ac68SChris Mason * compute the csum for a btree block, and either verify it or write it 255d352ac68SChris Mason * into the csum field of the block. 256d352ac68SChris Mason */ 25719c00ddcSChris Mason static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 25819c00ddcSChris Mason int verify) 25919c00ddcSChris Mason { 2606c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 261607d432dSJosef Bacik char *result = NULL; 26219c00ddcSChris Mason unsigned long len; 26319c00ddcSChris Mason unsigned long cur_len; 26419c00ddcSChris Mason unsigned long offset = BTRFS_CSUM_SIZE; 26519c00ddcSChris Mason char *kaddr; 26619c00ddcSChris Mason unsigned long map_start; 26719c00ddcSChris Mason unsigned long map_len; 26819c00ddcSChris Mason int err; 26919c00ddcSChris Mason u32 crc = ~(u32)0; 270607d432dSJosef Bacik unsigned long inline_result; 27119c00ddcSChris Mason 27219c00ddcSChris Mason len = buf->len - offset; 27319c00ddcSChris Mason while (len > 0) { 27419c00ddcSChris Mason err = map_private_extent_buffer(buf, offset, 32, 275a6591715SChris Mason &kaddr, &map_start, &map_len); 276d397712bSChris Mason if (err) 27719c00ddcSChris Mason return 1; 27819c00ddcSChris Mason cur_len = min(len, map_len - (offset - map_start)); 27919c00ddcSChris Mason crc = btrfs_csum_data(root, kaddr + offset - map_start, 28019c00ddcSChris Mason crc, cur_len); 28119c00ddcSChris Mason len -= cur_len; 28219c00ddcSChris Mason offset += cur_len; 28319c00ddcSChris Mason } 284607d432dSJosef Bacik if (csum_size > sizeof(inline_result)) { 285607d432dSJosef Bacik result = kzalloc(csum_size * sizeof(char), GFP_NOFS); 286607d432dSJosef Bacik if (!result) 287607d432dSJosef Bacik return 1; 288607d432dSJosef Bacik } else { 289607d432dSJosef Bacik result = (char *)&inline_result; 290607d432dSJosef Bacik } 291607d432dSJosef Bacik 29219c00ddcSChris Mason btrfs_csum_final(crc, result); 29319c00ddcSChris Mason 29419c00ddcSChris Mason if (verify) { 295607d432dSJosef Bacik if (memcmp_extent_buffer(buf, result, 0, csum_size)) { 296e4204dedSChris Mason u32 val; 297e4204dedSChris Mason u32 found = 0; 298607d432dSJosef Bacik memcpy(&found, result, csum_size); 299e4204dedSChris Mason 300607d432dSJosef Bacik read_extent_buffer(buf, &val, 0, csum_size); 3017a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs: %s checksum verify " 302193f284dSChris Mason "failed on %llu wanted %X found %X " 303193f284dSChris Mason "level %d\n", 30419c00ddcSChris Mason root->fs_info->sb->s_id, 305193f284dSChris Mason (unsigned long long)buf->start, val, found, 306193f284dSChris Mason btrfs_header_level(buf)); 307607d432dSJosef Bacik if (result != (char *)&inline_result) 308607d432dSJosef Bacik kfree(result); 30919c00ddcSChris Mason return 1; 31019c00ddcSChris Mason } 31119c00ddcSChris Mason } else { 312607d432dSJosef Bacik write_extent_buffer(buf, result, 0, csum_size); 31319c00ddcSChris Mason } 314607d432dSJosef Bacik if (result != (char *)&inline_result) 315607d432dSJosef Bacik kfree(result); 31619c00ddcSChris Mason return 0; 31719c00ddcSChris Mason } 31819c00ddcSChris Mason 319d352ac68SChris Mason /* 320d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the 321d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we 322d352ac68SChris Mason * detect blocks that either didn't get written at all or got written 323d352ac68SChris Mason * in the wrong place. 324d352ac68SChris Mason */ 3251259ab75SChris Mason static int verify_parent_transid(struct extent_io_tree *io_tree, 3261259ab75SChris Mason struct extent_buffer *eb, u64 parent_transid) 3271259ab75SChris Mason { 3282ac55d41SJosef Bacik struct extent_state *cached_state = NULL; 3291259ab75SChris Mason int ret; 3301259ab75SChris Mason 3311259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 3321259ab75SChris Mason return 0; 3331259ab75SChris Mason 3342ac55d41SJosef Bacik lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 3352ac55d41SJosef Bacik 0, &cached_state, GFP_NOFS); 3362ac55d41SJosef Bacik if (extent_buffer_uptodate(io_tree, eb, cached_state) && 3371259ab75SChris Mason btrfs_header_generation(eb) == parent_transid) { 3381259ab75SChris Mason ret = 0; 3391259ab75SChris Mason goto out; 3401259ab75SChris Mason } 3417a36ddecSDavid Sterba printk_ratelimited("parent transid verify failed on %llu wanted %llu " 342193f284dSChris Mason "found %llu\n", 3431259ab75SChris Mason (unsigned long long)eb->start, 3441259ab75SChris Mason (unsigned long long)parent_transid, 3451259ab75SChris Mason (unsigned long long)btrfs_header_generation(eb)); 3461259ab75SChris Mason ret = 1; 3472ac55d41SJosef Bacik clear_extent_buffer_uptodate(io_tree, eb, &cached_state); 34833958dc6SChris Mason out: 3492ac55d41SJosef Bacik unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 3502ac55d41SJosef Bacik &cached_state, GFP_NOFS); 3511259ab75SChris Mason return ret; 3521259ab75SChris Mason } 3531259ab75SChris Mason 354d352ac68SChris Mason /* 355d352ac68SChris Mason * helper to read a given tree block, doing retries as required when 356d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try. 357d352ac68SChris Mason */ 358f188591eSChris Mason static int btree_read_extent_buffer_pages(struct btrfs_root *root, 359f188591eSChris Mason struct extent_buffer *eb, 360ca7a79adSChris Mason u64 start, u64 parent_transid) 361f188591eSChris Mason { 362f188591eSChris Mason struct extent_io_tree *io_tree; 363f188591eSChris Mason int ret; 364f188591eSChris Mason int num_copies = 0; 365f188591eSChris Mason int mirror_num = 0; 366f188591eSChris Mason 367a826d6dcSJosef Bacik clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 368f188591eSChris Mason io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 369f188591eSChris Mason while (1) { 370bb82ab88SArne Jansen ret = read_extent_buffer_pages(io_tree, eb, start, 371bb82ab88SArne Jansen WAIT_COMPLETE, 372f188591eSChris Mason btree_get_extent, mirror_num); 3731259ab75SChris Mason if (!ret && 3741259ab75SChris Mason !verify_parent_transid(io_tree, eb, parent_transid)) 375f188591eSChris Mason return ret; 376d397712bSChris Mason 377a826d6dcSJosef Bacik /* 378a826d6dcSJosef Bacik * This buffer's crc is fine, but its contents are corrupted, so 379a826d6dcSJosef Bacik * there is no reason to read the other copies, they won't be 380a826d6dcSJosef Bacik * any less wrong. 381a826d6dcSJosef Bacik */ 382a826d6dcSJosef Bacik if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 383a826d6dcSJosef Bacik return ret; 384a826d6dcSJosef Bacik 385f188591eSChris Mason num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, 386f188591eSChris Mason eb->start, eb->len); 3874235298eSChris Mason if (num_copies == 1) 388f188591eSChris Mason return ret; 3894235298eSChris Mason 390f188591eSChris Mason mirror_num++; 3914235298eSChris Mason if (mirror_num > num_copies) 392f188591eSChris Mason return ret; 393f188591eSChris Mason } 394f188591eSChris Mason return -EIO; 395f188591eSChris Mason } 39619c00ddcSChris Mason 397d352ac68SChris Mason /* 398d397712bSChris Mason * checksum a dirty tree block before IO. This has extra checks to make sure 399d397712bSChris Mason * we only fill in the checksum field in the first page of a multi-page block 400d352ac68SChris Mason */ 401d397712bSChris Mason 402b2950863SChristoph Hellwig static int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 40319c00ddcSChris Mason { 404d1310b2eSChris Mason struct extent_io_tree *tree; 40535ebb934SChris Mason u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 40619c00ddcSChris Mason u64 found_start; 40719c00ddcSChris Mason unsigned long len; 40819c00ddcSChris Mason struct extent_buffer *eb; 409f188591eSChris Mason int ret; 410f188591eSChris Mason 411d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 41219c00ddcSChris Mason 413eb14ab8eSChris Mason if (page->private == EXTENT_PAGE_PRIVATE) { 414eb14ab8eSChris Mason WARN_ON(1); 41519c00ddcSChris Mason goto out; 416eb14ab8eSChris Mason } 417eb14ab8eSChris Mason if (!page->private) { 418eb14ab8eSChris Mason WARN_ON(1); 41919c00ddcSChris Mason goto out; 420eb14ab8eSChris Mason } 42119c00ddcSChris Mason len = page->private >> 2; 422d397712bSChris Mason WARN_ON(len == 0); 423d397712bSChris Mason 424ba144192SDavid Sterba eb = alloc_extent_buffer(tree, start, len, page); 42591ca338dSTsutomu Itoh if (eb == NULL) { 42691ca338dSTsutomu Itoh WARN_ON(1); 42791ca338dSTsutomu Itoh goto out; 42891ca338dSTsutomu Itoh } 429ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE, 430ca7a79adSChris Mason btrfs_header_generation(eb)); 431f188591eSChris Mason BUG_ON(ret); 432784b4e29SChris Mason WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN)); 433784b4e29SChris Mason 43419c00ddcSChris Mason found_start = btrfs_header_bytenr(eb); 43519c00ddcSChris Mason if (found_start != start) { 43655c69072SChris Mason WARN_ON(1); 43755c69072SChris Mason goto err; 43855c69072SChris Mason } 43955c69072SChris Mason if (eb->first_page != page) { 44055c69072SChris Mason WARN_ON(1); 44155c69072SChris Mason goto err; 44255c69072SChris Mason } 44355c69072SChris Mason if (!PageUptodate(page)) { 44455c69072SChris Mason WARN_ON(1); 44555c69072SChris Mason goto err; 44619c00ddcSChris Mason } 44719c00ddcSChris Mason csum_tree_block(root, eb, 0); 44855c69072SChris Mason err: 44919c00ddcSChris Mason free_extent_buffer(eb); 45019c00ddcSChris Mason out: 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 540b2950863SChristoph Hellwig static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 541ce9adaa5SChris Mason struct extent_state *state) 542ce9adaa5SChris Mason { 543ce9adaa5SChris Mason struct extent_io_tree *tree; 544ce9adaa5SChris Mason u64 found_start; 545ce9adaa5SChris Mason int found_level; 546ce9adaa5SChris Mason unsigned long len; 547ce9adaa5SChris Mason struct extent_buffer *eb; 548ce9adaa5SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 549f188591eSChris Mason int ret = 0; 550ce9adaa5SChris Mason 551ce9adaa5SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 552ce9adaa5SChris Mason if (page->private == EXTENT_PAGE_PRIVATE) 553ce9adaa5SChris Mason goto out; 554ce9adaa5SChris Mason if (!page->private) 555ce9adaa5SChris Mason goto out; 556d397712bSChris Mason 557ce9adaa5SChris Mason len = page->private >> 2; 558d397712bSChris Mason WARN_ON(len == 0); 559d397712bSChris Mason 560ba144192SDavid Sterba eb = alloc_extent_buffer(tree, start, len, page); 56191ca338dSTsutomu Itoh if (eb == NULL) { 56291ca338dSTsutomu Itoh ret = -EIO; 56391ca338dSTsutomu Itoh goto out; 56491ca338dSTsutomu Itoh } 565f188591eSChris Mason 566ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb); 56723a07867SChris Mason if (found_start != start) { 5687a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad tree block start " 569193f284dSChris Mason "%llu %llu\n", 570a1b32a59SChris Mason (unsigned long long)found_start, 571a1b32a59SChris Mason (unsigned long long)eb->start); 572f188591eSChris Mason ret = -EIO; 573ce9adaa5SChris Mason goto err; 574ce9adaa5SChris Mason } 575ce9adaa5SChris Mason if (eb->first_page != page) { 576d397712bSChris Mason printk(KERN_INFO "btrfs bad first page %lu %lu\n", 577d397712bSChris Mason eb->first_page->index, page->index); 578ce9adaa5SChris Mason WARN_ON(1); 579f188591eSChris Mason ret = -EIO; 580ce9adaa5SChris Mason goto err; 581ce9adaa5SChris Mason } 5822b82032cSYan Zheng if (check_tree_block_fsid(root, eb)) { 5837a36ddecSDavid Sterba printk_ratelimited(KERN_INFO "btrfs bad fsid on block %llu\n", 584d397712bSChris Mason (unsigned long long)eb->start); 5851259ab75SChris Mason ret = -EIO; 5861259ab75SChris Mason goto err; 5871259ab75SChris Mason } 588ce9adaa5SChris Mason found_level = btrfs_header_level(eb); 589ce9adaa5SChris Mason 59085d4e461SChris Mason btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), 59185d4e461SChris Mason eb, found_level); 5924008c04aSChris Mason 593ce9adaa5SChris Mason ret = csum_tree_block(root, eb, 1); 594a826d6dcSJosef Bacik if (ret) { 595f188591eSChris Mason ret = -EIO; 596a826d6dcSJosef Bacik goto err; 597a826d6dcSJosef Bacik } 598a826d6dcSJosef Bacik 599a826d6dcSJosef Bacik /* 600a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so 601a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just 602a826d6dcSJosef Bacik * return -EIO. 603a826d6dcSJosef Bacik */ 604a826d6dcSJosef Bacik if (found_level == 0 && check_leaf(root, eb)) { 605a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 606a826d6dcSJosef Bacik ret = -EIO; 607a826d6dcSJosef Bacik } 608ce9adaa5SChris Mason 609ce9adaa5SChris Mason end = min_t(u64, eb->len, PAGE_CACHE_SIZE); 610ce9adaa5SChris Mason end = eb->start + end - 1; 611ce9adaa5SChris Mason err: 6124bb31e92SArne Jansen if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) { 6134bb31e92SArne Jansen clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags); 6144bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, ret); 6154bb31e92SArne Jansen } 6164bb31e92SArne Jansen 617ce9adaa5SChris Mason free_extent_buffer(eb); 618ce9adaa5SChris Mason out: 619f188591eSChris Mason return ret; 620ce9adaa5SChris Mason } 621ce9adaa5SChris Mason 6224bb31e92SArne Jansen static int btree_io_failed_hook(struct bio *failed_bio, 6234bb31e92SArne Jansen struct page *page, u64 start, u64 end, 62432240a91SJan Schmidt int mirror_num, struct extent_state *state) 6254bb31e92SArne Jansen { 6264bb31e92SArne Jansen struct extent_io_tree *tree; 6274bb31e92SArne Jansen unsigned long len; 6284bb31e92SArne Jansen struct extent_buffer *eb; 6294bb31e92SArne Jansen struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 6304bb31e92SArne Jansen 6314bb31e92SArne Jansen tree = &BTRFS_I(page->mapping->host)->io_tree; 6324bb31e92SArne Jansen if (page->private == EXTENT_PAGE_PRIVATE) 6334bb31e92SArne Jansen goto out; 6344bb31e92SArne Jansen if (!page->private) 6354bb31e92SArne Jansen goto out; 6364bb31e92SArne Jansen 6374bb31e92SArne Jansen len = page->private >> 2; 6384bb31e92SArne Jansen WARN_ON(len == 0); 6394bb31e92SArne Jansen 6404bb31e92SArne Jansen eb = alloc_extent_buffer(tree, start, len, page); 6414bb31e92SArne Jansen if (eb == NULL) 6424bb31e92SArne Jansen goto out; 6434bb31e92SArne Jansen 6444bb31e92SArne Jansen if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) { 6454bb31e92SArne Jansen clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags); 6464bb31e92SArne Jansen btree_readahead_hook(root, eb, eb->start, -EIO); 6474bb31e92SArne Jansen } 648c674e04eSChris Mason free_extent_buffer(eb); 6494bb31e92SArne Jansen 6504bb31e92SArne Jansen out: 6514bb31e92SArne Jansen return -EIO; /* we fixed nothing */ 6524bb31e92SArne Jansen } 6534bb31e92SArne Jansen 654ce9adaa5SChris Mason static void end_workqueue_bio(struct bio *bio, int err) 655ce9adaa5SChris Mason { 656ce9adaa5SChris Mason struct end_io_wq *end_io_wq = bio->bi_private; 657ce9adaa5SChris Mason struct btrfs_fs_info *fs_info; 658ce9adaa5SChris Mason 659ce9adaa5SChris Mason fs_info = end_io_wq->info; 660ce9adaa5SChris Mason end_io_wq->error = err; 6618b712842SChris Mason end_io_wq->work.func = end_workqueue_fn; 6628b712842SChris Mason end_io_wq->work.flags = 0; 663d20f7043SChris Mason 6647b6d91daSChristoph Hellwig if (bio->bi_rw & REQ_WRITE) { 6650cb59c99SJosef Bacik if (end_io_wq->metadata == 1) 666cad321adSChris Mason btrfs_queue_worker(&fs_info->endio_meta_write_workers, 667cad321adSChris Mason &end_io_wq->work); 6680cb59c99SJosef Bacik else if (end_io_wq->metadata == 2) 6690cb59c99SJosef Bacik btrfs_queue_worker(&fs_info->endio_freespace_worker, 6700cb59c99SJosef Bacik &end_io_wq->work); 671cad321adSChris Mason else 672e6dcd2dcSChris Mason btrfs_queue_worker(&fs_info->endio_write_workers, 673e6dcd2dcSChris Mason &end_io_wq->work); 674d20f7043SChris Mason } else { 675d20f7043SChris Mason if (end_io_wq->metadata) 676d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 677d20f7043SChris Mason &end_io_wq->work); 678e6dcd2dcSChris Mason else 679d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_workers, 680d20f7043SChris Mason &end_io_wq->work); 681d20f7043SChris Mason } 682ce9adaa5SChris Mason } 683ce9adaa5SChris Mason 6840cb59c99SJosef Bacik /* 6850cb59c99SJosef Bacik * For the metadata arg you want 6860cb59c99SJosef Bacik * 6870cb59c99SJosef Bacik * 0 - if data 6880cb59c99SJosef Bacik * 1 - if normal metadta 6890cb59c99SJosef Bacik * 2 - if writing to the free space cache area 6900cb59c99SJosef Bacik */ 69122c59948SChris Mason int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 69222c59948SChris Mason int metadata) 6930b86a832SChris Mason { 694ce9adaa5SChris Mason struct end_io_wq *end_io_wq; 695ce9adaa5SChris Mason end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 696ce9adaa5SChris Mason if (!end_io_wq) 697ce9adaa5SChris Mason return -ENOMEM; 698ce9adaa5SChris Mason 699ce9adaa5SChris Mason end_io_wq->private = bio->bi_private; 700ce9adaa5SChris Mason end_io_wq->end_io = bio->bi_end_io; 70122c59948SChris Mason end_io_wq->info = info; 702ce9adaa5SChris Mason end_io_wq->error = 0; 703ce9adaa5SChris Mason end_io_wq->bio = bio; 70422c59948SChris Mason end_io_wq->metadata = metadata; 705ce9adaa5SChris Mason 706ce9adaa5SChris Mason bio->bi_private = end_io_wq; 707ce9adaa5SChris Mason bio->bi_end_io = end_workqueue_bio; 70822c59948SChris Mason return 0; 70922c59948SChris Mason } 71022c59948SChris Mason 711b64a2851SChris Mason unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info) 7124854ddd0SChris Mason { 7134854ddd0SChris Mason unsigned long limit = min_t(unsigned long, 7144854ddd0SChris Mason info->workers.max_workers, 7154854ddd0SChris Mason info->fs_devices->open_devices); 7164854ddd0SChris Mason return 256 * limit; 7174854ddd0SChris Mason } 7184854ddd0SChris Mason 7194a69a410SChris Mason static void run_one_async_start(struct btrfs_work *work) 7204a69a410SChris Mason { 7214a69a410SChris Mason struct async_submit_bio *async; 7224a69a410SChris Mason 7234a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7244a69a410SChris Mason async->submit_bio_start(async->inode, async->rw, async->bio, 725eaf25d93SChris Mason async->mirror_num, async->bio_flags, 726eaf25d93SChris Mason async->bio_offset); 7274a69a410SChris Mason } 7284a69a410SChris Mason 7294a69a410SChris Mason static void run_one_async_done(struct btrfs_work *work) 7308b712842SChris Mason { 7318b712842SChris Mason struct btrfs_fs_info *fs_info; 7328b712842SChris Mason struct async_submit_bio *async; 7334854ddd0SChris Mason int limit; 7348b712842SChris Mason 7358b712842SChris Mason async = container_of(work, struct async_submit_bio, work); 7368b712842SChris Mason fs_info = BTRFS_I(async->inode)->root->fs_info; 7374854ddd0SChris Mason 738b64a2851SChris Mason limit = btrfs_async_submit_limit(fs_info); 7394854ddd0SChris Mason limit = limit * 2 / 3; 7404854ddd0SChris Mason 7418b712842SChris Mason atomic_dec(&fs_info->nr_async_submits); 7420986fe9eSChris Mason 743b64a2851SChris Mason if (atomic_read(&fs_info->nr_async_submits) < limit && 744b64a2851SChris Mason waitqueue_active(&fs_info->async_submit_wait)) 7454854ddd0SChris Mason wake_up(&fs_info->async_submit_wait); 7464854ddd0SChris Mason 7474a69a410SChris Mason async->submit_bio_done(async->inode, async->rw, async->bio, 748eaf25d93SChris Mason async->mirror_num, async->bio_flags, 749eaf25d93SChris Mason async->bio_offset); 7504a69a410SChris Mason } 7514a69a410SChris Mason 7524a69a410SChris Mason static void run_one_async_free(struct btrfs_work *work) 7534a69a410SChris Mason { 7544a69a410SChris Mason struct async_submit_bio *async; 7554a69a410SChris Mason 7564a69a410SChris Mason async = container_of(work, struct async_submit_bio, work); 7578b712842SChris Mason kfree(async); 7588b712842SChris Mason } 7598b712842SChris Mason 76044b8bd7eSChris Mason int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 76144b8bd7eSChris Mason int rw, struct bio *bio, int mirror_num, 762c8b97818SChris Mason unsigned long bio_flags, 763eaf25d93SChris Mason u64 bio_offset, 7644a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_start, 7654a69a410SChris Mason extent_submit_bio_hook_t *submit_bio_done) 76644b8bd7eSChris Mason { 76744b8bd7eSChris Mason struct async_submit_bio *async; 76844b8bd7eSChris Mason 76944b8bd7eSChris Mason async = kmalloc(sizeof(*async), GFP_NOFS); 77044b8bd7eSChris Mason if (!async) 77144b8bd7eSChris Mason return -ENOMEM; 77244b8bd7eSChris Mason 77344b8bd7eSChris Mason async->inode = inode; 77444b8bd7eSChris Mason async->rw = rw; 77544b8bd7eSChris Mason async->bio = bio; 77644b8bd7eSChris Mason async->mirror_num = mirror_num; 7774a69a410SChris Mason async->submit_bio_start = submit_bio_start; 7784a69a410SChris Mason async->submit_bio_done = submit_bio_done; 7794a69a410SChris Mason 7804a69a410SChris Mason async->work.func = run_one_async_start; 7814a69a410SChris Mason async->work.ordered_func = run_one_async_done; 7824a69a410SChris Mason async->work.ordered_free = run_one_async_free; 7834a69a410SChris Mason 7848b712842SChris Mason async->work.flags = 0; 785c8b97818SChris Mason async->bio_flags = bio_flags; 786eaf25d93SChris Mason async->bio_offset = bio_offset; 7878c8bee1dSChris Mason 788cb03c743SChris Mason atomic_inc(&fs_info->nr_async_submits); 789d313d7a3SChris Mason 7907b6d91daSChristoph Hellwig if (rw & REQ_SYNC) 791d313d7a3SChris Mason btrfs_set_work_high_prio(&async->work); 792d313d7a3SChris Mason 7938b712842SChris Mason btrfs_queue_worker(&fs_info->workers, &async->work); 7949473f16cSChris Mason 795771ed689SChris Mason while (atomic_read(&fs_info->async_submit_draining) && 796771ed689SChris Mason atomic_read(&fs_info->nr_async_submits)) { 797771ed689SChris Mason wait_event(fs_info->async_submit_wait, 798771ed689SChris Mason (atomic_read(&fs_info->nr_async_submits) == 0)); 799771ed689SChris Mason } 800771ed689SChris Mason 80144b8bd7eSChris Mason return 0; 80244b8bd7eSChris Mason } 80344b8bd7eSChris Mason 804ce3ed71aSChris Mason static int btree_csum_one_bio(struct bio *bio) 805ce3ed71aSChris Mason { 806ce3ed71aSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 807ce3ed71aSChris Mason int bio_index = 0; 808ce3ed71aSChris Mason struct btrfs_root *root; 809ce3ed71aSChris Mason 810ce3ed71aSChris Mason WARN_ON(bio->bi_vcnt <= 0); 811ce3ed71aSChris Mason while (bio_index < bio->bi_vcnt) { 812ce3ed71aSChris Mason root = BTRFS_I(bvec->bv_page->mapping->host)->root; 813ce3ed71aSChris Mason csum_dirty_buffer(root, bvec->bv_page); 814ce3ed71aSChris Mason bio_index++; 815ce3ed71aSChris Mason bvec++; 816ce3ed71aSChris Mason } 817ce3ed71aSChris Mason return 0; 818ce3ed71aSChris Mason } 819ce3ed71aSChris Mason 8204a69a410SChris Mason static int __btree_submit_bio_start(struct inode *inode, int rw, 8214a69a410SChris Mason struct bio *bio, int mirror_num, 822eaf25d93SChris Mason unsigned long bio_flags, 823eaf25d93SChris Mason u64 bio_offset) 82422c59948SChris Mason { 8258b712842SChris Mason /* 8268b712842SChris Mason * when we're called for a write, we're already in the async 8275443be45SChris Mason * submission context. Just jump into btrfs_map_bio 8288b712842SChris Mason */ 829ce3ed71aSChris Mason btree_csum_one_bio(bio); 8304a69a410SChris Mason return 0; 83122c59948SChris Mason } 83222c59948SChris Mason 8334a69a410SChris Mason static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 834eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 835eaf25d93SChris Mason u64 bio_offset) 8364a69a410SChris Mason { 8378b712842SChris Mason /* 8384a69a410SChris Mason * when we're called for a write, we're already in the async 8394a69a410SChris Mason * submission context. Just jump into btrfs_map_bio 8408b712842SChris Mason */ 8418b712842SChris Mason return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); 8420b86a832SChris Mason } 8430b86a832SChris Mason 84444b8bd7eSChris Mason static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 845eaf25d93SChris Mason int mirror_num, unsigned long bio_flags, 846eaf25d93SChris Mason u64 bio_offset) 84744b8bd7eSChris Mason { 8484a69a410SChris Mason int ret; 849cad321adSChris Mason 850355808c2SJeff Mahoney ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, bio, 1); 851355808c2SJeff Mahoney if (ret) 852355808c2SJeff Mahoney return ret; 8534a69a410SChris Mason 8547b6d91daSChristoph Hellwig if (!(rw & REQ_WRITE)) { 855cad321adSChris Mason /* 856cad321adSChris Mason * called for a read, do the setup so that checksum validation 857cad321adSChris Mason * can happen in the async kernel threads 858cad321adSChris Mason */ 8594a69a410SChris Mason return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, 8606f3577bdSChris Mason mirror_num, 0); 86144b8bd7eSChris Mason } 862d313d7a3SChris Mason 863cad321adSChris Mason /* 864cad321adSChris Mason * kthread helpers are used to submit writes so that checksumming 865cad321adSChris Mason * can happen in parallel across all CPUs 866cad321adSChris Mason */ 86744b8bd7eSChris Mason return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 868c8b97818SChris Mason inode, rw, bio, mirror_num, 0, 869eaf25d93SChris Mason bio_offset, 8704a69a410SChris Mason __btree_submit_bio_start, 8714a69a410SChris Mason __btree_submit_bio_done); 87244b8bd7eSChris Mason } 87344b8bd7eSChris Mason 8743dd1462eSJan Beulich #ifdef CONFIG_MIGRATION 875784b4e29SChris Mason static int btree_migratepage(struct address_space *mapping, 876a6bc32b8SMel Gorman struct page *newpage, struct page *page, 877a6bc32b8SMel Gorman enum migrate_mode mode) 878784b4e29SChris Mason { 879784b4e29SChris Mason /* 880784b4e29SChris Mason * we can't safely write a btree page from here, 881784b4e29SChris Mason * we haven't done the locking hook 882784b4e29SChris Mason */ 883784b4e29SChris Mason if (PageDirty(page)) 884784b4e29SChris Mason return -EAGAIN; 885784b4e29SChris Mason /* 886784b4e29SChris Mason * Buffers may be managed in a filesystem specific way. 887784b4e29SChris Mason * We must have no buffers or drop them. 888784b4e29SChris Mason */ 889784b4e29SChris Mason if (page_has_private(page) && 890784b4e29SChris Mason !try_to_release_page(page, GFP_KERNEL)) 891784b4e29SChris Mason return -EAGAIN; 892a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 893784b4e29SChris Mason } 8943dd1462eSJan Beulich #endif 895784b4e29SChris Mason 8965f39d397SChris Mason static int btree_writepage(struct page *page, struct writeback_control *wbc) 8975f39d397SChris Mason { 898d1310b2eSChris Mason struct extent_io_tree *tree; 899b9473439SChris Mason struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 900b9473439SChris Mason struct extent_buffer *eb; 901b9473439SChris Mason int was_dirty; 9025443be45SChris Mason 903b9473439SChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 904b9473439SChris Mason if (!(current->flags & PF_MEMALLOC)) { 905b9473439SChris Mason return extent_write_full_page(tree, page, 906b9473439SChris Mason btree_get_extent, wbc); 907b9473439SChris Mason } 908b9473439SChris Mason 9095443be45SChris Mason redirty_page_for_writepage(wbc, page); 910784b4e29SChris Mason eb = btrfs_find_tree_block(root, page_offset(page), PAGE_CACHE_SIZE); 911b9473439SChris Mason WARN_ON(!eb); 912b9473439SChris Mason 913b9473439SChris Mason was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); 914b9473439SChris Mason if (!was_dirty) { 915b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 916b9473439SChris Mason root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE; 917b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 918b9473439SChris Mason } 919b9473439SChris Mason free_extent_buffer(eb); 920b9473439SChris Mason 9215443be45SChris Mason unlock_page(page); 9225443be45SChris Mason return 0; 9235443be45SChris Mason } 9240da5468fSChris Mason 9250da5468fSChris Mason static int btree_writepages(struct address_space *mapping, 9260da5468fSChris Mason struct writeback_control *wbc) 9270da5468fSChris Mason { 928d1310b2eSChris Mason struct extent_io_tree *tree; 929d1310b2eSChris Mason tree = &BTRFS_I(mapping->host)->io_tree; 930d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) { 931b9473439SChris Mason struct btrfs_root *root = BTRFS_I(mapping->host)->root; 932793955bcSChris Mason u64 num_dirty; 93324ab9cd8SChris Mason unsigned long thresh = 32 * 1024 * 1024; 934448d640bSChris Mason 935448d640bSChris Mason if (wbc->for_kupdate) 936448d640bSChris Mason return 0; 937448d640bSChris Mason 938b9473439SChris Mason /* this is a bit racy, but that's ok */ 939b9473439SChris Mason num_dirty = root->fs_info->dirty_metadata_bytes; 940d397712bSChris Mason if (num_dirty < thresh) 941793955bcSChris Mason return 0; 942793955bcSChris Mason } 9430da5468fSChris Mason return extent_writepages(tree, mapping, btree_get_extent, wbc); 9440da5468fSChris Mason } 9450da5468fSChris Mason 946b2950863SChristoph Hellwig static int btree_readpage(struct file *file, struct page *page) 9475f39d397SChris Mason { 948d1310b2eSChris Mason struct extent_io_tree *tree; 949d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9508ddc7d9cSJan Schmidt return extent_read_full_page(tree, page, btree_get_extent, 0); 9515f39d397SChris Mason } 9525f39d397SChris Mason 95370dec807SChris Mason static int btree_releasepage(struct page *page, gfp_t gfp_flags) 9545f39d397SChris Mason { 955d1310b2eSChris Mason struct extent_io_tree *tree; 956d1310b2eSChris Mason struct extent_map_tree *map; 9575f39d397SChris Mason int ret; 9585f39d397SChris Mason 95998509cfcSChris Mason if (PageWriteback(page) || PageDirty(page)) 96098509cfcSChris Mason return 0; 96198509cfcSChris Mason 962d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 963d1310b2eSChris Mason map = &BTRFS_I(page->mapping->host)->extent_tree; 9646af118ceSChris Mason 9650c4e538bSDavid Sterba /* 9660c4e538bSDavid Sterba * We need to mask out eg. __GFP_HIGHMEM and __GFP_DMA32 as we're doing 9670c4e538bSDavid Sterba * slab allocation from alloc_extent_state down the callchain where 9680c4e538bSDavid Sterba * it'd hit a BUG_ON as those flags are not allowed. 9690c4e538bSDavid Sterba */ 9700c4e538bSDavid Sterba gfp_flags &= ~GFP_SLAB_BUG_MASK; 9710c4e538bSDavid Sterba 9727b13b7b1SChris Mason ret = try_release_extent_state(map, tree, page, gfp_flags); 973d397712bSChris Mason if (!ret) 9746af118ceSChris Mason return 0; 9756af118ceSChris Mason 9766af118ceSChris Mason ret = try_release_extent_buffer(tree, page); 9775f39d397SChris Mason if (ret == 1) { 9785f39d397SChris Mason ClearPagePrivate(page); 9795f39d397SChris Mason set_page_private(page, 0); 980d98237b3SChris Mason page_cache_release(page); 9815f39d397SChris Mason } 9826af118ceSChris Mason 983d98237b3SChris Mason return ret; 984d98237b3SChris Mason } 985d98237b3SChris Mason 9865f39d397SChris Mason static void btree_invalidatepage(struct page *page, unsigned long offset) 987d98237b3SChris Mason { 988d1310b2eSChris Mason struct extent_io_tree *tree; 989d1310b2eSChris Mason tree = &BTRFS_I(page->mapping->host)->io_tree; 9905f39d397SChris Mason extent_invalidatepage(tree, page, offset); 9915f39d397SChris Mason btree_releasepage(page, GFP_NOFS); 9929ad6b7bcSChris Mason if (PagePrivate(page)) { 993d397712bSChris Mason printk(KERN_WARNING "btrfs warning page private not zero " 994d397712bSChris Mason "on page %llu\n", (unsigned long long)page_offset(page)); 9959ad6b7bcSChris Mason ClearPagePrivate(page); 9969ad6b7bcSChris Mason set_page_private(page, 0); 9979ad6b7bcSChris Mason page_cache_release(page); 9989ad6b7bcSChris Mason } 999d98237b3SChris Mason } 1000d98237b3SChris Mason 10017f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = { 1002d98237b3SChris Mason .readpage = btree_readpage, 1003d98237b3SChris Mason .writepage = btree_writepage, 10040da5468fSChris Mason .writepages = btree_writepages, 10055f39d397SChris Mason .releasepage = btree_releasepage, 10065f39d397SChris Mason .invalidatepage = btree_invalidatepage, 10075a92bc88SChris Mason #ifdef CONFIG_MIGRATION 1008784b4e29SChris Mason .migratepage = btree_migratepage, 10095a92bc88SChris Mason #endif 1010d98237b3SChris Mason }; 1011123abc88SChris Mason 1012ca7a79adSChris Mason int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1013ca7a79adSChris Mason u64 parent_transid) 1014090d1875SChris Mason { 10155f39d397SChris Mason struct extent_buffer *buf = NULL; 10165f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1017de428b63SChris Mason int ret = 0; 1018090d1875SChris Mason 1019db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 10205f39d397SChris Mason if (!buf) 1021090d1875SChris Mason return 0; 1022d1310b2eSChris Mason read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1023bb82ab88SArne Jansen buf, 0, WAIT_NONE, btree_get_extent, 0); 10245f39d397SChris Mason free_extent_buffer(buf); 1025de428b63SChris Mason return ret; 1026090d1875SChris Mason } 1027090d1875SChris Mason 1028ab0fff03SArne Jansen int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize, 1029ab0fff03SArne Jansen int mirror_num, struct extent_buffer **eb) 1030ab0fff03SArne Jansen { 1031ab0fff03SArne Jansen struct extent_buffer *buf = NULL; 1032ab0fff03SArne Jansen struct inode *btree_inode = root->fs_info->btree_inode; 1033ab0fff03SArne Jansen struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1034ab0fff03SArne Jansen int ret; 1035ab0fff03SArne Jansen 1036ab0fff03SArne Jansen buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 1037ab0fff03SArne Jansen if (!buf) 1038ab0fff03SArne Jansen return 0; 1039ab0fff03SArne Jansen 1040ab0fff03SArne Jansen set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags); 1041ab0fff03SArne Jansen 1042ab0fff03SArne Jansen ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK, 1043ab0fff03SArne Jansen btree_get_extent, mirror_num); 1044ab0fff03SArne Jansen if (ret) { 1045ab0fff03SArne Jansen free_extent_buffer(buf); 1046ab0fff03SArne Jansen return ret; 1047ab0fff03SArne Jansen } 1048ab0fff03SArne Jansen 1049ab0fff03SArne Jansen if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1050ab0fff03SArne Jansen free_extent_buffer(buf); 1051ab0fff03SArne Jansen return -EIO; 1052ab0fff03SArne Jansen } else if (extent_buffer_uptodate(io_tree, buf, NULL)) { 1053ab0fff03SArne Jansen *eb = buf; 1054ab0fff03SArne Jansen } else { 1055ab0fff03SArne Jansen free_extent_buffer(buf); 1056ab0fff03SArne Jansen } 1057ab0fff03SArne Jansen return 0; 1058ab0fff03SArne Jansen } 1059ab0fff03SArne Jansen 10600999df54SChris Mason struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 10610999df54SChris Mason u64 bytenr, u32 blocksize) 10620999df54SChris Mason { 10630999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10640999df54SChris Mason struct extent_buffer *eb; 10650999df54SChris Mason eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1066f09d1f60SDavid Sterba bytenr, blocksize); 10670999df54SChris Mason return eb; 10680999df54SChris Mason } 10690999df54SChris Mason 10700999df54SChris Mason struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 10710999df54SChris Mason u64 bytenr, u32 blocksize) 10720999df54SChris Mason { 10730999df54SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 10740999df54SChris Mason struct extent_buffer *eb; 10750999df54SChris Mason 10760999df54SChris Mason eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 1077ba144192SDavid Sterba bytenr, blocksize, NULL); 10780999df54SChris Mason return eb; 10790999df54SChris Mason } 10800999df54SChris Mason 10810999df54SChris Mason 1082e02119d5SChris Mason int btrfs_write_tree_block(struct extent_buffer *buf) 1083e02119d5SChris Mason { 10848aa38c31SChristoph Hellwig return filemap_fdatawrite_range(buf->first_page->mapping, buf->start, 10858aa38c31SChristoph Hellwig buf->start + buf->len - 1); 1086e02119d5SChris Mason } 1087e02119d5SChris Mason 1088e02119d5SChris Mason int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 1089e02119d5SChris Mason { 10908aa38c31SChristoph Hellwig return filemap_fdatawait_range(buf->first_page->mapping, 10918aa38c31SChristoph Hellwig buf->start, buf->start + buf->len - 1); 1092e02119d5SChris Mason } 1093e02119d5SChris Mason 1094db94535dSChris Mason struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1095ca7a79adSChris Mason u32 blocksize, u64 parent_transid) 1096e20d96d6SChris Mason { 10975f39d397SChris Mason struct extent_buffer *buf = NULL; 109819c00ddcSChris Mason int ret; 109919c00ddcSChris Mason 1100db94535dSChris Mason buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 11015f39d397SChris Mason if (!buf) 1102d98237b3SChris Mason return NULL; 1103e4204dedSChris Mason 1104ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 1105ce9adaa5SChris Mason 1106d397712bSChris Mason if (ret == 0) 1107b4ce94deSChris Mason set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags); 11085f39d397SChris Mason return buf; 1109ce9adaa5SChris Mason 1110eb60ceacSChris Mason } 1111eb60ceacSChris Mason 1112d5c13f92SJeff Mahoney void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 11135f39d397SChris Mason struct extent_buffer *buf) 1114ed2ff2cbSChris Mason { 11155f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 111655c69072SChris Mason if (btrfs_header_generation(buf) == 1117925baeddSChris Mason root->fs_info->running_transaction->transid) { 1118b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 1119b4ce94deSChris Mason 1120b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) { 1121b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 1122b9473439SChris Mason if (root->fs_info->dirty_metadata_bytes >= buf->len) 1123b9473439SChris Mason root->fs_info->dirty_metadata_bytes -= buf->len; 1124d5c13f92SJeff Mahoney else { 1125d5c13f92SJeff Mahoney spin_unlock(&root->fs_info->delalloc_lock); 1126d5c13f92SJeff Mahoney btrfs_panic(root->fs_info, -EOVERFLOW, 1127d5c13f92SJeff Mahoney "Can't clear %lu bytes from " 1128d5c13f92SJeff Mahoney " dirty_mdatadata_bytes (%lu)", 1129d5c13f92SJeff Mahoney buf->len, 1130d5c13f92SJeff Mahoney root->fs_info->dirty_metadata_bytes); 1131d5c13f92SJeff Mahoney } 1132b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 1133b9473439SChris Mason } 1134b4ce94deSChris Mason 1135b9473439SChris Mason /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1136b9473439SChris Mason btrfs_set_lock_blocking(buf); 1137d1310b2eSChris Mason clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, 113855c69072SChris Mason buf); 1139925baeddSChris Mason } 11405f39d397SChris Mason } 11415f39d397SChris Mason 1142143bede5SJeff Mahoney static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 114387ee04ebSChris Mason u32 stripesize, struct btrfs_root *root, 11449f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 1145e20d96d6SChris Mason u64 objectid) 1146d97e63b6SChris Mason { 1147cfaa7295SChris Mason root->node = NULL; 1148a28ec197SChris Mason root->commit_root = NULL; 1149db94535dSChris Mason root->sectorsize = sectorsize; 1150db94535dSChris Mason root->nodesize = nodesize; 1151db94535dSChris Mason root->leafsize = leafsize; 115287ee04ebSChris Mason root->stripesize = stripesize; 1153123abc88SChris Mason root->ref_cows = 0; 11540b86a832SChris Mason root->track_dirty = 0; 1155c71bf099SYan, Zheng root->in_radix = 0; 1156d68fc57bSYan, Zheng root->orphan_item_inserted = 0; 1157d68fc57bSYan, Zheng root->orphan_cleanup_state = 0; 11580b86a832SChris Mason 11590f7d52f4SChris Mason root->objectid = objectid; 11600f7d52f4SChris Mason root->last_trans = 0; 116113a8a7c8SYan, Zheng root->highest_objectid = 0; 116258176a96SJosef Bacik root->name = NULL; 11636bef4d31SEric Paris root->inode_tree = RB_ROOT; 116416cdcec7SMiao Xie INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); 1165f0486c68SYan, Zheng root->block_rsv = NULL; 1166d68fc57bSYan, Zheng root->orphan_block_rsv = NULL; 11670b86a832SChris Mason 11680b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list); 11697b128766SJosef Bacik INIT_LIST_HEAD(&root->orphan_list); 11705d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list); 1171d68fc57bSYan, Zheng spin_lock_init(&root->orphan_lock); 11725d4f98a2SYan Zheng spin_lock_init(&root->inode_lock); 1173f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock); 1174a2135011SChris Mason mutex_init(&root->objectid_mutex); 1175e02119d5SChris Mason mutex_init(&root->log_mutex); 11767237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait); 11777237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]); 11787237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]); 11797237f183SYan Zheng atomic_set(&root->log_commit[0], 0); 11807237f183SYan Zheng atomic_set(&root->log_commit[1], 0); 11817237f183SYan Zheng atomic_set(&root->log_writers, 0); 11827237f183SYan Zheng root->log_batch = 0; 11837237f183SYan Zheng root->log_transid = 0; 1184257c62e1SChris Mason root->last_log_commit = 0; 1185d0c803c4SChris Mason extent_io_tree_init(&root->dirty_log_pages, 1186f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 1187017e5369SChris Mason 11883768f368SChris Mason memset(&root->root_key, 0, sizeof(root->root_key)); 11893768f368SChris Mason memset(&root->root_item, 0, sizeof(root->root_item)); 11906702ed49SChris Mason memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 119158176a96SJosef Bacik memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 11923f157a2fSChris Mason root->defrag_trans_start = fs_info->generation; 119358176a96SJosef Bacik init_completion(&root->kobj_unregister); 11946702ed49SChris Mason root->defrag_running = 0; 11954d775673SChris Mason root->root_key.objectid = objectid; 11960ee5dc67SAl Viro root->anon_dev = 0; 11973768f368SChris Mason } 11983768f368SChris Mason 1199200a5c17SJeff Mahoney static int __must_check find_and_setup_root(struct btrfs_root *tree_root, 12009f5fae2fSChris Mason struct btrfs_fs_info *fs_info, 12019f5fae2fSChris Mason u64 objectid, 1202e20d96d6SChris Mason struct btrfs_root *root) 12033768f368SChris Mason { 12043768f368SChris Mason int ret; 1205db94535dSChris Mason u32 blocksize; 120684234f3aSYan Zheng u64 generation; 12073768f368SChris Mason 1208db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 120987ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 121087ee04ebSChris Mason root, fs_info, objectid); 12113768f368SChris Mason ret = btrfs_find_last_root(tree_root, objectid, 12123768f368SChris Mason &root->root_item, &root->root_key); 12134df27c4dSYan, Zheng if (ret > 0) 12144df27c4dSYan, Zheng return -ENOENT; 1215200a5c17SJeff Mahoney else if (ret < 0) 1216200a5c17SJeff Mahoney return ret; 12173768f368SChris Mason 121884234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1219db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1220af31f5e5SChris Mason root->commit_root = NULL; 1221db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 122284234f3aSYan Zheng blocksize, generation); 122368433b73SChris Mason if (!root->node || !btrfs_buffer_uptodate(root->node, generation)) { 122468433b73SChris Mason free_extent_buffer(root->node); 1225af31f5e5SChris Mason root->node = NULL; 122668433b73SChris Mason return -EIO; 122768433b73SChris Mason } 12284df27c4dSYan, Zheng root->commit_root = btrfs_root_node(root); 1229d97e63b6SChris Mason return 0; 1230d97e63b6SChris Mason } 1231d97e63b6SChris Mason 1232f84a8bd6SAl Viro static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) 12336f07e42eSAl Viro { 12346f07e42eSAl Viro struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); 12356f07e42eSAl Viro if (root) 12366f07e42eSAl Viro root->fs_info = fs_info; 12376f07e42eSAl Viro return root; 12386f07e42eSAl Viro } 12396f07e42eSAl Viro 12407237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1241e02119d5SChris Mason struct btrfs_fs_info *fs_info) 12420f7d52f4SChris Mason { 12430f7d52f4SChris Mason struct btrfs_root *root; 12440f7d52f4SChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 12457237f183SYan Zheng struct extent_buffer *leaf; 1246e02119d5SChris Mason 12476f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 1248e02119d5SChris Mason if (!root) 12497237f183SYan Zheng return ERR_PTR(-ENOMEM); 1250e02119d5SChris Mason 1251e02119d5SChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 1252e02119d5SChris Mason tree_root->sectorsize, tree_root->stripesize, 1253e02119d5SChris Mason root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1254e02119d5SChris Mason 1255e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; 1256e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1257e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; 12587237f183SYan Zheng /* 12597237f183SYan Zheng * log trees do not get reference counted because they go away 12607237f183SYan Zheng * before a real commit is actually done. They do store pointers 12617237f183SYan Zheng * to file data extents, and those reference counts still get 12627237f183SYan Zheng * updated (along with back refs to the log tree). 12637237f183SYan Zheng */ 1264e02119d5SChris Mason root->ref_cows = 0; 1265e02119d5SChris Mason 12665d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 126766d7e7f0SArne Jansen BTRFS_TREE_LOG_OBJECTID, NULL, 126866d7e7f0SArne Jansen 0, 0, 0, 0); 12697237f183SYan Zheng if (IS_ERR(leaf)) { 12707237f183SYan Zheng kfree(root); 12717237f183SYan Zheng return ERR_CAST(leaf); 12727237f183SYan Zheng } 1273e02119d5SChris Mason 12745d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 12755d4f98a2SYan Zheng btrfs_set_header_bytenr(leaf, leaf->start); 12765d4f98a2SYan Zheng btrfs_set_header_generation(leaf, trans->transid); 12775d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 12785d4f98a2SYan Zheng btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 12797237f183SYan Zheng root->node = leaf; 1280e02119d5SChris Mason 1281e02119d5SChris Mason write_extent_buffer(root->node, root->fs_info->fsid, 1282e02119d5SChris Mason (unsigned long)btrfs_header_fsid(root->node), 1283e02119d5SChris Mason BTRFS_FSID_SIZE); 1284e02119d5SChris Mason btrfs_mark_buffer_dirty(root->node); 1285e02119d5SChris Mason btrfs_tree_unlock(root->node); 12867237f183SYan Zheng return root; 12877237f183SYan Zheng } 12887237f183SYan Zheng 12897237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 12907237f183SYan Zheng struct btrfs_fs_info *fs_info) 12917237f183SYan Zheng { 12927237f183SYan Zheng struct btrfs_root *log_root; 12937237f183SYan Zheng 12947237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info); 12957237f183SYan Zheng if (IS_ERR(log_root)) 12967237f183SYan Zheng return PTR_ERR(log_root); 12977237f183SYan Zheng WARN_ON(fs_info->log_root_tree); 12987237f183SYan Zheng fs_info->log_root_tree = log_root; 12997237f183SYan Zheng return 0; 13007237f183SYan Zheng } 13017237f183SYan Zheng 13027237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 13037237f183SYan Zheng struct btrfs_root *root) 13047237f183SYan Zheng { 13057237f183SYan Zheng struct btrfs_root *log_root; 13067237f183SYan Zheng struct btrfs_inode_item *inode_item; 13077237f183SYan Zheng 13087237f183SYan Zheng log_root = alloc_log_tree(trans, root->fs_info); 13097237f183SYan Zheng if (IS_ERR(log_root)) 13107237f183SYan Zheng return PTR_ERR(log_root); 13117237f183SYan Zheng 13127237f183SYan Zheng log_root->last_trans = trans->transid; 13137237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid; 13147237f183SYan Zheng 13157237f183SYan Zheng inode_item = &log_root->root_item.inode; 13167237f183SYan Zheng inode_item->generation = cpu_to_le64(1); 13177237f183SYan Zheng inode_item->size = cpu_to_le64(3); 13187237f183SYan Zheng inode_item->nlink = cpu_to_le32(1); 13197237f183SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 13207237f183SYan Zheng inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 13217237f183SYan Zheng 13225d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node); 13237237f183SYan Zheng 13247237f183SYan Zheng WARN_ON(root->log_root); 13257237f183SYan Zheng root->log_root = log_root; 13267237f183SYan Zheng root->log_transid = 0; 1327257c62e1SChris Mason root->last_log_commit = 0; 1328e02119d5SChris Mason return 0; 1329e02119d5SChris Mason } 1330e02119d5SChris Mason 1331e02119d5SChris Mason struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, 1332e02119d5SChris Mason struct btrfs_key *location) 1333e02119d5SChris Mason { 1334e02119d5SChris Mason struct btrfs_root *root; 1335e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info; 13360f7d52f4SChris Mason struct btrfs_path *path; 13375f39d397SChris Mason struct extent_buffer *l; 133884234f3aSYan Zheng u64 generation; 1339db94535dSChris Mason u32 blocksize; 13400f7d52f4SChris Mason int ret = 0; 13410f7d52f4SChris Mason 13426f07e42eSAl Viro root = btrfs_alloc_root(fs_info); 13430cf6c620SChris Mason if (!root) 13440f7d52f4SChris Mason return ERR_PTR(-ENOMEM); 13450f7d52f4SChris Mason if (location->offset == (u64)-1) { 1346db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 13470f7d52f4SChris Mason location->objectid, root); 13480f7d52f4SChris Mason if (ret) { 13490f7d52f4SChris Mason kfree(root); 13500f7d52f4SChris Mason return ERR_PTR(ret); 13510f7d52f4SChris Mason } 135213a8a7c8SYan, Zheng goto out; 13530f7d52f4SChris Mason } 13540f7d52f4SChris Mason 1355db94535dSChris Mason __setup_root(tree_root->nodesize, tree_root->leafsize, 135687ee04ebSChris Mason tree_root->sectorsize, tree_root->stripesize, 135787ee04ebSChris Mason root, fs_info, location->objectid); 13580f7d52f4SChris Mason 13590f7d52f4SChris Mason path = btrfs_alloc_path(); 1360db5b493aSTsutomu Itoh if (!path) { 1361db5b493aSTsutomu Itoh kfree(root); 1362db5b493aSTsutomu Itoh return ERR_PTR(-ENOMEM); 1363db5b493aSTsutomu Itoh } 13640f7d52f4SChris Mason ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 136513a8a7c8SYan, Zheng if (ret == 0) { 13665f39d397SChris Mason l = path->nodes[0]; 13675f39d397SChris Mason read_extent_buffer(l, &root->root_item, 13685f39d397SChris Mason btrfs_item_ptr_offset(l, path->slots[0]), 13690f7d52f4SChris Mason sizeof(root->root_item)); 137044b36eb2SYan Zheng memcpy(&root->root_key, location, sizeof(*location)); 137113a8a7c8SYan, Zheng } 13720f7d52f4SChris Mason btrfs_free_path(path); 13730f7d52f4SChris Mason if (ret) { 13745e540f77STsutomu Itoh kfree(root); 137513a8a7c8SYan, Zheng if (ret > 0) 137613a8a7c8SYan, Zheng ret = -ENOENT; 13770f7d52f4SChris Mason return ERR_PTR(ret); 13780f7d52f4SChris Mason } 137913a8a7c8SYan, Zheng 138084234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item); 1381db94535dSChris Mason blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1382db94535dSChris Mason root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 138384234f3aSYan Zheng blocksize, generation); 13845d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root); 13850f7d52f4SChris Mason BUG_ON(!root->node); 138613a8a7c8SYan, Zheng out: 138708fe4db1SLi Zefan if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 13880f7d52f4SChris Mason root->ref_cows = 1; 138908fe4db1SLi Zefan btrfs_check_and_init_root_item(&root->root_item); 139008fe4db1SLi Zefan } 139113a8a7c8SYan, Zheng 13925eda7b5eSChris Mason return root; 13935eda7b5eSChris Mason } 13945eda7b5eSChris Mason 1395edbd8d4eSChris Mason struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 1396edbd8d4eSChris Mason struct btrfs_key *location) 13975eda7b5eSChris Mason { 13985eda7b5eSChris Mason struct btrfs_root *root; 13995eda7b5eSChris Mason int ret; 14005eda7b5eSChris Mason 1401edbd8d4eSChris Mason if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 1402edbd8d4eSChris Mason return fs_info->tree_root; 1403edbd8d4eSChris Mason if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 1404edbd8d4eSChris Mason return fs_info->extent_root; 14058f18cf13SChris Mason if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 14068f18cf13SChris Mason return fs_info->chunk_root; 14078f18cf13SChris Mason if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 14088f18cf13SChris Mason return fs_info->dev_root; 14090403e47eSYan Zheng if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 14100403e47eSYan Zheng return fs_info->csum_root; 14114df27c4dSYan, Zheng again: 14124df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 14135eda7b5eSChris Mason root = radix_tree_lookup(&fs_info->fs_roots_radix, 14145eda7b5eSChris Mason (unsigned long)location->objectid); 14154df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 14165eda7b5eSChris Mason if (root) 14175eda7b5eSChris Mason return root; 14185eda7b5eSChris Mason 1419e02119d5SChris Mason root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 14205eda7b5eSChris Mason if (IS_ERR(root)) 14215eda7b5eSChris Mason return root; 14223394e160SChris Mason 1423581bb050SLi Zefan root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); 1424581bb050SLi Zefan root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), 1425581bb050SLi Zefan GFP_NOFS); 142635a30d7cSDavid Sterba if (!root->free_ino_pinned || !root->free_ino_ctl) { 142735a30d7cSDavid Sterba ret = -ENOMEM; 1428581bb050SLi Zefan goto fail; 142935a30d7cSDavid Sterba } 1430581bb050SLi Zefan 1431581bb050SLi Zefan btrfs_init_free_ino_ctl(root); 1432581bb050SLi Zefan mutex_init(&root->fs_commit_mutex); 1433581bb050SLi Zefan spin_lock_init(&root->cache_lock); 1434581bb050SLi Zefan init_waitqueue_head(&root->cache_wait); 1435581bb050SLi Zefan 14360ee5dc67SAl Viro ret = get_anon_bdev(&root->anon_dev); 1437ac08aedfSChris Mason if (ret) 1438ac08aedfSChris Mason goto fail; 14393394e160SChris Mason 1440d68fc57bSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) { 1441d68fc57bSYan, Zheng ret = -ENOENT; 1442d68fc57bSYan, Zheng goto fail; 1443d68fc57bSYan, Zheng } 1444d68fc57bSYan, Zheng 1445d68fc57bSYan, Zheng ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); 1446d68fc57bSYan, Zheng if (ret < 0) 1447d68fc57bSYan, Zheng goto fail; 1448d68fc57bSYan, Zheng if (ret == 0) 1449d68fc57bSYan, Zheng root->orphan_item_inserted = 1; 1450d68fc57bSYan, Zheng 14514df27c4dSYan, Zheng ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 14524df27c4dSYan, Zheng if (ret) 14534df27c4dSYan, Zheng goto fail; 14544df27c4dSYan, Zheng 14554df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 14562619ba1fSChris Mason ret = radix_tree_insert(&fs_info->fs_roots_radix, 14572619ba1fSChris Mason (unsigned long)root->root_key.objectid, 14580f7d52f4SChris Mason root); 1459d68fc57bSYan, Zheng if (ret == 0) 14604df27c4dSYan, Zheng root->in_radix = 1; 1461d68fc57bSYan, Zheng 14624df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 14634df27c4dSYan, Zheng radix_tree_preload_end(); 14640f7d52f4SChris Mason if (ret) { 14654df27c4dSYan, Zheng if (ret == -EEXIST) { 14664df27c4dSYan, Zheng free_fs_root(root); 14674df27c4dSYan, Zheng goto again; 14680f7d52f4SChris Mason } 14694df27c4dSYan, Zheng goto fail; 14704df27c4dSYan, Zheng } 14714df27c4dSYan, Zheng 1472edbd8d4eSChris Mason ret = btrfs_find_dead_roots(fs_info->tree_root, 14735d4f98a2SYan Zheng root->root_key.objectid); 14744df27c4dSYan, Zheng WARN_ON(ret); 1475edbd8d4eSChris Mason return root; 14764df27c4dSYan, Zheng fail: 14774df27c4dSYan, Zheng free_fs_root(root); 14784df27c4dSYan, Zheng return ERR_PTR(ret); 1479edbd8d4eSChris Mason } 1480edbd8d4eSChris Mason 148104160088SChris Mason static int btrfs_congested_fn(void *congested_data, int bdi_bits) 148204160088SChris Mason { 148304160088SChris Mason struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 148404160088SChris Mason int ret = 0; 148504160088SChris Mason struct btrfs_device *device; 148604160088SChris Mason struct backing_dev_info *bdi; 1487b7967db7SChris Mason 14881f78160cSXiao Guangrong rcu_read_lock(); 14891f78160cSXiao Guangrong list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) { 1490dfe25020SChris Mason if (!device->bdev) 1491dfe25020SChris Mason continue; 149204160088SChris Mason bdi = blk_get_backing_dev_info(device->bdev); 149304160088SChris Mason if (bdi && bdi_congested(bdi, bdi_bits)) { 149404160088SChris Mason ret = 1; 149504160088SChris Mason break; 149604160088SChris Mason } 149704160088SChris Mason } 14981f78160cSXiao Guangrong rcu_read_unlock(); 149904160088SChris Mason return ret; 150004160088SChris Mason } 150104160088SChris Mason 150238b66988SChris Mason /* 1503ad081f14SJens Axboe * If this fails, caller must call bdi_destroy() to get rid of the 1504ad081f14SJens Axboe * bdi again. 1505ad081f14SJens Axboe */ 150604160088SChris Mason static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 150704160088SChris Mason { 1508ad081f14SJens Axboe int err; 1509ad081f14SJens Axboe 1510ad081f14SJens Axboe bdi->capabilities = BDI_CAP_MAP_COPY; 1511e6d086d8SJens Axboe err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY); 1512ad081f14SJens Axboe if (err) 1513ad081f14SJens Axboe return err; 1514ad081f14SJens Axboe 15154575c9ccSChris Mason bdi->ra_pages = default_backing_dev_info.ra_pages; 151604160088SChris Mason bdi->congested_fn = btrfs_congested_fn; 151704160088SChris Mason bdi->congested_data = info; 151804160088SChris Mason return 0; 151904160088SChris Mason } 152004160088SChris Mason 1521ce9adaa5SChris Mason static int bio_ready_for_csum(struct bio *bio) 1522ce9adaa5SChris Mason { 1523ce9adaa5SChris Mason u64 length = 0; 1524ce9adaa5SChris Mason u64 buf_len = 0; 1525ce9adaa5SChris Mason u64 start = 0; 1526ce9adaa5SChris Mason struct page *page; 1527ce9adaa5SChris Mason struct extent_io_tree *io_tree = NULL; 1528ce9adaa5SChris Mason struct bio_vec *bvec; 1529ce9adaa5SChris Mason int i; 1530ce9adaa5SChris Mason int ret; 1531ce9adaa5SChris Mason 1532ce9adaa5SChris Mason bio_for_each_segment(bvec, bio, i) { 1533ce9adaa5SChris Mason page = bvec->bv_page; 1534ce9adaa5SChris Mason if (page->private == EXTENT_PAGE_PRIVATE) { 1535ce9adaa5SChris Mason length += bvec->bv_len; 1536ce9adaa5SChris Mason continue; 1537ce9adaa5SChris Mason } 1538ce9adaa5SChris Mason if (!page->private) { 1539ce9adaa5SChris Mason length += bvec->bv_len; 1540ce9adaa5SChris Mason continue; 1541ce9adaa5SChris Mason } 1542ce9adaa5SChris Mason length = bvec->bv_len; 1543ce9adaa5SChris Mason buf_len = page->private >> 2; 1544ce9adaa5SChris Mason start = page_offset(page) + bvec->bv_offset; 1545ce9adaa5SChris Mason io_tree = &BTRFS_I(page->mapping->host)->io_tree; 1546ce9adaa5SChris Mason } 1547ce9adaa5SChris Mason /* are we fully contained in this bio? */ 1548ce9adaa5SChris Mason if (buf_len <= length) 1549ce9adaa5SChris Mason return 1; 1550ce9adaa5SChris Mason 1551ce9adaa5SChris Mason ret = extent_range_uptodate(io_tree, start + length, 1552ce9adaa5SChris Mason start + buf_len - 1); 1553ce9adaa5SChris Mason return ret; 1554ce9adaa5SChris Mason } 1555ce9adaa5SChris Mason 15568b712842SChris Mason /* 15578b712842SChris Mason * called by the kthread helper functions to finally call the bio end_io 15588b712842SChris Mason * functions. This is where read checksum verification actually happens 15598b712842SChris Mason */ 15608b712842SChris Mason static void end_workqueue_fn(struct btrfs_work *work) 1561ce9adaa5SChris Mason { 1562ce9adaa5SChris Mason struct bio *bio; 15638b712842SChris Mason struct end_io_wq *end_io_wq; 15648b712842SChris Mason struct btrfs_fs_info *fs_info; 1565ce9adaa5SChris Mason int error; 1566ce9adaa5SChris Mason 15678b712842SChris Mason end_io_wq = container_of(work, struct end_io_wq, work); 1568ce9adaa5SChris Mason bio = end_io_wq->bio; 15698b712842SChris Mason fs_info = end_io_wq->info; 15708b712842SChris Mason 1571cad321adSChris Mason /* metadata bio reads are special because the whole tree block must 15728b712842SChris Mason * be checksummed at once. This makes sure the entire block is in 15738b712842SChris Mason * ram and up to date before trying to verify things. For 15748b712842SChris Mason * blocksize <= pagesize, it is basically a noop 15758b712842SChris Mason */ 15767b6d91daSChristoph Hellwig if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata && 1577cad321adSChris Mason !bio_ready_for_csum(bio)) { 1578d20f7043SChris Mason btrfs_queue_worker(&fs_info->endio_meta_workers, 15798b712842SChris Mason &end_io_wq->work); 1580ce9adaa5SChris Mason return; 1581ce9adaa5SChris Mason } 1582ce9adaa5SChris Mason error = end_io_wq->error; 1583ce9adaa5SChris Mason bio->bi_private = end_io_wq->private; 1584ce9adaa5SChris Mason bio->bi_end_io = end_io_wq->end_io; 1585ce9adaa5SChris Mason kfree(end_io_wq); 1586ce9adaa5SChris Mason bio_endio(bio, error); 1587ce9adaa5SChris Mason } 158844b8bd7eSChris Mason 1589a74a4b97SChris Mason static int cleaner_kthread(void *arg) 1590a74a4b97SChris Mason { 1591a74a4b97SChris Mason struct btrfs_root *root = arg; 1592a74a4b97SChris Mason 1593a74a4b97SChris Mason do { 1594a74a4b97SChris Mason vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 159576dda93cSYan, Zheng 159676dda93cSYan, Zheng if (!(root->fs_info->sb->s_flags & MS_RDONLY) && 159776dda93cSYan, Zheng mutex_trylock(&root->fs_info->cleaner_mutex)) { 159824bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 1599a74a4b97SChris Mason btrfs_clean_old_snapshots(root); 1600a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 16014cb5300bSChris Mason btrfs_run_defrag_inodes(root->fs_info); 160276dda93cSYan, Zheng } 1603a74a4b97SChris Mason 1604a0acae0eSTejun Heo if (!try_to_freeze()) { 1605a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16068929ecfaSYan, Zheng if (!kthread_should_stop()) 1607a74a4b97SChris Mason schedule(); 1608a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1609a74a4b97SChris Mason } 1610a74a4b97SChris Mason } while (!kthread_should_stop()); 1611a74a4b97SChris Mason return 0; 1612a74a4b97SChris Mason } 1613a74a4b97SChris Mason 1614a74a4b97SChris Mason static int transaction_kthread(void *arg) 1615a74a4b97SChris Mason { 1616a74a4b97SChris Mason struct btrfs_root *root = arg; 1617a74a4b97SChris Mason struct btrfs_trans_handle *trans; 1618a74a4b97SChris Mason struct btrfs_transaction *cur; 16198929ecfaSYan, Zheng u64 transid; 1620a74a4b97SChris Mason unsigned long now; 1621a74a4b97SChris Mason unsigned long delay; 1622a74a4b97SChris Mason int ret; 1623a74a4b97SChris Mason 1624a74a4b97SChris Mason do { 1625a74a4b97SChris Mason delay = HZ * 30; 1626a74a4b97SChris Mason vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1627a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1628a74a4b97SChris Mason 1629a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1630a74a4b97SChris Mason cur = root->fs_info->running_transaction; 1631a74a4b97SChris Mason if (!cur) { 1632a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1633a74a4b97SChris Mason goto sleep; 1634a74a4b97SChris Mason } 163531153d81SYan Zheng 1636a74a4b97SChris Mason now = get_seconds(); 16378929ecfaSYan, Zheng if (!cur->blocked && 16388929ecfaSYan, Zheng (now < cur->start_time || now - cur->start_time < 30)) { 1639a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1640a74a4b97SChris Mason delay = HZ * 5; 1641a74a4b97SChris Mason goto sleep; 1642a74a4b97SChris Mason } 16438929ecfaSYan, Zheng transid = cur->transid; 1644a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 164556bec294SChris Mason 16467a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 16473612b495STsutomu Itoh BUG_ON(IS_ERR(trans)); 16488929ecfaSYan, Zheng if (transid == trans->transid) { 16498929ecfaSYan, Zheng ret = btrfs_commit_transaction(trans, root); 16508929ecfaSYan, Zheng BUG_ON(ret); 16518929ecfaSYan, Zheng } else { 16528929ecfaSYan, Zheng btrfs_end_transaction(trans, root); 16538929ecfaSYan, Zheng } 1654a74a4b97SChris Mason sleep: 1655a74a4b97SChris Mason wake_up_process(root->fs_info->cleaner_kthread); 1656a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1657a74a4b97SChris Mason 1658a0acae0eSTejun Heo if (!try_to_freeze()) { 1659a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE); 16608929ecfaSYan, Zheng if (!kthread_should_stop() && 16618929ecfaSYan, Zheng !btrfs_transaction_blocked(root->fs_info)) 1662a74a4b97SChris Mason schedule_timeout(delay); 1663a74a4b97SChris Mason __set_current_state(TASK_RUNNING); 1664a74a4b97SChris Mason } 1665a74a4b97SChris Mason } while (!kthread_should_stop()); 1666a74a4b97SChris Mason return 0; 1667a74a4b97SChris Mason } 1668a74a4b97SChris Mason 1669af31f5e5SChris Mason /* 1670af31f5e5SChris Mason * this will find the highest generation in the array of 1671af31f5e5SChris Mason * root backups. The index of the highest array is returned, 1672af31f5e5SChris Mason * or -1 if we can't find anything. 1673af31f5e5SChris Mason * 1674af31f5e5SChris Mason * We check to make sure the array is valid by comparing the 1675af31f5e5SChris Mason * generation of the latest root in the array with the generation 1676af31f5e5SChris Mason * in the super block. If they don't match we pitch it. 1677af31f5e5SChris Mason */ 1678af31f5e5SChris Mason static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen) 1679af31f5e5SChris Mason { 1680af31f5e5SChris Mason u64 cur; 1681af31f5e5SChris Mason int newest_index = -1; 1682af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1683af31f5e5SChris Mason int i; 1684af31f5e5SChris Mason 1685af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) { 1686af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i; 1687af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1688af31f5e5SChris Mason if (cur == newest_gen) 1689af31f5e5SChris Mason newest_index = i; 1690af31f5e5SChris Mason } 1691af31f5e5SChris Mason 1692af31f5e5SChris Mason /* check to see if we actually wrapped around */ 1693af31f5e5SChris Mason if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) { 1694af31f5e5SChris Mason root_backup = info->super_copy->super_roots; 1695af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup); 1696af31f5e5SChris Mason if (cur == newest_gen) 1697af31f5e5SChris Mason newest_index = 0; 1698af31f5e5SChris Mason } 1699af31f5e5SChris Mason return newest_index; 1700af31f5e5SChris Mason } 1701af31f5e5SChris Mason 1702af31f5e5SChris Mason 1703af31f5e5SChris Mason /* 1704af31f5e5SChris Mason * find the oldest backup so we know where to store new entries 1705af31f5e5SChris Mason * in the backup array. This will set the backup_root_index 1706af31f5e5SChris Mason * field in the fs_info struct 1707af31f5e5SChris Mason */ 1708af31f5e5SChris Mason static void find_oldest_super_backup(struct btrfs_fs_info *info, 1709af31f5e5SChris Mason u64 newest_gen) 1710af31f5e5SChris Mason { 1711af31f5e5SChris Mason int newest_index = -1; 1712af31f5e5SChris Mason 1713af31f5e5SChris Mason newest_index = find_newest_super_backup(info, newest_gen); 1714af31f5e5SChris Mason /* if there was garbage in there, just move along */ 1715af31f5e5SChris Mason if (newest_index == -1) { 1716af31f5e5SChris Mason info->backup_root_index = 0; 1717af31f5e5SChris Mason } else { 1718af31f5e5SChris Mason info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS; 1719af31f5e5SChris Mason } 1720af31f5e5SChris Mason } 1721af31f5e5SChris Mason 1722af31f5e5SChris Mason /* 1723af31f5e5SChris Mason * copy all the root pointers into the super backup array. 1724af31f5e5SChris Mason * this will bump the backup pointer by one when it is 1725af31f5e5SChris Mason * done 1726af31f5e5SChris Mason */ 1727af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info) 1728af31f5e5SChris Mason { 1729af31f5e5SChris Mason int next_backup; 1730af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1731af31f5e5SChris Mason int last_backup; 1732af31f5e5SChris Mason 1733af31f5e5SChris Mason next_backup = info->backup_root_index; 1734af31f5e5SChris Mason last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) % 1735af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1736af31f5e5SChris Mason 1737af31f5e5SChris Mason /* 1738af31f5e5SChris Mason * just overwrite the last backup if we're at the same generation 1739af31f5e5SChris Mason * this happens only at umount 1740af31f5e5SChris Mason */ 1741af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + last_backup; 1742af31f5e5SChris Mason if (btrfs_backup_tree_root_gen(root_backup) == 1743af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)) 1744af31f5e5SChris Mason next_backup = last_backup; 1745af31f5e5SChris Mason 1746af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup; 1747af31f5e5SChris Mason 1748af31f5e5SChris Mason /* 1749af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled 1750af31f5e5SChris Mason * regardless of which ones we use today 1751af31f5e5SChris Mason */ 1752af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup)); 1753af31f5e5SChris Mason 1754af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS; 1755af31f5e5SChris Mason 1756af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start); 1757af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup, 1758af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node)); 1759af31f5e5SChris Mason 1760af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup, 1761af31f5e5SChris Mason btrfs_header_level(info->tree_root->node)); 1762af31f5e5SChris Mason 1763af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start); 1764af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup, 1765af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node)); 1766af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup, 1767af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node)); 1768af31f5e5SChris Mason 1769af31f5e5SChris Mason btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start); 1770af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup, 1771af31f5e5SChris Mason btrfs_header_generation(info->extent_root->node)); 1772af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup, 1773af31f5e5SChris Mason btrfs_header_level(info->extent_root->node)); 1774af31f5e5SChris Mason 17757c7e82a7SChris Mason /* 17767c7e82a7SChris Mason * we might commit during log recovery, which happens before we set 17777c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in. 17787c7e82a7SChris Mason */ 17797c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) { 17807c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup, 17817c7e82a7SChris Mason info->fs_root->node->start); 1782af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup, 1783af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node)); 1784af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup, 1785af31f5e5SChris Mason btrfs_header_level(info->fs_root->node)); 17867c7e82a7SChris Mason } 1787af31f5e5SChris Mason 1788af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start); 1789af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup, 1790af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node)); 1791af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup, 1792af31f5e5SChris Mason btrfs_header_level(info->dev_root->node)); 1793af31f5e5SChris Mason 1794af31f5e5SChris Mason btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start); 1795af31f5e5SChris Mason btrfs_set_backup_csum_root_gen(root_backup, 1796af31f5e5SChris Mason btrfs_header_generation(info->csum_root->node)); 1797af31f5e5SChris Mason btrfs_set_backup_csum_root_level(root_backup, 1798af31f5e5SChris Mason btrfs_header_level(info->csum_root->node)); 1799af31f5e5SChris Mason 1800af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup, 1801af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy)); 1802af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup, 1803af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy)); 1804af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup, 1805af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy)); 1806af31f5e5SChris Mason 1807af31f5e5SChris Mason /* 1808af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered 1809af31f5e5SChris Mason * for the next commit 1810af31f5e5SChris Mason */ 1811af31f5e5SChris Mason memcpy(&info->super_copy->super_roots, 1812af31f5e5SChris Mason &info->super_for_commit->super_roots, 1813af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS); 1814af31f5e5SChris Mason } 1815af31f5e5SChris Mason 1816af31f5e5SChris Mason /* 1817af31f5e5SChris Mason * this copies info out of the root backup array and back into 1818af31f5e5SChris Mason * the in-memory super block. It is meant to help iterate through 1819af31f5e5SChris Mason * the array, so you send it the number of backups you've already 1820af31f5e5SChris Mason * tried and the last backup index you used. 1821af31f5e5SChris Mason * 1822af31f5e5SChris Mason * this returns -1 when it has tried all the backups 1823af31f5e5SChris Mason */ 1824af31f5e5SChris Mason static noinline int next_root_backup(struct btrfs_fs_info *info, 1825af31f5e5SChris Mason struct btrfs_super_block *super, 1826af31f5e5SChris Mason int *num_backups_tried, int *backup_index) 1827af31f5e5SChris Mason { 1828af31f5e5SChris Mason struct btrfs_root_backup *root_backup; 1829af31f5e5SChris Mason int newest = *backup_index; 1830af31f5e5SChris Mason 1831af31f5e5SChris Mason if (*num_backups_tried == 0) { 1832af31f5e5SChris Mason u64 gen = btrfs_super_generation(super); 1833af31f5e5SChris Mason 1834af31f5e5SChris Mason newest = find_newest_super_backup(info, gen); 1835af31f5e5SChris Mason if (newest == -1) 1836af31f5e5SChris Mason return -1; 1837af31f5e5SChris Mason 1838af31f5e5SChris Mason *backup_index = newest; 1839af31f5e5SChris Mason *num_backups_tried = 1; 1840af31f5e5SChris Mason } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) { 1841af31f5e5SChris Mason /* we've tried all the backups, all done */ 1842af31f5e5SChris Mason return -1; 1843af31f5e5SChris Mason } else { 1844af31f5e5SChris Mason /* jump to the next oldest backup */ 1845af31f5e5SChris Mason newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) % 1846af31f5e5SChris Mason BTRFS_NUM_BACKUP_ROOTS; 1847af31f5e5SChris Mason *backup_index = newest; 1848af31f5e5SChris Mason *num_backups_tried += 1; 1849af31f5e5SChris Mason } 1850af31f5e5SChris Mason root_backup = super->super_roots + newest; 1851af31f5e5SChris Mason 1852af31f5e5SChris Mason btrfs_set_super_generation(super, 1853af31f5e5SChris Mason btrfs_backup_tree_root_gen(root_backup)); 1854af31f5e5SChris Mason btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup)); 1855af31f5e5SChris Mason btrfs_set_super_root_level(super, 1856af31f5e5SChris Mason btrfs_backup_tree_root_level(root_backup)); 1857af31f5e5SChris Mason btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup)); 1858af31f5e5SChris Mason 1859af31f5e5SChris Mason /* 1860af31f5e5SChris Mason * fixme: the total bytes and num_devices need to match or we should 1861af31f5e5SChris Mason * need a fsck 1862af31f5e5SChris Mason */ 1863af31f5e5SChris Mason btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup)); 1864af31f5e5SChris Mason btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup)); 1865af31f5e5SChris Mason return 0; 1866af31f5e5SChris Mason } 1867af31f5e5SChris Mason 1868af31f5e5SChris Mason /* helper to cleanup tree roots */ 1869af31f5e5SChris Mason static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root) 1870af31f5e5SChris Mason { 1871af31f5e5SChris Mason free_extent_buffer(info->tree_root->node); 1872af31f5e5SChris Mason free_extent_buffer(info->tree_root->commit_root); 1873af31f5e5SChris Mason free_extent_buffer(info->dev_root->node); 1874af31f5e5SChris Mason free_extent_buffer(info->dev_root->commit_root); 1875af31f5e5SChris Mason free_extent_buffer(info->extent_root->node); 1876af31f5e5SChris Mason free_extent_buffer(info->extent_root->commit_root); 1877af31f5e5SChris Mason free_extent_buffer(info->csum_root->node); 1878af31f5e5SChris Mason free_extent_buffer(info->csum_root->commit_root); 1879af31f5e5SChris Mason 1880af31f5e5SChris Mason info->tree_root->node = NULL; 1881af31f5e5SChris Mason info->tree_root->commit_root = NULL; 1882af31f5e5SChris Mason info->dev_root->node = NULL; 1883af31f5e5SChris Mason info->dev_root->commit_root = NULL; 1884af31f5e5SChris Mason info->extent_root->node = NULL; 1885af31f5e5SChris Mason info->extent_root->commit_root = NULL; 1886af31f5e5SChris Mason info->csum_root->node = NULL; 1887af31f5e5SChris Mason info->csum_root->commit_root = NULL; 1888af31f5e5SChris Mason 1889af31f5e5SChris Mason if (chunk_root) { 1890af31f5e5SChris Mason free_extent_buffer(info->chunk_root->node); 1891af31f5e5SChris Mason free_extent_buffer(info->chunk_root->commit_root); 1892af31f5e5SChris Mason info->chunk_root->node = NULL; 1893af31f5e5SChris Mason info->chunk_root->commit_root = NULL; 1894af31f5e5SChris Mason } 1895af31f5e5SChris Mason } 1896af31f5e5SChris Mason 1897af31f5e5SChris Mason 1898ad2b2c80SAl Viro int open_ctree(struct super_block *sb, 1899dfe25020SChris Mason struct btrfs_fs_devices *fs_devices, 1900dfe25020SChris Mason char *options) 1901eb60ceacSChris Mason { 1902db94535dSChris Mason u32 sectorsize; 1903db94535dSChris Mason u32 nodesize; 1904db94535dSChris Mason u32 leafsize; 1905db94535dSChris Mason u32 blocksize; 190687ee04ebSChris Mason u32 stripesize; 190784234f3aSYan Zheng u64 generation; 1908f2b636e8SJosef Bacik u64 features; 19093de4586cSChris Mason struct btrfs_key location; 1910a061fc8dSChris Mason struct buffer_head *bh; 19114d34b278SIlya Dryomov struct btrfs_super_block *disk_super; 1912815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1913f84a8bd6SAl Viro struct btrfs_root *tree_root; 19144d34b278SIlya Dryomov struct btrfs_root *extent_root; 19154d34b278SIlya Dryomov struct btrfs_root *csum_root; 19164d34b278SIlya Dryomov struct btrfs_root *chunk_root; 19174d34b278SIlya Dryomov struct btrfs_root *dev_root; 1918e02119d5SChris Mason struct btrfs_root *log_tree_root; 1919eb60ceacSChris Mason int ret; 1920e58ca020SYan int err = -EINVAL; 1921af31f5e5SChris Mason int num_backups_tried = 0; 1922af31f5e5SChris Mason int backup_index = 0; 19234543df7eSChris Mason 1924f84a8bd6SAl Viro tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); 19256f07e42eSAl Viro extent_root = fs_info->extent_root = btrfs_alloc_root(fs_info); 19266f07e42eSAl Viro csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); 19276f07e42eSAl Viro chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 19286f07e42eSAl Viro dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); 19298790d502SChris Mason 1930f84a8bd6SAl Viro if (!tree_root || !extent_root || !csum_root || 1931f84a8bd6SAl Viro !chunk_root || !dev_root) { 193239279cc3SChris Mason err = -ENOMEM; 193339279cc3SChris Mason goto fail; 193439279cc3SChris Mason } 193576dda93cSYan, Zheng 193676dda93cSYan, Zheng ret = init_srcu_struct(&fs_info->subvol_srcu); 193776dda93cSYan, Zheng if (ret) { 193876dda93cSYan, Zheng err = ret; 193976dda93cSYan, Zheng goto fail; 194076dda93cSYan, Zheng } 194176dda93cSYan, Zheng 194276dda93cSYan, Zheng ret = setup_bdi(fs_info, &fs_info->bdi); 194376dda93cSYan, Zheng if (ret) { 194476dda93cSYan, Zheng err = ret; 194576dda93cSYan, Zheng goto fail_srcu; 194676dda93cSYan, Zheng } 194776dda93cSYan, Zheng 194876dda93cSYan, Zheng fs_info->btree_inode = new_inode(sb); 194976dda93cSYan, Zheng if (!fs_info->btree_inode) { 195076dda93cSYan, Zheng err = -ENOMEM; 195176dda93cSYan, Zheng goto fail_bdi; 195276dda93cSYan, Zheng } 195376dda93cSYan, Zheng 1954a6591715SChris Mason mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 19551561dedaSMiao Xie 195676dda93cSYan, Zheng INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 19578fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list); 1958facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots); 195924bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs); 196019c00ddcSChris Mason INIT_LIST_HEAD(&fs_info->hashers); 1961ea8c2819SChris Mason INIT_LIST_HEAD(&fs_info->delalloc_inodes); 19625a3f23d5SChris Mason INIT_LIST_HEAD(&fs_info->ordered_operations); 196311833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups); 19641832a6d5SChris Mason spin_lock_init(&fs_info->delalloc_lock); 1965a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock); 196631153d81SYan Zheng spin_lock_init(&fs_info->ref_cache_lock); 196776dda93cSYan, Zheng spin_lock_init(&fs_info->fs_roots_radix_lock); 196824bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock); 19694cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock); 19702bf64758SJosef Bacik spin_lock_init(&fs_info->free_chunk_lock); 19717585717fSChris Mason mutex_init(&fs_info->reloc_mutex); 197219c00ddcSChris Mason 197358176a96SJosef Bacik init_completion(&fs_info->kobj_unregister); 19740b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 19756324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info); 19760b86a832SChris Mason btrfs_mapping_init(&fs_info->mapping_tree); 1977f0486c68SYan, Zheng btrfs_init_block_rsv(&fs_info->global_block_rsv); 1978f0486c68SYan, Zheng btrfs_init_block_rsv(&fs_info->delalloc_block_rsv); 1979f0486c68SYan, Zheng btrfs_init_block_rsv(&fs_info->trans_block_rsv); 1980f0486c68SYan, Zheng btrfs_init_block_rsv(&fs_info->chunk_block_rsv); 1981f0486c68SYan, Zheng btrfs_init_block_rsv(&fs_info->empty_block_rsv); 19826d668ddaSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_block_rsv); 1983cb03c743SChris Mason atomic_set(&fs_info->nr_async_submits, 0); 1984771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0); 19858c8bee1dSChris Mason atomic_set(&fs_info->async_submit_draining, 0); 19860986fe9eSChris Mason atomic_set(&fs_info->nr_async_bios, 0); 19874cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0); 1988e20d96d6SChris Mason fs_info->sb = sb; 19896f568d35SChris Mason fs_info->max_inline = 8192 * 1024; 19909ed74f2dSJosef Bacik fs_info->metadata_ratio = 0; 19914cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT; 1992a4abeea4SJosef Bacik fs_info->trans_no_join = 0; 19932bf64758SJosef Bacik fs_info->free_chunk_space = 0; 1994c8b97818SChris Mason 199590519d66SArne Jansen /* readahead state */ 199690519d66SArne Jansen INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 199790519d66SArne Jansen spin_lock_init(&fs_info->reada_lock); 19982c90e5d6SChris Mason 1999b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long, 2000b34b086cSChris Mason num_online_cpus() + 2, 8); 20010afbaf8cSChris Mason 20023eaa2885SChris Mason INIT_LIST_HEAD(&fs_info->ordered_extents); 20033eaa2885SChris Mason spin_lock_init(&fs_info->ordered_extent_lock); 200416cdcec7SMiao Xie fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), 200516cdcec7SMiao Xie GFP_NOFS); 200616cdcec7SMiao Xie if (!fs_info->delayed_root) { 200716cdcec7SMiao Xie err = -ENOMEM; 200816cdcec7SMiao Xie goto fail_iput; 200916cdcec7SMiao Xie } 201016cdcec7SMiao Xie btrfs_init_delayed_root(fs_info->delayed_root); 20113eaa2885SChris Mason 2012a2de733cSArne Jansen mutex_init(&fs_info->scrub_lock); 2013a2de733cSArne Jansen atomic_set(&fs_info->scrubs_running, 0); 2014a2de733cSArne Jansen atomic_set(&fs_info->scrub_pause_req, 0); 2015a2de733cSArne Jansen atomic_set(&fs_info->scrubs_paused, 0); 2016a2de733cSArne Jansen atomic_set(&fs_info->scrub_cancel_req, 0); 2017a2de733cSArne Jansen init_waitqueue_head(&fs_info->scrub_pause_wait); 2018a2de733cSArne Jansen init_rwsem(&fs_info->scrub_super_lock); 2019a2de733cSArne Jansen fs_info->scrub_workers_refcnt = 0; 202021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 202121adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0; 202221adbd5cSStefan Behrens #endif 2023a2de733cSArne Jansen 2024c9e9f97bSIlya Dryomov spin_lock_init(&fs_info->balance_lock); 2025c9e9f97bSIlya Dryomov mutex_init(&fs_info->balance_mutex); 2026837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_running, 0); 2027837d5b6eSIlya Dryomov atomic_set(&fs_info->balance_pause_req, 0); 2028a7e99c69SIlya Dryomov atomic_set(&fs_info->balance_cancel_req, 0); 2029c9e9f97bSIlya Dryomov fs_info->balance_ctl = NULL; 2030837d5b6eSIlya Dryomov init_waitqueue_head(&fs_info->balance_wait_q); 2031a061fc8dSChris Mason 20320afbaf8cSChris Mason sb->s_blocksize = 4096; 20330afbaf8cSChris Mason sb->s_blocksize_bits = blksize_bits(4096); 203432a88aa1SJens Axboe sb->s_bdi = &fs_info->bdi; 20350afbaf8cSChris Mason 203676dda93cSYan, Zheng fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2037bfe86848SMiklos Szeredi set_nlink(fs_info->btree_inode, 1); 20380afbaf8cSChris Mason /* 20390afbaf8cSChris Mason * we set the i_size on the btree inode to the max possible int. 20400afbaf8cSChris Mason * the real end of the address space is determined by all of 20410afbaf8cSChris Mason * the devices in the system 20420afbaf8cSChris Mason */ 20430afbaf8cSChris Mason fs_info->btree_inode->i_size = OFFSET_MAX; 2044d98237b3SChris Mason fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 204504160088SChris Mason fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 204604160088SChris Mason 20475d4f98a2SYan Zheng RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2048d1310b2eSChris Mason extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2049f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 2050a8067e02SDavid Sterba extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 20510da5468fSChris Mason 2052d1310b2eSChris Mason BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2053d1310b2eSChris Mason 20540f7d52f4SChris Mason BTRFS_I(fs_info->btree_inode)->root = tree_root; 20550f7d52f4SChris Mason memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 20560f7d52f4SChris Mason sizeof(struct btrfs_key)); 205776dda93cSYan, Zheng BTRFS_I(fs_info->btree_inode)->dummy_inode = 1; 205822b0ebdaSChris Mason insert_inode_hash(fs_info->btree_inode); 205939279cc3SChris Mason 2060e02119d5SChris Mason spin_lock_init(&fs_info->block_group_cache_lock); 20616bef4d31SEric Paris fs_info->block_group_cache_tree = RB_ROOT; 2062925baeddSChris Mason 206311833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[0], 2064f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 206511833d66SYan Zheng extent_io_tree_init(&fs_info->freed_extents[1], 2066f993c883SDavid Sterba fs_info->btree_inode->i_mapping); 206711833d66SYan Zheng fs_info->pinned_extents = &fs_info->freed_extents[0]; 2068e6dcd2dcSChris Mason fs_info->do_barriers = 1; 2069f9295749SChris Mason 2070d98237b3SChris Mason 20715a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex); 207230ae8467SChris Mason mutex_init(&fs_info->tree_log_mutex); 2073925baeddSChris Mason mutex_init(&fs_info->chunk_mutex); 2074925baeddSChris Mason mutex_init(&fs_info->transaction_kthread_mutex); 2075a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex); 2076a74a4b97SChris Mason mutex_init(&fs_info->volume_mutex); 2077276e680dSYan Zheng init_rwsem(&fs_info->extent_commit_sem); 2078c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem); 207976dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem); 2080fa9c0d79SChris Mason 2081fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2082fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2083fa9c0d79SChris Mason 20847d9eb12cSChris Mason init_waitqueue_head(&fs_info->transaction_throttle); 20853768f368SChris Mason init_waitqueue_head(&fs_info->transaction_wait); 2086bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait); 20874854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait); 20889a8dd150SChris Mason 20890b86a832SChris Mason __setup_root(4096, 4096, 4096, 4096, tree_root, 20902c90e5d6SChris Mason fs_info, BTRFS_ROOT_TREE_OBJECTID); 20917eccb903SChris Mason 2092a512bbf8SYan Zheng bh = btrfs_read_dev_super(fs_devices->latest_bdev); 209320b45077SDave Young if (!bh) { 209420b45077SDave Young err = -EINVAL; 209516cdcec7SMiao Xie goto fail_alloc; 209620b45077SDave Young } 209739279cc3SChris Mason 20986c41761fSDavid Sterba memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy)); 20996c41761fSDavid Sterba memcpy(fs_info->super_for_commit, fs_info->super_copy, 21006c41761fSDavid Sterba sizeof(*fs_info->super_for_commit)); 2101a061fc8dSChris Mason brelse(bh); 21025f39d397SChris Mason 21036c41761fSDavid Sterba memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE); 21040b86a832SChris Mason 21056c41761fSDavid Sterba disk_super = fs_info->super_copy; 21060f7d52f4SChris Mason if (!btrfs_super_root(disk_super)) 210716cdcec7SMiao Xie goto fail_alloc; 21080f7d52f4SChris Mason 2109acce952bSliubo /* check FS state, whether FS is broken. */ 2110acce952bSliubo fs_info->fs_state |= btrfs_super_flags(disk_super); 2111acce952bSliubo 2112acce952bSliubo btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 2113acce952bSliubo 211475e7cb7fSLiu Bo /* 2115af31f5e5SChris Mason * run through our array of backup supers and setup 2116af31f5e5SChris Mason * our ring pointer to the oldest one 2117af31f5e5SChris Mason */ 2118af31f5e5SChris Mason generation = btrfs_super_generation(disk_super); 2119af31f5e5SChris Mason find_oldest_super_backup(fs_info, generation); 2120af31f5e5SChris Mason 2121af31f5e5SChris Mason /* 212275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super 212375e7cb7fSLiu Bo * block, and it'll be used for per file compression control. 212475e7cb7fSLiu Bo */ 212575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 212675e7cb7fSLiu Bo 21272b82032cSYan Zheng ret = btrfs_parse_options(tree_root, options); 21282b82032cSYan Zheng if (ret) { 21292b82032cSYan Zheng err = ret; 213016cdcec7SMiao Xie goto fail_alloc; 21312b82032cSYan Zheng } 2132dfe25020SChris Mason 2133f2b636e8SJosef Bacik features = btrfs_super_incompat_flags(disk_super) & 2134f2b636e8SJosef Bacik ~BTRFS_FEATURE_INCOMPAT_SUPP; 2135f2b636e8SJosef Bacik if (features) { 2136f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount because of " 2137f2b636e8SJosef Bacik "unsupported optional features (%Lx).\n", 213821380931SJoel Becker (unsigned long long)features); 2139f2b636e8SJosef Bacik err = -EINVAL; 214016cdcec7SMiao Xie goto fail_alloc; 2141f2b636e8SJosef Bacik } 2142f2b636e8SJosef Bacik 21435d4f98a2SYan Zheng features = btrfs_super_incompat_flags(disk_super); 21445d4f98a2SYan Zheng features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 2145a6fa6faeSLi Zefan if (tree_root->fs_info->compress_type & BTRFS_COMPRESS_LZO) 2146a6fa6faeSLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 21475d4f98a2SYan Zheng btrfs_set_super_incompat_flags(disk_super, features); 21485d4f98a2SYan Zheng 2149f2b636e8SJosef Bacik features = btrfs_super_compat_ro_flags(disk_super) & 2150f2b636e8SJosef Bacik ~BTRFS_FEATURE_COMPAT_RO_SUPP; 2151f2b636e8SJosef Bacik if (!(sb->s_flags & MS_RDONLY) && features) { 2152f2b636e8SJosef Bacik printk(KERN_ERR "BTRFS: couldn't mount RDWR because of " 2153f2b636e8SJosef Bacik "unsupported option features (%Lx).\n", 215421380931SJoel Becker (unsigned long long)features); 2155f2b636e8SJosef Bacik err = -EINVAL; 215616cdcec7SMiao Xie goto fail_alloc; 2157f2b636e8SJosef Bacik } 215861d92c32SChris Mason 215961d92c32SChris Mason btrfs_init_workers(&fs_info->generic_worker, 216061d92c32SChris Mason "genwork", 1, NULL); 216161d92c32SChris Mason 21625443be45SChris Mason btrfs_init_workers(&fs_info->workers, "worker", 216361d92c32SChris Mason fs_info->thread_pool_size, 216461d92c32SChris Mason &fs_info->generic_worker); 2165c8b97818SChris Mason 2166771ed689SChris Mason btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 216761d92c32SChris Mason fs_info->thread_pool_size, 216861d92c32SChris Mason &fs_info->generic_worker); 2169771ed689SChris Mason 21705443be45SChris Mason btrfs_init_workers(&fs_info->submit_workers, "submit", 2171b720d209SChris Mason min_t(u64, fs_devices->num_devices, 217261d92c32SChris Mason fs_info->thread_pool_size), 217361d92c32SChris Mason &fs_info->generic_worker); 217461b49440SChris Mason 2175bab39bf9SJosef Bacik btrfs_init_workers(&fs_info->caching_workers, "cache", 2176bab39bf9SJosef Bacik 2, &fs_info->generic_worker); 2177bab39bf9SJosef Bacik 217861b49440SChris Mason /* a higher idle thresh on the submit workers makes it much more 217961b49440SChris Mason * likely that bios will be send down in a sane order to the 218061b49440SChris Mason * devices 218161b49440SChris Mason */ 218261b49440SChris Mason fs_info->submit_workers.idle_thresh = 64; 218353863232SChris Mason 2184771ed689SChris Mason fs_info->workers.idle_thresh = 16; 21854a69a410SChris Mason fs_info->workers.ordered = 1; 218661b49440SChris Mason 2187771ed689SChris Mason fs_info->delalloc_workers.idle_thresh = 2; 2188771ed689SChris Mason fs_info->delalloc_workers.ordered = 1; 2189771ed689SChris Mason 219061d92c32SChris Mason btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1, 219161d92c32SChris Mason &fs_info->generic_worker); 21925443be45SChris Mason btrfs_init_workers(&fs_info->endio_workers, "endio", 219361d92c32SChris Mason fs_info->thread_pool_size, 219461d92c32SChris Mason &fs_info->generic_worker); 2195d20f7043SChris Mason btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 219661d92c32SChris Mason fs_info->thread_pool_size, 219761d92c32SChris Mason &fs_info->generic_worker); 2198cad321adSChris Mason btrfs_init_workers(&fs_info->endio_meta_write_workers, 219961d92c32SChris Mason "endio-meta-write", fs_info->thread_pool_size, 220061d92c32SChris Mason &fs_info->generic_worker); 22015443be45SChris Mason btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 220261d92c32SChris Mason fs_info->thread_pool_size, 220361d92c32SChris Mason &fs_info->generic_worker); 22040cb59c99SJosef Bacik btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write", 22050cb59c99SJosef Bacik 1, &fs_info->generic_worker); 220616cdcec7SMiao Xie btrfs_init_workers(&fs_info->delayed_workers, "delayed-meta", 220716cdcec7SMiao Xie fs_info->thread_pool_size, 220816cdcec7SMiao Xie &fs_info->generic_worker); 220990519d66SArne Jansen btrfs_init_workers(&fs_info->readahead_workers, "readahead", 221090519d66SArne Jansen fs_info->thread_pool_size, 221190519d66SArne Jansen &fs_info->generic_worker); 221261b49440SChris Mason 221361b49440SChris Mason /* 221461b49440SChris Mason * endios are largely parallel and should have a very 221561b49440SChris Mason * low idle thresh 221661b49440SChris Mason */ 221761b49440SChris Mason fs_info->endio_workers.idle_thresh = 4; 2218b51912c9SChris Mason fs_info->endio_meta_workers.idle_thresh = 4; 2219b51912c9SChris Mason 22209042846bSChris Mason fs_info->endio_write_workers.idle_thresh = 2; 22219042846bSChris Mason fs_info->endio_meta_write_workers.idle_thresh = 2; 222290519d66SArne Jansen fs_info->readahead_workers.idle_thresh = 2; 22239042846bSChris Mason 22240dc3b84aSJosef Bacik /* 22250dc3b84aSJosef Bacik * btrfs_start_workers can really only fail because of ENOMEM so just 22260dc3b84aSJosef Bacik * return -ENOMEM if any of these fail. 22270dc3b84aSJosef Bacik */ 22280dc3b84aSJosef Bacik ret = btrfs_start_workers(&fs_info->workers); 22290dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->generic_worker); 22300dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->submit_workers); 22310dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delalloc_workers); 22320dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->fixup_workers); 22330dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_workers); 22340dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_workers); 22350dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_meta_write_workers); 22360dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_write_workers); 22370dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->endio_freespace_worker); 22380dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->delayed_workers); 22390dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->caching_workers); 22400dc3b84aSJosef Bacik ret |= btrfs_start_workers(&fs_info->readahead_workers); 22410dc3b84aSJosef Bacik if (ret) { 22420dc3b84aSJosef Bacik ret = -ENOMEM; 22430dc3b84aSJosef Bacik goto fail_sb_buffer; 22440dc3b84aSJosef Bacik } 22454543df7eSChris Mason 22464575c9ccSChris Mason fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 2247c8b97818SChris Mason fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 2248c8b97818SChris Mason 4 * 1024 * 1024 / PAGE_CACHE_SIZE); 22494575c9ccSChris Mason 2250db94535dSChris Mason nodesize = btrfs_super_nodesize(disk_super); 2251db94535dSChris Mason leafsize = btrfs_super_leafsize(disk_super); 2252db94535dSChris Mason sectorsize = btrfs_super_sectorsize(disk_super); 225387ee04ebSChris Mason stripesize = btrfs_super_stripesize(disk_super); 2254db94535dSChris Mason tree_root->nodesize = nodesize; 2255db94535dSChris Mason tree_root->leafsize = leafsize; 2256db94535dSChris Mason tree_root->sectorsize = sectorsize; 225787ee04ebSChris Mason tree_root->stripesize = stripesize; 2258a061fc8dSChris Mason 2259a061fc8dSChris Mason sb->s_blocksize = sectorsize; 2260a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize); 2261db94535dSChris Mason 226239279cc3SChris Mason if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, 226339279cc3SChris Mason sizeof(disk_super->magic))) { 2264d397712bSChris Mason printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id); 226539279cc3SChris Mason goto fail_sb_buffer; 226639279cc3SChris Mason } 226719c00ddcSChris Mason 2268941b2ddfSKeith Mannthey if (sectorsize < PAGE_SIZE) { 2269941b2ddfSKeith Mannthey printk(KERN_WARNING "btrfs: Incompatible sector size " 2270941b2ddfSKeith Mannthey "found on %s\n", sb->s_id); 2271941b2ddfSKeith Mannthey goto fail_sb_buffer; 2272941b2ddfSKeith Mannthey } 2273941b2ddfSKeith Mannthey 2274925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex); 2275e4404d6eSYan Zheng ret = btrfs_read_sys_array(tree_root); 2276925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex); 227784eed90fSChris Mason if (ret) { 2278d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read the system " 2279d397712bSChris Mason "array on %s\n", sb->s_id); 22805d4f98a2SYan Zheng goto fail_sb_buffer; 228184eed90fSChris Mason } 22820b86a832SChris Mason 22830b86a832SChris Mason blocksize = btrfs_level_size(tree_root, 22840b86a832SChris Mason btrfs_super_chunk_root_level(disk_super)); 228584234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super); 22860b86a832SChris Mason 22870b86a832SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 22880b86a832SChris Mason chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 22890b86a832SChris Mason 22900b86a832SChris Mason chunk_root->node = read_tree_block(chunk_root, 22910b86a832SChris Mason btrfs_super_chunk_root(disk_super), 229284234f3aSYan Zheng blocksize, generation); 22930b86a832SChris Mason BUG_ON(!chunk_root->node); 229483121942SDavid Woodhouse if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 229583121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 229683121942SDavid Woodhouse sb->s_id); 2297af31f5e5SChris Mason goto fail_tree_roots; 229883121942SDavid Woodhouse } 22995d4f98a2SYan Zheng btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 23005d4f98a2SYan Zheng chunk_root->commit_root = btrfs_root_node(chunk_root); 23010b86a832SChris Mason 2302e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 2303e17cade2SChris Mason (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 2304e17cade2SChris Mason BTRFS_UUID_SIZE); 2305e17cade2SChris Mason 23060b86a832SChris Mason ret = btrfs_read_chunk_tree(chunk_root); 23072b82032cSYan Zheng if (ret) { 2308d397712bSChris Mason printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n", 2309d397712bSChris Mason sb->s_id); 2310af31f5e5SChris Mason goto fail_tree_roots; 23112b82032cSYan Zheng } 23120b86a832SChris Mason 2313dfe25020SChris Mason btrfs_close_extra_devices(fs_devices); 2314dfe25020SChris Mason 2315a6b0d5c8SChris Mason if (!fs_devices->latest_bdev) { 2316a6b0d5c8SChris Mason printk(KERN_CRIT "btrfs: failed to read devices on %s\n", 2317a6b0d5c8SChris Mason sb->s_id); 2318a6b0d5c8SChris Mason goto fail_tree_roots; 2319a6b0d5c8SChris Mason } 2320a6b0d5c8SChris Mason 2321af31f5e5SChris Mason retry_root_backup: 2322db94535dSChris Mason blocksize = btrfs_level_size(tree_root, 2323db94535dSChris Mason btrfs_super_root_level(disk_super)); 232484234f3aSYan Zheng generation = btrfs_super_generation(disk_super); 23250b86a832SChris Mason 2326e20d96d6SChris Mason tree_root->node = read_tree_block(tree_root, 2327db94535dSChris Mason btrfs_super_root(disk_super), 232884234f3aSYan Zheng blocksize, generation); 2329af31f5e5SChris Mason if (!tree_root->node || 2330af31f5e5SChris Mason !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) { 233183121942SDavid Woodhouse printk(KERN_WARNING "btrfs: failed to read tree root on %s\n", 233283121942SDavid Woodhouse sb->s_id); 2333af31f5e5SChris Mason 2334af31f5e5SChris Mason goto recovery_tree_root; 233583121942SDavid Woodhouse } 2336af31f5e5SChris Mason 23375d4f98a2SYan Zheng btrfs_set_root_node(&tree_root->root_item, tree_root->node); 23385d4f98a2SYan Zheng tree_root->commit_root = btrfs_root_node(tree_root); 2339db94535dSChris Mason 2340db94535dSChris Mason ret = find_and_setup_root(tree_root, fs_info, 2341e20d96d6SChris Mason BTRFS_EXTENT_TREE_OBJECTID, extent_root); 23420b86a832SChris Mason if (ret) 2343af31f5e5SChris Mason goto recovery_tree_root; 23440b86a832SChris Mason extent_root->track_dirty = 1; 23450b86a832SChris Mason 23460b86a832SChris Mason ret = find_and_setup_root(tree_root, fs_info, 23470b86a832SChris Mason BTRFS_DEV_TREE_OBJECTID, dev_root); 23480b86a832SChris Mason if (ret) 2349af31f5e5SChris Mason goto recovery_tree_root; 23505d4f98a2SYan Zheng dev_root->track_dirty = 1; 23513768f368SChris Mason 2352d20f7043SChris Mason ret = find_and_setup_root(tree_root, fs_info, 2353d20f7043SChris Mason BTRFS_CSUM_TREE_OBJECTID, csum_root); 2354d20f7043SChris Mason if (ret) 2355af31f5e5SChris Mason goto recovery_tree_root; 2356d20f7043SChris Mason 2357d20f7043SChris Mason csum_root->track_dirty = 1; 2358d20f7043SChris Mason 23598929ecfaSYan, Zheng fs_info->generation = generation; 23608929ecfaSYan, Zheng fs_info->last_trans_committed = generation; 23618929ecfaSYan, Zheng 2362c59021f8Sliubo ret = btrfs_init_space_info(fs_info); 2363c59021f8Sliubo if (ret) { 2364c59021f8Sliubo printk(KERN_ERR "Failed to initial space info: %d\n", ret); 2365c59021f8Sliubo goto fail_block_groups; 2366c59021f8Sliubo } 2367c59021f8Sliubo 23681b1d1f66SJosef Bacik ret = btrfs_read_block_groups(extent_root); 23691b1d1f66SJosef Bacik if (ret) { 23701b1d1f66SJosef Bacik printk(KERN_ERR "Failed to read block groups: %d\n", ret); 23711b1d1f66SJosef Bacik goto fail_block_groups; 23721b1d1f66SJosef Bacik } 23739078a3e1SChris Mason 2374a74a4b97SChris Mason fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 2375a74a4b97SChris Mason "btrfs-cleaner"); 237657506d50SQinghuang Feng if (IS_ERR(fs_info->cleaner_kthread)) 23771b1d1f66SJosef Bacik goto fail_block_groups; 2378a74a4b97SChris Mason 2379a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread, 2380a74a4b97SChris Mason tree_root, 2381a74a4b97SChris Mason "btrfs-transaction"); 238257506d50SQinghuang Feng if (IS_ERR(fs_info->transaction_kthread)) 23833f157a2fSChris Mason goto fail_cleaner; 2384a74a4b97SChris Mason 2385c289811cSChris Mason if (!btrfs_test_opt(tree_root, SSD) && 2386c289811cSChris Mason !btrfs_test_opt(tree_root, NOSSD) && 2387c289811cSChris Mason !fs_info->fs_devices->rotating) { 2388c289811cSChris Mason printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD " 2389c289811cSChris Mason "mode\n"); 2390c289811cSChris Mason btrfs_set_opt(fs_info->mount_opt, SSD); 2391c289811cSChris Mason } 2392c289811cSChris Mason 239321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 239421adbd5cSStefan Behrens if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) { 239521adbd5cSStefan Behrens ret = btrfsic_mount(tree_root, fs_devices, 239621adbd5cSStefan Behrens btrfs_test_opt(tree_root, 239721adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 239821adbd5cSStefan Behrens 1 : 0, 239921adbd5cSStefan Behrens fs_info->check_integrity_print_mask); 240021adbd5cSStefan Behrens if (ret) 240121adbd5cSStefan Behrens printk(KERN_WARNING "btrfs: failed to initialize" 240221adbd5cSStefan Behrens " integrity check module %s\n", sb->s_id); 240321adbd5cSStefan Behrens } 240421adbd5cSStefan Behrens #endif 240521adbd5cSStefan Behrens 2406acce952bSliubo /* do not make disk changes in broken FS */ 2407acce952bSliubo if (btrfs_super_log_root(disk_super) != 0 && 2408acce952bSliubo !(fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)) { 2409e02119d5SChris Mason u64 bytenr = btrfs_super_log_root(disk_super); 2410d18a2c44SChris Mason 24117c2ca468SChris Mason if (fs_devices->rw_devices == 0) { 2412d397712bSChris Mason printk(KERN_WARNING "Btrfs log replay required " 2413d397712bSChris Mason "on RO media\n"); 24147c2ca468SChris Mason err = -EIO; 24157c2ca468SChris Mason goto fail_trans_kthread; 24167c2ca468SChris Mason } 2417e02119d5SChris Mason blocksize = 2418e02119d5SChris Mason btrfs_level_size(tree_root, 2419e02119d5SChris Mason btrfs_super_log_root_level(disk_super)); 2420e02119d5SChris Mason 24216f07e42eSAl Viro log_tree_root = btrfs_alloc_root(fs_info); 2422676e4c86SDan Carpenter if (!log_tree_root) { 2423676e4c86SDan Carpenter err = -ENOMEM; 2424676e4c86SDan Carpenter goto fail_trans_kthread; 2425676e4c86SDan Carpenter } 2426e02119d5SChris Mason 2427e02119d5SChris Mason __setup_root(nodesize, leafsize, sectorsize, stripesize, 2428e02119d5SChris Mason log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2429e02119d5SChris Mason 2430e02119d5SChris Mason log_tree_root->node = read_tree_block(tree_root, bytenr, 243184234f3aSYan Zheng blocksize, 243284234f3aSYan Zheng generation + 1); 2433e02119d5SChris Mason ret = btrfs_recover_log_trees(log_tree_root); 2434e02119d5SChris Mason BUG_ON(ret); 2435e556ce2cSYan Zheng 2436e556ce2cSYan Zheng if (sb->s_flags & MS_RDONLY) { 2437e556ce2cSYan Zheng ret = btrfs_commit_super(tree_root); 2438e556ce2cSYan Zheng BUG_ON(ret); 2439e556ce2cSYan Zheng } 2440e02119d5SChris Mason } 24411a40e23bSZheng Yan 244276dda93cSYan, Zheng ret = btrfs_find_orphan_roots(tree_root); 244376dda93cSYan, Zheng BUG_ON(ret); 244476dda93cSYan, Zheng 24457c2ca468SChris Mason if (!(sb->s_flags & MS_RDONLY)) { 2446d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(fs_info); 2447d68fc57bSYan, Zheng BUG_ON(ret); 2448d68fc57bSYan, Zheng 24495d4f98a2SYan Zheng ret = btrfs_recover_relocation(tree_root); 2450d7ce5843SMiao Xie if (ret < 0) { 2451d7ce5843SMiao Xie printk(KERN_WARNING 2452d7ce5843SMiao Xie "btrfs: failed to recover relocation\n"); 2453d7ce5843SMiao Xie err = -EINVAL; 2454d7ce5843SMiao Xie goto fail_trans_kthread; 2455d7ce5843SMiao Xie } 24567c2ca468SChris Mason } 24571a40e23bSZheng Yan 24583de4586cSChris Mason location.objectid = BTRFS_FS_TREE_OBJECTID; 24593de4586cSChris Mason location.type = BTRFS_ROOT_ITEM_KEY; 24603de4586cSChris Mason location.offset = (u64)-1; 24613de4586cSChris Mason 24623de4586cSChris Mason fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 24633de4586cSChris Mason if (!fs_info->fs_root) 24647c2ca468SChris Mason goto fail_trans_kthread; 24653140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) { 24663140c9a3SDan Carpenter err = PTR_ERR(fs_info->fs_root); 24673140c9a3SDan Carpenter goto fail_trans_kthread; 24683140c9a3SDan Carpenter } 2469c289811cSChris Mason 2470e3acc2a6SJosef Bacik if (!(sb->s_flags & MS_RDONLY)) { 2471e3acc2a6SJosef Bacik down_read(&fs_info->cleanup_work_sem); 247266b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(fs_info->fs_root); 247366b4ffd1SJosef Bacik if (!err) 247466b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(fs_info->tree_root); 2475e3acc2a6SJosef Bacik up_read(&fs_info->cleanup_work_sem); 247659641015SIlya Dryomov 247759641015SIlya Dryomov if (!err) 247859641015SIlya Dryomov err = btrfs_recover_balance(fs_info->tree_root); 247959641015SIlya Dryomov 248066b4ffd1SJosef Bacik if (err) { 248166b4ffd1SJosef Bacik close_ctree(tree_root); 2482ad2b2c80SAl Viro return err; 248366b4ffd1SJosef Bacik } 2484e3acc2a6SJosef Bacik } 2485e3acc2a6SJosef Bacik 2486ad2b2c80SAl Viro return 0; 248739279cc3SChris Mason 24887c2ca468SChris Mason fail_trans_kthread: 24897c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread); 24903f157a2fSChris Mason fail_cleaner: 2491a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread); 24927c2ca468SChris Mason 24937c2ca468SChris Mason /* 24947c2ca468SChris Mason * make sure we're done with the btree inode before we stop our 24957c2ca468SChris Mason * kthreads 24967c2ca468SChris Mason */ 24977c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping); 24987c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 24997c2ca468SChris Mason 25001b1d1f66SJosef Bacik fail_block_groups: 25011b1d1f66SJosef Bacik btrfs_free_block_groups(fs_info); 2502af31f5e5SChris Mason 2503af31f5e5SChris Mason fail_tree_roots: 2504af31f5e5SChris Mason free_root_pointers(fs_info, 1); 2505af31f5e5SChris Mason 250639279cc3SChris Mason fail_sb_buffer: 250761d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 2508306c8b68SChris Mason btrfs_stop_workers(&fs_info->readahead_workers); 2509247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 2510771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 25118b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 25128b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 2513d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 2514cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2515e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 25160cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 25171cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 251816cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 2519bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 252016cdcec7SMiao Xie fail_alloc: 25214543df7eSChris Mason fail_iput: 2522586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree); 2523586e46e2SIlya Dryomov 25247c2ca468SChris Mason invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 25254543df7eSChris Mason iput(fs_info->btree_inode); 2526ad081f14SJens Axboe fail_bdi: 25277e662854SQinghuang Feng bdi_destroy(&fs_info->bdi); 252876dda93cSYan, Zheng fail_srcu: 252976dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 25307e662854SQinghuang Feng fail: 2531586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices); 2532ad2b2c80SAl Viro return err; 2533af31f5e5SChris Mason 2534af31f5e5SChris Mason recovery_tree_root: 2535af31f5e5SChris Mason if (!btrfs_test_opt(tree_root, RECOVERY)) 2536af31f5e5SChris Mason goto fail_tree_roots; 2537af31f5e5SChris Mason 2538af31f5e5SChris Mason free_root_pointers(fs_info, 0); 2539af31f5e5SChris Mason 2540af31f5e5SChris Mason /* don't use the log in recovery mode, it won't be valid */ 2541af31f5e5SChris Mason btrfs_set_super_log_root(disk_super, 0); 2542af31f5e5SChris Mason 2543af31f5e5SChris Mason /* we can't trust the free space cache either */ 2544af31f5e5SChris Mason btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE); 2545af31f5e5SChris Mason 2546af31f5e5SChris Mason ret = next_root_backup(fs_info, fs_info->super_copy, 2547af31f5e5SChris Mason &num_backups_tried, &backup_index); 2548af31f5e5SChris Mason if (ret == -1) 2549af31f5e5SChris Mason goto fail_block_groups; 2550af31f5e5SChris Mason goto retry_root_backup; 2551eb60ceacSChris Mason } 2552eb60ceacSChris Mason 2553f2984462SChris Mason static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 2554f2984462SChris Mason { 2555f2984462SChris Mason char b[BDEVNAME_SIZE]; 2556f2984462SChris Mason 2557f2984462SChris Mason if (uptodate) { 2558f2984462SChris Mason set_buffer_uptodate(bh); 2559f2984462SChris Mason } else { 25607a36ddecSDavid Sterba printk_ratelimited(KERN_WARNING "lost page write due to " 2561f2984462SChris Mason "I/O error on %s\n", 2562f2984462SChris Mason bdevname(bh->b_bdev, b)); 25631259ab75SChris Mason /* note, we dont' set_buffer_write_io_error because we have 25641259ab75SChris Mason * our own ways of dealing with the IO errors 25651259ab75SChris Mason */ 2566f2984462SChris Mason clear_buffer_uptodate(bh); 2567f2984462SChris Mason } 2568f2984462SChris Mason unlock_buffer(bh); 2569f2984462SChris Mason put_bh(bh); 2570f2984462SChris Mason } 2571f2984462SChris Mason 2572a512bbf8SYan Zheng struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 2573a512bbf8SYan Zheng { 2574a512bbf8SYan Zheng struct buffer_head *bh; 2575a512bbf8SYan Zheng struct buffer_head *latest = NULL; 2576a512bbf8SYan Zheng struct btrfs_super_block *super; 2577a512bbf8SYan Zheng int i; 2578a512bbf8SYan Zheng u64 transid = 0; 2579a512bbf8SYan Zheng u64 bytenr; 2580a512bbf8SYan Zheng 2581a512bbf8SYan Zheng /* we would like to check all the supers, but that would make 2582a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS. 2583a512bbf8SYan Zheng * So, we need to add a special mount option to scan for 2584a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead 2585a512bbf8SYan Zheng */ 2586a512bbf8SYan Zheng for (i = 0; i < 1; i++) { 2587a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2588a512bbf8SYan Zheng if (bytenr + 4096 >= i_size_read(bdev->bd_inode)) 2589a512bbf8SYan Zheng break; 2590a512bbf8SYan Zheng bh = __bread(bdev, bytenr / 4096, 4096); 2591a512bbf8SYan Zheng if (!bh) 2592a512bbf8SYan Zheng continue; 2593a512bbf8SYan Zheng 2594a512bbf8SYan Zheng super = (struct btrfs_super_block *)bh->b_data; 2595a512bbf8SYan Zheng if (btrfs_super_bytenr(super) != bytenr || 2596a512bbf8SYan Zheng strncmp((char *)(&super->magic), BTRFS_MAGIC, 2597a512bbf8SYan Zheng sizeof(super->magic))) { 2598a512bbf8SYan Zheng brelse(bh); 2599a512bbf8SYan Zheng continue; 2600a512bbf8SYan Zheng } 2601a512bbf8SYan Zheng 2602a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) { 2603a512bbf8SYan Zheng brelse(latest); 2604a512bbf8SYan Zheng latest = bh; 2605a512bbf8SYan Zheng transid = btrfs_super_generation(super); 2606a512bbf8SYan Zheng } else { 2607a512bbf8SYan Zheng brelse(bh); 2608a512bbf8SYan Zheng } 2609a512bbf8SYan Zheng } 2610a512bbf8SYan Zheng return latest; 2611a512bbf8SYan Zheng } 2612a512bbf8SYan Zheng 26134eedeb75SHisashi Hifumi /* 26144eedeb75SHisashi Hifumi * this should be called twice, once with wait == 0 and 26154eedeb75SHisashi Hifumi * once with wait == 1. When wait == 0 is done, all the buffer heads 26164eedeb75SHisashi Hifumi * we write are pinned. 26174eedeb75SHisashi Hifumi * 26184eedeb75SHisashi Hifumi * They are released when wait == 1 is done. 26194eedeb75SHisashi Hifumi * max_mirrors must be the same for both runs, and it indicates how 26204eedeb75SHisashi Hifumi * many supers on this one device should be written. 26214eedeb75SHisashi Hifumi * 26224eedeb75SHisashi Hifumi * max_mirrors == 0 means to write them all. 26234eedeb75SHisashi Hifumi */ 2624a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device, 2625a512bbf8SYan Zheng struct btrfs_super_block *sb, 2626a512bbf8SYan Zheng int do_barriers, int wait, int max_mirrors) 2627a512bbf8SYan Zheng { 2628a512bbf8SYan Zheng struct buffer_head *bh; 2629a512bbf8SYan Zheng int i; 2630a512bbf8SYan Zheng int ret; 2631a512bbf8SYan Zheng int errors = 0; 2632a512bbf8SYan Zheng u32 crc; 2633a512bbf8SYan Zheng u64 bytenr; 2634a512bbf8SYan Zheng 2635a512bbf8SYan Zheng if (max_mirrors == 0) 2636a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX; 2637a512bbf8SYan Zheng 2638a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) { 2639a512bbf8SYan Zheng bytenr = btrfs_sb_offset(i); 2640a512bbf8SYan Zheng if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) 2641a512bbf8SYan Zheng break; 2642a512bbf8SYan Zheng 2643a512bbf8SYan Zheng if (wait) { 2644a512bbf8SYan Zheng bh = __find_get_block(device->bdev, bytenr / 4096, 2645a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2646a512bbf8SYan Zheng BUG_ON(!bh); 2647a512bbf8SYan Zheng wait_on_buffer(bh); 26484eedeb75SHisashi Hifumi if (!buffer_uptodate(bh)) 26494eedeb75SHisashi Hifumi errors++; 26504eedeb75SHisashi Hifumi 26514eedeb75SHisashi Hifumi /* drop our reference */ 26524eedeb75SHisashi Hifumi brelse(bh); 26534eedeb75SHisashi Hifumi 26544eedeb75SHisashi Hifumi /* drop the reference from the wait == 0 run */ 2655a512bbf8SYan Zheng brelse(bh); 2656a512bbf8SYan Zheng continue; 2657a512bbf8SYan Zheng } else { 2658a512bbf8SYan Zheng btrfs_set_super_bytenr(sb, bytenr); 2659a512bbf8SYan Zheng 2660a512bbf8SYan Zheng crc = ~(u32)0; 2661a512bbf8SYan Zheng crc = btrfs_csum_data(NULL, (char *)sb + 2662a512bbf8SYan Zheng BTRFS_CSUM_SIZE, crc, 2663a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE - 2664a512bbf8SYan Zheng BTRFS_CSUM_SIZE); 2665a512bbf8SYan Zheng btrfs_csum_final(crc, sb->csum); 2666a512bbf8SYan Zheng 26674eedeb75SHisashi Hifumi /* 26684eedeb75SHisashi Hifumi * one reference for us, and we leave it for the 26694eedeb75SHisashi Hifumi * caller 26704eedeb75SHisashi Hifumi */ 2671a512bbf8SYan Zheng bh = __getblk(device->bdev, bytenr / 4096, 2672a512bbf8SYan Zheng BTRFS_SUPER_INFO_SIZE); 2673a512bbf8SYan Zheng memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 2674a512bbf8SYan Zheng 26754eedeb75SHisashi Hifumi /* one reference for submit_bh */ 2676a512bbf8SYan Zheng get_bh(bh); 26774eedeb75SHisashi Hifumi 26784eedeb75SHisashi Hifumi set_buffer_uptodate(bh); 2679a512bbf8SYan Zheng lock_buffer(bh); 2680a512bbf8SYan Zheng bh->b_end_io = btrfs_end_buffer_write_sync; 2681a512bbf8SYan Zheng } 2682a512bbf8SYan Zheng 2683387125fcSChris Mason /* 2684387125fcSChris Mason * we fua the first super. The others we allow 2685387125fcSChris Mason * to go down lazy. 2686387125fcSChris Mason */ 268721adbd5cSStefan Behrens ret = btrfsic_submit_bh(WRITE_FUA, bh); 26884eedeb75SHisashi Hifumi if (ret) 2689a512bbf8SYan Zheng errors++; 2690a512bbf8SYan Zheng } 2691a512bbf8SYan Zheng return errors < i ? 0 : -1; 2692a512bbf8SYan Zheng } 2693a512bbf8SYan Zheng 2694387125fcSChris Mason /* 2695387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting 2696387125fcSChris Mason * for the barrier when it is done 2697387125fcSChris Mason */ 2698387125fcSChris Mason static void btrfs_end_empty_barrier(struct bio *bio, int err) 2699387125fcSChris Mason { 2700387125fcSChris Mason if (err) { 2701387125fcSChris Mason if (err == -EOPNOTSUPP) 2702387125fcSChris Mason set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); 2703387125fcSChris Mason clear_bit(BIO_UPTODATE, &bio->bi_flags); 2704387125fcSChris Mason } 2705387125fcSChris Mason if (bio->bi_private) 2706387125fcSChris Mason complete(bio->bi_private); 2707387125fcSChris Mason bio_put(bio); 2708387125fcSChris Mason } 2709387125fcSChris Mason 2710387125fcSChris Mason /* 2711387125fcSChris Mason * trigger flushes for one the devices. If you pass wait == 0, the flushes are 2712387125fcSChris Mason * sent down. With wait == 1, it waits for the previous flush. 2713387125fcSChris Mason * 2714387125fcSChris Mason * any device where the flush fails with eopnotsupp are flagged as not-barrier 2715387125fcSChris Mason * capable 2716387125fcSChris Mason */ 2717387125fcSChris Mason static int write_dev_flush(struct btrfs_device *device, int wait) 2718387125fcSChris Mason { 2719387125fcSChris Mason struct bio *bio; 2720387125fcSChris Mason int ret = 0; 2721387125fcSChris Mason 2722387125fcSChris Mason if (device->nobarriers) 2723387125fcSChris Mason return 0; 2724387125fcSChris Mason 2725387125fcSChris Mason if (wait) { 2726387125fcSChris Mason bio = device->flush_bio; 2727387125fcSChris Mason if (!bio) 2728387125fcSChris Mason return 0; 2729387125fcSChris Mason 2730387125fcSChris Mason wait_for_completion(&device->flush_wait); 2731387125fcSChris Mason 2732387125fcSChris Mason if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 2733387125fcSChris Mason printk("btrfs: disabling barriers on dev %s\n", 2734387125fcSChris Mason device->name); 2735387125fcSChris Mason device->nobarriers = 1; 2736387125fcSChris Mason } 2737387125fcSChris Mason if (!bio_flagged(bio, BIO_UPTODATE)) { 2738387125fcSChris Mason ret = -EIO; 2739387125fcSChris Mason } 2740387125fcSChris Mason 2741387125fcSChris Mason /* drop the reference from the wait == 0 run */ 2742387125fcSChris Mason bio_put(bio); 2743387125fcSChris Mason device->flush_bio = NULL; 2744387125fcSChris Mason 2745387125fcSChris Mason return ret; 2746387125fcSChris Mason } 2747387125fcSChris Mason 2748387125fcSChris Mason /* 2749387125fcSChris Mason * one reference for us, and we leave it for the 2750387125fcSChris Mason * caller 2751387125fcSChris Mason */ 2752387125fcSChris Mason device->flush_bio = NULL;; 2753387125fcSChris Mason bio = bio_alloc(GFP_NOFS, 0); 2754387125fcSChris Mason if (!bio) 2755387125fcSChris Mason return -ENOMEM; 2756387125fcSChris Mason 2757387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier; 2758387125fcSChris Mason bio->bi_bdev = device->bdev; 2759387125fcSChris Mason init_completion(&device->flush_wait); 2760387125fcSChris Mason bio->bi_private = &device->flush_wait; 2761387125fcSChris Mason device->flush_bio = bio; 2762387125fcSChris Mason 2763387125fcSChris Mason bio_get(bio); 276421adbd5cSStefan Behrens btrfsic_submit_bio(WRITE_FLUSH, bio); 2765387125fcSChris Mason 2766387125fcSChris Mason return 0; 2767387125fcSChris Mason } 2768387125fcSChris Mason 2769387125fcSChris Mason /* 2770387125fcSChris Mason * send an empty flush down to each device in parallel, 2771387125fcSChris Mason * then wait for them 2772387125fcSChris Mason */ 2773387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info) 2774387125fcSChris Mason { 2775387125fcSChris Mason struct list_head *head; 2776387125fcSChris Mason struct btrfs_device *dev; 2777387125fcSChris Mason int errors = 0; 2778387125fcSChris Mason int ret; 2779387125fcSChris Mason 2780387125fcSChris Mason /* send down all the barriers */ 2781387125fcSChris Mason head = &info->fs_devices->devices; 2782387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 2783387125fcSChris Mason if (!dev->bdev) { 2784387125fcSChris Mason errors++; 2785387125fcSChris Mason continue; 2786387125fcSChris Mason } 2787387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 2788387125fcSChris Mason continue; 2789387125fcSChris Mason 2790387125fcSChris Mason ret = write_dev_flush(dev, 0); 2791387125fcSChris Mason if (ret) 2792387125fcSChris Mason errors++; 2793387125fcSChris Mason } 2794387125fcSChris Mason 2795387125fcSChris Mason /* wait for all the barriers */ 2796387125fcSChris Mason list_for_each_entry_rcu(dev, head, dev_list) { 2797387125fcSChris Mason if (!dev->bdev) { 2798387125fcSChris Mason errors++; 2799387125fcSChris Mason continue; 2800387125fcSChris Mason } 2801387125fcSChris Mason if (!dev->in_fs_metadata || !dev->writeable) 2802387125fcSChris Mason continue; 2803387125fcSChris Mason 2804387125fcSChris Mason ret = write_dev_flush(dev, 1); 2805387125fcSChris Mason if (ret) 2806387125fcSChris Mason errors++; 2807387125fcSChris Mason } 2808387125fcSChris Mason if (errors) 2809387125fcSChris Mason return -EIO; 2810387125fcSChris Mason return 0; 2811387125fcSChris Mason } 2812387125fcSChris Mason 2813a512bbf8SYan Zheng int write_all_supers(struct btrfs_root *root, int max_mirrors) 2814f2984462SChris Mason { 2815e5e9a520SChris Mason struct list_head *head; 2816f2984462SChris Mason struct btrfs_device *dev; 2817a061fc8dSChris Mason struct btrfs_super_block *sb; 2818f2984462SChris Mason struct btrfs_dev_item *dev_item; 2819f2984462SChris Mason int ret; 2820f2984462SChris Mason int do_barriers; 2821a236aed1SChris Mason int max_errors; 2822a236aed1SChris Mason int total_errors = 0; 2823a061fc8dSChris Mason u64 flags; 2824f2984462SChris Mason 28256c41761fSDavid Sterba max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 2826f2984462SChris Mason do_barriers = !btrfs_test_opt(root, NOBARRIER); 2827af31f5e5SChris Mason backup_super_roots(root->fs_info); 2828f2984462SChris Mason 28296c41761fSDavid Sterba sb = root->fs_info->super_for_commit; 2830a061fc8dSChris Mason dev_item = &sb->dev_item; 2831e5e9a520SChris Mason 2832174ba509SChris Mason mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2833e5e9a520SChris Mason head = &root->fs_info->fs_devices->devices; 2834387125fcSChris Mason 2835387125fcSChris Mason if (do_barriers) 2836387125fcSChris Mason barrier_all_devices(root->fs_info); 2837387125fcSChris Mason 28381f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 2839dfe25020SChris Mason if (!dev->bdev) { 2840dfe25020SChris Mason total_errors++; 2841dfe25020SChris Mason continue; 2842dfe25020SChris Mason } 28432b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 2844dfe25020SChris Mason continue; 2845dfe25020SChris Mason 28462b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0); 2847a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type); 2848a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid); 2849a061fc8dSChris Mason btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 2850a061fc8dSChris Mason btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 2851a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align); 2852a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width); 2853a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 2854a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 28552b82032cSYan Zheng memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE); 2856a512bbf8SYan Zheng 2857a061fc8dSChris Mason flags = btrfs_super_flags(sb); 2858a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 2859f2984462SChris Mason 2860a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors); 2861a236aed1SChris Mason if (ret) 2862a236aed1SChris Mason total_errors++; 2863f2984462SChris Mason } 2864a236aed1SChris Mason if (total_errors > max_errors) { 2865d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 2866d397712bSChris Mason total_errors); 2867a236aed1SChris Mason BUG(); 2868a236aed1SChris Mason } 2869f2984462SChris Mason 2870a512bbf8SYan Zheng total_errors = 0; 28711f78160cSXiao Guangrong list_for_each_entry_rcu(dev, head, dev_list) { 2872dfe25020SChris Mason if (!dev->bdev) 2873dfe25020SChris Mason continue; 28742b82032cSYan Zheng if (!dev->in_fs_metadata || !dev->writeable) 2875dfe25020SChris Mason continue; 2876dfe25020SChris Mason 2877a512bbf8SYan Zheng ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors); 2878a512bbf8SYan Zheng if (ret) 28791259ab75SChris Mason total_errors++; 2880f2984462SChris Mason } 2881174ba509SChris Mason mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2882a236aed1SChris Mason if (total_errors > max_errors) { 2883d397712bSChris Mason printk(KERN_ERR "btrfs: %d errors while writing supers\n", 2884d397712bSChris Mason total_errors); 2885a236aed1SChris Mason BUG(); 2886a236aed1SChris Mason } 2887f2984462SChris Mason return 0; 2888f2984462SChris Mason } 2889f2984462SChris Mason 2890a512bbf8SYan Zheng int write_ctree_super(struct btrfs_trans_handle *trans, 2891a512bbf8SYan Zheng struct btrfs_root *root, int max_mirrors) 2892cfaa7295SChris Mason { 2893e66f709bSChris Mason int ret; 28942c90e5d6SChris Mason 2895a512bbf8SYan Zheng ret = write_all_supers(root, max_mirrors); 28965f39d397SChris Mason return ret; 2897cfaa7295SChris Mason } 2898cfaa7295SChris Mason 2899143bede5SJeff Mahoney void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 29002619ba1fSChris Mason { 29014df27c4dSYan, Zheng spin_lock(&fs_info->fs_roots_radix_lock); 29022619ba1fSChris Mason radix_tree_delete(&fs_info->fs_roots_radix, 29032619ba1fSChris Mason (unsigned long)root->root_key.objectid); 29044df27c4dSYan, Zheng spin_unlock(&fs_info->fs_roots_radix_lock); 290576dda93cSYan, Zheng 290676dda93cSYan, Zheng if (btrfs_root_refs(&root->root_item) == 0) 290776dda93cSYan, Zheng synchronize_srcu(&fs_info->subvol_srcu); 290876dda93cSYan, Zheng 2909581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_pinned); 2910581bb050SLi Zefan __btrfs_remove_free_space_cache(root->free_ino_ctl); 29114df27c4dSYan, Zheng free_fs_root(root); 29124df27c4dSYan, Zheng } 29134df27c4dSYan, Zheng 29144df27c4dSYan, Zheng static void free_fs_root(struct btrfs_root *root) 29154df27c4dSYan, Zheng { 291682d5902dSLi Zefan iput(root->cache_inode); 29174df27c4dSYan, Zheng WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 29180ee5dc67SAl Viro if (root->anon_dev) 29190ee5dc67SAl Viro free_anon_bdev(root->anon_dev); 29205f39d397SChris Mason free_extent_buffer(root->node); 29215f39d397SChris Mason free_extent_buffer(root->commit_root); 2922581bb050SLi Zefan kfree(root->free_ino_ctl); 2923581bb050SLi Zefan kfree(root->free_ino_pinned); 292458176a96SJosef Bacik kfree(root->name); 29252619ba1fSChris Mason kfree(root); 29262619ba1fSChris Mason } 29272619ba1fSChris Mason 2928143bede5SJeff Mahoney static void del_fs_roots(struct btrfs_fs_info *fs_info) 29290f7d52f4SChris Mason { 29300f7d52f4SChris Mason int ret; 29310f7d52f4SChris Mason struct btrfs_root *gang[8]; 29320f7d52f4SChris Mason int i; 29330f7d52f4SChris Mason 293476dda93cSYan, Zheng while (!list_empty(&fs_info->dead_roots)) { 293576dda93cSYan, Zheng gang[0] = list_entry(fs_info->dead_roots.next, 293676dda93cSYan, Zheng struct btrfs_root, root_list); 293776dda93cSYan, Zheng list_del(&gang[0]->root_list); 293876dda93cSYan, Zheng 293976dda93cSYan, Zheng if (gang[0]->in_radix) { 294076dda93cSYan, Zheng btrfs_free_fs_root(fs_info, gang[0]); 294176dda93cSYan, Zheng } else { 294276dda93cSYan, Zheng free_extent_buffer(gang[0]->node); 294376dda93cSYan, Zheng free_extent_buffer(gang[0]->commit_root); 294476dda93cSYan, Zheng kfree(gang[0]); 294576dda93cSYan, Zheng } 294676dda93cSYan, Zheng } 294776dda93cSYan, Zheng 29480f7d52f4SChris Mason while (1) { 29490f7d52f4SChris Mason ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 29500f7d52f4SChris Mason (void **)gang, 0, 29510f7d52f4SChris Mason ARRAY_SIZE(gang)); 29520f7d52f4SChris Mason if (!ret) 29530f7d52f4SChris Mason break; 29542619ba1fSChris Mason for (i = 0; i < ret; i++) 29555eda7b5eSChris Mason btrfs_free_fs_root(fs_info, gang[i]); 29560f7d52f4SChris Mason } 29570f7d52f4SChris Mason } 2958b4100d64SChris Mason 2959c146afadSYan Zheng int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) 2960c146afadSYan Zheng { 2961c146afadSYan Zheng u64 root_objectid = 0; 2962c146afadSYan Zheng struct btrfs_root *gang[8]; 2963c146afadSYan Zheng int i; 2964c146afadSYan Zheng int ret; 2965c146afadSYan Zheng 2966c146afadSYan Zheng while (1) { 2967c146afadSYan Zheng ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2968c146afadSYan Zheng (void **)gang, root_objectid, 2969c146afadSYan Zheng ARRAY_SIZE(gang)); 2970c146afadSYan Zheng if (!ret) 2971c146afadSYan Zheng break; 29725d4f98a2SYan Zheng 29735d4f98a2SYan Zheng root_objectid = gang[ret - 1]->root_key.objectid + 1; 2974c146afadSYan Zheng for (i = 0; i < ret; i++) { 297566b4ffd1SJosef Bacik int err; 297666b4ffd1SJosef Bacik 2977c146afadSYan Zheng root_objectid = gang[i]->root_key.objectid; 297866b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(gang[i]); 297966b4ffd1SJosef Bacik if (err) 298066b4ffd1SJosef Bacik return err; 2981c146afadSYan Zheng } 2982c146afadSYan Zheng root_objectid++; 2983c146afadSYan Zheng } 2984c146afadSYan Zheng return 0; 2985c146afadSYan Zheng } 2986c146afadSYan Zheng 2987c146afadSYan Zheng int btrfs_commit_super(struct btrfs_root *root) 2988c146afadSYan Zheng { 2989c146afadSYan Zheng struct btrfs_trans_handle *trans; 2990c146afadSYan Zheng int ret; 2991c146afadSYan Zheng 2992c146afadSYan Zheng mutex_lock(&root->fs_info->cleaner_mutex); 299324bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 2994c146afadSYan Zheng btrfs_clean_old_snapshots(root); 2995c146afadSYan Zheng mutex_unlock(&root->fs_info->cleaner_mutex); 2996c71bf099SYan, Zheng 2997c71bf099SYan, Zheng /* wait until ongoing cleanup work done */ 2998c71bf099SYan, Zheng down_write(&root->fs_info->cleanup_work_sem); 2999c71bf099SYan, Zheng up_write(&root->fs_info->cleanup_work_sem); 3000c71bf099SYan, Zheng 30017a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 30023612b495STsutomu Itoh if (IS_ERR(trans)) 30033612b495STsutomu Itoh return PTR_ERR(trans); 3004c146afadSYan Zheng ret = btrfs_commit_transaction(trans, root); 3005c146afadSYan Zheng BUG_ON(ret); 3006c146afadSYan Zheng /* run commit again to drop the original snapshot */ 30077a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 30083612b495STsutomu Itoh if (IS_ERR(trans)) 30093612b495STsutomu Itoh return PTR_ERR(trans); 3010c146afadSYan Zheng btrfs_commit_transaction(trans, root); 3011c146afadSYan Zheng ret = btrfs_write_and_wait_transaction(NULL, root); 3012c146afadSYan Zheng BUG_ON(ret); 3013c146afadSYan Zheng 3014a512bbf8SYan Zheng ret = write_ctree_super(NULL, root, 0); 3015c146afadSYan Zheng return ret; 3016c146afadSYan Zheng } 3017c146afadSYan Zheng 3018e20d96d6SChris Mason int close_ctree(struct btrfs_root *root) 3019eb60ceacSChris Mason { 30200f7d52f4SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3021c146afadSYan Zheng int ret; 3022e089f05cSChris Mason 3023facda1e7SChris Mason fs_info->closing = 1; 3024a2135011SChris Mason smp_mb(); 3025a2135011SChris Mason 3026837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */ 3027837d5b6eSIlya Dryomov btrfs_pause_balance(root->fs_info); 3028837d5b6eSIlya Dryomov 3029a2de733cSArne Jansen btrfs_scrub_cancel(root); 30304cb5300bSChris Mason 30314cb5300bSChris Mason /* wait for any defraggers to finish */ 30324cb5300bSChris Mason wait_event(fs_info->transaction_wait, 30334cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0)); 30344cb5300bSChris Mason 30354cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */ 3036e3029d9fSAl Viro btrfs_run_defrag_inodes(fs_info); 30374cb5300bSChris Mason 3038acce952bSliubo /* 3039acce952bSliubo * Here come 2 situations when btrfs is broken to flip readonly: 3040acce952bSliubo * 3041acce952bSliubo * 1. when btrfs flips readonly somewhere else before 3042acce952bSliubo * btrfs_commit_super, sb->s_flags has MS_RDONLY flag, 3043acce952bSliubo * and btrfs will skip to write sb directly to keep 3044acce952bSliubo * ERROR state on disk. 3045acce952bSliubo * 3046acce952bSliubo * 2. when btrfs flips readonly just in btrfs_commit_super, 3047ae0e47f0SJustin P. Mattock * and in such case, btrfs cannot write sb via btrfs_commit_super, 3048acce952bSliubo * and since fs_state has been set BTRFS_SUPER_FLAG_ERROR flag, 3049acce952bSliubo * btrfs will cleanup all FS resources first and write sb then. 3050acce952bSliubo */ 3051c146afadSYan Zheng if (!(fs_info->sb->s_flags & MS_RDONLY)) { 3052c146afadSYan Zheng ret = btrfs_commit_super(root); 3053d397712bSChris Mason if (ret) 3054d397712bSChris Mason printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3055c146afadSYan Zheng } 3056ed2ff2cbSChris Mason 3057acce952bSliubo if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 3058acce952bSliubo ret = btrfs_error_commit_super(root); 3059acce952bSliubo if (ret) 3060acce952bSliubo printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 3061acce952bSliubo } 3062acce952bSliubo 3063300e4f8aSJosef Bacik btrfs_put_block_group_cache(fs_info); 3064300e4f8aSJosef Bacik 3065e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread); 3066e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread); 30678929ecfaSYan, Zheng 3068f25784b3SYan Zheng fs_info->closing = 2; 3069f25784b3SYan Zheng smp_mb(); 3070f25784b3SYan Zheng 3071b0c68f8bSChris Mason if (fs_info->delalloc_bytes) { 3072d397712bSChris Mason printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n", 307321380931SJoel Becker (unsigned long long)fs_info->delalloc_bytes); 3074b0c68f8bSChris Mason } 307531153d81SYan Zheng if (fs_info->total_ref_cache_size) { 3076d397712bSChris Mason printk(KERN_INFO "btrfs: at umount reference cache size %llu\n", 3077d397712bSChris Mason (unsigned long long)fs_info->total_ref_cache_size); 307831153d81SYan Zheng } 307931153d81SYan Zheng 30805f39d397SChris Mason free_extent_buffer(fs_info->extent_root->node); 30815d4f98a2SYan Zheng free_extent_buffer(fs_info->extent_root->commit_root); 30825f39d397SChris Mason free_extent_buffer(fs_info->tree_root->node); 30835d4f98a2SYan Zheng free_extent_buffer(fs_info->tree_root->commit_root); 3084e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->node); 3085e3029d9fSAl Viro free_extent_buffer(fs_info->chunk_root->commit_root); 3086e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->node); 3087e3029d9fSAl Viro free_extent_buffer(fs_info->dev_root->commit_root); 3088e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->node); 3089e3029d9fSAl Viro free_extent_buffer(fs_info->csum_root->commit_root); 3090d20f7043SChris Mason 3091e3029d9fSAl Viro btrfs_free_block_groups(fs_info); 3092c146afadSYan Zheng 30930f7d52f4SChris Mason del_fs_roots(fs_info); 3094d10c5f31SChris Mason 3095c146afadSYan Zheng iput(fs_info->btree_inode); 30969ad6b7bcSChris Mason 309761d92c32SChris Mason btrfs_stop_workers(&fs_info->generic_worker); 3098247e743cSChris Mason btrfs_stop_workers(&fs_info->fixup_workers); 3099771ed689SChris Mason btrfs_stop_workers(&fs_info->delalloc_workers); 31008b712842SChris Mason btrfs_stop_workers(&fs_info->workers); 31018b712842SChris Mason btrfs_stop_workers(&fs_info->endio_workers); 3102d20f7043SChris Mason btrfs_stop_workers(&fs_info->endio_meta_workers); 3103cad321adSChris Mason btrfs_stop_workers(&fs_info->endio_meta_write_workers); 3104e6dcd2dcSChris Mason btrfs_stop_workers(&fs_info->endio_write_workers); 31050cb59c99SJosef Bacik btrfs_stop_workers(&fs_info->endio_freespace_worker); 31061cc127b5SChris Mason btrfs_stop_workers(&fs_info->submit_workers); 310716cdcec7SMiao Xie btrfs_stop_workers(&fs_info->delayed_workers); 3108bab39bf9SJosef Bacik btrfs_stop_workers(&fs_info->caching_workers); 310990519d66SArne Jansen btrfs_stop_workers(&fs_info->readahead_workers); 3110d6bfde87SChris Mason 311121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 311221adbd5cSStefan Behrens if (btrfs_test_opt(root, CHECK_INTEGRITY)) 311321adbd5cSStefan Behrens btrfsic_unmount(root, fs_info->fs_devices); 311421adbd5cSStefan Behrens #endif 311521adbd5cSStefan Behrens 3116dfe25020SChris Mason btrfs_close_devices(fs_info->fs_devices); 31170b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree); 3118b248a415SChris Mason 311904160088SChris Mason bdi_destroy(&fs_info->bdi); 312076dda93cSYan, Zheng cleanup_srcu_struct(&fs_info->subvol_srcu); 31210b86a832SChris Mason 3122eb60ceacSChris Mason return 0; 3123eb60ceacSChris Mason } 3124eb60ceacSChris Mason 31251259ab75SChris Mason int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid) 3126ccd467d6SChris Mason { 31271259ab75SChris Mason int ret; 3128810191ffSChris Mason struct inode *btree_inode = buf->first_page->mapping->host; 31291259ab75SChris Mason 31302ac55d41SJosef Bacik ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf, 31312ac55d41SJosef Bacik NULL); 31321259ab75SChris Mason if (!ret) 31331259ab75SChris Mason return ret; 31341259ab75SChris Mason 31351259ab75SChris Mason ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, 31361259ab75SChris Mason parent_transid); 31371259ab75SChris Mason return !ret; 31385f39d397SChris Mason } 31396702ed49SChris Mason 31405f39d397SChris Mason int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 31415f39d397SChris Mason { 3142810191ffSChris Mason struct inode *btree_inode = buf->first_page->mapping->host; 3143d1310b2eSChris Mason return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, 31445f39d397SChris Mason buf); 31455f39d397SChris Mason } 31465f39d397SChris Mason 31475f39d397SChris Mason void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 31485f39d397SChris Mason { 3149810191ffSChris Mason struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 31505f39d397SChris Mason u64 transid = btrfs_header_generation(buf); 31515f39d397SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 3152b9473439SChris Mason int was_dirty; 3153b4ce94deSChris Mason 3154b9447ef8SChris Mason btrfs_assert_tree_locked(buf); 3155ccd467d6SChris Mason if (transid != root->fs_info->generation) { 3156d397712bSChris Mason printk(KERN_CRIT "btrfs transid mismatch buffer %llu, " 3157d397712bSChris Mason "found %llu running %llu\n", 3158db94535dSChris Mason (unsigned long long)buf->start, 3159d397712bSChris Mason (unsigned long long)transid, 3160d397712bSChris Mason (unsigned long long)root->fs_info->generation); 3161ccd467d6SChris Mason WARN_ON(1); 3162ccd467d6SChris Mason } 3163b9473439SChris Mason was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, 3164b9473439SChris Mason buf); 3165b9473439SChris Mason if (!was_dirty) { 3166b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 3167b9473439SChris Mason root->fs_info->dirty_metadata_bytes += buf->len; 3168b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 3169b9473439SChris Mason } 3170eb60ceacSChris Mason } 3171eb60ceacSChris Mason 3172d3c2fdcfSChris Mason void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) 317335b7e476SChris Mason { 3174188de649SChris Mason /* 3175188de649SChris Mason * looks as though older kernels can get into trouble with 3176188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever 3177188de649SChris Mason */ 3178d6bfde87SChris Mason u64 num_dirty; 3179771ed689SChris Mason unsigned long thresh = 32 * 1024 * 1024; 3180d6bfde87SChris Mason 31816933c02eSJens Axboe if (current->flags & PF_MEMALLOC) 3182d6bfde87SChris Mason return; 3183d6bfde87SChris Mason 318416cdcec7SMiao Xie btrfs_balance_delayed_items(root); 318516cdcec7SMiao Xie 318616cdcec7SMiao Xie num_dirty = root->fs_info->dirty_metadata_bytes; 318716cdcec7SMiao Xie 318816cdcec7SMiao Xie if (num_dirty > thresh) { 318916cdcec7SMiao Xie balance_dirty_pages_ratelimited_nr( 319016cdcec7SMiao Xie root->fs_info->btree_inode->i_mapping, 1); 319116cdcec7SMiao Xie } 319216cdcec7SMiao Xie return; 319316cdcec7SMiao Xie } 319416cdcec7SMiao Xie 319516cdcec7SMiao Xie void __btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) 319616cdcec7SMiao Xie { 319716cdcec7SMiao Xie /* 319816cdcec7SMiao Xie * looks as though older kernels can get into trouble with 319916cdcec7SMiao Xie * this code, they end up stuck in balance_dirty_pages forever 320016cdcec7SMiao Xie */ 320116cdcec7SMiao Xie u64 num_dirty; 320216cdcec7SMiao Xie unsigned long thresh = 32 * 1024 * 1024; 320316cdcec7SMiao Xie 320416cdcec7SMiao Xie if (current->flags & PF_MEMALLOC) 320516cdcec7SMiao Xie return; 320616cdcec7SMiao Xie 3207585ad2c3SChris Mason num_dirty = root->fs_info->dirty_metadata_bytes; 3208585ad2c3SChris Mason 3209d6bfde87SChris Mason if (num_dirty > thresh) { 3210d3c2fdcfSChris Mason balance_dirty_pages_ratelimited_nr( 3211304fced6SChris Mason root->fs_info->btree_inode->i_mapping, 1); 321235b7e476SChris Mason } 3213188de649SChris Mason return; 3214d6bfde87SChris Mason } 32156b80053dSChris Mason 3216ca7a79adSChris Mason int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 32176b80053dSChris Mason { 3218810191ffSChris Mason struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 3219ce9adaa5SChris Mason int ret; 3220ca7a79adSChris Mason ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 3221d397712bSChris Mason if (ret == 0) 3222b4ce94deSChris Mason set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags); 3223ce9adaa5SChris Mason return ret; 32246b80053dSChris Mason } 32250da5468fSChris Mason 322601d658f2SChris Mason static int btree_lock_page_hook(struct page *page, void *data, 322701d658f2SChris Mason void (*flush_fn)(void *)) 32284bef0848SChris Mason { 32294bef0848SChris Mason struct inode *inode = page->mapping->host; 3230b9473439SChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 32314bef0848SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 32324bef0848SChris Mason struct extent_buffer *eb; 32334bef0848SChris Mason unsigned long len; 32344bef0848SChris Mason u64 bytenr = page_offset(page); 32354bef0848SChris Mason 32364bef0848SChris Mason if (page->private == EXTENT_PAGE_PRIVATE) 32374bef0848SChris Mason goto out; 32384bef0848SChris Mason 32394bef0848SChris Mason len = page->private >> 2; 3240f09d1f60SDavid Sterba eb = find_extent_buffer(io_tree, bytenr, len); 32414bef0848SChris Mason if (!eb) 32424bef0848SChris Mason goto out; 32434bef0848SChris Mason 324401d658f2SChris Mason if (!btrfs_try_tree_write_lock(eb)) { 324501d658f2SChris Mason flush_fn(data); 32464bef0848SChris Mason btrfs_tree_lock(eb); 324701d658f2SChris Mason } 32484bef0848SChris Mason btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); 3249b9473439SChris Mason 3250b9473439SChris Mason if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { 3251b9473439SChris Mason spin_lock(&root->fs_info->delalloc_lock); 3252b9473439SChris Mason if (root->fs_info->dirty_metadata_bytes >= eb->len) 3253b9473439SChris Mason root->fs_info->dirty_metadata_bytes -= eb->len; 3254b9473439SChris Mason else 3255b9473439SChris Mason WARN_ON(1); 3256b9473439SChris Mason spin_unlock(&root->fs_info->delalloc_lock); 3257b9473439SChris Mason } 3258b9473439SChris Mason 32594bef0848SChris Mason btrfs_tree_unlock(eb); 32604bef0848SChris Mason free_extent_buffer(eb); 32614bef0848SChris Mason out: 326201d658f2SChris Mason if (!trylock_page(page)) { 326301d658f2SChris Mason flush_fn(data); 32644bef0848SChris Mason lock_page(page); 326501d658f2SChris Mason } 32664bef0848SChris Mason return 0; 32674bef0848SChris Mason } 32684bef0848SChris Mason 3269acce952bSliubo static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 3270acce952bSliubo int read_only) 3271acce952bSliubo { 3272acce952bSliubo if (read_only) 3273acce952bSliubo return; 3274acce952bSliubo 3275acce952bSliubo if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 3276acce952bSliubo printk(KERN_WARNING "warning: mount fs with errors, " 3277acce952bSliubo "running btrfsck is recommended\n"); 3278acce952bSliubo } 3279acce952bSliubo 3280acce952bSliubo int btrfs_error_commit_super(struct btrfs_root *root) 3281acce952bSliubo { 3282acce952bSliubo int ret; 3283acce952bSliubo 3284acce952bSliubo mutex_lock(&root->fs_info->cleaner_mutex); 3285acce952bSliubo btrfs_run_delayed_iputs(root); 3286acce952bSliubo mutex_unlock(&root->fs_info->cleaner_mutex); 3287acce952bSliubo 3288acce952bSliubo down_write(&root->fs_info->cleanup_work_sem); 3289acce952bSliubo up_write(&root->fs_info->cleanup_work_sem); 3290acce952bSliubo 3291acce952bSliubo /* cleanup FS via transaction */ 3292acce952bSliubo btrfs_cleanup_transaction(root); 3293acce952bSliubo 3294acce952bSliubo ret = write_ctree_super(NULL, root, 0); 3295acce952bSliubo 3296acce952bSliubo return ret; 3297acce952bSliubo } 3298acce952bSliubo 3299143bede5SJeff Mahoney static void btrfs_destroy_ordered_operations(struct btrfs_root *root) 3300acce952bSliubo { 3301acce952bSliubo struct btrfs_inode *btrfs_inode; 3302acce952bSliubo struct list_head splice; 3303acce952bSliubo 3304acce952bSliubo INIT_LIST_HEAD(&splice); 3305acce952bSliubo 3306acce952bSliubo mutex_lock(&root->fs_info->ordered_operations_mutex); 3307acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3308acce952bSliubo 3309acce952bSliubo list_splice_init(&root->fs_info->ordered_operations, &splice); 3310acce952bSliubo while (!list_empty(&splice)) { 3311acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3312acce952bSliubo ordered_operations); 3313acce952bSliubo 3314acce952bSliubo list_del_init(&btrfs_inode->ordered_operations); 3315acce952bSliubo 3316acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3317acce952bSliubo } 3318acce952bSliubo 3319acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3320acce952bSliubo mutex_unlock(&root->fs_info->ordered_operations_mutex); 3321acce952bSliubo } 3322acce952bSliubo 3323143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 3324acce952bSliubo { 3325acce952bSliubo struct list_head splice; 3326acce952bSliubo struct btrfs_ordered_extent *ordered; 3327acce952bSliubo struct inode *inode; 3328acce952bSliubo 3329acce952bSliubo INIT_LIST_HEAD(&splice); 3330acce952bSliubo 3331acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3332acce952bSliubo 3333acce952bSliubo list_splice_init(&root->fs_info->ordered_extents, &splice); 3334acce952bSliubo while (!list_empty(&splice)) { 3335acce952bSliubo ordered = list_entry(splice.next, struct btrfs_ordered_extent, 3336acce952bSliubo root_extent_list); 3337acce952bSliubo 3338acce952bSliubo list_del_init(&ordered->root_extent_list); 3339acce952bSliubo atomic_inc(&ordered->refs); 3340acce952bSliubo 3341acce952bSliubo /* the inode may be getting freed (in sys_unlink path). */ 3342acce952bSliubo inode = igrab(ordered->inode); 3343acce952bSliubo 3344acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3345acce952bSliubo if (inode) 3346acce952bSliubo iput(inode); 3347acce952bSliubo 3348acce952bSliubo atomic_set(&ordered->refs, 1); 3349acce952bSliubo btrfs_put_ordered_extent(ordered); 3350acce952bSliubo 3351acce952bSliubo spin_lock(&root->fs_info->ordered_extent_lock); 3352acce952bSliubo } 3353acce952bSliubo 3354acce952bSliubo spin_unlock(&root->fs_info->ordered_extent_lock); 3355acce952bSliubo } 3356acce952bSliubo 3357acce952bSliubo static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3358acce952bSliubo struct btrfs_root *root) 3359acce952bSliubo { 3360acce952bSliubo struct rb_node *node; 3361acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs; 3362acce952bSliubo struct btrfs_delayed_ref_node *ref; 3363acce952bSliubo int ret = 0; 3364acce952bSliubo 3365acce952bSliubo delayed_refs = &trans->delayed_refs; 3366acce952bSliubo 3367acce952bSliubo spin_lock(&delayed_refs->lock); 3368acce952bSliubo if (delayed_refs->num_entries == 0) { 3369cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock); 3370acce952bSliubo printk(KERN_INFO "delayed_refs has NO entry\n"); 3371acce952bSliubo return ret; 3372acce952bSliubo } 3373acce952bSliubo 3374acce952bSliubo node = rb_first(&delayed_refs->root); 3375acce952bSliubo while (node) { 3376acce952bSliubo ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3377acce952bSliubo node = rb_next(node); 3378acce952bSliubo 3379acce952bSliubo ref->in_tree = 0; 3380acce952bSliubo rb_erase(&ref->rb_node, &delayed_refs->root); 3381acce952bSliubo delayed_refs->num_entries--; 3382acce952bSliubo 3383acce952bSliubo atomic_set(&ref->refs, 1); 3384acce952bSliubo if (btrfs_delayed_ref_is_head(ref)) { 3385acce952bSliubo struct btrfs_delayed_ref_head *head; 3386acce952bSliubo 3387acce952bSliubo head = btrfs_delayed_node_to_head(ref); 3388acce952bSliubo mutex_lock(&head->mutex); 3389acce952bSliubo kfree(head->extent_op); 3390acce952bSliubo delayed_refs->num_heads--; 3391acce952bSliubo if (list_empty(&head->cluster)) 3392acce952bSliubo delayed_refs->num_heads_ready--; 3393acce952bSliubo list_del_init(&head->cluster); 3394acce952bSliubo mutex_unlock(&head->mutex); 3395acce952bSliubo } 3396acce952bSliubo 3397acce952bSliubo spin_unlock(&delayed_refs->lock); 3398acce952bSliubo btrfs_put_delayed_ref(ref); 3399acce952bSliubo 3400acce952bSliubo cond_resched(); 3401acce952bSliubo spin_lock(&delayed_refs->lock); 3402acce952bSliubo } 3403acce952bSliubo 3404acce952bSliubo spin_unlock(&delayed_refs->lock); 3405acce952bSliubo 3406acce952bSliubo return ret; 3407acce952bSliubo } 3408acce952bSliubo 3409143bede5SJeff Mahoney static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t) 3410acce952bSliubo { 3411acce952bSliubo struct btrfs_pending_snapshot *snapshot; 3412acce952bSliubo struct list_head splice; 3413acce952bSliubo 3414acce952bSliubo INIT_LIST_HEAD(&splice); 3415acce952bSliubo 3416acce952bSliubo list_splice_init(&t->pending_snapshots, &splice); 3417acce952bSliubo 3418acce952bSliubo while (!list_empty(&splice)) { 3419acce952bSliubo snapshot = list_entry(splice.next, 3420acce952bSliubo struct btrfs_pending_snapshot, 3421acce952bSliubo list); 3422acce952bSliubo 3423acce952bSliubo list_del_init(&snapshot->list); 3424acce952bSliubo 3425acce952bSliubo kfree(snapshot); 3426acce952bSliubo } 3427acce952bSliubo } 3428acce952bSliubo 3429143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) 3430acce952bSliubo { 3431acce952bSliubo struct btrfs_inode *btrfs_inode; 3432acce952bSliubo struct list_head splice; 3433acce952bSliubo 3434acce952bSliubo INIT_LIST_HEAD(&splice); 3435acce952bSliubo 3436acce952bSliubo spin_lock(&root->fs_info->delalloc_lock); 34375be76758SDavid Sterba list_splice_init(&root->fs_info->delalloc_inodes, &splice); 3438acce952bSliubo 3439acce952bSliubo while (!list_empty(&splice)) { 3440acce952bSliubo btrfs_inode = list_entry(splice.next, struct btrfs_inode, 3441acce952bSliubo delalloc_inodes); 3442acce952bSliubo 3443acce952bSliubo list_del_init(&btrfs_inode->delalloc_inodes); 3444acce952bSliubo 3445acce952bSliubo btrfs_invalidate_inodes(btrfs_inode->root); 3446acce952bSliubo } 3447acce952bSliubo 3448acce952bSliubo spin_unlock(&root->fs_info->delalloc_lock); 3449acce952bSliubo } 3450acce952bSliubo 3451acce952bSliubo static int btrfs_destroy_marked_extents(struct btrfs_root *root, 3452acce952bSliubo struct extent_io_tree *dirty_pages, 3453acce952bSliubo int mark) 3454acce952bSliubo { 3455acce952bSliubo int ret; 3456acce952bSliubo struct page *page; 3457acce952bSliubo struct inode *btree_inode = root->fs_info->btree_inode; 3458acce952bSliubo struct extent_buffer *eb; 3459acce952bSliubo u64 start = 0; 3460acce952bSliubo u64 end; 3461acce952bSliubo u64 offset; 3462acce952bSliubo unsigned long index; 3463acce952bSliubo 3464acce952bSliubo while (1) { 3465acce952bSliubo ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3466acce952bSliubo mark); 3467acce952bSliubo if (ret) 3468acce952bSliubo break; 3469acce952bSliubo 3470acce952bSliubo clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 3471acce952bSliubo while (start <= end) { 3472acce952bSliubo index = start >> PAGE_CACHE_SHIFT; 3473acce952bSliubo start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3474acce952bSliubo page = find_get_page(btree_inode->i_mapping, index); 3475acce952bSliubo if (!page) 3476acce952bSliubo continue; 3477acce952bSliubo offset = page_offset(page); 3478acce952bSliubo 3479acce952bSliubo spin_lock(&dirty_pages->buffer_lock); 3480acce952bSliubo eb = radix_tree_lookup( 3481acce952bSliubo &(&BTRFS_I(page->mapping->host)->io_tree)->buffer, 3482acce952bSliubo offset >> PAGE_CACHE_SHIFT); 3483acce952bSliubo spin_unlock(&dirty_pages->buffer_lock); 3484acce952bSliubo if (eb) { 3485acce952bSliubo ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3486acce952bSliubo &eb->bflags); 3487acce952bSliubo atomic_set(&eb->refs, 1); 3488acce952bSliubo } 3489acce952bSliubo if (PageWriteback(page)) 3490acce952bSliubo end_page_writeback(page); 3491acce952bSliubo 3492acce952bSliubo lock_page(page); 3493acce952bSliubo if (PageDirty(page)) { 3494acce952bSliubo clear_page_dirty_for_io(page); 3495acce952bSliubo spin_lock_irq(&page->mapping->tree_lock); 3496acce952bSliubo radix_tree_tag_clear(&page->mapping->page_tree, 3497acce952bSliubo page_index(page), 3498acce952bSliubo PAGECACHE_TAG_DIRTY); 3499acce952bSliubo spin_unlock_irq(&page->mapping->tree_lock); 3500acce952bSliubo } 3501acce952bSliubo 3502acce952bSliubo page->mapping->a_ops->invalidatepage(page, 0); 3503acce952bSliubo unlock_page(page); 3504acce952bSliubo } 3505acce952bSliubo } 3506acce952bSliubo 3507acce952bSliubo return ret; 3508acce952bSliubo } 3509acce952bSliubo 3510acce952bSliubo static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 3511acce952bSliubo struct extent_io_tree *pinned_extents) 3512acce952bSliubo { 3513acce952bSliubo struct extent_io_tree *unpin; 3514acce952bSliubo u64 start; 3515acce952bSliubo u64 end; 3516acce952bSliubo int ret; 3517acce952bSliubo 3518acce952bSliubo unpin = pinned_extents; 3519acce952bSliubo while (1) { 3520acce952bSliubo ret = find_first_extent_bit(unpin, 0, &start, &end, 3521acce952bSliubo EXTENT_DIRTY); 3522acce952bSliubo if (ret) 3523acce952bSliubo break; 3524acce952bSliubo 3525acce952bSliubo /* opt_discard */ 35265378e607SLi Dongyang if (btrfs_test_opt(root, DISCARD)) 35275378e607SLi Dongyang ret = btrfs_error_discard_extent(root, start, 35285378e607SLi Dongyang end + 1 - start, 35295378e607SLi Dongyang NULL); 3530acce952bSliubo 3531acce952bSliubo clear_extent_dirty(unpin, start, end, GFP_NOFS); 3532acce952bSliubo btrfs_error_unpin_extent_range(root, start, end); 3533acce952bSliubo cond_resched(); 3534acce952bSliubo } 3535acce952bSliubo 3536acce952bSliubo return 0; 3537acce952bSliubo } 3538acce952bSliubo 3539acce952bSliubo static int btrfs_cleanup_transaction(struct btrfs_root *root) 3540acce952bSliubo { 3541acce952bSliubo struct btrfs_transaction *t; 3542acce952bSliubo LIST_HEAD(list); 3543acce952bSliubo 3544acce952bSliubo WARN_ON(1); 3545acce952bSliubo 3546acce952bSliubo mutex_lock(&root->fs_info->transaction_kthread_mutex); 3547acce952bSliubo 3548a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3549acce952bSliubo list_splice_init(&root->fs_info->trans_list, &list); 3550a4abeea4SJosef Bacik root->fs_info->trans_no_join = 1; 3551a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3552a4abeea4SJosef Bacik 3553acce952bSliubo while (!list_empty(&list)) { 3554acce952bSliubo t = list_entry(list.next, struct btrfs_transaction, list); 3555acce952bSliubo if (!t) 3556acce952bSliubo break; 3557acce952bSliubo 3558acce952bSliubo btrfs_destroy_ordered_operations(root); 3559acce952bSliubo 3560acce952bSliubo btrfs_destroy_ordered_extents(root); 3561acce952bSliubo 3562acce952bSliubo btrfs_destroy_delayed_refs(t, root); 3563acce952bSliubo 3564acce952bSliubo btrfs_block_rsv_release(root, 3565acce952bSliubo &root->fs_info->trans_block_rsv, 3566acce952bSliubo t->dirty_pages.dirty_bytes); 3567acce952bSliubo 3568acce952bSliubo /* FIXME: cleanup wait for commit */ 3569acce952bSliubo t->in_commit = 1; 3570acce952bSliubo t->blocked = 1; 3571acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3572acce952bSliubo wake_up(&root->fs_info->transaction_blocked_wait); 3573acce952bSliubo 3574acce952bSliubo t->blocked = 0; 3575acce952bSliubo if (waitqueue_active(&root->fs_info->transaction_wait)) 3576acce952bSliubo wake_up(&root->fs_info->transaction_wait); 3577acce952bSliubo 3578acce952bSliubo t->commit_done = 1; 3579acce952bSliubo if (waitqueue_active(&t->commit_wait)) 3580acce952bSliubo wake_up(&t->commit_wait); 3581acce952bSliubo 3582acce952bSliubo btrfs_destroy_pending_snapshots(t); 3583acce952bSliubo 3584acce952bSliubo btrfs_destroy_delalloc_inodes(root); 3585acce952bSliubo 3586a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3587acce952bSliubo root->fs_info->running_transaction = NULL; 3588a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3589acce952bSliubo 3590acce952bSliubo btrfs_destroy_marked_extents(root, &t->dirty_pages, 3591acce952bSliubo EXTENT_DIRTY); 3592acce952bSliubo 3593acce952bSliubo btrfs_destroy_pinned_extent(root, 3594acce952bSliubo root->fs_info->pinned_extents); 3595acce952bSliubo 359613c5a93eSJosef Bacik atomic_set(&t->use_count, 0); 3597acce952bSliubo list_del_init(&t->list); 3598acce952bSliubo memset(t, 0, sizeof(*t)); 3599acce952bSliubo kmem_cache_free(btrfs_transaction_cachep, t); 3600acce952bSliubo } 3601acce952bSliubo 3602a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 3603a4abeea4SJosef Bacik root->fs_info->trans_no_join = 0; 3604a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 3605acce952bSliubo mutex_unlock(&root->fs_info->transaction_kthread_mutex); 3606acce952bSliubo 3607acce952bSliubo return 0; 3608acce952bSliubo } 3609acce952bSliubo 3610d1310b2eSChris Mason static struct extent_io_ops btree_extent_io_ops = { 36114bef0848SChris Mason .write_cache_pages_lock_hook = btree_lock_page_hook, 3612ce9adaa5SChris Mason .readpage_end_io_hook = btree_readpage_end_io_hook, 36134bb31e92SArne Jansen .readpage_io_failed_hook = btree_io_failed_hook, 36140b86a832SChris Mason .submit_bio_hook = btree_submit_bio_hook, 3615239b14b3SChris Mason /* note we're sharing with inode.c for the merge bio hook */ 3616239b14b3SChris Mason .merge_bio_hook = btrfs_merge_bio_hook, 36170da5468fSChris Mason }; 3618