[ALSA] Clean up duplicate includes in sound/core/
[cascardo/linux.git] / fs / proc / inode.c
1 /*
2  *  linux/fs/proc/inode.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
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>
20
21 #include <asm/system.h>
22 #include <asm/uaccess.h>
23
24 #include "internal.h"
25
26 struct proc_dir_entry *de_get(struct proc_dir_entry *de)
27 {
28         if (de)
29                 atomic_inc(&de->count);
30         return de;
31 }
32
33 /*
34  * Decrements the use count and checks for deferred deletion.
35  */
36 void de_put(struct proc_dir_entry *de)
37 {
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;
44                 }
45
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);
51                         }
52                 }               
53                 unlock_kernel();
54         }
55 }
56
57 /*
58  * Decrement the use count of the proc_dir_entry.
59  */
60 static void proc_delete_inode(struct inode *inode)
61 {
62         struct proc_dir_entry *de;
63
64         truncate_inode_pages(&inode->i_data, 0);
65
66         /* Stop tracking associated processes */
67         put_pid(PROC_I(inode)->pid);
68
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);
75         }
76         clear_inode(inode);
77 }
78
79 struct vfsmount *proc_mnt;
80
81 static void proc_read_inode(struct inode * inode)
82 {
83         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
84 }
85
86 static struct kmem_cache * proc_inode_cachep;
87
88 static struct inode *proc_alloc_inode(struct super_block *sb)
89 {
90         struct proc_inode *ei;
91         struct inode *inode;
92
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;
103 }
104
105 static void proc_destroy_inode(struct inode *inode)
106 {
107         kmem_cache_free(proc_inode_cachep, PROC_I(inode));
108 }
109
110 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
111 {
112         struct proc_inode *ei = (struct proc_inode *) foo;
113
114         inode_init_once(&ei->vfs_inode);
115 }
116
117 int __init proc_init_inodecache(void)
118 {
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;
127 }
128
129 static int proc_remount(struct super_block *sb, int *flags, char *data)
130 {
131         *flags |= MS_NODIRATIME;
132         return 0;
133 }
134
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,
143 };
144
145 static void pde_users_dec(struct proc_dir_entry *pde)
146 {
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);
152 }
153
154 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
155 {
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);
159
160         spin_lock(&pde->pde_unload_lock);
161         /*
162          * remove_proc_entry() is going to delete PDE (as part of module
163          * cleanup sequence). No new callers into module allowed.
164          */
165         if (!pde->proc_fops) {
166                 spin_unlock(&pde->pde_unload_lock);
167                 return rv;
168         }
169         /*
170          * Bump refcount so that remove_proc_entry will wail for ->llseek to
171          * complete.
172          */
173         pde->pde_users++;
174         /*
175          * Save function pointer under lock, to protect against ->proc_fops
176          * NULL'ifying right after ->pde_unload_lock is dropped.
177          */
178         llseek = pde->proc_fops->llseek;
179         spin_unlock(&pde->pde_unload_lock);
180
181         if (!llseek)
182                 llseek = default_llseek;
183         rv = llseek(file, offset, whence);
184
185         pde_users_dec(pde);
186         return rv;
187 }
188
189 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
190 {
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 *);
194
195         spin_lock(&pde->pde_unload_lock);
196         if (!pde->proc_fops) {
197                 spin_unlock(&pde->pde_unload_lock);
198                 return rv;
199         }
200         pde->pde_users++;
201         read = pde->proc_fops->read;
202         spin_unlock(&pde->pde_unload_lock);
203
204         if (read)
205                 rv = read(file, buf, count, ppos);
206
207         pde_users_dec(pde);
208         return rv;
209 }
210
211 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
212 {
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 *);
216
217         spin_lock(&pde->pde_unload_lock);
218         if (!pde->proc_fops) {
219                 spin_unlock(&pde->pde_unload_lock);
220                 return rv;
221         }
222         pde->pde_users++;
223         write = pde->proc_fops->write;
224         spin_unlock(&pde->pde_unload_lock);
225
226         if (write)
227                 rv = write(file, buf, count, ppos);
228
229         pde_users_dec(pde);
230         return rv;
231 }
232
233 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
234 {
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 *);
238
239         spin_lock(&pde->pde_unload_lock);
240         if (!pde->proc_fops) {
241                 spin_unlock(&pde->pde_unload_lock);
242                 return rv;
243         }
244         pde->pde_users++;
245         poll = pde->proc_fops->poll;
246         spin_unlock(&pde->pde_unload_lock);
247
248         if (poll)
249                 rv = poll(file, pts);
250
251         pde_users_dec(pde);
252         return rv;
253 }
254
255 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
256 {
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);
261
262         spin_lock(&pde->pde_unload_lock);
263         if (!pde->proc_fops) {
264                 spin_unlock(&pde->pde_unload_lock);
265                 return rv;
266         }
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);
271
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();
280         }
281
282         pde_users_dec(pde);
283         return rv;
284 }
285
286 #ifdef CONFIG_COMPAT
287 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
288 {
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);
292
293         spin_lock(&pde->pde_unload_lock);
294         if (!pde->proc_fops) {
295                 spin_unlock(&pde->pde_unload_lock);
296                 return rv;
297         }
298         pde->pde_users++;
299         compat_ioctl = pde->proc_fops->compat_ioctl;
300         spin_unlock(&pde->pde_unload_lock);
301
302         if (compat_ioctl)
303                 rv = compat_ioctl(file, cmd, arg);
304
305         pde_users_dec(pde);
306         return rv;
307 }
308 #endif
309
310 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
311 {
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 *);
315
316         spin_lock(&pde->pde_unload_lock);
317         if (!pde->proc_fops) {
318                 spin_unlock(&pde->pde_unload_lock);
319                 return rv;
320         }
321         pde->pde_users++;
322         mmap = pde->proc_fops->mmap;
323         spin_unlock(&pde->pde_unload_lock);
324
325         if (mmap)
326                 rv = mmap(file, vma);
327
328         pde_users_dec(pde);
329         return rv;
330 }
331
332 static int proc_reg_open(struct inode *inode, struct file *file)
333 {
334         struct proc_dir_entry *pde = PDE(inode);
335         int rv = 0;
336         int (*open)(struct inode *, struct file *);
337
338         spin_lock(&pde->pde_unload_lock);
339         if (!pde->proc_fops) {
340                 spin_unlock(&pde->pde_unload_lock);
341                 return rv;
342         }
343         pde->pde_users++;
344         open = pde->proc_fops->open;
345         spin_unlock(&pde->pde_unload_lock);
346
347         if (open)
348                 rv = open(inode, file);
349
350         pde_users_dec(pde);
351         return rv;
352 }
353
354 static int proc_reg_release(struct inode *inode, struct file *file)
355 {
356         struct proc_dir_entry *pde = PDE(inode);
357         int rv = 0;
358         int (*release)(struct inode *, struct file *);
359
360         spin_lock(&pde->pde_unload_lock);
361         if (!pde->proc_fops) {
362                 spin_unlock(&pde->pde_unload_lock);
363                 return rv;
364         }
365         pde->pde_users++;
366         release = pde->proc_fops->release;
367         spin_unlock(&pde->pde_unload_lock);
368
369         if (release)
370                 rv = release(inode, file);
371
372         pde_users_dec(pde);
373         return rv;
374 }
375
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,
388 };
389
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,
400 };
401 #endif
402
403 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
404                                 struct proc_dir_entry *de)
405 {
406         struct inode * inode;
407
408         if (de != NULL && !try_module_get(de->owner))
409                 goto out_mod;
410
411         inode = iget(sb, ino);
412         if (!inode)
413                 goto out_ino;
414
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;
422                 }
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;
438                         }
439                         else
440                                 inode->i_fop = de->proc_fops;
441                 }
442         }
443
444         return inode;
445
446 out_ino:
447         if (de != NULL)
448                 module_put(de->owner);
449 out_mod:
450         return NULL;
451 }                       
452
453 int proc_fill_super(struct super_block *s, void *data, int silent)
454 {
455         struct inode * root_inode;
456
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;
463         
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;
474
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;
480 }
481 MODULE_LICENSE("GPL");