USB: drivers/usb/misc/sisusbvga/sisusb.c: kill two unused variables
[linux-2.6/next.git] / fs / proc / inode.c
blob0e4d37c93eea70208a82a5861735817209082fd7
1 /*
2 * linux/fs/proc/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/time.h>
8 #include <linux/proc_fs.h>
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/string.h>
12 #include <linux/stat.h>
13 #include <linux/completion.h>
14 #include <linux/poll.h>
15 #include <linux/file.h>
16 #include <linux/limits.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/smp_lock.h>
21 #include <asm/system.h>
22 #include <asm/uaccess.h>
24 #include "internal.h"
26 struct proc_dir_entry *de_get(struct proc_dir_entry *de)
28 if (de)
29 atomic_inc(&de->count);
30 return de;
34 * Decrements the use count and checks for deferred deletion.
36 void de_put(struct proc_dir_entry *de)
38 if (de) {
39 lock_kernel();
40 if (!atomic_read(&de->count)) {
41 printk("de_put: entry %s already free!\n", de->name);
42 unlock_kernel();
43 return;
46 if (atomic_dec_and_test(&de->count)) {
47 if (de->deleted) {
48 printk("de_put: deferred delete of %s\n",
49 de->name);
50 free_proc_entry(de);
53 unlock_kernel();
58 * Decrement the use count of the proc_dir_entry.
60 static void proc_delete_inode(struct inode *inode)
62 struct proc_dir_entry *de;
64 truncate_inode_pages(&inode->i_data, 0);
66 /* Stop tracking associated processes */
67 put_pid(PROC_I(inode)->pid);
69 /* Let go of any associated proc directory entry */
70 de = PROC_I(inode)->pde;
71 if (de) {
72 if (de->owner)
73 module_put(de->owner);
74 de_put(de);
76 clear_inode(inode);
79 struct vfsmount *proc_mnt;
81 static void proc_read_inode(struct inode * inode)
83 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
86 static struct kmem_cache * proc_inode_cachep;
88 static struct inode *proc_alloc_inode(struct super_block *sb)
90 struct proc_inode *ei;
91 struct inode *inode;
93 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
94 if (!ei)
95 return NULL;
96 ei->pid = NULL;
97 ei->fd = 0;
98 ei->op.proc_get_link = NULL;
99 ei->pde = NULL;
100 inode = &ei->vfs_inode;
101 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
102 return inode;
105 static void proc_destroy_inode(struct inode *inode)
107 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
110 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
112 struct proc_inode *ei = (struct proc_inode *) foo;
114 inode_init_once(&ei->vfs_inode);
117 int __init proc_init_inodecache(void)
119 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
120 sizeof(struct proc_inode),
121 0, (SLAB_RECLAIM_ACCOUNT|
122 SLAB_MEM_SPREAD),
123 init_once);
124 if (proc_inode_cachep == NULL)
125 return -ENOMEM;
126 return 0;
129 static int proc_remount(struct super_block *sb, int *flags, char *data)
131 *flags |= MS_NODIRATIME;
132 return 0;
135 static const struct super_operations proc_sops = {
136 .alloc_inode = proc_alloc_inode,
137 .destroy_inode = proc_destroy_inode,
138 .read_inode = proc_read_inode,
139 .drop_inode = generic_delete_inode,
140 .delete_inode = proc_delete_inode,
141 .statfs = simple_statfs,
142 .remount_fs = proc_remount,
145 static void pde_users_dec(struct proc_dir_entry *pde)
147 spin_lock(&pde->pde_unload_lock);
148 pde->pde_users--;
149 if (pde->pde_unload_completion && pde->pde_users == 0)
150 complete(pde->pde_unload_completion);
151 spin_unlock(&pde->pde_unload_lock);
154 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
156 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
157 loff_t rv = -EINVAL;
158 loff_t (*llseek)(struct file *, loff_t, int);
160 spin_lock(&pde->pde_unload_lock);
162 * remove_proc_entry() is going to delete PDE (as part of module
163 * cleanup sequence). No new callers into module allowed.
165 if (!pde->proc_fops) {
166 spin_unlock(&pde->pde_unload_lock);
167 return rv;
170 * Bump refcount so that remove_proc_entry will wail for ->llseek to
171 * complete.
173 pde->pde_users++;
175 * Save function pointer under lock, to protect against ->proc_fops
176 * NULL'ifying right after ->pde_unload_lock is dropped.
178 llseek = pde->proc_fops->llseek;
179 spin_unlock(&pde->pde_unload_lock);
181 if (!llseek)
182 llseek = default_llseek;
183 rv = llseek(file, offset, whence);
185 pde_users_dec(pde);
186 return rv;
189 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
191 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
192 ssize_t rv = -EIO;
193 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
195 spin_lock(&pde->pde_unload_lock);
196 if (!pde->proc_fops) {
197 spin_unlock(&pde->pde_unload_lock);
198 return rv;
200 pde->pde_users++;
201 read = pde->proc_fops->read;
202 spin_unlock(&pde->pde_unload_lock);
204 if (read)
205 rv = read(file, buf, count, ppos);
207 pde_users_dec(pde);
208 return rv;
211 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
213 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
214 ssize_t rv = -EIO;
215 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
217 spin_lock(&pde->pde_unload_lock);
218 if (!pde->proc_fops) {
219 spin_unlock(&pde->pde_unload_lock);
220 return rv;
222 pde->pde_users++;
223 write = pde->proc_fops->write;
224 spin_unlock(&pde->pde_unload_lock);
226 if (write)
227 rv = write(file, buf, count, ppos);
229 pde_users_dec(pde);
230 return rv;
233 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
235 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
236 unsigned int rv = DEFAULT_POLLMASK;
237 unsigned int (*poll)(struct file *, struct poll_table_struct *);
239 spin_lock(&pde->pde_unload_lock);
240 if (!pde->proc_fops) {
241 spin_unlock(&pde->pde_unload_lock);
242 return rv;
244 pde->pde_users++;
245 poll = pde->proc_fops->poll;
246 spin_unlock(&pde->pde_unload_lock);
248 if (poll)
249 rv = poll(file, pts);
251 pde_users_dec(pde);
252 return rv;
255 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
257 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
258 long rv = -ENOTTY;
259 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
260 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
262 spin_lock(&pde->pde_unload_lock);
263 if (!pde->proc_fops) {
264 spin_unlock(&pde->pde_unload_lock);
265 return rv;
267 pde->pde_users++;
268 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
269 ioctl = pde->proc_fops->ioctl;
270 spin_unlock(&pde->pde_unload_lock);
272 if (unlocked_ioctl) {
273 rv = unlocked_ioctl(file, cmd, arg);
274 if (rv == -ENOIOCTLCMD)
275 rv = -EINVAL;
276 } else if (ioctl) {
277 lock_kernel();
278 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
279 unlock_kernel();
282 pde_users_dec(pde);
283 return rv;
286 #ifdef CONFIG_COMPAT
287 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
289 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
290 long rv = -ENOTTY;
291 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
293 spin_lock(&pde->pde_unload_lock);
294 if (!pde->proc_fops) {
295 spin_unlock(&pde->pde_unload_lock);
296 return rv;
298 pde->pde_users++;
299 compat_ioctl = pde->proc_fops->compat_ioctl;
300 spin_unlock(&pde->pde_unload_lock);
302 if (compat_ioctl)
303 rv = compat_ioctl(file, cmd, arg);
305 pde_users_dec(pde);
306 return rv;
308 #endif
310 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
312 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
313 int rv = -EIO;
314 int (*mmap)(struct file *, struct vm_area_struct *);
316 spin_lock(&pde->pde_unload_lock);
317 if (!pde->proc_fops) {
318 spin_unlock(&pde->pde_unload_lock);
319 return rv;
321 pde->pde_users++;
322 mmap = pde->proc_fops->mmap;
323 spin_unlock(&pde->pde_unload_lock);
325 if (mmap)
326 rv = mmap(file, vma);
328 pde_users_dec(pde);
329 return rv;
332 static int proc_reg_open(struct inode *inode, struct file *file)
334 struct proc_dir_entry *pde = PDE(inode);
335 int rv = 0;
336 int (*open)(struct inode *, struct file *);
338 spin_lock(&pde->pde_unload_lock);
339 if (!pde->proc_fops) {
340 spin_unlock(&pde->pde_unload_lock);
341 return rv;
343 pde->pde_users++;
344 open = pde->proc_fops->open;
345 spin_unlock(&pde->pde_unload_lock);
347 if (open)
348 rv = open(inode, file);
350 pde_users_dec(pde);
351 return rv;
354 static int proc_reg_release(struct inode *inode, struct file *file)
356 struct proc_dir_entry *pde = PDE(inode);
357 int rv = 0;
358 int (*release)(struct inode *, struct file *);
360 spin_lock(&pde->pde_unload_lock);
361 if (!pde->proc_fops) {
362 spin_unlock(&pde->pde_unload_lock);
363 return rv;
365 pde->pde_users++;
366 release = pde->proc_fops->release;
367 spin_unlock(&pde->pde_unload_lock);
369 if (release)
370 rv = release(inode, file);
372 pde_users_dec(pde);
373 return rv;
376 static const struct file_operations proc_reg_file_ops = {
377 .llseek = proc_reg_llseek,
378 .read = proc_reg_read,
379 .write = proc_reg_write,
380 .poll = proc_reg_poll,
381 .unlocked_ioctl = proc_reg_unlocked_ioctl,
382 #ifdef CONFIG_COMPAT
383 .compat_ioctl = proc_reg_compat_ioctl,
384 #endif
385 .mmap = proc_reg_mmap,
386 .open = proc_reg_open,
387 .release = proc_reg_release,
390 #ifdef CONFIG_COMPAT
391 static const struct file_operations proc_reg_file_ops_no_compat = {
392 .llseek = proc_reg_llseek,
393 .read = proc_reg_read,
394 .write = proc_reg_write,
395 .poll = proc_reg_poll,
396 .unlocked_ioctl = proc_reg_unlocked_ioctl,
397 .mmap = proc_reg_mmap,
398 .open = proc_reg_open,
399 .release = proc_reg_release,
401 #endif
403 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
404 struct proc_dir_entry *de)
406 struct inode * inode;
408 if (de != NULL && !try_module_get(de->owner))
409 goto out_mod;
411 inode = iget(sb, ino);
412 if (!inode)
413 goto out_ino;
415 PROC_I(inode)->fd = 0;
416 PROC_I(inode)->pde = de;
417 if (de) {
418 if (de->mode) {
419 inode->i_mode = de->mode;
420 inode->i_uid = de->uid;
421 inode->i_gid = de->gid;
423 if (de->size)
424 inode->i_size = de->size;
425 if (de->nlink)
426 inode->i_nlink = de->nlink;
427 if (de->proc_iops)
428 inode->i_op = de->proc_iops;
429 if (de->proc_fops) {
430 if (S_ISREG(inode->i_mode)) {
431 #ifdef CONFIG_COMPAT
432 if (!de->proc_fops->compat_ioctl)
433 inode->i_fop =
434 &proc_reg_file_ops_no_compat;
435 else
436 #endif
437 inode->i_fop = &proc_reg_file_ops;
439 else
440 inode->i_fop = de->proc_fops;
444 return inode;
446 out_ino:
447 if (de != NULL)
448 module_put(de->owner);
449 out_mod:
450 return NULL;
453 int proc_fill_super(struct super_block *s, void *data, int silent)
455 struct inode * root_inode;
457 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
458 s->s_blocksize = 1024;
459 s->s_blocksize_bits = 10;
460 s->s_magic = PROC_SUPER_MAGIC;
461 s->s_op = &proc_sops;
462 s->s_time_gran = 1;
464 de_get(&proc_root);
465 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
466 if (!root_inode)
467 goto out_no_root;
468 root_inode->i_uid = 0;
469 root_inode->i_gid = 0;
470 s->s_root = d_alloc_root(root_inode);
471 if (!s->s_root)
472 goto out_no_root;
473 return 0;
475 out_no_root:
476 printk("proc_read_super: get root inode failed\n");
477 iput(root_inode);
478 de_put(&proc_root);
479 return -ENOMEM;
481 MODULE_LICENSE("GPL");