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); 589f7fec0bSFilipe Manana if (inode) 599f7fec0bSFilipe Manana inode_init_owner(inode, NULL, S_IFREG); 609f7fec0bSFilipe Manana 619f7fec0bSFilipe Manana return inode; 62294e30feSJosef Bacik } 63294e30feSJosef Bacik 648632daaeSJeff Mahoney static int btrfs_init_test_fs(void) 65294e30feSJosef Bacik { 66294e30feSJosef Bacik int ret; 67294e30feSJosef Bacik 68294e30feSJosef Bacik ret = register_filesystem(&test_type); 69294e30feSJosef Bacik if (ret) { 70294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot register test file system\n"); 71294e30feSJosef Bacik return ret; 72294e30feSJosef Bacik } 73294e30feSJosef Bacik 74294e30feSJosef Bacik test_mnt = kern_mount(&test_type); 75294e30feSJosef Bacik if (IS_ERR(test_mnt)) { 76294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot mount test file system\n"); 77294e30feSJosef Bacik unregister_filesystem(&test_type); 7804e1b65aSWei Yongjun return PTR_ERR(test_mnt); 79294e30feSJosef Bacik } 80294e30feSJosef Bacik return 0; 81294e30feSJosef Bacik } 82294e30feSJosef Bacik 838632daaeSJeff Mahoney static void btrfs_destroy_test_fs(void) 84294e30feSJosef Bacik { 85294e30feSJosef Bacik kern_unmount(test_mnt); 86294e30feSJosef Bacik unregister_filesystem(&test_type); 87294e30feSJosef Bacik } 88faa2dbf0SJosef Bacik 89b3ad2c17SNikolay Borisov struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info) 90b3ad2c17SNikolay Borisov { 91b3ad2c17SNikolay Borisov struct btrfs_device *dev; 92b3ad2c17SNikolay Borisov 93b3ad2c17SNikolay Borisov dev = kzalloc(sizeof(*dev), GFP_KERNEL); 94b3ad2c17SNikolay Borisov if (!dev) 95b3ad2c17SNikolay Borisov return ERR_PTR(-ENOMEM); 96b3ad2c17SNikolay Borisov 97b3ad2c17SNikolay Borisov extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); 98b3ad2c17SNikolay Borisov INIT_LIST_HEAD(&dev->dev_list); 99b3ad2c17SNikolay Borisov list_add(&dev->dev_list, &fs_info->fs_devices->devices); 100b3ad2c17SNikolay Borisov 101b3ad2c17SNikolay Borisov return dev; 102b3ad2c17SNikolay Borisov } 103b3ad2c17SNikolay Borisov 104b3ad2c17SNikolay Borisov static void btrfs_free_dummy_device(struct btrfs_device *dev) 105b3ad2c17SNikolay Borisov { 106b3ad2c17SNikolay Borisov extent_io_tree_release(&dev->alloc_state); 107b3ad2c17SNikolay Borisov kfree(dev); 108b3ad2c17SNikolay Borisov } 109b3ad2c17SNikolay Borisov 110da17066cSJeff Mahoney struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize) 111faa2dbf0SJosef Bacik { 112faa2dbf0SJosef Bacik struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info), 1138cce83baSDavid Sterba GFP_KERNEL); 114faa2dbf0SJosef Bacik 115faa2dbf0SJosef Bacik if (!fs_info) 116faa2dbf0SJosef Bacik return fs_info; 117faa2dbf0SJosef Bacik fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices), 1188cce83baSDavid Sterba GFP_KERNEL); 119faa2dbf0SJosef Bacik if (!fs_info->fs_devices) { 120faa2dbf0SJosef Bacik kfree(fs_info); 121faa2dbf0SJosef Bacik return NULL; 122faa2dbf0SJosef Bacik } 1238260edbaSJosef Bacik INIT_LIST_HEAD(&fs_info->fs_devices->devices); 1248260edbaSJosef Bacik 125faa2dbf0SJosef Bacik fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block), 1268cce83baSDavid Sterba GFP_KERNEL); 127faa2dbf0SJosef Bacik if (!fs_info->super_copy) { 128faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 129faa2dbf0SJosef Bacik kfree(fs_info); 130faa2dbf0SJosef Bacik return NULL; 131faa2dbf0SJosef Bacik } 132faa2dbf0SJosef Bacik 1338260edbaSJosef Bacik btrfs_init_fs_info(fs_info); 1348260edbaSJosef Bacik 135da17066cSJeff Mahoney fs_info->nodesize = nodesize; 136da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 137da17066cSJeff Mahoney 138faa2dbf0SJosef Bacik if (init_srcu_struct(&fs_info->subvol_srcu)) { 139faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 140faa2dbf0SJosef Bacik kfree(fs_info->super_copy); 141faa2dbf0SJosef Bacik kfree(fs_info); 142faa2dbf0SJosef Bacik return NULL; 143faa2dbf0SJosef Bacik } 144faa2dbf0SJosef Bacik 1457c0260eeSJeff Mahoney set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1467c0260eeSJeff Mahoney 1477c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = fs_info; 1487c0260eeSJeff Mahoney 149faa2dbf0SJosef Bacik return fs_info; 150faa2dbf0SJosef Bacik } 151faa2dbf0SJosef Bacik 1527c0260eeSJeff Mahoney void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) 153faa2dbf0SJosef Bacik { 154faa2dbf0SJosef Bacik struct radix_tree_iter iter; 155faa2dbf0SJosef Bacik void **slot; 156b3ad2c17SNikolay Borisov struct btrfs_device *dev, *tmp; 157faa2dbf0SJosef Bacik 1587c0260eeSJeff Mahoney if (!fs_info) 1597c0260eeSJeff Mahoney return; 1607c0260eeSJeff Mahoney 1617c0260eeSJeff Mahoney if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, 1627c0260eeSJeff Mahoney &fs_info->fs_state))) 1637c0260eeSJeff Mahoney return; 1647c0260eeSJeff Mahoney 1657c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = NULL; 1667c0260eeSJeff Mahoney 167faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 168faa2dbf0SJosef Bacik radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) { 169faa2dbf0SJosef Bacik struct extent_buffer *eb; 170faa2dbf0SJosef Bacik 171f1e3c289SSasha Levin eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock); 172faa2dbf0SJosef Bacik if (!eb) 173faa2dbf0SJosef Bacik continue; 174faa2dbf0SJosef Bacik /* Shouldn't happen but that kind of thinking creates CVE's */ 175faa2dbf0SJosef Bacik if (radix_tree_exception(eb)) { 176faa2dbf0SJosef Bacik if (radix_tree_deref_retry(eb)) 177c28f2420SMatthew Wilcox slot = radix_tree_iter_retry(&iter); 178faa2dbf0SJosef Bacik continue; 179faa2dbf0SJosef Bacik } 180148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 181faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 182faa2dbf0SJosef Bacik free_extent_buffer_stale(eb); 183faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 184faa2dbf0SJosef Bacik } 185faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 186faa2dbf0SJosef Bacik 187b3ad2c17SNikolay Borisov btrfs_mapping_tree_free(&fs_info->mapping_tree); 188b3ad2c17SNikolay Borisov list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices, 189b3ad2c17SNikolay Borisov dev_list) { 190b3ad2c17SNikolay Borisov btrfs_free_dummy_device(dev); 191b3ad2c17SNikolay Borisov } 192faa2dbf0SJosef Bacik btrfs_free_qgroup_config(fs_info); 193faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 194faa2dbf0SJosef Bacik cleanup_srcu_struct(&fs_info->subvol_srcu); 195faa2dbf0SJosef Bacik kfree(fs_info->super_copy); 196bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 197faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 198faa2dbf0SJosef Bacik kfree(fs_info); 199faa2dbf0SJosef Bacik } 200faa2dbf0SJosef Bacik 201faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root) 202faa2dbf0SJosef Bacik { 203faa2dbf0SJosef Bacik if (!root) 204faa2dbf0SJosef Bacik return; 2057c0260eeSJeff Mahoney /* Will be freed by btrfs_free_fs_roots */ 2067c0260eeSJeff Mahoney if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state))) 2077c0260eeSJeff Mahoney return; 2089cfc8ba7SNikolay Borisov if (root->node) { 2099cfc8ba7SNikolay Borisov /* One for allocate_extent_buffer */ 210faa2dbf0SJosef Bacik free_extent_buffer(root->node); 2119cfc8ba7SNikolay Borisov } 212*00246528SJosef Bacik btrfs_put_root(root); 213faa2dbf0SJosef Bacik } 214faa2dbf0SJosef Bacik 21532da5386SDavid Sterba struct btrfs_block_group * 216da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, 217da17066cSJeff Mahoney unsigned long length) 2187c55ee0cSOmar Sandoval { 21932da5386SDavid Sterba struct btrfs_block_group *cache; 2207c55ee0cSOmar Sandoval 2218cce83baSDavid Sterba cache = kzalloc(sizeof(*cache), GFP_KERNEL); 2227c55ee0cSOmar Sandoval if (!cache) 2237c55ee0cSOmar Sandoval return NULL; 2247c55ee0cSOmar Sandoval cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 2258cce83baSDavid Sterba GFP_KERNEL); 2267c55ee0cSOmar Sandoval if (!cache->free_space_ctl) { 2277c55ee0cSOmar Sandoval kfree(cache); 2287c55ee0cSOmar Sandoval return NULL; 2297c55ee0cSOmar Sandoval } 2307c55ee0cSOmar Sandoval 231b3470b5dSDavid Sterba cache->start = 0; 232b3470b5dSDavid Sterba cache->length = length; 233da17066cSJeff Mahoney cache->full_stripe_len = fs_info->sectorsize; 234da17066cSJeff Mahoney cache->fs_info = fs_info; 2357c55ee0cSOmar Sandoval 2367c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->list); 2377c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->cluster_list); 2387c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->bg_list); 2397c55ee0cSOmar Sandoval btrfs_init_free_space_ctl(cache); 2407c55ee0cSOmar Sandoval mutex_init(&cache->free_space_lock); 2417c55ee0cSOmar Sandoval 2427c55ee0cSOmar Sandoval return cache; 2437c55ee0cSOmar Sandoval } 2447c55ee0cSOmar Sandoval 24532da5386SDavid Sterba void btrfs_free_dummy_block_group(struct btrfs_block_group *cache) 2467c55ee0cSOmar Sandoval { 2477c55ee0cSOmar Sandoval if (!cache) 2487c55ee0cSOmar Sandoval return; 2497c55ee0cSOmar Sandoval __btrfs_remove_free_space_cache(cache->free_space_ctl); 2507c55ee0cSOmar Sandoval kfree(cache->free_space_ctl); 2517c55ee0cSOmar Sandoval kfree(cache); 2527c55ee0cSOmar Sandoval } 2537c55ee0cSOmar Sandoval 254483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 255483bce06SNikolay Borisov struct btrfs_fs_info *fs_info) 2567c55ee0cSOmar Sandoval { 2577c55ee0cSOmar Sandoval memset(trans, 0, sizeof(*trans)); 2587c55ee0cSOmar Sandoval trans->transid = 1; 2597c55ee0cSOmar Sandoval trans->type = __TRANS_DUMMY; 260483bce06SNikolay Borisov trans->fs_info = fs_info; 2617c55ee0cSOmar Sandoval } 2628632daaeSJeff Mahoney 2638632daaeSJeff Mahoney int btrfs_run_sanity_tests(void) 2648632daaeSJeff Mahoney { 2658632daaeSJeff Mahoney int ret, i; 2668632daaeSJeff Mahoney u32 sectorsize, nodesize; 2678632daaeSJeff Mahoney u32 test_sectorsize[] = { 2688632daaeSJeff Mahoney PAGE_SIZE, 2698632daaeSJeff Mahoney }; 2708632daaeSJeff Mahoney ret = btrfs_init_test_fs(); 2718632daaeSJeff Mahoney if (ret) 2728632daaeSJeff Mahoney return ret; 2738632daaeSJeff Mahoney for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) { 2748632daaeSJeff Mahoney sectorsize = test_sectorsize[i]; 2758632daaeSJeff Mahoney for (nodesize = sectorsize; 2768632daaeSJeff Mahoney nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE; 2778632daaeSJeff Mahoney nodesize <<= 1) { 2788632daaeSJeff Mahoney pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n", 2798632daaeSJeff Mahoney sectorsize, nodesize); 2808632daaeSJeff Mahoney ret = btrfs_test_free_space_cache(sectorsize, nodesize); 2818632daaeSJeff Mahoney if (ret) 2828632daaeSJeff Mahoney goto out; 2838632daaeSJeff Mahoney ret = btrfs_test_extent_buffer_operations(sectorsize, 2848632daaeSJeff Mahoney nodesize); 2858632daaeSJeff Mahoney if (ret) 2868632daaeSJeff Mahoney goto out; 2878632daaeSJeff Mahoney ret = btrfs_test_extent_io(sectorsize, nodesize); 2888632daaeSJeff Mahoney if (ret) 2898632daaeSJeff Mahoney goto out; 2908632daaeSJeff Mahoney ret = btrfs_test_inodes(sectorsize, nodesize); 2918632daaeSJeff Mahoney if (ret) 2928632daaeSJeff Mahoney goto out; 2938632daaeSJeff Mahoney ret = btrfs_test_qgroups(sectorsize, nodesize); 2948632daaeSJeff Mahoney if (ret) 2958632daaeSJeff Mahoney goto out; 2968632daaeSJeff Mahoney ret = btrfs_test_free_space_tree(sectorsize, nodesize); 2978632daaeSJeff Mahoney if (ret) 2988632daaeSJeff Mahoney goto out; 2998632daaeSJeff Mahoney } 3008632daaeSJeff Mahoney } 30172b28077SLiu Bo ret = btrfs_test_extent_map(); 3027a61f880SColin Ian King 3038632daaeSJeff Mahoney out: 3048632daaeSJeff Mahoney btrfs_destroy_test_fs(); 3058632daaeSJeff Mahoney return ret; 3068632daaeSJeff Mahoney } 307