blob: d6a7ca1fdac53dfdf47fb7109207c0ec7597a3e7 [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>
Al Viro9043476f2008-07-15 08:54:06 -040019#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/system.h>
23#include <asm/uaccess.h>
24
Adrian Bunkfee781e2006-01-08 01:04:16 -080025#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Al Viro82679522010-06-04 22:17:56 -040027static void proc_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct proc_dir_entry *de;
Al Virodfef6dcd32011-03-08 01:25:28 -050030 struct ctl_table_header *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Mark Fashehfef26652005-09-09 13:01:31 -070032 truncate_inode_pages(&inode->i_data, 0);
Al Viro82679522010-06-04 22:17:56 -040033 end_writeback(inode);
Mark Fashehfef26652005-09-09 13:01:31 -070034
Eric W. Biederman99f89552006-06-26 00:25:55 -070035 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070036 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /* Let go of any associated proc directory entry */
39 de = PROC_I(inode)->pde;
Alexey Dobriyan99b76232009-03-25 22:48:06 +030040 if (de)
Alexey Dobriyan135d5652009-12-15 16:45:39 -080041 pde_put(de);
Al Virodfef6dcd32011-03-08 01:25:28 -050042 head = PROC_I(inode)->sysctl;
43 if (head) {
44 rcu_assign_pointer(PROC_I(inode)->sysctl, NULL);
45 sysctl_head_put(head);
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49struct vfsmount *proc_mnt;
50
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static struct inode *proc_alloc_inode(struct super_block *sb)
54{
55 struct proc_inode *ei;
56 struct inode *inode;
57
Christoph Lametere94b1762006-12-06 20:33:17 -080058 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!ei)
60 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070061 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070062 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ei->op.proc_get_link = NULL;
64 ei->pde = NULL;
Al Viro9043476f2008-07-15 08:54:06 -040065 ei->sysctl = NULL;
66 ei->sysctl_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 inode = &ei->vfs_inode;
68 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
69 return inode;
70}
71
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110072static void proc_i_callback(struct rcu_head *head)
73{
74 struct inode *inode = container_of(head, struct inode, i_rcu);
75 INIT_LIST_HEAD(&inode->i_dentry);
76 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void proc_destroy_inode(struct inode *inode)
80{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110081 call_rcu(&inode->i_rcu, proc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070084static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct proc_inode *ei = (struct proc_inode *) foo;
87
Christoph Lametera35afb82007-05-16 22:10:57 -070088 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Paul Mundt20c2df82007-07-20 10:11:58 +090090
Alexey Dobriyan5bcd7ff2008-10-17 03:43:55 +040091void __init proc_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
94 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -080095 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -070096 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +090097 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800100static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .alloc_inode = proc_alloc_inode,
102 .destroy_inode = proc_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .drop_inode = generic_delete_inode,
Al Viro82679522010-06-04 22:17:56 -0400104 .evict_inode = proc_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700108static void __pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700109{
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700110 pde->pde_users--;
111 if (pde->pde_unload_completion && pde->pde_users == 0)
112 complete(pde->pde_unload_completion);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700113}
114
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300115void pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700116{
117 spin_lock(&pde->pde_unload_lock);
118 __pde_users_dec(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700119 spin_unlock(&pde->pde_unload_lock);
120}
121
122static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
123{
124 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
125 loff_t rv = -EINVAL;
126 loff_t (*llseek)(struct file *, loff_t, int);
127
128 spin_lock(&pde->pde_unload_lock);
129 /*
130 * remove_proc_entry() is going to delete PDE (as part of module
131 * cleanup sequence). No new callers into module allowed.
132 */
133 if (!pde->proc_fops) {
134 spin_unlock(&pde->pde_unload_lock);
135 return rv;
136 }
137 /*
138 * Bump refcount so that remove_proc_entry will wail for ->llseek to
139 * complete.
140 */
141 pde->pde_users++;
142 /*
143 * Save function pointer under lock, to protect against ->proc_fops
144 * NULL'ifying right after ->pde_unload_lock is dropped.
145 */
146 llseek = pde->proc_fops->llseek;
147 spin_unlock(&pde->pde_unload_lock);
148
149 if (!llseek)
150 llseek = default_llseek;
151 rv = llseek(file, offset, whence);
152
153 pde_users_dec(pde);
154 return rv;
155}
156
157static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
158{
159 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
160 ssize_t rv = -EIO;
161 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
162
163 spin_lock(&pde->pde_unload_lock);
164 if (!pde->proc_fops) {
165 spin_unlock(&pde->pde_unload_lock);
166 return rv;
167 }
168 pde->pde_users++;
169 read = pde->proc_fops->read;
170 spin_unlock(&pde->pde_unload_lock);
171
172 if (read)
173 rv = read(file, buf, count, ppos);
174
175 pde_users_dec(pde);
176 return rv;
177}
178
179static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
180{
181 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
182 ssize_t rv = -EIO;
183 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
184
185 spin_lock(&pde->pde_unload_lock);
186 if (!pde->proc_fops) {
187 spin_unlock(&pde->pde_unload_lock);
188 return rv;
189 }
190 pde->pde_users++;
191 write = pde->proc_fops->write;
192 spin_unlock(&pde->pde_unload_lock);
193
194 if (write)
195 rv = write(file, buf, count, ppos);
196
197 pde_users_dec(pde);
198 return rv;
199}
200
201static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
202{
203 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700204 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700205 unsigned int (*poll)(struct file *, struct poll_table_struct *);
206
207 spin_lock(&pde->pde_unload_lock);
208 if (!pde->proc_fops) {
209 spin_unlock(&pde->pde_unload_lock);
210 return rv;
211 }
212 pde->pde_users++;
213 poll = pde->proc_fops->poll;
214 spin_unlock(&pde->pde_unload_lock);
215
216 if (poll)
217 rv = poll(file, pts);
218
219 pde_users_dec(pde);
220 return rv;
221}
222
223static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
224{
225 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
226 long rv = -ENOTTY;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200227 long (*ioctl)(struct file *, unsigned int, unsigned long);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700228
229 spin_lock(&pde->pde_unload_lock);
230 if (!pde->proc_fops) {
231 spin_unlock(&pde->pde_unload_lock);
232 return rv;
233 }
234 pde->pde_users++;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200235 ioctl = pde->proc_fops->unlocked_ioctl;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700236 spin_unlock(&pde->pde_unload_lock);
237
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200238 if (ioctl)
239 rv = ioctl(file, cmd, arg);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700240
241 pde_users_dec(pde);
242 return rv;
243}
244
245#ifdef CONFIG_COMPAT
246static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
247{
248 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
249 long rv = -ENOTTY;
250 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
251
252 spin_lock(&pde->pde_unload_lock);
253 if (!pde->proc_fops) {
254 spin_unlock(&pde->pde_unload_lock);
255 return rv;
256 }
257 pde->pde_users++;
258 compat_ioctl = pde->proc_fops->compat_ioctl;
259 spin_unlock(&pde->pde_unload_lock);
260
261 if (compat_ioctl)
262 rv = compat_ioctl(file, cmd, arg);
263
264 pde_users_dec(pde);
265 return rv;
266}
267#endif
268
269static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
270{
271 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
272 int rv = -EIO;
273 int (*mmap)(struct file *, struct vm_area_struct *);
274
275 spin_lock(&pde->pde_unload_lock);
276 if (!pde->proc_fops) {
277 spin_unlock(&pde->pde_unload_lock);
278 return rv;
279 }
280 pde->pde_users++;
281 mmap = pde->proc_fops->mmap;
282 spin_unlock(&pde->pde_unload_lock);
283
284 if (mmap)
285 rv = mmap(file, vma);
286
287 pde_users_dec(pde);
288 return rv;
289}
290
291static int proc_reg_open(struct inode *inode, struct file *file)
292{
293 struct proc_dir_entry *pde = PDE(inode);
294 int rv = 0;
295 int (*open)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700296 int (*release)(struct inode *, struct file *);
297 struct pde_opener *pdeo;
298
299 /*
300 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
301 * sequence. ->release won't be called because ->proc_fops will be
302 * cleared. Depending on complexity of ->release, consequences vary.
303 *
304 * We can't wait for mercy when close will be done for real, it's
305 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
306 * by hand in remove_proc_entry(). For this, save opener's credentials
307 * for later.
308 */
309 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
310 if (!pdeo)
311 return -ENOMEM;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700312
313 spin_lock(&pde->pde_unload_lock);
314 if (!pde->proc_fops) {
315 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700316 kfree(pdeo);
Alexey Dobriyan300b9942008-10-03 00:18:52 +0400317 return -EINVAL;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700318 }
319 pde->pde_users++;
320 open = pde->proc_fops->open;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700321 release = pde->proc_fops->release;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700322 spin_unlock(&pde->pde_unload_lock);
323
324 if (open)
325 rv = open(inode, file);
326
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700327 spin_lock(&pde->pde_unload_lock);
328 if (rv == 0 && release) {
329 /* To know what to release. */
330 pdeo->inode = inode;
331 pdeo->file = file;
332 /* Strictly for "too late" ->release in proc_reg_release(). */
333 pdeo->release = release;
334 list_add(&pdeo->lh, &pde->pde_openers);
335 } else
336 kfree(pdeo);
337 __pde_users_dec(pde);
338 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700339 return rv;
340}
341
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700342static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
343 struct inode *inode, struct file *file)
344{
345 struct pde_opener *pdeo;
346
347 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
348 if (pdeo->inode == inode && pdeo->file == file)
349 return pdeo;
350 }
351 return NULL;
352}
353
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700354static int proc_reg_release(struct inode *inode, struct file *file)
355{
356 struct proc_dir_entry *pde = PDE(inode);
357 int rv = 0;
358 int (*release)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700359 struct pde_opener *pdeo;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700360
361 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700362 pdeo = find_pde_opener(pde, inode, file);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700363 if (!pde->proc_fops) {
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700364 /*
365 * Can't simply exit, __fput() will think that everything is OK,
366 * and move on to freeing struct file. remove_proc_entry() will
367 * find slacker in opener's list and will try to do non-trivial
368 * things with struct file. Therefore, remove opener from list.
369 *
370 * But if opener is removed from list, who will ->release it?
371 */
372 if (pdeo) {
373 list_del(&pdeo->lh);
374 spin_unlock(&pde->pde_unload_lock);
375 rv = pdeo->release(inode, file);
376 kfree(pdeo);
377 } else
378 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700379 return rv;
380 }
381 pde->pde_users++;
382 release = pde->proc_fops->release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700383 if (pdeo) {
384 list_del(&pdeo->lh);
385 kfree(pdeo);
386 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700387 spin_unlock(&pde->pde_unload_lock);
388
389 if (release)
390 rv = release(inode, file);
391
392 pde_users_dec(pde);
393 return rv;
394}
395
396static const struct file_operations proc_reg_file_ops = {
397 .llseek = proc_reg_llseek,
398 .read = proc_reg_read,
399 .write = proc_reg_write,
400 .poll = proc_reg_poll,
401 .unlocked_ioctl = proc_reg_unlocked_ioctl,
402#ifdef CONFIG_COMPAT
403 .compat_ioctl = proc_reg_compat_ioctl,
404#endif
405 .mmap = proc_reg_mmap,
406 .open = proc_reg_open,
407 .release = proc_reg_release,
408};
409
David Miller778f3dd2007-07-27 22:58:37 -0700410#ifdef CONFIG_COMPAT
411static const struct file_operations proc_reg_file_ops_no_compat = {
412 .llseek = proc_reg_llseek,
413 .read = proc_reg_read,
414 .write = proc_reg_write,
415 .poll = proc_reg_poll,
416 .unlocked_ioctl = proc_reg_unlocked_ioctl,
417 .mmap = proc_reg_mmap,
418 .open = proc_reg_open,
419 .release = proc_reg_release,
420};
421#endif
422
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800423struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct inode * inode;
426
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800427 inode = iget_locked(sb, de->low_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);
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800477 root_inode = proc_get_inode(s, &proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 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}