blob: 99ca00485fc307ff52014dc303b60fd918f87de4 [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{
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 */
Alexey Dobriyan76956502007-05-08 00:25:45 -070036void de_put(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
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 */
60static void proc_delete_inode(struct inode *inode)
61{
62 struct proc_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Mark Fashehfef26652005-09-09 13:01:31 -070064 truncate_inode_pages(&inode->i_data, 0);
65
Eric W. Biederman99f89552006-06-26 00:25:55 -070066 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070067 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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
79struct vfsmount *proc_mnt;
80
81static void proc_read_inode(struct inode * inode)
82{
83 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
84}
85
Christoph Lametere18b8902006-12-06 20:33:20 -080086static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct inode *proc_alloc_inode(struct super_block *sb)
89{
90 struct proc_inode *ei;
91 struct inode *inode;
92
Christoph Lametere94b1762006-12-06 20:33:17 -080093 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!ei)
95 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070096 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070097 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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
105static void proc_destroy_inode(struct inode *inode)
106{
107 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
108}
109
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700110static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct proc_inode *ei = (struct proc_inode *) foo;
113
Christoph Lametera35afb82007-05-16 22:10:57 -0700114 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Paul Mundt20c2df82007-07-20 10:11:58 +0900116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117int __init proc_init_inodecache(void)
118{
119 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
120 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800121 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700122 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900123 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125}
126
127static int proc_remount(struct super_block *sb, int *flags, char *data)
128{
129 *flags |= MS_NODIRATIME;
130 return 0;
131}
132
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800133static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .alloc_inode = proc_alloc_inode,
135 .destroy_inode = proc_destroy_inode,
136 .read_inode = proc_read_inode,
137 .drop_inode = generic_delete_inode,
138 .delete_inode = proc_delete_inode,
139 .statfs = simple_statfs,
140 .remount_fs = proc_remount,
141};
142
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700143static void pde_users_dec(struct proc_dir_entry *pde)
144{
145 spin_lock(&pde->pde_unload_lock);
146 pde->pde_users--;
147 if (pde->pde_unload_completion && pde->pde_users == 0)
148 complete(pde->pde_unload_completion);
149 spin_unlock(&pde->pde_unload_lock);
150}
151
152static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
153{
154 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
155 loff_t rv = -EINVAL;
156 loff_t (*llseek)(struct file *, loff_t, int);
157
158 spin_lock(&pde->pde_unload_lock);
159 /*
160 * remove_proc_entry() is going to delete PDE (as part of module
161 * cleanup sequence). No new callers into module allowed.
162 */
163 if (!pde->proc_fops) {
164 spin_unlock(&pde->pde_unload_lock);
165 return rv;
166 }
167 /*
168 * Bump refcount so that remove_proc_entry will wail for ->llseek to
169 * complete.
170 */
171 pde->pde_users++;
172 /*
173 * Save function pointer under lock, to protect against ->proc_fops
174 * NULL'ifying right after ->pde_unload_lock is dropped.
175 */
176 llseek = pde->proc_fops->llseek;
177 spin_unlock(&pde->pde_unload_lock);
178
179 if (!llseek)
180 llseek = default_llseek;
181 rv = llseek(file, offset, whence);
182
183 pde_users_dec(pde);
184 return rv;
185}
186
187static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
188{
189 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
190 ssize_t rv = -EIO;
191 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
192
193 spin_lock(&pde->pde_unload_lock);
194 if (!pde->proc_fops) {
195 spin_unlock(&pde->pde_unload_lock);
196 return rv;
197 }
198 pde->pde_users++;
199 read = pde->proc_fops->read;
200 spin_unlock(&pde->pde_unload_lock);
201
202 if (read)
203 rv = read(file, buf, count, ppos);
204
205 pde_users_dec(pde);
206 return rv;
207}
208
209static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
210{
211 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
212 ssize_t rv = -EIO;
213 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
214
215 spin_lock(&pde->pde_unload_lock);
216 if (!pde->proc_fops) {
217 spin_unlock(&pde->pde_unload_lock);
218 return rv;
219 }
220 pde->pde_users++;
221 write = pde->proc_fops->write;
222 spin_unlock(&pde->pde_unload_lock);
223
224 if (write)
225 rv = write(file, buf, count, ppos);
226
227 pde_users_dec(pde);
228 return rv;
229}
230
231static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
232{
233 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700234 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700235 unsigned int (*poll)(struct file *, struct poll_table_struct *);
236
237 spin_lock(&pde->pde_unload_lock);
238 if (!pde->proc_fops) {
239 spin_unlock(&pde->pde_unload_lock);
240 return rv;
241 }
242 pde->pde_users++;
243 poll = pde->proc_fops->poll;
244 spin_unlock(&pde->pde_unload_lock);
245
246 if (poll)
247 rv = poll(file, pts);
248
249 pde_users_dec(pde);
250 return rv;
251}
252
253static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
254{
255 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
256 long rv = -ENOTTY;
257 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
258 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
259
260 spin_lock(&pde->pde_unload_lock);
261 if (!pde->proc_fops) {
262 spin_unlock(&pde->pde_unload_lock);
263 return rv;
264 }
265 pde->pde_users++;
266 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
267 ioctl = pde->proc_fops->ioctl;
268 spin_unlock(&pde->pde_unload_lock);
269
270 if (unlocked_ioctl) {
271 rv = unlocked_ioctl(file, cmd, arg);
272 if (rv == -ENOIOCTLCMD)
273 rv = -EINVAL;
274 } else if (ioctl) {
275 lock_kernel();
276 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
277 unlock_kernel();
278 }
279
280 pde_users_dec(pde);
281 return rv;
282}
283
284#ifdef CONFIG_COMPAT
285static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
286{
287 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
288 long rv = -ENOTTY;
289 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
290
291 spin_lock(&pde->pde_unload_lock);
292 if (!pde->proc_fops) {
293 spin_unlock(&pde->pde_unload_lock);
294 return rv;
295 }
296 pde->pde_users++;
297 compat_ioctl = pde->proc_fops->compat_ioctl;
298 spin_unlock(&pde->pde_unload_lock);
299
300 if (compat_ioctl)
301 rv = compat_ioctl(file, cmd, arg);
302
303 pde_users_dec(pde);
304 return rv;
305}
306#endif
307
308static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
309{
310 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
311 int rv = -EIO;
312 int (*mmap)(struct file *, struct vm_area_struct *);
313
314 spin_lock(&pde->pde_unload_lock);
315 if (!pde->proc_fops) {
316 spin_unlock(&pde->pde_unload_lock);
317 return rv;
318 }
319 pde->pde_users++;
320 mmap = pde->proc_fops->mmap;
321 spin_unlock(&pde->pde_unload_lock);
322
323 if (mmap)
324 rv = mmap(file, vma);
325
326 pde_users_dec(pde);
327 return rv;
328}
329
330static int proc_reg_open(struct inode *inode, struct file *file)
331{
332 struct proc_dir_entry *pde = PDE(inode);
333 int rv = 0;
334 int (*open)(struct inode *, struct file *);
335
336 spin_lock(&pde->pde_unload_lock);
337 if (!pde->proc_fops) {
338 spin_unlock(&pde->pde_unload_lock);
339 return rv;
340 }
341 pde->pde_users++;
342 open = pde->proc_fops->open;
343 spin_unlock(&pde->pde_unload_lock);
344
345 if (open)
346 rv = open(inode, file);
347
348 pde_users_dec(pde);
349 return rv;
350}
351
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 *);
357
358 spin_lock(&pde->pde_unload_lock);
359 if (!pde->proc_fops) {
360 spin_unlock(&pde->pde_unload_lock);
361 return rv;
362 }
363 pde->pde_users++;
364 release = pde->proc_fops->release;
365 spin_unlock(&pde->pde_unload_lock);
366
367 if (release)
368 rv = release(inode, file);
369
370 pde_users_dec(pde);
371 return rv;
372}
373
374static const struct file_operations proc_reg_file_ops = {
375 .llseek = proc_reg_llseek,
376 .read = proc_reg_read,
377 .write = proc_reg_write,
378 .poll = proc_reg_poll,
379 .unlocked_ioctl = proc_reg_unlocked_ioctl,
380#ifdef CONFIG_COMPAT
381 .compat_ioctl = proc_reg_compat_ioctl,
382#endif
383 .mmap = proc_reg_mmap,
384 .open = proc_reg_open,
385 .release = proc_reg_release,
386};
387
David Miller778f3dd2007-07-27 22:58:37 -0700388#ifdef CONFIG_COMPAT
389static const struct file_operations proc_reg_file_ops_no_compat = {
390 .llseek = proc_reg_llseek,
391 .read = proc_reg_read,
392 .write = proc_reg_write,
393 .poll = proc_reg_poll,
394 .unlocked_ioctl = proc_reg_unlocked_ioctl,
395 .mmap = proc_reg_mmap,
396 .open = proc_reg_open,
397 .release = proc_reg_release,
398};
399#endif
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
402 struct proc_dir_entry *de)
403{
404 struct inode * inode;
405
Kirill Korotaeve9543652005-10-30 15:02:26 -0800406 if (de != NULL && !try_module_get(de->owner))
407 goto out_mod;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 inode = iget(sb, ino);
410 if (!inode)
Kirill Korotaeve9543652005-10-30 15:02:26 -0800411 goto out_ino;
412
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800413 PROC_I(inode)->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 PROC_I(inode)->pde = de;
415 if (de) {
416 if (de->mode) {
417 inode->i_mode = de->mode;
418 inode->i_uid = de->uid;
419 inode->i_gid = de->gid;
420 }
421 if (de->size)
422 inode->i_size = de->size;
423 if (de->nlink)
424 inode->i_nlink = de->nlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (de->proc_iops)
426 inode->i_op = de->proc_iops;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700427 if (de->proc_fops) {
David Miller778f3dd2007-07-27 22:58:37 -0700428 if (S_ISREG(inode->i_mode)) {
429#ifdef CONFIG_COMPAT
430 if (!de->proc_fops->compat_ioctl)
431 inode->i_fop =
432 &proc_reg_file_ops_no_compat;
433 else
434#endif
435 inode->i_fop = &proc_reg_file_ops;
436 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700437 else
438 inode->i_fop = de->proc_fops;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return inode;
443
Kirill Korotaeve9543652005-10-30 15:02:26 -0800444out_ino:
445 if (de != NULL)
446 module_put(de->owner);
447out_mod:
Kirill Korotaeve9543652005-10-30 15:02:26 -0800448 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451int proc_fill_super(struct super_block *s, void *data, int silent)
452{
453 struct inode * root_inode;
454
Linus Torvalds92d03282006-07-15 12:20:05 -0700455 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 s->s_blocksize = 1024;
457 s->s_blocksize_bits = 10;
458 s->s_magic = PROC_SUPER_MAGIC;
459 s->s_op = &proc_sops;
460 s->s_time_gran = 1;
461
Alexey Dobriyan76956502007-05-08 00:25:45 -0700462 de_get(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
464 if (!root_inode)
465 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 root_inode->i_uid = 0;
467 root_inode->i_gid = 0;
468 s->s_root = d_alloc_root(root_inode);
469 if (!s->s_root)
470 goto out_no_root;
471 return 0;
472
473out_no_root:
474 printk("proc_read_super: get root inode failed\n");
475 iput(root_inode);
Alexey Dobriyan76956502007-05-08 00:25:45 -0700476 de_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -ENOMEM;
478}
479MODULE_LICENSE("GPL");