mount.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/kernfs/mount.c - kernfs mount implementation
  4. *
  5. * Copyright (c) 2001-3 Patrick Mochel
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/mount.h>
  11. #include <linux/init.h>
  12. #include <linux/magic.h>
  13. #include <linux/slab.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/namei.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/exportfs.h>
  18. #include "kernfs-internal.h"
  19. struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
  20. static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
  21. {
  22. struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
  23. struct kernfs_syscall_ops *scops = root->syscall_ops;
  24. if (scops && scops->show_options)
  25. return scops->show_options(sf, root);
  26. return 0;
  27. }
  28. static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
  29. {
  30. struct kernfs_node *node = kernfs_dentry_node(dentry);
  31. struct kernfs_root *root = kernfs_root(node);
  32. struct kernfs_syscall_ops *scops = root->syscall_ops;
  33. if (scops && scops->show_path)
  34. return scops->show_path(sf, node, root);
  35. seq_dentry(sf, dentry, " \t\n\\");
  36. return 0;
  37. }
  38. const struct super_operations kernfs_sops = {
  39. .statfs = simple_statfs,
  40. .drop_inode = generic_delete_inode,
  41. .evict_inode = kernfs_evict_inode,
  42. .show_options = kernfs_sop_show_options,
  43. .show_path = kernfs_sop_show_path,
  44. };
  45. static int kernfs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  46. struct inode *parent)
  47. {
  48. struct kernfs_node *kn = inode->i_private;
  49. if (*max_len < 2) {
  50. *max_len = 2;
  51. return FILEID_INVALID;
  52. }
  53. *max_len = 2;
  54. *(u64 *)fh = kn->id;
  55. return FILEID_KERNFS;
  56. }
  57. static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,
  58. struct fid *fid, int fh_len,
  59. int fh_type, bool get_parent)
  60. {
  61. struct kernfs_super_info *info = kernfs_info(sb);
  62. struct kernfs_node *kn;
  63. struct inode *inode;
  64. u64 id;
  65. if (fh_len < 2)
  66. return NULL;
  67. switch (fh_type) {
  68. case FILEID_KERNFS:
  69. id = *(u64 *)fid;
  70. break;
  71. case FILEID_INO32_GEN:
  72. case FILEID_INO32_GEN_PARENT:
  73. /*
  74. * blk_log_action() exposes "LOW32,HIGH32" pair without
  75. * type and userland can call us with generic fid
  76. * constructed from them. Combine it back to ID. See
  77. * blk_log_action().
  78. */
  79. id = ((u64)fid->i32.gen << 32) | fid->i32.ino;
  80. break;
  81. default:
  82. return NULL;
  83. }
  84. kn = kernfs_find_and_get_node_by_id(info->root, id);
  85. if (!kn)
  86. return ERR_PTR(-ESTALE);
  87. if (get_parent) {
  88. struct kernfs_node *parent;
  89. parent = kernfs_get_parent(kn);
  90. kernfs_put(kn);
  91. kn = parent;
  92. if (!kn)
  93. return ERR_PTR(-ESTALE);
  94. }
  95. inode = kernfs_get_inode(sb, kn);
  96. kernfs_put(kn);
  97. if (!inode)
  98. return ERR_PTR(-ESTALE);
  99. return d_obtain_alias(inode);
  100. }
  101. static struct dentry *kernfs_fh_to_dentry(struct super_block *sb,
  102. struct fid *fid, int fh_len,
  103. int fh_type)
  104. {
  105. return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, false);
  106. }
  107. static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
  108. struct fid *fid, int fh_len,
  109. int fh_type)
  110. {
  111. return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, true);
  112. }
  113. static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
  114. {
  115. struct kernfs_node *kn = kernfs_dentry_node(child);
  116. return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
  117. }
  118. static const struct export_operations kernfs_export_ops = {
  119. .encode_fh = kernfs_encode_fh,
  120. .fh_to_dentry = kernfs_fh_to_dentry,
  121. .fh_to_parent = kernfs_fh_to_parent,
  122. .get_parent = kernfs_get_parent_dentry,
  123. };
  124. /**
  125. * kernfs_root_from_sb - determine kernfs_root associated with a super_block
  126. * @sb: the super_block in question
  127. *
  128. * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
  129. * %NULL is returned.
  130. */
  131. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  132. {
  133. if (sb->s_op == &kernfs_sops)
  134. return kernfs_info(sb)->root;
  135. return NULL;
  136. }
  137. /*
  138. * find the next ancestor in the path down to @child, where @parent was the
  139. * ancestor whose descendant we want to find.
  140. *
  141. * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root
  142. * node. If @parent is b, then we return the node for c.
  143. * Passing in d as @parent is not ok.
  144. */
  145. static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
  146. struct kernfs_node *parent)
  147. {
  148. if (child == parent) {
  149. pr_crit_once("BUG in find_next_ancestor: called with parent == child");
  150. return NULL;
  151. }
  152. while (child->parent != parent) {
  153. if (!child->parent)
  154. return NULL;
  155. child = child->parent;
  156. }
  157. return child;
  158. }
  159. /**
  160. * kernfs_node_dentry - get a dentry for the given kernfs_node
  161. * @kn: kernfs_node for which a dentry is needed
  162. * @sb: the kernfs super_block
  163. */
  164. struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
  165. struct super_block *sb)
  166. {
  167. struct dentry *dentry;
  168. struct kernfs_node *knparent = NULL;
  169. BUG_ON(sb->s_op != &kernfs_sops);
  170. dentry = dget(sb->s_root);
  171. /* Check if this is the root kernfs_node */
  172. if (!kn->parent)
  173. return dentry;
  174. knparent = find_next_ancestor(kn, NULL);
  175. if (WARN_ON(!knparent)) {
  176. dput(dentry);
  177. return ERR_PTR(-EINVAL);
  178. }
  179. do {
  180. struct dentry *dtmp;
  181. struct kernfs_node *kntmp;
  182. if (kn == knparent)
  183. return dentry;
  184. kntmp = find_next_ancestor(kn, knparent);
  185. if (WARN_ON(!kntmp)) {
  186. dput(dentry);
  187. return ERR_PTR(-EINVAL);
  188. }
  189. dtmp = lookup_positive_unlocked(kntmp->name, dentry,
  190. strlen(kntmp->name));
  191. dput(dentry);
  192. if (IS_ERR(dtmp))
  193. return dtmp;
  194. knparent = kntmp;
  195. dentry = dtmp;
  196. } while (true);
  197. }
  198. static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
  199. {
  200. struct kernfs_super_info *info = kernfs_info(sb);
  201. struct inode *inode;
  202. struct dentry *root;
  203. info->sb = sb;
  204. /* Userspace would break if executables or devices appear on sysfs */
  205. sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
  206. sb->s_blocksize = PAGE_SIZE;
  207. sb->s_blocksize_bits = PAGE_SHIFT;
  208. sb->s_magic = kfc->magic;
  209. sb->s_op = &kernfs_sops;
  210. sb->s_xattr = kernfs_xattr_handlers;
  211. if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
  212. sb->s_export_op = &kernfs_export_ops;
  213. sb->s_time_gran = 1;
  214. /* sysfs dentries and inodes don't require IO to create */
  215. sb->s_shrink.seeks = 0;
  216. /* get root inode, initialize and unlock it */
  217. mutex_lock(&kernfs_mutex);
  218. inode = kernfs_get_inode(sb, info->root->kn);
  219. mutex_unlock(&kernfs_mutex);
  220. if (!inode) {
  221. pr_debug("kernfs: could not get root inode\n");
  222. return -ENOMEM;
  223. }
  224. /* instantiate and link root dentry */
  225. root = d_make_root(inode);
  226. if (!root) {
  227. pr_debug("%s: could not get root dentry!\n", __func__);
  228. return -ENOMEM;
  229. }
  230. sb->s_root = root;
  231. sb->s_d_op = &kernfs_dops;
  232. return 0;
  233. }
  234. static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
  235. {
  236. struct kernfs_super_info *sb_info = kernfs_info(sb);
  237. struct kernfs_super_info *info = fc->s_fs_info;
  238. return sb_info->root == info->root && sb_info->ns == info->ns;
  239. }
  240. static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
  241. {
  242. struct kernfs_fs_context *kfc = fc->fs_private;
  243. kfc->ns_tag = NULL;
  244. return set_anon_super_fc(sb, fc);
  245. }
  246. /**
  247. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  248. * @sb: super_block of interest
  249. *
  250. * Return the namespace tag associated with kernfs super_block @sb.
  251. */
  252. const void *kernfs_super_ns(struct super_block *sb)
  253. {
  254. struct kernfs_super_info *info = kernfs_info(sb);
  255. return info->ns;
  256. }
  257. /**
  258. * kernfs_get_tree - kernfs filesystem access/retrieval helper
  259. * @fc: The filesystem context.
  260. *
  261. * This is to be called from each kernfs user's fs_context->ops->get_tree()
  262. * implementation, which should set the specified ->@fs_type and ->@flags, and
  263. * specify the hierarchy and namespace tag to mount via ->@root and ->@ns,
  264. * respectively.
  265. */
  266. int kernfs_get_tree(struct fs_context *fc)
  267. {
  268. struct kernfs_fs_context *kfc = fc->fs_private;
  269. struct super_block *sb;
  270. struct kernfs_super_info *info;
  271. int error;
  272. info = kzalloc(sizeof(*info), GFP_KERNEL);
  273. if (!info)
  274. return -ENOMEM;
  275. info->root = kfc->root;
  276. info->ns = kfc->ns_tag;
  277. INIT_LIST_HEAD(&info->node);
  278. fc->s_fs_info = info;
  279. sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
  280. if (IS_ERR(sb))
  281. return PTR_ERR(sb);
  282. if (!sb->s_root) {
  283. struct kernfs_super_info *info = kernfs_info(sb);
  284. kfc->new_sb_created = true;
  285. error = kernfs_fill_super(sb, kfc);
  286. if (error) {
  287. deactivate_locked_super(sb);
  288. return error;
  289. }
  290. sb->s_flags |= SB_ACTIVE;
  291. mutex_lock(&kernfs_mutex);
  292. list_add(&info->node, &info->root->supers);
  293. mutex_unlock(&kernfs_mutex);
  294. }
  295. fc->root = dget(sb->s_root);
  296. return 0;
  297. }
  298. void kernfs_free_fs_context(struct fs_context *fc)
  299. {
  300. /* Note that we don't deal with kfc->ns_tag here. */
  301. kfree(fc->s_fs_info);
  302. fc->s_fs_info = NULL;
  303. }
  304. /**
  305. * kernfs_kill_sb - kill_sb for kernfs
  306. * @sb: super_block being killed
  307. *
  308. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  309. * user needs extra cleanup, it can implement its own kill_sb() and call
  310. * this function at the end.
  311. */
  312. void kernfs_kill_sb(struct super_block *sb)
  313. {
  314. struct kernfs_super_info *info = kernfs_info(sb);
  315. mutex_lock(&kernfs_mutex);
  316. list_del(&info->node);
  317. mutex_unlock(&kernfs_mutex);
  318. /*
  319. * Remove the superblock from fs_supers/s_instances
  320. * so we can't find it, before freeing kernfs_super_info.
  321. */
  322. kill_anon_super(sb);
  323. kfree(info);
  324. }
  325. void __init kernfs_init(void)
  326. {
  327. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  328. sizeof(struct kernfs_node),
  329. 0, SLAB_PANIC, NULL);
  330. /* Creates slab cache for kernfs inode attributes */
  331. kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",
  332. sizeof(struct kernfs_iattrs),
  333. 0, SLAB_PANIC, NULL);
  334. }