mount.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "kernfs-internal.h"
  19. struct kmem_cache *kernfs_node_cache;
  20. static int kernfs_sop_remount_fs(struct super_block *sb, int *flags, char *data)
  21. {
  22. struct kernfs_root *root = kernfs_info(sb)->root;
  23. struct kernfs_syscall_ops *scops = root->syscall_ops;
  24. if (scops && scops->remount_fs)
  25. return scops->remount_fs(root, flags, data);
  26. return 0;
  27. }
  28. static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
  29. {
  30. struct kernfs_root *root = kernfs_root(dentry->d_fsdata);
  31. struct kernfs_syscall_ops *scops = root->syscall_ops;
  32. if (scops && scops->show_options)
  33. return scops->show_options(sf, root);
  34. return 0;
  35. }
  36. static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
  37. {
  38. struct kernfs_node *node = dentry->d_fsdata;
  39. struct kernfs_root *root = kernfs_root(node);
  40. struct kernfs_syscall_ops *scops = root->syscall_ops;
  41. if (scops && scops->show_path)
  42. return scops->show_path(sf, node, root);
  43. seq_dentry(sf, dentry, " \t\n\\");
  44. return 0;
  45. }
  46. const struct super_operations kernfs_sops = {
  47. .statfs = simple_statfs,
  48. .drop_inode = generic_delete_inode,
  49. .evict_inode = kernfs_evict_inode,
  50. .remount_fs = kernfs_sop_remount_fs,
  51. .show_options = kernfs_sop_show_options,
  52. .show_path = kernfs_sop_show_path,
  53. };
  54. /**
  55. * kernfs_root_from_sb - determine kernfs_root associated with a super_block
  56. * @sb: the super_block in question
  57. *
  58. * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
  59. * %NULL is returned.
  60. */
  61. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  62. {
  63. if (sb->s_op == &kernfs_sops)
  64. return kernfs_info(sb)->root;
  65. return NULL;
  66. }
  67. /*
  68. * find the next ancestor in the path down to @child, where @parent was the
  69. * ancestor whose descendant we want to find.
  70. *
  71. * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root
  72. * node. If @parent is b, then we return the node for c.
  73. * Passing in d as @parent is not ok.
  74. */
  75. static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
  76. struct kernfs_node *parent)
  77. {
  78. if (child == parent) {
  79. pr_crit_once("BUG in find_next_ancestor: called with parent == child");
  80. return NULL;
  81. }
  82. while (child->parent != parent) {
  83. if (!child->parent)
  84. return NULL;
  85. child = child->parent;
  86. }
  87. return child;
  88. }
  89. /**
  90. * kernfs_node_dentry - get a dentry for the given kernfs_node
  91. * @kn: kernfs_node for which a dentry is needed
  92. * @sb: the kernfs super_block
  93. */
  94. struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
  95. struct super_block *sb)
  96. {
  97. struct dentry *dentry;
  98. struct kernfs_node *knparent = NULL;
  99. BUG_ON(sb->s_op != &kernfs_sops);
  100. dentry = dget(sb->s_root);
  101. /* Check if this is the root kernfs_node */
  102. if (!kn->parent)
  103. return dentry;
  104. knparent = find_next_ancestor(kn, NULL);
  105. if (WARN_ON(!knparent))
  106. return ERR_PTR(-EINVAL);
  107. do {
  108. struct dentry *dtmp;
  109. struct kernfs_node *kntmp;
  110. if (kn == knparent)
  111. return dentry;
  112. kntmp = find_next_ancestor(kn, knparent);
  113. if (WARN_ON(!kntmp))
  114. return ERR_PTR(-EINVAL);
  115. dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
  116. strlen(kntmp->name));
  117. dput(dentry);
  118. if (IS_ERR(dtmp))
  119. return dtmp;
  120. knparent = kntmp;
  121. dentry = dtmp;
  122. } while (true);
  123. }
  124. static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
  125. {
  126. struct kernfs_super_info *info = kernfs_info(sb);
  127. struct inode *inode;
  128. struct dentry *root;
  129. info->sb = sb;
  130. sb->s_blocksize = PAGE_SIZE;
  131. sb->s_blocksize_bits = PAGE_SHIFT;
  132. sb->s_magic = magic;
  133. sb->s_op = &kernfs_sops;
  134. sb->s_time_gran = 1;
  135. /* get root inode, initialize and unlock it */
  136. mutex_lock(&kernfs_mutex);
  137. inode = kernfs_get_inode(sb, info->root->kn);
  138. mutex_unlock(&kernfs_mutex);
  139. if (!inode) {
  140. pr_debug("kernfs: could not get root inode\n");
  141. return -ENOMEM;
  142. }
  143. /* instantiate and link root dentry */
  144. root = d_make_root(inode);
  145. if (!root) {
  146. pr_debug("%s: could not get root dentry!\n", __func__);
  147. return -ENOMEM;
  148. }
  149. kernfs_get(info->root->kn);
  150. root->d_fsdata = info->root->kn;
  151. sb->s_root = root;
  152. sb->s_d_op = &kernfs_dops;
  153. return 0;
  154. }
  155. static int kernfs_test_super(struct super_block *sb, void *data)
  156. {
  157. struct kernfs_super_info *sb_info = kernfs_info(sb);
  158. struct kernfs_super_info *info = data;
  159. return sb_info->root == info->root && sb_info->ns == info->ns;
  160. }
  161. static int kernfs_set_super(struct super_block *sb, void *data)
  162. {
  163. int error;
  164. error = set_anon_super(sb, data);
  165. if (!error)
  166. sb->s_fs_info = data;
  167. return error;
  168. }
  169. /**
  170. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  171. * @sb: super_block of interest
  172. *
  173. * Return the namespace tag associated with kernfs super_block @sb.
  174. */
  175. const void *kernfs_super_ns(struct super_block *sb)
  176. {
  177. struct kernfs_super_info *info = kernfs_info(sb);
  178. return info->ns;
  179. }
  180. /**
  181. * kernfs_mount_ns - kernfs mount helper
  182. * @fs_type: file_system_type of the fs being mounted
  183. * @flags: mount flags specified for the mount
  184. * @root: kernfs_root of the hierarchy being mounted
  185. * @magic: file system specific magic number
  186. * @new_sb_created: tell the caller if we allocated a new superblock
  187. * @ns: optional namespace tag of the mount
  188. *
  189. * This is to be called from each kernfs user's file_system_type->mount()
  190. * implementation, which should pass through the specified @fs_type and
  191. * @flags, and specify the hierarchy and namespace tag to mount via @root
  192. * and @ns, respectively.
  193. *
  194. * The return value can be passed to the vfs layer verbatim.
  195. */
  196. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  197. struct kernfs_root *root, unsigned long magic,
  198. bool *new_sb_created, const void *ns)
  199. {
  200. struct super_block *sb;
  201. struct kernfs_super_info *info;
  202. int error;
  203. info = kzalloc(sizeof(*info), GFP_KERNEL);
  204. if (!info)
  205. return ERR_PTR(-ENOMEM);
  206. info->root = root;
  207. info->ns = ns;
  208. sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
  209. if (IS_ERR(sb) || sb->s_fs_info != info)
  210. kfree(info);
  211. if (IS_ERR(sb))
  212. return ERR_CAST(sb);
  213. if (new_sb_created)
  214. *new_sb_created = !sb->s_root;
  215. if (!sb->s_root) {
  216. struct kernfs_super_info *info = kernfs_info(sb);
  217. error = kernfs_fill_super(sb, magic);
  218. if (error) {
  219. deactivate_locked_super(sb);
  220. return ERR_PTR(error);
  221. }
  222. sb->s_flags |= MS_ACTIVE;
  223. mutex_lock(&kernfs_mutex);
  224. list_add(&info->node, &root->supers);
  225. mutex_unlock(&kernfs_mutex);
  226. }
  227. return dget(sb->s_root);
  228. }
  229. /**
  230. * kernfs_kill_sb - kill_sb for kernfs
  231. * @sb: super_block being killed
  232. *
  233. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  234. * user needs extra cleanup, it can implement its own kill_sb() and call
  235. * this function at the end.
  236. */
  237. void kernfs_kill_sb(struct super_block *sb)
  238. {
  239. struct kernfs_super_info *info = kernfs_info(sb);
  240. struct kernfs_node *root_kn = sb->s_root->d_fsdata;
  241. mutex_lock(&kernfs_mutex);
  242. list_del(&info->node);
  243. mutex_unlock(&kernfs_mutex);
  244. /*
  245. * Remove the superblock from fs_supers/s_instances
  246. * so we can't find it, before freeing kernfs_super_info.
  247. */
  248. kill_anon_super(sb);
  249. kfree(info);
  250. kernfs_put(root_kn);
  251. }
  252. /**
  253. * kernfs_pin_sb: try to pin the superblock associated with a kernfs_root
  254. * @kernfs_root: the kernfs_root in question
  255. * @ns: the namespace tag
  256. *
  257. * Pin the superblock so the superblock won't be destroyed in subsequent
  258. * operations. This can be used to block ->kill_sb() which may be useful
  259. * for kernfs users which dynamically manage superblocks.
  260. *
  261. * Returns NULL if there's no superblock associated to this kernfs_root, or
  262. * -EINVAL if the superblock is being freed.
  263. */
  264. struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns)
  265. {
  266. struct kernfs_super_info *info;
  267. struct super_block *sb = NULL;
  268. mutex_lock(&kernfs_mutex);
  269. list_for_each_entry(info, &root->supers, node) {
  270. if (info->ns == ns) {
  271. sb = info->sb;
  272. if (!atomic_inc_not_zero(&info->sb->s_active))
  273. sb = ERR_PTR(-EINVAL);
  274. break;
  275. }
  276. }
  277. mutex_unlock(&kernfs_mutex);
  278. return sb;
  279. }
  280. void __init kernfs_init(void)
  281. {
  282. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  283. sizeof(struct kernfs_node),
  284. 0, SLAB_PANIC, NULL);
  285. }