Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[pv_ops_mirror.git] / fs / proc / inode.c
blobabe6a3f04368490aa9776dbb919cd1cc5c0dd075
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(struct kmem_cache * cachep, void *foo)
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|SLAB_PANIC),
123 init_once);
124 return 0;
127 static int proc_remount(struct super_block *sb, int *flags, char *data)
129 *flags |= MS_NODIRATIME;
130 return 0;
133 static const struct super_operations proc_sops = {
134 .alloc_inode = proc_alloc_inode,
135 .destroy_inode = proc_destroy_inode,
136 .read_inode = proc_read_inode,
137 .drop_inode = generic_delete_inode,
138 .delete_inode = proc_delete_inode,
139 .statfs = simple_statfs,
140 .remount_fs = proc_remount,
143 static void pde_users_dec(struct proc_dir_entry *pde)
145 spin_lock(&pde->pde_unload_lock);
146 pde->pde_users--;
147 if (pde->pde_unload_completion && pde->pde_users == 0)
148 complete(pde->pde_unload_completion);
149 spin_unlock(&pde->pde_unload_lock);
152 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
154 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
155 loff_t rv = -EINVAL;
156 loff_t (*llseek)(struct file *, loff_t, int);
158 spin_lock(&pde->pde_unload_lock);
160 * remove_proc_entry() is going to delete PDE (as part of module
161 * cleanup sequence). No new callers into module allowed.
163 if (!pde->proc_fops) {
164 spin_unlock(&pde->pde_unload_lock);
165 return rv;
168 * Bump refcount so that remove_proc_entry will wail for ->llseek to
169 * complete.
171 pde->pde_users++;
173 * Save function pointer under lock, to protect against ->proc_fops
174 * NULL'ifying right after ->pde_unload_lock is dropped.
176 llseek = pde->proc_fops->llseek;
177 spin_unlock(&pde->pde_unload_lock);
179 if (!llseek)
180 llseek = default_llseek;
181 rv = llseek(file, offset, whence);
183 pde_users_dec(pde);
184 return rv;
187 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
189 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
190 ssize_t rv = -EIO;
191 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
193 spin_lock(&pde->pde_unload_lock);
194 if (!pde->proc_fops) {
195 spin_unlock(&pde->pde_unload_lock);
196 return rv;
198 pde->pde_users++;
199 read = pde->proc_fops->read;
200 spin_unlock(&pde->pde_unload_lock);
202 if (read)
203 rv = read(file, buf, count, ppos);
205 pde_users_dec(pde);
206 return rv;
209 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
211 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
212 ssize_t rv = -EIO;
213 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
215 spin_lock(&pde->pde_unload_lock);
216 if (!pde->proc_fops) {
217 spin_unlock(&pde->pde_unload_lock);
218 return rv;
220 pde->pde_users++;
221 write = pde->proc_fops->write;
222 spin_unlock(&pde->pde_unload_lock);
224 if (write)
225 rv = write(file, buf, count, ppos);
227 pde_users_dec(pde);
228 return rv;
231 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
233 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
234 unsigned int rv = DEFAULT_POLLMASK;
235 unsigned int (*poll)(struct file *, struct poll_table_struct *);
237 spin_lock(&pde->pde_unload_lock);
238 if (!pde->proc_fops) {
239 spin_unlock(&pde->pde_unload_lock);
240 return rv;
242 pde->pde_users++;
243 poll = pde->proc_fops->poll;
244 spin_unlock(&pde->pde_unload_lock);
246 if (poll)
247 rv = poll(file, pts);
249 pde_users_dec(pde);
250 return rv;
253 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
255 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
256 long rv = -ENOTTY;
257 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
258 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
260 spin_lock(&pde->pde_unload_lock);
261 if (!pde->proc_fops) {
262 spin_unlock(&pde->pde_unload_lock);
263 return rv;
265 pde->pde_users++;
266 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
267 ioctl = pde->proc_fops->ioctl;
268 spin_unlock(&pde->pde_unload_lock);
270 if (unlocked_ioctl) {
271 rv = unlocked_ioctl(file, cmd, arg);
272 if (rv == -ENOIOCTLCMD)
273 rv = -EINVAL;
274 } else if (ioctl) {
275 lock_kernel();
276 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
277 unlock_kernel();
280 pde_users_dec(pde);
281 return rv;
284 #ifdef CONFIG_COMPAT
285 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
287 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
288 long rv = -ENOTTY;
289 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
291 spin_lock(&pde->pde_unload_lock);
292 if (!pde->proc_fops) {
293 spin_unlock(&pde->pde_unload_lock);
294 return rv;
296 pde->pde_users++;
297 compat_ioctl = pde->proc_fops->compat_ioctl;
298 spin_unlock(&pde->pde_unload_lock);
300 if (compat_ioctl)
301 rv = compat_ioctl(file, cmd, arg);
303 pde_users_dec(pde);
304 return rv;
306 #endif
308 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
310 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
311 int rv = -EIO;
312 int (*mmap)(struct file *, struct vm_area_struct *);
314 spin_lock(&pde->pde_unload_lock);
315 if (!pde->proc_fops) {
316 spin_unlock(&pde->pde_unload_lock);
317 return rv;
319 pde->pde_users++;
320 mmap = pde->proc_fops->mmap;
321 spin_unlock(&pde->pde_unload_lock);
323 if (mmap)
324 rv = mmap(file, vma);
326 pde_users_dec(pde);
327 return rv;
330 static int proc_reg_open(struct inode *inode, struct file *file)
332 struct proc_dir_entry *pde = PDE(inode);
333 int rv = 0;
334 int (*open)(struct inode *, struct file *);
336 spin_lock(&pde->pde_unload_lock);
337 if (!pde->proc_fops) {
338 spin_unlock(&pde->pde_unload_lock);
339 return rv;
341 pde->pde_users++;
342 open = pde->proc_fops->open;
343 spin_unlock(&pde->pde_unload_lock);
345 if (open)
346 rv = open(inode, file);
348 pde_users_dec(pde);
349 return rv;
352 static int proc_reg_release(struct inode *inode, struct file *file)
354 struct proc_dir_entry *pde = PDE(inode);
355 int rv = 0;
356 int (*release)(struct inode *, struct file *);
358 spin_lock(&pde->pde_unload_lock);
359 if (!pde->proc_fops) {
360 spin_unlock(&pde->pde_unload_lock);
361 return rv;
363 pde->pde_users++;
364 release = pde->proc_fops->release;
365 spin_unlock(&pde->pde_unload_lock);
367 if (release)
368 rv = release(inode, file);
370 pde_users_dec(pde);
371 return rv;
374 static const struct file_operations proc_reg_file_ops = {
375 .llseek = proc_reg_llseek,
376 .read = proc_reg_read,
377 .write = proc_reg_write,
378 .poll = proc_reg_poll,
379 .unlocked_ioctl = proc_reg_unlocked_ioctl,
380 #ifdef CONFIG_COMPAT
381 .compat_ioctl = proc_reg_compat_ioctl,
382 #endif
383 .mmap = proc_reg_mmap,
384 .open = proc_reg_open,
385 .release = proc_reg_release,
388 #ifdef CONFIG_COMPAT
389 static const struct file_operations proc_reg_file_ops_no_compat = {
390 .llseek = proc_reg_llseek,
391 .read = proc_reg_read,
392 .write = proc_reg_write,
393 .poll = proc_reg_poll,
394 .unlocked_ioctl = proc_reg_unlocked_ioctl,
395 .mmap = proc_reg_mmap,
396 .open = proc_reg_open,
397 .release = proc_reg_release,
399 #endif
401 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
402 struct proc_dir_entry *de)
404 struct inode * inode;
406 if (de != NULL && !try_module_get(de->owner))
407 goto out_mod;
409 inode = iget(sb, ino);
410 if (!inode)
411 goto out_ino;
413 PROC_I(inode)->fd = 0;
414 PROC_I(inode)->pde = de;
415 if (de) {
416 if (de->mode) {
417 inode->i_mode = de->mode;
418 inode->i_uid = de->uid;
419 inode->i_gid = de->gid;
421 if (de->size)
422 inode->i_size = de->size;
423 if (de->nlink)
424 inode->i_nlink = de->nlink;
425 if (de->proc_iops)
426 inode->i_op = de->proc_iops;
427 if (de->proc_fops) {
428 if (S_ISREG(inode->i_mode)) {
429 #ifdef CONFIG_COMPAT
430 if (!de->proc_fops->compat_ioctl)
431 inode->i_fop =
432 &proc_reg_file_ops_no_compat;
433 else
434 #endif
435 inode->i_fop = &proc_reg_file_ops;
437 else
438 inode->i_fop = de->proc_fops;
442 return inode;
444 out_ino:
445 if (de != NULL)
446 module_put(de->owner);
447 out_mod:
448 return NULL;
451 int proc_fill_super(struct super_block *s)
453 struct inode * root_inode;
455 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
456 s->s_blocksize = 1024;
457 s->s_blocksize_bits = 10;
458 s->s_magic = PROC_SUPER_MAGIC;
459 s->s_op = &proc_sops;
460 s->s_time_gran = 1;
462 de_get(&proc_root);
463 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
464 if (!root_inode)
465 goto out_no_root;
466 root_inode->i_uid = 0;
467 root_inode->i_gid = 0;
468 s->s_root = d_alloc_root(root_inode);
469 if (!s->s_root)
470 goto out_no_root;
471 return 0;
473 out_no_root:
474 printk("proc_read_super: get root inode failed\n");
475 iput(root_inode);
476 de_put(&proc_root);
477 return -ENOMEM;
479 MODULE_LICENSE("GPL");