mount.c 11 KB

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