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