1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2294e30feSJosef Bacik /* 3294e30feSJosef Bacik * Copyright (C) 2013 Fusion IO. All rights reserved. 4294e30feSJosef Bacik */ 5294e30feSJosef Bacik 6294e30feSJosef Bacik #include <linux/fs.h> 7294e30feSJosef Bacik #include <linux/mount.h> 8389e22fbSDavid Howells #include <linux/pseudo_fs.h> 9294e30feSJosef Bacik #include <linux/magic.h> 10294e30feSJosef Bacik #include "btrfs-tests.h" 11294e30feSJosef Bacik #include "../ctree.h" 127c55ee0cSOmar Sandoval #include "../free-space-cache.h" 137c55ee0cSOmar Sandoval #include "../free-space-tree.h" 147c55ee0cSOmar Sandoval #include "../transaction.h" 15faa2dbf0SJosef Bacik #include "../volumes.h" 16faa2dbf0SJosef Bacik #include "../disk-io.h" 17faa2dbf0SJosef Bacik #include "../qgroup.h" 18aac0023cSJosef Bacik #include "../block-group.h" 19294e30feSJosef Bacik 20294e30feSJosef Bacik static struct vfsmount *test_mnt = NULL; 21294e30feSJosef Bacik 22703de426SDavid Sterba const char *test_error[] = { 23703de426SDavid Sterba [TEST_ALLOC_FS_INFO] = "cannot allocate fs_info", 24703de426SDavid Sterba [TEST_ALLOC_ROOT] = "cannot allocate root", 25703de426SDavid Sterba [TEST_ALLOC_EXTENT_BUFFER] = "cannot extent buffer", 26703de426SDavid Sterba [TEST_ALLOC_PATH] = "cannot allocate path", 27703de426SDavid Sterba [TEST_ALLOC_INODE] = "cannot allocate inode", 28703de426SDavid Sterba [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group", 29703de426SDavid Sterba [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map", 30703de426SDavid Sterba }; 31703de426SDavid Sterba 32aaedb55bSJosef Bacik static const struct super_operations btrfs_test_super_ops = { 33aaedb55bSJosef Bacik .alloc_inode = btrfs_alloc_inode, 34aaedb55bSJosef Bacik .destroy_inode = btrfs_test_destroy_inode, 35aaedb55bSJosef Bacik }; 36aaedb55bSJosef Bacik 37389e22fbSDavid Howells 38389e22fbSDavid Howells static int btrfs_test_init_fs_context(struct fs_context *fc) 39294e30feSJosef Bacik { 40389e22fbSDavid Howells struct pseudo_fs_context *ctx = init_pseudo(fc, BTRFS_TEST_MAGIC); 41389e22fbSDavid Howells if (!ctx) 42389e22fbSDavid Howells return -ENOMEM; 43389e22fbSDavid Howells ctx->ops = &btrfs_test_super_ops; 44389e22fbSDavid Howells return 0; 45294e30feSJosef Bacik } 46294e30feSJosef Bacik 47294e30feSJosef Bacik static struct file_system_type test_type = { 48294e30feSJosef Bacik .name = "btrfs_test_fs", 49389e22fbSDavid Howells .init_fs_context = btrfs_test_init_fs_context, 50294e30feSJosef Bacik .kill_sb = kill_anon_super, 51294e30feSJosef Bacik }; 52294e30feSJosef Bacik 53294e30feSJosef Bacik struct inode *btrfs_new_test_inode(void) 54294e30feSJosef Bacik { 559f7fec0bSFilipe Manana struct inode *inode; 569f7fec0bSFilipe Manana 579f7fec0bSFilipe Manana inode = new_inode(test_mnt->mnt_sb); 58*675a4fc8SJosef Bacik if (!inode) 59*675a4fc8SJosef Bacik return NULL; 60*675a4fc8SJosef Bacik 61*675a4fc8SJosef Bacik inode->i_mode = S_IFREG; 62*675a4fc8SJosef Bacik BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 63*675a4fc8SJosef Bacik BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 64*675a4fc8SJosef Bacik BTRFS_I(inode)->location.offset = 0; 659f7fec0bSFilipe Manana inode_init_owner(inode, NULL, S_IFREG); 669f7fec0bSFilipe Manana 679f7fec0bSFilipe Manana return inode; 68294e30feSJosef Bacik } 69294e30feSJosef Bacik 708632daaeSJeff Mahoney static int btrfs_init_test_fs(void) 71294e30feSJosef Bacik { 72294e30feSJosef Bacik int ret; 73294e30feSJosef Bacik 74294e30feSJosef Bacik ret = register_filesystem(&test_type); 75294e30feSJosef Bacik if (ret) { 76294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot register test file system\n"); 77294e30feSJosef Bacik return ret; 78294e30feSJosef Bacik } 79294e30feSJosef Bacik 80294e30feSJosef Bacik test_mnt = kern_mount(&test_type); 81294e30feSJosef Bacik if (IS_ERR(test_mnt)) { 82294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot mount test file system\n"); 83294e30feSJosef Bacik unregister_filesystem(&test_type); 8404e1b65aSWei Yongjun return PTR_ERR(test_mnt); 85294e30feSJosef Bacik } 86294e30feSJosef Bacik return 0; 87294e30feSJosef Bacik } 88294e30feSJosef Bacik 898632daaeSJeff Mahoney static void btrfs_destroy_test_fs(void) 90294e30feSJosef Bacik { 91294e30feSJosef Bacik kern_unmount(test_mnt); 92294e30feSJosef Bacik unregister_filesystem(&test_type); 93294e30feSJosef Bacik } 94faa2dbf0SJosef Bacik 95b3ad2c17SNikolay Borisov struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info) 96b3ad2c17SNikolay Borisov { 97b3ad2c17SNikolay Borisov struct btrfs_device *dev; 98b3ad2c17SNikolay Borisov 99b3ad2c17SNikolay Borisov dev = kzalloc(sizeof(*dev), GFP_KERNEL); 100b3ad2c17SNikolay Borisov if (!dev) 101b3ad2c17SNikolay Borisov return ERR_PTR(-ENOMEM); 102b3ad2c17SNikolay Borisov 103b3ad2c17SNikolay Borisov extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); 104b3ad2c17SNikolay Borisov INIT_LIST_HEAD(&dev->dev_list); 105b3ad2c17SNikolay Borisov list_add(&dev->dev_list, &fs_info->fs_devices->devices); 106b3ad2c17SNikolay Borisov 107b3ad2c17SNikolay Borisov return dev; 108b3ad2c17SNikolay Borisov } 109b3ad2c17SNikolay Borisov 110b3ad2c17SNikolay Borisov static void btrfs_free_dummy_device(struct btrfs_device *dev) 111b3ad2c17SNikolay Borisov { 112b3ad2c17SNikolay Borisov extent_io_tree_release(&dev->alloc_state); 113b3ad2c17SNikolay Borisov kfree(dev); 114b3ad2c17SNikolay Borisov } 115b3ad2c17SNikolay Borisov 116da17066cSJeff Mahoney struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize) 117faa2dbf0SJosef Bacik { 118faa2dbf0SJosef Bacik struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info), 1198cce83baSDavid Sterba GFP_KERNEL); 120faa2dbf0SJosef Bacik 121faa2dbf0SJosef Bacik if (!fs_info) 122faa2dbf0SJosef Bacik return fs_info; 123faa2dbf0SJosef Bacik fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices), 1248cce83baSDavid Sterba GFP_KERNEL); 125faa2dbf0SJosef Bacik if (!fs_info->fs_devices) { 126faa2dbf0SJosef Bacik kfree(fs_info); 127faa2dbf0SJosef Bacik return NULL; 128faa2dbf0SJosef Bacik } 1298260edbaSJosef Bacik INIT_LIST_HEAD(&fs_info->fs_devices->devices); 1308260edbaSJosef Bacik 131faa2dbf0SJosef Bacik fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block), 1328cce83baSDavid Sterba GFP_KERNEL); 133faa2dbf0SJosef Bacik if (!fs_info->super_copy) { 134faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 135faa2dbf0SJosef Bacik kfree(fs_info); 136faa2dbf0SJosef Bacik return NULL; 137faa2dbf0SJosef Bacik } 138faa2dbf0SJosef Bacik 1398260edbaSJosef Bacik btrfs_init_fs_info(fs_info); 1408260edbaSJosef Bacik 141da17066cSJeff Mahoney fs_info->nodesize = nodesize; 142da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 143ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 1447c0260eeSJeff Mahoney set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1457c0260eeSJeff Mahoney 1467c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = fs_info; 1477c0260eeSJeff Mahoney 148faa2dbf0SJosef Bacik return fs_info; 149faa2dbf0SJosef Bacik } 150faa2dbf0SJosef Bacik 1517c0260eeSJeff Mahoney void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) 152faa2dbf0SJosef Bacik { 153faa2dbf0SJosef Bacik struct radix_tree_iter iter; 154faa2dbf0SJosef Bacik void **slot; 155b3ad2c17SNikolay Borisov struct btrfs_device *dev, *tmp; 156faa2dbf0SJosef Bacik 1577c0260eeSJeff Mahoney if (!fs_info) 1587c0260eeSJeff Mahoney return; 1597c0260eeSJeff Mahoney 1607c0260eeSJeff Mahoney if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, 1617c0260eeSJeff Mahoney &fs_info->fs_state))) 1627c0260eeSJeff Mahoney return; 1637c0260eeSJeff Mahoney 1647c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = NULL; 1657c0260eeSJeff Mahoney 166faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 167faa2dbf0SJosef Bacik radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) { 168faa2dbf0SJosef Bacik struct extent_buffer *eb; 169faa2dbf0SJosef Bacik 170f1e3c289SSasha Levin eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock); 171faa2dbf0SJosef Bacik if (!eb) 172faa2dbf0SJosef Bacik continue; 173faa2dbf0SJosef Bacik /* Shouldn't happen but that kind of thinking creates CVE's */ 174faa2dbf0SJosef Bacik if (radix_tree_exception(eb)) { 175faa2dbf0SJosef Bacik if (radix_tree_deref_retry(eb)) 176c28f2420SMatthew Wilcox slot = radix_tree_iter_retry(&iter); 177faa2dbf0SJosef Bacik continue; 178faa2dbf0SJosef Bacik } 179148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 180faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 181faa2dbf0SJosef Bacik free_extent_buffer_stale(eb); 182faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 183faa2dbf0SJosef Bacik } 184faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 185faa2dbf0SJosef Bacik 186b3ad2c17SNikolay Borisov btrfs_mapping_tree_free(&fs_info->mapping_tree); 187b3ad2c17SNikolay Borisov list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices, 188b3ad2c17SNikolay Borisov dev_list) { 189b3ad2c17SNikolay Borisov btrfs_free_dummy_device(dev); 190b3ad2c17SNikolay Borisov } 191faa2dbf0SJosef Bacik btrfs_free_qgroup_config(fs_info); 192faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 193faa2dbf0SJosef Bacik kfree(fs_info->super_copy); 194bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 1958c38938cSJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 196faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 197faa2dbf0SJosef Bacik kfree(fs_info); 198faa2dbf0SJosef Bacik } 199faa2dbf0SJosef Bacik 200faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root) 201faa2dbf0SJosef Bacik { 202faa2dbf0SJosef Bacik if (!root) 203faa2dbf0SJosef Bacik return; 2047c0260eeSJeff Mahoney /* Will be freed by btrfs_free_fs_roots */ 2057c0260eeSJeff Mahoney if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state))) 2067c0260eeSJeff Mahoney return; 20700246528SJosef Bacik btrfs_put_root(root); 208faa2dbf0SJosef Bacik } 209faa2dbf0SJosef Bacik 21032da5386SDavid Sterba struct btrfs_block_group * 211da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, 212da17066cSJeff Mahoney unsigned long length) 2137c55ee0cSOmar Sandoval { 21432da5386SDavid Sterba struct btrfs_block_group *cache; 2157c55ee0cSOmar Sandoval 2168cce83baSDavid Sterba cache = kzalloc(sizeof(*cache), GFP_KERNEL); 2177c55ee0cSOmar Sandoval if (!cache) 2187c55ee0cSOmar Sandoval return NULL; 2197c55ee0cSOmar Sandoval cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 2208cce83baSDavid Sterba GFP_KERNEL); 2217c55ee0cSOmar Sandoval if (!cache->free_space_ctl) { 2227c55ee0cSOmar Sandoval kfree(cache); 2237c55ee0cSOmar Sandoval return NULL; 2247c55ee0cSOmar Sandoval } 2257c55ee0cSOmar Sandoval 226b3470b5dSDavid Sterba cache->start = 0; 227b3470b5dSDavid Sterba cache->length = length; 228da17066cSJeff Mahoney cache->full_stripe_len = fs_info->sectorsize; 229da17066cSJeff Mahoney cache->fs_info = fs_info; 2307c55ee0cSOmar Sandoval 2317c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->list); 2327c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->cluster_list); 2337c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->bg_list); 234cd79909bSJosef Bacik btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 2357c55ee0cSOmar Sandoval mutex_init(&cache->free_space_lock); 2367c55ee0cSOmar Sandoval 2377c55ee0cSOmar Sandoval return cache; 2387c55ee0cSOmar Sandoval } 2397c55ee0cSOmar Sandoval 24032da5386SDavid Sterba void btrfs_free_dummy_block_group(struct btrfs_block_group *cache) 2417c55ee0cSOmar Sandoval { 2427c55ee0cSOmar Sandoval if (!cache) 2437c55ee0cSOmar Sandoval return; 2447c55ee0cSOmar Sandoval __btrfs_remove_free_space_cache(cache->free_space_ctl); 2457c55ee0cSOmar Sandoval kfree(cache->free_space_ctl); 2467c55ee0cSOmar Sandoval kfree(cache); 2477c55ee0cSOmar Sandoval } 2487c55ee0cSOmar Sandoval 249483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 250483bce06SNikolay Borisov struct btrfs_fs_info *fs_info) 2517c55ee0cSOmar Sandoval { 2527c55ee0cSOmar Sandoval memset(trans, 0, sizeof(*trans)); 2537c55ee0cSOmar Sandoval trans->transid = 1; 2547c55ee0cSOmar Sandoval trans->type = __TRANS_DUMMY; 255483bce06SNikolay Borisov trans->fs_info = fs_info; 2567c55ee0cSOmar Sandoval } 2578632daaeSJeff Mahoney 2588632daaeSJeff Mahoney int btrfs_run_sanity_tests(void) 2598632daaeSJeff Mahoney { 2608632daaeSJeff Mahoney int ret, i; 2618632daaeSJeff Mahoney u32 sectorsize, nodesize; 2628632daaeSJeff Mahoney u32 test_sectorsize[] = { 2638632daaeSJeff Mahoney PAGE_SIZE, 2648632daaeSJeff Mahoney }; 2658632daaeSJeff Mahoney ret = btrfs_init_test_fs(); 2668632daaeSJeff Mahoney if (ret) 2678632daaeSJeff Mahoney return ret; 2688632daaeSJeff Mahoney for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) { 2698632daaeSJeff Mahoney sectorsize = test_sectorsize[i]; 2708632daaeSJeff Mahoney for (nodesize = sectorsize; 2718632daaeSJeff Mahoney nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE; 2728632daaeSJeff Mahoney nodesize <<= 1) { 2738632daaeSJeff Mahoney pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n", 2748632daaeSJeff Mahoney sectorsize, nodesize); 2758632daaeSJeff Mahoney ret = btrfs_test_free_space_cache(sectorsize, nodesize); 2768632daaeSJeff Mahoney if (ret) 2778632daaeSJeff Mahoney goto out; 2788632daaeSJeff Mahoney ret = btrfs_test_extent_buffer_operations(sectorsize, 2798632daaeSJeff Mahoney nodesize); 2808632daaeSJeff Mahoney if (ret) 2818632daaeSJeff Mahoney goto out; 2828632daaeSJeff Mahoney ret = btrfs_test_extent_io(sectorsize, nodesize); 2838632daaeSJeff Mahoney if (ret) 2848632daaeSJeff Mahoney goto out; 2858632daaeSJeff Mahoney ret = btrfs_test_inodes(sectorsize, nodesize); 2868632daaeSJeff Mahoney if (ret) 2878632daaeSJeff Mahoney goto out; 2888632daaeSJeff Mahoney ret = btrfs_test_qgroups(sectorsize, nodesize); 2898632daaeSJeff Mahoney if (ret) 2908632daaeSJeff Mahoney goto out; 2918632daaeSJeff Mahoney ret = btrfs_test_free_space_tree(sectorsize, nodesize); 2928632daaeSJeff Mahoney if (ret) 2938632daaeSJeff Mahoney goto out; 2948632daaeSJeff Mahoney } 2958632daaeSJeff Mahoney } 29672b28077SLiu Bo ret = btrfs_test_extent_map(); 2977a61f880SColin Ian King 2988632daaeSJeff Mahoney out: 2998632daaeSJeff Mahoney btrfs_destroy_test_fs(); 3008632daaeSJeff Mahoney return ret; 3018632daaeSJeff Mahoney } 302