root.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * linux/fs/proc/root.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc root directory handling functions
  7. */
  8. #include <asm/uaccess.h>
  9. #include <linux/errno.h>
  10. #include <linux/time.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/stat.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mount.h>
  18. #include <linux/pid_namespace.h>
  19. #include <linux/parser.h>
  20. #include "internal.h"
  21. static int proc_test_super(struct super_block *sb, void *data)
  22. {
  23. return sb->s_fs_info == data;
  24. }
  25. static int proc_set_super(struct super_block *sb, void *data)
  26. {
  27. int err = set_anon_super(sb, NULL);
  28. if (!err) {
  29. struct pid_namespace *ns = (struct pid_namespace *)data;
  30. sb->s_fs_info = get_pid_ns(ns);
  31. }
  32. return err;
  33. }
  34. enum {
  35. Opt_err,
  36. };
  37. static const match_table_t tokens = {
  38. {Opt_err, NULL},
  39. };
  40. static int proc_parse_options(char *options, struct pid_namespace *pid)
  41. {
  42. char *p;
  43. substring_t args[MAX_OPT_ARGS];
  44. pr_debug("proc: options = %s\n", options);
  45. if (!options)
  46. return 1;
  47. while ((p = strsep(&options, ",")) != NULL) {
  48. int token;
  49. if (!*p)
  50. continue;
  51. args[0].to = args[0].from = 0;
  52. token = match_token(p, tokens, args);
  53. switch (token) {
  54. default:
  55. pr_err("proc: unrecognized mount option \"%s\" "
  56. "or missing value\n", p);
  57. return 0;
  58. }
  59. }
  60. return 1;
  61. }
  62. int proc_remount(struct super_block *sb, int *flags, char *data)
  63. {
  64. struct pid_namespace *pid = sb->s_fs_info;
  65. return !proc_parse_options(data, pid);
  66. }
  67. static struct dentry *proc_mount(struct file_system_type *fs_type,
  68. int flags, const char *dev_name, void *data)
  69. {
  70. int err;
  71. struct super_block *sb;
  72. struct pid_namespace *ns;
  73. struct proc_inode *ei;
  74. char *options;
  75. if (flags & MS_KERNMOUNT) {
  76. ns = (struct pid_namespace *)data;
  77. options = NULL;
  78. } else {
  79. ns = current->nsproxy->pid_ns;
  80. options = data;
  81. }
  82. sb = sget(fs_type, proc_test_super, proc_set_super, ns);
  83. if (IS_ERR(sb))
  84. return ERR_CAST(sb);
  85. if (!sb->s_root) {
  86. sb->s_flags = flags;
  87. if (!proc_parse_options(options, ns)) {
  88. deactivate_locked_super(sb);
  89. return ERR_PTR(-EINVAL);
  90. }
  91. err = proc_fill_super(sb);
  92. if (err) {
  93. deactivate_locked_super(sb);
  94. return ERR_PTR(err);
  95. }
  96. sb->s_flags |= MS_ACTIVE;
  97. }
  98. ei = PROC_I(sb->s_root->d_inode);
  99. if (!ei->pid) {
  100. rcu_read_lock();
  101. ei->pid = get_pid(find_pid_ns(1, ns));
  102. rcu_read_unlock();
  103. }
  104. return dget(sb->s_root);
  105. }
  106. static void proc_kill_sb(struct super_block *sb)
  107. {
  108. struct pid_namespace *ns;
  109. ns = (struct pid_namespace *)sb->s_fs_info;
  110. kill_anon_super(sb);
  111. put_pid_ns(ns);
  112. }
  113. static struct file_system_type proc_fs_type = {
  114. .name = "proc",
  115. .mount = proc_mount,
  116. .kill_sb = proc_kill_sb,
  117. };
  118. void __init proc_root_init(void)
  119. {
  120. int err;
  121. proc_init_inodecache();
  122. err = register_filesystem(&proc_fs_type);
  123. if (err)
  124. return;
  125. err = pid_ns_prepare_proc(&init_pid_ns);
  126. if (err) {
  127. unregister_filesystem(&proc_fs_type);
  128. return;
  129. }
  130. proc_symlink("mounts", NULL, "self/mounts");
  131. proc_net_init();
  132. #ifdef CONFIG_SYSVIPC
  133. proc_mkdir("sysvipc", NULL);
  134. #endif
  135. proc_mkdir("fs", NULL);
  136. proc_mkdir("driver", NULL);
  137. proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
  138. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  139. /* just give it a mountpoint */
  140. proc_mkdir("openprom", NULL);
  141. #endif
  142. proc_tty_init();
  143. #ifdef CONFIG_PROC_DEVICETREE
  144. proc_device_tree_init();
  145. #endif
  146. proc_mkdir("bus", NULL);
  147. proc_sys_init();
  148. }
  149. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  150. )
  151. {
  152. generic_fillattr(dentry->d_inode, stat);
  153. stat->nlink = proc_root.nlink + nr_processes();
  154. return 0;
  155. }
  156. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  157. {
  158. if (!proc_lookup(dir, dentry, nd)) {
  159. return NULL;
  160. }
  161. return proc_pid_lookup(dir, dentry, nd);
  162. }
  163. static int proc_root_readdir(struct file * filp,
  164. void * dirent, filldir_t filldir)
  165. {
  166. unsigned int nr = filp->f_pos;
  167. int ret;
  168. if (nr < FIRST_PROCESS_ENTRY) {
  169. int error = proc_readdir(filp, dirent, filldir);
  170. if (error <= 0)
  171. return error;
  172. filp->f_pos = FIRST_PROCESS_ENTRY;
  173. }
  174. ret = proc_pid_readdir(filp, dirent, filldir);
  175. return ret;
  176. }
  177. /*
  178. * The root /proc directory is special, as it has the
  179. * <pid> directories. Thus we don't use the generic
  180. * directory handling functions for that..
  181. */
  182. static const struct file_operations proc_root_operations = {
  183. .read = generic_read_dir,
  184. .readdir = proc_root_readdir,
  185. .llseek = default_llseek,
  186. };
  187. /*
  188. * proc root can do almost nothing..
  189. */
  190. static const struct inode_operations proc_root_inode_operations = {
  191. .lookup = proc_root_lookup,
  192. .getattr = proc_root_getattr,
  193. };
  194. /*
  195. * This is the root "inode" in the /proc tree..
  196. */
  197. struct proc_dir_entry proc_root = {
  198. .low_ino = PROC_ROOT_INO,
  199. .namelen = 5,
  200. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  201. .nlink = 2,
  202. .count = ATOMIC_INIT(1),
  203. .proc_iops = &proc_root_inode_operations,
  204. .proc_fops = &proc_root_operations,
  205. .parent = &proc_root,
  206. .name = "/proc",
  207. };
  208. int pid_ns_prepare_proc(struct pid_namespace *ns)
  209. {
  210. struct vfsmount *mnt;
  211. mnt = kern_mount_data(&proc_fs_type, ns);
  212. if (IS_ERR(mnt))
  213. return PTR_ERR(mnt);
  214. ns->proc_mnt = mnt;
  215. return 0;
  216. }
  217. void pid_ns_release_proc(struct pid_namespace *ns)
  218. {
  219. kern_unmount(ns->proc_mnt);
  220. }