mount.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <linux/namei.h>
  17. #include <linux/seq_file.h>
  18. #include "kernfs-internal.h"
  19. struct kmem_cache *kernfs_node_cache;
  20. static int kernfs_sop_remount_fs(struct super_block *sb, int *flags, char *data)
  21. {
  22. struct kernfs_root *root = kernfs_info(sb)->root;
  23. struct kernfs_syscall_ops *scops = root->syscall_ops;
  24. if (scops && scops->remount_fs)
  25. return scops->remount_fs(root, flags, data);
  26. return 0;
  27. }
  28. static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
  29. {
  30. struct kernfs_root *root = kernfs_root(dentry->d_fsdata);
  31. struct kernfs_syscall_ops *scops = root->syscall_ops;
  32. if (scops && scops->show_options)
  33. return scops->show_options(sf, root);
  34. return 0;
  35. }
  36. static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
  37. {
  38. struct kernfs_node *node = dentry->d_fsdata;
  39. struct kernfs_root *root = kernfs_root(node);
  40. struct kernfs_syscall_ops *scops = root->syscall_ops;
  41. if (scops && scops->show_path)
  42. return scops->show_path(sf, node, root);
  43. seq_dentry(sf, dentry, " \t\n\\");
  44. return 0;
  45. }
  46. const struct super_operations kernfs_sops = {
  47. .statfs = simple_statfs,
  48. .drop_inode = generic_delete_inode,
  49. .evict_inode = kernfs_evict_inode,
  50. .remount_fs = kernfs_sop_remount_fs,
  51. .show_options = kernfs_sop_show_options,
  52. .show_path = kernfs_sop_show_path,
  53. };
  54. /**
  55. * kernfs_root_from_sb - determine kernfs_root associated with a super_block
  56. * @sb: the super_block in question
  57. *
  58. * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
  59. * %NULL is returned.
  60. */
  61. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  62. {
  63. if (sb->s_op == &kernfs_sops)
  64. return kernfs_info(sb)->root;
  65. return NULL;
  66. }
  67. /*
  68. * find the next ancestor in the path down to @child, where @parent was the
  69. * ancestor whose descendant we want to find.
  70. *
  71. * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root
  72. * node. If @parent is b, then we return the node for c.
  73. * Passing in d as @parent is not ok.
  74. */
  75. static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
  76. struct kernfs_node *parent)
  77. {
  78. if (child == parent) {
  79. pr_crit_once("BUG in find_next_ancestor: called with parent == child");
  80. return NULL;
  81. }
  82. while (child->parent != parent) {
  83. if (!child->parent)
  84. return NULL;
  85. child = child->parent;
  86. }
  87. return child;
  88. }
  89. /**
  90. * kernfs_node_dentry - get a dentry for the given kernfs_node
  91. * @kn: kernfs_node for which a dentry is needed
  92. * @sb: the kernfs super_block
  93. */
  94. struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
  95. struct super_block *sb)
  96. {
  97. struct dentry *dentry;
  98. struct kernfs_node *knparent = NULL;
  99. BUG_ON(sb->s_op != &kernfs_sops);
  100. dentry = dget(sb->s_root);
  101. /* Check if this is the root kernfs_node */
  102. if (!kn->parent)
  103. return dentry;
  104. knparent = find_next_ancestor(kn, NULL);
  105. if (WARN_ON(!knparent))
  106. return ERR_PTR(-EINVAL);
  107. do {
  108. struct dentry *dtmp;
  109. struct kernfs_node *kntmp;
  110. if (kn == knparent)
  111. return dentry;
  112. kntmp = find_next_ancestor(kn, knparent);
  113. if (WARN_ON(!kntmp))
  114. return ERR_PTR(-EINVAL);
  115. dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
  116. strlen(kntmp->name));
  117. dput(dentry);
  118. if (IS_ERR(dtmp))
  119. return dtmp;
  120. knparent = kntmp;
  121. dentry = dtmp;
  122. } while (true);
  123. }
  124. static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
  125. {
  126. struct kernfs_super_info *info = kernfs_info(sb);
  127. struct inode *inode;
  128. struct dentry *root;
  129. info->sb = sb;
  130. /* Userspace would break if executables or devices appear on sysfs */
  131. sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
  132. sb->s_blocksize = PAGE_SIZE;
  133. sb->s_blocksize_bits = PAGE_SHIFT;
  134. sb->s_magic = magic;
  135. sb->s_op = &kernfs_sops;
  136. sb->s_xattr = kernfs_xattr_handlers;
  137. sb->s_time_gran = 1;
  138. /* get root inode, initialize and unlock it */
  139. mutex_lock(&kernfs_mutex);
  140. inode = kernfs_get_inode(sb, info->root->kn);
  141. mutex_unlock(&kernfs_mutex);
  142. if (!inode) {
  143. pr_debug("kernfs: could not get root inode\n");
  144. return -ENOMEM;
  145. }
  146. /* instantiate and link root dentry */
  147. root = d_make_root(inode);
  148. if (!root) {
  149. pr_debug("%s: could not get root dentry!\n", __func__);
  150. return -ENOMEM;
  151. }
  152. kernfs_get(info->root->kn);
  153. root->d_fsdata = info->root->kn;
  154. sb->s_root = root;
  155. sb->s_d_op = &kernfs_dops;
  156. return 0;
  157. }
  158. static int kernfs_test_super(struct super_block *sb, void *data)
  159. {
  160. struct kernfs_super_info *sb_info = kernfs_info(sb);
  161. struct kernfs_super_info *info = data;
  162. return sb_info->root == info->root && sb_info->ns == info->ns;
  163. }
  164. static int kernfs_set_super(struct super_block *sb, void *data)
  165. {
  166. int error;
  167. error = set_anon_super(sb, data);
  168. if (!error)
  169. sb->s_fs_info = data;
  170. return error;
  171. }
  172. /**
  173. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  174. * @sb: super_block of interest
  175. *
  176. * Return the namespace tag associated with kernfs super_block @sb.
  177. */
  178. const void *kernfs_super_ns(struct super_block *sb)
  179. {
  180. struct kernfs_super_info *info = kernfs_info(sb);
  181. return info->ns;
  182. }
  183. /**
  184. * kernfs_mount_ns - kernfs mount helper
  185. * @fs_type: file_system_type of the fs being mounted
  186. * @flags: mount flags specified for the mount
  187. * @root: kernfs_root of the hierarchy being mounted
  188. * @magic: file system specific magic number
  189. * @new_sb_created: tell the caller if we allocated a new superblock
  190. * @ns: optional namespace tag of the mount
  191. *
  192. * This is to be called from each kernfs user's file_system_type->mount()
  193. * implementation, which should pass through the specified @fs_type and
  194. * @flags, and specify the hierarchy and namespace tag to mount via @root
  195. * and @ns, respectively.
  196. *
  197. * The return value can be passed to the vfs layer verbatim.
  198. */
  199. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  200. struct kernfs_root *root, unsigned long magic,
  201. bool *new_sb_created, const void *ns)
  202. {
  203. struct super_block *sb;
  204. struct kernfs_super_info *info;
  205. int error;
  206. info = kzalloc(sizeof(*info), GFP_KERNEL);
  207. if (!info)
  208. return ERR_PTR(-ENOMEM);
  209. info->root = root;
  210. info->ns = ns;
  211. sb = sget_userns(fs_type, kernfs_test_super, kernfs_set_super, flags,
  212. &init_user_ns, info);
  213. if (IS_ERR(sb) || sb->s_fs_info != info)
  214. kfree(info);
  215. if (IS_ERR(sb))
  216. return ERR_CAST(sb);
  217. if (new_sb_created)
  218. *new_sb_created = !sb->s_root;
  219. if (!sb->s_root) {
  220. struct kernfs_super_info *info = kernfs_info(sb);
  221. error = kernfs_fill_super(sb, magic);
  222. if (error) {
  223. deactivate_locked_super(sb);
  224. return ERR_PTR(error);
  225. }
  226. sb->s_flags |= MS_ACTIVE;
  227. mutex_lock(&kernfs_mutex);
  228. list_add(&info->node, &root->supers);
  229. mutex_unlock(&kernfs_mutex);
  230. }
  231. return dget(sb->s_root);
  232. }
  233. /**
  234. * kernfs_kill_sb - kill_sb for kernfs
  235. * @sb: super_block being killed
  236. *
  237. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  238. * user needs extra cleanup, it can implement its own kill_sb() and call
  239. * this function at the end.
  240. */
  241. void kernfs_kill_sb(struct super_block *sb)
  242. {
  243. struct kernfs_super_info *info = kernfs_info(sb);
  244. struct kernfs_node *root_kn = sb->s_root->d_fsdata;
  245. mutex_lock(&kernfs_mutex);
  246. list_del(&info->node);
  247. mutex_unlock(&kernfs_mutex);
  248. /*
  249. * Remove the superblock from fs_supers/s_instances
  250. * so we can't find it, before freeing kernfs_super_info.
  251. */
  252. kill_anon_super(sb);
  253. kfree(info);
  254. kernfs_put(root_kn);
  255. }
  256. /**
  257. * kernfs_pin_sb: try to pin the superblock associated with a kernfs_root
  258. * @kernfs_root: the kernfs_root in question
  259. * @ns: the namespace tag
  260. *
  261. * Pin the superblock so the superblock won't be destroyed in subsequent
  262. * operations. This can be used to block ->kill_sb() which may be useful
  263. * for kernfs users which dynamically manage superblocks.
  264. *
  265. * Returns NULL if there's no superblock associated to this kernfs_root, or
  266. * -EINVAL if the superblock is being freed.
  267. */
  268. struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns)
  269. {
  270. struct kernfs_super_info *info;
  271. struct super_block *sb = NULL;
  272. mutex_lock(&kernfs_mutex);
  273. list_for_each_entry(info, &root->supers, node) {
  274. if (info->ns == ns) {
  275. sb = info->sb;
  276. if (!atomic_inc_not_zero(&info->sb->s_active))
  277. sb = ERR_PTR(-EINVAL);
  278. break;
  279. }
  280. }
  281. mutex_unlock(&kernfs_mutex);
  282. return sb;
  283. }
  284. void __init kernfs_init(void)
  285. {
  286. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  287. sizeof(struct kernfs_node),
  288. 0, SLAB_PANIC, NULL);
  289. }