proc: always do ->release
[safe/jmp/linux-2.6] / 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         atomic_inc(&de->count);
29         return de;
30 }
31
32 /*
33  * Decrements the use count and checks for deferred deletion.
34  */
35 void de_put(struct proc_dir_entry *de)
36 {
37         lock_kernel();
38         if (!atomic_read(&de->count)) {
39                 printk("de_put: entry %s already free!\n", de->name);
40                 unlock_kernel();
41                 return;
42         }
43
44         if (atomic_dec_and_test(&de->count))
45                 free_proc_entry(de);
46         unlock_kernel();
47 }
48
49 /*
50  * Decrement the use count of the proc_dir_entry.
51  */
52 static void proc_delete_inode(struct inode *inode)
53 {
54         struct proc_dir_entry *de;
55
56         truncate_inode_pages(&inode->i_data, 0);
57
58         /* Stop tracking associated processes */
59         put_pid(PROC_I(inode)->pid);
60
61         /* Let go of any associated proc directory entry */
62         de = PROC_I(inode)->pde;
63         if (de) {
64                 if (de->owner)
65                         module_put(de->owner);
66                 de_put(de);
67         }
68         clear_inode(inode);
69 }
70
71 struct vfsmount *proc_mnt;
72
73 static struct kmem_cache * proc_inode_cachep;
74
75 static struct inode *proc_alloc_inode(struct super_block *sb)
76 {
77         struct proc_inode *ei;
78         struct inode *inode;
79
80         ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
81         if (!ei)
82                 return NULL;
83         ei->pid = NULL;
84         ei->fd = 0;
85         ei->op.proc_get_link = NULL;
86         ei->pde = NULL;
87         inode = &ei->vfs_inode;
88         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
89         return inode;
90 }
91
92 static void proc_destroy_inode(struct inode *inode)
93 {
94         kmem_cache_free(proc_inode_cachep, PROC_I(inode));
95 }
96
97 static void init_once(struct kmem_cache * cachep, void *foo)
98 {
99         struct proc_inode *ei = (struct proc_inode *) foo;
100
101         inode_init_once(&ei->vfs_inode);
102 }
103
104 int __init proc_init_inodecache(void)
105 {
106         proc_inode_cachep = kmem_cache_create("proc_inode_cache",
107                                              sizeof(struct proc_inode),
108                                              0, (SLAB_RECLAIM_ACCOUNT|
109                                                 SLAB_MEM_SPREAD|SLAB_PANIC),
110                                              init_once);
111         return 0;
112 }
113
114 static int proc_remount(struct super_block *sb, int *flags, char *data)
115 {
116         *flags |= MS_NODIRATIME;
117         return 0;
118 }
119
120 static const struct super_operations proc_sops = {
121         .alloc_inode    = proc_alloc_inode,
122         .destroy_inode  = proc_destroy_inode,
123         .drop_inode     = generic_delete_inode,
124         .delete_inode   = proc_delete_inode,
125         .statfs         = simple_statfs,
126         .remount_fs     = proc_remount,
127 };
128
129 static void __pde_users_dec(struct proc_dir_entry *pde)
130 {
131         pde->pde_users--;
132         if (pde->pde_unload_completion && pde->pde_users == 0)
133                 complete(pde->pde_unload_completion);
134 }
135
136 static void pde_users_dec(struct proc_dir_entry *pde)
137 {
138         spin_lock(&pde->pde_unload_lock);
139         __pde_users_dec(pde);
140         spin_unlock(&pde->pde_unload_lock);
141 }
142
143 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
144 {
145         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
146         loff_t rv = -EINVAL;
147         loff_t (*llseek)(struct file *, loff_t, int);
148
149         spin_lock(&pde->pde_unload_lock);
150         /*
151          * remove_proc_entry() is going to delete PDE (as part of module
152          * cleanup sequence). No new callers into module allowed.
153          */
154         if (!pde->proc_fops) {
155                 spin_unlock(&pde->pde_unload_lock);
156                 return rv;
157         }
158         /*
159          * Bump refcount so that remove_proc_entry will wail for ->llseek to
160          * complete.
161          */
162         pde->pde_users++;
163         /*
164          * Save function pointer under lock, to protect against ->proc_fops
165          * NULL'ifying right after ->pde_unload_lock is dropped.
166          */
167         llseek = pde->proc_fops->llseek;
168         spin_unlock(&pde->pde_unload_lock);
169
170         if (!llseek)
171                 llseek = default_llseek;
172         rv = llseek(file, offset, whence);
173
174         pde_users_dec(pde);
175         return rv;
176 }
177
178 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
179 {
180         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
181         ssize_t rv = -EIO;
182         ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
183
184         spin_lock(&pde->pde_unload_lock);
185         if (!pde->proc_fops) {
186                 spin_unlock(&pde->pde_unload_lock);
187                 return rv;
188         }
189         pde->pde_users++;
190         read = pde->proc_fops->read;
191         spin_unlock(&pde->pde_unload_lock);
192
193         if (read)
194                 rv = read(file, buf, count, ppos);
195
196         pde_users_dec(pde);
197         return rv;
198 }
199
200 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
201 {
202         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
203         ssize_t rv = -EIO;
204         ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
205
206         spin_lock(&pde->pde_unload_lock);
207         if (!pde->proc_fops) {
208                 spin_unlock(&pde->pde_unload_lock);
209                 return rv;
210         }
211         pde->pde_users++;
212         write = pde->proc_fops->write;
213         spin_unlock(&pde->pde_unload_lock);
214
215         if (write)
216                 rv = write(file, buf, count, ppos);
217
218         pde_users_dec(pde);
219         return rv;
220 }
221
222 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
223 {
224         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
225         unsigned int rv = DEFAULT_POLLMASK;
226         unsigned int (*poll)(struct file *, struct poll_table_struct *);
227
228         spin_lock(&pde->pde_unload_lock);
229         if (!pde->proc_fops) {
230                 spin_unlock(&pde->pde_unload_lock);
231                 return rv;
232         }
233         pde->pde_users++;
234         poll = pde->proc_fops->poll;
235         spin_unlock(&pde->pde_unload_lock);
236
237         if (poll)
238                 rv = poll(file, pts);
239
240         pde_users_dec(pde);
241         return rv;
242 }
243
244 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
245 {
246         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
247         long rv = -ENOTTY;
248         long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
249         int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
250
251         spin_lock(&pde->pde_unload_lock);
252         if (!pde->proc_fops) {
253                 spin_unlock(&pde->pde_unload_lock);
254                 return rv;
255         }
256         pde->pde_users++;
257         unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
258         ioctl = pde->proc_fops->ioctl;
259         spin_unlock(&pde->pde_unload_lock);
260
261         if (unlocked_ioctl) {
262                 rv = unlocked_ioctl(file, cmd, arg);
263                 if (rv == -ENOIOCTLCMD)
264                         rv = -EINVAL;
265         } else if (ioctl) {
266                 lock_kernel();
267                 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
268                 unlock_kernel();
269         }
270
271         pde_users_dec(pde);
272         return rv;
273 }
274
275 #ifdef CONFIG_COMPAT
276 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
277 {
278         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
279         long rv = -ENOTTY;
280         long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
281
282         spin_lock(&pde->pde_unload_lock);
283         if (!pde->proc_fops) {
284                 spin_unlock(&pde->pde_unload_lock);
285                 return rv;
286         }
287         pde->pde_users++;
288         compat_ioctl = pde->proc_fops->compat_ioctl;
289         spin_unlock(&pde->pde_unload_lock);
290
291         if (compat_ioctl)
292                 rv = compat_ioctl(file, cmd, arg);
293
294         pde_users_dec(pde);
295         return rv;
296 }
297 #endif
298
299 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
300 {
301         struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
302         int rv = -EIO;
303         int (*mmap)(struct file *, struct vm_area_struct *);
304
305         spin_lock(&pde->pde_unload_lock);
306         if (!pde->proc_fops) {
307                 spin_unlock(&pde->pde_unload_lock);
308                 return rv;
309         }
310         pde->pde_users++;
311         mmap = pde->proc_fops->mmap;
312         spin_unlock(&pde->pde_unload_lock);
313
314         if (mmap)
315                 rv = mmap(file, vma);
316
317         pde_users_dec(pde);
318         return rv;
319 }
320
321 static int proc_reg_open(struct inode *inode, struct file *file)
322 {
323         struct proc_dir_entry *pde = PDE(inode);
324         int rv = 0;
325         int (*open)(struct inode *, struct file *);
326         int (*release)(struct inode *, struct file *);
327         struct pde_opener *pdeo;
328
329         /*
330          * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
331          * sequence. ->release won't be called because ->proc_fops will be
332          * cleared. Depending on complexity of ->release, consequences vary.
333          *
334          * We can't wait for mercy when close will be done for real, it's
335          * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
336          * by hand in remove_proc_entry(). For this, save opener's credentials
337          * for later.
338          */
339         pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
340         if (!pdeo)
341                 return -ENOMEM;
342
343         spin_lock(&pde->pde_unload_lock);
344         if (!pde->proc_fops) {
345                 spin_unlock(&pde->pde_unload_lock);
346                 kfree(pdeo);
347                 return rv;
348         }
349         pde->pde_users++;
350         open = pde->proc_fops->open;
351         release = pde->proc_fops->release;
352         spin_unlock(&pde->pde_unload_lock);
353
354         if (open)
355                 rv = open(inode, file);
356
357         spin_lock(&pde->pde_unload_lock);
358         if (rv == 0 && release) {
359                 /* To know what to release. */
360                 pdeo->inode = inode;
361                 pdeo->file = file;
362                 /* Strictly for "too late" ->release in proc_reg_release(). */
363                 pdeo->release = release;
364                 list_add(&pdeo->lh, &pde->pde_openers);
365         } else
366                 kfree(pdeo);
367         __pde_users_dec(pde);
368         spin_unlock(&pde->pde_unload_lock);
369         return rv;
370 }
371
372 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
373                                         struct inode *inode, struct file *file)
374 {
375         struct pde_opener *pdeo;
376
377         list_for_each_entry(pdeo, &pde->pde_openers, lh) {
378                 if (pdeo->inode == inode && pdeo->file == file)
379                         return pdeo;
380         }
381         return NULL;
382 }
383
384 static int proc_reg_release(struct inode *inode, struct file *file)
385 {
386         struct proc_dir_entry *pde = PDE(inode);
387         int rv = 0;
388         int (*release)(struct inode *, struct file *);
389         struct pde_opener *pdeo;
390
391         spin_lock(&pde->pde_unload_lock);
392         pdeo = find_pde_opener(pde, inode, file);
393         if (!pde->proc_fops) {
394                 /*
395                  * Can't simply exit, __fput() will think that everything is OK,
396                  * and move on to freeing struct file. remove_proc_entry() will
397                  * find slacker in opener's list and will try to do non-trivial
398                  * things with struct file. Therefore, remove opener from list.
399                  *
400                  * But if opener is removed from list, who will ->release it?
401                  */
402                 if (pdeo) {
403                         list_del(&pdeo->lh);
404                         spin_unlock(&pde->pde_unload_lock);
405                         rv = pdeo->release(inode, file);
406                         kfree(pdeo);
407                 } else
408                         spin_unlock(&pde->pde_unload_lock);
409                 return rv;
410         }
411         pde->pde_users++;
412         release = pde->proc_fops->release;
413         if (pdeo) {
414                 list_del(&pdeo->lh);
415                 kfree(pdeo);
416         }
417         spin_unlock(&pde->pde_unload_lock);
418
419         if (release)
420                 rv = release(inode, file);
421
422         pde_users_dec(pde);
423         return rv;
424 }
425
426 static const struct file_operations proc_reg_file_ops = {
427         .llseek         = proc_reg_llseek,
428         .read           = proc_reg_read,
429         .write          = proc_reg_write,
430         .poll           = proc_reg_poll,
431         .unlocked_ioctl = proc_reg_unlocked_ioctl,
432 #ifdef CONFIG_COMPAT
433         .compat_ioctl   = proc_reg_compat_ioctl,
434 #endif
435         .mmap           = proc_reg_mmap,
436         .open           = proc_reg_open,
437         .release        = proc_reg_release,
438 };
439
440 #ifdef CONFIG_COMPAT
441 static const struct file_operations proc_reg_file_ops_no_compat = {
442         .llseek         = proc_reg_llseek,
443         .read           = proc_reg_read,
444         .write          = proc_reg_write,
445         .poll           = proc_reg_poll,
446         .unlocked_ioctl = proc_reg_unlocked_ioctl,
447         .mmap           = proc_reg_mmap,
448         .open           = proc_reg_open,
449         .release        = proc_reg_release,
450 };
451 #endif
452
453 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
454                                 struct proc_dir_entry *de)
455 {
456         struct inode * inode;
457
458         if (!try_module_get(de->owner))
459                 goto out_mod;
460
461         inode = iget_locked(sb, ino);
462         if (!inode)
463                 goto out_ino;
464         if (inode->i_state & I_NEW) {
465                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
466                 PROC_I(inode)->fd = 0;
467                 PROC_I(inode)->pde = de;
468
469                 if (de->mode) {
470                         inode->i_mode = de->mode;
471                         inode->i_uid = de->uid;
472                         inode->i_gid = de->gid;
473                 }
474                 if (de->size)
475                         inode->i_size = de->size;
476                 if (de->nlink)
477                         inode->i_nlink = de->nlink;
478                 if (de->proc_iops)
479                         inode->i_op = de->proc_iops;
480                 if (de->proc_fops) {
481                         if (S_ISREG(inode->i_mode)) {
482 #ifdef CONFIG_COMPAT
483                                 if (!de->proc_fops->compat_ioctl)
484                                         inode->i_fop =
485                                                 &proc_reg_file_ops_no_compat;
486                                 else
487 #endif
488                                         inode->i_fop = &proc_reg_file_ops;
489                         } else {
490                                 inode->i_fop = de->proc_fops;
491                         }
492                 }
493                 unlock_new_inode(inode);
494         } else
495                module_put(de->owner);
496         return inode;
497
498 out_ino:
499         module_put(de->owner);
500 out_mod:
501         return NULL;
502 }                       
503
504 int proc_fill_super(struct super_block *s)
505 {
506         struct inode * root_inode;
507
508         s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
509         s->s_blocksize = 1024;
510         s->s_blocksize_bits = 10;
511         s->s_magic = PROC_SUPER_MAGIC;
512         s->s_op = &proc_sops;
513         s->s_time_gran = 1;
514         
515         de_get(&proc_root);
516         root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
517         if (!root_inode)
518                 goto out_no_root;
519         root_inode->i_uid = 0;
520         root_inode->i_gid = 0;
521         s->s_root = d_alloc_root(root_inode);
522         if (!s->s_root)
523                 goto out_no_root;
524         return 0;
525
526 out_no_root:
527         printk("proc_read_super: get root inode failed\n");
528         iput(root_inode);
529         de_put(&proc_root);
530         return -ENOMEM;
531 }