mount.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * fs/kernfs/mount.c - kernfs mount implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/mount.h>
  12. #include <linux/init.h>
  13. #include <linux/magic.h>
  14. #include <linux/slab.h>
  15. #include <linux/pagemap.h>
  16. #include "kernfs-internal.h"
  17. struct kmem_cache *kernfs_node_cache;
  18. static int kernfs_sop_remount_fs(struct super_block *sb, int *flags, char *data)
  19. {
  20. struct kernfs_root *root = kernfs_info(sb)->root;
  21. struct kernfs_syscall_ops *scops = root->syscall_ops;
  22. if (scops && scops->remount_fs)
  23. return scops->remount_fs(root, flags, data);
  24. return 0;
  25. }
  26. static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
  27. {
  28. struct kernfs_root *root = kernfs_root(dentry->d_fsdata);
  29. struct kernfs_syscall_ops *scops = root->syscall_ops;
  30. if (scops && scops->show_options)
  31. return scops->show_options(sf, root);
  32. return 0;
  33. }
  34. const struct super_operations kernfs_sops = {
  35. .statfs = simple_statfs,
  36. .drop_inode = generic_delete_inode,
  37. .evict_inode = kernfs_evict_inode,
  38. .remount_fs = kernfs_sop_remount_fs,
  39. .show_options = kernfs_sop_show_options,
  40. };
  41. /**
  42. * kernfs_root_from_sb - determine kernfs_root associated with a super_block
  43. * @sb: the super_block in question
  44. *
  45. * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
  46. * %NULL is returned.
  47. */
  48. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  49. {
  50. if (sb->s_op == &kernfs_sops)
  51. return kernfs_info(sb)->root;
  52. return NULL;
  53. }
  54. static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
  55. {
  56. struct kernfs_super_info *info = kernfs_info(sb);
  57. struct inode *inode;
  58. struct dentry *root;
  59. info->sb = sb;
  60. sb->s_blocksize = PAGE_CACHE_SIZE;
  61. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  62. sb->s_magic = magic;
  63. sb->s_op = &kernfs_sops;
  64. sb->s_time_gran = 1;
  65. /* get root inode, initialize and unlock it */
  66. mutex_lock(&kernfs_mutex);
  67. inode = kernfs_get_inode(sb, info->root->kn);
  68. mutex_unlock(&kernfs_mutex);
  69. if (!inode) {
  70. pr_debug("kernfs: could not get root inode\n");
  71. return -ENOMEM;
  72. }
  73. /* instantiate and link root dentry */
  74. root = d_make_root(inode);
  75. if (!root) {
  76. pr_debug("%s: could not get root dentry!\n", __func__);
  77. return -ENOMEM;
  78. }
  79. kernfs_get(info->root->kn);
  80. root->d_fsdata = info->root->kn;
  81. sb->s_root = root;
  82. sb->s_d_op = &kernfs_dops;
  83. return 0;
  84. }
  85. static int kernfs_test_super(struct super_block *sb, void *data)
  86. {
  87. struct kernfs_super_info *sb_info = kernfs_info(sb);
  88. struct kernfs_super_info *info = data;
  89. return sb_info->root == info->root && sb_info->ns == info->ns;
  90. }
  91. static int kernfs_set_super(struct super_block *sb, void *data)
  92. {
  93. int error;
  94. error = set_anon_super(sb, data);
  95. if (!error)
  96. sb->s_fs_info = data;
  97. return error;
  98. }
  99. /**
  100. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  101. * @sb: super_block of interest
  102. *
  103. * Return the namespace tag associated with kernfs super_block @sb.
  104. */
  105. const void *kernfs_super_ns(struct super_block *sb)
  106. {
  107. struct kernfs_super_info *info = kernfs_info(sb);
  108. return info->ns;
  109. }
  110. /**
  111. * kernfs_mount_ns - kernfs mount helper
  112. * @fs_type: file_system_type of the fs being mounted
  113. * @flags: mount flags specified for the mount
  114. * @root: kernfs_root of the hierarchy being mounted
  115. * @magic: file system specific magic number
  116. * @new_sb_created: tell the caller if we allocated a new superblock
  117. * @ns: optional namespace tag of the mount
  118. *
  119. * This is to be called from each kernfs user's file_system_type->mount()
  120. * implementation, which should pass through the specified @fs_type and
  121. * @flags, and specify the hierarchy and namespace tag to mount via @root
  122. * and @ns, respectively.
  123. *
  124. * The return value can be passed to the vfs layer verbatim.
  125. */
  126. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  127. struct kernfs_root *root, unsigned long magic,
  128. bool *new_sb_created, const void *ns)
  129. {
  130. struct super_block *sb;
  131. struct kernfs_super_info *info;
  132. int error;
  133. info = kzalloc(sizeof(*info), GFP_KERNEL);
  134. if (!info)
  135. return ERR_PTR(-ENOMEM);
  136. info->root = root;
  137. info->ns = ns;
  138. sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
  139. if (IS_ERR(sb) || sb->s_fs_info != info)
  140. kfree(info);
  141. if (IS_ERR(sb))
  142. return ERR_CAST(sb);
  143. if (new_sb_created)
  144. *new_sb_created = !sb->s_root;
  145. if (!sb->s_root) {
  146. struct kernfs_super_info *info = kernfs_info(sb);
  147. error = kernfs_fill_super(sb, magic);
  148. if (error) {
  149. deactivate_locked_super(sb);
  150. return ERR_PTR(error);
  151. }
  152. sb->s_flags |= MS_ACTIVE;
  153. mutex_lock(&kernfs_mutex);
  154. list_add(&info->node, &root->supers);
  155. mutex_unlock(&kernfs_mutex);
  156. }
  157. return dget(sb->s_root);
  158. }
  159. /**
  160. * kernfs_kill_sb - kill_sb for kernfs
  161. * @sb: super_block being killed
  162. *
  163. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  164. * user needs extra cleanup, it can implement its own kill_sb() and call
  165. * this function at the end.
  166. */
  167. void kernfs_kill_sb(struct super_block *sb)
  168. {
  169. struct kernfs_super_info *info = kernfs_info(sb);
  170. struct kernfs_node *root_kn = sb->s_root->d_fsdata;
  171. mutex_lock(&kernfs_mutex);
  172. list_del(&info->node);
  173. mutex_unlock(&kernfs_mutex);
  174. /*
  175. * Remove the superblock from fs_supers/s_instances
  176. * so we can't find it, before freeing kernfs_super_info.
  177. */
  178. kill_anon_super(sb);
  179. kfree(info);
  180. kernfs_put(root_kn);
  181. }
  182. /**
  183. * kernfs_pin_sb: try to pin the superblock associated with a kernfs_root
  184. * @kernfs_root: the kernfs_root in question
  185. * @ns: the namespace tag
  186. *
  187. * Pin the superblock so the superblock won't be destroyed in subsequent
  188. * operations. This can be used to block ->kill_sb() which may be useful
  189. * for kernfs users which dynamically manage superblocks.
  190. *
  191. * Returns NULL if there's no superblock associated to this kernfs_root, or
  192. * -EINVAL if the superblock is being freed.
  193. */
  194. struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns)
  195. {
  196. struct kernfs_super_info *info;
  197. struct super_block *sb = NULL;
  198. mutex_lock(&kernfs_mutex);
  199. list_for_each_entry(info, &root->supers, node) {
  200. if (info->ns == ns) {
  201. sb = info->sb;
  202. if (!atomic_inc_not_zero(&info->sb->s_active))
  203. sb = ERR_PTR(-EINVAL);
  204. break;
  205. }
  206. }
  207. mutex_unlock(&kernfs_mutex);
  208. return sb;
  209. }
  210. void __init kernfs_init(void)
  211. {
  212. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  213. sizeof(struct kernfs_node),
  214. 0, SLAB_PANIC, NULL);
  215. }