blob: aea8502e58a3a247aae6c4ea5a85e99c7da36b03 [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>
Al Viro9043476f2008-07-15 08:54:06 -040020#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/system.h>
24#include <asm/uaccess.h>
25
Adrian Bunkfee781e2006-01-08 01:04:16 -080026#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static void proc_delete_inode(struct inode *inode)
29{
30 struct proc_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Mark Fashehfef26652005-09-09 13:01:31 -070032 truncate_inode_pages(&inode->i_data, 0);
33
Eric W. Biederman99f89552006-06-26 00:25:55 -070034 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070035 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 /* Let go of any associated proc directory entry */
38 de = PROC_I(inode)->pde;
Alexey Dobriyan99b76232009-03-25 22:48:06 +030039 if (de)
Alexey Dobriyan135d5652009-12-15 16:45:39 -080040 pde_put(de);
Al Viro9043476f2008-07-15 08:54:06 -040041 if (PROC_I(inode)->sysctl)
42 sysctl_head_put(PROC_I(inode)->sysctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 clear_inode(inode);
44}
45
46struct vfsmount *proc_mnt;
47
Christoph Lametere18b8902006-12-06 20:33:20 -080048static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static struct inode *proc_alloc_inode(struct super_block *sb)
51{
52 struct proc_inode *ei;
53 struct inode *inode;
54
Christoph Lametere94b1762006-12-06 20:33:17 -080055 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!ei)
57 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070058 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070059 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 ei->op.proc_get_link = NULL;
61 ei->pde = NULL;
Al Viro9043476f2008-07-15 08:54:06 -040062 ei->sysctl = NULL;
63 ei->sysctl_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 inode = &ei->vfs_inode;
65 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
66 return inode;
67}
68
69static void proc_destroy_inode(struct inode *inode)
70{
71 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
72}
73
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070074static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct proc_inode *ei = (struct proc_inode *) foo;
77
Christoph Lametera35afb82007-05-16 22:10:57 -070078 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Paul Mundt20c2df82007-07-20 10:11:58 +090080
Alexey Dobriyan5bcd7ff2008-10-17 03:43:55 +040081void __init proc_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
84 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -080085 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -070086 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +090087 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080090static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .alloc_inode = proc_alloc_inode,
92 .destroy_inode = proc_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .drop_inode = generic_delete_inode,
94 .delete_inode = proc_delete_inode,
95 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Alexey Dobriyan881adb82008-07-25 01:48:29 -070098static void __pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070099{
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700100 pde->pde_users--;
101 if (pde->pde_unload_completion && pde->pde_users == 0)
102 complete(pde->pde_unload_completion);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700103}
104
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300105void pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700106{
107 spin_lock(&pde->pde_unload_lock);
108 __pde_users_dec(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700109 spin_unlock(&pde->pde_unload_lock);
110}
111
112static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
113{
114 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
115 loff_t rv = -EINVAL;
116 loff_t (*llseek)(struct file *, loff_t, int);
117
118 spin_lock(&pde->pde_unload_lock);
119 /*
120 * remove_proc_entry() is going to delete PDE (as part of module
121 * cleanup sequence). No new callers into module allowed.
122 */
123 if (!pde->proc_fops) {
124 spin_unlock(&pde->pde_unload_lock);
125 return rv;
126 }
127 /*
128 * Bump refcount so that remove_proc_entry will wail for ->llseek to
129 * complete.
130 */
131 pde->pde_users++;
132 /*
133 * Save function pointer under lock, to protect against ->proc_fops
134 * NULL'ifying right after ->pde_unload_lock is dropped.
135 */
136 llseek = pde->proc_fops->llseek;
137 spin_unlock(&pde->pde_unload_lock);
138
139 if (!llseek)
140 llseek = default_llseek;
141 rv = llseek(file, offset, whence);
142
143 pde_users_dec(pde);
144 return rv;
145}
146
147static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
148{
149 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
150 ssize_t rv = -EIO;
151 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
152
153 spin_lock(&pde->pde_unload_lock);
154 if (!pde->proc_fops) {
155 spin_unlock(&pde->pde_unload_lock);
156 return rv;
157 }
158 pde->pde_users++;
159 read = pde->proc_fops->read;
160 spin_unlock(&pde->pde_unload_lock);
161
162 if (read)
163 rv = read(file, buf, count, ppos);
164
165 pde_users_dec(pde);
166 return rv;
167}
168
169static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
170{
171 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
172 ssize_t rv = -EIO;
173 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
174
175 spin_lock(&pde->pde_unload_lock);
176 if (!pde->proc_fops) {
177 spin_unlock(&pde->pde_unload_lock);
178 return rv;
179 }
180 pde->pde_users++;
181 write = pde->proc_fops->write;
182 spin_unlock(&pde->pde_unload_lock);
183
184 if (write)
185 rv = write(file, buf, count, ppos);
186
187 pde_users_dec(pde);
188 return rv;
189}
190
191static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
192{
193 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700194 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700195 unsigned int (*poll)(struct file *, struct poll_table_struct *);
196
197 spin_lock(&pde->pde_unload_lock);
198 if (!pde->proc_fops) {
199 spin_unlock(&pde->pde_unload_lock);
200 return rv;
201 }
202 pde->pde_users++;
203 poll = pde->proc_fops->poll;
204 spin_unlock(&pde->pde_unload_lock);
205
206 if (poll)
207 rv = poll(file, pts);
208
209 pde_users_dec(pde);
210 return rv;
211}
212
213static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
214{
215 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
216 long rv = -ENOTTY;
217 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
218 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
219
220 spin_lock(&pde->pde_unload_lock);
221 if (!pde->proc_fops) {
222 spin_unlock(&pde->pde_unload_lock);
223 return rv;
224 }
225 pde->pde_users++;
226 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
227 ioctl = pde->proc_fops->ioctl;
228 spin_unlock(&pde->pde_unload_lock);
229
230 if (unlocked_ioctl) {
231 rv = unlocked_ioctl(file, cmd, arg);
232 if (rv == -ENOIOCTLCMD)
233 rv = -EINVAL;
234 } else if (ioctl) {
Frederic Weisbeckerc2f98052010-03-30 07:32:50 +0200235 WARN_ONCE(1, "Procfs ioctl handlers must use unlocked_ioctl, "
236 "%pf will be called without the Bkl held\n", ioctl);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700237 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700238 }
239
240 pde_users_dec(pde);
241 return rv;
242}
243
244#ifdef CONFIG_COMPAT
245static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
246{
247 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
248 long rv = -ENOTTY;
249 long (*compat_ioctl)(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 compat_ioctl = pde->proc_fops->compat_ioctl;
258 spin_unlock(&pde->pde_unload_lock);
259
260 if (compat_ioctl)
261 rv = compat_ioctl(file, cmd, arg);
262
263 pde_users_dec(pde);
264 return rv;
265}
266#endif
267
268static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
269{
270 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
271 int rv = -EIO;
272 int (*mmap)(struct file *, struct vm_area_struct *);
273
274 spin_lock(&pde->pde_unload_lock);
275 if (!pde->proc_fops) {
276 spin_unlock(&pde->pde_unload_lock);
277 return rv;
278 }
279 pde->pde_users++;
280 mmap = pde->proc_fops->mmap;
281 spin_unlock(&pde->pde_unload_lock);
282
283 if (mmap)
284 rv = mmap(file, vma);
285
286 pde_users_dec(pde);
287 return rv;
288}
289
290static int proc_reg_open(struct inode *inode, struct file *file)
291{
292 struct proc_dir_entry *pde = PDE(inode);
293 int rv = 0;
294 int (*open)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700295 int (*release)(struct inode *, struct file *);
296 struct pde_opener *pdeo;
297
298 /*
299 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
300 * sequence. ->release won't be called because ->proc_fops will be
301 * cleared. Depending on complexity of ->release, consequences vary.
302 *
303 * We can't wait for mercy when close will be done for real, it's
304 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
305 * by hand in remove_proc_entry(). For this, save opener's credentials
306 * for later.
307 */
308 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
309 if (!pdeo)
310 return -ENOMEM;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700311
312 spin_lock(&pde->pde_unload_lock);
313 if (!pde->proc_fops) {
314 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700315 kfree(pdeo);
Alexey Dobriyan300b9942008-10-03 00:18:52 +0400316 return -EINVAL;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700317 }
318 pde->pde_users++;
319 open = pde->proc_fops->open;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700320 release = pde->proc_fops->release;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700321 spin_unlock(&pde->pde_unload_lock);
322
323 if (open)
324 rv = open(inode, file);
325
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700326 spin_lock(&pde->pde_unload_lock);
327 if (rv == 0 && release) {
328 /* To know what to release. */
329 pdeo->inode = inode;
330 pdeo->file = file;
331 /* Strictly for "too late" ->release in proc_reg_release(). */
332 pdeo->release = release;
333 list_add(&pdeo->lh, &pde->pde_openers);
334 } else
335 kfree(pdeo);
336 __pde_users_dec(pde);
337 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700338 return rv;
339}
340
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700341static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
342 struct inode *inode, struct file *file)
343{
344 struct pde_opener *pdeo;
345
346 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
347 if (pdeo->inode == inode && pdeo->file == file)
348 return pdeo;
349 }
350 return NULL;
351}
352
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700353static int proc_reg_release(struct inode *inode, struct file *file)
354{
355 struct proc_dir_entry *pde = PDE(inode);
356 int rv = 0;
357 int (*release)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700358 struct pde_opener *pdeo;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700359
360 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700361 pdeo = find_pde_opener(pde, inode, file);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700362 if (!pde->proc_fops) {
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700363 /*
364 * Can't simply exit, __fput() will think that everything is OK,
365 * and move on to freeing struct file. remove_proc_entry() will
366 * find slacker in opener's list and will try to do non-trivial
367 * things with struct file. Therefore, remove opener from list.
368 *
369 * But if opener is removed from list, who will ->release it?
370 */
371 if (pdeo) {
372 list_del(&pdeo->lh);
373 spin_unlock(&pde->pde_unload_lock);
374 rv = pdeo->release(inode, file);
375 kfree(pdeo);
376 } else
377 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700378 return rv;
379 }
380 pde->pde_users++;
381 release = pde->proc_fops->release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700382 if (pdeo) {
383 list_del(&pdeo->lh);
384 kfree(pdeo);
385 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700386 spin_unlock(&pde->pde_unload_lock);
387
388 if (release)
389 rv = release(inode, file);
390
391 pde_users_dec(pde);
392 return rv;
393}
394
395static const struct file_operations proc_reg_file_ops = {
396 .llseek = proc_reg_llseek,
397 .read = proc_reg_read,
398 .write = proc_reg_write,
399 .poll = proc_reg_poll,
400 .unlocked_ioctl = proc_reg_unlocked_ioctl,
401#ifdef CONFIG_COMPAT
402 .compat_ioctl = proc_reg_compat_ioctl,
403#endif
404 .mmap = proc_reg_mmap,
405 .open = proc_reg_open,
406 .release = proc_reg_release,
407};
408
David Miller778f3dd2007-07-27 22:58:37 -0700409#ifdef CONFIG_COMPAT
410static const struct file_operations proc_reg_file_ops_no_compat = {
411 .llseek = proc_reg_llseek,
412 .read = proc_reg_read,
413 .write = proc_reg_write,
414 .poll = proc_reg_poll,
415 .unlocked_ioctl = proc_reg_unlocked_ioctl,
416 .mmap = proc_reg_mmap,
417 .open = proc_reg_open,
418 .release = proc_reg_release,
419};
420#endif
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
423 struct proc_dir_entry *de)
424{
425 struct inode * inode;
426
David Howellsa1d4aeb2008-02-07 00:15:45 -0800427 inode = iget_locked(sb, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!inode)
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300429 return NULL;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800430 if (inode->i_state & I_NEW) {
431 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
432 PROC_I(inode)->fd = 0;
433 PROC_I(inode)->pde = de;
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700434
435 if (de->mode) {
436 inode->i_mode = de->mode;
437 inode->i_uid = de->uid;
438 inode->i_gid = de->gid;
439 }
440 if (de->size)
441 inode->i_size = de->size;
442 if (de->nlink)
443 inode->i_nlink = de->nlink;
444 if (de->proc_iops)
445 inode->i_op = de->proc_iops;
446 if (de->proc_fops) {
447 if (S_ISREG(inode->i_mode)) {
David Howellsa1d4aeb2008-02-07 00:15:45 -0800448#ifdef CONFIG_COMPAT
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700449 if (!de->proc_fops->compat_ioctl)
450 inode->i_fop =
451 &proc_reg_file_ops_no_compat;
452 else
David Howellsa1d4aeb2008-02-07 00:15:45 -0800453#endif
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700454 inode->i_fop = &proc_reg_file_ops;
455 } else {
456 inode->i_fop = de->proc_fops;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800457 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700458 }
David Howellsa1d4aeb2008-02-07 00:15:45 -0800459 unlock_new_inode(inode);
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300460 } else
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800461 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Pavel Emelyanov07543f52007-10-18 23:40:08 -0700465int proc_fill_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct inode * root_inode;
468
Linus Torvalds92d03282006-07-15 12:20:05 -0700469 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 s->s_blocksize = 1024;
471 s->s_blocksize_bits = 10;
472 s->s_magic = PROC_SUPER_MAGIC;
473 s->s_op = &proc_sops;
474 s->s_time_gran = 1;
475
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800476 pde_get(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
478 if (!root_inode)
479 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 root_inode->i_uid = 0;
481 root_inode->i_gid = 0;
482 s->s_root = d_alloc_root(root_inode);
483 if (!s->s_root)
484 goto out_no_root;
485 return 0;
486
487out_no_root:
488 printk("proc_read_super: get root inode failed\n");
489 iput(root_inode);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800490 pde_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -ENOMEM;
492}