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