inode.c 4.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  linux/fs/proc/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

#include <linux/time.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/file.h>
#include <linux/limits.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/smp_lock.h>

#include <asm/system.h>
#include <asm/uaccess.h>

22
#include "internal.h"
L
Linus Torvalds 已提交
23

24
struct proc_dir_entry *de_get(struct proc_dir_entry *de)
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33
{
	if (de)
		atomic_inc(&de->count);
	return de;
}

/*
 * Decrements the use count and checks for deferred deletion.
 */
34
void de_put(struct proc_dir_entry *de)
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	if (de) {	
		lock_kernel();		
		if (!atomic_read(&de->count)) {
			printk("de_put: entry %s already free!\n", de->name);
			unlock_kernel();
			return;
		}

		if (atomic_dec_and_test(&de->count)) {
			if (de->deleted) {
				printk("de_put: deferred delete of %s\n",
					de->name);
				free_proc_entry(de);
			}
		}		
		unlock_kernel();
	}
}

/*
 * Decrement the use count of the proc_dir_entry.
 */
static void proc_delete_inode(struct inode *inode)
{
	struct proc_dir_entry *de;

62 63
	truncate_inode_pages(&inode->i_data, 0);

64
	/* Stop tracking associated processes */
65
	put_pid(PROC_I(inode)->pid);
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	/* Let go of any associated proc directory entry */
	de = PROC_I(inode)->pde;
	if (de) {
		if (de->owner)
			module_put(de->owner);
		de_put(de);
	}
	clear_inode(inode);
}

struct vfsmount *proc_mnt;

static void proc_read_inode(struct inode * inode)
{
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
}

84
static struct kmem_cache * proc_inode_cachep;
L
Linus Torvalds 已提交
85 86 87 88 89 90

static struct inode *proc_alloc_inode(struct super_block *sb)
{
	struct proc_inode *ei;
	struct inode *inode;

91
	ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
92 93
	if (!ei)
		return NULL;
94
	ei->pid = NULL;
95
	ei->fd = 0;
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106 107
	ei->op.proc_get_link = NULL;
	ei->pde = NULL;
	inode = &ei->vfs_inode;
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
	return inode;
}

static void proc_destroy_inode(struct inode *inode)
{
	kmem_cache_free(proc_inode_cachep, PROC_I(inode));
}

108
static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
L
Linus Torvalds 已提交
109 110 111
{
	struct proc_inode *ei = (struct proc_inode *) foo;

C
Christoph Lameter 已提交
112
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
113 114 115 116 117 118
}
 
int __init proc_init_inodecache(void)
{
	proc_inode_cachep = kmem_cache_create("proc_inode_cache",
					     sizeof(struct proc_inode),
119 120
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132
					     init_once, NULL);
	if (proc_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static int proc_remount(struct super_block *sb, int *flags, char *data)
{
	*flags |= MS_NODIRATIME;
	return 0;
}

133
static const struct super_operations proc_sops = {
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
	.alloc_inode	= proc_alloc_inode,
	.destroy_inode	= proc_destroy_inode,
	.read_inode	= proc_read_inode,
	.drop_inode	= generic_delete_inode,
	.delete_inode	= proc_delete_inode,
	.statfs		= simple_statfs,
	.remount_fs	= proc_remount,
};

struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
				struct proc_dir_entry *de)
{
	struct inode * inode;

148 149 150
	if (de != NULL && !try_module_get(de->owner))
		goto out_mod;

L
Linus Torvalds 已提交
151 152
	inode = iget(sb, ino);
	if (!inode)
153 154
		goto out_ino;

155
	PROC_I(inode)->fd = 0;
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	PROC_I(inode)->pde = de;
	if (de) {
		if (de->mode) {
			inode->i_mode = de->mode;
			inode->i_uid = de->uid;
			inode->i_gid = de->gid;
		}
		if (de->size)
			inode->i_size = de->size;
		if (de->nlink)
			inode->i_nlink = de->nlink;
		if (de->proc_iops)
			inode->i_op = de->proc_iops;
		if (de->proc_fops)
			inode->i_fop = de->proc_fops;
	}

	return inode;

175 176 177 178 179
out_ino:
	if (de != NULL)
		module_put(de->owner);
out_mod:
	return NULL;
L
Linus Torvalds 已提交
180 181 182 183 184 185
}			

int proc_fill_super(struct super_block *s, void *data, int silent)
{
	struct inode * root_inode;

186
	s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
L
Linus Torvalds 已提交
187 188 189 190 191 192
	s->s_blocksize = 1024;
	s->s_blocksize_bits = 10;
	s->s_magic = PROC_SUPER_MAGIC;
	s->s_op = &proc_sops;
	s->s_time_gran = 1;
	
193
	de_get(&proc_root);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
	root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
	if (!root_inode)
		goto out_no_root;
	root_inode->i_uid = 0;
	root_inode->i_gid = 0;
	s->s_root = d_alloc_root(root_inode);
	if (!s->s_root)
		goto out_no_root;
	return 0;

out_no_root:
	printk("proc_read_super: get root inode failed\n");
	iput(root_inode);
207
	de_put(&proc_root);
L
Linus Torvalds 已提交
208 209 210
	return -ENOMEM;
}
MODULE_LICENSE("GPL");