root.c 5.1 KB

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