mount.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. dput(dentry);
  169. return ERR_PTR(-EINVAL);
  170. }
  171. do {
  172. struct dentry *dtmp;
  173. struct kernfs_node *kntmp;
  174. if (kn == knparent)
  175. return dentry;
  176. kntmp = find_next_ancestor(kn, knparent);
  177. if (WARN_ON(!kntmp)) {
  178. dput(dentry);
  179. return ERR_PTR(-EINVAL);
  180. }
  181. dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
  182. strlen(kntmp->name));
  183. dput(dentry);
  184. if (IS_ERR(dtmp))
  185. return dtmp;
  186. knparent = kntmp;
  187. dentry = dtmp;
  188. } while (true);
  189. }
  190. static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
  191. {
  192. struct kernfs_super_info *info = kernfs_info(sb);
  193. struct inode *inode;
  194. struct dentry *root;
  195. info->sb = sb;
  196. /* Userspace would break if executables or devices appear on sysfs */
  197. sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
  198. sb->s_blocksize = PAGE_SIZE;
  199. sb->s_blocksize_bits = PAGE_SHIFT;
  200. sb->s_magic = magic;
  201. sb->s_op = &kernfs_sops;
  202. sb->s_xattr = kernfs_xattr_handlers;
  203. if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
  204. sb->s_export_op = &kernfs_export_ops;
  205. sb->s_time_gran = 1;
  206. /* get root inode, initialize and unlock it */
  207. mutex_lock(&kernfs_mutex);
  208. inode = kernfs_get_inode(sb, info->root->kn);
  209. mutex_unlock(&kernfs_mutex);
  210. if (!inode) {
  211. pr_debug("kernfs: could not get root inode\n");
  212. return -ENOMEM;
  213. }
  214. /* instantiate and link root dentry */
  215. root = d_make_root(inode);
  216. if (!root) {
  217. pr_debug("%s: could not get root dentry!\n", __func__);
  218. return -ENOMEM;
  219. }
  220. sb->s_root = root;
  221. sb->s_d_op = &kernfs_dops;
  222. return 0;
  223. }
  224. static int kernfs_test_super(struct super_block *sb, void *data)
  225. {
  226. struct kernfs_super_info *sb_info = kernfs_info(sb);
  227. struct kernfs_super_info *info = data;
  228. return sb_info->root == info->root && sb_info->ns == info->ns;
  229. }
  230. static int kernfs_set_super(struct super_block *sb, void *data)
  231. {
  232. int error;
  233. error = set_anon_super(sb, data);
  234. if (!error)
  235. sb->s_fs_info = data;
  236. return error;
  237. }
  238. /**
  239. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  240. * @sb: super_block of interest
  241. *
  242. * Return the namespace tag associated with kernfs super_block @sb.
  243. */
  244. const void *kernfs_super_ns(struct super_block *sb)
  245. {
  246. struct kernfs_super_info *info = kernfs_info(sb);
  247. return info->ns;
  248. }
  249. /**
  250. * kernfs_mount_ns - kernfs mount helper
  251. * @fs_type: file_system_type of the fs being mounted
  252. * @flags: mount flags specified for the mount
  253. * @root: kernfs_root of the hierarchy being mounted
  254. * @magic: file system specific magic number
  255. * @new_sb_created: tell the caller if we allocated a new superblock
  256. * @ns: optional namespace tag of the mount
  257. *
  258. * This is to be called from each kernfs user's file_system_type->mount()
  259. * implementation, which should pass through the specified @fs_type and
  260. * @flags, and specify the hierarchy and namespace tag to mount via @root
  261. * and @ns, respectively.
  262. *
  263. * The return value can be passed to the vfs layer verbatim.
  264. */
  265. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  266. struct kernfs_root *root, unsigned long magic,
  267. bool *new_sb_created, const void *ns)
  268. {
  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 ERR_PTR(-ENOMEM);
  275. info->root = root;
  276. info->ns = ns;
  277. INIT_LIST_HEAD(&info->node);
  278. sb = sget_userns(fs_type, kernfs_test_super, kernfs_set_super, flags,
  279. &init_user_ns, info);
  280. if (IS_ERR(sb) || sb->s_fs_info != info)
  281. kfree(info);
  282. if (IS_ERR(sb))
  283. return ERR_CAST(sb);
  284. if (new_sb_created)
  285. *new_sb_created = !sb->s_root;
  286. if (!sb->s_root) {
  287. struct kernfs_super_info *info = kernfs_info(sb);
  288. error = kernfs_fill_super(sb, magic);
  289. if (error) {
  290. deactivate_locked_super(sb);
  291. return ERR_PTR(error);
  292. }
  293. sb->s_flags |= SB_ACTIVE;
  294. mutex_lock(&kernfs_mutex);
  295. list_add(&info->node, &root->supers);
  296. mutex_unlock(&kernfs_mutex);
  297. }
  298. return dget(sb->s_root);
  299. }
  300. /**
  301. * kernfs_kill_sb - kill_sb for kernfs
  302. * @sb: super_block being killed
  303. *
  304. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  305. * user needs extra cleanup, it can implement its own kill_sb() and call
  306. * this function at the end.
  307. */
  308. void kernfs_kill_sb(struct super_block *sb)
  309. {
  310. struct kernfs_super_info *info = kernfs_info(sb);
  311. mutex_lock(&kernfs_mutex);
  312. list_del(&info->node);
  313. mutex_unlock(&kernfs_mutex);
  314. /*
  315. * Remove the superblock from fs_supers/s_instances
  316. * so we can't find it, before freeing kernfs_super_info.
  317. */
  318. kill_anon_super(sb);
  319. kfree(info);
  320. }
  321. /**
  322. * kernfs_pin_sb: try to pin the superblock associated with a kernfs_root
  323. * @kernfs_root: the kernfs_root in question
  324. * @ns: the namespace tag
  325. *
  326. * Pin the superblock so the superblock won't be destroyed in subsequent
  327. * operations. This can be used to block ->kill_sb() which may be useful
  328. * for kernfs users which dynamically manage superblocks.
  329. *
  330. * Returns NULL if there's no superblock associated to this kernfs_root, or
  331. * -EINVAL if the superblock is being freed.
  332. */
  333. struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns)
  334. {
  335. struct kernfs_super_info *info;
  336. struct super_block *sb = NULL;
  337. mutex_lock(&kernfs_mutex);
  338. list_for_each_entry(info, &root->supers, node) {
  339. if (info->ns == ns) {
  340. sb = info->sb;
  341. if (!atomic_inc_not_zero(&info->sb->s_active))
  342. sb = ERR_PTR(-EINVAL);
  343. break;
  344. }
  345. }
  346. mutex_unlock(&kernfs_mutex);
  347. return sb;
  348. }
  349. void __init kernfs_init(void)
  350. {
  351. /*
  352. * the slab is freed in RCU context, so kernfs_find_and_get_node_by_ino
  353. * can access the slab lock free. This could introduce stale nodes,
  354. * please see how kernfs_find_and_get_node_by_ino filters out stale
  355. * nodes.
  356. */
  357. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  358. sizeof(struct kernfs_node),
  359. 0,
  360. SLAB_PANIC | SLAB_TYPESAFE_BY_RCU,
  361. NULL);
  362. }