btrfs-tests.c 7.1 KB

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