mount.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "kernfs-internal.h"
  17. struct kmem_cache *kernfs_node_cache;
  18. static const struct super_operations kernfs_sops = {
  19. .statfs = simple_statfs,
  20. .drop_inode = generic_delete_inode,
  21. .evict_inode = kernfs_evict_inode,
  22. };
  23. static int kernfs_fill_super(struct super_block *sb)
  24. {
  25. struct kernfs_super_info *info = kernfs_info(sb);
  26. struct inode *inode;
  27. struct dentry *root;
  28. sb->s_blocksize = PAGE_CACHE_SIZE;
  29. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  30. sb->s_magic = SYSFS_MAGIC;
  31. sb->s_op = &kernfs_sops;
  32. sb->s_time_gran = 1;
  33. /* get root inode, initialize and unlock it */
  34. mutex_lock(&kernfs_mutex);
  35. inode = kernfs_get_inode(sb, info->root->kn);
  36. mutex_unlock(&kernfs_mutex);
  37. if (!inode) {
  38. pr_debug("kernfs: could not get root inode\n");
  39. return -ENOMEM;
  40. }
  41. /* instantiate and link root dentry */
  42. root = d_make_root(inode);
  43. if (!root) {
  44. pr_debug("%s: could not get root dentry!\n", __func__);
  45. return -ENOMEM;
  46. }
  47. kernfs_get(info->root->kn);
  48. root->d_fsdata = info->root->kn;
  49. sb->s_root = root;
  50. sb->s_d_op = &kernfs_dops;
  51. return 0;
  52. }
  53. static int kernfs_test_super(struct super_block *sb, void *data)
  54. {
  55. struct kernfs_super_info *sb_info = kernfs_info(sb);
  56. struct kernfs_super_info *info = data;
  57. return sb_info->root == info->root && sb_info->ns == info->ns;
  58. }
  59. static int kernfs_set_super(struct super_block *sb, void *data)
  60. {
  61. int error;
  62. error = set_anon_super(sb, data);
  63. if (!error)
  64. sb->s_fs_info = data;
  65. return error;
  66. }
  67. /**
  68. * kernfs_super_ns - determine the namespace tag of a kernfs super_block
  69. * @sb: super_block of interest
  70. *
  71. * Return the namespace tag associated with kernfs super_block @sb.
  72. */
  73. const void *kernfs_super_ns(struct super_block *sb)
  74. {
  75. struct kernfs_super_info *info = kernfs_info(sb);
  76. return info->ns;
  77. }
  78. /**
  79. * kernfs_mount_ns - kernfs mount helper
  80. * @fs_type: file_system_type of the fs being mounted
  81. * @flags: mount flags specified for the mount
  82. * @root: kernfs_root of the hierarchy being mounted
  83. * @ns: optional namespace tag of the mount
  84. *
  85. * This is to be called from each kernfs user's file_system_type->mount()
  86. * implementation, which should pass through the specified @fs_type and
  87. * @flags, and specify the hierarchy and namespace tag to mount via @root
  88. * and @ns, respectively.
  89. *
  90. * The return value can be passed to the vfs layer verbatim.
  91. */
  92. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  93. struct kernfs_root *root, const void *ns)
  94. {
  95. struct super_block *sb;
  96. struct kernfs_super_info *info;
  97. int error;
  98. info = kzalloc(sizeof(*info), GFP_KERNEL);
  99. if (!info)
  100. return ERR_PTR(-ENOMEM);
  101. info->root = root;
  102. info->ns = ns;
  103. sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
  104. if (IS_ERR(sb) || sb->s_fs_info != info)
  105. kfree(info);
  106. if (IS_ERR(sb))
  107. return ERR_CAST(sb);
  108. if (!sb->s_root) {
  109. error = kernfs_fill_super(sb);
  110. if (error) {
  111. deactivate_locked_super(sb);
  112. return ERR_PTR(error);
  113. }
  114. sb->s_flags |= MS_ACTIVE;
  115. }
  116. return dget(sb->s_root);
  117. }
  118. /**
  119. * kernfs_kill_sb - kill_sb for kernfs
  120. * @sb: super_block being killed
  121. *
  122. * This can be used directly for file_system_type->kill_sb(). If a kernfs
  123. * user needs extra cleanup, it can implement its own kill_sb() and call
  124. * this function at the end.
  125. */
  126. void kernfs_kill_sb(struct super_block *sb)
  127. {
  128. struct kernfs_super_info *info = kernfs_info(sb);
  129. struct kernfs_node *root_kn = sb->s_root->d_fsdata;
  130. /*
  131. * Remove the superblock from fs_supers/s_instances
  132. * so we can't find it, before freeing kernfs_super_info.
  133. */
  134. kill_anon_super(sb);
  135. kfree(info);
  136. kernfs_put(root_kn);
  137. }
  138. void __init kernfs_init(void)
  139. {
  140. kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
  141. sizeof(struct kernfs_node),
  142. 0, SLAB_PANIC, NULL);
  143. kernfs_inode_init();
  144. }