btrfs-tests.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/magic.h>
  8. #include "btrfs-tests.h"
  9. #include "../ctree.h"
  10. #include "../free-space-cache.h"
  11. #include "../free-space-tree.h"
  12. #include "../transaction.h"
  13. #include "../volumes.h"
  14. #include "../disk-io.h"
  15. #include "../qgroup.h"
  16. static struct vfsmount *test_mnt = NULL;
  17. static const struct super_operations btrfs_test_super_ops = {
  18. .alloc_inode = btrfs_alloc_inode,
  19. .destroy_inode = btrfs_test_destroy_inode,
  20. };
  21. static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
  22. int flags, const char *dev_name,
  23. void *data)
  24. {
  25. return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
  26. NULL, BTRFS_TEST_MAGIC);
  27. }
  28. static struct file_system_type test_type = {
  29. .name = "btrfs_test_fs",
  30. .mount = btrfs_test_mount,
  31. .kill_sb = kill_anon_super,
  32. };
  33. struct inode *btrfs_new_test_inode(void)
  34. {
  35. return new_inode(test_mnt->mnt_sb);
  36. }
  37. static int btrfs_init_test_fs(void)
  38. {
  39. int ret;
  40. ret = register_filesystem(&test_type);
  41. if (ret) {
  42. printk(KERN_ERR "btrfs: cannot register test file system\n");
  43. return ret;
  44. }
  45. test_mnt = kern_mount(&test_type);
  46. if (IS_ERR(test_mnt)) {
  47. printk(KERN_ERR "btrfs: cannot mount test file system\n");
  48. unregister_filesystem(&test_type);
  49. return PTR_ERR(test_mnt);
  50. }
  51. return 0;
  52. }
  53. static void btrfs_destroy_test_fs(void)
  54. {
  55. kern_unmount(test_mnt);
  56. unregister_filesystem(&test_type);
  57. }
  58. struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
  59. {
  60. struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
  61. GFP_KERNEL);
  62. if (!fs_info)
  63. return fs_info;
  64. fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
  65. GFP_KERNEL);
  66. if (!fs_info->fs_devices) {
  67. kfree(fs_info);
  68. return NULL;
  69. }
  70. fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
  71. GFP_KERNEL);
  72. if (!fs_info->super_copy) {
  73. kfree(fs_info->fs_devices);
  74. kfree(fs_info);
  75. return NULL;
  76. }
  77. fs_info->nodesize = nodesize;
  78. fs_info->sectorsize = sectorsize;
  79. if (init_srcu_struct(&fs_info->subvol_srcu)) {
  80. kfree(fs_info->fs_devices);
  81. kfree(fs_info->super_copy);
  82. kfree(fs_info);
  83. return NULL;
  84. }
  85. spin_lock_init(&fs_info->buffer_lock);
  86. spin_lock_init(&fs_info->qgroup_lock);
  87. spin_lock_init(&fs_info->qgroup_op_lock);
  88. spin_lock_init(&fs_info->super_lock);
  89. spin_lock_init(&fs_info->fs_roots_radix_lock);
  90. spin_lock_init(&fs_info->tree_mod_seq_lock);
  91. mutex_init(&fs_info->qgroup_ioctl_lock);
  92. mutex_init(&fs_info->qgroup_rescan_lock);
  93. rwlock_init(&fs_info->tree_mod_log_lock);
  94. fs_info->running_transaction = NULL;
  95. fs_info->qgroup_tree = RB_ROOT;
  96. fs_info->qgroup_ulist = NULL;
  97. atomic64_set(&fs_info->tree_mod_seq, 0);
  98. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  99. INIT_LIST_HEAD(&fs_info->dead_roots);
  100. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  101. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  102. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  103. extent_io_tree_init(&fs_info->freed_extents[0], NULL);
  104. extent_io_tree_init(&fs_info->freed_extents[1], NULL);
  105. fs_info->pinned_extents = &fs_info->freed_extents[0];
  106. set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  107. test_mnt->mnt_sb->s_fs_info = fs_info;
  108. return fs_info;
  109. }
  110. void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
  111. {
  112. struct radix_tree_iter iter;
  113. void **slot;
  114. if (!fs_info)
  115. return;
  116. if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
  117. &fs_info->fs_state)))
  118. return;
  119. test_mnt->mnt_sb->s_fs_info = NULL;
  120. spin_lock(&fs_info->buffer_lock);
  121. radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
  122. struct extent_buffer *eb;
  123. eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
  124. if (!eb)
  125. continue;
  126. /* Shouldn't happen but that kind of thinking creates CVE's */
  127. if (radix_tree_exception(eb)) {
  128. if (radix_tree_deref_retry(eb))
  129. slot = radix_tree_iter_retry(&iter);
  130. continue;
  131. }
  132. slot = radix_tree_iter_resume(slot, &iter);
  133. spin_unlock(&fs_info->buffer_lock);
  134. free_extent_buffer_stale(eb);
  135. spin_lock(&fs_info->buffer_lock);
  136. }
  137. spin_unlock(&fs_info->buffer_lock);
  138. btrfs_free_qgroup_config(fs_info);
  139. btrfs_free_fs_roots(fs_info);
  140. cleanup_srcu_struct(&fs_info->subvol_srcu);
  141. kfree(fs_info->super_copy);
  142. kfree(fs_info->fs_devices);
  143. kfree(fs_info);
  144. }
  145. void btrfs_free_dummy_root(struct btrfs_root *root)
  146. {
  147. if (!root)
  148. return;
  149. /* Will be freed by btrfs_free_fs_roots */
  150. if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
  151. return;
  152. if (root->node)
  153. free_extent_buffer(root->node);
  154. kfree(root);
  155. }
  156. struct btrfs_block_group_cache *
  157. btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
  158. unsigned long length)
  159. {
  160. struct btrfs_block_group_cache *cache;
  161. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  162. if (!cache)
  163. return NULL;
  164. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  165. GFP_KERNEL);
  166. if (!cache->free_space_ctl) {
  167. kfree(cache);
  168. return NULL;
  169. }
  170. cache->key.objectid = 0;
  171. cache->key.offset = length;
  172. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  173. cache->full_stripe_len = fs_info->sectorsize;
  174. cache->fs_info = fs_info;
  175. INIT_LIST_HEAD(&cache->list);
  176. INIT_LIST_HEAD(&cache->cluster_list);
  177. INIT_LIST_HEAD(&cache->bg_list);
  178. btrfs_init_free_space_ctl(cache);
  179. mutex_init(&cache->free_space_lock);
  180. return cache;
  181. }
  182. void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
  183. {
  184. if (!cache)
  185. return;
  186. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  187. kfree(cache->free_space_ctl);
  188. kfree(cache);
  189. }
  190. void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
  191. struct btrfs_fs_info *fs_info)
  192. {
  193. memset(trans, 0, sizeof(*trans));
  194. trans->transid = 1;
  195. trans->type = __TRANS_DUMMY;
  196. trans->fs_info = fs_info;
  197. }
  198. int btrfs_run_sanity_tests(void)
  199. {
  200. int ret, i;
  201. u32 sectorsize, nodesize;
  202. u32 test_sectorsize[] = {
  203. PAGE_SIZE,
  204. };
  205. ret = btrfs_init_test_fs();
  206. if (ret)
  207. return ret;
  208. for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
  209. sectorsize = test_sectorsize[i];
  210. for (nodesize = sectorsize;
  211. nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
  212. nodesize <<= 1) {
  213. pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n",
  214. sectorsize, nodesize);
  215. ret = btrfs_test_free_space_cache(sectorsize, nodesize);
  216. if (ret)
  217. goto out;
  218. ret = btrfs_test_extent_buffer_operations(sectorsize,
  219. nodesize);
  220. if (ret)
  221. goto out;
  222. ret = btrfs_test_extent_io(sectorsize, nodesize);
  223. if (ret)
  224. goto out;
  225. ret = btrfs_test_inodes(sectorsize, nodesize);
  226. if (ret)
  227. goto out;
  228. ret = btrfs_test_qgroups(sectorsize, nodesize);
  229. if (ret)
  230. goto out;
  231. ret = btrfs_test_free_space_tree(sectorsize, nodesize);
  232. if (ret)
  233. goto out;
  234. }
  235. }
  236. ret = btrfs_test_extent_map();
  237. out:
  238. btrfs_destroy_test_fs();
  239. return ret;
  240. }