blob: ffe66c38488b6d6925614e00147d213dfc271e0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/root.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc root directory handling functions
7 */
8
9#include <asm/uaccess.h>
10
11#include <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/bitops.h>
18#include <linux/smp_lock.h>
Eric W. Biedermanf6c7a1f2006-10-02 02:17:07 -070019#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Adrian Bunkfee781e2006-01-08 01:04:16 -080021#include "internal.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver;
24
25#ifdef CONFIG_SYSCTL
26struct proc_dir_entry *proc_sys_root;
27#endif
28
David Howells454e2392006-06-23 02:02:57 -070029static int proc_get_sb(struct file_system_type *fs_type,
30 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Eric W. Biedermanf6c7a1f2006-10-02 02:17:07 -070032 if (proc_mnt) {
33 /* Seed the root directory with a pid so it doesn't need
34 * to be special in base.c. I would do this earlier but
35 * the only task alive when /proc is mounted the first time
36 * is the init_task and it doesn't have any pids.
37 */
38 struct proc_inode *ei;
39 ei = PROC_I(proc_mnt->mnt_sb->s_root->d_inode);
40 if (!ei->pid)
41 ei->pid = find_get_pid(1);
42 }
David Howells454e2392006-06-23 02:02:57 -070043 return get_sb_single(fs_type, flags, data, proc_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46static struct file_system_type proc_fs_type = {
47 .name = "proc",
48 .get_sb = proc_get_sb,
49 .kill_sb = kill_anon_super,
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052void __init proc_root_init(void)
53{
54 int err = proc_init_inodecache();
55 if (err)
56 return;
57 err = register_filesystem(&proc_fs_type);
58 if (err)
59 return;
60 proc_mnt = kern_mount(&proc_fs_type);
61 err = PTR_ERR(proc_mnt);
62 if (IS_ERR(proc_mnt)) {
63 unregister_filesystem(&proc_fs_type);
64 return;
65 }
66 proc_misc_init();
67 proc_net = proc_mkdir("net", NULL);
68 proc_net_stat = proc_mkdir("net/stat", NULL);
69
70#ifdef CONFIG_SYSVIPC
71 proc_mkdir("sysvipc", NULL);
72#endif
73#ifdef CONFIG_SYSCTL
74 proc_sys_root = proc_mkdir("sys", NULL);
75#endif
76#if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
77 proc_mkdir("sys/fs", NULL);
78 proc_mkdir("sys/fs/binfmt_misc", NULL);
79#endif
80 proc_root_fs = proc_mkdir("fs", NULL);
81 proc_root_driver = proc_mkdir("driver", NULL);
82 proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
83#if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
84 /* just give it a mountpoint */
85 proc_mkdir("openprom", NULL);
86#endif
87 proc_tty_init();
88#ifdef CONFIG_PROC_DEVICETREE
89 proc_device_tree_init();
90#endif
91 proc_bus = proc_mkdir("bus", NULL);
92}
93
Al Viro76b61592006-02-08 14:37:40 -050094static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
95)
96{
97 generic_fillattr(dentry->d_inode, stat);
98 stat->nlink = proc_root.nlink + nr_processes();
99 return 0;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (!proc_lookup(dir, dentry, nd)) {
105 return NULL;
106 }
107
108 return proc_pid_lookup(dir, dentry, nd);
109}
110
111static int proc_root_readdir(struct file * filp,
112 void * dirent, filldir_t filldir)
113{
114 unsigned int nr = filp->f_pos;
115 int ret;
116
117 lock_kernel();
118
119 if (nr < FIRST_PROCESS_ENTRY) {
120 int error = proc_readdir(filp, dirent, filldir);
121 if (error <= 0) {
122 unlock_kernel();
123 return error;
124 }
125 filp->f_pos = FIRST_PROCESS_ENTRY;
126 }
127 unlock_kernel();
128
129 ret = proc_pid_readdir(filp, dirent, filldir);
130 return ret;
131}
132
133/*
134 * The root /proc directory is special, as it has the
135 * <pid> directories. Thus we don't use the generic
136 * directory handling functions for that..
137 */
138static struct file_operations proc_root_operations = {
139 .read = generic_read_dir,
140 .readdir = proc_root_readdir,
141};
142
143/*
144 * proc root can do almost nothing..
145 */
146static struct inode_operations proc_root_inode_operations = {
147 .lookup = proc_root_lookup,
Al Viro76b61592006-02-08 14:37:40 -0500148 .getattr = proc_root_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151/*
152 * This is the root "inode" in the /proc tree..
153 */
154struct 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 .proc_iops = &proc_root_inode_operations,
161 .proc_fops = &proc_root_operations,
162 .parent = &proc_root,
163};
164
165EXPORT_SYMBOL(proc_symlink);
166EXPORT_SYMBOL(proc_mkdir);
167EXPORT_SYMBOL(create_proc_entry);
168EXPORT_SYMBOL(remove_proc_entry);
169EXPORT_SYMBOL(proc_root);
170EXPORT_SYMBOL(proc_root_fs);
171EXPORT_SYMBOL(proc_net);
172EXPORT_SYMBOL(proc_net_stat);
173EXPORT_SYMBOL(proc_bus);
174EXPORT_SYMBOL(proc_root_driver);