blob: d15aa1b1cc8fea9f9a7e702849035095e38f4613 [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 Viro90434762008-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 Virodfef6dc2011-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 Virodfef6dc2011-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
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static struct inode *proc_alloc_inode(struct super_block *sb)
52{
53 struct proc_inode *ei;
54 struct inode *inode;
55
Christoph Lametere94b1762006-12-06 20:33:17 -080056 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (!ei)
58 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070059 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070060 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ei->op.proc_get_link = NULL;
62 ei->pde = NULL;
Al Viro90434762008-07-15 08:54:06 -040063 ei->sysctl = NULL;
64 ei->sysctl_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 inode = &ei->vfs_inode;
66 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
67 return inode;
68}
69
Nick Pigginfa0d7e32011-01-07 17:49:49 +110070static void proc_i_callback(struct rcu_head *head)
71{
72 struct inode *inode = container_of(head, struct inode, i_rcu);
73 INIT_LIST_HEAD(&inode->i_dentry);
74 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void proc_destroy_inode(struct inode *inode)
78{
Nick Pigginfa0d7e32011-01-07 17:49:49 +110079 call_rcu(&inode->i_rcu, proc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070082static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct proc_inode *ei = (struct proc_inode *) foo;
85
Christoph Lametera35afb82007-05-16 22:10:57 -070086 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Paul Mundt20c2df82007-07-20 10:11:58 +090088
Alexey Dobriyan5bcd7ff2008-10-17 03:43:55 +040089void __init proc_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
92 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -080093 0, (SLAB_RECLAIM_ACCOUNT|
Alexey Dobriyan040b5c62007-10-16 23:26:10 -070094 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +090095 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080098static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .alloc_inode = proc_alloc_inode,
100 .destroy_inode = proc_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .drop_inode = generic_delete_inode,
Al Viro82679522010-06-04 22:17:56 -0400102 .evict_inode = proc_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700106static void __pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700107{
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700108 pde->pde_users--;
109 if (pde->pde_unload_completion && pde->pde_users == 0)
110 complete(pde->pde_unload_completion);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700111}
112
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300113void pde_users_dec(struct proc_dir_entry *pde)
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700114{
115 spin_lock(&pde->pde_unload_lock);
116 __pde_users_dec(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700117 spin_unlock(&pde->pde_unload_lock);
118}
119
120static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
121{
122 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
123 loff_t rv = -EINVAL;
124 loff_t (*llseek)(struct file *, loff_t, int);
125
126 spin_lock(&pde->pde_unload_lock);
127 /*
128 * remove_proc_entry() is going to delete PDE (as part of module
129 * cleanup sequence). No new callers into module allowed.
130 */
131 if (!pde->proc_fops) {
132 spin_unlock(&pde->pde_unload_lock);
133 return rv;
134 }
135 /*
136 * Bump refcount so that remove_proc_entry will wail for ->llseek to
137 * complete.
138 */
139 pde->pde_users++;
140 /*
141 * Save function pointer under lock, to protect against ->proc_fops
142 * NULL'ifying right after ->pde_unload_lock is dropped.
143 */
144 llseek = pde->proc_fops->llseek;
145 spin_unlock(&pde->pde_unload_lock);
146
147 if (!llseek)
148 llseek = default_llseek;
149 rv = llseek(file, offset, whence);
150
151 pde_users_dec(pde);
152 return rv;
153}
154
155static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
156{
157 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
158 ssize_t rv = -EIO;
159 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
160
161 spin_lock(&pde->pde_unload_lock);
162 if (!pde->proc_fops) {
163 spin_unlock(&pde->pde_unload_lock);
164 return rv;
165 }
166 pde->pde_users++;
167 read = pde->proc_fops->read;
168 spin_unlock(&pde->pde_unload_lock);
169
170 if (read)
171 rv = read(file, buf, count, ppos);
172
173 pde_users_dec(pde);
174 return rv;
175}
176
177static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
178{
179 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
180 ssize_t rv = -EIO;
181 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
182
183 spin_lock(&pde->pde_unload_lock);
184 if (!pde->proc_fops) {
185 spin_unlock(&pde->pde_unload_lock);
186 return rv;
187 }
188 pde->pde_users++;
189 write = pde->proc_fops->write;
190 spin_unlock(&pde->pde_unload_lock);
191
192 if (write)
193 rv = write(file, buf, count, ppos);
194
195 pde_users_dec(pde);
196 return rv;
197}
198
199static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
200{
201 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Alexey Dobriyandd23aae2007-09-11 15:23:55 -0700202 unsigned int rv = DEFAULT_POLLMASK;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700203 unsigned int (*poll)(struct file *, struct poll_table_struct *);
204
205 spin_lock(&pde->pde_unload_lock);
206 if (!pde->proc_fops) {
207 spin_unlock(&pde->pde_unload_lock);
208 return rv;
209 }
210 pde->pde_users++;
211 poll = pde->proc_fops->poll;
212 spin_unlock(&pde->pde_unload_lock);
213
214 if (poll)
215 rv = poll(file, pts);
216
217 pde_users_dec(pde);
218 return rv;
219}
220
221static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
222{
223 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
224 long rv = -ENOTTY;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200225 long (*ioctl)(struct file *, unsigned int, unsigned long);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700226
227 spin_lock(&pde->pde_unload_lock);
228 if (!pde->proc_fops) {
229 spin_unlock(&pde->pde_unload_lock);
230 return rv;
231 }
232 pde->pde_users++;
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200233 ioctl = pde->proc_fops->unlocked_ioctl;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700234 spin_unlock(&pde->pde_unload_lock);
235
Arnd Bergmannb19dd422010-07-04 00:15:10 +0200236 if (ioctl)
237 rv = ioctl(file, cmd, arg);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700238
239 pde_users_dec(pde);
240 return rv;
241}
242
243#ifdef CONFIG_COMPAT
244static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
245{
246 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
247 long rv = -ENOTTY;
248 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
249
250 spin_lock(&pde->pde_unload_lock);
251 if (!pde->proc_fops) {
252 spin_unlock(&pde->pde_unload_lock);
253 return rv;
254 }
255 pde->pde_users++;
256 compat_ioctl = pde->proc_fops->compat_ioctl;
257 spin_unlock(&pde->pde_unload_lock);
258
259 if (compat_ioctl)
260 rv = compat_ioctl(file, cmd, arg);
261
262 pde_users_dec(pde);
263 return rv;
264}
265#endif
266
267static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
268{
269 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
270 int rv = -EIO;
271 int (*mmap)(struct file *, struct vm_area_struct *);
272
273 spin_lock(&pde->pde_unload_lock);
274 if (!pde->proc_fops) {
275 spin_unlock(&pde->pde_unload_lock);
276 return rv;
277 }
278 pde->pde_users++;
279 mmap = pde->proc_fops->mmap;
280 spin_unlock(&pde->pde_unload_lock);
281
282 if (mmap)
283 rv = mmap(file, vma);
284
285 pde_users_dec(pde);
286 return rv;
287}
288
289static int proc_reg_open(struct inode *inode, struct file *file)
290{
291 struct proc_dir_entry *pde = PDE(inode);
292 int rv = 0;
293 int (*open)(struct inode *, struct file *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700294 int (*release)(struct inode *, struct file *);
295 struct pde_opener *pdeo;
296
297 /*
298 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
299 * sequence. ->release won't be called because ->proc_fops will be
300 * cleared. Depending on complexity of ->release, consequences vary.
301 *
302 * We can't wait for mercy when close will be done for real, it's
303 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
304 * by hand in remove_proc_entry(). For this, save opener's credentials
305 * for later.
306 */
307 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
308 if (!pdeo)
309 return -ENOMEM;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700310
311 spin_lock(&pde->pde_unload_lock);
312 if (!pde->proc_fops) {
313 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700314 kfree(pdeo);
Alexey Dobriyan300b9942008-10-03 00:18:52 +0400315 return -EINVAL;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700316 }
317 pde->pde_users++;
318 open = pde->proc_fops->open;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700319 release = pde->proc_fops->release;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700320 spin_unlock(&pde->pde_unload_lock);
321
322 if (open)
323 rv = open(inode, file);
324
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700325 spin_lock(&pde->pde_unload_lock);
326 if (rv == 0 && release) {
327 /* To know what to release. */
328 pdeo->inode = inode;
329 pdeo->file = file;
330 /* Strictly for "too late" ->release in proc_reg_release(). */
331 pdeo->release = release;
332 list_add(&pdeo->lh, &pde->pde_openers);
333 } else
334 kfree(pdeo);
335 __pde_users_dec(pde);
336 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700337 return rv;
338}
339
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700340static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
341 struct inode *inode, struct file *file)
342{
343 struct pde_opener *pdeo;
344
345 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
346 if (pdeo->inode == inode && pdeo->file == file)
347 return pdeo;
348 }
349 return NULL;
350}
351
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700352static 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 *);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700357 struct pde_opener *pdeo;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700358
359 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700360 pdeo = find_pde_opener(pde, inode, file);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700361 if (!pde->proc_fops) {
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700362 /*
363 * Can't simply exit, __fput() will think that everything is OK,
364 * and move on to freeing struct file. remove_proc_entry() will
365 * find slacker in opener's list and will try to do non-trivial
366 * things with struct file. Therefore, remove opener from list.
367 *
368 * But if opener is removed from list, who will ->release it?
369 */
370 if (pdeo) {
371 list_del(&pdeo->lh);
372 spin_unlock(&pde->pde_unload_lock);
373 rv = pdeo->release(inode, file);
374 kfree(pdeo);
375 } else
376 spin_unlock(&pde->pde_unload_lock);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700377 return rv;
378 }
379 pde->pde_users++;
380 release = pde->proc_fops->release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700381 if (pdeo) {
382 list_del(&pdeo->lh);
383 kfree(pdeo);
384 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700385 spin_unlock(&pde->pde_unload_lock);
386
387 if (release)
388 rv = release(inode, file);
389
390 pde_users_dec(pde);
391 return rv;
392}
393
394static const struct file_operations proc_reg_file_ops = {
395 .llseek = proc_reg_llseek,
396 .read = proc_reg_read,
397 .write = proc_reg_write,
398 .poll = proc_reg_poll,
399 .unlocked_ioctl = proc_reg_unlocked_ioctl,
400#ifdef CONFIG_COMPAT
401 .compat_ioctl = proc_reg_compat_ioctl,
402#endif
403 .mmap = proc_reg_mmap,
404 .open = proc_reg_open,
405 .release = proc_reg_release,
406};
407
David Miller778f3dd2007-07-27 22:58:37 -0700408#ifdef CONFIG_COMPAT
409static const struct file_operations proc_reg_file_ops_no_compat = {
410 .llseek = proc_reg_llseek,
411 .read = proc_reg_read,
412 .write = proc_reg_write,
413 .poll = proc_reg_poll,
414 .unlocked_ioctl = proc_reg_unlocked_ioctl,
415 .mmap = proc_reg_mmap,
416 .open = proc_reg_open,
417 .release = proc_reg_release,
418};
419#endif
420
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800421struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct inode * inode;
424
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800425 inode = iget_locked(sb, de->low_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!inode)
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300427 return NULL;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800428 if (inode->i_state & I_NEW) {
429 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
430 PROC_I(inode)->fd = 0;
431 PROC_I(inode)->pde = de;
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700432
433 if (de->mode) {
434 inode->i_mode = de->mode;
435 inode->i_uid = de->uid;
436 inode->i_gid = de->gid;
437 }
438 if (de->size)
439 inode->i_size = de->size;
440 if (de->nlink)
441 inode->i_nlink = de->nlink;
442 if (de->proc_iops)
443 inode->i_op = de->proc_iops;
444 if (de->proc_fops) {
445 if (S_ISREG(inode->i_mode)) {
David Howellsa1d4aeb2008-02-07 00:15:45 -0800446#ifdef CONFIG_COMPAT
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700447 if (!de->proc_fops->compat_ioctl)
448 inode->i_fop =
449 &proc_reg_file_ops_no_compat;
450 else
David Howellsa1d4aeb2008-02-07 00:15:45 -0800451#endif
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700452 inode->i_fop = &proc_reg_file_ops;
453 } else {
454 inode->i_fop = de->proc_fops;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800455 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700456 }
David Howellsa1d4aeb2008-02-07 00:15:45 -0800457 unlock_new_inode(inode);
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300458 } else
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800459 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Pavel Emelyanov07543f52007-10-18 23:40:08 -0700463int proc_fill_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct inode * root_inode;
466
Linus Torvalds92d03282006-07-15 12:20:05 -0700467 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 s->s_blocksize = 1024;
469 s->s_blocksize_bits = 10;
470 s->s_magic = PROC_SUPER_MAGIC;
471 s->s_op = &proc_sops;
472 s->s_time_gran = 1;
473
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800474 pde_get(&proc_root);
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800475 root_inode = proc_get_inode(s, &proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!root_inode)
477 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 root_inode->i_uid = 0;
479 root_inode->i_gid = 0;
480 s->s_root = d_alloc_root(root_inode);
481 if (!s->s_root)
482 goto out_no_root;
483 return 0;
484
485out_no_root:
486 printk("proc_read_super: get root inode failed\n");
487 iput(root_inode);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800488 pde_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -ENOMEM;
490}