root.c 4.4 KB

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