blob: b85e36e153bad4053f4af7a3a1a1294813865542 [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;
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
187proc_file_write(struct file *file, const char __user *buffer,
188 size_t count, loff_t *ppos)
189{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800190 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct proc_dir_entry * dp;
192
193 dp = PDE(inode);
194
195 if (!dp->write_proc)
196 return -EIO;
197
198 /* FIXME: does this routine need ppos? probably... */
199 return dp->write_proc(file, buffer, count, dp->data);
200}
201
202
203static loff_t
204proc_file_lseek(struct file *file, loff_t offset, int orig)
205{
Linus Torvalds8b90db02005-12-30 08:39:10 -0800206 loff_t retval = -EINVAL;
207 switch (orig) {
208 case 1:
209 offset += file->f_pos;
210 /* fallthrough */
211 case 0:
212 if (offset < 0 || offset > MAX_NON_LFS)
213 break;
214 file->f_pos = retval = offset;
215 }
216 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Alexey Dobriyan76df0c22008-02-08 04:18:27 -0800219static const struct file_operations proc_file_operations = {
220 .llseek = proc_file_lseek,
221 .read = proc_file_read,
222 .write = proc_file_write,
223};
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
226{
227 struct inode *inode = dentry->d_inode;
228 struct proc_dir_entry *de = PDE(inode);
229 int error;
230
231 error = inode_change_ok(inode, iattr);
232 if (error)
233 goto out;
234
235 error = inode_setattr(inode, iattr);
236 if (error)
237 goto out;
238
239 de->uid = inode->i_uid;
240 de->gid = inode->i_gid;
241 de->mode = inode->i_mode;
242out:
243 return error;
244}
245
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700246static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
247 struct kstat *stat)
248{
249 struct inode *inode = dentry->d_inode;
250 struct proc_dir_entry *de = PROC_I(inode)->pde;
251 if (de && de->nlink)
252 inode->i_nlink = de->nlink;
253
254 generic_fillattr(inode, stat);
255 return 0;
256}
257
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800258static const struct inode_operations proc_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .setattr = proc_notify_change,
260};
261
262/*
263 * This function parses a name such as "tty/driver/serial", and
264 * returns the struct proc_dir_entry for "/proc/tty/driver", and
265 * returns "serial" in residual.
266 */
267static int xlate_proc_name(const char *name,
268 struct proc_dir_entry **ret, const char **residual)
269{
270 const char *cp = name, *next;
271 struct proc_dir_entry *de;
272 int len;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800273 int rtn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700275 de = *ret;
276 if (!de)
277 de = &proc_root;
278
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800279 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 while (1) {
281 next = strchr(cp, '/');
282 if (!next)
283 break;
284
285 len = next - cp;
286 for (de = de->subdir; de ; de = de->next) {
287 if (proc_match(len, cp, de))
288 break;
289 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800290 if (!de) {
291 rtn = -ENOENT;
292 goto out;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 cp += len + 1;
295 }
296 *residual = cp;
297 *ret = de;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800298out:
299 spin_unlock(&proc_subdir_lock);
300 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static DEFINE_IDR(proc_inum_idr);
304static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
305
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400306#define PROC_DYNAMIC_FIRST 0xF0000000U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/*
309 * Return an inode number between PROC_DYNAMIC_FIRST and
310 * 0xffffffff, or zero on failure.
311 */
312static unsigned int get_inode_number(void)
313{
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400314 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int error;
316
317retry:
318 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
319 return 0;
320
321 spin_lock(&proc_inum_lock);
322 error = idr_get_new(&proc_inum_idr, NULL, &i);
323 spin_unlock(&proc_inum_lock);
324 if (error == -EAGAIN)
325 goto retry;
326 else if (error)
327 return 0;
328
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400329 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
330 spin_lock(&proc_inum_lock);
331 idr_remove(&proc_inum_idr, i);
332 spin_unlock(&proc_inum_lock);
333 }
334 return PROC_DYNAMIC_FIRST + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static void release_inode_number(unsigned int inum)
338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_lock(&proc_inum_lock);
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400340 idr_remove(&proc_inum_idr, inum - PROC_DYNAMIC_FIRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock(&proc_inum_lock);
342}
343
Al Viro008b1502005-08-20 00:17:39 +0100344static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 nd_set_link(nd, PDE(dentry->d_inode)->data);
Al Viro008b1502005-08-20 00:17:39 +0100347 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800350static const struct inode_operations proc_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .readlink = generic_readlink,
352 .follow_link = proc_follow_link,
353};
354
355/*
356 * As some entries in /proc are volatile, we want to
357 * get rid of unused dentries. This could be made
358 * smarter: we could keep a "volatile" flag in the
359 * inode to indicate which ones to keep.
360 */
361static int proc_delete_dentry(struct dentry * dentry)
362{
363 return 1;
364}
365
366static struct dentry_operations proc_dentry_operations =
367{
368 .d_delete = proc_delete_dentry,
369};
370
371/*
372 * Don't create negative dentries here, return -ENOENT by hand
373 * instead.
374 */
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800375struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
376 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int error = -ENOENT;
380
381 lock_kernel();
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800382 spin_lock(&proc_subdir_lock);
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700383 for (de = de->subdir; de ; de = de->next) {
384 if (de->namelen != dentry->d_name.len)
385 continue;
386 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
387 unsigned int ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700389 ino = de->low_ino;
390 de_get(de);
391 spin_unlock(&proc_subdir_lock);
392 error = -EINVAL;
393 inode = proc_get_inode(dir->i_sb, ino, de);
394 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800397 spin_unlock(&proc_subdir_lock);
Alexey Dobriyan4237e0d2008-02-08 04:18:27 -0800398out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unlock_kernel();
400
401 if (inode) {
402 dentry->d_op = &proc_dentry_operations;
403 d_add(dentry, inode);
404 return NULL;
405 }
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700406 if (de)
407 de_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ERR_PTR(error);
409}
410
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800411struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
412 struct nameidata *nd)
413{
414 return proc_lookup_de(PDE(dir), dir, dentry);
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
418 * This returns non-zero if at EOF, so that the /proc
419 * root directory can use this and check if it should
420 * continue with the <pid> entries..
421 *
422 * Note that the VFS-layer doesn't care about the return
423 * value of the readdir() call, as long as it's non-negative
424 * for success..
425 */
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800426int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
427 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned int ino;
430 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800431 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int ret = 0;
433
434 lock_kernel();
435
436 ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 i = filp->f_pos;
438 switch (i) {
439 case 0:
440 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
441 goto out;
442 i++;
443 filp->f_pos++;
444 /* fall through */
445 case 1:
446 if (filldir(dirent, "..", 2, i,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800447 parent_ino(filp->f_path.dentry),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 DT_DIR) < 0)
449 goto out;
450 i++;
451 filp->f_pos++;
452 /* fall through */
453 default:
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800454 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 de = de->subdir;
456 i -= 2;
457 for (;;) {
458 if (!de) {
459 ret = 1;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800460 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto out;
462 }
463 if (!i)
464 break;
465 de = de->next;
466 i--;
467 }
468
469 do {
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700470 struct proc_dir_entry *next;
471
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800472 /* filldir passes info to user space */
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700473 de_get(de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800474 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700476 de->low_ino, de->mode >> 12) < 0) {
477 de_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700479 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800480 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 filp->f_pos++;
Darrick J. Wong59cd0cb2007-05-08 00:25:47 -0700482 next = de->next;
483 de_put(de);
484 de = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } while (de);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800486 spin_unlock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 ret = 1;
489out: unlock_kernel();
490 return ret;
491}
492
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800493int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
494{
495 struct inode *inode = filp->f_path.dentry->d_inode;
496
497 return proc_readdir_de(PDE(inode), filp, dirent, filldir);
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * These are the generic /proc directory operations. They
502 * use the in-memory "struct proc_dir_entry" tree to parse
503 * the /proc directory.
504 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800505static const struct file_operations proc_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 .read = generic_read_dir,
507 .readdir = proc_readdir,
508};
509
510/*
511 * proc directories can do almost nothing..
512 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800513static const struct inode_operations proc_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .lookup = proc_lookup,
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700515 .getattr = proc_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .setattr = proc_notify_change,
517};
518
519static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
520{
521 unsigned int i;
Zhang Rui94413d82008-02-08 04:18:29 -0800522 struct proc_dir_entry *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 i = get_inode_number();
525 if (i == 0)
526 return -EAGAIN;
527 dp->low_ino = i;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (S_ISDIR(dp->mode)) {
530 if (dp->proc_iops == NULL) {
531 dp->proc_fops = &proc_dir_operations;
532 dp->proc_iops = &proc_dir_inode_operations;
533 }
534 dir->nlink++;
535 } else if (S_ISLNK(dp->mode)) {
536 if (dp->proc_iops == NULL)
537 dp->proc_iops = &proc_link_inode_operations;
538 } else if (S_ISREG(dp->mode)) {
539 if (dp->proc_fops == NULL)
540 dp->proc_fops = &proc_file_operations;
541 if (dp->proc_iops == NULL)
542 dp->proc_iops = &proc_file_inode_operations;
543 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700544
545 spin_lock(&proc_subdir_lock);
Zhang Rui94413d82008-02-08 04:18:29 -0800546
547 for (tmp = dir->subdir; tmp; tmp = tmp->next)
548 if (strcmp(tmp->name, dp->name) == 0) {
549 printk(KERN_WARNING "proc_dir_entry '%s' already "
550 "registered\n", dp->name);
551 dump_stack();
552 break;
553 }
554
Changli Gao99fc06d2007-07-15 23:40:09 -0700555 dp->next = dir->subdir;
556 dp->parent = dir;
557 dir->subdir = dp;
558 spin_unlock(&proc_subdir_lock);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561}
562
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800563static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 const char *name,
565 mode_t mode,
566 nlink_t nlink)
567{
568 struct proc_dir_entry *ent = NULL;
569 const char *fn = name;
570 int len;
571
572 /* make sure name is valid */
573 if (!name || !strlen(name)) goto out;
574
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700575 if (xlate_proc_name(name, parent, &fn) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto out;
577
578 /* At this point there must not be any '/' characters beyond *fn */
579 if (strchr(fn, '/'))
580 goto out;
581
582 len = strlen(fn);
583
584 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
585 if (!ent) goto out;
586
587 memset(ent, 0, sizeof(struct proc_dir_entry));
588 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
589 ent->name = ((char *) ent) + sizeof(*ent);
590 ent->namelen = len;
591 ent->mode = mode;
592 ent->nlink = nlink;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800593 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700594 ent->pde_users = 0;
595 spin_lock_init(&ent->pde_unload_lock);
596 ent->pde_unload_completion = NULL;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700597 INIT_LIST_HEAD(&ent->pde_openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 out:
599 return ent;
600}
601
602struct proc_dir_entry *proc_symlink(const char *name,
603 struct proc_dir_entry *parent, const char *dest)
604{
605 struct proc_dir_entry *ent;
606
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800607 ent = __proc_create(&parent, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
609
610 if (ent) {
611 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
612 if (ent->data) {
613 strcpy((char*)ent->data,dest);
614 if (proc_register(parent, ent) < 0) {
615 kfree(ent->data);
616 kfree(ent);
617 ent = NULL;
618 }
619 } else {
620 kfree(ent);
621 ent = NULL;
622 }
623 }
624 return ent;
625}
626
627struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
628 struct proc_dir_entry *parent)
629{
630 struct proc_dir_entry *ent;
631
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800632 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (proc_register(parent, ent) < 0) {
635 kfree(ent);
636 ent = NULL;
637 }
638 }
639 return ent;
640}
641
Denis V. Lunev78e92b92008-05-02 04:12:41 -0700642struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
643 struct proc_dir_entry *parent)
644{
645 struct proc_dir_entry *ent;
646
647 ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
648 if (ent) {
649 ent->data = net;
650 if (proc_register(parent, ent) < 0) {
651 kfree(ent);
652 ent = NULL;
653 }
654 }
655 return ent;
656}
657EXPORT_SYMBOL_GPL(proc_net_mkdir);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659struct proc_dir_entry *proc_mkdir(const char *name,
660 struct proc_dir_entry *parent)
661{
662 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
663}
664
665struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
666 struct proc_dir_entry *parent)
667{
668 struct proc_dir_entry *ent;
669 nlink_t nlink;
670
671 if (S_ISDIR(mode)) {
672 if ((mode & S_IALLUGO) == 0)
673 mode |= S_IRUGO | S_IXUGO;
674 nlink = 2;
675 } else {
676 if ((mode & S_IFMT) == 0)
677 mode |= S_IFREG;
678 if ((mode & S_IALLUGO) == 0)
679 mode |= S_IRUGO;
680 nlink = 1;
681 }
682
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800683 ent = __proc_create(&parent, name, mode, nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (proc_register(parent, ent) < 0) {
686 kfree(ent);
687 ent = NULL;
688 }
689 }
690 return ent;
691}
692
Denis V. Lunev59b74352008-04-29 01:02:00 -0700693struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
694 struct proc_dir_entry *parent,
695 const struct file_operations *proc_fops,
696 void *data)
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800697{
698 struct proc_dir_entry *pde;
699 nlink_t nlink;
700
701 if (S_ISDIR(mode)) {
702 if ((mode & S_IALLUGO) == 0)
703 mode |= S_IRUGO | S_IXUGO;
704 nlink = 2;
705 } else {
706 if ((mode & S_IFMT) == 0)
707 mode |= S_IFREG;
708 if ((mode & S_IALLUGO) == 0)
709 mode |= S_IRUGO;
710 nlink = 1;
711 }
712
713 pde = __proc_create(&parent, name, mode, nlink);
714 if (!pde)
715 goto out;
716 pde->proc_fops = proc_fops;
Denis V. Lunev59b74352008-04-29 01:02:00 -0700717 pde->data = data;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800718 if (proc_register(parent, pde) < 0)
719 goto out_free;
720 return pde;
721out_free:
722 kfree(pde);
723out:
724 return NULL;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727void free_proc_entry(struct proc_dir_entry *de)
728{
729 unsigned int ino = de->low_ino;
730
731 if (ino < PROC_DYNAMIC_FIRST)
732 return;
733
734 release_inode_number(ino);
735
Alexey Dobriyanfd2cbe42008-02-08 04:18:28 -0800736 if (S_ISLNK(de->mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 kfree(de->data);
738 kfree(de);
739}
740
741/*
742 * Remove a /proc entry and free it if it's not currently in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
745{
746 struct proc_dir_entry **p;
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700747 struct proc_dir_entry *de = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 const char *fn = name;
749 int len;
750
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700751 if (xlate_proc_name(name, &parent, &fn) != 0)
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 len = strlen(fn);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800754
755 spin_lock(&proc_subdir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 for (p = &parent->subdir; *p; p=&(*p)->next ) {
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700757 if (proc_match(len, fn, *p)) {
758 de = *p;
759 *p = de->next;
760 de->next = NULL;
761 break;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800764 spin_unlock(&proc_subdir_lock);
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700765 if (!de)
766 return;
767
768 spin_lock(&de->pde_unload_lock);
769 /*
770 * Stop accepting new callers into module. If you're
771 * dynamically allocating ->proc_fops, save a pointer somewhere.
772 */
773 de->proc_fops = NULL;
774 /* Wait until all existing callers into module are done. */
775 if (de->pde_users > 0) {
776 DECLARE_COMPLETION_ONSTACK(c);
777
778 if (!de->pde_unload_completion)
779 de->pde_unload_completion = &c;
780
781 spin_unlock(&de->pde_unload_lock);
782
783 wait_for_completion(de->pde_unload_completion);
784
785 goto continue_removing;
786 }
787 spin_unlock(&de->pde_unload_lock);
788
789continue_removing:
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700790 spin_lock(&de->pde_unload_lock);
791 while (!list_empty(&de->pde_openers)) {
792 struct pde_opener *pdeo;
793
794 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
795 list_del(&pdeo->lh);
796 spin_unlock(&de->pde_unload_lock);
797 pdeo->release(pdeo->inode, pdeo->file);
798 kfree(pdeo);
799 spin_lock(&de->pde_unload_lock);
800 }
801 spin_unlock(&de->pde_unload_lock);
802
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700803 if (S_ISDIR(de->mode))
804 parent->nlink--;
805 de->nlink = 0;
Arjan van de Ven267e2a92008-07-25 19:45:41 -0700806 WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700807 "'%s/%s', leaking at least '%s'\n", __func__,
808 de->parent->name, de->name, de->subdir->name);
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700809 if (atomic_dec_and_test(&de->count))
810 free_proc_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}