btrfs-tests.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "../volumes.h"
  24. #include "../disk-io.h"
  25. #include "../qgroup.h"
  26. static struct vfsmount *test_mnt = NULL;
  27. static const struct super_operations btrfs_test_super_ops = {
  28. .alloc_inode = btrfs_alloc_inode,
  29. .destroy_inode = btrfs_test_destroy_inode,
  30. };
  31. static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
  32. int flags, const char *dev_name,
  33. void *data)
  34. {
  35. return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
  36. NULL, BTRFS_TEST_MAGIC);
  37. }
  38. static struct file_system_type test_type = {
  39. .name = "btrfs_test_fs",
  40. .mount = btrfs_test_mount,
  41. .kill_sb = kill_anon_super,
  42. };
  43. struct inode *btrfs_new_test_inode(void)
  44. {
  45. return new_inode(test_mnt->mnt_sb);
  46. }
  47. int btrfs_init_test_fs(void)
  48. {
  49. int ret;
  50. ret = register_filesystem(&test_type);
  51. if (ret) {
  52. printk(KERN_ERR "btrfs: cannot register test file system\n");
  53. return ret;
  54. }
  55. test_mnt = kern_mount(&test_type);
  56. if (IS_ERR(test_mnt)) {
  57. printk(KERN_ERR "btrfs: cannot mount test file system\n");
  58. unregister_filesystem(&test_type);
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. void btrfs_destroy_test_fs(void)
  64. {
  65. kern_unmount(test_mnt);
  66. unregister_filesystem(&test_type);
  67. }
  68. struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(void)
  69. {
  70. struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
  71. GFP_NOFS);
  72. if (!fs_info)
  73. return fs_info;
  74. fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
  75. GFP_NOFS);
  76. if (!fs_info->fs_devices) {
  77. kfree(fs_info);
  78. return NULL;
  79. }
  80. fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
  81. GFP_NOFS);
  82. if (!fs_info->super_copy) {
  83. kfree(fs_info->fs_devices);
  84. kfree(fs_info);
  85. return NULL;
  86. }
  87. if (init_srcu_struct(&fs_info->subvol_srcu)) {
  88. kfree(fs_info->fs_devices);
  89. kfree(fs_info->super_copy);
  90. kfree(fs_info);
  91. return NULL;
  92. }
  93. spin_lock_init(&fs_info->buffer_lock);
  94. spin_lock_init(&fs_info->qgroup_lock);
  95. spin_lock_init(&fs_info->qgroup_op_lock);
  96. spin_lock_init(&fs_info->super_lock);
  97. spin_lock_init(&fs_info->fs_roots_radix_lock);
  98. spin_lock_init(&fs_info->tree_mod_seq_lock);
  99. mutex_init(&fs_info->qgroup_ioctl_lock);
  100. mutex_init(&fs_info->qgroup_rescan_lock);
  101. rwlock_init(&fs_info->tree_mod_log_lock);
  102. fs_info->running_transaction = NULL;
  103. fs_info->qgroup_tree = RB_ROOT;
  104. fs_info->qgroup_ulist = NULL;
  105. atomic64_set(&fs_info->tree_mod_seq, 0);
  106. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  107. INIT_LIST_HEAD(&fs_info->dead_roots);
  108. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  109. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  110. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  111. return fs_info;
  112. }
  113. static void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
  114. {
  115. struct radix_tree_iter iter;
  116. void **slot;
  117. spin_lock(&fs_info->buffer_lock);
  118. restart:
  119. radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
  120. struct extent_buffer *eb;
  121. eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
  122. if (!eb)
  123. continue;
  124. /* Shouldn't happen but that kind of thinking creates CVE's */
  125. if (radix_tree_exception(eb)) {
  126. if (radix_tree_deref_retry(eb))
  127. goto restart;
  128. continue;
  129. }
  130. spin_unlock(&fs_info->buffer_lock);
  131. free_extent_buffer_stale(eb);
  132. spin_lock(&fs_info->buffer_lock);
  133. }
  134. spin_unlock(&fs_info->buffer_lock);
  135. btrfs_free_qgroup_config(fs_info);
  136. btrfs_free_fs_roots(fs_info);
  137. cleanup_srcu_struct(&fs_info->subvol_srcu);
  138. kfree(fs_info->super_copy);
  139. kfree(fs_info->fs_devices);
  140. kfree(fs_info);
  141. }
  142. void btrfs_free_dummy_root(struct btrfs_root *root)
  143. {
  144. if (!root)
  145. return;
  146. if (root->node)
  147. free_extent_buffer(root->node);
  148. if (root->fs_info)
  149. btrfs_free_dummy_fs_info(root->fs_info);
  150. kfree(root);
  151. }