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); 58675a4fc8SJosef Bacik if (!inode) 59675a4fc8SJosef Bacik return NULL; 60675a4fc8SJosef Bacik 61675a4fc8SJosef Bacik inode->i_mode = S_IFREG; 62*cf2404a9SFilipe Manana inode->i_ino = BTRFS_FIRST_FREE_OBJECTID; 63675a4fc8SJosef Bacik BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 64675a4fc8SJosef Bacik BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 65675a4fc8SJosef Bacik BTRFS_I(inode)->location.offset = 0; 6621cb47beSChristian Brauner inode_init_owner(&init_user_ns, inode, NULL, S_IFREG); 679f7fec0bSFilipe Manana 689f7fec0bSFilipe Manana return inode; 69294e30feSJosef Bacik } 70294e30feSJosef Bacik 718632daaeSJeff Mahoney static int btrfs_init_test_fs(void) 72294e30feSJosef Bacik { 73294e30feSJosef Bacik int ret; 74294e30feSJosef Bacik 75294e30feSJosef Bacik ret = register_filesystem(&test_type); 76294e30feSJosef Bacik if (ret) { 77294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot register test file system\n"); 78294e30feSJosef Bacik return ret; 79294e30feSJosef Bacik } 80294e30feSJosef Bacik 81294e30feSJosef Bacik test_mnt = kern_mount(&test_type); 82294e30feSJosef Bacik if (IS_ERR(test_mnt)) { 83294e30feSJosef Bacik printk(KERN_ERR "btrfs: cannot mount test file system\n"); 84294e30feSJosef Bacik unregister_filesystem(&test_type); 8504e1b65aSWei Yongjun return PTR_ERR(test_mnt); 86294e30feSJosef Bacik } 87294e30feSJosef Bacik return 0; 88294e30feSJosef Bacik } 89294e30feSJosef Bacik 908632daaeSJeff Mahoney static void btrfs_destroy_test_fs(void) 91294e30feSJosef Bacik { 92294e30feSJosef Bacik kern_unmount(test_mnt); 93294e30feSJosef Bacik unregister_filesystem(&test_type); 94294e30feSJosef Bacik } 95faa2dbf0SJosef Bacik 96b3ad2c17SNikolay Borisov struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info) 97b3ad2c17SNikolay Borisov { 98b3ad2c17SNikolay Borisov struct btrfs_device *dev; 99b3ad2c17SNikolay Borisov 100b3ad2c17SNikolay Borisov dev = kzalloc(sizeof(*dev), GFP_KERNEL); 101b3ad2c17SNikolay Borisov if (!dev) 102b3ad2c17SNikolay Borisov return ERR_PTR(-ENOMEM); 103b3ad2c17SNikolay Borisov 104b3ad2c17SNikolay Borisov extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); 105b3ad2c17SNikolay Borisov INIT_LIST_HEAD(&dev->dev_list); 106b3ad2c17SNikolay Borisov list_add(&dev->dev_list, &fs_info->fs_devices->devices); 107b3ad2c17SNikolay Borisov 108b3ad2c17SNikolay Borisov return dev; 109b3ad2c17SNikolay Borisov } 110b3ad2c17SNikolay Borisov 111b3ad2c17SNikolay Borisov static void btrfs_free_dummy_device(struct btrfs_device *dev) 112b3ad2c17SNikolay Borisov { 113b3ad2c17SNikolay Borisov extent_io_tree_release(&dev->alloc_state); 114b3ad2c17SNikolay Borisov kfree(dev); 115b3ad2c17SNikolay Borisov } 116b3ad2c17SNikolay Borisov 117da17066cSJeff Mahoney struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize) 118faa2dbf0SJosef Bacik { 119faa2dbf0SJosef Bacik struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info), 1208cce83baSDavid Sterba GFP_KERNEL); 121faa2dbf0SJosef Bacik 122faa2dbf0SJosef Bacik if (!fs_info) 123faa2dbf0SJosef Bacik return fs_info; 124faa2dbf0SJosef Bacik fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices), 1258cce83baSDavid Sterba GFP_KERNEL); 126faa2dbf0SJosef Bacik if (!fs_info->fs_devices) { 127faa2dbf0SJosef Bacik kfree(fs_info); 128faa2dbf0SJosef Bacik return NULL; 129faa2dbf0SJosef Bacik } 1308260edbaSJosef Bacik INIT_LIST_HEAD(&fs_info->fs_devices->devices); 1318260edbaSJosef Bacik 132faa2dbf0SJosef Bacik fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block), 1338cce83baSDavid Sterba GFP_KERNEL); 134faa2dbf0SJosef Bacik if (!fs_info->super_copy) { 135faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 136faa2dbf0SJosef Bacik kfree(fs_info); 137faa2dbf0SJosef Bacik return NULL; 138faa2dbf0SJosef Bacik } 139faa2dbf0SJosef Bacik 1408260edbaSJosef Bacik btrfs_init_fs_info(fs_info); 1418260edbaSJosef Bacik 142da17066cSJeff Mahoney fs_info->nodesize = nodesize; 143da17066cSJeff Mahoney fs_info->sectorsize = sectorsize; 144ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 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 { 15401cd3909SDavid Sterba struct radix_tree_iter iter; 15501cd3909SDavid Sterba 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 16701cd3909SDavid Sterba spin_lock(&fs_info->buffer_lock); 16801cd3909SDavid Sterba radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) { 16901cd3909SDavid Sterba struct extent_buffer *eb; 17001cd3909SDavid Sterba 17101cd3909SDavid Sterba eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock); 17201cd3909SDavid Sterba if (!eb) 17301cd3909SDavid Sterba continue; 17401cd3909SDavid Sterba /* Shouldn't happen but that kind of thinking creates CVE's */ 17501cd3909SDavid Sterba if (radix_tree_exception(eb)) { 17601cd3909SDavid Sterba if (radix_tree_deref_retry(eb)) 17701cd3909SDavid Sterba slot = radix_tree_iter_retry(&iter); 17801cd3909SDavid Sterba continue; 179faa2dbf0SJosef Bacik } 18001cd3909SDavid Sterba slot = radix_tree_iter_resume(slot, &iter); 18101cd3909SDavid Sterba spin_unlock(&fs_info->buffer_lock); 18201cd3909SDavid Sterba free_extent_buffer_stale(eb); 18301cd3909SDavid Sterba spin_lock(&fs_info->buffer_lock); 18401cd3909SDavid Sterba } 18501cd3909SDavid Sterba 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 kfree(fs_info->super_copy); 195bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 1968c38938cSJosef Bacik btrfs_extent_buffer_leak_debug_check(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 */ 206fc7cbcd4SDavid Sterba if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state))) 2077c0260eeSJeff Mahoney return; 208abed4aaaSJosef Bacik btrfs_global_root_delete(root); 20900246528SJosef Bacik btrfs_put_root(root); 210faa2dbf0SJosef Bacik } 211faa2dbf0SJosef Bacik 21232da5386SDavid Sterba struct btrfs_block_group * 213da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, 214da17066cSJeff Mahoney unsigned long length) 2157c55ee0cSOmar Sandoval { 21632da5386SDavid Sterba struct btrfs_block_group *cache; 2177c55ee0cSOmar Sandoval 2188cce83baSDavid Sterba cache = kzalloc(sizeof(*cache), GFP_KERNEL); 2197c55ee0cSOmar Sandoval if (!cache) 2207c55ee0cSOmar Sandoval return NULL; 2217c55ee0cSOmar Sandoval cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 2228cce83baSDavid Sterba GFP_KERNEL); 2237c55ee0cSOmar Sandoval if (!cache->free_space_ctl) { 2247c55ee0cSOmar Sandoval kfree(cache); 2257c55ee0cSOmar Sandoval return NULL; 2267c55ee0cSOmar Sandoval } 2277c55ee0cSOmar Sandoval 228b3470b5dSDavid Sterba cache->start = 0; 229b3470b5dSDavid Sterba cache->length = length; 230da17066cSJeff Mahoney cache->full_stripe_len = fs_info->sectorsize; 231da17066cSJeff Mahoney cache->fs_info = fs_info; 2327c55ee0cSOmar Sandoval 2337c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->list); 2347c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->cluster_list); 2357c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->bg_list); 236cd79909bSJosef Bacik btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 2377c55ee0cSOmar Sandoval mutex_init(&cache->free_space_lock); 2387c55ee0cSOmar Sandoval 2397c55ee0cSOmar Sandoval return cache; 2407c55ee0cSOmar Sandoval } 2417c55ee0cSOmar Sandoval 24232da5386SDavid Sterba void btrfs_free_dummy_block_group(struct btrfs_block_group *cache) 2437c55ee0cSOmar Sandoval { 2447c55ee0cSOmar Sandoval if (!cache) 2457c55ee0cSOmar Sandoval return; 2467c55ee0cSOmar Sandoval __btrfs_remove_free_space_cache(cache->free_space_ctl); 2477c55ee0cSOmar Sandoval kfree(cache->free_space_ctl); 2487c55ee0cSOmar Sandoval kfree(cache); 2497c55ee0cSOmar Sandoval } 2507c55ee0cSOmar Sandoval 251483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 252483bce06SNikolay Borisov struct btrfs_fs_info *fs_info) 2537c55ee0cSOmar Sandoval { 2547c55ee0cSOmar Sandoval memset(trans, 0, sizeof(*trans)); 2557c55ee0cSOmar Sandoval trans->transid = 1; 2567c55ee0cSOmar Sandoval trans->type = __TRANS_DUMMY; 257483bce06SNikolay Borisov trans->fs_info = fs_info; 2587c55ee0cSOmar Sandoval } 2598632daaeSJeff Mahoney 2608632daaeSJeff Mahoney int btrfs_run_sanity_tests(void) 2618632daaeSJeff Mahoney { 2628632daaeSJeff Mahoney int ret, i; 2638632daaeSJeff Mahoney u32 sectorsize, nodesize; 2648632daaeSJeff Mahoney u32 test_sectorsize[] = { 2658632daaeSJeff Mahoney PAGE_SIZE, 2668632daaeSJeff Mahoney }; 2678632daaeSJeff Mahoney ret = btrfs_init_test_fs(); 2688632daaeSJeff Mahoney if (ret) 2698632daaeSJeff Mahoney return ret; 2708632daaeSJeff Mahoney for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) { 2718632daaeSJeff Mahoney sectorsize = test_sectorsize[i]; 2728632daaeSJeff Mahoney for (nodesize = sectorsize; 2738632daaeSJeff Mahoney nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE; 2748632daaeSJeff Mahoney nodesize <<= 1) { 2758632daaeSJeff Mahoney pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n", 2768632daaeSJeff Mahoney sectorsize, nodesize); 2778632daaeSJeff Mahoney ret = btrfs_test_free_space_cache(sectorsize, nodesize); 2788632daaeSJeff Mahoney if (ret) 2798632daaeSJeff Mahoney goto out; 2808632daaeSJeff Mahoney ret = btrfs_test_extent_buffer_operations(sectorsize, 2818632daaeSJeff Mahoney nodesize); 2828632daaeSJeff Mahoney if (ret) 2838632daaeSJeff Mahoney goto out; 2848632daaeSJeff Mahoney ret = btrfs_test_extent_io(sectorsize, nodesize); 2858632daaeSJeff Mahoney if (ret) 2868632daaeSJeff Mahoney goto out; 2878632daaeSJeff Mahoney ret = btrfs_test_inodes(sectorsize, nodesize); 2888632daaeSJeff Mahoney if (ret) 2898632daaeSJeff Mahoney goto out; 2908632daaeSJeff Mahoney ret = btrfs_test_qgroups(sectorsize, nodesize); 2918632daaeSJeff Mahoney if (ret) 2928632daaeSJeff Mahoney goto out; 2938632daaeSJeff Mahoney ret = btrfs_test_free_space_tree(sectorsize, nodesize); 2948632daaeSJeff Mahoney if (ret) 2958632daaeSJeff Mahoney goto out; 2968632daaeSJeff Mahoney } 2978632daaeSJeff Mahoney } 29872b28077SLiu Bo ret = btrfs_test_extent_map(); 2997a61f880SColin Ian King 3008632daaeSJeff Mahoney out: 3018632daaeSJeff Mahoney btrfs_destroy_test_fs(); 3028632daaeSJeff Mahoney return ret; 3038632daaeSJeff Mahoney } 304