btrfs-tests.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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(u32 nodesize, u32 sectorsize)
  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. fs_info->nodesize = nodesize;
  91. fs_info->sectorsize = sectorsize;
  92. if (init_srcu_struct(&fs_info->subvol_srcu)) {
  93. kfree(fs_info->fs_devices);
  94. kfree(fs_info->super_copy);
  95. kfree(fs_info);
  96. return NULL;
  97. }
  98. spin_lock_init(&fs_info->buffer_lock);
  99. spin_lock_init(&fs_info->qgroup_lock);
  100. spin_lock_init(&fs_info->qgroup_op_lock);
  101. spin_lock_init(&fs_info->super_lock);
  102. spin_lock_init(&fs_info->fs_roots_radix_lock);
  103. spin_lock_init(&fs_info->tree_mod_seq_lock);
  104. mutex_init(&fs_info->qgroup_ioctl_lock);
  105. mutex_init(&fs_info->qgroup_rescan_lock);
  106. rwlock_init(&fs_info->tree_mod_log_lock);
  107. fs_info->running_transaction = NULL;
  108. fs_info->qgroup_tree = RB_ROOT;
  109. fs_info->qgroup_ulist = NULL;
  110. atomic64_set(&fs_info->tree_mod_seq, 0);
  111. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  112. INIT_LIST_HEAD(&fs_info->dead_roots);
  113. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  114. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  115. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  116. extent_io_tree_init(&fs_info->freed_extents[0], NULL);
  117. extent_io_tree_init(&fs_info->freed_extents[1], NULL);
  118. fs_info->pinned_extents = &fs_info->freed_extents[0];
  119. set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  120. test_mnt->mnt_sb->s_fs_info = fs_info;
  121. return fs_info;
  122. }
  123. void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
  124. {
  125. struct radix_tree_iter iter;
  126. void **slot;
  127. if (!fs_info)
  128. return;
  129. if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
  130. &fs_info->fs_state)))
  131. return;
  132. test_mnt->mnt_sb->s_fs_info = NULL;
  133. spin_lock(&fs_info->buffer_lock);
  134. radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
  135. struct extent_buffer *eb;
  136. eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
  137. if (!eb)
  138. continue;
  139. /* Shouldn't happen but that kind of thinking creates CVE's */
  140. if (radix_tree_exception(eb)) {
  141. if (radix_tree_deref_retry(eb))
  142. slot = radix_tree_iter_retry(&iter);
  143. continue;
  144. }
  145. slot = radix_tree_iter_resume(slot, &iter);
  146. spin_unlock(&fs_info->buffer_lock);
  147. free_extent_buffer_stale(eb);
  148. spin_lock(&fs_info->buffer_lock);
  149. }
  150. spin_unlock(&fs_info->buffer_lock);
  151. btrfs_free_qgroup_config(fs_info);
  152. btrfs_free_fs_roots(fs_info);
  153. cleanup_srcu_struct(&fs_info->subvol_srcu);
  154. kfree(fs_info->super_copy);
  155. kfree(fs_info->fs_devices);
  156. kfree(fs_info);
  157. }
  158. void btrfs_free_dummy_root(struct btrfs_root *root)
  159. {
  160. if (!root)
  161. return;
  162. /* Will be freed by btrfs_free_fs_roots */
  163. if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
  164. return;
  165. if (root->node)
  166. free_extent_buffer(root->node);
  167. kfree(root);
  168. }
  169. struct btrfs_block_group_cache *
  170. btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
  171. unsigned long length)
  172. {
  173. struct btrfs_block_group_cache *cache;
  174. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  175. if (!cache)
  176. return NULL;
  177. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  178. GFP_KERNEL);
  179. if (!cache->free_space_ctl) {
  180. kfree(cache);
  181. return NULL;
  182. }
  183. cache->key.objectid = 0;
  184. cache->key.offset = length;
  185. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  186. cache->sectorsize = fs_info->sectorsize;
  187. cache->full_stripe_len = fs_info->sectorsize;
  188. cache->fs_info = fs_info;
  189. INIT_LIST_HEAD(&cache->list);
  190. INIT_LIST_HEAD(&cache->cluster_list);
  191. INIT_LIST_HEAD(&cache->bg_list);
  192. btrfs_init_free_space_ctl(cache);
  193. mutex_init(&cache->free_space_lock);
  194. return cache;
  195. }
  196. void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
  197. {
  198. if (!cache)
  199. return;
  200. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  201. kfree(cache->free_space_ctl);
  202. kfree(cache);
  203. }
  204. void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans)
  205. {
  206. memset(trans, 0, sizeof(*trans));
  207. trans->transid = 1;
  208. INIT_LIST_HEAD(&trans->qgroup_ref_list);
  209. trans->type = __TRANS_DUMMY;
  210. }
  211. int btrfs_run_sanity_tests(void)
  212. {
  213. int ret, i;
  214. u32 sectorsize, nodesize;
  215. u32 test_sectorsize[] = {
  216. PAGE_SIZE,
  217. };
  218. ret = btrfs_init_test_fs();
  219. if (ret)
  220. return ret;
  221. for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
  222. sectorsize = test_sectorsize[i];
  223. for (nodesize = sectorsize;
  224. nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
  225. nodesize <<= 1) {
  226. pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n",
  227. sectorsize, nodesize);
  228. ret = btrfs_test_free_space_cache(sectorsize, nodesize);
  229. if (ret)
  230. goto out;
  231. ret = btrfs_test_extent_buffer_operations(sectorsize,
  232. nodesize);
  233. if (ret)
  234. goto out;
  235. ret = btrfs_test_extent_io(sectorsize, nodesize);
  236. if (ret)
  237. goto out;
  238. ret = btrfs_test_inodes(sectorsize, nodesize);
  239. if (ret)
  240. goto out;
  241. ret = btrfs_test_qgroups(sectorsize, nodesize);
  242. if (ret)
  243. goto out;
  244. ret = btrfs_test_free_space_tree(sectorsize, nodesize);
  245. if (ret)
  246. goto out;
  247. }
  248. }
  249. out:
  250. btrfs_destroy_test_fs();
  251. return ret;
  252. }