btrfs-tests.c 6.8 KB

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