Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / fs / proc / inode.c
blob9c70229b6c35d24eba67b5fc48c64987b27910a3
1 /*
2 * linux/fs/proc/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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/sysctl.h>
20 #include <linux/slab.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
25 #include "internal.h"
27 static void proc_evict_inode(struct inode *inode)
29 struct proc_dir_entry *de;
30 struct ctl_table_header *head;
31 const struct proc_ns_operations *ns_ops;
32 void *ns;
34 truncate_inode_pages(&inode->i_data, 0);
35 end_writeback(inode);
37 /* Stop tracking associated processes */
38 put_pid(PROC_I(inode)->pid);
40 /* Let go of any associated proc directory entry */
41 de = PROC_I(inode)->pde;
42 if (de)
43 pde_put(de);
44 head = PROC_I(inode)->sysctl;
45 if (head) {
46 rcu_assign_pointer(PROC_I(inode)->sysctl, NULL);
47 sysctl_head_put(head);
49 /* Release any associated namespace */
50 ns_ops = PROC_I(inode)->ns_ops;
51 ns = PROC_I(inode)->ns;
52 if (ns_ops && ns)
53 ns_ops->put(ns);
56 static struct kmem_cache * proc_inode_cachep;
58 static struct inode *proc_alloc_inode(struct super_block *sb)
60 struct proc_inode *ei;
61 struct inode *inode;
63 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
64 if (!ei)
65 return NULL;
66 ei->pid = NULL;
67 ei->fd = 0;
68 ei->op.proc_get_link = NULL;
69 ei->pde = NULL;
70 ei->sysctl = NULL;
71 ei->sysctl_entry = NULL;
72 ei->ns = NULL;
73 ei->ns_ops = NULL;
74 inode = &ei->vfs_inode;
75 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
76 return inode;
79 static void proc_i_callback(struct rcu_head *head)
81 struct inode *inode = container_of(head, struct inode, i_rcu);
82 INIT_LIST_HEAD(&inode->i_dentry);
83 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
86 static void proc_destroy_inode(struct inode *inode)
88 call_rcu(&inode->i_rcu, proc_i_callback);
91 static void init_once(void *foo)
93 struct proc_inode *ei = (struct proc_inode *) foo;
95 inode_init_once(&ei->vfs_inode);
98 void __init proc_init_inodecache(void)
100 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
101 sizeof(struct proc_inode),
102 0, (SLAB_RECLAIM_ACCOUNT|
103 SLAB_MEM_SPREAD|SLAB_PANIC),
104 init_once);
107 static const struct super_operations proc_sops = {
108 .alloc_inode = proc_alloc_inode,
109 .destroy_inode = proc_destroy_inode,
110 .drop_inode = generic_delete_inode,
111 .evict_inode = proc_evict_inode,
112 .statfs = simple_statfs,
115 static void __pde_users_dec(struct proc_dir_entry *pde)
117 pde->pde_users--;
118 if (pde->pde_unload_completion && pde->pde_users == 0)
119 complete(pde->pde_unload_completion);
122 void pde_users_dec(struct proc_dir_entry *pde)
124 spin_lock(&pde->pde_unload_lock);
125 __pde_users_dec(pde);
126 spin_unlock(&pde->pde_unload_lock);
129 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
131 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
132 loff_t rv = -EINVAL;
133 loff_t (*llseek)(struct file *, loff_t, int);
135 spin_lock(&pde->pde_unload_lock);
137 * remove_proc_entry() is going to delete PDE (as part of module
138 * cleanup sequence). No new callers into module allowed.
140 if (!pde->proc_fops) {
141 spin_unlock(&pde->pde_unload_lock);
142 return rv;
145 * Bump refcount so that remove_proc_entry will wail for ->llseek to
146 * complete.
148 pde->pde_users++;
150 * Save function pointer under lock, to protect against ->proc_fops
151 * NULL'ifying right after ->pde_unload_lock is dropped.
153 llseek = pde->proc_fops->llseek;
154 spin_unlock(&pde->pde_unload_lock);
156 if (!llseek)
157 llseek = default_llseek;
158 rv = llseek(file, offset, whence);
160 pde_users_dec(pde);
161 return rv;
164 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
166 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
167 ssize_t rv = -EIO;
168 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
170 spin_lock(&pde->pde_unload_lock);
171 if (!pde->proc_fops) {
172 spin_unlock(&pde->pde_unload_lock);
173 return rv;
175 pde->pde_users++;
176 read = pde->proc_fops->read;
177 spin_unlock(&pde->pde_unload_lock);
179 if (read)
180 rv = read(file, buf, count, ppos);
182 pde_users_dec(pde);
183 return rv;
186 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
188 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
189 ssize_t rv = -EIO;
190 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
192 spin_lock(&pde->pde_unload_lock);
193 if (!pde->proc_fops) {
194 spin_unlock(&pde->pde_unload_lock);
195 return rv;
197 pde->pde_users++;
198 write = pde->proc_fops->write;
199 spin_unlock(&pde->pde_unload_lock);
201 if (write)
202 rv = write(file, buf, count, ppos);
204 pde_users_dec(pde);
205 return rv;
208 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
210 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
211 unsigned int rv = DEFAULT_POLLMASK;
212 unsigned int (*poll)(struct file *, struct poll_table_struct *);
214 spin_lock(&pde->pde_unload_lock);
215 if (!pde->proc_fops) {
216 spin_unlock(&pde->pde_unload_lock);
217 return rv;
219 pde->pde_users++;
220 poll = pde->proc_fops->poll;
221 spin_unlock(&pde->pde_unload_lock);
223 if (poll)
224 rv = poll(file, pts);
226 pde_users_dec(pde);
227 return rv;
230 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
232 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
233 long rv = -ENOTTY;
234 long (*ioctl)(struct file *, unsigned int, unsigned long);
236 spin_lock(&pde->pde_unload_lock);
237 if (!pde->proc_fops) {
238 spin_unlock(&pde->pde_unload_lock);
239 return rv;
241 pde->pde_users++;
242 ioctl = pde->proc_fops->unlocked_ioctl;
243 spin_unlock(&pde->pde_unload_lock);
245 if (ioctl)
246 rv = ioctl(file, cmd, arg);
248 pde_users_dec(pde);
249 return rv;
252 #ifdef CONFIG_COMPAT
253 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
255 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
256 long rv = -ENOTTY;
257 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
259 spin_lock(&pde->pde_unload_lock);
260 if (!pde->proc_fops) {
261 spin_unlock(&pde->pde_unload_lock);
262 return rv;
264 pde->pde_users++;
265 compat_ioctl = pde->proc_fops->compat_ioctl;
266 spin_unlock(&pde->pde_unload_lock);
268 if (compat_ioctl)
269 rv = compat_ioctl(file, cmd, arg);
271 pde_users_dec(pde);
272 return rv;
274 #endif
276 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
278 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
279 int rv = -EIO;
280 int (*mmap)(struct file *, struct vm_area_struct *);
282 spin_lock(&pde->pde_unload_lock);
283 if (!pde->proc_fops) {
284 spin_unlock(&pde->pde_unload_lock);
285 return rv;
287 pde->pde_users++;
288 mmap = pde->proc_fops->mmap;
289 spin_unlock(&pde->pde_unload_lock);
291 if (mmap)
292 rv = mmap(file, vma);
294 pde_users_dec(pde);
295 return rv;
298 static int proc_reg_open(struct inode *inode, struct file *file)
300 struct proc_dir_entry *pde = PDE(inode);
301 int rv = 0;
302 int (*open)(struct inode *, struct file *);
303 int (*release)(struct inode *, struct file *);
304 struct pde_opener *pdeo;
307 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
308 * sequence. ->release won't be called because ->proc_fops will be
309 * cleared. Depending on complexity of ->release, consequences vary.
311 * We can't wait for mercy when close will be done for real, it's
312 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
313 * by hand in remove_proc_entry(). For this, save opener's credentials
314 * for later.
316 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
317 if (!pdeo)
318 return -ENOMEM;
320 spin_lock(&pde->pde_unload_lock);
321 if (!pde->proc_fops) {
322 spin_unlock(&pde->pde_unload_lock);
323 kfree(pdeo);
324 return -ENOENT;
326 pde->pde_users++;
327 open = pde->proc_fops->open;
328 release = pde->proc_fops->release;
329 spin_unlock(&pde->pde_unload_lock);
331 if (open)
332 rv = open(inode, file);
334 spin_lock(&pde->pde_unload_lock);
335 if (rv == 0 && release) {
336 /* To know what to release. */
337 pdeo->inode = inode;
338 pdeo->file = file;
339 /* Strictly for "too late" ->release in proc_reg_release(). */
340 pdeo->release = release;
341 list_add(&pdeo->lh, &pde->pde_openers);
342 } else
343 kfree(pdeo);
344 __pde_users_dec(pde);
345 spin_unlock(&pde->pde_unload_lock);
346 return rv;
349 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
350 struct inode *inode, struct file *file)
352 struct pde_opener *pdeo;
354 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
355 if (pdeo->inode == inode && pdeo->file == file)
356 return pdeo;
358 return NULL;
361 static int proc_reg_release(struct inode *inode, struct file *file)
363 struct proc_dir_entry *pde = PDE(inode);
364 int rv = 0;
365 int (*release)(struct inode *, struct file *);
366 struct pde_opener *pdeo;
368 spin_lock(&pde->pde_unload_lock);
369 pdeo = find_pde_opener(pde, inode, file);
370 if (!pde->proc_fops) {
372 * Can't simply exit, __fput() will think that everything is OK,
373 * and move on to freeing struct file. remove_proc_entry() will
374 * find slacker in opener's list and will try to do non-trivial
375 * things with struct file. Therefore, remove opener from list.
377 * But if opener is removed from list, who will ->release it?
379 if (pdeo) {
380 list_del(&pdeo->lh);
381 spin_unlock(&pde->pde_unload_lock);
382 rv = pdeo->release(inode, file);
383 kfree(pdeo);
384 } else
385 spin_unlock(&pde->pde_unload_lock);
386 return rv;
388 pde->pde_users++;
389 release = pde->proc_fops->release;
390 if (pdeo) {
391 list_del(&pdeo->lh);
392 kfree(pdeo);
394 spin_unlock(&pde->pde_unload_lock);
396 if (release)
397 rv = release(inode, file);
399 pde_users_dec(pde);
400 return rv;
403 static const struct file_operations proc_reg_file_ops = {
404 .llseek = proc_reg_llseek,
405 .read = proc_reg_read,
406 .write = proc_reg_write,
407 .poll = proc_reg_poll,
408 .unlocked_ioctl = proc_reg_unlocked_ioctl,
409 #ifdef CONFIG_COMPAT
410 .compat_ioctl = proc_reg_compat_ioctl,
411 #endif
412 .mmap = proc_reg_mmap,
413 .open = proc_reg_open,
414 .release = proc_reg_release,
417 #ifdef CONFIG_COMPAT
418 static const struct file_operations proc_reg_file_ops_no_compat = {
419 .llseek = proc_reg_llseek,
420 .read = proc_reg_read,
421 .write = proc_reg_write,
422 .poll = proc_reg_poll,
423 .unlocked_ioctl = proc_reg_unlocked_ioctl,
424 .mmap = proc_reg_mmap,
425 .open = proc_reg_open,
426 .release = proc_reg_release,
428 #endif
430 struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
432 struct inode * inode;
434 inode = iget_locked(sb, de->low_ino);
435 if (!inode)
436 return NULL;
437 if (inode->i_state & I_NEW) {
438 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
439 PROC_I(inode)->fd = 0;
440 PROC_I(inode)->pde = de;
442 if (de->mode) {
443 inode->i_mode = de->mode;
444 inode->i_uid = de->uid;
445 inode->i_gid = de->gid;
447 if (de->size)
448 inode->i_size = de->size;
449 if (de->nlink)
450 inode->i_nlink = de->nlink;
451 if (de->proc_iops)
452 inode->i_op = de->proc_iops;
453 if (de->proc_fops) {
454 if (S_ISREG(inode->i_mode)) {
455 #ifdef CONFIG_COMPAT
456 if (!de->proc_fops->compat_ioctl)
457 inode->i_fop =
458 &proc_reg_file_ops_no_compat;
459 else
460 #endif
461 inode->i_fop = &proc_reg_file_ops;
462 } else {
463 inode->i_fop = de->proc_fops;
466 unlock_new_inode(inode);
467 } else
468 pde_put(de);
469 return inode;
472 int proc_fill_super(struct super_block *s)
474 struct inode * root_inode;
476 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
477 s->s_blocksize = 1024;
478 s->s_blocksize_bits = 10;
479 s->s_magic = PROC_SUPER_MAGIC;
480 s->s_op = &proc_sops;
481 s->s_time_gran = 1;
483 pde_get(&proc_root);
484 root_inode = proc_get_inode(s, &proc_root);
485 if (!root_inode)
486 goto out_no_root;
487 root_inode->i_uid = 0;
488 root_inode->i_gid = 0;
489 s->s_root = d_alloc_root(root_inode);
490 if (!s->s_root)
491 goto out_no_root;
492 return 0;
494 out_no_root:
495 printk("proc_read_super: get root inode failed\n");
496 iput(root_inode);
497 pde_put(&proc_root);
498 return -ENOMEM;