root.c 4.4 KB

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