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; 137ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(sectorsize); 1387c0260eeSJeff Mahoney set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 1397c0260eeSJeff Mahoney 1407c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = fs_info; 1417c0260eeSJeff Mahoney 142faa2dbf0SJosef Bacik return fs_info; 143faa2dbf0SJosef Bacik } 144faa2dbf0SJosef Bacik 1457c0260eeSJeff Mahoney void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) 146faa2dbf0SJosef Bacik { 147faa2dbf0SJosef Bacik struct radix_tree_iter iter; 148faa2dbf0SJosef Bacik void **slot; 149b3ad2c17SNikolay Borisov struct btrfs_device *dev, *tmp; 150faa2dbf0SJosef Bacik 1517c0260eeSJeff Mahoney if (!fs_info) 1527c0260eeSJeff Mahoney return; 1537c0260eeSJeff Mahoney 1547c0260eeSJeff Mahoney if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, 1557c0260eeSJeff Mahoney &fs_info->fs_state))) 1567c0260eeSJeff Mahoney return; 1577c0260eeSJeff Mahoney 1587c0260eeSJeff Mahoney test_mnt->mnt_sb->s_fs_info = NULL; 1597c0260eeSJeff Mahoney 160faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 161faa2dbf0SJosef Bacik radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) { 162faa2dbf0SJosef Bacik struct extent_buffer *eb; 163faa2dbf0SJosef Bacik 164f1e3c289SSasha Levin eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock); 165faa2dbf0SJosef Bacik if (!eb) 166faa2dbf0SJosef Bacik continue; 167faa2dbf0SJosef Bacik /* Shouldn't happen but that kind of thinking creates CVE's */ 168faa2dbf0SJosef Bacik if (radix_tree_exception(eb)) { 169faa2dbf0SJosef Bacik if (radix_tree_deref_retry(eb)) 170c28f2420SMatthew Wilcox slot = radix_tree_iter_retry(&iter); 171faa2dbf0SJosef Bacik continue; 172faa2dbf0SJosef Bacik } 173148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 174faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 175faa2dbf0SJosef Bacik free_extent_buffer_stale(eb); 176faa2dbf0SJosef Bacik spin_lock(&fs_info->buffer_lock); 177faa2dbf0SJosef Bacik } 178faa2dbf0SJosef Bacik spin_unlock(&fs_info->buffer_lock); 179faa2dbf0SJosef Bacik 180b3ad2c17SNikolay Borisov btrfs_mapping_tree_free(&fs_info->mapping_tree); 181b3ad2c17SNikolay Borisov list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices, 182b3ad2c17SNikolay Borisov dev_list) { 183b3ad2c17SNikolay Borisov btrfs_free_dummy_device(dev); 184b3ad2c17SNikolay Borisov } 185faa2dbf0SJosef Bacik btrfs_free_qgroup_config(fs_info); 186faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info); 187faa2dbf0SJosef Bacik kfree(fs_info->super_copy); 188bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info); 1898c38938cSJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info); 190faa2dbf0SJosef Bacik kfree(fs_info->fs_devices); 191faa2dbf0SJosef Bacik kfree(fs_info); 192faa2dbf0SJosef Bacik } 193faa2dbf0SJosef Bacik 194faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root) 195faa2dbf0SJosef Bacik { 196faa2dbf0SJosef Bacik if (!root) 197faa2dbf0SJosef Bacik return; 1987c0260eeSJeff Mahoney /* Will be freed by btrfs_free_fs_roots */ 1997c0260eeSJeff Mahoney if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state))) 2007c0260eeSJeff Mahoney return; 20100246528SJosef Bacik btrfs_put_root(root); 202faa2dbf0SJosef Bacik } 203faa2dbf0SJosef Bacik 20432da5386SDavid Sterba struct btrfs_block_group * 205da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, 206da17066cSJeff Mahoney unsigned long length) 2077c55ee0cSOmar Sandoval { 20832da5386SDavid Sterba struct btrfs_block_group *cache; 2097c55ee0cSOmar Sandoval 2108cce83baSDavid Sterba cache = kzalloc(sizeof(*cache), GFP_KERNEL); 2117c55ee0cSOmar Sandoval if (!cache) 2127c55ee0cSOmar Sandoval return NULL; 2137c55ee0cSOmar Sandoval cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 2148cce83baSDavid Sterba GFP_KERNEL); 2157c55ee0cSOmar Sandoval if (!cache->free_space_ctl) { 2167c55ee0cSOmar Sandoval kfree(cache); 2177c55ee0cSOmar Sandoval return NULL; 2187c55ee0cSOmar Sandoval } 2197c55ee0cSOmar Sandoval 220b3470b5dSDavid Sterba cache->start = 0; 221b3470b5dSDavid Sterba cache->length = length; 222da17066cSJeff Mahoney cache->full_stripe_len = fs_info->sectorsize; 223da17066cSJeff Mahoney cache->fs_info = fs_info; 2247c55ee0cSOmar Sandoval 2257c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->list); 2267c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->cluster_list); 2277c55ee0cSOmar Sandoval INIT_LIST_HEAD(&cache->bg_list); 228*cd79909bSJosef Bacik btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 2297c55ee0cSOmar Sandoval mutex_init(&cache->free_space_lock); 2307c55ee0cSOmar Sandoval 2317c55ee0cSOmar Sandoval return cache; 2327c55ee0cSOmar Sandoval } 2337c55ee0cSOmar Sandoval 23432da5386SDavid Sterba void btrfs_free_dummy_block_group(struct btrfs_block_group *cache) 2357c55ee0cSOmar Sandoval { 2367c55ee0cSOmar Sandoval if (!cache) 2377c55ee0cSOmar Sandoval return; 2387c55ee0cSOmar Sandoval __btrfs_remove_free_space_cache(cache->free_space_ctl); 2397c55ee0cSOmar Sandoval kfree(cache->free_space_ctl); 2407c55ee0cSOmar Sandoval kfree(cache); 2417c55ee0cSOmar Sandoval } 2427c55ee0cSOmar Sandoval 243483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 244483bce06SNikolay Borisov struct btrfs_fs_info *fs_info) 2457c55ee0cSOmar Sandoval { 2467c55ee0cSOmar Sandoval memset(trans, 0, sizeof(*trans)); 2477c55ee0cSOmar Sandoval trans->transid = 1; 2487c55ee0cSOmar Sandoval trans->type = __TRANS_DUMMY; 249483bce06SNikolay Borisov trans->fs_info = fs_info; 2507c55ee0cSOmar Sandoval } 2518632daaeSJeff Mahoney 2528632daaeSJeff Mahoney int btrfs_run_sanity_tests(void) 2538632daaeSJeff Mahoney { 2548632daaeSJeff Mahoney int ret, i; 2558632daaeSJeff Mahoney u32 sectorsize, nodesize; 2568632daaeSJeff Mahoney u32 test_sectorsize[] = { 2578632daaeSJeff Mahoney PAGE_SIZE, 2588632daaeSJeff Mahoney }; 2598632daaeSJeff Mahoney ret = btrfs_init_test_fs(); 2608632daaeSJeff Mahoney if (ret) 2618632daaeSJeff Mahoney return ret; 2628632daaeSJeff Mahoney for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) { 2638632daaeSJeff Mahoney sectorsize = test_sectorsize[i]; 2648632daaeSJeff Mahoney for (nodesize = sectorsize; 2658632daaeSJeff Mahoney nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE; 2668632daaeSJeff Mahoney nodesize <<= 1) { 2678632daaeSJeff Mahoney pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n", 2688632daaeSJeff Mahoney sectorsize, nodesize); 2698632daaeSJeff Mahoney ret = btrfs_test_free_space_cache(sectorsize, nodesize); 2708632daaeSJeff Mahoney if (ret) 2718632daaeSJeff Mahoney goto out; 2728632daaeSJeff Mahoney ret = btrfs_test_extent_buffer_operations(sectorsize, 2738632daaeSJeff Mahoney nodesize); 2748632daaeSJeff Mahoney if (ret) 2758632daaeSJeff Mahoney goto out; 2768632daaeSJeff Mahoney ret = btrfs_test_extent_io(sectorsize, nodesize); 2778632daaeSJeff Mahoney if (ret) 2788632daaeSJeff Mahoney goto out; 2798632daaeSJeff Mahoney ret = btrfs_test_inodes(sectorsize, nodesize); 2808632daaeSJeff Mahoney if (ret) 2818632daaeSJeff Mahoney goto out; 2828632daaeSJeff Mahoney ret = btrfs_test_qgroups(sectorsize, nodesize); 2838632daaeSJeff Mahoney if (ret) 2848632daaeSJeff Mahoney goto out; 2858632daaeSJeff Mahoney ret = btrfs_test_free_space_tree(sectorsize, nodesize); 2868632daaeSJeff Mahoney if (ret) 2878632daaeSJeff Mahoney goto out; 2888632daaeSJeff Mahoney } 2898632daaeSJeff Mahoney } 29072b28077SLiu Bo ret = btrfs_test_extent_map(); 2917a61f880SColin Ian King 2928632daaeSJeff Mahoney out: 2938632daaeSJeff Mahoney btrfs_destroy_test_fs(); 2948632daaeSJeff Mahoney return ret; 2958632daaeSJeff Mahoney } 296