blob: b9dd3628d43a233bcf2860a587a12f59b969388d [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>
16#include <linux/mount.h>
17#include <linux/smp_lock.h>
18#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
41proc_file_read(struct file *file, char __user *buf, size_t nbytes,
42 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;
72 if (dp->get_info) {
73 /* Handle old net routines */
74 n = dp->get_info(page, &start, *ppos, count);
75 if (n < count)
76 eof = 1;
77 } else if (dp->read_proc) {
78 /*
79 * How to be a proc read function
80 * ------------------------------
81 * Prototype:
82 * int f(char *buffer, char **start, off_t offset,
83 * int count, int *peof, void *dat)
84 *
85 * Assume that the buffer is "count" bytes in size.
86 *
87 * If you know you have supplied all the data you
88 * have, set *peof.
89 *
90 * You have three ways to return data:
91 * 0) Leave *start = NULL. (This is the default.)
92 * Put the data of the requested offset at that
93 * offset within the buffer. Return the number (n)
94 * of bytes there are from the beginning of the
95 * buffer up to the last byte of data. If the
96 * number of supplied bytes (= n - offset) is
97 * greater than zero and you didn't signal eof
98 * and the reader is prepared to take more data
99 * you will be called again with the requested
100 * offset advanced by the number of bytes
101 * absorbed. This interface is useful for files
102 * no larger than the buffer.
103 * 1) Set *start = an unsigned long value less than
104 * the buffer address but greater than zero.
105 * Put the data of the requested offset at the
106 * beginning of the buffer. Return the number of
107 * bytes of data placed there. If this number is
108 * greater than zero and you didn't signal eof
109 * and the reader is prepared to take more data
110 * you will be called again with the requested
111 * offset advanced by *start. This interface is
112 * useful when you have a large file consisting
113 * of a series of blocks which you want to count
114 * and return as wholes.
115 * (Hack by Paul.Russell@rustcorp.com.au)
116 * 2) Set *start = an address within the buffer.
117 * Put the data of the requested offset at *start.
118 * Return the number of bytes of data placed there.
119 * If this number is greater than zero and you
120 * didn't signal eof and the reader is prepared to
121 * take more data you will be called again with the
122 * requested offset advanced by the number of bytes
123 * absorbed.
124 */
125 n = dp->read_proc(page, &start, *ppos,
126 count, &eof, dp->data);
127 } else
128 break;
129
130 if (n == 0) /* end of file */
131 break;
132 if (n < 0) { /* error */
133 if (retval == 0)
134 retval = n;
135 break;
136 }
137
138 if (start == NULL) {
139 if (n > PAGE_SIZE) {
140 printk(KERN_ERR
141 "proc_file_read: Apparent buffer overflow!\n");
142 n = PAGE_SIZE;
143 }
144 n -= *ppos;
145 if (n <= 0)
146 break;
147 if (n > count)
148 n = count;
149 start = page + *ppos;
150 } else if (start < page) {
151 if (n > PAGE_SIZE) {
152 printk(KERN_ERR
153 "proc_file_read: Apparent buffer overflow!\n");
154 n = PAGE_SIZE;
155 }
156 if (n > count) {
157 /*
158 * Don't reduce n because doing so might
159 * cut off part of a data block.
160 */
161 printk(KERN_WARNING
162 "proc_file_read: Read count exceeded\n");
163 }
164 } else /* start >= page */ {
165 unsigned long startoff = (unsigned long)(start - page);
166 if (n > (PAGE_SIZE - startoff)) {
167 printk(KERN_ERR
168 "proc_file_read: Apparent buffer overflow!\n");
169 n = PAGE_SIZE - startoff;
170 }
171 if (n > count)
172 n = count;
173 }
174
175 n -= copy_to_user(buf, start < page ? page : start, n);
176 if (n == 0) {
177 if (retval == 0)
178 retval = -EFAULT;
179 break;
180 }
181
182 *ppos += start < page ? (unsigned long)start : n;
183 nbytes -= n;
184 buf += n;
185 retval += n;
186 }
187 free_page((unsigned long) page);
188 return retval;
189}
190
191static ssize_t
192proc_file_write(struct file *file, const char __user *buffer,
193 size_t count, loff_t *ppos)
194{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800195 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct proc_dir_entry * dp;
197
198 dp = PDE(inode);
199
200 if (!dp->write_proc)
201 return -EIO;
202
203 /* FIXME: does this routine need ppos? probably... */
204 return dp->write_proc(file, buffer, count, dp->data);
205}
206
207
208static loff_t
209proc_file_lseek(struct file *file, loff_t offset, int orig)
210{
Linus Torvalds8b90db02005-12-30 08:39:10 -0800211 loff_t retval = -EINVAL;
212 switch (orig) {
213 case 1:
214 offset += file->f_pos;
215 /* fallthrough */
216 case 0:
217 if (offset < 0 || offset > MAX_NON_LFS)
218 break;
219 file->f_pos = retval = offset;
220 }
221 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Alexey Dobriyan76df0c22008-02-08 04:18:27 -0800224static const struct file_operations proc_file_operations = {
225 .llseek = proc_file_lseek,
226 .read = proc_file_read,
227 .write = proc_file_write,
228};
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
231{
232 struct inode *inode = dentry->d_inode;
233 struct proc_dir_entry *de = PDE(inode);
234 int error;
235
236 error = inode_change_ok(inode, iattr);
237 if (error)
238 goto out;
239
240 error = inode_setattr(inode, iattr);
241 if (error)
242 goto out;
243
244 de->uid = inode->i_uid;
245 de->gid = inode->i_gid;
246 de->mode = inode->i_mode;
247out:
248 return error;
249}
250
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700251static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
252 struct kstat *stat)
253{
254 struct inode *inode = dentry->d_inode;
255 struct proc_dir_entry *de = PROC_I(inode)->pde;
256 if (de && de->nlink)
257 inode->i_nlink = de->nlink;
258
259 generic_fillattr(inode, stat);
260 return 0;
261}
262
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800263static const struct inode_operations proc_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .setattr = proc_notify_change,
265};
266
267/*
268 * This function parses a name such as "tty/driver/serial", and
269 * returns the struct proc_dir_entry for "/proc/tty/driver", and
270 * returns "serial" in residual.
271 */
272static int xlate_proc_name(const char *name,
273 struct proc_dir_entry **ret, const char **residual)
274{
275 const char *cp = name, *next;
276 struct proc_dir_entry *de;
277 int len;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800278 int rtn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800280 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 de = &proc_root;
282 while (1) {
283 next = strchr(cp, '/');
284 if (!next)
285 break;
286
287 len = next - cp;
288 for (de = de->subdir; de ; de = de->next) {
289 if (proc_match(len, cp, de))
290 break;
291 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800292 if (!de) {
293 rtn = -ENOENT;
294 goto out;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 cp += len + 1;
297 }
298 *residual = cp;
299 *ret = de;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800300out:
301 spin_unlock(&proc_subdir_lock);
302 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static DEFINE_IDR(proc_inum_idr);
306static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
307
308#define PROC_DYNAMIC_FIRST 0xF0000000UL
309
310/*
311 * Return an inode number between PROC_DYNAMIC_FIRST and
312 * 0xffffffff, or zero on failure.
313 */
314static unsigned int get_inode_number(void)
315{
316 int i, inum = 0;
317 int error;
318
319retry:
320 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
321 return 0;
322
323 spin_lock(&proc_inum_lock);
324 error = idr_get_new(&proc_inum_idr, NULL, &i);
325 spin_unlock(&proc_inum_lock);
326 if (error == -EAGAIN)
327 goto retry;
328 else if (error)
329 return 0;
330
331 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
332
333 /* inum will never be more than 0xf0ffffff, so no check
334 * for overflow.
335 */
336
337 return inum;
338}
339
340static void release_inode_number(unsigned int inum)
341{
342 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
343
344 spin_lock(&proc_inum_lock);
345 idr_remove(&proc_inum_idr, id);
346 spin_unlock(&proc_inum_lock);
347}
348
Al Viro008b1502005-08-20 00:17:39 +0100349static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 nd_set_link(nd, PDE(dentry->d_inode)->data);
Al Viro008b1502005-08-20 00:17:39 +0100352 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800355static const struct inode_operations proc_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .readlink = generic_readlink,
357 .follow_link = proc_follow_link,
358};
359
360/*
361 * As some entries in /proc are volatile, we want to
362 * get rid of unused dentries. This could be made
363 * smarter: we could keep a "volatile" flag in the
364 * inode to indicate which ones to keep.
365 */
366static int proc_delete_dentry(struct dentry * dentry)
367{
368 return 1;
369}
370
371static struct dentry_operations proc_dentry_operations =
372{
373 .d_delete = proc_delete_dentry,
374};
375
376/*
377 * Don't create negative dentries here, return -ENOENT by hand
378 * instead.
379 */
380struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
381{
382 struct inode *inode = NULL;
383 struct proc_dir_entry * de;
384 int error = -ENOENT;
385
386 lock_kernel();
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800387 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 de = PDE(dir);
389 if (de) {
390 for (de = de->subdir; de ; de = de->next) {
391 if (de->namelen != dentry->d_name.len)
392 continue;
393 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
Eric W. Biederman2b1e3002007-12-02 00:33:17 +1100394 unsigned int ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Eric W. Biederman2b1e3002007-12-02 00:33:17 +1100396 if (de->shadow_proc)
397 de = de->shadow_proc(current, de);
398 ino = de->low_ino;
Alexey Dobriyan76956502007-05-08 00:25:45 -0700399 de_get(de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800400 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 error = -EINVAL;
402 inode = proc_get_inode(dir->i_sb, ino, de);
Alexey Dobriyan4237e0d2008-02-08 04:18:27 -0800403 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
406 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800407 spin_unlock(&proc_subdir_lock);
Alexey Dobriyan4237e0d2008-02-08 04:18:27 -0800408out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unlock_kernel();
410
411 if (inode) {
412 dentry->d_op = &proc_dentry_operations;
413 d_add(dentry, inode);
414 return NULL;
415 }
Alexey Dobriyan76956502007-05-08 00:25:45 -0700416 de_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ERR_PTR(error);
418}
419
420/*
421 * This returns non-zero if at EOF, so that the /proc
422 * root directory can use this and check if it should
423 * continue with the <pid> entries..
424 *
425 * Note that the VFS-layer doesn't care about the return
426 * value of the readdir() call, as long as it's non-negative
427 * for success..
428 */
429int proc_readdir(struct file * filp,
430 void * dirent, filldir_t filldir)
431{
432 struct proc_dir_entry * de;
433 unsigned int ino;
434 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800435 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int ret = 0;
437
438 lock_kernel();
439
440 ino = inode->i_ino;
441 de = PDE(inode);
442 if (!de) {
443 ret = -EINVAL;
444 goto out;
445 }
446 i = filp->f_pos;
447 switch (i) {
448 case 0:
449 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
450 goto out;
451 i++;
452 filp->f_pos++;
453 /* fall through */
454 case 1:
455 if (filldir(dirent, "..", 2, i,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800456 parent_ino(filp->f_path.dentry),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 DT_DIR) < 0)
458 goto out;
459 i++;
460 filp->f_pos++;
461 /* fall through */
462 default:
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800463 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 de = de->subdir;
465 i -= 2;
466 for (;;) {
467 if (!de) {
468 ret = 1;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800469 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out;
471 }
472 if (!i)
473 break;
474 de = de->next;
475 i--;
476 }
477
478 do {
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700479 struct proc_dir_entry *next;
480
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800481 /* filldir passes info to user space */
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700482 de_get(de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800483 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700485 de->low_ino, de->mode >> 12) < 0) {
486 de_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700488 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800489 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 filp->f_pos++;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700491 next = de->next;
492 de_put(de);
493 de = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } while (de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800495 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 ret = 1;
498out: unlock_kernel();
499 return ret;
500}
501
502/*
503 * These are the generic /proc directory operations. They
504 * use the in-memory "struct proc_dir_entry" tree to parse
505 * the /proc directory.
506 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800507static const struct file_operations proc_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .read = generic_read_dir,
509 .readdir = proc_readdir,
510};
511
512/*
513 * proc directories can do almost nothing..
514 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800515static const struct inode_operations proc_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .lookup = proc_lookup,
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700517 .getattr = proc_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 .setattr = proc_notify_change,
519};
520
521static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
522{
523 unsigned int i;
Zhang Rui94413d82008-02-08 04:18:29 -0800524 struct proc_dir_entry *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 i = get_inode_number();
527 if (i == 0)
528 return -EAGAIN;
529 dp->low_ino = i;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (S_ISDIR(dp->mode)) {
532 if (dp->proc_iops == NULL) {
533 dp->proc_fops = &proc_dir_operations;
534 dp->proc_iops = &proc_dir_inode_operations;
535 }
536 dir->nlink++;
537 } else if (S_ISLNK(dp->mode)) {
538 if (dp->proc_iops == NULL)
539 dp->proc_iops = &proc_link_inode_operations;
540 } else if (S_ISREG(dp->mode)) {
541 if (dp->proc_fops == NULL)
542 dp->proc_fops = &proc_file_operations;
543 if (dp->proc_iops == NULL)
544 dp->proc_iops = &proc_file_inode_operations;
545 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700546
547 spin_lock(&proc_subdir_lock);
Zhang Rui94413d82008-02-08 04:18:29 -0800548
549 for (tmp = dir->subdir; tmp; tmp = tmp->next)
550 if (strcmp(tmp->name, dp->name) == 0) {
551 printk(KERN_WARNING "proc_dir_entry '%s' already "
552 "registered\n", dp->name);
553 dump_stack();
554 break;
555 }
556
Changli Gao99fc06d2007-07-15 23:40:09 -0700557 dp->next = dir->subdir;
558 dp->parent = dir;
559 dir->subdir = dp;
560 spin_unlock(&proc_subdir_lock);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
566 const char *name,
567 mode_t mode,
568 nlink_t nlink)
569{
570 struct proc_dir_entry *ent = NULL;
571 const char *fn = name;
572 int len;
573
574 /* make sure name is valid */
575 if (!name || !strlen(name)) goto out;
576
577 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
578 goto out;
579
580 /* At this point there must not be any '/' characters beyond *fn */
581 if (strchr(fn, '/'))
582 goto out;
583
584 len = strlen(fn);
585
586 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
587 if (!ent) goto out;
588
589 memset(ent, 0, sizeof(struct proc_dir_entry));
590 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
591 ent->name = ((char *) ent) + sizeof(*ent);
592 ent->namelen = len;
593 ent->mode = mode;
594 ent->nlink = nlink;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800595 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700596 ent->pde_users = 0;
597 spin_lock_init(&ent->pde_unload_lock);
598 ent->pde_unload_completion = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 out:
600 return ent;
601}
602
603struct proc_dir_entry *proc_symlink(const char *name,
604 struct proc_dir_entry *parent, const char *dest)
605{
606 struct proc_dir_entry *ent;
607
608 ent = proc_create(&parent,name,
609 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
610
611 if (ent) {
612 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
613 if (ent->data) {
614 strcpy((char*)ent->data,dest);
615 if (proc_register(parent, ent) < 0) {
616 kfree(ent->data);
617 kfree(ent);
618 ent = NULL;
619 }
620 } else {
621 kfree(ent);
622 ent = NULL;
623 }
624 }
625 return ent;
626}
627
628struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
629 struct proc_dir_entry *parent)
630{
631 struct proc_dir_entry *ent;
632
633 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
634 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (proc_register(parent, ent) < 0) {
636 kfree(ent);
637 ent = NULL;
638 }
639 }
640 return ent;
641}
642
643struct proc_dir_entry *proc_mkdir(const char *name,
644 struct proc_dir_entry *parent)
645{
646 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
647}
648
649struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
650 struct proc_dir_entry *parent)
651{
652 struct proc_dir_entry *ent;
653 nlink_t nlink;
654
655 if (S_ISDIR(mode)) {
656 if ((mode & S_IALLUGO) == 0)
657 mode |= S_IRUGO | S_IXUGO;
658 nlink = 2;
659 } else {
660 if ((mode & S_IFMT) == 0)
661 mode |= S_IFREG;
662 if ((mode & S_IALLUGO) == 0)
663 mode |= S_IRUGO;
664 nlink = 1;
665 }
666
667 ent = proc_create(&parent,name,mode,nlink);
668 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (proc_register(parent, ent) < 0) {
670 kfree(ent);
671 ent = NULL;
672 }
673 }
674 return ent;
675}
676
677void free_proc_entry(struct proc_dir_entry *de)
678{
679 unsigned int ino = de->low_ino;
680
681 if (ino < PROC_DYNAMIC_FIRST)
682 return;
683
684 release_inode_number(ino);
685
Alexey Dobriyanfd2cbe42008-02-08 04:18:28 -0800686 if (S_ISLNK(de->mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 kfree(de->data);
688 kfree(de);
689}
690
691/*
692 * Remove a /proc entry and free it if it's not currently in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
695{
696 struct proc_dir_entry **p;
697 struct proc_dir_entry *de;
698 const char *fn = name;
699 int len;
700
701 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
702 goto out;
703 len = strlen(fn);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800704
705 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 for (p = &parent->subdir; *p; p=&(*p)->next ) {
707 if (!proc_match(len, fn, *p))
708 continue;
709 de = *p;
710 *p = de->next;
711 de->next = NULL;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700712
713 spin_lock(&de->pde_unload_lock);
714 /*
715 * Stop accepting new callers into module. If you're
716 * dynamically allocating ->proc_fops, save a pointer somewhere.
717 */
718 de->proc_fops = NULL;
719 /* Wait until all existing callers into module are done. */
720 if (de->pde_users > 0) {
721 DECLARE_COMPLETION_ONSTACK(c);
722
723 if (!de->pde_unload_completion)
724 de->pde_unload_completion = &c;
725
726 spin_unlock(&de->pde_unload_lock);
727 spin_unlock(&proc_subdir_lock);
728
729 wait_for_completion(de->pde_unload_completion);
730
731 spin_lock(&proc_subdir_lock);
732 goto continue_removing;
733 }
734 spin_unlock(&de->pde_unload_lock);
735
736continue_removing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (S_ISDIR(de->mode))
738 parent->nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 de->nlink = 0;
740 WARN_ON(de->subdir);
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800741 if (atomic_dec_and_test(&de->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 free_proc_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800745 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746out:
747 return;
748}