root.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "internal.h"
  20. static int proc_test_super(struct super_block *sb, void *data)
  21. {
  22. return sb->s_fs_info == data;
  23. }
  24. static int proc_set_super(struct super_block *sb, void *data)
  25. {
  26. struct pid_namespace *ns;
  27. ns = (struct pid_namespace *)data;
  28. sb->s_fs_info = get_pid_ns(ns);
  29. return set_anon_super(sb, NULL);
  30. }
  31. static struct dentry *proc_mount(struct file_system_type *fs_type,
  32. int flags, const char *dev_name, void *data)
  33. {
  34. int err;
  35. struct super_block *sb;
  36. struct pid_namespace *ns;
  37. struct proc_inode *ei;
  38. if (proc_mnt) {
  39. /* Seed the root directory with a pid so it doesn't need
  40. * to be special in base.c. I would do this earlier but
  41. * the only task alive when /proc is mounted the first time
  42. * is the init_task and it doesn't have any pids.
  43. */
  44. ei = PROC_I(proc_mnt->mnt_sb->s_root->d_inode);
  45. if (!ei->pid)
  46. ei->pid = find_get_pid(1);
  47. }
  48. if (flags & MS_KERNMOUNT)
  49. ns = (struct pid_namespace *)data;
  50. else
  51. ns = current->nsproxy->pid_ns;
  52. sb = sget(fs_type, proc_test_super, proc_set_super, ns);
  53. if (IS_ERR(sb))
  54. return ERR_CAST(sb);
  55. if (!sb->s_root) {
  56. sb->s_flags = flags;
  57. err = proc_fill_super(sb);
  58. if (err) {
  59. deactivate_locked_super(sb);
  60. return ERR_PTR(err);
  61. }
  62. ei = PROC_I(sb->s_root->d_inode);
  63. if (!ei->pid) {
  64. rcu_read_lock();
  65. ei->pid = get_pid(find_pid_ns(1, ns));
  66. rcu_read_unlock();
  67. }
  68. sb->s_flags |= MS_ACTIVE;
  69. }
  70. return dget(sb->s_root);
  71. }
  72. static void proc_kill_sb(struct super_block *sb)
  73. {
  74. struct pid_namespace *ns;
  75. ns = (struct pid_namespace *)sb->s_fs_info;
  76. kill_anon_super(sb);
  77. put_pid_ns(ns);
  78. }
  79. static struct file_system_type proc_fs_type = {
  80. .name = "proc",
  81. .mount = proc_mount,
  82. .kill_sb = proc_kill_sb,
  83. };
  84. void __init proc_root_init(void)
  85. {
  86. int err;
  87. proc_init_inodecache();
  88. err = register_filesystem(&proc_fs_type);
  89. if (err)
  90. return;
  91. proc_mnt = kern_mount_data(&proc_fs_type, &init_pid_ns);
  92. if (IS_ERR(proc_mnt)) {
  93. unregister_filesystem(&proc_fs_type);
  94. return;
  95. }
  96. init_pid_ns.proc_mnt = proc_mnt;
  97. proc_symlink("mounts", NULL, "self/mounts");
  98. proc_net_init();
  99. #ifdef CONFIG_SYSVIPC
  100. proc_mkdir("sysvipc", NULL);
  101. #endif
  102. proc_mkdir("fs", NULL);
  103. proc_mkdir("driver", NULL);
  104. proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
  105. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  106. /* just give it a mountpoint */
  107. proc_mkdir("openprom", NULL);
  108. #endif
  109. proc_tty_init();
  110. #ifdef CONFIG_PROC_DEVICETREE
  111. proc_device_tree_init();
  112. #endif
  113. proc_mkdir("bus", NULL);
  114. proc_sys_init();
  115. }
  116. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  117. )
  118. {
  119. generic_fillattr(dentry->d_inode, stat);
  120. stat->nlink = proc_root.nlink + nr_processes();
  121. return 0;
  122. }
  123. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  124. {
  125. if (!proc_lookup(dir, dentry, nd)) {
  126. return NULL;
  127. }
  128. return proc_pid_lookup(dir, dentry, nd);
  129. }
  130. static int proc_root_readdir(struct file * filp,
  131. void * dirent, filldir_t filldir)
  132. {
  133. unsigned int nr = filp->f_pos;
  134. int ret;
  135. if (nr < FIRST_PROCESS_ENTRY) {
  136. int error = proc_readdir(filp, dirent, filldir);
  137. if (error <= 0)
  138. return error;
  139. filp->f_pos = FIRST_PROCESS_ENTRY;
  140. }
  141. ret = proc_pid_readdir(filp, dirent, filldir);
  142. return ret;
  143. }
  144. /*
  145. * The root /proc directory is special, as it has the
  146. * <pid> directories. Thus we don't use the generic
  147. * directory handling functions for that..
  148. */
  149. static const struct file_operations proc_root_operations = {
  150. .read = generic_read_dir,
  151. .readdir = proc_root_readdir,
  152. .llseek = default_llseek,
  153. };
  154. /*
  155. * proc root can do almost nothing..
  156. */
  157. static const struct inode_operations proc_root_inode_operations = {
  158. .lookup = proc_root_lookup,
  159. .getattr = proc_root_getattr,
  160. };
  161. /*
  162. * This is the root "inode" in the /proc tree..
  163. */
  164. struct proc_dir_entry proc_root = {
  165. .low_ino = PROC_ROOT_INO,
  166. .namelen = 5,
  167. .name = "/proc",
  168. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  169. .nlink = 2,
  170. .count = ATOMIC_INIT(1),
  171. .proc_iops = &proc_root_inode_operations,
  172. .proc_fops = &proc_root_operations,
  173. .parent = &proc_root,
  174. };
  175. int pid_ns_prepare_proc(struct pid_namespace *ns)
  176. {
  177. struct vfsmount *mnt;
  178. mnt = kern_mount_data(&proc_fs_type, ns);
  179. if (IS_ERR(mnt))
  180. return PTR_ERR(mnt);
  181. ns->proc_mnt = mnt;
  182. return 0;
  183. }
  184. void pid_ns_release_proc(struct pid_namespace *ns)
  185. {
  186. mntput(ns->proc_mnt);
  187. }