root.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. int err;
  77. proc_init_inodecache();
  78. err = register_filesystem(&proc_fs_type);
  79. if (err)
  80. return;
  81. proc_mnt = kern_mount_data(&proc_fs_type, &init_pid_ns);
  82. if (IS_ERR(proc_mnt)) {
  83. unregister_filesystem(&proc_fs_type);
  84. return;
  85. }
  86. init_pid_ns.proc_mnt = proc_mnt;
  87. proc_symlink("mounts", NULL, "self/mounts");
  88. proc_net_init();
  89. #ifdef CONFIG_SYSVIPC
  90. proc_mkdir("sysvipc", NULL);
  91. #endif
  92. proc_mkdir("fs", NULL);
  93. proc_mkdir("driver", NULL);
  94. proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
  95. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  96. /* just give it a mountpoint */
  97. proc_mkdir("openprom", NULL);
  98. #endif
  99. proc_tty_init();
  100. #ifdef CONFIG_PROC_DEVICETREE
  101. proc_device_tree_init();
  102. #endif
  103. proc_mkdir("bus", NULL);
  104. proc_sys_init();
  105. }
  106. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  107. )
  108. {
  109. generic_fillattr(dentry->d_inode, stat);
  110. stat->nlink = proc_root.nlink + nr_processes();
  111. return 0;
  112. }
  113. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  114. {
  115. if (!proc_lookup(dir, dentry, nd)) {
  116. return NULL;
  117. }
  118. return proc_pid_lookup(dir, dentry, nd);
  119. }
  120. static int proc_root_readdir(struct file * filp,
  121. void * dirent, filldir_t filldir)
  122. {
  123. unsigned int nr = filp->f_pos;
  124. int ret;
  125. if (nr < FIRST_PROCESS_ENTRY) {
  126. int error = proc_readdir(filp, dirent, filldir);
  127. if (error <= 0)
  128. return error;
  129. filp->f_pos = FIRST_PROCESS_ENTRY;
  130. }
  131. ret = proc_pid_readdir(filp, dirent, filldir);
  132. return ret;
  133. }
  134. /*
  135. * The root /proc directory is special, as it has the
  136. * <pid> directories. Thus we don't use the generic
  137. * directory handling functions for that..
  138. */
  139. static const struct file_operations proc_root_operations = {
  140. .read = generic_read_dir,
  141. .readdir = proc_root_readdir,
  142. .llseek = default_llseek,
  143. };
  144. /*
  145. * proc root can do almost nothing..
  146. */
  147. static const struct inode_operations proc_root_inode_operations = {
  148. .lookup = proc_root_lookup,
  149. .getattr = proc_root_getattr,
  150. };
  151. /*
  152. * This is the root "inode" in the /proc tree..
  153. */
  154. struct proc_dir_entry proc_root = {
  155. .low_ino = PROC_ROOT_INO,
  156. .namelen = 5,
  157. .name = "/proc",
  158. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  159. .nlink = 2,
  160. .count = ATOMIC_INIT(1),
  161. .proc_iops = &proc_root_inode_operations,
  162. .proc_fops = &proc_root_operations,
  163. .parent = &proc_root,
  164. };
  165. int pid_ns_prepare_proc(struct pid_namespace *ns)
  166. {
  167. struct vfsmount *mnt;
  168. mnt = kern_mount_data(&proc_fs_type, ns);
  169. if (IS_ERR(mnt))
  170. return PTR_ERR(mnt);
  171. ns->proc_mnt = mnt;
  172. return 0;
  173. }
  174. void pid_ns_release_proc(struct pid_namespace *ns)
  175. {
  176. mntput(ns->proc_mnt);
  177. }