blob: 354c0848582517baaf844e002e4a1c1e102f9a93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070013#include <linux/completion.h>
Alexey Dobriyandd23aae2007-09-11 15:23:55 -070014#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
Adrian Bunkfee781e2006-01-08 01:04:16 -080024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alexey Dobriyan76956502007-05-08 00:25:45 -070026struct proc_dir_entry *de_get(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -070028 atomic_inc(&de->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return de;
30}
31
32/*
33 * Decrements the use count and checks for deferred deletion.
34 */
Alexey Dobriyan76956502007-05-08 00:25:45 -070035void de_put(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -070037 lock_kernel();
38 if (!atomic_read(&de->count)) {
39 printk("de_put: entry %s already free!\n", de->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unlock_kernel();
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -070041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -070043
44 if (atomic_dec_and_test(&de->count))
45 free_proc_entry(de);
46 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49/*
50 * Decrement the use count of the proc_dir_entry.
51 */
52static void proc_delete_inode(struct inode *inode)
53{
54 struct proc_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Mark Fashehfef26652005-09-09 13:01:31 -070056 truncate_inode_pages(&inode->i_data, 0);
57
Eric W. Biederman99f89552006-06-26 00:25:55 -070058 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070059 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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
71struct vfsmount *proc_mnt;
72
Christoph Lametere18b8902006-12-06 20:33:20 -080073static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct inode *proc_alloc_inode(struct super_block *sb)
76{
77 struct proc_inode *ei;
78 struct inode *inode;
79
Christoph Lametere94b1762006-12-06 20:33:17 -080080 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!ei)
82 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070083 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070084 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 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
92static void proc_destroy_inode(struct inode *inode)
93{
94 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
95}
96
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -070097static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct proc_inode *ei = (struct proc_inode *) foo;
100
Christoph Lametera35afb82007-05-16 22:10:57 -0700101 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Paul Mundt20c2df82007-07-20 10:11:58 +0900103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104int __init proc_init_inodecache(void)
105{
106 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
107 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800108 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700109 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900110 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 0;
112}
113
114static int proc_remount(struct super_block *sb, int *flags, char *data)
115{
116 *flags |= MS_NODIRATIME;
117 return 0;
118}
119
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800120static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .alloc_inode = proc_alloc_inode,
122 .destroy_inode = proc_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .drop_inode = generic_delete_inode,
124 .delete_inode = proc_delete_inode,
125 .statfs = simple_statfs,
126 .remount_fs = proc_remount,
127};
128
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700129static void __pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700130{
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700131 pde->pde_users--;
132 if (pde->pde_unload_completion && pde->pde_users == 0)
133 complete(pde->pde_unload_completion);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700134}
135
136static void pde_users_dec(struct proc_dir_entry *pde)
137{
138 spin_lock(&pde->pde_unload_lock);
139 __pde_users_dec(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700140 spin_unlock(&pde->pde_unload_lock);
141}
142
143static 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
178static 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
200static 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
222static 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);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700225 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700226 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
244static 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
276static 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
299static 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
321static 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 *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700326 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;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700342
343 spin_lock(&pde->pde_unload_lock);
344 if (!pde->proc_fops) {
345 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700346 kfree(pdeo);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700347 return rv;
348 }
349 pde->pde_users++;
350 open = pde->proc_fops->open;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700351 release = pde->proc_fops->release;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700352 spin_unlock(&pde->pde_unload_lock);
353
354 if (open)
355 rv = open(inode, file);
356
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700357 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);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700369 return rv;
370}
371
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700372static 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
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700384static 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 *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700389 struct pde_opener *pdeo;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700390
391 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700392 pdeo = find_pde_opener(pde, inode, file);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700393 if (!pde->proc_fops) {
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700394 /*
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);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700409 return rv;
410 }
411 pde->pde_users++;
412 release = pde->proc_fops->release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700413 if (pdeo) {
414 list_del(&pdeo->lh);
415 kfree(pdeo);
416 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700417 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
426static 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
David Miller778f3dd2007-07-27 22:58:37 -0700440#ifdef CONFIG_COMPAT
441static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
454 struct proc_dir_entry *de)
455{
456 struct inode * inode;
457
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700458 if (!try_module_get(de->owner))
Kirill Korotaeve9543652005-10-30 15:02:26 -0800459 goto out_mod;
460
David Howellsa1d4aeb2008-02-07 00:15:45 -0800461 inode = iget_locked(sb, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!inode)
Kirill Korotaeve9543652005-10-30 15:02:26 -0800463 goto out_ino;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800464 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;
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700468
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)) {
David Howellsa1d4aeb2008-02-07 00:15:45 -0800482#ifdef CONFIG_COMPAT
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700483 if (!de->proc_fops->compat_ioctl)
484 inode->i_fop =
485 &proc_reg_file_ops_no_compat;
486 else
David Howellsa1d4aeb2008-02-07 00:15:45 -0800487#endif
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700488 inode->i_fop = &proc_reg_file_ops;
489 } else {
490 inode->i_fop = de->proc_fops;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800491 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700492 }
David Howellsa1d4aeb2008-02-07 00:15:45 -0800493 unlock_new_inode(inode);
Denis V. Lunevc4185a02008-05-23 13:04:47 -0700494 } else
495 module_put(de->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return inode;
497
Kirill Korotaeve9543652005-10-30 15:02:26 -0800498out_ino:
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700499 module_put(de->owner);
Kirill Korotaeve9543652005-10-30 15:02:26 -0800500out_mod:
Kirill Korotaeve9543652005-10-30 15:02:26 -0800501 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Pavel Emelyanov07543f52007-10-18 23:40:08 -0700504int proc_fill_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct inode * root_inode;
507
Linus Torvalds92d03282006-07-15 12:20:05 -0700508 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 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
Alexey Dobriyan76956502007-05-08 00:25:45 -0700515 de_get(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
517 if (!root_inode)
518 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 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
526out_no_root:
527 printk("proc_read_super: get root inode failed\n");
528 iput(root_inode);
Alexey Dobriyan76956502007-05-08 00:25:45 -0700529 de_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -ENOMEM;
531}