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