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