root.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/module.h>
  15. #include <linux/bitops.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/mount.h>
  18. #include "internal.h"
  19. struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver;
  20. #ifdef CONFIG_SYSCTL
  21. struct proc_dir_entry *proc_sys_root;
  22. #endif
  23. static int proc_get_sb(struct file_system_type *fs_type,
  24. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  25. {
  26. if (proc_mnt) {
  27. /* Seed the root directory with a pid so it doesn't need
  28. * to be special in base.c. I would do this earlier but
  29. * the only task alive when /proc is mounted the first time
  30. * is the init_task and it doesn't have any pids.
  31. */
  32. struct proc_inode *ei;
  33. ei = PROC_I(proc_mnt->mnt_sb->s_root->d_inode);
  34. if (!ei->pid)
  35. ei->pid = find_get_pid(1);
  36. }
  37. return get_sb_single(fs_type, flags, data, proc_fill_super, mnt);
  38. }
  39. static struct file_system_type proc_fs_type = {
  40. .name = "proc",
  41. .get_sb = proc_get_sb,
  42. .kill_sb = kill_anon_super,
  43. };
  44. void __init proc_root_init(void)
  45. {
  46. int err = proc_init_inodecache();
  47. if (err)
  48. return;
  49. err = register_filesystem(&proc_fs_type);
  50. if (err)
  51. return;
  52. proc_mnt = kern_mount(&proc_fs_type);
  53. err = PTR_ERR(proc_mnt);
  54. if (IS_ERR(proc_mnt)) {
  55. unregister_filesystem(&proc_fs_type);
  56. return;
  57. }
  58. proc_misc_init();
  59. proc_net = proc_mkdir("net", NULL);
  60. proc_net_stat = proc_mkdir("net/stat", NULL);
  61. #ifdef CONFIG_SYSVIPC
  62. proc_mkdir("sysvipc", NULL);
  63. #endif
  64. #ifdef CONFIG_SYSCTL
  65. proc_sys_root = proc_mkdir("sys", NULL);
  66. #endif
  67. #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
  68. proc_mkdir("sys/fs", NULL);
  69. proc_mkdir("sys/fs/binfmt_misc", NULL);
  70. #endif
  71. proc_root_fs = proc_mkdir("fs", NULL);
  72. proc_root_driver = proc_mkdir("driver", NULL);
  73. proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
  74. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  75. /* just give it a mountpoint */
  76. proc_mkdir("openprom", NULL);
  77. #endif
  78. proc_tty_init();
  79. #ifdef CONFIG_PROC_DEVICETREE
  80. proc_device_tree_init();
  81. #endif
  82. proc_bus = proc_mkdir("bus", NULL);
  83. }
  84. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  85. )
  86. {
  87. generic_fillattr(dentry->d_inode, stat);
  88. stat->nlink = proc_root.nlink + nr_processes();
  89. return 0;
  90. }
  91. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  92. {
  93. if (!proc_lookup(dir, dentry, nd)) {
  94. return NULL;
  95. }
  96. return proc_pid_lookup(dir, dentry, nd);
  97. }
  98. static int proc_root_readdir(struct file * filp,
  99. void * dirent, filldir_t filldir)
  100. {
  101. unsigned int nr = filp->f_pos;
  102. int ret;
  103. lock_kernel();
  104. if (nr < FIRST_PROCESS_ENTRY) {
  105. int error = proc_readdir(filp, dirent, filldir);
  106. if (error <= 0) {
  107. unlock_kernel();
  108. return error;
  109. }
  110. filp->f_pos = FIRST_PROCESS_ENTRY;
  111. }
  112. unlock_kernel();
  113. ret = proc_pid_readdir(filp, dirent, filldir);
  114. return ret;
  115. }
  116. /*
  117. * The root /proc directory is special, as it has the
  118. * <pid> directories. Thus we don't use the generic
  119. * directory handling functions for that..
  120. */
  121. static struct file_operations proc_root_operations = {
  122. .read = generic_read_dir,
  123. .readdir = proc_root_readdir,
  124. };
  125. /*
  126. * proc root can do almost nothing..
  127. */
  128. static struct inode_operations proc_root_inode_operations = {
  129. .lookup = proc_root_lookup,
  130. .getattr = proc_root_getattr,
  131. };
  132. /*
  133. * This is the root "inode" in the /proc tree..
  134. */
  135. struct proc_dir_entry proc_root = {
  136. .low_ino = PROC_ROOT_INO,
  137. .namelen = 5,
  138. .name = "/proc",
  139. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  140. .nlink = 2,
  141. .proc_iops = &proc_root_inode_operations,
  142. .proc_fops = &proc_root_operations,
  143. .parent = &proc_root,
  144. };
  145. EXPORT_SYMBOL(proc_symlink);
  146. EXPORT_SYMBOL(proc_mkdir);
  147. EXPORT_SYMBOL(create_proc_entry);
  148. EXPORT_SYMBOL(remove_proc_entry);
  149. EXPORT_SYMBOL(proc_root);
  150. EXPORT_SYMBOL(proc_root_fs);
  151. EXPORT_SYMBOL(proc_net);
  152. EXPORT_SYMBOL(proc_net_stat);
  153. EXPORT_SYMBOL(proc_bus);
  154. EXPORT_SYMBOL(proc_root_driver);