blob: 2791907744edffdc25719cebe93f15b0d0e23242 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * proc/fs/generic.c --- generic routines for the proc-fs
3 *
4 * This file contains generic proc-fs routines for handling
5 * directories and files.
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
9 */
10
11#include <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/idr.h>
20#include <linux/namei.h>
21#include <linux/bitops.h>
Steven Rostedt64a07bd2006-03-26 01:36:55 -080022#include <linux/spinlock.h>
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070023#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25
Adrian Bunkfee781e2006-01-08 01:04:16 -080026#include "internal.h"
27
Steven Rostedt64a07bd2006-03-26 01:36:55 -080028DEFINE_SPINLOCK(proc_subdir_lock);
29
Eric W. Biederman77b14db2007-02-14 00:34:12 -080030static int proc_match(int len, const char *name, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 if (de->namelen != len)
33 return 0;
34 return !memcmp(name, de->name, len);
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* buffer size is one page but our output routines use some slack for overruns */
38#define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
39
40static ssize_t
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +030041__proc_file_read(struct file *file, char __user *buf, size_t nbytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 loff_t *ppos)
43{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -080044 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 char *page;
46 ssize_t retval=0;
47 int eof=0;
48 ssize_t n, count;
49 char *start;
50 struct proc_dir_entry * dp;
Linus Torvalds8b90db02005-12-30 08:39:10 -080051 unsigned long long pos;
52
53 /*
54 * Gaah, please just use "seq_file" instead. The legacy /proc
55 * interfaces cut loff_t down to off_t for reads, and ignore
56 * the offset entirely for writes..
57 */
58 pos = *ppos;
59 if (pos > MAX_NON_LFS)
60 return 0;
61 if (nbytes > MAX_NON_LFS - pos)
62 nbytes = MAX_NON_LFS - pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 dp = PDE(inode);
Mel Gormane12ba742007-10-16 01:25:52 -070065 if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -ENOMEM;
67
68 while ((nbytes > 0) && !eof) {
69 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
70
71 start = NULL;
Alexey Dobriyan8731f142008-04-29 01:01:58 -070072 if (dp->read_proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /*
74 * How to be a proc read function
75 * ------------------------------
76 * Prototype:
77 * int f(char *buffer, char **start, off_t offset,
78 * int count, int *peof, void *dat)
79 *
80 * Assume that the buffer is "count" bytes in size.
81 *
82 * If you know you have supplied all the data you
83 * have, set *peof.
84 *
85 * You have three ways to return data:
86 * 0) Leave *start = NULL. (This is the default.)
87 * Put the data of the requested offset at that
88 * offset within the buffer. Return the number (n)
89 * of bytes there are from the beginning of the
90 * buffer up to the last byte of data. If the
91 * number of supplied bytes (= n - offset) is
92 * greater than zero and you didn't signal eof
93 * and the reader is prepared to take more data
94 * you will be called again with the requested
95 * offset advanced by the number of bytes
96 * absorbed. This interface is useful for files
97 * no larger than the buffer.
98 * 1) Set *start = an unsigned long value less than
99 * the buffer address but greater than zero.
100 * Put the data of the requested offset at the
101 * beginning of the buffer. Return the number of
102 * bytes of data placed there. If this number is
103 * greater than zero and you didn't signal eof
104 * and the reader is prepared to take more data
105 * you will be called again with the requested
106 * offset advanced by *start. This interface is
107 * useful when you have a large file consisting
108 * of a series of blocks which you want to count
109 * and return as wholes.
110 * (Hack by Paul.Russell@rustcorp.com.au)
111 * 2) Set *start = an address within the buffer.
112 * Put the data of the requested offset at *start.
113 * Return the number of bytes of data placed there.
114 * If this number is greater than zero and you
115 * didn't signal eof and the reader is prepared to
116 * take more data you will be called again with the
117 * requested offset advanced by the number of bytes
118 * absorbed.
119 */
120 n = dp->read_proc(page, &start, *ppos,
121 count, &eof, dp->data);
122 } else
123 break;
124
125 if (n == 0) /* end of file */
126 break;
127 if (n < 0) { /* error */
128 if (retval == 0)
129 retval = n;
130 break;
131 }
132
133 if (start == NULL) {
134 if (n > PAGE_SIZE) {
135 printk(KERN_ERR
136 "proc_file_read: Apparent buffer overflow!\n");
137 n = PAGE_SIZE;
138 }
139 n -= *ppos;
140 if (n <= 0)
141 break;
142 if (n > count)
143 n = count;
144 start = page + *ppos;
145 } else if (start < page) {
146 if (n > PAGE_SIZE) {
147 printk(KERN_ERR
148 "proc_file_read: Apparent buffer overflow!\n");
149 n = PAGE_SIZE;
150 }
151 if (n > count) {
152 /*
153 * Don't reduce n because doing so might
154 * cut off part of a data block.
155 */
156 printk(KERN_WARNING
157 "proc_file_read: Read count exceeded\n");
158 }
159 } else /* start >= page */ {
160 unsigned long startoff = (unsigned long)(start - page);
161 if (n > (PAGE_SIZE - startoff)) {
162 printk(KERN_ERR
163 "proc_file_read: Apparent buffer overflow!\n");
164 n = PAGE_SIZE - startoff;
165 }
166 if (n > count)
167 n = count;
168 }
169
170 n -= copy_to_user(buf, start < page ? page : start, n);
171 if (n == 0) {
172 if (retval == 0)
173 retval = -EFAULT;
174 break;
175 }
176
177 *ppos += start < page ? (unsigned long)start : n;
178 nbytes -= n;
179 buf += n;
180 retval += n;
181 }
182 free_page((unsigned long) page);
183 return retval;
184}
185
186static ssize_t
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300187proc_file_read(struct file *file, char __user *buf, size_t nbytes,
188 loff_t *ppos)
189{
190 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
191 ssize_t rv = -EIO;
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 spin_unlock(&pde->pde_unload_lock);
200
201 rv = __proc_file_read(file, buf, nbytes, ppos);
202
203 pde_users_dec(pde);
204 return rv;
205}
206
207static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208proc_file_write(struct file *file, const char __user *buffer,
209 size_t count, loff_t *ppos)
210{
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300211 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
212 ssize_t rv = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300214 if (pde->write_proc) {
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 spin_unlock(&pde->pde_unload_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Alexey Dobriyan3dec7f52009-02-20 17:04:33 +0300223 /* FIXME: does this routine need ppos? probably... */
224 rv = pde->write_proc(file, buffer, count, pde->data);
225 pde_users_dec(pde);
226 }
227 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230
231static loff_t
232proc_file_lseek(struct file *file, loff_t offset, int orig)
233{
Linus Torvalds8b90db02005-12-30 08:39:10 -0800234 loff_t retval = -EINVAL;
235 switch (orig) {
236 case 1:
237 offset += file->f_pos;
238 /* fallthrough */
239 case 0:
240 if (offset < 0 || offset > MAX_NON_LFS)
241 break;
242 file->f_pos = retval = offset;
243 }
244 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Alexey Dobriyan76df0c22008-02-08 04:18:27 -0800247static const struct file_operations proc_file_operations = {
248 .llseek = proc_file_lseek,
249 .read = proc_file_read,
250 .write = proc_file_write,
251};
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
254{
255 struct inode *inode = dentry->d_inode;
256 struct proc_dir_entry *de = PDE(inode);
257 int error;
258
259 error = inode_change_ok(inode, iattr);
260 if (error)
261 goto out;
262
263 error = inode_setattr(inode, iattr);
264 if (error)
265 goto out;
266
267 de->uid = inode->i_uid;
268 de->gid = inode->i_gid;
269 de->mode = inode->i_mode;
270out:
271 return error;
272}
273
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700274static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
275 struct kstat *stat)
276{
277 struct inode *inode = dentry->d_inode;
278 struct proc_dir_entry *de = PROC_I(inode)->pde;
279 if (de && de->nlink)
280 inode->i_nlink = de->nlink;
281
282 generic_fillattr(inode, stat);
283 return 0;
284}
285
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800286static const struct inode_operations proc_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .setattr = proc_notify_change,
288};
289
290/*
291 * This function parses a name such as "tty/driver/serial", and
292 * returns the struct proc_dir_entry for "/proc/tty/driver", and
293 * returns "serial" in residual.
294 */
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800295static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
296 const char **residual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 const char *cp = name, *next;
299 struct proc_dir_entry *de;
300 int len;
301
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700302 de = *ret;
303 if (!de)
304 de = &proc_root;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 while (1) {
307 next = strchr(cp, '/');
308 if (!next)
309 break;
310
311 len = next - cp;
312 for (de = de->subdir; de ; de = de->next) {
313 if (proc_match(len, cp, de))
314 break;
315 }
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800316 if (!de) {
317 WARN(1, "name '%s'\n", name);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800318 return -ENOENT;
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 cp += len + 1;
321 }
322 *residual = cp;
323 *ret = de;
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800324 return 0;
325}
326
327static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
328 const char **residual)
329{
330 int rv;
331
332 spin_lock(&proc_subdir_lock);
333 rv = __xlate_proc_name(name, ret, residual);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800334 spin_unlock(&proc_subdir_lock);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800335 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400338static DEFINE_IDA(proc_inum_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
340
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400341#define PROC_DYNAMIC_FIRST 0xF0000000U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343/*
344 * Return an inode number between PROC_DYNAMIC_FIRST and
345 * 0xffffffff, or zero on failure.
346 */
347static unsigned int get_inode_number(void)
348{
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400349 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int error;
351
352retry:
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400353 if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355
356 spin_lock(&proc_inum_lock);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400357 error = ida_get_new(&proc_inum_ida, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_unlock(&proc_inum_lock);
359 if (error == -EAGAIN)
360 goto retry;
361 else if (error)
362 return 0;
363
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400364 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
365 spin_lock(&proc_inum_lock);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400366 ida_remove(&proc_inum_ida, i);
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400367 spin_unlock(&proc_inum_lock);
Alexey Dobriyancc996092008-08-02 07:30:48 +0400368 return 0;
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400369 }
370 return PROC_DYNAMIC_FIRST + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void release_inode_number(unsigned int inum)
374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_lock(&proc_inum_lock);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400376 ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_unlock(&proc_inum_lock);
378}
379
Al Viro008b1502005-08-20 00:17:39 +0100380static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 nd_set_link(nd, PDE(dentry->d_inode)->data);
Al Viro008b1502005-08-20 00:17:39 +0100383 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800386static const struct inode_operations proc_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .readlink = generic_readlink,
388 .follow_link = proc_follow_link,
389};
390
391/*
392 * As some entries in /proc are volatile, we want to
393 * get rid of unused dentries. This could be made
394 * smarter: we could keep a "volatile" flag in the
395 * inode to indicate which ones to keep.
396 */
397static int proc_delete_dentry(struct dentry * dentry)
398{
399 return 1;
400}
401
Al Virod72f71e2009-02-20 05:58:47 +0000402static const struct dentry_operations proc_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 .d_delete = proc_delete_dentry,
405};
406
407/*
408 * Don't create negative dentries here, return -ENOENT by hand
409 * instead.
410 */
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800411struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
412 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int error = -ENOENT;
416
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800417 spin_lock(&proc_subdir_lock);
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700418 for (de = de->subdir; de ; de = de->next) {
419 if (de->namelen != dentry->d_name.len)
420 continue;
421 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
422 unsigned int ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700424 ino = de->low_ino;
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800425 pde_get(de);
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700426 spin_unlock(&proc_subdir_lock);
427 error = -EINVAL;
428 inode = proc_get_inode(dir->i_sb, ino, de);
429 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800432 spin_unlock(&proc_subdir_lock);
Alexey Dobriyan4237e0d2008-02-08 04:18:27 -0800433out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (inode) {
436 dentry->d_op = &proc_dentry_operations;
437 d_add(dentry, inode);
438 return NULL;
439 }
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700440 if (de)
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800441 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ERR_PTR(error);
443}
444
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800445struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
446 struct nameidata *nd)
447{
448 return proc_lookup_de(PDE(dir), dir, dentry);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
452 * This returns non-zero if at EOF, so that the /proc
453 * root directory can use this and check if it should
454 * continue with the <pid> entries..
455 *
456 * Note that the VFS-layer doesn't care about the return
457 * value of the readdir() call, as long as it's non-negative
458 * for success..
459 */
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800460int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
461 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned int ino;
464 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800465 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int ret = 0;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 i = filp->f_pos;
470 switch (i) {
471 case 0:
472 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
473 goto out;
474 i++;
475 filp->f_pos++;
476 /* fall through */
477 case 1:
478 if (filldir(dirent, "..", 2, i,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800479 parent_ino(filp->f_path.dentry),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 DT_DIR) < 0)
481 goto out;
482 i++;
483 filp->f_pos++;
484 /* fall through */
485 default:
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800486 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 de = de->subdir;
488 i -= 2;
489 for (;;) {
490 if (!de) {
491 ret = 1;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800492 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto out;
494 }
495 if (!i)
496 break;
497 de = de->next;
498 i--;
499 }
500
501 do {
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700502 struct proc_dir_entry *next;
503
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800504 /* filldir passes info to user space */
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800505 pde_get(de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800506 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700508 de->low_ino, de->mode >> 12) < 0) {
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800509 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto out;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700511 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800512 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 filp->f_pos++;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700514 next = de->next;
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800515 pde_put(de);
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700516 de = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 } while (de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800518 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 ret = 1;
Alexey Dobriyanb4df2b92008-10-27 22:48:36 +0300521out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return ret;
523}
524
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800525int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
526{
527 struct inode *inode = filp->f_path.dentry->d_inode;
528
529 return proc_readdir_de(PDE(inode), filp, dirent, filldir);
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
533 * These are the generic /proc directory operations. They
534 * use the in-memory "struct proc_dir_entry" tree to parse
535 * the /proc directory.
536 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800537static const struct file_operations proc_dir_operations = {
Alexey Dobriyanb4df2b92008-10-27 22:48:36 +0300538 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .read = generic_read_dir,
540 .readdir = proc_readdir,
541};
542
543/*
544 * proc directories can do almost nothing..
545 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800546static const struct inode_operations proc_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .lookup = proc_lookup,
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700548 .getattr = proc_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .setattr = proc_notify_change,
550};
551
552static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
553{
554 unsigned int i;
Zhang Rui94413d82008-02-08 04:18:29 -0800555 struct proc_dir_entry *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 i = get_inode_number();
558 if (i == 0)
559 return -EAGAIN;
560 dp->low_ino = i;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (S_ISDIR(dp->mode)) {
563 if (dp->proc_iops == NULL) {
564 dp->proc_fops = &proc_dir_operations;
565 dp->proc_iops = &proc_dir_inode_operations;
566 }
567 dir->nlink++;
568 } else if (S_ISLNK(dp->mode)) {
569 if (dp->proc_iops == NULL)
570 dp->proc_iops = &proc_link_inode_operations;
571 } else if (S_ISREG(dp->mode)) {
572 if (dp->proc_fops == NULL)
573 dp->proc_fops = &proc_file_operations;
574 if (dp->proc_iops == NULL)
575 dp->proc_iops = &proc_file_inode_operations;
576 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700577
578 spin_lock(&proc_subdir_lock);
Zhang Rui94413d82008-02-08 04:18:29 -0800579
580 for (tmp = dir->subdir; tmp; tmp = tmp->next)
581 if (strcmp(tmp->name, dp->name) == 0) {
Arjan van de Ven6c2f91e2008-09-13 19:51:30 -0700582 WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n",
Alexey Dobriyan665020c2008-09-13 02:33:06 -0700583 dir->name, dp->name);
Zhang Rui94413d82008-02-08 04:18:29 -0800584 break;
585 }
586
Changli Gao99fc06d2007-07-15 23:40:09 -0700587 dp->next = dir->subdir;
588 dp->parent = dir;
589 dir->subdir = dp;
590 spin_unlock(&proc_subdir_lock);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800595static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 const char *name,
597 mode_t mode,
598 nlink_t nlink)
599{
600 struct proc_dir_entry *ent = NULL;
601 const char *fn = name;
602 int len;
603
604 /* make sure name is valid */
605 if (!name || !strlen(name)) goto out;
606
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700607 if (xlate_proc_name(name, parent, &fn) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto out;
609
610 /* At this point there must not be any '/' characters beyond *fn */
611 if (strchr(fn, '/'))
612 goto out;
613
614 len = strlen(fn);
615
616 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
617 if (!ent) goto out;
618
619 memset(ent, 0, sizeof(struct proc_dir_entry));
620 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
621 ent->name = ((char *) ent) + sizeof(*ent);
622 ent->namelen = len;
623 ent->mode = mode;
624 ent->nlink = nlink;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800625 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700626 ent->pde_users = 0;
627 spin_lock_init(&ent->pde_unload_lock);
628 ent->pde_unload_completion = NULL;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700629 INIT_LIST_HEAD(&ent->pde_openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 out:
631 return ent;
632}
633
634struct proc_dir_entry *proc_symlink(const char *name,
635 struct proc_dir_entry *parent, const char *dest)
636{
637 struct proc_dir_entry *ent;
638
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800639 ent = __proc_create(&parent, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
641
642 if (ent) {
643 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
644 if (ent->data) {
645 strcpy((char*)ent->data,dest);
646 if (proc_register(parent, ent) < 0) {
647 kfree(ent->data);
648 kfree(ent);
649 ent = NULL;
650 }
651 } else {
652 kfree(ent);
653 ent = NULL;
654 }
655 }
656 return ent;
657}
Helight.Xu587d4a12009-12-30 13:24:41 +0800658EXPORT_SYMBOL(proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
661 struct proc_dir_entry *parent)
662{
663 struct proc_dir_entry *ent;
664
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800665 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (proc_register(parent, ent) < 0) {
668 kfree(ent);
669 ent = NULL;
670 }
671 }
672 return ent;
673}
674
Denis V. Lunev78e92b92008-05-02 04:12:41 -0700675struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
676 struct proc_dir_entry *parent)
677{
678 struct proc_dir_entry *ent;
679
680 ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
681 if (ent) {
682 ent->data = net;
683 if (proc_register(parent, ent) < 0) {
684 kfree(ent);
685 ent = NULL;
686 }
687 }
688 return ent;
689}
690EXPORT_SYMBOL_GPL(proc_net_mkdir);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692struct proc_dir_entry *proc_mkdir(const char *name,
693 struct proc_dir_entry *parent)
694{
695 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
696}
Helight.Xu587d4a12009-12-30 13:24:41 +0800697EXPORT_SYMBOL(proc_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
700 struct proc_dir_entry *parent)
701{
702 struct proc_dir_entry *ent;
703 nlink_t nlink;
704
705 if (S_ISDIR(mode)) {
706 if ((mode & S_IALLUGO) == 0)
707 mode |= S_IRUGO | S_IXUGO;
708 nlink = 2;
709 } else {
710 if ((mode & S_IFMT) == 0)
711 mode |= S_IFREG;
712 if ((mode & S_IALLUGO) == 0)
713 mode |= S_IRUGO;
714 nlink = 1;
715 }
716
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800717 ent = __proc_create(&parent, name, mode, nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (proc_register(parent, ent) < 0) {
720 kfree(ent);
721 ent = NULL;
722 }
723 }
724 return ent;
725}
Helight.Xu587d4a12009-12-30 13:24:41 +0800726EXPORT_SYMBOL(create_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Denis V. Lunev59b74352008-04-29 01:02:00 -0700728struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
729 struct proc_dir_entry *parent,
730 const struct file_operations *proc_fops,
731 void *data)
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800732{
733 struct proc_dir_entry *pde;
734 nlink_t nlink;
735
736 if (S_ISDIR(mode)) {
737 if ((mode & S_IALLUGO) == 0)
738 mode |= S_IRUGO | S_IXUGO;
739 nlink = 2;
740 } else {
741 if ((mode & S_IFMT) == 0)
742 mode |= S_IFREG;
743 if ((mode & S_IALLUGO) == 0)
744 mode |= S_IRUGO;
745 nlink = 1;
746 }
747
748 pde = __proc_create(&parent, name, mode, nlink);
749 if (!pde)
750 goto out;
751 pde->proc_fops = proc_fops;
Denis V. Lunev59b74352008-04-29 01:02:00 -0700752 pde->data = data;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800753 if (proc_register(parent, pde) < 0)
754 goto out_free;
755 return pde;
756out_free:
757 kfree(pde);
758out:
759 return NULL;
760}
Helight.Xu587d4a12009-12-30 13:24:41 +0800761EXPORT_SYMBOL(proc_create_data);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800762
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800763static void free_proc_entry(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 unsigned int ino = de->low_ino;
766
767 if (ino < PROC_DYNAMIC_FIRST)
768 return;
769
770 release_inode_number(ino);
771
Alexey Dobriyanfd2cbe42008-02-08 04:18:28 -0800772 if (S_ISLNK(de->mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 kfree(de->data);
774 kfree(de);
775}
776
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800777void pde_put(struct proc_dir_entry *pde)
778{
779 if (atomic_dec_and_test(&pde->count))
780 free_proc_entry(pde);
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * Remove a /proc entry and free it if it's not currently in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
786void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
787{
788 struct proc_dir_entry **p;
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700789 struct proc_dir_entry *de = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 const char *fn = name;
791 int len;
792
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800793 spin_lock(&proc_subdir_lock);
794 if (__xlate_proc_name(name, &parent, &fn) != 0) {
795 spin_unlock(&proc_subdir_lock);
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700796 return;
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 len = strlen(fn);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 for (p = &parent->subdir; *p; p=&(*p)->next ) {
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700801 if (proc_match(len, fn, *p)) {
802 de = *p;
803 *p = de->next;
804 de->next = NULL;
805 break;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800808 spin_unlock(&proc_subdir_lock);
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800809 if (!de) {
810 WARN(1, "name '%s'\n", name);
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700811 return;
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800812 }
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700813
814 spin_lock(&de->pde_unload_lock);
815 /*
816 * Stop accepting new callers into module. If you're
817 * dynamically allocating ->proc_fops, save a pointer somewhere.
818 */
819 de->proc_fops = NULL;
820 /* Wait until all existing callers into module are done. */
821 if (de->pde_users > 0) {
822 DECLARE_COMPLETION_ONSTACK(c);
823
824 if (!de->pde_unload_completion)
825 de->pde_unload_completion = &c;
826
827 spin_unlock(&de->pde_unload_lock);
828
829 wait_for_completion(de->pde_unload_completion);
830
831 goto continue_removing;
832 }
833 spin_unlock(&de->pde_unload_lock);
834
835continue_removing:
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700836 spin_lock(&de->pde_unload_lock);
837 while (!list_empty(&de->pde_openers)) {
838 struct pde_opener *pdeo;
839
840 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
841 list_del(&pdeo->lh);
842 spin_unlock(&de->pde_unload_lock);
843 pdeo->release(pdeo->inode, pdeo->file);
844 kfree(pdeo);
845 spin_lock(&de->pde_unload_lock);
846 }
847 spin_unlock(&de->pde_unload_lock);
848
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700849 if (S_ISDIR(de->mode))
850 parent->nlink--;
851 de->nlink = 0;
Arjan van de Ven267e2a92008-07-25 19:45:41 -0700852 WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700853 "'%s/%s', leaking at least '%s'\n", __func__,
854 de->parent->name, de->name, de->subdir->name);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800855 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Helight.Xu587d4a12009-12-30 13:24:41 +0800857EXPORT_SYMBOL(remove_proc_entry);