blob: 176ce4cda68a113d10e3107830730848dc0cd7f5 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mark Fashehfef26652005-09-09 13:01:31 -070031 truncate_inode_pages(&inode->i_data, 0);
Al Viro82679522010-06-04 22:17:56 -040032 end_writeback(inode);
Mark Fashehfef26652005-09-09 13:01:31 -070033
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}
44
45struct vfsmount *proc_mnt;
46
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static struct inode *proc_alloc_inode(struct super_block *sb)
50{
51 struct proc_inode *ei;
52 struct inode *inode;
53
Christoph Lametere94b1762006-12-06 20:33:17 -080054 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!ei)
56 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070057 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070058 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 ei->op.proc_get_link = NULL;
60 ei->pde = NULL;
Al Viro9043476f2008-07-15 08:54:06 -040061 ei->sysctl = NULL;
62 ei->sysctl_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 inode = &ei->vfs_inode;
64 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
65 return inode;
66}
67
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110068static void proc_i_callback(struct rcu_head *head)
69{
70 struct inode *inode = container_of(head, struct inode, i_rcu);
71 INIT_LIST_HEAD(&inode->i_dentry);
72 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void proc_destroy_inode(struct inode *inode)
76{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110077 call_rcu(&inode->i_rcu, proc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070080static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct proc_inode *ei = (struct proc_inode *) foo;
83
Christoph Lametera35afb82007-05-16 22:10:57 -070084 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Paul Mundt20c2df82007-07-20 10:11:58 +090086
Alexey Dobriyan5bcd7ff2008-10-17 03:43:55 +040087void __init proc_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
90 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -080091 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -070092 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +090093 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080096static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .alloc_inode = proc_alloc_inode,
98 .destroy_inode = proc_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .drop_inode = generic_delete_inode,
Al Viro82679522010-06-04 22:17:56 -0400100 .evict_inode = proc_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700104static void __pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700105{
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700106 pde->pde_users--;
107 if (pde->pde_unload_completion && pde->pde_users == 0)
108 complete(pde->pde_unload_completion);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700109}
110
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300111void pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700112{
113 spin_lock(&pde->pde_unload_lock);
114 __pde_users_dec(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700115 spin_unlock(&pde->pde_unload_lock);
116}
117
118static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
119{
120 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
121 loff_t rv = -EINVAL;
122 loff_t (*llseek)(struct file *, loff_t, int);
123
124 spin_lock(&pde->pde_unload_lock);
125 /*
126 * remove_proc_entry() is going to delete PDE (as part of module
127 * cleanup sequence). No new callers into module allowed.
128 */
129 if (!pde->proc_fops) {
130 spin_unlock(&pde->pde_unload_lock);
131 return rv;
132 }
133 /*
134 * Bump refcount so that remove_proc_entry will wail for ->llseek to
135 * complete.
136 */
137 pde->pde_users++;
138 /*
139 * Save function pointer under lock, to protect against ->proc_fops
140 * NULL'ifying right after ->pde_unload_lock is dropped.
141 */
142 llseek = pde->proc_fops->llseek;
143 spin_unlock(&pde->pde_unload_lock);
144
145 if (!llseek)
146 llseek = default_llseek;
147 rv = llseek(file, offset, whence);
148
149 pde_users_dec(pde);
150 return rv;
151}
152
153static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
154{
155 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
156 ssize_t rv = -EIO;
157 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
158
159 spin_lock(&pde->pde_unload_lock);
160 if (!pde->proc_fops) {
161 spin_unlock(&pde->pde_unload_lock);
162 return rv;
163 }
164 pde->pde_users++;
165 read = pde->proc_fops->read;
166 spin_unlock(&pde->pde_unload_lock);
167
168 if (read)
169 rv = read(file, buf, count, ppos);
170
171 pde_users_dec(pde);
172 return rv;
173}
174
175static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
176{
177 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
178 ssize_t rv = -EIO;
179 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
180
181 spin_lock(&pde->pde_unload_lock);
182 if (!pde->proc_fops) {
183 spin_unlock(&pde->pde_unload_lock);
184 return rv;
185 }
186 pde->pde_users++;
187 write = pde->proc_fops->write;
188 spin_unlock(&pde->pde_unload_lock);
189
190 if (write)
191 rv = write(file, buf, count, ppos);
192
193 pde_users_dec(pde);
194 return rv;
195}
196
197static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
198{
199 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700200 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700201 unsigned int (*poll)(struct file *, struct poll_table_struct *);
202
203 spin_lock(&pde->pde_unload_lock);
204 if (!pde->proc_fops) {
205 spin_unlock(&pde->pde_unload_lock);
206 return rv;
207 }
208 pde->pde_users++;
209 poll = pde->proc_fops->poll;
210 spin_unlock(&pde->pde_unload_lock);
211
212 if (poll)
213 rv = poll(file, pts);
214
215 pde_users_dec(pde);
216 return rv;
217}
218
219static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
220{
221 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
222 long rv = -ENOTTY;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200223 long (*ioctl)(struct file *, unsigned int, unsigned long);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700224
225 spin_lock(&pde->pde_unload_lock);
226 if (!pde->proc_fops) {
227 spin_unlock(&pde->pde_unload_lock);
228 return rv;
229 }
230 pde->pde_users++;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200231 ioctl = pde->proc_fops->unlocked_ioctl;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700232 spin_unlock(&pde->pde_unload_lock);
233
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200234 if (ioctl)
235 rv = ioctl(file, cmd, arg);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700236
237 pde_users_dec(pde);
238 return rv;
239}
240
241#ifdef CONFIG_COMPAT
242static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
243{
244 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
245 long rv = -ENOTTY;
246 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
247
248 spin_lock(&pde->pde_unload_lock);
249 if (!pde->proc_fops) {
250 spin_unlock(&pde->pde_unload_lock);
251 return rv;
252 }
253 pde->pde_users++;
254 compat_ioctl = pde->proc_fops->compat_ioctl;
255 spin_unlock(&pde->pde_unload_lock);
256
257 if (compat_ioctl)
258 rv = compat_ioctl(file, cmd, arg);
259
260 pde_users_dec(pde);
261 return rv;
262}
263#endif
264
265static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
266{
267 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
268 int rv = -EIO;
269 int (*mmap)(struct file *, struct vm_area_struct *);
270
271 spin_lock(&pde->pde_unload_lock);
272 if (!pde->proc_fops) {
273 spin_unlock(&pde->pde_unload_lock);
274 return rv;
275 }
276 pde->pde_users++;
277 mmap = pde->proc_fops->mmap;
278 spin_unlock(&pde->pde_unload_lock);
279
280 if (mmap)
281 rv = mmap(file, vma);
282
283 pde_users_dec(pde);
284 return rv;
285}
286
287static int proc_reg_open(struct inode *inode, struct file *file)
288{
289 struct proc_dir_entry *pde = PDE(inode);
290 int rv = 0;
291 int (*open)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700292 int (*release)(struct inode *, struct file *);
293 struct pde_opener *pdeo;
294
295 /*
296 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
297 * sequence. ->release won't be called because ->proc_fops will be
298 * cleared. Depending on complexity of ->release, consequences vary.
299 *
300 * We can't wait for mercy when close will be done for real, it's
301 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
302 * by hand in remove_proc_entry(). For this, save opener's credentials
303 * for later.
304 */
305 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
306 if (!pdeo)
307 return -ENOMEM;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700308
309 spin_lock(&pde->pde_unload_lock);
310 if (!pde->proc_fops) {
311 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700312 kfree(pdeo);
Alexey Dobriyan300b9942008-10-03 00:18:52 +0400313 return -EINVAL;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700314 }
315 pde->pde_users++;
316 open = pde->proc_fops->open;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700317 release = pde->proc_fops->release;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700318 spin_unlock(&pde->pde_unload_lock);
319
320 if (open)
321 rv = open(inode, file);
322
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700323 spin_lock(&pde->pde_unload_lock);
324 if (rv == 0 && release) {
325 /* To know what to release. */
326 pdeo->inode = inode;
327 pdeo->file = file;
328 /* Strictly for "too late" ->release in proc_reg_release(). */
329 pdeo->release = release;
330 list_add(&pdeo->lh, &pde->pde_openers);
331 } else
332 kfree(pdeo);
333 __pde_users_dec(pde);
334 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700335 return rv;
336}
337
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700338static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
339 struct inode *inode, struct file *file)
340{
341 struct pde_opener *pdeo;
342
343 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
344 if (pdeo->inode == inode && pdeo->file == file)
345 return pdeo;
346 }
347 return NULL;
348}
349
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700350static int proc_reg_release(struct inode *inode, struct file *file)
351{
352 struct proc_dir_entry *pde = PDE(inode);
353 int rv = 0;
354 int (*release)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700355 struct pde_opener *pdeo;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700356
357 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700358 pdeo = find_pde_opener(pde, inode, file);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700359 if (!pde->proc_fops) {
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700360 /*
361 * Can't simply exit, __fput() will think that everything is OK,
362 * and move on to freeing struct file. remove_proc_entry() will
363 * find slacker in opener's list and will try to do non-trivial
364 * things with struct file. Therefore, remove opener from list.
365 *
366 * But if opener is removed from list, who will ->release it?
367 */
368 if (pdeo) {
369 list_del(&pdeo->lh);
370 spin_unlock(&pde->pde_unload_lock);
371 rv = pdeo->release(inode, file);
372 kfree(pdeo);
373 } else
374 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700375 return rv;
376 }
377 pde->pde_users++;
378 release = pde->proc_fops->release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700379 if (pdeo) {
380 list_del(&pdeo->lh);
381 kfree(pdeo);
382 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700383 spin_unlock(&pde->pde_unload_lock);
384
385 if (release)
386 rv = release(inode, file);
387
388 pde_users_dec(pde);
389 return rv;
390}
391
392static const struct file_operations proc_reg_file_ops = {
393 .llseek = proc_reg_llseek,
394 .read = proc_reg_read,
395 .write = proc_reg_write,
396 .poll = proc_reg_poll,
397 .unlocked_ioctl = proc_reg_unlocked_ioctl,
398#ifdef CONFIG_COMPAT
399 .compat_ioctl = proc_reg_compat_ioctl,
400#endif
401 .mmap = proc_reg_mmap,
402 .open = proc_reg_open,
403 .release = proc_reg_release,
404};
405
David Miller778f3dd2007-07-27 22:58:37 -0700406#ifdef CONFIG_COMPAT
407static const struct file_operations proc_reg_file_ops_no_compat = {
408 .llseek = proc_reg_llseek,
409 .read = proc_reg_read,
410 .write = proc_reg_write,
411 .poll = proc_reg_poll,
412 .unlocked_ioctl = proc_reg_unlocked_ioctl,
413 .mmap = proc_reg_mmap,
414 .open = proc_reg_open,
415 .release = proc_reg_release,
416};
417#endif
418
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800419struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct inode * inode;
422
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800423 inode = iget_locked(sb, de->low_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!inode)
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300425 return NULL;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800426 if (inode->i_state & I_NEW) {
427 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
428 PROC_I(inode)->fd = 0;
429 PROC_I(inode)->pde = de;
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700430
431 if (de->mode) {
432 inode->i_mode = de->mode;
433 inode->i_uid = de->uid;
434 inode->i_gid = de->gid;
435 }
436 if (de->size)
437 inode->i_size = de->size;
438 if (de->nlink)
439 inode->i_nlink = de->nlink;
440 if (de->proc_iops)
441 inode->i_op = de->proc_iops;
442 if (de->proc_fops) {
443 if (S_ISREG(inode->i_mode)) {
David Howellsa1d4aeb2008-02-07 00:15:45 -0800444#ifdef CONFIG_COMPAT
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700445 if (!de->proc_fops->compat_ioctl)
446 inode->i_fop =
447 &proc_reg_file_ops_no_compat;
448 else
David Howellsa1d4aeb2008-02-07 00:15:45 -0800449#endif
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700450 inode->i_fop = &proc_reg_file_ops;
451 } else {
452 inode->i_fop = de->proc_fops;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800453 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700454 }
David Howellsa1d4aeb2008-02-07 00:15:45 -0800455 unlock_new_inode(inode);
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300456 } else
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800457 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Pavel Emelyanov07543f52007-10-18 23:40:08 -0700461int proc_fill_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct inode * root_inode;
464
Linus Torvalds92d03282006-07-15 12:20:05 -0700465 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 s->s_blocksize = 1024;
467 s->s_blocksize_bits = 10;
468 s->s_magic = PROC_SUPER_MAGIC;
469 s->s_op = &proc_sops;
470 s->s_time_gran = 1;
471
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800472 pde_get(&proc_root);
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800473 root_inode = proc_get_inode(s, &proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!root_inode)
475 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 root_inode->i_uid = 0;
477 root_inode->i_gid = 0;
478 s->s_root = d_alloc_root(root_inode);
479 if (!s->s_root)
480 goto out_no_root;
481 return 0;
482
483out_no_root:
484 printk("proc_read_super: get root inode failed\n");
485 iput(root_inode);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800486 pde_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -ENOMEM;
488}