root.c 5.0 KB

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