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