root.c 3.8 KB

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