mount.c 11 KB

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