mount.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <linux/uuid.h>
  19. #include <linux/statfs.h>
  20. #include "kernfs-internal.h"
  21. struct kmem_cache *kernfs_node_cache __ro_after_init;
  22. struct kmem_cache *kernfs_iattrs_cache __ro_after_init;
  23. struct kernfs_global_locks *kernfs_locks __ro_after_init;
  24. static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
  25. {
  26. struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
  27. struct kernfs_syscall_ops *scops = root->syscall_ops;
  28. if (scops && scops->show_options)
  29. return scops->show_options(sf, root);
  30. return 0;
  31. }
  32. static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
  33. {
  34. struct kernfs_node *node = kernfs_dentry_node(dentry);
  35. struct kernfs_root *root = kernfs_root(node);
  36. struct kernfs_syscall_ops *scops = root->syscall_ops;
  37. if (scops && scops->show_path)
  38. return scops->show_path(sf, node, root);
  39. seq_dentry(sf, dentry, " \t\n\\");
  40. return 0;
  41. }
  42. static int kernfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  43. {
  44. simple_statfs(dentry, buf);
  45. buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b);
  46. return 0;
  47. }
  48. const struct super_operations kernfs_sops = {
  49. .statfs = kernfs_statfs,
  50. .drop_inode = inode_just_drop,
  51. .evict_inode = kernfs_evict_inode,
  52. .show_options = kernfs_sop_show_options,
  53. .show_path = kernfs_sop_show_path,
  54. /*
  55. * sysfs is built on top of kernfs and sysfs provides the power
  56. * management infrastructure to support suspend/hibernate by
  57. * writing to various files in /sys/power/. As filesystems may
  58. * be automatically frozen during suspend/hibernate implementing
  59. * freeze/thaw support for kernfs generically will cause
  60. * deadlocks as the suspending/hibernation initiating task will
  61. * hold a VFS lock that it will then wait upon to be released.
  62. * If freeze/thaw for kernfs is needed talk to the VFS.
  63. */
  64. .freeze_fs = NULL,
  65. .unfreeze_fs = NULL,
  66. .freeze_super = NULL,
  67. .thaw_super = NULL,
  68. };
  69. static int kernfs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  70. struct inode *parent)
  71. {
  72. struct kernfs_node *kn = inode->i_private;
  73. if (*max_len < 2) {
  74. *max_len = 2;
  75. return FILEID_INVALID;
  76. }
  77. *max_len = 2;
  78. *(u64 *)fh = kn->id;
  79. return FILEID_KERNFS;
  80. }
  81. static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,
  82. struct fid *fid, int fh_len,
  83. int fh_type, bool get_parent)
  84. {
  85. struct kernfs_super_info *info = kernfs_info(sb);
  86. struct kernfs_node *kn;
  87. struct inode *inode;
  88. u64 id;
  89. if (fh_len < 2)
  90. return NULL;
  91. switch (fh_type) {
  92. case FILEID_KERNFS:
  93. id = *(u64 *)fid;
  94. break;
  95. case FILEID_INO32_GEN:
  96. case FILEID_INO32_GEN_PARENT:
  97. /*
  98. * blk_log_action() exposes "LOW32,HIGH32" pair without
  99. * type and userland can call us with generic fid
  100. * constructed from them. Combine it back to ID. See
  101. * blk_log_action().
  102. */
  103. id = ((u64)fid->i32.gen << 32) | fid->i32.ino;
  104. break;
  105. default:
  106. return NULL;
  107. }
  108. kn = kernfs_find_and_get_node_by_id(info->root, id);
  109. if (!kn)
  110. return ERR_PTR(-ESTALE);
  111. if (get_parent) {
  112. struct kernfs_node *parent;
  113. parent = kernfs_get_parent(kn);
  114. kernfs_put(kn);
  115. kn = parent;
  116. if (!kn)
  117. return ERR_PTR(-ESTALE);
  118. }
  119. inode = kernfs_get_inode(sb, kn);
  120. kernfs_put(kn);
  121. return d_obtain_alias(inode);
  122. }
  123. static struct dentry *kernfs_fh_to_dentry(struct super_block *sb,
  124. struct fid *fid, int fh_len,
  125. int fh_type)
  126. {
  127. return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, false);
  128. }
  129. static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
  130. struct fid *fid, int fh_len,
  131. int fh_type)
  132. {
  133. return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, true);
  134. }
  135. static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
  136. {
  137. struct kernfs_node *kn = kernfs_dentry_node(child);
  138. struct kernfs_root *root = kernfs_root(kn);
  139. guard(rwsem_read)(&root->kernfs_rwsem);
  140. return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_parent(kn)));
  141. }
  142. static const struct export_operations kernfs_export_ops = {
  143. .encode_fh = kernfs_encode_fh,
  144. .fh_to_dentry = kernfs_fh_to_dentry,
  145. .fh_to_parent = kernfs_fh_to_parent,
  146. .get_parent = kernfs_get_parent_dentry,
  147. };
  148. /**
  149. * kernfs_root_from_sb - determine kernfs_root associated with a super_block
  150. * @sb: the super_block in question
  151. *
  152. * Return: the kernfs_root associated with @sb. If @sb is not a kernfs one,
  153. * %NULL is returned.
  154. */
  155. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  156. {
  157. if (sb->s_op == &kernfs_sops)
  158. return kernfs_info(sb)->root;
  159. return NULL;
  160. }
  161. /*
  162. * find the next ancestor in the path down to @child, where @parent was the
  163. * ancestor whose descendant we want to find.
  164. *
  165. * Say the path is /a/b/c/d. @child is d, @parent is %NULL. We return the root
  166. * node. If @parent is b, then we return the node for c.
  167. * Passing in d as @parent is not ok.
  168. */
  169. static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
  170. struct kernfs_node *parent)
  171. {
  172. if (child == parent) {
  173. pr_crit_once("BUG in find_next_ancestor: called with parent == child");
  174. return NULL;
  175. }
  176. while (kernfs_parent(child) != parent) {
  177. child = kernfs_parent(child);
  178. if (!child)
  179. return NULL;
  180. }
  181. return child;
  182. }
  183. /**
  184. * kernfs_node_dentry - get a dentry for the given kernfs_node
  185. * @kn: kernfs_node for which a dentry is needed
  186. * @sb: the kernfs super_block
  187. *
  188. * Return: the dentry pointer
  189. */
  190. struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
  191. struct super_block *sb)
  192. {
  193. struct dentry *dentry;
  194. struct kernfs_node *knparent;
  195. struct kernfs_root *root;
  196. BUG_ON(sb->s_op != &kernfs_sops);
  197. dentry = dget(sb->s_root);
  198. /* Check if this is the root kernfs_node */
  199. if (!rcu_access_pointer(kn->__parent))
  200. return dentry;
  201. root = kernfs_root(kn);
  202. /*
  203. * As long as kn is valid, its parent can not vanish. This is cgroup's
  204. * kn so it can't have its parent replaced. Therefore it is safe to use
  205. * the ancestor node outside of the RCU or locked section.
  206. */
  207. if (WARN_ON_ONCE(!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)))
  208. return ERR_PTR(-EINVAL);
  209. scoped_guard(rcu) {
  210. knparent = find_next_ancestor(kn, NULL);
  211. }
  212. if (WARN_ON(!knparent)) {
  213. dput(dentry);
  214. return ERR_PTR(-EINVAL);
  215. }
  216. do {
  217. struct dentry *dtmp;
  218. struct kernfs_node *kntmp;
  219. const char *name;
  220. if (kn == knparent)
  221. return dentry;
  222. scoped_guard(rwsem_read, &root->kernfs_rwsem) {
  223. kntmp = find_next_ancestor(kn, knparent);
  224. if (WARN_ON(!kntmp)) {
  225. dput(dentry);
  226. return ERR_PTR(-EINVAL);
  227. }
  228. name = kstrdup(kernfs_rcu_name(kntmp), GFP_KERNEL);
  229. }
  230. if (!name) {
  231. dput(dentry);
  232. return ERR_PTR(-ENOMEM);
  233. }
  234. dtmp = lookup_noperm_positive_unlocked(&QSTR(name), dentry);
  235. dput(dentry);
  236. kfree(name);
  237. if (IS_ERR(dtmp))
  238. return dtmp;
  239. knparent = kntmp;
  240. dentry = dtmp;
  241. } while (true);
  242. }
  243. static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
  244. {
  245. struct kernfs_super_info *info = kernfs_info(sb);
  246. struct kernfs_root *kf_root = kfc->root;
  247. struct inode *inode;
  248. struct dentry *root;
  249. info->sb = sb;
  250. /* Userspace would break if executables or devices appear on sysfs */
  251. sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
  252. sb->s_blocksize = PAGE_SIZE;
  253. sb->s_blocksize_bits = PAGE_SHIFT;
  254. sb->s_magic = kfc->magic;
  255. sb->s_op = &kernfs_sops;
  256. sb->s_xattr = kernfs_xattr_handlers;
  257. if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
  258. sb->s_export_op = &kernfs_export_ops;
  259. sb->s_time_gran = 1;
  260. sb->s_maxbytes = MAX_LFS_FILESIZE;
  261. /* sysfs dentries and inodes don't require IO to create */
  262. sb->s_shrink->seeks = 0;
  263. /* get root inode, initialize and unlock it */
  264. down_read(&kf_root->kernfs_rwsem);
  265. inode = kernfs_get_inode(sb, info->root->kn);
  266. up_read(&kf_root->kernfs_rwsem);
  267. if (!inode) {
  268. pr_debug("kernfs: could not get root inode\n");
  269. return -ENOMEM;
  270. }
  271. /* instantiate and link root dentry */
  272. root = d_make_root(inode);
  273. if (!root) {
  274. pr_debug("%s: could not get root dentry!\n", __func__);
  275. return -ENOMEM;
  276. }
  277. sb->s_root = root;
  278. set_default_d_op(sb, &kernfs_dops);
  279. return 0;
  280. }
  281. static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
  282. {
  283. struct kernfs_super_info *sb_info = kernfs_info(sb);
  284. struct kernfs_super_info *info = fc->s_fs_info;
  285. return sb_info->root == info->root && sb_info->ns == info->ns;
  286. }
  287. static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
  288. {
  289. struct kernfs_fs_context *kfc = fc->fs_private;
  290. kfc->ns_tag = NULL;
  291. return set_anon_super_fc(sb, fc);
  292. }
  293. /**
  294. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  295. * @sb: super_block of interest
  296. *
  297. * Return: the namespace tag associated with kernfs super_block @sb.
  298. */
  299. const struct ns_common *kernfs_super_ns(struct super_block *sb)
  300. {
  301. struct kernfs_super_info *info = kernfs_info(sb);
  302. return info->ns;
  303. }
  304. /**
  305. * kernfs_get_tree - kernfs filesystem access/retrieval helper
  306. * @fc: The filesystem context.
  307. *
  308. * This is to be called from each kernfs user's fs_context->ops->get_tree()
  309. * implementation, which should set the specified ->@fs_type and ->@flags, and
  310. * specify the hierarchy and namespace tag to mount via ->@root and ->@ns,
  311. * respectively.
  312. *
  313. * Return: %0 on success, -errno on failure.
  314. */
  315. int kernfs_get_tree(struct fs_context *fc)
  316. {
  317. struct kernfs_fs_context *kfc = fc->fs_private;
  318. struct super_block *sb;
  319. struct kernfs_super_info *info;
  320. int error;
  321. info = kzalloc_obj(*info);
  322. if (!info)
  323. return -ENOMEM;
  324. info->root = kfc->root;
  325. info->ns = kfc->ns_tag;
  326. INIT_LIST_HEAD(&info->node);
  327. fc->s_fs_info = info;
  328. sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
  329. if (IS_ERR(sb))
  330. return PTR_ERR(sb);
  331. if (!sb->s_root) {
  332. struct kernfs_super_info *info = kernfs_info(sb);
  333. struct kernfs_root *root = kfc->root;
  334. kfc->new_sb_created = true;
  335. error = kernfs_fill_super(sb, kfc);
  336. if (error) {
  337. deactivate_locked_super(sb);
  338. return error;
  339. }
  340. sb->s_flags |= SB_ACTIVE;
  341. uuid_t uuid;
  342. uuid_gen(&uuid);
  343. super_set_uuid(sb, uuid.b, sizeof(uuid));
  344. down_write(&root->kernfs_supers_rwsem);
  345. list_add(&info->node, &info->root->supers);
  346. up_write(&root->kernfs_supers_rwsem);
  347. }
  348. fc->root = dget(sb->s_root);
  349. return 0;
  350. }
  351. void kernfs_free_fs_context(struct fs_context *fc)
  352. {
  353. /* Note that we don't deal with kfc->ns_tag here. */
  354. kfree(fc->s_fs_info);
  355. fc->s_fs_info = NULL;
  356. }
  357. /**
  358. * kernfs_kill_sb - kill_sb for kernfs
  359. * @sb: super_block being killed
  360. *
  361. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  362. * user needs extra cleanup, it can implement its own kill_sb() and call
  363. * this function at the end.
  364. */
  365. void kernfs_kill_sb(struct super_block *sb)
  366. {
  367. struct kernfs_super_info *info = kernfs_info(sb);
  368. struct kernfs_root *root = info->root;
  369. down_write(&root->kernfs_supers_rwsem);
  370. list_del(&info->node);
  371. up_write(&root->kernfs_supers_rwsem);
  372. /*
  373. * Remove the superblock from fs_supers/s_instances
  374. * so we can't find it, before freeing kernfs_super_info.
  375. */
  376. kill_anon_super(sb);
  377. kfree(info);
  378. }
  379. static void __init kernfs_mutex_init(void)
  380. {
  381. int count;
  382. for (count = 0; count < NR_KERNFS_LOCKS; count++)
  383. mutex_init(&kernfs_locks->open_file_mutex[count]);
  384. }
  385. static void __init kernfs_lock_init(void)
  386. {
  387. kernfs_locks = kmalloc_obj(struct kernfs_global_locks);
  388. WARN_ON(!kernfs_locks);
  389. kernfs_mutex_init();
  390. }
  391. void __init kernfs_init(void)
  392. {
  393. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  394. sizeof(struct kernfs_node),
  395. 0, SLAB_PANIC, NULL);
  396. /* Creates slab cache for kernfs inode attributes */
  397. kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",
  398. sizeof(struct kernfs_iattrs),
  399. 0, SLAB_PANIC, NULL);
  400. kernfs_lock_init();
  401. }