btrfs-tests.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Fusion IO. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/pseudo_fs.h>
  8. #include <linux/magic.h>
  9. #include "btrfs-tests.h"
  10. #include "../ctree.h"
  11. #include "../free-space-cache.h"
  12. #include "../free-space-tree.h"
  13. #include "../transaction.h"
  14. #include "../volumes.h"
  15. #include "../disk-io.h"
  16. #include "../qgroup.h"
  17. #include "../block-group.h"
  18. #include "../fs.h"
  19. static struct vfsmount *test_mnt = NULL;
  20. const char *test_error[] = {
  21. [TEST_ALLOC_FS_INFO] = "cannot allocate fs_info",
  22. [TEST_ALLOC_ROOT] = "cannot allocate root",
  23. [TEST_ALLOC_EXTENT_BUFFER] = "cannot extent buffer",
  24. [TEST_ALLOC_PATH] = "cannot allocate path",
  25. [TEST_ALLOC_INODE] = "cannot allocate inode",
  26. [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group",
  27. [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map",
  28. [TEST_ALLOC_CHUNK_MAP] = "cannot allocate chunk map",
  29. [TEST_ALLOC_IO_CONTEXT] = "cannot allocate io context",
  30. [TEST_ALLOC_TRANSACTION] = "cannot allocate transaction",
  31. };
  32. static const struct super_operations btrfs_test_super_ops = {
  33. .alloc_inode = btrfs_alloc_inode,
  34. .destroy_inode = btrfs_test_destroy_inode,
  35. };
  36. static int btrfs_test_init_fs_context(struct fs_context *fc)
  37. {
  38. struct pseudo_fs_context *ctx = init_pseudo(fc, BTRFS_TEST_MAGIC);
  39. if (!ctx)
  40. return -ENOMEM;
  41. ctx->ops = &btrfs_test_super_ops;
  42. return 0;
  43. }
  44. static struct file_system_type test_type = {
  45. .name = "btrfs_test_fs",
  46. .init_fs_context = btrfs_test_init_fs_context,
  47. .kill_sb = kill_anon_super,
  48. };
  49. struct inode *btrfs_new_test_inode(void)
  50. {
  51. struct inode *inode;
  52. inode = new_inode(test_mnt->mnt_sb);
  53. if (!inode)
  54. return NULL;
  55. inode->i_mode = S_IFREG;
  56. btrfs_set_inode_number(BTRFS_I(inode), BTRFS_FIRST_FREE_OBJECTID);
  57. inode_init_owner(&nop_mnt_idmap, inode, NULL, S_IFREG);
  58. return inode;
  59. }
  60. static int btrfs_init_test_fs(void)
  61. {
  62. int ret;
  63. ret = register_filesystem(&test_type);
  64. if (ret) {
  65. printk(KERN_ERR "btrfs: cannot register test file system\n");
  66. return ret;
  67. }
  68. test_mnt = kern_mount(&test_type);
  69. if (IS_ERR(test_mnt)) {
  70. printk(KERN_ERR "btrfs: cannot mount test file system\n");
  71. unregister_filesystem(&test_type);
  72. return PTR_ERR(test_mnt);
  73. }
  74. return 0;
  75. }
  76. static void btrfs_destroy_test_fs(void)
  77. {
  78. kern_unmount(test_mnt);
  79. unregister_filesystem(&test_type);
  80. }
  81. struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info)
  82. {
  83. struct btrfs_device *dev;
  84. dev = kzalloc_obj(*dev);
  85. if (!dev)
  86. return ERR_PTR(-ENOMEM);
  87. btrfs_extent_io_tree_init(fs_info, &dev->alloc_state, 0);
  88. INIT_LIST_HEAD(&dev->dev_list);
  89. list_add(&dev->dev_list, &fs_info->fs_devices->devices);
  90. return dev;
  91. }
  92. static void btrfs_free_dummy_device(struct btrfs_device *dev)
  93. {
  94. btrfs_extent_io_tree_release(&dev->alloc_state);
  95. kfree(dev);
  96. }
  97. struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
  98. {
  99. struct btrfs_fs_info *fs_info = kzalloc_obj(struct btrfs_fs_info);
  100. if (!fs_info)
  101. return fs_info;
  102. fs_info->fs_devices = kzalloc_obj(struct btrfs_fs_devices);
  103. if (!fs_info->fs_devices) {
  104. kfree(fs_info);
  105. return NULL;
  106. }
  107. INIT_LIST_HEAD(&fs_info->fs_devices->devices);
  108. fs_info->super_copy = kzalloc_obj(struct btrfs_super_block);
  109. if (!fs_info->super_copy) {
  110. kfree(fs_info->fs_devices);
  111. kfree(fs_info);
  112. return NULL;
  113. }
  114. btrfs_init_fs_info(fs_info);
  115. fs_info->nodesize = nodesize;
  116. fs_info->sectorsize = sectorsize;
  117. fs_info->sectorsize_bits = ilog2(sectorsize);
  118. /* CRC32C csum size. */
  119. fs_info->csum_size = 4;
  120. fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) /
  121. fs_info->csum_size;
  122. set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  123. test_mnt->mnt_sb->s_fs_info = fs_info;
  124. return fs_info;
  125. }
  126. void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
  127. {
  128. struct btrfs_device *dev, *tmp;
  129. struct extent_buffer *eb;
  130. unsigned long index;
  131. if (!fs_info)
  132. return;
  133. if (WARN_ON(!btrfs_is_testing(fs_info)))
  134. return;
  135. test_mnt->mnt_sb->s_fs_info = NULL;
  136. xa_lock_irq(&fs_info->buffer_tree);
  137. xa_for_each(&fs_info->buffer_tree, index, eb) {
  138. xa_unlock_irq(&fs_info->buffer_tree);
  139. free_extent_buffer(eb);
  140. xa_lock_irq(&fs_info->buffer_tree);
  141. }
  142. xa_unlock_irq(&fs_info->buffer_tree);
  143. btrfs_mapping_tree_free(fs_info);
  144. list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
  145. dev_list) {
  146. btrfs_free_dummy_device(dev);
  147. }
  148. btrfs_free_qgroup_config(fs_info);
  149. btrfs_free_fs_roots(fs_info);
  150. kfree(fs_info->super_copy);
  151. btrfs_check_leaked_roots(fs_info);
  152. btrfs_extent_buffer_leak_debug_check(fs_info);
  153. kfree(fs_info->fs_devices);
  154. kfree(fs_info);
  155. }
  156. void btrfs_free_dummy_root(struct btrfs_root *root)
  157. {
  158. if (IS_ERR_OR_NULL(root))
  159. return;
  160. /* Will be freed by btrfs_free_fs_roots */
  161. if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
  162. return;
  163. btrfs_global_root_delete(root);
  164. btrfs_put_root(root);
  165. }
  166. struct btrfs_block_group *
  167. btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
  168. unsigned long length)
  169. {
  170. struct btrfs_block_group *cache;
  171. cache = kzalloc_obj(*cache);
  172. if (!cache)
  173. return NULL;
  174. cache->free_space_ctl = kzalloc_obj(*cache->free_space_ctl);
  175. if (!cache->free_space_ctl) {
  176. kfree(cache);
  177. return NULL;
  178. }
  179. cache->start = 0;
  180. cache->length = length;
  181. cache->full_stripe_len = fs_info->sectorsize;
  182. cache->fs_info = fs_info;
  183. INIT_LIST_HEAD(&cache->list);
  184. INIT_LIST_HEAD(&cache->cluster_list);
  185. INIT_LIST_HEAD(&cache->bg_list);
  186. btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
  187. mutex_init(&cache->free_space_lock);
  188. return cache;
  189. }
  190. void btrfs_free_dummy_block_group(struct btrfs_block_group *cache)
  191. {
  192. if (!cache)
  193. return;
  194. btrfs_remove_free_space_cache(cache);
  195. kfree(cache->free_space_ctl);
  196. kfree(cache);
  197. }
  198. void btrfs_init_dummy_transaction(struct btrfs_transaction *trans, struct btrfs_fs_info *fs_info)
  199. {
  200. memset(trans, 0, sizeof(*trans));
  201. trans->fs_info = fs_info;
  202. xa_init(&trans->delayed_refs.head_refs);
  203. xa_init(&trans->delayed_refs.dirty_extents);
  204. spin_lock_init(&trans->delayed_refs.lock);
  205. }
  206. void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
  207. struct btrfs_fs_info *fs_info)
  208. {
  209. memset(trans, 0, sizeof(*trans));
  210. trans->transid = 1;
  211. trans->type = __TRANS_DUMMY;
  212. trans->fs_info = fs_info;
  213. }
  214. int btrfs_run_sanity_tests(void)
  215. {
  216. int ret, i;
  217. u32 sectorsize, nodesize;
  218. u32 test_sectorsize[] = {
  219. PAGE_SIZE,
  220. };
  221. ret = btrfs_init_test_fs();
  222. if (ret)
  223. return ret;
  224. for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
  225. sectorsize = test_sectorsize[i];
  226. for (nodesize = sectorsize;
  227. nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
  228. nodesize <<= 1) {
  229. pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n",
  230. sectorsize, nodesize);
  231. ret = btrfs_test_free_space_cache(sectorsize, nodesize);
  232. if (ret)
  233. goto out;
  234. ret = btrfs_test_extent_buffer_operations(sectorsize,
  235. nodesize);
  236. if (ret)
  237. goto out;
  238. ret = btrfs_test_extent_io(sectorsize, nodesize);
  239. if (ret)
  240. goto out;
  241. ret = btrfs_test_inodes(sectorsize, nodesize);
  242. if (ret)
  243. goto out;
  244. ret = btrfs_test_qgroups(sectorsize, nodesize);
  245. if (ret)
  246. goto out;
  247. ret = btrfs_test_free_space_tree(sectorsize, nodesize);
  248. if (ret)
  249. goto out;
  250. ret = btrfs_test_raid_stripe_tree(sectorsize, nodesize);
  251. if (ret)
  252. goto out;
  253. ret = btrfs_test_delayed_refs(sectorsize, nodesize);
  254. if (ret)
  255. goto out;
  256. ret = btrfs_test_chunk_allocation(sectorsize, nodesize);
  257. if (ret)
  258. goto out;
  259. }
  260. }
  261. ret = btrfs_test_extent_map();
  262. out:
  263. btrfs_destroy_test_fs();
  264. return ret;
  265. }