mount.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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)
  55. {
  56. struct kernfs_super_info *info = kernfs_info(sb);
  57. struct inode *inode;
  58. struct dentry *root;
  59. sb->s_blocksize = PAGE_CACHE_SIZE;
  60. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  61. sb->s_magic = SYSFS_MAGIC;
  62. sb->s_op = &kernfs_sops;
  63. sb->s_time_gran = 1;
  64. /* get root inode, initialize and unlock it */
  65. mutex_lock(&kernfs_mutex);
  66. inode = kernfs_get_inode(sb, info->root->kn);
  67. mutex_unlock(&kernfs_mutex);
  68. if (!inode) {
  69. pr_debug("kernfs: could not get root inode\n");
  70. return -ENOMEM;
  71. }
  72. /* instantiate and link root dentry */
  73. root = d_make_root(inode);
  74. if (!root) {
  75. pr_debug("%s: could not get root dentry!\n", __func__);
  76. return -ENOMEM;
  77. }
  78. kernfs_get(info->root->kn);
  79. root->d_fsdata = info->root->kn;
  80. sb->s_root = root;
  81. sb->s_d_op = &kernfs_dops;
  82. return 0;
  83. }
  84. static int kernfs_test_super(struct super_block *sb, void *data)
  85. {
  86. struct kernfs_super_info *sb_info = kernfs_info(sb);
  87. struct kernfs_super_info *info = data;
  88. return sb_info->root == info->root && sb_info->ns == info->ns;
  89. }
  90. static int kernfs_set_super(struct super_block *sb, void *data)
  91. {
  92. int error;
  93. error = set_anon_super(sb, data);
  94. if (!error)
  95. sb->s_fs_info = data;
  96. return error;
  97. }
  98. /**
  99. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  100. * @sb: super_block of interest
  101. *
  102. * Return the namespace tag associated with kernfs super_block @sb.
  103. */
  104. const void *kernfs_super_ns(struct super_block *sb)
  105. {
  106. struct kernfs_super_info *info = kernfs_info(sb);
  107. return info->ns;
  108. }
  109. /**
  110. * kernfs_mount_ns - kernfs mount helper
  111. * @fs_type: file_system_type of the fs being mounted
  112. * @flags: mount flags specified for the mount
  113. * @root: kernfs_root of the hierarchy being mounted
  114. * @new_sb_created: tell the caller if we allocated a new superblock
  115. * @ns: optional namespace tag of the mount
  116. *
  117. * This is to be called from each kernfs user's file_system_type->mount()
  118. * implementation, which should pass through the specified @fs_type and
  119. * @flags, and specify the hierarchy and namespace tag to mount via @root
  120. * and @ns, respectively.
  121. *
  122. * The return value can be passed to the vfs layer verbatim.
  123. */
  124. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  125. struct kernfs_root *root, bool *new_sb_created,
  126. const void *ns)
  127. {
  128. struct super_block *sb;
  129. struct kernfs_super_info *info;
  130. int error;
  131. info = kzalloc(sizeof(*info), GFP_KERNEL);
  132. if (!info)
  133. return ERR_PTR(-ENOMEM);
  134. info->root = root;
  135. info->ns = ns;
  136. sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
  137. if (IS_ERR(sb) || sb->s_fs_info != info)
  138. kfree(info);
  139. if (IS_ERR(sb))
  140. return ERR_CAST(sb);
  141. if (new_sb_created)
  142. *new_sb_created = !sb->s_root;
  143. if (!sb->s_root) {
  144. error = kernfs_fill_super(sb);
  145. if (error) {
  146. deactivate_locked_super(sb);
  147. return ERR_PTR(error);
  148. }
  149. sb->s_flags |= MS_ACTIVE;
  150. }
  151. return dget(sb->s_root);
  152. }
  153. /**
  154. * kernfs_kill_sb - kill_sb for kernfs
  155. * @sb: super_block being killed
  156. *
  157. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  158. * user needs extra cleanup, it can implement its own kill_sb() and call
  159. * this function at the end.
  160. */
  161. void kernfs_kill_sb(struct super_block *sb)
  162. {
  163. struct kernfs_super_info *info = kernfs_info(sb);
  164. struct kernfs_node *root_kn = sb->s_root->d_fsdata;
  165. /*
  166. * Remove the superblock from fs_supers/s_instances
  167. * so we can't find it, before freeing kernfs_super_info.
  168. */
  169. kill_anon_super(sb);
  170. kfree(info);
  171. kernfs_put(root_kn);
  172. }
  173. void __init kernfs_init(void)
  174. {
  175. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  176. sizeof(struct kernfs_node),
  177. 0, SLAB_PANIC, NULL);
  178. kernfs_inode_init();
  179. }