btrfs-tests.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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 ret;
  63. }
  64. return 0;
  65. }
  66. 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. return fs_info;
  118. }
  119. static void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
  120. {
  121. struct radix_tree_iter iter;
  122. void **slot;
  123. spin_lock(&fs_info->buffer_lock);
  124. restart:
  125. radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
  126. struct extent_buffer *eb;
  127. eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
  128. if (!eb)
  129. continue;
  130. /* Shouldn't happen but that kind of thinking creates CVE's */
  131. if (radix_tree_exception(eb)) {
  132. if (radix_tree_deref_retry(eb))
  133. goto restart;
  134. continue;
  135. }
  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. if (root->node)
  153. free_extent_buffer(root->node);
  154. if (root->fs_info)
  155. btrfs_free_dummy_fs_info(root->fs_info);
  156. kfree(root);
  157. }
  158. struct btrfs_block_group_cache *
  159. btrfs_alloc_dummy_block_group(unsigned long length)
  160. {
  161. struct btrfs_block_group_cache *cache;
  162. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  163. if (!cache)
  164. return NULL;
  165. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  166. GFP_KERNEL);
  167. if (!cache->free_space_ctl) {
  168. kfree(cache);
  169. return NULL;
  170. }
  171. cache->fs_info = btrfs_alloc_dummy_fs_info();
  172. if (!cache->fs_info) {
  173. kfree(cache->free_space_ctl);
  174. kfree(cache);
  175. return NULL;
  176. }
  177. cache->key.objectid = 0;
  178. cache->key.offset = length;
  179. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  180. cache->sectorsize = 4096;
  181. cache->full_stripe_len = 4096;
  182. INIT_LIST_HEAD(&cache->list);
  183. INIT_LIST_HEAD(&cache->cluster_list);
  184. INIT_LIST_HEAD(&cache->bg_list);
  185. btrfs_init_free_space_ctl(cache);
  186. mutex_init(&cache->free_space_lock);
  187. return cache;
  188. }
  189. void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
  190. {
  191. if (!cache)
  192. return;
  193. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  194. kfree(cache->free_space_ctl);
  195. kfree(cache);
  196. }
  197. void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans)
  198. {
  199. memset(trans, 0, sizeof(*trans));
  200. trans->transid = 1;
  201. INIT_LIST_HEAD(&trans->qgroup_ref_list);
  202. trans->type = __TRANS_DUMMY;
  203. }